texts:
I created simple quicksort algorithm using iterators. I'm happy with implementation but not with its speed. Profiler tells me that the slowest operation here is std::iter_swap. I don't really know how to improve this code. Note that I am not asking for different kind of quicksort algorithm but about improving this one.

#include <iterator>
#include <utility>
#include <functional>

template <typename RandomIt, typename Compare = std::less_equal<>>
void Sort::QuickSort(RandomIt first, RandomIt last, Compare compare = Compare())
{
    if (std::distance(first, last) <= 1) return;
    RandomIt pivot = Partition(first, last, compare);
    QuickSort(first, pivot, compare);
    QuickSort(std::next(pivot, 1), last, compare);
}

template <typename RandomIt, typename Compare>
RandomIt Sort::Partition(RandomIt first, RandomIt last, Compare compare)
{
    RandomIt pivot = MedianOf3(first, last, compare);
    RandomIt i = first;
    for (RandomIt j = first; j != pivot; ++j)
        if (compare(*j, *pivot)) {
            std::iter_swap(i, j);
            ++i;
        }
    std::iter_swap(i, pivot);
    return i;
}

template <typename RandomIt, typename Compare>
RandomIt Sort::MedianOf3(RandomIt first, RandomIt last, Compare compare)
{
    auto collectionSize = std::distance(first, last);
    RandomIt middle = std::next(first, collectionSize / 2);
    RandomIt targetPivot = std::prev(last, 1);
    if (compare(*middle, *first))
        std::iter_swap(middle, first);
    if (compare(*targetPivot, *first))
        std::iter_swap(targetPivot, first);
    if (compare(*middle, *targetPivot))
        std::iter_swap(middle, targetPivot);
    return targetPivot;
}

For testing I used vectors of 100 000 random elements. This code average sorting time is about 1626 ms on my machine and std::sort on the same set of data performs 10 times better, around 163 ms. I know that std::sort uses introsort but still quicksort shouldn't be 10 times slower.


texts:
I receive a string with a lot of characters that I don't need and I am trying to remove them and replace them with characters that I am able to work with. My current structure has me redefining the var multiple times which I feel is not efficient and can probably be done better. Please let me know of a more effective way I can do this.

I define the date then remove the unwanted characters then append the date to the "clean" string with and underscore:

var d = Date.now()
  var article = a.replace(/ |\./g, "_")
  article = article.replace(/\r?\n|\r/g,"")
  article = article.replace(/\$|\#|\[|\]/g, "")
  article = d + "_" + article

This does work but I am curious if there is a better way.


texts: Schema design for user profile management

texts:
Is this a good design? I would like to create a view on user table to get a list of users based on filtered conditions. Please suggest ways of improving this schema design.

A view example that I would like to add on this schema:

Select all users who belong to group and all groups created after that group.
Select all users with last message, status, location and media URLs included.

CREATE TABLE IF NOT EXISTS users
  (
     userid     TEXT NOT NULL,
     name       TEXT NULL,
     lmessage   INTEGER NULL,
     statusid   INTEGER NULL,  /* statusid should refer to last status of the user in status table*/
     locationid INTEGER NULL,  /* locationid should refer to last status of the user in locations table */
     registered INTEGER NOT NULL,
     tinypic    INTEGER NULL /* this refers to media id in media table */,
     largepic   INTEGER NULL /* this also refers to media id in media table */,
     groupid    INTEGER NULL /* this refers to id in groups table */ ,
     PRIMARY KEY (userid)
  );

CREATE TABLE IF NOT EXISTS locations
  (
     serial INTEGER,
     locationid TEXT NOT NULL,
     userid TEXT NOT NULL,
     time     INTEGER NULL,
     PRIMARY KEY (serial)
  );

CREATE TABLE IF NOT EXISTS status
  (
     serial INTEGER,
     userid TEXT NULL,
     message  TEXT NOT NULL,
     time     INTEGER NULL,
     PRIMARY KEY (serial)
  );

CREATE TABLE IF NOT EXISTS messages
  (
     sno       INTEGER,
     messageid INTEGER NOT NULL,
     sender    TEXT NOT NULL,
     receiver  TEXT NOT NULL,
     time      INTEGER NULL,
     message   TEXT NULL,
     image     INTEGER NULL,
     video     INTEGER NULL,
     audio     INTEGER NULL,
     PRIMARY KEY (sno)
  );

CREATE TABLE IF NOT EXISTS media
  (
     mediaid    TEXT NOT NULL UNIQUE,
     url        TEXT NULL,
     downloaded INTEGER NULL,
     thumbnail  TEXT NULL,
     PRIMARY KEY (mediaid)
  );

CREATE TABLE IF NOT EXISTS groups
  (
     serial INTEGER,
     name TEXT NOT NULL,
     id INTEGER NOT NULL
     PRIMARY KEY(serial)
  );

CREATE UNIQUE INDEX IF NOT EXISTS id_unique ON users (userid ASC);

CREATE UNIQUE INDEX IF NOT EXISTS serial_unique ON status (serial ASC);

CREATE UNIQUE INDEX IF NOT EXISTS id_unique ON messages (sno ASC);

CREATE UNIQUE INDEX IF NOT EXISTS serial_unique ON patterns (serial DESC);

CREATE UNIQUE INDEX IF NOT EXISTS mediaid_unique ON media (mediaid ASC);


texts: Making an iterative formula shorter in Excel

texts:
I have just typed out very long code in Excel that just repeats itself 17 times, but each time with different cell references. Is there a more concise way of writing my code, perhaps by using another column of cells?

The code that is repeated is:

=IF(OR(ISBLANK(X2),ISBLANK(Y2)),0,X2/VLOOKUP(YEAR(Y2),$EU$3:$FH$57,14,FALSE))*$FH$57

This figure is then added on to other figures which are obtained from columns further along. I am finding the real value of the cost of alien clearing in 2014 prices over a number of years. In the first example, the column X2 holds the nominal value of the alien clearing in year YEAR(Y2), array $EU$3:$FH$57 holds the CPI data, with the year in column 1 (EU) and the average index for each year in column 14 (FH). $FH$57 holds the CPI index for 2014.

I copied the following formula down an entire column to get the real value of clearing for a number of different projects:

=IF(OR(ISBLANK(X2),ISBLANK(Y2)),0,X2/VLOOKUP(YEAR(Y2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(AE2),ISBLANK(AF2)),0,AE2/VLOOKUP(YEAR(AF2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(AL2),ISBLANK(AM2)),0,AL2/VLOOKUP(YEAR(AM2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(AS2),ISBLANK(AT2)),0,AS2/VLOOKUP(YEAR(AT2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(AZ2),ISBLANK(BA2)),0,AZ2/VLOOKUP(YEAR(BA2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(BG2),ISBLANK(BH2)),0,BG2/VLOOKUP(YEAR(BH2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(BN2),ISBLANK(BO2)),0,BN2/VLOOKUP(YEAR(BO2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(BU2),ISBLANK(BV2)),0,BU2/VLOOKUP(YEAR(BV2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(CB2),ISBLANK(CC2)),0,CB2/VLOOKUP(YEAR(CC2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(CI2),ISBLANK(CJ2)),0,CI2/VLOOKUP(YEAR(CJ2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(CP2),ISBLANK(CQ2)),0,CP2/VLOOKUP(YEAR(CQ2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(CW2),ISBLANK(CX2)),0,CW2/VLOOKUP(YEAR(CX2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(DD2),ISBLANK(DE2)),0,DD2/VLOOKUP(YEAR(DE2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(DK2),ISBLANK(DL2)),0,DK2/VLOOKUP(YEAR(DL2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(DR2),ISBLANK(DS2)),0,DR2/VLOOKUP(YEAR(DS2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(DY2),ISBLANK(DZ2)),0,DY2/VLOOKUP(YEAR(DZ2),$EU$3:$FH$57,14,FALSE))*$FH$57+IF(OR(ISBLANK(EF2),ISBLANK(EG2)),0,EF2/VLOOKUP(YEAR(EG2),$EU$3:$FH$57,14,FALSE))*$FH$57


texts:
I'm using $window.scroll twice in a jQuery script for different uses. Should I try and combine them?

jQuery(document).ready(function($) {

// Add class .fixed when .site-header hits top of viewport

var distance = $('.site-header').offset().top,
    $window = $(window);

$window.scroll(function() {
    if ( $window.scrollTop() >= distance ) {
        $(".site-header").addClass("fixed");
    }
    if ( $window.scrollTop() < distance ) {
        $(".site-header").removeClass("fixed");
    }
});

// Add class .scrolled on scroll to control show/hide of back-top button

$(window).scroll(function() {
    var scroll = $(window).scrollTop();

    if (scroll >= 200) {
        $("body").addClass("scrolled");
    }
    if (scroll <= 200) {
        $("body").removeClass("scrolled");
    }
});

});


texts: VersionString (eg "1.0.2") IComparer algorithm

texts:
For a project I'm working on, I need to compare version strings where:

version strings are composed only of numbers and periods
version strings are made up of an arbitrary number of segments (major.minor, manjor.minor.build, etc)
1.0 is equivalent to 1.0.0, etc.

Please provide feedback on performance considerations or style.

Here's my shot at it:

public class VersionStringComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        if (x == y) return 0;

        var xparts = x.Split('.');
        var yparts = y.Split('.');

        var length = new[] {xparts.Length, yparts.Length}.Max();

        for (var i = 0; i < length; i++)
        {
            int xint;
            int yint;

            if (!Int32.TryParse(xparts.ElementAtOrDefault(i), out xint)) xint = 0;
            if (!Int32.TryParse(yparts.ElementAtOrDefault(i), out yint)) yint = 0;

            if (xint > yint) return 1;
            if (yint > xint) return -1;
        }

        //they're equal value but not equal strings, eg 1 and 1.0
        return 0;
    }
}

And two quick unit tests (using the Should.Fluent library:

    [TestMethod]
    public void VersionStringComparerSortsMajorAndMinorVersionString()
    {
        var versions = new[]{"1.5","1.0","2.0","1.0"};
        var versionStringComparer = new ReleaseGateway.Util.VersionStringComparer();

        var sorted = versions.OrderBy(x => x, versionStringComparer).ToArray();

        var expected = new[] {"1.0", "1.0", "1.5", "2.0"};
        sorted.Should().Equal(expected);
    }

    [TestMethod]
    public void VersionStringComparerSortsArbitraryRadixVersionStrings()
    {
        var versions = new[] { "1.5.4", "1.7", "1.0", "1","1.4" };
        var versionStringComparer = new ReleaseGateway.Util.VersionStringComparer();

        var sorted = versions.OrderBy(x => x, versionStringComparer).ToArray();

        var expected = new[] { "1.0", "1", "1.4", "1.5.4", "1.7" };
        sorted.Should().Equal(expected);
    }


texts:
I'm writing an application which works with huge amounts of sequential data, and often found the need to use std::transform. I see two potential improvements to std::transform:

Allow for variable number parameters.
Take advantage of the linear separability of the data by multithreading.

Can anyone suggest any design/performance improvements on my implementation?

threaded_transform.h

#include <vector>
#include <thread>

template<typename InputIterator, typename OutputIterator,
         typename Function, typename... Params>
OutputIterator
trans(InputIterator first, InputIterator last, OutputIterator result,
          Function f, Params... params)
{
    for (; first != last; ++first, ++result)
        *result = f(*first, params...);
    return result;
}

template<typename InputIterator, typename OutputIterator,
         typename Function, typename... Params>
OutputIterator
threaded_transform(unsigned num_threads, InputIterator first,
                   InputIterator last, OutputIterator result,
                   Function f, Params... params)
{
    std::size_t num_values = last - first;
    std::size_t num_values_per_threads = num_values / num_threads;

    std::vector<std::thread> threads;
    threads.reserve(num_threads);

    for (unsigned i = 1; i <= num_threads; ++i) {
        if (i == num_threads) {
            // The last thread processes the remaining values.
            threads.push_back(std::thread(trans<InputIterator, OutputIterator, Function, Params...>,
                                          first, last, result, f, params...));
        } else {
            threads.push_back(std::thread(trans<InputIterator, OutputIterator, Function, Params...>,
                                          first, first + num_values_per_threads, result, f, params...));
        }
        first  += num_values_per_threads;
        result += num_values_per_threads;
    }

    for (auto& thread : threads)
        thread.join();

    return result;
}

main.cpp

#include <vector>
#include "threaded_transform.h"

int main()
{
    auto sum = [] (int a, int b) { return a + b; };

    std::vector<int> values = {1,2,3,4,5,6,7,8,9,10};
    std::vector<int> results;
    results.resize(10);

    threaded_transform(4, values.cbegin(), values.cend(), results.begin(), sum, 10);

    for (auto result : results) {
        std::cout << result << std::endl;
    }
}


texts:
Problem

  Find 2nd degree connections ( friends’ friends), output these 2nd
  degree connections ranked by number of common friends (i.e 1st degree
  connections) with you, (example: if 2nd degree connection A has 10
  common friends (1st degree connections) with you but 2nd degree
  connection B has 8 common friends (1st degree connections)with you,
  then A should be ranked first) Input is your connection graph
  represented by undirected graph nodes, output is list of 2nd degree
  connections represented by graph nodes.

My current solution is to find all first degree connection, and then for each first degree connection, I try to find true (i.e. not overlapped with first degree connection) second degree connection, and build a common connection (between self, and 2nd degree connection) frequency dictionary. Finally, I sort the dictionary by frequency.

I'm wondering if any advice of more efficient implementation (in terms of algorithm time complexity), bugs in my code and code style advice. I am thinking of using BFS could improve performance (in terms of algorithm time complexity) for this problem.

from collections import defaultdict
class Graph:
    def __init__(self):
        self.out_neighbour = defaultdict(list)
    def add_edge(self, from_node, to_node):
        self.out_neighbour[from_node].append(to_node)
        self.out_neighbour[to_node].append(from_node)
    def second_order_rank(self, from_node):
        first_order_set = set()
        for n in self.out_neighbour[from_node]:
            first_order_set.add(n)
        second_order_rank = defaultdict(int) # key: node id, value: count of common first order connection
        for first_order_node in self.out_neighbour[from_node]:
            for second_order_node in self.out_neighbour[first_order_node]:
                if second_order_node == from_node or second_order_node in first_order_set:
                    continue
                for second_order_neighbour in self.out_neighbour[second_order_node]:
                    if second_order_neighbour in first_order_set:
                        second_order_rank[second_order_node] += 1
        rank_order = []
        for n,c in second_order_rank.items():
            rank_order.append((c,n))
        rank_order = sorted(rank_order, reverse=True)
        result = []
        for r in rank_order:
            result.append(r[1])
        return result
if __name__ == "__main__":
    g = Graph()
    edges = [(1,2),(1,3),(2,3),(2,4),(2,5),(3,4),(3,6),(4,7),(4,10),(5,9),(6,8)]
    for e in edges:
        g.add_edge(e[0],e[1])
    print g.second_order_rank(1)


texts: Reading and returning the HTML content from a specified URL

texts:
I wanted to create a simple function that can read and return the HTML content from a specified URL. This is what reading here and there lead me to:

from socket import timeout
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError

def get_html_content(url, max_attempt = 3):
    req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    content = ""
    attempt = 1
    while True:
        try:
            html_page = urlopen(req, timeout=10)
            content = html_page.read()
        except (HTTPError, URLError, timeout) as e:
            if isinstance(e, HTTPError):
                print("The server couldn\'t fulfill the request....attempt %d/%d" % (attempt, max_attempt))
                print('Error code: ', e.code)
            if isinstance(e, URLError):
                print("We failed to reach a server....attempt %d/%d" % (attempt, max_attempt))
                print('Reason: ', e.reason)
            if isinstance(e, timeout):
                print('timeout...attempt %d/%d' % (attempt, max_attempt))
            attempt += 1
            if attempt > max_attempt:
                break
            continue
        else:
            break

    return content

I would use this function to parse the content of many URLs. For if content = "", I would raise a random exception after writing to some file whatever I had already successfully gathered.


texts: Adding two different group of objects to a list of groups

texts:
I have two lists of Strings and two lists of objects. I need to copy the list of strings to the correspondence list of objects with specific values.

public List<Name> retrieveCitiesAndCountries(){
  List<String> list1 = new ArrayList<String>(); //list of cities
  List<String> list2 = new ArrayList<String>(); //list of countries
  list1 = retrieveListOfCities();
  list2 = retrieveListOfCountries();

  List<Name> namesList = new ArrayList<Names>();
  return namesList;
}

Name class

class Name{
  String name;
  String attrib;
  ..
}

I need to add both lists to namesList and add the respective attribs. For example, all cities should have City as their attribs and all countries should have Country as their attribs.

 for(int i=0;i<list1.size();i++){
     Name name = new Name();
     name.setName(list1.get(i));
     name.setAttrib("City");
     namesList.add(name);
   }

  for(int i=0;i<list2.size();i++){
     Name name = new Name();
     name.setName(list2.get(i));
     name.setAttrib("Country");
     namesList.add(name);
   }

I am wondering if there is any better way to do it.

I use namesList to return in JSON format.

@RequestMapping(value = "/names")
public @ResponseBody List<Name> retrieveCitiesAndCountries(){
    List<Name> namesList = retrievalServ.retrieveCitiesAndCountries();
    return namesList;
}


texts: Copying a range from one file to another

texts:
Since it has too many loop, loop inside loop, this works very slowly. How can we increase the speed of this program? I am trying to copy a range from one file to another file of same name in different folder. I have folders named "A","B","C"..."G" inside c:\Charts\1\ and c:\Charts\0\. Inside each folder "A","B","C"..."G" there are files named 1,2,3,4,..10.

dim vArr1 as array
Dim vArr as array
vArr1 = Array("A", "B", "C", "D", "E", "F", "G")
vArr = Array("A", "B", "C", "D", "E", "F", "G")
Dim fileName1, Pathname1 As String
Pathname1 = "c:\Charts\1\"
Pathname="c:\charts\0\"
For Each vFile1 In vArr1
    fileName1 = Dir(Pathname1 & vFile1 & "\" & "*.xlsx")
    Do While fileName1 <> ""
        For Each vFile In vArr
            filename = Dir(Pathname & vFile & "\" & "*.xlsx")
            Do While filename <> ""
                If filename = fileName1 Then
                Set WB1 = Workbooks.Open(Pathname1 & vFile & "\" & fileName1)
                    WB1.Application.ScreenUpdating = False
                    WB1.ActiveSheet.Range("M1:M30").Copy
                    WB1.Close (False)
                Set WBD1 = Workbooks.Open(Pathname & vFile & "\" & filename)
                WBD1.ActiveSheet.Range("C1").Select
                    WBD1.ActiveSheet.Paste
                    WBD1.ActiveSheet.Cells(1, 3).Value = "HSI Q4 2014-15"
                    WBD1.Close (True)
                    filename = Dir()
                Else

                End If
                fileName1 = filename
            Loop
        Next
    Loop
Next


texts: Recursive and iterative implementation on Kosaraju algorithm

texts:
Kosaraju algorithm is mainly phrased as two recursive subroutines running postorder DFS twice to mark SCCs with linear time complexity O(V+E) below,

  For each vertex u of the graph, mark u as unvisited. Let L be empty.
  For each vertex u of the graph do Visit(u), where Visit(u) is the recursive subroutine:

                If u is unvisited then:

  1. Mark u as visited.

  2. For each out-neighbour v of u, do Visit(v).

  3. Prepend u to L.

  Otherwise do nothing.

  For each element u of L in order, do Assign(u,u) where Assign(u,root) is the recursive subroutine:

  If u has not been assigned to a component then:

  1. Assign u as belonging to the component whose root is root.

  2. For each in-neighbour v of u, do Assign(v,root).

  Otherwise do nothing.

Here is the recursive implementation in Python according to the above recipe,

def kosaraju(G):

    # postorder DFS on G to transpose the graph and push root vertices to L

    N = len(G)
    T, L, U = [[] for _ in range(N)], [], [False] * N

    def visit(u):
        if not U[u]:
            U[u] = True
            for v in G[u]:
                visit(v)
                T[v].append(u)
            L.append(u)

    for u in range(N):
        visit(u)

    # postorder DFS on T to pop root vertices from L and mark SCCs

    C = [None] * N

    def assign(u, r):
        if U[u]:
            U[u] = False
            C[u] = r
            for v in T[u]:
                assign(v, r)

    while L:
        u = L.pop()
        assign(u, u)

    return C

The following iterative implementation scales well against the stack overflow due to excessively deep recursion, however it deserves to be shared for further improvement. I'll be glad about all your opinions or alternative implementations.

@EDIT

The inner loop of the first iterative DFS has been corrected below, so the linear time complexity O(V+E) is guaranteed now.

def kosaraju(G):

    # postorder DFS on G to transpose the graph and push root vertices to L
    N = len(G)
    T, L, U = [[] for _ in range(N)], [], [False] * N
    for u in range(N):
        if not U[u]:
            U[u], S = True, [u]
            while S:
                u, done = S[-1], True
                for v in G[u]:
                    T[v].append(u)
                    if not U[v]:
                        U[v], done = True, False
                        S.append(v)
                        break
                if done:
                    S.pop()
                    L.append(u)

    # postorder DFS on T to pop root vertices from L and mark SCCs
    C = [None] * N
    while L:
        r = L.pop()
        S = [r]
        if U[r]:
            U[r], C[r] = False, r
        while S:
            u, done = S[-1], True
            for v in T[u]:
                if U[v]:
                    U[v] = done = False
                    S.append(v)
                    C[v] = r
                    break
            if done:
                S.pop()

    return C

Test example:

G = [[1], [0, 2], [0, 3, 4], [4], [5], [6], [4], [6]]

print(kosaraju(G)) # => [0, 0, 0, 3, 4, 4, 4, 7]


texts: Counting SQL guids from a server log and printing stats

texts:
I've made a python script to get some statistics from a server log. I would like some guidelines to improve my code: make it run faster, be more pythonic, etc. I don't write python professionally (embedded developer) so please go easy on me.

Here's the code:

'''
Find the number of 'exceptions' and 'added' event's in the exception log
with respect to the device ID.

author: clmno
date: Dec 23 2018
'''

from time import time
import re

def timer(fn):
    ''' Used to time a function's execution'''
    def f(*args, **kwargs):
        before = time()
        rv = fn(*args, **kwargs)
        after = time()
        print("elapsed", after - before)
        return rv
    return f

def find_sql_guid(txt):
    ''' From the passed in txt, find the SQL guid using re'''
    re1 = '.*?'
    # SQL GUID 1
    re2='([A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12})'
    rg = re.compile(re1+re2, re.IGNORECASE|re.DOTALL)
    m = rg.search(txt)
    if m:
        guid1 = m.group(1)
        return guid1
    else:
        print("ERROR: No SQL guid in line. Check the code")
        exit(-1)

def find_device_IDs(path, element):
    """ Find the element (type: str) within the file (file path is
        provide as arg). Then find the SQL guid from the line at hand.
        (Each line has a SQL guid)
        Return a dict of {element: [<list of SQL guids>]}
    """
    lines = dict()
    lines[element] = []
    with open(path) as f:
        for line in f:
            if str(element) in line:
                #find the sql-guid from the line-str & append
                lines[element].append(find_sql_guid(line))
    return lines

# @timer
def find_num_occurences(path, key, search_val, unique_values):
    """ Find and append SQL guids that are in a line that contains a string
        that's in search_val into 'exception' and 'added'
        Return a dict of {'exception':set(<set of SQL guids>),
                        'added': set(<set of SQL guids>)}
    """
    lines = {'exception':set(), 'added': set()}

    with open(path) as f:
        for line in f:
            for value in unique_values:
                if value in line:
                    if search_val[0] in line:
                        lines['exception'].add(value)
                    elif search_val[1] in line:
                        lines['added'].add(value)
    return lines

def print_stats(num_exceptions_dict):
    for key in num_exceptions_dict.keys():
        print("{} added ".format(key) +
            str(len(list(num_exceptions_dict[key]["added"]))))
        print("{} exceptions ".format(key) +
            str(len(list(num_exceptions_dict[key]["exception"]))))

if __name__ == "__main__":
    path  = 'log/server.log'
    search_list = ['3BAA5C42', '3BAA5B84', '3BAA5C57', '3BAA5B67']

    #find every occurance of device ID and find their corresponding SQL
    # guids (unique ID)
    unique_ids_dict = dict()
    for element in search_list:
        unique_ids_dict[element] = find_device_IDs(path, element)

    #Now for each unique ID find if string ["Exception occurred",
    # "Packet record has been added"] is found in it's SQL guid list.
    search_with_in_deviceID = ["Exception occurred",
                                "Packet record has been added"]

    num_exceptions_dict = dict()
    for elem in search_list:
        num_exceptions_dict[elem] = find_num_occurences(path, elem,
            search_with_in_deviceID, list(unique_ids_dict[elem].values())[0])

    print_stats(num_exceptions_dict)

and here's a small server log for you to experiment on.
This code works but the time elapsed to run through a log file of 55000 lines is 42 seconds

$ time python findUniqueVal_log.py
 real   0m42.343s
 user   0m42.245s
 sys    0m0.100s

I would like guidelines on:

Optimizing in both coding and performance efficiency
Any better approach to this problem
I keep opening the file and closing it. Is that IO delay causing any noticeable delay?


texts:
The following code checks if a string is a pangram or not:

public static class StringExtensions
{
    public static IEnumerable<char> EnglishAlphabet
    {
        get
        {
            for (char i = 'a'; i <= 'z'; i++)
            {
                yield return i;
            }

            yield break;
        }
    }

    /// <summary>
    /// Check if the source string is a pangram.
    /// </summary>
    /// <remarks>
    /// The default dictionary is the english one.
    /// </remarks>
    public static bool IsPangram(this string source)
    {
        return source.IsPangram(EnglishAlphabet);
    }

    /// <summary>
    /// Check if the source string is a pangram.
    /// </summary>
    /// <remarks>
    /// A pangram is a string that uses every letter in the alphabet.
    /// </remarks>
    public static bool IsPangram(this string source, IEnumerable<char> alphabet)
    {
        var lowerAlphabet = new string(alphabet.ToArray()).ToLower();
        IDictionary<char, bool> alphabetCharacters = lowerAlphabet.ToDictionary(c => c, c => false);
        var lowerSource = source.ToLower();

        foreach(char character in lowerSource)
        {
            alphabetCharacters[character] = true;
        }

        return !alphabetCharacters.Values.Contains(false);
    }
}

Do you see anything wrong/weird in it? Any feedback (style, naming, performance) is more than welcome.


texts: Optimal use of Python 3's multiprocessing package to create Sudoku grids

texts:
I want to create Sudoku grids using Python. As it's pretty slow if I want to create a bunch of grids, I decided to use multiprocessing.

My computer has 4 virtual cores (dual core with hyper threading), so I expected the new program to run about 4 times faster, minus a little overhead maybe. But it's only almost exactly two times faster!

Here's the snippet where I use a multiprocessing.Pool:

worker_pool = Pool(processes)
sudokus = list(filter(None, worker_pool.imap_unordered(create_sudoku, range(n), n // processes)))

Why does this happen? Am I using the multiprocessing module in a wrong or inefficient way? Please help me to optimize it.

Here's the code of the main module, I guess the grid module is not important for multiprocessing performance:

import grid  # my own sudoku grid module

import subprocess
import random
import time
from multiprocessing import Pool, cpu_count

def create_sudoku(*_, level="medium"):
    numbers = list("123456789")

    random.shuffle(numbers)
    grid_string = "".join(numbers[:3]) + "." * 6 + "".join(numbers[3:6]) + "." * 6 + "".join(numbers[6:9]) + "." * 6
    random.shuffle(numbers)
    grid_string += "." * 3 + "".join(numbers[:3]) + "." * 6 + "".join(numbers[3:6]) + "." * 6 + "".join(numbers[6:9])
    random.shuffle(numbers)
    grid_string += "." * 9 + "".join(numbers[:3]) + "." * 6 + "".join(numbers[3:6]) + "." * 6 + "".join(numbers[6:9])
    g = grid.Grid(grid_string)
    g.try_to_solve()

    return (grid_string, level) if g.is_solved() else None

if __name__ == "__main__":

    n = int(input("How many grids to create? "))
    use_multiprocessing = None
    while use_multiprocessing is None:
        answer = input("Use multiprocessing to speed things up? (Y/n) ").strip().lower()
        if len(answer) == 1 and answer in "yn":
            use_multiprocessing = True if answer == "y" else False
    print()

    t0 = time.time()

    if use_multiprocessing:
        processes = cpu_count()
        worker_pool = Pool(processes)

        print("Creating {} sudokus using {} processes. Please wait...".format(n, processes))
        sudokus = list(filter(None,
                              worker_pool.imap_unordered(create_sudoku, range(n), n // processes)
                              ))
    else:
        progress_bar, progress_bar_length = 0, 10
        sudokus = []

        print("Creating {} sudokus".format(n), end="", flush=True)
        for i in range(n):
            p = int((i / n) * progress_bar_length)
            if p > progress_bar:
                print("." * (p-progress_bar), end="", flush=True)
                progress_bar = p
            new_sudoku = create_sudoku()
            if new_sudoku:
                sudokus.append(new_sudoku)
        print()

    t = time.time() - t0
    print("\nSuccessfully created {}/{} grids ({:.1f}%) in {:.3f}s (average {:.3f}s per grid)!".format(
        len(sudokus), n, 100*len(sudokus)/n, t, t/n
    ))

And here are two sample runs:

How many grids to create? 100
Use multiprocessing to speed things up? (Y/n) y

Creating 100 sudokus using 4 processes. Please wait...

Successfully created 100/100 grids (100.0%) in 19.507s (average 0.195s per grid)!

How many grids to create? 100
Use multiprocessing to speed things up? (Y/n) n

Loading 100 sudokus.........

Successfully created 100/100 grids (100.0%) in 37.675s (average 0.377s per grid)!


texts:
As a self-teaching Python beginner for almost 4 months, I have mostly been doing online challenges including Project Euler problems.

Problem 45 asks:

  Triangle, pentagonal, and hexagonal numbers are generated by the
  following formulae:

  $$
\begin{array}{lll}
\textrm{Triangle}   & T_n=n(n+1)/2  & 1, 3,  6, 10, 15, \ldots  \\
\textrm{Pentagonal} & P_n=n(3n−1)/2 & 1, 5, 12, 22, 35, \ldots  \\
\textrm{Hexagonal}  & H_n=n(2n−1)   & 1, 6, 15, 28, 45, \ldots \\
\end{array}
$$

  It can be verified that \$T_{285} = P_{165} = H_{143} = 40755\$.

  Find the next triangle number that is also pentagonal and hexagonal.

I encountered this problem and was able to write the code without much problem although most of the solutions I write are mostly brute force. However I am not satisfied with the time the coding took to find the answer as it took 530.7 seconds to find the solution with this code:

import time

startTime = time.time()

limit = 1000000
triangle = []
pentagonal = []
hexagonal = []
triangle_number = []

class Shape:
    def __init__(self, term):
        self.term = term

    def triangle(self):
        return int(self.term * (self.term + 1) / 2)

    def pentagonal(self):
        return int(self.term * (3 * self.term -1) / 2)

    def hexagonal(self):
        return int(self.term * (2 * self.term - 1))

def generate():
    for terms in range(1, limit + 1):
        product = Shape(terms)

        triangle.append(product.triangle())
        pentagonal.append(product.pentagonal())
        hexagonal.append(product.hexagonal())

def main():
    generate()

    for _, terms in enumerate(hexagonal):
        if len(triangle_number) == 3:
            break
        elif terms in triangle and terms in pentagonal:
            triangle_number.append(terms)
            print(terms)

    print(triangle_number)
    print(time.time() - startTime, "seconds")

main()

I am aware that using the class is unecessary for this problem, but I want to include it as a practice of getting used to writing solutions that require classes.

Alternatively I have re-written another solution of this problem without any classes included but keeping the same style, however it still took 328.2 seconds.

So what suggestions are there to improve the code to run faster? I have tried to review other solutions from the solution page however I do not understand how it was simplified so much to make it run more efficient.


texts:
I am very new to HTML and I've decided to attempt to put what I know into an HTML page that allows you to enter any amount of numbers, separated by commas, and would display the sum of all the numbers below.

<!DOCTYPE html>

  <title>Sum of two numbers</title>

Enter any amount of numbers separated by a comma:

  <input id="nums" type="text" required="required" />
  <button onclick="sum()">Get sum</button>

Are there any bad practices in my code?


texts:
I've written this Wack-a-mole like game on my iPad in the app Pythonista. Keeping that in mind, it uses a different graphic interface from the one I have usually seen used, Tkinter. I'm a beginner and this is my first major project, so any tips on how to code more efficiently and improve this program would be greatly appreciated.

import random
import ui
from time import sleep
import console
import sys

#turn button to "on" state
def turn_on(sender):
    sender.title = 'on'
    sender.tint_color = ('yellow')

#reverts button to off state
def turn_off(sender):
    sender.title = 'off'
    sender.tint_color = 'light blue'

#briefly turns button to "on" state then reverts button to       original     state
def blink(sender):
     turn_on(sender)
     sleep(.5)
     turn_off(sender)

#When the button is tapped, it reverses the current state of the button
def button_tapped(sender):
     turn_off(sender)

#Describes game
def game_Description():
    console.alert("Game objective:", 'Click the button to turn the   light off before the next light turns on', "Ready")

#Pops up when user loses game
def game_Over_Alert():
    play_Again = console.alert('Game Over!','','Play Again?')
    return play_Again

#Checks to see if all lights are off
def check_Lights():
    if button.title == 'off':
        if button2.title == 'off':
            if button3.title == 'off':
                if button4. title == 'off':
                        return True

#Turns off all lights
def all_Off():
    turn_off(button)
    turn_off(button2)
    turn_off(button3)
    turn_off(button4)

#Increase score by 1 and display
def increase_Score(x):
    x += 1
    score.title = 'Score: %d' % x
    return x

#setting UI and buttons
view = ui.View()
view.name = 'Light Panel'
view.background_color = 'black'

button = ui.Button(title = 'off')
button.center = (view.width*.2, view.height*.5)
button.flex = 'LRTB'
button.action = button_tapped
view.add_subview(button)

button2 = ui.Button(title = 'off')
button2.center = (view.width*.4, view.height*.5)
button2.flex = 'LRTB'
button2.action = button_tapped
view.add_subview(button2)

button3 = ui.Button(title = 'off')
button3.center = (view.width*.6, view.height*.5)
button3.flex = 'LRTB'
button3.action = button_tapped
view.add_subview(button3)

button4 = ui.Button(title = 'off')
button4.center = (view.width*.8, view.height*.5)
button4.flex = 'LRTB'
button4.action = button_tapped
view.add_subview(button4)

scoreCount = 0

score = ui.Button(title = 'Score: 0')
score.center = (view.width*.5, view.height*.75)
score.flex = 'LRTB'
view.add_subview(score)

#Set up display
view.present('sheet')

#Runs the game and handles the function
def play_Game():

    scoreCount = 0
    speed = 2.55
    game_Description()
    random.seed()
    while check_Lights():
     x = random.randint(0,3)
        if x == 0:
            turn_on(button)
        elif x == 1:
            turn_on(button2)
        elif x == 2:
            turn_on(button3)
        else:
            turn_on(button4)
        scoreCount = increase_Score(scoreCount)
        sleep(speed)
        if speed >= 1.5:
        speed-= .07

    if game_Over_Alert():
        all_Off()
        play_Game()

play_Game()
view.close()


texts: Buying bunnies, eggs and elections with tax and a discount

texts:
I have a small problem with the code below. Although it executes the task perfectly, It's supposed to be more "short" and compact according to the assignment feedback. I was wondering if someone can please help me change my code from its longer version to a more short version that completes the same task...

from math import *

ask = input("Would you like to buy bunnies, eggs, or elections?: ")
if ask == 'bunnies':
    bunnies = 30
    print("Bunnies will cost " + str(bunnies) + " dollars each.")
    quantity = int(input("How many would you like to buy?: "))
    cost1 = bunnies * quantity
    tax = (0.089 * cost1)
    final_cost = (cost1 + tax)
    if final_cost == final_cost > 57.89:
        discount1 = (0.13 * final_cost)
        with_discount1 = (final_cost - discount1)
        print("Your total will be " + str(with_discount1) + " with a discount.")
    if final_cost == final_cost < 57.89:
        print("Your total will be " + str(final_cost) + " without a discount")

if ask == 'eggs':
    eggs = 10
    print("Eggs will cost " + str(eggs) + " dollars each.")
    quantity = int(input("How many would you like to buy?: "))
    cost2 = eggs * quantity
    tax = (0.089 * cost2)
    final_cost2 = (cost2 + tax)
    if final_cost2 == final_cost2 > 57.89:
        discount = (0.13 * final_cost2)
        with_discount = (final_cost2 - discount)
        print("Your total will be " + str(with_discount) + " with a discount for eggs.")
    if final_cost2 == final_cost2 < 57.89:
        print("Your total will be " + str(final_cost2) + " without a discount for eggs")

if ask == 'elections':
    elections = 20
    print("Elections will cost " + str(elections) + " dollars.")
    quantity = int(input("How many are you looking to purchase?: "))
    cost3 = quantity * elections
    tax = (0.089 * cost3)
    final_cost3 = (cost3 + tax)
    if final_cost3 == final_cost3 > 57.89:
        discount2 = (0.13 * final_cost3)
        withdiscount = (final_cost3 - discounts)
        print("Your total will be " + str(withdiscount) + " dollars with a discount for elections.")
    if final_cost3 == final_cost3 < 57.89:
        print("Your total will be " + str(final_cost3) + " dollars without a discount for elections.")


texts:
This is primarily a container for quicksort and mergesort:

#include "c_arclib.cpp"
template <class T> class dynamic_array
  {
  private:
    T* array;
    T* scratch;
  public:
    int size;
    dynamic_array(int sizein)
      {
      size=sizein;
      array = new T[size]();
      }
    void print_array()
      {
      for (int i = 0; i < size; i++) cout << array[i] << endl;
      }
    void merge_recurse(int left, int right)
      {
      if(right == left + 1)
        {
        return;
        }
      else
        {
        int i = 0;
        int length = right - left;
        int midpoint_distance = length/2;
        int l = left, r = left + midpoint_distance;
        merge_recurse(left, left + midpoint_distance);
        merge_recurse(left + midpoint_distance, right);
        for(i = 0; i < length; i++)
          {
          if((l < (left + midpoint_distance)) && (r == right || array[l] > array[r]))
            {
            scratch[i] = array[l];
            l++;
            }
          else
            {
            scratch[i] = array[r];
            r++;
            }
          }
        for(i = left; i < right; i++)
          {
          array[i] = scratch[i - left];
          }
        }
      }
    int merge_sort()
      {
      scratch = new T[size]();
      if(scratch != NULL)
        {
        merge_recurse(0, size);
        return 1;
        }
      else
        {
        return 0;
        }
      }
    void quick_recurse(int left, int right)
      {
      int l = left, r = right, tmp;
      int pivot = array[(left + right) / 2];
      while (l <= r)
        {
        while (array[l] < pivot)l++;
        while (array[r] > pivot)r--;
        if (l <= r)
          {
          tmp = array[l];
          array[l] = array[r];
          array[r] = tmp;
          l++;
          r--;
          }
        }
      if (left < r)quick_recurse(left, r);
      if (l < right)quick_recurse(l, right);
      }
    void quick_sort()
      {
      quick_recurse(0,size);
      }
    void rand_to_array()
      {
      srand(time(NULL));
      int* k;
      for (k = array; k != array + size; ++k)
        {
        *k=rand();
        }
      }
  };
int main()
  {
  dynamic_array<int> d1(10);
  cout << d1.size;
  d1.print_array();
  d1.rand_to_array();
  d1.print_array();
  d1.merge_sort();
  d1.print_array();
  }


texts: How can I re-write the conditionals for this button

texts:
I've been trying out React for a couple of days and I tried to build a button component with some conditionals. Everything works but I feel like I am not doing it the right way.
Could anyone review my code and give me some advice and tell me what I am doing wrong or what i could do better. I think I am using too many return statements.
I use Typescript for type checking.
Would appreciate any help!
import { Link } from "gatsby";

interface Props {
  content: string,
  color: string,
  path?: string,
  type?: string
}

export default function Button(props: Props): JSX.Element {
  let elementType;

  const checkProps = () => {
    const availableColors = ['blue', 'grey', 'dark-grey', 'white'];
    const borderColors = [''];
    let buttonStyle;

    // Check if props.color exists in availableColors
    if(!availableColors.includes(props.color)) {
      console.error(`"${props.color} color key not available for button style`);
      return;
    }

    buttonStyle = buttonStyles[`button--${props.color}`];

    // Check if props.content is valid
    if(!props.content) {
      console.error(`"${props.content} is not valid as content`);
      return;
    }

    // Check if props.type exists
    if(!props.type) {
      elementType = <button className={ buttonStyle }>{ props.content }</button>
      return;
    }

    if(props.type !== 'link') {
      console.error(`"${props.type} is not a valid type, use 'link' instead or use nothing for default button`);
      return;
    } else {
        elementType = <Link to={ props.path } className={ buttonStyle }>{ props.content }</Link>
    }
  }

  checkProps();

  return (
    <React.Fragment>
      { elementType }
    </React.Fragment>
  );
}```


texts: Empty Line delimiter, single line output

texts:
I'm used to only processing, at most, one line of a file at a time. This is my first time changing the delimiter, and the objective here is to take a file containing lines such as:

Bubbles,
Blossom and
Buttercup

Nostalgic
Examples for the
Win.

Quick Brown Fox
Jumping Over Lazy
Dog.

and produce single line output:

Bubbles, Blossom and Buttercup.
Nostalgic Examples for the Win.
Quick Brown Fox Jumping Over Lazy Dog.

What I've done works, but it feels like a work-around to some superior alternative I'm sure exists. What do you think?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.regex.Pattern;
import java.util.Scanner;

public class TestDelim {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File(args[0]))
            .useDelimiter(Pattern.compile("^\\s*$", Pattern.MULTILINE));
        Scanner output;
        StringBuilder sb = new StringBuilder();

        while (input.hasNext()) {
            output = new Scanner(input.next());

            while (output.hasNextLine()) {
                sb.append(' ').append(output.nextLine());
            }

            System.out.println(sb.toString().trim());
            sb.setLength(0);
        }
    }
}


texts: VBA code that compares 2 sheets from different systems

texts:
In this code looking at 2 worksheets that contain similar data from different systems. Column 1 contains a unique staff number so there can be a match on the person then here there is a difference in NiNo ws1.cell(,17) and ws2.cell(,24)(This criteria will change each time the full code is run, to find Date of birth for example) between the sheets then certain values for that person are copied to a 3rd sheet.

How ever there are to be 18 different worksheets all looking at different criteria, so this code will have to run 18 times and will take a while. any ideas how I can speed it up examples please.

I think the answer is to use arrays but I'm not great with arrays.

Sub NINODifferences()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
    Dim i As Long, j As Long, iCol As Long, iRow As Long, sl1 as Long, sl2 as Long

sl1 = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row
sl2 = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row

    Set ws1 = ActiveWorkbook.Sheets("SheetA")
    Set ws2 = ActiveWorkbook.Sheets("SheetB")
    Set ws3 = ActiveWorkbook.Sheets("NINO Differences")' this will be a different sheet as I change the criteria

    iRow = 2
    iCol = 1

        For i = 1 To sl1
            For j = 1 To sl2

                If Trim(ws1.Cells(i, 1).Value2) = Trim(ws2.Cells(j, 1).Value2) Then

                    If Trim(ws1.Cells(i, 17).Value2) <> Trim(ws2.Cells(j, 24).Value2) Then 'this is the criteria so if different.

                        ws3.Cells(iRow, iCol).Value2 = ws1.Cells(i, 1).Value2
                        iCol = iCol + 1
                        ws3.Cells(iRow, iCol).Value2 = ws1.Cells(i, 2).Value2
                        iCol = iCol + 1
                        ws3.Cells(iRow, iCol).Value2 = ws1.Cells(i, 3).Value2
                        iCol = iCol + 1
                        ws3.Cells(iRow, iCol).Value2 = ws1.Cells(i, 17).Value2
                        iCol = iCol + 1
                        ws3.Cells(iRow, iCol).Value2 = ws2.Cells(j, 24).Value2

                        iCol = 1
                        iRow = iRow + 1

                    Else
                    End If

                Else
                End If

            Next j
       Next i

    Set ws1 = Nothing
    Set ws2 = Nothing
    Set ws3 = Nothing

    End Sub


texts:
This is my first attempt at learning binary trees. I'm looking for general feedback on how it could be made cleaner, more idiomatic, or more efficient.

data Tree a = ETree | Node (Tree a) (Tree a) a deriving (Show)

tInsert :: Ord o => o -> Tree o -> Tree o
tInsert x ETree = Node ETree ETree x
tInsert x (Node lT rT a)
    | x < a = Node (tInsert x lT) rT a
    | x > a = Node lT (tInsert x rT) a
    | otherwise = Node lT rT x

-- Folds a list into a tree
tFromListL, tFromListR :: Ord o => [o] -> Tree o
tFromListL = foldl (flip tInsert) ETree
tFromListR = foldr tInsert ETree

-- Turns a tree into a list
tToList :: Ord o => Tree o -> [o]
tToList ETree = []
tToList (Node lT rT a) = (tToList lT) ++ [a] ++ (tToList rT)

-- Splits a list roughly in half (as part of balancing)
splitInHalf :: [a] -> ([a],[a])
splitInHalf xs = splitAt (round $ (fromIntegral $ length xs) / 2.0) xs

-- Returns how unbalanced a node is
tUnbalancedBy :: Tree a -> Int
tUnbalancedBy ETree = 0
tUnbalancedBy (Node lT rT _) = absDiff (tDepth lT) (tDepth rT)

-- Arranges a list in such a way that it forms a more balanced tree
balanceList :: [a] -> [a]
balanceList xs = let (fH,sH) = splitInHalf xs in (reverse fH) ++ sH

-- "Inefficient balance"
tIneffBalance :: Ord o => Tree o -> Tree o
tIneffBalance = tFromListL . balanceList . tToList

-- Finds the min/max values of a tree
tMin, tMax :: Ord o => Tree o -> o
tMin ETree = error "tMin called on an Empty Tree"
tMin (Node lT _ a) = case lT of
    ETree           -> a
    (Node lT' _ _)  -> tMin lT'

tMax ETree = error "tMax called on an Empty Tree"
tMax (Node _ rT a) = case rT of
    ETree           -> a
    (Node _ rT' _)  -> tMax rT'

-- Find the max depth of a tree
tDepth :: Tree a -> Int
tDepth ETree = 0
tDepth (Node lT rT _) = 1 + max (tDepth lT) (tDepth rT)

-- Finds how many nodes a tree contains
tSize :: Tree a -> Int
tSize ETree = 0
tSize (Node lT rT _) = 1 + (tSize lT) + (tSize rT)

absDiff :: Int -> Int -> Int
absDiff x y = abs $ x - y

-- Checks if a node is balanced
tIsBalanced :: Tree a -> Bool
tIsBalanced ETree = True
tIsBalanced n
    | tUnbalancedBy n > 2 = False
    | otherwise = True

-- Checks if a value is an element of the tree
tElem :: Ord o => o -> Tree o -> Bool
tElem x ETree = False
tElem x (Node lT rT a)
    | x < a = tElem x lT
    | x > a = tElem x rT
    | otherwise = True

tDelete :: Ord o => o -> Tree o -> Tree o
tDelete _ ETree = ETree
tDelete _ n@(Node ETree ETree _) = n -- Or give "Not found" error?
tDelete tD n@(Node lT rT a)
    | tD < a = Node (tDelete tD lT) rT a
    | tD > a = Node lT (tDelete tD rT) a
    | otherwise = case (lT,rT) of
        (ETree,t)   -> t
        (t,ETree)   -> t
        (t,t')      -> let fMin = tMin t' in Node t (tDelete (tMin t') t') fMin

My main concerns are:

The balancing algorithm. I thought it would work beautifully at first, then I realized that it basically just turns the tree into a "V". It's more efficient then a linked list, but still not very balanced
The 2 "tToList*" functions. Should I use foldl or foldr? They seem to act fairly equivalently; although foldl is easier to use because you typically read a list from the left
The "tDelete" function. It works (from my testing), but it looks very ugly. I basically just wrote it case-by-case, which made it very long.


texts:
This works fine but I feel it can be better optimized.

#!/usr/bin/env py

s = "1:5.9,1.5:7,2:10,4:18,8:40"
load_value = raw_input("Enter a load value\n")
li =  s.split(',')
driver = None
for index,item in enumerate(li):
    if load_value == li[index].split(':')[1]:
        driver = li[index].split(':')[0]
        print driver
if not driver:
    print "Could not find driver value"

Output:

Enter a load value
5.9
1


texts: define_method vs method_missing and CodeClimate scores

texts:
This may be a better fit for another board, but here goes:

I've made a very simple gem that acts as a days-of-the-week bitmask. To be hip, I registered it on CodeClimate, where it's currently scoring a B/3.0 grade.

Since there's only 1 extremely simple class, the B is bugging me. CodeClimate's reason is that there's too much code outside methods. This is no doubt due to my use of define_method to programmatically add read/write accessor methods like monday,  tuesday and so on to the class.

The methods are currently being added like so:

DAY_NAMES = %w(sunday monday tuesday wednesday thursday friday saturday).map(&:to_sym).freeze
DAY_BITS  = Hash[ DAY_NAMES.zip(Array.new(7) { |i| 2**i }) ].freeze

# ...

# Create the day=, day, and day? methods
DAY_BITS.each do |day, bit|
  define_method(day) do
    get_bit bit
  end
  alias_method :"#{day}?", day

  define_method(:"#{day}=") do |bool|
    set_bit bit, bool
  end
end

Note that the class supports two reader syntaxes (with and without ?).

Now, I could use method_missing instead to make CodeClimate happy, but as I see it, that's only more complex. The method names are all known in advance, so method_missing seems unnecessary, and much less efficient than defining the methods on the class once.

If I were to use method_missing I'd do something like this (get_bit and set_bit already accept both strings and symbols):

DAY_NAME_PATTERN = "((?:sun|mon|tues|wednes|thurs|fri|satur)day)".freeze

# ...

def method_missing(method, *args)
  case method.to_s
  when %r{\A#{DAY_NAME_PATTERN}\??\Z}
    get_bit $1
  when %r{\A#{DAY_NAME_PATTERN}=\Z}
    set_bit $1, args.first
  else
    super
  end
end

I'll still need the DAY_NAMES and DAY_BITS constants, so DAY_NAME_PATTERN would be yet another const - one that seems somewhat like duplication (though I could build it from DAY_NAMES of course, though that would be more expressions outside methods).

So to my mind, using method_missing would only improve the CodeClimate score, yet (ironically) make the code worse. Would you agree? Or do you know some clever 3rd way?

Aside: As Tokland rightly notes in his answer a bitmask is a fairly low-level approach to all of this compared to using sets.

I'm using a bitmask because the class is primarily intended to be used as an ActiveRecord serializer in a Rails app. That's where I extracted it from (partly as an exercise for myself). In the case of my app, the database already had a bitmask column, and this was an encapsulation of that logic. It seemed to me a good way to store the value (even if it is a little arcane for Ruby), as it allows for fast but complex day intersection/union queries at the SQL layer.

All of that could still be accomplished using sets, and only calculating the bitmask when necessary. Regardless of the specifics, however, my question is still valid on its own. Tokland's answer is great, but not quite an answer to the specific question.


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

texts:
I am studying the composite pattern and have created a very simple project that uses composite to introduce family members. I have decided to go with the 'fully transparent' implementation by defining all of my common functions in the interface class, I feel this is the neatest implementation and am considering throwing an exception in Leaf classes if client tries to call add / remove...

Below are the sample classes I have created. I would like to get some thoughts on how I implemented.

Am I implementing composite correctly?
Is this a good piece of sample code?
Thoughts on throwing an exception in leaf class for un-needed function implementations like add / remove Person?

Client class

public class Client {

public static void main(String args[]) {

    Person gretchen = new Parent("gretchen", 101);
    Person steve = new Parent("Steve", 57);
    Person mary = new Parent("Mary", 56);
    Person timmy = new Child("Timmy", 10);
    Person molly = new Child("Molly", 5);

    gretchen.add(steve);
    gretchen.add(mary);
    steve.add(timmy);
    steve.add(molly);
    mary.add(timmy);
    mary.add(molly);

    //introduces entire family
    gretchen.introduce();

}

}

Component Class

public interface Person {

  public String getName();

  public int getAge();

  public int getNumOfChildren();

  public void add(Person person);

  public void remove(Person person);

  public void introduce();
}

Composite class

public class Parent implements Person {

private String name;
private int age;
private List<Person> children = new ArrayList<Person>();

public Parent(String name, int age) {
    this.name = name;
    this.age = age;
}

public void introduce() {

    System.out.println("Hi, I am ".concat(getName()));
    System.out.println("I am ".concat(Integer.toString(getAge()).concat(
            " years old")));
    System.out.println("I have ".concat(Integer.toString(children.size()).concat(" kids.")));
    System.out.println("Children... introduce your selves!");

    for (Person person : children) {
        person.introduce();
    }

}

@Override
public String getName() {
    return this.name;
}

@Override
public int getAge() {
    return this.age;
}

@Override
public int getNumOfChildren() {
    return this.children.size();
}

@Override
public void add(Person person) {
    children.add(person);
}

@Override
public void remove(Person person) {
    children.remove(person);
}

}

Leaf class

public class Child implements Person {

private String name;
private int age;

public Child(String name, int age) {
    this.name = name;
    this.age = age;
}

@Override
public void introduce() {

    System.out.println("Hi, I am ".concat(getName()));
    System.out.println("I am ".concat(Integer.toString(getAge()).concat(
            " years old")));

}

@Override
public String getName() {
    return this.name;
}

@Override
public int getAge() {
    return this.age;
}

@Override
public int getNumOfChildren() {
    return 0;
}

@Override
public void add(Person person) {
    // no implementation here, throw exception maybe?

}

@Override
public void remove(Person person) {
    // no implementation here, throw exception maybe?

}

}


texts: Simple Linux upgrade script in Bash revision 2

texts:
As I will be deploying this script on multiple machines with the very same system Linux Mint 18 with rather same configuration, I would like to be semi-sure I won't screw things up much.

This little script will run every day from the root's crontab and will be logged into syslog or my own log AFTER this manual script passes some quality checks. Logging is not the question now. Automation is also not the question yet. In the meantime it is being executed as normal user and elevated with sudo internally. No help message needed. And no docs needed either. I ran it through Shellcheck.net. I have also ruled out the use of subjectively nicer apt, because it is not meant to be used in scripts.

Conditions still are:

Code readability
Output readability
Colored headings
Attempt to correct things
Clean-up after update

My current idea is a little more advanced than the first one, having implemented exit code checking was a great idea:

#!/bin/bash

RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
NOCOLOR="\033[0m"

function show_success {

  echo -e "${GREEN}Success.${NOCOLOR}\n"

}

function show_error_and_exit {

  echo -e "${RED}An error occured.${NOCOLOR}\n"

  exit "$1"

}

function error_handler {

  if [[ $1 -ne 0 ]];
  then

    show_error_and_exit "$2"

  else

    show_success

  fi

}

echo -e "\n${GREEN}Step 1: configure packages${NOCOLOR}"
echo -e "${YELLOW}dpkg --configure -a${NOCOLOR}"

sudo dpkg --configure -a

error_handler $?  1

echo -e "${GREEN}Step 2: fix broken dependencies${NOCOLOR}"
echo -e "${YELLOW}apt-get install --fix-broken${NOCOLOR}"

sudo apt-get install --fix-broken

error_handler $?  2

echo -e "${GREEN}Step 3: update cache${NOCOLOR}"
echo -e "${YELLOW}apt-get update${NOCOLOR}"

sudo apt-get update

error_handler $?  3

echo -e "${GREEN}Step 4: upgrade packages${NOCOLOR}"
echo -e "${YELLOW}apt-get upgrade${NOCOLOR}"

sudo apt-get upgrade

error_handler $?  4

echo -e "${GREEN}Step 5: upgrade distribution${NOCOLOR}"
echo -e "${YELLOW}apt-get dist-upgrade${NOCOLOR}"

sudo apt-get dist-upgrade

error_handler $?  5

echo -e "${GREEN}Step 6: remove unused packages${NOCOLOR}"
echo -e "${YELLOW}apt-get --purge autoremove${NOCOLOR}"

sudo apt-get --purge autoremove

error_handler $?  6

echo -e "${GREEN}Step 7: clean up${NOCOLOR}"
echo -e "${YELLOW}apt-get autoclean${NOCOLOR}"

sudo apt-get autoclean

error_handler $?  7


texts: Searching an entire workbook for keywords and then printing the results on another worksheet

texts:
I made a search page at the beginning of a work book. This page has a drop down that allows you to choose what category to search in, then you type your phrase or keyword into the search box and it finds all of the results. It then takes those results and pastes them in in the chart at the bottom of the search page. You then have the option to print out those results or clear the chart.  Pictures are also posted for better understanding.

Private Sub ComboBox1_Change()

End Sub

Private Sub ComboBox2_Change()
    UpdateSearchBox
End Sub

Private Sub CommandButton1_Click()
    Select Case TextBox1.Value
        Case "F"
            TextBox1.Value = "G"
        Case "E"
            TextBox1.Value = "F"
        Case "D"
            TextBox1.Value = "E"
        Case "C"
            TextBox1.Value = "D"
        Case "B"
            TextBox1.Value = "C"
        Case "A"
            TextBox1.Value = "B"
        Case "G"
            TextBox1.Value = "A"
    End Select
End Sub

Private Sub CommandButton2_Click()
    FindOne
End Sub

Private Sub TextBox1_Change()
    UpdateSearchBox
End Sub

Sub UpdateSearchBox()
    Dim PageName As String, searchColumn As String, ListFiller As String
    Dim lastRow As Long

    If TextBox1.Value <> "" Then
        PageName = TextBox1.Value
    Else
        Exit Sub
    End If

    Select Case ComboBox2.Value
        Case "EQUIPMENT NUMBER"
            searchColumn = "A"
        Case "EQUIPMENT NAME"
            searchColumn = "C"
        Case "DUPONT NUMBER"
            searchColumn = "F"
        Case "SAP NUMBER"
            searchColumn = "G"
        Case "SSI NUMBER"
            searchColumn = "H"
        Case "PART NAME"
            searchColumn = "I"
        Case ""
            MsgBox "Please select a value for what you are searching by."
    End Select

    lastRow = Sheets(PageName).Range("A65536").End(xlUp).Row

        If lastRow <> 0 And PageName <> "" And searchColumn <> "" Then
        ListFiller = PageName & "!" & searchColumn & "2" & ":" & searchColumn & lastRow
        ComboBox1.ListFillRange = ListFiller
    End If
End Sub
Sub FindOne()

    Range("B19:J1500") = ""

    Application.ScreenUpdating = False

    Dim k As Integer, EndPasteLoopa As Integer
    Dim myText As String, searchColumn As String
    Dim totalValues As Long
    Dim nextCell As Range

    k = ThisWorkbook.Worksheets.Count
    myText = ComboBox1.Value
    Set nextCell = Range("B20")
    If myText = "" Then
        MsgBox "No Address Found"
        Exit Sub
    End If

    Select Case ComboBox2.Value
        Case "EQUIPMENT NUMBER"
            searchColumn = "A"
        Case "EQUIPMENT NAME"
            searchColumn = "C"
        Case "DUPONT NUMBER"
            searchColumn = "F"
        Case "SAP NUMBER"
            searchColumn = "G"
        Case "SSI NUMBER"
            searchColumn = "H"
        Case "PART NAME"
            searchColumn = "I"
        Case ""
            MsgBox "Please select a value for what you are searching by."
    End Select

    For i = 2 To k
        totalValues = Sheets(i).Range("A65536").End(xlUp).Row
        ReDim AddressArray(totalValues) As String

        For j = 0 To totalValues
            AddressArray(j) = Sheets(i).Range(searchColumn & j + 1).Value
        Next j

        For j = 0 To totalValues
            If (myText = AddressArray(j)) Then
                EndPasteLoop = 1
                If (Sheets(i).Range(searchColumn & j + 2).Value = "") Then EndPasteLoop = Sheets(i).Range(searchColumn & j + 1).End(xlDown).Row - j - 1
                For r = 1 To EndPasteLoop
                    Range(nextCell, nextCell.Offset(0, 8)).Value = Sheets(i).Range("A" & j + r, "I" & j + r).Value
                    Set nextCell = nextCell.Offset(1, 0)
                Next r
            End If
        Next j
    Next i

    Application.ScreenUpdating = True
End Sub


texts:
I've posted the code for the following question. What could be improved?

  Q: Write a program that allows the user to type in a phrase and then
  outputs the acronym for that phrase.

  Note: The acronym should be all uppercase, even if the words in the
  phrase are not capitalized.

def main():

    ## input the phrase
    phrase = input("Enter the phrase: ")

    ## split the phrase into substrings
    phrase_split = phrase.split()

    acronym = ""

    ## iterate through every substring
    for i in phrase_split:
        acronym = acronym + i[0].upper()

    print("The acronym for your phrase is ",acronym + ".")

main()


texts: Ordering a .txt file numerically by size and alphabetically

texts:
I am doing a controlled assessment and one of the tasks is that we have to sort the saved pupils scores (numerically), and their names (alphabetically) in a .txt file. Any other improvements to the code would also be greatly appreciated.

import random
classcheck = False
while classcheck == False:
    classnumber = input("what class are you in")
    if classnumber == "1":
        print()
        classcheck = True
    elif classnumber == "2":
        print()
        classcheck = True
    elif classnumber == "3":
        print()
        classcheck = True
    else:
        print("that is not a valid class number")

student1name = input("please enter your name")
student1score = 0

for i in range(10):
    question1no1 = random.randint(1,20)
    question1no2 = random.randint(1,20)

    operators = ['+','-','*']
    op_number  = random.randint(0,2)
    op_sym = operators[op_number]
    op = op_number

    if op == 0:
        ans = question1no1 + question1no2
    elif op == 1:
        ans = question1no1 - question1no2
    elif op == 2:
        ans = question1no1 * question1no2

    print(str(question1no1), str(op_sym) , str(question1no2))

    question = "what is "+str(question1no1) + str(op_sym) +                                             str(question1no2)+"?"

    student1answer1 = input(question)

    print (student1answer1)

    if str(student1answer1) == str(ans):
        print("congrats you got the answer right")
        student1score = student1score + 1

    else:
        print("sorry you got the answer wrong")

print("your score was " + str(student1score) + " out of 10")

if classnumber == "1":
    class1score = open("class1score.txt", "a")
    class1score.write("\n" + student1name + (" ") + str(student1score))
    class1score.close()

if classnumber == "2":
    class2score = open("class2score.txt", "a")
    class2score.write("\n" + student1name + (" ") + str(student1score))
    class2score.close()

if classnumber == "3":

    class3score = open("class3score.txt", "a")
    class3score.write("\n" + student1name + (" ") + str(student1score))
    class3score.close()


texts: Read framed nested sensor data from memory stream

texts:
I'm implementing a TCP client to read data from a sensor. The sensor sends messages to connected clients framed with a start sequence, messages, checksum and end sum bytes. Because the sensor is installed at a remote location, I'm currently reading previously captured network data from a pcap dump into a MemoryStream. The TCP data is simulated from a capture file using bittwist (not real-time client-server communication).

Please advise on how to improve the method below that reads TCP messages including framing bytes, through a MemoryStream since I can't use NetworkStream? Is my checksum calculation correct? The sensor documentation indicates that to calculate it you must include all payload data except starting and ending sequence.

I have two while loops, one to first check start sequence (frame start), once done.

The second loop checks for the checksum value, end sequence (frame end) and if not, treats this as a message payload and reads from the stream until a valid end sequence (frame end) is found. Each (n) payload starts with a 3 bytes header (2 bytes identifier + 1 byte length of the succeeding data) and then the actual data.

private static void Decode(MemoryStream memoryStream)
    {
        var frameStartSequenceBuffer = new byte[] { 0xCA, 0xCB, 0xCC, 0xCD };
        var frameChecksumBuffer = new byte[1];
        var frameEndSequenceBuffer = new byte[] { 0xEA, 0xEB, 0xEC, 0xED };
        var startSequenceBuffer = new byte[4];
        var endSequenceBuffer = new byte[4];
        var messages = new List<byte[]>();
        var read = 0;

        while (read < startSequenceBuffer.Length)
        {
            read += memoryStream.Read(startSequenceBuffer, 0, startSequenceBuffer.Length);

            if (Helpers.ByteArrayCompare(startSequenceBuffer, frameStartSequenceBuffer))
                break;
        }

        while (memoryStream.Capacity > read)
        {
            read += memoryStream.Read(frameChecksumBuffer, 0, frameChecksumBuffer.Length);
            read += memoryStream.Read(endSequenceBuffer, 0, endSequenceBuffer.Length);

            if (Helpers.ByteArrayCompare(endSequenceBuffer, frameEndSequenceBuffer))
                break;

            var readPayload = frameChecksumBuffer.Concat(endSequenceBuffer).ToArray();
            var messageBuffer = new byte[3 + endSequenceBuffer[1]];

            readPayload.CopyTo(messageBuffer, 0);
            read += memoryStream.Read(messageBuffer, readPayload.Length, messageBuffer.Length - readPayload.Length);

            messages.Add(messageBuffer);
        }

        var calculatedChecksum = Helpers.CalculateChecksumBitwise(messages.SelectMany(o => o).Concat(frameChecksumBuffer).ToArray());

        if (calculatedChecksum != frameChecksumBuffer[0])
        {
            Console.WriteLine("Messages: {0} - Corrupt", messages.Count);
            return;
        }

        Console.WriteLine("Messages: {0} - Valid", messages.Count);
    }

Each TCP packet from the sensor encapsulates an array of (n) messages delimited with a 3 bytes headers.

The checksum is calculated from all data without the start sequence and the end sequence. The Checksum is a simple XOR Assignment of all n data bytes. Below is the checksum calculation method that I've implemented.

public static byte CalculateChecksumBitwise(IList<byte> byteToCalculate)
    {
        var checksum = 0;

        for (var i = 0; i < byteToCalculate.Count - 1; i++)
        {
            checksum = checksum ^ byteToCalculate[i];
        }

        return (byte)checksum;
    }


texts: Temperature and currency converters in OOP JS

texts:
I just started learning OOP from Mosh on YouTube and I looked for easiest exercises to practice. Can I get a feedback on my code please? What I did wrong and what I should change?

In the code there are two exercises: the first one is a temperature converter and the second one is a currency exchange.

let cel = "";
let fah = "";

function Termometer(kel) {
  this.kel = kel;

  this.getCelsius = function() {
    cel = this.kel - 273;
    return cel;
  },

  this.getFahrenheit = function() {
    fah = Math.floor(cel * (9/5) + 32);
    return fah;
  },

  this.init = function() {
    this.getCelsius();
    this.getFahrenheit();
  }
}

// currency exchange
var eur = "";
var usd = "";
var gbp = "";
function Valuta(sum1, sum2, sum3) {
  this.sum1 = sum1;
  this.sum2 = sum2;
  this.sum3 = sum3;

  this.getEur = function() {
    eur = Math.floor(this.sum1 / 4.72);
  };

  this.getUsd = function() {
    usd = Math.floor(this.sum2 / 4.15);
  };

  this.getGbp = function() {
    gbp = Math.floor(this.sum3 / 5.27);
  };

  this.init = function() {
    this.getEur();
    this.getUsd();
    this.getGbp();
  }
}

var valuta = new Valuta(300, 300, 300);
valuta.init();
console.log(eur, usd, gbp)


texts:
Based on the value of what is in column B (column A is hidden) it groups/colors/removes the corresponding node(s). "R - " values are nodes that need to be grouped. The rows are 6 cells apart before a new node starts. It also adds a formula below the value.

I wrote this code and it of course looks messy. I just want to know if there is a way to make it a little easier to understand.  It's not the best when it comes to recursion and I need to explain this to a few peers so they will be able to keep the model running as well.

Example:

Code for Review:

    'Set values to 5; first row to begin grouping
    Start_Row = 5
    Last_Row = 5

 Application.Wait Now + TimeValue("00:00:02")
 Application.StatusBar = "Grouping and formating workgroups..."

While WorksheetFunction.CountA(Rows(Start_Row)) <> 0

   'Color code certain ranges based on ending values of functions/nodes
   If (Right(range("B" & Start_Row).Value, 10) = "- Overhead" Or Right(range("B" & Start_Row).Value, 14) = "- Non Overhead" Or Right(range("B" & Start_Row).Value, 4) = "- WG") Then
        Finish_Row = Start_Row - 1
        Last_Row = Start_Row + 6
    End If

    With ActiveSheet
    lastRow_das_delete = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    'Delete actual and support nodes, loop twice to remove R value rollups
    For double_actuals_support = 0 To lastRow_das_delete
    range("B" & (Start_Row + 1)).Formula = "=CONCATENATE(""~ "" & INDEX('Complete Database Pull'!B:B,MATCH(B" & Start_Row & ",'Complete Database Pull'!C:C,0)))"
    If Right(range("B" & Start_Row).Value, 19) = "- Actuals & Support" Then
        For adjust_delete = 0 To 5
            Rows(Start_Row).EntireRow.Delete
        Next adjust_delete
    End If
    Next double_actuals_support

    'Group R- nodes together
    If Left(range("B" & Start_Row).Value, 2) = "R-" And Start_Row <> 5 And Last_Row <> Start_Row Then
        Finish_Row = Start_Row - 1
        Rows(Finish_Row & ":" & Last_Row).Group
        Last_Row = Start_Row + 6
        'If R- is in the first row, don't group up
    ElseIf Left(range("B" & Start_Row).Value, 2) = "R-" And Start_Row = 5 Then
        Last_Row = Start_Row + 6
    End If

    'Color WG rows/columns
    If (Right(range("B" & Start_Row).Value, 10) = "- Overhead" Or Right(range("B" & Start_Row).Value, 14) = "- Non Overhead") Then
        range("B" & Start_Row & ":Y" & (Start_Row + 3)).Interior.Color = 15853276
        range("B" & Start_Row & ":B" & (Start_Row + 5)).Interior.Color = 15853276
    End If

    If Right(range("B" & Start_Row).Value, 4) = "- WG" Then
        range("B" & Start_Row & ":Y" & (Start_Row + 3)).Interior.Color = 15523812
        range("B" & Start_Row & ":B" & (Start_Row + 5)).Interior.Color = 15523812
        Last_Row = Start_Row + 6
    End If

    'Adjust Start_Row 6 rows
    Start_Row = Start_Row + 6
Wend


texts: SelectAsync With Max Degree Of Parallelism

texts:
I have been looking for the best approach to creating an extension method that would give me the ability to select in a linq query using async/await with a max degree of parallelism.  I'm aware that this can be achieved with the use of different libraries but wanted to create one without.  Using postings I found online I made a few changes to convert a ForEachAsync to the following:

public static async Task<IEnumerable<TOut>> SelectAsync<TIn, TOut>(this IEnumerable<TIn> source,
        Func<TIn, Task<TOut>> body,
        int maxDegreeOfParallelism)
{
    var bag = new ConcurrentBag<TOut>();

    var selectAsync = Partitioner.Create(source)
        .GetPartitions(maxDegreeOfParallelism)
        .Select(async partition =>
        {
            using (partition)
            {
                while (partition.MoveNext())
                {
                    var item = await body(partition.Current);
                    bag.Add(item);
                }
            }
        });

    await Task
        .WhenAll(selectAsync);

    return bag;
}

Here is the unit test:

[TestMethod]
public async Task SelectAsync_EnsureMaxConcurrency_SuccessfulOnChecksAsync()
{
    // Arrange
    const int itemsMax = 10;
    const int dop = 5;
    const int delayInMs = 1000;

    var itemsToProcess = Enumerable
        .Range(1, itemsMax);

    // Act
    var started = DateTime.Now;

    var result = await itemsToProcess
        .SelectAsync(async item =>
        {
            await Task.Delay(1000);
            return item;
        }, dop);

    var elapsed = (DateTime.Now - started)
        .TotalMilliseconds;

    const double expected = (double)itemsMax / dop * delayInMs;

    // Assert
    Debug.WriteLine($"Elapsed:{elapsed} Expected:{expected}");

    Assert.AreEqual(itemsMax, result.Count());
    Assert.IsTrue(elapsed > expected);
}


texts: Efficient way to deal with maintaining Many:Many relationships in EF Code-First

texts:
I've got this all working, but it seems to be quite long-winded, and I thought I'd post here and see if I'm doing it wrong...

I have a M:M relationship between an Installer and a MasterInstance. The classes (code-first) look like:

public class MasterInstance
{
    .. rest of fields here ..
    public virtual ICollection<Installer> PermittedInstallers { get; set; }
}

public class Installer
{
    .. rest of fields here ..
    public virtual ICollection<MasterInstance> PermittedMasterInstances { get; set; }
}

I want to be able to edit these in my Installer view, using a multi-list box. So, I create a ViewModel for what I need:

public class InstallerViewModel
{
    public Installer Installer { get; set; }
    public List<MasterInstance> PermittedMasterInstances { get; set; }
    public int[] SelectedMasterInstances { get; set; }
}

And then I place this in my view like so so that I can select the instances for my installer:

<div class="editor-field">
    @Html.ListBoxFor(model => model.SelectedMasterInstances,(Model.PermittedMasterInstances).Select(option => new SelectListItem {
        Text = option.Name,
        Value = option.MasterInstanceId.ToString()
}))
</div>

In the controller, for the edit action, I need to set this view model up:

var installer = context.Installers.Include(i => i.PermittedMasterInstances).Single(x => x.InstallerId == installerId);
            InstallerViewModel model = new InstallerViewModel
                                           {
                                               Installer = installer,
                                               PermittedMasterInstances = context.MasterInstances.ToList(),
                                               SelectedMasterInstances = installer.PermittedMasterInstances.Select(i => i.MasterInstanceId).ToArray()
                                           };
return View(model);

Finally, on the post of the edit, I need to delete any relationships that are no longer there and add the new ones:

// Grab the model from the viewmodel and attach to the context
var installer = installerModel.Installer;
context.Installers.Attach(installer);

// Load the related records (dont know why Lazy Loading wouldn't kick in here)
context.Entry(installer).Collection(i => i.PermittedMasterInstances).Load();
// Iterate and delete existing relationships
var instancesToDelete = installer.PermittedMasterInstances.Where(mi => !installerModel.SelectedMasterInstances.Contains(i.MasterInstanceId)).ToList();
instancesToDelete.ForEach(mi => installer.PermittedMasterInstances.Remove(mi));

// Now loop through an int[] and add those new relations, WITHOUT the pain of fetching them from the DB
foreach (var permittedMasterInstanceId in installerModel.SelectedMasterInstances)
{
    if (!installer.PermittedMasterInstances.Any(pmi => pmi.MasterInstanceId == permittedMasterInstanceId))
    {
        var masterInstance = new MasterInstance {MasterInstanceId = permittedMasterInstanceId};
        context.MasterInstances.Attach(masterInstance);
        installer.PermittedMasterInstances.Add(masterInstance);
    }
}

// We're done - save and finish.
context.Entry<Installer>(installer).State = EntityState.Modified;
context.SaveChanges();

So this works... But, it seemed like a lot of effort, is this the right/best way to achieve it?


texts:
I am doing some simple projects in an attempt to get good at programming, this is my first GUI would love some feedback, and some guidelines.

from tkinter import *
import random

root = Tk()
root.resizable(False, False)

root.title('Coinflipper')

topframe = Frame(root)
topframe.pack()
botframe = Frame(root)
botframe.pack(side=BOTTOM)
midframe = Frame(root)
midframe.pack()

choice = Label(topframe, text="Enter the number of flips: ")
choice.grid(row=1)
ent = Entry(topframe)
ent.grid(row=1, column=2)

clickit = Button(botframe, text="FLIP THE COIN!!!")
clickit.pack()

out = Text(midframe, width=15, height=1)
out2 = Text(midframe, width=15, height=1)
out.grid(row=1, column=1, columnspan=3)
out2.grid(row=2, column=1, columnspan=3)

def flipy(event):
    guess = ent.get()
    heads = []
    tails = []

    if guess == '' or guess == str(guess):
        out.delete(1.0, "end-1c")
        out.insert("end-1c", 'Invalid')

    for flips in range(int(guess)):
        out.delete(1.0, "end-1c")
        out2.delete(1.0, "end-1c")
        random_number = random.randint(1, 2)
        if random_number == 1:
            heads.append("Heads")
        elif random_number == 2:
            tails.append("Tails")

    out.insert("end-1c", len(tails))
    out.insert("end-1c", " -TAILS")
    out2.insert("end-1c", len(heads))
    out2.insert("end-1c", " -HEADS")

clickit.bind("<Button-1>", flipy)

root.mainloop()


texts:
For the first time I have to play with Threads in java.

Basically, my code is a simple FIFO job queue.
The main thread regularly puts jobs in the queue wich need to be executed.

The code below is a striped down version of what I am doing :

public class JobPerformer implements Runnable {
    private final ArrayList<Job> jobList = new ArrayList<Job>();
    private final AtomicBoolean threadStop = new AtomicBoolean(false);
    private Thread thread;
    public JobExecuter(){
       thread = new Thread(this)
       thread.start();
    }

    public void perform(Job job, bool async){
        if(async){
            jobList.add(job);
            jobList.notifyAll();
        }
        else{
            job.perform();
        }
    }

    public void perform(Job job){
        this.perform(job, false);
    }

    public void shutdown(){
         threadStop.set(true);
         if(thread.getState() == Thread.State.WAITING){
             thread.interrupt();
         }
    }

    public void run(){
        while(! threadStop.get()){
            if(jobList.isEmpty()){
                try{
                    jobList.wait();
                } catch (InterruptedException ex) {}
            }
            jobList.get(0).perform();
            jobList.remove(0);
        }

        // Finish the jobs still in the queue
        while(!jobList.isEmpty()){
             jobList.get(0).perform();
             jobList.remove(0);
        }
    }
}

I have two questions :

I use wait() and notifyAll() to avoid polling. But nowhere do I synchronize. Since it's a FIFO queue, threads are just adding jobs at the end or reading at the start. I don't see any conflict that would make me say "Hey, I have to make sure that during this op. only one thread can be working on this object", is there a possible conflict I am overseeing that would require me to synchronize anyway ?
I am using an "Atomic boolean" because of this question : Multithreading correctly done? What was wrong with using a normal boolean ?


texts:
I'm trying to convert some very simple C# string utility functions to F# as a learning exercise. Ignore the fact that the C# version is implemented as an extension method for the moment.

The function adds a blank space before any upper case letters (with the exception of the very first char) - so, "ATestString" becomes "A Test String":

return string.Concat(str.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');

My first pass using F#:

let padWithSpace str =
    (str |> Seq.fold (fun acc elm -> if Char.IsUpper(elm) then acc + (sprintf " %c" <| elm) else acc + (string elm)) "").TrimStart([| ' ' |])

Is there a more concise, or more importantly, idiomatic way to do this in F#?


texts: Reading a binary file containing periodic samples

texts:
I have the following code for reading HTK feature files. The code below is working completely correct (verified it with unit tests and the output of the original HTK toolkit).

from HTK_model import FLOAT_TYPE
from numpy import array
from struct import unpack

def feature_reader(file_name):
    with open(file_name, 'rb') as in_f:
        #There are four standard headers. Sample period is not used
        num_samples = unpack('>i', in_f.read(4))[0]
        sample_period = unpack('>i', in_f.read(4))[0]
        sample_size = unpack('>h', in_f.read(2))[0]
        param_kind = unpack('>h', in_f.read(2))[0]

        compressed = bool(param_kind & 02000)

        #If compression is used, two matrices are defined. In that case the values are shorts, and the real values are:
        # (x+B)/A
        A = B = 0
        if compressed:
            A = array([unpack('>f',in_f.read(4))[0] for _ in xrange(sample_size/2)], dtype=FLOAT_TYPE)
            B = array([unpack('>f',in_f.read(4))[0] for _ in xrange(sample_size/2)], dtype=FLOAT_TYPE)
            #The first 4 samples were the matrices
            num_samples -= 4

        for _ in xrange(0,num_samples):
            if compressed:
                yield ((array( unpack('>' + ('h' * (sample_size//2)),in_f.read(sample_size)) ,dtype=FLOAT_TYPE) + B) / A)
            else:
                yield (array( unpack('>' + ('f' * (sample_size//4)),in_f.read(sample_size)), dtype=FLOAT_TYPE))

How can I speed up this code, are there things I should improve in the code?


texts:
I wrote this piece of code yesterday at work. This function will traverse a deep nested object to find and modify a value.

I know it's bad, so I am trying to refactor it. I have not found any significant way to improve it. This is kind of dealing with legacy code. Changing the object structure is off the book, I should only modify this function, because things are working I don't dare to touch other parts.

How would you refactor this?

Object structure:

{
    panel1Name: {
        screen: {
            screen1Name: {
                variable: {
                    var1: {
                        custom: [],
                    },
                    var2: {
                        custom: [],
                    },
                    var3: {
                        custom: [],
                    },
                    // many other var
                }
            },
            screen2Name: {
                // ...
            },
            // many other screens
        }
    },
    panel2Name: {
        // ...
    },
    // many other panels
}

// custom is the value I need to check and modify

Code

function removeImageFromSettings(imageName) {
  for (let panel in $scope.data) {
    if ($scope.data.hasOwnProperty(panel)) {
      for (let screen in $scope.data[panel].screen) {
        if ($scope.data[panel].screen.hasOwnProperty(screen)) {
          for (let variable in $scope.data[panel].screen[screen].variable) {
            if ($scope.data[panel].screen[screen].variable.hasOwnProperty(variable)) {
              const data = $scope.data[panel].screen[screen].variable[variable];

              if (data.custom && data.custom[0] === imageName) {
                data.custom[0] = '';
              }
            }
          }
        }
      }
    }
  }
}


texts:
I need to build something that is not very complicated, but efficient. I don't have much time to debug.

I don't know if this method is strong enough to encrypt a data to be displayed in a Qr code.

It will be compiled in Qt.

    #include <botan/botan.h>
    #include <botan/aes.h>
    #include <botan/kdf.h>
    #include <botan/pbkdf2.h>
    #include <botan/hmac.h>
    #include <botan/sha160.h>

    using namespace std;
    using namespace Botan;

    // .....

    bool BotanWrapper::EncryptFile(QString Source, QString Destination)
    {
    try
    {
    //Setup the key derive functions
    PKCS5_PBKDF2 pbkdf2(new HMAC(new SHA_160));
    const u32bit PBKDF2_ITERATIONS = 8192;

    //Create the KEY and IV
    KDF* kdf = get_kdf("KDF2(SHA-1)");

    //Create the master key
    SecureVector<byte> mMaster = pbkdf2.derive_key(48, mPassword.toStdString(), &mSalt[0], mSalt.size(),PBKDF2_ITERATIONS).bits_of();
    SymmetricKey mKey = kdf->derive_key(32, mMaster, "salt1");
    InitializationVector mIV = kdf->derive_key(16, mMaster, "salt2");

    string inFilename = Source.toStdString();
    string outFilename = Destination.toStdString();
    std::ifstream in(inFilename.c_str(),std::ios::binary);
    std::ofstream out(outFilename.c_str(),std::ios::binary);

    Pipe pipe(get_cipher("AES-256/CBC/PKCS7", mKey, mIV,ENCRYPTION),new DataSink_Stream(out));
    pipe.start_msg();
    in >> pipe;
    pipe.end_msg();

    out.flush();
    out.close();
    in.close();

    return true;
   }
   catch(...)
   {
    return false;
    }
   }


texts:
Attempted to use strtol() to parse a string into an integer and to output any appropriate error messages for failed conversions. How did I do? How could/should it be done? Code works as intended if I did not miss anything.

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

#define MAXLENGTH 1024

void retrieveStringInput(char *str, size_t buffersize)
{
    for (;;) {
        if (fgets(str, buffersize, stdin) != NULL) {
            str[strcspn(str, "\n")] = '\0';
            return;
        }
        printf("No input detected please try again...\n");
    }
}

int stringToInt(char *str)
{
    char *end = NULL;
    long number = strtol(str, &end, 10);
    if (!*end && end != str) {
        if (number <= INT_MIN || number >= INT_MAX) {
            printf("ERROR INPUT IS OUT OF RANGE FOR INTEGER VALUE, %d < number < %d\n", INT_MIN, INT_MAX);
            return 0;
        }
        return (int)number;
    }
    printf("STRING FAIILED TO CONVERT...\n");
    return 0;
}

int main(void)
{
    char number[MAXLENGTH];
    int convertNumber = 0;
    retrieveStringInput(number, sizeof number);

    if (number[0] = '0') {
        printf("Your number is: %d\n", convertNumber);
    }

    if ((convertNumber = stringToInt(number)) != 0)
        printf("YOUR NUMBER IS: %d\n", convertNumber);

    printf("Press any key to continue...\n");
    getch();
    return 0;
}


texts: Comment on the style of this C++ class for a 2D Vector

texts:
I wrote a template class for a 2D vector that is intended to provide 2D vector arithmetic to be used in a Physics simulation program. Note that x and y are public or ease of use since encapsulation here would just decrease the readability of code that uses the class.

Is the general style of my code good and does it comply with modern C++ best practices? Thank you for the suggestions.

template <typename T>
class Vector2D final {
public:
    T x;
    T y;

    // Defaulted constructors
    Vector2D(const Vector2D<T>& v) = default;
    ~Vector2D() = default;

    Vector2D(T ix, T iy);

    // Member Utility Functions
    void Set(T ix, T iy);
    T Magnitude();

    // Overloaded Compound Operators
    Vector2D<T>& operator+=(const Vector2D<T>& rhs);
    Vector2D<T>& operator*=(const Vector2D<T>& rhs);
    Vector2D<T>& operator-=(const Vector2D<T>& rhs);
    Vector2D<T>& operator/=(const T& rhs);
    Vector2D<T>& operator=(const  Vector2D<T>& rhs);
};

// Non-Compound Operators.
template <typename T> Vector2D<T> operator+(Vector2D<T> lhs, const Vector2D<T>& rhs);
template <typename T> Vector2D<T> operator*(Vector2D<T> lhs, const Vector2D<T>& rhs);
template <typename T> Vector2D<T> operator-(Vector2D<T> lhs, const Vector2D<T>& rhs);
template <typename T> Vector2D<T> operator/(Vector2D<T> lhs, const T& rhs);

template <typename T>
Vector2D<T>::Vector2D(T ix, T iy)
        : x(ix)
        , y(iy)
{
}

template <typename T>
void Vector2D<T>::Set(T ix, T iy) {
    x = ix;
    y = iy;
}

template <typename T>
T Vector2D<T>::Magnitude() {
    return x*x + y*y;
}

template <typename T>
Vector2D<T>& Vector2D<T>::operator+=(const Vector2D<T>& rhs) {
    x += rhs.x;
    y += rhs.y;
    return *this;
}

template <typename T>
Vector2D<T>& Vector2D<T>::operator*=(const Vector2D<T>& rhs) {
    x *= rhs.x;
    y *= rhs.y;
    return *this;
}

template <typename T>
Vector2D<T>& Vector2D<T>::operator-=(const Vector2D<T>& rhs) {
    x -= rhs.x;
    y -= rhs.y;
    return *this;
}

template <typename T>
Vector2D<T>& Vector2D<T>::operator/=(const T& rhs) {
    x /= rhs;
    y /= rhs;
    return *this;
}

template <typename T>
Vector2D<T>& Vector2D<T>::operator=(const Vector2D<T>& rhs) {
    x = rhs.x;
    y = rhs.y;
    return *this;
}

template <typename T>
Vector2D<T> operator+(Vector2D<T> lhs, const Vector2D<T>& rhs) {
    lhs += rhs;
    return lhs;
}

template <typename T>
Vector2D<T> operator*(Vector2D<T> lhs, const Vector2D<T>& rhs) {
    lhs *= rhs;
    return lhs;
}

template <typename T>
Vector2D<T> operator-(Vector2D<T> lhs, const Vector2D<T>& rhs) {
    lhs -= rhs;
    return lhs;
}

template <typename T>
Vector2D<T> operator/(Vector2D<T> lhs, const T& rhs) {
    lhs /= rhs;
    return lhs;
}

Some examples of using this with float. I intend to only use it with floating point types (float, double, long double etc) and have tested it with other types but will present float here for brevity.

Vector2D<float> v1 = {1.0, 2.0};
Vector2D<float> v2 = {2.0, 3.0};

v1 += v2;
assert(v1.x == 3.0 && v1.y == 5.0);
v1 -= v2;
assert(v1.x == 1.0 && v1.y == 2.0);
v1 *= v2;
assert(v1.x == 2.0 && v1.y == 6.0);
v1 /= 2.0;
assert(v1.x == 1.0 && v1.y == 3.0);

v1.Set(1.0, 2.0);
v2.Set(2.0, 3.0);
Vector2D<float> v3 = {0.0, 0.0};

v3 = v1 + v2;
assert(v3.x == 3.0 && v3.y == 5.0);
v3 = v1 - v2;
assert(v3.x == -1.0 && v3.y == -1.0);
v3 = v1 * v2;
assert(v3.x == 2.0 && v3.y == 6.0);
v3 = v1 / 2.0f;
assert(v3.x == 0.5 && v3.y == 1.0);


texts:
I have a legacy code written as below:

if($value < 0) {
    $value = 0;
}
if($value > 4) {
    $value = 4;
}

Is there an easier and more elegant way to do this?


texts: Cyclomatic complexity for method is too high

texts:
How do i refactor my code to fix the lint error i am getting?

  def value=(val)
    if val
      if [:char].include?(@type) && val.length >= args[:limit] && val == 'true'
        val = 'Y'
      elsif [:char].include?(@type) && val.length >= args[:limit] && val == 'false'
        val = 'N'
      elsif [:bit].include?(@type) && val.length >= args[:limit] && val == 'true'
        val = 1
      elsif [:bit].include?(@type) && val.length >= args[:limit] && val == 'false'
        val = 0
      elsif [:varchar, :char, :nvarchar].include?(@type) && @args[:limit]
        unless val.length <= args[:limit]
          msg = "The length of #{@name} exceeds the max limit #{@args[:limit]}"
          raise Programmability::BadRequestError.new(nil, msg)
        end
      elsif [:numeric].include?(@type) && @args[:precision] && @args[:scale]
        valid_numeric?(val, @args[:precision], @args[:scale])
      elsif [:datetime].include?(@type)
        valid_date?(val)
      end
    end
    @value = val
  end


texts:
I'm looking for a more cleaner and simpler solution to auto-format a text-box field intended only for the user to enter their college Grade Point Average (GPA). Here is my code:

<label for="collegeGPA">GPA:</label>
<input type="text" name="collegeGPA" id="collegeGPA" maxlength="4" style="width:45px;">

<script type="text/javascript">

//<!-- When executed this script [BELOW] auto-formats a Grade Point Average (GPA) field given the element ID [BEGIN] -->

function FormatGPA(gPAElementID) {

    re = /\D/g; // remove any characters that are not numbers
    gPAVal = document.getElementById(gPAElementID).value.replace(re,"");
    gPAValFirstCharacter = gPAVal.charAt(0);

    if ( ( gPAVal >= 0 ) &&  ( gPAValFirstCharacter < 5 ) )
        {

            gPALen = gPAVal.length;

            if( gPALen > 1)
                {
                    gPAa=gPAVal.slice(0,1);
                    gPAb=gPAVal.slice(1);
                    document.getElementById(gPAElementID).value = gPAa + "." + gPAb;
                }
        else
                {
                    document.getElementById(gPAElementID).value=gPAVal;
                };

        }

    else
        {
            document.getElementById(gPAElementID).value="";
        };

};

//<!-- When executed this script [ABOVE] auto-formats a Grade Point Average (GPA) field given the element ID [END] -->

document.getElementById('collegeGPA').onblur = function (e) { FormatGPA(this.id); };
document.getElementById('collegeGPA').onfocus = function (e) { FormatGPA(this.id); };
document.getElementById('collegeGPA').onkeypress = function (e) { FormatGPA(this.id); };
document.getElementById('collegeGPA').oninput = function (e) { FormatGPA(this.id);  };
document.getElementById('collegeGPA').onchange = function (e) { FormatGPA(this.id); };

</script>

...and here is my JSFiddle: http://jsfiddle.net/JamesAndersonJr/kxaBJ/1/
I'm looking for a simpler way to format the GPA input dynamically (i.e. as the user types it in).
The format should be:

1st Character: any digit 0-4
2nd Character: Always a Period (should    also be auto-inserted after
user types a valid first digit)
3rd Character: any digit
4th Character: any digit

UPDATE: Lowest possible GPA is 0.00, not 1.00.


texts:
I'm working through the common divisibility tests, and implementing them in Racket.  I'm working on divisible by three, for which I thought it'd be nice to keep summing until I got to a single digit (which I can then check for divisibility by 3).

My code is here, but it feels like there are rather a lot of steps, and I wonder if there's a way to make it feel a bit less imperative.

(define (digits n)
  (cond
    [(zero? n) '()]
    [else (cons (remainder n 10) (digits (quotient n 10)))]))

(define (digisum n)
  (apply + (digits n)))

(define (repeated-digit-sum x)
  (let ([ds (digisum x)])
  (if (< ds 10) ds
    (repeated-digit-sum ds))))