texts:
I've been trying to build an ORM. I started with the connector layer, which is responsible for the database server connections and executes raw SQL as the code below shows.
I have built the _dbconnect decorator to separate database connection procedures from the CRUD functions. This will be responsible for SQL conversions only, maintaining the single responsibility principle.
Is the idea of this decorator good or not since it doesn't return any thing?
In the future the create_record(), delete_record() or read_record() functions will need some sort of values to be returned. The result is also going to be different for all of them because when creating a record you will need to return the id only which is supposed to be an int, but selecting record will be a list of records in predefined data structure.
So is it OK for the decorator to be returning different types like mentioned above, and what are the better ways to handle it ?
from abstract_connector import AbstractConnector

import psycopg2

class PsqlConnector(AbstractConnector):
    def __init__(self, username: str, password: str, dbname: str):
        self.username = username
        self.password = password
        self.dbname = dbname

    def _dbconnect(func):
        def wrapper(self, *args, **kwargs):
            query = func(self, *args, **kwargs)
            conn = psycopg2.connect("dbname=%s user=%s, password=%s" % (self.dbname, self.username, self.password))
            cur = conn.cursor()
            cur.execute(query)
            conn.commit()
            cur.close()
            conn.close()

        return wrapper

    @_dbconnect
    def create_table(self, name: str):
        return "CREATE TABLE %s(id serial PRIMARY KEY, name VARCHAR(100));" % name

    @_dbconnect
    def drop_table(self, name: str):
        return "DROP TABLE IF EXISTS %s" % name


texts:
Please review code quality and give me shortcuts or optimization tips as well as corrections.

<!DOCTYPE html>

<meta charset="utf-8">
<title>Clean HTML Code</title>

<textarea id="code" cols="80" rows="15" autofocus></textarea>

<input id="comments" type="checkbox" checked> Remove All HTML Comments

<input id="head" type="checkbox"> Remove Head Tags

<input id="style" type="checkbox"> Remove Style Tags

<input id="script" type="checkbox"> Remove All Script Tags (also removes NoScript Tags)

<button id="btn">Clean Up HTML</button>


texts: Web-scraping through a rotating proxy script

texts:
I've created a script in python which is able to parse proxies (supposed to support "https") from a website. Then the script will use those proxies randomly to parse the title of different coffe shops from a website. With every new request, the script is supposed to use new proxies. I've tried my best to make it flawless. The scraper is doing fine at this moment.

I'll be happy to shake off any redundancy within my script (I meant DRY) or to bring about any change to make it better.

This is the complete approach:

import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
from random import choice

links = ['https://www.yellowpages.com/search?search_terms=Coffee%20Shops&geo_location_terms=Los%20Angeles%2C%20CA&page={}'.format(page) for page in range(1,6)]

def get_proxies():
    link = 'https://www.sslproxies.org/'
    response = requests.get(link)
    soup = BeautifulSoup(response.text,"lxml")
    proxies = [':'.join([item.select_one("td").text,item.select_one("td:nth-of-type(2)").text]) for item in soup.select("table.table tr") if "yes" in item.text]
    return proxies  #producing list of proxies that supports "https"

def check_proxy(session, proxy_list=get_proxies(), validated=False):
    proxy = choice(proxy_list)
    session.proxies = {'https': 'https://{}'.format(proxy)}
    try:
        print(session.get('https://httpbin.org/ip').json())
        validated = True  #try to make sure it is a working proxy
        return
    except Exception: pass

    while True:
        proxy = choice(proxy_list)
        session.proxies = {'https': 'https://{}'.format(proxy)}
        if not validated:  #otherwise get back to ensure it does fetch a working proxy
            print("-------go validate--------")
            return

def parse_content(url):
    ua = UserAgent()
    session = requests.Session()
    session.headers = {'User-Agent': ua.random}
    check_proxy(session)  #collect a working proxy to be used to fetch a valid response

    while True:
        try:
            response = session.get(url)
            break     #as soon as it fetch a valid response, it will break out of the while loop to continue with the rest
        except Exception as e:
            session.headers = {'User-Agent': ua.random}
            check_proxy(session)  #if exception is raised, start over again
            parse_content(url)

    soup = BeautifulSoup(response.text, 'lxml')
    for items in soup.select(".info span[itemprop='name']"):
        print(items.text)

if __name__ == '__main__':
    for link in links:
        parse_content(link)


texts:
I have a long text (about 6,000,000 chars) and a short text (about 6,000 chars).  The long text contains the short text, but not exactly - a small number of words are missing, abbreviated or just erroneous. So I want to do an approximate search.

There are several Python libraries that do fuzzy string matching. For example, FuzzySet accepts a list of candidates and a string and returns the candidate that is most similar to the string. However, here I do not have a list of candidates - I have just a very long text.

A naive solution is to take, as the list of candidates, all substrings of the long text. However, the number of candidates will be huge. So, now I use make several heuristic assumptions:

I assume that the first several characters at the start and end of the short text are not erroneous in the long text.
I assume that the length of the best match of the short text inside the long text is at least half and at most twice the length of the short text.

These assumptions allow me to reduce the number of candidates dramatically, so all of them can be checked in a reasonable time. Here is the current solution:

import re
from fuzzyset import FuzzySet

def fuzzyFind(needle:str, haystack:str, needleStart:str, needleEnd:str)->str:
    """
    needle: short text to look for.
    haystack: long text to look in.
    needleStart: first few chars in needle,
    needleEnd  : last few chars in needle - assumed to exist verbatim in haystack.

    returns a subset of haystack that is a best match for needle.
    """
    minPossibleLength = len(needle)//2
    maxPossibleLength = len(needle)*2
    possibleStarts = [m.start() for m in re.finditer(needleStart, haystack)]
    possibleEnds =   [m.end()   for m in re.finditer(needleEnd,   haystack)]
    possibleMatches = FuzzySet()
    for iStart in possibleStarts:
        for iEnd in possibleEnds:
            possibleLength = iEnd-iStart
            if minPossibleLength <= possibleLength and possibleLength <= maxPossibleLength:
                possibleMatch = haystack[iStart:iEnd]
                print("possible match from {} to {}".format(iStart,iEnd))
                possibleMatches.add(possibleMatch)
    matches = possibleMatches.get(needle)
    bestMatch = matches[0]
    bestMatchText = bestMatch[1]
    return bestMatchText

This heuristic solution is quick and quite accurate in practice, but of course it is not guaranteed to work. I would like to get rid of the assumptions, especically assumption #1. Is there a quick solution that does not need to assume anything about the first and last characters of the needle?

Note: before writing this function I tried to use the fuzzysearch library. However, this function requires an upper bound on the Levenshtein distance between the short text and its variant inside long text. Even with modest upper bounds (e.g, 60-70) it becomes quite slow.


texts:
This code validates the following:

enddate should not be less than or equal to startdate.
startdate should not be less than or equal to the date today unless document.getElementById("ltype").value == "1".
The maximum gap between startdate and enddate is one year, more than that should cause an error alert.

Please review my code. I think I covered all test cases, but I am extremely new to JavaScript, so this code might have bugs. And is it fine to compare dates as strings? I did this because as far as I know HTML5 date is a string.

// creates a date object then converts it to a string with yyyy-mm-dd format
function dateFormat(date, addyear) { // addyear is boolean, true means add another year to the date
    var dd = date.getDate();
    var mm = date.getMonth()+1;
    var yyyy = date.getFullYear();
    if (addyear) {
        yyyy++;
    }
    if(dd<10) {
        dd='0'+dd;
    }
    if(mm<10) {
        mm='0'+mm;
    }
    return yyyy+'-'+mm+'-'+dd;
}

function date_compare() {
    var today = dateFormat(new Date(),false);
    // from and to are html5 date fields
    var d1=document.getElementById("from").value;
    var d2=document.getElementById("to").value;
    var startdate = dateFormat(new Date(d1),false);
    var enddate = dateFormat(new Date(d2),false);
    var yeardate = dateFormat(new Date(d1),true);
    if (enddate <= startdate || enddate > yeardate) {
        alert("Dates out of range");
        return false;
    }
    if (document.getElementById("ltype").value != "1" && startdate <= today) {
        alert("Error! date goes backwards!");
        return false;
    }
    return true
}


texts: Writing a chunk of a file to a socket in Swift

texts:
I'm writing a chunk of a file from an offset to a socket.

This is my implementation and it's working OK from my tests.

Any comments or speed improvements?

private func sendChunk(_ source: Int32, _ target: Int32, _ offset: off_t, _ count: UInt64) -> Int64 {
    let bufferSize:Int = 1024
    var buffer = [UInt8](repeating: 0, count: bufferSize)
    var writed:UInt64 = 0
    var read:Int = 0
    while true {
        let remaining = Int(count - writed)
        if remaining < bufferSize {
            read = pread(source, &buffer, remaining, offset + Int64(writed))
        } else {
            read = pread(source, &buffer, buffer.count, offset + Int64(writed))
        }
        guard read > 0 else {return Int64(read)}
        var writeCounter = 0
        while writeCounter < read, writed < count {
        let writeResult = write(target, &buffer + writeCounter, read - writeCounter)
            guard writeResult > 0 else {return Int64(writeResult)}
            writeCounter = writeCounter + writeResult
            writed += UInt64(writeCounter)
        }
    }
}


texts: Python script to add unittest decorators to source code files

texts:
This is a simple Python script to add @unittest.skipUnless() decorators in a specified format above every function starting with test_. I want to simplify this and try and improve it. It runs extremely quickly and does the job right now, but I'm wondering if this is best practice for modifying Python files.

Do Python developers normally just search for a regex using the IDE's search function, and then modify the search results? Is it a good idea to create a script to modify files if you're going to be repeating a code snippet a lot? How could I improve this code?

import fileinput
import re
import sys

def decorate(file):
    '''
    Goes through a file and adds @unittest.skipUnless decorators \n
    arg file -- the relative path to the file from this root directory, e.g. tests/hardware/test_device
    '''

    if file is not str:
        file = str(file)

    pattern = "def test_" # the pattern that defines a test function
    template = lambda x: fr'@unittest.skipUnless((not to_test or "{x}" in to_test) and "{x}" not in to_skip, "\n**SKIPPED: {x} test**\n")'

    for line in fileinput.input(file, inplace = True):
        if pattern in line:
            if re.search(fr"{pattern}(\d\d)", line) is not None: # if there are test function numbers (e.g. test_01)
                func_name = re.search(r"def test_\d\d_(.*)\(", line).groups()[0]
            else: # if there are not test function numbers
                func_name = re.search(r"def test_(.*)\(", line).groups()[0]
            print(f'    {template(func_name)}')
        print(line, end="")

    print(f"\nThe file {file} has been decorated by unittest.skipUnless decorators")

# get the filepath from the command line, then run decorate() on that file
if len(sys.argv) <= 1:
    print("\nERROR: Need one more command line argument (relative path to file)\n")
elif len(sys.argv) > 2:
    print("\nERROR: Too many command line arguments, should only be one (relative path to file)\n")
else:
    file_in = sys.argv[1]
    decorate(file_in)


texts: Haskell test performance for different container types

texts:
I want to test the performance for various types of containers in Haskell. So far I got Vector and List.  My goal is to learn how to use typeclass, how to use containers etc. Could anyone point out suggestions of below code?
module Lib where

import           Data.Array
import           Data.List
import qualified Data.Vector.Algorithms.Radix as R
import qualified Data.Vector.Unboxed          as V
import           System.CPUTime
import           System.Random
import           Text.Printf

class TestSTL t where
  stlGenerate ::(Random a, V.Unbox a) => Int -> IO (t a) --generate random number of t a
  stlSort :: (Ord a, V.Unbox a, R.Radix a) => t a -> t a
  stlSearchIndex :: (Ord a, V.Unbox a) => a -> t a -> Maybe Int

instance TestSTL [] where
  stlGenerate i = take i . randoms <$> getStdGen
  stlSort l = sort l
  stlSearchIndex = elemIndex

instance TestSTL V.Vector where
  stlGenerate i = V.replicateM i randomIO
  stlSort = V.modify R.sort
  stlSearchIndex = V.elemIndex
-- instance (Ix ix) => TestSTL (Array ix) where
--   stlGenerate i = take i . randoms <$> getStdGen
--   stlSort l = sort l
--   stlSearchIndex = elemIndex

time :: IO a -> IO a
time action = do
  t0 <- getCPUTime
  r <- action
  t1 <- getCPUTime
  let tm :: Double
      tm = fromInteger (t1 - t0) * 1e-9
  printf "%.3f ms\n" tm
  return r

test :: IO ()
test = do
  -- preparation
  putStrLn "Please input an Int:"
  e <- readLn :: IO Int

  -- test list
  putStrLn "Testing List:"
  r <- time
    $ stlSearchIndex e . stlSort
    <$> (stlGenerate 1000000 :: IO [Int])

  case r of
    Just i  -> putStrLn $ "Found at " ++ show i
    Nothing -> putStrLn "Not found"

  -- test vector
  putStrLn "Testing Vector:"

  r' <- time
    $ stlSearchIndex e . stlSort
    <$> (stlGenerate 1000000 :: IO (V.Vector Int))

  case r' of
    Just i  -> putStrLn $ "Found at " ++ show i
    Nothing -> putStrLn "Not found"


texts:
I know there are some answers about a user function to split chars and I have one already running (found one years ago in the net and modified it to my own needs).

Since I use this function very often, I'd like to ask the best SQL professionals (which are probably here) to review this function for performance. Is the function ok or is there a faster or better way to do this now? Minimum requirements are SQL Server 2005 to work, but if there are better ways from 2008 I can roll out two versions (most usage of the DB is 2008, only a few 2005 left).

CREATE FUNCTION [dbo].[fn_Split](@text nvarchar(4000), @delimiter char(1) = ',')

RETURNS @Strings TABLE
(
position int IDENTITY PRIMARY KEY,
value nvarchar(4000)
)

AS
BEGIN

DECLARE @index int
SET @index = -1

SET @text = RTRIM(LTRIM(@text))

WHILE (LEN(@text) > 0)
BEGIN
  SET @index = CHARINDEX(@delimiter , @text)
  IF (@index = 0) AND (LEN(@text) > 0)
  BEGIN
    INSERT INTO @Strings VALUES (@text)
    BREAK
    END
    IF (@index > 1)
    BEGIN
    INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))
    SET @text = RIGHT(@text, (LEN(@text) - @index))
  END
  ELSE
    SET @text = RIGHT(@text, (LEN(@text) - @index))
  END
  RETURN
END


texts: Creating some Loaded Dice to make our board game fairer

texts:
Some friends and I were playing a board game and decided that the random dice rolls just weren't fair! So we came up with a scheme for making the rolls slightly more evenly distributed, and I implemented it in python. The class represents 2 six sided dice and makes the probability of a given combination coming up (there are 36 options for 2 dice) inversely proportional to the number of times it has come up previously - thereby ensuring the distribution tends to be more even than randomness allows:
from collections import Counter
from typing import Tuple, Counter as TCounter
import random

class LoadedDice:
    """
    Simulation of 2 Dice being rolled.
    Weights the random roll to try and tend to an even distribution
    """

    def __init__(self, sides: int = 6) -> None:
        self.sides = sides
        self.history: TCounter[int] = Counter(range(sides ** 2))

    def roll(self) -> Tuple[int, int]:
        result_index = self.get_weighted_random_roll_index()
        self.history[result_index] += 1
        return self.roll_value_from_index(result_index)

    def get_weighted_random_roll_index(self) -> int:
        roll_threshold = random.random()
        reciprocals_total = sum(self.reciprocals())
        running_total = 0
        for result_index, value in enumerate(self.reciprocals()):
            running_total += value / reciprocals_total
            if roll_threshold <= running_total:
                return result_index

    def roll_value_from_index(self, index: int) -> Tuple[int, int]:
        mod, remainder = divmod(index, self.sides)
        roll_1 = mod + 1
        roll_2 = remainder + 1
        return roll_1, roll_2

    def reciprocals(self):
        for v in self.history.values():
            yield 1 / v

    @property
    def roll_history(self) -> TCounter[Tuple[int, int]]:
        result: TCounter[Tuple[int, int]] = Counter()
        for roll_index, count in self.history.items():
            result[self.roll_value_from_index(roll_index)] += count
        return result

    @property
    def result_history(self) -> TCounter[int]:
        result: TCounter[int] = Counter()
        for roll_index, count in self.history.items():
            result[sum(self.roll_value_from_index(roll_index))] += count
        return result

    def __repr__(self):
        return repr(self.roll_history)

... which can be used like this:
especially_fair_dice = LoadedDice(6)  # simulate 2 6-sided dice
especially_fair_dice.roll()  # returns the dice values results like (1,5)

Feedback
I'd like some review on types, as I've never done them before, efficiency of my algorithm; I feel like it's a bit overly complicated, anything I've missed about pseudo-random numbers that make my approach undesirable etc.
Results
I plotted some of the results, for 100 rolls, the loaded die approach tends to perform better than random at keeping to the expected distribution, while still feeling random (that's the goal, interpret it as you wish, I don't know how to quantify it either:)


texts: Return the correct object type in Javascript

texts:
I am currently wondering if the following piece of Javascript code could be simplified? I would like to return the correct {},[],"" without having to pass a type as argument. Is it possible?

function getObjectProperty(object,field,type){
    switch (type){
      case "Array":
        return object && object.hasOwnProperty(field) ? object[field] : [];
        break;
      case "String":
        return object && object.hasOwnProperty(field) ? object[field] : "";
        break;
      case "Object":
        return object && object.hasOwnProperty(field) ? object[field] : {};
        break;
      case "Boolean":
        return object && object.hasOwnProperty(field) ? object[field] : false;
        break;
    }
  }


texts: Compensating for typos in strings by removing spaces and making uppercase for comparison

texts:
I'm working with a database that has typos in the values. To compensate for them I've made a function called standardizer() which removes spaces and converts all letters to uppercase. I do this so the value red in from the database can correctly be interpreted by the program. Also in values starting with the prefix 'sp_etc_', I noticed a common mistake that the 't' is left out, giving 'sp_ec_' so I compensate for this as well. Bellow is an example:

import sys

#remove all spaces and convert to uppercase, for comparision purposes
def standardizer(str):
    str = str.replace("sp_ec_", "sp_etc_")#this is  a common typo in the db, the 't' is left out
    str = str.replace(" ", "")
    str = str.upper()
    return str

#this function is for testing purposes and would actually read in values from db
def getUserInput():
    return ' this  is a test'

def main():
    str1 = "I'mok"
    str2 = 'this is a test'
    str3 = 'two spaces'
    str4 = ' spaces at front and end '
    str5 = 'sp_ec_blah'
    print(str1, standardizer(str1))
    print(str2, standardizer(str2))
    print(str3, standardizer(str3))
    print(str4, standardizer(str4))
    print(str5, standardizer(str5))
    #this is an example of how the function would actually be used
    print(standardizer(str2), standardizer(getUserInput()))
    if standardizer(str2) == standardizer(getUserInput()):
        print('matched')
    else:
        print('not a match')

if __name__ == '__main__':
    main()

Any suggestions on the standardizer() function? First off I think it needs a better name. I'm wondering if I should break it into two functions, one being for the missing 't' and the other being for removing spaces and making upper case (by the way, from what I've seen it's more common to convert everything to upper case than lower case, for comparison purposes). Also, how would you comment something like this?


texts: Python function to find all integers between two numbers whose sum of squared divisors is a perfect square

texts:
Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a square!
Given two integers m, n (1 <= m <= n) we want to find all integers between m and n whose sum of squared divisors is itself a square. 42 is such a number.
The result will be an array of arrays, each subarray having two elements, first the number whose squared divisors is a square and then the sum of the squared divisors.

I have a working function as follows, however it does not execute fast enough.
from math import sqrt
def list_squared(m, n):
    def D(x):return sum(i**2 for i in range(1,x+1) if not x%i)
    return [[i,D(i)] for i in range(m,n) if sqrt(D(i)).is_integer()]

I have also tried a for loop instead of list comprehension, using a variable to save the output of the D function so it doesn't get called twice, and it didn't help. I have no experience optimizing code for speed.
from math import sqrt
def list_squared(m, n):
    def D(x):return sum(i**2 for i in range(1,x+1) if not x%i)
    z = []
    for i in range(m,n):
        x = D(i)
        if sqrt(x).is_integer():
            z.append([i,x])
    return z

#Examples
list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
list_squared(42, 250) --> [[42, 2500], [246, 84100]]


texts: Get Rectangle Count from Given List of Co-ordinates

texts:
Problem Description

  Write a program to find the maximum number of rectangles that can be
  formed both horizontal and vertical with the help of a given n number
  of coordinate(x,y) points. [x,y points will always be positive].

  The method getRectangleCount takes 1 input parameter which is an int
  array called coOrdinates.

  The method getRectangleCount takes the coordinates as a 2D integer
  array and returns the maximum number of rectangles as an integer.

public int getRectangleCount(int[][] coOrdinates)

  There is no need to count the rectangles that can be created from the
  overlapping rectangles.

Example

  Input : int coordinates[][] = { {1,1}, {7,1}, {1,4}, {1,5}, {7,4}, {7,5} }

  Output : Returns 3

  Explanation:

  Three rectangles can be formed.

  First -

    (1,4)----(7,4)
     |         |
     |         |
    (1,1)----(7,1)

  Second -

    (1,5)----(7,5)
     |         |
     |         |
    (1,1)----(7,1)

  Third -

    (1,5)----(7,5)
     |         |
     |         |
    (1,4)----(7,4)

My Concerns

Is it readable? Is it maintainable? I intentionally didn't comment in the code because I want to see if the code is self-explanatory or not.
for inside for inside if inside.... you get the picture... Is it nasty?
Can it be optimized, as it is from a programming challenge, time is a factor.

My Solution

You can browse the GitHub Project for more info.

public int getRectangleCount(int [][]coOrdinates) {

    // exception handling part coded here... forget about it

    final int MIN_POINTS_TO_CREATE_RECTANGLE = 4;

    // can't make a rectangle with less than 4 points
    if(coOrdinates.length < MIN_POINTS_TO_CREATE_RECTANGLE)
        return 0;

    final Point []points = new Point[coOrdinates.length];

    for(int i = 0; i < coOrdinates.length; i++)
        points[i] = new Point(coOrdinates[i][0], coOrdinates[i][1]);

    Arrays.sort(points); // points are sorted like (1,1) (1,3) (3,1) (3,3)....

    int rectangleCount = 0;
    for(int i = 0; i < points.length; i++) {
        Point leftDown = points[i];

        for(int j = i+1; j < points.length; j++) {
            Point leftUp = points[j];

            if(leftDown.getX() == leftUp.getX()) {

                for(int k = j+1; k < points.length; k++) {
                    Point rightDown = points[k];
                    Point probableRightUp = new Point(rightDown.getX(), leftUp.getY());

                    if((leftDown.getY() == rightDown.getY())
                                &&
                        probableRightUp.existsIn(points)) {

                        rectangleCount++;

                    }

                }

            }

        }

    }
    return rectangleCount;
}

I also have a custom Point class that implements Comparable<Point>. Point overrides compareTo() method like following

@Override
public int compareTo(Point p) {
    if(Integer.compare(this.getX(),p.getX()) == 0)
    {
        return Integer.compare(this.getY(), p.getY());
    }
    return Integer.compare(this.getX(),p.getX());
}

and method existsIn(Point []graph) works like as you think. It returns true if this exists in the graph else false.

Yes I will create a Points collection class later and will move existsIn(Point []graph). I will also change the signature to

public static boolean existsIn(Point []graph, Point toCheck) // Promise I will ;)


texts:
How could this VHDL counter and its test bench be improved?  I am interested in anything you see that could be done better, but especially in the test bench:

Is wait for 10 ns better or worse than any other time delay?
The test is very minimal.  Should it do more?

counter32.vhdl:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity counter32 is
  port (
    clk: in std_ulogic;
    ena: in std_ulogic;
    rst: in std_ulogic;
    q  : out std_ulogic_vector(31 downto 0));
end entity;

architecture rtl of counter32 is

  signal count : unsigned(31 downto 0);

begin

  process(clk, rst)
  begin
    if rst = '1' then
      count <= X"00000000";
    elsif rising_edge(clk) then
      if ena = '1' then
        count <= count + 1;
      end if;
    end if;
  end process;

  q <= std_ulogic_vector(count);

end architecture;

counter32_tb.vhdl:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity counter32_tb is
end counter32_tb;

architecture tb of counter32_tb is

  component counter32
    port (
      clk: in std_ulogic;
      ena: in std_ulogic;
      rst: in std_ulogic;
      q  : out std_ulogic_vector(31 downto 0));
  end component;

  signal clk: std_ulogic;
  signal ena: std_ulogic;
  signal rst: std_ulogic;
  signal q  : std_ulogic_vector(31 downto 0);

begin

  dut : counter32 port map (
    clk => clk,
    ena => ena,
    rst => rst,
    q => q);

  process
  begin

    -- Reset
    clk <= '0';
    ena <= '1';
    rst <= '1';
    wait for 10 ns;
    rst <= '0';
    wait for 10 ns;

    -- Counts on clock leading edge if enabled
    clk <= '1';
    wait for 10 ns;
    assert q = x"00000001";

    -- Does not count on clock trailing edge
    clk <= '0';
    wait for 10 ns;
    assert q = x"00000001";

    -- Does not count when not enabled
    ena <= '0';
    clk <= '1';
    wait for 10 ns;
    assert q = x"00000001";

    -- Clears on reset
    rst <= '1';
    wait for 10 ns;
    assert q = x"00000000";

    wait;

  end process;

end tb;

Shell script to run the test bench in GHDL:

OPTS=--workdir=build
ghdl -a ${OPTS} *.vhdl
ghdl -e ${OPTS} counter32_tb
ghdl -r ${OPTS} counter32_tb --vcd=counter32.vcd


texts: Changing desktop background to the next available one

texts:
How could I improve this working Perl script, which changes my desktop background to the next available one, without opening a terminal? Specifically, how could I iterate over my .fehbg and walls/ lazily, without reading them into arrays?  I couldn't find a way to do that; I need the for loops.

Other than that, are there any inefficiencies in my program, which could be spotted by a more experienced programmer?

#!/usr/bin/perl
# A background switching script which can be run from dmenu

use 5.18.2;
use strict;
use warnings;
use File::Basename;

my @fehbg = ();
my @files = ();
open 'FEHFH', '</home/chummer/.fehbg';
while(<FEHFH>){
    @fehbg = split/'/;
}
close FEHFH;
my $current = basename($fehbg[-2]);
opendir WALLSDIR, '/home/chummer/pictures/walls';
foreach(readdir WALLSDIR){
    unless($_ eq '.' || $_ eq '..'){
    push(@files, $_);
}
}
closedir WALLSDIR;
for(my $i = 0; $i <= $#files; ++$i){
    if($current eq $files[$i]){
    if(exists($files[$i+1])){
        foreach($files[$i+1]){ s/\s+/\\ /g; }
    system"feh --bg-fill /home/chummer/pictures/walls/$files[$i+1]";
    last;
    }
    else{
        foreach($files[0]){ s/\s+/\\ /g; }
    system"feh --bg-fill /home/chummer/pictures/walls/$files[0]";
    last;
    }
}
}


texts: Recursively find TypeScript files imported/exported from an entry point

texts:
I'd like to know what's the best practice as functional programming in JavaScript to work with arrays and passing them as an argument to a recursive function that may run itself within its branch.

In my code I'm trying to recursively find all typescript files that are imported/exported from an entry point by calling preprocessTSFiles. To do this, I read the entry and crawl to all other files. This leads to a recursive call of getChildren and I don't want to call handleFile twice on the same file.

I tried this approach but I wanted an insight of expert in functional programming (please note that I'm not very familiar with functional programming and bear with me):

function copyFile(file: VinylFile, destFilePath: string): void {
  const destDir = path.dirname(destFilePath);
  ensureMakeDir(destDir);
  writeFile(destFilePath, file.contents.toString());
}

function getVinyl(filePath: string): VinylFile {
  const contents = new Buffer(readFile(filePath));
  return new vinyl({
    base: path.dirname(filePath),
    path: filePath,
    contents,
  });
}

function getChildren(file: VinylFile): VinylFile[] {
  const statementRE = /(import|export)\s*((\{[^}]*\}|\*)(\s*as\s+\w+)?(\s+from)?\s*)?([`'"])(.*)\6/ig;;
  const result: VinylFile[] = [];

  let match: RegExpMatchArray | null;
  while ((match = statementRE.exec(file.contents.toString())) && !isNil(match)) {
    const inFile = match[7];

    if (!isEmpty(inFile) && inFile[0] === '.') {
      let resolvedPath = path.resolve(file.base, inFile);
      if (/\.ts$/.test(resolvedPath) === false) {
        resolvedPath += '.ts';
      }
      if (isFile(resolvedPath)) {

        result.push(getVinyl(resolvedPath));
      } else if (isDirectory(resolvedPath)) {

        debugger;
        const indexBarrel = path.resolve(resolvedPath, 'index.ts');
        if (isFile(indexBarrel)) {
          result.push(getVinyl(indexBarrel));
        }
      }
    }
  }
  return result;
}

async function handleFile(file: VinylFile, destDir: string, baseDir: string, plugins: HandlerPlugin[], processedList: string[]): Promise<void> {

  for (const child of getChildren(file)) {
    if (processedList.indexOf(child.path) < 0) {
      processedList.push(child.path);

      await handleFile(child, destDir, baseDir, plugins, processedList);
    }
  }

  if (isArray(plugins)) {
    for (const plugin of plugins) {
      const result = await plugin(file);

      if (!isEmpty(result) && result !== file.contents.toString()) {
        file.contents = new Buffer(result);
      }
    }
  }

  const destFilePath = file.path.replace(baseDir, destDir);
  copyFile(file, destFilePath);
}

export function preprocessTSFiles(entryFilePath: string, destDir: string, baseDir?: string): Promise<void> {
  const fileList: string[] = [];
  const plugins = [InlineTemplate, InlineStyles];

  if (isNil(baseDir)) {
    baseDir = path.dirname(entryFilePath);
  }

  return handleFile(getVinyl(entryFilePath), destDir, baseDir, plugins, fileList);
}


texts: Project Euler #47: find four consecutive integers, each with four distinct prime factors

texts:
Project Euler Problem 47

  Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?

This is my solution for problem 47 on Project Euler.

def primes2(n):
    """ Input n>=6, Returns a list of primes, 2 <= p < n """
    n, correction = n-n%6+6, 2-(n%6>1)
    sieve = [True] * (n//3)
    for i in range(1,int(n**0.5)//3+1):
      if sieve[i]:
        k=3*i+1|1
        sieve[      k*k//3      ::2*k] = [False] * ((n//6-k*k//6-1)//k+1)
        sieve[k*(k-2*(i&1)+4)//3::2*k] = [False] * ((n//6-k*(k-2*(i&1)+4)//6-1)//k+1)
    return [2,3] + [3*i+1|1 for i in range(1,n//3-correction) if sieve[i]]

def findSeq(lst):
    """ Returns the index of the first term of a 4 term arithmetic sequence """
    for x in range(len(lst)-2):
        if sum(lst[x:x+4]) == (4*lst[x]+6):
                return x
    return False

def generateFactors(num,values,factors):
    if len(values[num]) > 0:
        factors += values[num]
        return factors
    else:
        for i in range(2,num-1):
            if num % i == 0:
                return generateFactors(num//i,values,factors+[i])
n = 200000

primes = set(primes2(n))

factors = dict([[x,[]] for x in range(2,n)])

for item in factors.keys():
    if item in primes:
        factors[item] = [item]
    else:
        factors[item] = generateFactors(item,factors,[])

fourFact = [item for item in factors.keys() if len(set(factors[item])) == 4]

pos = findSeq(fourFact)

print(fourFact[pos])

The primes2 function isn't my code but I have included it for completeness. I am looking for feedback on my implementation and how to optimise the code.


texts: Highest pit - only climbing through the pit once

texts:
Problem description:

  A non-empty zero-indexed array A consisting of N integers is given. A pit in this array is any triplet of integers (P, Q, R) such that:

  0 ≤ P < Q < R < N;

  sequence [A[P], A[P+1], ..., A[Q]] is strictly decreasing, i.e. A[P] > A[P+1] > ... > A[Q];

  sequence A[Q], A[Q+1], ..., A[R] is strictly increasing, i.e. A[Q] < A[Q+1] < ... < A[R].

  The depth of a pit (P, Q, R) is the number min{A[P] − A[Q], A[R] − A[Q]}.

  For example, consider array A consisting of 10 elements such that:

  A[0] =  0
  A[1] =  1
  A[2] =  3
  A[3] = -2
  A[4] =  0
  A[5] =  1
  A[6] =  0
  A[7] = -3
  A[8] =  2
  A[9] =  3

  Triplet (2, 3, 4) is one of pits in this array, because sequence [A[2], A[3]] is strictly decreasing (3 > −2) and sequence [A[3], A[4]] is strictly increasing (−2 < 0). Its depth is min{A[2] − A[3], A[4] − A[3]} = 2.

  Triplet (2, 3, 5) is another pit with depth 3.

  Triplet (5, 7, 8) is yet another pit with depth 4. There is no pit in this array deeper (i.e. having depth greater) than 4.

Imagine I would write this code at an interview. What would you say?

Time complexity: \$O(n)\$

Space complexity: \$O(n)\$

Auxiliary space complexity: \$O(1)\$

public class FindDeepestPit {

    public static void main(String[] args) {
        int[] heights = { 0, 9, 6, -2, 7, 8, 0, -3, 2, 3 };
        int result = findDeepestPit(heights);
        System.out.println(result);
    }

    private static int findDeepestPit(int[] heights) {
        int firstIndex = 0;
        int deepest = -1;
        int depth = 0;
        boolean climbingUp = false;

        /*
        * mark current position as highest (firstIndex)
        * - go to next as long as we're going down
        * - when we're not going down anymore, switch to mark us going up
        * - go up until we can't go up anymore, then save the current depth of the pit, and mark the current position as highest
        * */

        for (int i = 0; i < heights.length - 1; i++) {
            int currentHeight = heights[i];
            int nextHeight = heights[i + 1];
            // find higher point
            if (!climbingUp) { // climbing down
                if (currentHeight < nextHeight) {
                    // we can't go further down here
                    climbingUp = true;
                    deepest = i;
                }
            } else { // climbing up
                if (currentHeight > nextHeight) {
                    // we can't get further up here.
                    int lastIndex = i;
                    int depthA = heights[firstIndex] - heights[deepest];
                    int depthB = heights[lastIndex] - heights[deepest];
                    int currDepth = Math.min(depthA, depthB);
                    depth = Math.max(depth, currDepth);
                    firstIndex = i;
                    climbingUp = false;
                }
            }
        }

        int depthA = heights[firstIndex] - heights[deepest];
        int depthB = heights[heights.length - 1] - heights[deepest];
        int currDepth = Math.min(depthA, depthB);
        depth = Math.max(depth, currDepth);

        return depth;
    }
}

Primary concerns:

Is the approach clear?
Are there any edge cases I did not think about?
At an interview, would adding unit tests for this method be a good idea?


texts: RESTEasy client that should use generics

texts:
I have written some code using RESTEasy to handle RESTful service calls and parse the response XML into relevant JAXB annotated classes. At the moment I have seperate methods for each return type:

public class RestBase {
    protected final RestClient restClient;

    public RestBase() {
        restClient = RestManager.getClient();
    }

    protected List<GridNodeDTO> getGridNodes(String path, List<String[]> params){
        Response entity = getEntity(path, params);
        return entity.readEntity(new GenericType<List<GridNodeDTO>>() {});
    }

    protected UnauthorizedAgentDTO getAgent(String path, List<String[]> params){
        Response entity = getEntity(path, params);
        return entity.readEntity(new GenericType<UnauthorizedAgentDTO>() {});
    }

    // other types skipped ...

    protected Response getEntity(String path, List<String[]> params) {
        ResteasyWebTarget target = getTarget();
        target = target.path(path);
        if (params != null) {
            for (String[] s : params) {
                target = target.queryParam(s[0], s[1]);
            }
        }

        Response response = target.request(MediaType.APPLICATION_XML).get();
        if (response.getStatus() != Response.Status.OK.getStatusCode()) {
            throw new RuntimeException(String.format("Invalid response status: %d\n%s", response.getStatus(), response));
        }
        return response;
    }

    private ResteasyWebTarget getTarget() {
        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target(UriBuilder.fromUri(PropertiesHandler.getPropertyByKey("restUrl")).build());
        client.register(new AddAuthHeadersRequestFilter(restClient.getUser(), restClient.getPass()));
        return target;
    }

Could I reduce the amount of code by using generics? I have had some luck with using:

@SuppressWarnings("unchecked")
protected <T> T getRestResponse(String path, List<String[]> params, final Class<T> type) {
    // skipped ... getResponse()
    return (T)response.readEntity(new GenericType(type) {});
}

But this does not work with lists, because it seems, that the program knows nothing of generic type of list at runtime. And it seems that the compiler also kind of knows it (type erasure).
What ways there are to optimize my routines?


texts:
I need a code review for the following question:

  Using C++ object oriented design, provide the implementation of Kennel
  so that:

  The method AddCat() adds a Cat to the Kennel, providing its name.
  The method AddDog() adds a Dog to the Kennel, providing its name.
  The method RollCall() prints the Animal's name and sound to stdout:

  Cats identify themselves by printing "Meow" to stdout.
  Dogs identify themselves by printing "Woof" to stdout.

Kennel .h

    #pragma once
    #ifndef KENNEL_H
    #define KENNEL_H

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

    class Kennel
    {

    public:
        Kennel() { };
        virtual ~Kennel();
        void AddCat(const std::string & name);
        void AddDog(const std::string & name);
        void RollCall();
        virtual void makeSound(std::string name) { }

    private:
        std::vector <Kennel*> KennelList;
    protected:
        std::string name;

    };

    //Dog inherits Kennel
    class Dog :public Kennel
    {
    public:
        Dog(std::string dogName)
        {
            name = dogName;
        }
        ~Dog() { };
        void makeSound(std::string name)
        {
            std::cout << name << " says Woof" << std::endl;
        };
    };

    //Cat inherits Kennel
    class Cat :public Kennel
    {
    public:
        Cat(std::string catName)
        {
            name = catName;
        }
        ~Cat() { };
        void makeSound(std::string name)
        {
            std::cout << name << " says Meow" << std::endl;
        };
    };

    #endif

Kennel.cpp

    #include "Kennel.h"

    Kennel::~Kennel()
    {
        for (auto i : KennelList)
        {
            delete i;
        }
    }

    void Kennel::AddCat(const std::string & name)
    {
        KennelList.push_back(new Cat(name));
    }

    void Kennel::AddDog(const std::string & name)
    {
        KennelList.push_back(new Dog(name));
    }

    void Kennel::RollCall()
    {
        for (unsigned int i = 0; i < KennelList.size(); ++i)
        {
            KennelList[i]->makeSound(KennelList[i]->name);
        }
    }

main

#include "Kennel.h"
    int main()
    {
    Kennel kennel;

    kennel.AddCat("Garfield");
    kennel.AddDog("Odie");
    kennel.AddDog("Pluto");
    kennel.AddCat("Felix");
    kennel.AddCat("Sylvester");
    kennel.AddCat("Scratchy");
    kennel.AddDog("Scooby Doo");
    kennel.AddCat("Puss in Boots");
    kennel.AddDog("Goofy");
    kennel.AddDog("Old Yeller");

    kennel.RollCall();
}


texts:
The objective was to write a locale-independent function that prints floating-point numbers (i.e. one that always uses . as a decimal point regardless of the locale) that was going to be used in a library that was meant to produce JSON. I was aiming for the following:

Implementation simplicity
Ability to be used on different sorts of streams (writing into a file, a string, etc)
Reasonable corrrectness

Here's the main code:
#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>

typedef int (*printfn_t)(void *, const char *, ...);

#define print(...) if (printfn(stream, __VA_ARGS__) < 0) {\
    return false;\
}

static const int PRECISION = 15;

static int count_digits(double n) {
  return floor(log10(n) + 1);
}

bool print_number(
    printfn_t printfn,
    void *stream,
    double number
) {
    if (number == 0) {
        print("0");

        return true;
    }

    if (number < 0) {
        print("-");

        number = -number;
    }

    uint64_t integral = 0, decimal = 0;
    int exponent = 0;

    if (number > 9007199254740991) {
        exponent = count_digits(number) - 1;

        number = number / pow(10, exponent);
    } else if (number < 1 / pow(10, PRECISION)) {
        exponent = count_digits(number) - 1;

        number = number * pow(10, -exponent);
    }

    integral = number;
    decimal = round(fmod(number, 1) * pow(10, PRECISION));

    print("%" PRId64, integral);

    if (decimal != 0) {
        print(".");

        int decimal_length = count_digits(decimal);

        for (int i = 0; i < PRECISION - decimal_length; i++) {
            print("0");
        }

        print("%" PRId64, decimal);
    }

    if (exponent != 0) {
        print("e%i", exponent);
    }

    return true;
}

And here's how you use it:
int main() {
    double input = 0;

    scanf("%lf", &input);

    print_number(
        (printfn_t) fprintf,
        stdout,
        input
    );
}


texts: Recursion to map phone number to strings

texts:
I am trying to solve the following via recursion: In a phone we have each digit mapped to a number.

Example:

1       2(ABC)  3(DEF)
4(GHI)  5(JKL)  6(MNO)
7(PRS)  8(TUV)  9(XYZ)
*       0       #

I need to print out all the possible sequences of a phone number (example: 637-8687).

I did the following that seems to solve it:

static Map<String, String> digits;
    static{
        digits = new HashMap<String, String>();
        digits.put("2", "ABC");
        digits.put("3", "DEF");
        digits.put("4", "GHI");
        digits.put("5", "JKL");
        digits.put("6", "MNO");
        digits.put("7", "PRS");
        digits.put("8", "TUV");
        digits.put("9", "XYZ");
    }

    public static void printSequences(String phoneNumber, Map<String, String> associations){
        if(phoneNumber == null || phoneNumber.isEmpty()){
            throw new IllegalArgumentException();
        }
        if(associations == null || associations.isEmpty()){
            throw new IllegalArgumentException();
        }
        String[] letters = new String[phoneNumber.length()];
        for(int i = 0; i < phoneNumber.length(); i++){
            String seq = associations.get(phoneNumber.charAt(i) + "");
            if(seq == null)continue;
            letters[i] = seq;
        }
        printSequences(letters, 0, letters.length, "");
    }
private static void printSequences(String[] letters, int start, int end, String prefix) {

        if(start == end){
            System.out.println(prefix);
            return;
        }

        String sequence = letters[start];
        if(sequence == null)
            return;

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

            printSequences(letters, start + 1, end, prefix + sequence.charAt(i));
        }

    }

The String[] letters has all the relevant digits (extracted from a Map). In this case it is [MNO, DEF, PRS, TUV, MNO, TUV, PRS] for phone 6378687.

This code seems to work but does not for 637-8687.  For 637-8687 it actually prints nothing at all!

I am trying to practice recursion, so could you please help me out with this one?  Also, is recursion actually the best approach on such a problem?


texts:
I recently built a CI for my course project in C. I made 3 stages, to build docker image, compile my C sources and run the said project.

That said, i think i could've do way better here but i don't really know how. I don't have a really big experience with CI, and even if that one works i would like to hear about what you think about it.

What i would like to improve is scalability, generality (there probably will be a need at some point to separate my dockerfiles per subproject (main-atoi is a subproject), ...

Source code here : Gitlab

Docker file

FROM debian:stretch
RUN apt-get update \
&& apt-get install gcc g++ -y

ADD ./ /home

VOLUME /home/build /home/build

.gitlab-ci.yml

stages:
- build-image
- build
- run

before_script:
- docker info

build-image:
  stage: build-image
  tags:
  - build-docker
  script:
  - docker build -t artandor/debian9-cpp:v1-tp1 .

build:
  stage: build
  tags:
  - cours
  script:
  - docker run -v /home/build:/home/build --rm artandor/debian9-cpp:v1-tp1 gcc -Wall -Werror /home/main-atoi/myatoibase.c /home/main-atoi/main-atoi.c -o /home/build/main-test

run:
  stage: run
  dependencies:
  - build
  tags:
  - cours
  script:
  - docker run -v /home/build:/home/build --rm artandor/debian9-cpp:v1-tp1 /home/build/main-test
  - cp /home/build/main-test ./
  artifacts:
    name: "Main Atoi Binary - $CI_COMMIT_REF_NAME"
    paths:
    - ./main-test


texts: ReactJs stateWrapper for nested state objects

texts:
I made a simple state wrapper for a react project I am working on, so I can keep my state at the top component but reason about it at a lower class level.

The example below is very simple, but it works and provides enough of an example of what StateWrapper does, obviously i am using it in reality with a much more complex object at the top state level.

This works even when nesting StateWrapped components, the state always lives at the top level.

I'm not 100% sure that this is the right way to go to be honest, but it seems to work quite nicely so that I can keep a complex component's logic local to its class while still keeping the state at the top level and allowing components to interact with eachother at the top stateful level.

Welcome any feedback, i'm aware it might be a bit of an anti-pattern but i can follow the state down through my classes from the top level in my code easily, and the state all lives at the top component so it debugs nicely in chrome and performs well on my more complex app.

Simple working example is as follows:

import * as React from 'react';

export default class Test extends React.Component {

    constructor(...props) {
        super(...props)

        this.component1 = new WrappedComponent (
            props => this.setState({component1: props})
        )

        this.component2 = new WrappedComponent (
            props => this.setState({component2: props})
        )

        //this one passes some initial props
        this.component3 = new WrappedComponent (
            props => this.setState({component3: props}),
            {...this.props}
        )

        this.state = {
            component1: this.component1.state,
            component2: this.component2.state,
            component3: this.component3.state,
        }

    }

    render() {

        return(

                <this.component1.component
                    onClick={() => this.component2.setText("component1")} />

                <this.component2.component
                    onClick={() => this.component3.setText("component2")} />

                <this.component3.component
                    onClick={() => this.component1.setText("component3")} />

        );
    }

}

class StateWrapper {

    constructor(setState, initialProps = []) {
        this.setState = props => {
            this.state = {...this.state, ...props}
            setState(this.state)
        }
        this.props = initialProps
    }

    render() {
        return(
render() not defined
)
    }

    component = props => {
        this.props = {...this.props, ...props}
        return this.render()
    }
}

class WrappedComponent extends StateWrapper {

    constructor(...props) {
        super(...props)
        this.state = {
            text: "no text has been set"
        }
    }

    render() {
        let text = `Received text: ${this.state.text}`
        return(

{text}

        )
    }

    onClick = () => {
        this.setState({text: "i got clicked!"})
        this.props.onClick()
    }

    setText = name => this.setState({text: `${name} set my text`})
}


texts:
This is my Queue implementation in Java. Would appreciate a review of the code.

import java.util.Iterator;

public class Queue<T>implements Iterable<T> {

    private Node first, last;
    private int size;

    @Override
    public QueueIterator iterator() {
        return new QueueIterator();
    }

    private class QueueIterator implements Iterator<T>{
        private Node current = first;
        public boolean hasNext(){
                return current != null;
        }
        public T next(){
            T item = current.data;
            current = current.next;
            return item;
        }

    }

    private class Node{
        Node(T data){
            this.data = data;
            this.next = null;
        }
        T data;
        Node next;
    }
    public Queue(){
        first = last = null;
        size = 0;
    }

    public void enqueue(T item){
        Node p = new Node(item);
        if(first == null){
            first = last = p;
            size++;
            return;
        }
        last.next = p;
        last = p;
        size++;

    }

    public T dequeue(){

       Node p = first;
        try{
            first = first.next;
            size--;
        }catch(Exception e){
            System.out.println("Dequeing an empty queue :" + e);
        }

        return p.data;

    }

    public boolean isEmpty(){
        return last == null;
    }

}


texts: Implementation of a cellular automata to model breast cancer

texts:
I've created a program - it's an implementation of the cellular automata described in this paper (can't find a free PDF, sorry - anyway it's not really relevant to this question). The CA is implemented as a 3D array of integers of size int[length][20][20], 'length' being an arbitrary value determined by the user.

The tl;dr is: I've parallelized the program and not only does it not speed up, it slows down sometimes. Here's the code for the sequential and concurrent versions of the program, as well as a main benchmarking program  (this and this).

The way it's parallelized is that the array is distributed between threads - say if length = 200 and we use 4 threads, thread 0 would process (we will call the lattice itself 'duct' from now on) duct[0][0][0] through duct[49][19][19], thread 1 would  process duct[50][0][0] through duct[99][19][19], and so on. While processing a cell, a thread might have to access a different thread domain, so ReentrantLocks are used on the boundaries between sections to preserve integrity. Synchronization is achieved with a CyclicBarrier.

Anyway, the program is not speeding up at all. I suspect it's due to a memory bottleneck; while processing a given cell the program accesses its 26 neighbors, which might be the reason for the slow results. Otherwise, I've no idea where to start optimizing this program so parallelization yields results, or even if parallelization will ever yield results with this particular program.

Since I've to post some code here besides pastebin, here's how parallelization is implemented. This is done on the constructor:

int step = length/n_threads;
for(int i=0 ; i<n_threads ; i++)
{
    int begin =     i*step;
    int end   = (i+1)*step-1;
    if ((i+1) == n_threads)
        end = length-1;

    threadArray[i] = new Duct(begin, end, i);
    if (i < n_threads-1)
        thread_locks[i]  = new ReentrantLock();
    }
}

Here's the thread object constructor:

public Duct(int begin, int end, int index)
{
    thread_index = index;
    index_array = new Integer[end-begin+1];
    // Aquí Coordinate se usa como un par de números, no como una coordenada
    boundaries = new Coordinate(begin, end, 0);
    // [begin, end] ambos inclusive
    for(int z=begin, i=0 ; z<=end ; z++, i++)
    {
        index_array[i] = z;
    }
}

and the function that controls synchronization

protected void execute(int nGens)
{
    if(generation_number == 0)
    {
        init_routine_stem(); // Hacemos la primera generación (colocación de stem cells) secuencialmente
    }

    total_generations = nGens;

    try {
    threadPool = Executors.newFixedThreadPool(n_threads);
    for (int i=0; i<threadArray.length; i++)
        threadPool.execute(threadArray[i]);
    } catch(Exception e){System.out.println(e);}

    for (int i=0; i<total_generations; i++)
    {
        try
        {
            threadBarrier.await();
            generation_number++;

            if(!initialized)
            {
                initialized = threadArray[0].get_local_initialized();
                for (int t=1; t<threadArray.length; t++)
                    initialized = initialized && threadArray[t].get_local_initialized();
            }
        }
        catch (BrokenBarrierException | InterruptedException e){}
    }
    threadPool.shutdown();
    while(!threadPool.isTerminated()){}
}


texts: Good usage of MailboxProcessor/Agent in class rather than mutable field?

texts:
I've written a simple Simple Service Discovery Protocol (SSDP) module.  This module contains a class, Session that you create and then use to issue commands and subscribe to interesting events.  I include the relevant code snippets below, but you can also view the entire SSDP module gist.

The usage looks like this:

//Fire up a session.
let ssdpSession = Ssdp.Session.Start(1985)
//Startlistening
ssdpSession.MessageReceived.Add(fun msg -> printfn "%s" msg)
//Do an MSearch.
let search = { MSearch.SearchTarget = All; DeviceResponseDelay = OneSecond; }
ssdpSession.BroadcastMSearch(search)

It is intended that you hang on to the Session instance for as long as you need it.  However, the Session is listening on one or more IP addresses for the lifetime of your usage, and these IP addresses may change (for example, you renew an IP DHCP lease).  The Session class uses System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged to detect this.

The question: What would be the elegant/idiomatic options to handle the changing of the IP and hence UdpClient listener set?  I am trying to avoid mutable fields, but I wonder if I'm going too far with my approach below (just tell me, am I an astronaut!?).  Are there elegant/idiomatic approaches for this?

What I have done so far, is run an MailboxProcessor (Agent) that fires up the UdpClient listeners and then waits for a message to fire up a new set (or tear them all down for good).  The current set is passed to the recursive function running in the agent, so we always have it to hand when a change is requested or it is time to kill things off.

It looks like this:

let udpClientListeningController =
        Agent.Start(fun inbox ->

            let rec loop (runningClients:UdpClient seq) = async {
                let! msg = inbox.Receive()

                let closeRunningClients () =
                    runningClients |> Seq.iter (fun client -> client.ProtectedClose())

                match msg with

                | ListenForSearchResponsesAndNotifyAdverts (port, ipAddresses) ->

                    closeRunningClients ()

                    let uniAndMulticastUdpClients = seq {
                        let portIPPairs = ipAddresses |> Seq.zip (seq { yield port })
                        yield! portIPPairs |> Seq.map createUnicastClient
                        yield! portIPPairs |> Seq.map (snd >> createMulticastClient)
                    }

                    //Start em up.
                    uniAndMulticastUdpClients
                    |> Seq.iter (udpClientReceiveLoopAsync >> Async.Start)

                    return! loop uniAndMulticastUdpClients

                | StopListening ->
                    closeRunningClients ()
                    return! loop Seq.empty
            }
            loop Seq.empty
        )


texts: WCF general response validation for multiple services

texts:
Background information

I am building a web application that consumes 3 different WCF services that are hosted internally.

Each response object from the service inherits from a base class ResponseBase. This class contains properties int ErrorCode and string ErrorMessage.

When I am consuming these services in my web application I want to implement some general error handling that verifies if the response object contains an errorcode < 0.

What I've come up with is a static class ServiceHelper with several overloads of the following method:

public static void VerifyResponse(dynamic response)
{
    if (response == null) throw new ArgumentNullException("response");

    if (response.ErrorCode < 0)
        throw new InvalidServiceResponseException(response.ErrorCode, response.ErrorMessage);
}

Now as you can see the type of the parameter is dynamic. The reason for this, and this is basically why I'm asking this question, is that I don't have a general base object to define all response objects. Because each WCF service will creat its own contracts there can't be a shared base object between services. Well... it can be done but we prefer to keep it simple for now.

Question

Do you of some magic way to validate 3 different types of response objects but with a properly typed parameter and without creating a different method for each service. Because each service has a different ResponseBase type even though it is actually the same.

Or do you think it is permitted to use a dynamic parameter in this context?


texts: Improving performance in a Webdriver method

texts:
I'm testing an application which is not exactly an e-commerce application but which behaves enough like one that you can think of it as an e-commerce application for the purposes of understanding this code :)

This code relies heavily on the Selenium Webdriver library.

The code comes from the Product page, where you have searched and found a list of products. This is a utility method to get the list of products being displayed so I can verify correctness along any axis. This method only gets those products that are visible, which may differ from the total list of products if filtering is involved.

I maintain a list of WebElements representing the container for the product display, one for each product on the page, which is initialized in initLists() (along with some other non-important lists) so that we don't have stale references. That's what's stored in allProducts.

ProductView is an inner class that encapsulates a specific Product record in the list; interactions with a given product (such as clicking any of the links for more information) are delegated to the ProductView for that Product.

public ArrayList<Product> getAllVisibleProducts() {
    initLists(); //Reinitialize lists

    Utils.WaitForAjax(driver);
    ArrayList<Product> retval = new ArrayList<Product>();
    for (WebElement rProduct : allProducts) {
        try {
            if (rProduct.isDisplayed()) retval.add(new ProductView(rProduct).getProduct());
        } catch (StaleElementReferenceException e) {
            continue; //We just ignore that element
        }
    }
    return retval;
}

I'm getting hangs of sometimes multiple minutes where the test appears to do nothing on the product page. I'm currently still trying to reproduce the super-long lag, but at least part of my problem is that this method takes quite a long time to return. I put log statements at various portions of the app, and between starting the loop and returning, it was something like 44 seconds for 8 products. Am I doing too much web scraping here?

(ProductView stores references to the elements that are interactable for the given product and attempts to build an in-memory representation of that product by scraping the page using Webdriver. The code to build the product is the vast majority of the constructor and clocks in about 70-odd lines, so I omitted it).

Ideally I think I need to hear from someone who has used WebDriver before, since this probably boils down to WebDriver best practices.


texts: Customized class crawler to harvest names and phone numbers from a webpage

texts:
I've written some code using python to scrape names and phone numbers from a webpage which is working nice at this moment.

However, the way I've written my class crawler is unconventional. As I've hardly seen any crawler which have been written the way I did here, I'm little dubious as to whether this style I should keep up. It serves the purpose, though! Expecting reviews to do the betterment of this crawler. Thanks in advance.

Here is the script:

import requests

from lxml import html

class YelPage:

    base_url = "https://www.yellowpages.com/search?search_terms=pizza&geo_location_terms=Los+Angeles%2C+CA"
    url = "https://www.yellowpages.com"

    def __init__(self):

        self.vault = []

    def parse(self):

        self.get_urls(self.base_url)
        self.process_links(self.base_url)

    # Going to the main page for collecting links leading to the target page
    def get_urls(self, link):

        response = requests.get(link)
        tree = html.fromstring(response.text)

        for items in tree.xpath("//div[@class='info']"):
            item_link = items.xpath(".//a[@class='business-name'][not(@itemprop='name')]/@href")
            for page in item_link:
                self.target_page(self.url + page)

    # Creating links leading to the next page
    def process_links(self, link):

        response = requests.get(link)
        tree = html.fromstring(response.text)
        next_page = tree.xpath("//div[@class='pagination']//li/a/@href")

        for n_page in next_page:
            self.get_urls(self.url + n_page)

    # Scraping required data reaching this target page
    def target_page(self, urls):

        print(urls)
        response = requests.get(urls)
        tree = html.fromstring(response.text)

        for posts in tree.xpath("//*[@id='main-header']"):
            name = posts.xpath(".//div[@class='sales-info']/h1/text()")[0] if posts.xpath(".//div[@class='sales-info']/h1/text()") else ""
            phone = posts.xpath('.//p[@class="phone"]/text()')[0] if posts.xpath('.//p[@class="phone"]/text()') else ""
            records = name, phone
            if records not in self.vault:
                self.vault.append(records)

    def __str__(self):

        return self.vault

if __name__ == '__main__':

    crawler = YelPage()
    crawler.parse()
    for item in crawler.vault:
        print(item)


texts:
I wrote the following code for generating a table with random numbers and would like to ask if there is any way to optimize/enhance/modularize the JavaScript code, especially the addColumn and the addRow functions.

<form>
<input type="button" class="button" value="Add Row" onclick="addRow()" />
<input type="button" class="button" value="Delete Row" onclick="deleteRow()" />
<input type="button" class="button" value="Add column" onclick="addColumn()" />
<input type="button" class="button" value="delete column" onclick="deleteColumn()" />
</form>

This is the JavaScript code:

var blocks;
var myTable;
var nums = [];

function initlization() {
  myTable = document.getElementsByTagName("table")[0];
  shuffle(); //need to shuffle initially
}

function shuffle() {
  //need to get all tds each time the table is shuffled
  blocks = document.getElementsByTagName("td");
  tableSize = blocks.length;
  shuffledArray(0, tableSize);
  for (var i = 0; i < tableSize; i++) {
    blocks[i].innerHTML = '' + nums[i] + '';
  }
}

function addColumn() {
  // Getting current table size by multiplying number of rows by number of   columns
  tableSize = (myTable.rows.length) * (myTable.rows[0].cells.length);
  // Getting new table size by adding the number of rows
  newTableSize = tableSize + (myTable.rows.length);
  shuffledArray(tableSize, newTableSize);
  index = Math.floor(Math.random() * (myTable.rows[0].cells.length) + 1);
  for (var i = 0; i < myTable.rows.length; i++) {
    myCell = myTable.rows[i].insertCell(index);
    myCell.innerHTML = '' + nums[i] + '';
  }
}

function addRow() {
  tableSize = (myTable.rows.length) * (myTable.rows[0].cells.length);
  newTableSize = (myTable.rows.length + 1) * (myTable.rows[0].cells.length);
  shuffledArray(tableSize, newTableSize);
  index = Math.floor(Math.random() * (myTable.rows.length) + 1);
  var row = myTable.insertRow(index);
  for (var i = 0; i < myTable.rows[0].cells.length; i++) {
    var myCell = row.insertCell(i);
    myCell.innerHTML = '' + nums[i] + '';

  }
}

function deleteColumn() {
  var allRows = myTable.rows;
  if (allRows[0].cells.length > 2) {
    for (var i = 0; i < allRows.length; i++) {
      allRows[i].deleteCell(-1);
    }
    shuffle();
  }
}

function deleteRow() {
  if (myTable.rows.length > 2) {
    myTable.deleteRow(0);
    shuffle();
  }
}

function shuffledArray( from,  to) {
for(i=from; i<to;i++){
    nums[i-from]=i;
    }
for (var i = to-from; i; i--) {
    j= Math.floor(Math.random() *i);

    [nums[i - 1], nums[j]] = [nums[j], nums[i - 1]];
    }
}


texts: CSV file reader in PHP that supports large files (>15k lines)

texts:
I have written the following function in PHP to read a CSV file. It works correctly for small files.

However, if I try to read in files that are bigger than 15k lines, it takes between 1–2 minutes to process them. How can I optimize this code to make it run faster on large files?

Is there anything else that I should improve?

function read_csv($file){
        $return_waarde = array();
        if(!is_null($file) && !is_empty($file)){
            $header = str_getcsv(utf8_encode(array_shift($file)), ';');
            $header_trimmed = array();

            foreach($header as $value){
                $trim = trim($value);
                if(!in_array($value, $header_trimmed)){
                    $header_trimmed[] = $trim;
                } else {
                    $header_trimmed[] = $trim . "1";
                }
            }
            ini_set('memory_limit', '512M');
            ini_set('max_execution_time', '180');

            foreach($file as $record)
            {
                if(!in_array($record,$return_waarde)){
                    $return_waarde[] = array_combine($header_trimmed, str_getcsv(utf8_encode($record), ';'));
                }
            }
        } else {
            $return_waarde = "there is no file";
        }
        return $return_waarde;
    }


texts:
I'm learning Python from Coursera and am doing a class. One of the assignments is as follows:

  Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.

I have completed this assignment and I think I did pretty good.  How can I make it better?

def get_email(filename, name):
    count = 0
    user = {}
    handle = open(filename)

    for line in handle:
        if name in line and 'From' in line and 'From:' not in line:
            arr = line.split(' ')
            count += 1
            user['email'] = count

    email = ''.join(arr[1])
    print "%s %d" % (email, count)

get_email('mbox-short.txt', 'cwen')
#<= cwen@iupui.edu 5
get_email('mbox-short.txt', 'david')
#<= david.horwitz@uct.ac.za 4

Full text file to read from here


texts:
I'm working on a project which simulates (very simple/watered down) a network. The basis of this is to learn how to implement a Queue. I understand there are many ways to go about this, but I ultimately went with a LinkedList mainly because of it's nature (FIFO) which makes it easy to utilize in a Queue. I would appreciate any feedback on my implementation, and would also greatly appreciate with any critiques on my Queue's toString(), I know it is an unconventional way of doing it but I thought it would by a nice chance to work on recursion.

The data class in the this project is the Packet. It represents a packet and code is as follows. I've omitted the getters and setters for brevity:

private static int packetCount;         // Determined at the time of the objects creation [incremental]
private int id;                         // Assigned by packetCount
private int packetSize;                 // Size of the packet being sent
private int timeArrive;                 // Creation "time stamp"
private int timeToDest;                 // Number of simulation units it took for the packet to arrive to destination

/**
 * Default constructor
 */
public Packet() {
    id = 0;
    packetSize = 0;
    timeArrive = 0;
    timeToDest = 0;

}

public Packet(int id, int packetSize, int timeArrive, int timeToDest) {
    this.id = id;
    this.packetSize = packetSize;
    this.timeArrive = timeArrive;
    this.timeToDest = timeToDest;
}
@Override
public String toString() {
    return "Packet #" + id + " => arrive at simulation unit: " + timeArrive + " => packet size: " + packetSize;
    }
}

Here is my Node class:

public class Node {
private Packet data;
private Node next;
private Node prev;

public Node() {
    next = null;
    data = null;
    prev = null;
}

public Node(Packet data) {
    this.data = data;
    next = null;
    prev = null;
}

public Packet getData() {
    return data;
}

public Node getNext() {
    return next;
}

public Node getPrev() {
    return prev;
}

public void setData(Packet data) {
    this.data = data;
}

public void setNext(Node next) {
    this.next = next;
}

public void setPrev(Node prev) {
    this.prev = prev;
}

}

And my Queue class which is actually a router within this simulation:

/**
 * Represents a router on the network [queue]
 */
public class Router{

private Node head;
private Node tail;
int size;

public Router() {
    head = null;
    tail = null;
    size = 0;
}

public boolean isEmpty() {
    return head == null;
}

public int size() {
    return size;
}

public void enqueue(Packet packet) {
    Node node = new Node(packet);

    if(head == null) {
        head = node;
        tail = node;
    }
    tail.setNext(node);
    node.setPrev(tail);
    tail = node;

    size++;
}

public Packet dequeue(){
    if(isEmpty()) {
        try {
            throw new Exception("Underflow");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Node ptr = head;
    head = ptr.getNext();

    if(head == null) {
        tail = null;
    }

    size--;

    return ptr.getData();
}

public Packet peek() {
    if(isEmpty()) {
        try {
            throw new Exception("Underflow");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return head.getData();
}

public String recToString(StringBuilder strBlder, Node cursor) {

    if(cursor !=head) {
        strBlder.append(cursor.getData().toString() + "\n");
        recToString(strBlder, cursor.getPrev());
    }

    strBlder.append(cursor.getData().toString());
    return strBlder.toString();
}

@Override
public String toString() {
    StringBuilder s = new StringBuilder();
    return recToString(s, tail);
}
}


texts:
I recieve a number n and then recieve n*n integers, representing a matrix, or an array of [n][n]. I want to create an array [n][n], where every cell contains the sum of all cells "left and above" from the original array, while not creating the array, just using the values.
For example, the cell [5][7], will contain the sum of the block [0][0] to [5][7] out of the original array.

I seriously need to simplify or speed up my code.

void read() {
    size = scan.nextInt();
    arrayLoad();
}

void arrayLoad() {
    area_values = new int [size][size];
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            add(scan.nextInt(),i,j);
        }
    }
}

void add(int num, int x, int y) {
    for (int i = x; i < size; i++) {
        for (int j = y; j < size; j++) {
            area_values[i][j] += num;
        }
    }
}

This is O(n^4), which, as you may be able to tell, is not exactly easy for n 4+ digits.


texts: Find char which appears maximal number of times

texts:
I had 15 minutes to code the following question:

Given a file name return the char which appears maximal number of times.

please do not take into account the unit test.
Please look at the final foreach loop, I could have also used a for loop, I have doubts what is the best way to implement finding a max value and get the key from the dictionary.

Also please comment about code style.
Thanks

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

namespace JobInterviewTests
{

    [TestClass]
    public class GetMostRepeatedCharQuestion
    {
        [TestMethod]
        public void TestForAasMax()
        {
            char result = GetMaxCharHelper.GetMaxReaptedCharFromFile("MaxA.txt");
            Assert.AreEqual('A', result);
        }
    }

    public class GetMaxCharHelper
    {
        public static char GetMaxReaptedCharFromFile(string fileName)
        {
            Dictionary<char, int> charToCount = new Dictionary<char, int>();
            using (var streamReader = new StreamReader(fileName))
            {
                string line = streamReader.ReadLine();
                while (line != null)
                {
                    char[] charArray = line.ToCharArray();
                    foreach (var currentChar in charArray)
                    {
                        if (charToCount.ContainsKey(currentChar))
                        {
                            charToCount[currentChar]++;
                        }
                        else
                        {
                            charToCount.Add(currentChar, 1);
                        }
                    }
                    //don't forget to get next line when reading from a file
                    line = streamReader.ReadLine();

                }

                int maxCount = 0;
                char maxChar = '\0';
                foreach (var current in charToCount)
                {
                    if (current.Value > maxCount)
                    {
                        maxCount = current.Value;
                        maxChar = current.Key;
                    }
                }
                return maxChar;
            }
        }
    }
}


texts: Python web page to display SSL certificate status

texts:
My task was to create a Python script and connect to various TLS sites and grab the expiry dates. I've done this with openssl and ssl modules just fine.

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

I've done that.

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

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

I.E

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

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

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

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

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

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

Any suggestions?


texts:
The following is function that I have written to group a string to groups, based on whether there's consecutive repeated occurrence. Fop example AAAABBBBAAB is grouped as [A+,B+,A+,B]. Is it possible to make below code more pythonic?  If yes, how?

def create_groups(alphabets):
    """ function group the alphabets to list of A(+)s and B(+)s """
    index = 1
    current = alphabets[0]
    count = 0
    groups = []
    accumulate = False
    while index < len(alphabets):
        if current == alphabets[index]:
            count += 1
            accumulate = True
        else:
            accumulate = False
        if accumulate == False or index == len(alphabets)-1:
            group_indicator = current + '+' if count > 0 else current
            groups.append(group_indicator)
            current = alphabets[index]
            count = 0
        index += 1
    return groups


texts: Parse a file containing key-value pairs followed by coordinates of nodes in a graph

texts:
I've a file which is divided into two parts:

The header
The data part

The header has of course a different format than the data part. The header part contains lines that look like this KEY: VALUE or KEY : VALUE or KEY:VALUE, in theory, clearly.

The data part starts with a delimiter, which in my specific case should always be NODE_COORD_SECTION, and ends with another delimiter, which should always be EOF.

In the header, I only need to keep track of two key-value pairs: the DIMENSION and the BEST_KNOWN.

Each line or entry in the data section should be formatted as

  INDEX X_COORDINATE Y_COORDINATE

There could be more than one space between the values. INDEX should be an int, X_COORDINATE and Y_COORDINATE should be floats or doubles.

I'm parsing the header and the data section differently. The first one is with getline with the help of the find method of the string class, and the second with the overloaded operator >> of the ifstream object. I've read on a Stack Overflow's post that mixing these two ways of parsing lines could be dangerous and thus should be avoided, but I thought that for my case it would be the best way to do it.

I want my code first to be correct, but I would like it to also be as efficient and fast as possible, because this parsing of the file, structured as I've been describing above, is part of a program which must be as fast as possible.

Note, I'm throwing exceptions whenever a line is not formatted as I was expecting. I think this a good way of handling these kinds of errors in general, but I don't know if in C++ this is actually a good idea. Notice that I've already read something about exception handling in C++.

I think this is the relevant code for you:

int dimension = -1;
int best_known = -1;
vector <unique_ptr<Node>> nodes;

void read_file(const char *file_name) {

    ifstream file(file_name);

    if (file.fail()) {
        throw runtime_error("file failed to open");
    }

    string line;
    size_t found_at;

    while (getline(file, line)) {

        if (line.find("BEST_KNOWN") != string::npos) {
            found_at = line.find(':');

            if (found_at == string::npos) {
                throw runtime_error("line containing 'BEST_KNOWN' formatted not as expected");
            }

            best_known = stoi(line.substr(found_at + 1));

        } else if (line.find("DIMENSION") != string::npos) {
            found_at = line.find(':');

            if (found_at == string::npos) {
                throw runtime_error("line containing 'DIMENSION' formatted not as expected");
            }

            dimension = stoi(line.substr(found_at + 1));

        } else if (line.find("NODE_COORD_SECTION") != string::npos) {

            if (dimension == -1 || best_known == -1) {
                throw runtime_error("dimension and best known result should have already been read");
            }

            unsigned index;
            double x, y;

            while (file >> index >> x >> y) {
                unique_ptr <Node> p(new Node(index, x, y));
                nodes.push_back(move(p));
            }

            break;
        }
    }

    file.close();
}

Node is a class that represents a node in a graph. I'm using unique_ptrs here to handle memory release automatically, but I don't know if this has a bad impact on the overall performance. Honestly I would be using C if it wasn't for the classes that C++ provides, because I feel that with C++ I will be losing performance.


texts:
This method takes a string and parses the syllables. I've broken much of the logic into helper methods. Without getting into the helper methods, it's pretty easy to tell what this is doing. But there are a lot of conditionals. I tried handling the conditions that handle adding to the syllables variable and those that subtract as two separate methods, but syllables kept changing value out from under me.

def count_syllables
  exceptions = YAML::load_file(File.join(__dir__, 'exceptions.yml'))['exceptions']
  return exceptions[word.to_s] if exceptions.keys.include?(word)
  syllables = count_vowels
  syllables += count_ys_in_vowel_role if contains_non_initial_y?
  syllables += 1 if contains_le_vowel_sound?
  syllables += 1 if begins_with_re_vowel?
  syllables += 1 if ends_in_sm?
  syllables -= 1 if ends_in_silent_e?
  syllables -= count_diphthongs if contains_diphthongs?
  syllables <= 1 ? 1 : syllables
end


texts: Shortest code to generate a full mesh of elements

texts:
I am trying to find the shortest code that generates a full mesh of the elements fed into it in graphviz format
e.g. the full mesh of [0, 1, 2] is:

2 -> 0
2 -> 1
1 -> 0

I have the code below and I think there might be a way to combine the if and for loops, and avoid needing to directly create the empty string

def graph(l):
    if not l:
        return ""
    i = l.pop()
    out = ""
    for z in l:
        out += "  {}->{}\n".format(i, z)
    return out + graph(l)
print graph(range(3))


texts: Adding multiple rows in an HTML table using jQuery

texts:
I have this function that allows the user to supply a number and that many rows are appended to the existing table:

$('#add-row').click(function() {
    if($('#insert-rows-amnt').val() < 1) {
        alert('Please enter number of rows to insert');
    } else {
        for(i = 1; i <= $('#insert-rows-amnt').val(); i++) {
            $('table tr:last').clone(true).appendTo("tbody");
        }
    }
});

Where #insert-rows-amnt is an input field and in this case contains the number 5000.

This currently takes 48 seconds on my machine to add 5,000 additional rows - is there a quicker way of doing this?

I'm not against putting a loading message somewhere if there isn't a way to significantly increase the speed. I'm just curious if this is the most efficient way.


texts: Javascript RPC Call Handles Data and Returns DB Response

texts:
I have a service within a larger set of microservices that's handling a RPC call. My function mainly just parses through the request, uses the data to insert data into our MongoDB database, and then returns the data that got stored into the database (should be idempotent).

Endpoint Function

type GAPFType = {
  facultyId: number,
  created: number,
  lastModified: number,
  status: Status,
  attachedDocuments: Array<DocumentType>
};

type CallbackType = {
  error: ?string,
  payload: ?GAPFType
};

const submitGAPFBackend = async (
  gapfRequest: GAPFType
): Promise<CallbackType> => {
  logger.info("Enter submitGAPFBackend with request body %j", gapfRequest);

  const { facultyId } = gapfRequest;
  if (facultyId === undefined) {
    logger.error("facultyId missing from request body: %j", gapfRequest);
    return invalidSubmitGAPFCallError;
  }

  const gapf = createSubmitGAPFObject(gapfRequest);
  logger.info("Request GAPF object: %j", gapf);

  try {
    const submittedForm = await GAPFApplication.submit(gapf);
    const submittedData = getSubmittedGAPF(submittedForm);
    logger.info("DB response with filtered data: %j", submittedData);

    const payload = GAPF.create(submittedData);
    logger.info("Response payload data: %j", payload);
    return { error: null, payload: payload };
  } catch (error) {
    logger.error("Error: %j", error.message);
    return { error: error, payload: null };
  }
};

Helper Functions

export const createSubmitGAPFObject = gapfRequest => {
  const now = getUNIXTimestamp();
  const {
    facultyId,
    status = "SUBMITTED",
    created = now,
    attachedDocuments = [{}]
  } = gapfRequest;

  return {
    facultyId: facultyId,
    created: created,
    lastModified: now,
    status: status,
    attachedDocuments: attachedDocuments
  };
};

export const getSubmittedGAPF = submittedForm => {
  return {
    facultyId: submittedForm.facultyId,
    created: submittedForm.created,
    lastModified: submittedForm.lastModified,
    status: submittedForm.status,
    attachedDocuments: submittedForm.attachedDocuments.map(doc => {
      // removing _id from document data
      return {
        name: doc.name,
        link: doc.link,
        attachedDate: doc.attachedDate
      };
    })
  };
};

export const invalidSubmitGAPFCallError = {
  error: {
    message: "facultyId missing from request body",
    status: grpc.status.INVALID_ARGUMENT
  },
  payload: null
};

export const getUNIXTimestamp = () => {
  return Math.floor(Date.now() / 1000);
};

I don't program in Javascript often, so I was hoping to get some feedback on writing cleaner or more idiomatic code. The original code has docstrings, but I've left those out for brevity. And the type annotations are from flow.


texts:
The Node.JS HTTP and HTTPS modules only provide .get shortcut function, unlike AngularJS's $http which provides them all. I went about creating a function that returns one of these shortcut functions:

'use strict';

var http = require('http');

function httpF(method) {
    return function (options, body_or_cb, cb) {
        if (!cb) {
            cb = body_or_cb;
            body_or_cb = null;
        }
        options['method'] = method;
        if (body_or_cb)
            if (!options)
                options = {'headers': {'Content-Length': Buffer.byteLength(body_or_cb)}};
            else if (!options.headers)
                options.headers = {'Content-Length': Buffer.byteLength(body_or_cb)};
            else if (!options.headers['Content-Length'])
                options.headers['Content-Length'] = Buffer.byteLength(body_or_cb);
        var req = http.request(options, function (res) {
            if (!res)
                return cb(res);
            else if ((res.statusCode / 100 | 0) > 3)
                return cb(res);
            return cb(null, res);
        });
        body_or_cb && req.write(body_or_cb);
        req.end();
        return req;
    };
}

It pushes the callback through an (err, res) signature also. The idea here is to enable chaining in async.js or use as a Bluebird promise.

Usage:

var httpPOST = httpF('POST');

httpPOST({
    protocol: 'http:',
    host: 'httpbin.org',
    path: '/post',
    headers: {'Content-Type': 'application/json'}
}, JSON.stringify({hello: 'world'}), function (err, res) {
    if (err)
        console.error(err.statusCode, err.statusMessage);
    else
        console.info(res.statusCode, res.statusMessage);
});

Original TypeScript

Some improvement ideas:

Use url.parse to enable httpPOST('https://httpbin.org/post', .. (including use of https module)
Wrap all errors up into something util.inherits from Error
Fix bug that stops async.series from continuing after failure (even happens with this hack e ? cb() : cb(null, r))

I also submitted this as a PR, but it was rejected (as expected).


texts:
I'm implementing an Entity-Component-System. Basically, an entity is a wrapper around an ID, a component is just a POD struct, and a System is a class that does work on components corresponding to the Entities subscribed to that system.

I'd like to take advantage of smart pointers, so the cleanup will be much smoother. However, I've ended up with a rather ugly looking function for getting the component attached to an Entity.

For this implementation of an ECS, all components inherit from a base struct just simply called Component. Entities have a hashmap of the components they own, with that components ID as the key.

Entity.h

//Forward declaration
struct Component;

struct Entity
{
    /*A unique ID*/
    const unsigned long long int kID;

    /*A map containing all components held by this entity*/
    std::map<int, std::shared_ptr<Component>> component_map_;
};

Component.h

typedef int COMPONENT_ID;

/*Base component class*/
struct Component
{
    static const COMPONENT_ID kTYPE = -1;
};

/*Components*/
struct CPosition : public Component
{
    static const COMPONENT_ID kTYPE = 0;
    float x, y;
};

EntityManager.h templated function for getting a component from an entity

/*Gets a component from an entity*/
template<typename T>
std::shared_ptr<T> GetComponent(std::shared_ptr<Entity> entity) {
    return std::shared_ptr<T>((T*)entity->component_map_.at(T::kTYPE).get());
}

An example of how it would be used:

int main(int argc, char** argv)
{
    /*Create the entitymanager*/
    EntityManager em;
    /*Create an entity*/
    auto a = em.CreateEntity();
    /*Create a new Position Component*/
    /*struct CPosition : public Component */
    auto component = new CPosition();
    component->x = 55;
    component->y = 200;
    /*Add the CPosition component to the entity a*/
    em.AddComponent(a, component);
    /*Get the component back from the entity a*/
    auto mcomponent = em.GetComponent<CPosition>(a);
    return 0;
}

Is there a cleaner way to get the component back from the entity, without grabbing the raw pointer and casting it and sticking it back into a shared_ptr? What about having the static ID of components and using that to map the components in the entity? Any better solutions?


texts: Method to draw an open or closed polyline

texts:
I'm writing a simple 2D rendering class.

Below is the method that creates a polyline. A polyline can be closed or not according to a parameter closed.

void Polyline(IList<Point> points, bool closed)
{
    var pointsCount = points.Count;
    if (pointsCount < 2)
        return;

    int count = pointsCount;
    if (!closed)
        count = pointsCount - 1;

    for (int i1 = 0; i1 < count; i1++)
    {
        int i2 = (i1 + 1) == pointsCount ? 0 : i1 + 1;
        Point p1 = points[i1];
        Point p2 = points[i2];
        AddLineSegment(p1, p2);
    }
}

But I think the parameter name closed is a little confusing. It may have two different meanings:

points have already closed the polyline: points[0] == point[points.Count-1]
points doesn't close the polyline and we will add the first point to rendered polyline in the method

I want to make it clear to users that the second is the right meaning. So what kind of name should I use?

I think closesIt or something similar should be better but it still sounds ambiguous. What's your opinion?


texts: Checking for circular primes - Project Euler Problem 35

texts:
I have been trying to solve Project Euler problem number 35. The problem is to find:

  How many circular primes are there below one million?

A circular prime is a prime where all the rotations of its digits are primes too. E.g. 197 is a circular prime as 197, 971, and 719 are all primes too.

Initially I was "brute-forcing" it by checking for divisors of the number up to its square root. However, I've realised how inefficient this was when the browser crashed above 10,000. Then I've tried using the Sieve of Eratosthenes, and now it can calculate the primes up to 1 million in ~4 seconds.

However, the part that is still to inefficient is the function to check if the primes are circular. I have made a few changes, but the browser still crashes above 100,000. What extra changes can I make?

//Erastosthenes sieve
function findPrimes(range) {
    var numbers = [1];
    var prime = 2;
    var primesArr = [2]

    //Stop searching once prime is as big as range
    while(prime < range) {
        //Fill var numbers all mulitipes of prime
        for(var i = 1; i <= range && prime * i <= range ; ++i) {
            numbers[prime * i - 1] = prime * i;
        }
        //Move onto the next prime
        while(numbers[prime-1] !== undefined) {
            ++prime;
        }
        //Add current prime to the array
        primesArr.push(prime);
    }
    //Delete the last prime which is bigger than range
    primesArr.pop();
    return primesArr;

}

//Find primes within range which are circular primes
function findCircularPrimes(range) {
    //Generate array of primes from findPrimes array
    var primes = findPrimes(range);
    var circularPrimes = [];
    //Loop through each of the primes
    for(var i = 0; i < primes.length; ++i) {
        //Split current prime into an array
        var numArr = primes[i].toString().split('');
        var truefalse = true;
        //Cycle through each of the digits and check if its a prime (and therefore a member of primes)
        for (var j = 0; j < numArr.length; ++j) {
            numArr.push(numArr.shift());
            //If current rotation is not a prime break the loop and 'return' false
            if (primes.indexOf(Number(numArr.join(''))) === -1) {
                truefalse = false;
                break;
            }
        }
        //If truefalse variable is true add circular prime to array
        if(truefalse) {
            circularPrimes.push(primes[i]);
        }

    }

    return circularPrimes;
}
var then = Date.now();
var primes = findCircularPrimes(100000).length
alert(primes);