texts: Reusing thread and UdpClient for sending and receiving on same port

texts:
The working and functional code below is a simplification of a real test application I've written. It acts as a client which asynchronously sends UDP data from a local endpoint and listens for an echo'ed response from a server implemented elsewhere.

The requirements are:

Send and listen on the same port number;
The port number must be able to change (simulates data coming in from various
sources)

The program does what I want, but I have doubts about its correctness. Specifically regarding the class members of listenThread and udpClient which are reused when the port number changes.  Are they implemented, closed and cleaned-up correctly.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace cs_console
{
    class Tester
    {
        public void Send(string message, int localPort)
        {
            PrepareClient(localPort);
            SendBytes(System.Text.Encoding.UTF8.GetBytes(message));
        }

        private void PrepareClient(int localPort)
        {
            if (localEP == null || localEP.Port != localPort)
            {
                localEP = new IPEndPoint(IPAddress.Parse(localHost), localPort);
                listenThread = new Thread(StartListening);
                listenThread.Start();
            }
        }

        private void StartListening()
        {
            udpClient = new UdpClient();
            udpClient.ExclusiveAddressUse = false;
            udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            udpClient.Client.Bind(localEP);

            var s = new UdpState(localEP, udpClient);
            currentAsyncResult = udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
        }

        private void ReceiveCallback(IAsyncResult ar)
        {
            if (ar == currentAsyncResult)
            {
                UdpClient c = (UdpClient)((UdpState)(ar.AsyncState)).c;
                IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;

                Byte[] buffer = c.EndReceive(ar, ref e);
                if (buffer.Length > 0)
                    System.Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer));

                UdpState s = new UdpState(e, c);
                currentAsyncResult = udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
            }
        }

        private void SendBytes(byte[] messageData)
        {
            var sender = new UdpClient();
            sender.ExclusiveAddressUse = false;
            sender.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            sender.Client.Bind(localEP);
            sender.Send(messageData, messageData.Length, remoteEP);
            sender.Close();
        }

        private static string localHost = "10.27.21.109";
        private IPEndPoint localEP = null;
        private IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(localHost), 11000);
        private Thread listenThread = null;
        private UdpClient udpClient = null;
        private IAsyncResult currentAsyncResult = null;

        private class UdpState : Object
        {
            public UdpState(IPEndPoint e, UdpClient c) { this.e = e; this.c = c; }
            public IPEndPoint e;
            public UdpClient c;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Tester tester = new Tester();
            int localPort = 2532;
            while (true)
            {
                ++localPort;
                var message = Console.ReadLine();
                tester.Send(message, localPort);
            }
        }
    }
}

This is as compact a working example as I could make.


texts: Solving a linear system with two variables

texts:
Background:

Equation 1: 2x-y=6

Equation 2: 4x+3y=22

Because 1 is ax-by=c and 2 is dx-ey=f

y=(a * f - c * d) / (a * e - b * d) and x is simply (c - (b * y)) / a)

What I am trying to do:

Ultimately, generate linear equations that can be solved simultaneously for practice (which the code I wrote below can do). So I am creating variations of Equations 1 and 2 above that have whole number solutions for x and y. I do this by:

1.Creating random numbers.

2.Use the above calculation to find x and y from these numbers.

3.Ensure that x and y are whole numbers and if not, create new numbers for a-f until they are.

Working code:

The function NumberGeneration() is called with a button click.

double a;
double b;
double c;
double d;
double e;
double f;
double x;
double y;
System.Random rnd = new System.Random();
string question;
string answer;

public void NumberGeneration()//1.Generates the variables, calculates x and y.
{
    a = rnd.Next(1, 10);
    b = rnd.Next(1, 10);
    c = rnd.Next(1, 10);
    d = rnd.Next(1, 10);
    e = rnd.Next(1, 10);
    f = rnd.Next(1, 10);
    y = ((a * f - c * d) / (a * e - b * d));
    x = ((c - (b * y)) / a);
    CheckForZeros();
}

void CheckForZeros() //2.Checking for zeros
{
    if ((a * e - b * d) == 0) //To avoid dividing by 0, if this is 0 generate new numbers.
    {
        NumberGeneration();
    }
    else if((a * e - b * d) != 0) // otherwise proceed to make sure x and y are whole numbers
    {
        EnsureWhole();
    }

}

void EnsureWhole()
{
    decimal dy = Convert.ToDecimal(y);
    decimal dx = Convert.ToDecimal(x);
    if ((dy % 1) == 0 && (dx % 1) == 0) //3.If both x and y are whole numbers, setup the question with these numbers
    {
        question = string.Format($"{a}x+{b}y={c}, \n {d}x+{e}y={f}", a, b, c, d, e, f);
        answer = string.Format($"x={x} and y={y}");
    }
    else if((dy % 1) != 0 || (dx % 1) != 0)//Otherwise start again, generate a new set of numbers and attempt for a new answer where x and y are ints
    {
        NumberGeneration();
    };
}

Questions:

What is the proper way to do this?
How to optimize the code?
Is this the optimal way to generate random numbers?

Bonus follow-up:
Do you have any personal resource suggestions for learning C#? I get that instinctive feeling that my methods have holes in them, I know I am not good at coding, I just don't have the education to know better and I want to educate myself. Even better within the context of maths.


texts: Running a shell command when a file is added to FTP on Linux Ubuntu

texts:
I've recently set up a continuous integration pipeline to deploy a .NET Core application to my Linux machine living on the cloud.

My CI pipeline publishes my application, (specifically a .NET web application), zips it and then uploads by ftp.

Now, I've developed the following shell script which I will run as a service.

This script is able to detect as soon as a file has been uploaded to
my FTP folder (/home/MyName/ftp/files) by tailing the vbftp log.
The script then stops the service running the actual web
application, taking the site temporarily offline.
The existing site is then backed up.
We then unzip the newly uploaded file to the location where the web
app lives.(/var/www/ MyProject).
The newly uploaded file is deleted and the service restarted.

Below are the entire contents of the shell script

#!/bin/sh

tail -F /var/log/vsftpd.log | while read line; do
  if echo "$line" | grep -q 'OK UPLOAD:'; then
    filename=$(echo "$line" | cut -d, -f2)
    if [ "$ filename" == "MyProject" ]; then
     # Stop site service
     service MyProject stop
     # Make backup of existing files
     sudo zip -r /var/backups/site/$(date +%F)_MyProject /var/www/MyProject
     # unzip the newly received files
     # (-o to overwrite only the files which have changed)
     sudo unzip -o /home/MyName/ftp/files/MyProject.zip -d /var/www/MyProject
     # Remove uploaded zip
     sudo rm /home/MyName/ftp/files/MyProject.zip
     #Restart service
     service MyProject start
  fi
done

I'm somewhat new to Linux and shell and would love some feedback on the script I've thus far produced above.


texts: Concatenate function arguments with "AND" string if they're not None.

texts:
Basically, if the function parameter is None, I ignore it, else I concatenate all arguments like arg1:arg1_value AND arg2:arg2_value AND arg4:arg4_value considering arg3==None when the function got called.

def _get_query_string_for_get_config(self, host, app_name, instance, config_source, config_name):
    query_string = ""
    if host is not None:
        query_string += "host:{} AND ".format(host)
    if app_name is not None:
        query_string += "app_name:{} AND ".format(app_name)
    if instance is not None:
        query_string += "instance:{} AND ".format(instance)
    if config_source is not None:
        query_string += "config_source:{} AND ".format(config_source)
    if config_name is not None:
        query_string += "config_name:{}".format(config_name)

    return query_string


texts: Scrolling in Horizontal ScrollView losing selection

texts:
When I chose an element from this HorizontalScrollView, I was setting focus to that element by calling:

else if (v.getParent() == candidatesScrollView.getChildAt(0))
{
    Button candidateButton = (Button) v;
    v.requestFocusFromTouch();
    v.setSelected(true);
            (...)
}

Here is part of my XML:

<HorizontalScrollView
        android:id="@+id/CandidatesHorizontalScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout2"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:visibility="gone" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/horizontalscrollview1_button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:textSize="25sp" />

        (...)
        // 11 more buttons
    </LinearLayout>

</HorizontalScrollView>

After that, when I scrolled the list without choosing other element, I was losing focus of previously selected element. I made some research about this topic, but there was no solution that could work for me.  Finally, after almost two weeks I have created some solution by myself.

I would like you to review it and tell me what can I improve on.

I created a custom HorizontalScrollView class inside and have overridden the onTouchEvent() method. I don't think this is optimal way of doing that, because in that case I have to do calculations every time I move even one pixel.  For example, if I add toast.show() to the below method, it will try to show as many toast as many I moved pixels (If I move by 10 pixels, it will try to show 10 Toast). Anyway, it works for me and the selection and focus are being kept. Please help me modify this code to make finally a good answer for that known issue:

@Override
public boolean onTouchEvent(MotionEvent ev)
{
    int i = 0;
    Button button = null;
    for (; i < 11; i++)
    {
        button = (Button)((LinearLayout)getChildAt(0)).getChildAt(i);
        if(button.isSelected())
            break;
    }
    super.onTouchEvent(ev);
    button.setSelected(true);
    button.requestFocusFromTouch();

    return true;
}

To be sure that the above code will work, you need to have only one selected item in your HorizontalScrollView at a time, i.e when you press different Button, you need to make the previous one setSelected(false).


texts: Checking whether two strings are isomorphic

texts:
I was trying to solve a problem to determine if 2 strings are isomorphic. They are isomorphic when each character in string one can be replaced with another character in string 2. Two different characters may not map to the same value, and same character must not match to different values. Also the length of the strings must be same.

I've used exception for logic flow:

module Isomorphism

  class NotIsomorphicException < StandardError
    attr_reader :object
    def initialize(object)
      @object = object
    end
  end

  def self.isomorphic? string_a,string_b
    begin
      self.isomorphism string_a,string_b
      true
    rescue NotIsomorphicException => _
      return false
    end
  end

  protected
  def self.isomorphism string_a,string_b
    raise NotIsomorphicException.new("Unequal length") unless string_a.size == string_b.size

    isomorphism_map = {}
    associations = string_a.chars.zip string_b.chars

    associations.each do |char_one,char_two|
      if(isomorphism_map.key? char_one)
        raise NotIsomorphicException.new("Single character needs to map to multiple characters, not isomorphic") unless isomorphism_map[char_one] == char_two
      elsif(isomorphism_map.value? char_two)
        raise NotIsomorphicException.new("Two characters map to same value")
      end
      isomorphism_map[char_one] = char_two
    end
  end

end

Please let me know if it's okay to code this way and any improvements that I might be able to make this better.

Following are the specs, just in case:

describe Isomorphism do
  it "should return true for single character words" do
    expect(Isomorphism.isomorphic? "t","d").to be true
  end

  it "should return true for empty strings" do
    expect(Isomorphism.isomorphic? "","").to be true
  end

  it "should return false if strings are of unequal length" do
    expect(Isomorphism.isomorphic? "dh","dhruv").to be false
  end

  it "should return false when 2 characters need to map to same character" do
    expect(Isomorphism.isomorphic? "ab","aa").to be false
  end

  it "should return true for egg and add" do
    expect(Isomorphism.isomorphic? "egg","add").to be true
  end

  it "should return false for foo and bar" do
    expect(Isomorphism.isomorphic? "foo","bar").to be false
  end

  it "should return true for paper and title" do
    expect(Isomorphism.isomorphic? "paper","title").to be true
  end

  it "should return false for aab and aaa" do
    expect(Isomorphism.isomorphic? "aab","aaa").to be false
  end
end


texts:
After years of fear and procrastination I decided to learn regular expressions. This is the result:

CoffeScript:

str = "
#{chapter.title}
\n\n#{chapter.content}\n\n"

str.replace(/>([^>]+)</g, (r) ->
  r.replace(/(>|\s)"/g, "$1“")
   .replace(/"/g, "”")
   .replace(/("|\s)'/g, "$1‘")
   .replace(/'/g, "’")
)

JavaScript:

var str = "
" + chapter.title + "
\n\n" + chapter.content + "\n\n";

str.replace(/>([^>]+)</g, function(r) {
  return r.replace(/(>|\s)"/g, "$1“")
          .replace(/"/g, "”")
          .replace(/("|\s)'/g, "$1‘")
          .replace(/'/g, "’");
});

As you can see, the code turns all "straight" quotation characters (plus apostrophe) to “curly” ones. It was a bit hard because the input had stuff like this:

"Yes," he said, "I met her. She's very 'friendly.'"

So I had to make sure the quotes inside the HTML tags were not being included. And make the code know that quotations wouldn't always be preceded by a space (sometimes by an >).

I welcome any suggestion to make the code shorter, more efficient/readable.


texts: Vectors assignations and operations in a loop and parallelization with OpenMP

texts:
I use this piece of code to compute a short-time Fourier transform:

// Output pre-allocation
std::vector<std::vector<std::complex<T> > > y(nFft, std::vector<std::complex<T> >(nFrames, 0.0));

std::vector<T> xt;
std::vector<std::complex<T> > yt;
xt.reserve(nFft);
yt.reserve(nFft);

#pragma omp parallel for private(xt,yt)
for (unsigned int t = 0; t < nFrames; ++t) {
    const int offset = -((int) wSize/2) + t*nHop;

    // Extract frame of input data
    if (offset < 0) {
        xt.assign(x.begin(), x.begin() + offset + wSize);
        xt.resize(wSize, 0.0);
    }
    else if (offset + wSize > n) {
        xt.assign(x.begin() + offset, x.end());
        xt.resize(wSize, 0.0);
    }
    else
        xt.assign(x.begin() + offset, x.begin() + offset + wSize);

    // Apply window to current frame
    std::transform(xt.begin(), xt.end(), w.begin(), xt.begin(), std::multiplies<T>());

    // Zero padding
    std::rotate(xt.begin(), xt.begin() + wSize/2, xt.end());
    xt.insert(xt.begin() + wSize/2, nFft - wSize, 0.0);

    yt = fft(xt);  // Perform the FFT!

    #pragma omp critical
    {
        for (unsigned int f = 0; f < nFft; ++f)
            y[f][t] = yt[f];
    }
}

Is there something that I can improve, either in design style or performances, without sacrificing readability?
Of course the point is to improve my coding skills without using any external library!

Regarding the xt and yt vectors, I put them outside the loop to improve the performances in mono-threaded (when OpenMP is disabled). In multi-threaded, there are copied for each thread anyway (hence the use of the private close).


texts: Calculating mesh centers runs very slowly

texts:
I have a piece of code that is used to calculate the cell centers in a rectangular mesh. The mesh size is pretty large (650 columns by 1150 rows). The code runs very slowly. I was wondering if I could employ any tricks to reduce the runtime.

In the example below, deltax is a 1D array of column spacings and deltay is a 1D array of row spacings. Maybe I need to do some sort of vectorization.

center_x = np.empty((nrow,ncol),dtype ='double')
center_y = np.empty((nrow,ncol),dtype ='double')
for i in range(nrow-1,-1,-1):
    if(i == nrow-1):
        center_y[i,:] = 0.5*deltay[nrow-1]
    else:
        center_y[i,:] = center_y[i+1,:] + 0.5*deltay[i+1] + 0.5*deltay[i]

for j in range(0,ncol):
    if(j ==0):
        center_x[:,j] = 0.5*deltax[0]
    else:
        center_x[:,j]= center_x[0,j-1] + 0.5*deltax[j-1] + 0.5*deltax[j]


texts: Get Minimum of four and maximum up to a limit

texts:
Sita shortlists 4 gifts priced at r1, r2, r3 and r4
  respectively. She wants to buy 3 of these gifts such that it doesn't
  exceed her budget of x.

  Input and Output Format:

  Input consists of 5 integers that correspond
  to r1, r2, r3, r4 and x. Output consists of 2 integers. The first
  integer corresponds to the minimal amount she would spend for buying 3
  gifts. The second integer corresponds to the maximal amount she would
  spend for buying 3 gifts, staying within the budget.

  Sample Input

20 30 10 25 80

  Sample Output

55 75

  #include<stdio.h>

 int main()
 {
int x,a[4],i,j,n,temp,big,small,sum=0;

// scanf("%d",&r1);
// scanf("%d",&r2);
// scanf("%d",&r3);
// scanf("%d",&r4);
// scanf("%d",&x);
big = 0;
n=4;
for(i=0;i<n;i++)
{
    scanf("%d",&a[i]);
}
scanf("%d",&x);
for(i=0;i<n;i++)
{
    for(j=i;j<(n);j++)
    {
        if(a[i]>a[j]){
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    sum+=a[i];
}
//sum = a[0]+a[]
small = sum-a[3];

if((sum-a[0])<=x)
big = sum-a[0];
else if((sum-a[1])<=x)
big = sum-a[1];
else if((sum-a[2])<=x)
big = sum-a[2];
else
big = small;

if(small<=x){
    printf("%d",small);
}

if(big == small){
    return 0;
}

if(big<=x){
    printf("\n%d",big);
}
return 0;
}


texts:
In my Android app I store days of week in an int variable by using bit masking.

I'm using the following SparseIntArray to get the day flags for the corresponding days:

public static final SparseIntArray DAY_FLAGS = new SparseIntArray(7);
static {
    DAYS.put(Calendar.MONDAY, 1);
    DAYS.put(Calendar.TUESDAY, 2);
    DAYS.put(Calendar.WEDNESDAY, 4);
    DAYS.put(Calendar.THURSDAY, 8);
    DAYS.put(Calendar.FRIDAY, 16);
    DAYS.put(Calendar.SATURDAY, 32);
    DAYS.put(Calendar.SUNDAY, 64);
}

I'd like to be able to create String from a mask, for example:

  int value - String

  1 - "Mon"

  42 - "Tue Thu Sat"

  127 - "Mon Tue Wed Thu Fri Sat Sun"

For this I'm using the following method:

public static String maskToString(Context context, int mask, int firstDayOfWeek) {
    List<Integer> dayList = new ArrayList<>(7);

    // The idea is to iterate through the SparseIntArray and check
    // if the bit in the mask is set for the particular day.
    // If it is, add that day to the dayList.
    for (int i = 0; i < DAY_FLAGS.size(); i++) {
        if (isFlagSet(DAY_FLAGS.valueAt(i), mask)) {
            dayList.add(DAY_FLAGS.keyAt(i));
        }
    }

    // Sort the days by taking the first day of the week into account
    Collections.sort(dayList, new DayComparator(firstDayOfWeek));

    // This List will contain the individual Strings for the days
    List<String> dayStringList = new ArrayList<>(dayList.size());

    // dayToString() simply returns a String resource for a given day
    for (Integer day : dayList) {
        dayStringList.add(dayToString(context, day));
    }

    // join the individual Strings into a single String
    return TextUtils.join(" ", dayStringList);
}

The isFlagSet() method simply checks if a given flag is set in a given mask:

private static boolean isFlagSet(int flag, int mask) {
    return ((mask & flag) == flag);
}

My maskToString() method seems clumsy (mostly in terms of unnecessary(?) object creation). I believe it can be optimized, I just don't know how.

(I can use only Java 7 features, no Java 8)

I'd appreciate any advice.

EDIT:

One thing I came up with regarding optimization is to create a static final DayComparator instance and pass the firstDayOfWeek variable in a setter (instead of the constructor):

private static final DayComparator DAY_COMPARATOR = new DayComparator();

public static String maskToString(Context context, int mask, int firstDayOfWeek) {
    List<Integer> dayList = new ArrayList<>(7);

    for (int i = 0; i < DAY_FLAGS.size(); i++) {
        if (isFlagSet(DAY_FLAGS.valueAt(i), mask)) {
            dayList.add(DAY_FLAGS.keyAt(i));
        }
    }

    // so we do not create a new object here every time the method is called
    DAY_COMPARATOR.setFirstDayOfWeek(firstDayOfWeek);
    Collections.sort(days, DAY_COMPARATOR);

    List<String> dayStringList = new ArrayList<>(dayList.size());

    for (Integer day : dayList) {
        dayStringList.add(dayToString(context, day));
    }

    return TextUtils.join(" ", dayStringList);
}

What do you think? Any other suggestions?


texts:
Working from a C# and EntityFramework background. Jimmy Bogard makes the point of modelling your domain model correctly, such that an Email Address is not a simple string but should be of type EmailAddress.
Also known as Primitive Obsession.
I've come to this part of a piece of software where a Application form is to be filled out.
For the user a Yes/No question where they can answer Yes or No is a very easy thing, you can even create really go user experiences around yes/no.
So I have quickly thought of two ways to handle Yes/No.
A complex object:
public class YesNoAnswer
{
    public YesNoAnswer(string state)
    {
        this.SetState(state);
    }

    public bool Yes { get; private set; }

    public void SetState(string state)
    {
        var uppedState = state.ToUpper();

        if (uppedState == "YES")
        {
            this.Yes = true;
            return;
        }
        if (uppedState == "NO")
        {
            this.Yes = false;
            return;
        }

        throw new ArgumentOutOfRangeException(nameof(state), "Yes or No strings not found in argument");
    }
}

Or a simple extension method:
public static class StringExtensions
{
    public static bool GetYesNoState(this string state)
    {
        var uppedState = state.ToUpper();

        if (uppedState == "YES")
        {
            return true;
        }
        if (uppedState == "NO")
        {
            return false;
        }

        throw new ArgumentOutOfRangeException(nameof(state), "Yes or No strings not found in argument");
    }
}

Obviously the validation should be done earlier in the pipeline (or at least it is in our application).
Which is better? They both achieve the same thing, the first is much clearer, neater. The second is well, slightly obfuscated - not a great deal but takes more than a second to think about what it is doing.
Usage of both
var application = new ApplicationForm { IsOkayToWork = new YesNoAnswer(state) };

or
var application = new ApplicationForm { IsOkayToWork = state.GetYesNoState() };


texts:
I worked through the Flowers challenge on hackerrank.com and would like to get feedback on solution and algorithm implementation.  It was suggested by hackerrank to review greedy algorithm to solve this challenge.  As far as I understand this is an implementation of greedy algorithm.

  You and your \$K−1\$ friends want to buy \$N\$ flowers. Flower number \$i\$ has cost \$c_i\$. Unfortunately the seller does not want just one customer to buy a lot of flowers, so he tries to change the price of flowers for customers who have already bought some flowers. More precisely, if a customer has already bought \$x\$ flowers, he should pay \$(x+1) \times c_i\$ dollars to buy flower number \$i\$.

  You and your \$K−1\$ friends want to buy all \$N\$ flowers in such a way that you spend the least amount of money. You can buy the flowers in any order.

  Input:

  The first line of input contains two integers \$N\$ and \$K\$ (\$K≤N\$). The next line contains \$N\$ space separated positive integers \$c_1,c_2,\dots,c_N\$.

  Output:

  Print the minimum amount of money you (and your friends) have to pay in order to buy all \$N\$ flowers.

import java.io.*;
import java.util.*;

public class Flowers {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        ArrayList <Integer> flowerPriceList = new ArrayList<Integer>();
        int numFlowers = sc.nextInt();
        int numFriends = sc.nextInt();
        for(int i = 0; i<numFlowers;i++){
            flowerPriceList.add(sc.nextInt());
        }
        // Sort the ArrayList in reverse order to start bying flowers from most expensive
        Collections.sort(flowerPriceList,Collections.reverseOrder());
        int flowersBought = 0;
        int friendNum = 0;
        int total = 0;
        for(int price:flowerPriceList){
            //itterate throught all the flower prices and calculate the total
            total +=(flowersBought+1)*price;
            friendNum++;
            if(friendNum == numFriends){
                //if all friends bought flowers reset the friend counter and restart the cycle
                friendNum = 0;
                flowersBought++;
            }
        }
        System.out.println(total);
    }
}


texts:
In my programming class I am learning how to make graphical user interfaces, and so I decided to make one.

import tkinter as t
from random import choice

COLORS = ["white", "black", "red", "green", "blue", "cyan", "yellow", "magenta"]

def button_clicked():
    display_area.config(text = "You clicked the button!")
    screen.itemconfig(epilepsy_man, fill = choice(COLORS))

def epilepsy_man_movment(event):
    key = event.keysym
    if key == "Right":
        screen.move(epilepsy_man, 10, 0)
        screen.itemconfig(epilepsy_man, fill = choice(COLORS))
    elif key == "Left":
        screen.move(epilepsy_man, -10, 0)
        screen.itemconfig(epilepsy_man, fill = choice(COLORS))
    elif key == "Up":
        screen.move(epilepsy_man, 0, -10)
        screen.itemconfig(epilepsy_man, fill = choice(COLORS))
    elif key == "Down":
        screen.move(epilepsy_man, 0, 10)
        screen.itemconfig(epilepsy_man, fill = choice(COLORS))

window = t.Tk()
window.title("Your reading this right now. Please stop.")

button = t.Button(window, text = "Can you click me?", command = button_clicked)
button.pack()

display_area = t.Label(window, text = "Go on! Click the button!")
display_area.pack()

screen = t.Canvas(window, width = 500, height = 500)
screen.pack()
epilepsy_man = screen.create_oval(200, 200, 300, 300, fill = choice(COLORS))
screen.bind_all("<Key>", epilepsy_man_movment)

window.mainloop()

Please note "epilepsy". If you run this there will be flashing images.

I want to know if I properly followed any conventions, as well as if there is any better way of doing what I have done.

What this program does is make a button that changes the color of the circle and the text on a textbox, as well as creating said textbox and said circle which moves with the arrow keys and changes with each key press.


texts: Is this a reasonable binary search implementation?

texts:
import java.util.Arrays;

public class BinarySearch {

  // this class should not be instantiated

  private BinarySearch() { }

  // searches for the integer key in the sorted array a[]
  // @param key the search key
  // @param a the array of integers, must be sorted in ascending order
  // @return index of key in array a[] if present; -1 if not present

  public static int rank(int key, int[] a) {
    int lo = 0;
    int hi = a.length - 1;
    while (lo <= hi) {
      // key is in a[lo..hi] or not present
      int mid = lo + (hi - lo) / 2;
      if (key < a[mid]) hi = mid - 1;
      else if (key > a[mid]) lo = mid + 1;
      else return mid;
    }
    return 1;

  }

  // reads in a sequence of integers from the whitelist file, specified as a command line argument. reads in integers from standard input and prints to standard output
  // those integers that do not appear in the file.
  public static void main(String[] args){

    // read the integers from a file
    In in = new In(args[0]);
    int[] whitelist = in.readAllInts();

    // sort the array
    Arrays.sort(whitelist);

    // read integer key from standard input; print if not in whitelist
    while (!StdIn.isEmpty()) {
      int key = StdIn.readInt();
      if (rank(key, whitelist) == -1)
        StdOut.println(key);
    }
  }

}


texts:
A post by Yakk alerted me to  the idea of named operators in C++. This look splendid (albeit very unorthodox). For instance, the following code can be made to compile trivially:

vector<int> vec{ 1, 2, 3 };

cout << "3 in " << vec << ": " << (3 <in> vec) << '\n'
     << "5 in " << vec << ": " << (5 <in> vec) << '\n';

Of course the whole thing has to be generic so any binary function-like thing can be used to define operators, e.g.

auto in = make_named_operator(
    [](int i, vector<int> const& x) {
        return find(begin(x), end(x), i) != end(x);
    });

I’d like to know whether the following implementation is sufficient to handle all “interesting”1 cases, and whether it’s robust. For instance, I’m storing the operands in references. That should work since they’re only stored until after the expression has completed, and thus should never go stale. I’m especially interested in feedback on the return types of the operator functions below and on the design rationale of using the <…> syntax for named operators.2

It seems to handle templates as well as a mixture of different types (and cv-qualification).

#include <utility>

template <typename F>
struct named_operator_wrapper {
    F f;
};

template <typename T, typename F>
struct named_operator_lhs {
    F f;
    T& value;
};

template <typename T, typename F>
inline named_operator_lhs<T, F> operator <(T& lhs, named_operator_wrapper<F> rhs) {
    return {rhs.f, lhs};
}

template <typename T, typename F>
inline named_operator_lhs<T const, F> operator <(T const& lhs, named_operator_wrapper<F> rhs) {
    return {rhs.f, lhs};
}

template <typename T1, typename T2, typename F>
inline auto operator >(named_operator_lhs<T1, F> const& lhs, T2 const& rhs)
    -> decltype(lhs.f(std::declval<T1>(), std::declval<T2>()))
{
    return lhs.f(lhs.value, rhs);
}

template <typename T1, typename T2, typename F>
inline auto operator >=(named_operator_lhs<T1, F> const& lhs, T2 const& rhs)
    -> decltype(lhs.value = lhs.f(std::declval<T1>(), std::declval<T2>()))
{
    return lhs.value = lhs.f(lhs.value, rhs);
}

template <typename F>
inline constexpr named_operator_wrapper<F> make_named_operator(F f) {
    return {f};
}

For the interested, a full example implementation is on GitHub.

1 <insert your definition of “interesting” here>

2 I considered other alternatives, such as %…% which is used by R, and allowing different operators (as done by Yakk) to allow for different operator precedences but I decided against that because I think it makes operator precedence even more complicated than it already is in C++.


texts: Given a string S and a set of words D, find the longest word in D that is a subsequence of S

texts:
I'm trying to improve my code and algorithm to find out the longest word from a dictionary occurring as the sub-sequence in the given string.

Example : For example, given the input of S = "abppplee" and D = {"able", "ale", "apple", "bale", "kangaroo"} the correct output would be "apple".

Currently my algorithm compares each character of the dictionary-word with the given-word and advances to next char in dictionary-word on finding a match and so on till either the dictionary-word is iterated over completely(which means the word is a valid sub-sequence) or the given word is iterated over completely(which means not a valid string).

Being a beginner to c++ and algorithms in general, my methods and logic could be further improved. So kindly suggest if any improvements in the algorithm or code which can improve the performance. Thanks.

#include <iostream>
#include <string>
#include <tuple>
#include <vector>

class StringManipulater {
  public:
  StringManipulater(const std::string& p) : given_string_{p} {}
  std::string given_string_;
  std::tuple<std::string, typename std::string::size_type> current_longest_ = {
      "", 0};

  const std::string&& FindLongestSubSeq(const std::vector<std::string>& dict)
  {
    // flag determines whether current word is present in given_string input
    bool flag;

    // iterate through each of the dict-words
    for (const std::string& word : dict) {
      flag = false;
      auto word_len = word.size();

      // fair optimization : only consider current word if its
      // length is greater than previous one
      if (word_len > std::get<1>(this->current_longest_)) {
        // iterate through the letters in both given string and current word
        // to be compared
        for (typename std::string::size_type i = 0, j = 0; j < word_len; ++i) {
          // no need to go further checking if we have iterated over given
          // string
          if (i == this->given_string_.size()) break;

          // compare each letter of both words
          if (word[j] == given_string_[i]) {
            flag = true;
            // advance to next charcter in the dictionary word
            ++j;
          }
          else {
            // if the char couldn't be found
            flag = false;
          }
        }  // end of comparison loop
        if (flag) {
          std::get<0>(this->current_longest_) = word;
          std::get<1>(this->current_longest_) = word_len;
        }
      }  // top if
    }    // end of iteration of dictionary words
    return std::move(std::get<0>(this->current_longest_));
  }
};

int main()
{
  StringManipulater s_manip{"abppplee"};
  std::cout << "Longest subsequence = "
            << s_manip.FindLongestSubSeq(
                   {"able", "ale", "apple", "bale", "kangaroo"})
            << "\n";
  return 0;
}


texts: Homework distribution terminal app in C++

texts:
I'm working on cleaning this code up a bit and was hoping for some suggestions. Right now it works well enough on a basic level, but it doesn't check for out of bounds numbers or improper input.

Also, one of my big concerns is too much nesting. Does the code look ugly? Any suggestions on how to clean it up?

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int numClasses();

int main()
{
    double finalResult[4], daysUntilDue, result, numOfProblems[199], numOfDays[99];
    string assignmentName[4];
    int numOfClasses, numItems[3], i, j;
    string classNames[3];

    numOfClasses = numClasses();

    ofstream myFile;
    myFile.open ("/Users/nicolasparsons/Desktop/HWAverages.txt");

    for (i = 0; i < numOfClasses; i++)
    {
        cout << "What is the name of class #" << i + 1 << "? (do not use spaces): ";
        cin >> classNames[i];
        cout << "How many assignments do you have for " << classNames[i] << "? (must be 5 or less): ";
        cin >> numItems[i];
        for (j = 0; j < numItems[i]; j++)
        {
            cout << "Enter a name for assignment #" << j + 1 << "? (do not use spaces): ";
            cin >> assignmentName[j];
            cout << "Enter number of problems for " << assignmentName[j] << " (must be 200 or less): ";
            cin >> numOfProblems[j];
            cout << "Number of days until this assignment is due (must be 100 or less): ";
            cin >> numOfDays[j];
            finalResult[i] = numOfProblems[j] / numOfDays[j];
            //cout << finalResult[i] << endl;
            myFile << classNames[i] << endl << assignmentName[j] << ": " << finalResult[i] << " per day until due date. " << endl << endl;
        }
    }

return 0;
}

int numClasses()
{
    int result;
    cout << "How many classes do you have? (must be 4 or less): ";
    cin >> result;
    return result;
}


texts:
I've created a simple logging system that I'm planning to use and I've tried out some modern C++ features to make it generic. First I've made a log class that represents a single log message:

struct log : std::stringstream
{
private:
    template <typename Level, size_t I = 0, typename... Stamps>
    static inline typename std::enable_if<I == sizeof...(Stamps), void>::type
        __stamp(log& l)
    {
    }

    template <typename Level, size_t I = 0, typename... Stamps>
    static inline typename std::enable_if<I < sizeof...(Stamps), void>::type
        __stamp(log& l)
    {
        l << std::tuple_element_t<I, std::tuple<Stamps...>>::Get<Level>();
        __stamp<Level, I + 1, Stamps...>(l);
    }

public:
    template <typename Level, typename... Stamps>
    static log& emit()
    {
        static log l;
        __stamp<Level, 0, Stamps...>(l);
        l << ": ";
        return l;
    }

    template <typename T>
    log& operator<<(T el)
    {
        *((std::stringstream*)this) << el;
        return *this;
    }

    template <typename Logger>
    void Flush()
    {
        Logger::Log(str().c_str());
        clear();
    }
};

template <typename Logger>
std::ostream& endlog(std::ostream& out)
{
    ((log*)&out)->Flush<Logger>();
    return out;
}

typedef Logger<ConsoleSink> DefaultLogger;
std::ostream& endlog(std::ostream& out)
{
    ((log*)&out)->Flush<DefaultLogger>();
    return out;
}

And I've created the main logger:

template <typename... Sinks>
class Logger
{
private:
    static std::tuple<Sinks...> s_Sinks;

private:
    template <size_t I = 0, typename... Tp>
    static inline typename std::enable_if<I == sizeof...(Tp), void>::type
        __Log(const char* msg)
    {
    }

    template <size_t I = 0, typename... Tp>
    static inline typename std::enable_if<I < sizeof...(Tp), void>::type
        __Log(const char* msg)
    {
        std::get<I>(s_Sinks).Write(msg);
        __Log<I + 1, Tp...>(msg);
    }

public:
    static void Log(const char* msg)
    {
        __Log<0, Sinks...>(msg);
    }
};

template <typename... Sinks>
std::tuple<Sinks...> Logger<Sinks...>::s_Sinks;

An example usage would be:

log::emit<Info, LevelStamp, SeparatorStamp, TimeStamp>() << "Hello, Logging " << 3 << endlog;

Now I can see a lot of problems with my own code, for example because of my emit function is defined with variadic stamps, there's no way to define a default overload for it so if I always want the same stamps, I have to type them out. Another is that my DefaultLogger is not configurable from outside the logger code which I think should be a feature.

Is my approach any good? How could I improve it?


texts: Printing random lines from a file and compiling it to WebAssembly for web view

texts:
I want to learn Wasm and Rust. This project can execute Wasm from GitHub Pages. The first attempts failed because of some policy error with the Wasm MIME type. When I tried Yew Framework then it worked to run Wasm for my first attempt (it just writes random rows from a text file).

For some reason I got a MIME type error when trying the official example from Mozilla and I want to be independent of node and npm whereas the official example requires npm (and would generate the MIME type error when deployed to GitHub Pages (but running as expected locally with the npm webpack)).

Here is my code done with yew framework. It is also in the repository.

Cargo.toml

[package]
name = "spooks-wasm"
version = "0.1.1"
authors = ["Niklas <n*****@*****mail.com>"]
edition = "2018"

[dependencies]
stdweb = "0.4.20"
getopts = "0.2"
rand = {version = "0.7", features = ['stdweb']}
easy_reader = "0.5.0"
yew = { path = "../.." }

main.rs

fn main() {
    yew::start_app::<counter::Model>();
}

lib.rs

#![recursion_limit = "128"]

extern crate easy_reader;
extern crate getopts;
extern crate rand;

use stdweb::web::Date;
use yew::services::ConsoleService;
use yew::{html, Component, ComponentLink, Html, ShouldRender};

use std::env;
use std::io::{Cursor, Error, Seek, SeekFrom, Write};
use crate::rand::Rng;

use easy_reader::EasyReader;
use getopts::Options;

pub struct Model {
    link: ComponentLink<Self>,
    console: ConsoleService,
    value: i64,
    spooks: String,
}

pub enum Msg {
    Increment,
    Decrement,
    Bulk(Vec<Msg>),
}

impl Component for Model {
    type Message = Msg;
    type Properties = ();

    fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {

        let mut c = Cursor::new(Vec::new());
        let str42 = include_str!("spook.lines").as_bytes();

        // // Write into the "file" and seek to the beginning
        c.write_all(str42).unwrap();
        c.seek(SeekFrom::Start(0)).unwrap();

        let mut reader = EasyReader::new(c).unwrap();
        let _res = reader.build_index();
        let tmpspooks = reader.random_line().unwrap().unwrap() + &reader.random_line().unwrap().unwrap() + &reader.random_line().unwrap().unwrap() + &reader.random_line().unwrap().unwrap() + &reader.random_line().unwrap().unwrap();

        Model {
            link,
            console: ConsoleService::new(),
            value: 0,
            spooks: tmpspooks
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {

        let mut c = Cursor::new(Vec::new());
        let str42 = include_str!("spook.lines").as_bytes();

        // // Write into the "file" and seek to the beginning
        c.write_all(str42).unwrap();
        c.seek(SeekFrom::Start(0)).unwrap();

        let mut reader = EasyReader::new(c).unwrap();
        let _res = reader.build_index();
        self.spooks = reader.random_line().unwrap().unwrap() + &reader.random_line().unwrap().unwrap() + &reader.random_line().unwrap().unwrap() + &reader.random_line().unwrap().unwrap() + &reader.random_line().unwrap().unwrap();

        match msg {
            Msg::Increment => {
                self.value = self.value + 1;
                self.console.log("plus one");
            }
            Msg::Decrement => {
                self.value = self.value - 1;
                self.console.log("minus one");
            }
            Msg::Bulk(list) => {
                for msg in list {
                    self.update(msg);
                    self.console.log("Bulk action");
                }
            }
        }
        true
    }

    fn view(&self) -> Html {
        html! {

{ &self.spooks }

                <button onclick=self.link.callback(|_| Msg::Increment)>
                    { "Spook" }
                </button>

        }
    }
}


texts: Django model design to analyse user activities on external websites

texts:
I am working on an app that analyses the activities of users on some external sites. The contents of the target sites fall into three types:

Forum post: A user can either start a thread or reply to an existing one, the reply will get an unique url;
Blog entry: Any user can post. The blog entry gets an unique url, comments do not.
News article: Not user posted, from a number of news sources. Comments do not have an unique url.

The focus of my app is on user activities, below are some possible questions:

How often does user A post (with respect to types, daily, weekly, etc.)?
What topics are the user mostly interested in? (for this, all threads will be tagged based on their original category and scanned keyword from title/content).
Is user A tend to reply user B's posts more often then to user C's? (obviously this particular question suits more on forum posts and blog entries).

I plan to consolidate a Thread model to hold all three types of contents, do not address the diffrences in them, for example, theat the news source just as a regular user. The Thread model will have roughly 10 millions of records.

Below are the models that I came up in very rudimentary form. I have few questions that need your help:

Is the logic clear in the models?
Regarding the first_seen and last_seen fields in Nick model: if I can look up the Activity model inexpensively in runtime, I would rather not to have them, since the two fields need to be computed each time I insert a new record. What are my better options here?
There will be about 10 sites included, should I have another model just for sites?
The url field in Activity model exists only for a forum type, meaning quite a few millions of records (Blogs and News) will not have a value there, is this a good practice?

Thank you all very much! I appreciate any input!

# should i index some fields?
class Thread(models.Model):
    site = models.CharField(max_length=40)
    thread_type = models.CharField(max_length=20)
    category = models.CharField(max_length=20)
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField()
    url = models.URLField()
    hits = models.IntegerField()
    votes = models.IntegerField()

    def __str__(self):
        return self.title

class Nick(models.Model):
    site = models.CharField(max_length=40)
    username = models.CharField(max_length=40)
    first_seen = models.DateTimeField(null=True, blank=True)
    last_seen = models.DateTimeField(null=True, blank=True)

    class Meta:
        unique_together = ("site", "username")

    def __str__(self):
        return self.username

class Activity(models.Model):
    usernames = models.ForeignKey(Nick)
    threads = models.ForeignKey(Thread)
    post_date = models.DateTimeField(default=datetime.now, blank=True)
    is_original = models.BooleanField()
    url = models.URLField()     # in case a forum reply and not original

    def __str__(self):
        return Nick.username    # needs to be more informative

Update: The users of all sites are almost from the same population, they may register on multiple sites, may or may not use the same nickname. One of my research goal (in a later phase) is to explore the correlations between users of different sites.


texts: Sorted linked-list in C (self-compiling)

texts:
I needed a sorted-list that I can iterate over to render elements in layers. Right now, delete_item deletes the first node with a matching priority it finds but I intend to delete specific list items in my implementation.

I didn't know if I needed the list to be doubly linked, but it turns out it makes deleting an item from the list easier. I've also seen people initializing the memory with calloc, is it important in this case?

There's also probably a way to give each list item a unique identifier (apart from its pointer) so that I can delete a specific item instead of deleting the first occurrence. For now, I'll just use the pointer...

Also, what do you think of my self-compiling trick?

//usr/bin/gcc ${0##*/} -o temp && ./temp ; rm temp 2>/dev/null ; exit

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

struct node {
    struct node *next;
    struct node *prev;

    int priority;
};

struct node *head;

void add_item(int priority)
{
    struct node *curr = head;
    struct node *prev = NULL;

    while((curr != NULL) && (curr->priority < priority)) {
        prev = curr;
        curr = curr->next;
    }

    struct node *new = malloc(sizeof(struct node));
    new->priority = priority;
    new->next = curr;
    new->prev = prev;

    if(prev != NULL) {
        prev->next = new;
    } else {
        head = new;
    }
}

void remove_item(int priority)
{
    struct node *curr = head;
    while((curr != NULL) && (curr->priority != priority)) {
        curr = curr->next;
    }

    if(!curr)
        return;

    if(curr == head) {
        head = curr->next;
    } else {
        curr->prev->next = curr->next;
        if(curr->next)
            curr->next->prev = curr->prev;
    }
    free(curr);
}

void delete_list()
{
    struct node *curr;
    while(head != NULL) {
        curr = head;
        head = head->next;
        free(curr);
    }
}

void print_items()
{
    int member = 0;
    for(struct node *iter = head; iter != NULL; iter = iter->next)
    {
        printf("Member: %d -> priority: %d\n", member, iter->priority);
        member++;
    }
}

int main(int argc, char** argv)
{
    printf("This file is self-compiling\n");

    add_item(1);
    add_item(1);
    add_item(2);
    add_item(3);
    add_item(1);
    add_item(5);
    add_item(4);
    add_item(3);
    remove_item(1);
    remove_item(4);
    remove_item(5);
    add_item(5);

    print_items();
    delete_list();
}


texts: Update / save / display values in a table

texts:
I am relatively new to Angular and need to put together an application to update / save / display values. The code is working, but I wanted your input the make sure that I am doing this 'the angular way'.

JavaScript

var app = angular.module('store', []);

app.controller('TableController', ['$scope', '$log',function($scope, $log){
   $scope.family = familyMembers;

   $scope.SaveMember = function() {
       var saveMember = { name: $scope.name, age: $scope.age };
       if ($scope.editItem == -1) {
            $scope.family.push(saveMember);
            // insert in db here
       }
       else {
           $scope.family[$scope.editItem] = saveMember;
           $scope.editItem = -1;
           // update db here
       }
       Init();
   };

   $scope.DeleteMember = function(idx) {
       $scope.family.splice(idx,1);
       // delete from db here
   }

   $scope.EditMember = function(idx){
       $scope.name = $scope.family[idx].name;
       $scope.age = $scope.family[idx].age;
       $scope.editItem = idx;
       $scope.buttonText = 'Update';
   }

   function Init() {
        $scope.name = '';
        $scope.age = '';
        $scope.editItem = -1;
        $scope.buttonText = 'Add new';
   }

   Init();

}]);

var familyMembers = [
    { name: "Joe", age: 42}
    , { name: "Jane", age: 40}
    , { name: "Bob", age: 13}
];

HTML

        <link href="./css/bootstrap.min.css" rel="stylesheet" type="text/css" />

Family Members:

                    {{p.name}}
                    {{p.age}}
                    X

Add member

            name

            <input type="text" ng-model="name">

            age

            <input type="number" ng-model="age">

            <input type="button" value="{{buttonText}}" ng-click="SaveMember()" />


texts: Elegant solution to find a pair of Strings in a List

texts:
I'm new to Java8 and I must find an elegant solution to a problem. The problem goes I have a List of letters (of any size) that could contain any letter such as "A", "B", "C", "D", etc. I must check if I have an pair of "A" and "B". So, if I have something like ["A", "A", "C", "Z"], since I don't have both "A" and "B" the code should throw an error. I need to refactor something like this in java8...

import java.util.Arrays;
import java.util.List;

public class atest {

    public static void checkForABPair(List<String> letters) {
        int iA = 0;
        int iB = 0;
        for (String letter : letters) {
            if (letter.equals("A")) {
                iA = 1;
            }
            if (letter.equals("B")) {
                iB = 1;
            }
        }
        if ((1 == iA || 1 == iB) && (1 != iA || 1 != iB)) {
            System.out.println(letters + " - ERROR! The pair of letters [A - B] must be present at least once, or not at all!");
        } else {
            System.out.println(letters + " - Everything fine!");
        }
    }

    public static void main(String[] args) {

        List<String> letters = Arrays.asList("B", "A", "C");
        checkForABPair(letters);

        letters = Arrays.asList("A", "A", "C");
        checkForABPair(letters);

        letters = Arrays.asList("M", "S", "C");
        checkForABPair(letters);
    }
}

This must return the following:

[B, A, C] - Everything fine!
[A, A, C] - ERROR! The pair of letters [A - B] must be present at least once, or not at all!
[M, S, C] - Everything fine!

I need it done in a more elegant way. Anyone have a more elegant solution? Thanks!


texts: Alternative to using strncpy() as 'safe' version of strcpy()

texts:
I am thinking of using this as part of a header file to give me a useful string-manipulation tool. Elements is size of dest to prevent overflow:

int copystring(char *dest,char *source,int elements)
{
    int run,omitted=0;
    for(run=0;run<elements-1;run++)//last reserved for'\0'
    {
       dest[run]=source [run];
       if (dest[run]=='\0')
       {
           break;
           //doesn't increment, so a below will be '\0'
       }
    }
    //now see if characters missed out, starting from first non-copied character
    char a=source[run];
    while (a!='\0')
    {
       run++;//move on to next char in source
       omitted++;// missed out one more char
       a=source[run];
    }
    dest[elements-1]='\0';//could add an if condition if omitted is 0
    return omitted;///tells parent function how many letters weren't copied
}

It may be worth my while adding a macro defining maximum amount of omitted chars to check for, and if exceeded to return -1.

Advantages: terminates in null charcter like strcpy while avoiding overflow like strncpy; returns the amount of characters, if any, that were omitted, so saves writing bit of code for handling this if needed.


texts: Can I simplify this recursive grid search?

texts:
I've written a recursive method for fetching grid neighbours in a symmetrical 2-dimensional grid, the problem is that I've found myself duplicating the code more or less into an overload to prevent my list from re-initializing everytime the method calls on itself.

I'm guessing there are ways to get around this that doesn't involve code duplication. Any help is appreciated.

public List<Cell<int>> GetNeighbours(Cell<int> cell, Direction direction, int neighboursToGet)
        {
            var neighbours = new List<Cell<int>>();

            if (neighboursToGet > 0)
            {
                int x = 0;
                int y = 0;
                Cell<int> neighbour;

                switch (direction)
                {
                    case Direction.Left:
                        x = -1;
                        break;
                    case Direction.Right:
                        x = 1;
                        break;
                    case Direction.Up:
                        y = -1;
                        break;
                    case Direction.Down:
                        y = 1;
                        break;
                }

                neighbour = cells[cell.Position.Row + y, cell.Position.Column + x];
                neighbours.Add(neighbour);
                GetNeighbours(neighbour, direction, neighboursToGet - 1, neighbours);
            }
            return neighbours;
        }

        public List<Cell<int>> GetNeighbours(Cell<int> cell, Direction direction, int neighboursToGet, List<Cell<int>> neighbours)
        {
            if (neighboursToGet > 0)
            {
                int x = 0;
                int y = 0;
                Cell<int> neighbour;

                switch (direction)
                {
                    case Direction.Left:
                        x = -1;
                        break;
                    case Direction.Right:
                        x = 1;
                        break;
                    case Direction.Up:
                        y = -1;
                        break;
                    case Direction.Down:
                        y = 1;
                        break;
                }

                neighbour = cells[cell.Position.Row + y, cell.Position.Column + x];
                neighbours.Add(neighbour);
                GetNeighbours(neighbour, direction, neighboursToGet - 1, neighbours);
            }
            return neighbours;
        }


texts:
The following code is an implementation of heapsort on an array

public static void heapSort(int[] inputArray){
    /* Creates an array A which will contain the heap */
    /* A has size n+1 to allow 1-based indexing */
    int n = inputArray.length;
    int[] A = new int[n+1];
    int temp = 0;

    /* Copies the array inputArray into A, with inputArray[i] being stored in A[i+1] */

    for(int i=0; i<n; i++){
        A[i+1] = inputArray[i];
    }
    constructHeap(A, n, 1);
    removeMax(A, n);
    copyBack(A, inputArray);
}

    /* Transforms A into a max-heap using a 'bottom-up' algorithm. */
public static void constructHeap(int[] A, int n, int i){
    if(2*i>n) return;
    constructHeap(A, n, 2*i);
    constructHeap(A, n, 2*i+1);
    bubbleDown(A, n, i);
}
/*recursively swaps parent/child relationships until the max-heap property is satisfied. */
public static void bubbleDown(int[] A, int n, int i){
    if(2*i>n) return;
    int leftChild = 2*i;
    int rightChild = 2*i+1;
    int max = leftChild;
    if(rightChild<=n && A[max]<A[rightChild]){
        max = rightChild;
    }
    if(A[i]<A[max]){
        int temp = A[i];
        A[i] = A[max];
        A[max] = temp;
        bubbleDown(A, n, max);
    }
}

    /* Performs a sequence of n 'remove-maximum' operations, storing the removed element at
       each step in successively smaller indices of A */

public static void removeMax(int[] A, int i){
    for(int i=n; i>0; i--){
        int temp = A[1];
        A[1] = A[i];
        bubbleDown(A, i, 1);
        A[i] = temp;
}

    /* Copies the sorted values in A back into inputArray, with inputArray[i] getting
       the value from A[i+1] */

public static void copyBack(int[] A, int[] inputArray){
    for(int i=0; i<inputArray.length; i++){
        inputArray[i] = A[i+1];
    }
}


texts:
I wrote this simple page to help at work, and as an exercise to get more familiar with JavaScript.

I'm especially interested in help with style and structure - is this "standard"?

I know the input isn't validated at all - feel free to make suggestions here but this isn't my main concern at this time.

behavior.js:

var output_prefix = 'Total duration is: ';
var entries = [];
var list_display = null;

function addDuration() {
    var input_text = document.getElementById("input_text");
 if (1) {  //add validation, input must be hh:mm
     var hhmm = input_text.value.split(":");
     var input_val = Number(hhmm[0]*60) + Number(hhmm[1]);
  entries.push(input_val);
     updateTotalDisplay();
     var new_node = document.createElement("P");
     new_node.appendChild(document.createTextNode(formatTime(input_val)));
     list_display.appendChild(new_node);
 } else {
     alert("not a number");
 }
 input_text.value = "";
 input_text.focus();
}

function popDuration() {
    entries.pop();
 updateTotalDisplay();
 list_display.removeChild(list_display.lastChild);
}

function updateTotalDisplay() {
    var total = 0;
    for (var i = 0; i<entries.length; i++) {
     total = total + entries[i];
 }
    document.getElementById("totaltimetext").innerHTML = output_prefix + formatTime(total);
    return;
}

function formatTime(num_minutes) {
    var time_str = "" + Math.floor(num_minutes/60).toString() + ":";
 var mm = num_minutes%60;
 if (mm>=0 && mm<10) {
     time_str += "0";
 }
 time_str += mm;
    return time_str;
}

window.onload = function() {
    list_display = document.getElementById("duration_list");
    updateTotalDisplay();
 document.getElementById("add_button").addEventListener("click", addDuration);
 document.getElementById("pop_button").addEventListener("click", popDuration);
}
<!DOCTYPE html>

Simple Hours:Minutes Adder

Enter duration in format hh:mm and select ok, running total at top

text

    <form>
        <input id="input_text" type="text"></input>
        <input id="add_button" type="button" value="add">
        <input id="pop_button" type="button" value="pop item">
    </form>


texts:
I have a couple of models - User & Score. I am displaying data from those models to my template. Data from Score is filtered based on data from User. I am using nested for loops and an if condition within the template to get my desired result. However, I want to know if there is a better way of coming up with the same result because once the data gets huge by number, the nested for loop is going to use up a lot of time and resource.

models.py

class User(models.Model):
    name = models.CharField(max_length=20)
    age = models.IntegerField()

class Score(models.Model):
    user = models.ForeignKey('User')
    subject = models.CharField(max_length=20)
    score = models.IntegerField()

views.py

def Home(request):
    users = User.objects.all()
    scores = Score.objects.all()
    return render(request, "home.html", {'users': users, 'scores': scores})

home.html (template)

{% for user in users %}
    {% for score in scores %}
    {% if score.user == user %} {{ user }} -- {{ score.subject }} -- {{ score.score }} <br>{% endif %}
    {% endfor %}
    {% endfor %}

Result

Michel -- Physics -- 80
Michel -- Chemistry -- 90
Zarif -- Physics -- 100
Zarif -- Biology -- 80

This question is a simplified version of this (Django ORM usage in template). If you can, maybe answer over there too!


texts:
This is basically a straightforward heap implementation. I am just moving from C to Python and I wanted to make sure that I follow Python's best practices in general. This heap is supposed to support data from any data type with any comparing function.

class Heap(object):
    """"
    Attributes:
        heap: List representation of the heap
        compar(p, c): comparator function, returns true if the relation between p and c is parent-chield
    """
    def __init__(self, compar):
        self.heap = []
        self.compar = compar
    def is_empty(self):
        return len(self.heap) == 0
    def _inv_heapify(self, element_id):
        """
        Do heapifying starting from bottom till it reaches the root.
        """
        while element_id > 0:
            if self.compar(self.heap[element_id / 2], self.heap[element_id]):
                return
            self.heap[element_id / 2], self.heap[element_id] = self.heap[element_id], self.heap[element_id / 2]
            element_id /=2
    def _heapify(self, element_id):
        """
        Do heepifying starting from the root.
        """
        l = len(self.heap)
        if l == 1:
            return
        while 2 * element_id < l:
            el_id = 2 * element_id
            if 2 * element_id + 1 < l and self.compar(self.heap[element_id * 2 + 1], self.heap[element_id * 2]):
                el_id += 1
            if self.compar(self.heap[element_id], self.heap[el_id]):
                return
            self.heap[element_id], self.heap[el_id] = self.heap[el_id], self.heap[element_id]
            element_id = el_id
    def del_min(self):
        if self.is_empty():
            return None
        x = self.heap.pop(0)
        if not self.is_empty():
            self.heap = [self.heap[-1]] + self.heap[0:-1]
            self._heapify (0)
        return x
    def min(self):
        if self.is_empty():
            return None
        return self.heap[0]
    def add(self, element):
        self.heap +=[element]
        self._inv_heapify (len (self.heap) - 1)


texts: Extremely lightweight dependency injection

texts:
I need to have access to a set of common data inside a few functions. The data values are determined only at runtime, and are far away in terms of control flow, so handing them to the function as an argument is out of the question. What I want to do, in the end, is

where I have the data:

from .injection import offer

def launch_new_case(customer_type):
    case_id = get_random_uuid()
    offer(case_id=case_id, customer_type=customer_type)
    launch_computation()

and then somewhere deep in the computation:

from .injection import retrieve

def computation_step(arg1, arg2):
    customer_type = retrieve("customer_type")
    if customer_type == 1:
        return arg1
    elif customer_type == 2:
        return arg2
    else:
        case_id = retrieve("case_id")
        logger.error(f"Encountered invalid {customer_type=} for {case_id=}.")

The basic idea is that I have a submodule, injection.py that holds a module-level dictionary and provides a function to add to that dict and read from that dict. To be extra sure not to screw things up (I will have to read from the dict in a multithreading environment), I added a lock.

# my_project/injection.py
"""Provide lightweight dependency injection for my_project."""

from threading import Lock
import warnings

__injector = {}

def offer(**kwargs):
    """Offer objects to be injected elsewhere.

    Parameters
    ----------
    kwargs
        Each keyword argument will be added to the injector as given

    """
    existing_injections = set(kwargs.keys).intersection(set(__injector.keys))
    if existing_injections:
        warnings.warn(f"Overwriting keys in injector: "
                      f"{', '.join(existing_injections}")
    with Lock():
        __injector.update(kwargs)

def retrieve(key):
    """Retreive an object from the injector.

    Parameters
    ----------
    key: string
        The key of the object to inject

    """
    with Lock():
        return __injector[key]

I am aware that usual dependency injection setups involve several classes handling the organization and distribution of the objects to be injected. What are potential pitfalls with my solution? How can it be improved?


texts: Dealing with Entity Framework's lack of enum support

texts:
I'm using Entity Framework Code First CTP 5 for a current project. There is still no enum support in Entity Framework so this is what I am using in order to avoid having magic numbers or casts littered throughout my code.

Simplified Example:

Domain Model Consumed by Code First

public class Address
{
    // This would normally be an enum
    public int Type { get; set; }
}

Repository

public class AddressRepository
{
    public Add(Address address)
    {
        // Compare using enum semantics
        if (address.Type == AddressType.Home)
            // Do something

        context.Add(address);
    }
}

View Model

public class AddressVM
{
    // Using the AddressType class for the view model
    public AddressType Type { get; set; }

    public AddressVM(Address address)
    {
        this.Type = address.Type;
    }
}

Enum Replacement Class

public interface IEnumClass
{
    List<string> Properties { get; }
}

public class AddressType : IEnumClass
{
    public static int Home { get { return 0; } }
    public static int Work { get { return 1; } }

    public List<string> Properties { get { return properties; } }

    private List<string> properties;
    private int id;

    public AddressType()
    {
        SetProperties();
    }
    public AddressType(int id)
    {
        this.id = id;
        SetProperties();
    }

    private void SetProperties()
    {
        properties = this.GetType().GetProperties(BindingFlags.Public|BindingFlags.Static)
            .Select(x => x.Name).ToList();
    }

    public static implicit operator AddressType(int id)
    {
        return new AddressType(id);
    }

    public static implicit operator String(AddressType addressType)
    {
        return addressType.properties[addressType.id];
    }

    public static implicit operator int(AddressType addressType)
    {
        return addressType.id;
    }
}

My Personal Assessment

Pros:

Allows me to write business rules without using magic numbers
Uses the same semantics/syntax as enums for easy refactoring down the road
Allows for an implicit conversion to a string for the view model
My select list factory class can easily be made to accept any IEnumClass and generate a select list for the view model

Cons:

A little verbose
As far as I can tell it is impossible to use a nice abstract generic class because all the actual work uses implicit type casting

I'm asking for feedback here because I'm a total novice programmer so I want to solicit some critiques before implementing something I just came up with!


texts: Let's speed that file sentence searching program

texts:
Intro:
I've written a small piece of Python program which is looking for a given sentence in multiple sub directories of a given path.
I'm looking for improvements regarding the speed of my script.

Code:
from os import walk
from os.path import join

def get_magik_files(base_path):
    """
    Yields each path from all the base_path subdirectories

    :param base_path: this is the base path from where we'll start looking after .magik files
    :return: yield full path of a .magik file
    """
    for dirpath, _, filenames in walk(base_path):
        for filename in [f for f in filenames if f.endswith(".magik")]:
            yield join(dirpath, filename)

def search_sentence_in_file(base_path, sentence):
    """
    Prints each file path, line and line content where sentence was found

    :param base_path: this is the base path from where we'll start looking after .magik files
    :param sentence: the sentence we're looking up for
    :return: print the file path, line number and line content where sentence was found
    """
    for each_magik_file in get_magik_files(base_path):
        with open(each_magik_file) as magik_file:
            for line_number, line in enumerate(magik_file):
                if sentence in line:
                    print('[# FILE PATH    #] {} ...\n'
                          '[# LINE NUMBER  #] At line  {}\n'
                          '[# LINE CONTENT #] Content: {}'.format(each_magik_file, line_number, line.strip()))
                    print('---------------------------------------------------------------------------------')

def main():
    basepath = r'some_path'
    sentence_to_search = 'some sentence'

    search_sentence_in_file(basepath, sentence_to_search)

if __name__ == '__main__':
    main()

Miscellaneous:
As you may already figured out, the reason for my program being so slow resides in search_sentence_in_file(base_path, sentence) where I need to open each file, read it line by line and look for a specific sentence.
I know I could use a logging library instead of printing the results to see who matched what, but that wouldn't serve the purposes of the program. So I'm not looking for that (I'm building this to have a fast way of looking for certain classes / methods / slots definitions in multiple .magik files in a fast manner. Opening a log file won't satisfy me).
For whoever is interested in the Magik language, and as a bonus of taking your time to take a look at this question, here's a small introduction to Magik.

To sum up:

is there any way of improving the speed of my program ?
do you have other suggestions regarding the way I'm searching a sentence ?

PS: I'm looking for answers which aims a Windows distro.
Any other improvements are welcomed !


texts:
I am learning C++ and today I've implemented a version of the binary search tree. I would like your thoughts about it.

node.h

#pragma once

struct Node
{
   int val;
   Node *left;
   Node *right;
};

bTree.h

#include "node.h"

#pragma once

class BTree
{
private:

    Node *root;
    void addValue(int n, Node *node);
    void traverse(Node *node);
    Node* search(int n, Node *node);

public:

    BTree();

    void addValue(int n);
    void traverse();
    void search(int n);
};

bTree.cpp

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

BTree::BTree()
{
    root = nullptr;
}

void BTree::addValue(int n, Node *node) {
    if (n < node->val)
    {
        if (node->left != nullptr)
        {
            addValue(n, node->left);
        }
        else
        {
            node->left = new Node;
            node->left->val = n;
            node->left->left = nullptr;
            node->left->right = nullptr;
        }
    }
    else if (n >= node->val)
    {
        if (node->right != nullptr)
        {
            addValue(n, node->right);
        }
        else
        {
            node->right = new Node;
            node->right->val = n;
            node->right->left = nullptr;
            node->right->right = nullptr;
        }

    }
}

void BTree::traverse(Node *node) {
    if (node->left != nullptr)
        traverse(node->left);
    std::cout << node->val << ' ';
    if (node->right != nullptr)
        traverse(node->right);
}

Node* BTree::search(int n, Node *node) {
    if (node != nullptr)
    {
        if (node->val == n)
            return node;
        else if (n < node->val)
            return search(n, node->left);
        else if (n > node->val)
            return search(n, node->right);
    }
    else
        return nullptr;
}

void BTree::addValue(int n) {
    if (root == nullptr)
    {
        root = new Node;
        root->val = n;
        root->left = nullptr;
        root->right = nullptr;
    }
    else
    {
        addValue(n, root);
    }
}

void BTree::traverse() {
    if (root == nullptr)
    {
        std::cout << "No nodes in the tree" << std::endl;
    }
    else
    {
        traverse(root);
    }
}
void BTree::search(int n) {
    if (root == nullptr)
    {
        std::cout << "No nodes in the tree" << std::endl;
    }
    else
    {
        if (search(n, root))
            std::cout << n << " found" << std::endl;
        else
            std::cout << n << " not found" << std::endl;
    }
}

main.cpp

#include <iostream>
#include "bTree.h"
#include <ctime>

int main()
{
    srand(static_cast<unsigned int>(time(0)));
    rand();

    BTree bTree{};

    for (int i = 0; i < 50; i++)
    {
        int n = rand() % 100;
        std::cout << n  << ' ';
        bTree.addValue(n);
    }

    std::cout << std::endl << std::endl;

    bTree.traverse();

    std::cout << std::endl << std::endl;

    for (int i = 0; i < 10; i++)
    {
        int n = rand() % 100;
        bTree.search(n);
    }

    std::cout << std::endl << std::endl;

    system("pause");

    return 0;
}


texts: Get data from user input as workbook or worksheet

texts:
I had two different functions for populating a data array. I tried to refactor them using a single argument as to whether or not the user should be selecting a worksheet or just the workbook.

The primary issue being whether I need to prompt the user to select a range or not. The main difference between the two before was if using just a sheet I didn't need xlApp.

I came at the refactoring a little backwards, so I imagine it can be improved.

Option Explicit
Private Function ImportDataFromExternalSource(ByVal pickSheet As Boolean, Optional ByVal numberOfColumns As Long = 0) As Variant
    Dim lastRow As Long
    Dim fileName As String
    Dim xlApp As New Application
    Set xlApp = New Excel.Application
    Dim targetBook As Workbook
    Dim targetSheet As Worksheet
    Dim targetDataRange As Range
    On Error GoTo ErrorHandler
    fileName = File_Picker

    Set targetBook = xlApp.Workbooks.Open(fileName)
    Set targetSheet = targetBook.Sheets(1)

    If pickSheet Then
        xlApp.ActiveWorkbook.Windows(1).Visible = True
        xlApp.Visible = True
        targetBook.Activate
        targetBook.Sheets(1).Activate
        Set targetDataRange = xlApp.InputBox("Pick a cell on the sheet you would like to import", Type:=8)
        Set targetSheet = targetDataRange.Parent
    End If

    If numberOfColumns = 0 Then numberOfColumns = targetSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    lastRow = targetSheet.Cells(Rows.Count, 1).End(xlUp).Row
    ImportDataFromExternalSource = targetSheet.Range(targetSheet.Cells(1, 1), targetSheet.Cells(lastRow, numberOfColumns))

CleanExit:
    If pickSheet Then
        xlApp.Quit
        Exit Function
    End If
    ThisWorkbook.Activate
    targetBook.Close
    Exit Function

ErrorHandler:
    MsgBox "you've cancelled"
    Resume CleanExit
End Function

Public Function File_Picker() As String
    Dim workbookName As String
    Dim selectFile As FileDialog
    Set selectFile = Application.FileDialog(msoFileDialogOpen)
    With selectFile
        .AllowMultiSelect = False
        .Title = "Select the file with your data."
        .Filters.Clear
        .Filters.Add "Excel Document", ("*.csv, *.xls*")
        .InitialView = msoFileDialogViewDetails
        If .Show Then File_Picker = .SelectedItems(1)
    End With
    Set selectFile = Nothing
End Function

I was using this little procedure to test -

Public Sub test()
    Dim x As Variant
    x = ImportDataFromExternalSource(False, 1)
    Debug.Print x(1)
End Sub


texts: Program that reads 4 files and outputs contents of it into another file after formatting it

texts:
The below Python 3 program scans 4 files a.txt, b.txt, c.txt, d.txt and then outputs the read data into a file output.txt in a formatted way. The first line of each file is guaranteed to have a header and the second line of each file will be blank. I'm required to scan those four files.

Program:

def main():
    with open('a.txt', 'r') as file_a, open('b.txt', 'r') as file_b, open('c.txt', 'r') as file_c, open('d.txt', 'r') as file_d:
        lines1, lines2, lines3, lines4 = file_a.readlines(), file_b.readlines(), file_c.readlines(), file_d.readlines()
        lines = [lines1, lines2, lines3, lines4]
        number_of_spaces = 5

        assert len(lines1) == len(lines2) == len(lines3) == len(lines4), "Error. Different number of lines found in the files"

        row, column = 0, 1
        with open('output.txt', 'w') as output:

            while row < len(lines):
                output.write(lines[row][0].strip() + ' ' * number_of_spaces)
                row += 1
            output.write('\n')
            row = 0

            while column < len(lines1):
                while row < len(lines):
                    output.write(lines[row][column].strip() + ' ' * (number_of_spaces + len(lines[row][0].strip()) - len(lines[row][column].strip())))
                    row += 1
                output.write('\n')
                column += 1
                row = 0

if __name__ == "__main__":
    main()

When executed gives output.txt:

Sl.No     Elements     Abbreviation     Mass

1         Hydrogen     H                1
2         Helium       He               4
3         Lithium      Li               7
4         Beryllium    Be               9
...
98        Californium  Cf               251
99        Einsteinium  Es               252
100       Fermium      Fm               257

Is there any room for improvement?

Additional information (if necessary):

Note that ... in the files mean that there are a lot of similar data and not that the file contains those dots.

a.txt:

Sl.No

1
2
3
...
99
100

b.txt:

Elements

Hydrogen
Helium
Lithium
Beryllium
...
Californium
Einsteinium
Fermium

c.txt:

Abbreviation

H
He
Li
Be
...
Cf
Es
Fm

d.txt:

Mass

1
4
7
9
...
251
252
257


texts: Reading in millions of lines of AOL query logs for query suggestion/expansion

texts:
I am working on a query suggestion program, and to faciliate this, I am reading in millions of AOL queries logs. The logs have this format:

AnonID  Query   QueryTime
479 family guy  2006-03-01 16:01:20
479 family guy movie references 2006-03-03 22:37:46
479 top grossing movies of all time 2006-03-03 22:42:42
479 car decals  2006-03-03 23:20:12
479 car window decals   2006-03-03 23:24:05
479 car window sponsor decals   2006-03-03 23:27:17
479 car sponsor decals  2006-03-03 23:28:59
...

Where each "column" is separated by a tab character.

This following code reads in the AOL Query logs into a Guava MultiMap (to allow duplicates) and then puts those into an Apache-Collections PatriciaTrie so that prefixes can be grabbed efficiently for future query suggestion/expansion.

private void readInAolQueries(String directory)
{
    try (Stream<Path> paths = Files.walk(Paths.get(directory)))
    {
        Multimap<String, QueryLog> multimap = TreeMultimap.create();
        paths
                .filter(Files::isRegularFile)
                .filter(path -> path.toString().endsWith(".txt"))
                .filter(path -> path.toString().contains("Clean-Data-"))
                .forEach(path ->
                {
                    String fileName = path.toString();
                    List<QueryLog> queryLogs = new AolQueryLogsProcessor(fileName).getQueryLogs();
                    // Read logs into a multimap to preserve duplicates
                    multimap.putAll(Multimaps.index(queryLogs, QueryLog::getQueryString));
                });
        //Put the multimap into the trie. It now also has duplicates.
        queryTrie.addAll(multimap.asMap());
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

The AOL logs files are in my resources directory (which is passed into the readInAolQueries method), and the files all have the form Clean-Data-XX.txt where XX is a number.

Here is the code for the AolQueryLogsProcessor class from the code above:

public class AolQueryLogsProcessor
{
    private List<QueryLog> queryLogs;

    public AolQueryLogsProcessor(String fileName)
    {
        queryLogs = new ArrayList<>();
        try
        {
            List<String[]> lines = readFile(fileName)
                    .stream()
                    .filter(line -> !line.isEmpty())
                    .map(line -> line.split("\t"))
                    .collect(Collectors.toCollection(ArrayList<String[]>::new));

            //Dates looks like this 2006-03-28 20:39:58
            final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            //The first line is the header. Skip it.
            for (int i = 1; i < lines.size(); ++i)
            {
                String anonId = lines.get(i)[0];
                String[] query = lines.get(i)[1].split("\\s");
                LocalDateTime timeStamp = LocalDateTime.parse(lines.get(i)[2], formatter);
                QueryLog log = new QueryLog(anonId, query, timeStamp);
                queryLogs.add(log);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private List<String> readFile(String fileName) throws IOException
    {
        return Files.readAllLines(Paths.get(fileName), Charset.forName("UTF-8"));
    }

    public List<QueryLog> getQueryLogs()
    {
        return Collections.unmodifiableList(queryLogs);
    }
}

I'm using a lot of Java 8 Streams, lambdas, and the like because I am trying to familiarize myself with them.

I'm looking for a better and more efficient way to read in the millions of query logs. On my reasonably fast computer, it takes 45-50 seconds to read in all of the logs.

Is there something I can do to speed up this whole process?


texts:
The TopCoder question specifies (in summary):

  Two words are considered rhyming if they have the same ending pattern (defined below). This comparison is case-insensitive (see Example 1.)

  An ending pattern is a substring of a word such that:

  The word ends with that substring,
  The substring contains exactly one contiguous string of vowels,
  The first letter of the substring is a vowel, and
  The substring must either be the whole string, or the letter immediately preceding the start of the substring must be a nonvowel.

  For example, the ending pattern of "bought" is "ought", the ending pattern of "spying" would be "ying", and the ending pattern of "all" would be "all". (Note that "spy" has no vowels, and thus is not a legal word.)

  Example 1:

{"     ",
 "Measure your height",
 "AND WEIGHT      ",
 "said the doctor",
 "",
 "And make sure to take your pills",
 "   to   cure   your    ills",
 "Every",
 "DAY"}

  Returns: " aab ccde"
  Even though "height" and "weight" don't actually rhyme in English, they do by the rules laid out above.

I want to get general feedback and feedback about if I can make this more Ruby-like, if there are any obvious inefficiencies, and if I need to organize my class differently.

#!/usr/bin/env ruby -w
require 'set'

class Poetry
  def find_ending(str)
    vowels = Set.new(['a', 'e', 'i', 'o', 'u'])
    vowel_seen = false
    ending = ""
    str.reverse.each_char.with_index do |c, i|
      if vowels.include?(c) or
         (c == 'y' and i != 0 and i != str.length-1)
        vowel_seen = true
        ending = c + ending
      elsif !vowel_seen
        ending = c + ending
      else
        break
      end
    end
    ending
  end

  def rhyme_scheme(poem_array)
    scheme = ""
    next_label = 'a'
    ending_labels = {}
    poem_array.each do |line|
      if line.strip.empty?
        scheme += ' '
      else
        word = line.split(' ')[-1]
        ending = self.find_ending(word.downcase)
        label = ending_labels[ending]
        if label.nil?
          scheme += next_label
          puts ending_labels
          ending_labels[ending] = next_label
          puts ending_labels
          if next_label == 'z'
            next_label = 'A'
          else
            next_label = next_label.next
          end
        else
          scheme += label
        end
      end
    end
    scheme
  end
end

poetry = Poetry.new

poem_array = ["I hope this problem",
 "is a whole lot better than",
 "this stupid haiku"]

puts "'#{poetry.rhyme_scheme(poem_array)}'"


texts:
I have created a function that converts from UTF-16 to UTF-8.
This function converts from UTF-16 to codepoint firstly,  then from codepoint to UTF-8.
void ToUTF8(char16_t *str) {
    while (*str) {
        unsigned int codepoint = 0x0;

        //-------(1) UTF-16 to codepoint -------

        if (*str <= 0xD7FF) {
            codepoint = *str;
            str++;
        } else if (*str <= 0xDBFF) {
            unsigned short highSurrogate = (*str - 0xD800) * 0x400;
            unsigned short lowSurrogate = *(str+1) - 0xDC00;
            codepoint = (lowSurrogate | highSurrogate) + 0x10000;
            str += 2;
        }

        //-------(2) Codepoint to UTF-8 -------

        if (codepoint <= 0x007F) {
            unsigned char hex[2] = { 0 };
            hex[0] = (char)codepoint;
            hex[1] = 0;
            cout << std::hex << std::uppercase << "(1Byte) " << (unsigned short)hex[0] << endl;
        } else if (codepoint <= 0x07FF) {
            unsigned char hex[3] = { 0 };
            hex[0] = ((codepoint >> 6) & 0x1F) | 0xC0;
            hex[1] = (codepoint & 0x3F) | 0x80;
            hex[2] = 0;
            cout << std::hex << std::uppercase << "(2Bytes) " << (unsigned short)hex[0] << "-" << (unsigned short)hex[1] << endl;
        } else if (codepoint <= 0xFFFF) {
            unsigned char hex[4] = { 0 };
            hex[0] = ((codepoint >> 12) & 0x0F) | 0xE0;
            hex[1] = ((codepoint >> 6) & 0x3F) | 0x80;
            hex[2] = ((codepoint) & 0x3F) | 0x80;
            hex[3] = 0;
            cout << std::hex << std::uppercase << "(3Bytes) " << (unsigned short)hex[0] << "-" << (unsigned short)hex[1] << "-" << (unsigned short)hex[2] << endl;
        } else if (codepoint <= 0x10FFFF) {
            unsigned char hex[5] = { 0 };
            hex[0] = ((codepoint >> 18) & 0x07) | 0xF0;
            hex[1] = ((codepoint >> 12) & 0x3F) | 0x80;
            hex[2] = ((codepoint >> 6) & 0x3F) | 0x80;
            hex[3] = ((codepoint) & 0x3F) | 0x80;
            hex[4] = 0;
            cout << std::hex << std::uppercase << "(4Bytes) " << (unsigned short)hex[0] << "-" << (unsigned short)hex[1] << "-" << (unsigned short)hex[2] << "-" << (unsigned short)hex[3] << endl;
        }
    }
}

Also, you can compile and test the code from here
What do you think about that function in terms of performance, and ease?


texts:
I took information from a series of posts and some prior knowledge to implement the following hashing algorithm.

However, there is a lot of talk about what implementations are secure and not secure.  How does my method measure up?  Is it secure? Are there more secure methods in PHP for hashing tokens and matching with tokens later on?

public static function sha512($token,$cost = 50000,$salt = null) {
        $salt = ($salt == null) ? (generateToken(32)) : ($salt);
        $salt = '$6$rounds=' . $cost . '$' . $salt . ' $';
        return crypt($token, $salt);
}

public static function sha512Equals($token,$hash) {
    return (crypt($token,$hash) == $hash);
}

public static function generateToken($length,$characterPool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
    $token = '';
    $max = mb_strlen($characterPool);

    for ($i = 0;$i < $length;$i++){
        $token .= $characterPool[cryptorand(0,$max)];
    }

    return $token;
}

public static function cryptorand($min, $max) {
    $range = $max - $min;

    if ($range < 0)
        return $min;

    $log = log($range, 2);
    $bytes = (int) ($log / 8) + 1; // length in bytes
    $bits = (int) $log + 1; // length in bits
    $filter = (int) (1 << $bits) - 1; // set all lower bits to 1

    do {
        $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
        $rnd = $rnd & $filter; // discard irrelevant bits
    } while ($rnd >= $range);

    return $min + $rnd;
}


texts: Comparing XML string with different formats

texts:
I need to compare 2 xml strings where the formatting may be different. The approach is to use the XmlWriterSettings to format both strings and compare the formatted result:

public static class XmlHelper
{
    public static string FormatXml(string xml)
    {
        try
        {
            var stringBuilder = new StringBuilder();

            var element = XDocument.Parse(xml);

            var settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = "  ";
            settings.NewLineChars = Environment.NewLine;
            settings.NewLineOnAttributes = false;
            settings.NewLineHandling = NewLineHandling.Replace;

            using (var xmlWriter = XmlWriter.Create(stringBuilder, settings))
                element.Save(xmlWriter);

            return stringBuilder.ToString();
        }
        catch (Exception ex)
        {
            Logger.Error("Unable to format XML: '" + xml + "'", ex);
            return xml;
        }
    }

    public static bool CompareXml(string xmlA, string xmlB)
    {
        if (xmlA == null && xmlB == null)
            return true;
        if (xmlA == null || xmlB == null)
            return false;

        var xmlFormattedA = FormatXml(xmlA);
        var xmlFormattedB = FormatXml(xmlB);

        return xmlFormattedA.Equals(xmlFormattedB, StringComparison.InvariantCultureIgnoreCase);
    }
}

My questions are:

Do you see any problems with that approach?
Is there a simpler way to accomplish that kind of comparison?


texts:
I have a list of categories (in this example I used String but in my project I have a model) which must be sorted like this:

on top we should have 2 categories which have a fixed position in the list (in this example (C, F))
at the bottom of the list we should have 3 categories which have a fixed position in the list (in this example (R, M, P))
in the middle of the list we should have the rest of the elements sorted alphabetically

I want to know if there is a better solution than I used, for sorting the list this way. I do not want to use HashMap, so if anyone could find a better solution for sorting the ArrayList, I would highly appreciate it.

List<String> categories = new ArrayList<>();
    categories.add("A");
    categories.add("B");
    categories.add("C");
    categories.add("D");
    categories.add("E");
    categories.add("F");
    categories.add("G");
    categories.add("H");
    categories.add("I");
    categories.add("J");
    categories.add("K");
    categories.add("L");
    categories.add("M");
    categories.add("N");
    categories.add("O");
    categories.add("P");
    categories.add("Q");
    categories.add("R");

    sortCategories(categories);

The sortCategories() method is:

public void sortCategories(List<String> categories) {
    List<String> topCategories = new ArrayList<>();
    List<String> bottomCategories = new ArrayList<>();

    for (String category : categories) {
        if (category.equals("C")) {
            topCategories.add(category);
            break;
        }
    }

    for (String category : categories) {
        if (category.equals("F")) {
            topCategories.add(category);
            break;
        }
    }

    for (String category : categories) {
        if (category.equals("R")) {
            bottomCategories.add(category);
            break;
        }
    }

    for (String category : categories) {
        if (category.equals("M")) {
            bottomCategories.add(category);
            break;
        }
    }

    for (String category : categories) {
        if (category.equals("P")) {
            bottomCategories.add(category);
            break;
        }
    }

    categories.removeAll(topCategories);
    categories.removeAll(bottomCategories);

    Collections.sort(categories, new Comparator<String>() {

        @Override
        public int compare(String lhs, String rhs) {
            return lhs.compareTo(rhs);
        }
    });

    categories.addAll(0, topCategories);
    categories.addAll(categories.size(), bottomCategories);
}

And the output is:

Category NAME C
Category NAME F
Category NAME A
Category NAME B
Category NAME D
Category NAME E
Category NAME G
Category NAME H
Category NAME I
Category NAME J
Category NAME K
Category NAME L
Category NAME N
Category NAME O
Category NAME Q
Category NAME R
Category NAME M
Category NAME P


texts:
I am getting images using this glob pattern, but the code looks really bad.

First I get all images which have the name screenshot + a number from 0-9 in an array, after that I get all screenshort + a number from 10-99 and so on.

I want to improve this code.

    $dirScreenshotsOne[]   = glob($value.'/screenshot[0-9].png'); // 0-9
    $dirScreenshotsTwo[]   = glob($value.'/screenshot[0-9][0-9].png'); // 10-99
    $dirScreenshotsThree[] = glob($value.'/screenshot[0-9][0-9][0-9].png'); // 100-999

        $dirScreenshotsAll = array_merge(
                         $dirScreenshotsOne,
                         $dirScreenshotsTwo,
                         $dirScreenshotsThree);

    foreach($dirScreenshotsAll as $screenshotKey => $screenshotValue)
    ...


texts: Review of java interface for constructing brain model

texts:
I have built a partial human brain model and the following is an example of how I use all the classes. I was wondering if anyone could critique my implementation strategies because I feel like the following setup is very clumsy. You can view all the code at:
https://github.com/quinnliu/WalnutiQ

public class HowToUseMARK_I extends junit.framework.TestCase {
    private NervousSystem nervousSystem;
    private MemoryClassifier memoryClassifier_Digits;

    private Gson gson;

    public void setUp() throws IOException {
        this.gson = new Gson();
        this.nervousSystem = this.constructConnectedNervousSystem();
        this.memoryClassifier_Digits = this.trainMemoryClassifierWithNervousSystem();
    }

    private NervousSystem constructConnectedNervousSystem() {
        // construct Neocortex with just V1
        Region rootRegionOfNeocortex = new Region("V1", 4, 4, 4, 50, 3);
        RegionToRegionConnect neocortexConnectType = new RegionToRegionRectangleConnect();
        Neocortex unconnectedNeocortex = new Neocortex(rootRegionOfNeocortex,
                neocortexConnectType);

        // construct LGN
        Region LGNRegion = new Region("LGN", 8, 8, 1, 50, 3);
        LateralGeniculateNucleus unconnectedLGN = new LateralGeniculateNucleus(
                LGNRegion);

        // construct Retina
        VisionCell[][] visionCells = new VisionCell[65][65];
        for (int x = 0; x < visionCells.length; x++) {
            for (int y = 0; y < visionCells[0].length; y++) {
                visionCells[x][y] = new VisionCell();
            }
        }
        Retina unconnectedRetina = new Retina(visionCells);

        // construct 1 object of NervousSystem to encapsulate all classes in
        // MARK II
        NervousSystem nervousSystem = new NervousSystem(unconnectedNeocortex,
                unconnectedLGN, unconnectedRetina);

        // connect Retina to LGN
        Retina retina = nervousSystem.getPNS().getSNS().getRetina();

        LateralGeniculateNucleus LGN = nervousSystem.getCNS().getBrain()
                .getThalamus().getLGN();

        SensorCellsToRegionConnect retinaToLGN = new SensorCellsToRegionRectangleConnect();
        retinaToLGN.connect(retina.getVisionCells(), LGN.getRegion(), 0, 0);

        // connect LGN to V1 Region of Neocortex
        Neocortex neocortex = nervousSystem.getCNS().getBrain().getCerebrum()
                .getCerebralCortex().getNeocortex();

        RegionToRegionConnect LGNToV1 = new RegionToRegionRectangleConnect();
        LGNToV1.connect(LGN.getRegion(), neocortex.getCurrentRegion(), 0, 0);

        return nervousSystem;
    }