texts: Google Crawler. Searches the 100 first google results for some term

texts:
I wrote a simple Google Crawler in PHP, and my GoogleProxy interface does not seem to follow the Single Responsibility Principle.

Here's my Interface code:

<?php
/**
 * Interface that represents an online proxy service.
 * Every class that implement it must know how to return the apropriate ResponseInterface to an url based on what
 * the proxy service needs, and must know how to parse an url used within its pages.
 *
 * @package CViniciussDias\GoogleCrawler\Proxy
 * @author Vinicius Dias
 */
interface GoogleProxy
{
    /**
     * Gets the ResponseInterface for the informed URL based on all the information that
     * the proxy service needs
     *
     * @param string $url
     * @return ResponseInterface
     */
    public function getHttpResponse(string $url): ResponseInterface;

    /**
     * Parses an URL based on how they are encoded in the proxy service
     *
     * @param string $url
     * @return string
     */
    public function parseUrl(string $url): string;
}

And one of its implementations:

<?php
namespace CViniciusSDias\GoogleCrawler\Proxy;

use CViniciusSDias\GoogleCrawler\Exception\InvalidResultException;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;

/**
 * Class that represents the absense of a proxy service, making the direct request to the url
 * and returning its response
 *
 * @package CViniciussDias\GoogleCrawler\Proxy
 * @author Vinicius Dias
 */
class NoProxy implements GoogleProxy
{
    public function getHttpResponse(string $url): ResponseInterface
    {
        return (new Client())->request('GET', $url);
    }

    public function parseUrl(string $url): string
    {
        // Separates the url parts
        $link = parse_url($url);
        // Parses the parameters of the url query
        parse_str($link['query'], $link);

        $url = filter_var($link['q'], FILTER_VALIDATE_URL);
        // If this is not a valid URL, so the result is (probably) an image, news or video suggestion
        if (!$url) {
            throw new InvalidResultException();
        }

        return $url;
    }
}

Here's the full code if needed (I would enjoy some pull requests too):
https://github.com/CViniciusSDias/google-crawler

Should I split the two tasks it performs into 2 separate classes? Several other Proxies will be implemented in a near future. Wouldn't it make the number of classes increase more than necessary?


texts:
I've taken a look at the SPL Listener/Observer model but found that it doesn't work when using static methods. So I wrote my own but it's very simply.  Can anyone suggest ways to make it more like the Model standards and if there is anything obvious that I've missed?

<?php
namespace modular\core;

class Listeners{
    private static $listeners = array();
    private static $triggered = array();
    private static $broadcasts = array();

    public static function attach($event, $key, $callback){
        $config = \modular\core\ModuleLoader::getModuleConfig($key);
        $config['instance'] = get_class($callback[0]);
        $callback['config'] = $config;
        static::$listeners[$event][md5($event.$key)] = array('module' => $key, 'callback' => $callback);
    }

    public static function detach($event, $key, $callback){
        unset(static::$listeners[$event][md5($event.$key)]);
    }

    public static function listeners($event = ''){
        return ($event == '') ? static::$listeners : static::$listeners[$event];
    }

    public static function listenersRegistered(){
        return static::$listeners;
    }

    public static function listenersTriggered(){
        return static::$triggered;
    }

    public static function getBroadcasts(){
        return static::$broadcasts;
    }

    public static function broadcast($event){
        $rendered = \modular\core\Renderer::rendered();

        $caller = debug_backtrace();//true, 1 - check version and implement this
        array_shift($caller);

        static::$broadcasts[$event][] = $caller;

        if(isset(static::$listeners[$event])){
            if(!$rendered)
                ob_start();

            foreach(static::$listeners[$event] as $listener){
                $myEvent = (\modular\core\Router::routed() ? ($listener['module'] == \modular\core\ModuleLoader::getActiveModule()) : true);
                if($myEvent){
                    static::$triggered[$event] = $listener;
                    $test = \modular\core\ModuleLoader::runModule($listener);
                }
            }

            if(!$rendered)
                ob_end_clean();
        }
    }
}
?>

The code to add a listener:

public static function addListener($event, $callback){
        $modules = \modular\core\ModuleLoader::getModules();

        if(($key = array_search($callback[0], $modules)) !== false){
            \modular\core\Listeners::attach($event, $key, $callback);
        }
        else{
            //problem - object not found...
        }
    }

_::addListener('pre-render', array(&$this, 'init'));

Callback is a callback function set as an array to specify the instance

The MD5 is used to combine both the $event and $key as a $key (module) can register to multiple events and vice-versa.

Broadcast is purely checking if any output has already occurred, if not then block all output if so then allow output whilst it calls the module callback method.

Because it's a framework static methods are used as my preferred method to access a class from multiple classes without having to worry about creating variables of instances that I either have to remember globally or re-instantiating the class constantly.


texts: Apple picking game. More efficient method than using global variables

texts:
I have been working on a small game called apple picker, it's text-based, and relvolves around picking and selling apples. Unfortunately I've been using global variables for this whole thing and it's starting to get a bit ugly looking. Is there a more efficient way to do this than using global variables?

from random import randint
import console, time

global gold
global apples
apples = 1
gold = 1

prompt = "> "

def main():
    global gold
    global apples
    console.clear()
    print "Gold: %r Apples: %r" % (gold, apples)
    print "Pick an apple?"
    choice = raw_input(prompt)
    if choice == "yes":
        pick()
    elif choice == "no":
        console.clear()
        global gold
        global apples
        print "Apples: %r Gold: %r" % (apples, gold)
        print "Sell apples?"
        sell = raw_input(prompt)
        if sell == "yes" and apples >= 1:
            global gold
            global apples
            apple_sales = randint(1,5)
            gold = (gold * (apples / 2)) * apple_sales
            apples = apples - apples
            if gold >= 25 ** 25:
                console.clear()
                print "\t\t\tYou won!"
                print "Congrats on controlling the apple market!"
            else:
                main()
        elif sell == "yes" and apples <= 0:
            print "\nNot enough apples!"
            time.sleep(0.7)
            main()
        elif sell == "no":
            main()
        else:
            main()
    elif choice == "exit":
        console.clear()
        print "Bye..."
    else:
        main()

def pick():
    console.clear()
    global gold
    global apples
    print "Type 0 to exit. How many?"
    print "Apples: %r Gold %r" % (apples, gold)
    try:
        apple_num = int(raw_input(prompt))
        if apple_num == 3 or apple_num == 2 or apple_num == 1:
            global apples
            apples = apples + apple_num
            time.sleep(0.5)
            pick()
        elif apple_num >= 4:
            console.clear()
            print "You can't haul that many apples!"
            time.sleep(0.5)
            pick()
        elif apple_num == 0:
            main()
    except ValueError:
        pick()

main()

Ignore the console module. I wrote this script on IOS.


texts: Find the longest word in set D that is a subsequence of string S

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

  Word W is a subsequence of S if some number of characters, possibly zero, can be deleted from S to form W, without reordering the remaining characters.

  Note: D can appear in any format (list, hash table, prefix tree, etc.)

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

  The words "able" and "ale" are both subsequences of S, but they are shorter than "apple".
  The word "bale" is not a subsequence of S because even though S has all the right letters, they are not in the right order.
  The word "kangaroo" is the longest word in D, but it isn't a subsequence of S.

I am currently trying to learn how to become a better programmer.  I am taking the Google Tech Dev Guide recommended path and this is the first problem.  I came up with my own solution, and read Google's first recommended answer is using brute force, and a slight optimization is to at least ensure the dictionary is represented by a hash table or prefix tree so that lookups are efficient.

This is my first time using a dictionary, and I also don't feel confident about using objects and classes.  If you could review and recommend better ways to improve my code, maybe by using more (or better) objects, I would appreciate it.

class Main:
    def create_dictionary(string):
        dictionary = {}
        index = 0

        for letter in string:
            if letter in dictionary:
                dictionary[letter].append(index)
            else:
                dictionary[letter] = [index]
            index += 1
        return(dictionary)

    def get_word_is_substring(word, dictionary):
        index_of_last_letter_found = None
        for letter in word:
            if letter in dictionary and (index_of_last_letter_found is None or dictionary[letter][-1] > index_of_last_letter_found):
                index = 0
                while index < len(dictionary[letter]):
                    if index_of_last_letter_found is None or index_of_last_letter_found < dictionary[letter][index]:
                        index_of_last_letter_found = dictionary[letter][index]
                        break
                    else:
                        index += 1

            else:
                return False
        return True

    def replace_word_if_necessary(word, longest_word):
        if (longest_word is None) or (len(word) > len(longest_word)):
            longest_word = word
        return longest_word

    def get_longest_word(s, d):
        dictionary = Main.create_dictionary(s)
        longest_word = None

        for word in d:
            word_is_substring = Main.get_word_is_substring(word, dictionary)
            if word_is_substring:
                longest_word = Main.replace_word_if_necessary(word, longest_word)
        print(longest_word)

Main.get_longest_word("abppplee", {"ale", "bale", "able", "apple", "kangaroo"})


texts: C++ makefile for CPLEX with environment loading

texts:
CPLEX is a solver for mathematical problems made by IBM, and it offers callable C libraries.

My usual project configuration is a main.cpp file will all the important code and a simple cpxmacro.h file with some macros. Sometimes there are also .dat files with problem data. When the solver does its job (i.e. the main is run), it outputs .lp and .sol files.

I used to let my IDE come up with proper make files, but this time I was provided with a hand-made make file. What a good opportunity to look into it.

CXX = g++
CXXFLAGS = -g -Wall -O
LDADD =

CPX_INCDIR  = /opt/ibm/ILOG/CPLEX_Studio126/cplex/include
CPX_LDPATH  = /opt/ibm/ILOG/CPLEX_Studio126/cplex/lib/x86-64_linux/static_pic
CPX_LDADD = -lcplex -lm -pthread

OBJ = main.o

%.o: %.cpp
    $(CXX) $(CXXFLAGS) -I$(CPX_INCDIR) -c $^ -o $@

main: $(OBJ)
    $(CXX) $(CXXFLAGS) $(OBJ) -o main -L$(CPX_LDPATH) $(CPX_LDADD)

clean:
    rm -rf $(OBJ) main

.PHONY: clean

As long as I only change the main file, it works. I just have to manually load a small file first, by typing . cplex_env. This is what the file contains:

#!/bin/sh

export PATH=$PATH:/opt/ibm/ILOG/CPLEX_Studio126/opl/bin/x86-64_linux:/opt/ibm/ILOG/CPLEX_Studio126/cplex/bin/x86-64_linux
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/opt/ibm/ILOG/CPLEX_Studio126/opl/bin/x86-64_linux:/opt/ibm/ILOG/CPLEX_Studio126/cplex/bin/x86-64_linux:/opt/ibm/ILOG/CPLEX_Studio126/cpoptimizer/bin/x86-64_linux/

I never tried to change the .h macro file and see what happens, it doesn't look to me like it's being accounted for anywhere in the makefile.

I'd like to:

include the path exports in the make file
make the makefile a little more generic


texts: Modifying an array during a foreach loop

texts:
This question is always on my mind. I usually alter the array while looping through its keys, based on my gut feelings:

foreach (array_keys($array) as $key) {
  $array[$key] = perform_changes_on($array[$key]);
}

I'm aware of other methods, though. The array item can be passed by reference on the foreach call:

foreach ($array as &$item) {
  $item = perform_changes_on($item);
}

Finally, the array can be modified directly during the loop:

foreach ($array as $key => $value) {
    $array[$key] = perform_changes_on($value);
}

What are the performance and security implications of each approach? Is there a recommended approach?

UPDATE: What I'm actually worried about is that the two last approaches modify the array while I'm looping it.


texts:
I have this code that helps me to paste data from an Excel file to a Datagridview:

If e.Control AndAlso e.KeyCode = Keys.V Then

    DGVLookUP.Rows.Clear()
    DGVLookUP.Columns.Clear()

    Try
        Dim Generar As Boolean = False
        Dim N As Integer
        For Each line As String In Clipboard.GetText.Split(vbNewLine)
            If Not line.Trim.ToString = "" Then
                Dim item() As String = line.Trim.Split(vbTab)
                If Generar = False Then
                    For C = 0 To UBound(item)
                        DGVLookUP.Columns.Add("C" + CStr(N), CStr(N))
                        N += 1
                    Next
                    Generar = True
                End If
                Me.DGVLookUP.Rows.Add(item)
            End If
        Next

    Catch ex As Exception
        MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

End If

I am using VB.Net and I am pasting over 3k rows and it takes me between 5-10 mins to paste the info. I would like to optimize my code or if there is another way better to paste the data.


texts: To use or not to use public variables in javascript?

texts:
Question:

I'm attemping to modify a Javascript snippet to more efficiently get the user's current Zipcode. One problem that I am facing however is the lack of understanding around public functions. I am wondering if they are requried, and if they are not, then what would make the code cleaner and more efficient in terms of using or not using public.?

Code:

var geoLocateAddress = function () {
        var public = {};
        public.init = function() {
            public.initEventHandlers();
        };
        public.initEventHandlers = function() {
            console.log("Using your browser's GeoLocation API");
            navigator.geolocation.getCurrentPosition(
            public.loadAddressByGeolocation);
        };
        public.loadAddressByGeolocation = function(position) {
            console.dir(position);
            console.log("Your browser only returns Long/Lat");
            public.loadAddressByLngLat(position);
        };
        public.loadAddressByLngLat = function(position){
            var data = {
                lat : position.coords.latitude,
                lng : position.coords.longitude
            };
            var url = "http://ws.geonames.org/findNearestAddressJSON";
            $.ajax({
                url: url,
                dataType: 'jsonp',
                data: data,
                success: function(data, textStatus, xhr) {
                    public.updateAddressFields({
                        zipCode: data.address.postalcode,
                    });
                },
                error: function(xhr, textStatus, errorThrown) {
                    console.log(textStatus);
                }
            });
            return false;
        };
        public.updateAddressFields = function(address) {
            console.log('Zip: ' + address.zipCode);
            $("#zip").text(address.zipCode);
            console.log('Zip Logged');

        };
        return public;
    } ();
geoLocateAddress.init();


texts: Generating a sinusoid sound signal in Java

texts:
Is there any legroom left for optimization (without switching to C)?

static byte[] generateSound(double frequency, int sampleRate, int samples) {
    byte output[] = new byte[2 * samples];

    int idx = 0;

    double _tone = 2 * Math.PI / (sampleRate / frequency);

    for (int i = 0; i < samples; ++i) {
        double dVal = Math.sin(_tone * i);

        final short val = (short) ((dVal * 32767));

        output[idx++] = (byte) (val & 0x00ff);
        output[idx++] = (byte) ((val & 0xff00) >>> 8);
    }

    return output;
}


texts:
A similar question must have been asked a thousand times, but for the life of me I can't figure this out. I am new to Java and I want to create a programme that asks a user for his or her height and weight, and calculate the corresponding BMI. I want to throw exceptions for the wrong input (has to be a double, non-negative and not zero) and I want to catch any mathematical errors that may occur. The follow code does that, but I have exact duplication for handling the input of height and weight. How can I simplify this into a more concise format without losing any functionality?

import java.util.InputMismatchException;
import java.util.Scanner;

public class BMIcalculator {
    public static void main(String[] args){
        double BMI = calculateBMI();
        System.out.print("\nYour BMI is " + BMI + ".");
    }

    public static double calculateBMI() {
        // Scanner allows us to request a users input
        Scanner scan = new Scanner(System.in);
        // Initialize doubles as zero values
        double w=0, h=0;

        // Ask for user input (weight)
        System.out.println("Please enter your weight");
        while(true) {
            try {
                w = scan.nextDouble();
                if (w <= 0) throw new ArithmeticException();
                break;
            } catch (InputMismatchException ime) {
                System.err.println("Ahw, error: " + ime.getMessage());
                continue;
            } catch (ArithmeticException ae) {
                System.err.println("Weight can't be less than or equal to zero!");
                continue;
            }
        }
        // Ask for user input (height)
        System.out.println("Please enter your height");
        while(true) {
            try {
                h = scan.nextDouble();
                if (h <= 0) throw new ArithmeticException();
                break;
            } catch (InputMismatchException ime) {
                System.err.println("Ahw, error: " + ime.getMessage());
                continue;
            } catch (ArithmeticException ae) {
                System.err.println("Height can't be less than or equal to zero!");
                continue;
            }
        }
        // try-catch not necessary; we only get here when our input is valid

        // we have to close the input connection to the user
        scan.close();
        return w / (h * h);
    }
}


texts: Bash script to find included files in C code

texts:
I've got the following shell script (let say get_includes.sh):

#!/usr/bin/env bash
includes=($(grep ^#include file.c | grep -o '"[^"]\+"' | tr -d '"'))
echo "0: ${includes[0]}"
echo "1: ${includes[1]}"

which aims at finding relative include files in source code file.

So for given file like this (file.c):

#include "foo.h"
#include "bar.h"

#include <stdio.h>

int main(void) {
    printf("Hello World\n");
    return 0;
}

It'll return the following results which are correct:

$ ./get_includes.sh
0: foo.h
1: bar.h

The code works as expected, however shellcheck complains about the following issues:

$ shellcheck get_includes.sh

    In get_includes.sh line 2:
    includes=($(grep ^#include file.c | grep -o '"[^"]\+"' | tr -d '"'))
              ^-- SC2207: Prefer mapfile or read -a to split command output (or quote to avoid splitting).

    For more information:
      https://www.shellcheck.net/wiki/SC2207 -- Prefer mapfile or read -a to spli...
      https://www.shellcheck.net/wiki/SC2236 -- Use -n instead of ! -z.

So:

I can't quote command substitution, as I expect the command to expand to an array.
I don't want to ignore the warning, I'd like to correct it.

I'm using Bash 4.

So, how I can correct the above line to satisfy shellcheck? If possible, I'd like to keep it in one-liner.

I've tried the following approaches which failed:

$ (grep ^#include file.c | grep -o '"[^"]\+"' | read -a myarr; echo $myarr)
(nothing is printed)
$ (grep ^#include file.c | grep -o '"[^"]\+"' | mapfile myarr; echo $myarr)
(nothing is printed)


texts: Code to generate a dictionary from two lists

texts:
I created a script to perform a union on 2 lists: keys and words. I needed to create a dictionary from them. If there are more keys than words, missing words are replaced with "None". Any excess words are removed.

How can I improve this code to be more compact and perform better?

#! /usr/bin/env python3
import random
import string
from itertools import zip_longest
def generator(listKeys,listWords):

    diff = len(listKeys)-len(listWords)
    if diff > 0:
        for i in range(0,diff):
            listWords.append(None)
    elif diff<0:
        listWords = listWords[0:len(listKeys)]

    done = dict(zip(listKeys,listWords))
    return done

if __name__ == "__main__":
    listKeys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
    listWords = [94, 87, 92, 5, 75, 91, 60, 5]
    print("Keys more values:\nKeys: %s \nValues %s\nResult: %s\n" % (listKeys, listWords,generator(listKeys,listWords)))

    listKeys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
    listWords = [9, 65, 18, 80, 2, 16, 81, 98, 73, 2, 88, 30, 1, 78, 92]
    print("Values more keys:\nKeys: %s \nValues %s\nResult: %s\n" % (listKeys, listWords,generator(listKeys,listWords)))

    listKeys = []
    listWords = []

    for i in range(0,random.randrange(5,15)):
        listKeys.append(list(string.ascii_lowercase)[i])

    for i in range(0,random.randrange(6,16)):
        listWords.append(random.randrange(0,100))

    print("Random:\nKeys: %s \nValues %s\nResult: %s\n" % (listKeys, listWords,generator(listKeys,listWords)))


texts:
I'm creating a data set to be used for a machine learning application. To do this I use my webcam to record videos at 1 frame per minute and did this for a few days.

I then created a program to extract these frames (in gray scale) to create my data set and let it run over the weekend.

After I noticed that not all my videos were set to 1 frame per minute (I assume that some were set to 60 frames per second). My data set is now saturated with extremely similar images (I believe this will cause issues when trying to train my ML application) and because of this I have decided to try to filter out similar images.

The small python script I've written to do this compare images and if they are "similar" then they are moved to a folder that I'll delete after. The amount of files that this script has to compare is very large and I'm afraid that it'll start slowing down to a crawl.

Would you be able to help review my code and offer any suggestions to optimize the performance of it. Thanks in advance.

I think the place that would benefit from the greatest improvement is how I choose to compare two images. To determine if an image is similar I add up the differences in pixels between this image and each "unique" image I've currently found. If this is value is less than a threshold I have set (15 million) than I label the image as similar and move it if not I add it to the "unique" list.

#import 3rd party Lib
import time
import numpy as np
import cv2
import sys
import traceback
import os
from os import listdir
from os.path import isfile, join
import pickle
import hashlib
import time

def Compare(dirTarget, dirDest):

    compareFiles = []

    sourceFileList = [f for f in listdir(dirTarget) if isfile(join(dirTarget, f))]
    print("Files Read from HD")
    i = 0

    startT = time.time()
    while len(sourceFileList) > 0:

        file = sourceFileList.pop()
        targetImage = cv2.imread(dirTarget + file,0)

        isSimilar = False

        for compareImage in compareFiles:
            deltaArr = np.absolute(np.subtract(targetImage, compareImage))
            dist = round(sum(map(sum, deltaArr))/1000000,0)

            if dist < 15:
                isSimilar = True
                continue

        if isSimilar:
            os.rename(dirTarget + file, dirDest + file)
        else:
            compareFiles.append(targetImage)

        if i == 100:
            i = 0
            endT = time.time()
            difference = int(endT - startT)
            startT = endT
            print("Unsimilar: " + str(len(compareFiles)) +
                  " Left: " + str(len(sourceFileList)) +
                  " Time: " + str(difference))
        else:
            i += 1

hashedUniqueList = list()

dir1 = "Superevised Set\One\\"
dir0 = "Superevised Set\Zero\\"

destdir1 = "ToDel\One\\"
destdir0 = "ToDel\Zero\\"

dirTarget = dir1
dirDest = destdir1

The reason I choose 15 million as my threshold is because I did a quick sampling of how similar an image was to a subset of images and I got the following distribution. This is roughly 0.6% of my data. Do you think this number should be higher or (I doubt) lower.

Delta   Count
11  1
12  16
13  32
14  112
15  306
16  436
17  995
18  1756
19  980
20  1195
21  822
22  651
23  1067
24  1656
25  4561
26  4676
27  3105
28  4008
29  2125
30  3587
31  4694
32  3557
33  2136
34  1491
35  1969
36  794
37  949
38  286
39  801
40  569
41  425
42  471
43  225
44  241
45  59
46  367
47  190
48  146
49  459
50  220
51  84
52  734
53  1100
54  257
55  521
56  1071
57  296
58  928
59  1624
60  764
61  1001
62  1792
63  1884
64  1496
65  4074
66  6557
67  11


texts: Finding all paths between nodes in a graph

texts:
I wanted to write a function paths that returns the possible routes  between two nodes in a graph.

For example:

const graph = [
    ['a', 'b'],  // i.e. directed edge a => b
    ['a', 'c'],  // i.e. directed edge a => c
    ['b', 'a'],  // i.e. directed edge b => a
    ['b', 'c'],  // ...and so on
    ['c', 'a'],
    ['c', 'b']
];

paths({ graph, from: 'a', to: 'a' });

Should output:

[
  ["a","b","a"],
  ["a","b","c","a"],
  ["a","b","c","b","a"],
  ["a","c","a"],
  ["a","c","b","a"],
  ["a","c","b","c","a"]
]

Feedback on correctness and style is welcome.

function paths({ graph = [], from, to }, path = []) {
    const linkedNodes = memoize(nodes.bind(null, graph));
    return explore(from, to);

    function explore(currNode, to, paths = []) {
        path.push(currNode);
        for (let linkedNode of linkedNodes(currNode)) {
            if (linkedNode === to) {
                let result = path.slice(); // copy values
                result.push(to);
                paths.push(result);
                continue;
            }
            // do not re-explore edges
            if (!hasEdgeBeenFollowedInPath({
                    edge: {
                        from: currNode,
                        to: linkedNode
                    },
                    path
                })) {
                explore(linkedNode, to, paths);
            }
        }
        path.pop(); // sub-graph fully explored
        return paths;
    }
}

/**
 * Get all nodes linked
 * to from `node`.
 */
function nodes(graph, node) {
    return graph.reduce((p, c) => {
        (c[0] === node) && p.push(c[1]);
        return p;
    }, []);
}

/**
 * Has an edge been followed
 * in the given path?
 */
function hasEdgeBeenFollowedInPath({ edge, path }) {
    var indices = allIndices(path, edge.from);
    return indices.some(i => path[i + 1] === edge.to);
}

/**
 * Utility to get all indices of
 * values matching `val` in `arr`.
 */
function allIndices(arr, val) {
    var indices = [],
        i;
    for (i = 0; i < arr.length; i++) {
        if (arr[i] === val) {
            indices.push(i);
        }
    }
    return indices;
}

/**
 * Avoids recalculating linked
 * nodes.
 */
function memoize(fn) {
    const cache = new Map();
    return function() {
        var key = JSON.stringify(arguments);
        var cached = cache.get(key);
        if (cached) {
            return cached;
        }
        cached = fn.apply(this, arguments)
        cache.set(key, cached);
        return cached;;
    };
}

/**
 * Example usage.
 **/
const graph = [
    ['a', 'b'],
    ['a', 'c'],
    ['b', 'a'],
    ['b', 'c'],
    ['c', 'a'],
    ['c', 'b']
];
document.write(JSON.stringify(paths({
    graph,
    from: 'a',
    to: 'a'
})));


texts: Project Euler 28: sum of spiral diagonals using recursion

texts:
I've solved Project Euler #28 using recursion.

  If this 5 × 5 spiral pattern is extended to 1001 × 1001, what would be the sum of the red diagonals?

  $$\begin{matrix}
\color{red}{21} & 22 & 23 & 24 & \color{red}{25} \\
20 &  \color{red}{7} &  8 &  \color{red}{9} & 10 \\
19 &  6 &  \color{red}{1} &  2 & 11 \\
18 &  \color{red}{5} &  4 &  \color{red}{3} & 12 \\
\color{red}{17} & 16 & 15 & 14 & \color{red}{13}
\end{matrix}$$

Is this a good enough solution, or is there some other way to optimize the code?

static long GetSumofDiagonals(int cubeSize)
    {
        long sum = 0;
        if (cubeSize == 1)
            return 1;
        else
        {
            // As the diagonal numbers follows a sequence
            // UR = n^2 - 1;
            // UL = UR - (n - 1)
            // LL = UL - (n -1 )
            // LR = LL - (n - 1)
            // This works only for odd n
            long UpperRight = (long) Math.Pow(cubeSize, 2);
            long UpperLeft = UpperRight - (cubeSize - 1);
            long LowerLeft = UpperLeft - (cubeSize - 1) ;
            long LowerRight = LowerLeft - (cubeSize - 1);
            sum = UpperRight + UpperLeft + LowerLeft + LowerRight;
            return sum + GetSumofDiagonals(cubeSize - 2);
        }
    }


texts: Getting student demographics report with ActiveRecord

texts:
I have two active records StudentDemographics and StudentWeeklyReport both having has_many relation like this:

class StudentDemographics < ActiveRecord::Base
  has_many :student_weekly_reports, :foreign_key => :student_id
end

I have to check marks for each student in the last fifth week seminar with the latest one. If the result is true, student should be active, else inactive. I have the following code. Here, I am repeating the loop with each date. @distinct is an array of dates.

def active_learner

  safe = []

  @active = []

  @inactive = []

  @distinct = StudentDemographics.select(:date).map(&:date).uniq.sort

  @students = StudentDemographics.includes(:student_weekly_reports).select("student_id,date")

@distinct.each_with_index  do |date,i|

 if i < 4

     @count = StudentDemographics.where("date <= ?", date).count

     @active[i] = @count

     @inactive[i] = 0

 else

     sum = safe.length

     active = inactive = 0

     (@students - safe).each do |student|

        next if student.date > date

        @stu = student.student_weekly_reports.last(5)

            if @stu.length > 4

                    if @stu[4].golden_eggs > @stu[0].golden_eggs

                      safe << student

                      active += 1

                    else

                      inactive += 1

                    end

            else

                    safe << student

                    active += 1

            end

      end

      @active[i] = active + sum

      @inactive[i] = inactive

 end

end
end

The performance is not good. It's taking more than 3 secs of time. My MySQL database has 13600 in the StudentWeeklyReports table and 2000 in the StudentDemographics table. Can anyone suggest how I can optimize this?


texts:
The challenge was to find the missing letter in an array in alphabetical order.

  Find the missing letter in the passed letter range and return it.

  If all letters are present in the range, return undefined.

My solution was this:

function fearNotLetter(str) {
  var letters = str.split('');
  var codes = [];
  var missing = 0;
  var start = 0 , next = 0 , c = 0;
  for(var i= 0; i < letters.length; i++){
    var charcode = letters[i].toString();
    codes.push(charcode.charCodeAt());
  }
  for(var j = 1; j < codes.length; j++){
    start = codes[j] -1;
    if(codes.indexOf(start) === -1){
      missing += start;
    }

  }
  if(missing === 0){
    return undefined
  }else {
    return String.fromCharCode(missing);
  }
}

I am wondering whether I have followed some bad practices or used dry code which might cause problems in a later use. Thanks in advance.


texts:
I know what you are thinking, it's never a good idea to roll your own encryption functions, but I'm doing this for fun and self-learning.

I created a function that allows you to send a string as a parameter and it will return an encrypted version of the string.

Here is my encryption function:

function encrypt($string) {
    $characters = str_split($string);
    foreach($characters as &$character) {
        $ascii = ord($character);
        $random = rand(0,9);
        if($ascii < 44) {
            $prefix = chr(rand(35,78));
            $letter = chr($ascii + $random);
        } else {
            $prefix = chr(rand(79,122));
            $letter = chr($ascii - $random);
        }
        $r_ascii = [chr(rand(35,122)), chr(rand(35,122)), chr(rand(35,122))];
        $character = "{$prefix}{$random}{$r_ascii[0]}{$letter}{$r_ascii[1]}{$r_ascii[2]}";
    }
    return implode("", $characters);
}

This will encrypt the string, whereas every character will become 6 characters. The 1st 2 of each character are keys that are required to decrypt the letter, and the 4th character is the actual character being encrypted except masked by a random digit (Basically the letter is converted to its ASCII number and a random number between 0-9 is added/subtracted from the letter). The 1st key (labeled prefix) lets the decrypt function know if the random number needs to be added or subtracted, the random number is passed as the 2nd character in the encryption, and characters 3, 5 and 6 are random noise characters.

Note: The prefix is generated depending on the ASCII number of the character that is being encrypted. If this ASCII number is below 44, the random number is always added to the character being encrypted, else it is always subtracted. The prefix itself is a character in a certain range, if the ASCII number is below 44, a random character between ASCII 35 and 78 is generated, any of these characters tell the decrypt function to subtract the random number from the encrypted character, else ASCII between 79 and 122 will always add the number.

For example, the string "Never roll your own encryption functions." may encrypt to:

s7uGsxW0ueMkX99m^Zr6E_3jP1cqJ_>67&KyX6jlX.k7@hb/m1BkFDQ94cD7<0: c$`9Zp3ot6*iHLW2.sSsm2YpZ,=00 4zW3Tl\ti5@r4ir2]l<791H!9'f3ebw0y8ef%1a7B\>)u2_pY)s4qu0-[1ZoRRv6)nIn\2kgx?n9df@&j8sfP8#9))p1z1JejRd9Hl(t[27l+9R5y^>GT0&tu\w2]gAif8ig;AZ3mkf@Q7Yl#/h86&np

Now, the decrypt function:

function decrypt($string) {
    $characters = str_split($string, 6);
    foreach($characters as &$character) {
        if(strlen($character) > 6) continue;
        $parts = str_split($character);
        if(ord($parts[0]) > 78) {
            $character = chr(ord($parts[3])+$parts[1]);
        } else {
            $character = chr(ord($parts[3])-$parts[1]);
        }
    }
    return implode("", $characters);
}

This function takes the encrypted string and breaks it into parts of 6 (Because every encrypted character is equal to 6 characters) and reverses all of the randomizations that I did during encryption to return the string:

Never roll your own encryption functions.

My question(s): What shortcomings will I have with these functions?

Another question I have is; how can these functions be improved?


texts: Reactive operator for distinct values that must change by amount

texts:
I would like feedback on this DistinctUntilChangedBy operator for System.Reactive that only emits when the current value is different than the last by a specified amount. Is there a better implementation?

I want to avoid multiple emits when the stream looks like this: 0.80, 0.81, 0.80, 0.81, etc.

Using the operator like this:

stream.DistinctUntilChangedBy(0.01).Subscribe(x => Console.WriteLine(x));

Should output this:

0.80

The operator looks like this and it is also on GitHub as a gist:

using System;
using System.Collections.Generic;
using System.Reactive.Linq;

namespace Gists.Reactive.Linq
{
    public static partial class ObservableExtensions
    {
        ///<summary>
        ///Only emit when the current value is different than the last by the specified amount.
        ///</summary>
        public static IObservable<decimal> DistinctUntilChangedBy(this IObservable<decimal> source, decimal amount)
        {
            return source.DistinctUntilChanged(new DistinctUntilChangedByEqualityComparer(amount));
        }

        private class DistinctUntilChangedByEqualityComparer : IEqualityComparer<decimal>
        {
            private readonly decimal amount;

            public DistinctUntilChangedByEqualityComparer(decimal amount)
            {
                this.amount = amount;
            }

            public bool Equals(decimal x, decimal y)
            {
                var diff = Math.Abs(x - y);
                return diff <= Math.Abs(amount);
            }

            public int GetHashCode(decimal obj)
            {
                throw new NotSupportedException();
            }
        }
    }
}


texts: Minimum window substring - LeetCode challenge

texts:
Below is my code for the “Minimum Window Substring” LeetCode problem in Swift:

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC

My solution fails on a very long string,  but passes all the other 267 cases apart from that. I can't figure out why the Leetcode online judge says time limit exceeded, my solution seems o(n), any help is much appreciated.
func minWindow(_ s: String, _ t: String) -> String {
        var end = 0
        var start = 0

        if s == "" || t == "" || s.count < t.count {
            return ""
        }

        var freq: [Character : Int] = [:]

        for curChar in t {
            if let val = freq[curChar] {
                freq[curChar] = val + 1
            } else {
                freq[curChar] = 1
            }
        }

        let stringArray = Array(s)

        var resStart = 0
        var resLen = Int.max

        var distinct = freq.keys.count

        while end < s.count {
            let curChar = stringArray[end]

            if let val = freq[curChar] {
                freq[curChar] = val - 1
                if val - 1 == 0 {
                    distinct -= 1
                }
            }

            while (distinct == 0) {
                if resLen > end - start {
                    resLen = end - start
                   resStart = start
                }

                let curStart = stringArray[start]
                if let val = freq[curStart] {
                    freq[curStart] = val + 1
                    if val + 1 > 0 {
                        distinct += 1
                    }
                }
                start += 1
            }

            end += 1
        }

        if resLen == Int.max {
            return ""
        } else {
            return String(stringArray[resStart...(resStart + resLen)])
        }

    }


texts: Alternate threads to print even and odd numbers

texts:
I have written the following piece of code to have two threads printing odd and even numbers alternatively. I know this is a common question but I wanted to get this reviewed and understand if it is right or wrong. If wrong, can this be tweaked slightly to get desired result or the approach itself is wrong. Thanks for help.

public class ThreadTest implements Runnable{

    private static int counter = 1;
    private static final Object lock = new Object();

    public static void main(String[] args) {
        // TODO Auto-generated method stub

         Thread t1 = new Thread(new ThreadTest(), "arunabh");
         t1.start();
         Thread t2 = new Thread(new ThreadTest(), "hejib");
         t2.start();

    }

@Override
public void run() {
    while (counter<=100) {
        synchronized (lock) {
            if (counter % 2 == 0) {
                System.out.println(counter +" Written By even Thread-"+ Thread.currentThread().getName());
                counter++;
                try {
                    lock.notify();
                    lock.wait();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            } else if (counter % 2 == 1) {
                System.out.println(counter +" Written By odd Thread-"+ Thread.currentThread().getName());
                counter++;

                try {
                     lock.notify();
                     lock.wait();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }
  }
}


texts: Implementing timeout of a resource (in this case a database connection)

texts:
I am currently having a problem with too many database connections. Therefore, I am trying to implement a database connection timeout. In the code below the changing of value of the shared resource will be replaced with connecting and disconnecting to the database. I am new to programming with threads and I kindly wonder if I have made any logical errors or if there are any better approaches to this problem? There is no built-in timeout in OCCI which I use.

#include <thread>
#include <chrono>
#include <iostream>
#include <mutex>
#include <atomic>

namespace chrono = std::chrono;

class scoped_thread{
  std::thread t;
public:
  explicit scoped_thread(std::thread t_): t(std::move(t_)){
    if ( !t.joinable()) throw std::logic_error("No thread");
  }
  ~scoped_thread(){
    t.join();
    std::cout << "Thread " << std::this_thread::get_id() << " joined.\n";
  }
  scoped_thread(scoped_thread&)= delete;
  scoped_thread& operator=(scoped_thread const &)= delete;
};

template<typename T>
class LockWrapper {
    T* t;
    std::mutex mut;

public:
    class Proxy {
        T* t;
        std::unique_lock<std::mutex> lock;
        Proxy(LockWrapper& wr) : t(wr.t), lock(wr.mut) { }
        friend LockWrapper;
    public:
        T& operator*() { return *t; }
        T* operator->() { return t; }
    };
    LockWrapper(T* t) : t(t) {}

    Proxy aquire() { return {*this}; }
};

using clock_type = chrono::system_clock;
using shared_deadline = std::atomic<clock_type::time_point>;
constexpr auto tlim=100;
template<typename T> void timeout(T& shared_resource,const shared_deadline& deadline) {
    while(clock_type::now()<deadline.load())
    {
        constexpr auto sleeping_time=1;
        std::cout << "Sleeping for another  " << 1 << " seconds\n";
        std::this_thread::sleep_until(clock_type::now()+chrono::seconds(1));
    }
    *shared_resource.aquire()=0; // will replace by database disconnect
    std::cout << "Setting resource to " << (*shared_resource.aquire()) << " after timeout occurred in thread " << std::this_thread::get_id() << ".\n";
}
int main()
{
    int int_value=1;
    LockWrapper<int> shared_resource(&int_value);
    constexpr auto timeout_length=10;
    auto get_new_deadline=[timeout_length](){return clock_type::now()+chrono::seconds(timeout_length);};
    shared_deadline deadline(get_new_deadline());
    auto th = std::make_unique<scoped_thread>(std::thread(timeout<LockWrapper<int>>, std::ref(shared_resource),std::cref(deadline)));
    constexpr int sleeping_time=20;
    for(auto t=0;t<tlim;t+=sleeping_time) // This loop simulates repeated database access
    {
        std::cout << "Slept for " << t << " seconds. Sleeping for another " << sleeping_time << " seconds\n";
        std::this_thread::sleep_for(chrono::seconds(sleeping_time));
        if((*shared_resource.aquire())==0)
        {
            *shared_resource.aquire()=1; // will replace by database connect
            std::cout << "Setting resource to " << (*shared_resource.aquire()) << "\n";
            deadline=get_new_deadline();
            th = std::make_unique<scoped_thread>(std::thread(timeout<LockWrapper<int>>, std::ref(shared_resource),std::cref(deadline)));
        }
        else
        {
            deadline=get_new_deadline();
        }
    }
}


texts: Gives a live view to a changing file on server

texts:
I have a file on my server which contains a running output of a script. This file is constantly changing and fluctuates in size between 0 - 1Mb. Now I want to take this file and make it viewable from the web. Currently I am using jQuery to get the file then I set a textarea's value to be the file content. In order to make this a "live" window I repeat the code every 5 seconds.

This was working meh at first but now is causing high cpu usage and essentially crashing my webpage.

Here is the code I am currently using.

function getUtilLog(){
    $("#livewindow").resizable();
    $.ajaxSetup({
        // Disable caching of AJAX responses
        cache: false
    });

    readUtilLog();
    setInterval(function(){readUtilLog();}, 5000);

    function readUtilLog(){
        $.get("UtilRun.log", function(data){
            if (data.length < 256000){
                $.get("UtilRun.log.1", function(data1){
                    d=data1 + data;
                    $('textarea#livewindow').val(d);
                });
            }
            else{
                d=data;
                $('textarea#livewindow').val(d);
            }
        });
    }
}

I want to know a better way of doing this which will be less cpu intensive. What is the correct way of accomplishing this?


texts: Are there pitfalls to this solution to read messages from a queue in parallel?

texts:
I've posted a question on stackoverflow: How can I consequently read messages from a queue in parallel? I would like my own answer to be reviewed.
Situation
We have one message queue. We would like to process messages in parallel and limit the number of simultaneously processed messages.
Our solution
Based on answers given and some research of our own, we've come to a solution. We're using a SemaphoreSlim to limit our number of parallel Tasks.
Are there any pitfalls to this solution?
I'm also interested in any better solutions.
static string queue = @".\Private$\concurrenttest";

private static async Task Process(CancellationToken token)
{
    MessageQueue msMq = new MessageQueue(queue);
    msMq.Formatter = new XmlMessageFormatter(new Type[] { typeof(Command1) });
    SemaphoreSlim s = new SemaphoreSlim(15, 15);

    while (true)
    {
        await s.WaitAsync();
        await PeekAsync(msMq);
        Command1 message = await ReceiveAsync(msMq);
        Task.Run(async () =>
        {
            try
            {
                await HandleMessage(message);
            }
            catch (Exception)
            {
                // Exception handling
            }
            s.Release();
        });
    }
}

private static Task HandleMessage(Command1 message)
{
    Console.WriteLine("id: " + message.id + ", name: " + message.name);
    Thread.Sleep(1000);
    return Task.FromResult(1);
}

private static Task<Message> PeekAsync(MessageQueue msMq)
{
    return Task.Factory.FromAsync<Message>(msMq.BeginPeek(), msMq.EndPeek);
}

public class Command1
{
    public int id { get; set; }
    public string name { get; set; }
}

private static async Task<Command1> ReceiveAsync(MessageQueue msMq)
{
    var receiveAsync = await Task.Factory.FromAsync<Message>(msMq.BeginReceive(), msMq.EndPeek);
    return (Command1)receiveAsync.Body;
}


texts:
I am trying to implement a search routine for an application written in C#. The requirements are as follows:

The user can select one of the following options for search:

Property Search(Exact): the text entered in the search box has to match exactly with the results, e.g. searching for PL should only show the results whose properties contain PL.
Property Search(Contains): the text entered in the search box can be part of the results, e.g. searching for PL should show all the results whose properties contain PL, e.g. PLApp, Planning, playing, etc.

I have implemented it in my code by passing a boolean flag to my method, and using if..else to perform the searches accordingly.

    public override bool PropertySearch(string value, bool searchPartial = false)
    {
        if(searchPartial)
        {
            if (dataValue.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
                return true;
        } else
        {
            if (dataValue.Equals(value, StringComparison.OrdinalIgnoreCase))
                return true;
        }
        return false;
    }

This was my first implementation, and I have a feeling there might be a better and concise way to achieve this. Any thoughts?


texts: Given an array A[] and a number x, check for pair in A[] with sum as x

texts:
LINQ/FP style:

Assert.IsTrue(new[] {1,2,3}.AnyPairSum(4));

Where:

static class LinqDemo
{
    public static bool AnyPairSum(this IEnumerable<int> source, int sum)
    {
        var a = source.OrderBy(i => i).ToArray();
        return scan(0, a.Length - 1);
        bool scan(int l, int r) =>
            l >= r ? false :
            a[l] + a[r] > sum ? scan(l, r - 1) :
            a[l] + a[r] < sum ? scan(l + 1, r) :
            true;
    }
}


texts:
I am trying to write a simple reusable pool of curl handles. I found this wonderful implementation of the blocking queue.

The pool itself looks like this:

class curl_pool
{
private:
    int        m_size;
    queue<int> m_queue;
    CURL     **m_handles;
public:
    class connection
    {
        friend class curl_pool;
    private:
        curl_pool &m_pool;
        int        m_index;
        connection(curl_pool &pool, int index) : m_pool(pool), m_index(index) {}
    public:
        connection(connection&& that) : m_pool(that.m_pool), m_index(that.m_index) {}
        CURL *handle() const {
            return m_pool.m_handles[m_index];
        }
        void release() {
            m_pool.m_queue.push(m_index);
        }
        virtual ~connection() {
            release();
        }
    };
    connection open() {
        return connection(*this, m_queue.pop());
    }
    curl_pool(const std::string &root_cert, int size) : m_size(size) {
        curl_global_init(CURL_GLOBAL_DEFAULT);
        m_handles = new CURL*[m_size];
        for (int i = 0; i < m_size; i++) {
            CURL *curl = curl_easy_init();
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
            curl_easy_setopt(curl, CURLOPT_CAINFO, root_cert.c_str());
            curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
            curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);
            m_handles[i] = curl;
            m_queue.push(i);
        }
    }
    virtual ~curl_pool() {
        for (int i = 0; i < m_size; i++) {
            curl_easy_cleanup(m_handles[i]);
        }
        delete m_handles;
        curl_global_cleanup();
    }
};

This is the intended use:

{
    curl_pool::connection c = pool.open();
    CURL *curl = c.handle();
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.***.com/");
    CURLcode res = curl_easy_perform(curl);
    stream << "Status code: " << res;
}

I am not a C++ person at all, so I am certain there are a lot of things to improve.


texts: Function that helps with reaching nested objects

texts:
/**
 * Returns the value of a nested property of source object if it exists.
 * Even if the nested property evaluates to false it will be returned.
 * If the nested property does not exist, undefined will be returned.
 * @param {Object} source       source object
 * @param {String} path         path without first dot e.g. "response.http.statusCode"
 * @param {String=} sourceName  if provided, any catched error will be logged to console
 * @returns {*}                 value under the source.path or undefined
 */
function reach(source, path, sourceName) {
    var result;
    try {
        if (sourceName) {
            eval('var ' + sourceName + '=source; result=' + sourceName + '.' + path);
        } else {
            eval('result=source.' + path);
        }
    } catch (err) {
        if (sourceName) {
            console.log(err);
        }
    }
    return result;
}

I know that eval is dangerous, but I made the assumption that it's the caller's responsibility.


texts:
Say that I have running id from 1 to n, and a value column:

set.seed(1)
x <- data.frame(c(1:10),rnorm(10,10, sd = 2.5))
colnames(x) <- c("id", "value")

   id     value
1   1  8.433865
2   2 10.459108
3   3  7.910928
4   4 13.988202
5   5 10.823769
6   6  7.948829
7   7 11.218573
8   8 11.845812
9   9 11.439453
10 10  9.236529

Now let's imagine that I have for some reason lost some of that data, but I nevertheless need to fill it with some value

# Let's lose data
(x <-  x[-5,])

Now I am missing observation #5, but I still need to replace it with a value (e.g. 0 or NA). Note that in reality I don't necessary know what observation ID is missing.

This is what I wrote, and it works. However, I am wondering whether there is a vectorized way of doing this (or a more efficient way in general)?

f <- function(x, fill_value){
  # Get number of rows
  n <- nrow(x)
  max_id <- max(x$id)

  # Get missing data position
  no_data_position <-  which(!(1:max_id %in% x$id))

  # Fill missing data
  out <- data.frame()
  start <-  0
  counter <- 1

  for(i in 1:max_id){
    if(!i %in% no_data_position){
      out[start + i, "id"] <- start + i
      out[start + i, "value"] <- x$value[counter]
      counter <- counter + 1
    } else {
      out[start + i, "id"] <- start + i
      out[start + i, "value"] <- fill_value
    }
  }
  return(out)
}

f(x, NA)

   id     value
1   1  8.433865
2   2 10.459108
3   3  7.910928
4   4 13.988202
5   5        NA
6   6  7.948829
7   7 11.218573
8   8 11.845812
9   9 11.439453
10 10  9.236529


texts: Listing all paths through a matrix, stepping right or down only

texts:
I have the following method, which finds all the possible paths from the top left to bottom right of an N x M matrix. I was wondering what is the best way to optimize it for speed as it is a little slow right now. The resulting paths are then stored in a set.You can only move down or right to an adjacent spot, no diagonals from your current position.

    static public void printPaths (String tempString, int i, int j, int m, int n, char [][] arr, HashSet<String> palindrome) {
        String newString = tempString + arr[i][j];
        if (i == m -1 && j == n-1) {
            palindrome.add(newString);
            return;
        }
        //right
        if (j+1 < n) {
            printPaths (newString, i, j+1, m, n, arr, palindrome);
        }
        //down
        if (i+1 < m) {
            printPaths (newString, i+1, j, m, n, arr, palindrome);
        }
    }


texts:
I have implemented the heap sort algorithm which passes my tests. Although, I would like to ask you if I can improve anything from readability to performance.

import java.util.List;

/**
 * Created by Orestis on 17/07/2015
 */

public class HeapSort {

    public static <T extends Comparable<? super T>> void sort(List<T> list) {
        heapSort(list);
    }

    private static <T extends Comparable<? super T>> void heapSort(List<T> list){
        int heapSize = list.size() - 1;
        buildMaxHeap(list, heapSize);
        for (int i = list.size() - 1; i >= 1; i--) {
            exchange(0, i, list);
            heapSize--;
            maxHeapify(list, 0, heapSize);
        }
    }

    /**
     *
     * Maintain the max-heap property (list[find_parent(i)] >= list[i])
     * If the max-heap property is violated float down list[i]
     */
    private static <T extends Comparable<? super T>> void maxHeapify(List<T> list, int index, int heapSize) {
        int largest = index; // initialise largest to index.

        int left = findLeftIndex(index);
        int right = findRightIndex(index);

        if (left <= heapSize && list.get(left).compareTo(list.get(index)) >= 1) {
            largest = left;
        }

        if (right <= heapSize && list.get(right).compareTo(list.get(largest)) >= 1) {
            largest = right;
        }

        if (largest != index) {
            exchange(index, largest, list);
            maxHeapify(list, largest, heapSize);
        }
    }

    /**
     *
     * This function goes through the remaining nodes of the heap tree and
     * runs maxHeapify on each one.
     */
    private static <T extends Comparable<? super T>> void buildMaxHeap(List<T> list, int heapSize) {
        int start = (int) Math.floor((heapSize / 2));
        for (int i = start; i >= 0; i--) {
            maxHeapify(list, i, heapSize);
        }
    }

    private static <T extends Comparable<? super T>> void exchange(int i, int largest, List<T> array){
        T temp = array.get(largest);
        array.set(largest, array.get(i));
        array.set(i, temp);
    }

    private static int findParentIndex(int index) {
        return (index >> 1) ^ 1;
    }

    private static int findLeftIndex(int index) {
        return (index << 1) ^ 1;
    }

    private static int findRightIndex(int index) {
        return (index << 1) + 2;
    }
}


texts:
https://leetcode.com/problems/house-robber-iii/

Please review for performance

  The thief has found himself a new place for his thievery again. There
  is only one entrance to this area, called the "root." Besides the
  root, each house has one and only one parent house. After a tour, the
  smart thief realized that "all houses in this place forms a binary
  tree". It will automatically contact the police if two directly-linked
  houses were broken into on the same night.

  Determine the maximum amount of money the thief can rob tonight
  without alerting the police.

  Gilad's comment: In simple words - you can't take the sum of parent and child. they have to be no directly connected nodes.  for example Max(level 1 +3, level 2)

  Example 1:

Input: [3,2,3,null,3,null,1]

     3
    / \
   2   3
    \   \
     3   1

  Output: 7

  Explanation: Maximum amount of money the thief can rob = 3
  + 3 + 1 = 7.

  Example 2:

Input: [3,4,5,1,3,null,1]

     3
    / \
   4   5
  / \   \
 1   3   1

  Output: 9

  Explanation: Maximum amount of money the thief can rob = 4 +
  5 = 9.

using System;
using System.Collections.Generic;
using GraphsQuestions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TreeQuestions
{
    /// <summary>
    /// https://leetcode.com/problems/house-robber-iii/
    /// </summary>
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */

    [TestClass]
    public class HouseRobber3
    {
        [TestMethod]
        public void TestMethod1()
        {

            //     3              // level 1
            //    / \
            //    2   3           // level 2
            //    \   \
            //     3   1          // level 3
            TreeNode root = new TreeNode(3);
            root.left = new TreeNode(2);
            root.left.right = new TreeNode(3);
            root.right = new TreeNode(3);
            root.right.right = new TreeNode(1);
            //Output: 7
            //Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
            HouseRobber3Class robber = new HouseRobber3Class();
            Assert.AreEqual(7, robber.Rob(root));
        }
    }

    public class HouseRobber3Class
    {
        //we can't rob directly connected nodes.
        // so we can rob root which is level 1 + level 3
        // or only level 2 which is root.left+root.right
        // we need to find the max of the two
        public int Rob(TreeNode root)
        {
            return Helper(root, new Dictionary<TreeNode, int>());
        }

        public int Helper(TreeNode root, Dictionary<TreeNode, int> hash)
        {
            if (root == null)
            {
                return 0;
            }

            if (hash.ContainsKey(root))
            {
                return hash[root];
            }

            int sum = 0;
            if (root.left != null)
            {
                sum += Helper(root.left.left, hash) + Helper(root.left.right, hash);
            }

            if (root.right != null)
            {
                sum += Helper(root.right.left,hash) + Helper(root.right.right, hash);
            }

            sum =  Math.Max(root.val + sum, Helper(root.left, hash) + Helper(root.right, hash));
            hash.Add(root, sum);
            return sum;

        }
    }
}


texts:
Can someone look over this?  This is the entire authentication file.  It's referenced at the beginning of all my catalog editing files to make sure only the specified user is logged in.  I want to make sure it's secure.

<?php
//      pageauth.php
//      Asks for a username and a password and checks it.

if($_SESSION['logged_in'] != 1){
    if(!isset($_SERVER['PHP_AUTH_USER'])){
        header('WWW-Authenticate: Basic realm="Catalog Administration"');
        header('HTTP/1.0 401 Unauthorized');
        echo '
Hey! You can\'t be here!

Try logging in first!
';
        exit;
    }elseif(md5($_SERVER['PHP_AUTH_USER']) != "04b2f0a4ad7772ca864aa569917b2d2d"){
        echo '
Wrong Username!

Only the admin username and password are accepted.
';
    exit;
    }elseif(md5($_SERVER['PHP_AUTH_PW']) != "ed972411dfcca5313ab151694af01da8"){
        echo '
Wrong Password!

For obvious reasons, we need a correct password!
';
        exit;
    }else{
        session_start();
        $_SESSION['logged_in'] = 1;
    }
}
?>

I just need to authenticate for one user.  I don't need to go into databases or anything, so I thought PHP_AUTH would be a good solution.


texts:
I have this code that places a marker and on mouse-over this marker is scaled out and then back to the 'original' scale:

this.drawPerson = function () {
   self.svg.append("path")
        .attr("d", personPath)
        .attr("transform", "translate(100,100)scale(0.1)")
        .attr("class", "member")
        .style("fill", "steelblue")
        .on("mouseover", function(){
            d3.select(this).transition()
                .style("fill", "red")
                .attr("transform", "translate(100,100)scale(0.2)")
            })
        .on("mouseout", function() {
            d3.select(this).transition()
                .style("fill", "steelblue")
                .attr("transform", "translate(100,100)scale(0.1)")
            });
}

The x and y are the coordinates for the position on the canvas.

Here is the example: http://jsfiddle.net/SuTZR/8/


texts: Combine a set of “ranges” to find least number of ranges

texts:
Given a set of ranges with lower bound and upper bound, I need to combine these ranges to produce minimum number of ranges which will cover all the values in the original set and nothing else.

Example 1:

Input:

[100, 200], [120-170],[210 - 230]

Output:

[100-200], [210-230]

Example 2:

Input:

[100, 200], [120-210],[210 - 230]

Output:

[100-230]

This is what I have so far. This program works as expected and prints out the results as expected. But I'm wondering if there is any better way to do it.

public class Range {

        public static void main(String[] args) {

            int[][] Ranges1 = new int[][] {     new int[] { 94133, 94133 },
                                                new int[] { 94226, 94399 },
                                                new int[] { 94200, 94299 }

                                            };

            Range Range = new Range();

            int[][] normalizedRanges = Range.getTheNormalizedRanges(Ranges1);

            System.out.println("Normalized range:");
            Range.print(normalizedRanges);

        }

        private int[][]  getTheNormalizedRanges(int[][] RangesToNormalize) {

            int[][] newrange = new int[RangesToNormalize.length][];

            boolean[] rangePurged = new boolean[RangesToNormalize.length];

            int newIndex = 0;

            this.sortRange(RangesToNormalize);

                      for (int i = 0; i < RangesToNormalize.length; i++) {

                if (rangePurged[i])
                    continue;

                if (i == (RangesToNormalize.length - 1) ){
                    newrange[newIndex] = RangesToNormalize[i];
                    break;
                }

                int min = RangesToNormalize[i][0];
                int max = RangesToNormalize[i][1];

                int nextMin = RangesToNormalize[i + 1][0];
                int nextMax = RangesToNormalize[i + 1][1];

                if (max >= nextMin) {

                    if (max >= nextMax)
                        newrange[newIndex] = new int[] { min, max };
                    else
                        newrange[newIndex] = new int[] { min, nextMax };

                    //keep track of the ranges that are purged from the original list.

                    rangePurged[i+1] = true;

                } else {

                    //if there is no overlap, add the current range to the new list

                    newrange[newIndex] = RangesToNormalize[i];
                }

                newIndex++;

            }

            return newrange;

        }

        private void print(int[][] printRange){

            if(printRange == null || printRange.length <=0)
                throw new IllegalArgumentException("printRange is empty or null");

            for (int i = 0; i < printRange.length; i++) {

                if (printRange[i] != null)
                {

                    System.out.println("["+ printRange[i][0] + ", "
                            + printRange[i][1]+"]");
                }
            }
        }

        private void sortRange(int[][] rangeToSort){

            Arrays.sort(rangeToSort, new Comparator<int[]>() {

                @Override
                public int compare(int[] range1, int[] range2) {

                    int range1Min = range1[0];
                    int range2Min = range2[0];

                    if (range1Min > range2Min)
                        return 1;

                    if (range1Min < range2Min)
                        return -1;

                    return 0;
                }

            });
        }
    }


texts: Create an associative array with values as an array from an another associative array

texts:
I have a query that returns an array or associative arrays of ids and years. It looks like this:

[{"id":8,"year":1950},{"id":9,"year":1950},{"id":8,"year":1951},{"id":9,"year":1951},{"id":8,"year":1952}]

There is a fixed number of ids, but they each have many years associated. I wrote the following script to turn the results of the query into a more simple associative array with the ids as keys and an array of years as the value.

$rows = array("8" => array(),"9" => array());

foreach(array_keys($rows) as $key){
    foreach($results as $result){
        if ($key == $result['id']){
            array_push($rows[$key], $result['year']);
        }
    }
}

returns:

{"8":[1950,1951,1952],"9":[1950,1951]}

I would like to change this so that the initial $rows array is not hard-coded (ie get the keys from the initial query). I would also like to avoid the nested foreach/foreach/if nastiness as well if possible.


texts: Finding the first non-repeated character in a string

texts:
Given a string, find the first non-repeated character in it.

E.g. "yellow" should return "y"

There are several solutions for this in other languages but I haven't seen one written in Perl.

My main question is: can this be done in a more Perl-ish way or generally in an even shorter way?

use strict;
use warnings "all";

while (<DATA>)
{
    while (m,(.),g)
    {
        my $c = $1;
        if (s,$1,,g < 2)
        {
            print "$c\n";
            last;
        }
    }
}

__DATA__
yellow
tooth


texts: Using a Dictionary to retrieve BOOL values

texts:
I have some values pulled from a web service giving me a bool value for a key.  I did it this way, but feel like it is repeating without need.  How can I refactor this?

Please note that the key on the dictionary has the same name as the object property.

for (NSDictionary *itemDicto in switchesArray) {
    if ([[itemDicto objectForKey:@"id"]isEqualToString:@"FindBookMeeting"]) {
        currentAppswitches.FindBookMeeting = [[itemDicto objectForKey:@"value"]boolValue];
    }
    if ([[itemDicto objectForKey:@"id"]isEqualToString:@"FindCapabilityLocate"]) {
        currentAppswitches.FindCapabilityLocate = [[itemDicto objectForKey:@"value"]boolValue];
    }
    if ([[itemDicto objectForKey:@"id"]isEqualToString:@"FindDesk"]) {
        currentAppswitches.FindDesk = [[itemDicto objectForKey:@"value"]boolValue];
    }
    if ([[itemDicto objectForKey:@"id"]isEqualToString:@"FindEmployeeLocate"]) {
        currentAppswitches.FindEmployeeLocate = [[itemDicto objectForKey:@"value"]boolValue];
    }
    if ([[itemDicto objectForKey:@"id"]isEqualToString:@"Help"]) {
        currentAppswitches.Help = [[itemDicto objectForKey:@"value"]boolValue];
    }
    if ([[itemDicto objectForKey:@"id"]isEqualToString:@"ReportProblemIT"]) {
        currentAppswitches.ReportProblemIT = [[itemDicto objectForKey:@"value"]boolValue];
        //test ok
        //currentAppswitches.ReportProblemIT = NO;
    }
    if ([[itemDicto objectForKey:@"id"]isEqualToString:@"ReportProblemProperty"]) {
        currentAppswitches.ReportProblemProperty = [[itemDicto objectForKey:@"value"]boolValue];
    }
    if ([[itemDicto objectForKey:@"id"]isEqualToString:@"Settings"]) {
        currentAppswitches.Settings = [[itemDicto objectForKey:@"value"]boolValue];
    }
}


texts: Pushing a value to a Processor on a separate thread

texts:
I am attempting to use Room along with a full-text search (FTS) table. The issue is that Room doesn't recognize or support FTS tables. Though there is a work around for this; you just create the FTS table using execSQL() after the Room database is created. Though you are limited to using RawQuery on tables created this way (instead of Query, Insert, etc.). See my other question on implementing search with Room for more information.
This is works well, but it limits the table to a subset of the functionality normally provided by Room. For instance; you cannot use reactive queries with the RxJava artifact, you can only get a (blocking) result from the database.

So I am trying to mimic this functionality by adding it to my Dao.
To do this, I added a BehaviorProcessor that publishes a new Flowable for each access method. The Processor is then updated each time a value is inserted or deleted into the table.
The issue is that the RawQuery method is a blocking method, and throws an exception if accessed from the main thread. So I wrapped the read method in a Flowable that pushes the result to the Processor upon success. This works fairly well, but I am unsure if this is the best way to go about updating the Processor.
Is there a more "RxJava" way of achieving this effect? Possibly some method I failed to recognize in the Processor that allows me to update it away from the main thread without creating a separate Flowable?
@Dao
abstract class PlaceDao(private val database: MapDatabase) {

    private val processor = BehaviorProcessor.createDefault<List<Place>>(emptyList())

    @RawQuery
    protected abstract fun rawReadList(query: SupportSQLiteQuery): Place

    private fun refreshProcessor() {
        Flowable.create<List<Place>>({
            emitter.onNext(rawReadList(SimpleSQLiteQuery("SELECT * FROM `places`")))
        }, BackpressureStrategy.LATEST).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe({ places ->
            processor.onNext(places)
        }
    }

    fun readAll(): Flowable<List<Place>> {
        refreshProcessor()
        return processor.publish().autoConnect()
    }

    fun findById(): Flowable<Place> {
        refreshProcessor()
        return processor.map { it.first { it.id == id } }.publish().autoConnect()
    }

    fun insert(varargs places: Place) {
        val writableDatabase = database.openHelper.writableDatabase

        for (place in places) {
            writableDatabase.execSQL(
                "INSERT OR REPLACE INTO `places` " +
                    "(`id`, `title`, `subtitle`, `description`, " +
                    "`latitude`, `longitude`, `type`, `parent`) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
                arrayOf(place.id, place.title, place.subtitle, place.description,
                    place.location.latitude, place.location.longitude,
                    place.type, place.parent))
            )
        }

        refreshProcessor()
    }

    fun deleteAll() {
        val writableDatabase = database.openHelper.writableDatabase
        writableDatabase.execSQL("DELETE FROM `places`")
        refreshProcessor()
    }

}

Update
I found out that using a ConnectedFlowable is not the way to go. It seems as if the Flowable returned by publish() is not connected to the BehaviorProcessor and either does not emit the most recent item observed or does not emit any item pushed to the BehaviorSubject after publish() is called.
So I removed any calls to publish() and autoConnect() in my code and just return the BehaviorProcessor itself instead.


texts: Program to find the maximum number/factor in a list

texts:
I have written the following basic assembly program to find the maximum number in a list divisible by a number. Here is what I have thus far:
# Program: given a list or integers and a factor
# Find the max number in the list that the factor divides
# For example: INPUT: [3,9,50,27], factor: 3 | OUTPUT: 27
.section .rodata
nums:   .long 3,9,50,27,-1
factor: .long 3

.section .data
cur_value:  .long -1

# first three args: %edi, %esi, %edx
.section .text
.globl _start
_start:

    # SETUP
    # %r8 will store the array index
    # %r11 will store the max value
    # %esi will store the factor/divisor.
    mov $0,     %r10d
    mov $0,     %r11d
    mov factor, %esi

loop:

    # get current value and store it in %rdi
    # we'll also update our variable for `cur_value`
    mov nums(, %r10d, 4), %edi
    cmp $-1, %edi
    je exit
    movl %edi, cur_value

    # Call the function and increment the aray index
    call is_divisible_by
    inc %r10d

    # if it was NOT divisible (rax = False or 0) jump back to the beginning
    cmp $0, %rax
    je loop

    # if it was divisible, check to make sure it's larger than the current max
    cmp %r11d, cur_value
    jl loop
    mov cur_value, %r11d
    jmp loop
  exit:
    mov %r11d, %edi
    mov $60, %eax
    syscall

is_divisible_by:

    # Return 0 (false) if not divisible; 1 (true) if divisible

    # A (dividend, %eax) / B (divisor)
    # dividend needs to first be moved into eax
    mov %edi, %eax

    # divide by a register, immediate, or memory address
    # this is unsigned (positive), use idiv for signed
    div %esi

    # the resultant integer quotient goes in %eax, and the remainder goes in %edx
    # if %rdx is zero it means A is divisible by B: we don't care about %eax
    mov $0, %eax

    cmp $0, %edx
    jne end
    mov $1, %rax

  end:
    ret

It's compiled using:
$ as file.s -o file.o && ld file.o -o file
$ ./file; echo $?
# 27

Here are a few particular questions related to this:

Is it common to use named variables (such as cur_value in .section .data) or not? I use them while learning a bit so it's easier to view the value of an easily-rememberable entity, i.e., I can just do x &cur_value in gdb to see what it is.

What is the suggested way to handle an if statement. I've tried to do this in the is_divisible_by function -- setting it to $0 by default and then 'overwriting' it if the true condition is met. -- but this seems pretty hacky. I suppose another way to do it would be something like:
  cmp $0, %edx
  je set_true
set_false:
  mov $0, %eax
  jmp clean_up
set_true:
  mov $1, %eax
  jmp clean_up
clean_up:
  ret

Is it common to have end-labels on functions and such? I find myself often adding an end or whatever to be able to 'exit' things easily.

For labels within a main label (such as exit or end or set_true etc.), what is a good way to name these? I see gcc uses something like .L1029 but that seems not-too-friendly when writing my own and having to remember then.

Would any of the above be better done 'on the stack' rather than using registers or named variables? I find it a bit more difficult to use the stack than registers as you cannot do something like mov mem1, mem2

Finally, how could I extract the is_divisible_by function into another file and call it from within this main file.s file?


texts: making dynamic tables with JSON - angular2

texts:
Okay, so simple enough task - I'm using Ionic/Angular2 to develop an auditing app. Using angular I can map Objects into the DOM rather easily, so what I need to do is create an object - but I would like some suggestions or advice on how I can do this in a modular way.

The app displays a table of input fields with some information (excuse my ASCII art) depending on the table object.

 ______________________________________________________
| Tests   | Regulations    | itemOne     | itemTwo     |
|------------------------------------------------------|
| testOne | some regs here | <HTMLinput> | <HTMLinput> |
|------------------------------------------------------|
| testTwo | some regs here | <HTMLinput> | <HTMLinput> |
|------------------------------------------------------|
| etc     | etc            | <HTMLinput> | <HTMLinput> |
|______________________________________________________|

This was my first attempt:

table = [{
  title: 'First set of tests',
  time: '5:00 am',
  tests: [{
    title: 'testOne',
    regs: 'some regs here',
    type: 'X',
    items: {
      itemOne: true,
      itemTwo: true
    }
  },
  {
    title: 'testTwo',
    regs: 'some regs here',
    type: 'Y',
    items: {
      itemOne: false,
      itemTwo: true
    }
  }]
},
{
  etc
},
{
  etc
}];

I gave up on the first attempt since I would be left with a massive JSON object that I would have to edit manually in order to update it in the future.
In an attempt to make it a little more modular / easier to change / programatically update I tried modifying it like so:

/**
 * Configures tests
 * @param  {String} test  Name of test to configure
 * @param  {Array}  items Which items to test
 * @return {Object}       An object describing which items are enabled
 */
function configureTests(test, enabledItems) {

  // items object
  o = {};

  // loop through enabledItems and return an object describing which
  // items are to be tested with test
  for (var i = 0; i < enabledItems.length; i++) {
    var index = items.indexOf(enabledItems[i]);
    if (index > -1) o[items[index]] = true;
    else o[items[index]] = false;
  }

  // return tests object
  return {
    title: tests[test].title,
    regs: tests[test].regs,
    type: tests[test].type,
    items: o
  }
}

// items
items = [
  'itemOne',
  'itemTwo'
];

// tests
tests = {
  testOne: {
    title: "Test One",
    regs: "some regs here",
    type: "X"
  },
  testTwo: {
    title: "Test Two",
    regs: "some regs here",
    type: "Y"
  }
}

// configure table
table = [{
  title: 'First set of tests',
  time: '5:00 am',
  tests: [
    configureTests('testOne', items),
    configureTests('testTwo', ['itemTwo'])
  ]
},
{
  title: 'Second set of tests',
  time: '5:00 am',
  tests: [
    configureTests('testOne', items),
    configureTests('testTwo', ['itemTwo'])
  ]
},
{
  title: 'Third set of tests',
  time: '5:00 am',
  tests: [
    configureTests('testOne', items),
    configureTests('testTwo', items)
  ]
}];

I still think that there is an easier way to do this.

My end goal is to be able to add tests and items from within my app (like an editor) and for it to be rather simple in terms of programming and updating the existing table.

This is my first attempt at mapping such large objects to the DOM and using them to build tables, but any help/suggestions/criticisms/advice would be much appreciated!


texts:
I created a small library and an example application in Python for learning about facial recognition and experimenting with it. Right now though, I am loading a list of file names into memory and then allowing the code to iterate over those names in order to do comparisons or to find the faces.

I think my example and the library is pretty inefficient but I am unsure of how best to handle large amounts of photos using my library. Also, it only works on one photo at a time but my understanding is that the facial recognition library uses all cores and I should only look at one photo at a time.

How can I make my code more efficient?

The original facial recognition library I am using is found at https://github.com/ageitgey/face_recognition

My Library:

import face_recognition
import os, os.path
import ntpath
from PIL import Image

def get_image_directory(path):
  imgs = []
  valid_images = [".jpg",".gif",".png",".tga"]
  for f in os.listdir(path):
    ext = os.path.splitext(f)[1]
    if ext.lower() not in valid_images:
      continue
    imgs.append(os.path.join(path,f))
  return imgs

def recognize_faces(path):
  file_name = path_leaf(path)
  image = face_recognition.load_image_file(path)
  face_locations = face_recognition.face_locations(image)
  i = 0
  for face_location in face_locations:
    top, right, bottom, left = face_location
    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.save( 'output/' + str(file_name) + '_' +str(i)+".png")
    i+=1

def compare_faces(unknown_images, known_images):
  for image in unknown_images:
    unknown_comparison_image = create_face_comparison_encoding(image)
    for k_image in known_images:
      results = face_recognition.compare_faces([k_image],unknown_comparison_image)
      if results[0] == True:
        print("This person appears familiar!")
        print("Writing the faces to output folder!")
        recognize_faces(image)
      else:
        print("This doesn't appear to be familiar!")

def path_leaf(path):
  head, tail = ntpath.split(path)
  return tail or ntpath.basename(head)

def create_face_comparison_encoding(image):
  known_image = face_recognition.load_image_file(image)
  known_image_encoding = face_recognition.face_encodings(known_image)[0]
  return known_image_encoding

Application:

from ml_face import *

##
# The Program
##
unknown_persons = get_image_directory('images/') # Unknown Persons Directory
known_persons = get_image_directory('known_person/') # Known Persons Directory
known_list = [] # Empty List
for k_per in known_persons:
  known_comparison_image = create_face_comparison_encoding(k_per)
  known_list.append(known_comparison_image)
compare_faces(unknown_persons,known_list)


texts: Mixing in-class initialization and initializer lists

texts:
From this question, I do know that initialization list and in-class initialization are different things.

The class in the following code has members with in-class initializers and those initialized in initialization list in constructors:

class Bamboo {
public:
    int a = {0};
    int b;

    Bamboo() :a(3), b(12) {
        std::cout << a << ", " << b;
    }
};

int main() {
    Bamboo spikey; // outputs "3, 12"
}

This code compiles, but is it okay? Is it bad practice?

The only reason I am not using the initialization list for all members is that in the class I have many other members which are never initialized by constructor arguments. Plus, it's tedious to write the entire initialization list out in the constructor:

class Bamboo {
public:
    int a;
    std::string str;
    bool b;

    int c = {1}, d = {2}, e = {3}, f = {4}, g = {5};

    // each of c, d, e, f and g is always initialized to the same values regardless of which constructor is called
    Bamboo(bool cb)                           :a(1),  str("hello"), b(cb)    {}
    Bamboo(std::string cstr, int ca)          :a(ca), str(cstr),    b(false) {}
    Bamboo(int ca, bool cb, std::string cstr) :a(ca), str(cstr),    b(cb)    {}
}


texts: Colorized zsh prompt with support for Virtualenv and Git

texts:
I took the agnoster theme and re-wrote it to this:

CURRENT_FG='NONE'

# I don't know if this is important or not...
() {
   local LC_ALL="" LC_CTYPE="en_US.UTF-8"
}

# Begin a segment
# Takes two arguments, background and foreground.
prompt_segment() {
  echo -n "%{%K{$1}%}%{%F{$2}%} "
  CURRENT_FG=$2
  echo -n ${@:3}
}

# End the prompt, closing any open segments
prompt_end() {
  echo -n "%{%k%F{$CURRENT_FG}%}"
  echo -n "\$%{%f%}"
  CURRENT_FG=''
}

### Prompt components
# Each component will draw itself, and hide itself if no information needs to be shown

# Context: user@hostname (who am I and where am I)
prompt_context() {
  if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
    prompt_segment black default "%(!.%{%F{yellow}%}.)$USER@%m"
  fi
}

# Git: branch/detached head, dirty status
prompt_git() {
  (( $+commands[git] )) || return
  local PL_BRANCH_CHAR
  () {
    local LC_ALL="" LC_CTYPE="en_US.UTF-8"
    PL_BRANCH_CHAR=$'\ue0a0' # 
  }
  local ref

  if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
    ref='refs/heads/master' # $(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
    if command git status --porcelain 2> /dev/null | read -k1 -u0; then
      prompt_segment default yellow
    else
      prompt_segment default green
    fi

    setopt promptsubst
    # these are too slow
    #autoload -Uz vcs_info

    # these were too slow
    #zstyle ':vcs_info:*' enable git
    #zstyle ':vcs_info:*' get-revision true
    #zstyle ':vcs_info:*' check-for-changes true
    #zstyle ':vcs_info:*' stagedstr '†'
    #zstyle ':vcs_info:*' unstagedstr '*'
    #zstyle ':vcs_info:*' formats '%u%c'
    #zstyle ':vcs_info:*' actionformats '%u%c'
    #vcs_info
    echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }"
  fi
}

# Dir: current working directory
prompt_dir() {
  prompt_segment default cyan $(echo -n "${PWD/#$HOME/~}" | awk -F '/' '{ if (length($0) > 14) { if (NF > 4) print $1 "/" $2 "/.../" $(NF-1) "/" $NF; else if (NF > 3) print $1 "/" $2 "/.../" $NF; else if (NF > 2) print $1 "/.../" $NF; else print $0;} else print $0;}')
  #prompt_segment default cyan $(echo -n "${PWD/#$HOME/~}")
}

# Virtualenv: current working virtualenv
prompt_virtualenv() {
  local virtualenv_path="$VIRTUAL_ENV"
  if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
    prompt_segment blue black "(`basename $virtualenv_path`)"
  fi
}

# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
  ( [[ $(jobs -l | wc -l) -gt 0 ]] ) && prompt_segment blue black "◷"
}

## Main prompt
build_prompt() {
  RETVAL=$?
  prompt_status
  prompt_virtualenv
  prompt_context
  prompt_dir
  prompt_git
  prompt_end
}

PROMPT='%{%f%b%k%}$(build_prompt) '

How can I speed up this theme to make it as fast as possible?

I'd like to be able to hold down the enter key without gaps between lines and weird lag.


texts: Is there a better way to call the same method with different parameters?

texts:
I have a method(C#) which takes an XmlNodeList and a String and saves the xml data into a database. That code isn't the problem (at least for the moment), but because I have multiple XmlNodeLists, each needing a different String (the heading), I end up with code like

saveToDB(students, "Students");
saveToDB(teachers, "Teachers");
saveToDB(workers, "Workers");

...etc, there's about 8 of those in total. It all works okay, but I wonder if there's a better way to call the function, rather than doing it 8 times? I guess I could wrap all of the data into an object, but I'd still have to set it up, and pull the data out before and afterwards, so that's surely just moving the problem elsewhere? Or is there simply no real way around this, and the issue lies with how I've approached this method in the first place?

I look forward to your responses.


texts: Batching calls to server in python with asyncio

texts:
I've written this decorator to avoid too many requests to a backend. Did I get it right? Do I need some locks or can I assume some things are atomic / thread safe because this is asyncio?

def batch_calls(f):
    """Batches single call into one request.

    Turns `f`, a function that gets a `tuple` of independent requests, into a function
    that gets a single request.
    """
    queue = {}

    async def make_call():
        if not queue:
            return
        await asyncio.sleep(0.1)
        if not queue:
            return
        queue_copy = dict(queue)
        queue.clear()
        try:
            for async_result, result in zip(
                queue_copy.values(), await f(tuple(queue_copy))
            ):
                async_result.set_result(result)
        except Exception as exception:
            for async_result in queue_copy.values():
                async_result.set_exception(exception)

    async def wrapped(hashable_input):
        if hashable_input in queue:
            return await queue[hashable_input]
        async_result = asyncio.Future()
        # Check again because of context switch due to the creation of `asyncio.Future`.
        # TODO(uri): Make sure this is needed.
        if hashable_input in queue:
            return await queue[hashable_input]
        queue[hashable_input] = async_result
        if len(queue) == 1:
            asyncio.create_task(make_call())
        return await async_result

    return wrapped

The user might use it like so:

@batch_calls
async def call_server(texts: Tuple[Text, ...]):
    return tuple(
        (
            await httpx.post(
                url=SERVICE_URL,
                json={"texts": texts},
            )
        ).json()
    )

This code is causing a deadlock on some situations - not easily reproducible.


texts: Counting Bouncy Numbers: Project Euler #113

texts:
Problem statement:

  Working from left-to-right if no digit is exceeded by the digit to its
  left it is called an increasing number; for example, 134468.

  Similarly if no digit is exceeded by the digit to its right it is
  called a decreasing number; for example, 66420.

  We shall call a positive integer that is neither increasing nor
  decreasing a "bouncy" number; for example, 155349.

  As n increases, the proportion of bouncy numbers below n increases
  such that there are only 12951 numbers below one-million that are not
  bouncy and only 277032 non-bouncy numbers below \$10^{10}\$.

  How many numbers below a googol (\$10^{100}\$) are not bouncy?

Code:

import time
from scipy import special

def count_non_bouncy_numbers(n):
    '''n - number of digits'''
    include_zeroes = scipy.special.comb(10,n,exact=True,repetition=True) - 1
    exclude_zeroes = scipy.special.comb(9,n,exact=True,repetition=True)
    only_zeroes = include_zeroes - exclude_zeroes
    total = only_zeroes + exclude_zeroes*2 - 9
    return total

if __name__ == '__main__':
    total = 0
    for n in range(1,101) : total+= count_non_bouncy_numbers(n)
    print(total)

The reasoning behind the code:

The formula I should note here is the combinations with replacement, which is \$ {\frac{(q+r-1)!}{r!(q-1)!} }\$. I will denote it as \$C(q,r)\$ where \$q\$ represents number of items that can be selected and \$r\$ numbers of items that are selected.

My function takes an argument \$n\$, which represents number of digits, e.g:

If \$n =  2\$, then we consider all the numbers in range [10,99]

If \$n = 3 \$, then we consider all the numbers in range [100,999]

Step 1. We find all the non-bouncy numbers that contain zeroes. Specifically, for all numbers with \$n\$ digits, there will be \$C(10,n) - C(9,n)\$ non-bouncy numbers that contain zeroes in them

Step 2. We find number of strictly increasing non-bouncy  numbers with \$n\$ digits, there are \$C(9,n)\$ of them.

Step 3. We find number of strictly decreasing non-bouncy numbers with \$n\$ digits. Since decreasing numbers are just increasing numbers in reverse, we can see that there are \$C(9,n)\$ of them.

Step 4. We note that numbers such as 111, 33, 555 etc can be considered as strictly decreasing/increasing at the same time, hence we need to remove the overcount. To do so, we subtract \$9\$.

Step 5. Add up the results, i.e: \$C(10,n) - C(9,n) + C(9,n) - C(9,n) - 9\$ , which represents total number of non-bouncy numbers with \$n\$ digits.

What can be improved?


texts: TypeScript Express.js proxy server overall design and structure

texts:
I have built a Express.js proxy server using TypeScript to be able to hide my front-end API keys.
I designed it to be able to include multiple API services and to reject connections that are not from allowed domains.
It is working as expected, but I'm new to TypeScript and wanted to get some feedback, also on the overall design of the project.
This is app.ts:
import { Request, Response, NextFunction } from 'express';
import Debug from 'debug';
import dotenv = require('dotenv');
import proxies from './proxies';

dotenv.config();

import express = require('express');

const debug = Debug('express:server');
const app = express();

function notFound(req: Request, res: Response, next: NextFunction) {
  res.status(404);
  const error = new Error('Not Found');
  next(error);
}

function errorHandler(error: Error, req: Request, res: Response) {
  res.status(res.statusCode || 500);
  res.json({
    message: error.message,
  });
}

proxies.forEach((proxy) => app.use(proxy));
app.use(notFound);
app.use(errorHandler);

const port = process.env.PORT || 5000;
app.listen(port, () => debug(`Listening on port ${port}`));

This is proxies.ts which is responsible for creating the proxies using the http-proxy-middleware and the proxies defined on the config.ts file:
import querystring, { ParsedUrlQueryInput } from 'querystring';
import { Request } from 'express';
import { createProxyMiddleware, Options, Filter } from 'http-proxy-middleware';
import config from './config';

const { allowedDomains: globalAllowedDomains = [], proxies } = config;

export default proxies.map(
  ({
    route,
    target,
    allowedDomains = [],
    allowedMethods = ['GET'],
    queryparams = {},
    headers = {},
    auth,
  }) => {
    const filter: Filter = (pathname: string, req: Request) => {
      if (typeof req.headers.origin === 'string') {
        return (
          pathname.startsWith(route) &&
          allowedMethods.includes(req.method) &&
          [...globalAllowedDomains, ...allowedDomains].includes(
            req.headers.origin
          )
        );
      }
      return false;
    };
    const options: Options = {
      target,
      changeOrigin: true,
      headers,
      auth,
      pathRewrite(path: string, req: Request) {
        const qp = querystring.stringify({
          ...req.query,
          ...queryparams,
        } as ParsedUrlQueryInput);
        const newPath = `${path.split('?')[0].replace(route, '')}?${qp}`;
        return newPath;
      },
      logLevel: process.env.NODE_ENV === 'development' ? 'debug' : 'info',
    };

    return createProxyMiddleware(filter, options);
  }
);

And this is config.ts which is a configuration file where I can enable multiple API services:
import { Options } from 'http-proxy-middleware';
import { ParsedUrlQueryInput } from 'querystring';

export interface Proxy extends Options {
  route: string;
  allowedMethods: string[];
  queryparams?: ParsedUrlQueryInput;
  allowedDomains?: string[];
}

export interface Config {
  allowedDomains: string[];
  proxies: Proxy[];
}

const config: Config = {
  allowedDomains:
    process.env.NODE_ENV === 'production'
      ? ['https://www.mauriciorobayo.com']
      : ['http://localhost:8080'],
  proxies: [
    {
      route: '/weather',
      allowedMethods: ['GET'],
      target: 'https://api.openweathermap.org/data/2.5/weather',
      queryparams: {
        appid: process.env.WEATHER_API_KEY,
      },
    },
    {
      route: '/ipinfo',
      allowedMethods: ['GET'],
      target: 'https://ipinfo.io/',
      queryparams: {
        token: process.env.IPINFO_TOKEN,
      },
    },
    {
      route: '/github',
      allowedMethods: ['GET'],
      target: 'https://api.github.com',
      headers: {
        Accept: 'application/vnd.github.v3+json',
        Authorization: `Token ${process.env.GITHUB_TOKEN}`,
      },
    },
  ],
};

export default config;