texts: Calculating primes until a user-given value

texts:
I am new to C# programming. I wrote a little program in C# to calculate all primes until a user-given value. Ignoring the lack of computing power I wrote this program to handle theoretically very huge numbers.

The code works properly. I have no error handling simply because it is a little program for myself. But I'd like some suggestions about error handling. :)

Can you give me suggestions to improve the code? Maybe even to make it faster?

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace ConsolePrimes
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Up to which number shall all primes be calculated?");
            string inputvar = Console.ReadLine();
            ulong inputnumber = Convert.ToUInt64(inputvar);
            var primes = new List<ulong>();
            primes.Add(2);
            primes.Add(3);
            bool isprime = false;
            double result = 0;
            for (ulong i = 4; i<inputnumber; i++)
            {
                isprime = true;
                foreach (ulong prime in primes)
                {
                    result = i % prime;
                    if (result == 0)
                    {
                        isprime = false;
                        break;
                    }
                }
                if (isprime == true)
                {
                    primes.Add(i);
                }
            }
            int numberofprimes = primes.Count;
            Console.WriteLine("The Range from 0 to " + inputvar + " has " + Convert.ToString(numberofprimes) + " primes.");
            Console.WriteLine("The list of all primes is now getting exported to \"primes.txt\".");
            TextWriter tw = new StreamWriter("primes.txt");
            foreach (ulong nr in primes)
            {
               tw.WriteLine(nr);
            }
            tw.Close();
            Console.ReadLine();
        }
    }
}


texts:
My today's goal was to further practice POSIX shell scripting.

So, I decided to write the following POSIX shell script for generating pseudo-random numbers.

Any and all reviews would be appreciated.

#!/bin/sh

bold=$(tput bold)
red=$(tput setaf 1)
nocolor=$(tput sgr0)

bold_red="${bold}${red}"

print_error_and_exit()
{
    # check if exactly two arguments have been passed
    if [ "$#" -ne 2 ]
    then
        print_error_and_exit "print_error_and_exit(): There have not been passed two arguments!" 2
    fi

    # check if the second argument is a number
    if ! is_this_a_number "$2"
    then
        print_error_and_exit "print_error_and_exit(): The argument #2 is not a number!" 5
    fi

    echo "${bold_red}$1 Exit code = $2.${nocolor}" 1>&2
    exit "$2"
}

is_this_a_number()
{
    # check if exactly one argument has been passed
    if [ "$#" -ne 1 ]
    then
        print_error_and_exit "is_this_a_number(): There has not been passed exactly one argument!" 2
    fi

    # check if the argument is an integer number
    if [ "$1" -eq "$1" ] 2> /dev/null
    then
        return 0
    else
        return 1
    fi
}

random_number()
{
    # check if exactly two arguments have been passed
    if [ "$#" -ne 2 ]
    then
        print_error_and_exit "random_number(): There have not been passed exactly two arguments!" 3
    fi

    # check if the first argument is a number
    if ! is_this_a_number "$1"
    then
        print_error_and_exit "random_number(): The argument #1 is not a number!" 4
    fi

    # check if the second argument is a number
    if ! is_this_a_number "$2"
    then
        print_error_and_exit "random_number(): The argument #2 is not a number!" 5
    fi

    # min is inclusive
    range_min="$1"

    # max is inclusive too
    range_max="$2"

    # RANDOM is not POSIX-available,
    # so we can use shuf for instance
    shuf -i "$range_min"-"$range_max" -n 1
}

# temporary testing loop
i=1
while [ "$i" -lt 10 ]
do
    random_number 1 9

    i=$((i + 1))
done


texts: DataTable runtime grouping on user defined criteria

texts:
I one of my tools I use the user can define grouping for a DataTable. The criteria are known only at run-time. To achieve this I use a Dictionary<string, object> with a custom dictionary comparer that the GroupBy extension cosumes so that I can aggregate the groups. This works pretty well but I was wondering if there is still room for improvement.

The number and names of columns can change. There is nothing known about the DataTable until a JSON configuration is loaded. This is an example of such a configuration from one of my unit tests.

"Columns": [
    "_nvarchar | key",
    "_datetime2",
    "_int | sum",
    "_float | avg",
    "_bit | count",
    "_money",
    "_numeric"
],

(The configuration is already being taken care of and is not a part of the review, it's just for reference to show how the columns can be configured.)

To make it simpler here's some example data:

var dataTable = new DataTable();
dataTable.Columns.Add("FirstName", typeof(string));
dataTable.Columns.Add("LastName", typeof(string));
dataTable.Columns.Add("ItemCount", typeof(int));

dataTable.Rows.Add("foo", "bar", 2);
dataTable.Rows.Add("foo", "bar", 5);
dataTable.Rows.Add("baz", "qux", 3);
dataTable.Rows.Add("foo", "bar", 2);
dataTable.Rows.Add("baz", "qux", 6);

The keyColumns are those marked with key - they are not the same as database keys and they are for aggregation later.

var keyColumns = new[] { "FirstName", "LastName" };

The implementation is very simple. Just enumerate the rows and put the name of the key column in the dictionary's key and the value of that column as the value of that dictionary item.

This is exactly the implementation I currently use:

var rowGroups = dataTable.AsEnumerable().GroupBy(x =>
    keyColumns.ToDictionary(
        column => column,
        column => x[column],
        StringComparer.OrdinalIgnoreCase
    ),
    new DictionaryComparer<string, object>()
).ToList();

To actually be able to group the dictionaries there is also this comparer:

public class DictionaryComparer<TKey, TValue> : IEqualityComparer<IDictionary<TKey, TValue>>
{
    public bool Equals(IDictionary<TKey, TValue> x, IDictionary<TKey, TValue> y)
    {
        return x.All(item => y.TryGetValue(item.Key, out TValue value) && item.Value.Equals(value));
    }

    // Ignore dictionary hash-code and just compare the keys and values.
    public int GetHashCode(IDictionary<TKey, TValue> obj) => 0;
}

And the result is:


texts:
I wanted to know is this the best way to handle a SegmentedControl using the e.NewValue? Base on the selected segment I hide and select different data?

XAML

<telerikInput:RadSegmentedControl.ItemsSource>
                            <x:Array Type="{x:Type x:String}">
                                <x:String>Option One</x:String>
                                <x:String>Option Two</x:String>
                                <x:String>Favorites</x:String>
                            </x:Array>
</telerikInput:RadSegmentedControl.ItemsSource>

c#

private void Show_SelectionChanged(object sender, ValueChangedEventArgs<int> e)
        {
            if (HomeListView.ItemsSource != null)
            {
                TilesViewModel gtvm = new TilesViewModel();

                switch (e.NewValue)
                {

                    case 0:
                        var selectoneItems = gtvm.ListItems.Where(x => x.IsProfessional).ToList();
                        HomeGuidelineListView.ItemsSource = selectoneItems ;
                        selecttwoTitle.IsVisible = false;
                        selectoneTitle.IsVisible = true;
                        FavOptionsGrid.IsVisible = false;
                        HomeOptionsGrid.IsVisible = true;
                        NoFavsMessage.IsVisible = false;
                        break;
                    case 1:
                        var selecttwoItems = gtvm.ListItems.Where(x => x.IsProfessional == false).ToList();
                        selecttwoTitle.IsVisible = true;
                        selectoneTitle.IsVisible = false;
                        FavOptionsGrid.IsVisible = false;
                        HomeOptionsGrid.IsVisible = true;
                        NoFavsMessage.IsVisible = false;
                        break;
                    case 2:
                         var favItems = gtvm.GuidelineListItems.Where(x => x.IsFavorite).ToList();
                         HomeGuidelineListView.ItemsSource = favItems;
                         HomeOptionsGrid.IsVisible = false;
                         FavOptionsGrid.IsVisible = true;
                        break;
                }
            }
        }


texts: Comparing strings with different newlines

texts:
Recently, I was burdened with the task of finding a bug. It turns out the problem was strings from different systems containing different newlines. Comparing two strings with different newlines (but same "text") still are not equal. E.g. "new\nline" (Unix flavor) and "new\r\nline" (Windows flavor) are not equal.

Since the code will be dealing with both types of newlines, I wrote a method to test for equality independent from newline type (the code treats "\n", "\r", "\r\n" and "\n\r" the same (even though "\n\r" isn't really used as newline)).

After I got the code done I would like your opinion on it. What do you think of variable names or method names (I know I could have chosen better names)? Is there a way to optimize the code or make it more readable?

public class StringUtils {
    public static final char LF = '\n';
    public static final char CR = '\r';

    public static boolean equalsIgnoreNewlineTwirks(String str, String other){
        if (str == null || other == null){
            return false;
        }
        if (str == other){
            return true;
        }

        char[] s1 = str.toCharArray();
        char[] s2 = other.toCharArray();
        int index1 = 0, index2 = 0;
        while (true){
            boolean oob1 = index1 >= s1.length, oob2 = index2 >= s2.length;
            if (oob1 | oob2){
                return oob1 & oob2;
            }

            char ch1 = s1[index1], ch2 = s2[index2];
            if (ch1 != ch2){
                if (ch1 != LF && ch1 != CR) return false;
                if (ch2 != LF && ch2 != CR) return false;

                if (index1 + 1 < s1.length && isCRAndLF(s1[index1], s1[index1 + 1])){
                    index1++;
                }
                if (index2 + 1 < s2.length && isCRAndLF(s2[index2], s2[index2 + 1])){
                    index2++;
                }
            }

            index1++; index2++;
        }
    }

    private static boolean isCRAndLF(char ch1, char ch2){
        return (ch1 == CR && ch2 == LF) || (ch1 == LF && ch2 == CR);
    }
}


texts: Optimizing python code for big data sets

texts:
I'm trying to optimize a python string that works on big data sets, the way it works is by taking in a with a list of keywords and scores and taking in a file loaded with data from the twitter api. The program does a keyword match against tweet text. At the end of the program I want to produce an average for each term found in text object of the json file. e.g.

sad 3

With sad being the keyword and 3 being the average score.

It's running way too slow but I'm new to Python coming from a php background and I think I'm doing things the php way in python.

How can I get this code to run faster?

import sys
import json
import re

def findRecord(key, records):
    for r in records:
        if r[0] == key:
            return r

def average_records(records):
    for r in records:
        if r[1] > 0:
            avg = r[1] / r[2]
            print r[0] + ' ' + str(avg)
        else:
            avg = r[3] / r[4]
            print r[0] + ' ' + str(avg)

def hw(sent_file, tweet_file):
    scores = {}

    sent_file = open(sent_file, 'r')

    for line in sent_file:
        term, score = line.split("\t")
        scores[term] = int(score)

    recored_affin = []

    #print scores.items()

    data = []

    with open(tweet_file, 'r') as f:
        for line in f:
            data.append(json.loads(line))

        #print data[4]['text']

        for tweet in data:
            total = 0
            if 'text' in tweet:
                for k, v in scores.iteritems():

                    #print tweet['text']
                    num_of_aff = len(re.findall(k, tweet['text']))
                    if num_of_aff > 0:
                        #print "Number is: " + str(num_of_aff)
                        #print "Word is: " + k
                        #print "Tweet is: " + tweet['text']
                        total += (v * num_of_aff)
                        #print "Score is: " + str(total)

                        #while count < len(recorded_affin):

                        foundRow = findRecord(k, recored_affin)

                        if foundRow != None:
                            index = recored_affin.index(foundRow)
                            quick_rec = recored_affin[index]

                            if v > 0:
                                new_value = quick_rec[1] + v
                                new_count = quick_rec[2] + 1
                                old_neg_value = 0
                                old_neg_count = 0
                                recored_affin.append([k, new_value, new_count, old_neg_value, old_neg_count])
                                recored_affin.remove(foundRow)
                            elif v < 0:

                                old_pos_value = 0
                                old_pos_count = 0
                                new_value = quick_rec[3] + v
                                new_count = quick_rec[4] + 1
                                recored_affin.append([k, old_pos_value, old_pos_count, new_value, new_count])
                                recored_affin.remove(foundRow)

                        else:
                            if v > 0:
                                recored_affin.append([k,v,1,0,0])
                            elif v < 0:
                                recored_affin.append([k,0,0,v,1])

                            #print recored_affin

                        ##print foundRow

                ##print total
    average_records(recored_affin)

def lines(fp):
    print str(len(fp.readlines()))

def main():
    sent_file = open(sys.argv[1])
    tweet_file = open(sys.argv[2])
    hw(sys.argv[1], sys.argv[2])
    #lines(sent_file)
    #lines(tweet_file)

if __name__ == '__main__':
    main()


texts:
I have been using aws-sdk gem with Rails, for uploading and downloading objects. I believe it's working fine.

Demo code:

begin

s3 = AWS::S3.new
bucket = s3.buckets[ENV['BUCKET_NAME']]
bucket.objects.create(fname, data)
object = bucket.objects[fname]
exported_url   =  object.url_for(:get, {expires: 3.weeks,
             response_content_type: "text/csv", response_content_disposition:
"attachment; filename=#{fname}.csv"}).to_s
rescue Aws::S3::Errors::ServiceError => e
 puts e.message
end

Are there any issues if uploading a large file to s3? I mean, that issues will affect my code. Will it handle that situation for large files? Should I go for the multipart options in s3?


texts: Correctly applying the "rule of five" to a RAII socket wrapper

texts:
I was trying to create a simple RAII wrapper with rule of 5 for a TCP POSIX socket. My aim was to try learn how to apply rule of five in different situations, but this one was somehow tricky.

class tcp_socket {
public:
    tcp_socket() {
        _s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    }

    tcp_socket(const tcp_socket& other) = delete;
    tcp_socket& operator=(const tcp_socket& other) = delete;

    tcp_socket(tcp_socket&& other) noexcept {
        _s = other._s;
        other._s = -1;
    }

    tcp_socket& operator=(tcp_socket&& other) noexcept {
        if (this != &other) {
            _s = other._s;
            other._s = -1;
        }
        return (*this);
    }

    ~tcp_socket() {
        close(_s);
    }

private:
    int _s = -1;
};

In this class, I like to create socket in constructor and close it in destructor, a logical RAII approach. But I noticed closing socket in destructor creates a lot of side effects for writing other 4 routines of rule of five.

Move constructor/assignment:

As you see, I use other._s = -1; in move constructor/assignment. Because if I don't, when the moved object deletes, it will also close my socket in moved-to object. I think this will solve my problem, because when moved object will be destroyed, it will try to close -1 file descriptor which is an error, but will not ruin anything.

Copy constructor/assignment:

As you see, the only method that I could think of for copy constructor/assignment is deleting them. Because at first I thought to use dup() to duplicate file descriptor for copying, but if I do so, operations on one one instance (for example shutdown()) will affect another instance.

What do you think about this class? Do you think if this class does provide a simple RAII with correct rule of five or I need to adjust it somehow? Is it possible to write Copy constructor/assignment somehow or it is not logical to provide them?


texts: Iterate over large list of lists and replace its elements

texts:
I have a large list of lists, where each list contains words/characters as elements, and each list is of different length and may contain same words/characters more than once, as for example:

list_ = [['black', 'window', 'says', 'window'], ['dog', 'great'], ['after', 'end', 'explains', 'income', '.', '?']]

I also have a large dictionary of words/characters like this:

dictionary = {'word1': 'RARE', 'word2': 'RARE', 'word3': 'RARE', '%': 'RARE'} etc.

The length of list_ is around 40 000 (the total number of words in all lists is about 1 mln) and the length of dictionary is around 31 000. For each word in dictionary, I need to check if it's in any of the lists of list_ and if it is, I need to replace it with 'RARE'.

For example, if 'black'== 'word1' and 'after' == word3, the final list_ will be:

list_ = [['RARE', 'window', 'says'], ['dog', 'says'], ['RARE', 'end', 'explains', 'income']]

Because of the large length of list_ and dictionary, the code that I currently have below takes almost 40min to run. I'm new to Python and I would appreciate if someone could suggest whether there's a way to do this task much faster/more efficiently.

def replace_all(word_list, dictionary):
   for i, j in dictionary.iteritems():
      for lst in word_list:
         if i in set(lst):
            lst[lst.index(i)] = j

replace_all(list_, dictionary)


texts:
This program connects to Soundcloud and turns the supplied username into a CSV file with the user's like list. The code accomplishes this although it looks rather messy and was wondering what I could change to improve it.

import soundcloud
import urllib.request
import json
import requests

client = soundcloud.Client(client_id="********************************")

def get_username():
    """
    Asks user for their Soundcloud username.
    """
    username = input("Please enter your Soundcloud username: ")
    user_id = (valid_username(username))
    print(user_id)

def valid_username(username):
    """
    Checks if supplied username is valid.
    """
    global user
    try:
        user = client.get("/resolve", url="http://soundcloud.com/{}/".format(username))
        user_info(user.id)
    except requests.HTTPError:  # Runs if account cannot be found.
        print("The username you entered is invalid, please try again")
        get_username()

def user_info(user_id):
    """
    Retrieves the users information from their user ID
    and prints the number of tracks they have liked.
    """
    global number_of_user_likes
    retrieve = urllib.request.urlopen("https://api.soundcloud.com/users/{}.json?consumer_key="
                                      "********************************".format(user_id)).read()
    decode_user_info = json.loads(retrieve.decode())
    number_of_user_likes = decode_user_info['public_favorites_count']
    username = decode_user_info['username']
    print("You have liked a total of {} tracks".format(number_of_user_likes))
    save_likes(username)

def save_likes(username):
    """
    Creates a CSV file with headers and then saves the users likes to the CSV file.
    """
    csv_file = open("{}s like list.csv".format(username), 'w', encoding='UTF-8')
    csv_file.write("Track Title, Track URL\n")  # Writes headers to CSV file.
    offset_number = 0
    complete = False
    while number_of_user_likes > 0 and complete is False:
        if offset_number < number_of_user_likes:
            try:
                track_fetch = urllib.request.urlopen(
                    "http://api.soundcloud.com/users/{}/favorites.json?client_id="
                    "********************************&offset={}&limit1".format(user.id, offset_number)).read()
                track_data = json.loads(track_fetch.decode())
                if "," in track_data[0]["title"]:
                    track_title = track_data[0]["title"].replace(",",
                                                                 "")  # Removes commas as causes issues with .csv files
                else:
                    track_title = track_data[0]["title"]
                csv_file.write("{},{}\n".format(track_title, track_data[0]["permalink_url"]))
                offset_number += 1
                print("{} of {} ({}%)".format(offset_number, number_of_user_likes,
                                              round(float(100 / number_of_user_likes * offset_number), 2)))
            except IndexError:
                print("There is an issue with Soundcloud, please try again")
            except requests.HTTPError:
                print("There is an issue with Soundcloud, please try again")
            except requests.ConnectionError:
                print("Check your internet connection")
        else:
            complete = True
            finished()

def finished():
    """
    Runs after the users like list has finished being saved.
    """
    print("Finished")

get_username()


texts:
I would like to hear your recommendations on when to use JavaScript's const over let, especially when applied to mutable objects as demonstrated in the following comparative code samples:

Strict preference of let:

// Create 100 x 100 zero-filled matrix:
let matrix = new Array(100);
for (let i = 0, length = matrix.length; i < length; ++i) {
  matrix[i] = new Array(100).fill(0);
}

// Fill matrix with values:
function fill(matrix, value) {
  for (let row of matrix) {
    row.fill(value);
  }
}

Strict preference of const:

// Create 100 x 100 zero-filled matrix:
const matrix = new Array(100),
      length = matrix.length;
for (let i = 0; i < length; ++i) {
  matrix[i] = new Array(100).fill(0);
}

// Fill matrix with values:
function fill(matrix, value) {
  for (const row of matrix) {
    row.fill(value);
  }
}

In my opinion, the const semantics in JavaScript are too weak to help static code analysis or to allow for compiler optimizations. Where const is (mis-)used to signal immutability, I'd go with e.g. declaring ALL_CAPS identifiers instead.

Is my thinking flawed? When writing robust, maintainable code, which of the above samples would you prefer? Is there a middle ground?


texts: Restructuring loop logic and its effect on allocations

texts:
I came across in a code base # TODO this seems very clumsy to duplicate the loop code like this? so I figured I'd give it a shot as a learning exercise. I'd like some feedback on what I came up with, particularly:

Is this proper idiomatic Julia, or close? Or am I just writing Python in Julia?
What explains the difference in allocations between the two approaches?
(Arguments used: [1,1], octaves=10)

Old: 0.000013 seconds (63 allocations: 1.062 KiB)
New: 0.000008 seconds (41 allocations: 1.500 KiB)

Thanks!

Original function:

# coords is [x] or [x, y] or [x, y, z] or [x, y, z, w]
function _octaves(coords::Array{T, 1} where T <: Real;
        octaves::Int = 1,
        persistence=1.0)
    total     = 0.0
    frequency = 1.0
    amplitude = 1.0
    maxval    = 0.0
    # TODO this seems very clumsy to duplicate the loop code like this?
    l = length(coords)
    if l == 1
        for i in 1:octaves
            total += simplexnoise(coords[1] * frequency) * amplitude
            maxval += amplitude
            amplitude *= persistence
            frequency *= 2
        end
    elseif l == 2
        for i in 1:octaves
            total += simplexnoise(coords[1] * frequency, coords[2] * frequency) * amplitude
            maxval += amplitude
            amplitude *= persistence
            frequency *= 2
        end
    elseif l == 3
        for i in 1:octaves
            total += simplexnoise(coords[1] * frequency, coords[2] * frequency, coords[3] * frequency) * amplitude
            maxval += amplitude
            amplitude *= persistence
            frequency *= 2
        end
    elseif l == 4
        for i in 1:octaves
            total += simplexnoise(coords[1] * frequency, coords[2] * frequency, coords[3] * frequency, coords[4] * frequency) * amplitude
            maxval += amplitude
            amplitude *= persistence
            frequency *= 2
        end
    end
    return total / maxval
end

And my refactor:

function _octaves(coords::Array{T, 1} where T <: Real;
        octaves::Int = 1,
        persistence=1.0)
    total     = 0.0
    frequency = 1.0
    amplitude = 1.0
    maxval    = 0.0

    for _ in 1:octaves
        inputs = coords .* frequency
        total += simplexnoise(inputs...) * amplitude

        maxval += amplitude
        amplitude *= persistence
        frequency *= 2
    end

    return total / maxval
end


texts:
Lets say you have several .txt files in a folder and want to merge all text files into a single Excel document:

A.txt

A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
A11
A12
A13
A14
A15
A16
A17
A18
A19
A20
A21
A22

B.txt

B1
B2
B3
B4
B5
B6
B7
B8
B9
B10
B11
B12
B13
B14

C.txt

C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
C11
C12
C13
C14
C15
C16
C17
C18
C19
C20
C21
C22
C23
C24
C25
C26
C27
C28
C29
C30
C31
C32
C33

D.txt

//empty file

E.txt

E1
E2
E3
E4
E5
E6
E7
E8
E9
E10
E11
E12
E13
E14
E15
E16
E17
E18
E19
E20
E21
E22
E23
E24
E25
E26
E27
E28
E29
E30
E31
E32
E33
E34
E35
E36
E37
E38

The Output:

spreadsheet_to_text.py

"""
Reads all .txt files in path of the script into a single
spreadsheet. In the first line of the spreadsheet the filename were
the data is from is displayed. Then the data follows
"""

import os
from typing import List
import openpyxl
from openpyxl.utils import get_column_letter

def text_into_spreadsheet():
    """main logic for read .txt into spreadsheet"""
    workbook = openpyxl.Workbook()
    sheet = workbook.active
    column: int = 1
    filenames: List[str] = os.listdir()
    for filename in filenames:
        if filename.endswith(".txt"):
            with open(filename) as textfile:
                lines: List[int] = textfile.readlines()
                sheet[get_column_letter(column) + '1'] = filename
                row: int = 2
                for line in lines:
                    sheet[get_column_letter(column) + str(row)] = line
                    row += 1
            column += 1
    workbook.save('result.xlsx')

if __name__ == "__main__":
    text_into_spreadsheet()

What do you think about the code?
How can it be improved?

edit: you can find a programm doing it in reversed in Spreadsheets to text files


texts: Python Find Tuple by Geographic Priority

texts:
Requirement:

Python Function will read a LIST of TUPLES. The structure of the tuple is ("STRING", "STRING","STRING",INT,"GEOGRAPHIC VALUE")
This Geographic value can have 5 values "zip", "city", "county", "state" and "country"
Now if a Tuple has elements where geographic values are (....,"zip"),(....,"county"), (....,"country") then function should return the element/tuple which contains "zip"
Examples :
Input : [('aa', 'bb', 'cc', 1, 'state'), ('aa', 'bb', 'cc', 2, 'zip')]
Output : ['aa', 'bb', 'cc', 2, 'zip']
In other words 'zip' is higher priority(hp) than 'city'.
'city' is HP than 'county',
'county' is HP than 'state'
and 'state' is HP than 'country'.

I have written code for that but being new to Python, the code looks clunky. Is there any shorter method?
def unique_list(input):
    my_list = input
    print(my_list)
    last_list = []
    zipcnt = -1
    citicnt = -1
    countycnt = -1
    statecnt = -1
    return_list_zip = []
    return_list_city = []
    return_list_county = []
    return_list_state = []
    return_list_country = []
    for j in range(len(my_list)):
          if(my_list[j][4]) == "zip":
             zipcnt = len(my_list)
             return_list_zip = list(my_list[j])
             continue
          elif (my_list[j][4] == 'city' and zipcnt == -1):
              citycnt = len(my_list)
              return_list_city = list(my_list[j])
              continue

          elif (my_list[j][4] == 'county' and zipcnt == -1 and citicnt == -1):
              countycnt = len(my_list)
              return_list_county = list(my_list[j])
              continue
          elif (my_list[j][4]  == 'state'and zipcnt == -1 and citicnt == -1 and countycnt == -1):
              statecnt = len(my_list)
              return_list_state = list(my_list[j])
              continue
          elif (my_list[j][4] == 'country'and zipcnt == -1 and citicnt == -1 and countycnt == -1 and statecnt == -1):
              return_list_country = list(my_list[j])
              continue
    if(zipcnt != -1):
        return_list = return_list_zip
    elif(citycnt != -1):
        return_list = return_list_city
    elif(countycnt != -1):
        return_list = return_list_county
    elif (statecnt != -1):
        return_list = return_list_state
    else:
        return_list = return_list_country
    return return_list
input_list = [ ("aa", "bb", "cc", 1,"state"), ("aa","bb","cc",2,"zip")]
input_list1 = [("XX","YY","NN",1,"country"),("XX","YY","NN",2,"city"),  ("XX","YY","NN",3,"state")]
input_list2 = [("NN","FF","PP",10,"state"),("NN","FF","PP",11,"country"),("NN","FF","PP",10,"city")]
print(unique_list(input_list))
print(unique_list(input_list1))
print(unique_list(input_list2))


texts: Read list of dictionaries with nested dictionaries

texts:
I have a file as content a list of dictionaries (Around 75000). For instance, this is an example of first line I got when reading the file (value for v):

{
 "id": 1,
 "name": "Explosives",
 "category_id": 1,
 "average_price": 294,
 "is_rare": 0,
 "max_buy_price": 755,
 "max_sell_price": 1774,
 "min_buy_price": 99,
 "min_sell_price": 18,
 "buy_price_lower_average": 176,
 "sell_price_upper_average": 924,
 "is_non_marketable": 0,
 "ed_id": 128049204,
 "category": {
   "id": 1,
   "name": "Chemicals"
 }
}

My actual working code is :

for v in d:
    commodities_reference = []
    for k, g in v.items():
        if isinstance(g, dict):
            dict1 = g
            my_value1 = dict1.get("id")
            my_value2 = dict1.get("name")

    for s, i in v.items():
        if not isinstance(i, dict):
            commodities_reference.append(i)
    commodities_reference.append(my_value1)
    commodities_reference.append(my_value2)

Output wanted = All the values in same list in the same order for doing a SQL INSERT Statement afterwards (Meaning values from the nested dict must be also at the end.)

[1, 'Explosives', 1, 294, 0, 755, 1774, 99, 18, 176, 924, 0, 128049204, 1, 'Chemicals']

From a performance perspective, with SQLITE3/python 3.7, it is a catastrophe. I am looking for some advices in order to make it more efficient. I am thinking about using executemany statement but it seems it takes tuple instead of list.


texts: Calculating all combinations to make sure a Random algorithm has done them all

texts:
I have done this, but it's pretty ugly and it feels like I may be missing something to shorten it:

    vector<int> xcomb, ycomb;
    int limitW = Pdriver->getViewPort().getWidth() / 32;
    int limitH = Pdriver->getViewPort().getHeight() / 32;
    int width = Pdriver->getViewPort().getWidth();
    int height = Pdriver->getViewPort().getHeight();
    while (true){
    start:
        ax = -rand() % width+32;
        ax = (ceil((double)ax / 32) * 32);

        ay = -rand() % height + 32;
        ay = (ceil((double)ay / 32) * 32);

        int temp = 0;

        if (xcomb.size() == 0)
            xcomb.push_back(ax);
        for (int i = 0; i < xcomb.size(); i++)
        {
            if (ax != xcomb[i])
                temp++;
        }
        if (temp == xcomb.size())
            xcomb.push_back(ax);
        temp = 0;

        if (ycomb.size() == 0)
            ycomb.push_back(ay);

        for (int i = 0; i < ycomb.size(); i++)
        {
            if (ay != ycomb[i])
                temp++;
        }
        if (temp == ycomb.size())
            ycomb.push_back(ay);

        if (ycomb.size() == limitH && xcomb.size() == limitW)
        {
            char na[20];
            sprintf_s(na, "GAME COMPLETE!");
            OutputDebugString(na);
        }

        for (u16 i = 0; i < TailCollision.size(); i++)
            if (TailCollision[i] == core::rect<s32>(32 - ax, 32 - ay, 64 - ax, 64 - ay)){

            OutputDebugString("\nAPPLE TAIL CRASH PREVENTED!\n");
            goto start;
            }

        if (core::rect<s32>(32 - ax, 32 - ay, 64 - ax, 64 - ay) != Block(x, y)){
            break;
        }
    }
    xcomb.clear();
    ycomb.clear();
}

If you excuse the mess, you can see that I want a number that is divisible by 32.
I use this to make a Max combination number (limitW and limitH).

I then use two vectors and add all random values that hasn't been tried before during this process (while loop).

And to do that I think I may be doing it in a bit of a workaround. I use a temporary int which I increment for each comparison, and if it has compared the same amount as the size of the vector, it means that number already exists and it has to redo the calculation.

It may be that this is the only option, but it just seems that I do too many steps.


texts:
This code is an answer to an exercise I found on the cplusplus forums.
I'm a beginner so any reviews will be appreciated :)

  Make a program that outputs a simple grid based gameboard to the screen using either numbers or characters. Allow the user  to move either up, down, left, or right each turn. If the player steps on a trap then they lose. If they make it to the treasure 'X' then they win.

#include <iostream>
#include <string>
#include <time.h>
#include <Windows.h>
#include <algorithm>
int main(){
std::string grid = "0000000000\n"
    "0000000000\n"
    "0000000000\n"
    "0000000000\n"
    "0000000000\n"
    "0000000000\n"
    "0000000000\n";
char trap = 'T';
grid[15] = trap;
grid[35] = trap;
grid[45] = trap;
grid[67] = trap;
char player = 'G';
int n = 1;
grid[n] = player;
char end = 'X';
grid[75] = end;
char wasd;
std::cin >> wasd;
while (n != 15 && n != 35 && n != 45 && n != 67 && n!=11 && n!=21 && n!=31 && n!=41 && n!=51 && n!=61 && n!=71 ){
    if (wasd == 'd'){
        grid[n] = '0';
        n++;
        grid[n] = player;
        std::cout << grid;
        std::cin >> wasd;

    }
    else if (wasd == 'a'){
        grid[n] = '0';
        n--;
        grid[n] = player;
        std::cout << grid;
        std::cin >> wasd;
    }
    else if (wasd == 's'){
        grid[n] = '0';
        n = n + 11;
        grid[n] = player;
        std::cout << grid;
        std::cin >> wasd;
    }
    else if (wasd == 'w'){
        grid[n] = '0';
        n = n - 11;
        grid[n] = player;
        std::cout << grid;
        std::cin >> wasd;
    }

    }
if (grid[15] == player || grid[35] == player || grid[45] == player || grid[67] == player){
    std::cout << "You lose!";
}
}


texts:
I have this class that pulls people out of a database. I want it to be a little prettier and function better.

<?php
class comps extends db_config {

    public $output = array();
    private $arr = array();
    public $area;
    public $me;
    public $options = array();

    public function __construct() {
        $this->pdo = new PDO('mysql:host='.$this->_host.';dbname='.$this->_db.';charset='.$this->_charset, $this->_user, $this->_pass);
    }

    public function __toString() {
        return var_dump($this->result());
    }

    public function result() {
        return $this->output;
    }

    public function setoption($option, $value) {
        array_push($this->options, array($option=>$value));
    }

    public function run() {

        $where = array();
        $limit = '';
        $showMe = '';

        if (isset($this->options['admin'])) {
            $where[] = "admin = '".$this->options['admin']."'";
        }

        if (isset($this->options['area'])) {
            $where[] = "area = '".$this->area."'";
        }

        if (isset($this->options['showMe']) && $this->options['showMe'] = true) {
            $showMe = "AND missionary <> '".$this->me."'";
        }

        if (isset($this->options['limit'])) {
            $limit = " LIMIT ".$this->options['admin'];
        }

        if (count($where) > 0) {

            $where = implode(' AND ', $where);

        } else {
            $where = '';
        }

        $area = isset($this->area) ? $this->area : '';

        if ($area != '') {
            $sth = $this->pdo->prepare("SELECT missionary FROM missionary_areas WHERE area_uid = :area AND missionary_released = '0' ".$showMe);
            $sth->bindParam(':area', $area);
            $sth->execute();
            $sth->setFetchMode(PDO::FETCH_OBJ);

            while ($mis = $sth->fetch()) {
                $sth2 = $this->pdo->prepare("SELECT * FROM missionarios WHERE ".$where." mid = :mid LIMIT 1");
                $sth2->bindParam(':mid', $mis->missionary);
                $sth2->execute();

                $this->arr[] = $sth2->fetch(PDO::FETCH_ASSOC);
            }

            foreach($this->arr as $a) {
                array_push($this->output, $a);
            }
        } else {
            $sth = $this->pdo->prepare("SELECT * FROM missionarios ".$where.$limit);
            $sth->execute();
            $res = $sth->fetchAll(PDO::FETCH_ASSOC);

            foreach($res as $a) {
                array_push($this->output, $a);
            }
        }

    }
}


texts: Download a zip archive and extract one file from it

texts:
I wrote a function that downloads a file https://www.ohjelmointiputka.net/tiedostot/junar.zip if it not already downloaded, unzips it and returns the content of junar1.in found in this zip. I have PEP8 complaints about the length of lines that I would like to fix. Is there a way to make the code more readable?

My code :

import os.path
import urllib.request
import shutil
import zipfile

def download_and_return_content():
    if not os.path.isfile('/tmp/junar.zip'):
        url = 'https://www.ohjelmointiputka.net/tiedostot/junar.zip'
        with urllib.request.urlopen(url) as response, open('junar.zip', 'wb') as out:
            data = response.read() # a `bytes` object
            out.write(data)
        shutil.move('junar.zip','/tmp')
    with zipfile.ZipFile('/tmp/junar.zip', 'r') as zip_ref:
        zip_ref.extractall('/tmp/')
    with open('/tmp/junar1.in') as f:
        return f.read()


texts:
My goal is to continually update an object and record any changes and the times they were made at. The input has to be JSON but the output/storage format doesn't.

This function will check a JSON object/dictionary against a reference object and return a list containing the updated object and another object containing just the updates.

I've tested it a bit and I think it handles inconsistencies in the objects, such as the old object not having the correct nesting.

It only compares new to old, so any missing keys in the new version won't be removed from the old version. It's not memory-impervious but there's no recursion so depth itself isn't a problem.

How can I make my approach more efficient? (Faster, less steps involved)

def getFromDict(dataDict, mapList):
    return reduce(lambda d, k: d[k], mapList, dataDict)

def setInDict(dataDict, mapList, value):
    getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value

def record_changes(obj, keys, key, value):
    for _ in xrange(len(keys)+10):
        try:
            setInDict(obj, keys+[key], value)
            return obj
        except:
            for i in range(len(keys)+1)[1:]:
                try:
                    assert isinstance(getFromDict(obj, keys[:i]), dict)
                except:
                    setInDict(obj, keys[:i], {})
    else:
        raise Exception

def compare_object(N, record):
    """
    :param:N new object
    :param:record old object
    """
    changes = {}
    D = N.copy()
    nested_keys = []
    for key in D:
        if isinstance(D[key], dict):
            nested_keys.append([key])
        else:
            if D[key] != record.get(key, None):
                record[key] = D[key]
                changes[key] = D[key]
    for keys in nested_keys:
        d = getFromDict(N, keys)
        for key in d:
            if isinstance(d[key], dict):
                nested_keys.append(keys+[key])
            else:
                try:
                    if d[key] != getFromDict(record, keys+[key]):
                        setInDict(record, keys+[key], d[key])
                        changes = record_changes(changes, keys, key, d[key])
                except:
                    # record = Help(D, record, keys+[key])
                    record = record_changes(record, keys, key, d[key])
                    changes = record_changes(changes, keys, key, d[key])
    return [record, changes]

Usage:

>>> new = {'a':{'b':1, 'q':0}, 'c':5}
>>> old = {'a':{'b':2, 'q':0}, 'f':9}
>>> print compare_object(new, old)
[{'a': {'q': 0, 'b': 1}, 'c': 5, 'f': 9}, {'a': {'b': 1}, 'c': 5}]


texts:
I have this array of objects:

const array = [
  {continent: 'Africa', country:'Algeria', year:'2018', value:10},
  {continent: 'Africa', country:'Algeria', year:'2017', value:15},
  {continent: 'Africa', country:'Algeria', year:'2016', value:2},
  {continent: 'Africa', country:'Egypt', year:'2018', value:20},
  {continent: 'Africa', country:'Egypt', year:'2017', value:1},
  {continent: 'Africa', country:'Egypt', year:'2016', value:20},
]

I want a new array with the mean by year:

const result = [
  {continent: 'Africa', country:'', year:'2018', value:15},
  {continent: 'Africa', country:'', year:'2017', value:8},
  {continent: 'Africa', country:'', year:'2016', value:11},
]

This is my code:

const yearsTemp = Array.from(new Set(dataset.map((datum: any) => datum.year)).values())
const years = yearsTemp.map(year => year.toString())
const means = years.map(year => {
  const filtered = filter(dataset, { continent: continent, year: year })
  const mean = filtered.reduce((acc, item) => acc + item.value, 0) / filtered.length
  return {
    continent: dataset[0].continent,
    country: '',
    year: year,
    value: mean,
  }
})

Is there a better way?


texts:
I have a small and weak microcontroller. I also don't have access to the complex library. I wrote this iterative version of the FFT to hopefully get better performance than the recursive version, and also just to learn how the FFT works, and brush up on C.
I'd like to know if the code itself is understandable, and if there are some performance improvements I can make.
#include <stdio.h>
#include <math.h>

#define FALSE 0
#define TRUE 1
#if !defined(M_PI)
#   define M_PI 3.14159265358979323846
#endif

struct complex {
    double real;
    double imaj;
};
typedef struct complex complex_t;

complex_t complex_init(const double real, const double imaj) {
    complex_t temp;
    temp.real = real;
    temp.imaj = imaj;
    return temp;
}

complex_t complex_add(const complex_t a, const complex_t b)
{
    return complex_init(a.real + b.real, a.imaj + b.imaj);
}

complex_t complex_subtract(const complex_t a, const complex_t b) {
    return complex_init(a.real - b.real, a.imaj - b.imaj);
}

complex_t complex_multiply(const complex_t a, const complex_t b) {
    return complex_init(a.real * b.real - a.imaj * b.imaj, a.real * b.imaj + a.imaj * b.real);
}

_Bool is_power_of_2(const unsigned int x) {
    return x != 0 && (x & (x - 1)) == 0;
}

_Bool fft(complex_t* input, complex_t* output, const unsigned int size) {
    if (!is_power_of_2(size))
        return FALSE;

    if (size == 1) {
        output[0] = input[0];
        return TRUE;
    }

    const unsigned int half_size = size / 2;

    // Initial loop. Do the input shuffle and first butterfly at the same time.
    // shuffle is the bit reversed representation of i. If i is 11000, then shuffle is 00011.
    for (unsigned int skip = size, i = 0, shuffle = 0; i < half_size; ++i) {
        const complex_t even = input[shuffle];
        const complex_t odd = input[shuffle + half_size];
        output[i * 2] = complex_add(even, odd);
        output[i * 2 + 1] = complex_subtract(even, odd);

        if (i == 0 || is_power_of_2(i + 1)) {
            skip /= 2;
            shuffle = skip / 2;
        } else
            shuffle += skip;
    }

    // Do the rest of the butterfly operations
    for (unsigned int even_to_odd = 2; even_to_odd < size; even_to_odd *= 2) {
        const double angle = -M_PI / even_to_odd;
        const complex_t partial_rotation = complex_init(cos(angle), sin(angle));
        complex_t current_rotation = complex_init(1, 0);
        for (unsigned int i = 0, to_even = 0; i < half_size; ++i, ++ to_even) {
            if (i % even_to_odd == 0) {
                to_even = 2 * i;
                current_rotation = complex_init(1, 0);
            }
            complex_t* even = output + to_even;
            complex_t* odd = even + even_to_odd;
            const complex_t odd_rotated = complex_multiply(*odd, current_rotation);
            *odd = complex_subtract(*even, odd_rotated);
            *even = complex_add(*even, odd_rotated);
            current_rotation = complex_multiply(current_rotation, partial_rotation);
        }
    }
    return TRUE;
}


texts:
Currently, I have a function that loops to gather input for program execution, but I feel that it is a bit lacking, overthought and unoptimized. I would love any input on the process, coding style, or usability of the function.

I feel like the use of while (true) is not really optimized here.

My code uses curses and unistd.h.

// Heart of the display of the program
void startEvolutionLoop (Grid* grid, size_t speed) {
    clear();

    // Infinitely loops until 'q' is pressed
    while (true) {
        // First of all displays the grid with supporting information
        displayGrid(grid);
        printw("[Space] Evolve once        [Enter] Run evolutions        [q] Exit\n");

        // Gets the most recently touched character
        int c = getch();

        //  - if space (' ') evolves and loops again
        //  - if ('q') ends the loops
        //  - if enter ('\n') starts a sub-loop similar to the main one, but waits for any character to stop
        if (c == ' ') {
            grid->evolve();
        } else if (c == 'q') {
            return;
        } else if (c == '\n') {
            nodelay(stdscr, TRUE); // Getting of the input is asyncronous
            // Infinitely loops until any key is pressed
            while (true) {
                grid->evolve();
                displayGrid(grid);

                printw("Press any key to end execution...                       \n"); // Extra whitespace to override previously written stuff in this area

                // If there has been something inputted, then break out of this sub-loop
                if (getch() != ERR) {
                    break;
                }

                usleep((__useconds_t)speed * 1000); // Sleep the desired amount of time (in micro seconds converted to milliseconds)
            }
        }
    }
}

For context (which is not necessary), you can check out my GitHub Repo.


texts:
Print the following pattern, when you invoke inverse_cascade(1234)

1
12
123
1234
123
12
1

Solution:

def inverse_cascade(n):
    def grow(n):
        if n < 10:
            print(n)
        else:
            grow(n // 10)
            print(n)
    def shrink(n):
        if n < 10:
            print(n)
        else:
            print(n)
            shrink(n // 10)
    grow(n // 10)
    print(n)
    shrink(n // 10)

Using a recursion approach, how can I improve this code without using higher order functions?

Note: No data models have to be used.


texts: Filling A Linked List With Data From File And Handling User Status Messages

texts:
I have completed an university assignment on C. While the code is fully functional based on the specifications of the exercise, I like high-quality code and would like to ask for opinions on how can certain parts of it be improved. Be advised that I cannot change any function signature.

The first part is the below load() function whose job is to create and fill a linked list based on entries of a file. If the specified file doesn't exist then a new file must be created. Unfortunately, due to the structure of the signature, I am forced to fclose() the file (opening / closing files are expensive operations) as I don't have any way to store the file pointer.

I don't quite like the double fopen() call there. Also, while I've found a way to detect when a file is blank or when it has wrong format, I still have the feeling that there are cases where undefined behavior can occur.

studentList* load(char *filename)
{
    studentList *list = newList();

    FILE *f = fopen(filename, "r");
    if (f == NULL)
    {
        f = fopen(filename, "w");
        fclose(f);
        printf("File %s not found. A new blank file has been created.\n",filename);
        return list;
    }

    fseek(f, 0, SEEK_END);
    if (ftell(f) != 0)
    {
        rewind(f);
        while (!feof(f))
        {
            student st;
            int res = fscanf(f, "%d%s", &st.id, st.name);
            if (res == 2)
                add(list, st);
        }
        fflush(f);
        fclose(f);
    }
    printf("File %s loaded successfully.\n",filename);
    return list;
}

The second part is more generic and has to do with the way status messages are printed to the console for the user. Excluding load() where I don't have any other option due to the nature of the signature , the other functions that provide basic functionalities of a linked list like add() , delete() , find() etc. don't have any printf() or puts() as I consider that method a dirty code style and besides that, I want to have full control of when a status message is printed.

Instead, these functions return an status code int where then an other function takes that status message and prints the corresponding status message to the console. I have not found a better method to achieve this result. Any ideas in this part would be highly appreciated.

Example of my code:

int res = add(list, st);
printAddStudentResult(res);


texts: Generate n appointments in the next m months

texts:
I work with an API where I need to write code to generate n appointment dates in the coming m months (from today). For the purpose, I write a Random date generator class provided. One condition is I need to use the java.sql.Date for the class.

import java.sql.Date;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Objects;
import java.util.Random;
public class RandomDate {

    private final Random random;

    private final Date currentDate;
    private final int months;

    public RandomDate(Random random, Date currentDate, int months) {

        this.random = random;
        this.currentDate = currentDate;
        this.months = months;
    }

    public Date getRangeEndDate() {

    Calendar calendar = new GregorianCalendar();

    calendar.setTimeInMillis(this.currentDate.getTime());
    calendar.add(Calendar.DATE, this.months * 30);

    return new Date(calendar.getTimeInMillis());
}

public Date generateRandomDate(Date endDate) {

    int start = (int) this.currentDate.toLocalDate().toEpochDay();
    int end = (int) endDate.toLocalDate().toEpochDay();

    long randomDay = start + random.nextInt(end - start);
    return Date.valueOf(LocalDate.ofEpochDay(randomDay));
}

}

I have an issue that within the code I use the time classes from 3 different packages (java.sql, java.time, and java.util). How can I write it more elegantly?


texts: Calculate the closest point to many hyperbolic paraboloids

texts:
In this question I asked for a way to compute the closest projected point to a hyperbolic paraboloid using python.

Using the iterative approximation answer, I'm able to use the code below to calculate the closest point to multiple paraboloids.

import numpy as np
np.set_printoptions(suppress=True)

# This function calculate the closest projection on a hyperbolic paraboloid
# As Answered by @Jaime https://stackoverflow.com/questions/18858448/speeding-up-a-closest-point-on-a-hyperbolic-paraboloid-algorithm

def solve_uv(p0, p1, p2, p3, p, tol=1e-6, niter=100):
    a = p1 - p0
    b = p3 - p0
    c = p2 - p3 - p1 + p0
    p_ = p - p0
    u = 0.5
    v = 0.5
    error = None
    while niter and (error is None or error > tol):
        niter -= 1

        u_ = np.dot(p_ - v*b, a + v*c) / np.dot(a + v*c, a + v*c)
        v_ = np.dot(p_ - u*a, b + u*c) / np.dot(b + u*c, b + u*c)
        error = np.linalg.norm([u - u_, v - v_])
        u, v = u_, v_
    return np.array([u, v])

# Generate random hyperbolic paraboloids
COUNT = 1000
p0 = np.random.random_sample((COUNT,3))
p1 = np.random.random_sample((COUNT,3))
p2 = np.random.random_sample((COUNT,3))
p3 = np.random.random_sample((COUNT,3))
p = np.random.random_sample(3)

iterated = np.empty((COUNT,2))
for i in xrange(COUNT):
    iterated[i,:] = solve_uv(p0[i], p1[i], p2[i], p3[i], p)

This works fine, but obviously doesn't scale well and becomes very slow when dealing with large data sets.

To remedy this I made some changes to vectorize the function. It will stop iterating until all the given items pass the tolerance test or reach the maximum iteration count. I also use inner1d from numpy.core.umath_tests instead of np.dot and np.linalg.norm because in my tests inner1d performs better with large data sets.

import numpy as np
from numpy.core.umath_tests import inner1d

def solver_vectorized(p0, p1, p2, p3, p, tol=1e-6, niter=100):
    a = p1 - p0
    b = p3 - p0
    c = p2 - p3 - p1 + p0
    p_ = p - p0
    u  = 0.5 * np.ones((p0.shape[0],1))
    v  = 0.5 * np.ones((p0.shape[0],1))

    # Init index list of elements to update
    mask = np.arange(p0.shape[0])

    while niter and mask.shape[0]:
        niter -= 1

        avc = a[mask] + v[mask]*c[mask]
        buc = b[mask] + u[mask]*c[mask]
        u_ = (inner1d(p_[mask] - v[mask]*b[mask], avc) / inner1d(avc,avc))[:,np.newaxis]
        v_ = (inner1d(p_[mask] - u[mask]*a[mask], buc) / inner1d(buc,buc))[:,np.newaxis]

        # Calculate delta and update u,v
        delta = np.hstack([u[mask]-u_, v[mask]-v_])
        delta = inner1d(delta,delta)**0.5
        u[mask], v[mask] = u_, v_

        # Update the index list only with items which fail the tolerance test
        mask = mask[np.where(delta > tol)]

    # Return uv's
    return np.hstack([u, v])

vectorized = solver_vectorized(p0, p1, p2, p3, p)
print np.allclose(iterated,vectorized) #Returns: True

Question

On very large data sets (over 100,000 items) the vectorized version will solve in ~1.5 seconds. But I am sure there are some inefficiencies that could be improved. For example the square root could probably be dropped for a small performance increase. Or by using masks differently. How would you change the function to improve its performance?


texts:
My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you what could be done better.

Some background: This application I'm building now is for use in a school project with people that don't have much knowledge of PHP. Therefore I'm not using a framework like Laravel, or implementing a whole template engine, but I'm trying it to keep as simple as possible while still using classes and code separation.

The homeController:

Class homeController extends BaseController
{

    public function index()
    {
        $this->view->make("common/home");
    }
}

The View class:

Class View{

    public static $file;

    public function make($file){
        View::$file = $file;
        require_once DIR_VIEW . "default/template.php";
    }
}

The template.php file:

<!DOCTYPE html>
<html>
<head>
    <title>Just an title</title>
</head>
<body>
    <?php require_once 'header.php'; ?>

    <div id="mainContent">
        <?php require_once DIR_VIEW . View::$file . ".php"; ?>
    </div>

    <?php require_once 'footer.php'; ?>
</body>
</html>

BaseController:

class BaseController
{

    public $load;
    public $url;
    public $view;

    public function __construct()
    {
        $this->load = new Loader();
        $this->url = new Url();
        $this->view = new View();
    }
}

The files are required by an loader class with this piece of code: (don't know if it ads some relevance)

foreach (glob(DIR_LIB . "*.php") as $filename) {
    require_once $filename;
}

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but I know that that isn't the solution.

What do you think? Is this good enough or not even close?

Note: there will always be 1 file included, so that's why I thought that a simple require would succeed.


texts: Compute the height of a tower made of buckets

texts:
I need to speed up my algorithm. It is about finding the height of a tower. The tower is built from buckets. Each bucket has height and radius (1 <= height,radius <= 1000). Variable bucketCount describe how many buckets are placed on tower (1 <= bucketCount <= 10^6). We set buckets in sequence. Thickness of bucket is 0 (for simplicity)

Image of example tower:

I decided to use a stack. My algorithm for each bucket:

If stack is empty, then push bucket to stack.
If the bucket I hold is narrower then bucket on top then put it on the stack.
Else while bucket I hold is wider: pop and find maximum height (for new bucket ground), after that push bucket which I hold.

For each bucket I added additional an variable ground, which specifies the height at which the bucket is placed. Meantime I keep maximum height of tower in variable.

This task is automatically checked and gives output: time limit exceeded. I tried to optimize code but nothing gives positive result.

Example input: 2 20 20 30 30
Output: 50

#include <iostream>
#include <stack>

using namespace std;

struct Bucket{
public:
    int height;
    int radius;
    int ground;
};

int main()
{
    int ile;
    cin >> ile;
    for(int c = 0; c < ile; c++){
    stack<Bucket> tower;
    int hightestPoint = 0;
    int bucketCount;
    cin >> bucketCount;
    Bucket temp;
    Bucket temporary; //Used to speed up a bit
    int maksimum;
    int sum;
    int counter = 0; //Its size of the Stack. Used it because thought its faster then empty() function
    for(int i = 0; i < bucketCount; i++){
        cin >> temp.radius >> temp.height;
        maksimum = -1;
        sum = 0;
        if(counter == 0){
            temp.ground = 0;
            tower.push(temp);
            counter++;
        } else {
            if(temp.radius < tower.top().radius){ //If bucket is narrower then push it
                temp.ground = tower.top().ground;
                tower.push(temp);
                counter++;
            } else { //If bucket is wider
                while((counter > 0) && temp.radius >= (temporary = tower.top()).radius){ //Pop and search for new ground (maximum)
                    sum = temporary.height + temporary.ground;
                    if(maksimum < sum){
                        maksimum = sum;
                    }
                    tower.pop();
                    counter--;
                }
                temp.ground = maksimum; //Set ground for new bucket
                tower.push(temp);
                counter++;
            }
        }
        sum = temp.height + temp.ground; //Meantime find highest point in stack
        if(hightestPoint < sum){
            hightestPoint = sum;
        }
    }
    cout << hightestPoint << endl;
    }

    return 0;
}


texts:
I was using such code:

class Counter
{
    private int i = 0;

    public int Next()
    {
        lock (this)
        {
            return i++;
        }
    }

    public void Reset()
    {
        lock (this)
        {
            i = 0;
        }
    }

}

I refactored it such a way:

class Counter
{
    private int i = 0;

    public int Next()
    {
        return Interlocked.Increment(ref i);
    }

    public void Reset()
    {
        i = 0;
    }

}

Will that work?

I'm using .NET 4.5 if this matters.


texts:
I am wondering about the best way to save on RAM/memory when there are many required arrays (I plan to have many more NPCs and various other arrays with text and items). And I am wondering what I can improve on so far.

  #include <iostream>
  #include <cstring>

  #define NUM_NPC 10       // Number of NPC players

  #define DETAILS_HEARTS 0 // Heart Position
  #define DETAILS_BDMON  1 // Birthday Month Position
  #define DETAILS_BDDAY  2 // Birthday Day Position
  #define DETAILS_GENDER 3 // NPC Gender

  using namespace std;

  class npcPlayers {
     public:
        int getHearts(int playerID);
        int getItem(int playerID, int itemID);
        int getBirthdayMonth(int playerID);
        int getBirthdayDay(int playerID);
        char *getName(int playerID);
        npcPlayers();
     private:
        int npcDetails[NUM_NPC][10];
        char npcNames[NUM_NPC][20];
        int updateHearts(int PlayerID,int value);
  };

  npcPlayers::npcPlayers() {
     npcNames = {"Ace","Amber","Benny","Ethan","Michael","Jay","William","Mia","Chloe","Emily"};
     npcDetails = {
        {0,3,5,0},    // Ace
        {0,9,20,1},   // Amber
        {0,1,10,0},   // Bennny
        {0,12,30,0},  // Ethan
        {0,2,17,0},   // Michael
        {0,6,10,0},   // Jay
        {0,8,9,0},    // William
        {0,4,1,1},    // Mia
        {0,5,13,1},   // Chloe
        {0,8,5,1}     // Emily
     };
  }

  int npcPlayers::getHearts(int playerID) {
     return npcDetails[playerID][DETAILS_HEARTS];
  }
  int npcPlayers::getItem(int playerID, int itemID) {

  }
  int npcPlayers::getBirthdayDay(int playerID) {
     return npcDetails[playerID][DETAILS_BDDAY];
  }
  int npcPlayers::getBirthdayMonth(int playerID) {
     return npcDetails[playerID][DETAILS_BDMON];
  }
  char *npcPlayers::getName(int playerID) {
     return npcNames[playerID];
  }

  int main() {
     npcPlayers npc;
     cout << "Player 1 Details:" << endl;
     cout << "Name: " << npc.getName(1) << endl;
     cout << "Hearts: " << npc.getHearts(1) << endl;
     cout << "Birthday: " << npc.getBirthdayMonth(1) << "/" << npc.getBirthdayDay(1) << endl;
     return 0;
  }


texts:
My code works as expected but I am not sure how to optimize the code. The purpose of the code is to find files with given parameters and copy them to a separate folder.

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.IOException;
import java.awt.Frame;
import java.awt.image.RenderedImage;
import javax.media.jai.widget.ScrollingImagePanel;
import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;

public class FileSearch {

    int sizeOfFile = Integer.parseInt((JOptionPane.showInputDialog(null,"Enter Size: ")));

    public void findFiles(File root) throws IOException {

        File[] listOfFiles = root.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) {
            String iName = listOfFiles[i].getName();
            if (listOfFiles[i].isFile() && iName.endsWith(".tif")) {

                RenderedImage renderedImage[], page;
                File tiffFile = new File(listOfFiles[i].getAbsolutePath());

                SeekableStream seekableStream = new FileSeekableStream(tiffFile);
                ImageDecoder imageDecoder = ImageCodec.createImageDecoder("tiff",seekableStream, null);
                renderedImage = new RenderedImage[imageDecoder.getNumPages()];

                int tifPageCount = 0;
                for (int k = 0; k < imageDecoder.getNumPages(); k++) {
                    renderedImage[k] = imageDecoder.decodeAsRenderedImage(k);
                    tifPageCount++;
                }

                long fileSize = listOfFiles[i].length();

                if ((fileSize <= (sizeOfFile*1000)) && (tifPageCount <=15)) {
                    File file = new File("\\\\server\\filename\\TestFiles\\" + listOfFiles[i].getName());

                    if (file.exists()) {
                        System.out.println(file.getName() + " already exists");
                    } else {
                        File folder = new File("\\\\server\\filename\\TestFiles\\Files by Size\\Files of size " + ((sizeOfFile*1000) / 1000) + " KB");
                        if (!folder.exists()) {
                            if (folder.mkdir()) {
                                System.out.println("Folder : " + folder.getAbsolutePath());
                            }
                        }

                        try{
                            System.out.println(listOfFiles[i].getName() + " | " + FileUtils.byteCountToDisplaySize(fileSize));
                            FileUtils.copyFileToDirectory(listOfFiles[i], folder);
                        }
                        catch(IOException e){
                            System.out.println("Same files");
                            return;
                        }
                    }
                }
            }

            else if (listOfFiles[i].isDirectory()) {
                findFiles(listOfFiles[i]);
            }
        }
    }
}


texts: Invocable callout(Post) from processbuilder

texts:
I am relatively new in making (invocable)callouts. My main concern is if I should be using smaller parts for example for the body and using a query only once in this case. Any suggestions would be appreciated.

Class:

public class or_service {
    @InvocableMethod
    public static void InvocePostProperty(List<ID> pIds) {
        for (Id pId : pIds) {
            PostProperty(pId);
        }
    }
    @future(callout=true)
    public static void PostProperty(Id pId) {
        List<Orbirental_Service__c> os = null;
        List<Property__c> prop = null;
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        try {
            os = [SELECT Url__c, ApiKey__c, agencyUid__c FROM Orbirental_Service__c LIMIT 1];
            prop = [SELECT id,  Name__c, BaseGuests__c, maximumGuests__c, baseDailyRate__c, city__c, state__c, acceptInstantBook__c, isActive__c ,uid__c
                    FROM Property__c  WHERE id =:pId LIMIT 1];
        }catch(QueryException ex) {
            os = null;
            prop = null;
        }
        request.setEndpoint(os[0].Url__c +'v1/properties/');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json');
        request.setHeader('X-ORBIRENTAL-APIKEY', os[0].ApiKey__c);
        // Set the body as a JSON object
        request.setBody('{"type": "HOUSE"'+
                        ',"name":' + prop[0].Name__c +
                        ',"agencyUid":' + os[0].agencyUid__c +
                        ',"baseGuests":' + prop[0].BaseGuests__c +
                        ',"maximumGuests":' + prop[0].maximumGuests__c +
                        ',"baseDailyRate":' + prop[0].baseDailyRate__c +
                        ',"city":' + prop[0].city__c +
                        ',"state":'+ prop[0].state__c +
                        ',"acceptInstantBook":'+ prop[0].acceptInstantBook__c +
                        ',"isActive":'+ prop[0].isActive__c+
                        '}');
        HttpResponse response = http.send(request);
        // Parse the JSON response
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
            updateProperty(response.getBody(),pId);
        }
    }

    public static void updateProperty(string jsonString, Id pId) {
        List<Property__c> prop = null;
        or_propertyJSON propClass = or_propertyJSON.parse(jsonString);
        System.debug('myClass.uid '+ propClass.uid);
        try {
            prop = [SELECT id,  Name__c, BaseGuests__c, maximumGuests__c, baseDailyRate__c, city__c, state__c, acceptInstantBook__c, isActive__c ,uid__c
                    FROM Property__c  WHERE id =:pId LIMIT 1];
        }catch(QueryException ex) {
            prop = null;
        }
        prop[0].uid__c = propClass.uid;
        update prop;
    }
}


texts:
This question is part of a series solving the Rosalind challenges. For the previous question in this series, see A sequence of mistakes. The repository with all my up-to-date solutions so far can be found here.

Problem: PRTM

  In a weighted alphabet, every symbol is assigned a positive real number called a weight. A string formed from a weighted alphabet is called a weighted string, and its weight is equal to the sum of the weights of its symbols.

  The standard weight assigned to each member of the 20-symbol amino acid alphabet is the monoisotopic mass of the corresponding amino acid.

Given:

  A protein string \$P\$ of length at most 1000 aa.

Return:

  The total weight of \$P\$. Consult the monoisotopic mass table.

Sample Dataset:

SKADYEK

Sample Output:

821.392

My solution solves the sample dataset and the actual dataset given.

Dataset:

FQLCECNPKWNEEKRQYGACNQRNEYQDMKMVLRLNTRALAFLVEKFCTQATANSHGWQYRIENPLQLNHLPLTICRHGFDEDRIQRYQDDHNGVHIHVAMQITHSEWVLETWASSHQHANISPLSGNSQRYYPANHEGRRIQCCQFDCESHLMEDLWREATRSHESKVTRFKHDHNTPLHFTVFWPSDMEHDNGKTEYKSGWQLMFKIEGSVGKECELHTSYFSMKVKYTTVEIQTNPIERMCYAELVYWLRAEMEGVAKDRDHKWAMEHEQYIYSPMEGLINSVEYHMVYPQMARSIAKDRYWTPATLRRIHVWYCREIVQIMWIDHPKGLVCVMPDSNVGALIKWYTHYTDVKLETHQTCRKNNAGMVRDGRKKVAAEVFIIYEMGMPEYKPHEDANRDYRIPIDHPWRPHNNMCFGISGEWRRSCMKHQIQLSRWWANITKIMPMRWSIESGTTLRFVQGMVYRVNRFDDRYPRMNDCYWDAIWVCGAWTMHLTVPDLLMQMHHCMGAIHDVVFSEMGVKIAYQAVWFASVRIPASFQDWFCHEDHFQRGLRKCKLEHNDVLYETWIAMEPASYHTVHHCWHWREVFCHPCVFIHQMYSYPEFHIPDVELSLHYSQIWMNRETGWSPIRMELMCTSAPPNPDGGTPLLSFWYFGFIIQRHCGCETIMIGQWDTCFNSHHDVWAVWVHPENSQFTKERICCASFTDNSDTRECKQTVQHSSPGPTELWADSKTQVQWWYISQWLGKGLMYSEFQVSISGLSTPMLVYWLPNSMIEMNDVMEILNSPAGAQITTCIAGVNFRMIFETSWRTWYINRH

Output:

97283.202

PRTM.rb:

module Rosalind
  RESIDUE_BY_MONOISOTOPIC_MASS = {
    71.03711  => ["A"],
    103.00919 => ["C"],
    115.02694 => ["D"],
    129.04259 => ["E"],
    147.06841 => ["F"],
    57.02146  => ["G"],
    137.05891 => ["H"],
    113.08406 => ["I", "L"],
    128.09496 => ["K"],
    131.04049 => ["M"],
    114.04293 => ["N"],
    97.05276  => ["P"],
    128.05858 => ["Q"],
    156.10111 => ["R"],
    87.03203  => ["S"],
    101.04768 => ["T"],
    99.06841  => ["V"],
    186.07931 => ["W"],
    163.06333 => ["Y"]
  }

  MONOISOTOPIC_MASS_BY_RESIDUE = RESIDUE_BY_MONOISOTOPIC_MASS.
    flat_map { |c, as| as.map { |a| [a, c] } }.to_h

  def self.problem_prtm(aminoacids_string)
    aminoacids_string.
      scan(/./).
      map { |residue| MONOISOTOPIC_MASS_BY_RESIDUE[residue] }.
      take_while { |aminoacid| aminoacid != "" }.
      inject(0){ |sum, x| sum + x }
  end
end

user_input = gets.chomp
puts '%.3f' % Rosalind.problem_prtm(user_input)

The problem statement is quite similar to PROT, so the code looks quite similar to the answers given there.

Basically, I declare a bunch of value-key pairs which get inverted. This saves me from having the same value multiple times in the hash (when multiple residue weigh the same amount).

I'm mainly looking for idiomaticness and re-usability. I'm pretty sure my take_while could be written better and my naming is always open for critique.


texts:
I left questions in the comments strategically, since I think it's easier to answer questions this way.

Basically what my small class does is abstracts away functionality that was originally in a function.

This class reads json form a file and creates an object which I can use to traverse the json. I understand that if something can be made into a function it shouldn't be made into a class, but I need to practice.

Below I outlined things I'm looking for. Choose one or choose all.

Looking for:

Did I use references "&" correctly
#include header files correctly
A way to initialize object like so: Root rate("test.json", ["query"]["results"]["rate"]) (syntax can be different)
Best practice advice

Not looking for (at least not yet):

Exceptions and Error handling
Advice form C programmers

root.h

#ifndef ROOT_H
#define ROOT_H

// Should header files always be included in root.h as opposed to root.cpp?
#include <fstream>
#include <string>   // Seems like I do not need to include this string container, why?
                    //is it because json/json.h contains it?

#include "json/json.h" // Would I ever create a class with a dependency like this one?
                       // jsoncpp.sourceforge.net/annotated.html

class Root
{
private:
    std::ifstream m_json;

public:
    Json::Value m_root;
    Json::Value m_query;

    Root(const std::string&);
    ~Root();
};

#endif // ROOT_H

root.cpp

#include "root.h"

Root::Root(const std::string & filename) : m_json(filename, std::ifstream::binary)
// Is std::ifstream::binary ok to put it in here like this ^?
// It's working, but would that be good practice?
{
    m_json >> m_root;
    m_json.close();  // Do I need .close() here?

}
Root::~Root(){}

main.cpp

#include <iostream>
#include "root.h"

int main()
{

    Root rate("test.json");
    rate.m_query = rate.m_root["query"]["items"]; // Is it ok to assign member to a member like so,
                                                  // as opposed to just a variable?

    // How can I instantiate my object like the line below?
    // Root rate("test.json", ["query"]["results"]["rate"]);
    // Syntax does not have to match precisely?

    for(const auto & it : rate.m_query)
    {
        std::cout << it << std::endl;
    }
}

test.json

{
  "query": {
    "count": 3,
    "items": [
      {"item": "4"},
      {"item": "3"},
      {"item": "2"}
    ]
  }


texts: RESTful API echo server using Python bottles

texts:
I just wrote an echo server that responds to curl requests with the method used and the data sent. Sticking to GET and POST for now, but I would like to know if there's something I can do to improve my RESTful API.

I'm supposed to:

Only be able to call the /data endpoint
Only allow JSON parameters
Use best practices of coding a RESTful API

Expected JSON response:

{
  "method": <HTTP METHOD REQUESTED>,
  "data": <HTTP PARAMETERS SENT IN THE REQUEST>
}

Here is my current code:

from bottle import run, post, request, response, get, route, put, delete
from json import dumps
import json

@get('/data')
def data():
    #if headers are not specified we can check if body is a json the following way
    #postdata = request.body.read()
    postdata = request.json
    try:
        #rv = {"method": request.method, "data": json.loads(postdata.decode('utf-8'))}
        rv = {"method": request.method, "data": postdata}
    except:
        raise ValueError
    response.content_type = 'application/json'
    return dumps(rv)

@post('/data')
def data():
    #postdata = request.body.read()
    postdata = request.json
    try:
        #rv = {"method": request.method, "data": json.loads(postdata.decode('utf-8'))}
        rv = {"method": request.method, "data": postdata}
    except:
        raise ValueError
    response.content_type = 'application/json'
    return dumps(rv)

Everything seems to work fine for now. I'm looking to improve my code so any feedback is appreciated.


texts:
Recently Torbjörn Söderstedt shipped the chicken programing specification. The basic premise of chicken is this: Only the word chicken, spaces and line breaks may be used. It was inspired by this paper and this talk.

Along with the specification he also created a javascript function that implements the chicken VM. Sadly (or joyfully?) he wrote the function with little more than the word chicken and thus mad if very very hard to understand.

I was wondering, could anyone do a code review of chicken.js? Maybe add a bunch of in-line comments?

function chicken(CHICKEN, Chicken) {
    Chicken &&( chicken. chicken =[,
    CHICKEN, CHICKEN = Chicken = chicken.
    $Chicken =-( CHICKEN ==( chicken.
    Chicken = Chicken ))], chicken.
    chicken [Chicken++] = chicken. chicken, chicken.
    CHICKEN = ++Chicken, chicken (--Chicken), chicken.
    $Chicken = ++Chicken, chicken. CHICKEN++ );
    Chicken = chicken. Chicken [chicken.
    $Chicken++ ]; chicken. Chicken = CHICKEN? Chicken?
    '\012'== Chicken? chicken (++ CHICKEN, chicken.
    chicken [++ chicken. CHICKEN ]=
    CHICKEN - CHICKEN ): Chicken
    ==' '|'\015'== Chicken ||
    (Chicken   )== "c" &  chicken. Chicken [chicken.
    $Chicken++ ]== "h" &  chicken. Chicken [chicken.
    $Chicken++ ]== "i" &  chicken. Chicken [chicken.
    $Chicken++ ]== "c" &  chicken. Chicken [chicken.
    $Chicken++ ]== "k" &  chicken. Chicken [chicken.
    $Chicken++ ]== "e" &  chicken. Chicken [chicken.
    $Chicken++ ]== "n"&&++chicken. chicken [chicken.
    CHICKEN]? chicken (CHICKEN)
    :[ "Error on line "+CHICKEN+": expected 'chicken'",
       chicken. CHICKEN = CHICKEN ++- CHICKEN ]:
    chicken. chicken :( CHICKEN = chicken.
    Chicken[chicken.CHICKEN], Chicken? (Chicken =

    --Chicken? --Chicken? --Chicken? --Chicken? --Chicken?
    --Chicken? --Chicken? --Chicken? --Chicken?
    chicken. CHICKEN++ &&
    --Chicken :'&#'+CHICKEN+';': chicken.
    Chicken [chicken. Chicken [-- chicken. CHICKEN ]&&
    (chicken. $Chicken += CHICKEN), --chicken.
    CHICKEN ]: chicken. Chicken [chicken.
    Chicken [CHICKEN] = chicken. Chicken
    [-- chicken. CHICKEN ],-- chicken. CHICKEN ]:
    chicken. Chicken [chicken. Chicken [chicken.
    $Chicken++ ]] [CHICKEN]: CHICKEN == chicken.
    Chicken [-- chicken. CHICKEN ]:
    CHICKEN*chicken. Chicken [-- chicken.
    CHICKEN ]: chicken. Chicken [-- chicken.
    CHICKEN ]- CHICKEN: chicken. Chicken [-- chicken.
    CHICKEN ]+ CHICKEN: chicken.
    CHICKEN ++ && "chicken", chicken.
    Chicken [chicken. CHICKEN ]= Chicken, chicken
    ()): CHICKEN );

    return chicken.
    Chicken
}


texts:
Find all pairs in an array that sum to a given number without using HashMap.
  Duplicate pairs are not allowed. Input array cannot be modified.

  input:   {-2, -1, -1, 5, 7, 7, 7, 7, 8}, target = 7

  output: (-1, 8)

As it is mentioned, using HashMap is not allowed, so I decided to use binary search.

GitHub

public class TwoSumProblemUsingBinarySearch {

    public static class Pair {

        private final int x;
        private final int y;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }

        @Override
        public boolean equals(Object other) {
            if (other instanceof Pair) {
                Pair o = (Pair) other;
                return this.x == o.x && this.y == o.y;
            }

            return false;
        }

        @Override
        public String toString() {
            return String.format("(%d, %d)", x, y);
        }
    }

    public static Set<Pair> findAllParis(int input[], int target) {
        int numbers[] = Arrays.copyOf(input, input.length);
        Set<Pair> pairs = new HashSet<>();

        Arrays.sort(numbers);

        for (int low = 0, high = input.length - 1; low < high; ) {
            int sum = input[low] + input[high];

            if (sum > target) {
                high--;
            } else if (sum < target) {
                low++;
            } else {
                pairs.add(new Pair(input[low], input[high]));
                high--;
                low++;
            }
        }

        return pairs;
    }
}

@Test
public void findAllParis() throws Exception {

    System.out.println(TwoSumProblemUsingBinarySearch.findAllParis(new int[]{-2, -1, -1, 5, 7, 7, 7, 7, 8}, 7));
    assertEquals(1, TwoSumProblemUsingBinarySearch.findAllParis(new int[]{-2, -1, -1, 5, 7, 7, 7, 7, 8}, 7).size());
}


texts:
I wrote a random world generator for the classic Snakes and Ladders board game.

Snakes and Ladders is a dice game played over a grid, usually 10x10. You win by reaching the last square first. The board has ladders and snakes that connect specific squares. Land at the base of a ladder and you climb to the ladder's head (good). Land on the head of a snake and you slide down to the snake's tail (bad).

Snakes and ladders are key value pairs; the key represents the start point, and the value represents the end point. Both are identical except that a snake's start point must be greater than its end point, while the opposite is true for a ladder.

World rules:

A snake's head can't be on the last square otherwise you can't win the game.
A ladder's base can't be on the last square since there's no where to go.
A square can not contain the base of a ladder and the head of a snake. You either slide, or climb.
Players start off the board.

A square can technically contain the tail of a snake and the base of a ladder, or head of the snake and head of a ladder. But this could have weird side effects (presumably why I've not seen a board with those characteristics): You slide down a snake only to reach the base of a ladder that takes you to the winning square; or you climb up a ladder only to reach the head of a snake that drags you down. While not fatal, such side effects muddle the role definition of ladders and snakes; in that, ladders are no longer necessarily good, and neither are snakes necessarily bad.

Example World

# snakes
{97=>62, 35=>21, 80=>67, 38=>12, 64=>6, 79=>20, 26=>11, 89=>33, 22=>21, 45=>25}
# Ladders
{10=>74, 77=>88, 5=>68, 24=>93, 68=>90, 73=>84, 76=>85, 50=>53, 78=>85, 12=>73}

Builder

  class World
    attr_reader :size, :ladders, :snakes

    def initialize(size, ladders = 10, snakes = 10)
      @size = size
      @snakes = place_snakes(snakes)
      @ladders = place_ladders(ladders)
    end

    def place_snakes(count)
      count.times.each_with_object({}) do |_, hash|
        start_point = rand(snake_habitable_range)
        end_point = rand(snake_habitable_range.begin..(start_point - 1))
        hash[start_point] = end_point
      end
    end

    def place_ladders(count)
      count.times.each_with_object({}) do |_, hash|
        begin
          start_point = rand(ladder_habitable_range)
        end while snakes.keys.include?(start_point)

        begin
          end_point = rand((start_point + 1)..ladder_habitable_range.end)
        end while snakes.keys.include?(end_point)

        hash[start_point] = end_point
      end
    end

  private

    def snake_habitable_range
      2..(size - 1)
    end

    def ladder_habitable_range
      1..(size - 1)
    end
  end


texts:
A naïve quicksort will take O(n^2) time to sort an array containing no unique keys, because all keys will be partitioned either before or after the pivot value. There are ways to handle duplicate keys (like one described in Quicksort is Optimal). The proposed solution only works for the Hoare partition, but I've implemented the Lomuto partition. To deal with duplicate keys, I alternated between moving duplicates to the left of the pivot and moving duplicates to the right of the pivot. Here's my quicksort (ignore the lack of generics):

public static void swap(Comparable[] sort, int a, int b){
    Comparable temp=sort[a];
    sort[a]=sort[b];
    sort[b]=temp;
}
public static void quicksort(Comparable[] sort, int start, int end){
    while(end-start>1){//normal case
        int pivot=gen.nextInt(end-start)+start;//random pivot
        swap(sort, pivot, start);
        int index=start;//walking index
        boolean dupHandler=false;//init to true works also
        for(int i=start+1; i<end; ++i){//Lomuto partition
            int val=sort[start].compareTo(sort[i]);
            if(val>0 || ( val==0 && (dupHandler=!dupHandler) ) )
                swap(sort, ++index, i);
        }
        swap(sort, start, index);
        if(index-start<end-index-1){//recurse into smaller partition
            quicksort(sort, start, index);
            start=index+1;//use iteration for other partition
        }
        else{
            quicksort(sort, index+1, end);
            end=index;
        }
    }
}

Is there a better (more efficient) way to handle duplicate keys? If not, is there any way to significantly improve my code?


texts: Dynamically initializing Context and creating connection string for Web API

texts:
Scenario:
There is a 20 years old windows application with SQL Server as database. There are 42 companies within the Group and each company has its own database.
Now we have started building new applications and all critical data like finances etc need to come from and be written back to old system's database.

To make this easier and uniform across all new applications I've started an API project. Issue obviously is to map to specific database based on company from old databases while also use Entity framework for new databases.

My solution is to use mix of Entity Framework and Dapper.

If there are complex queries required or stored procedures are already present, then we use Dapper else for simple APIs for new databases we use Entity Framework.

I have used Application_BeginRequest event of global.asax to initialize database. I'm passing company code in headers to identify which database to use and form a connection string.

I'm also using Mehdime.Entity to manage transaction scopes and DbContexts.

here is the Application_BeginRequest code

protected void Application_BeginRequest(Object source, EventArgs e)
    {
        var request = Request;
        var requestHeaders = request.Headers.Keys;

        var companyCode = request.Headers.GetValues("CompanyCode");
        var builder = new ContainerBuilder();

        ApiVariables.Company = companyCode?[0];
        var connectionString = ApiVariables.GetConnectionString(ApiVariables.Company);
        // Get your HttpConfiguration.
        var config = GlobalConfiguration.Configuration;

        builder.RegisterGeneric(typeof(ApiRepository<>)).As(typeof(IRepository<>));
        builder.RegisterAssemblyTypes(Assembly.Load("HRQ.Service"))
            .Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerLifetimeScope();

        builder.RegisterType<DbContextScopeFactory>().As<IDbContextScopeFactory>();
        builder.RegisterType<AmbientDbContextLocator>().As<IAmbientDbContextLocator>();
        builder.RegisterType<ApiDbContext>().As<IApiDbContext>().WithParameter("connectionString", connectionString);

        // Register your Web API controllers.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // OPTIONAL: Register the Autofac filter provider.
        builder.RegisterWebApiFilterProvider(config);

        // Set the dependency resolver to be Autofac.
        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        Database.SetInitializer<ApiDbContext>(new CreateDatabaseIfNotExists<ApiDbContext>());
    }

For queries to old database, I use:

using (var connection = DapperConnectionFactory.GetOpenConnection())
        { //execute queries here}

To initiate connection for dapper:

public static DbConnection GetOpenConnection()
    {
        var connection = new SqlConnection(ApiVariables.GetConnectionString(ApiVariables.Company));
        connection.Open();
        return connection;
    }

Am I in right path? or is this a terrible code?


texts: Bitmask type to perform binary operations

texts:
Without any specific implementation details, how would you create a bitmask type that wraps binary arithmetic into something easy to read? Ideally, this would be used with an enum, which is why there are some generic methods.

Some Questions

Is the type a struct or class?
Is the type generic or are certain methods generic?
Is it okay to constrain the generics to struct, IConvertible?
Is a type like this preferred when working with bitmasks?

Starting Example: Here's a fiddle.

//Example Use
var days = new Bitmask((ulong)(Day.Monday | Day.Tuesday));
days.Contains((ulong)Day.Friday); //false
days.Contains((ulong)Day.Monday); //true
//Example Use END

public enum Day { Monday = 1, Tuesday = 2, Wednesday = 4, Thursday = 8, Friday = 16, Saturday = 32, Sunday = 64 }

public struct Bitmask
{
    //Bitmask Value
    private ulong _value;
    public ulong Value
    {
        get { return _value; }
        set { _value = value; }
    }

    //Constructor
    public Bitmask(ulong value) { _value = value; }

    //Methods
    public bool Any() { return _value != 0; }

    public void Clear() { _value = 0; }

    public bool Contains(ulong value) { return (_value & value) == value; }

    public bool Equals(ulong value) { return _value == value; }

    public List<TEnum> ToList<TEnum>() where TEnum : struct, IConvertible
    {
        var local = this;
        var result = ((TEnum[])Enum.GetValues(typeof(TEnum))).Where(m => local.Contains<TEnum>(m)).ToList();
        return result;
    }
}


texts: Grouping a Pandas Dataframe by two parameters

texts:
I have a .csv file of 8k+ rows which looks like this:

                       state                   assembly           candidate  \
0  Andaman & Nicobar Islands  Andaman & Nicobar Islands     BISHNU PADA RAY
1  Andaman & Nicobar Islands  Andaman & Nicobar Islands  KULDEEP RAI SHARMA
2  Andaman & Nicobar Islands  Andaman & Nicobar Islands      SANJAY MESHACK
3  Andaman & Nicobar Islands  Andaman & Nicobar Islands        ANITA MONDAL
4  Andaman & Nicobar Islands  Andaman & Nicobar Islands             K.G.DAS

                                party  votes
0              Bharatiya Janata Party  90969
1            Indian National Congress  83157
2                     Aam Aadmi Party   3737
3        All India Trinamool Congress   2283
4  Communist Party of India (Marxist)   1777

The end dataframe I wanted to get was one which contains all the states as rows and two columns - one which has votes received by a particular party ("Bhartiya Janata Party", in this case) in that row's state and another which has the total votes from the state. Like this:

      State                Total Votes   BJP Votes
Andaman & Nicobar Islands       190328     90969.0
Andhra Pradesh                48358545   4091876.0
Arunachal Pradesh               596956    275344.0
Assam                         15085883   5507152.0
Bihar                         35885366  10543023.0

My code works but I'm pretty sure there's a much better way to get this done using fewer lines of code and without creating too many dataframes. Here's my code:

dff = df.groupby(['party'])[['votes']].agg('sum')
dff = dff.sort_values('votes')

BJP_df = df[df["party"]=="Bharatiya Janata Party"]
#print(BJP_df.head())

group = BJP_df.groupby(['state'])[['votes']].agg('sum')
state = df.groupby(['state'])[['votes']].agg('sum')

result = pd.concat([state, group], axis = 1, sort=False)
result.columns = ["Total Votes","BJP Votes"]

Any tips, suggestions, pointers would be very much appreciated.


texts: Alternative for storing id in hidden input

texts:
Normally, when editing something, say when admin is approving a comment to a product, the id of the comment is stored in <input type="hidden"> to be included during form submission. But with this, just a change to the id may affect other pending comment, or those that are already approved.

So instead I used password_hash to hash the comment id, and stores it with the original comment id in a session array. Then after submitting the form, I check the submitted hashed comment id to the session array, looking for a match, then getting the original comment id.

Here's how I generate the hashed comment id:

foreach ($pendingComments as $comment) {
    $commentId = $comment['Id'];
    $hashed = password_hash($commentId, PASSWORD_DEFAULT);
    $_SESSION['COMMENT_IDS'][] = ['hash'=>$hashed, 'id'=>$commentId];
    $customerName = $comment['GivenName'] . ' ' . $comment['Surname'];
    $message = $comment['Message'];
    $timestamp = $comment['Timestamp'];
    echo '

            ' . $customerName . '
            ' . $message . '
            ' . $timestamp . '

                <form action="" method="POST">
                    <input type="hidden" value="'.$hashed.'" name="commentId">
                    <button type="submit" class="btn btn-outline-success btn-sm" name="changeCommentStatus" value="1">Approve</button>
                    <button type="submit" class="btn btn-outline-danger btn-sm" name="changeCommentStatus" value="2">Reject</button>
                </form>

    ';
}

Here's how I check for a match after submission:

if (isset($_POST['changeCommentStatus'])) {
    $hashedId = $_POST['commentId'];
    $status = $_POST['changeCommentStatus'];
    $commentId = '';
    foreach ($_SESSION['COMMENT_IDS'] as $data) {
        $hash = $data['hash'];
        $id = $data['id'];
        if ($hashedId === $hash) {
            $commentId = $id;
            break;
        }
    }
    if (empty($commentId)) {
        // this means that the $hashedId didn't matched anything, which also means that it has been altered
        // redirect back to the page and says that the id is invalid
    }
    echo $commentId;die;
}
$_SESSION['COMMENT_IDS'] = [];

As of now, it is working. But I'm not sure if there are any critical disadvantage /drawbacks or something, or if this is not recommended at all.

Note:

Some may say that an admin will not intentionally alter the source or something, but I'm planning to use this every time I need to use <input type="hidden" >
This may be more useful if while a user edits his comment, and somehow changed the comment id to another comment id that also belongs to the same user, instead the other comment has been edited
I can somehow prevent the action if the replaced comment id belonged to another user, preventing the current user from indirectly editing other users' comments
And of course, this is also to be used for other content that uses <input type="hidden">, especially when editing, not just for comments


texts: Encryption/decryption of a string with a salt

texts:
I have a new job coming up soon and want to increase my PHP knowledge.

This is my first attempt at creating and using a class. I just want to know if it's the correct way to do things.

It's very basic: a simple encrypt / decrypt of a string with a salt.

/**
* Encryption Class
*
* Use for encrypting / decrypting a string securely
*
*/

class stringEncryption
{

  // generate random string if the variable in a encrypt / decrypt is set as false
  public function generateRandomString($length = 10) {
    return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
  }

  // encrypts the string
  public function encryptString($string, $salt)
  {
    //if there is no salt, generate one
    if (!$salt)
    {
      $salt = self::generateRandomString();
    }
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $string, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
  }

  // decrypt the string
  public function decryptString($string, $salt)
  {
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($string), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
  }

}

This is how I have created 2 objects and encrypted / decrypted

include 'libs/PHP_Classes/class.encryption.php';

// Password BEFORE encryption
$passwordBefore = "TestPassword";
$salt = "8i3bf92f";

echo "Current Password: $passwordBefore
";

// object to handle the new password to be encrypted
$safePassword = new stringEncryption;
// object to handle the decryption of a password
$decrypt = new stringEncryption;

// create the encrypted password
$encryptedPassword = $safePassword->encryptString($passwordBefore, $salt);
// decrypt the password
$decryptedPassword = $decrypt->decryptString($encryptedPassword, $salt);

// show the password with encryption
echo "Encrypted: $encryptedPassword
";
// show the password decrypted
echo "Decrypted: $decryptedPassword";


texts: Comparing data in 2 tables on different servers with CHECKSUM

texts:
So I've had a problem where I need to compare data in 2 different tables on two different servers.  Now, I know MySQL supports CHECKSUM TABLES, but from my testing and understanding, it's not reliable across server instances and versions.

So I created this query:

$part = '@CRC := MD5(CONCAT_WS(\'#\', COALESCE(`'.
    implode('`, "#NULL#"), COALESCE(`', $this->_columns).
    '`, "#NULL#")))';
$sql1 = "SELECT COUNT(*) AS cnt,
        SUM(CONV(SUBSTRING({$part}, 1, 4), 16, 10)) as a1,
        SUM(CONV(SUBSTRING(@CRC, 5, 4), 16, 10)) as a2,
        SUM(CONV(SUBSTRING(@CRC, 9, 4), 16, 10)) as a3,
        SUM(CONV(SUBSTRING(@CRC, 13, 4), 16, 10)) as a4,
        SUM(CONV(SUBSTRING(@CRC, 17, 4), 16, 10)) as a5,
        SUM(CONV(SUBSTRING(@CRC, 21, 4), 16, 10)) as a6,
        SUM(CONV(SUBSTRING(@CRC, 25, 4), 16, 10)) as a7,
        SUM(CONV(SUBSTRING(@CRC, 29, 4), 16, 10)) as a8
    FROM `dbname`.`tablename`
    WHERE `id` >= $min AND `id` <= $max ";

So basically, it's concatenating each row together (all the columns of each row more specifically) and then MD5ing them.  Then it walks 4 hexbits at a time through that MD5 and sums them across all rows (4 hexbits to allow me to do huge tables without needing to worry about overflowing).  Then, I just compare the result of this query on both tables to see if everything is the same.

By using this binary search, I am able to rather quickly narrow down where the changes are so that I can port them.

It's actually reasonably efficient, so I'm not too concerned about that.  What I am concerned about is if this is even necessary.  It's screaming to me "You're doing it wrong", but I can't figure out any cleaner method around it...

What are your thoughts?