LeetCode 639: Decode Ways II I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time!
Problem

A message containing letters from A-Z is being encoded to numbers
using the following mapping way:
'A' -> 1
'B' -> 2
...
'Z' -> 26

Beyond that, now the encoded string can also contain the character
'*', which can be treated as one of the numbers from 1 to 9.
Given the encoded message containing digits and the character '*',
return the total number of ways to decode it.
Also, since the answer may be very large, you should return the output
mod \$10^9 + 7\$.
Example 1:

Input: "*"
Output: 9
Explanation: The encoded message can be decoded to the string: "A", "B", "C", "D", "E", "F", "G", "H", "I".

Example 2:

Input: "1*"
Output: 9 + 9 = 18

Example 3:

Input: "2*"
Output: 15

Example 4:

Input: "3*"
Output: 9

Example 5:

Input: "44*4"
Output: 11

Note:

The length of the input string will fit in range [1, 105].
The input string will only contain the character '*' and digits '0' - '9'.

Code
#include <string>
#include <vector>

class Solution {
    static constexpr size_t MOD = 1e9 + 7;
public:
    static size_t numDecodings(const std::string message);
    static size_t decode(const char a_num_ast);
    static size_t decode(const char a_num_ast, const char b_num_ast);
};

inline size_t Solution::decode(const char a_num_ast) {
    if (a_num_ast == '*') {
        return 9;

    } else if (a_num_ast == '0') {
        return 0;

    } else {
        return 1;
    }
}

inline size_t Solution::decode(const char a_num_ast, const char b_num_ast) {
    if (a_num_ast == '1') {
        if (b_num_ast == '*') {
            return 9;

        } else if (b_num_ast >= '0' && b_num_ast <= '9') {
            return 1;
        }

    } else if (a_num_ast == '2') {
        if (b_num_ast == '*') {
            return 6;

        } else if (b_num_ast >= '0' && b_num_ast <= '6') {
            return 1;
        }

    } else if (a_num_ast == '0') {
        return 0;

    } else if (a_num_ast == '*') {
        return decode('1', b_num_ast) + decode('2',  b_num_ast);
    }

    return 0;
}

inline size_t Solution::numDecodings(const std::string message) {
    const size_t length = message.size();
    std::vector<size_t> decodes_dp(3, 0);
    decodes_dp[0] = 1;
    decodes_dp[1] = decode(message[0]);

    for (size_t index = 2; index <= length; index++) {
        decodes_dp[index % 3] = (decodes_dp[(index - 1) % 3] * decode(message[index - 1]) % MOD +
                                 decodes_dp[(index - 2) % 3] * decode(message[index - 2], message[index - 1]) % MOD) % MOD;
    }

    return decodes_dp[length % 3];
}

References

Problem

Solution

Discuss

Make helper functions private
Member functions that are not part of the public API should be marked private.
You should know that by now :)
Use uint64_t instead of size_t
There is no guarantee that size_t is big enough for the calculations you are doing. While you might only need 32 bits to store the results, you need to do the calculations using 64 bit integers (because you are multiplying two numbers that are each up to 30 bits in size). So to be safe, I would use uint64_t. You could use uint32_t as well, but then you need to explicitly cast to uint64_t before doing the multiplications inside numDecodings().
Use size_t for sizes and counts, but not for other purposes.
Make the decode() functions constexpr
I see you made MOD constexpr, which is great, but you can make the decode() functions constepxr as well.
Naming things
a_num_ast and b_num_ast are weird looking names. I'm guessing by a_num_ast you mean "a variable that can hold a number or an asterisk". But you shouldn't try to encode the type in the variable name. Just use a and b here.
What does decodes_dp mean? I would try to give it a better name. Use nouns for variables. Perhaps number_of_possibilities, or num_decodings (although that almost clashes with the function name).
Use std::array for fixed-length vectors
This avoids unnecessary heap allocations. So:
std::array<uint64_t, 3> decodes_dp{1, decode(message[0]), 0};

Remove unnecessary modulo operations
In the following expression:
decodes_dp[index % 3] = (
        decodes_dp[(index - 1) % 3] * decode(message[index - 1]) % MOD +
        decodes_dp[(index - 2) % 3] * decode(message[index - 2], message[index - 1]) % MOD
    ) % MOD;

Since you already need to use uint64_t for the result of the multiplications to not wrap, you don't need the modulo operations inside the outermost parentheses.
Consider using switch-statements
Your decode() functions can be rewritten as follows:
inline uint64_t Solution::decode(const char a) {
    switch(a) {
        case '0':
            return 0;
        case '*':
            return 9;
        default:
            return 1;
    }
}

inline uint64_t Solution::decode(const char a, const char b) {
    switch(a) {
        case '0':
            return 0;
        case '1':
            return b == '*' ? 9 : 1;
        case '2':
            switch(b) {
                case '0'...'6':
                    return 1;
                case '*':
                    return 6;
                default:
                    return 0;
            }
        case '*':
            return decode('1', b) + decode('2', b);
        default:
            return 0;
    }
}

It's more compact and avoids repeating a lot of if (a_num_ast ...), making it easier to see the structure of your code.


Divide all elements in the column of a sparse matrix Using Python Scipy, I am trying to divide all numbers in all columns of a sparse matrix (400K × 500K, density 0.0005), by the sum of the squares of all numbers in a column.

If a column is [ [ 0 ] , [ 2 ] , [ 4 ] ], the sum of the squares is 20, so after computation the column should be [ [ 0 ] , [ 0.1 ] , [ 0.2 ] ].

This was my first attempt:

# Loading the sparse matrix
csc = np.load('sparse_matrix.npz')
csc = sp.csc_matrix((csc['data'], csc['indices'], csc['indptr']), shape = csc['shape'], dtype=np.float)

# Computing sum of squares, per column
maxv = np.zeros((csc.shape[1]))
for i in xrange(csc.shape[1]) :
    maxv[i] = sum(np.square(csc[:,i].data))

# Division of non-zero elements by the corresponding sum
for i in xrange(csc.shape[1]) :
    x,y = csc[:,i].nonzero()
    del y
    if x.shape[0] > 0 :
        csc[x,i] = np.array(csc[x,i].todense()) / maxv[i]

However, this seemed to take forever.  I improved the second part (using SciPy sparse: optimize computation on non-zero elements of a sparse matrix (for tf-idf)):

csc = np.load('sparse_matrix.npz')
csc = sp.csc_matrix((csc['data'], csc['indices'], csc['indptr']), shape = csc['shape'], dtype=np.float)

# THIS PART is slow
# Computing sum of squares, per column
maxv = np.zeros((csc.shape[1]))
for i in xrange(csc.shape[1]) :
    maxv[i] = sum(np.square(csc[:,i].data))

# Division of non-zero elements by the corresponding sum
csc = sp.csr_matrix(csc)
xs,ys = csc.nonzero()
csc.data /= maxv[ys]
csc = sp.csc_matrix(csc)

… but I wonder if the computation of squares part can be improved further.

When trying to speed up Numpy code, it's important to scrutinize every loop. A loop that has to run in the Python interpreter can be hundreds of times slower than a vectorized loop running inside Numpy.

Also, when trying to improve the performance of code, there's no substitute for measurement. So let's set up a test case. The matrix csc here has the same density as yours, but is one-hundredth the size (to make the runtimes tractable):

import numpy as np
from scipy.sparse import csc_matrix
from timeit import timeit

shape = 40000, 50000
density = 0.0005
n = np.prod(shape) * density
rowi, coli = [np.random.randint(s, size=n) for s in shape]
csc = csc_matrix((np.random.rand(n), (rowi, coli)), shape=shape)

Here's your sum-of-squares-by-column algorithm:

def sum_squares_by_column_1():
    maxv = np.zeros((csc.shape[1]))
    for i in range(csc.shape[1]) :
        maxv[i] = sum(np.square(csc[:,i].data))
    return maxv

>>> timeit(sum_squares_by_column_1, number=1)
19.718280024942942

So how can we get rid of that for loop? Well, if this were an ordinary Numpy array then we could write:

np.sum(np.square(csc), axis=1)

but this doesn't work here because scipy.sparse.csc_matrix is a matrix rather than an array (see the answers to this Stack Overflow question) and so np.square doesn't work. That's because np.square(a) just multiplies a by itself (as if you'd written a * a), and this is matrix multiplication when a is a matrix.

So what we need to do instead is to read carefully through the scipy.sparse.csc_matrix documentation to see if there are methods for the operations we need. There's no square method, but there is a multiply method that does "point-wise multiplication" and a sum method that "sums the matrix over the given axis". So, putting these together:

def sum_squares_by_column_2():
    return csc.multiply(csc).sum(1)

>>> timeit(sum_squares_by_column_2, number=1)
0.04036429896950722

which is about 500 times faster.


Redux isomorphic fetch in interval I'm not really sure if the following solution is the best fit, and I would like to know best practices when manipulating state and/or Ajax isomorphic fetch requests, in this case, in intervals.

  Users Module

import { createAction, handleActions } from 'redux-actions'
import { api } from '../utils/api'

export const REPORT_POSITION_INTERVAL = 'REPORT_POSITION_INTERVAL'
export const CLEAR_POSITION_INTERVAL = 'CLEAR_POSITION_INTERVAL'
export const POSITION_UPDATED = 'POSITION_UPDATED'

export const positionUpdated = createAction(POSITION_UPDATED, (value) => value)
export const setReportPositionInterval = createAction(REPORT_POSITION_INTERVAL, (interval) => interval)
export const clearPositionInterval = createAction(CLEAR_POSITION_INTERVAL)

export const reportPosition = () => {
  return (dispatch, getState) => {
    if (!navigator.geolocation) {
      return
    }

    function success (position) {
      var latitude = position.coords.latitude
      var longitude = position.coords.longitude

      if (getState().user.userId !== null && getState().user.currentCheckup !== null) {
        var data = {
          lat: latitude,
          lng: longitude,
          user_id: getState().user.userId,
          vehicle_id: getState().user.currentCheckup.vehicle_id
        }
        api(dispatch, getState, '/api/positions/add', 'POST', data, positionUpdated)
      }
    }

    function error () {
      alert('error')
    }

    if (getState().user.currentCheckup !== null) {
      var options = {enableHighAccuracy: true}
      navigator.geolocation.getCurrentPosition(success, error, options)
    }
  }
}

export const reportPositionPeriodically = () => {
  return (dispatch, getState) => {
    dispatch(setReportPositionInterval(setInterval(() => {
      dispatch(reportPosition())
    }, 10000)))
  }
}

export const actions = {
  reportPositionPeriodically,
  clearPositionInterval
}

export default handleActions({
  [REPORT_POSITION_INTERVAL]: (state, { payload }) => {
    return {
      ...state, reportPositionInterval: payload
    }
  },
  [CLEAR_POSITION_INTERVAL]: (state, { payload }) => {
    var clearedInterval = clearInterval(state.reportPositionInterval)
    return {
      ...state, reportPositionInterval: clearedInterval
    }
  },
  [POSITION_UPDATED]: (state, { payload }) => {
    return state
  }
}, {reportPositionInterval: null})

The reportPositionPeriodically action is called from the view.

The reportPositionPeriodically() passes along a setInterval arrow function to setReportPositionInterval which keeps the interval in the state and runs reportPosition().

reportPosition calls a function named api() imported from utils.

  Utils API

import fetch from 'isomorphic-fetch'
import config from 'config'
export const api = (dispatch, getState, endpoint, method, data, success)
...
fetch(config.api_host + endpoint + query, params).then((response) => {
    dispatch(loaderActions.finished())
    return response.json()
}).then((value) => {
    if (value.message) {
        dispatch(loaderActions.fetchError(value.message))
    }
    if ((value.code !== undefined && value.code >= 200 && value.code <= 300) || (value.success !== undefined && value.success)) {
        dispatch(success(value))
    }
})

To clear the timer and stop the fetch requests, I perform a call to clearPositionInterval() which runs clearInterval on the interval in the state.

I find redux very hard and I'm having a hard time finding out how to do things properly.

It looks OK, but I would just return the promise from the api module, and handle the errors, success and so on in the place where redux lives.. namely in reportPositionPeriodically

This way your API-module will be smaller and more reusable, for instance:

  I find redux very hard and I'm having a hard time finding out how to do things properly.

you could replace it later, but the api methods would still work ;)
If you do so, you also have a better overview of your state and whats happening to your store


Returning null or what? I have a C# function like below, and in the else block, the function must return something. I don't know whether returning null is a good practice or not.

public Nullable<Point> GetSprite(int x, int y)
{
    if (x <= xCount && y <= yCount)
    {
        return new Point(x * spriteHeight, y * spriteWidth);
    }
    else
    {
        return null;
    }
}

Yes, you could just write:

public Point GetSprite(int x, int y)
{
    if (x <= xCount && y <= xCount)
    {
        return new Point(x * spriteHeight, y * spriteWidth);
    }

    return null;
}

And write in the documentation it could return null if if (x <= xCount && y <= xCount)

So who use your code could just write

Point point = GetSprite(x, y);
if (point == null) { /* something went wrong, check variables! */ }
else { /* ok! go ahead! */ }

I found this syntax very useful, and could not lead to bugs if the programmer is alerted about the null.

And, anyway you don't need the else block since the return call will stop the function (except if there are any finally blocks, but it's not the case.).


Handling SIGINT signal for a server program I have created a small server program that encrypts text using a named pipe:

#define UNICODE
#define WIN32_WINNT 0x0500
#include <stdio.h>
#include <windows.h>
#include <signal.h>

HANDLE hPipe;
DWORD WINAPI ServerProc(LPVOID lpVoid)
{

    hPipe = CreateNamedPipe(L"\\\\.\\pipe\\testpipe", PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |PIPE_WAIT, 1, 256, 256, 0, NULL);
    if(!hPipe)
        return wprintf(L"ERROR : Cannot create pipe.\n");

    while(ConnectNamedPipe(hPipe, NULL))
    {
        WCHAR szText[80] = {0};
        DWORD dwSize;
        INT i;

        ReadFile(hPipe, szText, 158, &dwSize, NULL);

        for(i = 0; i < dwSize; i++)
            szText[i] ^= '#';

        WriteFile(hPipe, szText, 158, &dwSize, NULL);
        FlushFileBuffers(hPipe);
            DisconnectNamedPipe(hPipe);
    }

    CloseHandle(hPipe);

    return 0;
}

void SignalHandler(int signal)
{
    DisconnectNamedPipe(hPipe);
    CloseHandle(hPipe);
    printf("Application closing...\n");
    exit(0);
}

int wmain(void)
{
    HANDLE hThread;

    signal(SIGINT, SignalHandler);

    hThread = CreateThread(NULL, 0, ServerProc, NULL, 0, NULL);

    WaitForSingleObject(hThread, INFINITE);

    CloseHandle(hThread);
}

In this code I have created a hPipe handle as global because my program thread and the signal handler both need the handle.

Is this a correct way to handle signals?

It depends on what you want the server to do when an interrupt is sent.

If you really want the program to stop when it is sent an interrupt, even though it is a service (Windows) or daemon (Unix), then what you did works.  If, however, the program was run with settings to ignore interrupts, your code overrides that.  The canonical sequence for setting a signal handler (for signal number signum) is:

if (signal(signum, SIG_IGN) != SIG_IGN)
    signal(signum, sighandler);

If the program was ignoring the signal, it continues to do so.  Otherwise, it sets its own handler to handle the signal.

On Unix, you usually use SIGTERM to do a controlled stop of a program, and for daemons, you typically use SIGHUP to indicate that the daemon should revisit its configuration.

Also, in the POSIX world, it is generally recommended to use sigaction() rather than signal() because it gives different and better guarantees about what happens.

If your program needed to continue after receiving the signal, you would have to reset the signal handler in the signal handler - and there's a small window of vulnerability which means that if two signals are sent fast enough, the program stops despite your handler.  Using sigaction() avoids this timing vulnerability.


Car Savings Calculator I've created a second 'calculator-like' program, this time using the JOptionPane rather than typing in the console.  At the moment the code looks to have a lot of repetition so I'm looking to simplify it; any advice and suggestions would be great!

import javax.swing.JOptionPane;

public class SaveCalc {

    public static void main(String[] args) {

        //Text prompts to collect data
        String carCost = JOptionPane.showInputDialog("What is the total cost of the car?");

        String insCost = JOptionPane.showInputDialog("What is the yearly cost of the insurance?");

        String manCost = JOptionPane.showInputDialog("How much would you like to save for maintenance?");

        String saveMonth = JOptionPane.showInputDialog("How much will you be saving each month?");

        //Converts string inputs to Doubles
        double carCostP = Double.parseDouble(carCost);
        double insCostP = Double.parseDouble(insCost);
        double manCostP = Double.parseDouble(manCost);
        double saveMonthP = Double.parseDouble(saveMonth);

        //Calculates the total cost and the months to save
        double costTotal = carCostP + insCostP + manCostP;
        double months = costTotal / saveMonthP;

        //Converts the months value to an integer
        int monthsP = (int) months;

        System.out.printf
        ("With a saving of \u00A3%5.2f each month and a total cost of \u00A3%5.2f it will take "
        + monthsP
        + " months to save up.",saveMonthP,costTotal);
    }
}

Do not include types in variable names

carCostP
       ^

Remove that P (probably stands for a number type?) the compiler takes care of types for you.

Avoid so many variables

You can avoid those four variables altogether, you may just convert to double as soon as you read the vars, like this:

double carCost = Double.parseDouble(
     JOptionPane.showInputDialog("What is the total cost of the car?"));


Simple Compute Pay Function Can this be made any simpler/shorter/more efficient?

This function will compute paycheck amount according to hours worked and rate per hour.

def compute_pay(hours: int, rate: float) -> float:
    """Computes pay according to hours worked and rate per hours.

    For every hour worked after the 40th hour the rate per hour
    is multipled by 1.5
    """

    pay = 0

    if hours > 40:
        pay += 40 * rate
        hours -= 40
        pay += hours * rate * 1.5
    else:
        pay = hours * rate

    return pay

hrs = int(input('Hours: '))
hrs_rate = float(input('Rate: '))

print('Pay:', compute_pay(hrs, hrs_rate))

This is my second take on the above function:

def compute_pay(hours: int, rate: float) -> float:
    """Computes pay according to hours worked and rate per hours.

    For every hour worked after the 40th hour the rate per hour
    is multipled by 1.5
    """

    hours -= 40

    if hours > 0:
        pay = 40 * rate + (hours * rate * 1.5)
    else:
        pay = (hours + 40) * rate

    return pay

hrs = int(input('Hours: '))
hrs_rate = float(input('Rate: '))

print('Pay:', compute_pay(hrs, hrs_rate))

My aim is to have begginers' friendly logic while introducing Python features.

Welcome to Code Review. I think the simpler way of expressing your code would look like this:

def compute_pay(hours: int, rate: float) -> float:
    return (min(hours, 40) + (max(0, hours - 40) * 1.5)) * rate

hrs = int(input('Hours: '))
hrs_rate = float(input('Rate: '))

print('Pay:', compute_pay(hrs, hrs_rate))


Looks at order data and arranges it to focus on order locations I have a sheet that order a bunch of information on individual orders. Order numbers are in column A, other information is in columns B-P, and then product numbers are the headers for columns Q-DL. The cells within the order number and product number contains an integer that corresponds to the location that product is placed.

My code creates a new sheet that loops through all of these rows and then each cell within the row and compares it to headers of the new sheet which contain the location numbers.
If a cell matches the location header, it puts a 1 in the corresponding cell of the new sheet to indicate that product is needed from that location and a 0 if it does not match.

My code works, but it works very slow. So advice on how to make it faster would be appreciated.

Sub PopulateData()
    Dim s1 As Worksheet: Dim s2 As Worksheet
    Dim rng As Range: Dim rng2 As Range
    Dim cell: Dim header

    With Application
        .ScreenUpdating = False
    End With

    With ThisWorkbook
        Set s1 = .Sheets("OrderLvl")
        Set s2 = .Sheets("sheet1")
    End With
    With s1
        Set rng = Range(.Cells(4, 16), .Cells(.Cells(Rows.Count, 116).End(xlUp).row, 116))
    End With
    Set rng2 = Range(s2.Cells(3, 2), s2.Cells(3, 265))

    For Each cell In rng.Cells
        For Each header In rng2.Cells
            If cell.Value = 0 Then

            ElseIf cell.Value <> header.Value And s2.Cells(cell.row, header.Column).Value <> 1 Then
                s2.Cells(cell.row, header.Column).Value = 0
            Else
                s2.Cells(cell.row, header.Column).Value = 1

            ' indicates that this order features a line from this location
            End If
        Next header
    Next cell
    With Application
        .ScreenUpdating = True
    End With
End Sub

A quick fix would be to change:

For Each cell In rng.Cells
    For Each header In rng2.Cells
        If cell.Value = 0 Then

        ElseIf cell.Value <> header.Value And s2.Cells(cell.row, header.Column).Value <> 1 Then
            s2.Cells(cell.row, header.Column).Value = 0
        Else
            s2.Cells(cell.row, header.Column).Value = 1 ' indicates that this order features a line from this location
        End If
    Next header
Next cell

to

For Each cell In rng.Cells
  If cell.Value <> 0 Then
    For Each header In rng2.Cells
        If cell.Value <> header.Value And s2.Cells(cell.row, header.Column).Value <> 1 Then
            s2.Cells(cell.row, header.Column).Value = 0
        Else
            s2.Cells(cell.row, header.Column).Value = 1 ' indicates that this order features a line from this location
        End If
    Next header
  End If
Next cell

That way you only loop through the headers if the cell value isn't 0, instead of looking at every header for every cell no matter its value, then checking to see if it's a cell you're interested in.

It also gets rid of the awkward If x do nothing else do something construct.


Goldilocks temperature I don't want to use the if-statement chain. Can I write the code in a different way? I want to use the smallest amount of if-statements.

#include <stdio.h>
#include <stdlib.h>
main()
{
    int temp;

    printf("type a number: "),
    scanf("%d", &temp);
    printf("\n\n");

    if (temp == 18)
    {
        printf("The temperature is normal, stable conditions. \n");
    }
    else if (temp < 5)
    {
        printf("The temperature is too cold. Massive damage. \n");
    }
    else if (temp > 25)
    {
        printf("The temperature is too hot. Massive damage. \n");
    }

    else
    {
        printf("The temperature is unstable. This condition can make damages. \n");
    }
    system("PAUSE");
}

One single if and you can add an indeterminate number of temperature conditions without adding a single if.

And this code is very short, the struct stuff isn't code

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

struct
{
  int low;
  int high;
  const char *text;
} temperatures[] =
{
  { 18, 18, "The temperature is normal, stable conditions. \n" },
  { -INT_MAX, 5, "The temperature is too cold. Massive damage. \n" },
  { 25, INT_MAX, "The temperature is too hot. Massive damage. \n" },
};

int main()
{
  int temp;

  printf("type a number: "),
    scanf("%d", &temp);
  printf("\n\n");

  for (int i = 0; i < sizeof(temperatures) / sizeof(temperatures[0]); i++)
  {
    if ((temp > temperatures[i].low && temp < temperatures[i].high) || (temp == temperatures[i].low && temp == temperatures[i].high))
    {
      printf("%s", temperatures[i].text);
      return 0;
    }
  }

  printf("The temperature is unstable. This condition can make damages. \n");
}


Encryption/Decryption between Python and Node JS I have created a Python & Node JS encryption/decryption AES 256-CFB. The point of this little project is that I will encrypt a special URL with Python covered with base64 & URL encoding and then the Node JS will decrypt it.
For now the Node JS is working perfectly fine that can starts by decoding the URL, decoding the Base64 and then decrypt the AES with special password key. Meaning that I will have the same key for Node JS and Python to be able to decrypt. Something to add, this will not be added to any other clients/users meaning that covering the password is not needed since no one will reach the code ever but me :)
import base64
import hashlib
import urllib.parse

from Crypto import Random
from Crypto.Cipher import AES

key = "testing"  # Will of course be another password

def encrypt(url):
    """
    Encrypting the given url
    :param url:
    :return:
    """
    private_key = hashlib.sha256(key.encode()).digest()
    rem = len(url) % 16
    padded = str.encode(url) + (b'\0' * (16 - rem)) if rem > 0 else str.encode(url)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(private_key, AES.MODE_CFB, iv, segment_size=128)
    enc = cipher.encrypt(padded)[:len(url)]
    return base64.b64encode(iv + enc).decode()

def main():
    encrypted = urllib.parse.quote(encrypt("https://stackoverflow.com/"))
    url_decode = urllib.parse.unquote(encrypted)

    print(f'Encrypted and URL encoded: {encrypted}\n')
    print(f'URL decoded: {urllib.parse.unquote(url_decode)}\n')

if __name__ == '__main__':
    main()

Now I wonder if there is anyhow where I can improve the code in any how? In my eyes I don't see any ways to improve anyhow but I always end up be wrong :D

The padding step is unnecessary - CFB works out of the box on any plaintext length since it XORS the plaintext with the AES output.
Lack of authentication means that the scheme is vulnerable to a bit flipping attack, where you can flip bits in the ciphertext and cause a corresponding bit flip in the plaintext.
Other nitpicks:

str.encode(url) should be url.encode()
your docstring has a :return: but it doesn't declare anything


Can I simplify my phone look-up program? I needed to make a phone number look-up program with 10 contacts. I feel like I can shorten the program up a bit, it seems very long, but I don't know how. Any suggestion? Do I need all of the Java imports?

package phoneLookUp;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class phoneLookUp
{
static HashMap<String,String> contact = new HashMap<String, String>();

// Method getName() will return the name for given number if it is exist in the contact

public static String getName(String number)
{
Set set = contact.entrySet();
Iterator it = set.iterator();

while(it.hasNext())
{
Map.Entry me = (Map.Entry)it.next();

String tNum = (String)me.getValue();

if(tNum.equals(number))
{
return (String)me.getKey();
}

}

//if given number is not found

return "";
}

// Method getNumber() will return the Name of the given phone number if it is exist in the contact

public static String getNumber(String name)
{
Set set = contact.entrySet();
Iterator it = set.iterator();

while(it.hasNext())
{
Map.Entry me = (Map.Entry)it.next();

String tName = (String)me.getKey();

if(tName.equals(name))
{
String formattedNumber = outputFormat((String)me.getValue());
return formattedNumber;
}

}

// if name if not found in the contact

return "";
}

// Formatting the given phone number

public static String outputFormat(String phone)
{
String temp = "(";

for(int i = 0; i < phone.length(); i++)
{
if(i < 2)
temp += phone.charAt(i);

else if(i == 2)
temp += phone.charAt(i)+") ";

else if(i < 5)
temp += phone.charAt(i);

else if(i == 5)
temp += phone.charAt(i)+" - ";

else
temp += phone.charAt(i);
}

return temp;

}

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

// contacts

contact.put("Tom","6037571122");
contact.put("Alice","6037779057");
contact.put("George","6037779103");
contact.put("Ben", "6031234322");
contact.put("Jack","6037764522");
contact.put("Greg","6039985434");
contact.put("Franklin","60344578993");
contact.put("Sam", "6033458765");
contact.put("Mike","6036673322");
contact.put("Alex", "6035561199");

System.out.println("Welcome to the Phone Look up Program ");

char ch = ' ';

do
{
System.out.print("please enter the name or phone number ( no delemeters please) (q to quit) : ");
String input = sc.next();

ch = input.charAt(0);

if(ch == 'q')
break;

// Finding whether given input is Name or Phone Number

if(Character.isDigit(ch))
{
String name = getName(input);

if(!name.equals(""))
System.out.println("Number : "+outputFormat(input)+" belongs to : "+name);

else
System.out.println("There is no listing for : "+outputFormat(input));
}

else
{
String phone = getNumber(input);

if(!phone.equals(""))
System.out.println(input+"'s"+" phone number is : "+phone);

else
System.out.println("There is no listing for : "+input);
}

}while(true);

}

}

This is my output:

  Welcome to the Phone Look up Program
  please enter the name or phone number ( no delemeters please) (q to quit) : Alice
  Alice's phone number is : (603) 777 - 9057
  please enter the name or phone number ( no delemeters please) (q to quit) : Tom
  Tom's phone number is : (603) 757 - 1122
  please enter the name or phone number ( no delemeters please) (q to quit) : Sally
  There is no listing for : Sally
  please enter the name or phone number ( no delemeters please) (q to quit) : q

Yes , you don't need to iterate through the map to know if it contains a certain value or not, you can just call get

 String contact = contacts.get(name);
 if(contact == null){
  // no such value
 }

You might have notice that I called it contacts instead of contact, because a map is a collection that holds things and things are plural.
Note: If you need to iterate over a map, don't use iterator, use enhanced-for instead.

for(Map.Entry<String,String> entry: map.entrySet()){

}

any language you use, you have to follow its naming conventions. Use Pascal casing for classes in Java

    class PhoneLookUp

and not

    class phoneLookUp

Use StringBuilder instead of String when you need to append frequently. Every time you do += on a String, it creates a new String because strings are immutable in Java, consider using StringBuilder instead.

  public static String outputFormat(String phone)
  {
  StringBuilder temp = new StringBuilder("(");

   for(int i = 0; i < phone.length(); i++)
   {
   if(i < 2){
     temp.append(phone.charAt(i));
   }
   else if(i == 2){
    temp.append(phone.charAt(i)+") ");
   }
   .....
  return temp.toString();

  }


First time writing test case I have a list of input, a greedy method and a dynamic programming method. I wrote a unittest script to test my two methods with input list but I don't know if I'm writing it right:

import unittest
from land_selling import Offer, Seller

class LandSellingTest(unittest.TestCase):
    offers1 = [
        Offer(0, 6, 2),
        Offer(1, 2, 4),
        Offer(3, 5, 1),
        Offer(5, 9, 3),
        Offer(7, 10, 2),
        Offer(10, 20, 5),
        Offer(34, 2, 1)
    ]
    offers2 = [
        Offer(6, 10, 4),
        Offer(1, 5, 2),
        Offer(7, 20, 3),
        Offer(2, 7, 1),
        Offer(14, 30, 2),
        Offer(3, 6, 3),
        Offer(8, 15, 2),
        Offer(20, 35, 5),
        Offer(25, 5, 3)
    ]
    offers3 = [
        Offer(17, 1, 12),
        Offer(7, 2, 10),
        Offer(17, 2, 8),
        Offer(20, 3, 6),
        Offer(3, 11, 4),
        Offer(22, 12, 2)
    ]

    // input list
    global offers_list
    offers_list = [offers1, offers2, offers3]

    // greedy output list
    global greedy_answers
    greedy_answers = [4, 3, 2]

    // dynamic output list
    global dynamic_answers
    dynamic_answers = [13, 12, 16]

    @classmethod
    def setUpClass(self):
        self.offers_list = offers_list
        self.greedy_answers = greedy_answers
        self.dynamic_answers = dynamic_answers

    def test_greedy(self):
        for offers, ans in zip(self.offers_list, self.greedy_answers):
            seller = Seller(offers)
            seller.set_up()
            self.assertEqual(len(seller.accepted_offers()), ans)

    def test_dynamic(self):
        for offers, ans in zip(self.offers_list,self.dynamic_answers):
            seller = Seller(offers)
            seller.sort_offers_by_finish()
            self.assertEqual(seller.maximum_profit(), ans)

if __name__ == '__main__':
    unittest.main()

Using py.test with scenarios

Instead of using unittest, I prefer using pytest. There are few reasons:

no need for using test classes
test cases are easy to write as very simple functions
any inputs to test can be parametrized and easily fed by fixtures or parametrization
very easy to have one test case run for one combinations of parameters - makes easier to identify
how large is the share of failing cases.

Here is my rewrite to pytest (all put in one file test_it.py:

Imports:

import pytest
from land_selling import Offer, Seller

Function generating scenarios:

def gen_scenarios(mode):
    assert mode in ["greedy", "dynamic"]

    offers1 = [
        Offer(0, 6, 2),
        Offer(1, 2, 4),
        Offer(3, 5, 1),
        Offer(5, 9, 3),
        Offer(7, 10, 2),
        Offer(10, 20, 5),
        Offer(34, 2, 1)
    ]
    offers2 = [
        Offer(6, 10, 4),
        Offer(1, 5, 2),
        Offer(7, 20, 3),
        Offer(2, 7, 1),
        Offer(14, 30, 2),
        Offer(3, 6, 3),
        Offer(8, 15, 2),
        Offer(20, 35, 5),
        Offer(25, 5, 3)
    ]
    offers3 = [
        Offer(17, 1, 12),
        Offer(7, 2, 10),
        Offer(17, 2, 8),
        Offer(20, 3, 6),
        Offer(3, 11, 4),
        Offer(22, 12, 2)
    ]

    # input list
    offers_list = [offers1, offers2, offers3]

    if mode == "greedy":
        greedy_answers = [4, 3, 2]
        return zip(offers_list, greedy_answers)
    elif mode == "dynamic":
        dynamic_answers = [13, 12, 16]
        return zip(offers_list, dynamic_answers)

Parametrized tests:

@pytest.mark.parametrize("scenario", gen_scenarios("greedy"))
def test_greedy(scenario):
    offers, ans = scenario
    seller = Seller(offers)
    seller.set_up()
    assert len(seller.accepted_offers()) == ans

@pytest.mark.parametrize("scenario", gen_scenarios("dynamic"))
def test_dynamic(scenario):
    offers, ans = scenario
    seller = Seller(offers)
    seller.sort_offers_by_finish()
    assert seller.maximum_profit() == ans

Run it by:

$ py.test -sv test_it.py

I cannot provide sample output as I do not have access to the imported code, but there you will see
another beauty of py.test - really well readable test output.


Generating Permutations in Python To brush up my Python knowledge I an algorithm to generate permutations in lexicographic order. I ended up with the following code:

def swap(a, i, j):
    tmp = a[i]
    a[i] = a[j]
    a[j] = tmp

def next_permutation(a):
    n = len(a)
    if n == 0 or n == 1:
        return False
    kFound = False
    for k in reversed(range(n - 1)):
        if a[k] < a[k + 1]:
            kFound = True
            break
    if not kFound:
        return False

    for l in reversed(range(n)):
        if a[k] < a[l]:
            break
    swap(a, k, l)
    return a[:k + 1] + [i for i in reversed(a[k + 1:])]

I did a lot of C++ and miss std::swap(..), what is the proper way to do this in Python? In the last line, because reversed(..) returns an iterator I cannot write simply: a[:k + 1] + reversed(a[k + 1:]), is there a simpler way to trigger the unfolding of the iterator into a list?

Python has so many neat stuff to help you with this one, I feel alot can be rewritten:

  I did a lot of C++ and miss std::swap(..)

Luckily you can swap really easy in python

For instance the swap method, can be rewritten like

def swap(a, i, j):
    a[i], a[j] = a[j], a[i]

  Is there a simpler way to trigger the unfolding of the iterator into a list?

Yes there is list slicing.

Just with list slicing only the reversed could be a[k + 1::-1]
Where the last -1 is the step, and -1 means we step over it in reverse
This returns a list and not a generator, thus your reverse could be
return a[:k + 1] + a[k + 1::-1]

@user2357112, I feel like a rookie now.

I made a mistake, intuitively while fast typing I thought list reversing would be like this list[start:end:step] but instead it works differently, with exclusion.

[first i to include:first i to exclude:step], and becomes:

return a[:k + 1] + a[:k:-1]


Parsing a string as fast as possible that is using comma delimiters I have made a simple static parser for a bigger program. This parser takes a string that has comma deliminators and I need to separate the strings in to a vector for handling in a different section of the code.

I would like this to run as fast as possible so the rest of the program isn't tied up separating strings. I also need this parser to be robust, so I can't set how many columns it should have or anything like that.

For the life of me however, I can't seem to figure out how I can make this any less of a O notation. Right now it is O(N) because of the for loop, if there is something in the string. So worse case, the function is O(N). Is there any way to speed it up? I made this, because I have some special cases where if something has nothing between the commas IE ",," then it still needs to make a spot in the vector so the ",," can be placed in a CSV. Also if we need the parser to ignore a comma between quotes it can do that by setting a bool value.

The main problem, or what I would like to see is, is there a way to reduce the complexity of the function to less than \$O(N)\$?

//pass in a string and a vector and if quotes should be used or not and what     char should be used for delimation.
void Strtov::parse(vector<string>& inboundVector, const string& stringToBeParsed, bool quotesUsed, const char& charToSepBy)
{
    string temporary = "";
    //vector<string> retVal;
    bool quoteTriggered = false;
    //if we get an empty string do nothing.
    if (stringToBeParsed.size() > 0) {
        //if the string has something in it then go ahead down the list.
        for (size_t i = 0; i < stringToBeParsed.size(); i++) {

            if (stringToBeParsed[i] == '\"' && quotesUsed) {
                //if they are true, then mark them false, if they are false then mark them true
                quoteTriggered = quoteTriggered ? false : true;
                if (quoteTriggered) {
                    temporary.push_back(stringToBeParsed[i]);
                }
                else {
                    //place quote in to the temporary string and store it.
                    temporary.push_back(stringToBeParsed[i]);
                }
            }
            //place comma if the string is in a quote.
            else if (stringToBeParsed[i] == charToSepBy && quoteTriggered) {
                temporary.push_back(stringToBeParsed[i]); //<-push the comma on to the string because quotes are being used.
            }
            else if (stringToBeParsed[i] == charToSepBy && !quoteTriggered) {
                inboundVector.push_back(temporary);
                temporary = "";
            }
            else if (stringToBeParsed[i] != charToSepBy)
            {
                temporary.push_back(stringToBeParsed[i]);
            }

        }
        if (stringToBeParsed[stringToBeParsed.size() - 1] == charToSepBy) {
            inboundVector.push_back(string()); //if the last character is a comma then push an empty string on the end.
        }
        else if (!temporary.empty())
        {
            inboundVector.push_back(temporary);
        }
     }

  }

Is there any way to improve this code? Obviously I'm the only one working on it, so forgive me if the names seem a bit rough. I'm not the most original in naming things, but I do try and make them descriptive enough for the average person to see what is going on.

There is no way to go under \$O(N)\$, just because you need to inspect the whole string.
The test if (stringToBeParsed.size() > 0) does not speed up the execution; you may safely omit it.
quoteTriggered = quoteTriggered ? false : true; is a long way to say quoteTriggered = !quoteTriggered.
In

    if (quoteTriggered) {
        temporary.push_back(stringToBeParsed[i]);
    }
    else {
        //place quote in to the temporary string and store it.
        temporary.push_back(stringToBeParsed[i]);
    }

the character is pushed anyway, so testing for quoteTriggered is pointless.
The remaining flow in the loop can be streamlined as:

    if quoteIsTriggered or char_is_not_separator
        push_to_temporary
    else
        substring is parsed

Upon the loop termination, you don't need to inspect the last character - if it was comma, temporary will be conveniently empty. Just push temporary and be done.

Putting it all together:

    void parse(vector<string>& inboundVector, const string& stringToBeParsed, bool quotesUsed, const char& charToSepBy)
    {
        string temporary = "";
        bool quoteTriggered = false;
        for (size_t i = 0; i < stringToBeParsed.size(); i++) {
            if (stringToBeParsed[i] == '\"' && quotesUsed) {
                quoteTriggered = !quoteTriggered;
                temporary.push_back(stringToBeParsed[i]);
                continue;
            }

            if (quoteTriggered || stringToBeParsed[i] != charToSepBy) {
                temporary.push_back(stringToBeParsed[i]);
            } else {
                inboundVector.push_back(temporary);
                temporary = "";
            }
        }

        inboundVector.push_back(temporary);
    }


How to access and share MVC Model? I have the following model:

namespace Site.Models.Country
{
    public class Country
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
        public string CountryUrl { get; set; }
    }
}

In a separate model folder for a different view and controller, I have a different model like this:

namespace Site.Models.Directory
{
    public class DirectoryProfileView
    {
        public List<Country> Countries { get; set; }

        public DirectoryProfileView()
        {
            this.Countries = new List<Country>(Country.GetCountryRegions());
        }
    }
}

Is it correct for me to use the Country model from a different model ?  I tried adding using Site.Models.Country;

However for the following to work:

public List<Country> Countries { get; set; }

I need to call by

public List<Country.Country> Countries { get; set; }

My two questions are:

Is this correct?  I don't really want to be creating another model exactly like my first Country model.
Also, any feedback on my naming conventions would be appreciated too.

I think you need to read up a little on namespaces. Both of your namespaces don't make sense unless you will have multiple types of countries and directories.

Your namespace should be if you will have multiple countries

namespace Site.Models.Countries

otherwise it should be

namespace Site.Models

In regards to naming your view models, I would think on how you will use these models in future. If you are likely to have a page for creating, updating, removing and viewing the country, then you might well have the following view models:

DisplayCountry
UpdateCountry
RemoveCountry
SaveCountry

Some people choose to reuse single country model, but I've seen this cause more trouble than benefit. For example, in the RemoveCountry model I'll have only two properties:

public int Id { get; set; }
public string Name { get; set;

The first property is purely for data removal purposes - I assume that you'll pass id to your data layer eventually. Second property is mainly for better UX journey. You might use it for notifying user that they are about to remove a country. E.g., in your view you might have

Warning

Are you sure you want to remove @Model.Name


For each line in a file, read two integers and output the minimum I am going through some CodingAbbey problems to learn basic programming tasks, with a focus on taking input from a file, and saving to another. I have just completed this task:

  Of two numbers, please, select one with minimum value. Here are several pairs of numbers for thorough testing.

  Input data will contain number of test-cases in the first line.
  Following lines will contain a pair of numbers to compare each.
  For Answer please enter the same amount of minimums separated by space, for example:

  data:

3
5 3
2 8
100 15

  answer:

3 2 15

Here is my code:

using System;

namespace cosnPrjo
{
    class MinimumOfTwo
    {

        const string inputPath = @"/Users/rs/Dropbox/InputFile.txt";
        const string outputPath = @"/Users/rs/Dropbox/OutputFile.txt";
        const string searchAfterParam = "\n";

        static void Main ()
        {
            string input = readFile (inputPath);
            input = input.Substring (input.IndexOf (searchAfterParam) + searchAfterParam.Length);
            string[] pairDelims = { "\n" };
            string[] paramDelims = { " " };
            string[] pairs = input.Split (pairDelims, StringSplitOptions.RemoveEmptyEntries);
            string result = "";

            foreach (string pair in pairs) {
                int numToAppend = 0;
                string[] paramas = pair.Split (paramDelims, StringSplitOptions.RemoveEmptyEntries);

                int firstNum = Convert.ToInt32 (paramas [0]);
                int secondNum = Convert.ToInt32 (paramas [1]);

                numToAppend = firstNum < secondNum ? firstNum : secondNum;

                result += numToAppend.ToString () + " ";
            }
            writeFile (outputPath, result.TrimEnd ());
        }

        static string readFile (string input)
        {
            return System.IO.File.ReadAllText (input);
        }

        static void writeFile (string path, string contents)
        {
            System.IO.File.WriteAllText (path, contents);
        }
    }
}

My questions are:

Is this method (reading from and writing to files) an effective and/or sensible way to deal with multi-line input? I find it more cumbersome when I am using the console.
Should a StringBuilder be used to append elements to string in this case?
Is the exercise of ignoring the first number, as I have done here, useful or useless in this context? I don't need to use that number, but should I?
This code is pretty long for such a small task. Is it more useful to write shorter code for such tasks?

There are a few methods that will make the task easier for you:

File.ReadLines to read the file line-by-line (see also File.ReadAllLines)
Enumerable.Skip to skip the first line
Enumerable.Select to map the the lines to the minimum of the two numbers
string.Join for formatting the output

You might also consider taking the input and output file paths from the args parameter. Putting it all together (without error checking) could look like this:

public static void Main(string[] args)
{
    var minimums = File.ReadLines(args[0]).Skip(1).Select(GetMinimum);
    File.WriteAllText(args[1], string.Join(" ", minimums));
}

private static int GetMinimum(string line)
{
    var numbers = line.Split(' ').Select(int.Parse).ToArray();
    return Math.Min(numbers[0], numbers[1]);
}

I've been lazy here with error checking -- it would be a good exercise to add that to the code. It's also good practice to specify the file encoding when reading and writing files, which again I haven't shown here.


Avoding CT and other sins? I'm a SQL learner. In answering the question* For the region with the largest sales total_amt_usd, how many total orders were placed? for the following schema:

(I am unsure of the dialect is but I think it is Postgres)
I came up with the following solution and I wonder if am committing any bad practice (since the official solution is very different).
SELECT
  r.name AS region_name,
  COUNT(*) AS orders_n
FROM ORDERS AS o
INNER JOIN accounts AS a
  ON o.account_id = a.id
INNER JOIN sales_reps as s
  ON a.sales_rep_id = s.id
INNER JOIN region as r
  ON s.region_id = r.id
GROUP BY 1
ORDER BY SUM(o.total_amt_usd) DESC
LIMIT 1;

Official solution:
WITH t1 AS (
   SELECT r.name region_name, SUM(o.total_amt_usd) total_amt
   FROM sales_reps s
   JOIN accounts a
   ON a.sales_rep_id = s.id
   JOIN orders o
   ON o.account_id = a.id
   JOIN region r
   ON r.id = s.region_id
   GROUP BY r.name),
t2 AS (
   SELECT MAX(total_amt)
   FROM t1)
SELECT r.name, COUNT(o.total) total_orders
FROM sales_reps s
JOIN accounts a
ON a.sales_rep_id = s.id
JOIN orders o
ON o.account_id = a.id
JOIN region r
ON r.id = s.region_id
GROUP BY r.name
HAVING SUM(o.total_amt_usd) = (SELECT * FROM t2);

* This is a part of exercise 4.13 of a great online course by Udacity.

Your solution honestly sounds great and better than proposed solution in readability, length, probably performance as well (although it's never too easy to know the engine complexity based on how they optimizes requests). And from my understanding there is no semantic difference.
Some nitpicks: I indent request differently, and I use the column name rather than index in the GROUP BY
SELECT r.name AS region_name,
       COUNT(*) AS orders_n
  FROM orders AS o
    INNER JOIN accounts AS a
      ON o.account_id = a.id
    INNER JOIN sales_reps AS s
      ON a.sales_rep_id = s.id
    INNER JOIN region AS r
      ON s.region_id = r.id
  GROUP BY region_name
  ORDER BY SUM(o.total_amt_usd) DESC
  LIMIT 1;

The request optimization engine will equally optimize your request and decide itself the join order if you use a WHERE clause. So you can also write it like this
SELECT r.name AS region_name,
       COUNT(*) AS orders_n
  FROM orders AS o,
       accounts AS a,
       sales_reps AS s,
       region AS r
  WHERE o.account_id = a.id
    AND a.sales_rep_id = s.id
    AND s.region_id = r.id
  GROUP BY region_name
  ORDER BY SUM(o.total_amt_usd) DESC
  LIMIT 1;


Generating the property name of an object $roleId     = pg_escape_string($_POST['role']);
$grantValue = pg_escape_string($_POST['grant']);
$feature    = pg_escape_string($_POST['feature']);
$permission = pg_escape_string($_POST['permission']);

$permissionsModel = new Permissions();
$record           = $permissionsModel->getPermission($roleId);

//assemble object's property name based on the action taking place and save changes.
$property          = trim($feature . '_' . $permission);
$record->$property = $grantValue;
$record->save(false);

Here is an example of an actual property of the object:

$record->report_w;

$record->facebook_d;

My second question is about escaping the data: can you suggest a better way of validating user input?

Yes, this is bad practice, sometimes

You are giving the user the option to dynamically change object properties. What if the property doesn't exist? What if a boogie monster passes by?

Sometimes, generating the property could be usefull, but only do this when you trust the thing that is generating the property. Is it a user? bad practice. Is it the program itself, wel, maybe.

escaping data

Don't escape data, don't try and remove all 'bad' data. A better approach is to check for a whitelist.

$role has to be an integer, so make sure it is

<?php

if ( preg_match('/^[0-9]+$/',$_POST['role']) != 1 )
{
    throw new Exception('I need an int');
}

feature and permission probably only accept alpha characters? And probably only lower case? So let's check that:

if ( preg_match('/^[0-9a-z]+$/',$_POST['role']) != 1 )
{
    throw new Exception('I need an int');
}

whitelists are better

Use whitelists instead of blacklist. It's easier to know what is allowed then to know what isn't. Simply because we can't know all the bad things out there


Finding the use of the word 'Cthulhu' in tags on Stack Overflow I've written the following SQL to count the number of times the name 'Cthulhu' turns up for each tag on Stack Overflow (original here):

select t.TagName, count (*) 'Tainted'
from Posts p, Tags t, PostTags pt
where p.Body like '%cthulhu%'
and (pt.PostId = p.Id and t.Id = pt.TagId)
group by t.TagName
order by Tainted DESC, t.TagName ASC

It works, but I'm not used to cutting SQL manually; I'm more accustomed to using ORMs.  I tried using CONTAINS instead of LIKE, but apparently Body isn't set up for full-text search.

Could you please provide me some feedback - in particular, are there any best practices I'm missing, and whether there are standard formatting rules for SQL that would make it a bit easier on the eye?

select t.TagName, count (*) 'Tainted'
  from Posts p
 inner join PostTags pt on (pt.PostId == p.Id)
 inner join Tags t on (t.Id == pt.TagId)
 where lower(p.Body) like '%cthulhu%'
 group by t.TagName
 order by Tainted desc, t.TagName asc

Notice the lower on the body, because like is (should?) be case sensitive.
The joins are also easier to read IMHO than the conditions in the where clause.
The formatting is based on right-aligning keywords and left-aligning clauses.


python Improve a function in a elegant way I have a grid as

>>> data = np.zeros((3, 5))
>>> data
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]]

i wrote a function in order to get the ID of each tile.

array([[(0,0), (0,1), (0,2), (0,3), (0,4)],
      [(1,0), (1,1), (1,2), (1,3), (1,4)],
      [(2,0), (2,1), (2,2), (2,3), (2,4)]]

def get_IDgrid(nx,ny):
    lstx = list()
    lsty = list()
    lstgrid = list()
    for p in xrange(ny):
        lstx.append([p]*nx)
    for p in xrange(ny):
        lsty.append(range(0,nx))
    for p in xrange(ny):
        lstgrid.extend(zip(lstx[p],lsty[p]))
    return lstgrid

where nx is the number of columns and ny the number of rows

test = get_IDgrid(5,3)
print test
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]

this function will be embed inside a class

class Grid(object):
    __slots__= ("xMin","yMax","nx","ny","xDist","yDist","ncell")
    def __init__(self,xMin,yMax,nx,ny,xDist,yDist):
        self.xMin = xMin
        self.yMax = yMax
        self.nx = nx
        self.ny = ny
        self.xDist = xDist
        self.yDist= yDist
        self.ncell = nx*ny

Numpy has built in functions for most simple tasks like this one. In your case, numpy.ndindex should do the trick:

>>> import numpy as np
>>> [j for j in np.ndindex(3, 5)]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4),
 (2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]

You can get the same result in a similarly compact way using itertools.product :

>>> import itertools
>>> [j for j in itertools.product(xrange(3), xrange(5))]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4),
 (2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]

EDIT Note that the (now corrected) order of the parameters is reversed with respect to the OP's get_IDgrid.

Both expressions above require a list comprehension, because what gets returned is a generator. You may want to consider whether you really need the whole list of index pairs, or if you could consume them one by one.


Splitting Hashmap by converting to TreeMap I have a project requirement where I have to split a HashMap of around 60-70 entries into multiple HashMaps. The critical number of the split is 30 due to performance reasons because eventually this HashMap is going to be wrapped in a customer Request object and sent in an API call. The receiving end during performance testing found that 30 is the only number they can take for good performance.

So, I have to split the HashMap of 70 subscribers into sub-HashMaps of - 30, 30, 10.

Since HashMaps do not have methods like - subMap() or tailMap() - I am thinking of converting the HashMap into TreeMap. Here's my implementation:

checkAndSplitSubscribers(Map<String, Type> originalMap) throws Exception {
      List<Map<String, Type>> listOfSplitMaps = new ArrayList<Map<String, Type>>();
      int criticalNumber = 30; (Although this will be configurable)

      try {
           TreepMap<String, Type> treeMap = new TreeMap<String, Type> (originalMap);
           List<String> keys = new ArrayList<String> (originalMap.keySet());

           final int originalMapSize = treeMap.size();

           for (int i = 0; i < originalMapSize; i += criticalNumber) {
               if (i + criticalNumber < originalMapSize) {
                    listOfSplitMaps.add(treeMap.subMap(keys.get(i), keys.get(i + criticalNumber));
               } else {
                    listOfSplitMaps.add(treeMap.tailMap(keys.get(i));
               }
           }
      } catch (Exception e) {
           throw e;
      }

      return listOfSplitMaps;
  }

I want to ask if there is anything wrong with this implementation? Is converting HashMap to TreeMap mid way of the code really a bad programming practice? Or if there's any better way to achieve the above, please suggest (I have searched and gone through almost all splitting hashmap questions on SO but I did not find any answer to my satisfaction).

Interfaces over implementations

           TreepMap<String, Type> treeMap = new TreeMap<String, Type> (originalMap);

This could be

           SortedMap<String, Type> splittable = new TreeMap<>(original);

That gets rid of the typo TreepMap.

It saves specifying the types twice.  You don't need to do that unless you are building against an old version of Java.

Specifying the interface rather than the implementation makes it easier to change implementations in the future.  You could also use NavigableMap, but it's not necessary for these operations.

I prefer descriptive names like splittable and original to type names.  Of course, your coding standards could be different.

Exceptions

      } catch (Exception e) {
           throw e;
      }

If you're just going to throw the same exception, there's no reason to catch it.  If you want to catch it, then handle it.  If you want it to percolate up to the caller, then just let it go without catching it.  As is, the try/catch doesn't do anything.  You could simplify the code by taking it out.

Alternatives

I'm not convinced that this does enough to be worthwhile.  It's probably going to be less efficient than the more manual solution.  And it's not less code.  Consider

    Map<String, Type> current = new HashMap<>(MAXIMUM_CAPACITY);

    for (Map.Entry<String, Type> entry : original.entrySet()) {
        current.put(entry.getKey(), entry.getValue());

        if (current.size() >= MAXIMUM_SIZE) {
            splitMaps.add(current);
            current = new HashMap<>(MAXIMUM_CAPACITY);
        }
    }

    if (!current.isEmpty()) {
        splitMaps.add(current);
    }

This is about the same amount of code, but it saves turning the keySet into a List.

Both versions have to iterate over the original map.  This one does so manually while the original code did so implicitly by converting to a TreeMap.

I would find the code more compelling if the data was moved from the HashMap to a TreeMap as the base source.  I.e. if there never was a HashMap and the map came as a TreeMap.

This assumes the creation of two new constants.  You might be able to find better names with more context.  I don't like criticalNumber as a name because it doesn't tell me what makes the number critical.  MAXIMUM_SIZE is a bit better in my opinion, as it tells me that the number is a size and that we don't want to go beyond it.


Sorting elements in array with a certain condition This is what I have:

function get_create returns a link with new filtered/sorted properties:

Code on screenshot

function get_create ($new_query = array(), $script = null, $ext = true, $amp = true) {
   $pr_url = $GLOBALS['pref_url'].$GLOBALS['host_name'];
   $arr_query = array();

if (!empty($new_query)):

   $new_query = array_filter($new_query, function($element) {
        return !empty($element);}); //delete empty elements

 if ($ext) {
   $cur_url = str_replace("&amp;", "&",$GLOBALS['cur_url'])
   $str_query = parse_url($cur_url, PHP_URL_QUERY); // get string of queries
   parse_str($str_query,$arr_query); // create an array with queries

   $arr_query = array_replace_recursive($arr_query,$new_query)
   $arr_query = get_sort_by_key($arr_query);
 }
 else {
   $arr_query = $new_query; // if $new_query empty
 }

 //make a new link with new parameters
  $fn_query = (!empty($arr_query) ? '?'.http_build_query($arr_query,'',($amp ? '&amp;' : '')) : null);
  $fn_script = (!empty($script) ? $script : $GLOBALS['script_name']);

 return $pr_url.$fn_script.$fn_query; // return the link
else :
 return $GLOBALS['home_url'];
endif;

} // end func

In parameters we have:

$new_query (which contains the new properties for $_get part)
$script (contain a new way to script, where data)
$ext (boolean given parameter which make the logic different)
$amp (if true then formats and again)

function get_sort_by_key helps with sorting array (the keys in array must be in this order: lng-topic-page-sort-search).

Code 2 on screenshot

function get_sort_by_key($array = array()) {
$new_array = array();
 foreach ($array as $key => $value) {
    $key = strtolower(trim($key));
    switch ($key) {
        case $key == 'lng': $order = 0;
            break;
        case $key == 'topic': $order = 1;
            break;
        case $key == 'page': $order = 2;
            break;
        case $key == 'sort': $order = 3;
            break;
        case $key == 'search': $order = 4;
            break;
        default: $order = 5; break;
    }
    $new_array[$order][$key] = $value;
 }

ksort($new_array);
   return $fn_array = call_user_func_array('array_merge',$new_array);
}

I need to create the function which gets an array full of new parameters and adds or replaces the current $_GET by them in a specific order.

An example:

I had the URL like this: http://localhost/test.php. After initialization func: get_create(array('sort' => 1, 'topic' => 2) I get  http://localhost/test.php?topic=2&sort=1, where the order is important (not http://localhost/test.php?sort=1&topic=2).

How can I make the function look better? My code works, at least now. But I know that I'm wrong. Maybe some elegant solution exists in my case.

How can I make the function look better?

One alternative approach to the switch statement inside get_sort_by_key() is to define a lookup-table using an array:

define('KEY_MAPPING',array('lng','topic','page','sort','search'));

(or it could be a constant defined on a class if such a relevant class exists).

Then the code block in the foreach can be simplified using array_search() to see if a corresponding index is returned. If not (i.e. it returned false) then use 5 as the fallback value.

foreach ($array as $key => $value) {
    $key = strtolower(trim($key));
    $order = array_search($key, KEY_MAPPING);
    if ($order === false) {
        $order = 5;
    }
    $new_array[$order][$key] = $value;
 }

See this playground example for a demonstration.

For small lists of parameters like this the time difference won't be much. I found this SO question from 6 years ago (and the results may have changed since Ariel's answer as PHP internals have changed but likely not by much).

I tried to find some online posts comparing switch statements with array lookups. The first couple results I see in Google are 6-9 years old:

TechnoSophos: Microbenchmarking PHP: Switch Statements are Slow
DevShed: PHP: The Switch Statement and Arrays ("can they not indent??")
ExceptionsHub: In PHP what's faster, big Switch statement, or Array key lookup

The general consensus is that the array lookup is faster. And typically it can require fewer lines of code. There is a quote somebody said (I can't remember who, so if you know who to give credit please let me know): "The easiest line to debug is the line you didn't write".


Round Robin Tournament RoundRobin is an implementation of the Round-robin style tournament:

public class RoundRobin implements Tournament {
    private final Game game;
    private final List<Player> allPlayers;
    private final List<Player> remainingPlayers;

    public RoundRobin(Game game){
        this.game = game;
        this.allPlayers = new ArrayList<>();
        this.remainingPlayers = new ArrayList<>();
    }

    /**
     * Starts a round of a game, selecting players in a round-robin fashion
     */
    @Override
    public Round nextRound() {
        int playersRemaining = getPlayerCount();
        List<Player> nextPlayers = new ArrayList<>();
        while (playersRemaining > 0) {
            playersRemaining = addPlayers(nextPlayers, playersRemaining);
            if (remainingPlayers.isEmpty()) {
                populateRemainingPlayers(nextPlayers);
            }
        }

        return new Round(game, nextPlayers);
    }

    /**
     * Repopulates the remaining player list, avoiding players that have already been used if the game disallows it
     */
    private void populateRemainingPlayers(List<Player> usedPlayers){
        if (game.allowDuplicatePlayers()) {
            remainingPlayers.addAll(allPlayers);
        } else {
            Set<Player> addedPlayers = new HashSet<>(usedPlayers);
            remainingPlayers.addAll(allPlayers.stream()
                    .filter(addedPlayers::contains).collect(Collectors.toList()));
        }
        Collections.shuffle(remainingPlayers, Controller.random);
    }

    /**
     * Adds players to the upcoming game
     */
    private int addPlayers(List<Player> nextPlayers, int playersRemaining) {
        if (remainingPlayers.size() < playersRemaining) {
            nextPlayers.addAll(remainingPlayers);
            playersRemaining-=remainingPlayers.size();
            remainingPlayers.clear();
            return playersRemaining;
        }
        Set<Player> chosenPlayers = new HashSet<>(remainingPlayers.subList(0, playersRemaining));
        nextPlayers.addAll(chosenPlayers);
        remainingPlayers.removeAll(chosenPlayers);
        return 0;
    }

    /**
     * Checks to make sure there are enough players to play, and returns the number of players we are going to play with
     */
    private int getPlayerCount(){
        if (allPlayers.size() < Math.max(game.minPlayers(), 1) && !game.allowDuplicatePlayers()){
            throw new NotEnoughPlayersException();
        }
        int playerCount = Math.min(allPlayers.size(), game.maxPlayers());
        if (playerCount == -1){
            return allPlayers.size();
        }
        return playerCount;
    }

    /**
     * Adds players to the pool
     */
    @Override
    public void addPlayers(List<Player> players) {
        this.allPlayers.addAll(players);
        this.remainingPlayers.addAll(players);
        Collections.shuffle(this.remainingPlayers, Controller.random);
    }
}

Some notes:

This is for a personal project, so feel free to overhaul the entire thing if you'd like
Game stores data/logic of the actual game, while a Round is an instance of a game.
Games can define a minimum/maximum number of players.  If the maximum is -1, then it allows for infinite amount of players.
A round should have as many players as possible.
Oftentimes games don't care if there are multiple instances of the same player in a game.

This is somehow the most obfuscated code that I seen on this year perhaps, but it's not that bad, at least not after spending some time to figure it out.
But that's why we are here, to make it better.

For this review I will be replacing Controller.random with new Random(1) for simplification.

public Round nextRound() {
    int playersRemaining = getPlayerCount();
    List<Player> nextPlayers = new ArrayList<>();
    while (playersRemaining > 0) {
        playersRemaining = addPlayers(nextPlayers, playersRemaining);
        if (remainingPlayers.isEmpty()) {
            populateRemainingPlayers(nextPlayers);
        }
    }
    return new Round(game, nextPlayers);
}

There are some things that are not intuitive on your while body.

addPlayers update playersRemaining, letting us being uncertain on when the while exits
seems that populateRemainingPlayers can run more than once
You are adding players from remainingPlayers in populateRemainingPlayers and removing on addPlayers
and in the end it's hard to tell what you are really trying to achieve on your algorithm

public void addPlayers(List<Player> players) {
    this.allPlayers.addAll(players);
    this.remainingPlayers.addAll(players);
    Collections.shuffle(this.remainingPlayers, new Random(1));
}

If the game had a list of all players you probably could initialize the lists on the constructor instead of doing it on this method.

I tend to hate method that receive a list as a parameter and then add elements to it (like addPlayers(List<Player> nextPlayers, int playersRemaining)), but I have some good reasons to do so:

The caller needs to instantiate the list
The method that receives the list may need to call other methods with that same list
Which means that elements can be added to the list in different methods
You should separate data handling from logic handling
You can test more easily a method that returns a list that is ready to use

Let's resume your need so we can achieve a better solution.
It may be a little bit bad worded but I did the best I could.

If the game does not allowDuplicatePlayers then the player list should have enough players to play the game, however it may have more
If the list have more players than necessary, then we want to keep track of the last player that was able "to play", so all the players are able to play
After all the players have played it seems you want to have a different player order
If the game allowDuplicatePlayers you can take as many players needed from the player list sequentially, going back to start if necessary taking the same player more than once

Let's try to implement something along those lines:

private int idxLastPlayer;
public Round nextRound() {
    return new Round(game, getNextPlayers());
}

private List<Player> getNextPlayers(){
    int playersRemaining = getPlayerCount();
    List<Player> players = new ArrayList<Player>();
    for(int i = 0; i < playersRemaining; ++i){
        //with idxLastPlayer we still round robin players if game.allowDuplicatePlayers()
        int idx = (i + idxLastPlayer)%remainingPlayers.size();
        players.add(remainingPlayers.get(idx));
        ++idxLastPlayer;
    }
    updateRemainingPlayers();
    return players;
}

private void updateRemainingPlayers(){
    int playersRemaining = getPlayerCount();
    if (!game.allowDuplicatePlayers()){
        int nPlayers = Math.max(playersRemaining, remainingPlayers.size() - 1);
        for(int i = 0; i < nPlayers; ++i){
            remainingPlayers.remove(0); //removing from the start it's not the best but let's not worry about it
        }
    }else{
        //we can keep remainingPlayers always on the same state
        //Todo Should we shuffle players when we reach the end? But we could leap to an index > 0...
        return;
    }
    //if we no longer have enough players we need to reset remainingPlayers with allPlayers
    if(remainingPlayers.size() == 0 ||
        remainingPlayers.size() < playersRemaining && !game.allowDuplicatePlayers()) //this will discard the last allPlayers.size() % playersRemaining players
    {
        //Todo prioritize the players that were still on remainingPlayers?
        remainingPlayers = new ArrayList<Player>(allPlayers);
        //Start the next round on a different order
        Collections.shuffle(this.remainingPlayers, new Random(1));
    }
}


Check if any of class properties is not null/empty/was assigned I have a class that I use to display on a web page fieldset. However, all of the properties of this class are optionally filled in by the user, which means all of them could be null/default. In this case I would like not to render the fieldset at all, instead of displaying an empty one.

I wrote a simple isEmpty() method that checks all property values using reflection, but I'm afraid this may become way too expansive.

public class User
{
    [Display(Name = "First Name:")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name:")]
    public string LastName { get; set; }

    public bool isEmpty()
    {
        Type t = this.GetType();

        var properties = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (var prop in properties)
        {
            var value = prop.GetValue(this, null);

            if (value != null && (string)value != "")
            {
                return false;
            }
        }

        return true;
    }
}

The isEmpty(), of course, assumes all properties are strings.

What do you think? Will this become potentially expansive as the class grows?

Naming:

Don't use variable names such as t. They're not meaningful, not for you, not for others. In this case, use type.

Class names, public fields and method names use PascalCase and not camelCase. So, isEmpty will become IsEmpty. But then again, this is also not a meaningful name. Boolean methods or properties in C# will most likely start with Is or Has. An example for the name of your method would be HasAllEmptyProperties.

var keyword:

Use var to declare your variables instead of declaring them explicitly. The compiler will determine the type for you and your code looks cleaner.

The method:

Your code looks good. You could however make it cleaner by using LinQ. Loops won't be avoided but it looks cleaner. I'm not saying this is a perfect solution but here goes:

public bool HasAllEmptyProperties()
{
    var type = GetType();
    var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    var hasProperty = properties.Select(x => x.GetValue(this, null))
                                .Any(y => y != null && !String.IsNullOrWhiteSpace(y.ToString()));
    return !hasProperty;
}

I could also have used the All() method and positive comparison instead of Any(). But this is inefficient as all elements have to be checked against the condition whereas Any() will return as soon as an element satisfies the condition (like in your loop).

You can take the comparison code and throw it in an extension method for reusability. It also makes your method-code cleaner, for example:

public static class Extensions
{
    public static bool IsNullOrEmpty(this object obj)
    {
        return obj == null || String.IsNullOrWhiteSpace(obj.ToString());
    }
}

public bool HasAllEmptyProperties()
{
    var type = GetType();
    var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    var hasProperty = properties.Select(x => x.GetValue(this, null))
                                .Any(x => !x.IsNullOrEmpty());
    return !hasProperty;
}

Since you only have two properties, performace won't be a problem. Even if you were to add a couple more properties. Hope this helps!


Multithreaded Network Server I would like the following code reviewed:

  I am trying to create a local server which listens indefinitely and
  provides the latest information. The server runs another while loop
  which fetches the latest info every one hour.

  I have tried
  multi-threading approach, in which one thread is a socket server (runs
  indefinitely) and another thread (runs indefinitely with sleep)
  updates the latest info to a global variable.The global variable is
  then accesses by the server and is sent to it's clients.

from socket import *
import threading
import pandas as pd
import json
import time
import datetime
import thread

#Global Info variable
info = pd.DataFrame()

def getInfo():
    global info
    while 1:
        print "Information retrieval running"
        try:
            #This will bring the required info as pandas df
            info = retriveInfoFromDB()
        except:
            pass
        time.sleep(3600)

def runServer():
    global info
    address = ('localhost', 6005)
    server_socket = socket(AF_INET, SOCK_DGRAM)
    server_socket.bind(address)
    while(1):
        print "Listening"
        recv_data, addr = server_socket.recvfrom(2048)
        server_socket.sendto(info.to_json(path_or_buf = None, orient = 'records', date_format = 'epoch', double_precision = 10, force_ascii = True, date_unit = 'ms', default_handler = None), addr)

th1 = threading.Thread(target=runServer)
th2 = threading.Thread(target=getInfo)

th1.start()
th2.start()

def getInfo():
    global info
    while 1:

Don't use while 1 to symbolize an infinite loop; it does not look very pythonic. A better way to write this would be to use True.

try:
    #This will bring the required info as pandas df
    info = retriveInfoFromDB()
except:
    pass

Don't just except; you should be catching a specific type of exception. In this case, the exception you would catch would be whatever exception could possibly be raised from retriveInfoFromDB.

server_socket.bind(address)
while(1):
    print "Listening"

and

global info
while 1:
    print "Information retrieval running"

Why did you use different styling for the while loop in both of these loops? Yes, they should both be using True rather than 1, but why did you put ()s around one's conditional and nothing around the other's conditional?

In my opinion, you should not be using ()s because they make code look more cluttered and less pythonic.

Be careful with these lines:

server_socket = socket(AF_INET, SOCK_DGRAM)
server_socket.bind(address)

These lines are both capable of raising an error.

The python socket library comes with it's own error called socket.error. It split up into a tuple: (errno, string) Where errno is the error number raised by the system call, and string is a descriptive error message of the errno.

To be safe with these lines, just encase them in a try/except statement:

try:
    server_socket = socket(AF_INET, SOCK_DGRAM)
    server_socket.bind(address)
except socket.error as error_message:
    print "Error #%d: %s" % (error_message[0], error_message[1])
    sys.exit(-1)

At the end of the except, I added a function call to quit the program with an exit status of -1 (this tells external programs that there was in running this application).

I think it should be a good idea to exit because, if you are having socket problems, the code is probably not going to work in the first place so it would be best to just exit the code.

Note: an error might be able to arise from

recv_data, addr = server_socket.recvfrom(2048)

If an error can come from here, I recommend that you catch it as I showed above, but I don't think you should have to quit out of the program for it.

The variable names

th1 = threading.Thread(target=runServer)
th2 = threading.Thread(target=getInfo)

are not very descriptive for a variable. Yes, I know it is a thread; the variable name should not tell me that.

The variable name should tell me what the variable is going to be used for. For example, you could call th1 something along the lines of server_listener.

I recommend a different approach to your multi-threading.

In this loop right here:

while True:
    print "Listening"
    recv_data, addr = server_socket.recvfrom(2048)
    server_socket.sendto(info.to_json(path_or_buf = None, orient = 'records', date_format = 'epoch', double_precision = 10, force_ascii = True, date_unit = 'ms', default_handler = None), addr)

Things can get a little hung up with all the reading and the writing; especially reading and writing of that size.

I recommend opening up a new thread every time you have a new connection, and then closing it every time you are done reading and writing the connection.

That way, if your program gets hung up on one connection, the rest won't be affected.

Here is what I mean:

while True:
    connection = threading.Thread(target=read_and_write_data)
    connection.start()

Where read_and_write_data is

def read_and_write_data():
    global info
    global server_socket

    print "Listening"
    recv_data, addr = server_socket.recvfrom(2048)
    server_socket.sendto(info.to_json(path_or_buf = None, orient = 'records', date_format = 'epoch', double_precision = 10, force_ascii = True, date_unit = 'ms', default_handler = None), addr)

Sorry for the poor naming of that function.

To make splitting up into different threads per connection easier, I made the variables address, and server_socket. That way, different functions can easily access the same information; it would be a pain and unnecessary to re-make the socket every time a new thread was created.

And, no need to worry about closing the thread: the thread will automatically close after it's target has finished running.

Python's naming case for variables and functions is snake_case, not camelCase. Your functions getInfo and runServer should be snake_case. See PEP 8, the official Python style guide.

Putting it all together:

from socket import *
import threading
import pandas as pd
import json
import time
import datetime
import thread

#Global Info variable
info = pd.DataFrame()

try:
    server_socket = socket(AF_INET, SOCK_DGRAM)
    server_socket.bind(address)
except socket.error as error_message:
    print "Error #%d: %s" % (error_message[0], error_message[1])
    sys.exit(-1)

def get_info():
    # same code

def run_server():
    while True:
        connection = threading.Thread(target=read_and_write_data)
        connection.start()

def read_and_write_data():
    global info
    global server_socket

    print "Listening"
    recv_data, addr = server_socket.recvfrom(2048)
    server_socket.sendto(info.to_json(path_or_buf = None, orient = 'records', date_format = 'epoch', double_precision = 10, force_ascii = True, date_unit = 'ms', default_handler = None), addr)

server_listener = threading.Thread(target=run_server)
info_reader = threading.Thread(target=get_info)

server_listener.start()
info_reader.start()


Convert hours and minutes to total minutes This is a simple exercise in C designed to work on IO. The purpose of the program is simple: convert a number of hours and minutes to a total number of minutes.

#include <stdio.h>

const unsigned short MinutesPerHour = 60;

int main(int argc, char* argv[])
{
    int hours, minutes, total;

    if(argc == 1)
    {
        puts("Enter the number of hours:");
        scanf("%d", &hours);
        puts("Enter the number of minutes:");
        scanf("%d", &minutes);
    }
    else if(argc == 2)
    {
        hours = atoi(argv[1]);
        minutes = 0;
    }
    else if(argc == 3)
    {
        hours = atoi(argv[1]);
        minutes = atoi(argv[2]);
    }
    else if(argc != 3)
    {
        printf("Usage: %s (int hours) (int minutes)\n", argv[0]);

        return -1;
    }

    total = (hours * MinutesPerHour) + minutes;
    printf("%d hourss and %d minutes is %d minutes\n", hours, minutes, total);

    return 0;
}

Is there specifically any way I could improve the argument/input parsing blocks? Would it be more consistent to ask the user for minutes if they supply an hours command line argument? Should I offer the variety of options at all, or should I stick to command line arguments only or "dynamic" input only?

Bonus: Are there any meaningful names that could be assigned to the 1, 2, and 3 in the parsing blocks that would make the code more readable? Magic numbers are bad, but since it is checking the number of arguments, I can't think of anything other than if (argc == OneArgument) else if (argc == TwoArguments)... which seems silly.

OP's Questions

Is there specifically any way I could improve the argument/input parsing blocks?
See #3 far below
Would it be more consistent to ask the user for minutes if they supply an hours command line argument?
Could go either way on that.  #3 far below does that.
Should I offer the variety of options at all, or should I stick to command line arguments only or "dynamic" input only?

Break the needed input into 2 groups: required and optional.  An optional parameter may be the format to print the result another way - it is not required to run the core function of code.  A required one is the hours.  Minutes could go in either group.

If none of the required parameters are supplied, use interactive mode.

If all of the required parameters are supplied, proceed without asking.

If partial required parameters are supplied, error out.

Are there any meaningful names that could be assigned to the 1, 2, and 3 in the parsing blocks that would make the code more readable?

Not really.  Alternatively  the argument should be paired with a prefix one.  Sample code invocation:

    MBM_Minutes -hour 12 -minute 33
    same result as
    MBM_Minutes -minute 33 -hour 12

Optional arguments could be "-help" "-format AMPM", etc.

Other observations

argv[]

Should code trust that the string will convert to a valid int number of hours?  Look to strtol() or sscanf() if in doubt (or experienced).

// hours = atoi(argv[1]);

if (sscanf(argv[1], "%d", &hours) == 1) Success();
else Handle_Failure();

// better strtol() example down below

The last should be a catch-all else.

// else if(argc != 3) {
else {
  printf("Usage: %s (int hours) (int minutes)\n", argv[0]);

From a code layout, often is is best to qualify variables first, then proceed to the normal flow of code.  Note: argc == 0 is possible, though rare.

if (argc < 1 || argc > 3) {
  printf("Usage: %s (int hours) (int minutes)\n", argv[0]);
  // or send to stderr
  fprintf(stderrr, "Usage: %s (int hours) (int minutes)\n", argv[0]);
  return -1;
}

int hours;
if (argc < 2) {
  GetInt("Enter hours:", &hours, 0, 24);
} else {
  hours = atoi(argv[1]);
}

int minutes;
if (argc < 3) {
  GetInt("Enter minutes:", &hours, 0, 59);
} else {
  hours = atoi(argv[1]);
}

User Input

When dealing with user input, avoid being a hacker target.  Step 1: check user input function results.

puts("Enter the number of hours:");
if (scanf("%d", &hours) != 1) {
  perror("Number not entered\n");
  eixt(-1);
}

To be certain the output is truly outputted and not buffered, make certain the output is flushed before attempting input.

puts("Enter the number of hours:");
fflush(stdout);
if (scanf("%d", &hours) != 1) {

Input values should be qualified to the accept range.

if (scanf("%d", &hours) != 1) {
  ...
}
if (hours < 0 || hours > 24) {
  Handle_Range_Error(hours, 0, 24);
  ...
}

Take user input in a string and then parse it.  Easier to cope with bad input.  Suggests avoiding scanf() and use fgets().
Common code should exist in a helper function.  Putting the above ideas together results in a not-so-simple code and it is certainly overkill for a simple exercise.  Yet it is here to give you ideas of what it takes to handle general user input.

// return 0 on success
int GetInt(const char *prompt, int *dest, int min, int max) {
  long number;
  do {
    fputs(prompt);
    fflush(stdout);
    char buf[100];
    if (fgets(buf, sizeof buf, stdin) == NULL) return EOF;
    char *endptr;
    errno = 0;
    long number = strtol(buf, &endptr, 10);
    if (endptr == buf) continue;  // no conversion
    if (errno) continue; //overflow in conversion
    if (number < min || number > max) continue; // out of range
    while (isspace(*endptr)) endptr++;
  } while (*endptr); // extra junk at the end of the string
  *dest = (int) number;
  return 0;
}

int main(...

   if (GetInt("Enter hours:", &hours, 0, 24)) {
     Handle_EOF();  // Maybe post an error message and exit
   }

Constants

Alternatives to declaring a global object.  There are various pros and cons to these.  What is best is to code to your group's coding standards.  In doubt, go with the last, simplest one.

    const unsigned short MinutesPerHour = 60;
    // or full width to avoid promote to `int`
    const unsigned MinutesPerHour = 60;
    // or
    #define MinutesPerHour 60
    // or insure unsigned-ness
    #define MINUTES_PER_HOUR 60u
    // or classically
    #define MINUTES_PER_HOUR 60

Minor

Spelling: hourss --> hours


Ordering a sequence of objects by type The below code takes an incoming sequence of objects, checks each object's type and then yields the object in a specific order.  This is all taking place in an active pattern.  As I am still new to F#, I would like to know if there is a better or more efficient way of accomplishing this task.

    type ArticleObj =
    | Submission
    | Photos
    | ReviewRating
    | BusinessReviewRating
    | ArticleTag

let (|OrderArticleObj|_|) (objSeq : seq<obj>) =

    // Create DU array
    let articleParts = [|
                        Submission; Photos; ReviewRating;
                        BusinessReviewRating; ArticleTag
                    |]

    // create ordered array of objects
    let orderedSeq = seq{
                for row in articleParts do
                    for row2 in objSeq do
                        match row with
                        | Submission ->
                                        let isValid =
                                            match row2 with
                                            | :? ArticleSubmission as Sub -> true
                                            | _ -> false

                                        if isValid
                                        then
                                            yield row2
                        | Photos ->
                                    let isValid =
                                            match row2 with
                                            | :? seq<Photos> as Photo -> true
                                            | _ -> false

                                    if isValid
                                    then
                                        yield row2
                        | ReviewRating ->
                                            let isValid =
                                                match row2 with
                                                | :? ArticleReviewRating as RevRating -> true
                                                | _ -> false

                                            if isValid
                                            then
                                                yield row2
                        | BusinessReviewRating ->
                                                    let isValid =
                                                        match row2 with
                                                        | :? BusinessReviewRating as BizRev -> true
                                                        | _ -> false

                                                    if isValid
                                                    then
                                                        yield row2
                        | ArticleTag ->
                                        let isValid =
                                            match row2 with
                                            | :? seq<ArticleTags> as ArtTag -> true
                                            | _ -> false

                                        if isValid
                                        then
                                            yield row2
            }

    if not (Seq.isEmpty orderedSeq)
    then Some(orderedSeq) else None

You could define a sorted list of the types and use that to sort the input. Here's what a function that does that would look like.

let sortArticles : seq<obj> -> seq<obj> =
    let sorted =
        [
            typeof<ArticleSubmission>
            typeof<seq<Photos>>
            typeof<ArticleReviewRating>
            typeof<BusinessReviewRating>
            typeof<seq<ArticleTags>>
        ]
    Seq.sortBy (fun o -> sorted |> List.findIndex ((=) (o.GetType())))


Separating Duplicates from Uniques I have a MySQL table with a very large data.  What I need is to find and separate duplicates from the unique rows

Let's say this is my table:

indx, data_lname, data_fname, data_mname, data_dob, data_mobile

What I'm doing right now is selecting all the rows in one query and then comparing each row with the same table using a different query.  This works ok but slow as hell.

Can this be done with a single query?

private sub poplist()
    DBstrSQL = "SELECT * from tbl_data order by indx asc"
    Dim myCmd As New MySqlCommand
    myCmd.CommandTimeout = 300
    myCmd.CommandText = DBstrSQL
    myCmd.Connection = MySqlConn
    Dim myReader As MySqlDataReader = myCmd.ExecuteReader()
    If myReader.HasRows = True Then
          While myReader.Read()
             if checkifdup(myReader.GetString("indx"),myReader.GetString("data_fname"),myReader.GetString("data_mname"),myReader.GetString("data_lname"),myReader.GetString("data_dob"),myReader.GetString("data_mobile"))=false then
                          With lstUnique.Items.Add(myreader.getstring("data_lname"))
                           .SubItems.Add(myreader.getstring("data_fname"))
                           .SubItems.Add(myreader.getstring("data_mname"))
                           .SubItems.Add(myreader.getstring("data_dob"))
                           .SubItems.Add(myreader.getstring("data_mobile"))
                          End With
             else
                          With lstDup.Items.Add(myreader.getstring("data_lname"))
                           .SubItems.Add(myreader.getstring("data_fname"))
                           .SubItems.Add(myreader.getstring("data_mname"))
                           .SubItems.Add(myreader.getstring("data_dob"))
                           .SubItems.Add(myreader.getstring("data_mobile"))
                          End With
             end if
          End While
    end if
    myReader.Close()
end sub

private function checkifdup(dataindx sa string, data1 as string,data2 as string, data3 as string, data4 as string, data5 as string) as boolean
    myCmd.CommandText = "SELECT * from tbl_data where indx<>@indx and data_lname=@lname and data_mname=@mname and data_fname=@fname and (date_format(data_dob,'%m-%d-%Y')=@dob or data_mobile=@mobile) limit 1"
    myCmd.Prepare()
    myCmd.Parameters.AddWithValue("@lname", data3)
    myCmd.Parameters.AddWithValue("@mname", data2)
    myCmd.Parameters.AddWithValue("@fname", data1)
    myCmd.Parameters.AddWithValue("@dob", data4)
    myCmd.Parameters.AddWithValue("@mobile", data5)
    myCmd.Parameters.AddWithValue("@indx", dataindx)
    Dim myReader As MySqlDataReader = myCmd.ExecuteReader()
    If myReader.HasRows = True Then
          myReader.Close()
          return True
    Else
          myReader.Close()
          return false
    End If
end function

You should never run a query in a loop, especially a loop where the number of queries issued scales with the size of the data.  There is almost always a way to formulate the SQL such that you get the results you want with a small, fixed number of queries.

In your case, you want two queries: one to find rows that are unique (ignoring the indx column), and another to find the rows that appear more than once (ignoring the indx column).  You could formulate those queries as:

SELECT data_lname, data_fname, data_mname, data_dob, data_mobile
    FROM tbl_data
    GROUP BY data_lname, data_fname, data_mname, data_dob, data_mobile
    HAVING COUNT(indx) = 1
    ORDER BY indx;

SELECT data_lname, data_fname, data_mname, data_dob, data_mobile
    FROM tbl_data
    GROUP BY data_lname, data_fname, data_mname, data_dob, data_mobile
    HAVING COUNT(indx) > 1
    ORDER BY indx;

Use those queries to populate lstUnique and lstDup, respectively.

Edit: I read the code carelessly, and misinterpreted the criteria for considering two records to be "duplicates".  I would reformulate the query close to the way your VB code worked.

To find the unique records:

SELECT *
    FROM tbl_data AS a
    WHERE NOT EXISTS (
        SELECT indx
            FROM tbl_data AS b
            WHERE
               a.indx <> b.indx
               AND a.data_lname = b.data_lname
               AND a.data_fname = b.data_fname
               AND a.data_mname = b.data_mname
               AND (a.data_dob = b.data_dob OR a.data_mobile = b.data_mobile)
    );

To find the records with duplicates, change WHERE NOT EXISTS to WHERE EXISTS.

This assumes that none of the fields can have a NULL value.

For performance, be sure that indexes exist on the table.  I assume that indx, being the primary key, already has a UNIQUE INDEX.  In addition, there should be an index on (data_lname, data_fname, data_mname).


Merging sorted arrays I'd like to know if it's ok to write like this or there is a better, prettier way to do this. The merging itself is ok. I'm more interested in dealing with the situation when one of the arrays is finished and I need to copy the rest of the other array to the result array. I wrote it in a try catch block.

int[] result = new int[fst.length + snd.length];
int fstIndex = 0;
int fstIndexValue = 0;
int sndIndex = 0;
int sndIndexValue = 0;

while (fstIndex + sndIndex != result.length) {

    try{
        fstIndexValue = fst[fstIndex];
    }catch( java.lang.ArrayIndexOutOfBoundsException e) {
        System.arraycopy(snd, sndIndex, result, fstIndex + sndIndex, snd.length - sndIndex);
        break;
    }

    try{
        sndIndexValue = snd[sndIndex];
    }catch( java.lang.ArrayIndexOutOfBoundsException e) {
        System.arraycopy(fst, fstIndex, result, fstIndex + sndIndex, fst.length - fstIndex);
        break;
    }

    if (fstIndexValue < sndIndexValue) {
        result[fstIndex + sndIndex] = fst[fstIndex++];
    } else {
        result[fstIndex + sndIndex] = snd[sndIndex++];
    }
}
return result;

Generally, you should do what you can not to throw an exception - it is slow and it looks bad.

In this case, you should go with:

        if (fstIndex<fst.length){
            fstIndexValue = fst[fstIndex];
        } else {
            System.arraycopy(snd, sndIndex, result, fstIndex + sndIndex, snd.length - sndIndex);
            break;
        }

Exceptions, as the name implies, suggest an exceptional condition in which the application is. In your case, it's just the algorithm's termination point - a point which you want to reach. It's not exceptional, it's desired.

Also, I don't think there ever is a good excuse to explicitly catch a java.lang.ArrayIndexOutOfBoundsException - if your code is throwing it, then you're doing something else wrong (namely, missing appropriate checks).