String parser review So the task is that a string being passed to one of my methods looks like this

<DIV><GKY><UID><END>

it is generated this way from another program, so it will always have that format. Now to sum up how it works is like this.. This is to represent 3 byte arrays a DIV key (short for diversification) a Guest Key (GKY), and a Unique ID (UID). This particular class that i made stores those byte arrays as a 2 character hex string. IE {0x03, 0xFE, 0xE0} would be stored as "03FEE0"

so those are the requirements of the class.. I have to be able to parse that string to store those byte arrays for access later. DIV (if present) will always be 16 bytes long. GKY is variable and can be at most 192 bytes. UID is required and will always be present and is always 7 bytes. so I came up with this... as far as I can tell it works.

#pragma once

class KeyInputParser
{
public:
    KeyInputParser(void)
    {
        //DIV will always be 16bytes... but it is either not required
        DIV = new char[32];
        //uid will ALWAYS be 7 bytes and is required
        UID = new char[14];
    }
    ~KeyInputParser(void)
    {
        delete[] DIV;
        delete[] GKY;
        delete[] UID;
    }
    char* DIV;
    char* GKY;
    char* UID;
    void SetKeyString(const char* str)
    {
        wcout << "KeyInputParser::SetString()+" << endl;
        wcout << "\t" << str << endl;

        char* div;
        char* gky;
        char* uid;

        div = (char*)memchr(str, '>', strlen(str));
        div++;

        gky = (char*)memchr(div, '>', strlen(div));
        gky++;

        uid = (char*)memchr(gky, '>', strlen(gky));
        uid++;

        for(int i=0; i<strlen(div); i++)
        {
            if(div[i] == '<')
            {//DIV will always be 16bytes... but it is either not required
                memcpy(DIV, div, i);
                DIV[i] = '\0';
                break;
            }
        }
        for(int i=0; i<strlen(gky); i++)
        {//gky will vary, but max out at 192 bytes
            if(gky[i] == '<')
            {
                GKY = new char[i+1];
                memcpy(GKY, gky, i);
                GKY[i] = '\0';
                break;
            }
        }
        for(int i=0; i<strlen(uid); i++)
        {//uid will ALWAYS be 7 bytes and is required
            if(uid[i] == '<')
            {
                memcpy(UID, uid, i);
                UID[i] = '\0';
                break;
            }
        }

        wcout << "KeyInputParser::SetString()-" << endl;
    }
};

I don't care for the way it came out, but I am very new to C++.

First of all separate code into header (.hpp) and implementation file (.cpp)

Header : (my_header.hpp)

#ifndef MY_HEADER
#define MY_HEADER
class KeyInputParser
{
public:
    KeyInputParser  () // there is no need for funcName (void) in C++
    ~KeyInputParser () // that is neccessary only for C
    char* DIV;
    char* GKY;
    char* UID;
    void SetKeyString (const char* str);
    // Progress bewteen div, gky and uid
    char* Progress (char* previous);
    // Get data between <>
    void ExtractField (char* small, char* big);
};
#endif

Implementation : (my_code.cpp)

#include <cstring>
#include "my_header.hpp"

KeyInputParser::KeyInputParser ()
{
    //DIV will always be 16bytes... but it is either not required
    DIV = new char[32];
    //uid will ALWAYS be 7 bytes and is required
    UID = new char[14];
}

KeyInputParser::~KeyInputParser ()
{
    delete[] DIV;
    delete[] GKY;
    delete[] UID;
}

char* KeyInputParser::Progress (char* previous)
{
    char* arr = (char*) memchr (previous, '>', strlen (previous));
    arr++;
    return arr;
}

void KeyInputParser::ExtractField (char* small, char* big)
{
    for(int i = 0; i < strlen (small); i++)
    {
        if(small [i] == '<')
        {
            memcpy (big, small, i);
            big[i] = '\0';
            break;
        }
    }
}
void KeyInputParser::SetKeyString (const char* str)
{
    char* div = Progress (str);
    char* gky = Progress (div);
    char* uid = Progress (gky);

    ExtractField (div, DIV);
    // Code for GKY will be copied due to allocation
    // It might be possible to fix it to be used with ExtractField function
    for(int i = 0; i < strlen (gky); i++)
    {
        if(gky[i] == '<')
        {
            GKY = new char[i+1];
            memcpy(GKY, gky, i);
            GKY[i] = '\0';
            break;
        }
    }
    ExtractField (uid, UID);
}

This is just a rough refactoring of your code.
The thing number one that will need to be fixed is to drop all those C string functions and use std::string instead.

I did not test the code so I cannot guarantee that it will work but this is how it should look like. Hope I helped :)


Rotate array to the left I am a beginner in programming. To rotate the array to left, I have written the below code. Please point out how to optimize the code and anything I am doing which is not good programming practice.

package arrays;

import java.util.Scanner;

public class ArrayLeftRotation {

    public static int[] LeftRotation(int numberOfLeftRotations, int[] a, int[] b) {

        int n1 = numberOfLeftRotations, n2 = numberOfLeftRotations;
        /*
         * First for loop Given array a0=1,a1=2,a3=4,a4=5,a5=6,a6=7; First we
         * are shifting the first 3 elements to last 3 slots and the array looks
         * like below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; The above step we are
         * performing through below logic
         * a[array.length-numberOfLeftRotations]=b[i]; GIVEN ARRAY LENGTH -
         * NUMBER OF LEFT ROTATIONS IS THE STARTING POINT TO MOVE THE FIRST
         * ELEMENTS OF THE ARRAY TO LAST
         */
        for (int i = 0; i < numberOfLeftRotations; i++) {
            a[a.length - n1] = b[i];
            n1--;
        }
        /*
         * Second for loop After executing the above loop the array look like
         * below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; now except the last three
         * slots every thing needs to be changed
         * a0=4,a1=5,a2=6,a3=7,a4=1,a5=2,a6=3; we achieved the above array
         * result through below logic a[i]=b[numberOfLeftRotations]; REPLACED
         * THE REMAINING SLOTS WITH THE VALUES STARTING FROM
         * NUMBEROFLEFTROTATIONS
         */
        for (int i = 0; i < a.length - numberOfLeftRotations; i++) {
            a[i] = b[n2];
            n2++;
        }
        return a;
    }

    public static int[] LeftRotation(int numberOfLeftRotations, int[] a) {

        return a;

    }

    public static int[] printArray(int[] a) {

        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        return a;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of Left Rotations need to be done");
        int l = sc.nextInt();
        System.out.println("Enter number of elements in an array");
        int e = sc.nextInt();
        int[] a = new int[e];

        for (int i = 0; i < a.length; i++) {
            a[i] = sc.nextInt();
        }

        int[] b = a.clone();

        LeftRotation(l, a, b);
        printArray(a);

    }
}

Your class has 4 methods:

int[] LeftRotate(int, int[], int[])
int[] LeftRotate(int, int[])
int[] printArray(int[])
void main(String[])

The second method is never called, and can be removed.

The first and third methods (and the second) all return an int[], the a parameter, but the returned values are never assigned to anything.  This return value can be removed, and the methods declared to return void.

In the first method, you are looping over increasing i values, decrementing n1, and assigning to a[a.length - n1].  This “double negative” makes the code harder to understand; decrementing n1 increases the destination index value.  You already have a i index which in incrementing.  You could instead:

for(int i = 0; i < numberOfLeftRotations; i++) {
    a[a.length - numberOfLeftRotations + i] = b[i];
}

This is a little easier to understand; you are only changing i, increasing it, and adding it to a starting point for the destination.  We can make it a little clearer:

int dest_start = a.length - numberOfLeftRotations;
for(int i = 0; i < numberOfLeftRotations; i++) {
    a[dest_start + i] = b[i];
}

That is much more understandable.  Do the same for the second loop.

The above is just a cleanup of your existing code.  We can restructure / refactor the code to make it better and easier to use.

When you want to left-rotate an array, you always need to create a new array for the destination.  You can make the left-rotate function create the destination array for the caller.

int[] LeftRotate(int numberOfLeftRotations, int[] source) {
    int[] dest = new int[source.length];
    // ... copy rotated version from dest into source
    return dest;
}

And use it as:

int[] b = LeftRotation(l, a);

In fact, this is probably what was intended for your second method.  The code to copy the rotated version from dest to source could just be a call of the first “helper” method:

int[] LeftRotate(int numberOfLeftRotations, int[] source) {
    int[] dest = new int[source.length];
    LeftRotate(numberOfLeftRotations, dest, source);
    return dest;
}

Your first method should still be void, as it is not returning anything that wasn’t just passed to it.

Copying sequential elements from one array to another is a common operation, and Java provides a method which does just that: System.arraycopy().  Using this, your left rotation can be performed very efficiently in two statements:

System.arraycopy(source, 0, dest, dest.length - numberOfLeftRotations, numberOfLeftRotations);
System.arraycopy(source, numberOfLeftRotations, dest, 0, dest.length - numberOfLeftRotations);

Use better variable names.  a and b are unclear; source and dest give context to the reader.  numberOfLeftRotations is a very descriptive, but it is perhaps a little long.  numLeftRotations is just as clear and doesn’t result in lines becoming excessively long.  numRotations or rotations might be even better; the reader would know they are left rotations, because the variables are contained within a “left rotate” function.

Convention is for only Class names to begin with upper case letters; method names should not.  You should name the method leftRotate, not LeftRotate.


Wrap a long string to an array of short-enough strings I have made a function that wraps text in pygame (i.e. it turns a long string into an array of smaller strings that are short enough so they fit within the given width when rendered in the given font).

My function is below. text is the string we want to break up, font is a pygame font object, and max_width is the number of pixels wide we want the lines to be at maximum (an integer).

def wrap_text(text, font, max_width):
    lines = []
    words = text.split(" ")

    while words:
        line = words.pop(0)
        if words:
            width = font.size(" ".join((line, words[0])))[0]
            while width < max_width:
                if words[0] == "\n":
                    # Forced newline when "\n" is in the text
                    del words[0]
                    break
                line = " ".join((line, words.pop(0)))
                if not words:
                    break
                width = font.size(" ".join((line, words[0])))[0]

        if font.size(line)[0] > max_width:
            # When there is only one word on the line and it is still
            # too long to fit within the given maximum width.
            raise ValueError("".join(("\"", line, "\"", " is too long to be wrapped.")))
        lines.append(line)

    return lines

note: font.size(string) returns a tuple containing the width and height the string will be when rendered in the given font.

As you can see, I have the statements while words:, if words: and if not words: all within each other. I have been trying to refactor this by moving things around but I simply cannot think of a way to remove any of the 3 statements above. Any help is much appreciated :). Any comments about anything else in my code is welcome too.

Bug

You have a small bug. For text="abc \\n def" and max_width=10 the output is incorrectly ['abc', 'def'] instead of ['abc def'].

You can greatly simplify the handling of embedded whitespace characters by using re.split with a pattern r'\s+'.

Don't repeat yourself

This (or almost the same) piece of code appears in multiple places: " ".join((line, words[0])).
Look out for duplication like this and use helper functions to eliminate.

Overcomplicated string joins

Instead of " ".join((line, words[0])),
it would be a lot simpler to write line + " " + words[0].

Simplify the logic

Consider this alternative algorithm:

Create a word generator: from some text as input, extract the words one by one
For each word:

If the word is too long, raise an error
If the current line + word would be too long, then

Append the current line to the list of lines
Start a new line with the current word

If the current line + word is not too long, then append the word to the line

Append the current line to the list of lines

Implementation:

def wrap_text(text, font, max_width):
    def gen_words(text):
        yield from re.split(r'\s+', text)
        # or in older versions of Python:
        # for word in re.split(r'\s+', text):
        #     yield word

    def raise_word_too_long_error(word):
        raise ValueError("\"{}\" is too long to be wrapped.".format(word))

    def too_long(line):
        return font.size(line)[0] > max_width

    words = gen_words(text)
    line = next(words)
    if too_long(line):
        raise_word_too_long_error(line)

    lines = []
    for word in words:
        if too_long(word):
            raise_word_too_long_error(word)

        if too_long(line + " " + word):
            lines.append(line)
            line = word
        else:
            line += " " + word

    lines.append(line)

    return lines

Some doctests to verify it works:

def _wrap_text_tester(text, max_width):
    """
    >>> _wrap_text_tester("hello there", 7)
    ['hello', 'there']

    >>> _wrap_text_tester("I am legend", 7)
    ['I am', 'legend']

    >>> _wrap_text_tester("abc \\n def", 10)
    ['abc def']

    >>> _wrap_text_tester("toobigtofit", 7)
    Traceback (most recent call last):
        ...
    ValueError: "toobigtofit" is too long to be wrapped.

    >>> _wrap_text_tester("in the middle toobigtofit", 7)
    Traceback (most recent call last):
        ...
    ValueError: "toobigtofit" is too long to be wrapped.

    >>> _wrap_text_tester("", 7)
    ['']

    """
    return wrap_text(text, font, max_width)


Sudoku verifier in Java This program verifies if a matrix is a Sudoku or not. These conditions should be verified:

There are no duplicates in a row, column, or square
The sum of a row, column, or square should be equal to 45

public class SudokuChecker {

static int[][] invalidMatrix={

        {5,5,5,5,5,5,5,5,5},
        {1,2,5,5,5,5,5,5,5},
        {5,5,5,5,5,5,5,5,5},
        {5,5,5,5,5,5,5,5,5},
        {5,5,5,5,5,5,5,5,5},
        {5,5,5,5,5,5,5,5,5},
        {5,5,5,5,5,5,5,5,5},
        {5,5,5,5,5,5,5,5,5},
        {5,5,5,5,5,5,5,5,5}
    };

static int[][] validMatrix={

    {5,3,4,6,7,8,9,1,2},
    {6,7,2,1,9,5,3,4,8},
    {1,9,8,3,4,2,5,6,7},
    {8,5,9,7,6,1,4,2,3},
    {4,2,6,8,5,3,7,9,1},
    {7,1,3,9,2,4,8,5,6},
    {9,6,1,5,3,7,2,8,4},
    {2,8,7,4,1,9,6,3,5},
    {3,4,5,2,8,6,1,7,9}
    };

static int rSum = 0;
static int cSum = 0;
static int squareSum = 0;

static int[] rArray = new int[9];
static int[] cArray = new int[9];
static int[] squareArray = new int[9];

/*
 * check of there is duplicates
 */

public static boolean checkDuplicate(int[] array){

    Boolean duplicate = false;
    Set<Integer> set= new HashSet<>();
    for(int i : array){
        if(set.contains(i)){
            duplicate = true;
    } else {
        set.add(i);
    }
    }
    return duplicate;
}

/*
 * check if the sum of the element of a row are equals to 45
 */

public static boolean rowSum(int[][] matrix){
    boolean correct = true;
    for(int i =0; i < matrix.length; i++){
        for(int j=0 ; j < matrix[i].length; j++){
            rArray[j] = matrix[i][j];
            rSum += rArray[j];
        }
        if(rSum != 45 || checkDuplicate(rArray) ){
            correct = false;
            break;
        }
        rSum = 0;
    }
    return correct;

}

/*
 * check if the sum of the elements of a col are equals to 45
 */

public static boolean colSum(int[][] matrix){
    boolean correct = true;
    for(int j=0 ; j < matrix.length; j++){
        for(int i=0 ; i < matrix.length ; i++){
            cArray[i] = matrix[i][j];
            cSum += cArray[i];
        }
        if(cSum != 45 || checkDuplicate(cArray)){
            correct = false;
            break;
        }
        cSum = 0;
    }
    return correct;
}

/*
 * check if the sum of the elemnts of a square 3x3 are equals to 45
 */

public static boolean squareSum(int[][] matrix){
    boolean correct = true;
    for(int i=0; i < matrix.length ; i = i + 3){
        for(int j=0; j < matrix.length ; j = j +3){

            squareArray[0] = matrix[i][j];
            squareArray[1] = matrix[i][j+1];
            squareArray[2] = matrix[i][j+2];
            squareArray[3] = matrix[i+1][j];
            squareArray[4] = matrix[i+1][j+1];
            squareArray[5] = matrix[i+1][j+2];
            squareArray[6] = matrix[i+2][j];
            squareArray[7] = matrix[i+2][j+1];
            squareArray[8] = matrix[i+2][j+2];

            squareSum = matrix[i][j] + matrix[i][j+1] + matrix[i][j+2]
                      + matrix[i+1][j] + matrix[i+1][j+1] + matrix[i+1][j+2]
                      + matrix[i+2][j] + matrix[i+2][j+1] + matrix[i+2][j+2] ;
            if(squareSum != 45 || checkDuplicate(squareArray)){
                correct = false;
                break;
            } else{
                squareSum = 0;
            }
        }
    }
    return correct;
}

/*
 *  Test
 */

public static boolean test(int[][] matrix){

    boolean correct = true;
    if (!(rowSum(matrix) && colSum(matrix) && squareSum(matrix))){
        correct = false;
    }
    return correct;

}

public static void main(String[] args) {

    System.out.println(test(invalidMatrix) ? "Sudoku valide" : "Sudoku invalide");
}
}

Boolean duplicate = false;
    Set<Integer> set= new HashSet<>();
    for(int i : array){
        if(set.contains(i)){
            duplicate = true;
    } else {
        set.add(i);
    }
    }
    return duplicate;

This can be written more briefly as

    Set<Integer> seen = new HashSet<>();
    for (int i : array) {
        if (!seen.add(i)) {
            return true;
        }
    }

    return false;

You do not need to check contains before add.  The add will check contains for you.

You do not need to keep going after you've found a duplicate.  You can stop immediately and return.  Everything else will be just wasted work.

You do not need the duplicate variable.  You can just return instead.

The indent is now consistent, and I added some horizontal whitespace for readability.

Another alternative would be

    int[] seenCounts = new int[array.length];
    for (int i : array) {
        i--;
        if (seenCounts[i] >= 1) {
            return true;
        } else {
            seenCounts[i]++;
        }
    }

    return false;

The array will be more efficient than the Set.

Also, this will enforce that the numbers are in the range 1 to 9.  If you had a 0 or 10, this code would throw an exception.  This is important because an array with five 9s and four 0s would return true;


Lightweight localization for a Cordova app I had been trying to find a suitable solution to implement localization in my Phonegap/Cordova application. My search proved that any localization through seperate .xml or .json files was a nightmare (mostly due to cross-access-origin).

Because of this, I decided to create a (very) simple, lightweight localization solution.

Here is what I've written:

var locale = {
    getString:function(required){
        var language = localStorage.getItem("language");
        if(language === null){
            language = "nl";
        }

        return window['locale']['translation'][language][required];
    },

    translation: {
        en: {
            play:"Play",
            faq:"FAQ",
            highscore:"Highscore",
            gameStart:"Game starting in ",
        },
        nl: {
            play:"Speel",
            faq:"FAQ",
            highscore:"Highscore",
            gameStart:"Spel start in ",
        }
    }
}

I am really interested in any comments you have. Would there be a better solution or any better methods to use in my implementation?

AFAIK, accessing local files using XMLHttpRequest won't be a problem once you have your page up on a web server.

Anyway, if you decide to go with the solution above, here are some general tips:

getString() method:

Use this.translation rather than window['locale']['translation']. There no point traversing to window and back again when you can use the this pointer.
Try to be more descriptive when naming variables and methods. For example, the required argument. If a third party user is using your API, how would they pssobile know what to pass? A string? An object?

translation object:

Your language objects have some indentical values. To be less repetitive, create a default object and merge with each new language. I will use jQuery.extend() in my example.

Misc:

Create an object that keeps track of your constants and reuse it throughout your module.

The code could look something like this:

var locale = {

    constants: {
        language: {
            localStorageKey: 'language',
            default: 'nl'
        }
    },

    getTranslationByLanguage:function(languageProperty){

        if(!languageProperty)
            throw new ReferenceError('languageProperty not defined');

        var language = localStorage.getItem(this.constants.language.localStorageKey)
                       || this.constants.language.default;

        var translation = this.translation.getTranslationByLanguageKey(language);

        return translation[languageProperty];
    },

    translation: {

        defaults: {
            faq: 'FAQ',
            highscore: 'highscore'
        },

        en: {
            play:'Play',
            gameStart:'Game starting in '
        },

        nl: {
            play:'Speel',
            gameStart:'Spel start in '
        },

        getTranslationByLanguageKey: function(languageKey){
            return $.extend({}, this.defaults, this[languageKey] || {});
        }

    }
};


Go through every pixel of an image and then dispose of it I'm a little confused as to whether implementing the Dispose method serves a purpose in this code. It never gets referenced by any other class in my project, so should I simply get rid of it or should I make an explicit call to Dispose when releasing the bitmap image?

namespace ImageProcessor
    {
        /// <summary>
        /// A bitmap class that allows fast x, y access
        /// </summary>
        public unsafe class UnsafeBitmap
        {
            Bitmap source;
            int width;
            BitmapData bmpData = null;
            IntPtr baseLine = IntPtr.Zero;
            BGRA* pCurrentPixel = null;
            Point size;
            internal bool locked = false;

            /// <summary>
            /// Create an instance from an existing bitmap
            /// </summary>
            /// <param name="src">The source bitmap</param>
            public UnsafeBitmap(Bitmap src)
            {
                source = src;
                GraphicsUnit units = GraphicsUnit.Pixel;
                RectangleF boundsF = src.GetBounds(ref units);
                size = new Point((int)boundsF.Width, (int)boundsF.Height);
            }

            public Bitmap Bitmap
            {
                get { return (source); }
                set { source = value; }
            }

            /// <summary>
            /// Lock bitmap data
            /// </summary>
            public void LockBitmap()
            {
                try
                {
                    GraphicsUnit units = GraphicsUnit.Pixel;
                    RectangleF boundsF = source.GetBounds(ref units);
                    Rectangle bounds = Rectangle.Round(boundsF);

                    // For a standard BMP, each individual scan line needs to be a multiple
                    // of 4 bytes, so when we have a 24 bit image (3 bytes per pixel),
                    // we need to allow for padding at the end of every scan line
                    // to bring it up to a multiple of 4.
                    width = (int)boundsF.Width * sizeof(BGRA);
                    if (width % 4 != 0)
                        width = 4 * (width / 4 + 1);

                    // Lock the bitmap's bits
                    bmpData = source.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

                    // Get the address of the first line
                    //pBase = (Byte*)bmpData.Scan0.ToPointer();
                    baseLine = bmpData.Scan0;
                    locked = true;
                }
                catch(Exception)
                {
                    throw;
                }
            }

            /// <summary>
            /// Unlock bitmap data
            /// </summary>

            public void UnlockBitmap()
            {
                try
                {
                    source.UnlockBits(bmpData);
                    bmpData = null;
                    baseLine = IntPtr.Zero;
                    locked = false;
                }
                catch (Exception)
                {
                    throw;
                }
            }

            public void Dispose()
            {
                source.Dispose();
            }

            public Point GetSize
            {
                get { return size; }
            }

            public BGRA* this[int x, int y]
            {
                get
                {
                    return (BGRA*)(baseLine + y * width + x * sizeof(BGRA));
                }
            }
        }

Any other comments are welcome.

I think your Bitmap property shouldn't have a setter. If you use it, you don't dispose the old Bitmap and you don't reset size. Both seem to be erroneous to me.

public Bitmap Bitmap
{
    get { return (source); }
    set { source = value; }
}

Why is source in parentheses?

Also, this could be an auto-property. You would then use the (possibly private) setter instead of the field in the constructor.

catch(Exception)
{
    throw;
}

This doesn't do anything, get rid of it.

public BGRA* this[int x, int y]
{
    get
    {
        return (BGRA*)(baseLine + y * width + x * sizeof(BGRA));
    }
}

If the type of baseLine was BGRA* (and you also changed width not to include the size), then the return statement could be simplified to:

return &baseLine[y * width + x];

Also, I would consider returning BGRA directly, I think that's more natural (assuming doing that doesn't decrease performance noticeably).


Testing a Rails API controller I'm building a JSON API. I'm writing my controller tests so that they compare the response body to actual JSON I'm generating in my tests using ActiveRecord. I wrote two helper methods that make the process easier. The first simply parses the actual response body and returns it.

def json_response_body(&block)
  JSON.parse(response.body, symbolize_names: true).tap do |content|
    yield content if block_given?
  end
end

The second method returns a JSON representation of an object, and accepts options. This is what I expect the response to be. So I compare the output of this method to the actual response.

def as_parsed_json(object, options = {})
  # Converting to JSON and reparsing ensures
  # dates are in the same format as my controller responses.
  JSON.parse object.to_json(options), symbolize_names: true
end

This way, I can write my tests like this:

test "GET #show" do
  product = create(:product)
  get :show, id: product

  expected =
    as_parsed_json(product, only: [:id, :title, :price_in_cents])

  assert_equal expected, json_response_body
  assert_response :success
end

Is this a good way of testing me controllers? The part that makes me think is where I'm generating the JSON using Rails helpers instead of explicitly writing the JSON myself, or looking for keys in the response body. For example:

expected = { id: product.id, title: product.title }.to_json
# Or
assert_equal product.id, request.body[:id]

But since Rails is pretty well tested, I feel like I can trust its JSON return. I'm not sure if this approach makes my tests brittle, since any change in the response body will cause the tests to fail. But maybe that's a good thing? Here's another example:

test "POST #create with valid attributes" do
  user = create(:user)
  api_authorization_header token: user.auth_token

  post :create, product: attributes_for(:product)
  expected = as_parsed_json Product.last, json_options

  assert_equal expected, json_response_body
  assert_response :created
end

And it's controller action:

  # app/controllers/api/v1/users_controller.rb
  def create
    @user = User.new
    @user.assign_attributes permitted_attributes(@user)
    if @user.save
      render :create, status: 201, location: [:api, @user]
    else
      render json: { errors: @user.errors }, status: 422
    end
  end

I'm using Jbuilder to render the actual JSON.

# app/views/api/v1/users/create.json.jbuilder
json.(@user, :id, :name, :email, :created_at)
json.auth_token @user.auth_token

I think you're right to be suspicious of your test setup. Given your code, I imagine you're using #to_json/#as_json in your controllers already, which in effect reduces your test to:

assert_equal record.as_json, record.as_json

which doesn't prove much of anything.

It can also hide bugs. For instance, say your product attributes are name and an optional description, and you expect both to be saved. But your create action accidentally filters away description, so that's never set (and since it's optional, there's no validation error). But you're comparing the created record to itself, you'll never know that the description got lost on the way.

A stricter test would be to say:

attrs = attributes_for(:product)
assert_equal attrs, json_response_body.slice(*attrs.keys)

not pretty, but it's more comprehensive.

(Caveat: If your attributes_for method has some random or sequenced elements, you have to do some boring busywork to make sure it's only called once so you can use it for comparison purposes.)

But it's not totally comprehensive, though. Your response should probably also include added attributes like id, which aren't among the ones you specified in the request.

You could simply add a line like:

assert_equal Product.last.id, json_response_body[:id]

but if you have multiple server-computed attributes to check, this gets annoying too.

You could however use your current helpers for that part. I.e. check some attributes against the ones you posted (so you're sure they made it through), and the computed ones against the Product.last record. That'll help guarantee that the response JSON contains everything you expect it to contain.

All that being said, if your JSON becomes even slightly more complex (e.g. includes associated/nested records), the tests become trickier.

Similarly, your JSON could include values you'll have to check in yet another way. Maybe the JSON response contains something that's neither a saved attribute in the record, or a param you sent. Maybe it contains some random element that changes for each request (don't know why it would, but it's possible), you can't compare it to a known value. Instead you'll have to check it in some other way.

I'm pretty happy with this gem which lets you declare a "pattern" with varying degrees of specificity for the JSON you expect. E.g. you can check that the returned name is the specific string you expect, while id is simply an integer since its exact value isn't important but its type and presence are. Give it a look.


Unwanted return values in lambda functions? From a previous question, I got the idea of moving boilerplate code structures into a helper function and passing in a lambda function to it.

I then applied the idea to a data access class (code below).  I'm not sure how to handle the result of my ProcessDatabaseCall helper function; it seems irrelevant to my pattern of use-cases.  In this example I'm returning an IEnumerable; in other cases I'm returning int, School, etc.

    public IEnumerable<School> SchoolsList(string searchChars)
    {
        DataSet ds = new DataSet();
        List<School> schools = new List<School>();
        bool cool = ProcessDatabaseCall("dbo.usp_esrvs_API_SchoolList", cmd => {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@SearchValue", searchChars));

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);

            return true;
        });

        //Foreach row in the data set add a json array object
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            schools.Add(new School((int) row["esrvs_Account_ID"], row["SchoolName"].ToString()));
        }
        return schools;
    }

    private bool ProcessDatabaseCall(string procedureName, Func<SqlCommand,bool> processor)
    {
        using (SqlConnection Mackin1Conn = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand(procedureName, Mackin1Conn))
            {
                return processor(cmd);
            }
        }
    }

The function used to look like this:

public IEnumerable<School> SchoolsList(string searchChars)
    {
        DataSet ds = new DataSet();
        List<School> schools = new List<School>();
        using (SqlConnection Mackin1Conn = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("dbo.usp_esrvs_API_SchoolList", Mackin1Conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@SearchValue", searchChars));
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
            }
        }
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            schools.Add(new School((int) row["esrvs_Account_ID"], row["SchoolName"].ToString()));
        }
        return schools;
    }

Just replace Func delegate with Action delegate if you don't want return value:

private void ExecuteCommand(string commandText, Action<SqlCommand> action)
{
    using (var conn = new SqlConnection(connectionString))
    using (var cmd = new SqlCommand(commandText, conn))
        action(cmd);
}

Then getting schools will look like (with help of LINQ you can get rid of local list of schools):

public IEnumerable<School> GetSchools(string searchChars)
{
    DataSet ds = new DataSet();

    ExecuteCommand("dbo.usp_esrvs_API_SchoolList", cmd => {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@SearchValue", searchChars));
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
    });

    return from row in ds.Tables[0].AsEnumerable()
           select new School(row.Field<int>("esrvs_Account_ID"),
                             row.Field<string>("SchoolName"));
}

NOTE: Consider to use Dapper both methods above are equivalent to:

private IEnumerable<School> GetSchools(string searchChars)
{
    using (var conn = new SqlConnection(connectionString))
        return conn.Query<School>("dbo.usp_esrvs_API_SchoolList",
                                  new { SearchValue = searchChars },
                                  commandType: CommandType.StoredProcedure);
}


Reverse quick sort in Fortran I have written a quick sort routine to inplace sort an array in descending order:

module sort
  contains
    recursive subroutine qsort_self(array)
      real(kind=8)    :: array(:), temp
      integer :: length, pivot, i, j

      i = 0
      j = 0
      length = 0
      pivot = 1
      temp = 0
      length = size(array)
      if (length > 1) then
        i = 2
        j = size(array)
        do while (i <= j)
           do while (array(j) < array(pivot))
            j = j - 1
          enddo
          do while ((array(i) > array(pivot)) .and. (i<=length))
            i = i + 1
          enddo
         if (i > j) exit
          temp = array(j)
          array(j) = array(i)
          array(i) = temp
          i = i + 1
          j = j - 1
        enddo
          temp = array(pivot)
          array(pivot) = array(j)
          array(j) = temp
          pivot = j
          call qsort_self(array(1:pivot-1))
          call qsort_self(array(pivot + 1 : length))
      endif
    end subroutine qsort_self
  end module sort

program p
  use sort
  implicit none
  integer :: size_,i
  real(kind=8), allocatable    :: b6(:)

  size_ = 100
  allocate(b6(size_))
  do i = 1, size_
    b6(i) = real(rand(), 8)
  enddo
  print *, "unsorted: ", b6
  call qsort_self(b6)
  print *,"sorted a= ", b6
end program p

It works fine for now, but without .and. (i<=length) part of code, i run into errors where if array is in ascending order, i goes out of bound. I checked several online implementations, such as this:

https://stackoverflow.com/questions/11196571/quick-sort-sorts-descending-not-ascending

and I believe they suffer from same flaw. Is here any better way of doing it?
I Always want to take first element as pivot because of the array structure.

Errors in the code

This piece

array(i) > array(pivot) .and. i <= length

is not valid code. If i > length you will get an out of bounds access with array(i).
If you rewrite it as

i <= length .and. array(i) > array(pivot)

it is still wrong, because there is no short circuiting in Fortran. Or to be more precise: Fortran operators are neither short-circuit nor eager. The language specification allows the compiler to select the method for optimization. In your case the compiler reorders and short circuits apparently.
The only way to write it reliably correctly is unfortunately:

if (i <= length) then
    if (array(i) > array(pivot)) then
        ...

A declaration with a number literal like real(kind=8) is not portable. You might think, that it is 8 bytes and/or double precision, but the actual integer value for different kinds is implementation dependent. It just happens to be the case, that gfortran and ifort use the kind value 8 to denote double precision, but on another compiler this might be wrong.
You should always use

integer, parameter :: dp = selected_real_kind(15, 307)

if you want to have a certain range and precision or

use, intrinsic :: iso_fortran_env, only: real64

if you want to have a float with 8 bytes.
(Of course these both will be the same on most architectures today.)

There is no implicit none in the module. Strictly speaking this is not an error and you declared all variables correctly, but code without implicit none should be considered faulty.

Major Code improvements

You should always add one of intent(in), intent(out), intent(inout) to the dummy arguments of your procedures.

Fortran arrays can be declared with arbitrary numerical indices. You implicitly assumed in your code, that they start at 1 (which is the default).
If you want your routine to work with differently indexed arrays, it is necessary to query the upper and lower bounds.

do i = lbound(array, dim=1), ubound(array, dim=1)

This makes it also explicit when you really mean the size, and when the bounds.

What I initially wrote is wrong. If you use assumed-shape arrays, the dummy argument becomes again 1-indexed by default.
Had to learn that myself the hard way. Sorry. :-/

I agree you with you that these <40 lines don't require comments for the most parts, but you can write it more readable without comments, by defining a swap subroutine. This would also reduce the clutter of local variables.

If you define a get_pivot function, it will be easy to change to different pivoting strategies.

You have superfluous assignments to length in your code.
IMHO you don't need this variable anyway, because you can insert size(array) at every appearance. Any decent compiler will do common-subexpression-elimination on these calls.

Where possible I would use pure and elemental on your procedures.
They are amazing. ;-)

Architecture

I would always make all names in a module private and export explicitly with public. On the other hand I would only import used names.

In the long run, you want to have a second argument where one can pass a pure logical function that compares two elements. Doing so, the user can sort according to his/her constraints.

Formatting

Thank you for writing Fortran and not FORTRAN code.
If you don't scream at people it is much easier to read your code. ;-)

Of course this is opinion based so, whatever I say, stick to the style of your environment. But for example the whitespace around your binary operators was not consistent. An indentation of two spaces is IMHO too short.

I assume that you are from science and you and your peers use python. In this case it is quite easy to stick to the python formatting guidelines in Fortran.

Minor Code improvements

I agree that the code mostly does not require comments,
but at least public functions of a module should get a docstring on how to use them.

size in the program can be made constant.

The upper and lower bound in array slices can be ommited A(lbound(A, 1) : 5) == A(: 5)

I would not use p as program name. If you do so, you can't use it as variable name anymore.

There is random_number to generate an array of real numbers.

Possible improved version

module sort
    implicit none
    private
    public :: dp, qsort
    integer, parameter :: dp = selected_real_kind(15, 307)

    abstract interface
        logical pure function compare_t(a, b)
            import :: dp
            real(dp), intent(in) :: a, b
        end function
    end interface

contains

    !> @brief
    !>      Sort array inplace according to comparison function comp.
    !>
    !> @details
    !> The comparison function has to be a pure, logical function
    !> that compares two elements of array in a strict partial order.
    !> That means equality has to evaluate to false.
    pure recursive subroutine qsort(array, comp)
        real(kind=dp), intent(inout) :: array(:)
        procedure(compare_t) :: comp

        integer :: pivot, i, j

        i = 0
        j = 0
        pivot = get_pivot(array)
        if (size(array) > 1) then
            i = lbound(array, dim=1)
            j = ubound(array, dim=1)
            do while (i <= j)
                do while (comp(array(i), array(pivot)))
                    i = i + 1
                end do
                do while (comp(array(pivot), array(j)))
                    j = j - 1
                end do
                if (i >= j) exit
                call swap(array(i), array(j))
                i = i + 1
                j = j - 1
            end do
            call swap(array(pivot), array(j))
            call qsort(array(: j - 1), comp)
            call qsort(array(j + 1 : ), comp)
        end if
    end subroutine qsort

    pure subroutine swap(a, b)
        real(dp), intent(inout) :: a, b
        real(dp) :: temp
        temp = a
        a = b
        b = temp
    end subroutine

    pure function get_pivot(array) result(res)
        real(dp), intent(in) :: array(:)
        integer :: res
        res = lbound(array, dim=1)
    end function
  end module sort

program test_qsort_procedures
    use sort, only: dp, qsort
    implicit none
    integer, parameter :: size_ = 100
    real(kind=dp), allocatable :: b6(:)

    allocate(b6(size_))
    call random_number(b6)

    print *, "unsorted: ", b6
    call qsort(b6, increasing)
    print *,"increasing a= ", b6
    call qsort(b6, decreasing)
    print *,"decreasing a= ", b6

    contains

    logical pure function increasing(a, b)
        real(dp), intent(in) :: a, b
        increasing = a < b
    end function

    logical pure function decreasing(a, b)
        real(dp), intent(in) :: a, b
        decreasing = a > b
    end function
end program test_qsort_procedures


Python web page to display SSL certificate status My task was to create a Python script and connect to various TLS sites and grab the expiry dates. I've done this with openssl and ssl modules just fine.

In my mind I was wanting to create a web page as a dashboard.

I've done that.

It's as simple as it gets I think (which I am happy with):

But I ended up doing a for loop of the dictionary I stored the values for expiration, url, and status.

I.E

with open(htmlfile,'a') as fh:

        for urlandport,data in urldict.items():
            url,tcpport = urlandport
            msg,status = data
            #print(url)
            if status == 'OK':
                delta      = data[0] - now
                days_left  = str(abs(delta.days))
                color      = 'green'  if int(days_left) > 400 \
                        else 'orange' if int(days_left) < 100 \
                        else 'red'
                sclr       = 'green'
            else:
                days_left  = 'NA'
                expiration = 'NA'
                color      = 'blue'
                sclr       = 'blue'
                msg,status = status,msg
                #Swap dashboard order on failure
            fh.write(
                     "
" + "" + url + " " + str(tcpport) + "	"     + \
                     ""+days_left+""+"	" + \
                     ""+ str(msg)+""+"	" + \
                     ""+str(status)+""+"	"+"
"
                     )

In my mind this does not seem very modern or flexible to proceed in manually cobbling HTML like this. It works. But I am wondering if I should invest in learning something more suitable for scalability.

I was also also to separate out the certs into 4 different tables: soon to expire, OK for now, connect failure and too old to bother and display then in a particular order for the user.

Hence, I ended up just creating 4 lists and appending to them so I could loop over each as I chose.

It just feels like I wrote some eye searing python to cobble something together...I am not even sure if a "Web Framework" would help or frankly why...I've looked into some of them but it's not sinking in.

Any suggestions?

It just feels like I wrote some eye searing python to cobble something together...I am not even sure if a "Web Framework" would help or frankly why...I've looked into some of them but it's not sinking in.

I think it would definitely be better then what you have currently

I can recommend flask, a small web microframework.

Pretty easy to use and has plenty of features, it will handle serving the page, making nicely formatted html pages, handling routes.

If you want some pretty tables you could even use Flask-Table

But something simple as this should even work,

app.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def ssl_status():
    ssl_connections = [
        ["url.url", 165, "some-date-2019", "OK"]
    ]
    return render_template('ssl_status.html', ssl_connections=ssl_connections)

if __name__ == '__main__':
    app.run()

ssl_status.html

<!doctype html>
<title>SSL Status page</title>

    {% for row in ssl_connections %}

        {% for item in row %}
        {{ item }}
        {% endfor %}

    {% endfor %}

You'd need some .css probably to make the colors work, and some other stuff to make it look the same as your example.


LZW Compression Library I wrote a library that implements LZW compression and decompression. A goal of this project was to help me acquaint myself with modern C++ development practices (I primarily come from a Java background and have a smattering of C experience).
I want to use this library to compress data and stream it over TCP sockets to be decompressed by the recipient, all without storing a compressed version of the full data on either the sender or the recipient's machine (for hobby/non-production purposes).
lzw.hpp
#pragma once

#include <iostream>
#include <optional>
#include <unordered_map>
#include <vector>

namespace lzw {

class lzw_encoder {
public:
  lzw_encoder(std::istream &is, std::ostream &os);

  void encode();

private:
  uint32_t current_code = 0;
  std::string current;

  std::unordered_map<std::string, uint32_t> codebook;
  std::istream &is;
  std::ostream &os;
};

class lzw_decoder {
public:
  lzw_decoder(std::istream &is, std::ostream &os);

  void decode();

private:
  std::vector<std::string> codebook;
  std::optional<uint32_t> prev;
  std::istream &is;
  std::ostream &os;
};
} // namespace lzw

lzw.cpp
#include "lzw.hpp"

namespace lzw {

static constexpr size_t ENCODER_BUFFER_SIZE = 256;

static constexpr size_t DECODER_BUFFER_SIZE = 64;

lzw_encoder::lzw_encoder(std::istream &is, std::ostream &os)
    : is(is), os(os), current_code(0) {
  for (current_code = 0; current_code < 256; ++current_code) {
    codebook[std::string(1, static_cast<char>(current_code))] = current_code;
  }
}

void lzw_encoder::encode() {
  char buffer[ENCODER_BUFFER_SIZE];

  while (true) {
    is.read(buffer, ENCODER_BUFFER_SIZE);
    auto read_length = is.gcount();
    if (read_length == 0)
      break;

    for (size_t i = 0; i < read_length; ++i) {
      current.push_back(buffer[i]);

      auto iter = codebook.find(current);
      if (iter == codebook.end()) {
        codebook[current] = current_code++;

        current.pop_back();
        auto code_val = codebook[current];
        os.write(reinterpret_cast<char *>(&code_val), sizeof(code_val));

        current.clear();
        current.push_back(buffer[i]);
      }
    }
  }
  if (current.size()) {
    auto code_val = codebook[current];
    os.write(reinterpret_cast<char *>(&code_val), sizeof(code_val));
  }
}

lzw_decoder::lzw_decoder(std::istream &is, std::ostream &os)
    : is(is), os(os), prev{} {
  for (int i = 0; i < 256; ++i) {
    codebook.emplace_back(1, static_cast<char>(i));
  }
}

void lzw_decoder::decode() {
  uint32_t buffer[DECODER_BUFFER_SIZE];
  while (true) {
    is.read(reinterpret_cast<char *>(buffer),
            DECODER_BUFFER_SIZE * sizeof(uint32_t));
    auto read_length = is.gcount() / sizeof(uint32_t);
    if (read_length == 0)
      break;

    for (size_t i = 0; i < read_length; ++i) {
      if (buffer[i] < codebook.size()) {
        os << codebook[buffer[i]];
        if (prev) {
          codebook.push_back(codebook[*prev] + codebook[buffer[i]].front());
        }
      } else {
        codebook.push_back(codebook[*prev] + codebook[*prev].front());
        os << codebook.back();
      }
      prev = buffer[i];
    }
  }
}
} // namespace lzw

I plan on replacing the unordered_map in the lzw_encoder with a dictionary trie in a future edit.
Does my code exhibit a reasonable way to use io streams?
I feel that my usage of read and write did not have a feeling of modern C++, and I'm wondering if I am unaware of some standard library tools to help me with binary io. In particular, I don't like that I used
while(true) instead of some condition related to the input streams. Also, I was wondering if there was a way to do binary io without using reinterpret_cast to cast numeric/binary data pointers to char *.

Here are some things I see that may help you improve your code.
Shouldn't a compressed file be smaller?
Imagine my surprise when I discovered that a 2037-byte file (the lzw.cpp source code itself) became 3524 bytes when "compressed!"  The original LZW algorithm encoded 8-bit values into 12-bit codes.  This appears to be encoding 8-bit values as 32-bit codes which is unlikely to offer much compression for short files like this.  I did, however, try it on the plain text version of Bram Stoker's Dracula and, as expected, the resulting file was about 75% of the size of the original.  Because it's a stream and you don't have access to the length of the source, there may not be much you can do about it, but it's probably a good thing to warn potential users about.
Rethink the interface
In order to use the compression, one must first create an object and then use it, perhaps like this:
lzw::lzw_encoder lzw(in, out);
lzw.encode();

Wouldn't it be nicer to just be able to do this?
lzw::encode(in, out);

Write member initializers in declaration order
The lzw_encoder class has this constructor
lzw_encoder::lzw_encoder(std::istream &is, std::ostream &os)
    : is(is), os(os), current_code(0) {
  for (current_code = 0; current_code < 256; ++current_code) {
    codebook[std::string(1, static_cast<char>(current_code))] = current_code;
  }
}

That looks fine, but in fact, current_code will be initialized before is and os because members are always initialized in declaration order and current_code is declared before is in this class. To avoid misleading another programmer, you could simply omit current_code since it is already initialized by the declaration:
uint32_t current_code = 0;

Use standard algorithms where appropriate
Initializing the codebook uses this:
for (current_code = 0; current_code < 256; ++current_code) {
  codebook[std::string(1, static_cast<char>(current_code))] = current_code;
}

This can be improved in a number of ways.  First, we already know how large the codebook will be so we can reduce the number of memory reallocations by telling the compiler that information:
codebook.reserve(256);

Next, we can avoid the cast and gain a bit of efficiency by using emplace:
for (current_code = 0; current_code < 256; ++current_code) {
    codebook.emplace(std::string(1, current_code), current_code);
}

I'd also recommend replacing 256 here with a static constexpr initial_codebook_size.
Beware of endian-ness differences
The code currently contains these lines:
auto code_val = codebook[current];
os.write(reinterpret_cast<char *>(&code_val), sizeof(code_val));

There problem is that depending on whether this is a big-endian or little-endian machine, the encoding will be different.  If the compressed stream is intended to be sent to a different machine, this needs to be consistent.  Consider using something like the POSIX htonl function here.
Consider restructuring loops
The problem with while(true) is that it hides the loop exit condition.  Instead of this:
while (true) {
    is.read(buffer, ENCODER_BUFFER_SIZE);
    auto read_length = is.gcount();
    if (read_length == 0)
      break;
    // etc
}

Consider something like this:
while (is.read(buffer, ENCODER_BUFFER_SIZE)) {
    // handle full block
}
if (is.gcount()) {
    // handle final partial block
}

Understand the use of streams
It's possible that the caller has set one or both streams to throw an exception on encountering a failure such as end of file on read.  Either override this or handle it appropriately.
Consider adding convenience functions
The handling of blocks for encode and for decode could both be made into functions within the namespace.  This would make the restructuring of loops as mentioned above a bit easier and cleaner and would isolate the handling of the data structures from basic stream I/O.  That may make things a bit easier when you convert to a trie.  Here's my rewrite of the loop:
while (is.read(buffer, ENCODER_BUFFER_SIZE)) {
  encode_buffer(buffer, ENCODER_BUFFER_SIZE);
}
encode_buffer(buffer, is.gcount());


Manipulating URL parameters I am new in JS, I am writing some extra filters on the Interface. So extra filter needs to add or update the parameter in the URL.

Option 1. No parameter in existing URL:

url = https://custom.net/

We want to add new parameter in the URL where key issector and value is IndependentConsultingDoctors

Output will be: https://custom.net/?sector=IndependentConsultingDoctors

Option 2. Update value in parameter of existing URL

URL = https://custom.net/?filter=title&sector=1234&sortby=1&page=1

We want to update parameter in the URL where key issector and value is IndependentConsultingDoctors

Output will be: URL = https://custom.net/?filter=title&sortby=1&page=1&sector=IndependentConsultingDoctors

Option 3. Delete and add parameter:

URL = https://custom.net/?filter=title&reportype=1234&sortby=1&page=1

We want to add new parameter in the URL where key issector and value is IndependentConsultingDoctors and delete reportype and  inspectiontype parameters.

Output will be: URL = https://custom.net/?filter=title&sortby=1&page=1&sector=IndependentConsultingDoctors

The following is my code:

$(this).find('.btn').click(function(){
    //var pathname = window.location.href;
    var pathname = "https://abc.com/?filter_by=title&filter_condition=&sector=1234&reportype=3455&sortby=1&page=1&itemperpage=10&input_list=&current_selected_option=2&masterfilter=%26itemperpage%3D10%26filter_by%3DAll%26filter_condition%3D%26sortby%3D1%26from_date%3D%26to_date%3D&from_date=&to_date="
    // Key and Value which need to add in paramaetrs
    var key = "sector"
    var value = "Acutes";
    // Delete key from the parameters
    var delete_key = ["reportype", "inspectiontype"];

    // Split URL BASE and Get Parameters
    split_href = pathname.split("?");

    //No parameter present
    if (split_href.length==1) {
         window.location.href = split_href[0]+'?'+key+'='+value;
    }else{
        // Create dictionary of parameter
        var para_dict = {};
        var para_list = split_href[1].split("&");
        for (ii in para_list){
            var tmp = para_list[ii].split("=");
            para_dict[tmp[0]] = tmp[1];
        }
        // Add target kay and Value
        para_dict[key] = value;

        // Delete key from the dinctionary
        for (ii in delete_key){
            delete para_dict[delete_key[ii]];
        }
        //Create New Parameter string
        var new_para = "?";
        for (ii in para_dict){
            new_para = new_para+ii+"="+para_dict[ii]+"&";
        }
        console.log(split_href[0]+new_para);
    }

});

Can you review my code and give me a more optimized solution?

It's good to follow the single responsibility principle. The current method does many things that would make sense to extract to smaller helper functions, for example:

convert query string to dictionary
concert dictionary to query string
delete list of keys from dictionary
... and so on

What's the point? Next time you need something similar (and most certainly you will), you will be able to reuse the common elements easier.

And yeah, as @Quill said, the names are horrible. Some ideas:

params instead of para_dict
index instead of ii
key_value instead of tmp
url instead of pathname


Simple Sieve of Eratosthenes in Python 3 So yet another Sieve of Eratosthenes in Python 3.

The function returns a list of all primes smaller but not equal max_n.

The motivation is, as a practice, a simple implementation of the algorithm that is faithful, short, readable and transparent, while still getting a reasonable performance.

def primes(max_n):
    """Return a list of primes smaller than max_n."""

    sieve = [True] * max_n

    # p contains the largest prime yet found.
    p = 2

    # Only for p < sqrt(max_n) we check,
    # i.e. p ** 2 < max_n, to avoid float issues.
    while p ** 2 < max_n:

        # Cross-out all true multiples of p:
        for z in range(2 * p, max_n, p):
            sieve[z] = False

        # Find the next prime:
        for z in range(p + 1, max_n):
            if sieve[z]:
                p = z
                break

    # 0 and 1 are not returned:
    return [z for z in range(2, max_n) if sieve[z]]

IMHO it would be preferable to avoid p ** 2 < max_n and instead use p < max_n ** 0.5. Can we do this? It surprisingly seems to work as long as max_n ** 0.5 fits into the float mantissa, even if max_n doesn’t.

The second for-loop doesn’t look very nice with the break but I don’t have any idea how to do it otherwise…

Do you have any suggestions?

Are there still any simplifications possible? Or non-hackish ways to increase performance?

The second for-loop doesn’t look very nice with the break ...

You can remove that for-loop if you test p for being prime in the while-loop:

while p ** 2 < max_n:
    if sieve[p]:
        # p is prime: cross-out all true multiples of p:
        for z in range(2 * p, max_n, p):
            sieve[z] = False
    p += 1

  Are there still any simplifications possible? Or non-hackish ways to increase performance?

Here are two simple changes which increase the performance:
First, since 2 is the only even prime, you can increment p by two after the first iteration:

    p = p + 2 if p > 2 else 3

Second, it suffices to “cross-out” the multiples of p starting at p*p (instead of 2*p) because all lesser multiples have been handled before:

        for z in range(p * p, max_n, p):
            sieve[z] = False


My minesweeper implementation in Javascript I'm beginner coder and I would like to request for critique review of my minesweeper implementation. Game was made using bootstrap 3 and game board itself is a simple html DOM table. Game is complete and working but I feel it could be written in more simple and elegant manner. What in my code can be improved/written in different/better way? In game page you can choose size of board, then you have to click "start" to generate board and start the game. Ctrl + click on cell to flag it and prevent accidental click.

Main function responsible for revealing cells:

function reveal(ev){
  var x = parseInt(this.getAttribute('x')),
      y = parseInt(this.getAttribute('y'));

  if(ev.ctrlKey == false){
    if(cells[getFieldId(x,y)].markedByPlayer == false){

      if(cells[getFieldId(x,y)].hasBomb == true){
        document.getElementById(x+'x'+y).innerHTML = '*';
        alert('Bomb! You have lost.');
        gameState = 'loss';
        revealMap();
      }else if(cells[getFieldId(x,y)].neighbourNumber > 0 && cells[getFieldId(x,y)].hasBomb != true){
        document.getElementById(x + 'x' +y).innerHTML = cells[getFieldId(x,y)].neighbourNumber;
        cells[getFieldId(x,y)].hasBeenDiscovered = true;
        pointColor(x,y,'midnightblue');
        document.getElementById(x+'x'+y).style.color = 'silver';
        revealFields(x,y);
      }else{
        revealFields(x,y);
      }

      if(checkVictoryCondition() == bombsNumber){
        alert('You have won!');
        revealMap();
      }
    }
  }else if(ev.ctrlKey == true){

    if(cells[getFieldId(x,y)].markedByPlayer == false){
      document.getElementById(x+'x'+y).innerHTML = '!';
      cells[getFieldId(x,y)].markedByPlayer = true;
      document.getElementById(x+'x'+y).style.color = 'red';
    }else if(cells[getFieldId(x,y)].markedByPlayer == true){
      document.getElementById(x+'x'+y).innerHTML = '';
      cells[getFieldId(x,y)].markedByPlayer = false;
    }
  }
}

function revealFields(x,y){
  if(x<0 || y<0 || x>boardHeight - 1 || y>boardWidth - 1){
    return;
  }
  if(cells[getFieldId(x,y)].neighbourNumber > 0){
    document.getElementById(x+ 'x' +y).innerHTML = cells[getFieldId(x,y)].neighbourNumber;
    cells[getFieldId(x,y)].hasBeenDiscovered = true;
    pointColor(x,y,'midnightblue');
    document.getElementById(x+ 'x' +y).removeEventListener('click', reveal, true);
  }
  if(cells[getFieldId(x,y)].hasBeenDiscovered == true){
    return;
  }
  cells[getFieldId(x,y)].hasBeenDiscovered = true;
  pointColor(x,y,'midnightblue');
  document.getElementById(x+ 'x' +y).removeEventListener('click', reveal, true);

  setTimeout(function(){revealFields(x-1,y);}, 200);
  setTimeout(function(){revealFields(x+1,y);}, 200);
  setTimeout(function(){revealFields(x,y-1);}, 200);
  setTimeout(function(){revealFields(x,y+1);}, 200);
  setTimeout(function(){revealFields(x-1,y-1);}, 200);
  setTimeout(function(){revealFields(x-1,y+1);}, 200);
  setTimeout(function(){revealFields(x+1,y-1);}, 200);
  setTimeout(function(){revealFields(x+1,y+1);}, 200);
}

JS fiddle link: https://jsfiddle.net/pL1n8zwj/1/

Here are my personal thoughts:

Naming:

it's probably better to have placeBombs instead of initBombs, because you have a function called placeBomb.
Sometimes you use i and j , others you use x and y, it's better to be consistent.

Structure:

You should have a state field with constants indicating the state of the cell instead of various fields set to true or false.
you don't really need to look for the value inside of an hash to get the value of a cell. You can have a two dimensional array and reference the cell directly. This also means that you can remove the getFieldId function.

Something like this, for example:

var states = {
    MARKEDBYPLAYER: 1,
    DISCOVERED: 2,
    UNTOUCHED: 3
};

var boardSize = 16;
var cells = new Array(boardSize);

for(var x=0; x < boardSize; x++) {
    cells[x] = new Array(boardSize);
    for(var y=0; y < boardSize; y++) {
        cells[x][y] = {};
        cells[x][y].state = states.UNTOUCHED;
        cells[x][y].hasBomb = true;
        cells[x][y].neighbourNumber = null;
  }
}

if (cells[0][1].hasBomb) {
    console.log('Player has lost')
}

In generateBoard you have a bunch of instruction that would read better if you had just a call to a function that sets the required values.

Something like this:

function setDomCell(domCell, x, y, cellSize, cellFontSize) {
  domCell.id = x+'x'+y;
  domCell.style.width = cellSize;
  domCell.style.height = cellSize;
  domCell.style.fontSize = cellFontSize;
  domCell.setAttribute('x', x);
  domCell.setAttribute('y', y);
  domCell.addEventListener('click', reveal, true);
}

function generateBoard(){
  var domRow, domCell;

  for(var y=0; y<boardHeight; y++){
    domRow = document.createElement('tr');
    domBoard.appendChild(domRow);
    for(var x=0; x<boardWidth; x++){
      domCell = document.createElement('td');
      setDomCell(domCell, x, y, cellSize, cellFontSize);
      domRow.appendChild(domCell);
    }
  }
}

It would be even better if you had on object to take care of that, but take it one step at a time.

In the start function you have a switch to lookup values based on another value.

I'd say that's what a hash is for:

  var sizeVars = {
    'small': {
      boardWidth: 10,
      boardHeight: 10,
      cellSize: '48px',
      cellFontSize: '32px',
      bombsNumber: 16
    },
    'medium': {
      boardWidth: 20,
      boardHeight: 20,
      cellSize: '32px',
      cellFontSize: '16px',
      bombsNumber: 70
    },
    'large': {
      boardWidth: 30,
      boardHeight: 30,
      cellSize: '16px',
      cellFontSize: '8px',
      bombsNumber: 160
    }
  }

  var chosenSize = document.getElementById('size').value;
  console.log(sizeVars[chosenSize].boardWidth);


Wrapper for dynamic behavior I am writing a wrapper for dynamic behavior in C#. If I use GetMethod(), then an exception is thrown when there is more than one method (overloads).  I figure it will become apparent at runtime that if there is an argument mismatch anyway.  Is this a good way to solve the problem?

public class StaticMembersDynamicWrapper : DynamicObject
{
  private Type _type;

  public StaticMembersDynamicWrapper(Type type) { _type = type; }

  // Handle static methods
  public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
  {
     var methods = _type
        .GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public)
        .Where(methodInfo => methodInfo.Name == binder.Name);

     var method = methods.FirstOrDefault();
     if (method != null)
     {
        result = method.Invoke(null, args);
        return true;
     }

     result = null;
     return false;
  }
}

The order the method infos are returned in dose not appear to be a defined one, I would suggest filtering on argument types to avoid the risk of it all crumbling to dust when the order changes.

Also you may want to consider the case when a method is defined on both the base class and the subclass.

class A
{
    public static void DoAwsomeStuff()
    {
        // really awsome stuff.
    }
}

class B : A
{
    public static void DoAwsomeStuff()
    {
        // different but still awsome stuff.
    }
}

Hiding static methods like this should probably be frowned upon, but all the same I would suggest selecting the method with the most specific declaring type. Just to be safe.

MethodInfo method = null;

foreach(var current in methods)
{
    if(method == null || current.DeclaringType.IsAssignableFrom(method.DeclaringType))
    {
        method = current;
    }
}

Edit:

Another possibility worth exploring is the use of Type.InvokeMember(). This already takes the argument types and method hiding into account for you.

try
{
    result = _type.InvokeMember(
        binder.Name,
        BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.InvokeMethod,
        null,
        null,
        args
        );

    return true;
}
catch(MissingMethodException)
{
    result = null;
    return false;
}


Consumer producer implementation involving POSIX, semaphores, and locks I am trying to get over my fear of multithreading programming and teaching myself to code using POSIX. I wrote a small version of the consumer producer problem. I am hoping I can get some feedback if it works (I think it does, as I traced the threads when my for loop was at 20 and it made sense) and what I should do to improve my code.

The semaphore starts at 4, the consumer waits one second, and then consumes a semaphore.  The producer waits between 0 and 5 seconds, then produces (increments) the semaphore value. The consumer should wait if the semaphore is at 0 until the producer frees up the resources.

#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

sem_t my_semaphore;
pthread_mutex_t count_mutex;
void * consumerUse(void * ptr);
void * producerUse(void * ptr);

int main() {

    int value;
    pthread_t consumer, producer;
    int valConsumer =0;
    int valProducer =0;

    sem_init(&my_semaphore, 0, 4);
    sem_getvalue(&my_semaphore, &value);
    printf("The initial value of the semaphore is %d\n", value);

    for(int i=0; i < 100; i++)
    {

        valConsumer = pthread_create(& consumer, NULL, consumerUse, (void*)(intptr_t) value);
        if(valConsumer != 0)
        {
            fprintf(stderr, "Error - pthread create () return code: %d\n", valConsumer);
            exit(EXIT_FAILURE);
        }

        valProducer = pthread_create(& producer, NULL, producerUse, (void*) (intptr_t)value);
        if(valProducer != 0)
        {
            fprintf(stderr, "Error - pthread create () return code: %d\n", valProducer);
            exit(EXIT_FAILURE);
        }

    }
    pthread_join( consumer, NULL);
    pthread_join( producer, NULL);
    printf("End program\n");
    return 0;
}

void *consumerUse(void * ptr)
{
    sleep(1);
    int value;
    sem_getvalue(&my_semaphore, &value);
    printf("Before wait: %d\n", value);
    pthread_mutex_lock(&count_mutex);
    sem_wait(&my_semaphore);
    pthread_mutex_unlock(&count_mutex);
    sem_getvalue(&my_semaphore, &value);
    printf("Consumer semaphore value: %d\n", value);
    return 0;
}

void *producerUse(void * ptr)
{

    int pause = rand() % 5;
    sleep(pause);
    int value;
    sem_getvalue(&my_semaphore, &value);
    printf("Before post: %d\n", value);
    if(value < 4)
    {

        sem_post(&my_semaphore);
        sem_getvalue(&my_semaphore, &value);
        printf("Producer semaphore value: %d\n", value);
    }
    return 0;
}

General note

It is very hard to tell if this program works or not: I don't know what is supposed to be a correct behaviour. I can only speculate that you didn't want to see a semaphore value ever to exceed 4. If it is the case, than it is a pure coincidence -- caused by the large delays (try to remove sleep()). Absolutely nothing in the code prevents two or more producers to "simultaneously" test the value, and proceed to post the semaphore.

Particular problems

There is no reason to serialize consumers' access to a sem_wait(). In a sense it defeats the purpose of the semaphore.

stdio is not thread safe. The fact that printouts haven't messed up is another coincidence, also caused by the sleep() delays.

The code creates a multitude of threads, but joins only two. Not a problem in a toy program; a bad resource leak in general.

A minor note: since the thread functions ignore the argument, you should pass NULL. Passing anything else (like (void *) (intptr_t) value) is misleading.


Computing sum on column + sum on row + sum on each diagonal I have the following problem:
I read from a file the dimension(n) of the matrix(which is n * n) and the matrix.
For each element in the matrix I have to compute sum of row + sum of column + sum of the diagonal which the element is on and is parallel to main diagonal + sum of the diagonal which the element is on and is parallel to antidiagonal, and find the element with max corresponding sum and coordinates(row and column) on which the element is found. Element itself should be taken into count just once.

I have a working program, but I need it to be faster. How could I solve this problem faster? Can my algorithm be improved or I have to write another one? 2 ≤ n ≤ 300, -10000 ≤ m[i][j] ≤ 10000

Input file:

3
 1  -8  3
-2   4  5
 6  -9  7

Output file:

18
0 2

Code:

#include <set>
#include <iostream>
#include <fstream>
#include <limits>

using namespace std;
int sumOnRow(int row, int n, int **m)
{
    int i;
    int sum = 0;
    for (i = 0; i < n; i++)
        sum += m[row][i];
    return sum;
}

int sumOnColumn(int column, int n, int **m)
{
    int i;
    int sum = 0;
    for (i = 0; i < n; i++)
    {
        sum += m[i][column];
    }
    return sum;
}

int sumOnAD(int i, int j, int n, int **m)
{
    int a = i - 1;
    int b = j + 1;
    int sum = 0;
    while ((a >= 0) && (b < n))
    {
        sum += m[a--][b++];
    }
    a = i + 1;
    b = j - 1;
    while ((a < n) && (b >= 0))
    {
        sum += m[a++][b--];
    }
    return sum;
}

int sumOnMD(int i, int j, int n, int **m)
{
    int a = i + 1;
    int b = j + 1;
    int sum = 0;
    while ((b < n) && (a < n))
    {
        sum += m[a++][b++];
    }
    a = i - 1;
    b = j - 1;
    while ((a >= 0) && (b >= 0))
    {
        sum += m[a--][b--];
    }
    return sum;
}

int main()
{
    fstream in;
    in.open ("inputFile.in");
    if (!in)
    {
        cerr << "Can't open input file\n";
    }
    fstream out;
    out.open ("outputFile.out", ios::out);
    if (!out)
    {
        cerr << "Can't open output file\n";
    }

    short n;
    in >> n;

    int i;
    int **m = new int *[n];
    for (i = 0; i < n; i++)
        m[i] = new int [n];

    int j;
    //read the matrix
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            in >> m[i][j];
        }
    }

    int elementSum = 0;
    int maxSum = INT_MIN;
    int savedI = 0;
    int savedJ = 0;

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            elementSum += sumOnRow(i, n, m);
            elementSum += sumOnColumn(j, n, m);
            elementSum += sumOnMD(i, j, n, m);
            elementSum += sumOnAD(i, j, n, m);
            //element was taken into count both in the sum on row and the sum on column
            elementSum -= m[i][j];
            if (elementSum > maxSum)
            {
                maxSum = elementSum;
                savedI = i;
                savedJ = j;
            }
            elementSum = 0;
        }
    }
    out << maxSum << "\n" << savedI << " " << savedJ;
    return 0;
    }

For every element, you currently sum \$n\$ elements for the row, column, diagonal and anti-diagonal, so \$4*n\$ elements.
That's a total of \$4*n^3\$ adds.

That can be improved on easily by pre-processing:

Build an array of the sums for all rows, columns, diagonals and anti-diagonals. You need \$4*n\$ space and \$4*n^2\$ adds.
Add the proper sums for each element, that's \$4\$ adds per element, for a total of \$4*n^2\$ adds.
Together, that's \$8*n^2\$ adds.

In conclusion, you can trade \$4*n\$ extra space for a speed-up by a factor of \$n/2\$.

Regarding your current code:

I cannot find anyplace you use something from <set>. So, why include it?
You should avoid using namespace std; It's a plague and just biding its time to bite you later.
Why is “using namespace std;” considered bad practice?

You can import selected identifiers with using std::cout;, and qualify others with std::cin.
You know you can introduce a variable in the initializer-part of the for-loop?
There's no need for a separate step to open a fstream. User the right ctor.
You noticed an error, you wrote to stderr about it, so why do you try to muddle on instead of aborting?
You never check whether input failed. Are you completely sure the input is well-formed?
Consider merging your allocations where possible. new and delete are quite costly after all.
return 0; is implicit in main().
You never free the memory you allocated. That's actually a good thing as cleaning up just for the OS to take everything down is wasted effort. Beware false positives in leak-detectors though.
I suggest a comment // m and m[0] to m[n - 1] intentionally left allocated though, in case anyone (else) later pokes around and might conceivably adopt it into a greater whole. Also shows you considered it.
You might consider investing into a class for square matrices. It's quite easy to build one.


Webpack loader for Phaser.io asset files The following is a webpack loader for Phaser asset files.  The way this is supposed to work is that it looks at the json string, and finds all of values with "url" keys in the json object, and transforms that value to a function call with that value as the parameter.  I need to assume any level of nesting of the json values.

module.exports = function(source){
  this.cacheable();  //comment this line out to run standalone outside webpack loader context
  var s = source;
  var re = /"url":\s*\".*,/g;
  var m;
  do {
    m = re.exec(source);
    if (!!m) {
      var path = m[0].split(':')[1].trim().slice(0, -1);
      s = s.replace(path, 'require(' + path + ')');
   }
  } while (m);
  return s;
}

For example, the source could be the following as the parameter to the function (as a string)

{
    "level1": [{
            "type": "image",
            "key": "starwars",
            "url": "assets/pics/unknown-the_starwars_pic.png",
            "overwrite": false
        },{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ],
    "level2": [{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ]
}

and the return value would be a string of the following

{
    "level1": [{
            "type": "image",
            "key": "starwars",
            "url": require("assets/pics/unknown-the_starwars_pic.png"),
            "overwrite": false
        },{
            "type": "image",
            "key": "spaceship",
            "url": require("assets/pics/spaceship.png"),
            "overwrite": false
        }
    ],
    "level2": [{
            "type": "image",
            "key": "spaceship",
            "url": require("assets/pics/spaceship.png"),
            "overwrite": false
        }
    ]
}

How might I implement this in a more clear way that accounts for any nesting of the json objects?

Your current implementation doesn't work!

Look at this snippet, where I extracted the standalone function:

function exports(source) {
  var s = source;
  var re = /"url":\s*\".*,/g;
  var m;
  do {
    m = re.exec(source);
    if (!!m) {
      var path = m[0].split(':')[1].trim().slice(0, -1);
      s = s.replace(path, 'require(' + path + ')');
   }
  } while (m);
  return s;
}
var source = {
      "level1": [{
            "type": "image",
            "key": "starwars",
            "url": "assets/pics/unknown-the_starwars_pic.png",
            "overwrite": false
        },{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ],
    "level2": [{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ]
};
document.write(exports(JSON.stringify(source)).replace(/overwrite/g, '$&'));

The result is that the overwrite key following the url one is part of the require(...).
It comes from the way you expressed the regexp:

/"url":\s*\".*,/

Since the regexp is greedy the ending .*, consumes almost the whole string, up to the very last ,. Then when you apply split(':') on it, you keep the overwrite attached at the end of your path.

To fix this issue, you'd better to use this regexp:

/"url":\s*\"[^"]*",/

Here is the same snippet, now with this new regexp:

function exports(source) {
  var s = source;
  var re = /"url":\s*"[^"]*",/g;
  var m;
  do {
    m = re.exec(source);
    if (!!m) {
      var path = m[0].split(':')[1].trim().slice(0, -1);
      s = s.replace(path, 'require(' + path + ')');
   }
  } while (m);
  return s;
}
var source = {
      "level1": [{
            "type": "image",
            "key": "starwars",
            "url": "assets/pics/unknown-the_starwars_pic.png",
            "overwrite": false
        },{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ],
    "level2": [{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ]
};
document.write(exports(JSON.stringify(source)).replace(/overwrite/g, '$&'));

Well, as expected the overwrite is not included in the require(...).
But... now the require(...) itself is duplicated on the 2nd url occurrence, and omitted on the 3d one!

I din't try to look deeper on this second issue, because with the newly expressed regexp we clearly can do the job much more simply, like this:

function exports(source) {
  return source.replace(/"url"\s*:\s*"([^"]*)"/g, function(match, path) {
    return '"url":require("' + path + '")';
  });
}

Here is this version working:

function exports(source) {
  return source.replace(/"url"\s*:\s*"([^"]*)"/g, function(match, path) {
    return '"url":require("' + path + '")';
  });
}
var source = {
      "level1": [{
            "type": "image",
            "key": "starwars",
            "url": "assets/pics/unknown-the_starwars_pic.png",
            "overwrite": false
        },{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ],
    "level2": [{
            "type": "image",
            "key": "spaceship",
            "url": "assets/pics/spaceship.png",
            "overwrite": false
        }
    ]
};
document.write(exports(JSON.stringify(source)).replace(/overwrite/g, '$&'));


Compute power set of a given set in Python Problem:

  Given a set (let's say of integers), return the list of all of its subsets also known as the power set.

It looks to be working, but I would like feedback on the algorithm and Python coding style and techniques as I am new to Python.

def get_power_set(s):

    empty_set = set()
    power_set = [empty_set]

    for elem in s:
        # Need this temporary list because we can't change the power_set list while iterating it. Or so I think.
        new_sets = []
        for power_set_item in power_set:
            new_set = set().union(power_set_item) # Is this the best way to copy set?
            new_set.add(elem)
            new_sets.append(new_set)
        power_set.extend(new_sets)

    return power_set

Find the appropriate datatype for your problem.

This is actually a problem in the task.
The power set is by definition a set. You use even the variable name power_set so actually it is quite surprising, that it is a list and not a set.

Redundant naming:

It is obvious, that set() is an empty set:
Better use: power_set = [set()]

Don't abbreviate if it is not necessary:

You could write for element in s

Names:

Naming stuff is the hardest part. But I would call a power_set_item simply a subset.

Operators

This is open for debate and perhaps just my taste, but I would use | instead of the union method. This also means to use |= and += instead of update and extend.

Copy

In general the best way to copy is to call the copy method:
new_set = power_set_item.copy().
But in your case it is important to note, that | and .union return a new set, so you could write:

new_sets.append(subset | {element})

All points combined yield the following code:

def get_power_set(s):

    power_set = [set()]

    for element in s:
        new_sets = []
        for subset in power_set:
            new_sets.append(subset | {element})
        power_set.extend(new_sets)

    return power_set

List Comprehension:

As you wrote in your comment, you don't like the new_sets variable. You can easily get rid of it, by using list comprehensions.
In general if you have code like:

A = []
for j in B:
    x = do_stuff(j)
    A.add(x)

Always check if you can't write it in the following way:

A = [do_stuff(j) for j in B]

This gives now the following improvement:

def get_power_set(s):

    power_set = [set()]

    for element in s:
        one_element_set = {element}
        power_set += [subset | one_element_set for subset in power_set]

    return power_set

The line with one_element_set = {element} is to prevent the potentially costly repeated creation of a new object.

Datatype

To come back to the first point. If we take the previous code we can easily change it, to return a set of frozensets:

def get_power_set(s):

    power_set = {frozenset()}

    for element in s:
        one_element_set = frozenset({element})
        power_set |= {subset | one_element_set for subset in power_set}

    return power_set


Connect Four: Bitboard checking algorithm I'm rather new to Clojure and so I decided to program a Connect Four for fun and learning.

The code below is a Clojure implementation of this bitboard algorithm.

The whole code can be found here: https://gist.github.com/3639220

What I'm unsure about are the functions (insert) and (bit-check-board):

(defn insert
  "Inserts symbol for given player (either 1 or 2) at specified x and
   calls bit-insert for corresponding bitboard."
  [boards x player-num]
  (let [y (get-y (boards 0) x)]
    (if (= player-num 1) ; TODO: improve
      (vector
       (assoc-in (boards 0) [y x] (player player-num))
       (bit-insert (boards 1) (- 5 y) x)
       (boards 2))
      (vector
       (assoc-in (boards 0) [y x] (player player-num))
       (boards 1)
       (bit-insert (boards 2) (- 5 y) x)))))

(defn bit-check-board ; TODO: improve
  "Checks whether given bitboard is a win."
  [bitboard]
  (let [diag1 (bit-and bitboard (bit-shift-right bitboard 6))
        hori  (bit-and bitboard (bit-shift-right bitboard 7))
        diag2 (bit-and bitboard (bit-shift-right bitboard 8))
        vert  (bit-and bitboard (bit-shift-right bitboard 1))]
    (bit-or (bit-and diag1 (bit-shift-right diag1 (* 2 6)))
            (bit-and hori  (bit-shift-right hori  (* 2 7)))
            (bit-and diag2 (bit-shift-right diag2 (* 2 8)))
            (bit-and vert  (bit-shift-right vert  (* 2 1))))))

insert: Is there a better way to somehow make function calls depend on some test?
bit-check-board: A lot of similar lines where just one number (and one variable name) is different.

The bit-check-board can be indeed improved. This is what I got by just joining together common parts, even though I guess it could be further improved:

(defn check [c x]
  (bit-and c (bit-shift-right c x)))

(defn bit-check-board
  "Checks whether given bitboard is a win."
  [bitboard]
  (let [positions [6 7 8 1]
        coords (mapv (partial check bitboard) positions)]
    (apply bit-or (map check coords (map #(* 2 %) positions)))))

Also, you can reduce the repetition in the insert function by better modularize the code and use higher order functions with a pre-calculated transformation table:

(defn insert* [board x y]
  (bit-insert board (- 5 y) x))

(defn get-transforms [num]
  (if (= 1 num)
    [identity insert* identity]
    [identity identity insert*]))

(defn insert
  "Inserts symbol for given player (either 1 or 2) at specified x and
   calls bit-insert for corresponding bitboard."
  [boards x player-num]
  (let [y (get-y (boards 0) x)
        board0 (assoc-in (boards 0) [y x] (player player-num))
        transfv (get-transforms player-num)]
    (mapv [board0 (boards 1) (boards 2)] transv)))

If you're on Clojure < 1.4 than you have to change (mapv ...) in (vector (map ...))


Wikipedia Table Scraper I created this small script to strip the data out of tables that have hyperlinks as their <th /> elements. I was hoping to get input on code clarity and possibly simplification, efficiency is not much of an issue for me as there are not any huge tables on Wikipedia.

var arr = $('tbody').children('tr').map(function(idx) {
  return(idx === 0) ?
  {
    rows: $('th', this).map(function(idx) {
      return {
        href: $('a', this).attr('href'),
        title: $('a', this).text()
      }
    }).get()
  } : {
    href: $('th a', this).attr('href'),
    title: $('th a', this).text(),
    rows: $('td', this).map(function(idx) {
      return $(this).text();
    }).get()
  }
}).get();

Here is an example of a table that this scraper can be run on.

This looks pretty nice, I only have a few nit-picks, really.

What you call "rows" are actually "columns". It's a bit misleading.
The rows are actually the var arr that you're making.

The ternary is a bit confusing.
If clarity is important to you (as you mentioned),
then I think a good old-fashioned if-else would be better.

Lastly, the idx function parameters in the inner map calls are unused, so you could just as well drop them.


Embedding of size in Stream I am embedding my size in a Stream in the first 4 byte.  When I think about it, it should be possible to do it better, but that depends.

Here is how I currently do it:

bsize = ms.Length;
tcp.GetStream().Write(BitConverter.GetBytes(bsize), 0, intsize);
tcp.GetStream().Write(ms.GetBuffer(), 0, (int)bsize);

As you can see, it works and it's nothing weird.  I just send 2 streams, the first being the length and the other being the data.

So, there aren't many things that can be done to improve or even change this.

But my idea is: why use 2 streams when you can probably embed the size immediately on the second, so you only need to write it once?

Sadly, I am not sure how to do that, or if it's even an improvement.  Here is my testing code to achieve it:

bsize = ms.Length;
ms.Write(BitConverter.GetBytes(bsize), 0, intsize);
ms.Position = 0;
tcp.GetStream().Write(ms.GetBuffer(), 0, (int)bsize+4);

So, I thought: why can't you just write the length to the MemoryStream and add it before the actual data?

But, it doesn't work, and I am not sure if it's adding or overwriting data.  I suppose it's overwriting, which means it won't work.

From my testing, I think Write adds data to the end, which complicates things.

I was looking into the MemoryStream and I found this

  Except for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.

on MemoryStream.Write Method

I would look around a little bit on the MSDN sites that I am going to list and see if you want to change around the code that you have so that you can maybe use the Byte[] Parameter.

MemoryStream Class

although it looks like most of those Constructors create non-resizable instances, whereas the a Memory Stream Created with out any Parameters ...

  Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.

so to me it looks like the BlockCopy or 2 streams methods are the only way to go.


Plotting different parameterized polynoms For a university assignment I had to plot different polynomial functions depending on one single parameter. That parameter gave the number of supporting points to interpolate a given function in the domain \$\lbrack -1.0, 1.0 \rbrack \$.

The supporting points were to be calculated in two different fashions. Either they were to be equidistant or be Chebyshev nodes. The given definitions were:

$$ x_i = \frac{2i}{n} - 1 , \quad x_i = \cos \frac{(2i + 1)\pi}{2(n + 1)} $$

The plots are to be handed in in a pdf. The polynomial functions I had to calculate were given as:

\$\Phi_n(x) = \underset{i \neq j}{\underset{i = 0}{\overset{n}{\Pi}}} (x - x_i) \$ and the slightly more complicated \$\lambda(x) = \underset{i = 0}{\overset{n}{\Sigma}} \lvert l_{i,n}(x) \rvert \$. Here \$l_{i,n}(x)\$ denotes a Lagrange polynomial.  I'll just stop torturing you with math definitions, (because I'm reasonably sure I'm able to copy a formula from a script into code).

Note that \$\Phi_n\$ is called "Supporting point polynomial" and \$\lambda\$ is called "Lebesgue function" in the assignment.

So without further ado, here's my code.
Note that maintainabiltiy for future use is not a concern, so if you want you can mention docstrings and variable names, but those points don't really help me :)

import numpy as np
import matplotlib.pyplot as plt

def equidistant_points(count):
    points = []
    for i in np.arange(0, count):
        points.append((2 * i / count) - 1)
    return points

def tschebyscheff_points(count):
    points = []
    for i in np.arange(0, count):
        points.append(np.cos(((2 * i + 1) * np.pi) / (2 * (count + 1))))
    return points

def as_supporting_point_poly(points, x):
    poly = 1
    for point in points:
        poly = poly * (x - point)
    return poly

def lagrange_poly(points, j, x):
    poly = 1
    for i in range(0, len(points)):
        if (i != j):
            poly = poly * ((x - points[i]) / (points[j] - points[i]))
    return poly

def lebesgue_function(points, x):
    leb = 0
    for i in range(0, len(points)):
        leb = leb + np.fabs(lagrange_poly(points, i, x))
    return leb

def plot_and_save(n, x, poly_calc, name):
    equi = plt.plot(x, poly_calc(equidistant_points(n), x), 'r-', label='Äquidistante Stützstellen')
    tsch = plt.plot(x, poly_calc(tschebyscheff_points(n), x), 'g-', label='Tschebyscheff-Stützstellen')

    plt.xlabel("x")
    plt.ylabel("y")
    plt.title(name + " mit n = " + str(n))
    plt.grid(True)
    plt.legend(loc='upper right', shadow=False)
    plt.savefig("Aufg1"+ poly_calc.__name__ + str(n) + ".png")
    plt.show()

if __name__== '__main__':
    domain = np.arange(-1.0, 1.0001, 0.0001)
    plot_and_save(8, domain, as_supporting_point_poly, "Stützstellenpolynome")
    plot_and_save(20, domain, as_supporting_point_poly, "Stützstellenpolynome")

    plot_and_save(8, domain, lebesgue_function, "Lebesgue-Funktion")
    plot_and_save(20, domain, lebesgue_function, "Lebesgue-Funktion")

I'm especially interested in ways to make the calculation of the supporting points in equidistant_points and tschebyscheff_points cleaner.

You're not really using numpy at the moment. If we use the simple 'translation table' below, your code would work if you just replaced the NumPy function with the Python equivalent:

np.arange -> range, assuming your domain is just integers,
np.fabs -> abs,
np.cos -> math.cos, and,
np.pi -> math.pi.

Instead you want to take advantage of NumPy. Take tschebyscheff_points, you have the equation:

$$\cos(\frac{\pi(2i + 1)}{2(\text{count} + 1)})$$

But your Python code is:

def tschebyscheff_points(count):
    points = []
    for i in np.arange(0, count):
        points.append(np.cos(((2 * i + 1) * np.pi) / (2 * (count + 1))))
    return points

Yes it contains the equation, but with numpy you can just write the equation:

def tschebyscheff_points(count):
    return np.cos(((2 * np.arange(count) + 1) * np.pi) / (2 * (count + 1)))

This significantly improves both performance, and readability. As you only need to read the equation.

I'd also change your code to use comprehensions. lebesgue_function should use sum as writing the addition yourself is WET. And in as_supporting_point_poly and lagrange_poly you should factor out the multiplication into a product function.

I'm not too good with NumPy and matplotlib so I can't really help with improving the display of the data.
But the above should get you to the following code.
Note, that I have two lagrange_poly as I don't know if a pure Python function is better than the NumPy equivalent.

import numpy as np
import matplotlib.pyplot as plt
from operator import mul
from functools import reduce

def product(it, initial=1):
    return reduce(mul, it, initial)

def equidistant_points(count):
    return np.arange(count) * 2 / count - 1

def tschebyscheff_points(count):
    return np.cos(((2 * np.arange(count) + 1) * np.pi) / (2 * (count + 1)))

def as_supporting_point_poly(points, x):
    return product(x - point for point in points)

def lagrange_poly(points, j, x):
    point_j = points[j]
    p = np.delete(points, j)
    return product((x - p) / (point_j - p))

def lagrange_poly(points, j, x):
    return product(
        (x - point) / (points[j] - point)
        for i, point in enumerate(points)
        if i != j
    )

def lebesgue_function(points, x):
    return sum(
        np.fabs(lagrange_poly(points, i, x))
        for i in range(len(points))
    )

def plot_and_save(n, x, poly_calc, name):
    equi = plt.plot(x, poly_calc(equidistant_points(n), x), 'r-', label='Äquidistante Stützstellen')
    tsch = plt.plot(x, poly_calc(tschebyscheff_points(n), x), 'g-', label='Tschebyscheff-Stützstellen')

    plt.xlabel("x")
    plt.ylabel("y")
    plt.title(name + " mit n = " + str(n))
    plt.grid(True)
    plt.legend(loc='upper right', shadow=False)
    plt.savefig("Aufg1"+ poly_calc.__name__ + str(n) + ".png")
    plt.show()

if __name__== '__main__':
    domain = np.arange(-1.0, 1.0001, 0.0001)
    plot_and_save(8, domain, as_supporting_point_poly, "Stützstellenpolynome")
    plot_and_save(20, domain, as_supporting_point_poly, "Stützstellenpolynome")

    plot_and_save(8, domain, lebesgue_function, "Lebesgue-Funktion")
    plot_and_save(20, domain, lebesgue_function, "Lebesgue-Funktion")