texts: Correct way to delete elements from a ConcurrentDictionary with a predicate

texts:
I have written a caching wrapper class for ConcurrentDictionary. Basically it has a timer that checks if items are expired and removes them. Since ConcurrentDictionary does not have RemoveAll method that takes a predicate as a parameter, it has to decide which ones are expired first, then remove them. The problem is an item can be unexpired (it may have sliding expiration and it is accessed) before it is removed. I have to do something to make sure my code is correct.

Here's the current implementation:

private void CheckExpiredItems(object sender, ElapsedEventArgs e)
{
    _timer.Stop();
    IEnumerable<TKey> expiredItemKeys = _cachedItems.Where(item => item.Value.IsExpired)
                                                    .Select(item => item.Key)
                                                    .ToList();

    foreach (TKey expiredItemKey in expiredItemKeys)
    {
        ICacheItem<TValue> expiredItem;
        if (_cachedItems.TryRemove(expiredItemKey, out expiredItem))
        {
            if (expiredItem.IsExpired)
            {
                expiredItem.Expire();
            }
            else
            {
                _cachedItems.TryAdd(expiredItemKey, expiredItem);
            }
        }
    }
    _timer.Start();
}

It removes the expired items one by one while checking they are still expired. If it is not expired, it is added back only if there isn't a new item with the same key. However I'm still not sure if this code is correct. I would appreciate a few suggestions. If you want to take a look at the rest, it is on GitHub.


texts: Compare 2 dynamic jagged arrays of doubles and arrays of doubles

texts:
I want to be able to compare 2 jagged arrays of doubles for value equality. The arrays can have any number of dimensions. Inside any dimension could also be another array etc.

It is known that the values of any array dimension is a double, null or another array, so it should be able to compare any of these with one function:

double[]
double[][]
double[][][]
double[][double[]][double[][]]

public static bool ArraysEqual(dynamic a, dynamic b)
{
    // Same objects or both null
    if (ReferenceEquals(a, b)) return true;

    // Just one object is null
    if (a == null ^ b == null) return false;

    // Different number of elements
    if (a.Length != b.Length) return false;

    // Array is empty
    if (a.Length == 0) return true;

    // Test each item in the arrays
    for (int i = 0; i < a.Length; i++)
    {
        // Same object or both null
        if (ReferenceEquals(a[i], b[i])) continue;

        // Just one object is null
        if (a[i] == null ^ b[i] == null) return false;

        var aIsArray = a[i].GetType().IsArray;
        var bIsArray = b[i].GetType().IsArray;

        // Just one object is an array
        if (aIsArray ^ bIsArray) return false;

        // Both are arrays (no need to check b, since it was exclusive or'd prior)
        // Stop evaluating if the recursive call returned false;
        if (aIsArray)
        {
            if (!ArraysEqual(a[i], b[i])) return false;
        }
        else if (a[i] != b[i])
        {
            return false;
        }
    }

    return true;
}


texts:
I have just completed problem 29 from the Project Euler website:

  Consider all integer combinations of \$a^b\$ for \$2 \le a \le 5\$ and \$2 \le b \le 5\$:

  \$2^2=4\$, \$2^3=8\$, \$2^4=16\$, \$2^5=32\$

  \$3^2=9\$, \$3^3=27\$, \$3^4=81\$, \$3^5=243\$

  \$4^2=16\$, \$4^3=64\$, \$4^4=256\$, \$4^5=1024\$

  \$5^2=25\$, \$5^3=125\$, \$5^4=625\$, \$5^5=3125\$

  If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

  \$4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125\$

  How many distinct terms are in the sequence generated by \$a^b\$ for \$2 \le a \le 100\$ and \$2 \le b \le 100\$?

I have written the code for this problem in Java. With my knowledge of Java, I believe it is as efficient as it can be, however, I think it can be improved further.

import java.util.ArrayList;
import java.math.BigInteger;
public class problemTwentyNine {
  public static void main(String [] args) {
    long start = System.nanoTime();
    ArrayList<BigInteger> distinctTerms = new ArrayList<BigInteger>();
    BigInteger temp;
    for(int i = 2; i <= 100; i++) {
      for(int j = 2; j <= 100; j++) {
        temp = BigInteger.valueOf(i).pow(j);
        if(!distinctTerms.contains(temp)) {
          distinctTerms.add(temp);
        }
      }
    }
    System.out.println("Number of distinct terms: " + distinctTerms.size());
    long stop = System.nanoTime();
    System.out.println("Execution time: " + ((stop - start) / 1e+6) + " ms");
  }
}

Output:

  Number of distinct terms: 9183

  Execution time: 306.462625 ms

Now although this is already fairly quick, after looking through some solutions in other languages, I have found that this problem can be solved in just 6 ms in C#. Now I know that it will not be possible in Java as we don't have built in methods like SortedSet.

SortedSet set = new SortedSet();
for (int a = 2; a <= 100; a++) {
    for (int b = 2; b <= 100; b++) {
        double result = Math.Pow(a, b);
        set.Add(result);
     }
 }

C# coded sourced form here.

I'd appreciate any suggestions for ways in which I can improve the performance of the Java solution.


texts:
I recently tried my hand at CodeSprint2 through Interview Street.  Having not coded in quite some time, I wasn't surprised that I had difficulty writing optimized code, but on one problem in particular I can't find where I've gone wrong.  I've compared my code to successful algorithms used by other programmers, and I don't see much difference.  I tried in both Java and Ruby and hit a wall at the 4th test case with both (I'm just learning Ruby so thought it would be a fun exercise to try it - I tweaked my algorithm a little in the process).

The problem is Picking Cards and the description is here.

Code in Java:

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

public class Cards3 {

    public Cards3()
    {
    }

     public void run()
    {
        ArrayList<Integer> cards = new ArrayList<Integer>();
        try{
            /*
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String input = reader.readLine();

            StringTokenizer tokenizer = new StringTokenizer(input);
            int T = Integer.parseInt(tokenizer.nextToken());
            */

            DataInputStream reader = new DataInputStream(System.in);
            int T = reader.readInt();
            reader.readChar();
            System.out.println("T = " + T);
            //read in and test each subsequence
            for(int i = 0; i<T; i++)
            {
                /*
                input = reader.readLine();
                tokenizer = new StringTokenizer(input);
                int N = Integer.parseInt(tokenizer.nextToken());

                //read in and store cards
                String nextLine = reader.readLine();
                tokenizer = new StringTokenizer(nextLine);
                */

                int N = reader.readInt();
                reader.readChar();

                for(int j=0; j<N; j++)
                {
                    int next = reader.readInt();//Integer.parseInt(tokenizer.nextToken());
                    reader.readChar();
                    cards.add(next);
                }
                Collections.sort(cards);
                System.out.println(numWays(cards) % 1000000007);
                cards = new ArrayList<Integer>();
            }

            reader.close();
        }
        catch (IOException e)
        {
            System.out.println("Can not read input.");
        }
    }

    //computers the number of ways to pick up cards in currCards given that
    //numPicked cards have already been picked up
    public long numWays(ArrayList<Integer> cardsLeft)
    {
        int numPicked = 0;
        long numWays = 1;

        //calculate until all cards are picked up
        while(cardsLeft.size() >= 1)
        {
            //if we can't pick up the next card, we're at a dead end - no solutions
            if(cardsLeft.get(0) > numPicked)
                return 0;

            int numCanPick = 0;

            for(int i: cardsLeft)
            {
                if(i <= numPicked)
                {
                    numCanPick++;

                }
                else
                {
                    break;
                }
            }

            cardsLeft.remove(0);
            numWays *= numCanPick;
            numWays = numWays % 1000000007;
            numPicked++;
        }

        return numWays;
    }

     public static void main(String args[])
       {
            Cards3 d = new Cards3();
            d.run();
       }
}

Code in Ruby

def runProgram
  answers = []
  t = STDIN.gets.to_i
  t.times{
    n = STDIN.gets
    numWays = 1
    cards = STDIN.gets.split.map(&:to_i)
    numPicked = 0
    for i in 0...cards.length
      numCanPick = cards.count{|a| a<= i}
      numWays = numWays * (numCanPick-i)
      break if numCanPick==0
      numWays = numWays % 1000000007
    end
     answers << numWays
  }

  puts answers
end

if __FILE__ == $0
 runProgram
 end


texts:
I have to write a program that reads a word to the char array and next display it reversed. I must find best own optimal solution.

My solution of this exercise:

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

char* reverse(char* str);

int main(void) {
    char word[50];
    printf("Podaj slowo: ");
    scanf("%s", word);
    printf("Odwrocone slowo: %s\n", reverse(word));
    return 0;
}

char* reverse(char *str) {
    char* reversed = (char*) malloc(sizeof(char) * strlen(str) + 1);
    int j = 0;
    for (int i = strlen(str) - 1; i >= 0; i--, j++) {
        reversed[j] = str[i];
    }
    reversed[j + 1] = '\0';
    return reversed;
}

I'm applying linear method, specifically iterating through every elements and store it in a new pointer array but I'm not sure if there is any better solution that I don't know. Can anyone add hints how can I optimize code and review it?


texts: Hangman game, written after taking a Python course

texts:
I have recently finished a course on Python 3 and I really learned a lot, but every time I write a script, it is messy and I often feel like there must be shorter, more efficient ways to write and structure my scripts.

So my question is: How could I rewrite the below script to be shorter and more efficient, and what advice would you guys give me to improve my coding skills in general (maybe a way of thinking about the program that will make it easier to define the structure of what my script should look like...)

I am really enthused about the possibilities that coding bring and I would really like to improve, so don't hesitate to be harsh with your comments ;)

Here below is one of the scripts I wrote. This one is a hangman game: (The word list for the game are being picked from a .txt file that lives in the same directory as my script)

import string
import random

class Main:

    def __init__(self):
        self.place_holder = []
        self.wrong_guesses = 10
        self.guesses = []
        with open('./words.txt', mode='r') as f:
            self.word = random.choice(f.readlines())
            self.word = self.word.strip()
            print(self.word, end='')
            return self.place_holders()

    def place_holders(self):
        # creates the placeholders for the word
        for char in self.word:
            if char != " ":
                self.place_holder.append(' _ ')
            elif char == " ":
                self.place_holder.append('   ')
        print("".join(self.place_holder))  # prints initial placeholder
        return self.is_letter()

    @staticmethod
    def guess():
        # Prompts the user for a guess
        while True:
            guessed_letter = input(str('Give a guess: ')).lower()
            if guessed_letter not in string.ascii_lowercase or len(guessed_letter) > 1:
                print(
                    'You have inputted more than one character or the character entered was not recognized.\nPlease try again.')
            else:
                break
        return guessed_letter

    def is_letter(self):
        # finds out if the letter belongs to the word, and if so, places it on the right placeholder
        guessed_letter = self.guess()
        if guessed_letter in self.word and guessed_letter not in self.guesses:
            for i in range(len(self.word)):
                if guessed_letter == self.word[i]:
                    self.place_holder[i] = f' {self.word[i]} '
        elif guessed_letter in self.guesses:
            print('You already said that..\n')
        else:
            self.wrong_guesses -= 1
            print(f'Sorry, missed.\nYou have {self.wrong_guesses} guesses left.\n')

        self.guesses.append(guessed_letter)
        print("".join(self.place_holder))  # prints the updated placeholder
        return self.is_over()

    def is_over(self):
        # Checks if the players has guessed the full word or if the player ran out of guesses
        if ' _ ' in self.place_holder and self.wrong_guesses > 0:
            self.is_letter()
        elif ' _ ' in self.place_holder and self.wrong_guesses == 0:
            print(f'Sorry, Game Over!\nThe word to guess was: {self.word}')
            self.play_again()
        else:
            self.play_again()

    @staticmethod
    def play_again():
        # Prompts the player if he wants to play again or not
        if input('Do you want to play again? (y/n): ').lower().startswith('y'):
            Main()
        else:
            print('Fair enough.. Thanks for playing!')
            quit()

Main()


texts: Finding the percentage of even numbers in an array

texts:
I wrote a method which computes the ratio of even to odd numbers in an array. I know it's a simple piece of code, but I wanted to see if you have any feedback for improving it.

public double percentEven(int[] a) {
if(a.length <= 1) {
    return 0.0;
}
double even = 0.0;
for(int i = 0; i < a.length; i++) {
    if(a[i] % 2 == 0) {
        even++;
    }
}
return (double) (even / a.length) * 100;
}

If the array contains zero elements, or zero even elements, return 0.


texts: Checking for culture in comma separated list, any suggestions?

texts:
I have a basic class that I'd like to get some feedback on. I'm refactoring some old code but the new code looks more complex. Basically I'm passing a list of included and excluded cultures into a function that should return if the current culture should be allowed.  Exclusions win.  Any advice how to better it would be great.  Thanks

public static class CultureCheck
{
    public static bool IsCurrentCultureIncluded(string includedCultures, string excludedCultures)
    {
        if(includedCultures == null)
        {
            throw new ArgumentNullException("includedCultures");
        }

        if (excludedCultures == null)
        {
            throw new ArgumentNullException("excludedCultures");
        }

        var isIncluded = false;

        // exclusions win.
        if (IsCultureInList(excludedCultures) == true)
        {
            isIncluded = false;
        }
        else if(IsCultureInList(includedCultures) == true)
        {
            isIncluded = true;
        }

        return isIncluded;
    }

    private static bool IsCultureInList(string cultures)
    {
        string currentCultureName = Thread.CurrentThread.CurrentCulture.Name;
        var isFound = false;

        if (cultures.IndexOf("*") != -1)
        {
            isFound = true;
        }
        else
        {
            List<string> cultureList = new List<string>(cultures.Split(','));
            if (cultureList.Where(e => e.Equals(currentCultureName, StringComparison.InvariantCultureIgnoreCase)).Any() == true)
            {
                isFound = true;
            }
        }

        return isFound;
    }
}

OLD CODE

 public static bool IsCurrentCultureIncluded(string includedCultures, string excludedCultures)
    {
        #region Exclusion Test

        // exclusions win.
        if (excludedCultures.IndexOf("*") != -1)
        {
            return false;
        }

        string currentCultureName = Thread.CurrentThread.CurrentCulture.Name;
        string[] excludedCulturesSplit = excludedCultures.Split(new char[] { ',' });
        foreach (string excludedCulture in excludedCulturesSplit)
        {
            if (string.Equals(currentCultureName, excludedCulture, StringComparison.InvariantCultureIgnoreCase))
            {
                return false;
            }
        }

        #endregion

        #region Inclusion Test

        if (includedCultures.IndexOf("*") != -1)
        {
            return true;
        }

        string[] includedCulturesSplit = includedCultures.Split(new char[] { ',' });
        foreach (string includedCulture in includedCulturesSplit)
        {
            if (string.Equals(currentCultureName, includedCulture, StringComparison.InvariantCultureIgnoreCase))
            {
                return true;
            }
        }

        #endregion

        return false;
    }


texts: Implementation of Luhn credit card algorithm

texts:
I've implemented Luhn's algorithm for checking credit card numbers.
My code works, but I wanted to learn about a more efficient and more Pythonic way of doing this.

def validate_credit_card_number(card_number):
    #start writing your code here

    #Step 1a - complete
    temp_list=list(str(card_number))
    my_list=[]
    list1 = temp_list[-2::-2]
    list2=temp_list[::-2]
    list2 = [int (n) for n in list2]
    #print(list2)
    my_list=[int(n) for n in list1]
    #print(my_list)
    list1 = [int(n)*2 for n in list1]
    t_list=list1

    for el in list1:
        sum_res=0

        if el>9:
            idx = list1.index(el)
            t_list.pop(idx)

            while el:
                rem = el%10
                sum_res+=rem
                el = el//10
            t_list.insert(idx, sum_res)
    #print(t_list)

    #step 1b

    list1_sum=sum(t_list)
    list2_sum = sum(list2)
    #print(b_list)
    final_sum = list1_sum+ list2_sum
    #print(final_sum)

    if final_sum%10==0:
        return True
    return False

card_number= 1456734512345698 #4539869650133101  #1456734512345698 # #5239512608615007

result=validate_credit_card_number(card_number)
if(result):
    print("credit card number is valid")
else:
    print("credit card number is invalid")


texts:
This is my attempt to implement the Pascal Triangle in C#. It is meant to calculate and return a pascal triangle of size n (which it takes is through the parameter rows).

static List<List<int>> GetPascalTriangle(int rows)
{
    List<List<int>> result = new List<List<int>>();
    for (int x = 0; x <= rows - 1; x++)
    {
        if (x == 0 || x == 1)
        {
            result.Add(Enumerable.Repeat(1, x + 1).ToList());
        }
        else
        {
            List<int> prevRow = result[x - 1];
            List<int> semiResult = new List<int>();
            semiResult.Add(1);
            for (int x1 = 0; x1 <= prevRow.Count - 2; x1++)
            {
                semiResult.Add(prevRow[x1] + prevRow[x1 + 1]);
            }
            semiResult.Add(1);
            result.Add(semiResult);
        }
    }
    return result;
}

I feel this code is a little messy. Thus I seek some opinion on it and ways to improve it.


texts: Which approach is preferable string parsing?

texts:
I want to find total word count in string. I have two methods as mentioned below.

public static int Wordcount(string strName)
{
    bool wordfound;
    int ctr = 0;

    for (int i = 0; i < strName.Length; )
    {
        wordfound = false;

        while ((i < strName.Length) && (strName[i] == ' '))
            i++;

        while ((i < strName.Length) && (strName[i] != ' '))
        {
            if (!wordfound)
            {
                ctr++;
                wordfound = true;
            }
            i++;
        }
    }

    return ctr;
}

public static int Wordcount1(string strName)
{
    string temp = null;
    int ctr = 0;

    for (int i = 0; i < strName.Length; i++)
    {
        if (strName[i] != ' ')
        {
            if (temp == null)
                ctr++;

            temp += strName[i];
        }
        else if (strName[i] == ' ')
            temp = null;
    }

    return ctr;
}

Can anyone please comment on better approach w.r.t. logic, performance and memory utilization?


texts: Storing the contents of text fields and inserting the stored values on button-click

texts:
I've created a little GUI where the text entered in the text-fields is stored in a property of the currently selected button. If a different button is clicked, the contents stored for that button are inserted into the text-fields.
For storing the information I'm currently using a class where each text-field's contents represent a separate attribute:
from PyQt5.QtWidgets import *

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        v_layout = QVBoxLayout()
        cent_widget = QWidget()
        cent_widget.setLayout(v_layout)
        self.setCentralWidget(cent_widget)

        # Buttons
        h_layout = QHBoxLayout()
        v_layout.addLayout(h_layout)

        self.btn_group = QButtonGroup()
        self.btn_group.setExclusive(True)

        btn_1 = QPushButton('Button 1')
        btn_2 = QPushButton('Button 2')
        btn_3 = QPushButton('Button 3')

        for w in [btn_1, btn_2, btn_3]:
            w.setProperty('data', Storage())
            w.setCheckable(True)
            w.clicked.connect(self.set_data)
            self.btn_group.addButton(w)
            h_layout.addWidget(w)
        btn_1.setChecked(True)

        # Text Fields
        self.tf_1 = QLineEdit()
        self.tf_2 = QSpinBox()
        self.tf_3 = QLineEdit()

        for w in [self.tf_1, self.tf_2, self.tf_3]:
            w.editingFinished.connect(lambda x=w: self.update_storage(x))
            v_layout.addWidget(w)

    def update_storage(self, widget):
        active_storage = self.btn_group.checkedButton().property('data')

        if widget == self.tf_1:
            active_storage.field1 = self.tf_1.text()
        elif widget == self.tf_2:
            active_storage.field2 = self.tf_2.value()
        elif widget == self.tf_3:
            active_storage.field3 = self.tf_3.text()

    def set_data(self):
        btn = self.btn_group.checkedButton()
        storage = btn.property('data')

        self.tf_1.setText(storage.field1)
        self.tf_2.setValue(storage.field2)
        self.tf_3.setText(storage.field3)

class Storage:
    def __init__(self):
        self.field1 = ''
        self.field2 = -1
        self.field3 = ''

if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

I have a feeling like I'm working against Qt here or I'm at least not using its full potential, because I think Qt could include some tools to make this more efficient and I just did not find them.


texts: File reader and streamer component running in its own thread on Android

texts:
In an Android project,
I have a class whose job is to read lines from a file,
and then pump those lines to a message handling thread,
looping forever until told to stop.
The main operation of this class (file reading + message sending) must be on its own separate thread,
to run in parallel with whatever else the application is doing.
Multi-threaded programming is not my strongest suit,
so I'd like to reach out to those more knowledgeable than me in this area.

package net.bluetoothviewer;

import android.content.res.AssetManager;

import net.bluetoothviewer.util.AssetUtils;

import java.util.List;

public class MockLineByLineConnector implements DeviceConnector {

    public static final String SAMPLES_SUBDIR = "samples/line-by-line";

    private static final int SLEEP_MILLIS = 1000;

    private final MessageHandler messageHandler;
    private final AssetManager assets;
    private final String filename;

    private boolean running = false;

    public MockLineByLineConnector(MessageHandler messageHandler, AssetManager assets, String filename) {
        this.messageHandler = messageHandler;
        this.assets = assets;
        this.filename = filename;
    }

    @Override
    public synchronized void connect() {
        if (running) {
            return;
        }
        running = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                messageHandler.sendConnectingTo(filename);

                String mockFilePath = SAMPLES_SUBDIR + "/" + filename;
                List<String> lines = AssetUtils.readLinesFromStream(assets, mockFilePath);

                if (!lines.isEmpty()) {
                    loopLinesUntilStopped(lines);
                }

                messageHandler.sendConnectionLost();
            }

            private void loopLinesUntilStopped(List<String> lines) {
                messageHandler.sendConnectedTo(filename);

                while (running) {
                    for (String line : lines) {
                        if (!running) {
                            break;
                        }
                        messageHandler.sendLineRead(line);
                        try {
                            Thread.sleep(SLEEP_MILLIS);
                        } catch (InterruptedException e) {
                            // ok to swallow, this is our dedicated thread
                        }
                    }
                }
            }
        }).start();
    }

    @Override
    public synchronized void disconnect() {
        running = false;
    }

    @Override
    public void sendAsciiMessage(CharSequence chars) {
        // do nothing
    }
}

Do you spot issues with the way I work with the threads?
The app seems to work perfectly well, but it's not exposed to high-concurrency  scenarios that might trigger the problems caused by badly coded multi-threading.

Do I synchronize too much? Could I relax some of the synchronizations by making the running field volatile?

I'm aware that the AssetUtils.readLinesFromStream call within the synchronized region is suspicious, as it's an "alien method", but it has a trivial responsibility and I can trust it won't cause harm, so I think that can be acceptable in this case.

Using Thread and Runnable is a very low-level way of working with threads.
Can I improve this code by using higher level techniques that are recommended in Android development or in Java in general?

Are there any other improvements you'd recommend?

The class is part of the Bluetooth Viewer Android app, an open source project.
To play with it locally:

Clone the project from GitHub, and switch to tag v7 to get the same version as used in this question.
Build (in Android Studio, for example) and run it on a phone or emulator.
Enable mock devices: open the menu, select Settings, and check Enable mock devices.
Return to the main activity, click on the connect button, and select any of the mock devices.

Lines should start rolling on the screen,
coming from this class under review.


texts: Is this a good example of the strategy pattern?

texts:
I've been reading The Pragmatic Programmer for a few days and I've come across a reference to the strategy pattern. Looked it up in Design Patterns and figured I could refactor a piece of code I'm currently working on to comply with the strategy pattern.

I need to process text using many string algorithms much like Lucene analyze and tokenize text. This is for an importation routine. Strategy:

public class ChainOfProcessor: IStringProcessor
{
    public List<IStringProcessor> Processors;

    public ChainOfProcessor()
    {
        Processors = new List<IStringProcessor>();
    }

    public ChainOfProcessor Add<TProcessor>()
        where TProcessor: IStringProcessor, new()
    {
        return Add(new TProcessor());
    }

    public ChainOfProcessor Add(IStringProcessor processor)
    {
        Processors.Add(processor);
        return this;
    }

    public string Process(string input)
    {
        return Processors.Aggregate(input, (current, processor) => processor.Process(current));
    }
}

public interface IStringProcessor
{
    string Process(string input);
}

public class HtmlStripper: IStringProcessor
{
    public virtual string Process(string input)
    {
        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(input);
        return htmlDoc.DocumentNode.InnerText;
    }
}

public class HtmlDecoder: IStringProcessor
{
    public string Process(string input)
    {
        return HttpUtility.HtmlDecode(input);
    }
}

In Design Patterns, the authors use a Composition class and Compositor classes as examples. The client is allowed to chose a single Compositor through Composition to do whatever the Compositor is supposed to do. There is a subtle difference in how the client uses the Composition in my example. Instead of allowing a single IStringProcessor to be used through some Composition class, I allow many, to be executed one after the other through the concrete class ChainOfProcessor. Usage:

var text = new ChainOfProcessor()
    .Add<HtmlStripper>()
    .Add<HtmlDecoder>()
    .Process(html);


texts: Is this a conforming implementation of duration_cast?

texts:
Sadly VS2012's duration_cast is broken, and I actually need the functionality which is broken. So, I wrote my own:

template<typename ToUnit, typename Rep, typename Period>
ToUnit duration_cast_2(const std::chrono::duration<Rep, Period>& right)
{
    typedef std::ratio_divide<Period, typename ToUnit::period>::type ratio;
    typedef std::common_type<std::common_type<typename ToUnit::rep, Rep>::type, intmax_t>::type common_type;
    return ToUnit(static_cast<typename ToUnit::rep>(static_cast<common_type>(right.count()) * static_cast<common_type>(ratio::num) / static_cast<common_type>(ratio::den)));
}

But I'm not entirely confident that it's correct.


texts: Read lines from file, calling a consumer function for each line

texts:
I wrote a function for iterating over a large file with performance in mind. It takes in an InputStream and reads until it reaches the end of the file. Whenever it detects an end of line it will call the UnsafeConsumer#accept with the line.

UnsafeConsumer is the same as Consumer<T> but has a throws declaration on accept function
stream has to be closed outside the function

I am am not sure if this is the fastest way to do this. Is there anything different that I could do?

public static void fileLines(InputStream stream, UnsafeConsumer<String,IOException> cons) throws IOException{
    StringBuilder lineBuild=new StringBuilder();
    int character;

    while(true){ //read file
        while(true){ //read line
            character=stream.read();

            if(character==-1){
                cons.accept(lineBuild.toString());
                lineBuild.setLength(0);
                return;
            }
            if(character=='\n'){
                cons.accept(lineBuild.toString());
                lineBuild.setLength(0);

                character=stream.read();

                if(character==-1) return;
                else if(character!='\r'&&character!='\n') lineBuild.append((char)character);
                break;
            }
            lineBuild.append((char)character);
        }
    }
}


texts: Top users under age 30, sorted by age, with rank

texts:
In the pursuit of SQL enlightenment, I wrote a quick SEDE query to display Rep, Age and Overall Rank.
Sorted by Age, then Rank (Which is generated by Rep).

SELECT TOP 150
ROW_NUMBER() OVER (ORDER BY Users.Reputation DESC) AS Rank,
  Users.Id as [User Link],
  Users.Age,
  Users.Reputation as Rep
FROM Users
WHERE Users.Age IS NOT NULL AND Users.Reputation > 150 AND Users.Age <= 30
ORDER BY Users.Age ASC, Rank ASC;

I attempted formatting, but it feels wrong, and my (possibly) overly long WHERE statement seems incorrectly formatted as well.

What is a more optimal way to write this, if one does exist?

I've had parameters mentioned as an idea on improvement (age range and selection limitation).


texts: Is this reimplemented quicksort correct?

texts:
#include<iostream>
using namespace std;

int partition(int arr[],int first,int last,int pivot)
{
    int tmparr[100];
    int left=first,right=last;
    for(int i=first;i<=last;i++)
    {
        if(arr[i]<pivot)
            tmparr[left++]=arr[i];
        else if(arr[i]>pivot)
            tmparr[right--]=arr[i];
    }
    //for the case when pivot is duplicated
    for(int i=left;i<=right;i++)
        tmparr[i]=pivot;
    for(int i=first;i<=last;i++)
        arr[i]=tmparr[i];
    return left;
}
void quicksort(int arr[],int first,int last)
{
    if(first<last)
    {
    int pivotindex = partition(arr,first,last,arr[first]);
    quicksort(arr,first,pivotindex-1);
    quicksort(arr,pivotindex+1,last);
    }

}
int main()
{
    int arr[6]={6,6,9,5,3,7};
    int n=6;
    quicksort(arr,0,n-1);
    for(int i=0;i<n;i++)
    cout<<arr[i];
}

How do I avoid the copying to tmparr?
How does the current method of copying to tmparr and back affect my time efficiency as opposed to an in-place swap?
Am I missing anything in this code?  It looks much simpler than implementations I could find online.


texts: How to improve this functional python fast exponentiation routine?

texts:
I have the following python functions for exponentiation by squaring :

def rep_square(b,exp):
        return reduce(lambda sq,i: sq + [sq[-1]*sq[-1]],xrange(len(radix(exp,2))),[b])

def exponentiate(b,exp):
        return reduce(lambda res,(sq,p): res*sq if p == 1 else res,zip(rep_square(b,exp),radix(exp,2)),1)

They work. Calling print exponentiate(2,10), exponentiate(3,37) yields :

1024 450283905890997363

as is proper. But I am not happy with them because they need to calculate a list of squares. This seems to be a problem that could be resolved by functional programming because :

Each item in the list only depends on the previous one
Repeated squaring is recursive

Despite people mentioning that recursion is a good thing to employ in functional programming, and that lists are not good friends -- I am not sure how to turn this recursive list of squares into a recursive generator of the values that would avoid a list. I know I could use a stateful generator with yield but I like something that can be written in one line.

Is there a way to do this with tail recursion? Is there a way to make this into a generator expression?

The only thing I have thought of is this particularly ugly and also broken recursive generator :

def rep_square(n,times):
    if times <= 0:
        yield n,n*n
    yield n*n,list(rep_square(n*n,times-1))

It never returns.


texts:
The goal is to call startManager on application start so that the manager is initialized in background without blocking application main thread.

Is it a safe implementation? Any recommendation or best practice? (use an Executor, use a synchronized block, ...)

public class FileConverter
{
    /** The Constant logger. */
    private static final Logger logger = LoggerFactory.getLogger(FileConverter.class);

    private static RunnableFuture<OfficeManager> managerFuture;

    /**
     * Start manager.
     */
    public static synchronized void startManager()
    {
        if(managerFuture == null)
        {
            Callable<OfficeManager> callable = new Callable<OfficeManager>()
            {
                @Override
                public OfficeManager call() throws Exception
                {
                    OfficeManagerConfiguration configuration = new OfficeManagerConfiguration();
                    OfficeManager officeManager = configuration.buildOfficeManager();

                    officeManager.start();

                    logger.info("officeManager started");

                    return officeManager;
                }
            };

            managerFuture = new FutureTask<OfficeManager>(callable);
            new Thread(managerFuture).start();
        }
    }

    /**
     * Stop manager.
     */
    public static synchronized void stopManager()
    {
        if(managerFuture != null)
        {
            managerFuture.cancel(true);

            try
            {
                OfficeManager officeManager = managerFuture.get();
                if(officeManager.isRunning())
                {
                    officeManager.stop();
                }
            }
            catch(CancellationException e)
            {
                // ignore
            }
            catch(InterruptedException e)
            {
                Thread.currentThread().interrupt();
            }
            catch(ExecutionException e)
            {
                logger.warn(e.getMessage(), e);
            }
        }
    }

    /**
     * Convert.
     *
     * @param file the file
     * @param format the format
     * @return the file
     * @throws Exception the exception
     */
    public static File convert(File file, String format) throws Exception
    {
        OfficeManager officeManager = managerFuture.get();
        if(!officeManager.isRunning())
        {
            throw new IllegalStateException("officeManager is not running!");
        }

        File tempFile = File.createTempFile(FileConverter.class.getSimpleName() + "-", "." + format);
        try
        {
            if(file.getName().endsWith("." + format))
            {
                FileUtils.copyFile(file, tempFile);
            }
            else
            {
                OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
                converter.convert(file, tempFile);
            }

            return tempFile;
        }
        catch(Exception e)
        {
            tempFile.delete();
            throw e;
        }
    }
}

Reply to @EricStein

Right. Adding synchronyzed to stopManager() should serialize invocations.
Right. It can cause an unwanted restart.
Right. I misunderstood the InterruptedException being generated in manager thread not by future.get() itself.
This is server-side code: every time I recompile the server, it takes about 30s to startup. This implementation reduces startup time to 25s. I agree it's not worth on production, but it's a time saver on development.

before accepting your answer, I'd like to know if new Thread(managerFuture).start(); is a good way to start an asynchronous operation: it doesn't sound good to start something without preserving a reference...


texts: TypeScript Salesforce client library wrapper

texts:
I have made a class which wraps the JSForce Salesforce API Client:

import jsforce = require('jsforce');

export class SalesforcePortalClient {
  salesforceConsumerKey: string;
  salesforceConsumerSecret: string;
  salesforceUsername: string;
  salesforcePassword: string;
  constructor(salesforceConsumerKey: string, salesforceConsumerSecret: string, salesforceUsername: string, salesforcePassword: string) {
    this.salesforceConsumerKey = salesforceConsumerKey,
      this.salesforceConsumerSecret = salesforceConsumerSecret,
      this.salesforceUsername = salesforceUsername,
      this.salesforcePassword = salesforcePassword
  }

  private getConnection(): jsforce.Connection {
    return new jsforce.Connection({
      oauth2: {
        clientId: this.salesforceConsumerKey,
        clientSecret: this.salesforceConsumerSecret,
        redirectUri: 'http://localhost:3000/callback',
        loginUrl: 'https://test.salesforce.com'
      }
    })
  }

  private async login(): Promise<jsforce.Connection> {
    let conn = this.getConnection()
    await conn.login(this.salesforceUsername!, this.salesforcePassword!)
    return conn
  }

  async getInstanceId(): Promise<string> {
    return this.login().then(l => l.instanceUrl)
  }

  async postApexRest(projectTitle: string, organisationName: string): Promise<any> {
    return await this.login().then(l => l.apex.post('/loadData/', this.buildPayload(organisationName, projectTitle)))
  }

  private buildPayload = (organisationName: string, projectName: string) => {
    return {
      "projectName": projectName,
      "organisationName": organisationName,
    }
  }
}

I have some ideas about how this could be improved but not sure how to implement them, for example:

Is there a way to perform login() either when the class is instantiated or when it is first used and make it available at class level, so the token can be re-used rather than re-created on every request?
It seems like there is a fair amount of boilerplate in the arguments and constructor, is there anyway to reduce this whilst maintaining the same behaviour?

Also, I would welcome any suggestions on ways to improve this, as someone new to TypeScript.


texts: Checking for Internet connectivity on Android

texts:
I wrote this code to check an internet connection and was wondering if this is the best way to do this check.

public static boolean isNetworkAvailable(Context context) {
    boolean status = false;
    try {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getNetworkInfo(0);

        if (netInfo != null
                && netInfo.getState() == NetworkInfo.State.CONNECTED) {
            status = true;
        } else {
            netInfo = cm.getNetworkInfo(1);
            if (netInfo != null
                    && netInfo.getState() == NetworkInfo.State.CONNECTED)
                status = true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return status;
}


texts: Code to take a list, convert to string and insert comma between the 'elements' - From Automate The Boring Stuff

texts:
I have been working on this question in particular. I'm a bit of a python beginner myself, having re-learnt functions just a few days back.

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item. For example, passing the previous spam list to the function would return 'apples, bananas, tofu, and cats'. But your function should be able to work with any list value passed to it. Be sure to test the case where an empty list [] is passed to your function.

My idea for this was to have my function run a for loop on the range of the number of elements of the list, add the elements of the list to a comma. If the element of the list happens to be the last element, 'and' and that specific element are added together.
Here is the code:
def listsmash(listu):
    y = ''
    for x in range(len(listu)):
        if x == len(listu) -1 :
            y+= 'and '+ str(listu[x])
        else:
            y+= str(listu[x]) + ','
    return y
print(listsmash([1,345,34656,456456,456454564]))

Is this an efficient piece of code? Any way for me to make it more efficient than it already is?


texts:
I have a form that allows the user to select a server.  This will execute the exact same query just on a different server.

private void QueryButton_Click(object sender, EventArgs e)
{
    switch (this.FindByComboBox.SelectedIndex)
    {
        case 0:
            ByTargetVDN();
            break;
        // ...
    }
}
void ByTargetVDN()
{
    int value = 0;
    if (this.ValueTextBox.Text.Length > 0)
    {
        value = Convert.ToInt32(this.ValueTextBox.Text);
    }
    switch (this.ServerComboBox.SelectedIndex)
    {
        case 0:
            var gr01 = new GR01.TargetVDNDataTable();
            var adapter = new ICMQuery.Models.GR01TableAdapters.TargetVDNTableAdapter();
            if (value == 0)
            {
                adapter.Fill(gr01);
            }
            else
            {
                adapter.FillByPeripheralNumber(gr01, value);
            }
            this.FindScriptsDataGrid.DataSource = gr01;
            break;
        case 1:
            var ds01 = new DS01.TargetVDNDataTable();
            var adapterNew = new ICMQuery.Models.DS01TableAdapters.TargetVDNTableAdapter();
            if (value == 0)
            {
                adapterNew.Fill(ds01);
            }
            else
            {
                adapterNew.FillByPeripheralNumber(ds01, value);
            }
            this.FindScriptsDataGrid.DataSource = ds01;
            break;
    }
}

There has got to be a way I can streamline this.  I was hoping for something along the lines of

switch (this.ServerComboBox.SelectedIndex)
  {
    case 0:
      var ds = new GR01.TargetVDNDataTable();
      var adapter = new ICMQuery.Models.GR01TableAdapters.TargetVDNTableAdapter();
      break;
    case 1:
      ds = new GR01.TargetVDNDataTable();
      adapter = new ICMQuery.Models.GR01TableAdapters.TargetVDNTableAdapter();
      break;
  }
if (value == 0)
  {
    adapter.Fill(ds);
  }
else
  {
    adapter.FillByPeripheralNumber(ds, value);
  }
this.FindScriptsDataGrid.DataSource = ds;

but of course this doesn't work.


texts:
This cipher takes a byte array and applies a relatively 'random' offset for each byte. In order to keep track of the offsets, to allow the encryption to be reversed, I decided against maintaining a list of offsets, like the old school one-time pads, and went with using the first byte of the hash of the key, then rehashing the hash.  To increase the complexity of the encryption, I used the 1st bit of the second element of the hash, to set the direction of the offset.
The offsets aren't truly random, but the encrypted bytes of a 2mb text file, do pass all the tests in the NIST test suite
class Cipher
{

    private enum EncryptionMode
    {
        Encrypt = 1,
        Decrypt = -1
    }
    public static byte[] Encrypt(string key, byte[] data)
    {
        return DeEncrypt(key, data, EncryptionMode.Encrypt);
    }
    public static byte[] Decrypt(string key, byte[] data)
    {
        return DeEncrypt(key, data, EncryptionMode.Decrypt);
    }
    private static byte[] DeEncrypt(string key, byte[] data, EncryptionMode mode)
    {
        if(key == null)
        {
            key = "";
        }
        if(data == null)
        {
            data = new byte[] { 0 };
        }
        var temp = HashCode(Encoding.ASCII.GetBytes(key));
        var newData = new byte[data.Length];
        for (int i = 0; i < data.Length; ++i)
        {
            newData[i] = GetNewValue(data[i], temp, mode);
            temp = HashCode(temp);
        }
        return newData;
    }
    private static byte[] HashCode(byte[] input)
    {
        var hash = Sha3.Sha3512().ComputeHash(input);
        return hash;
    }
    private static byte GetNewValue(byte value, byte[] hash, EncryptionMode mode)
    {
        //This determines the direction of the offset based on the 1st bit of the second byte in the hash
        //and switches it according to the encryption mode.
        var direction = (((hash[1] & 1) * 2) - 1) * (int)mode;
        var offset = hash[0] * direction;
        return (byte)ShiftMod256((ulong)(value + 256 + offset));
    }

    //Since only mod 256 is used I figured bit shifting would be better than division.
    public static ulong ShiftMod256(ulong num)
    {
        return (num << 56) >> 56;
    }

}


texts: Insert data from one table to another (million+ rows)

texts:
I have a procedure that collects data from one table and inserts it into another table with the same columns, but after correlating with a few other tables. Since the existing table (from which data will be transferred) has more than a million records, the insertion into the other table (into which the data is being transferred) takes a very long time.

use acrs;
drop procedure getUA;
delimiter ;;
CREATE PROCEDURE getUA()
begin
DECLARE min INTEGER;
DECLARE max INTEGER;

select min(id) into min from user_info;
set max = min + 10000;
-- select max(id) into max from user_info;
CREATE TABLE IF NOT EXISTS user_activity_load(INDEX netactivity (id)) select * from user_info where id between min and max;
insert into user_activity_load (id, src_ip, dst_ip, src_port, dst_port, data, Created, smalicious, sgeoLoc, seq, app_id, tcp_flag, dgeoLoc, dmalicious)
  select id, src_ip, dst_ip, src_port, dst_port, data, Created, smalicious, sgeoLoc, seq, app_id, tcp_flag, dgeoLoc, dmalicious from user_info where id between min and max;
DELETE from user_info where id >= min and id <=max;
  WHILE (min <= max) DO
    BEGIN
      DECLARE src INTEGER unsigned;
      DECLARE dst INTEGER unsigned;
      DECLARE s_id INTEGER;
      DECLARE d_id INTEGER;
      select src_ip, dst_ip into src, dst from user_activity_load where id = min;
      select id into s_id from ip2location_db3 where (src between ip_from and ip_to);
      select id into d_id from ip2location_db3 where (dst between ip_from and ip_to);
      update user_activity_load set sgeoLoc = s_id, dgeoLoc = d_id where id = min;
    END;
    set min = min + 1;
  END WHILE;
end ;;
delimiter ;

Here user_info is the table where the data currently is(before transfer), and user_activity_table is where the data has to go. But a few columns in user_info is empty, i.e sgeoLoc and dgeoLoc. These data are fetched from a separate table called ip2location before being inserted into user_activity_load. Currently the max variable is set to 10000 because if set to max(id), then it will process a million logs and takes a lot of time. 10000 rows are currently taking 20 minutes to process.

Can somebody help me improve this so that it gets faster and processing a million logs does not take a day or so?


texts: A simple multithreaded FileLogger in C++

texts:
In order to learn about multithreading programming in C++, I am implementing a basic multithreaded logger.

I use a std::deque to store messages inside a FileLogger class. Each time a thread logs a message, that message is pushed to the back of the deque.

In a separate thread the FileLogger checks if there are any messages in the deque and if so writes them to file.

Access to the deque is guarded by a mutex.

In order to make it easy to log from anywhere, the logger is implemented as a singleton.

Is my code correct? How can it be improved?

// FileLogger.h:
class FileLogger
{
public:
    static void initialize(const char* filePath) { // called by main thread before any threads are spawned
        instance_ = new FileLogger(filePath);
    }
    static FileLogger* instance() { // called from many threads simultaneously
        return instance_;
    }
    void log(const std::string &msg);
private:
    FileLogger(const char* filePath);
    void writeToFile();
    static FileLogger*     instance_;
    std::deque<std::string> messages;
    std::mutex         messagesMutex; // lock/unlock this each time messages is pushed or popped
    std::ofstream               fout;
    std::thread         writerThread;
};
// FileLogger.cpp:
FileLogger* FileLogger::instance_ = nullptr;

void FileLogger::writeToFile() {
    for (;;) {
        std::string message;
        while (messages.empty()) {
            std::this_thread::sleep_for(std::chrono::nanoseconds(10));
        }
        messagesMutex.lock();
        message = messages.front();
        messages.pop_front();
        messagesMutex.unlock();
        fout << message << std::endl << std::flush;
    }
}

FileLogger::FileLogger(const char* filePath) {
    fout.open(filePath);
    std::thread t(&FileLogger::writeToFile, this);
    writerThread = std::move(t);
}

void FileLogger::log(const std::string &msg) {
    std::lock_guard<std::mutex> lg(messagesMutex);
    messages.push_back(msg);
}


texts: Password validator using regular expressions

texts:
I wanted to learn some regex, but learning regex is super boring so I decided to write a password validator that will validate a password with regular expressions.

How it works:

You enter a password that has no echo thanks to the getpass library
The password is run against five different validations such as, upper case, lower case, special character, and digits. These are done by using the re (regex) library.
If the password does not pass one of the validations it will output a random password using the random library that will match the given guidelines.

What I would like to know is:

Are there better ways to create a random string?
Are there better regular expressions I could use to verify that password?

Source:

import getpass
import re
import random

def random_password(length=3):

    """ Create a a random password to display if the
       primary validation fails. """

    valid_upcase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    valid_lowcase = 'abcdefghijklmnopqrstuvwxyz'
    valid_specs = '!$@&'
    valid_digits = '1234567890'

    return ''.join((random.choice(valid_upcase) + random.choice(valid_lowcase) + random.choice(valid_specs) + random.choice(valid_digits) for i in xrange(length)))

def length_error(password):
    """ Validate that the password is over 8 characters
       and no more than 20 characters. """

    if 8 > len(password) :
        return False
    else:
        return True

def lower_error(password):

    """ Confirm that the password contains at least one
       lower case letter """

    if re.search(r"[a-z{1,9}]", password) is None:
        return False
    else:
        return True

def symbol_error(password):

    """ Make sure that the password contains at least one
       of the following special characters: ! @ $ & """

    if re.search(r"[!@$&{1,5}]", password) is None:
        return False
    else:
        return True

def upcase_error(password):

    """ Confirm that the password contains at least one
       upper case character. """

    if re.search(r"[A-Z{1,5}]", password) is None:
        return False
    else:
        return True

def digit_error(password):

    """ Confirm that the password contains at least one
       digit. """

    if re.search(r"\d{1,5}", password) is None:
        return False
    else:
        return True

def prompt(info):

    """ Get the password without echo. """

    return getpass.getpass(info)
    #return raw_input(info)  # # Uncomment this for echo

def validate_password(password):

    """ Where the validation occurs, if the password does
       not pass one of the following tests, it will output
       a random string that does pass the test. """

    if lower_error(password) is False or upcase_error(password) is False:
        print "Password did not match the uppercase or lowercase requirements."
        print "Random password to use: {}".format(random_password())
    elif digit_error(password) is False or symbol_error(password) is False:
        print "Password did not match the integer and special character requirements."
        print "Random password to use: {}".format(random_password())
    elif length_error(password) is False:
        print "Password did not meet the length requirements."
        print "Password must be over 8 characters and no more then 20 characters"
        print "Random password to use: {}".format(random_password())
    else:
        print "Password validated successfully."

def obtain_password():

    """ Main method. """

    password_to_verify = prompt("Enter password: ")
    validate_password(password_to_verify)

if __name__ == '__main__':
    obtain_password()


texts:
I know my solution works, but I would like to know if the function can be simplified so that the loop is not called twice. Is there a way to pass the properties to be run through as parameters in the function?
My given data structure looks like (in which totalQuantities and typeQuantities might contain an uncertain amount of objects):
data = [
  {
    "totalQuantities": [
      {
        "ammount": 23
      }
    ],
    "typeQuantities": [
      {
        "type": {
          "id": 0,
          "name": "Test"
        },
        "singleQuantities": [
          {
            "ammount": 45
          }
        ]
      }
    ]
  }
]

My desired output is:
values = [
    {type: 'Total', ammount1: 23},
    {type: 'Test1', ammount1: 45}
]

And my solution - of which I'm wondering if there is another way to do so - is:
transformData(data) {
  const values = [];
  // loop through totalQuantities
  const objTotal: any = {};
  objTotal.type = 'Total';
  for (let i = 0; i < data.totalQuantities.length; i++) {
    const name = 'ammount' + (i + 1).toString();
    objTotal[name] = data.totalQuantities[i].ammount;
  }
  values.push(objTotal);
  // loop through each element of singleQuantities
  data.typeQuantities.map(element => {
    const obj: any = {};
    obj.type = element.type.name;
    for (let i = 0; i < element.singleQuantities.length; i++) {
      const name = 'ammount' + (i + 1).toString();
      obj[name] = element.singleQuantities[i].ammount;
    }
    values.push(obj);
  });
  return values;
}


texts:
I have col D (in sheet1 called Students), col A (in Sheet2 called Students too) and col B (in sheet2 called Age).  In colD there are a lot of similar Students names that are sorted from A to Z.

I have this code that finds matched students name and put his age in col D (sheet1):

Set rngSearch = Sheets("Sheet1").Range("D:D")
For Each rngSearch In Drng
    Set rngFound = Arng.Find(What:=rngSearch, LookIn:=xlValues, LookAt:=xlWhole,   SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    'if studentname is found:
     If Not rngFound Is Nothing Then
         rngSearch.Offset(0, 0) = rngFound.Offset(0, 1)
     Else
         rngSearch.Offset(0, 0) = "-"
     End If
Next

I want to loop col D once and if exists a match students name in colA (sheet2), I want to copy in colD (sheet1) the B column (sheet2) that matches to colA (sheet2).

I sorted colD because I don't want to loop every similar students name.  I mean: if a student name (for example: Andrew) in colD matches to a student name ('Andrew') in colA, then copy colB (matches to col A- for example Andrew has 15 years old) in colD (sheet1).  And if the string 'Andrew' is repeated in colD (I found it in another cells), don't loop for it again colB (sheet2), but copy the value from the first string.

For example:

(Sheet1)

colD: Students:
row1: Andrew
row2: Andrew
row3: Andrew
row4: Andrew
row5: Andrew
row6: Andrew
row7: Ben
row8: Ben
row9: Edoardo
row10: Helen
row11: Leonardo
row12: Leonardo
row13: Robert
rowN: ..

(Sheet2)

      Students:  colB: Age:
row1: Michael          16
row2: Timoth           20
row3: Andrew           15
row4: Edoardo          19
row5: Ben              13
row6: Robert           24
row7: Helen            17

What I want:

(Sheet1)

colD: Students:
row1: 15
row2: 15
row3: 15
row4: 15
row5: 15
row6: 15
row7: 13
row8: 13
row9: 19
row10: 17
row11: 0
row12: 0
row13: 24
rowN: ..

Can anyone help me to optimize this code, please?


texts:
I am trying to implement an algorithm for detecting outliers in R and I am pretty new to the language. The outlier algorithm is described in this paper in detail on page 10-11, but to summarize it works like this:

  Algorithm – Outlier Detection using K-Nearest Neighbor Data Distributions

  Find the set S(K) of K nearest neighbors to the test data point O.
  Calculate the K distances between O and the members of S(K). These distances define fK(d,O).
  Calculate the K(K-1)/2 distances among the points within S(K). These distances define fK(d,K).
  Compute the cumulative distribution functions CK(d,O) and CK(d,K), respectively, for fK(d,O)
  and fK(d,K).
  Perform the K-S Test on CK(d,O) and CK(d,K). Estimate the p-value of the test.
  Calculate the Outlier Index = 1-p.
  If Outlier Index > 0.95, then mark O as an “Outlier”. The Null Hypothesis is rejected.
  If 0.90 < Outlier Index < 0.95, then mark O as a “Potential Outlier”.
  If p > 0.10, then the Null Hypothesis is accepted: the two distance distributions are drawn from
  the same population. Data point O is not marked as an outlier.

I implemented this in the following code but I am unsure if this code is appropriate for R in terms of structure and style (compared with Python). Also, I don't know if there is a more efficient way than the simple step by step way I have done. I added code comments to delineate each step of the algorithm.

outlier_test <- function(data , neighbors){
  #Find the set S(K) of K nearest neighbors to the test data point O.
  sk <- nn2(data, k=neighbors)$nn.idx #matrix of indices of neighbors

  #Calculate the K distances between O and the members of S(K). These distances define fK(d,O).
  outlier_distances <- nn2(data, k=neighbors)$nn.dist #distances between value and its neighbors

  #Calculate the K(K-1)/2 distances among the points within S(K). These distances define fK(d,K).
  plyr::adply(sk, .margins = 1, function(row) { data[row, ] })

  df1<- plyr::alply(sk, .margins=1, function(row){data[row,]})
  df1 = plyr::ldply(df1, rbind)
  neighbor_distances <- plyr::alply(df1, 1, function(row) c(dist(unlist(row))))

  neighbor_distances <- df1 %>%
    dplyr::group_by(X1) %>%
    do({
      row_data <- .
      my_dist <- dist(row_data[ ,c(colnames(data)[1],colnames(data)[2])])
      as.data.frame(t(as.vector(my_dist)))
      }) %>%
    dplyr::ungroup() %>%
    dplyr::select(-X1) %>%
    as.matrix()

  #daply(test, .(X1), function(x) as.vector(dist(x))) This  is an alternative way to do the above piping

  #Compute the cumulative distribution functions CK(d,O) and CK(d,K), respectively, for fK(d,O)
  #and fK(d,K).
  # Since ks.test function in R can take vectors of values, we don't need to explicitly find the ECDF of our samples

  #Perform the K-S Test on CK(d,O) and CK(d,K). Estimate the p-value of the test.
  all_pvalues = sapply(1:nrow(outlier_distances), function(i) ks.test(as.vector(outlier_distances[i,]), as.vector(neighbor_distances[i,]))$p.value)

  #Calculate the Outlier Index = 1-p.
  outlier_index = 1-all_pvalues
  #If Outlier Index > 0.95, then mark O as an Outlier. The Null Hypothesis is rejected.
  #If 0.90 < Outlier Index < 0.95, then mark O as a Potential Outlier.
  #If p > 0.10, then the Null Hypothesis is accepted: the two distance distributions are drawn from
  #the same population. Data point O is not marked as an outlier
  return(outlier_index)
}


texts: Course Statistics Using Arrays and Files

texts:
I wanted to get some feedback here on my solution, because the course I am using is not allowing me to submit any answers.

  Objectives:

  Write a Java program that:

  Create a grades.txt file with some grades inputted in
  Reads these grades from the file and stores it into an ArrayList
  After storing all elements into the ArrayList return the max, min, and average grade of the list
  Return an Arraylist without any duplicate grades. All duplicated grades must be removed

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
       File file = new File("grades.txt");

      PrintWriter output = new PrintWriter(file);
      output.println("12.5");
      output.println("19.75");
      output.println("11.25");
      output.println("10");
      output.println("15");
      output.println("13.25");
      output.println("14");
      output.println("9");
      output.println("10");
      output.println("19.75");
      output.close();

      // Reading from the file //

        ArrayList<Double> gradesString = new ArrayList<Double>();

        Scanner input = new Scanner(file);

        while(input.hasNext())
        {
            String line = input.nextLine();
            gradesString.add(Double.parseDouble(line));
        }

        double result = 0;

        for(Double i : gradesString)
        {
            result += i;
        }

        double mean = result / gradesString.size();

        LinkedHashSet<Double> hashSet = new LinkedHashSet<>(gradesString);

        ArrayList<Double> gradesWithoutDuplicates = new ArrayList<Double>(hashSet);

        System.out.println("The grades are: " + gradesString);
        System.out.println("The highest grade is: " + Collections.max(gradesString));
        System.out.println("The lowest grade is: " + Collections.min(gradesString));
        System.out.println("The average is: " + mean);
        System.out.println("The grades list without duplicates is: " + gradesWithoutDuplicates);

    }

}

Output:

The grades are: [12.5, 19.75, 11.25, 10.0, 15.0, 13.25, 14.0, 9.0, 10.0, 19.75]
The highest grade is: 19.75
The lowest grade is: 9.0
The average is: 13.45
The grades list without duplicates is: [12.5, 19.75, 11.25, 10.0, 15.0, 13.25, 14.0, 9.0]


texts:
I have an up-down numeric control on my form (attEdgeUpDown), which has value 0-8. I have eight checkboxes (attEdgeBox1 to attEdgeBox8). I want to display as many boxes as the numeric control shows.

Can this be done more efficiently?

private void attEdgeUpDown_ValueChanged(object sender, EventArgs e)
{
int edgeValue = Convert.ToInt32(attEdgeUpDown.Value);
    for (int i = 1; i <= 8; i++)
    {
    string checkboxName = "attEdgeBox" + Convert.ToString(i);
    Control[] checkboxControl = this.Controls.Find(checkboxName, true);
        if (i <= edgeValue)
            {
                (checkboxControl[0] as CheckBox).Visible = true;
            }
            else
            {
                (checkboxControl[0] as CheckBox).Visible = false;
                (checkboxControl[0] as CheckBox).Checked = false;
            }
    }
}


texts: Shifting the origin of a large list of coordinates

texts:
Problem and goal

Given a large edge list file (such as 2 million nodes, 30 million edges) containing source and destination coordinates, I want to 'convert' them in such a way so that the coordinates start at 0. Hopefully the example will clear this up. My goal is to increase the performance (speed) of my current code.

Example

input_file.txt

54    414
12    54
414   87
20    54
12    3

Output

0    1
2    0
1    3
4    0
2    5

Notice how 54 becomes 0, 414 becomes 1, 12 becomes 2, 87 becomes 3 etc. In other words: if a vertex is re-used like 54, the same coordinate will be used.

My solution

While my solution works fine, it is slow for a large edge list such as: this edge list (or this small one for testing purposes), and I was wondering whether there are areas to improve my code.

Essentially, I am using a dictionary (map) that keeps track of each vertex and its coordinate respectfully. If a vertex is not present in the dictionary, it uses the current coordinate and increments it for any possible new vertex.

Code

#pragma warning (disable : 4996)
#include <iostream>
#include <unordered_map>
#include <iostream>
#include <string>
#include "conio.h"
#include <sstream>
#include <fstream>

using namespace std;

void load_graph_from_edge_list_file_to_coo(vector<int>& , vector<int>&, string);
int add_vertex_as_coordinate(vector<int>&, unordered_map<int, int>&, int, int);

int main() {
    vector<int> source_vertices;
    vector<int> destination_vertices;
    string input_file_path = "C:\\Users\\User\\Desktop\\input_file.txt";
    load_graph_from_edge_list_file_to_coo(source_vertices, destination_vertices, input_file_path);

    return 0;
}

void load_graph_from_edge_list_file_to_coo(vector<int>& source_vertices, vector<int>& destination_vertices, string file_path) {
    unordered_map<int, int> map_from_edge_to_coordinate;
    int current_coordinate = 0;

    std::ifstream infile(file_path);
    string line;
    while (getline(infile, line))
    {
        std::istringstream iss(line);
        int vertex_source, vertex_destination;
        if (!(iss >> vertex_source >> vertex_destination)) {
            continue;
        } else {
            current_coordinate = add_vertex_as_coordinate(source_vertices, map_from_edge_to_coordinate, vertex_source, current_coordinate);
            current_coordinate = add_vertex_as_coordinate(destination_vertices, map_from_edge_to_coordinate, vertex_destination, current_coordinate);
        }
    }
    infile.close();

    printf("\nTotal amount of vertices: %d", map_from_edge_to_coordinate.size());
    printf("\nTotal amount of edges: %d", source_vertices.size());
}

int add_vertex_as_coordinate(vector<int>& vertices_type, unordered_map<int, int>& map_from_edge_to_coordinate, int vertex, int coordinate) {
    if (map_from_edge_to_coordinate.count(vertex)) {
        vertices_type.push_back(map_from_edge_to_coordinate.at(vertex));

        return coordinate;
    }
    else {
        map_from_edge_to_coordinate[vertex] = coordinate;
        vertices_type.push_back(coordinate);
        coordinate++;

        return coordinate;
    }
}

Any tips to increase the performance would be greatly appreciated.


texts:
I am constructing a form builder in ASP.NET and I am wanting help with the structure of my POCO classes I have the following so far. I just don't see why I need to add to a list and then process it further again would their be a neater way to return a list of .net controls just to add to Panel using LoadControl(controlname).

POCO Class

public class FormClass
    {

        public string name { get; set; }
        public string displayName { get; set; }
        // This may actually work with Type, but I'm not so sure
        public string type { get; set; }
        public int length { get; set; }
        public bool key { get; set; }
        public bool required { get; set; }
        public string ControlType { get; set; }

    }

And then I'm using the following code to populate a list for further processing:

    PortalContext DBConext = new PortalContext();
    List<FormClass> formData = new List<FormClass>();
    List<FormStructure> frmstructList = new List<FormStructure>();
    public List<FormStructure> GetFormDataFromService()
    {   // XNamespace xNamespace = "http://CompanyName.AppName.Service.Contracts";

        Guid newGuid = Guid.NewGuid();
        XElement xdoc = XElement.Load(@"c:\form.xml");
        XNamespace ns = xdoc.GetDefaultNamespace();
        var classDetails = from classDetail in xdoc.Descendants(ns + "ClassDetails")
                           select new FormClass
                           {
                               name = (string)classDetail.Element(ns + "name"),
                               type = (string)classDetail.Element(ns + "type"),
                               displayName = (string)classDetail.Element(ns + "displayName"),
                               length = (int)classDetail.Element(ns + "length"),
                               key = (bool)classDetail.Element(ns + "key"),
                               required = (bool)classDetail.Element(ns + "required"),
                           };

        foreach (FormClass classDetail in classDetails)
        {
              FormStructure frmstruct = new FormStructure();
              frmstruct.FormStructureId = newGuid;
              frmstruct.name = classDetail.name;
              frmstruct.displayname = classDetail.displayName;
              frmstruct.FormKey = classDetail.key;
              frmstruct.Required = classDetail.required;
              frmstruct.ClassName = "Incident";

              if (classDetail.type == "System.String")
                  frmstruct.ControlType = "TextBox";

              if (classDetail.type == "System.Enum") //to be dropdown
                  frmstruct.ControlType = "DropDown";

              if (classDetail.type == "System.DateTime")//date picker
                  frmstruct.ControlType = "DateTimePicker";

              if (classDetail.type == "System.Boolean")
                  frmstruct.ControlType = "RadioButton";

              frmstructList.Add(frmstruct);

        }
        return frmstructList;

    }

XML Data

  <name>DisplayName</name>

  <displayName>Display Name</displayName>

  <parent>Object</parent>

  <type>System.String</type>

  <length>4000</length>

  <key>false</key>

  <required>false</required>

</ArrayOfClassDetails>


texts: Building a 2D array "month view" for a given date

texts:
I am working on a calendar widget for a project. For any given date, I need to build a 2D array representing the "Month" that the date falls in, where the rows represent weeks in the month and the cells in those rows are individual days. The weeks must start on Monday, and if the month does not start on a Monday, it must "backfill" in the days from the previous month. Similarly, if the last week in the month isn't "full" because it didn't end on a Sunday, fill the last week with the beginning of the following month.

For example, let's use today, May 26th of 2016. Given an input of new Date(2016, 04, 26), I would want an output of an array with 6 elements (the weeks), each element itself an array containing 7 date objects (the days). Because May started on a Sunday, the first date in the first week would actually be April 25th (the previous Monday). Because May ends on a Tuesday, the last date in the last week would be June 5th (the following Sunday).

function getCalendarMatrix(date){
    var calendarMatrix = []

    var startDay = new Date(date.getFullYear(), date.getMonth(), 1)
    var lastDay = new Date(date.getFullYear(), date.getMonth()+1, 0)

    // Modify the result of getDay so that we treat Monday = 0 instead of Sunday = 0
    var startDow = (startDay.getDay() + 6) % 7;
    var endDow = (lastDay.getDay() + 6) % 7;

    // If the month didn't start on a Monday, start from the last Monday of the previous month
    startDay.setDate(startDay.getDate() - startDow);

    // If the month didn't end on a Sunday, end on the following Sunday in the next month
    lastDay.setDate(lastDay.getDate() + (6-endDow));

    var week = []
    while(startDay <= lastDay){
        week.push(new Date(startDay));
        if (week.length === 7){
          calendarMatrix.push(week);
          week = []
        }
        startDay.setDate(startDay.getDate() + 1)
    }

    return calendarMatrix;
}

My code seems like it's working well, but I'm seeking input on any flaws this might have or how I could go about this in a "better" way.


texts:
I'm writing a logger extension that allows multiple threads to log a process, and then dump that log to the main log in one atomic operation. The point of it is to make the logs easier to read when many threads are executing. Is this test valid and clear or not?

/**
 * Test that thread logs do not interlace
 * @throws InterruptedException
 */
@Test
public void testDumpMultithreaded() throws InterruptedException {
    Thread t1 = new Thread() {
        @Override
        public void run() {
            logger.bufferedMessages.get().add("t1: One");
            logger.bufferedMessages.get().add("t1: Two");
            logger.bufferedMessages.get().add("t1: Three");
            logger.dump();
        }
    };

    Thread t2 = new Thread() {
        @Override
        public void run() {
            logger.bufferedMessages.get().add("t2: One");
            logger.bufferedMessages.get().add("t2: Two");
            logger.bufferedMessages.get().add("t2: Three");
            logger.dump();
        }
    };

    t1.start();
    t2.start();

    t1.join();
    t2.join();

    Iterator<String> i = logger.messages.iterator();
    boolean t1Correct = false;
    boolean t2Correct = false;

    while (i.hasNext()) {
        if (i.next().equals("t1: One")) {
            t1Correct = true;
            t2Correct &= i.next().equals("t1: Two");
            t2Correct &= i.next().equals("t1: Three");
        }
    }

    i = logger.messages.iterator();
    while (i.hasNext()) {
        if (i.next().equals("t2: One")) {
            t2Correct = true;
            t2Correct &= i.next().equals("t2: Two");
            t2Correct &= i.next().equals("t2: Three");
        }
    }

    assertEquals("Thread one's log not consecutive: ", true, t1Correct);
    assertEquals("Thread two's log not consecutive: ", true, t2Correct);
}


texts: Simplifying parsing command-line arguments

texts:
I'm currently working on a command-line "database migration" utility. Here are some of the requirements regarding reading the command-line arguments part:

It should accept regular database connection parameters: host, database name, port, user and password (do not allow to specify password as a command-line argument, ask for password separately)
It works in two modes - "validate" and "apply" - one of them has to be specified explicitly, both cannot be specified
There should be an integer "batch size" argument specified
There can be an optional range of integer ids specified via --id-begin and --id-end (the beginning of the range defaults to 1 if not specified)
Instead of a range, there can also be a --name argument specified
Both name and id range cannot be present
There should be a "verbose" flag

My Solution

Here is what I came up with:

I'm using argparse module and extracted the parsing command-line arguments into a separate .parse_args() function which returns the parsed arguments (which is argparse.Namespace instance):

import argparse
import getpass

def parse_arguments(args):
    """Parses command-line arguments."""
    parser = argparse.ArgumentParser()

    # general database connection settings
    parser.add_argument('-H', '--host', help="Database host IP address", required=True)
    parser.add_argument('-D', '--database', help="Database name", required=True)
    parser.add_argument('-P', '--port', help="Database port", required=True, type=int)

    parser.add_argument('-u', '--user', help='Database user name', required=True)

    # modes
    parser.add_argument('--validate', help='Enables "validate only" mode', action='store_true')
    parser.add_argument('--apply', help='Enables "apply" mode', action='store_true')

    # extra settings
    parser.add_argument('-b', '--batch-size', help='Batch size - determines how many cases are processed at a time',
                        type=int, default=5000)
    parser.add_argument('--id-begin', help='ID for the processing to begin with', type=int, default=None)
    parser.add_argument('--id-end', help='ID for the processing to end with', type=int, default=None)
    parser.add_argument('--name', help='NAME value', type=str, default=None)

    parser.add_argument('--verbose', help='Enables "verbose" reporting mode', action='store_true', default=False)

    args = parser.parse_args(args)

    # if both specified, id end cannot be smaller than id begin
    if args.id_begin and args.id_end and args.id_end < args.id_begin:
        raise parser.error("Invalid ID range.")

    # at least one mode should be specified
    if not args.validate and not args.apply:
        raise parser.error('Please specify one of the "validate" or "apply" modes.')

    # don't allow to use both "apply" and "validate" mode
    if args.apply and args.validate:
        raise parser.error('Cannot use both "validate" and "apply" modes.')

    # don't allow to use both NAME and ID
    if args.name and (args.id_begin or args.id_end):
        raise parser.error("Cannot use both NAME and ID.")

    # set the default id_begin value
    if not args.name and not args.id_begin:
        args.id_begin = 1

    args.password = getpass.getpass(prompt='Enter password: ')

    return args

The Questions

The code works and I've even covered it with tests (put them into a gist). But, I don't particularly like the way I check the requirements for name and id ranges, mutually exclusive "apply" and "validate" modes. It feels like I could have used more argparse specific features like custom types or actions. What do you think?

I would also appreciate other ideas about handling the password - using getpass() helps to not have the password shown on the terminal explicitly, but requires mocking it in tests.


texts:
I'm trying to reflect a ray of a triangle using Processing and not sure I got the code right. I'm using two bits of pseudo code from two locations:

Intersection of a Line with a Plane from Paul Bourke's site.
Reflecting a vector from 3DKingdoms site.

Here's my code so far:

PVector[] face = new PVector[3];
float ai = TWO_PI/3;//angle increment
float r  = 300;//overall radius
float ro = 150;//random offset

PVector n;//normal
Ray r1;

void setup(){
  size(500,500,P3D);
  for(int i = 0 ; i < 3; i++) face[i] = new PVector(cos(ai * i) * r + random(ro),random(-50,50),sin(ai * i) * r + random(ro));
  r1 = new Ray(new PVector(-100,-200,-300),new PVector(100,200,300));
}
void draw(){
  background(255);
  lights();
  translate(width/2, height/2,-500);
  rotateX(map(mouseY,0,height,-PI,PI));
  rotateY(map(mouseX,0,width,-PI,PI));
  //draw plane
  beginShape(TRIANGLES);
  for(PVector p : face) vertex(p.x,p.y,p.z);
  endShape();
  //normals
  PVector c = new PVector();//centroid
  for(PVector p : face) c.add(p);
  c.div(3.0);
  PVector cb = PVector.sub(face[2],face[1]);
  PVector ab = PVector.sub(face[0],face[1]);
  n = cb.cross(ab);//compute normal
  line(c.x,c.y,c.z,n.x,n.y,n.z);//draw normal

  pushStyle();
  //http://paulbourke.net/geometry/planeline/
  //line to plane intersection u = N dot ( P3 - P1 ) / N dot (P2 - P1), P = P1 + u (P2-P1), where P1,P2 are on the line and P3 is a point on the plane
  PVector P2SubP1 = PVector.sub(r1.end,r1.start);
  PVector P3SubP1 = PVector.sub(face[0],r1.start);
  float u = n.dot(P3SubP1) / n.dot(P2SubP1);
  PVector P = PVector.add(r1.start,PVector.mult(P2SubP1,u));
  strokeWeight(5);
  point(P.x,P.y,P.z);//point of ray-plane intersection

  //vector reflecting http://www.3dkingdoms.com/weekly/weekly.php?a=2
  //R = 2*(V dot N)*N - V
  //Vnew = -2*(V dot N)*N + V
  //PVector V = PVector.sub(r1.start,r1.end);
  PVector V = PVector.sub(r1.start,P);
  PVector R = PVector.sub(PVector.mult(n,3 * (V.dot(n))),V);
  strokeWeight(1);
  stroke(0,192,0);
  line(P.x,P.y,P.z,R.x,R.y,R.z);
  stroke(192,0,0);
  line(r1.start.x,r1.start.y,r1.start.z,P.x,P.y,P.z);
  stroke(0,0,192);
  line(P.x,P.y,P.z,r1.end.x,r1.end.y,r1.end.z);
  popStyle();

}
void keyPressed(){   setup();  }//reset
class Ray{
  PVector start = new PVector(),end = new PVector();
  Ray(PVector s,PVector e){   start = s ; end = e;  }
}

I've tried to keep it simple and comment it. Still, the reflected vector (drawn in green) doesn't seem to be quite right:

I'm not sure if the reflected vector should meet the normal.  Should I normalize/scale some vectors at some point?


texts: Extension content script multiple mouse events handling

texts:
I'm writing a translate extension for firefox just similar like google translate extension.
I achieve that the listening mauseup and mousedown events to get selection text and popup a div that you can click.

Due to i am new at javacript, i would like some feedback for above code.
 Can i do any improvement?

var bubbleDOM = document.createElement('div');
bubbleDOM.setAttribute('class', 'selection_bubble');
document.body.appendChild(bubbleDOM);
var selection = "";
function getSelectionText() {
  var text = "";
  if (window.getSelection) {
    text = window.getSelection().toString();
  } else if (document.selection && document.selection.type != "Control"){
    text = document.selection.createRange().text;
  }
  return text;
}
document.addEventListener("mousedown", checkSelectionText);
document.addEventListener("mouseup", checkSelectionText);

function checkSelectionText(event) {
  if (event.target == document.querySelector('.selection_bubble')) {
    return;
  }

  setTimeout(function() {
    selection = getSelectionText();
    if (selection) {
      document.querySelector('.selection_bubble').style.top = (event.pageY+3) + "px";
      document.querySelector('.selection_bubble').style.left = (event.pageX+3) + "px";
      document.querySelector('.selection_bubble').style.visibility = 'visible';
    } else {
      document.querySelector('.selection_bubble').style.visibility = 'hidden';
    }
  });
}

document.querySelector('.selection_bubble').addEventListener("click", openPopup);

function openPopup(event) {
  chrome.runtime.sendMessage({action: 'openModal', selection: selection});
  document.querySelector('.selection_bubble').style.visibility = 'hidden';

  event.preventDefault();
  event.stopPropagation();
}


texts: Analyzing pair by pair DNA sequences with for loops in Python

texts:
Here is a code I would need to make far faster: it is made to analyse more than 3000 DNA sequences of more than 100 000 characters each.

The matter is I have to compare it by pair, so that would be around 3000*2999/2 = 4498500 calculation and each takes 1 to 2 seconds...

Could you think of another way of doing it ? I've seen that there are algorithms that are used to go faster (like Boyer-Moore), but could we apply it here ? Or replace the for loops with something faster ? Any idea would be more than welcome.

import numpy as np
import time
import random

def calculate_distance(genom1, genom2):
    # in : two strings (sequences)
    # out : score between those 2 strings

    # example : 'AATTGCAT' compared to 'AAAACGTC'
    # => 'AA' and 'AA' = 2
    # => 'TT' and 'AA' = 0
    # => 'GC' and 'CG' = 2
    # => 'AT' and 'TC' = 1

    score = 0
    for i in range(0, len(genom1), 2):
        if (genom1[i] in genom2[i:i+2]) or (genom1[i+1] in genom2[i:i+2]):
            if sorted(genom1[i:i+2]) == sorted(genom2[i:i+2]):
                score += 2
            else :
                score += 1
    return score

def build_matrix(sequences, N):
    # in : list of lists
    # out : matrix of scores between each pair of sequences
    matrix = np.zeros((N, N))
    for i in range(N):
        for j in range(i, N):
            matrix[i][j] = calculate_distance(sequences[i], sequences[j])
    return matrix

def test(len_seq, N):
    sequences = []
    for i in range(N):
        sequences.append(''.join(random.choice(['0','A', 'T', 'C', 'G']) for x in range(len_seq)))

    start = time.clock()
    matrix = build_matrix(sequences, N)
    elapsed = time.clock() - start
    print('Run time for ' + str(N) + ' sequences of ' + str(len_seq) + ' characters : computed in ' + str(elapsed) + ' seconds')
    return matrix

test(10**6, 2)

Returns :

Run time for 2 sequences of 1000000 characters : computed in 2.742817 seconds


texts: Evaluating an expression with integers, +, and *, as well as -, /

texts:
There is a job interview question, and the source of the question is here.

The solution is pretty simple. We just need to split the input string by + and then by *. Then we compute products in a nested loop and sum up all the products.

public class Main {

    // http://www.careercup.com/question?id=4911380140392448
    public static void main(String[] args) {
        String equation = "1*5*4+8*9+16";
        int result = compute(equation);
        System.out.println(result);
    }

    static int compute(String equation) {
        int result = 0;
        String []byPluses = equation.split("\\+");
        for (String multipl : byPluses) {
            String []byMultipl = multipl.split("\\*");
            int multiplResult = 1;
            for (String operand : byMultipl) {
                multiplResult *= Integer.parseInt(operand);
            }
            result += multiplResult;
        }
        return result;
    }

}

But what if I was asked the following question after solving the initial problem:

  OK. What would you do if you needed to support not only + and * but also - and /?

The quick solution I thought up is this:

package careepcup.fb;

public class Main {

    // http://www.careercup.com/question?id=4911380140392448
    public static void main(String[] args) {
        // An equation with +, -, /, *
        String anotherEquation = "1*5*4+8*9+16/8-9"; // 85
        double another = computeAnother(anotherEquation);
        System.out.println(another);
    }

    static double computeAnother(String equation) {
        double result = 0.0;
        String noMinus = equation.replace("-", "+-");
        String[] byPluses = noMinus.split("\\+");

        for (String multipl : byPluses) {
            String[] byMultipl = multipl.split("\\*");
            double multiplResult = 1.0;
            for (String operand : byMultipl) {
                if (operand.contains("/")) {
                    String[] division = operand.split("\\/");
                    double divident = Double.parseDouble(division[0]);
                    for (int i = 1; i < division.length; i++) {
                        divident /= Double.parseDouble(division[i]);
                    }
                    multiplResult *= divident;
                } else {
                    multiplResult *= Double.parseDouble(operand);
                }
            }
            result += multiplResult;
        }

        return result;
    }
}

What do think of my solution with -, +, /, *?


texts: What's the best name for a service object in Rails?

texts:
One challenge I regularly face is how to name my service objects in Rails.

I like the advice in this blog post on Gourmet Service Objects, a service object performs one action. It holds the business logic to perform that action. For example:

# app/services/accept_invite.rb
class AcceptInvite

  def self.call(invite, user)
    invite.accept!(user)
    UserMailer.invite_accepted(invite).deliver
  end

end

However, I frequently find that my class names are rarely this short.

For example, I have areas that have many runners that have many goals.  I want to calculate, for a given area, the percentage of runners with a goal set.

Adhering to the above idea, that a service object performs one action, I've come up with the following:

# app/services/get_percentage_of_runners_with_goals.rb
class GetPercentageOfRunnersWithGoals

  def self.call(area)
    membership_count = area.runners.members.to_float
    goals_set_count = area.runners.with_goals.to_float
    percentage = goals_set_count / membership_count
    (percentage * 100).to_s(:percentage, precision: 0)
  end

end

What then tends to happen is I have hundreds of these service objects, named along the lines of VerbObjectForObject, i.e. GetMembershipStatusOfRunner, GetCurrentGoalOfRunner, SetGoalOfRunner, and so on.

Is there a better naming convention?


texts:
I have an interview coming up in the next few weeks, and I'm choosing to be interviewed in Python. I began programming in Python (it was about four years ago), so the syntax is natural to me, but I only learned about some useful tools of Python just this past year, when I took a class that recommended using it.

The Problem

We'll say there are \$N\$ houses, where \$N\$ is some integer. Each house can be painted in either Red, Green or Blue. The cost of coloring each house in each of the colors is different.

Figure out how to color each house so no two adjacent houses have the same color and the total cost of coloring all the houses is as low as possible.

My Solution

# Assume the prices of painting each house in a specific color is
# given by an array of size 3xN, giving the price to paint each house
# for each specific color
def find_cheap_house_painters(N, prices):
    # initialize the totals array
    totals = [[0 for i in range(N)] for j in range(3)]

    # the first houses are special case
    # we just paint them, that's it.
    for color in range(3):
        totals[color][0] = prices[color][0]

    # after that, dynamic programming does its magic.
    # at each step, add the price of painting this house
    # to the minimum of the total painting of the other two colors
    colors = set(range(3))
    for house in range(1, N):
        for color in colors:
            previous_totals = [totals[other_color][house-1] for other_color in (colors - {color})]
            totals[color][house] = prices[color][house] + min(previous_totals)

    # so now we have the totals to paint up to each house
    # we'll just work backwards to find the coloring of the houses.
    colorings = [3 for i in range(N)]
    colorings[-1] = min(colors, key=lambda x : totals[x][-1])
    for house in range(N-2, -1, -1):
        # take the index of the smaller option
        other_colors = colors - {colorings[house+1]}
        colorings[house] = min(other_colors, key=lambda x : totals[x][house])

    # convert to a human-readable string
    color_names = "RGB"
    house_colorings = "".join([color_names[colorings[house]] for house in range(N)])
    return house_colorings

My Questions

Are there specifically Pythonic structures, idioms, or "things" that I should be aware of that would help in this specific problem? If you're so inclined, I'm also looking for a list of generally useful and Pythonic "things". I became aware of list comprehension, any(), all(), and lambdas only recently, so tools at about that level are helpful.
I have yet to take my university's Programming Studio class, in which I'd learn a lot about writing readable, sensible, and maintainable code. How is my variable naming, commenting, etc? Again, if you're so inclined, if you want to provide a list of common errors and bad habits, I'd appreciate it.
Anything that strikes you. Just because I didn't specifically ask for it here doesn't mean it'll won't help me write better code.


texts:
I wrote a program to find the longest common subsequence among several strings. I used a naive algorithm and implementation.

The motivation was solving the Rosalind problem at http://rosalind.info/problems/lcs/ . You can find sample input there as well. The Rosalind problem concerns strings as DNA, but I think my code can be treated as a general string operation.

The problem asks for any of the common substrings if there is more than one, but I find all of them.

How can this code be improved? What obvious problems are there?

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

namespace Finding_a_Shared_Motif
{
    class Program
    {
        private static void Main()
        {
            var input = File.ReadAllLines("rosalind_lcs.txt").ToList();

            var t = new Stopwatch();
            t.Restart();
            var lcs = LongestCommonSubstring(input);
            t.Stop();

            File.WriteAllLines("output.txt", lcs);
            Console.WriteLine("Finished in {0} msec.", t.ElapsedMilliseconds);
            Console.ReadLine();
        }

        public static IEnumerable<string> LongestCommonSubstring(List<string> strings)
        {
            var lcs = LongestCommonSubstring(strings[0], strings[1]);

            for (var i = 2; i < strings.Count(); i++)
            {
                var new_lcs = new BestStrings();

                foreach (var s in lcs) new_lcs.Add(LongestCommonSubstring(s, strings[i]));

                lcs = new_lcs;
            }

            return lcs;
        }

        private static BestStrings LongestCommonSubstring(string s1, string s2)
        {
            var lcs = new BestStrings();

            for (var i = 1 - s2.Length; i < s1.Length; i++)
            {
                var substrings = BestSubstringWithAlignment(s1, s2, i);

                if (substrings.Length == 0) continue;

                lcs.Add(substrings);
            }

            return lcs;
        }

        private static BestStrings BestSubstringWithAlignment(string s1, string s2, int offset)
        {
            var substrings = new BestStrings();

            var substring = "";
            for (var i = Math.Max(0, offset); i < s1.Length && i < s2.Length + offset; i++)
            {
                var c1 = s1[i];

                var c2 = s2[i - offset];

                if (c1 == c2)
                {
                    substring = substring + c1;
                }
                else
                {
                    substrings.Add(substring);
                    substring = "";
                }
            }
            substrings.Add(substring);

            return substrings;
        }

        sealed class BestStrings : Collection<string>
        {
            public int Length
            {
                get { return base[0].Length; }
            }

            public BestStrings()
            {
                base.Add("");
            }

            public new void Add(string s)
            {
                if (s.Length == 0 || s.Length < Length || Contains(s)) return;

                if (s.Length > Length) Clear();
                base.Add(s);
            }

            public void Add(IEnumerable<string> collection)
            {
                foreach (var s in collection) Add(s);
            }
        }
    }
}


texts:
This creates something like constexpr mar<String, T> using C++14.

Collision resolution is with static_assert and manually increase of hashtable buckets.

I do not store keys inside the hashtable, so I can get wrong results for a non-existent key. However, for my use case, this is OK.

#include <cstdint>

#include <initializer_list>

template<typename T, uint32_t SIZE, T ZERO = T(0)>
class StringTable{
public:
    using size_type = uint32_t;

    struct Pair{
        const char  *key;
        T       value;
    };

    constexpr StringTable(const std::initializer_list<Pair>& list){
        for (auto &x : values)
            x = ZERO;

        for(const Pair &p : list)
            push_(p.key, p.value);
    }

    constexpr bool collisions() const{
        return collisions_;
    }

    constexpr const T &operator[](const char *key) const{
        auto const bucket = bucket__(key);

        return values[bucket];
    }

private:
    constexpr void push_(const char *key, const T &value){
        auto const bucket = bucket__(key);

        auto &element = values[bucket];

        if (element != ZERO)
            collisions_ = true;

        element = value;
    }

    constexpr static size_type bucket__(const char *key){
        return hash__(key) % SIZE;
    }

private:
    constexpr static size_type hash__(const char* str){
        size_type hash = 5381;

        for(auto it = str; *it; ++it)
            hash = ((hash << 5) + hash) + (*it);

        return hash;
    }

private:
    bool    collisions_ = false;
    T   values[SIZE]    = {};
};

#include <iostream>

int main(int argc, char **argv){
    enum class Commands : char{
        NONE    ,
        GET ,
        SET ,
        SETEX   ,
        SAVE
    };

    constexpr uint32_t BUCKETS = 21;

    using CmdTable = StringTable<Commands, BUCKETS, Commands::NONE>;
    using Pair  = CmdTable::Pair;

    constexpr CmdTable table{ {
        Pair{ "get",    Commands::GET   },
        Pair{ "set",    Commands::SET   },
        Pair{ "setex",  Commands::SETEX },
        Pair{ "save",   Commands::SAVE  },
        Pair{ "bgsave", Commands::SAVE  }
    } };

    static_assert( ! table.collisions() );

    static_assert( table["save"] == Commands::SAVE );
    static_assert( table["bala"] == Commands::NONE );

    return int( table[ argv[1] ] );
}