texts: Import huge XML data set into MongoDB while parsing data

texts:
I am currently working with the Stack Exchange Data Dump - to be more precise - with the dumped Posts.xml data set from Stack Overflow.

What am I trying to achieve?
I want to read the whole data set and import each row (a post on Stack Overflow) as a document into an MongoDB database.

What am I doing right now?
I am using the iterparse() function from lxml to iterate over each row, without building a DOM. Every row contains attributes which hold the actual data. As every attribute is a String, I need to parse some attributes into Integers, Dates and Lists. This is done by the attrib_to_dict() function. The resulting Dictionary is simply inserted into the database collection.

What is the problem?
The parsing of the attributes is quite slow. The whole process took about two hours on my machine. By using the multiprocessing module I was able to speed up the process substantially. The iteration over the whole data set without doing anything is quite fast.

# main.py
from lxml import etree as et
from tqdm import tqdm
import multiprocessing as mp
import pymongo

from constants import POSTS_SIZE
from posts import attrib_to_dict

client = pymongo.MongoClient("mongodb://localhost:27017/")

# database
stackoverflow = client["stackoverflow"]
# collection
posts = stackoverflow["posts"]

def work(elem):
    try:
        # turn the String back into an element, pass attributes to parsing function
        posts.insert_one(attrib_to_dict(et.fromstring(elem).attrib))
    except pymongo.errors.DuplicateKeyError:
        # skip element
        pass

if __name__ == "__main__":
    pool = mp.Pool(4)
    # progress bar
    pbar = tqdm(total=POSTS_SIZE)
    def update(*args):
        # add one to total processed elements
        pbar.update(1)
    try:
        for event, elem in et.iterparse("Posts.xml", tag="row"):
            # pass element as a String to the worker
            # passing the attribute object directly did not seem to work
            pool.apply_async(work, args=(et.tostring(elem),), callback=update)
            elem.clear()
        pool.close()
    except KeyboardInterrupt:
        pool.terminate()
    finally:
        pbar.close()
        pool.join()

# posts.py
from datetime import datetime as dt

def attrib_to_dict(attrib):
    result = {}

    result["_id"] = int(attrib.get("Id"))

    result["PostTypeId"] = int(attrib.get("PostTypeId"))

    # nullable attributes
    acceptedAnswerId = attrib.get("AcceptedAnswerId")
    if acceptedAnswerId: result["AcceptedAnswerId"] = int(acceptedAnswerId)

    result["CreationDate"] = dt.fromisoformat(attrib.get("CreationDate"))

    # about 10 more conversions ...

    tags = attrib.get("Tags")
    # "<python><mongodb>" -> ["python", "mongodb"]
    if tags: result["Tags"] = [tag[:-1] for tag in tags.split("<")[1:]]

    return result

Some performance metrics:

no inserts, no parsing, passing None to worker: 13427.88 items/s
no inserts, no parsing, passing et.tostring(elem) to worker: 10177.07 items/s
no inserts, parsing, passing et.tostring(elem) to worker: 9637.41 items/s
inserts, parsing, passing et.tostring(elem) to worker: 7185.15 items/s


texts:
I need to execute the same set of statements but with a different name of the field each time. I was wondering if there is a better way to write this?

int returnCode = collectValue("empName", record);
if(returnCode <0)
{
return false;
}

returnCode = collectValue("empNum", record);
if(returnCode < 0)
{
return false;
}

returnCode = collectValue("empSalary", record);
if(returnCode < 0)
{
return false;
}


texts:
I have written a small snippet to validate a substring in a string in O(n). I have tested the code using some combinations. I require your help to let me know if there are any bugs in my code with respect to any other cases that I might have missed out:-

class StringClass( object ):
    def __init__( self, s ):
        self._s = s
    def __contains__( self, s ):
        ind = 0
        for ch in self._s:
            if ind < len( s ) and ch == s[ind]:
                ind += 1
            elif ind >= len( s ):
                return True
            elif ch == s[0]:
                ind = 1
            elif ch != s[ind]:
                ind = 0

        if ind >= len( s ):
            return True

if __name__ == '__main__':
    s = StringClass( 'I llove bangaolove' )
    print 'love' in s

PS: Here, Instead of trying to find the string 'love' in 'I llove bangalove', I'm using the other method: i.e. to find if the string 'I llove bangalove' contains 'love'.

Please let me know if there are any corrections to be made here.


texts:
I have the following code to get the prime numbers between a range of numbers. Is this the best and fastest method?

int[] numberCollection = Enumerable.Range(startNum,Count).ToArray<int>();
string primeNumbers = string.Join(",", from number in numberCollection
                                       where (IsPrime(number)==true)
                                       select number);

Where :

startNum is the starting number
Count is the number of numbers in the collection
IsPrime is a method defined as follows:

  private static bool IsPrime(int number)
  {
      for (int i = 2; i < number/2; i++)
          if (number % i == 0) return false;
      return true;
  }


texts:
I'm learning Node.  I'm working on a personal project that uses Node + Express + Node-MySQL + Generic-Pool (for MySQL pooling).  I am new to the idea of module.exports and I'm still grokking it.

The following code works.  However I want to organize my code in a way that is logical and optimal, and MVCish.  I am wondering if those of you who have done this before could take a look at the following and let me know if this is the "right" way to structure things.  (Yes I know this going to depend a lot on the developer.  But at least knowing if there are obvious problems in my approach would be helpful.)

My folder structure looks roughly like this:

app.js
routes/
models/
base/

In my main app.js file I have...

var express = require( "express" ),
    mysql = require( "mysql" ),
    generic_pool = require ( "generic-pool" ),
    routes = require( "./routes" );

var app = module.exports = express(),
    pool = generic_pool.Pool({
        name: "mysql",
        create: function( cb ) {
            var db = mysql.createConnection( require( "./config/db" ) );
            db.connect();
            cb( null, db );
        },
        destroy: function( db ) {
            db.end();
        },
    });

app.configure(function(){
    app.set( "pool", pool );
    ...
});
routes( app );
app.listen(80, function());

Here is an example route/ file "stuff.js"

module.exports = function( app ){
    var model = require( "../models/stuff" );
    model.init( app );
    app.get( "/stuff", function( req, res ){
        model.get( function( result ){
            res.json( result );
        });
    });
};

Here is an example model/ file "stuff.js"

var db;
module.exports.init = function( app ){
    db = require( "../base/db" );
    db.init( app );
};
module.exports.get = function( cb ){
    var sql = "SELECT * FROM stuff";
    db.do( sql, false, cb );
};

And here is the base/db file

var pool;
module.exports.init = function( app ){
    pool = app.get( "pool" );
};
module.exports.do = function( sql, params, cb ){
    pool.acquire(function( err, db ){
        if (err) {
            // ...
        } else {
            db.query( sql, function( err, result ) {
                if ( err ) {
                    // ...
                } else if ( typeof cb === "function" ) {
                    cb( result );
                }
            });
        }
        pool.release( "db" );
    });
};


texts: Implementation of a templated Singly linked list class in c++

texts:
The code below is my implementation of singly linked list.I would prefer it, if you kindly review the code and give me pointers on how a professional programmer would have written the code. Basically, i want to know about some good code habits that I have missed in the code.

The class one_way_linked_list is the linked list class. It uses the node class to create the nodes of the list. It keeps record of every object's head node using the head pointer. The function list_create() creates the list. The function list_print() prints the code. The function list_delete() deletes the linked list by deallocating every node one by one.

Sorry for my english, as it is not my native language. Thank you in advance.

#include <iostream>

using namespace std;

template<typename t>
class node
{

public:
    t data;
    node *next;

    //constructors
    node()
    {
        next=NULL;
    }
    node(t d)
    {
        data=d;
        next=NULL;
    }
};

template<typename t>
class one_way_linked_list
{
    node<t> n;
public:
    node<t> *head=NULL;
    node<t> *prev=NULL;

    void list_create(t);
    void list_print();
    void list_delete()
};

template <typename t>
void one_way_linked_list<t>::list_create(t dat)
{
    node<t> *ptr=new node<t>(dat);
    if(head==NULL)
{
    head=ptr;
    prev=head;
}
else
{
    prev->next=ptr;
    prev=ptr;
}
}

template<typename t>
void one_way_linked_list<t>::list_print()
{
    node<t> *ptr=this->head;
    cout<<"Printing the list: "<<endl;
    while(ptr!=NULL)
    {
        cout<<ptr->data<<" ";
        ptr=ptr->next;
    }
    cout<<endl;
}

template<typename t>
void one_way_linked_list<t>::list_delete()
{
    node<t> *ptr=new node<t>();
    ptr->data=this->head->data;
    while(ptr!=NULL)
    {
        ptr=ptr->next;
        delete ptr;
    }
    delete this->head;
    head=NULL;
    cout<<"Deleted"<<endl;
}

int main()
{
    //driver program for int type list
    one_way_linked_list<int> list1;
    int temp;
    while(1)
    {
        cin>>temp;
        if(temp==-1) break;
        else
        {
            list1.list_create(temp);
        }
    }
    list1.list_print();
    list1.list_delete();
    list1.list_print();
}


texts: Is there a better way to translate a string into methods?

texts:
So I create 3D tree like structures using L-systems. Basically this means I generate a string like this:

 "FFFFFFFF-[[FFFF-[[FF-[[F-[[X]+X]+F[+FX]-X]+F]]]]]"

This string is then interpreted by a turtle performing certain actions for each character in the string. The problem is how to map characters to turtle actions in an elegant way in C++.

Right now I use a enum to specify the actions and performAction calls methods based on the action. Is there a better way of doing it?

The turtle code:

enum TurtleAction {
    MoveForward,
    TurnLeft,
    TurnRight,
    PitchDown,
    PitchUp,
    RollLeft,
    RollRight,
    TurnAround,
    PushState,
    PopState
};

struct TurtleState {
    Vec3f pos;

    Vec3f H;
    Vec3f L;
    Vec3f U;

    double width;
};

class Turtle {
    TurtleState m_currentState;
    stack<TurtleState> m_states;

    void(*m_drawFunc)(Vec3f,double); ///< Drawing callback

public:
    double alpha; ///< Turning angle

    Turtle(TurtleState startState,
           void(*drawFunc)(Vec3f,double));
    void performAction(TurtleAction action);

private:
    void move(double dist);
    void turn(double deg);
    void pitch(double deg);
    void roll(double deg);

    void push();
    void pop();

    void rotate(const Mat44f & R);
};

void
Turtle::performAction(TurtleAction action){
    switch (action) {
        case MoveForward:
            move(1.0f);
            break;

        case TurnLeft:
            turn(-alpha);

        case TurnRight:
            turn(+alpha);

        case PitchDown:
            pitch(-alpha);

        case PitchUp:
            pitch(+alpha);

        case RollLeft:
            roll(-alpha);

        case RollRight:
            roll(+alpha);

        case TurnAround:
            turn(180.0f);

        case PushState:
            push();

        case PopState:
            pop();

        default:
            break;
    }
}

The string parsing:

class LSystem {

    map<char,TurtleAction> m_actionRules;
    TurtleState m_start;

public:

    void draw(string & str,void(*drawFunc)(Vec3f,double));
    void computeString(string & axiom, int generations);

    LSystem();
    ~LSystem();

};

void
LSystem::draw(string &str, void (*drawFunc)(Vec3f, double))
{
    Turtle turtle(m_start,drawFunc);
    turtle.alpha = 25.0f;

    for (int s = 0; s < str.size(); s++) {
        char c = str[s];
        map<char,TurtleAction>::iterator it = m_actionRules.find(c);
        if (it != m_actionRules.end()) {
            turtle.performAction(it->second);
        }
    }
}


texts:
I'm working on an application that counts the number of employees in each department.

Here's my code:

SELECT
    count(case when (JS_TITLE = 'Accounting')then 1 end)as Accounting,
    count(case when (JS_TITLE = 'Management')then 1 end)as Management,
    count(case when(JS_TITLE = 'Marketing')then 1 end )as Marketing,
    count(case when(JS_TITLE = 'HR')then 1 end)as HR
FROM EMP
LEFT JOIN JOB_STATUS ON EMP.JS_REF = JOB_STATUS.JS_REF

but the thing is, the user may add a new department.

Is there a better way to make this static code more dynamic?


texts: C++11 class similar to .Net's ManualResetEvent, but without the ability to "reset"

texts:
The goal is to block all threads that call WaitOne(). When Set() is called, those threads continue. Any calls to WaitOne(), after Set(), return immediately. The challenge is to make sure that all calls to WaitOne() are either "before" or "after" the call to Set(), so that no threads see _state false, but miss the notify_all. I'm looking for feedback on performance and correctness.

#ifndef NO_RESET_EVENT_H
#define NO_RESET_EVENT_H

#include <condition_variable>
#include <atomic>

class NoResetEvent
{
public:
    NoResetEvent() : state(false) {}
    NoResetEvent(const NoResetEvent& other) = delete;

    void WaitOne() {
        std::unique_lock<std::mutex> lock(sync);
        while (!state) {
            underlying.wait(lock);
        }
    }

    void Set() {
        std::unique_lock<std::mutex> lock(sync);
        state = true;
        underlying.notify_all();
    }

private:
    std::condition_variable underlying;
    std::mutex sync;
    volatile bool state;
};

#endif


texts: Making repeated ADODB queries from Excel-SQL Server

texts:
I need to run 12 queries on a SQL Server DB.  Currently I pass the "SELECT..." string to a function below.

Is it optimal to open and close the connection again and again (as my code is doing), or can I set up an ADODB connection as a global variable and reuse the same connection?

Sub ConnectServer(FileName As String, rngToPrint As Range, strSqlQuery As String)
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sConnString As String
    Dim iCols As Long

    If Not (rs Is Nothing) Then
        If (rs.State And adStateOpen) = adStateOpen Then rs.Close
        Set rs = Nothing
    End If

    sConnString = "Poop;"

    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset

    conn.CommandTimeout = 90

    conn.Open sConnString

    Set rs = conn.Execute(strSqlQuery)

    'Add Headers
    For iCols = 0 To rs.Fields.Count - 1
        Sheets(rngToPrint.Worksheet.Name).Cells(rngToPrint.Row - 1, iCols + 1).Value = rs.Fields(iCols).Name
    Next

    If Not rs.EOF Then
        ' Transfer result.
        rngToPrint.CopyFromRecordset rs
    ' Close the recordset
        rs.Close
    Else
        MsgBox "Error: No records returned.", vbCritical
    End If

    If CBool(conn.State And adStateOpen) Then conn.Close
    Set conn = Nothing
    Set rs = Nothing
End Sub


texts: Eratosthenes' and Sundaram's prime sieves in Python

texts:
I recently started playing with Python. What can I do better?

def sieve_of_sundaram(limit):
    new_limit = (limit - 1) // 2
    boolean_list = [True] * new_limit;
    i = 1
    j = i
    while (i + j + 2 * i * j <= new_limit):
        while (i + j + 2 * i * j <= new_limit):
            boolean_list[2 * i * j + i + j - 1] = False
            j += 1
        i += 1
        j = i
    primes = [2]
    for i in range(0, new_limit):
        if boolean_list[i]:
            primes.append(i * 2 + 3)
    return primes

def sieve_of_eratosthenes(limit):
    boolean_list = [True] * limit;
    for i in range(2, limit):
        if i * i > limit:
            break
        while boolean_list[i - 1] == False:
            i += 1
        for j in range(i * i, limit + 1, i):
            boolean_list[j - 1] = False
    primes = []
    for i in range(1, limit):
        if boolean_list[i] == True:
            primes.append(i + 1)
    return primes

i = 100
sundaram_primes = sieve_of_sundaram(i)
eratosthenes_primes = sieve_of_eratosthenes(i)

print("Sundaram primes count: ", len(sundaram_primes))
print(sundaram_primes)
print("Eratosthenes primes count: ", len(eratosthenes_primes))
print(eratosthenes_primes)


texts: Generating a list of alternatively positioned odd and even number out of an array of unsorted integers

texts:
I had an half an hour interview with Microsoft for an intern position.  I was asked to generate a list of alternatively positioned odd and even number out of an array of unsorted integers.  I guess it was below the interviewer expectations since I did not get the offer.  Can any one give me some hints on improving my code?

import java.util.ArrayList;

public class EvenOddArray {

private int min(int n, int m)
{
    int min;
    if (n > m){
        min = m;
    }
    else {
        min = n;
    }
    return min;
}

public ArrayList<Integer> generateEvenOddArray(int[] list)
{
    ArrayList<Integer> oddList = new ArrayList<Integer>();
    ArrayList<Integer> evenList = new ArrayList<Integer>();
    ArrayList<Integer> resultList = new ArrayList<Integer>();

    for (int number : list) {
        if (number%2 == 0){
            evenList.add(number);
        }
        else {
            oddList.add(number);
        }

    }

    int numberOfEvenNumbers = evenList.size();
    int numberOfOddNumbers = oddList.size();
    int minOfOddAndEven = min(numberOfEvenNumbers,numberOfOddNumbers);
    ArrayList<Integer> longestList = getLongestList(evenList,oddList);

    int i ;
    for (i = 0; i < minOfOddAndEven; i++) {
        resultList.add(2*i,oddList.get(i) );
        resultList.add(2*i+1,evenList.get(i));
    }
    if(longestList != null ){
        for (int j = i; j < longestList.size(); j++) {
            resultList.add(longestList.get(j));
        }
    }
    return resultList;

}
private ArrayList<Integer> getLongestList(ArrayList<Integer> listone,
        ArrayList<Integer> listTwo) {
    if (listone.size()<listTwo.size()){
        return listTwo;
    }
    else if (listone.size()>listTwo.size()){
        return listone;
    }
    return null;
}


texts: A method to capitalize the first letter of all words in a String

texts:
I recently was given a task to capitalize the first character of a string provided the string does not begin with a non-alphabet. Below are the requirements and my implementation. Just want to know if there is a more efficient way to implement this.

Requirements

  Please implement this method to capitalize all first letters of the words in the given String. All other symbols shall remain intact. If a
  word starts not with a letter, it shall remain intact too.Assume that
  the parameter String can only contain spaces and alphanumeric
  characters.
          NOTE: please keep in mind that the words can be divided by single or multiple spaces.The spaces also can be found at the beginning or
  the end of the parameter string,and you need to preserve them.

public static String capitalizeFirstLetters(String s) {
        StringBuilder wordsWithFirstLetterCapitalized = new StringBuilder();
        s = s.trim();
        String words[] = s.split("\\ ");
        if (words.length <= 1) {
            if(words.toString().matches("^[A-Za-z].*$")) {
            words.toString().toUpperCase();
            wordsWithFirstLetterCapitalized.append(words);
            }
            else {
                wordsWithFirstLetterCapitalized.append(words);
            }
        }

        else {
            for (String word : words) {
                if (word.matches("^[A-Za-z].*$")) {
                    word = word.substring(0, 1).toUpperCase() + word.substring(1);
                    wordsWithFirstLetterCapitalized.append(word);
                    wordsWithFirstLetterCapitalized.append(" ");
                }
                else {
                    wordsWithFirstLetterCapitalized.append(word);
                }
            }
        }

        return wordsWithFirstLetterCapitalized.toString();
    }


texts: Wrapping fetch(), preserve promise-based API for outer function

texts:
One of my react component's methods is using an async fetch() internally. After fetching the response from the server, it is being converted to JSON, then I perform a check on that JSON string. Finally, I return the result wrapped in a new promise. Here is the code:

fetchCalendarData(offset, length) {
    return fetch(this.props.Uri + '?offset=' + offset + '&length=' + length)
    .then(response => response.json())
    .then((result) => {
        if (result.hasOwnProperty('redirect_to_login') && result.redirect_to_login == true) {
            window.location.reload(window.globalAppState.base_url + "/login");
        }
        return Promise.resolve(result);
    })
    .catch(() => {
        return Promise.reject('FAILURE');
    });
}

When I call the method, I do it like this:

this.fetchCalendarData(0, 6).then((fetched_calendar_data) => {
    this.calendar_data = fetched_calendar_data;
});

The code works, but I am not familiar with promise based flow at all, and those 3 return statements look a little weird to me. I also read about the promise constructor antipattern but am not sure if that applies here. And, is it necessary to return the result wrapped in a new promise, at all?


texts: Creating a shared, asynchronous, cancellable Observable with a custom function in RxPy

texts:
This is an exercise in using ReactiveX in Python. I want to create an Observable in RxPy with the following characteristics:

Emits items through a custom function
Runs asynchronously
Sets up resources upon start and it cleans up at the end
Runs a loop indefinitely (until cancelled)
Can be cancelled
Shared among observers

Kind of like wrapping HTTP streaming data with an Observable.

Thinking that it would work like RxJs, I started defining a straightforward function and using the create operator. It got hairy very quick and I ended up doing a lot of asyncio stuff that I wasn't expecting to do.

How does this code look and how could I improve it (particularly observable_fn)?

You can run it with Python 3.5 and stop it with ctrl+C. I'm working on a Mac.

import asyncio
from rx import Observer, Observable
from rx.core import Scheduler

def observable_fn(observer):
    _task = None

    # Customize this
    def _setup():
        print('Observable setup')

    # Customize this
    def _teardown():
        print('Observable teardown')
        if _task:
            _task.cancel()
        observer.on_completed()

    # Customize this
    async def _loop():
        counter = 0

        while True:
            await asyncio.sleep(2) # Simulating long-running task
            counter += 1
            observer.on_next(counter)

    # Don't touch this
    async def _run_loop():
        try:
            await _loop()
        except asyncio.CancelledError:
            print('Observable cancelled')
        finally:
            _teardown()

    # Don't touch this
    _setup()
    loop = asyncio.get_event_loop()
    _task = loop.create_task(_run_loop())

print('CREATING OBSERVABLE')
observable = Observable.create(observable_fn).observe_on(Scheduler.event_loop).share()

print('CREATING OBSERVERS')
observer1 = observable.subscribe(lambda x: print('O1: {0}'.format(x)))
observer2 = observable.subscribe(lambda x: print('O2: {0}'.format(x)))
observer3 = observable.subscribe(lambda x: print('O3: {0}'.format(x)))

loop = asyncio.get_event_loop()
try:
    print('STARTING LOOP')
    loop.run_forever()
except KeyboardInterrupt:
    pending_tasks = asyncio.Task.all_tasks()
    for task in pending_tasks:
        task.cancel()

    loop.run_until_complete(asyncio.gather(*pending_tasks))
    loop.close()

print('THE END')


texts:
I took a stab at implementing Promise.race():

"use strict";

function race (...promises) {
   return new Promise((resolve, reject) => {
      let [resolved, rejected] = [false, false];
      promises.forEach(promise => {
         promise.then(value => {
            if (!resolved) {
               resolve(value);
               resolved = true;
            }
         },
         reason => {
            if (!rejected) {
               reject(reason);
               rejected = true;
            }
         })
      });
   });
}


texts: Converting an absolute Path to a relative one

texts:
I've this code that I use in some tool, and I wonder if it can be improved:

fn make_relative(target_dir: &Path, current_dir: &Path) -> PathBuf {
    if target_dir.is_relative() {
        return target_dir.into();
    }
    let mut result = PathBuf::new();
    let mut path_before_current_dir = PathBuf::new();
    let mut after_current_dir = false;
    for component in target_dir.components() {
        if after_current_dir {
            result.push(component.as_os_str());
        } else {
            path_before_current_dir.push(component.as_os_str());
        }
        if path_before_current_dir == current_dir {
            after_current_dir = true;
        }
    }
    if result.to_string_lossy().is_empty() {
        ".".into()
    } else {
        result
    }
}

I also wonder if it's okay that I return PathBuf, instead of Path.


texts:
I'm writing some code that helps me deal with units better, by having a user defined type for things like Metres, Centimetres and Millimetres. Here is the class declaration for Metres:

template <typename StorageType>
class Metres : public Unit<StorageType>
{
public:
    static Metres fromMetres (StorageType metres) noexcept;

    Metres operator+ (Metres rhs) const noexcept;
    Metres operator+ (Centimetres<StorageType> rhs) const noexcept;
    Metres operator+ (Millimetres<StorageType> rhs) const noexcept;

protected:
    using Unit<StorageType>::Unit;
};

However, when writing the definitions of the operator+ functions, I find myself repeating Metres<StorageType> quite a lot:

template <typename StorageType>
Metres<StorageType> Metres<StorageType>::fromMetres (StorageType metres) noexcept
{ return { metres }; }

template <typename StorageType>
Metres<StorageType> Metres<StorageType>::operator+ (Millimetres<StorageType> rhs) const noexcept
{ return { this->getRawValue() + rhs.getRawValue() / StorageType (1000) }; }

template <typename StorageType>
Metres<StorageType> Metres<StorageType>::operator+ (Centimetres<StorageType> rhs) const noexcept
{ return { this->getRawValue() + rhs.getRawValue() / StorageType (100) }; }

template <typename StorageType>
Metres<StorageType> Metres<StorageType>::operator+ (Metres<StorageType> rhs) const noexcept
{ return { this->getRawValue() + rhs.getRawValue() }; }

I can't define the functions in the class declaration, as the types Centimetres and Millimetres forward declared. If I define these classes beforehand, I end up with the same problem in a different class.

I feel like this could be done better. How can I reduce the amount of visual noise in these function definitions?

Note:
If there are any rules in C++ that allow the omission of <StorageType> in certain places, quotes from the standard are much appreciated.


texts: Spawning random small cubes in a big invisible cube using Unity C#

texts:
My goal was to create a big invisible cube and inside that spawn small random cubes.
These small cubes would have:

A random position within the big cube.
A random colour.
A random size.
A random lifetime.
A higher chance to spawn with offset of 0 on y-axis than any other
offset.
Ability to rotate in sync with the big cube.
Ability to fade away as it ages.
To be replaced with new random cubes as they expire.

To achieve this, here is the code I wrote:
SpawnCube.cs
    public GameObject smallCubePrefab;
    public float rateOfSpawn = 1;
    private float nextSpawn = 0;

    // Update is called once per frame
    void Update()
    {
        // Spawn new cubes at specified spawn rate
        if (Time.time > nextSpawn)
        {
            nextSpawn = Time.time + rateOfSpawn;
            StartCoroutine(FadeOutCube());
        }
    }

    public List<GameObject> SpawnSmallCubes()
    {
        // Create an empty list of cubes
        List<GameObject> cubesList = new List<GameObject>();

        // Spawn cube at random position within the big cube's transform position
        Vector3 randPosition = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f));

        // Generate higher chance (2 chances in 3) of spawning small cubes with offset = 0 on Y-axis
        List<float> randomY = new List<float>() {0, 0, Random.Range(-1f, 1f)};
        randPosition.y = randomY[Random.Range(0, 3)];
        randPosition = transform.TransformPoint(randPosition * .5f);

        // Spawn small cube
        GameObject smallCube = Instantiate(smallCubePrefab, randPosition, transform.rotation);

        // Give random color
        smallCube.GetComponent<Renderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);

        // Give random size
        int randSize = Random.Range(1, 10);
        smallCube.transform.localScale = new Vector3(randSize, randSize, randSize);

        // Add spawned cube to the list of cubes
        cubesList.Add(smallCube);

        return cubesList;
    }

    public IEnumerator FadeOutCube()
    {
        // Give random lifetime
        float fadeSpeed = Random.Range(0.01f, 0.05f);

        List<GameObject> smallCubes = SpawnSmallCubes();

        foreach (GameObject cube in smallCubes.ToArray())
        {
            while (cube.GetComponent<Renderer>().material.color.a > 0)
            {
                Color cubeColor = cube.GetComponent<Renderer>().material.color;
                float fadeAmount = cubeColor.a - (fadeSpeed * Time.deltaTime);

                cubeColor = new Color(cubeColor.r, cubeColor.g, cubeColor.b, fadeAmount);
                cube.GetComponent<Renderer>().material.color = cubeColor;
                yield return null;
            }
            if(cube.GetComponent<Renderer>().material.color.a <= 0)
            {
                Destroy(cube);
                smallCubes.Remove(cube);
            }
        }
    }

Rotate.cs
public Vector3 rotationSpeed;

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(rotationSpeed * Time.deltaTime);
    }

Any suggestions for improvement or changes to make it better in any way?
Thanks


texts:
I just recently learned how to write Methods in C#. Here is one experiment I made. It calculates addition, subtraction, division, multiplication, and the square root of a number. fill free to give me suggestions on how I can improve the code. I also do know I need to set up a system that prevents the user from typing wrong answers. I just don't have enough experience to do it yet (I tried).
static void Main(string[] args)
    {
       string userAnswer;

       Console.WriteLine("Calculater or Square Root Calculater?");
       Console.WriteLine("-Please respond with Calculater or Square");
       userAnswer = Console.ReadLine().ToUpper();
       Console.Clear();

       switch(userAnswer)
       {
           case "CALCULATER":
            Calculater();
           break;
           case "SQUARE":
            SquareRootCalculater();
           break;
           default:
           Console.WriteLine("Invalid Input");
           break;
       }

        Console.ReadKey();
    }

     static void SquareRootCalculater()
    {
        int squareNumber;

        Console.WriteLine("Input Number:");
        squareNumber = Convert.ToInt32(Console.ReadLine());
        Console.Clear();

        Console.WriteLine($"The Square Root of {squareNumber} is");
        Console.WriteLine(Math.Sqrt(squareNumber));
    }

    static void Calculater()
    {
        int firstNumber;
        string sign;
        int secondNumber;
        int answer = 0;

        Console.WriteLine("Input Number:");
        firstNumber = Convert.ToInt32(Console.ReadLine());
        Console.Clear();

        Console.WriteLine("Input Operation:");
        sign = Console.ReadLine();
        Console.Clear();

        Console.WriteLine("Input Second Number:");
        secondNumber = Convert.ToInt32(Console.ReadLine());
        Console.Clear();

        switch (sign)
        {
            case "+":
                answer = firstNumber + secondNumber;
            break;
            case "-":
                answer = firstNumber - secondNumber;
            break;
            case "*":
                answer = firstNumber * secondNumber;
            break;
            case "/":
                answer = firstNumber / secondNumber;
            break;
            default:
                Console.WriteLine("Invalid Input");
            break;
        }
        Console.WriteLine($"The answer of {firstNumber} {sign} {secondNumber} is");
        Console.WriteLine(answer);

    }


texts:
I just wrote a small class that generates perfectly unique colors(or that is the plan anyway). I found the original index shifting snippet online, and was just wondering if it could be improved in anyway, or if any problems could arise from using it.

Here is the class which is used to create the unique colors.

public class UniqueColorGenerator
    {
        private int _colorIndex;
        public UniqueColorGenerator()
        {

        }

        public Color Next()
        {
            _colorIndex++;
            byte red = ( byte )( _colorIndex & 0x000000FF );
            byte green = ( byte )( ( _colorIndex & 0x0000FF00 ) >> 08 );
            byte blue = ( byte )( ( _colorIndex & 0x00FF0000 ) >> 16 );
            return Color.FromArgb( red , green , blue );
        }
    }

I am using this for "geometry picking" in OpenGL. It works beautifully, too. Here is a picture of 16,384 quads with their own unique color... or I guess I should say "unique shade of red and black".

When I first ran it, I could swear that there were obvious duplicates, but there aren't. Not only did the picking work flawlessly, but I also ran through the Colors and didn't get a single collision. Though I am still wondering if any problems could arise from how the generator is currently built—besides hitting integer max, which will also never happen.

Also, how can I randomize the colors more, so instead of getting different shades of red, I could receive more colors along the spectrum?


texts:
Your program is to use the brute-force approach in order to find the Answer to  Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All
  numbers at input are integers of one or two digits.

I submitted the code for the above problem on spoj and it says wrong answer even when writing the correct output.

The first solution I submitted was:

#include <stdio.h>
int main(void)
{
    int i;
    while (1) {
        scanf("%d", &i);
        if (i == 42)
            break;
        printf("%d", i);
    }
    return 0;
}

The above solution doesn't check whether the input is two digit or not so I wrote another one:

#include <stdio.h>
int main(void)
{
    int i;
    while (1) {
        scanf("%d", &i);
        if (i > 99)
            continue;
        if (i == 42)
            break;
        printf("%d", i);
    }
    return 0;
}

  ... rewrite small numbers from input to output. All numbers at input ... one or two digits.

However, both of the above solutions don't work on spoj.


texts:
I'm trying to follow the path of Sandi Metz and others who state that aiming for small and simple is good for writing understandable code. I've written a method that takes care of writing content to a file without overwriting any existing file of the same name. I wrote it TDD style and, once the tests passed, I refactored the rather large methods into smaller methods, hoping that it would improve readability.

I hope you can tell me whether I succeeded, or how I could further improve on my code. I've considered adding modules (such as a "Appender" module) to my class in order to improve the code according to the single responsibility principle, but can't figure out whether it would actually clarify things more, or whether the added lines would make the intended  clarification void.

class CautiousWriter
  def self.write(filename, content)
    if File.exists?(filename)
      rename_existing_file_beginning_with(filename)
    end
    create_file(filename, content)
  end

  private

  def self.rename_existing_file_beginning_with(filename)
    if File.exists? append_old_to(filename)
      rename_with_old_and_number_appended(filename)
    else
      rename_with_old_appended(filename)
    end
  end

  def self.rename_with_old_and_number_appended(filename)
    old_and_num_appended = find_available_filename(filename)
    rename(filename, old_and_num_appended)
  end

  def self.find_available_filename(filename)
    number = 0
    until File.exists?(append_old_to(filename) + number.to_s) == false
      number += 1
    end
    append_old_to(filename) + number.to_s
  end

  def self.rename_with_old_appended(filename)
    rename(filename, append_old_to(filename))
  end

  def self.rename(filename, new_filename)
    File.rename(filename, new_filename)
  end

  def self.create_file(filename, content)
    File.open(filename, 'w') { |file| file.write(content) }
  end

  def self.append_old_to(filename)
    filename + '_old'
  end
end


texts: My implementation of javascript class library

texts:
I'm reading a book called JavaScript Web Applications. In first chapter, it introduces an implementation of class library. Here's an intermediate version:

var Class = function () {
    var klass = function () {
        this.init.apply(this, arguments);
    }
    klass.prototype.init = function (args) {};

    // Adding class properties
    klass.extend = function (obj) {
        var extended = obj.extended;
        for (var i in obj) {
            klass[i] = obj[i];
        }
        if (extended) extended(klass);
    };

    // Adding instance propertied
    klass.include = function (obj) {
        var included = obj.included;
        for (var i in obj) {
            klass.prototype[i] = obj[i];
        }
        if (included) include(klass)
    };

    return klass;
}

Then it can be used like this:

var Person = new Class;
Person.extend({
    find: function (id) { /* ... */ }
});
var person = Person.find(1);

var Person = new Class;
Person.include({
    save: function () { /* ... */ }
});
var person = new Person;
person.save();

I modified it to that it support non-shared instance properties, and here's my version:

var Class = function (properties) {
    var klass = function () {
        this.init.apply(this, arguments);
    }
    klass.prototype.init = function (args) {
        var self = this;
        if (properties instanceof Array && typeof args === 'object') {
            properties.forEach(function (p) {
                if (typeof args[p] !== 'undefined') {
                    self[p] = args[p];
                }
            });
        }
    };

    // extend and include

    return klass;
}

So it can be used like this:

var Person = new Class(['name', 'age'];
var person = new Person({ name: 'bob', age: '30' });

Is my approach ok?
I also wonder that why Person.name was "" in my approach? I expected it to be undefined.


texts:
I've decided to write a very simply output programming language. All the user does is write ASCII values inside ASCII fish, and the interpreter pieces the values together and outputs them.

I'm mainly looking for feedback on the interpreter, as the language itself is very easy to understand.

Here's what a Hello, World! program looks like in Fishy:

><72> ><101> ><108> ><108> ><111> ><44> ><32> ><87> ><111> ><114> ><108> ><100> ><33>

All the rules of the language are listed in the module docstring of the program.

"""

Fishy (.fishy extension)

><> Frontfish

Implementation is simple:
You enter ASCII values between the facing signs <>

Commands on separate lines will have output separated by a new line

Example:

><98> ><112> ><113> ><107>
bpqk

><97>
><108>
><101>

a
l
e

NO TRAILING WHITESPACE!
Trailing whitespace after the last fish on the line will result in a syntax error

"""

import argparse
import os
import sys
from typing import List

def run_code(code: List[str]):
    """
    Runs the passed Fishy Code
    """
    for line in code:
        # Clean up code and separate commands#
        line = line.strip("\n")
        commands = line.split(" ")

        # Check if line has multiple statements in it
        if len(commands) > 1:
            if correct_syntax(commands):
                output = "".join(chr(get_number(fish)) for fish in commands)
                print(output)
        else:
           if correct_syntax(commands):
               print(chr(get_number(commands[0])))

def correct_syntax(pond: List[str]) -> bool:
    """
    Checks the syntax of the passed list of commands on the following criteria:

    Is a fish ><..>

    Correct Example:
    ><98> ><108> ><56>

    Incorrect Example:
    ><98> >><<76>> ><[108>

    """
    for fish in pond:
        if not is_fish(fish):
            sys.exit(f"Incorrect Syntax: {fish}")
    return True

def is_fish(fish: str) -> bool:
    """
    Returns if the passed fish is the fish or not

    Fish: Starts with >< ends with >

    A fish like so ><98g> will be caught by "get_number()" function

    """
    return fish.startswith("><") and fish.endswith(">")

def get_number(fish: str) -> int:
    """
    Returns the number in the fish
    """
    # Check font fish first #
    try:
        number = int(fish[2:-1])
    except ValueError:
        sys.exit(f"Incorrect Syntax: {fish}")
    return number

def get_content(file: str) -> List[str]:
    """
    Returns all the content in the passed file path

    :param file -> str: File to read content

    :return List[str]: Content in file
    """
    with open(file, "r") as file:
        return [line for line in file]

def main() -> None:
    """
    Sets up argparser and runs main program
    """
    parser = argparse.ArgumentParser(description="Enter path to .fishy program file")
    parser.add_argument("Path", metavar="path", type=str, help="path to .fishy program file")
    args = parser.parse_args()
    file_path = args.Path

    if not os.path.isfile(file_path):
        sys.exit("The file does not exist")

    content = get_content(file_path)
    run_code(content)

if __name__ == "__main__":
    main()


texts: Iteratively generating permutations with repetitions

texts:
I tried to solve the simple problem of generating all permutations of length n with m distinct elements where repetition is allowed. Doing this recursively took me like 10 minutes:

void genAllPermsRec(vector<int>& perm, vector<vector<int>>& perms, int n, int m) {
    if (perm.size() == n) {
        perms.push_back(perm);
        return;
    }
    for (int i = 0; i < m + 1; ++i) {
        perm.push_back(i);
        genAllPermsRec(perm, perms, n, m);
        perm.pop_back();
    }
}

I tried to do it iteratively then and it took me a lot longer. I ended up looking exactly at what happens in the recursive version and came up with the following solution:

vector<vector<int>> genAllPerms(int n, int m) {
    vector<vector<int>> perms;
    vector<int> perm(1, 0);
    int i = 0;
    while (!perm.empty()) {
        if (perm.size() == n) {
            perms.push_back(perm);
            if (perm.back() < m) {
                perm.pop_back();
                ++i;
                continue; // That is why Python has the while: .. else: construct.
            }
            while (perm.back() == m) { perm.pop_back(); }
            if (!perm.empty()) {
                perm.back() += 1;
                i = 0;
            }
        } else { perm.push_back(i); }
    }
    return perms;
}

It seems to work but I am not satisfied with the result. I suspect that there is a more intuitive way to approach the problem that would lead to shorter code. Please enlighten me :)


texts:
The following is my solution to the problem presented on this CodeReview post:

Friends A, B, C, D go for a trip. They spend on various expenses. Cost of the expense is shared.
Example :

A spends 100 for breakfast for A, B, C and D
D spends 500 for cab for B and C
B spends 300 for lunch for A, B and C

Write a program to calculate how much each should get or each should give to one another. App should be scalable that number of friends can change

Design:
We need two data sets, which each respectively:

record transactions
tally amount due to each person

Simply strip off the info we need from the transactions and record either how much each person is owed, or how much each person should give.
Any input is welcome!
using System;
using System.Collections.Generic;

namespace CodeReview
{
    class Transaction
    {
        class Account
        {
            private int totalDebt = 0;
            private Dictionary<char, int> debtors = new Dictionary<char, int>();

            public Account(char initialDebtor, int initialDebt)
            {
                debtors.Add(initialDebtor, initialDebt);
            }

            public Dictionary<char, int> Debtors
            {
                get
                {
                    return debtors;
                }
            }

            public int TotalDebt
            {
                get
                {
                    foreach (char debtor in debtors.Keys)
                    {
                        totalDebt += debtors[debtor];
                    }

                    return totalDebt;
                }
            }
        }

        static void Main(string[] args)
        {
            Queue<Tuple<char, int, List<char>>> transactions = new Queue<Tuple<char, int, List<char>>>();
            Dictionary<char, Account> ledger = new Dictionary<char, Account>();

            transactions.Enqueue(Tuple.Create('A', 100, new List<char>() { 'A', 'B', 'C', 'D' }));
            transactions.Enqueue(Tuple.Create('D', 500, new List<char>() { 'B', 'C' }));
            transactions.Enqueue(Tuple.Create('B', 300, new List<char>() { 'A', 'B', 'C' }));

            while (transactions.Count > 0)
            {
                Tuple<char, int, List<char>> transaction = transactions.Dequeue();

                foreach (char beneficiary in transaction.Item3)
                {
                    if (!beneficiary.Equals(transaction.Item1))
                    {
                        if (ledger.ContainsKey(beneficiary))
                        {
                            Dictionary<char, int> debtors = ledger[beneficiary].Debtors;

                            if (debtors.ContainsKey(transaction.Item1))
                            {
                                debtors[transaction.Item1] += transaction.Item2;
                            }
                            else
                            {
                                debtors.Add(transaction.Item1, transaction.Item2);
                            }
                        }
                        else
                        {
                            ledger.Add(beneficiary, new Account(transaction.Item1, transaction.Item2));
                        }
                    }
                }
            }

            foreach (char beneficiary in ledger.Keys)
            {
                Account account = ledger[beneficiary];

                Console.WriteLine("User " + beneficiary + " owes $" + account.TotalDebt + ":");

                foreach (char debtor in account.Debtors.Keys)
                {
                    int debt = account.Debtors[debtor];

                    Console.WriteLine(" - $" + debt + " to " + debtor);
                }
            }

            Console.ReadKey();
        }
    }
}


texts: Retrieve Father and Child structure in a Database Table

texts:
I have a table with this schema:

CREATE TABLE [td].[MyTable] (
     [ID] [int] NOT NULL
    ,[FatherID] [int] NULL
)

(Note: I have excluded all the columns not relevant to the discussion)

I receive an [ID] as input, and I need to collect the relative record and all of its fathers in one single output.
Every record has the id of the Father stored in the [FatherID] column. There is only one root element and it is recognized when we found the [FatherID] = 0.

This is the current full working query:

IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL DROP TABLE #MyTempTable

DECLARE @ID CHAR(11)

SET @ID = 13192

SELECT *
INTO #MyTempTable
FROM [MyTable]
WHERE [ID] = @ID

SELECT @ID = [FatherID]
FROM #MyTempTable
WHERE [ID] = @ID

WHILE @ID <> 0
BEGIN
    INSERT INTO #MyTempTable
    SELECT *
    FROM [MyTable]
    WHERE [ID] = @ID

    SELECT @ID = [FatherID]
    FROM #MyTempTable
    WHERE [ID] = @ID
END

SELECT *
FROM #MyTempTable
ORDER BY [ID]

The perfomance are fine, but I want improve the readability. Any suggestions?


texts:
This is my database connection class in order to handle multiple connections with the use of context managers. Brief explanation:

The __init__ method reads database configurations from an INI file in order to call the connection and set two dictionaries, one for connections and one for cursors.
The __enter__ method takes the specified database connection and cursor returning at the end the cursor to execute queries.
The __exit__ method commits the query and close the cursor if no exception has been identified.

I'm using PyMySQL as MySQL module but I think that the code can be generalized for all MySQL modules, the functions are always those.

Any advice is welcome, I'm new with Python!

import os.path
import configparser
import pymysql.cursors

class Database:
    def __init__(self):
        config = configparser.ConfigParser()

        dbConfigFile = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'dbconfig.ini'))
        config.read(dbConfigFile)

        # Connections (database:connection method)
        self.connections = {
            'db1': self.connection(dict(config.items('db1info'))),
            'db2': self.connection(dict(config.items('db2info')))
        }

        # Cursors list (database:cursor method)
        self.cursors = {
            'db1': self.cursor(self.connections['db1']),
            'db2': self.cursor(self.connections['db2'])
        }

    def connection(self, config):
        return pymysql.connect(**config)

    def cursor(self, connection):
        return connection.cursor

    def __call__(self, database):
        self.database = database
        return self

    def __enter__(self):
        self.db = self.connections[self.database]
        self.cursor = self.cursors[self.database](pymysql.cursors.SSDictCursor)
        return self.cursor

    def __exit__(self, exc_type, exc_value, tb):
        if not exc_type:
            self.db.commit()
            self.cursor.close()

Usage:

db = Database()
with db('db1') as cursor:
    cursor.execute("SELECT 1 FROM table_name")


texts: Implementing Addition, subtraction, multiplication, division of numbers of different base and give result in desired base:

texts:
Implement addition, subtraction, division and multiplication of given numbers of different base and provide result in desired base:

Example:
n1,b1 = 1010, 2
n2,b2 = 100, 10
desired_base = 9
addition(n1,b1,n2,b2,desired_base) = 22
subtraction(n1,b1,n2,b2,desired_base) = 0
multiplication(n1,b1,n2,b2,desired_base) = 121

I have written this code, but How to improve its complexity?
global dict_base, dict_reverse
dict_base = {'0': 0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6,'7':7,'8':8, '9':9, 'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15, 'G':16, 'H':17}
dict_reverse = {v: k for k, v in dict_base.iteritems()}
def convert_to_base_10(num_str, base):
    global dict_base
    k = 1
    ans = 0
    for char in num_str[::-1]:
        ans += dict_base[char] * k
        k = k * base
    return int(ans)

def convert_from_base_10(num, des_base):
    global dict_reverse
    ans = []
    neg_flag = False
    import pdb; pdb.set_trace()
    if num < 0:
        neg_flag = True
        num = num * (-1)
    while num>0:
        ans.append(dict_reverse[num % des_base])
        num = num / des_base
    if neg_flag:
        return '-'+''.join(str(digit) for digit in ans[::-1])
    return ''.join(str(digit) for digit in ans[::-1])

def addition(n1, b1, n2, b2, b):
    n1_10 = convert_to_base_10(n1,b1)
    n2_10 = convert_to_base_10(n1,b1)
    ans = n1_10 + n2_10
    ans = convert_from_base_10(ans, b)
    return ans

def subtraction(n1, b1, n2, b2, b):
    n1_10 = convert_to_base_10(n1,b1)
    n2_10 = convert_to_base_10(n1,b1)
    ans = n1_10 - n2_10
    ans = convert_from_base_10(ans, b)
    return ans

def multiplication(n1, b1, n2, b2, b):
    n1_10 = convert_to_base_10(n1,b1)
    n2_10 = convert_to_base_10(n1,b1)
    ans = n1_10 * n2_10
    ans = convert_from_base_10(ans, b)
    return ans

print subtraction('1010', 2,'100', 10, 9)
print addition('1010', 2,'100', 10, 9)
print multiplication('1010', 2,'100', 10, 9)


texts: Determine all prime, palindrome string integers within a range

texts:
I have written a program in Python 3x to determine all the prime, palindrome string integers in the range of two given numbers.

from math import sqrt

def check_prime(n):
    if n<2:
        return False
    for i in range(2,int(sqrt(n))+1):
        if n%i == 0:
            return False
    else:
        return True

for times in range(5):
    a,b = map(int,input().split())
    ct = 0
    for n in range(a,b+1):
        if str(n) == str(n)[::-1] and check_prime(n):
            ct += 1
    print(ct)

How can I make this program run faster, mainly optimizing my check_prime function so that it is faster than currently?


texts: Retrieve compilation files of smart contracts asynchronously via Ajax and add to database

texts:
I need to retrieve two files that are labeled :abi and :bin from a server. I then want to add these files to a database in a method with a signature like (q/upsert-contract <abi> <bin>).

To do this I used core.async and wrote the following code:

(defn fetch-contract-code [contract-key code-type]
  (let [result-chan (async/chan)
        handler     (fn [[ok data]]
                      (if ok
                        (go (async/>! result-chan {code-type data})
                            (async/close! result-chan))
                        (println "error fetching" contract-key)))
        request     {:method          :get
                     :uri             (string/format "./contracts/build/%s.%s"
                                                     (name contract-key)
                                                     (name code-type))
                     :timeout         6000
                     :response-format (if (= code-type :abi)
                                        (ajax/json-response-format)
                                        (ajax/text-response-format))
                     :handler         handler}]
    (ajax/ajax-request request)
    result-chan))

(defn add-compiled-contract
  "Retrieve :abi or :bin of smart contract with contract-key and store in db"
  [contract-key]
  (go (let [result-chans      (map (partial fetch-contract-code contract-key) [:abi :bin])
            {:keys [abi bin]} (async/<!
                               (go-loop [acc {} chans result-chans]
                                 (let [c (first chans)]
                                   (if c
                                     (recur (merge acc (async/<! c))
                                            (next chans))
                                     acc))))]
        (q/upsert-contract contract-key abi bin))))

So it returns core.async channels with the data on it in fetch-contract-code (labeling it with either :abi or :bin), and it is retrieved in add-compiled-contract later.

What I find ugly is that everything is returning channels, and I have to get data from all channels. I have a feeling things can be cleaned up, but I don't know how.

Could someone help me cleaning up the following code? Perhaps by using transducers? Or outside of the box thinking?


texts: Retrieving album artist info from third party API

texts:
I've recently just started with AngularJS and the best way for me to learn is to get some criticism to find out how I can do things better. I did a small project that calls a third party API and shows a bit of the return data.

HTML

      <input id="artist" type="text" placeholder="Enter artist name" ng-model="artist">
      <button ng-click="getAlbums()">Get Albums</button>

 0">

Album Info

            {{album.name}}

Controller

jamendoApp.controller('Album', function($scope, $http) {
  $scope.getAlbums = function() {
    $http.get("http://api.jamendo.com/v3.0/albums/?client_id=b6747d04&format=jsonpretty&artist_name=" + $scope.artist)
         .success(function(data) {
            if(data.headers.status == 'success') {
              $scope.albums = data.results;
            }
            else {
              alert("Error - " + data.headers.error_message);
            }
         })
         .error(function(x, status, error) {
           alert(error);
         });
     };
});

What I'm specifically interested in knowing is:

Am I writing the JavaScript for the controller correctly? It seems to work, but I can't help but feel like I'm not writing it the way actual JavaScript developers would.
Am I using the AngularJS directives correctly in the HTML? I'm especially concerned if I'm using the ng-if as it's intended.
Plus anything that may catch your eye that could be improved.

To view the whole application feel free to view it on GitHub.


texts:
Wanna learn Rust? Ya rly!

I can haz FizzBuzz? No wai!

Gimme /bin/cat then.  But rustc say LOL! Must handle teh err0rz!

Now iz all err0r handlurz, so /bin/lolcat.  Rust sux, or OP sux?

Plain English translation:

Wanted to learn Rust, but didn't want to write FizzBuzz. How about writing /bin/cat? But for every function call, Rust insists that you do something with the Result, which may be an error. Simple task turned out to be complicated by all the error handling. What went wrong?

// Using rustc 1.0.0-beta.3
use std::env;
use std::io::Read;
use std::io::Write;
use std::fs::File;
use std::path::Path;
use std::process;

macro_rules! println_stderr(
    ($($arg:tt)*) => (
        match writeln!(&mut ::std::io::stderr(), $($arg)* ) {
            Err(e) => panic!("Unable to write to stderr: {}", e),
            Ok(_) => {},
        }
    )
);

fn main() {
    let args: Vec<_> = env::args().collect();
    if args.len() < 2 {
        println_stderr!("Usage: {} GIMME_TEH_FILEZ",
                Path::new(&args[0]).file_name().unwrap().to_str().unwrap());
        process::exit(1);
    }

    let path = Path::new(&args[1]);
    let mut file = match File::open(path) {
        Err(e) => {
            println_stderr!("{}", e);
            process::exit(1);
        },
        Ok(f) => f,
    };

    let mut buf = String::new();
    match file.read_to_string(&mut buf) {
        Err(e) => {
            println_stderr!("{}", e);
            process::exit(1);
        },
        Ok(_) => {},
    }
    print!("{}", buf);
}


texts: helper method returning a call to itself

texts:
I wanted to eliminate some duplication and came up with something I've not seen so I figured I post it and see if others had.

Using a delegate to allow the method to return another call to itself.

The commented out code is what I replaced.

delegate ChainRepositoryCommand ChainRepositoryCommand(Action<Data.Repository> action);
ChainRepositoryCommand RepositoryCommand(Action<Data.Repository> action)
{
    using (var repo = new Data.Repository(true))
    {
        action(repo);
    }
    return new ChainRepositoryCommand(a => RepositoryCommand(a));
}

public void Register(ISender<T> sender)
{
    RepositoryCommand
        (repo => repo.AddClient(sender.Name, true))
        (repo => repo.AddMessageType<T>())
        (repo => repo.AddRegistration<T>(this.name, sender.Name));
    //using (var repo = new Data.Repository(true))
    //{
    //    repo.AddClient(sender.Name, true);
    //}
    //using (var repo = new Data.Repository(true))
    //{
    //    repo.AddMessageType<T>();
    //}
    //using (var repo = new Data.Repository(true))
    //{
    //    repo.AddRegistration<T>(this.name, sender.Name);
    //}
    sender.Sending += sender_Sending;
}

Update: I worked it into my repository class as a static method.  I know this is all probably obfuscating from a clean code standpoint, but it's fun to experiment with new things from time to time.

public class Repository : IDisposable
{
    ...
    public delegate Chain Chain(Action<Repository> action);
    public static Chain Command(Action<Repository> action)
    {
        using (var repo = new Data.Repository(true))
        {
            action(repo);
        }
        return new Chain(next => Command(next));
    }
    ...
}

public class Server<T> : IServer<T>
{
    ...
    public Server(string name)
    {
        serverName = name;
        Data.Repository.Command
            (repo => repo.AddServer(name));
    }

    public void Register(ISender<T> sender)
    {
        Data.Repository.Command
            (repo => repo.AddClient(sender.Name, true))
            (repo => repo.AddMessageType<T>())
            (repo => repo.AddRegistration<T>(this.serverName, sender.Name));
        sender.Sending += sender_Sending;
    }
    ...
}


texts: Python stack and queue classes, implemented using lists

texts:
I have made a stack and queue implementation with OOP in Python (sorry for variable names) . It is made from scratch. I just watched a video how these two things work. I am still trying to improve in OOP so I would be thankful for every advice. Two things which bother me the most are using que and sta in the beginning and whether it is okay to just use already implemented functions in Python such as .pop() .

QUEUE

que = []

class Queue:
    def __init__(self,queue):
        self.queue = queue

    def push(self,item):
        print ("PUSH",item)

        if len(self.queue)!=0:
            self.queue.append(0)
            for i in range(len(self.queue)-1,0,-1): ## replacing elements
                self.queue[i] = self.queue[i-1]
            self.queue[0] = item

        else:
            self.queue.append(item)

    def pop(self):
        print ("POP",self.queue[-1])

        if len(self.queue)!=0:
            self.queue.pop()
        else:
            raise ValueError('Cant pop from empty queue.')

    def __str__(self):
        return (str(self.queue))

## creating QUEUE
que = Queue(que)
que.push(5)
print (que)
que.push(6)
print (que)
que.push(7)
print (que)
que.pop()
print (que)

STACK

class Stack:
    def __init__(self,stack):
        self.stack = stack

    def push(self,item):
        print ("PUSH",item)

        if len(self.stack)!=0:
            self.stack.append(0)
            for i in range(len(self.stack)-1,0,-1):
                self.stack[i] = self.stack[i-1]
            self.stack[0] = item

        else:
            self.stack.append(item)

    def pop(self):
        print ("POP",self.stack[0])

        if len(self.stack)!=0:
            self.stack.pop(0)
        else:
            raise ValueError('Cant pop from empty stack.')

    def __str__(self):
        return (str(self.stack))
## creating STACK
stac = Stack(sta)
stac.push(5)
print(stac)
stac.push(7)
print(stac)
stac.push(2)
print(stac)
stac.pop()
print(stac)


texts:
I am dealing with the following problem:

  Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the contents of the queue.  Note that this does not mean simply displaying the contents of the underlying array.  You should show the queue contents from the first item inserted to the last, without indicating to the viewer whether the sequence is broken by wrapping around the end of the array.  be careful that one item and no items display properly, no matter where front and rear are.

I have managed to solve it, however, I am not sure if my solution is efficient or the best possible solution. I am always willing to learn from my mistakes.  I have posted the code below and in particular please take a look at my display() method and let me know if it's correct and also if I could have written a better solution. I am open to all suggestions and criticism.

public class Queue {

    private int maxSize;
    private int[] queArray;
    private int front;
    private int rear;
    private int nItems;

    public Queue(int s) {
        maxSize = s;
        queArray = new int[maxSize];
        front = 0;
        rear = -1;
        nItems = 0;
    }

    public void insert(int j) {
        if (rear == maxSize - 1)
            rear = -1;

        queArray[++rear] = j;
        nItems++;

    }

    public int remove() {
        int removed = queArray[front++];
        if (front == maxSize)
            front = 0;

        nItems--;

        return removed;
    }

    public int peek() {
        return queArray[front];
    }

    public boolean isEmpty() {
        return (nItems == 0);
    }

    public boolean isFull() {
        return (nItems == maxSize);
    }

    public int size() {
        return nItems;
    }

    public void display() {
        System.out.println("First Inserted Item to Last Inserted Item");

        if (rear < front && (!isEmpty())) {
            for (int i = front; i < maxSize; i++) {
                System.out.println(queArray[i]);
            }

            for (int i = 0; i <= rear; i++) {
                System.out.println(queArray[i]);
            }

        }

        else if (front >= rear && (!isEmpty())) {
            for (int i = front; i <= rear; i++) {
                System.out.println(queArray[i]);
            }
        }

        else {
            System.out.println("Queue is Empty!");
        }

    }

}


texts:
I'm new to Haskell, and here is my first not-totally-trivial program. It's the first program I tend to write in any language -- a Markov text generator. I'm wondering what I can change to make it more idiomatic, or what language features I could make better use of.

import Data.List
import System.Environment
import qualified Data.Map as Map
import Control.Monad.State
import System.Random

type MarkovMap = Map.Map String String
type MarkovState = (MarkovMap, StdGen, String)

transition :: State MarkovState Char
transition = do
    (m, gen, sofar) <- get
    let options = m Map.! sofar
        (index, newGen) = randomR (0, length options - 1) gen
        next = options !! index
    put (m, newGen, tail sofar ++ [next])
    return next

generateText :: State MarkovState Char -> State MarkovState String
generateText s = do
    x <- s
    xs <- generateText s
    return (x:xs)

getWords :: MarkovMap -> Int -> [String]
getWords m n =
    let keys        = filter ((==) ' ' . last) $ Map.keys m
        (r, gen)    = randomR (0, length keys - 1) $ mkStdGen 137
        startState  = keys !! r
        markovChars = evalState (generateText transition) (m, gen, startState)
    in  take n . words $ markovChars

printWords :: [String] -> IO ()
printWords ws = mapM_ putStrLn $ makeLines ws
    where makeLines [] = []
          makeLines ws = unwords (take 10 ws) : makeLines (drop 10 ws)

main :: IO ()
main = do
    (n:nwords:fileName:[]) <- getArgs
    contents <- readFile fileName
    let chainMap = chain (read n :: Int) . unwords . words $ contents
    printWords $ getWords chainMap (read nwords :: Int)

chain :: Int -> String -> Map.Map String String
chain n xs =
    let from = map (take n) . tails $ xs ++ " " ++ xs
        to   = drop n . map (:[]) $ xs ++ " " ++ take n xs
    in Map.fromListWith (++) $ zip from to

Example usage:

Keep track of the last 3 characters, take 100 words from tobeornottobe.txt

> runhaskell markov.hs 3 100 tobeornottobe.txt

delay, the law's count with who would bear things all;
and that make and sweary life; fortal shocks the hue
of returns tural consience of somethis againsolution. To die, thers
the he law's the have, or nobler retus resolence to
troublesh is noblesh is sicklied of regards of some will,
and arrows of? To beary from who would by a
life; for inst give spurns, and, but to sleep: perchan
flesh is heir thing afterprises us for no mortal shocks
turns of action devoutly to dreathe sleep: perchance thance the
ills we hue of time, to suffled of great pith


texts:
I need to inject same variables into settings.py for multiple django apps.hence i wrote a module which takes in output of local() and modifies. This works but is it the right way to do things?

def enable_theme(theme_name, container):
"""
Enable the settings for a custom theme, whose files should be stored
in ENV_ROOT/themes/THEME_NAME (e.g., edx_all/themes/stanford).

The THEME_NAME setting should be configured separately since it can't
be set here (this function closes too early). An idiom for doing this
is:

   THEME_NAME = "stanford"
   enable_theme(THEME_NAME)
"""
    container["THEME_NAME"] = theme_name
    container['MITX_FEATURES']['USE_CUSTOM_THEME'] = True
    # Calculate the location of the theme's files
    theme_root = container['THEME_ROOT'] / "themes" / theme_name
    # Include the theme's templates in the template search paths
    container['TEMPLATE_DIRS'].append(theme_root / 'templates')
    container['THEME_SASS_DIRECTORY'] = theme_root / 'static/saas'
    container['MAKO_TEMPLATES']['main'].append(theme_root / 'templates')
    # Namespace the theme's static files to 'themes/<theme_name>' to
    # avoid collisions with default edX static files
    container['STATICFILES_DIRS'].append((u'themes/%s' % theme_name,
                                   theme_root / 'static'))
    container['FAVICON_PATH'] = 'themes/%s/images/favicon.ico' % theme_name

#Test case
from theming_utils.template_customizer import enable_theme
THEME_NAME = "sokratik"
PIPELINE = False
MITX_FEATURES['USE_DJANGO_PIPELINE'] = False
DEBUG = True
TEMPLATE_DEBUG = True
enable_theme(THEME_NAME, locals())


texts: Find values in list which sum to a given value within tolerance

texts:
This is really a follow on from a question I asked earlier this year on Stack Overflow. I received a great answer to the following problem:

  I'm trying to code up something simple and pythonic to identify
  combinations of values from a list which sum to a defined value,
  within some tolerance.

  For example:

  if A=[0.4,2,3,1.4,2.6,6.3] and the target value is 5 +/- 0.5, then the
  output I want is (2,3), (1.4,2.6), (2,2.6), (0.4,2,3), (0.4,3,1.4)
  etc. if no combinations are found then the function should return 0 or
  none or something similar.

I implemented the suggestion in my code. However, the method has quickly become the performance limiting step in my code. It's fairly quick to run each iteration but it is being run many, many times.

Can you see any way to optimise this function or replace it with something much faster?

def findSum(self, dataArray, target, tolerance=0.5):
    for i in xrange(1, len(dataArray)+1):

        results = [list(comb) for comb in list(itertools.combinations(dataArray, i))
                   if target-tolerance < sum(map(float, comb)) < target+tolerance]

        if len(results) != 0:
            return results


texts: Quick Sort Implementation with iterators

texts:
Most quicksort examples online use C-style arrays and indexes. I have written below quicksort implementation with STL style iterators. It looks fine to me, but I feel like I may be making more than necessary copies of iterators. I am open for any suggestions/improvements:

#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
#include <iterator>

template <typename Iter>
inline Iter partition(const Iter& beg, const Iter& end) {
  using std::swap;
  assert(beg != end);
  auto piv = end;
  --piv; // Pivot is the last element
  auto index_small = beg;

  for (auto index_large = beg; index_large != piv; ++index_large) {
    if (*index_large <= *piv) {
      swap(*index_large, *index_small);
      ++index_small;
    }
  }
  swap(*index_small, *piv);
  return index_small;
}

template <typename Iter>
void quick_sort(const Iter& beg, const Iter& end) {
  if (end - beg > 1) {
    const auto& piv = partition(beg, end);
    quick_sort(beg, piv);
    quick_sort(piv, end);
  }
}

// Test
int main() {
  std::vector<int> vec{ 19, 54, 53, 9, 3, 96, 40, 36, 96, 43, 45,
                        78, 57, 6, 12, 100, 36, 15, 27, 45, 80, 39,
                        10, 71, 100, 15, 41, 43, 20, 32, 61, 2, 55,
                        33, 85, 32, 11, 50, 25, 17, 75, 74, 39, 49,
                        50, 17, 18, 55, 63, 81, };
  quick_sort(vec.begin(), vec.end());
  std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
}


texts:
To stay in practice with my Python I've decided to write an explicit song checker. It checks each word in the song against an array of 14 explicit words contained in the text file. I've decided not to include the words that are to be checked against, for I am not sure about the rule of having explicit words in a program's code. Feedback on efficiency and structure is what I'm going for mostly. Style and other critiques are invited as well.

explicit_song_checker.py

explicit_words = [
    #explicit words not shown
]

def isClean(song_path):
    with open(song_path) as song:
        for line in song:
            words = line.split(" ")
            for word in words:
                for explicit_word in explicit_words:
                    if word == explicit_word:
                        return False

        return True

def main():
    song = raw_input("Enter path to song: \n")
    if isClean(song):
        print("CLEAN")
    else:
        print("EXPLICIT")

if __name__ == '__main__':
    main()


texts:
C++17 introduces std::invoke which calls the exposition-only INVOKE. Here is an implementation. This isn't a large amount of code, and it is to a large extent a transliteration of [func.require].

I took the liberty to add invoke_r which calls INVOKE<R> since I find it quite useful at times.

// C++17 std::invoke implementation

#ifndef INC_INVOKE_HPP_6AY08xPaOo
#define INC_INVOKE_HPP_6AY08xPaOo

#include <functional>
#include <type_traits>
#include <utility>

namespace my_std {

  // 23.14.4 Function template invoke [func.invoke]
  // Returns: INVOKE(std::forward<F>(f), std::forward<Args>(args)...)
  // (See 23.14.3 Requirements [func.require] Paragraph 1)
  template <typename F, typename... Args>
  std::invoke_result_t<F, Args...> invoke(F&& f, Args&&... args)
    noexcept(std::is_nothrow_invocable_v<F, Args...>);

  // Added functionality
  // Returns: INVOKE<R>(std::forward<F>(f), std::forward<Args>(args)...)
  // (See 23.14.3 Requirements [func.require] Paragraph 2)
  template <typename R, typename F, typename... Args>
  R invoke_r(F&& f, Args&&... args)
    noexcept(std::is_nothrow_invocable_r_v<R, F, Args...>);

  namespace detail {
    template <typename T>
    struct is_reference_wrapper :std::false_type {};
    template <typename T>
    struct is_reference_wrapper<std::reference_wrapper<T>>
      :std::true_type {};
    template <typename T>
    inline constexpr bool is_reference_wrapper_v =
      is_reference_wrapper<T>::value;

    template <typename F, typename C, typename T, typename... Args>
    decltype(auto) invoke_mfp(F C::*f, T&& t, Args&&... args)
    {
      // (1.1)
      if constexpr (std::is_base_of_v<C, std::decay_t<T>>)
        return (t.*f)(std::forward<Args>(args)...);
      // (1.2)
      else if constexpr (is_reference_wrapper_v<std::decay_t<T>>)
        return (t.get().*f)(std::forward<Args>(args)...);
      // (1.3)
      else
        return ((*t).*f)(std::forward<Args>(args)...);
    }

    template <typename M, typename C, typename T>
    decltype(auto) invoke_mop(M C::*f, T&& t)
    {
      // (1.4)
      if constexpr (std::is_base_of_v<C, std::decay_t<T>>)
        return t.*f;
      // (1.5)
      else if constexpr (is_reference_wrapper_v<std::decay_t<T>>)
        return t.get().*f;
      // (1.6)
      else
        return (*t).*f;
    }
  }

  template <typename F, typename... Args>
  std::invoke_result_t<F, Args...> invoke(F&& f, Args&&... args)
    noexcept(std::is_nothrow_invocable_v<F, Args...>)
  {
    // (1.1), (1.2), (1.3)
    if constexpr (std::is_member_function_pointer_v<std::remove_reference_t<F>>)
      return detail::invoke_mfp(f, std::forward<Args>(args)...);
    // (1.4), (1.5), (1.6)
    else if constexpr (sizeof...(Args) == 1 &&
        std::is_member_object_pointer_v<std::remove_reference_t<F>>)
      return detail::invoke_mop(f, std::forward<Args>(args)...);
    // (1.7)
    else
      return std::forward<F>(f)(std::forward<Args>(args)...);
  }

  template <typename R, typename F, typename... Args>
  R invoke_r(F&& f, Args&&... args)
    noexcept(std::is_nothrow_invocable_r_v<R, F, Args...>)
  {
    if constexpr (std::is_void_v<R>)
      return static_cast<void>(
        invoke(std::forward<F>(f), std::forward<Args>(args)...)
      );
    else
      return invoke(std::forward<F>(f), std::forward<Args>(args)...);
  }

}

#endif


texts: Simple prime number search with trial division

texts:
I made a primality tester with trial division. I'm trying to make it as fast as possible and have reached the point now where I'm out of ideas.

I use the generator approach and not lists because I found out that it's awfully slow to generate and checklists for big ranges.

Does someone have ideas on how to improve the is_prime_td() and seeker() functions? I left some comments in the code to explain why I did it like this.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
    Made for Python 2.7.x
    See __main__ block at the end of this
    script to change the range to be searched.
'''

from math import sqrt as math_sqrt

def is_prime_td(num):
    ''' Test if a number is a prime with Trial Division.

        num: The number to be tested.

        Returns True if num is a prime and False if not.
    '''
    # No prime if smaller than 2
    if num < 2:
        return False

    # 2 is the only even prime
    if num == 2:
        return True

    # No prime if if num can be divided by 2 w/o a remainder except 2 itself
    if num % 2 == 0:
        return False

    # Check if num is divisible by any odd number (except 1) up two
    # ceil'ed square root of num. I didn't want to call math.ceil()
    # so i added just "+ 1"
    i = 3
    while i <= int(math_sqrt(num) + 1):
        # No prime if if num can be divided by i w/o a remainder
        if num % i == 0:
            return False
        # Go test with the next odd divisor
        i += 2

    # It's a prime if not proven false until here
    return True

def seeker(num_start, num_end):
    ''' Search for primes from and including num_start to num_end.
        Uses 'is_prime_td()'.
    '''
    # The smallest prime number is two. So, always skip if num's < 2.
    # We could add this check also to the loop below where we Increment
    # accordingly, but this would just add a useless extra check each
    # iteration.
    if num_start >= 2:
        num = num_start
    else:
        num = 2

    try:
        while num <= num_end:
            if is_prime_td(num):
                yield num
            # Since there are no even primes except 2, we only want to
            # test odd numbers in the loop. Increment accordingly so we never
            # test even numbers and thus saving some time.
            if num % 2 == 0:
                num += 1
            else:
                num += 2
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    # Define the range to be searched here.
    num_start = 0
    num_end   = 100

    # Print only positive findings.
    for num in seeker(num_start, num_end):
        print num


texts:
I'm new to JavaScript and was hoping there was a cleaner way to write this. It's a block of code that looks at a group of documents in a collection (called Messages), and makes a call to the GitHub API for each one. If any of the API calls finds a certain string in the response, the function returns true via callback. If all of the API calls are exhausted without finding the string, the function returns false. The only way I could really think to do this was with the async module, which feels like a dirty/ugly solution. I was hoping someone here might be able to show me if/how this could be possible without the async module.

function isUserBlacklisted(user, topCallback){
  // Grab each alert we've sent to that user,
  // see if any of their comments are 'STOP'
  var isBlacklisted = false;
  Messages.find({ user: user }).toArray(function(err, messages){
    async.eachSeries(messages, function(message, callback){
      if (isBlacklisted){
        // eachSeries checks each one. This short-circuits the call so we don't
        // waste API calls if we find out the user is blacklisted halfway
        // through the list.
        callback();
      }
      else{
        var options = {
          user: message.user,
          repo: message.repo,
          number: message.number
        }
        github.issues.getComments(options, function(err,obj){
          console.log("Made a call to github for issue #" + message.number);
          if(isStopInComments(obj)){
            Messages.update(options, {$set:{isBlacklisted: true}});
            isBlacklisted = true;
          }
          callback();
        });
      }
    },
    function(err){ topCallback(isBlacklisted)});
  });
}


texts: Increasing readability of maximum path sum algorithm

texts:
I had written a solution for Project Euler problem 18 in Python 3. It is good in its efficiency but the problem is the readability and I think reuse.

The algorithm used is that at every iteration the maximum for all numbers in a row is calculated starting from the top and going down to find the largest path.

#This function is in "files" and used in the solution
def get_lines(path, file_name):
    with open(path + os.sep + file_name) as f:
        for line in f:
            yield line.rstrip('\n')

def prob_018():
    matrix = [[int(i) for i in line.split(' ')]
              for line in files.get_lines(RESOURCES, "018.txt")]
    for row_num in range(1, len(matrix)):
        pre = matrix[row_num - 1]
        cur = matrix[row_num]

        for el_num in range(len(cur)):
            total = cur[el_num]
            if el_num == 0:
                total += pre[el_num]
            elif el_num == len(cur) - 1:
                total += pre[el_num - 1]
            else:
                total += pre[el_num - 1] if pre[el_num - 1] > pre[el_num] \
                    else pre[el_num]

            matrix[row_num][el_num] = total
    return max(matrix[len(matrix) - 1])


texts:
I'd appreciate feedback on the code itself (style, performance optimization (both space and time)), and also on the algorithm (because I think this is typically implemented using a stack).

The assumption I'm making here is that there are only two constraints to having balanced parentheses; one is that there be the same number of opening and closing parentheses, the other one that there not be at any point more closing parentheses than opening ones.

With this in mind, I implement my balanced parentheses checker using a simple counter, which is incremented with each opening paren, and decremented with each closing paren.

The two "checks" within the function are that the counter never go negative (return False if it does at any point), and that at the end the counter be 0.

def paren_checker(string):
    counter = 0
    for c in string:
        if c == '(':
            counter += 1
        elif c == ')':
            counter -= 1
        if counter < 0:
            return False

    if counter == 0:
        return True
    return False


texts:
I kind of started making an API for my site in a Django class based view because it seemed logical and now it has gotten quite large and unruly and I am starting to wonder if I am doing this correctly.

class URLAPI(View):

    def get(self, request):

         if request.GET.get('param') == "foo":
             ...

         elif request.GET.get('param') == "bar":
             ...

         elif request.GET.get('param') == "foo" and request.GET.get('param2') == "arg":
             ...

I did the same thing with the post function creating conditionals for the data that is coming in. What do you think about this?


texts:
I have an NSArray called program and I need to scan it and then produce an NSSet of objects that fit the test isOperation:.  If none are found I should return nil.  I wrote the following code.  How would you make this better? (FYI - the fact the program is of type (id) comes directly from the assignment so I can't change that one thing)

Revised code:

+ (NSSet *) variablesUsedInProgram:(id)program {
    NSSet *programSet, *variablesUsed;
    if([program isKindOfClass:[NSArray class]]) {
        programSet = [NSSet setWithArray:program];
        variablesUsed = [programSet objectsPassingTest:^BOOL(id term, BOOL *stop) {
            return (! ([term isKindOfClass:[NSNumber class]] || [CalculatorBrain isOperation:term]));
        }];
    }
    return ([variablesUsed count] > 0) ? variablesUsed : nil;
}


texts: Group students by grade and having max age

texts:
Please help to improve my Java 7 code. I would like a best code logic to achieve this.

package main.java;

import java.util.ArrayList;
import java.util.List;

public class Student {
    private int id;
    private String grade;
    private int age;

    public Student(int id, String grade, int age){
        this.id = id;
        this.grade = grade;
        this.age = age;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getGrade() {
        return grade;
    }
    public void setGrade(String grade) {
        this.grade = grade;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    // group student by grade and having max of age
    public static List<Student> getStudentsWithMaxAgeOfEachGrade(List<Student> students){
        ArrayList<Student> result = new ArrayList<>();

        for(Student s : students){
            boolean found = false;
            for(Student rs : result){
                if(rs.getGrade().equals(s.getGrade()) && rs.getAge() < s.getAge()){
                    found = true;
                    result.remove(rs);
                    result.add(s);
                    break;
                }
            }
            if(found == false){
                result.add(s);
            }
        }

        return result;

    }

    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        Student s1 = new Student(1, "a", 32);
        Student s2 = new Student(2, "a", 34);
        Student s3 = new Student(3, "b", 23);

        students.add(s1);
        students.add(s2);
        students.add(s3);

        List<Student> result = getStudentsWithMaxAgeOfEachGrade(students);

        for(Student s : result){
            System.out.println(s.getGrade() + ":" + s.getAge());
        }
    }
}


texts:
I have the following basic code, which connects to a websocket server and receives some data:
import websocket, json, time

def process_message(ws, msg):
    message = json.loads(msg)
    print(message)

def on_error(ws, error):
    print('Error', e)

def on_close(ws):
    print('Closing')

def on_open(ws):
    def run(*args):
        Subs = []

        tradeStr=  """{"method": "SUBSCRIBE", "params":%s, "id": 1}"""%(json.dumps(Subs))
        ws.send(tradeStr)

    thread.start_new_thread(run, ())

def Connect():
    websocket.enableTrace(False)
    ws = websocket.WebSocketApp("wss://myurl", on_message = process_message, on_error = on_error, on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

Connect()

Now, I would like to create more connections to different servers and receive data concurrently in the same script. I tried the following:
def run(url):

    def process_message(ws, msg):
        message = json.loads(msg)
        print(message)

    def on_error(ws, error):
        print('Error', e)

    def on_close(ws):
        print('Closing')

    def on_open(ws):
        def run(*args):
            Subs = []

            tradeStr=  """{"method": "SUBSCRIBE", "params":%s, "id": 1}"""%(json.dumps(Subs))
            ws.send(tradeStr)

        thread.start_new_thread(run, ())

    def Connect():
        websocket.enableTrace(False)
        ws = websocket.WebSocketApp(url, on_message = process_message, on_error = on_error, on_close = on_close)
        ws.on_open = on_open
        ws.run_forever()

    Connect()

threading.Thread(target=run, kwargs={'url': 'url1'}).start()
threading.Thread(target=run, kwargs={'url': 'url2'}).start()
threading.Thread(target=run, kwargs={'url': 'url3'}).start()

Now, this code works, but I'm connecting to different URLs and I'm streaming data from all of them, but it seemed to me an "hacky" solution. I also don't know if what I'm doing could be bad practice or not. Each connection will send around 600/700 small JSON dictionaries, and I need to update every record to the db.
So my question is: is this implementation ok? Since it works with threads, can it create problems in the long run? Should I do another library such as Tornado?


texts: Writing out a tree of Tnodes with words as values in alphabetical order

texts:
I'm doing this:

  Consider:

struct Tnode {
    string word;
    int count;
    Tnode* left;
    Tnode* right;
};

  Write a function for entering new words into a tree of Tnodes. Write a
  function to write out a tree of Tnodes. Write a function to write out
  a tree of Tnodes with the words in alphabetical order. Modify Tnode so
  that it stores (only) a pointer to an arbitrarily long word stored as
  an array of characters on free store using new. Modify the functions
  to use the new definition of Tnode.

Source

Here is my code for the first part ("write a function to write out a tree of Tnodes with the words in alphabetical order"). Please review this.

Note: I know I only have to write a function, but I'm interested in creating a class. Now I'm not sure if this is good code.

struct Tnode {
    std::string word;
    int count; //I'm not sure what's this for. I assume it's for counting nodes.
    Tnode* left;
    Tnode* right;
};

class TreeNode {
    Tnode* node;

    Tnode* add(Tnode* n, const std::string& word) {
        if (n == nullptr) {
            n = new Tnode{word};
        } else if (word < n->word) {
            n->left = add(n->left, word);
        } else if (word > n->word) {
            n->right = add(n->right, word);
        } else {
            n->word = word;
        }
        //I have to always check for nullptr because I found out dereferencing a nullptr causes undefined behavior. And I have to asign n->count back to 1 because I notice it keeps adding.
        n->count = 1;
        if (n->left != nullptr) {
            n->count += n->left->count;
        }
        if (n->right != nullptr) {
            n->count += n->right->count;
        }
        return n;
    }

    void traverse(Tnode* t) { //for testing purposes
        if (t == nullptr) {
            return;
        }
        std::cout << t->word << '\n';
        traverse(t->left);
        traverse(t->right);
    }

    void destroy_all(Tnode* t) { //I can't think of a better way to destroy all Tnode objects
        if (t == nullptr) {
            return;
        }
        destroy_all(t->left);
        destroy_all(t->right);
        delete t;
    }

public:
    TreeNode() { node = nullptr; } //Is doing this fine? I don't know what default value I would use aside from nullptr because I didn't write a subscripting function to access the nodes
    TreeNode(const std::string& word) :node{new Tnode{word}} { }

    void add(const std::string& word) {
        node = add(node,word);
    }

    void traverse() { //for testing purposes
        traverse(node);
    }

    int get_count() {
        return node->count;
    }

    ~TreeNode() {
        destroy_all(node);
    }

};

How can I make this code better?


texts: "Intelligent" removal of carriage returns that preserves paragraph breaks, headings, etc

texts:
It's often useful to strip carriage returns out of a plain text document, for example when copying and pasting into a field that automatically wraps lines.  However, it's usually a good idea to leave some of the carriage returns in — most obviously at paragraph breaks, but also around bullet lists, section headings, etc.  So one needs an algorithm to decide which carriage returns to retain.

In my attempt to address this problem, I came up with the following:

shouldMerge::[Char]->[Char]->Bool
shouldMerge "" _ = False
shouldMerge _ "" = False
shouldMerge _ nextline | (not . isAlphaNum . head) nextline = False
shouldMerge line nextline | length (line ++ " " ++ (head . words) nextline) <
                              length nextline = False
shouldMerge _ _  = True

where shouldMerge is a function that attempts to guess whether a line should be merged with its successor.  The rules state, roughly, 1) never merge blank lines; 2) don't merge with a line beginning with a non-alphanumeric character; and 3) if placing the first word on the next line at the end of the current line would result in a line shorter than the next line, don't merge, as the current line was probably cut short intentionally (catches things like section headings.)  This set of rules "seems to work" :) much of the time.

My question is: how would you improve on this algorithm (perhaps by considering more than one line of text at a time?)


texts: Filtering JSONArray by Branding & Modelling

texts:
I have a small function that takes in a list of JSON objects that has the format below. The function below that wants to see if the brand and model of car passed to it can be matched against the List parameter called jsonfiltersList. The code I have works, but being somewhat new to coding and Java I wanted to know if I can optimize the code snippet to make it more compact somehow. Any thoughts?

{
  auto_filters : [
   {
      "brand" : "Toyota"
   },
   {
      "brand" : "Honda",
      "models" : [ "Civic", "Accord" ]
   }
  ]
}

And the code:

  public static boolean filterMatch(String brand, String model, ArrayList<JSONObject> jsonFiltersList) {
    boolean brandMatch = false;
    boolean modelMatch = false;
    if ((jsonFiltersList == null) || (jsonFiltersList.size() < 1)) return true;
    try {
        for (JSONObject j : jsonFiltersList) {
                if (j.get("brand").equals(brand)) {
                    brandMatch = true;
                }
                if (brandMatch) {
                    JSONArray jArr = null;
                    if (j.has("models")) {
                        jArr = (JSONArray) j.get("models");
                    }

                        ArrayList<String> modelsArr = new ArrayList<String>();
                        if (jArr != null) {
                            for (int i = 0; i < jArr.length(); i++)
                                modelsArr.add(jArr.get(i).toString());
                        if (modelsArr.contains(model)) {
                            modelMatch = true;
                                break;
                        }
                    } else {
                        modelMatch = true;
                        break;
                    }
            }
            brandMatch = false;
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
    return brandMatch && modelMatch;
}


texts:
I've been learning how to use Maven, and in order to understand it better, I want to try and write the smallest possible pom.xml file, while being as modern as possible, i.e. using java 1.8, utf-8, having the latest possible version of plugins and dependencies without breaking compatibility.

And so I came up with the following pom.xml and wanted to have other people's opinion on it.

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>hello.maven</groupId>
  <artifactId>hello-maven</artifactId>
  <version>0.1.0</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>[4,5)</version>
    </dependency>
  </dependencies>
</project>

Edit: Is it possible to have a less redundant version number for maven-compiler-plugin in a way that maven picks the one appropriate to maven's own version?