Converting bytes to an escaped hexadecimal string I wrote this function to convert an array of bytes to a C/C++ string representation using hexadecimal escape codes (\xhh). Any suggestions are welcome:

std::string toEscapedHexaString(const std::uint8_t * data, const int dataSizeBytes,
                                const int maxCols = 80, const int padding = 0)
{
    assert(data != nullptr);
    assert((maxCols % 4) == 0);
    assert((padding % 2) == 0);

    int column = 0;
    char hexaStr[64] = {'\0'};

    std::string result = "\"";
    for (int i = 0; i < dataSizeBytes; ++i, ++data)
    {
        std::snprintf(hexaStr, sizeof(hexaStr), "\\x%02X", static_cast<unsigned>(*data));
        result += hexaStr;
        column += 4;

        if (column >= maxCols)
        {
            if (i != (dataSizeBytes - 1)) // If not the last iteration
            {
                result += "\"\n\"";
            }
            column = 0;
        }
    }

    // Add zero padding at the end to ensure the data size
    // is evenly divisible by the given padding value.
    if (padding > 0)
    {
        for (int i = dataSizeBytes; (i % padding) != 0; ++i)
        {
            result += "\\x00";
            column += 4;

            if (column >= maxCols)
            {
                if ((i + 1) % padding) // If not the last iteration
                {
                    result += "\"\n\"";
                }
                column = 0;
            }
        }
    }

    result += "\"";
    return result;
}

You can control the number of columns in the output, so for example, converting Hello World!\0 with 20 columns max (not counting the quotes):

const unsigned char str[] = "Hello World!";
auto result = toEscapedHexaString(str, sizeof(str), 20);

/* result =
   "\x48\x65\x6C\x6C\x6F"
   "\x20\x57\x6F\x72\x6C"
   "\x64\x21\x00"
*/

Use of assert

Some of the uses of assert here look semi-broken, at least to me. Part of the definition of assert is that when you compile with NDEBUG defined, assert becomes a nop. When it is enabled, a failed assertion immediately aborts the program. For testing inputs to a function, neither of those is generally desirable. For such purposes, I usually define an assure that always does its thing, and throws an exception when the test fails.

snprintf

At the very least, I'd try to wrap the ugliness of using snprintf up into a reasonably neat little function like to_hex that just converts a number into its hex representation:

std::string to_hex(int in) {
    // snprintf ugliness here
    return std::string(buffer);
}

Alternatively, since you're converting inputs to strings, and accumulating those together into a buffer anyway, consider using an std::ostringstream.

Logic

In a few places, the logic seems...less than pleasing, at least to me. For example:

if (column >= maxCols)

You've previously tested that maxCols % 4 == 0, and (assuming things work correctly) each item you add should be 4 more characters, so you should never see a result that's actually greater than maxCols--only less than maxCols, then equal to maxCols, then back to 0. This is harmless, and if you might want to support a maxCols that was not a multiple of 4, then it might even make good sense--but as things stand right now, it seems like there's a degree of...uncertainty about the real intent.

Reorganization

Right now you have code to ensure the maximum line length sprinkled liberally throughout the rest of the code. I'd be tempted to define a type that takes that as its single responsibility: when you create it, specify a maximum length. When you add characters to it, it tracks the length of the current "line", and when the maximum is reached (or exceeded) it adds a delimiter, and starts over. With this, the rest of the code gets a lot simpler in a hurry, and each piece of code has a much simpler, clearer purpose.


Wrapping fetch(), preserve promise-based API for outer function One of my react component's methods is using an async fetch() internally. After fetching the response from the server, it is being converted to JSON, then I perform a check on that JSON string. Finally, I return the result wrapped in a new promise. Here is the code:

fetchCalendarData(offset, length) {
    return fetch(this.props.Uri + '?offset=' + offset + '&length=' + length)
    .then(response => response.json())
    .then((result) => {
        if (result.hasOwnProperty('redirect_to_login') && result.redirect_to_login == true) {
            window.location.reload(window.globalAppState.base_url + "/login");
        }
        return Promise.resolve(result);
    })
    .catch(() => {
        return Promise.reject('FAILURE');
    });
}

When I call the method, I do it like this:

this.fetchCalendarData(0, 6).then((fetched_calendar_data) => {
    this.calendar_data = fetched_calendar_data;
});

The code works, but I am not familiar with promise based flow at all, and those 3 return statements look a little weird to me. I also read about the promise constructor antipattern but am not sure if that applies here. And, is it necessary to return the result wrapped in a new promise, at all?

but am not sure if that applies here. And, is it necessary to return
  the result wrapped in a new promise, at all?

Yes, that applies here.

No, returning Promise.resolve() is not necessary. .then() returns a new Promise object. Use

return result

within the function passed to .then().

Note, a value returned from .catch() results in a resolved Promise. Depending on how errors are handled an error could be thrown (withing {} of a function).

this.fetchCalendarData(0, 6).then((fetched_calendar_data) => {
    this.calendar_data = fetched_calendar_data;
});

should include the second parameter to .then() to handle potential error

.then((fetched_calendar_data)=>{}, err=>{/* handle error */})

given then Promise.reject() is returned from .catch(), or .catch() should be chained to .then()

.then((fetched_calendar_data)=>{}
.catch(err=>{/* handle error */})

fetchCalendarData(offset, length) {
  return fetch(this.props.Uri + '?offset=' + offset + '&length=' + length)
  .then(response => response.json())
  .then(result => {
    if (result.hasOwnProperty('redirect_to_login')
       && result.redirect_to_login == true) {
         window.location.reload(window.globalAppState.base_url + "/login");
    }
    return result;
  })
  .catch(() => {
    throw new Error('FAILURE');
  });
}

this.fetchCalendarData(0, 6)
.then(fetched_calendar_data => {
  this.calendar_data = fetched_calendar_data;
})
.catch(err => console.error(err));


Custom function don't read vectors I have this custom function to show the combinations:

x<-function(a,b){

  ncol(combn(a,b))

}

combinations<-numeric()

for(i in 0:12){
    combinations<-c(combinations,x(12,i))
}

combinations

It works, but I want to know if it's possible to do something simpler, like this:

x<-function(a,b){

  ncol(combn(a,b))

}

x(12,0:12)

The number of combinations of size k from a set of size n can be computed with choose(n, k) in R, and if you have multiple k values you can just pass in a vector for k. Therefore your code can be simplified to:

choose(12, 0:12)
#  [1]   1  12  66 220 495 792 924 792 495 220  66  12   1


Mocking utility class for database testing I have these classes that have a boilerplate way of initialization. So, instead of writing it each time, I wrote a class called MockUtil.java, and I put the boilerplate mocks in there.

MockUtil has static variables and methods which can be called to do the mocking for the test classes. So, when writing a new test class setup, MockUtil's setup, boilerplate expectations, replays, and verifications of mock are being called from the Test class.

I feel like this reduces a lot of boilerplate burden on the test classes. But, I still feel like the code structure/code pattern can be improved. I am not sure if it has to be a util class or something else, because two tests can't run simultaneously because of static variables and methods and I feel like it reduces the scope of enhancements, if any, that has to be done. Could you give me suggestions on what I can improve in my code?

MockUtil.java

public class MockUtil {

    public static JdbcTransactionImpl transaction;
    public static Connection connection;
    public static PreparedStatement statement;
    public static ResultSet resultSet;

    public static Connection getConnection() {
        return connection;
    }

    public static ResultSet getResultSet() {
        return resultSet;
    }

    public static PreparedStatement getStatement() {
        return statement;
    }

    public static void createMocks() {
                //more boilerplate mocks to create services to be used in each Test -- which are ignored here.
                //boilerplate tx mocks
        transaction = createMockBuilder(JdbcTransactionImpl.class)
                .addMockedMethod("getConnection").createMock();

        connection = createMock(Connection.class);
        statement = createMock(PreparedStatement.class);
        resultSet = createMock(ResultSet.class);
    }

    public static void setContextualExpectations(String...sqls) throws Exception {
                //boilerplate tx expectations.
        expect(context.getTransaction()).andReturn(transaction).anyTimes();
        expect(transaction.getConnection()).andReturn(connection).anyTimes();
        for(String sql: sqls) {
            expect(connection.prepareStatement(sql)).andReturn(statement);
        }
        expect(statement.executeQuery()).andReturn(resultSet).anyTimes();
    }

    public static void setReplays() throws SQLException {
        replay(transaction);
        replay(connection);
        replay(statement);
        replay(resultSet);
    }

    public static void closeResources() throws SQLException {
                 //boilerplate resource closing.
        statement.close();
        resultSet.close();
    }

    public static void verifyMocks() {
                 //boilerplate verification of mocks.
        verify(transaction);
        verify(connection);
        verify(statement);
        verify(resultSet);
    }
}

One of the several test classes:

Class1Test

private void setCustomExpectationsForTest1XXX() throws SQLException {
    PreparedStatement stmt = MockUtil.getStatement();
    ResultSet rs = MockUtil.getResultSet();
    checkExp(stmt, rs);
}

private void checkExp(PreparedStatement stmt, ResultSet rs)
        throws SQLException {
    stmt.setString(1, in.getCode());
    expect(rs.next()).andReturn(true);
    expect(rs.getString(1)).andReturn(customVal);
    MockUtil.closeResources();
}

    @Before
public void setUp() throws Exception {
    MockUtil.createMocks();
    createImportExportObj();
}

@After
public void tearDown() throws Exception {
    in = null;
    out = null;
}

@Test
public void test1XXX() throws Exception {
    MockUtil.setContextualExpectations(calcPbValForGldSqls);
    setCustomExpectationsForTest1XXX();
    MockUtil.setReplays();
    program.execute(in, out);
    BigDecimal expected = new BigDecimal("85000").setScale(4);
            BigDecimal actual = out.getAmt();
    assertEquals(expected, actual);
    MockUtil.verifyMocks();
}
}

You are right about the problem with static variables and methods possibly causing problems in a multithreaded environment. I recommend turning your MockUtil into an abstract class with no static methods and then have your test classes extend that. That way you are guaranteed to have fresh mocks for all your test classes.


Can this insertion sort be optimized? Could you provide feedback for this code? For arrays of length 2, is it more efficient not to use a sorting algorithm?

package insertion.sort;

import java.util.Arrays;

public class InsertionSort {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int[] testArr = {-10, -5, 100, 51, 6, 50};
        insertionSort(testArr);
        System.out.println(Arrays.toString(testArr));
    }

    public static void insertionSort(int[] arr) {
        for(int i = 1; i < arr.length; i++) {
            int temp = arr[i];
            int j;
            for(j = i; j > 0 && arr[j-1] > temp; j--)
                arr[j] = arr[j-1];
            arr[j] = temp;
        }
    }

}

By the way, I've noticed "quicksort" is spelt all as one word, but "insertion sort" is spelt as two. Is that right? Would anyone ever say "quicksort sort"?

As insertion sort is an O(n2) algorithm, there's not much point to optimizing it.  For any input that is large enough for you to care about the performance, you would want to pick a sorting algorithm with better bounds.  Quicksort, for example, is usually closer to O(n log n).

That said, I'll point out some style issues:

You should never omit optional braces, as you've done for the inner for-loop.  Think of it this way: anytime you omit braces, you're a contributing factor to a future coding accident.  (Apple learned this lesson the hard way; their new swift language requires braces.)
Variables can usually be named better than "temp".  Renaming temp to elementToInsert, for example, would make your code self-documenting.


Printing Command Line Unicode Chess Board Let me preface this by detailing some of my design philosophy. The intent is to view the board from the perspective of the active player. Player moves are also factored into the design. When determining move logic, all that needs to happen is comparing against '0'. For checking the whiteness or blackness of a piece, ord(c) % 265 should help by pulling the remainder and checking if it's in range(4,10) or range(10,16). Anyway, any optimizations and critique is welcome! I'm particularly interested if it involves numpy, scipy | bitwise operators.

chess.py

import numpy as np

chrs = {
    'b_checker': u'\u25FB',
    'b_pawn': u'\u265F',
    'b_rook': u'\u265C',
    'b_knight': u'\u265E',
    'b_bishop': u'\u265D',
    'b_king': u'\u265A',
    'b_queen': u'\u265B',
    'w_checker': u'\u25FC',
    'w_pawn': u'\u2659',
    'w_rook': u'\u2656',
    'w_knight': u'\u2658',
    'w_bishop': u'\u2657',
    'w_king': u'\u2654',
    'w_queen': u'\u2655'
}

def get_checkers():
    bw_row = [chrs['b_checker'], chrs['w_checker']]*4
    bw_checkers = []

    for i in range(8):
        bw_checkers.append(bw_row if i % 2 == 0 else bw_row[::-1])

    bw_checkers = np.array(bw_checkers)
    wb_checkers = bw_checkers[::-1]
    return {'W': wb_checkers, 'B': bw_checkers}

def get_board():

    def get_army(user):
        u = user.lower()
        guard = [chrs[u+'_rook'], chrs[u+'_knight'], chrs[u+'_bishop']]
        rear = guard + [chrs[u+'_king'], chrs[u+'_queen']] + guard[::-1]
        front = [chrs[u+'_pawn']]*8

        if user == 'B':
            return [rear, front]
        else:  # since white moves first
            return [front, rear]

    board = [squad for squad in get_army('B')]

    for _ in range(4):
        board.append(['0']*8)

    board += get_army('W')

    return np.array(board)

def print_board(board, checkers, user):
    chks = checkers[user]
    temp = board.copy() if user == 'W' else board.copy()[::-1]

    for i, row in enumerate(temp):
        for j, c in enumerate(row):
            print('', chks[i][j] if c == '0' else c, end='', flush=True)
        print()

if __name__ == "__main__":
    checkers = get_checkers()
    board = get_board()
    user = 'W'
    print_board(board, checkers, user)

Present Output:

White's Perspective

♜ ♞ ♝ ♚ ♛ ♝ ♞ ♜
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
◼ ◻ ◼ ◻ ◼ ◻ ◼ ◻
◻ ◼ ◻ ◼ ◻ ◼ ◻ ◼
◼ ◻ ◼ ◻ ◼ ◻ ◼ ◻
◻ ◼ ◻ ◼ ◻ ◼ ◻ ◼
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
♖ ♘ ♗ ♔ ♕ ♗ ♘ ♖

Black's Perspective

♖ ♘ ♗ ♔ ♕ ♗ ♘ ♖
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
◻ ◼ ◻ ◼ ◻ ◼ ◻ ◼
◼ ◻ ◼ ◻ ◼ ◻ ◼ ◻
◻ ◼ ◻ ◼ ◻ ◼ ◻ ◼
◼ ◻ ◼ ◻ ◼ ◻ ◼ ◻
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
♜ ♞ ♝ ♚ ♛ ♝ ♞ ♜

numpy
In this case there is no need to use. For an 8 by 8 board, filled with strings, there is no advantage to using it, apart from the possibility to index row and column at the same time
enums
You have a few properties wich would be best presented as an enum. The color and the type of the piece
import enum

class Color(enum.Enum):
    WHITE = 0
    BLACK = 1

class Piece(enum.Enum):
    EMPTY = enum.auto()
    PAWN = enum.auto()
    ROOK = enum.auto()
    KNIGHT = enum.auto()
    BISHOP = enum.auto()
    KING = enum.auto()
    QUEEN = enum.auto()

tuple
You have a chrs dictionary with keys that contain both the color and the piece. A better approach here would be to use tuples as keys
chrs = {
    (Color.WHITE, Piece.EMPTY): "\u25FB",
    (Color.WHITE, Piece.PAWN): "\u265F",
    (Color.WHITE, Piece.ROOK): "\u265C",
    (Color.WHITE, Piece.KNIGHT): "\u265E",
    (Color.WHITE, Piece.BISHOP): "\u265D",
    (Color.WHITE, Piece.KING): "\u265A",
    (Color.WHITE, Piece.QUEEN): "\u265B",
    (Color.BLACK, Piece.EMPTY): "\u25FC",
    (Color.BLACK, Piece.PAWN): "\u2659",
    (Color.BLACK, Piece.ROOK): "\u2656",
    (Color.BLACK, Piece.KNIGHT): "\u2658",
    (Color.BLACK, Piece.BISHOP): "\u2657",
    (Color.BLACK, Piece.KING): "\u2654",
    (Color.BLACK, Piece.QUEEN): "\u2655",
}

board
You keep a black and a white board. Better would be to keep one board, and just flip it at the time of presentation.
def board_begin():
    return (
        [
            [
                (Color.WHITE, Piece.ROOK),
                (Color.WHITE, Piece.KNIGHT),
                (Color.WHITE, Piece.BISHOP),
                (Color.WHITE, Piece.QUEEN),
                (Color.WHITE, Piece.KING),
                (Color.WHITE, Piece.BISHOP),
                (Color.WHITE, Piece.KNIGHT),
                (Color.WHITE, Piece.ROOK),
            ],
            [(Color.WHITE, Piece.PAWN) for _ in range(8)],
            *[[None] * 8 for _ in range(4)],
            [(Color.BLACK, Piece.PAWN) for _ in range(8)],
            [
                (Color.BLACK, Piece.ROOK),
                (Color.BLACK, Piece.KNIGHT),
                (Color.BLACK, Piece.BISHOP),
                (Color.BLACK, Piece.QUEEN),
                (Color.BLACK, Piece.KING),
                (Color.BLACK, Piece.BISHOP),
                (Color.BLACK, Piece.KNIGHT),
                (Color.BLACK, Piece.ROOK),
            ],
        ]
    )

Here I use None to represent an empty tile, and replace it by a white or black tile at the time of display.
flip board
If you use numpy to keep your board, you can np.flip, or a simple routine like this:
def flip(board):
    return [
        row[::-1] for row in reversed(board)
    ]

display the board
Here a simple routine, which takes a boolean flag on whether to flip it:
def display_board(board, flip_board=False):
    for i, row in enumerate(board if not flip_board else flip(board)):
        row_strings = [
            chrs.get(tile, chrs[(Color((i + j) % 2), Piece.EMPTY)])
            for j, tile in enumerate(row)
        ]
        print("".join(row_strings))

using dict.get to replace the empty tiles by the correct squares

board = board_begin()

[[(<Color.WHITE: 0>, <Piece.ROOK: 3>),
        (<Color.WHITE: 0>, <Piece.KNIGHT: 4>),
        (<Color.WHITE: 0>, <Piece.BISHOP: 5>),
        (<Color.WHITE: 0>, <Piece.QUEEN: 7>),
        (<Color.WHITE: 0>, <Piece.KING: 6>),
        (<Color.WHITE: 0>, <Piece.BISHOP: 5>),
        (<Color.WHITE: 0>, <Piece.KNIGHT: 4>),
        (<Color.WHITE: 0>, <Piece.ROOK: 3>)],
       [(<Color.WHITE: 0>, <Piece.PAWN: 2>),
        (<Color.WHITE: 0>, <Piece.PAWN: 2>),
        (<Color.WHITE: 0>, <Piece.PAWN: 2>),
        (<Color.WHITE: 0>, <Piece.PAWN: 2>),
        (<Color.WHITE: 0>, <Piece.PAWN: 2>),
        (<Color.WHITE: 0>, <Piece.PAWN: 2>),
        (<Color.WHITE: 0>, <Piece.PAWN: 2>),
        (<Color.WHITE: 0>, <Piece.PAWN: 2>)],
       [None, None, None, None, None, None, None, None],
       [None, None, None, None, None, None, None, None],
       [None, None, None, None, None, None, None, None],
       [None, None, None, None, None, None, None, None],
       [(<Color.BLACK: 1>, <Piece.PAWN: 2>),
        (<Color.BLACK: 1>, <Piece.PAWN: 2>),
        (<Color.BLACK: 1>, <Piece.PAWN: 2>),
        (<Color.BLACK: 1>, <Piece.PAWN: 2>),
        (<Color.BLACK: 1>, <Piece.PAWN: 2>),
        (<Color.BLACK: 1>, <Piece.PAWN: 2>),
        (<Color.BLACK: 1>, <Piece.PAWN: 2>),
        (<Color.BLACK: 1>, <Piece.PAWN: 2>)],
       [(<Color.BLACK: 1>, <Piece.ROOK: 3>),
        (<Color.BLACK: 1>, <Piece.KNIGHT: 4>),
        (<Color.BLACK: 1>, <Piece.BISHOP: 5>),
        (<Color.BLACK: 1>, <Piece.QUEEN: 7>),
        (<Color.BLACK: 1>, <Piece.KING: 6>),
        (<Color.BLACK: 1>, <Piece.BISHOP: 5>),
        (<Color.BLACK: 1>, <Piece.KNIGHT: 4>),
        (<Color.BLACK: 1>, <Piece.ROOK: 3>)]]

display_board(board, flip_board=False)

♜♞♝♛♚♝♞♜
♟♟♟♟♟♟♟♟
◻◼◻◼◻◼◻◼
◼◻◼◻◼◻◼◻
◻◼◻◼◻◼◻◼
◼◻◼◻◼◻◼◻
♙♙♙♙♙♙♙♙
♖♘♗♕♔♗♘♖

display_board(board, flip_board=True)

♖♘♗♔♕♗♘♖
♙♙♙♙♙♙♙♙
◻◼◻◼◻◼◻◼
◼◻◼◻◼◻◼◻
◻◼◻◼◻◼◻◼
◼◻◼◻◼◻◼◻
♟♟♟♟♟♟♟♟
♜♞♝♚♛♝♞♜

Board class
If you want to incorporate moves etc, it might pay to make a Class of the board, with it's own display, __getitem__ and move routines


Transferring large amounts of data in web editor I am making a web editor for fun and I was told that the way I was doing it (using PHP) would be a bad way. I also thought about it while I was making it, and in massive sums of data transfer it would be a bad idea to do it this way. I can't think of another way to do it and was looking for someone to help me improve it, by that I mean my save method I am using.

Editor:

    <title>Web Editor</title>
    <link href="controller.css" rel="stylesheet" />

File:

C:\hello.html

JS:

function save() {
  var dir = document.getElementById("file").innerHTML;
  var data = document.getElementById("editPad").innerHTML;
  window.location = "save.php?dir=" + encodeURIComponent(dir) + "&data=" + encodeURIComponent(data);
}

PHP:

<?php
    $dir = $_GET['dir'];
    $data = $_GET['data'];
    $lookFor = array("&lt;", "&gt;","
","<%2Fdiv>","
","
");
    $replaceWith = array("<", ">", "", "", "","");
    $newData = str_replace($lookFor,$replaceWith,$data);

    $f = fopen(urldecode($dir),"w");
    fwrite($f,urldecode($newData));
    fclose($f);
?>

All of it is just a work in progress and I need more done. But for right now, is there a better way for me to save the file with massive sums of data being transferred?

I'd prefer a form with a textarea rather than a contenteditable element. Forms and text areas were built for that purpose.
As for your save code, GET requests should not do anything on the server. It should only do what it was called to do, and that is to get data. I suggest you do a POST or PUT instead by using a form, or via AJAX.
An advantage of AJAX compared to forms is that you won't leave your page. Similar to how you did it, you grab the data and send it to the server.
Your editor is risky since it can write to an arbitrary file on the server (or worse, on the system). With this code, I can modify this PHP file itself and make it do all kinds of stuff. I suggest you do some research on how you can restrict where and what you can modify.
You need a more robust approach in stripping HTML from the data than doing it manually. PHP has some built-in functions to do that for you, like strip_tags. There could be better solutions that I'm not aware of as well.
Massive data? How much data do you expect you put in anyway? I suggest polishing the implementation first before optimizing for other stuff, like data size and so on.


Measuring the length of binary numbers in English words in C Inspired by this question (link to original video), I decided to write some code in C to perform this task, and it is much faster than the Python code, even when run with PyPY, up to a billion, it takes just 700 seconds.

For \$10^8\$ (a hundred million) it takes 62 seconds, while Python with pypy about 5 minutes!

Task

Given a number transform it into the length of the English one-zero representation: for example 2 is one_zero = 7. Calculate how many of these numbers end in 13 and in 18 when applying this step repeatedly. (All numbers end in one of the two.)

My code is really simple, but I am a beginner in C so I ask you in case I may have missed optimizations:

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <limits.h>

int letter_sum(int n) {
    int sum = 0;
    while (n != 0) {
        div_t x = div(n, 2);
        n = x.quot;
        sum += x.rem == 0 ? 4 : 3;
    }
    return sum;
}

int ends_in_18(int n) {
  if (n == 18) {return 1;}
  if (n == 13) {return 0;}
  return ends_in_18(letter_sum(n));
}

int main() {
    assert(letter_sum(18) == 18);

    int eighteen_count = 0;
    for (int i = 1; i < 100000000; i++) {
        if (ends_in_18(i)) {
            eighteen_count++;
        }
        else { printf("%d ", i); }
    }
    printf("%d %d\n", 100000000 < INT_MAX, eighteen_count);
}

Optimization, step by step
I ran your program and it took 38.5 seconds to run on my computer.  After playing around with it for a bit, I was able to speed it up by 100x.  I went through three different iterations to get to that point.
1) Use bit manipulation instead of division
The first thing I noticed was that your letter_sum() function was doing a loop across the bits, but it was using a division function to do so.  If you switched to using bitwise operators, it would speed up the program by a lot.  The remainder when dividing n by 2 is simply n & 1.  The quotient after dividing n by 2 is n >> 1.  In this rewrite, I have switched to using uint32_t to ensure that the shifting is an unsigned shift.
static uint32_t letter_sum(uint32_t n)
{
    uint32_t sum = 0;
    while (n != 0) {
        sum += (n & 1) ? 3 : 4;
        n >>= 1;
    }
    return sum;
}

Switching to this function made the program run in 4.4 seconds, or 8.75 times faster than the original.
2) Use more advanced bit tricks
The function can be improved even more by using more advanced bit counting techniques.  Essentially, you are adding 4 for each 0 bit and 3 for each 1 bit, up to the leading 1 bit in the number.  You can count these bits without using a loop by using two types of bit counting:

CLZ or Count Leading Zeros, gives you a count of how many zeros appear before the leading 1 bit.
POPCOUNT or Population Count, gives you a count of how many 1 bits are in the number.

Both CLZ and POPCOUNT can often be done in single machine instructions, so they are much more efficient than looping through each bit.
If you have the above two counts, then the letter sum of a 32-bit number will be:
128 - CLZ(n)*4 - POPCOUNT(n)

The reasoning for this equation is that you start with the maximum letter sum, which is 32 zeros = 128 letters.  From that, you subtract 4 for each leading zero, because leading zeros do not count towards the letter sum.  After that, you subtract 1 for each 1 bit, because "one" has one less letter than "zero".  So the function now looks like this (assuming a gcc compiler):
static inline uint32_t letter_sum(uint32_t n)
{
    return 128 - __builtin_clz(n)*4 - __builtin_popcount(n);
}

I added the inline keyword since this function is so simple it should be inlined.  After switching to this function, the program now ran in 2.4 seconds, or 16 times faster than the original.
3) Use memoization limited to a small range
The third step is to add memoization to the program.  But first, we should realize that the result of letter_sum() can only be in the range 3..127.  The lowest letter sum is for 1, with sum 3.  The highest letter sum is for 100000...0000, with sum 127.  So you can use a boolean array of size 128 to remember whether the sum ends in 18 or 13.
Here is the final rewrite using memoization.  I kept your main() function pretty much the same, although I wasn't sure why you were printing 100000000 < INT_MAX.
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <stdbool.h>

// Maximum index will be 127 because a 32-bit number with 1 "one" and 31
// "zero" has 127 letters.
#define CACHE_MAX        128

// cache[i] is true if letter_sum(i) eventually leads to 18.
static bool cache[CACHE_MAX];

// Maximum is 32 "zero" = 128
// Remove 4 letters for each leading zero, since leading zeros don't count
// Remove 1 letters for each "one", because "one" has 1 less letter than "zero"
static inline uint32_t letter_sum(uint32_t n)
{
    return 128 - __builtin_clz(n)*4 - __builtin_popcount(n);
}

static void fillCache(void)
{
    for (uint32_t i=1; i<CACHE_MAX; i++) {
        uint32_t n = i;
        while (n != 13 && n != 18) {
            n = letter_sum(n);
        }
        if (n == 18)
            cache[i] = true;
    }
}

static inline bool ends_in_18(uint32_t n)
{
    return cache[letter_sum(n)];
}

int main(void)
{
    assert(letter_sum(18) == 18);
    fillCache();

    uint32_t eighteen_count = 0;
    for (uint32_t i = 1; i < 100000000; i++) {
        if (ends_in_18(i)) {
            eighteen_count++;
        }
        else { printf("%d ", i); }
    }
    printf("%d %d\n", 100000000 < INT_MAX, eighteen_count);
}

This version ran in 0.39 seconds, or 100x faster than the original.


Creating a new REST user account I am studying JavaEE and I have made my project for the learning experience.

I want to understand how best to write code from the point of view of architecture and use of relevant technologies.  I am particularly interested to hear about the implementation of the REST-layer.

The purpose of this is the backend to the Android application.
Uses postgres, wildfly(EJB) and REST.

This is one from many methods in REST layer.

@POST
@Path("/reg")
@Produces(MediaType.APPLICATION_JSON)
public Response create(@QueryParam("email") String email, @QueryParam("pass") String pass) {
    log.info("create user");
    log.debug("create user: email=" + email + " pass=" + pass);
    Response response;
    try {
        response = Response.ok().entity(userM.reg(email, pass)).build();
        log.info("reg ok");
    } catch (Exception e) {
        AppException err = new AppException(e);
        response = Response.serverError().status(err.getCode()).entity(err).build();
        log.info("reg fail");
    }

    return response;
}

Your naming looks to be a little bit inconsistent

@Path("/reg")

userM.reg(email, pass)

First, reg seems to be short hand for something (registerUser?).  Unless there's some really compelling reason to leave it abbreviated, you should change the spelling so that the next person to read the code can figure out what you mean.

The inconsistency is that you seem to be using reg as a verb in the (again, poorly named) userM object, and also using it as a noun (@Path tells us that this is part of the URI, which would normally identify a resource).

Now, you will find those that argue, reasonably, that the characters in the URL don't make the link restful or not restful.  Nonetheless, there's some expectation that, if you are asking the server to insert an item into a collection, then the URL is a representation of the collection, rather than the act of inserting into it.

Assuming that your choice of POST is correct, meaning that clients should not expect the change being offered to be idempotent, then I would expect an endpoint like /users, or /registeredUsers (if there is more than one user collection).


Web scraping data from US postal zip codes Web scraping county, population and median home value from United States postal zip codes.

I switched from using InternetExplorer.Application to New MSXML2.XMLHTTP60. For the first time, I broke code into smaller subs or functions, among other things.

This code ran successfully 1 time, with other tests returning only 70-100 records.
UsingInternetExplorer.Application, I projected the code to complete in 1H:45M by timing 20 records. With the XML method, I projected 25-30 min as it takes about 5 min to fetch 70-100 records.
Excel goes completely blank when running (white screen).

There appears to be several things I can do:

Early binding (which I didn't understand how to implement based on the thread)
Creating VBScripts to simulate multi-threading; I've not created VBScripts with Excel so this option is taking me a little bit as I try to read and study VBScripts more in depth.
Can't seem to find the link, but elsewhere, I read that jumping around would make things slow. According to the thread, I should store all values in an array first, after all values are retrieved, I then should input them into the corresponding cells, instead of retrieving and inputting right away. (I think I can handle this but not sure if anyone has any pointers as to whether this actually works).

Variables

'ZipCodeScrape Variables
    Public ZipCodeRange As Range
    Public cell as Variant

'Web Variables
    Public IE As MSXML2.XMLHTTP60
    Public url As String
    Public post As Object
    Public HTML As MSHTML.HTMLDocument
    Public HTMLbody As MSHTML.HTMLbody

Gathering zip codes and using a function to retrieve data

Sub ZipCodeScrape()

    Set IE = New MSXML2.XMLHTTP60
    url = "https://www.unitedstateszipcodes.org/"

    Set ZipCodeRange = Range("C2", Range("C2").End(xlDown))

    Dim TargetElement(1 To 3) As String
        TargetElement(1) = "County:"
        TargetElement(2) = "Population"
        TargetElement(3) = "Median Home Value"
    Dim i As Integer

    For Each cell In ZipCodeRange
        For i = 1 To 3
            cell.Offset(0, i).Value = dataScrape("th", TargetElement(i), "td")
        Next i
    Next cell
End Sub

Here is the function I'm using to retrieve the data

Private Function dataScrape(ByVal TagName As String, Element As String, targetTagName)

    IE.Open "GET", url & cell.Value, False
    IE.send

    While IE.readyState <> 4: DoEvents: Wend

    Set HTML = New MSHTML.HTMLDocument
    Set HTMLbody = HTML.body
    HTMLbody.innerHTML = IE.responseText

        For Each post In HTMLbody.getElementsByTagName(TagName)
            If InStr(post.innerText, Element) > 0 Then
                dataScrape = post.ParentNode.getElementsByTagName(targetTagName)(0).innerText: Exit For
            End If
        Next post

End Function

"5 min to fetch 70-100 records" seems pretty slow. Even though, you are fetching the same URL 3 times per Zip Code, this still seems slow.

    For i = 1 To 3
        cell.Offset(0, i).Value = dataScrape("th", TargetElement(i), "td")
    Next i

Avoid using Global variables whenever possible.  Pass the values as parameters instead.

Turning off Application.ScreenUpdating and setting Application.Calculation = xlCalculationManual will speed up the writing the data to the Worksheet.

The code below processed 200 records in 41 seconds.

Refactored Code

Sub ZipCodeScrape()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Const BASE_URL = "https://www.unitedstateszipcodes.org/"
    Dim doc As MSHTML.HTMLDocument, cell As Range

    For Each cell In Range("C2", Range("C2").End(xlDown))
        Set doc = getDocument(BASE_URL & Format(cell.Value, "00000"))
        If Not doc Is Nothing Then
            cell.Offset(0, -2).Value = getPrimaryCity(doc)
            cell.Offset(0, -1).Value = getState(doc)
            cell.Offset(0, 1).Value = getTDByTH(doc, "County: ")
            cell.Offset(0, 2).Value = getTDByTH(doc, "Population")
            cell.Offset(0, 3).Value = getTDByTH(doc, "Median Home Value")
        End If

    Next cell
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

Private Function getTDByTH(doc As MSHTML.HTMLDocument, Heading As String) As String
    Dim post As Object
    For Each post In doc.getElementsByTagName("TH")
        If post.innerText = Heading Then
            getTDByTH = post.ParentNode.getElementsByTagName("TD")(0).innerText
            Exit For
        End If
    Next post
End Function

Private Function getPrimaryCity(doc As MSHTML.HTMLDocument) As String
    Dim PrimaryCity As String
    PrimaryCity = getTDByTH(doc, "Primary City:")
    getPrimaryCity = Split(PrimaryCity, ",")(0)
End Function

Private Function getState(doc As MSHTML.HTMLDocument) As String
    Dim PrimaryCity As String
    Dim PrimaryCityArray() As String
    PrimaryCity = getTDByTH(doc, "Primary City:")
    PrimaryCityArray = Split(PrimaryCity, ",")
    getState = Split(PrimaryCityArray(1), " ")(1)
End Function

Private Function getDocument(URL As String) As MSHTML.HTMLDocument
    Dim doc As MSHTML.HTMLDocument
    With New MSXML2.XMLHTTP60
        .Open "GET", URL, False
        .send
        If .readyState = 4 And .Status = 200 Then
            Set doc = New MSHTML.HTMLDocument
            doc.body.innerHTML = .responseText
            Set getDocument = doc
        Else
            Debug.Print "URL Not Responding:  "; URL, "Ready state: "; .readyState, "HTTP request status: "; .Status
        End If
    End With
End Function

The Multi-threaded VBA that you referred to would be considerably faster.


CMakeList to compile and link an executable using Boost I'm beginner with CMake and C++ compilation but it's seems that unlike Java I need to be very explicit with my CMakeList.txt in order to link src with correct header and libraries in my project.

Here is my CMakeList.txt

cmake_minimum_required(VERSION 3.4)
project(AwesomeScheduler)

include_directories(header)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)

find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

set(SOCKET_SOUCE_FILES
        src/socket.cpp
        header/socket.h)

set(DISPTACHER_SOURCE_FILES
        src/dispatcher.cpp
        header/dispatcher.h
        header/util.h)

set(ENDUSER_SOURCE_FILES
        src/end-user.cpp
        header/end-user.h
        header/util.h)

set(TASKPROPERTIES_SOURCE_FILES
        src/task_properties.cpp
        header/task_properties.h)

add_executable(Dispatcher ${SOCKET_SOUCE_FILES} ${DISPTACHER_SOURCE_FILES})

add_executable(EndUser ${SOCKET_SOUCE_FILES} ${ENDUSER_SOURCE_FILES} ${TASKPROPERTIES_SOURCE_FILES})
target_link_libraries(EndUser ${Boost_LIBRARIES})

The compilation works well and I get my two executables but I wonder if there is a way less verbose to get the same result?

As commented by @drescherjm and @NicolasHolthaus your example code already looks pretty good. If you are getting less verbose, you will always loose some information.

Minimal Version

To demonstrate this, let's take a look at a minimal version of your code:

cmake_minimum_required(VERSION 3.4)
project(AwesomeScheduler)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")

find_package(Boost REQUIRED COMPONENTS system filesystem)

include_directories(header)

add_executable(Dispatcher src/socket.cpp src/dispatcher.cpp)
add_executable(EndUser src/socket.cpp src/end-user.cpp src/task_properties.cpp)
target_link_libraries(EndUser Boost::system Boost::filesystem)

I have

moved the source files into the add_executable() calls
eliminated the header files
used the IMPORTED targets from Boost, which implicitly declares the header paths also
done some cosmetic changes to the global settings

used the more cross-platform CMAKE_CXX_STANDARD setting
using .. from the binary dir looks odd, I assumed something relative to the source dir

Something in Between

Because grouping the source files and adding the header files has its advantages in respect of readability, maintainability and CMake's own abilities during build environment generation, here is what I would have done in the setup you have described here:

cmake_minimum_required(VERSION 3.4)
project(AwesomeScheduler)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")

find_package(Boost REQUIRED COMPONENTS system filesystem)

set(
    SOCKET_SOUCE_FILES
        src/socket.cpp
        header/socket.h
)
set(
    ENDUSER_SOURCE_FILES
        src/end-user.cpp
        header/end-user.h
        src/task_properties.cpp
        header/task_properties.h
        header/util.h
)
set(
    DISPTACHER_SOURCE_FILES
        src/dispatcher.cpp
        header/dispatcher.h
        header/util.h
)

add_library(Socket OBJECT ${SOCKET_SOUCE_FILES})
target_include_directories(Socket PUBLIC header)

add_executable(Dispatcher ${DISPTACHER_SOURCE_FILES} $<TARGET_OBJECTS:Socket>)
target_include_directories(Dispatcher PRIVATE header)

add_executable(EndUser ${ENDUSER_SOURCE_FILES} $<TARGET_OBJECTS:Socket>)
target_include_directories(Dispatcher PRIVATE header)
target_link_libraries(EndUser Boost::system Boost::filesystem)

Now you have

the possibility to

add/remove files conditionally to your source file lists
reuse the lists in calls to e.g. set_source_files_properties() or source_group()

the headers getting checked for availability and added to IDE projects during generation
the grouping of Socket sources into a object library with e.g. it's own set of target properties to be modified
no global include directory anymore, but target specific include directories

on this small project with only one header folder it might be oversized, but if your code gets larger you should definitely think about the directory structure of your code
So it could look something like

AwesomeScheduler/
  CMakeLists.txt
  Dispatcher/
    CMakeLists.txt
    src/
      dispatcher.cpp
      dispatcher.h
  EndUser/
    CMakeLists.txt
    src/
      end-user.cpp
      end-user.h
  lib/
    Socket/
      CMakeLists.txt
      src/
        socket.cpp
      include/
        socket.h
  include/
    util.h

References

FindBoost CMake Module
https://stackoverflow.com/questions/6594796/how-do-i-make-cmake-output-into-a-bin-dir
https://stackoverflow.com/questions/10851247/how-to-activate-c-11-in-cmake
https://stackoverflow.com/questions/2924037/separate-include-and-src-folders-for-application-level-code
https://stackoverflow.com/questions/2360734/whats-a-good-directory-structure-for-larger-c-projects-using-makefile
https://stackoverflow.com/questions/36872117/various-libs-in-subfolders-how-do-i-export-them-to-the-parent


LeetCode: Top K Frequent Elements C# https://leetcode.com/explore/interview/card/top-interview-questions-medium/110/sorting-and-searching/799

  Given a non-empty array of integers, return the k most frequent
  elements.

  Example 1:

  Input: nums = [1,1,1,2,2,3], k = 2
  Output: [1,2]

  Example 2:

  Input: nums = [1], k = 1 Output: [1] Note:

  You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
  Your algorithm's time complexity must be better than O(n log n), where
  n is the array's size.

using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

//good example for bucket sort
namespace SortingQuestions
{
    /// <summary>
    /// https://leetcode.com/explore/interview/card/top-interview-questions-medium/110/sorting-and-searching/799
    /// </summary>
    [TestClass]
    public class TopKFrequentTest
    {
        [TestMethod]
        public void SimpleUseCaseTest()
        {
            int[] nums = { 1, 1, 1, 2, 2, 3 };
            int k = 2;
            int[] expected = { 1, 2 };
            CollectionAssert.AreEqual(expected, TopKFrequentClass.TopKFrequent(nums, k).ToArray());
        }
    }

    public class TopKFrequentClass
    {
        public static IList<int> TopKFrequent(int[] nums, int k)
        {
            Dictionary<int, int> number2Count = new Dictionary<int, int>();

            //we count how many times each number appears
            foreach (var num in nums)
            {
                number2Count.TryGetValue(num, out var temp);
                number2Count[num] = temp + 1;
            }

            List<int>[] bucket = new List<int>[nums.Length + 1];

            //we allocate an array in the size of the original list of numbers
            //we iterate all of the numbers and for add each number to the index in the array
            // the index represents how many times that number appeared
            //
            //    0 times -> none
            //    1 times -> number 3
            //    2 times -> number 2
            //    3 times -> number 1
            //    4 times -> none
            //    5 times -> none
            foreach (var key in number2Count.Keys)
            {
                int frequency = number2Count[key];
                if (bucket[frequency] == null)
                {
                    bucket[frequency] = new List<int>();
                }
                bucket[frequency].Add(key);
            }

            List<int> result = new List<int>();
            // we iterate the list bucket in reverse until the number of items in the result
            // list equals k, because we iterate in reserve we get the biggest numbers
            for (int pos = bucket.Length - 1; pos >= 0 && result.Count < k; pos--)
            {
                if (bucket[pos] != null)
                {
                    result.AddRange(bucket[pos]);
                }
            }

            return result;
        }
    }
}

I really like this question and it took me some time to understand how to implement the bucket sorting.

I know I can also use MaxHeap.
Since C# doesn't have a MaxHeap, do you think using a SortedList can help here somehow with the performance? (remember the question limitations)

I am mainly looking for performance optimizations.

I think that in terms of performance you'd be hard pressed to do better than a LINQ query.  Not only fast but very compact and relatively easy to understand.  It could look something like this:

public IList<int> TopKFrequent(int[] nums, int k) {
    var answer = (from int n in nums
                  group n by n into g
                  orderby g.Count() descending
                  select g.Key).Take((k)).ToList();
    return answer;
}

For those that prefer method syntax here's the equivalent:

public IList<int> TopKFrequent(int[] nums, int k) {
    var answer = nums.GroupBy(n => n).OrderByDescending(g => g.Count()).Take(k).Select(g => g.Key).ToList();
    return answer;
}


Calculating and Outputting Pascal's Triangle with only Indices I'm relatively new to Python, and decided to write this program that outputs Pascal's Triangle to test myself. About halfway through, I realized that it's a lot of work to find the value of Pascal's Triangle from an index instead of a (row, column) formula, and ended up taking a journey to StackExchange Math for an answer.

If anyone could provide some guidance on improving the code's readability or efficiency, I would be very appreciative. I'm sure that my code doesn't complete its task in the most pythonic way. Two specific examples of possibly inefficient code are the way that the code checks user inputs and the way that the triangle is displayed.

Thanks in advance for the help.

"""Prompts the user for a number of rows, then displays that many rows of Pascal's Triangle"""

import numpy
import math

# Used for simplifying FindValueFromIndex
def PlusOne(index):
    return numpy.floor((numpy.sqrt(8*index+1)-1)/2)
def MinusOne(index):
    return numpy.floor((numpy.sqrt(8*index+1)+1)/2)

# Finds the Value of a particular index of Pascal's Triangle
def FindValueFromIndex(index):
    return math.factorial(MinusOne(index))/(math.factorial(index-(MinusOne(index)*PlusOne(index))/2)
        *math.factorial(MinusOne(index)+(MinusOne(index)*PlusOne(index))/2-index))

# Determines whether a number is triangular
def IsTriangular(index):
    currtrinum = 0
    n=0
    while currtrinum < index:
        currtrinum += n
        n+=1
    if currtrinum != index:
        return False
    else:
        return True

# Variable Declaration
rows = 'a'
while not rows.isdigit():
    rows = input("Enter number of rows: ")
rows = int(rows)
count = rows
onesindex = []
nononesindex = 1

# The left-side ones values of Pascal's Triangle can be defined as a summation
# The right-side ones values of Pascal's Triangle can be found by subtracting one from the index from the left side
# The range starts at 2 because the first value is considered as part of the right side
for i in range(2,rows+1):
    onesindex.append(sum(range(i)))
    onesindex.append(sum(range(i))-1)

# The ones values can be populated from the onesindex values found in the previous section
# The non-one values can be calculated using the equation found in FindValueFromIndex()
triangle = [0 for x in range(sum(range(rows)))]
for i in range(len(triangle)):
    if i in onesindex:
        triangle[i] = 1
    else:
        while triangle[i] == 1 or triangle[i] == 0:
            triangle[i] = int(FindValueFromIndex(nononesindex))
            nononesindex += 1

# Prints out the result
rows -= 1
for i in range(rows-1):
    print('    ',end='')

for i in range(len(triangle)):
    print('{:^4}'.format(str(triangle[i])),end='    ')
    if IsTriangular(i+1):
        rows -= 1
        print('\n',end='')
        [print('    ',end='') for j in range(rows-1)]

This is SUPER bulky for what you actually need to be doing. I did this same project about a year ago in python3. You're thinking too hard about something that's a lot more simple than it looks.

while True:
  def num():
    n=input("Type however many rows you want (or 'info' for info about Pascal’s Triangle): ")
  if n=="quit":
    exit()
  if n.isnumeric():
    n = int(n)
    if n>1000:
      print("That's too many. Try again.")
      return
    else:
      a=[]
      for i in range(n):
        a.append([])
        a[i].append(1)
        for j in range(1,i):
          a[i].append(a[i-1][j-1]+a[i-1][j])
      if(n!=0):
        a[i].append(1)
        for i in range(n):
        print("   "*(n-i),end=" ",sep=" ")
        for j in range(0,i+1):
          print('{0:6}'.format(a[i][j]),end=" ",sep=" ")
        print()
  else:
    return
  num()

The indentation may be wrong (copy and paste is challenging) but it is a lot shorter and more efficient than all the checks you're doing.
Hopefully this helps!


Simplify bit flags checking code Please help make this code more cleaner. It's a part of window procedure that notifies my renderer before client area of window resized.

I need to check two combinations of bit blags: some of them must be set, and some must be not. How to do it without bunch of temp variables? (and probably calculate only 2 possible states and only once, not every call)

Thanks!

LRESULT CALLBACK MSWindow::Impl::WndProc( HWND hWnd, uint msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{

// ...

// WM_WINDOWPOSCHANGING is sent just about window position, size or order changed.
case WM_WINDOWPOSCHANGING:
    {
        const WINDOWPOS* wp = ((WINDOWPOS*)lParam);
        const auto& flags = wp->flags;

        const bool NOCOPYBITS       = (flags & SWP_NOCOPYBITS)      > 0;
        const bool NOSIZE           = (flags & SWP_NOSIZE)          > 0;
        const bool FRAMECHANGED     = (flags & SWP_FRAMECHANGED)    > 0;
        const bool NOOWNERZORDER    = (flags & SWP_NOOWNERZORDER)   > 0;
        const bool NOZORDER         = (flags & SWP_NOZORDER)        > 0;
        const bool NOACTIVATE       = (flags & SWP_NOACTIVATE)      > 0;
        const bool SHOWWINDOW       = (flags & SWP_SHOWWINDOW)      > 0;

        const bool IsResizingAboutToEnd  = !(NOSIZE) && NOOWNERZORDER && NOZORDER && (!NOACTIVATE);
        const bool IsGoingToMaximizeOrRestore = FRAMECHANGED && (!NOACTIVATE) && (!NOCOPYBITS) && (!SHOWWINDOW);

        if ( IsGoingToMaximizeOrRestore || IsResizingAboutToEnd )
        {
            Resize(Point(wp->cx, wp->cy));
        }

        break;
    }
// .....
}

You first need to select the bits in the flag you want to test by ANDing them with a mask containing all the bits.

This will zero all the bits that aren't being tested.

You then just need to check the value of the bits you want so the masks and values are:

const UINT EndMask = NOSIZE | NOOWNERZORDER | NOZORDER | NOACTIVATE;
const UINT MaxRestoreMask = FRAMECHANGED | NOACTIVATE | NOCOPYBITS | SHOWWINDOW;
const UINT EndValue = NOOWNERZORDER | NOZORDER;
const UINT MaxRestoreValue = FRAMECHANGED;

The test then becomes:

if ((flags & EndMask) == EndValue || (flags & MaxRestoreMask) == MaxRestoreValue)


Temperature and speed unit converter class in JS This is what I came up with, but I'm not sure if:

putting it in Class is the best approach
multiple repeats (this.val etc.) are acceptable
there is a better approach (maybe with constructor)

class UnitChange {
  temperature(val, to) {
    this.val = val;
    this.to = to;
    if (this.to === "toF") {
      return this.val * 1.8 + 32;
    } else {
      return (this.val - 32) / 1.8;
    }
  }
  speed(currentUnit, val, to) {
    this.currentUnit = currentUnit;
    this.val = val;
    this.to = to;
    if (this.to === "tomph") {
       if (this.currentUnit === "kph") {
       return this.val * 0.62;
      } else {
       return this.val * 2.23;
      }
    } else if (this.to === "tokph") {
      if (this.currentUnit === "mph") {
        return this.val * 1.60;
      } else {
        return this.val * 3.6;
      }
    } else if (this.to === "toms") {
    if (this.currentUnit === "kph") {
      return this.val * 0.27;
    } else {
      return this.val * 0.44;
    }
  }
 }
};

let change = new UnitChange();
console.log(change.temperature(8, "toF"));
console.log(change.speed("kph", 100, "tomph"));

A static Object
The class syntax is not very well suited to this type of object use. The object should be a static object, as you would only ever need one instance, and you should not need to instantiate it each time you need to use it.
Reduce complexity
Having to write a conversion from each type of unit means you add a lot of extra code.
Say you added a Kelvin Unit to temperatures, your code would need to convert from C => F, C => K, F => C, F=> K, K => C, and K => F. For some types of units this would quickly balloon out of control.
For conversion you always convert to a standard unit, and then convert that value to the unit you want. This reduces the complexity. I would advice you to use SI units, however what units you choice is is irrelevant.
Units have meaning
Be aware that there are limits to some units.
Temperatures have a base of -273.15C, returning a value less than that is meaningless.
The speed of light is the upper limit of speed, however conceptually speeds above this limit can still have meaning.
Why create the converter
A conversion object is by its nature an object of convenience, as the actual computations are trivial and for any computational work should be inline. So when you design such an object, consider how it is to be used. Adding common constants for example, provide aliases
You can also add functionality dynamically, see example that creates direct conversion function, eg the function Units.kmph2mph(kmph) is created dynamically
Example
The example has added a lot of extra functionality, none of it is a must outside your direct conversions, its just to give you some ideas on how these things can be done.
The snippet contains a static object 'Units' created via a self invoking function. It includes some aliases eg unit.F is same as unit.fahrenheit. some constants, and some dynamic functions.
All units are converted to a base unit type (kelvin and meters per second)
Usage as follows
Units.kmph = 100;
const mph = Units.mph;
Units.celsius = 38;
const f = Units.fahrenheit;

//or via direct conversion

const mph = Units.kmph2mph(100);
const f = Units.C2F(38);
const zeroF = Units.K2F(Units.ABSOLUTE_ZERO_K);
Units.C2K(-10000); // will return 0

Please note I have not tested this snippet and it may contain typos, it is as an example only.
const Units = (() => {
    var tempK = 0;
    var ms = 0; // meters per second
    const secondsPerHour = 60 * 60;
    const zeroC = - 273.15
    const API = {
        /* Temps */
        get fahrenheit(){ return API.celsius * 1.8 + 32},
        get celsius() {return API.kelvin + zeroC},
        get kelvin() {return tempK < 0 ? 0 : tempK },
        set fahrenheit(f) {tempK = (f - 32) / 1.8 - zeroC},
        set celsius(c) {tempK = c - zeroC},
        set kelvin(k) {tempK = k},
        /* Speeds */
        get milesPerHour() {return ms / secondsPerHour / 1600},
        get kilometersPerHour() {return ms / secondsPerHour / 1000},
        get metersPerSecond() {return ms },
        set milesPerHour(mph) {ms = mph * 1600 * secondsPerHour},
        set kilometersPerHour(kmph) {ms = kmph * 1000 * secondsPerHour},
        set metersPerSecond(ms) {ms = ms},
        /* Aliases */
        get F() {return API.fahrenheit},
        get C() {return API.celsius},
        get K() {return API.kelvin},
        set F(f) {API.fahrenheit = f},
        set C(c) {API.celsius = c },
        set K(k) {tempK = k },
        get mph() {return API.milesPerHour},
        get kmph() {return API.kilometersPerHour},
        get ms() {return ms},
        set mph(mph) {API.milesPerHour = mph},
        set kmph(kmph) {API.kilometersPerHour = kmph},
        set ms(ms) {ms = ms},
        /* Constants */
        SPEED_OF_LIGHT_MS : 299792458, // ms
        SPEED_OF_SOUND_MS : 343, // ms
        ABSOLUTE_ZERO_K : 0, // K
        MILES_PER_KILOMETER : 1 / 1.6,
        KILOMETERS_PER_MILE : 1 * 1.6,
    };

    // Create direct conversion from alias names using 2 for to. eg F2C
    [["F", "C", "K"], ["kmph", "mph", "ms"]].forEach(type => {
        for(const unitFrom of type) {  // unitFrom must be const or let
            for(const unitTo of type) { // unitTo must be const or let
                if (unitFrom !== unitTo) {
                    API[unitFrom + "2" + unitTo] = function(unit) {
                        API[unitFrom] = unit;
                        return API[unitTo];
                    }
                }
            }
        }
    });
    return Object.freeze(API); // freeze it so it can not be damaged
})();


Allow adding errors and warnings and remove both after a while with seperate timers I use this in my Blazor application, letting other services send errors or warnings to this AlertService. The service triggers an update of a component which simply displays the messages, errors in red warnings in yellow.
While getting no new warnings, the existing warnings should be deleted, every 5 seconds one less warning being displayed. Same goes for errors.
When a new warning comes in, the timer restarts (so when the service was 1 second before deleting a warning, that timer is now void and the new timer starts with 5 seconds again).
This is my class, I feel there must be a better way but I haven't figured it out yet:
    public class AlertService
{
    private readonly ObservableCollection<string> errors = new ObservableCollection<string>();
    private readonly ObservableCollection<string> warnings = new ObservableCollection<string>();
    private object? talkingStickErrors;
    private object? talkingStickWarnings;

    public ReadOnlyObservableCollection<string> Errors { get; }

    public ReadOnlyObservableCollection<string> Warnings { get; }

    public AlertService()
    {
        Errors = new ReadOnlyObservableCollection<string>(errors);
        Warnings = new ReadOnlyObservableCollection<string>(warnings);
    }

    public void Error(string message)
    {
        Console.WriteLine($"Error: {message}");
        errors.Add(message);
        StartErrorTimer();
    }

    private void StartErrorTimer()
    {
        talkingStickErrors = new object();
        RemoveErrorAfterDelay(talkingStickErrors);
    }

    private async void RemoveErrorAfterDelay(object localTalkingStick)
    {
        while (true)
        {
            await Task.Delay(5000);
            if (localTalkingStick != talkingStickErrors || !errors.Any()) return;
            errors.RemoveAt(0);
        }
    }

    public void Warning(string message)
    {
        Console.WriteLine($"Warning: {message}");
        warnings.Add(message);
        StartWarningTimer();
    }

    private void StartWarningTimer()
    {
        talkingStickWarnings = new object();
        RemoveWarningAfterDelay(talkingStickWarnings);
    }

    private async void RemoveWarningAfterDelay(object localTalkingStick)
    {
        while (true)
        {
            await Task.Delay(5000);
            if (localTalkingStick != talkingStickWarnings || !warnings.Any()) return;
            warnings.RemoveAt(0);
        }
    }
}

Also I couldn't think of a better word for the talking sticks. They're not exactly locks.
Update: No more talking sticks, it now just reused the last message it received.
Update 2: Talking sticks again, as string have no unique identity.
Update 3: Instead of an extra event, the alerts are now in ObservableCollections. I keep receiving stuff about encapsulation and Rx. ould appreciate someone helping me to DRY this.

Try this collection to hold your messages. It should do exactly what you are looking for - just create couple instances for errors and warnings.
public class MessageCollection : ObservableCollection<string>
{
    public MessageCollection()
    {
        Timer = new Timer(Callback, this, 5000, 5000);
        CollectionChanged += MessageCollection_CollectionChanged;
    }

    Timer Timer { get; }

    void MessageCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Timer.Change(5000, 5000);
    }
    static void Callback(object state)
    {
        var that = (MessageCollection)state;
        if (that.Any())
        {
            that.CollectionChanged -= that.MessageCollection_CollectionChanged;
            that.RemoveAt(that.Count - 1);
            that.CollectionChanged += that.MessageCollection_CollectionChanged;
        }
    }
}


Converting 'list of Foo' into 'lists of Foo properties' I'm looking at some code that goes a bit like this:

class Foo { public int Frob; public int Frib; }
class Bar { public Foo Left; public Foo Right; }

public void ProcessBars(Bar[] bars)
{
    this.LeftFrobs = new int[bars.Count];
    this.RightFrobs = new int[bars.Count];
    this.LeftFribs = new int[bars.Count];
    this.RightFribs = new int[bars.Count];
    //... you get the idea

    int counter = 0;
    foreach(Bar bar in bars)
    {
        LeftFrobs[counter] = bar.Left.Frob;
        LeftFribs[counter] = bar.Left.Frib;
        //...27 more lines of similar
        if(bar.Left.Frob == MAGICNUM_A) LeftFribs[counter] = MAGICNUM_B;

        RightFrobs[counter] = bar.Right.Frob;
        RightFribs[counter] = bar.Right.Frib;
        //...27 more lines of similar
        if(bar.Right.Frob == MAGICNUM_C) LeftFribs[counter] = MAGICNUM_D;

        counter++;
    }
}

Aesthetically, this bothers me, but so far, I haven't managed to come up with anything better.

That method is immense, and is surely impossible to easily read and maintain. Once you break it into smaller methods you will be able to work with it ("just like those acorns, one at a time").
This will also help avoid repetitions of code.

I propose creating a class to hold all that data, and merely instantiate a Left and a Right objects of that class. Then move the loading of data into the class as well.

Then the foreach is limited to 3 lines plus all the ifs:

public class WildGooseChaser
{
    class Foo { public int Frob; public int Frib; }
    class Bar { public Foo Left; public Foo Right; }

    private InternalData Left;
    private InternalData Right;

    class InternalData
    {
        public int[] Frobs;
        public int[] Fribs;
        // ... 27 more lines of similar.

        public InternalData(int dataLength) {
            Frobs = new int[dataLength];
            Fribs = new int[dataLength];
            //... you get the idea
        }

        public void LoadBar(int index, Bar bar) {
            Frobs[index] = bar.Left.Frob;
            Fribs[index] = bar.Left.Frib;
            //...27 more lines of similar
        }
    }

    public void ProcessBars(Bar[] bars) {
        Left = new InternalData(bars.Length);
        Right = new InternalData(bars.Length);

        int counter = 0;
        foreach (Bar bar in bars) {
            Left.LoadBar(counter, bar);
            Right.LoadBar(counter, bar);

            if (bar.Left.Frob == MAGICNUM_A) {
                Left.Fribs[counter] = MAGICNUM_B;
            }

            if (bar.Right.Frob == MAGICNUM_C) {
                Left.Fribs[counter] = MAGICNUM_D;
            }

            counter++;
        }
    }
}

If you receive an array, you can use for instead of foreach.
So you can either get rid of the counter variable, or receive an IEnumerable<Bar> instead.


Given an arbitrarily large file containing integers, return the largest N numbers, highest first I've written a top_n program in Ruby, which does pretty much exactly what the title says, as part of a coding exercise. I'm trying to learn about how to sort efficiently (i.e. deciding the best sorting algorithm and implementing them or their variations), to manage memory efficiently, and know whether I'm using the right language/constructs/ideas to tackle a sorting problem.

What I'm looking for is advice on the algorithm and the code in general. I feel it can be done MUCH, MUCH better (i.e. I'm doing splits and casting to integers... my intuition tells me something could be better).

I'd really appreciate if someone gave me some pointers on how could I improve this little program. Thanks in advance.

require 'set'

class File
  def self.tail(path, n = 10)
    result = File.open(path, 'r') do |file|
      buffer_size = 512
      line_count = 0

      file.seek(0, IO::SEEK_END)

      offset = file.pos

      while line_count <= n && offset > 0
       to_read = if (offset - buffer_size) < 0
                   offset
                 else
                   buffer_size
                 end

       file.seek(offset - to_read)

       data = file.read(to_read)

       data.reverse.each_char do |c|
         if line_count > n
           offset += 1
           break
         end

         offset -= 1

         if c == "\n"
           line_count += 1
         end
       end
      end

      file.seek(offset)
      file.read
    end

    result
  end

  def each_chunk(chunk_size)
    yield read(chunk_size) until eof?
  end
end

def top_n(filename, n = 100)
  pre_sorted_chunks = Dir[".#{filename}_sorted_chunk_*"]

  if pre_sorted_chunks.empty?
    build_pre_sorted_chunks_for(filename)
  end

  top = SortedSet.new

  # This reference takes ±0.141 seconds.
  # pre_sorted_chunks.each do |file|
  #   top << `tail -n #{n} #{file}`.strip.split.map(&:to_i)
  # end

  # This takes ±0.130 seconds. A little better.
  tasks = pre_sorted_chunks.map do |chunk_file_path|
    Thread.new(chunk_file_path) do |file_path|
      top << File.tail(file_path, n).strip.split.map(&:to_i)
    end
  end

  tasks.each(&:join)

  top.max(n)
end

# Current impl. takes ±5m. A lot of time.
def build_pre_sorted_chunks_for(filename)
  File.open(filename) do |file|
    n = 0

    # Chunks of 500MB.
    file.each_chunk(1024 ** 2 * 500) do |chunk|
      numbers = chunk.split.map(&:to_i)

      sorted_set = SortedSet.new(numbers)
      sorted_set = sorted_set.to_a.join("\n")

      File.open(".#{filename}_sorted_chunk_#{n}", 'w') do |f|
        f.write(sorted_set)
      end

      n += 1
    end
  end
end

unless ARGV.empty?
  filename = ARGV[0]
  n = ARGV[1].to_i

  puts top_n(filename, n)
else
  puts "Please provide a filename and a N value to calculate the top N."
end

I've used this method to create a file with random numbers:

def generate_random_number_file(n = 15_000_000)
  require 'set'

  randoms = Set.new

  loop do
    randoms << rand(n)
    break if randoms.size >= n
  end

  File.open('randos.txt', 'w') do |file|
    random_list = randoms.to_a.join("\n")
    file.write(random_list)
  end
end

Consider remembering the n largest numbers:

def top_n(filename, n = 100)
    require 'set'

    topn = SortedSet.new()

    File.foreach(filename) do |line|
        num = line.to_i
        if topn.length < n
            topn.add(num)
        elsif num > topn.first
            topn.delete(topn.first)
            topn.add(num)
        end
    end
    return topn.to_a
end


Idomatic way to ignore `finally` block after cancelling network request In React, it is quite common for me to have something similar to the following.

async componentDidMount() {
    try {
        this.setState({ isLoading: true })
        const { data } = await axios.get('...')
        this.setState({ data })
    } catch(error) {
         handleError(error)
    } finally {
        this.setState({ isLoading: false })
    }
}

This is great because the cleanup code (isLoading: false) is DRY. That is, until I try to cancel network requests on unmount. That code would look like this:

async componentDidMount() {
    try {
        this.axiosCancelTokenSource = axios.CancelToken.source()
        this.setState({ isLoading: true })
        const { data } = await axios.get('...', {
            cancelToken: this.axiosCancelTokenSource.token,
        })
        this.setState({ data })
    } catch(error) {
         if (axios.isCancel(error)) return
         handleError(error)
    } finally {
        this.setState({ isLoading: false })
    }
}
componentWillUnmount() {
    if (this.axiosCancelTokenSource) this.axiosCancelTokenSource.cancel()
}

The problem with this is that it will setState after the component unmounts, which React will warn againts.

As far as I see it, these are my options for dealing with this:

Ignore the warning.
React gives a warning when you setState after unmount because it indicates a memory leak (in this case, the lingering network request, if not cancelled). If the network request is cancelled, there is still a setState after unmount, but just to set a flag. There is no more lingering network request. It should be safe to ignore the warning in this case, but it doesn't feel right.
Check what error was thrown in the finally block and add the same if statement as the catch block. This seems incredibly hacky and would require extra code to save the error from the catch block.
Check if the component is mounted in the finally block. This is also hacky and requires boilerplate code to update a this.isMounted flag.
Put the cleanup code at the end of try and after the condition in catch. This is not DRY. Humans are also very forgetful; I cannot count how many times I have forgotten to set isLoading = false in catch.
Define a cleanup() function before the try and call it in try and catch. This is a decent option, but requires extra function calls, making it harder to follow.

So far, it looks like the first or fifth options are best, depending on how much you care about seeing warning messages. Am I missing any good options?

Ignore the warning. React gives a warning when you setState after
  unmount because it indicates a memory leak (in this case, the
  lingering network request, if not canceled). If the network request
  is canceled, there is still a setState after unmount, but just to set
  a flag. There is no more lingering network request. It should be safe
  to ignore the warning in this case, but it doesn't feel right.

Bad idea. Thing is, by the time the request will resolve, setState might be triggered on an unmounted component. And warning turns to error.

  Check what error was thrown in the finally block and add the same if
  statement as the catch block. This seems incredibly hacky and would
  require extra code to save the error from the catch block.

Meh :\

  Check if the component is mounted in the finally block. This is also
  hacky and requires boilerplate code to update a this.isMounted flag.

It's an Anti-pattern

  Put the cleanup code at the end of try and after the condition in
  catch. This is not DRY. Humans are also very forgetful; I cannot count
  how many times I have forgotten to set isLoading = false in catch.

This

  Define a cleanup() function before the try and call it in try and
  catch. This is a decent option, but requires extra function calls,
  making it harder to follow.

And that, are pretty much taking the same turn with different gear.

Suggestion from React Blog

If you use ES6 promises, you may need to wrap your promise in order to make it cancelable.

const cancelablePromise = makeCancelable(
  new Promise(r => component.setState({...}))
);

cancelablePromise
  .promise
  .then(() => console.log('resolved'))
  .catch((reason) => console.log('isCanceled', reason.isCanceled));

cancelablePromise.cancel(); // Cancel the promise

Where makeCancelable was originally defined by @istarkov as:

const makeCancelable = (promise) => {
  let hasCanceled_ = false;

  const wrappedPromise = new Promise((resolve, reject) => {
    promise.then(
      val => hasCanceled_ ? reject({isCanceled: true}) : resolve(val),
      error => hasCanceled_ ? reject({isCanceled: true}) : reject(error)
    );
  });

  return {
    promise: wrappedPromise,
    cancel() {
      hasCanceled_ = true;
    },
  };
};


Passing JSON object through loop to be used in jQuery Dialog I'm wondering if there is a better (cleaner?) method than my current implementation. I'm currently encoding a PHP SimpleXMLObject (USPS Tracking API) to JSON and looping through said JSON object via JavaScript to operate the front-end.

Examples from my current implementation below:

Function to display dialog implemented anonymously outside of .ready():

var moreInfo_popup = function(i) {
$('#moreinfo'+i).dialog({
        modal:false,
        autoOpen:false,
        height:555,
        title: 'Detailed View',
        width:500,
        draggable:false,
        buttons: {
            Ok: function(){
                $(this).dialog("close");
        }
    }
    });

        $('#moreinfo'+i).dialog('open');

        }

Main loop for displaying Tracking ID, Most Recent Event, and Mail Class for each API response- I'm currently generating a content div appended to #modal_container, then calling moreInfo_popup() inline via <input onClick="">:

for(var key in obj) {
                    if(obj.hasOwnProperty(key)) {
                    if(key % 2 === 0) {
                    $('#page-nav').append("

"+obj[key].TrackInfo.Attributes.ID+"	"+obj[key].TrackInfo.StatusSummary+"	"+obj[key].TrackInfo.Class+"	<input type=\"button\"class=\"moreInfo\"value=\"Detail\"id=\"_buttonMoreInfo\"onClick=\"moreInfo_popup("+key+")\">

");

                    $('#modal_container').append("

" + obj[key].TrackInfo.Attributes.ID +"

").hide();
                  }
                else {
                  $('#page-nav').append("

"
                                      + obj[key].TrackInfo.Attributes.ID + "	" + obj[key].TrackInfo.StatusSummary + "	"
                                      + obj[key].TrackInfo.Class + "	<input type=\"button\" value=\"Detail\" class=\"moreInfo\" id=\"_buttonMoreInfo\"onClick=\"moreInfo_popup("+key+")\">

");

                  $('#modal_container').append("

" + obj[key].TrackInfo.Attributes.ID +"

 <button>OK</button>

");

                }

            }

      $("#page-nav td:contains('undefined')").html("Invalid");
  }

As I'm sure you can see, this feels like an incredibly tedious way of accomplishing the desired outcome, to which there is surely a better alternative. As a newcomer to JavaScript/jQuery, I've done plenty of searching on this subject, but haven't really understood much of what I found, if indeed I was asking the right questions in the first place.

The key problem I have with your code right now is that it doesn't separate data from presentation. This is a maintainability issue because it means now your data is inexplicably tied to your view, and you cannot change one without altering the other. How can we fix this?

As someone earlier suggested, you could use a template library. When I'm doing stand-alone development (i.e, not using a framework), I really like rivets. It's lightweight (6.2kb), simple and unopinionated.

I would have recommended dropping jQuery, but I see that you're using a plugin for jQuery, so I can't really suggest that. I would say that if you can find a way to avoid using jQuery here, that would be great. The reason I don't want you using jQuery is because it's a heavyweight library; it's 34kb, which is over 5x the size of Rivets.

The time-to-last-byte is very important in JavaScript development because until your consumer has received the last byte, your JavaScript is doing nothing, which potentially makes part of your site unusuable. As such, if you can remove any cruft from your site to make it load all of the script tags faster, the better off you will be.

First, we'll define a HTML layout for your tracking info. As your tracking info is tangential to the current information, we can make this use the  HTML element.

      ID
      Summary
      Class

        <button>Detail</button>

So above is the basic structure of your dialog. There's a lot of inline styling, and the only thing your "if" statement is for is to add alternating classes. No need to do that here! We can do that in CSS just fine. First of all, lets apply your styles with a bit of CSS. We'll add a .data-table class to your table, although I would definitely recommend naming it something more descriptive; .data-table is pretty ambigious. You want to describe the role this thing fulfills, not what it is. In other words, be more declarative with your css names. In addition, we'll add a .results-table class to the encapsulating . Whilst I'm not sure this is completely correct, it appears to be closest to what you are doing.

First, let's tackle the overarching styles of each of the tables:

.data-table {
  border: 0;
  width: 60%;
  align: center;
}

You'll notice I have not included the  styling. This is because the width of a  is decided by the browser and not the CSS. This makes sense if you are using truly tabular data. If you're not using tabular data (and I suspect you're not), then you should use a
.
 are definition lists which may be more suited for this sort of thing.

You appear to have some kind of alternate styling based on whether or not a .data-table is an even .data-table. I'm not sure what this styling is because it isn't supplied, but you can replicate this class by using the following CSS selector:

.results-table:nth-child(even) {
  // some CSS styling here
}

So far, we have this:

        <button>Detail</button>

Now, your data model looks something like this:

var model = {
  someNumericValue: {
    TrackInfo: {
      Attributes: {
        ID: 0
      },
      StatusSummary: "summary",
      Class: "first"
    }
  }
};

Wow, this looks a lot like an array! It might be, or it might not be. It's hard to tell because arrays are actually objects. This means that they have access to the hasOwnProperty of an object. Lets assume it is an array because that makes more sense.

var models = [
  {
    TrackInfo: {
      Attributes: {
        ID: 0
      },
      StatusSummary: "summary",
      Class: "first"
    }
  }
];

With this data model, we can easily slot this into our template using rivets bindings. Lets take a look at how it pans out (I've skipped the popup at the moment, we'll get into that in a bit).

        <button>Detail</button>

Now when we set up rivets on this element, as long as we pass in all of our models to "models" - voila, you'll have data binding. Of course, this isn't 100% perfect because we're still relying on our JavaScript to populate the data, however it's a good start; the HTML structure is there and it's declarative. Anyone reading over this file with some knowledge of how rivets works would be able to tell what this does, and the HTML isn't hidden away in JavaScript - the only thing we're getting from JavaScript is the repeating behaviour and the values. This is a good shift!

How would you use this, you might ask? Well, to bind the  to data, it really is as simple as this:

var dialog = document.querySelector('aside.results-table');
rivets.bind(dialog, { order: models });

Where, obviously, models is your array of data. This will ensure that all of your data is bound correctly. Note that this is only one-way data binding though, so any changes you make (DOM -> Model, such as a form) will not be reflected in the change to the view - only model changes will update the view.

Alright, so we have the basic setup done. Now, how do we tackle the fact that we have a pop up that is displayed as well? Well, this gets a bit tricky because you're using a jQuery plugin. It's going to be difficult to neatly display a popup based on DOM elements without doing some nasty fudging. The easiest way would be to continue using jQuery like you're doing now. However, this presents some nasty stuff, namely that you're using numbered IDs to access the dialogs. Ouch! Instead, I would suggest creating a Dialog in HTML and not using JQuery. Like this:

<dialog>
  <title>Detailed View</title>

        <button>OK</button>

</dialog>

Note that the <dialog> element is a HTML5 element, so you may want to change this to a
 if you need to support browsers that aren't HTML5. However, given that I've used  and
 you would need to do that for everything else as well. Worst case scenario, use a HTML5 polyfill. Let's put this all together and add the functionality for the pop-up to be closed/opened after the button is clicked..

            <button rv-on-click='showMoreInfo'>Detail</button>

    <dialog rv-if='order.displayMoreInfo'>
      <title>Detailed View</title>

            <button rv-on-click='hideMoreInfo'>OK</button>

    </dialog>

Note how I've moved the rv-each-order to a
 that encapsulates both the
 and the <dialog>. This makes it easier to group the elements together and better represents that they are a single, isolated section of content compared to the other orders you've put in here. On clicking of the "Detail" button, the dialog will be shown. When the dialog "OK" button is clicked, the dialog will be hidden. Simple stuff, right? (Note: Shown and hidden is not a good analogy. What actually happens is that the entire <dialog> will be removed/added to the DOM. If you wish to instead actually show/hide the <dialog> - for performance reasons, maybe - then use rv-show/rv-hide).

Now, lets add the final bootstrapping logic..

function hideMoreInfo(event, context) {
  var currentOrder = context.order;
  currentOrder.displayMoreInfo = false;
}

function showMoreInfo(event, context) {
  var currentOrder = context.order;
  currentOrder.displayMoreInfo = true;
}

var resultsTable = document.querySelector('.results-table');
rivets.bind(resultsTable, {
  hideMoreInfo: hideMoreInfo,
  showMoreInfo: showMoreInfo,
  orders: models
});

That's it - we're done with the logic side of things! The only thing left would be to do CSS. Unfortunately, I can't do more CSS than what I've done for you because you haven't actually linked any, but if you would like some more help on tht please let me know and I'll gladly oblige.

To summarize, here's the finalised code:

HTML

            <button rv-on-click='showMoreInfo'>Detail</button>

    <dialog rv-if='order.displayMoreInfo'>
      <title>Detailed View</title>

            <button rv-on-click='hideMoreInfo'>OK</button>

    </dialog>

JavaScript

function hideMoreInfo(event, context) {
  var currentOrder = context.order;
  currentOrder.displayMoreInfo = false;
}

function showMoreInfo(event, context) {
  var currentOrder = context.order;
  currentOrder.displayMoreInfo = true;
}

var resultsTable = document.querySelector('.results-table');
rivets.bind(resultsTable, {
  hideMoreInfo: hideMoreInfo,
  showMoreInfo: showMoreInfo,
  orders: models
});

CSS

.data-table {
  border: 0;
  width: 60%;
  align: center;
}


Linear Regression on Pandas I'm working on a simple statistics problem with Pandas and sklearn. I'm aware that my code is ugly, but how can I improve it?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression

df = pd.read_csv("sphist.csv")
df["Date"] = pd.to_datetime(df["Date"])
df.sort_values(["Date"], inplace=True)
df["day_5"] = np.nan
df["day_30"] = np.nan
df["std_5"] = np.nan

for i in range(30, len(df)):
    last_5 = df.iloc[i-5:i, 4]
    last_30 = df.iloc[i-30:i, 4]
    df.iloc[i, -3] = last_5.mean()
    df.iloc[i, -2] = last_30.mean()
    df.iloc[i, -1] = last_5.std()

df = df.iloc[30:]
df.dropna(axis=0, inplace=True)

train = df[df["Date"] < datetime(2013, 1, 1)]
test = df[df["Date"] >= datetime(2013, 1, 1)]
# print(train.head(), test.head())

X_cols = ["day_5", "day_30", "std_5"]
y_col = "Close"

lr = LinearRegression()
lr.fit(train[X_cols], train[y_col])
yhat = lr.predict(test[X_cols])
mse = mean_squared_error(yhat, test[y_col])
rmse = mse/len(yhat)
score = lr.score(test[X_cols], test[y_col])

print(rmse, score)

plt.scatter(yhat, test[y_col], c="k", s=1)
plt.plot([.95*yhat.min(), 1.05*yhat.max()], [.95*yhat.min(), 1.05*yhat.max()], c="r")
plt.show()

It relies on hard-code iloc indices, which is hard to read or maintain. How can I change it to column names/row names?
The codes look messy. Any advice to improve it?

functions

This is 1 long script. Partition the code in logical blocks. This could be like this

get the raw data
summarize the data
split the test- and train data
get the result from the regression
plot the results

magical values

there are some magical values in your code, for example 4 as the column, datetime(2013, 1, 1) as the threshold to split the data. Define them as variables (or parameters for the functions)

dummy data

to illustrate this, I use this dummy data

def generate_dummy_data(
    x_label="x",
    date_label="date",
    size=100,
    seed=0,
    start="20120101",
    freq="7d",
):
    np.random.seed(seed)

    return pd.DataFrame(
        {
            "Close": np.random.randint(100, 200, size=size),
            x_label: np.random.randint(1000, 2000, size=size),
            date_label: pd.DatetimeIndex(start=start, freq=freq, periods=size),
        }
    )

summarize

The rolling mean and std you do can be done with builtin pandas functionality

You also change the raw data. It would be better to make this summary a different DataFrame, and not alter the original data.

def summarize(df, date_label, x_label, y_label="Close"):
    return pd.DataFrame(
        {
            y_label: df[y_label],
            date_label: df[date_label],
            "day_5": df[x_label].rolling(5).mean(),
            "std_5": df[x_label].rolling(5).std(),
            "day_30": df[x_label].rolling(30).mean(),
        }
    ).dropna()

regression

here I followed pep-8, and renamed X_cols to x_cols

def regression(train, test, x_cols, y_col):
    lr = LinearRegression()
    lr.fit(train[x_cols], train[y_col])
    yhat = lr.predict(test[x_cols])
    mse = mean_squared_error(yhat, test[y_col])
    rmse = mse/len(yhat)
    score = lr.score(test[x_cols], test[y_col])

    return yhat, rmse, score

main guard

If you put the calling code behind if __name__ == "__main__":, you can import this script in other code without running the analysis, and reuse the functions

if __name__ == "__main__":

    x_label = "x"
    date_label = "date"
    y_label = "Close"
    data = generate_dummy_data(
        x_label=x_label, date_label=date_label, y_label=y_label
    )

    summary = summarize(
        data, date_label=date_label, x_label=x_label, y_label=y_label
    )

    threshold = "20130101"

    train = summary.loc[summary[date_label] < threshold]
    test = summary.loc[summary[date_label] >= threshold]

    x_cols = ["day_5", "std_5", "day_30"]

    yhat, rmse, score = regression(train, test, x_cols, y_col)

    print(x_cols, rmse, score)

    plt.scatter(yhat, test[y_col], c="k", s=1)
    plt.plot(
        [0.95 * yhat.min(), 1.05 * yhat.max()],
        [0.95 * yhat.min(), 1.05 * yhat.max()],
        c="r",
    )
    plt.show()

If you want to compare what each of the 3 metrics do individually, you'll have to do something like this:

for x_label in x_cols:
    yhat, rmse, score = regression(train, test, [x_label], y_col)

    print(x_label, rmse, score)

    plt.scatter(yhat, test[y_col], c="k", s=1)
    plt.plot([.95*yhat.min(), 1.05*yhat.max()], [.95*yhat.min(), 1.05*yhat.max()], c="r")
    plt.show()


code duplication in conditional Promise I am writing a function in Typescript that involves checking a condition, doing some extra processing if the condition is satisfied, and then doing the bulk of the job.

Here's the catch: the extra processing stuff, that runs if the condition is satisfied, returns a promise. So instead of a clean, simple solution like this:

function openDoor(door) {
    if(door.locked) {
        door.unlock();
    }
    door.handle.turn();
    door.handle.push();
}

It turns into some horrible mess, like this:

function openDoor(door) {
    if(door.locked) {
        door.unlock().then(() => {
            door.handle.turn();
            door.handle.push();
        });
    } else {
        door.handle.turn();
        door.handle.push();
    }
}

Why, you ask?

Unlocking the door takes some time, and I can't turn and push the handle until it's done. Without code duplication, I wouldn't have time to unlock the door before pushing it.

So how can I make this better?

In the shortened example above, the duplicated section is quite small, but troublesome nonetheless. I would like to be able to make changes in a single place rather than two, when I update the code. So, how can I avoid code duplication while keeping my Promise?

You could move the door.locked check into the door.unlock() function (or into a new function) which would allow you return a promise for both cases:

function unlockDoor(door) {
   return door.locked
          ? door.unlock()
          : Promise.resolve();
   }
}

function openDoor(door) {
    unlockDoor(door).then(() => {
            door.handle.turn();
            door.handle.push();
        });
}


Show/hide description I'm trying to keep the code as clean as possible. Is this the best approach? I don't use jQuery that often.

// ==============================================
// Trigger longer description on product page
// ==============================================
var longDescription = $j("#js-long-description");
var shortDescription = $j("#js-shortened-description");
var showHide = $j("#js-trigger-long-description");
var inSpeed = 500;
var outSpeed = 100;

longDescription.hide();

showHide.on("click", function () {

    var buttonTxt = longDescription.is(':visible') ? 'Read More' : 'Read Less';

    showHide.text(buttonTxt);

    if (longDescription.is(':visible')) {

        shortDescription.fadeIn(inSpeed);
        longDescription.fadeOut(outSpeed);

    } else {

        shortDescription.fadeOut(outSpeed);
        longDescription.fadeIn(inSpeed);

    }
});

It looks pretty clean. You are saving the values into variables so to avoid re-running the query.

A minor improvement would be to use click function instead of on('click') as it more readable.

For the toggle part, one can identify 2 phases when clicking:

change button text
animate the description

If you don't need to keep them separate you can remove the ternary operator (the expr1? expr2: expr3) and do:

showHide.click( function () {
    if (longDescription.is(':visible')) {
        showHide.text('Read More');
        shortDescription.fadeIn(inSpeed);
        longDescription.fadeOut(outSpeed);
    } else {
        showHide.text('Read Less');
        shortDescription.fadeOut(outSpeed);
        longDescription.fadeIn(inSpeed);
    }
});


Sum of all digits in a string Given a string, return the sum of all the numbers in the string, 0 if none are present.

Below is my solution for the problem above. I feel like this can be turned into one line. I would really only like suggestions on the actual algorithm, but anything else is appreciated.

def sum_numbers(string: str) -> int:
    """
    Returns the sum of all the numbers in the string
    """
    num_sum = 0
    for char in string:
        if char.isnumeric():
            num_sum += int(char)
    return num_sum

if __name__ == '__main__':

    # Test Cases #

    assert sum_numbers("123") == 6
    assert sum_numbers("abc") == 0
    assert sum_numbers("4x7") == 11
    assert sum_numbers("wefbyug87iqu") == 15
    assert sum_numbers("123456789") == 45

You're right, it can be turned into one line using comprehension syntax which would be your best option of you're looking for a slightly more efficient code.

isdecimal() is better to use in this case than isnumeric() because the isnumeric() accepts types like ½ and ¼ and n² which might produce some side effects you don't want if you're using this function for some real application. Apart from this the code looks good, can be improved using the comprehension syntax which is more efficient and shorter.

Improved version:

def sum_digits(chars: str) -> int:
    """Return sum of numbers in chars"""
    return sum(int(char) for char in chars if char.isdecimal())

if __name__ == '__main__':
    print(sum_digits('abcd173fg'))
    print(sum_digits('abcd'))
    print(sum_digits('173678'))


Handling missing elements on login form in Selenium I try to improve my code so that I don't have to program a try - catch block all the time.

e.g. at the moment I have to write many codeblocks like this:

try {
    $login_feld = $SeleniumObj->driver->findElement(WebDriverBy::id("login_feld"));
} catch(NoSuchElementException $exception) {
    $SeleniumObj->setErrorMessage("1 ID login_feld does not exist.", $exception,  true, "login_field");
    break;
}
try {
    $password_field = $SeleniumObj->driver->findElement(WebDriverBy::id("password_field"));
} catch(NoSuchElementException $exception) {
    $SeleniumObj->setErrorMessage("1 ID password_field does not exist.", $exception,  true, "login_field");
    break;
}
try {
    $field3 = $SeleniumObj->driver->findElement(WebDriverBy::id("field3"));
} catch(NoSuchElementException $exception) {
    $SeleniumObj->setErrorMessage("1 ID field3 does not exist.", $exception,  true, "login_field");
    break;
}

My goal is to reduce this to one line.

Therefore I outsourced the whole driver->findElement(WebDriverBy::id("xy")); to a new function from the class Selenium called ById

/**
 *
 * @param id $id
 * @param bool $takeScreenshot
 * @return void
 */
public function ById($id, $takeScreenshot=false)
{
    try {
        return $this->driver->findElement(WebDriverBy::id($id));

    } catch(NoSuchElementException $exception) {

        $this->setErrorMessage("ID '$id' does not exist.", $exception,  $takeScreenshot, $id);
        //break;  <- does not work in this context
    }
}

Now theoretically I can code the whole block like this:

$login_feld = $SeleniumObj->ById('login_feld', true);
$password_field = $SeleniumObj->ById('password_field', true);
$field3 = $SeleniumObj->ById('field3', true);

However, one problem still persist. I can't call break in the exception block in the function ById, otherwise I get

  break' not in the 'loop' or 'switch' context

Now to solve this I could instead return a status, e.g. false if the code reached in the exception block and then check this before proceeding

/**
 *
 * @param id $id
 * @param bool $takeScreenshot
 * @return void
 */
public function ById($id, $takeScreenshot=false)
{
    try {
        return $this->driver->findElement(WebDriverBy::id($id));

    } catch(NoSuchElementException $exception) {

        $this->setErrorMessage("ID '$id' does not exist.", $exception,  $takeScreenshot, $id);
        return false;
    }
}

-

$login_field = $SeleniumObj->ById('login_field', true);
if ($login_field == false) { break; }
$pw_field = $SeleniumObj->ById('pw_field', true);
if ($pw_field == false) { break; }
$field3 = $SeleniumObj->ById('field3', true);
if ($field3 == false) { break; }

But now I need two lines per block instead of one.
Is it possible to improve this even further?

I don't understand what you are intending to do with break in your catch blocks to begin with.  Even in your first code example, you are not within a loop construct to where this makes sense.

In your new code, you should have method name begin with lower case (i.e. byId) as this is pretty much standard in PHP.  I don't really see what value this method is bringing though, as I would tend to agree with the post by @YourCommonSense that you might simplify your operation by using a looping construct.  However I might suggest you put the try-catch within the loop if your intent is to be able to engage in the try-catch for each element rather than for the collection of elements as a whole.

For example:

$elements = [
    'login_feld' => null,
    'password_field' => null,
    'field3' => null
];
foreach($elements as $id => $notUsed) {
    try {
        $elements[$id] = $SeleniumObj->driver->findElement(WebDriverBy::id($id));
    } catch (NoSuchElementException $e) {
        $SeleniumObj->setErrorMessage(
            '1 ID ' . $id . 'does not exist.',
            $e,
            true,
            $id
        );
        // if you want to break here, you now have proper loop context
        // break;
    }
}

// if successful, you can now access the elements from $elements array

This approach is scalable as you add more elements into your test cases, as you could just add new id keys into the element array and not worry about polluting your global namespace with a bunch of $[elementId] variables. You also now have the ability to simply look at the value stored at each key to determine if the element was found.  So for example, if you are going to perform a test action against a certain element, you can test for it like:

if(!is_null($elements['login_field'])) {
    // execute tests against element
} else {
    // log test fail
}

This approach might totally alleviate your need to break altogether, meaning you could now continue to execute your test suite even if one of the elements you expect to be present is missing.  You could just fail those particular tests relevant to the missing element.