texts: Keras stacked LSTM model for multiclass classification

texts:
I am working on a multiple classification problem and after dabbling with multiple neural network architectures, I settled for a stacked LSTM structure as it yields the best accuracy for my use-case. Unfortunately the network takes a long time (almost 48 hours) to reach a good accuracy (~1000 epochs) even when I use GPU acceleration.

At this point, I would like some feedback about my code and my results, did I miss something? and hopefully some suggestions on how to further speed-up and improve the training process/code.

My code is the following:

# -*- coding: utf-8 -*-
import keras
import numpy as np
from time import time
from utils import dmanip, vis
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.utils import to_categorical
from keras.callbacks import TensorBoard
from sklearn.preprocessing import LabelEncoder
from tensorflow.python.client import device_lib
from sklearn.model_selection import train_test_split

###############################################################################
####################### Extract the data from .csv file #######################
###############################################################################
# get data
data, column_names = dmanip.get_data(file_path='../data_one_outcome.csv')

# split data
X = data.iloc[:, :-1]
y = data.iloc[:, -1:].astype('category')

###############################################################################
########################## init global config vars ############################
###############################################################################
# check if GPU is used
print(device_lib.list_local_devices())

# init
n_epochs = 1500
n_comps = X.shape[1]

###############################################################################
################################## Keras RNN ##################################
###############################################################################
# encode the classification labels
le = LabelEncoder()
yy = to_categorical(le.fit_transform(y))

# split the dataset
x_train, x_test, y_train, y_test = train_test_split(X, yy, test_size=0.35,
                                                    random_state=True,
                                                    shuffle=True)

# exapand dimensions
x_train = np.expand_dims(x_train, axis=2)
x_test = np.expand_dims(x_test, axis=2)

# define model
model = Sequential()
model.add(LSTM(units=n_comps, return_sequences=True,
               input_shape=(x_train.shape[1], 1),
               dropout=0.2, recurrent_dropout=0.2))
model.add(LSTM(64, return_sequences=True, dropout=0.2, recurrent_dropout=0.2))
model.add(LSTM(32, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(4 ,activation='softmax'))

# print model architecture summary
print(model.summary())

# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Create a TensorBoard instance with the path to the logs directory
tensorboard = TensorBoard(log_dir='./logs/rnn/{}'.format(time()))

# fit the model
history = model.fit(x_train, y_train, epochs=n_epochs, batch_size=100,
                    validation_data=(x_test, y_test), callbacks=[tensorboard])

# plot results
vis.plot_nn_stats(history=history, stat_type="accuracy", fname="RNN-accuracy")
vis.plot_nn_stats(history=history, stat_type="loss", fname="RNN-loss")

The resulting accuracy and loss functions are:

My data is essentially a big matrix (38607, 150), where 150 is the number of features and 38607 is the number of samples, with a target vector including 4 classes.


texts: Rails Controller vs Service Object for Application Logic

texts:
I have a #change_account action in the accounts controller that verifies if the user has access to the requested account prior to changing the current_account which happens via setting the session[:account]. I am trying to find the best way/place to check that the user has access to the account.

class AccountsController < ApplicationController

#removed rest of controller that doesn't apply

  def change_account
    if check_if_user_has_access(params['user_account']['id'])
      session[:account] = (params[:user_account][:id])
      flash[:notice]= 'Successfully Changed Account!'
    else
      flash[:alert]= 'No access to this Account!'
    end
    redirect_to root_path
  end

  private

    def check_if_user_has_access(requested_account)
      current_user.user_accounts.any? {|h| h[:account_id] == requested_account.to_i}
    end

end

I'm not sure if the logic should be kept in the controller or potentially placed in a service object. I have seen a few explanations that say that service objects are best used for complex actions which this doesn't seem to fit. The model doesn't seem like a good fit, whether in the user_accounts or accounts models.

Here is how I take the session variable and update the current_account:

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  private

  def current_account
    if current_user.user_accounts.where(active: true).count >= 1
      session[:account] = current_user.user_accounts.first.account_id if session[:account].nil?
      current_user.user_accounts.find_by_account_id(session[:account]).account
    else
      nil
    end
  end
  helper_method :current_account

end

Or I could call a service object such in my controller:

if VerifyAccessToAccount.call(params['user_account']['id'], current_user)

and then set up the service object:

class VerifyAccessToAccount
  def self.call(requested_account, user)
    user.user_accounts.any? {|h| h[:account_id] == requested_account.to_i}
  end
end


texts: Detecting if an element is found inside a container

texts:
I just wrote this template to detect if a given element is found inside a container:

template <typename Iterator> bool is_contained(Iterator begin, Iterator end, decltype(*begin) object)
{
    for (; begin != end; ++begin)
    {
        if (*begin == object)
        {
            return true;
        }
    }
    return false;
}

Which then would be called for examples as:

bool test = is_contained<decltype(container.begin())>(container.begin(), container.end(), anything);

This works fine, but I believe it is not so readable. I am also new to using decltype which makes me wonder if this would crash, and somehow I wont be calling the template correctly. Any feedback is highly appreciated.


texts: Postgres Schema Design for School Classes with Cancellations and Substitute Instructors

texts:
I have modelled a series of school classes as follows.
The part I am not sure about is having a separate table for cancellations and substitute instructors.

With this setup every time you looked up the class schedule for a given week you would have to search the class_exceptions table in order to
see if the class was cancelled or the instructor was substituted with another one.

Is there a better way of modelling this schema?

CREATE TABLE staff (
 email TEXT,
 created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL default (now() at time zone 'utc'),
 updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL default (now() at time zone 'utc'),
 PRIMARY KEY (email)
);

CREATE TABLE class_types (
  id bigserial,
  name text NOT NULL,
  instructor_email TEXT NOT NULL,
  FOREIGN KEY (instructor_email) REFERENCES staff (email),
  created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL default (now() at time zone 'utc'),
  updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL default (now() at time zone 'utc'),
  PRIMARY KEY (id)
);

CREATE TABLE classes (
  class_type_id BIGINT NOT NULL,
  recurs_weekly BOOLEAN,
  is_public BOOLEAN,
  start_timestamp timestamp without time zone NOT NULL,
  end_timestamp timestamp without time zone NOT NULL,
  created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'),
  updated_at timestamp without time zone NOT NULL default (now() at time zone 'utc'),
  FOREIGN KEY (class_type_id) REFERENCES class_types (id),
  PRIMARY KEY (class_type_id, start_timestamp)
);

CREATE TABLE class_exceptions (
  is_cancelled BOOLEAN,
  class_type_id BIGINT NOT NULL,
  start_timestamp timestamp without time zone NOT NULL,
  cover_instructor_email TEXT,
  FOREIGN KEY (class_type_id, start_timestamp) REFERENCES classes(class_type_id, start_timestamp),
  FOREIGN KEY (cover_instructor_email) REFERENCES staff (email),
  created_at timestamp without time zone NOT NULL default (now() at time zone 'utc'),
  updated_at timestamp without time zone NOT NULL default (now() at time zone 'utc')
);
```


texts: Loading tab-separated tweet data into an array

texts:
I'm working on this school project and was wondering if there was any way of storing this information better, faster, more professionally. I'm also restricted to only using an array; don't ask me we are not allowed to use ArrayLists yet.

My Code:

public void loadTweets(String fileName){

    try {
        File file = new File(fileName);
        Scanner s = new Scanner(file);

        while(s.hasNextLine()){
            numberOfTweets++;
            s.nextLine();
        }
        tweets = new String[numberOfTweets];
        s.close();

        s = new Scanner(file);
        int counter = 0;
        while(s.hasNextLine()){
            String[] elements = s.nextLine().split("\t");
            tweets[counter] = elements[2];
            counter++;
        }
        s.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

File Example:

Each field is separated by a tab and it goes, user > date posted > tweet.

USER_989b85bb 2010-03-04T15:34:46 @USER_6921e61d can I be...
USER_989b85bb 2010-03-04T15:34:47 superstar
USER_a75657c2 2010-03-03T00:02:54 @USER_13e8a102 They reached a
USER_a75657c2 2010-03-07T21:45:48 So SunChips made a bag...
USER_ee551c6c 2010-03-07T15:40:27 drthema: Do something today that
USER_6c78461b 2010-03-03T05:13:34 @USER_a3d59856 yes, i watched...
USER_92b2293c 2010-03-04T14:00:11 RT @USER_5aac9e88: Let no 1 push u
USER_75c62ed9 2010-03-07T03:35:38 @USER_cb237f7f Congrats on...


texts:
I am trying to solve the common "print a character after every item in a list, except the last one" problem.
I do not want to use list indexes. There are a couple of simple ways to do this in Python, but I am not sure how to apply them to my code.
Is there a more readable/couple liner way to do this?
def echo(*args):
    for data in args:
        if data is args[-1]:
            ender = ''
        else:
            ender = ' '

        if isinstance(data, six.string_types) \
                    or isinstance(data, six.integer_types):

            print_(data, end=ender)
        else:
            op = getattr(data, '__str__', None)
            if op:
                print_(data.__str__(), end=ender)
            else:
                LOG.error("echo called with an unsupported data type")
    print_('')

Note: Code works in both Python 2 and Python 3.


texts:
I'm new to Rust and trying to convert some Python to Rust.  I have the following in Python:

>>> v = ((0, 0), (1,1), (2, 2))
>>> strings = "\n".join(("%s: (%s, %s)" % (i, val[0], val[1]) for i,val in enumerate(v))))
>>> print(strings)
0: (0, 0)
1: (1, 1)
2: (2, 2)

I'm trying to find the best/smartest/most rustic way of doing this:

let v = vec![(0, 0), (1,1), (2, 2)];

let strings = v
    .iter()
    .enumerate()
    .map(|(i, &(a,b))| format!("{:?}: ({}, {})", i, a, b))
    .collect::<Vec<_>>()
    .join("\n");

println!("{}", strings);


texts: Getting all number from a String like this "1,2,5-9"

texts:
I need to get the numbers in a string like "1,2,5-9", the function should return a list with these values [1,2,5,6,7,8,9]

Here's what I did. Is this the best way or there's something simpler/better?

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function getNumbers(stringNumbers) {
  var nums = [];
  var list1 = stringNumbers.split(",");
  var length = list1.length;
  for(var i = 0; i < length; i++) {
    var number = list1[i];
    if(number.indexOf("-") != -1) {
      range = number.split("-");
      if(isNumber(range[0]) && isNumber(range[1])) {
        var min = parseInt(range[0]);
        var max = parseInt(range[1]);

        //edit to accept range like 9-5

        if(min > max){
          var min_ref = min;
          min = max;
          max = min_ref;
        }

        while(min <= max) {
          nums.push(min);
          min++
        };
      }
    } else {
      if(isNumber(number)) {
        nums.push(parseInt(number));
      }
    }
  }

  //edit to sort list at the end
  return nums.sort(function(a,b){return a - b});
};


texts: Alone in a Couple implementation in Java

texts:
In a party everyone is in couple except one. People who are in couple
  have same numbers. Find out the person who is not in couple.

  Input:

  The first line contains an integer \$T\$ denoting the total
  number of test cases. In each test cases, the first line contains an
  integer \$N\$ denoting the size of array. The second line contains \$N\$
  space-separated integers \$A_1, A_2, \ldots, A_N\$ denoting the elements of the
  array. (\$N\$ is always odd)

  Output:

  In each seperate line print number of the person not in
  couple.

  Constraints:

  \$ 1 \leq T \leq 30\$

  \$ 1 \leq N \leq 500\$

  \$ 1 \leq A[i] \leq 500\$

  \$ N \% 2 = 1\$

  Example:

  Input:

1

5

1 2 3 2 1

  Output:

3

My approach:

/*package whatever //do not write package name here */

import java.util.Scanner;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

class GFG {

    private static int getAloneNum (int[] arr) {
        List<Integer> alone = new ArrayList<>();

        for (Integer elem : arr) {
            if (!(alone.contains(elem))) {
                alone.add(elem);
            }
            else {
                alone.remove(alone.indexOf(elem));
            }
        }

        return alone.get(0);
    }

    public static void main (String[] args) {
        Scanner sc = new Scanner (System.in);
        int numTests = sc.nextInt();

        for (int i = 0; i < numTests; i++) {
            int size = sc.nextInt();
            int[] arr = new int[size];

            for (int j = 0; j < size; j++) {
                arr[j] = sc.nextInt();
            }

            System.out.println(getAloneNum(arr));
        }
    }
}

I have the following questions with regards to the above code:

How can I further improve my approach?
Is there a better way to solve this question?
Are there any grave code violations that I have committed?
Can space and time complexity be further improved?
Is my code very redundant?

Reference


texts: Calculate possible balances in piggy-bank by weight

texts:
I have solved Piggy-Bank problem on SPOJ using dynamic programming.

The question asks you to get the minimum value that is possible with given weight (\$w\$) and one or more coins (\$n\$) with given value, \$value[j]\$ and weight, \$weight[j]\$.

I am using following recurrence relation (zero indexed) to solve the problem:

$$dp[i] = \min\left \{ val[j]  + dp[i - weight[j]]\right \}  \forall j \in \left [ 0..n-1  \right ] \&  i - weight[j]  \geq 0$$

where \$dp[i]\$ is the minimum monetary value in a piggy bank with weight \$i\$.

\$dp[w]\$ gives the minimum values possible with given coins for given weight.

But it seems like the question is more quickly solved using a knapsack approach. Can anyone kindly tell why so and why is my approach inefficient?

This code is giving correct answer for all the sample cases:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <climits>
#define out(i) printf("%d",i)
#define out2(i,j) printf("%d %d",i,j)
#define nl printf("\n")
#define in(i) scanf("%d",&i)
#define in2(i,j) scanf("%d %d",&i,&j)
#define sn(i) scanf("%s",i)
#define inf 0x3f3f3f3f
using namespace std;
int val[500], weight[500], dp[10001],n;

int getAnswer(int w)
{
    if(dp[w] != inf) return dp[w];

    for(int i = 0; i < n; i++)
    {
        if(w - weight[i] >= 0)
        dp[w] = min(dp[w],val[i] + getAnswer(w - weight[i]));
    }

    return dp[w];
}
int main()
{
    // your code goes here
    int t;
    in(t);

    while(t--)
    {
        int e,f,w,ans;
        in2(e,f);
        w = f - e;
        for(int i = 0; i <= w; i++)
            dp[i] = inf;
        in(n);
        dp[0] = 0;
        for(int i = 0; i < n; i++)
            in2(val[i], weight[i]);

        ans = getAnswer(w);
        if(ans != inf)
            printf("The minimum amount of money in the piggy-bank is %d.\n",ans);
        else
            printf("This is impossible.\n");
    }
    return 0;
}


texts:
I am currently using a SimpleConnectionPool from psycopg2 to lease transactions to a PostgreSQL database and would like a review of my current implementation.
Code
from contextlib import contextmanager
from dataclasses import dataclass

from psycopg2.pool import SimpleConnectionPool

@dataclass
class PostgreSQLSimplePool:

    pool: SimpleConnectionPool

    @contextmanager
    def transact_session(self, commit: bool = False):
        conn = self.pool.getconn()
        try:
            yield conn
            if commit:
                conn.commit()
        except Exception:
            conn.rollback()
            raise
        finally:
            self.pool.putconn(conn)

Config
simple_pool = psycopg2.pool.SimpleConnectionPool(
        # testing purposes
        1, 20,
        user=POSTGRES_USER,
        password=POSTGRES_PASS,
        host=POSTGRES_HOST,
        port=POSTGRES_PORT,
        dbname=POSTGRES_DB_NAME,
        sslmode='require'
    )

repo = PostgreSQLSimplePool(pool=simple_pool)

Usage
with repo.transact_session() as connection:
    with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
        # do some stuff with the cursor...


texts: Recurse on complex Perl data structure, applying needed rules

texts:
Inspired by this answer and previous a commentator of code review use:

Given a complex Perl data structure, traverse/jigger/modify as follows:

If ORACLE_SID is 'key' in any hash then keep only ORACLE_SID section matching of $ENV{ORACLE_SID}
Eliminate any single level arrays, if after step 1, we're left with s_level arrays
Enhance values of simple content values based on requirement

What else or better way can be done to achieve the intended goal?

# $reduce_1level is set by the caller of jigger()
our $reduce_1level = 1;

sub jigger
{
    our $cds; local *cds = \shift;

    my $type = ref $cds or return;

    if ($type eq 'HASH') {
        foreach my $key (keys %$cds) {
            if (ref $cds->{$key}) {
                $cds->{$key} = reduce_by_oracle_sid ($cds->{$key});
                delete $cds->{$key} if not defined $cds->{$key};
                jigger ($cds->{$key}) if ref $cds->{$key};
            }
            else {
                $cds->{$key} = enhance_value ($cds->{$key});
            }
        }
    }
    elsif ($type eq 'ARRAY')
    {
        @$cds = grep { $_ = reduce_by_oracle_sid($_); defined } @$cds;

        foreach my $elem (@$cds) {
            if (ref $elem) {
                jigger ($elem);
            }
            else {
                $elem = enhance_value ($elem);
            }
        }
        $cds = $cds->[0] if @$cds == 1 and $reduce_1level;
    }
}

I'm really only after jigger(). I'm listing the helper subs for the the same price.

sub reduce_by_oracle_sid
{
    my $node = shift;

    return $node unless ref $node eq 'HASH' and exists $node->{ORACLE_SID};
    return unless $node->{ORACLE_SID} =~ /$ENV{ORACLE_SID}/;

    delete $node->{ORACLE_SID};

    # If there was was just 'value' then we return only value...
    return exists $node->{value} ? $node->{value} : $node;
}

sub enhance_value
{
    my $v = shift;

    # Take out double //'s
    $v =~ s{//}{}g;

    # trim whitesapce
    $v =~ s/^\w*(\W*)\w*$/$1/;

    # inject SQL commands ...
    #
    # ... n more ...

    return $v;
}


texts:
I hacked together this little query on the Data Explorer

The query finds questions where an answerer has posted two or more answers (which in itself is not necessarily a bad thing), it gives you the link to the question, information about which user posted the multiple answers, and how many answers by that user that was posted.

select question.Id as [Post Link],
   answer.owneruserid as [User Link],
   count(answer.id) as answers
from Posts question
join Posts answer on (answer.parentid = question.id)
where answer.OwnerUserId != 0
group by answer.OwnerUserId,
  question.id, answer.owneruserid
having count(answer.id) >= 2
order by answers desc

The last two group by clauses seem quite useless to me in a way, as they will always only be one unique version of them after grouping by answer.OwnerUserId. My experience from mySQL is that it simply picks a seemingly random value if not grouping by it, but in the SEDE I get this result:

  Column 'Posts.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Is there a way to remove these extra GROUP BY's? Or is adding these "the way it's supposed to be done"?
How is this SQL overall, regarding performance, naming, structure, conventions and everything?
Are there any alternative ways to rewrite this query?


texts: Showing activity indicator (loading image) while processing in background

texts:
I have a login screen on my iOS app. When I click login button I want to make sure that user should be displayed a loading image in center of the screen.

I found lots of different methods online but I am after a simplest method. I found a way to display activity Indicator with minimum code:

First of all, import your UI Kit:

import UIKit;

Then you have to initiate activity indicator:

let activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView();

Then just copy paste this functions below which are self explainatory and call them whereever you need them:

func startLoading(){
    activityIndicator.center = self.view.center;
    activityIndicator.hidesWhenStopped = true;
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray;
    view.addSubview(activityIndicator);

    activityIndicator.startAnimating();
    UIApplication.shared.beginIgnoringInteractionEvents();

}

func stopLoading(){

    activityIndicator.stopAnimating();
    UIApplication.shared.endIgnoringInteractionEvents();

}

Although, I am still not convinced that it's the best way to do it. Could it still be reduced?

Here is how my screen looks with the code:


texts:
I got this class in C# which works fine. But I was wondering if there was a more elegant solution to what I am trying to do. It looks rather clumsy and inefficient for a Logging functionality.

The code should be fairly self-explanatory.

public static class Log
{
    public static string EngineName { get; set; }
    private static List<String> logdata = new List<string>();

    static void LogMessage(string msg)
    {
        Console.WriteLine("[{0}][LOG]: {1}", EngineName, msg);
        StringBuilder sb = new StringBuilder();
        sb.Append("[").Append(EngineName).Append("][LOG]: ").Append(msg);
        logdata.Add(sb.ToString());
    }

    public static void EngineMessage(string msg)
    {
        Console.WriteLine("[{0}][ENGINE]: {1}", EngineName, msg);
        StringBuilder sb = new StringBuilder();
        sb.Append("[").Append(EngineName).Append("][Engine]: ").Append(msg);
        logdata.Add(sb.ToString());
    }

    public static void ExceptionMessage(Exception e)
    {
        Console.WriteLine("[{0}][EXCEPTION]: {1}", EngineName, e.Message);
        StringBuilder sb = new StringBuilder();
        sb.Append("[").Append(EngineName).Append("][EXCEPTION]: ").Append(e.Message);
        logdata.Add(sb.ToString());
    }

    public static void CustomMessage(string title, string msg)
    {
        title.ToUpper();
        Console.WriteLine("[{0}][{1}]: {2}", EngineName, title, msg);
        StringBuilder sb = new StringBuilder();
        sb.Append("[").Append(EngineName).Append("][").Append(title).Append("]: ").Append(msg);
        logdata.Add(sb.ToString());
    }

    public static void ClearLogData()
    {
        logdata.Clear();
    }

    public static void PrintLog()
    {
        Console.WriteLine("===== LOG DATA =====\n");
        Console.WriteLine(new DateTime().ToString()+"\n");
        foreach(string s in logdata)
        {
            Console.WriteLine(s + "\n");
        }
    }

    public static void SaveLog(string path)
    {
        throw new NotSupportedException();
    }
}

EDIT

Based on feedback from the accepted answer here is my revised code:

public static class Log
{
    public static string EngineName { get; set; }
    private static List<String> logdata = new List<string>();

    public static void LogMessage(string msg, ELogflag flag, string title = "")
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("[" + EngineName + "]");
        switch (flag)
        {
            case ELogflag.LOG:
                sb.Append("[LOG]");
                break;
            case ELogflag.ENGINE:
                sb.Append("[ENGINE]");
                break;
            case ELogflag.CRITICAL:
                sb.Append("[CRITICAL]");
                break;
            case ELogflag.CUSTOM:
                title = title.ToUpper();
                sb.Append("[" + title + "]");
                break;
            default:
                sb.Append("[UNKNOWN]");
                break;
        }
        sb.Append(msg);
        Console.WriteLine(sb.ToString());
        logdata.Add(sb.ToString());
    }

    public static void ClearLogData()
    {
        logdata.Clear();
    }

    public static void PrintLog()
    {
        Console.WriteLine("===== LOG DATA =====\n");
        Console.WriteLine(new DateTime().ToString() + "\n");
        foreach (string s in logdata)
        {
            Console.WriteLine(s + "\n");
        }
    }

    public static void SaveLog(string path)
    {
        throw new NotSupportedException();
    }
}

And my new Enum:

enum ELogflag
{
    LOG,
    ENGINE,
    CRITICAL,
    CUSTOM
}


texts: Checking for the nth prime number between 1 and 1,000,000, must compute 1,000,000th in under 3 minutes

texts:
My mission is as the title states.
Here is the original code, before optimizing slightly (but still not enough).
Revised code is in my answer, and it still needs work.

Also, I want to write this without having to import any libraries. Time is only there for me to test, of course, time.

Here's the original code, before optimizing:

print("Enter a number n, 1-1,000,000 to find the nth prime number.")
goal=int(input("Number: "))

if goal>1000000:         #closes the program if the number doesn't fit the parameters
    exit()
if goal==1:              #takes care of the first prime, which is 2
    print("2")
if goal==2:              #and then the second, which is 3
    print("3")

chk=5         #the next odd number after 3, which is already taken care of above
primes=2    #the number of primes recorded, after counting 2 and 3.
x=0         #the last recorded prime number
while primes<goal:
    if  chk<9:     #if the number is odd and below 9....
        primes+=1  #it's prime
        x=chk      #record that number
        chk+=2     #and check the next odd number

    else:              #if it's =>9......
        odds=3         #check to see if it's divisible by the smallest odd number
        sqrtk=chk**.5  #all the way up to the square root of k

        while odds<sqrtk or odds==sqrtk:   #if at any time...
            if chk%odds==0:                #...k is divisible by this odd number....
                chk+=2                     #then raise the value of k to the next odd number
                break
            else:            #if not...
                odds+=2         #check to see if it's divisible by the next odd number.

        if odds>sqrtk:             #if k isn't divisible by any r
                primes+=1        #we have a prime number
                x=chk         #record that number
                chk+=2        #check the next odd number to see if it's prime
                odds=3         #and reset r back to 3 to use it next time

if primes==goal:                  #once we've reached the desired amount of prime numbers
    print("The",goal,"prime number is:",x)      #print that prime number


texts: Calculating binary tree height as asked in the interview

texts:
Input:

  The first line contains the number of vertices \$n\$. The second line
  contains \$n\$ integer numbers from the range \$\left[−1, n − 1\right]\$ representing the \$0\$-based index of the parent for each vertex.
  An index of \$-1\$ designates that vertex as a root without a parent.

  It is guaranteed that there is exactly one root.It is guaranteed that
  the input represents a tree.

Output:

  Output the height of the tree.

Code:

console.clear();

function heightSlow(parent) {
  var maxHeight = 0;
  // for each vertex check its height
  // and keep track of maximum height so far.
  for (var i = 0; i < parent.length; i++) {
    var height = 0;
    // O(n*n) // n is the number of vertex
    for (var vertex = i; vertex != -1; vertex = parent[vertex]) {
      height++;
    }
    // vertex should be -1 here
    maxHeight = Math.max(maxHeight, height);
  }
  return maxHeight;
}

function heightFast(parent) {
  var depth = [];
  var N = parent.length;

  function recur(parent, i, depth) {
    if (depth[i] !== 0) return;

    if (parent[i] === -1) {
      depth[i] = 1;
      return;
    }

    // Find the depth of parent
    if (depth[parent[i]] === 0) recur(parent, parent[i], depth);

    // We have depth of the parent now
    depth[i] = depth[parent[i]] + 1;
  }

  var vertex;
  // fill with default depth, although I could use null
  // but stick with general notion of invalid value mathematically
  // Theta(N)
  for (vertex = 0; vertex < N; vertex++)
    depth[vertex] = 0;

  // find depth of each node
  // recursion would be called for each node but
  // it would return immidiately hence we can ignore that call?
  // if yes then recur would be called N times exactly. Hence,
  // Theta(N)
  for (vertex = 0; vertex < N; vertex++)
    recur(parent, vertex, depth);

  // get the max depth, which is height
  // Theta(N)
  var height = 0;
  for (vertex = 0; vertex < N; vertex++) {
    height = Math.max(height, depth[vertex]);
  }
  return height;
}

// O(N^2)
console.log(heightSlow([4, -1, 4, 1, 1])); // 3
console.log(heightSlow([-1, 0, 4, 0, 3])); // 4

// Theta(N)
console.log(heightFast([4, -1, 4, 1, 1])); // 3

console.log(heightFast([-1, 0, 4, 0, 3])); // 4

Disclaimer:

I am more interested in fail proof approaches like writing correct loops, making quick tests without any testing frameworks to check the code correctness and finally reasoning about the run time complexity.

Having said that I would like to adhere to the problem specification in past I saw people arguing about the best practices which doesn't match with the problem statement and hence becomes immaterial.


texts:
This function draws a Christmas tree (with an angel on top) with text (AKA, ASCII art).

#define ARRSZ(x) (sizeof(x)/sizeof(x[0]))

void drawChristmasTree (int height) {
    const char * const angel[] = { " ._.", "(\\o/)", ")/|\\(", "/_*_\\", };
    const char stars[] = "***************************************";
    int i;
    int offset = 20 - height;

    if ((height < ARRSZ(angel)) || (height > ARRSZ(stars)/2)) {
        printf("The height has to be at least %zu and no more than %zu.\n",
               ARRSZ(angel), ARRSZ(stars)/2);
        return;
    }

    for (i = 0; i < height; ++i) {
        printf("%*s%s\n",
               height - (i < ARRSZ(angel) ? ARRSZ(angel) : i + 1), "",
               (i < ARRSZ(angel)) ? angel[i] : &stars[ARRSZ(stars) - 2*i]);
    }
    printf("%*s%c\n", height - 2, "", '*');
    printf("%*s%c\n", height - 2, "", '*');
}

For size 10, this code produces:

       ._.
      (\o/)
      )/|\(
      /_*_\
     *******
    *********
   ***********
  *************
 ***************
*****************
        *
        *


texts: Beginnings of Go web server with mongoDb

texts:
I am just learning how to use go and I'm a bit confused on how my code is supposed to be organised package wise.

Am I right to have the user package? I'm planning to redirect my /user/ routes to a handler in this package but I'm unsure if this is the go way to do things

My directory structure is

/go_rest_api
  /user
    -user.go
    -user_provider.go
  -app.go
  -main.go

main.go

package main

func main() {
  a := App{}
  a.Initialize()
  a.Run()
}

app.go

package main

import (
  "fmt"
  "log"
  "github.com/gorilla/mux"
  "gopkg.in/mgo.v2"
  "go_rest_api/user"
)

type App struct {
  Router *mux.Router
  Mongo *MongoConnection
}

type MongoConnection struct {
  Session *mgo.Session
}

func(a* App) GetMongoSession() *mgo.Session {
  return a.Mongo.Session.Copy()
}

func(a *App) Initialize() {
  session, err := mgo.Dial("127.0.0.1:27017")
  if err != nil {
    panic(err)
  }

  session.SetMode(mgo.Monotonic, true)

  a.Mongo = &MongoConnection{session}
  a.Router = mux.NewRouter()
}

func(a *App) Run() {
  defer a.Mongo.Session.Close()
  testMongo(a.GetMongoSession())
}

func testMongo(session *mgo.Session) {
  userProvider := user.Provider(session)
  err := userProvider.InsertUser(user.User{"test"})
  if err != nil {
    log.Fatal(err)
  }

  user := user.User{}
  err, user = userProvider.GetUser("test")
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println("username:", user.Username)
}

user/user.go

package user

type User struct {
  Username string
}

user/user_provider.go

package user

import (
  "gopkg.in/mgo.v2/bson"
  "gopkg.in/mgo.v2"
)

type provider struct {
  Collection *mgo.Collection
}

func Provider(mongoSession *mgo.Session) provider {
  p := provider{}
  p.Collection = mongoSession.DB("test").C("user")
  return p
}

func(p *provider) InsertUser(user User) error {
  return p.Collection.Insert(&user)
}

func (p *provider) GetUser(username string) (error, User) {
  result := User{}
  err := p.Collection.Find(bson.M{"username": username}).One(&result)
  return err, result
}


texts: Writing a hiveserver2 query result to CSV with Python using pyhs2

texts:
I am trying to create a small application that will help me extract data from Hadoop via hiveserver2 by simply writing it to CSV. At the moment, it takes about 40 seconds to pull 10,000 records from a hs2 table consisting of 500k rows with 203 columns. I would like to think that there is a faster, more efficient way of doing this than the way I am doing it now. Ideally I would like to eventually be able to pull and write 1 million rows per minute. It doesn't necessarily have to be written to CSV. It probably isn't feasible with my current skill level, but I like to set goals for myself.

import pyhs2
import time
import csv

csv_out = open('data.csv', 'wb')
mywriter = csv.writer(csv_out)

def generic_user():
    gu = 'xxxxxx'
    return gu

def password():
    pw = 'xxxxxxx'
    return pw

with pyhs2.connect(host='xxxxxxxx',
               port=10000,
               authMechanism='PLAIN',
               user=generic_user(),
               password=password(),
               database='xxxxxxxxxx') as conn:

    with conn.cursor() as cur:

        q = raw_input('Enter query: ').replace('csc', 'CSC')
        print q

        #timer start
        start_time = time.time()

        #Execute query
        cur.execute(q)

        col = []

        for key in cur.getSchema():
            col.append(key['columnName'])

        header = []
        header.append(col)

        for rows in zip(header):
            mywriter.writerows(rows)

        records = cur.fetch()
        # print records

        for rows in zip(records):
            mywriter.writerows(rows)

pull_time = time.time() - start_time
print pull_time


texts: How to refactor old browser detection JavaScript?

texts:
I'm looking to make this code more sensible, while making sure it still works as intended. The alert message at the bottom should trigger when the user is running a version of Microsoft Internet Explorer less than IE 9. But there's a lot of incomprehensible shorthand in the main code, that I need to be refactored, and simplified into long-hand, so it will make more sense to me.

Here is the code:

/*
----------------------------------------------------------
A short snippet for detecting versions of IE in JavaScript
without resorting to user-agent sniffing
----------------------------------------------------------

If you're not in IE (or IE version is less than 5) then:

ie === undefined

If you're in IE (>=5) then you can determine which version:

ie === 7; // IE7

Thus, to detect IE:

if (ie) {}

And to detect the version:

ie === 6 // IE6
ie > 7 // IE8, IE9 ...
ie < 9 // Anything less than IE9

----------------------------------------------------------
*/

/* IE Detection Snippet. DO NOT EDIT [BELOW] THIS LINE. [BEGIN] */

var ie = (function()
{
    var undef;
    var v = 3;
    var div = document.createElement('div');
    var all = div.getElementsByTagName('i');

    while
        (
            div.innerHTML = '',
            all[0]
        );

    return v > 4 ? v : undef;

}());

/* IE Detection Snippet. DO NOT EDIT [ABOVE] THIS LINE. [END] */

if (ie)
{
    if (ie < 9)
        {
            alert( "You are running an older version of Microsoft Internet Explorer:\r\nVersion " + ie.toFixed(1) + ", which is NOT supported.\r\n\r\nPlease upgrade your web browser to its latest version, or if you're still using Windows XP, to Google Chrome, Mozilla Firefox, Apple Safari, or Opera, to view this website, properly.\r\n\r\nClick \"OK\" to read instructions on how to download and install a newer, more compatible, web browser.");
            window.parent.location = "/libs/old_browser_detect/current/upgrade_browser.php";
        };
};

Original Source: https://j11y.io/javascript/detect-ie-in-js-using-conditional-comments/

Also, is it possible to change the variable names to ones, both, more relevant, and unique, to prevent coding conflicts?

@Przemek, Will these changes work?

/*

----------------------------------------------------------
A short snippet for detecting versions of IE in JavaScript
without resorting to user-agent sniffing
----------------------------------------------------------

If you're not in IE (or IE version is less than 5) then:

ieVersion === undefined

If you're in IE (>=5) then you can determine which version:

ieVersion === 7; // IE7

Thus, to detect IE:

if (ieVersion) {}

And to detect the version:

ieVersion === 6 // IE6
ieVersion > 7 // IE8, IE9 ...
ieVersion < 9 // Anything less than IE9

----------------------------------------------------------

*/

/* IE Detection Snippet. DO NOT EDIT [BELOW] THIS LINE. [BEGIN] */

var ieVersion = (function()
    {
        var elmt = document.createElement('span');
        var version = 3;

        do
            {
                elmt.innerHTML = '';
            }
        while (elmt.getElementsByTagName('span')[0] && version <= 9);

        return version > 4 ? version : false;
    }());

/* IE Detection Snippet. DO NOT EDIT [ABOVE] THIS LINE. [END] */

if ((ieVersion) && (ieVersion < 9))
    {
        alert( "You are running an older version of Microsoft Internet Explorer:\r\nVersion " + ieVersion.toFixed(1) + ", which is NOT supported.\r\n\r\nPlease upgrade your web browser to its latest version, or if you're still using Windows XP, to Google Chrome, Mozilla Firefox, Apple Safari, or Opera, to view this website, properly.\r\n\r\nClick \"OK\" to read instructions on how to download and install a newer, more compatible, web browser.");
        window.parent.location = "/libs/old_browser_detect/current/upgrade_browser.php";
    };


texts: Save on typing while using UTF8 encoding

texts:
Typing in something like Encoding.UTF8.GetString(...) and Encoding.UTF8.GetBytes(...) everywhere in your code could be eliminated by a helper UTF8 type:

public class UTF8_Should
{
    [Test]
    public void Convert()
    {
        var text = "Hello World";
        byte[] array = (UTF8)text;
        string copy = (UTF8)array;
        Assert.AreEqual(text, copy);
    }
}

Where:

struct UTF8
{
    public static implicit operator UTF8(byte[] array) => new UTF8(Encoding.UTF8.GetString(array));
    public static implicit operator string(UTF8 utf8) => utf8.Text;
    public static implicit operator UTF8(string text) => new UTF8(text);
    public static implicit operator byte[](UTF8 utf8) => Encoding.UTF8.GetBytes(utf8.Text);
    public UTF8(string text) => Text = text;
    string Text { get; }
}


texts:
I am just trying to solve one of the puzzle which is explained below:

  The sentence if man was meant to stay on the ground god would have
  given us roots after removing spaces is 54 characters long, so it is
  written in the form of a grid with 7 rows and 8 columns.

  The encoded message is obtained by displaying the characters in a
  column, inserting a space, and then displaying the next column and
  inserting a space, and so on. For example, the encoded message for the
  above rectangle is:

imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau

For more conditions please refer here:

from math import ceil, sqrt
from string import whitespace

def encrypt(string):
    enc_string = string.translate(None,whitespace)
    length = len(enc_string)

    #calculate the no of colums for the grid
    columns = int(ceil(sqrt(length)))

    #form a list of strings each of length equal to columns
    split_string = [enc_string[i:i+columns] for i in range(0, len(enc_string), columns)]

    #encrypted list of strings
    new_string = []
    for string in split_string:

        for i in range(len(string)):
            try:
                #replace the element in the list with the new string
                new_string[i] = new_string[i].replace(new_string[i], new_string[i] + string[i])
            except IndexError:
                #exception indicates that the string needs be added with new Index
                new_string.append(string[i])

    return " ".join(new_string)

if __name__ == "__main__":
    s = raw_input().strip()
    result = encrypt(s)
    print(result)

Please help me if multiple for loops can be avoided and any other improvements.


texts: Free a binary tree without using recursion or allocating memory

texts:
As the title says, the objective is to free a binary tree without using the stack or allocating memory.
This was required for a kernel module where resources were limited.

Result has a complexity of \$ O \left( n \right) \$.

template<typename T>
struct Node
{
    T     data;
    Node* left;
    Node* right;
};

// Utility function to find the bottom left
// Of the tree (because that has a NULL left
// pointer that we can use to store information.
Node* findBottomLeft(Node* t)
{
    while(t->left != NULL)
    {
        t = t->left;
    }
    return t;
}

void freeTree(Node* t)
{
    if (t == NULL)
    {   return;
    }

    // Points at the bottom left node.
    // Any right nodes are added to the bottom left as we go down
    // this progressively flattens the tree into a list as we go.
    Node* bottomLeft    = findBottomLeft(t);

    while(t != NULL)
    {
        // Technically we don't need the if (it works fine without)
        // But it makes the code easier to reason about with it here.
        if (t->right != NULL)
        {
            bottomLeft->left = t->right;
            bottomLeft = findBottomLeft(bottomLeft);
        }
        // Now just free the curent node
        Node*   old = t;
        t = t->left;
        delete old;
    }
}


texts:
I've been toying around with Unity 5 lately, and in an effort to start making an actual game, I've built a ball controller similar to the one found in the Unity tutorial Roll-a-ball.

In essence, the player controls a small ball that rolls around a "level". The ball can accelerate at a rate in the range \$1\rightarrow10\$ depending on what I (the designer) choose. The controller also manually calculates drag to create a "low-gravity" feel, but still have a sense of speed. Drag is calculated using the following formula:

$$\text{drag}=\frac{\text{acceleration vector magnitude}}{\text{some dividend}}$$

I'm wondering about the following things:

Is it appropriate for the controller to do things such as drag calculation, or should this be separated into a separate area?
I'm currently using a dictionary, looping through it, checking for keypresses, and then applying force if a key is pressed. Is this a good way to handle it? Will this ever slow anything down?
Are there any performance improvements that can be made?
Anything else?

BallController.cs

using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// This class contains various methods and attributes
/// used to control the "vehicle".
/// </summary>
[System.Serializable]
[RequireComponent(typeof(Rigidbody))]
public class MoveVehicle : MonoBehaviour
{
    /// <summary>
    /// The force of an acceleration. This will be applied
    /// to the ball when it's accelerating.
    /// </summary>
    [Range(1, 10)]
    public float accelerationForce;

    /// <summary>
    /// This determines the drag being exerted on the ball
    /// as it moves. Drag is determined through the formula
    /// drag = accelerationForce / dragDividend
    /// </summary>
    [Range(0, 1000)]
    public float dragDividend;

    private Rigidbody rigidBody;
    private Dictionary<KeyCode, Vector3> keyMappings = new Dictionary<KeyCode, Vector3>() {
        {KeyCode.Space, new Vector3(0, 5.5f, 0)},

        {KeyCode.W, new Vector3(0, 0, 1)},
        {KeyCode.A, new Vector3(-1, 0, 0)},
        {KeyCode.S, new Vector3(0, 0, -1)},
        {KeyCode.D, new Vector3(1, 0, 0)},

        {KeyCode.UpArrow, new Vector3(0, 0, 1)},
        {KeyCode.LeftArrow, new Vector3(-1, 0, 0)},
        {KeyCode.DownArrow, new Vector3(0, 0, -1)},
        {KeyCode.RightArrow, new Vector3(1, 0, 0)}
    };

    /// <summary>
    /// Check if the ball is near to, or touching the
    /// ground. This is used to determine whether the
    /// vehicle can move or not.
    /// </summary>
    public bool IsGrounded()
    { return Physics.Raycast(this.transform.position, -Vector3.up, 0.65f); }

    /// <summary>
    /// Initialize things like our Rigidbody component,
    /// or other things that can only be initialized in
    /// Start.
    /// </summary>
    public void Start()
    { this.rigidBody = GetComponent<Rigidbody>(); }

    /// <summary>
    /// Apply thrust to the ball. The faster the vehicle
    /// gets, the more drag we apply to the rigidbody. This will
    /// create the effect of reaching a "maximum" speed.
    /// </summary>
    public void FixedUpdate()
    {
        foreach(KeyValuePair<KeyCode, Vector3> keyMapping in this.keyMappings)
        {
            if(Input.GetKey(keyMapping.Key) && this.IsGrounded())
            {
                Vector3 objectForce = keyMapping.Value * this.accelerationForce;
                this.rigidBody.drag = objectForce.sqrMagnitude / this.dragDividend;
                this.rigidBody.AddForce(objectForce);
            }
        }
    }
}


texts:
I'm moving outside my comfort zone and trying to make a random number distribution program while also making sure it is still somewhat uniform. I was trying to generate this in debug mode without optimization on. Also, I am using Visual Studio 2015.

RandomDistribution.h file

#pragma once
#include <vector>
#include <random>
#include <iostream>

static float randy(float low, float high) {
    static  std::random_device rd;
    static  std::mt19937 random(rd());
      std::uniform_real_distribution<float> ran(low, high);
    return ran(random);
}

typedef std::vector<float> Vfloat;
class RandomDistribution
{
public:

    RandomDistribution();
    RandomDistribution(float percent, float contents, int container);
    ~RandomDistribution();
    void setvariables(float percent, float contents, int container);
    Vfloat RunDistribution();
private:
    float divider;
    float _percent;
     int jar_limit;
    float _contents;
    float _maxdistribution;
    Vfloat Jar;
    bool is0;
};

RandomDistribution.cpp

#include "RandomDistribution.h"

RandomDistribution::RandomDistribution() {

}
RandomDistribution::RandomDistribution(float percent, float contents, int containers):_contents(contents),jar_limit(containers)
{
    Jar.resize(containers);
    if (percent < 0)
        _percent = 0;

    else {
        _percent = percent;
    }
    divider = jar_limit * percent;
    is0 = false;
}

RandomDistribution::~RandomDistribution()
{
}
void RandomDistribution::setvariables(float percent, float contents, int container) {
    if (jar_limit != container)
        Jar.resize(container);

    _contents = contents;
    jar_limit = container;
    is0 = false;

    if (percent < 0)
        _percent = 0;

    else {
        _percent = percent;
    }
    divider = jar_limit * percent;
}

Vfloat RandomDistribution::RunDistribution() {

    for (int i = 0; i < jar_limit; i++) {

        if (!is0) {
            if (i + 1 >= jar_limit || _contents < 2) {
                Jar[i] = _contents;
                _contents -= Jar[i];
                is0 = true;
            }

            if (!_percent <= 0) {//making sure it does not get the hole container at once
                _maxdistribution = (_contents / (divider)) * (i + 1);
            }
            else {
                _maxdistribution = _contents;
            }

            Jar[i] = randy(0, _maxdistribution);

            if (Jar[i] < 1) {
                Jar[i] = 0;
                continue;
            }

            _contents -= Jar[i];
        }
        else {
            Jar[0];
        }
        //mixing Jar so it is randomly spaced out instead all at the top
        int swapper = randy(0, i);
        float hold = Jar[i];
        Jar[i] = Jar[swapper];
        Jar[swapper] = hold;

    }

    return Jar;
}

Source code

int main(){
    RandomDistribution distribution[100];
    for (int i = 0; i < 100; i++) {
         distribution[i] = {RandomDistribution(1.0f, 5000.0f, 2000) };
    }

    Vfloat k;
    k.resize(200);

    for (int i = 0; i < 10; i++) {
        auto t3 = chrono::steady_clock::now();

        for (int b = 0; b < 100; b++) {

            k = distribution[b].RunDistribution();
            distribution[b].setvariables(1.0f, 5000.0f, 2000);

        }

        auto t4 = chrono::steady_clock::now();
        auto time_span = chrono::duration_cast<chrono::duration<double>>(t4 - t3);
        cout << time_span.count() << " seconds\n";

    }
}

What prints out is usually between 1 to 2 seconds for each cycle. I want to bring it down to a tenth of a second if possible because this is gonna be only one step of the process to completion and I want to run it a lot more than 100 times. What can I do to speed this up? Any trick or something I'm just missing here?

Here is a sample of the timestamps:

4.71113 seconds
1.35444 seconds
1.45008 seconds
1.74961 seconds
2.59192 seconds
2.76171 seconds
1.90149 seconds
2.2822 seconds
2.36768 seconds
2.61969 seconds


texts: Two similar API calls PHP optimisation and structure

texts:
I have the following script that I'm part way through and I'm not happy with it at all, it feels messy:

<?php
class Process {
    public $search = 'http://api.themoviedb.org/2.1/Person.search/en/json/xxx/';
    public $info = 'http://api.themoviedb.org/2.1/Person.getInfo/en/json/xxx/';

    public function returnId($name){
        return $this->returnJson($this->search . $name);
    }

    public function returnMovies($id){
        return $this->returnJson($this->info . $id);
    }

    public function returnJson($url){
        $raw = file_get_contents($url);
        $json = json_decode($raw, true);
        return $json;
    }
}

$p = new Process();
$first = urlencode($_POST['first']);
$second = urlencode($_POST['second']);

$id = $p->returnId($first);
$movies = $p->returnMovies($id[0]['id']);

Now the idea is that two parameters get sent to this script and each of them run through the first API call ($search) and then the ID field is extracted from that results and passed into the $info URL call. The two arrays that come back from returnMovies will be compared with each other and any matches will be popped into another array.

Now, has anyone got any way for this to be better in structure and cleanliness?

It's not finished yet but its a simple script that I'd like to tidy up before I go on.


texts: Trying to catch and throw exceptions if error message window is found

texts:
I'm writing a test to validate that while clicking through a series of menu items, an error message pops up, and if it does, then I should throw an exception.

Now I wonder: Is it appropriate to use nested try-catch block here?

public static void main(String[] args) throws Exception{
        File file = new File("C:/Program Files/Selenium/IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
        driver.get("http://ap-apbase-d002/Ecx.Web/Account/Login/?ReturnUrl=%2fEcx.Web%2f");
        WebElement element;
        element = driver.findElement(By.id("username"));
        element.sendKeys("Jh");
        element = driver.findElement(By.id("password"));
        element.sendKeys("1");
        element.submit();
        WebElement homeFrame = (new WebDriverWait(driver, 30)).until(ExpectedConditions.presenceOfElementLocated(By.id("pnCenter_IFrame")));
        driver.switchTo().frame("pnCenter_IFrame");
        WebElement myHomeLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Projects")));
        driver.findElement(By.linkText("My Projects")).click();
        driver.switchTo().defaultContent();
        driver.switchTo().frame("pnCenter_IFrame");
        driver.switchTo().defaultContent();
        //parent try block
        try{
        WebElement UniversalInboxLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("Universal Inbox")));
        driver.findElement(By.linkText("Universal Inbox")).click();
        WebElement MyProjectsLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Projects")));
        driver.findElement(By.linkText("My Projects")).click();
        WebElement AllProjectsLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("All Projects")));
        driver.findElement(By.linkText("All Projects")).click();
        WebElement MyTeamLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Team")));
        driver.findElement(By.linkText("My Team")).click();
        WebElement WOTTaxFormLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("WOT Tax Form")));
        driver.findElement(By.linkText("WOT Tax Form")).click();
        WebElement MyDraftsLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Drafts")));
        driver.findElement(By.linkText("My Drafts")).click();
        WebElement MyArchivesLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Archives")));
        driver.findElement(By.linkText("My Archives")).click();
        WebElement RetrieveLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("Retrieve")));
        driver.findElement(By.linkText("Retrieve")).click();
            //Child try block
            try{
        List <WebElement> AppbaseErrorWindow = driver.findElements(By.cssSelector(".x-window-header"));
        for (WebElement item : AppbaseErrorWindow) {
            if (item.getText().contains("Request Failure")) {
                //Do Nothing
            }
        }
            }catch (Exception e){
        System.out.println("Exception is found here: " + e);

            }
            System.exit(0);
            driver.quit();
    }
        catch(Exception e){
        System.out.println("All is OK.");
        }
        System.out.println("Normal behavior");
 }


texts: Design class to handle calls for support functions

texts:
For the following problem statement, I design following classes.  Can you please tell me if this is the correct implementation or if there can be a better one?

Let's say there is call center with three levels of employees: fresher, technical lead (TL), product manager (PM). There can be multiple employees, but only one TL or PM. An incoming telephone call must be allocated to a fresher who is free. If a fresher can’t handle the call, he or she must escalate the call to technical lead. If the TL is not free or not able to handle it, then the call should be escalated to PM. Design the classes and data structures for this problem.

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

namespace TestNS
{
    public class Employee
    {
        public string Name { get; set; }
        public bool IsFree { get; set; }
        public Employee Manager { get; set; }
    }

    public class Call
    {
        public Employee AllocatedTo { get; set; }

        public bool HandleCall()
        {
            AllocatedTo.IsFree = false;
            bool isHandled = false;

            // Write custom logic to handle the call & set the value of isHandled

            if (isHandled)
            {
                AllocatedTo.IsFree = true;
            }

            return isHandled;
        }

        public void Escalate()
        {
            AllocatedTo = AllocatedTo.Manager;
            HandleCall();
        }
    }

    public class CallManager
    {
        List<Employee> fresherList = new List<Employee>();
        public void CreateEmployeeList()
        {
            Employee PM = new Employee() { Name = "PM1", IsFree = true };

            Employee TL = new Employee() { Name = "TL1", IsFree = true };
            TL.Manager = PM;

            fresherList = new List<Employee>();
            fresherList.Add(new Employee() { Name = "F1", Manager = TL, IsFree = true });
            fresherList.Add(new Employee() { Name = "F2", Manager = TL, IsFree = true });
            fresherList.Add(new Employee() { Name = "F3", Manager = TL, IsFree = true });
            fresherList.Add(new Employee() { Name = "F4", Manager = TL, IsFree = true });
            fresherList.Add(new Employee() { Name = "F5", Manager = TL, IsFree = true });
            fresherList.Add(new Employee() { Name = "F6", Manager = TL, IsFree = true });
        }

        private Employee FindFreeEmployee()
        {
            Employee e = null;
            foreach (Employee emp in fresherList)
            {
                if (emp.IsFree)
                {
                    e = emp;
                    break;
                }
            }

            return e;
        }

        public void ReceiveAndAllocateCall(Call call1)
        {
            call1.AllocatedTo = FindFreeEmployee();
            call1.HandleCall();
        }
    }
}


texts:
I just want to train before interview. I googled concurrency interview questions and one of the question was write concurrent ready stack.

public interface ConcurrentStack<T> {
    void put(T t) throws InterruptedException;

    T poll() throws InterruptedException;
}

class BlockingConcurrentStack<T> implements ConcurrentStack<T> {

    private List<T> list = new ArrayList<>();
    private int maxSize;

    public BlockingConcurrentStack(int maxSize) {
        this.maxSize = maxSize;
    }

    @Override
    public synchronized void put(T t) throws InterruptedException {
        while (maxSize <= list.size()) {
            wait();
        }
        list.add(t);
        notify();

    }

    @Override
    public synchronized T poll() throws InterruptedException {
        while (list.size() == 0) {
            wait();
        }
        T t = list.get(list.size() - 1);
        list.remove(t);
        notify();
        return t;
    }
}


texts:
I am working on a project that has a Model-View-Presenter structure and use the delegation design pattern that has the following structure/implementation. I would love feedback.

The ViewController will set up the MVP triad:

class BookViewController: UIViewController {

    private var bookModel: BookModel!
    private var bookView: BookView!
    private var bookPresenter: bookPresenter!

    override func loadView() {
        super.loadView()

        bookModel = BookModel()
        bookView = BookView()
        bookPresenter = BookPresenter(model: bookModel, view: bookView)

        view.addSubview(bookView)
    }
}

The Presenter will set itself up as the observer to both the View and Model:

class BookPresenter: BookModelObserver, BookViewObserver {

    let bookModel: BookModel
    var bookView: BookView

    init(model: BookModel, view: BookView) {
        self.bookModel = bookModel
        self.bookView = bookView

        self.bookModel.observer = self
        self.bookView.observer = self

        let book = self.model.currentBook
        self.bookView.setData(book)
    }
}

The BookView will have a protocol defined:

protocol BookViewObserver: class {

    func evt_bookView(bookView: BookView, didSelectItem: Book)
}

A user action on the View layer will emit an event to the Presenter:

class BookView: UIView, UITableViewDataSource, UITableViewDelegate {

    weak var observer: BookViewObserver?

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        observer?.evt_bookView(self, didSelectItem: books[indexPath.row])
    }
}

The Presenter will implement the BookViewObserver protocol, and handle that emitted event:

class BookPresenter: BookModelObserver, BookViewObserver {

    func evt_bookView(bookView: BookView, didSelectItem book: Book) {
        model.updateCurrentBook(book)
    }
}

The BookModel will have a protocol defined:

protocol BookModelObserver: class {

    func evt_bookModel(bookModel: BookModel, didUpdateCurrentBook currentBook:     Book)
}

The Model will update itself, and emit an event back up to the Presenter:

class BookModel {

    private(set) var currentBook: Book!
    weak var observer: BookModelObserver?

    func updateCurrentBook(book: Book) {
        currentBook = book
        observer?.evt_bookModel(self, didUpdateCurrentBook: currentBook
    }
}

The Presenter will implement the BookModelObserver protocol, and handle that emitted event. In some cases, it has to update the View again, and thus will call a function on the BookView.

class BookPresenter: BookModelObserver, BookViewObserver {

    func evt_bookModel(bookModel: BookModel, didUpdateCurrentBook book: Book) {
        var index: Int?
        for (key, value) in DataStore.defaultInstance.books.enumerate() {
            if value.id == book.id {
                index = key
                break
            }
        }

        if let _ = index {
            bookView.updateBook(book, atIndex: index!)
        }
    }
}

Finally the View will update itself, bringing everything full circle:

class BookView: UIView, UITableViewDataSource, UITableViewDelegate {

    func updateData(book: Book, atIndex index: Int) {
        books[index] = book
        tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: 0)], withRowAnimation: UITableViewRowAnimation.None)
    }
}


texts: Stream file to OpenCSV with the least amount of I/O possible

texts:
I have the following code snippet where I'm picking up a file from an FTP server and then streaming it to OpenCSV. I have very little experience with streams, so I'd like to have someone review this code for efficiency. I use this on the cloud, so I/O and memory efficiency is important. Generally my CSV files consist of a few hundred thousand records each.

public InputStream getData(ImportTask importTask) throws IOException {
    //FTP Connection code

    try {
                //enter passive mode
                ftp.enterLocalPassiveMode();

                if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
                    System.out.println("Setting binary file type failed.");
                }

                ByteArrayOutputStream output = new ByteArrayOutputStream();

                String file = importTask.isZipfile() ? importTask.getZipFilename() : importTask.getFilename();

                ftp.retrieveFile(file, output);
                InputStream inputStream = new ByteArrayInputStream(output.toByteArray());

                if (importTask.isZipfile()) {
                    inputStream = importUtils.getZipData(new ZipInputStream(inputStream), importTask.getFilename());
                }

                ftp.logout();
                ftp.disconnect();

                return inputStream;
            } catch (FileNotFoundException ex) {
                throw new Exception(ex);
            }
}

OpenCSV to POJO

InputStream is;

try {
        is = dataSource.getData(importTask);
    } catch (IOException ex) {
        logger.error(ex.getMessage());
    {
}

final HeaderColumnNameTranslateMappingStrategy<MyObject> strategy = new HeaderColumnNameTranslateMappingStrategy<>();
        strategy.setType(MyObject.class);
        strategy.setColumnMapping(columnMappings);

final CsvToBean csv = new CsvToBean();
List list = csv.parse(strategy, new InputStreamReader(is));

for (Object object : list) {
    MyObject myObject = (MyObject) object;
    //Do something
}


texts:
I'm a newb at C++ and I was wondering whether I used pointers correctly and whether it was appropriate to use them in this context.

Any other tips/information would be appreciated.

#include <iostream>

using namespace std;

int euclideanAlgorithm(int *a, int *b);

int main() {
    int x, y, a, b;
    cin >> a;
    cin >> b;
    x = a;
    y = b;
    cout << x / euclideanAlgorithm(&a, &b) * y << endl;
    return 0;
}

int euclideanAlgorithm(int *a, int *b) {
    if (*b == 0) {
        return *a;
    }
    while (*a >= *b) {
        *a -= *b;
    }
    euclideanAlgorithm(b, a);
}


texts: Convert FLAC files to MP3 in parallel on Ubuntu

texts:
I created a script to convert my FLAC music collection to MP3.  I did this so that I can play the MP3 files in my car, as the integrated head unit only supports MP3 and WMA files.  The script works on my Ubuntu system.  Are there any opportunities to improve its efficiency?

I chose to use ffmpeg to transcode and GNU parallel to utilize all CPU cores/threads.

Iteration 1

 #!/bin/bash

 ## Converts flac to mp3

 if ! dpkg-query -W -f='${Status}' ffmpeg | grep "ok installed"; then
 apt-get update  && apt-get install ffmpeg; fi

 if ! dpkg-query -W -f='${Status}' parallel | grep "ok installed"; then
 apt-get update  && apt-get install parallel; fi

 parallel -i -j$(nproc) \
         ffmpeg -n -i {} -acodec libmp3lame -ab 320k -map_metadata 0 -id3v2_version 3 {.}.mp3 ::: */*/*/*.flac

Iteration 2

 #!/bin/sh

 ## Converts flac to mp3

 die() { echo "$@" >&2; exit 1; }

 ### Check the preconditions

 if ! dpkg-query -W -f='${Status}' ffmpeg | grep "ok installed"; then
     apt-get update  && apt-get install ffmpeg libmp3lame parallel;
 fi

 ####

 flac_to_mp3() {
     ffmpeg -n -i "$1" -acodec libmp3lame -ab 320k \
        -map_metadata 0 -id3v2_version 3 "$2"
 }
 export -f flac_to_mp3

 parallel flac_to_mp3 {} '{.}.mp3' ::: */*/*/*.flac


texts: Sampling from a distribution with given probabilities

texts:
I have found myself needing to sample numbers from a given distribution and was wondering what the most efficient method of doing so is.

I have a set of N elements 0 to N-1 and an array mapping those elements to the probability of their selection, pp.

My approach is to create a new array, cdf, and accumulate the entries of pp so that the first element of cdf is pp[0] and the last element is the sum of all the elements in pp, namely 1. Then, since this array is necessarily sorted, I choose a random number between 0 and 1 and perform a binary search in the array.

This is my code so far (in Java):

public class Sample {
    /**
     * Selects q elements chosen at random according to the probabilities
     * given in pp.
     *
     * @param pp  The probabilites corresponding to each index
     *            (item 0 has probability pp[0] and so forth)
     * @param q   The number of elements to sample
     * @return    An array containing the elements sampled
     */
    public static int [] sample(double [] pp, int q) {
        double [] cdf = pp.clone();
        for (int i = 1; i < cdf.length; i++)
            cdf[i] += cdf[i - 1];

        int [] values = new int[q];
        for (int i = 0; i < q; i++) {
            // binarySearch returns a negative number at the insertion point
            // if the exact number isn't found. We don't expect to hit an
            // exact match with Math.random(), so the Math.abs() call
            // can't be too expensive.
            values[i] = Math.abs(Arrays.binarySearch(cdf, Math.random()));
        }

        return values;
    }
}

I'm mostly unsure about the behavior of the Arrays.binarySearch function. Am I at risk of missing the first/last element, or going out of bounds? The running time of this algorithm is \$O(N)\$ for the cdf construction, plus \$O(q \log N)\$ for the actual sampling. The second operation dominates (\$q\$ could be greater than \$N\$), so this is an \$O(q \log N)\$ algorithm, which is slower than sorting! Is there any way to do this more quickly?


texts: Finding top most N common character grams in English

texts:
I suspect there might be a much more efficient way to accomplish this task.  I would appreciate a more clever hint for performance increase.  I am also looking for feedback on style and simplification where appropriate.

I assume that the set of what counts as English words is a given and that I don't need to do something clever like an actual dictionary or use Google n-grams.  That being said, the results could differ depending on the domain and also what is accepted as a word.

The original problem set M=3 (3-gram characters), which makes the results not so interesting (ans=[(u'the', 191279), (u'and', 92292), (u'ing', 57323), (u'her', 44627), (u'hat', 38609)]).

#gets the top N most common substrings of M characters in English words
from nltk import FreqDist
from nltk.corpus import gutenberg

def get_words():
    for fileid in gutenberg.fileids():
        for word in gutenberg.words(fileid):
            yield word

def ngrams(N, which_list, strict=False):
    list_size = len(which_list)
    stop = list_size
    if strict:
        stop -= (N - 1)
    for i in xrange(0, stop):
        element = which_list[i]
        ngram = [element]
        j = 1
        index = j + i
        while j < N and index < list_size:
            ngram.append(which_list[index])
            j += 1
            index += 1
        yield ''.join(ngram)

def m_most_common_ngram_chars(M=5, N=3):
    n_grams = []
    for word in get_words():
        for ngram in ngrams(N, word, strict=True):
            n_grams.append(ngram)
    f = FreqDist(n_grams)
    return f.most_common(M)

l = m_most_common_ngram_chars(M=5, N=3)
print l


texts:
How can I make the solution to this https://www.codewars.com/kata/5506b230a11c0aeab3000c1f/train/fsharp more "functional"?
module Evaporator
    let evaporator (content: double) (evapPerDay: double) (threshold: double): int =
        let minUsefulAmount = content * (threshold / 100.)
        let evapAsPercentage = evapPerDay / 100.
        let rec solve (content: double) (dayCount: int) =
            let amountLost = content * evapAsPercentage
            let newContent = content - amountLost
            let stillUseful = newContent > minUsefulAmount
            let result =
                match stillUseful with
                | true -> solve newContent dayCount + 1
                | false -> dayCount
            result
        solve content 1


texts:
This code basically check whether a person is in the grid of another person.

The algorithm should check all possible 2-permutations of persons whether a person is in another person grid. In the first version. It was two for loops. The outer one iterates all persons while the inner one iterates all persons for the selected person in the outer for loop.

I changed the code by building and iterating on permutations of persons and using numpy array (original data is pytorch Variable). It got improved 10 times but I'm still looking for further improvements.

mnp = len(pedlist_seq)
#print(pedlist_seq)
#print(lookup_seq)

width, height = dimensions[0], dimensions[1]
#print("width: ",width,"height: ",height)

frame_mask = np.zeros((mnp, mnp, grid_size**2))
frame_np =  frame.data.numpy()

#print("frame: ", frame)

width_bound, height_bound = (neighborhood_size/(width*1.0)), (neighborhood_size/(height*1.0))
#width_bound, height_bound = (neighborhood_size/(width*1.0))*2, (neighborhood_size/(height*1.0))*2
#print("weight_bound: ", width_bound, "height_bound: ", height_bound)

list_indices = list(range(0, mnp))
for real_frame_index, other_real_frame_index in itertools.permutations(list_indices, 2):
    current_x, current_y = frame_np[real_frame_index, 0], frame_np[real_frame_index, 1]

    width_low, width_high = current_x - width_bound/2, current_x + width_bound/2
    height_low, height_high = current_y - height_bound/2, current_y + height_bound/2

    other_x, other_y = frame_np[other_real_frame_index, 0], frame_np[other_real_frame_index, 1]

    #if (other_x >= width_high).all() or (other_x < width_low).all() or (other_y >= height_high).all() or (other_y < height_low).all():
    if (other_x >= width_high) or (other_x < width_low) or (other_y >= height_high) or (other_y < height_low):
            # Ped not in surrounding, so binary mask should be zero
            #print("not surrounding")
            continue
    # If in surrounding, calculate the grid cell
    cell_x = int(np.floor(((other_x - width_low)/width_bound) * grid_size))
    cell_y = int(np.floor(((other_y - height_low)/height_bound) * grid_size))
    #print("cell_x: ", cell_x, "cell_y: ", cell_y)

    if cell_x >= grid_size or cell_x < 0 or cell_y >= grid_size or cell_y < 0:
            continue

    # Other ped is in the corresponding grid cell of current ped
    frame_mask[real_frame_index, other_real_frame_index, cell_x + cell_y*grid_size] = 1
    #print("frame mask shape %s"%str(frame_mask.shape))


texts: Jest / Enzyme test for a React component to calculate average data speed

texts:
I'm trying to master testing.  I struggle, conceptually, as to what should be tested.

Is this Jest / Enzyme test sufficient for this react component?
Is is too much?
Are there better practices that I should be implementing?

Here is my component.

import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import {AverageText} from './index';

const Row = styled.div`
  display: flex;
  justify-content: center;
`;

const AvgSpeed = ({tests}) => {
  const downloadSpeeds = extractSpeeds(tests, 'download');
  const uploadSpeeds = extractSpeeds(tests, 'upload');
  const downloadAverage = getAverage(downloadSpeeds);
  const uploadAverage = getAverage(uploadSpeeds);
  return (
    <Row>
      <AverageText label="download" value={downloadAverage} />
      <AverageText label="upload" value={uploadAverage} />
    </Row>
  );
};

AvgSpeed.propTypes = {
  tests: PropTypes.object,
};

//returns average from array of numbers fixed at 1
function getAverage(array) {
  return (
    array.reduce((a, b) => {
      return a + b;
    }) / array.length
  ).toFixed(1);
}

//extracts float value from object property string value
function extractSpeeds(obj, property) {
  return Object.keys(obj).map(item => parseFloat(obj[item][property]));
}

//export to test
export {getAverage, extractSpeeds};

export default AvgSpeed;

My test

import React from 'react';
import AvgSpeed, {getAverage, extractSpeeds} from './AvgSpeed.js';
import {shallow, mount} from 'enzyme';
import renderer from 'react-test-renderer';

describe('AvgSpeed Component', () => {
  const inputObj = {
    1: {
      download: '22 mbs',
      upload: '9 mbs',
    },
    2: {
      download: '24 mbs',
      upload: '18 mbs',
    },
  };

  it('matches the snapshot', () => {
    const tree = renderer.create(<AvgSpeed tests={inputObj} />).toJSON();
    expect(tree).toMatchSnapshot();
  });

  describe('getAverage function', () => {
    it('returns the average from an array (fixed at 1)', () => {
      let input = [1, 2, 3];
      let output = '2.0';

      expect(getAverage(input)).toBe(output);

      //2,3,4
      input = input.map(n => (n += 1));
      output = '3.0';
      expect(getAverage(input)).toBe(output);

      //4,5,6
      input = input.map(n => (n += 2));
      output = '5.0';
    });
  });

  describe('extractSpeeds function', () => {
    it('return an array of float numbers', () => {
      const downloadIn = 'download';
      const uploadIn = 'upload';

      const downloadOut = [22, 24];
      const uploadOut = [9, 18];

      expect(extractSpeeds(inputObj, downloadIn)).toEqual(downloadOut);
      expect(extractSpeeds(inputObj, uploadIn)).toEqual(uploadOut);
    });
  });
});


texts:
I'm getting my feet wet in embedded development, and like all those before me, my first task was to make an LED blink. I went a bit further than that and made a "runner" that lights up each led from 0 to 7 in sequence. The sequence begins as soon as the board is powered on. You can see it in action here.

I'm using an ATxMegaA1 chip with the avr-gcc compiler and tool chain. The document I consulted most was the Using IO pins and external interrupts doc.

I decided that creating an "Led" concept was a nice abstraction to wrap around the port they're attached to on the board. I also introduced a macro to change the delay when I'm debugging, because the simulator is orders of magnitudes slower than the real chip.

It's my first ever c, so please be brutal.

led.h

#include <avr/io.h>

#define LEDPORT PORTE_OUT
#define LEDPORT_DIR PORTE_DIR

void init(void);
void toggleLights(int ledPosition);

led.c

#include "led.h"

void init(void)
{
    LEDPORT_DIR = 0b11111111; //Configure LED port for output
    LEDPORT = 0b11111111; //LEDs are active low, this makes sure they're off on start up
}

//Turns light at provided port pin on and all others off.
void toggleLights(int ledPosition)
{
    LEDPORT = ~(1 << ledPosition);
}

Blink.c

#include <stdint.h>
#include "led.h"

#ifdef DEBUG
    #define DELAYITERATIONS 0
#else
    #define DELAYITERATIONS 10000
#endif

void delay(volatile uint32_t d)
{
    while (d-- != 0)     // loops while non-0 and decrements
    ;
}

int main(void)
{
    init();

    while(1)
    {
        for (int i = 0; i < 8; i++)
        {
            toggleLights(i);
            delay(DELAYITERATIONS);
        }
    }
}


texts: Clean way to handle file not found exception

texts:
BACKGROUND
I have a function where i cache file from aws s3. I provide it the path on s3 and also the local path of where i want the cached file to live. Sometimes if the file does not exist on s3 it will return a 404 error. That makes sense but i want to handle it gracefully. What i want to do is if the file i am trying to cache does not exist I want my get_overview_stream function to return None.
CODE
def get_overview_stream(*, environment, proxy_key):
    s3_overview_file_path = f"s3://{FOO_TRACK_BUCKET}/{environment}"
    overview_file = f"{proxy_key}.csv"
    local_path = cache_directory(environment)
    try:
        cache.cache_file(s3_overview_file_path, local_path, overview_file)
        overview_file_cache = local_path / f"{proxy_key}.csv"
        return overview_file_cache.open("r")
    except botocore.exceptions.ClientError as e:
        if e.response["Error"]["Code"] == "404":
            return None
        else:
            raise

Attempted Solution Issue
I am very new to python so i am not sure if this the best way to handle this exception and if there is a more cleaner way. If so i would love to hear feedback.


texts:
I have a specific method that I like because it lets me decide whether or not I want to use the default. If I want anything different I enter in :option => value otherwise I get the default. Here's a concrete example that works, but is a little ugly.

How can I accomplish the same thing in a more elegant manner?

def connect_to_oracle opts = {}
  host_name = opts[:host_name]
  host_name ||= 'a_default_host_name'
  db_name = opts[:db_name]
  db_name ||= 'a_default_db_name'
  userid = opts[:userid]
  userid ||= 'a_default_userid'
  password = opts[:password]
  password ||= 'a_default_password'
  url = "jdbc:oracle:thin:#{userid}/#{password}@#{host_name}:1521:#{db_name}"
  $db = Sequel.connect(url)
end


texts: Algorithm that checks if a tree is full and complete

texts:
I am trying to write a method that will return true if a binary tree is full and complete (each node has 2 children or none and all the leaves of the tree are at the same depth).

My idea is to use recursion. I will check for any node if its left son has number of children that is equal to its right son's number of children. If so, I will return true, otherwise false.

The algorithm will look like this:

public class Utils {

    public boolean isFullCompleteTree(Tree<Integer> t) {
            TreeInfo rootInfo = isFullCompleteTree(t.getRoot());
            return rootInfo.valid;
        }
    public TreeInfo isFullCompleteTree(Node<Integer> node) {
        boolean valid = true;

        if (node == null) {
            return new TreeInfo(true, 0);
        }

        TreeInfo rightInfo = isFullCompleteTree(node.goRight());
        TreeInfo leftInfo = isFullCompleteTree(node.goLeft());

        if ((!rightInfo.valid) || (!leftInfo.valid)) {
            valid = false;
        }
        if (rightInfo.numChildern != leftInfo.numChildern) {
            valid = false;
        }
        return new TreeInfo(valid, rightInfo.numChildern + leftInfo.numChildern
                + 1);
    }
    }

    class TreeInfo {
       public boolean valid;
       public int numChildern;

       public TreeInfo(boolean valid, int numChildern) {
           this.valid = valid;
           this.numChildern = numChildern;
    }

}

I didn't include the tree implementation but it is pretty straightforward.

The idea of the algorithm is to check if in every node the number of right son's children is equal to the left son's children. If a tree is not full and complete, then in some node this rule will not apply.

Do you think that my algorithm is correct or am I missing something?


texts: NodeFlow - How to connect Properties in a clean way?

texts:
I have implemented a project called NodeFlow. As you can see in the picture you can create multiple nodes and connect their properties with other (Function)Nodes to modify the properties and let the data flow through the nodes to create a result (in this case it is the addition and multiplication to the result 4). I implemented NodeFlow in similarity to the Node Editor in the 3D Software Blender.

You can see that NodeFlow is somewhat working, but I am very unhappy about my architecture. So I decided to implement the application in a better way and it would be great if you could tell me what you think. However, I am only providing interfaces because I am right now creating the architecture of my application and I want to discuss this architecture with you.

What do you need to know about the nodes?

public interface INode
{
    string Name { get; set; }

    NodeTypes Type { get; set; }

    bool HasInputProperties { get; }

    bool HasOutputProperties { get; }

    ObservableCollection<IValueAcceptorProperty> ValueAcceptorProperty { get; set; }

    ObservableCollection<IValueProviderProperty> ValueProviderProperty { get; set; }
}

More Explanations

The node is the thing you see in the picture as "Constant Value: 1", "Add Function" and "Multiply Function". Nodes contain a Name, Input and Output Properties. I called these properties in my code ValueProviderProperty and ValueAcceptorProperty because calling it Input and OutputProperty was very confusing while coding and because value providing properties (or output properties) can only be connected with value accepting properties (or input properties) you have a data flow in one direction. That means that the ValueAccpetorProperties are taking (or accepting) the value of the connected ValueProviderProperty.

Properties

public interface IProperty
{
    string Name { get; set; }

    PropertyDataTypes AllowedDataTypes { get; set; }

    IConnector Connector { get; set; }

    INode ParentNode { get; set; }

    bool HasValue { get; }
}

public interface IValueProviderProperty : IProperty
{
    // This is like Variant
    IPropertyValue Value { get; set; }
}

public interface IValueAcceptorProperty : IProperty
{
    // This is like Variant
    IPropertyValue Value { get; }
}

Property Code:

This implements the ValueAcceptorProperty to show how I want to use this interface. As you can see the ValueAcceptorProperty is just forwarding the Value of its ValueProviderProperty.

public class ValueAcceptorProperty : IValueAcceptorProperty
{
    public string Name { get; set; }

    public IPropertyValue Value
        => this.Connector.IsConnected
            ? this.Connector.Connection.ValueProviderProperty.Value
            : null;

    public PropertyDataTypes AllowedDataTypes { get; set; }
    public IConnector Connector { get; set; }
    public INode ParentNode { get; set; }
    public bool HasValue => this.Value != null;
}

Every property has a Connector (the visual of it is the circle next to each property) and the Connectors are connected via a Connection.

Connector and Connection: (These are in separated files)

public interface IConnection
{
    IConnector ValueProviderConnector { get; set; }

    IConnector ValueAcceptorConnector { get; set; }

    IValueProviderProperty ValueProviderProperty { get; set; }

    IValueAcceptorProperty ValueAcceptorProperty { get; set; }
}

public interface IConnector
{
    bool IsConnected { get; set; }

    IConnection Connection { get; set; }
}

Most of the missing definitions for PropertyDataTypes and so on are enums or other data structures which are in my opinion not that important to discuss the general idea of NodeFlow.

So my main Questions is: Do you think that there is a better way to model the Nodes, Properties, Connections and Connectors?

If you need further information I will update this question to provide whatever you need.


texts:
I have the following method which extracts a decimal value from a string.

using System;
using System.Linq;
using System.Text;

namespace Foo.Bar.Common.Converters
{
    public static class DecimalConverter
    {
        public static Decimal ExtractDecimalFromString(string str)
        {
            var sb = new StringBuilder();
            foreach (var c in str.Where(c => c == '.' || Char.IsDigit(c)))
            {
                sb.Append(c);
            }
            return Convert.ToDecimal(sb.ToString());
        }
    }
}

Is this the most effecticve way to do it?


texts: Extracting the classpath entries from a jar file's manifest

texts:
Sometimes I need to search for dependencies in the classpath entries in the manifest of jar files. A jar manifest with classpaths looks like this:

Manifest-Version: 1.0
Created-By: 1.6.0_29 (Sun Microsystems Inc.)
Class-Path: spring-aop-3.2.0.jar spring-aspects-3.2.0.jar spring-beans
 -3.2.0.jar spring-context-3.2.0.jar spring-context-support-3.2.0.jar
 spring-core-3.2.0.jar spring-expression-3.2.0.jar spring-instrument-3
 .2.0.jar spring-instrument-tomcat-3.2.0.jar spring-jdbc-3.2.0.jar spr
 ing-jms-3.2.0.jar spring-orm-3.2.0.jar spring-oxm-3.2.0.jar spring-st
 ruts-3.2.0.jar spring-test-3.2.0.jar spring-tx-3.2.0.jar spring-web-3
 .2.0.jar spring-webmvc-3.2.0.jar spring-webmvc-portlet-3.2.0.jar

Searching for jars in a manifest is troublesome:

Extract the META-INF/MANIFEST.MF file from the jar
Re-join the split lines to make searching humanely possible

Step 2 is necessary, because of the line breaks. For example, I won't find "struts" in the above, because the word is broken in the middle.

I use this shell script to make this process easier, I call it jar-manifest-classpath.sh:

#!/bin/bash

if test -d "$TMPDIR"; then
    :
elif test -d "$TMP"; then
    TMPDIR=$TMP
elif test -d /var/tmp; then
    TMPDIR=/var/tmp
else
    TMPDIR=/tmp
fi
workdir=$TMPDIR/$(basename "$0")-work-$$

cleanup() {
    rm -fr "$workdir"
}

mkdir -p "$workdir"
trap 'cleanup' 1 2 3 15

for jar; do
    if ! test -f $jar; then
        echo warning: not a file: $jar
        continue
    fi
    [[ $jar = /* ]] || jar=$PWD/$jar
    (
        cd "$workdir" || exit 1
        jar xf "$jar"
        sed -ne '/^Class-Path:/,$p' META-INF/MANIFEST.MF | sed -e 's/^Class-Path: //' -e 's/^ //' | tr -d '\n' | tr ' ' '\n'
    )
done

cleanup

For a jar file with the manifest as above, this will output:

spring-aop-3.2.0.jar
spring-aspects-3.2.0.jar
spring-beans-3.2.0.jar
spring-context-3.2.0.jar
spring-context-support-3.2.0.jar
spring-core-3.2.0.jar
spring-expression-3.2.0.jar
spring-instrument-3.2.0.jar
spring-instrument-tomcat-3.2.0.jar
spring-jdbc-3.2.0.jar
spring-jms-3.2.0.jar
spring-orm-3.2.0.jar
spring-oxm-3.2.0.jar
spring-struts-3.2.0.jar
spring-test-3.2.0.jar
spring-tx-3.2.0.jar
spring-web-3.2.0.jar
spring-webmvc-3.2.0.jar
spring-webmvc-portlet-3.2.0.jar

This is much more pleasent, and perfectly grep-able.

Let me know if you have any ideas to improve or simplify!
(The script is part of my collection on GitHub)


texts:
This is my homebrew bash script for batch renaming files extensions (my music library needs organizing):

#!/bin/bash

# rext.sh
# Syntax: rext.sh EXT1 EXT2
# Batch rename any file in current directory with extension of EXT1 to EXT2

EXT1=$1
EXT2=$2

for f in *.$EXT1
do
  fname=${f%.*}
  newfname="$fname.$EXT2"
  mv -v -i "$f" "$newfname"
done

It works beautifully with my music files, but is this code sufficient or can I still improve on it? Also I've seen examples using rename instead of mv, so is there any advantage in using rename?


texts: Invert bits of a number in clojurescript

texts:
Doing a challenge to print the number that you would get if you convert a given number to binary, invert its bits, then convert back to decimal.

I decided to give it a run in clojurescript (which I don't really know) and while all the below makes sense to me, it has to be possible to do it better and without dropping to js functions as often as I felt I had to.

(def invert-number [n] (+ 1 (* -1 n)))
(defn number-complement [num]
 (let [numstr (.toString num 2)
       bits (map (comp invert-number js/parseInt) numstr)
       complement-bits (reduce str bits)]
      js/parseInt complement-bits 2)))


texts: Display four random entries from an array

texts:
This code will take the array shown and display 4 random entries in the format shown (or near enough).

Are there any parts of this that can be optimised? I'm not happy about the if statement.

<?php
function shuffle_assoc($list) {
  if (!is_array($list)) return $list;

  $keys = array_keys($list);
  shuffle($keys);
  $random = array();
  $i = 0;
  foreach ($keys as $key) {
    $random[$key] = $list[$key];
    if ($i >= 3){
      break;
    }
    $i++;
  }
  return $random;
}

$arr = array();
$arr[] = array('img' => "http://placehold.it/150x150", 'link' => '#1', 'text1' => "ONE 1", 'text2' => 'ONE 2');
$arr[] = array('img' => "http://placehold.it/200x150", 'link' => '#2', 'text1' => "TWO 1", 'text2' => 'TWO 2');
$arr[] = array('img' => "http://placehold.it/250x150", 'link' => '#3', 'text1' => "THREE 1", 'text2' => 'THREE 2');
$arr[] = array('img' => "http://placehold.it/300x150", 'link' => '#4', 'text1' => "FOUR 1", 'text2' => 'FOUR 2');
$arr[] = array('img' => "http://placehold.it/350x150", 'link' => '#5', 'text1' => "FIVE 1", 'text2' => 'FIVE 2');
$arr[] = array('img' => "http://placehold.it/400x150", 'link' => '#6', 'text1' => "SIX 1", 'text2' => 'SIX 2');
$arr[] = array('img' => "http://placehold.it/450x150", 'link' => '#7', 'text1' => "SEVEN 1", 'text2' => 'SEVEN 2');

foreach(shuffle_assoc($arr) as $key => $value)
{
    echo '

';
    echo '
'.$value["link"].'
';
    echo '
'.$value["text1"].'
';
    echo '
'.$value["text2"].'
';
    echo '
';
}
?>


texts:
I've made a Ruby linked list data structure class and a lookup method. I know that lookup will only fetch the first occurrence and I'm ok with that.

Is it a bad implementation? Why is it bad? What can I improve to make it more idiomatic?

# linear collection of data elements, in which linear order is not given by
# their physical placement in memory. Instead, each element points to the next.
class LinkedList
  attr_accessor :value, :next

  def lookup(lookup_value)
    return self if value.eql? lookup_value
    return self.next.lookup(lookup_value) unless self.next.nil?
  end
end

Test code

root = LinkedList.new
root.value = 5

node1 = LinkedList.new
node1.value = 10

node2 = LinkedList.new
node2.value = 15

node3 = LinkedList.new
node3.value = 12

root.next = node1
node1.next = node2
node2.next = node3

p root.lookup(2) # nil
p root.lookup(15) # #<LinkedList:0x007f83db0a95b8 @value=15, @next=#<LinkedList:0x007f83db0a9590 @value=12>>
p root.lookup(12) # #<LinkedList:0x007fcbe91196a0 @value=12>


texts: Unit testing an unspecific order of method calls

texts:
I cannot figure out how I am supposed to unit test the implementation of GetFilteredEntites(IEnumerable<MyEntity> myEntities).

public class MyEntity
{
    public int Rank { get; set; }
    public int Points { get; set; }
}

public interface IMyMethodClass
{
    ICollection<MyEntity> RemoveBadRanking(IEnumerable<MyEntity> myEntities);
    ICollection<MyEntity> RemoveLowPoints(IEnumerable<MyEntity> myEntities);
}

public class MyMethodClass : IMyMethodClass
{
    public ICollection<MyEntity> RemoveBadRanking(IEnumerable<MyEntity> myEntities)
    {
        return myEntities.Where(m => m.Rank > 100).ToList();
    }

    public ICollection<MyEntity> RemoveLowPoints(IEnumerable<MyEntity> myEntities)
    {
        return myEntities.Where(m => m.Points < 500).ToList();
    }
}

public class MyTargetClass
{
    private readonly IMyMethodClass _myMethodClass;

    public MyTargetClass(IMyMethodClass myMethodClass)
    {
        _myMethodClass = myMethodClass;
    }

    public ICollection<MyEntity> GetFilteredEntites(IEnumerable<MyEntity> myEntities)
    {
        var a = _myMethodClass.RemoveBadRanking(myEntities);
        var b = _myMethodClass.RemoveLowPoints(a);
        return b;
    }
}

public class MyTestClass
{
    [Fact]
    public void Gets_No_Bad_Ranking_And_No_Low_Points()
    {
        var myEntities = new List<MyEntity>();
        var myMethodClass = Substitute.For<IMyMethodClass>();
        var target = new MyTargetClass(myMethodClass);
        var a = new List<MyEntity>();
        myMethodClass.RemoveBadRanking(Arg.Is(myEntities)).Returns(a);
        var b = new List<MyEntity>();
        myMethodClass.RemoveLowPoints(Arg.Is(a)).Returns(b);

        var filteredEntites = target.GetFilteredEntites(myEntities);

        Assert.Same(b, filteredEntites);
    }
}

The above code tests and requires GetFilteredEntites(IEnumerable<MyEntity> myEntities) to be implemented in a correct way. The following implementation is just as correct as the first one:

public ICollection<MyEntity> GetFilteredEntites(IEnumerable<MyEntity> myEntities)
{
    var a = _myMethodClass.RemoveLowPoints(myEntities);
    var b = _myMethodClass.RemoveBadRanking(a);
    return b;
}

The unit test I have wrote above requires the method calls to be in the right order, but this is not needed for the implementation to be correct. The second implementation of GetFilteredEntites(IEnumerable<MyEntity> myEntities) (right above here) will not pass the unit test I wrote.

How would you change the unit test of GetFilteredEntites(IEnumerable<MyEntity> myEntities), so the order of the method calls is not required to be in a specific order?


texts: Fine tuning of an informative replacement for rm

texts:
I want to replace rm by a more informative variant: I would like to see which files will be deleted, along with their size, and I would like this information to use the same coloring as ls. As an example (please imagine the coloring):

# remove -r foo/ bar/*
4K   bar/file1
1.2M foo/
6.1M bar/file2
remove -r 3 files, 7.3M [yn]? _

I built on suggestions found in another question, and now have a bash script that I begin to like. I have been using it for a few days without noticing any obvious errors. However, I would appreciate any help with making sure that the script will not misbehave in cases I did not foresee.

Here is my code for the coloring:

du_colored (){
    # read ls --color output into ls_colored_array
    # use \13 as a trick to handle names with spaces
    read -d '\n' -r -a ls_colored_array <<< $(ls -Ad --color "$@" | tr " " "\13")

    if [[ ${#ls_colored_array[@]} = 0 ]]; then return 1; fi

    # - loop over the array and issue du -sh for every element (without coloring)
    # - exchange du's ouput with ls's
    # - finally sort the output
    for i in "${ls_colored_array[@]}"; do
        i=`echo $i | tr "\13" " "`
        printf '%s' "${i}" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | \
            xargs -n1 -0 du -sh | awk -v i="$i" '{ printf "%-4s ", $1; print i }'
    done | sort -h
}

and the removal script:

# Separate options to rm from 'real' arguments
for i in "$@"; do
    case $i in
        -*) options+=( "$i" ) ;;
        *)  toRemove+=( "$i" ) ;;
    esac
done

# Print file list
# Abort if no files match
du_colored "${toRemove[@]}"
if [ "_$?" != "_0" ]; then exit 1; fi

# Print summary
count=$(find "${toRemove[@]}" 2>/dev/null | wc -l 2>/dev/null)
size=$(du -sch "${toRemove[@]}" 2>/dev/null | tail -1 | tr '\t' ' ')
plural="s"
if [ $count -eq 1 ]; then plural=""; fi
printf "remove ${options[@]} ($count file$plural, $size) [ny]? " ; read
if [ "_$REPLY" = "_y" ]; then
    /bin/rm ${options[@]} ${toRemove[@]}
else
    echo '(cancelled)'
fi

Does this look safe in the sense that what du_colored presents is exactly what will be deleted? Both files and directories seem to be treated correctly, even if they include spaces or wildcards. Are there scenarios that I missed? An obvious one are file names that contain a \13 character.
The initial ls in du_colored throws errors for non-existing files, which I like because it mimicks the behavior of regular rm. However, I currently discard all error messages of the final find and grep commands, such that non-existing files do not produce multiple errors. This seems like a dirty trick. Is there a cleaner way?
Bonus question: Is there an easy way to make the output use multiple columns, just as a regular ls would? I tried several ways to pipe the output of du_colored to other tools, but either the colors were lost completely or the control characters for the coloring broke the alignment, as they do in ls --color | pr -2 -t.