texts:
I need a little bit of refactor my initialize method because I think it will make initializer more flexible and readable.

class LogAdminData
  DEFAULT_EXCLUDED_PARAMS = %w[
    encrypted_password
    reset_password_token
    reset_password_sent_at
  ].freeze

  def initialize(admin_obj:, action_type:, old_data:, new_data:, excluded_params: %w[])
    excluded_params += DEFAULT_EXCLUDED_PARAMS
    @old_data = old_data.reject { |k, _v| excluded_params.include? k }
    @new_data = new_data.reject { |k, _v| excluded_params.include? k }
    @action_type = action_type
    @admin_email = admin_obj.email
    @admin_role = admin_obj.role
  end

  def call
    AdminPanelLog.create(
      admin_email: admin_email,
      admin_role: admin_role,
      action_type: action_type,
      new_data: new_data,
      old_data: old_data,
    )
  end

Maybe something like

  def cleanup_data(data)
    excluded_params += DEFAULT_EXCLUDED_PARAMS
    data.reject { |k, _v| excluded_params.include? k }
  end

But how to call it in initializer using old_data and new_data?


texts:
I am currently rewriting a legacy PHP4 application to a MVC-style object-oriented one.
As the most complex part is the models, I came up with the following controller-view scheme. I wanted to use only variables in my views, using only <? if ($foo === 'bar'): ?>-like blocks if necessary. The controllers instead generate the variables for the views from the data generated by my models.

Is the following a good approach to MVC code separation?

The controller code looks like this:

interface InterfaceController {
    public function setup();
    public function compile();
}

abstract class AbstractPageController implements InterfaceController
{
    public static $variables = array();
    public static $template = '';

    public function compile() {
        static::$variables['header'] = Controller::run('partials/header');
        static::$variables['footer'] = Controller::run('partials/footer');

        return (new View(static::$template, static::$variables))->render();
    }
}

class ExampleController extends AbstractPageController
{
    public function setup() {
        $variables['foo'] = 'bar';

        static::$template = 'examplepage';
        static::$variables = $variables;

        return $this;
    }
}

An example route would look like that:

/* ... */

Route::get('/example/supercoolfeature', function() {
    Controller::run('example/features/supercool');
});

/* ... */

So, the router calls the following Controller class:

class Controller
{
    /**
     * Executes a controller:
     * If called as "Controller::run('foo/bar')", the function will call the
     * class "App\Controllers\Foo\BarController" and execute the setup() and run()
     * methods.
     */
    public static function run($name) {
        $parts = array();

        foreach (explode('/', $name) as $part) {
            // uppercase each controller path part so you can
            // go lowercase in the actual code
            $parts[] = ucfirst($part);
        }

        // attach the parts together and append "Controller" at the end
        $name = '\\App\\Controllers\\' . implode('\\', $parts) . 'Controller';

        // run the controller, let it prepare a view and return the rendered code
        return (new $name())->setup()->compile();
    }
}

And finally, the respective view for the controller:

<?php echo $header; ?>

Hi there, <?php echo $foo; ?>!

<?php echo $footer; ?>


texts: Write to a CV::Mat from a higher scope within a function

texts:
I've written this function to read matrices from the ROS parameter server.

void read_matrix_from_param(ros::NodeHandle nodehandle, std::string param, cv::Mat &cv_matrix) {
      // 1D vector for temporarily holding matrix elements
      std::vector<double> _data;

      // matrix dimensions
      int _rows, _cols;

      // read from param server
      nodehandle.getParam(param + std::string("/data"), _data);
      nodehandle.getParam(param + std::string("/rows"), _rows);
      nodehandle.getParam(param + std::string("/cols"), _cols);

      // create cv::Mat and copy into cv_matrix
      cv::Mat(_rows, _cols, CV_64F, _data.data()).copyTo(cv_matrix);
  }

called like this:

cv::Mat test_matrix1, test_matrix2;
read_matrix_from_param(private_nh, std::string("camera_matrix"), test_matrix1);
read_matrix_from_param(private_nh, std::string("distortion_coefficients"), test_matrix2);

This works as expected but my editor gives me a style-guide warning for the function signature.

runtime/references: Is this a non-const reference? If so, make const or use a pointer: cv::Mat &cv_matrix

So how should this be done? I have tried to do as the style-guide suggests but been unable to get working code again.


texts:
I'm in the process of improving my coding style and performance hence I decided to delve into bit manipulation on Hackerrank. I thought the question was straight forward but on large input, my solution's performance declines. How do I improve it? Any smart tricks to solve the below question is welcome as well.

Below is the question

  Given an integer, \$n\$ , find each \$x\$ such that:

  \$0 \leq x \leq n \$

  \$ n + x = n \oplus x \$

  where \$\oplus\$ denotes the bitwise XOR operator. Then print an
  integer denoting the total number of \$x\$ 's satisfying the criteria
  above.

  Input Format

  A single integer, \$n\$ .

  Constraints

  \$0 \leq n \leq 10 ^ {15}\$

  Subtasks

  \$0 \leq n \leq 100\$ for \$60\%\$ of the maximum score

  Output Format

  Print the total number of integer \$x\$ 's satisfying both of the
  conditions specified above.

  Sample Input 0

5

  Sample Output 0

2

  Explanation 0

  For \$n = 5\$ , the \$x\$ values \$0\$  and \$2\$ satisfy the
  conditions:

  Thus, we print \$2\$ as our answer.

Here is my code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
     static int SumVsXoR(long number)
    {
        int count =0;
        long j = 0;
        while (j <= number)
        {
            if(j + number == (j^number)){
                count++;
            }
            j++;
        }
        return count;
    }

    static void Main(String[] args) {
        long n = Convert.ToInt64(Console.ReadLine());
        Console.WriteLine(SumVsXoR(n));
    }
}


texts:
A few days back I found an interesting problem which reads the following:
Given two vector spaces generate the resulting set of its cartesian product.
\begin{gather}
\text{Let: } \mathcal{V}, \mathcal{W} \text{ be vector spaces}\\
\mathcal{V} \times \mathcal{W} = \{ (v,w) \mid v \in \mathcal{V} \land w \in \mathcal{W} \}
\end{gather}

Hint 1: A vector space is a set of elements called vectors which accomplishes some properties
Hint 2: Design the solution for finite vector spaces
Tip 1: It is recommended to use structures
Constraint: You are forbidden to use any stl class

I solved this problem with the next approach:
struct vector_pair
{
    double *vector_a;
    double *vector_b;
    size_t a_dimension;
    size_t b_dimension;
};

struct cartesian_product_set
{
    vector_pair *pairs;
    size_t pairs_number;
};

cartesian_product_set vector_spaces_cartesian_product(double **space_v, size_t v_vectors,
    size_t v_dimension, double **space_w, size_t w_vectors, size_t w_dimension)
{
    cartesian_product_set product_set{new vector_pair[v_vectors * w_vectors], v_vectors * w_vectors};

    for (size_t i = 0, j, k = 0; i < v_vectors; i++)
        for (j = 0; j < w_vectors; j++)
            product_set.pairs[k++] = vector_pair{space_v[i], space_w[j], v_dimension, w_dimension};

    return product_set;
}

How could I improve this code if possible?
Thank you.


texts: Keep clean SimpleIoc initialization when reusing UserControl

texts:
Originally I had ChildUserControl inside ParentUserControl inside MainWindow with this view-models (here only constructors):

public MainViewModel(IParentViewModel pvm) {}

public ParentViewModel(IChildViewModel cvm) {}

public ChildViewModel(IMessenger m) {}

And straightforward DI initialization in ViewModelLocator:

var container = SimpleIoc.Default;
container.Register<IMessenger, Messenger>();
container.Register<IParentViewModel, ParentViewModel>();
container.Register<IChildViewModel, ChildViewModel>();
container.Register<IMainViewModel, MainViewModel>();
...
public IMainViewModel MainViewModel => ServiceLocator.Current.GetInstance<IMainViewModel>();

Then I needed to add second ParentUserControl instance near the first one, and let them work independently. MainViewModel constructor became to look like this:

public MainViewModel(IParentViewModel pvm1, IParentViewModel pvm2) {}

But DI initialization changed dramatically:

container.Register<IMessenger, Messenger>();

container.Register<IParentViewModel>(
    () => new ParentViewModel(new ChildViewModel(container.GetInstance<IMessenger>("key1"))),
    "key1");

container.Register<IParentViewModel>(
    () => new ParentViewModel(new ChildViewModel(container.GetInstance<IMessenger>("key2"))),
    "key2");

container.Register<IMainViewModel>(() => new MainViewModel(
    container.GetInstance<IParentViewModel>("key1"),
    container.GetInstance<IParentViewModel>("key2")));

As you can see, now I have to point to concrete ParentViewModel and ChildViewModel classes in several places, and I also have to manually use GetInstance everywhere. I think this is not good.

What can you tell about this latter DI initialization? I guess it can't be improved by SimpleIoc, but can Unity or Ninject help in this situation?


texts:
I am preparing for an interview and came to know about this question: implement a vector class in C++. I thought of how I would write it an interview and included the code below. The things I know I did not cover already are:
1) No use of templates for different data types
2) No use of iterators for iterating.
My point is I wanted to write a simple code and I want to know if that would be sufficient to crack the interview round or not?
Can you please go through the below code and point out the things that I must cover from an interview perspective. Thanks.

class Vector{
        int capacity;
        int sizet;
        int *arr;
    public:
        Vector():capacity(0),sizet(0),arr(new int){}
        Vector(int size):capacity(size),sizet(size),arr(new int[sizet]()){}
        Vector(const Vector &v){            //copy ctor
                capacity = v.capacity;
                sizet = v.sizet;
                arr = new int[sizet];
            for(int i=0;i<sizet;i++)
                arr[i] = v.arr[i];

        }
        int &operator [](int index){            //overloading index[] operator
            return arr[index];
        }
        Vector &operator==(const Vector& v){        //overloading assignment operator
        if(this != v){
                capacity = v.capacity;
                sizet = v.sizet;
                arr = new int[sizet];
            for(int i=0;i<sizet;i++)
                arr[i] = v.arr[i];
            }
            return *this;
        }
        void push_back(int elem){
            if(sizet == capacity){
                if(capacity ==0)
                    capacity++;
                else
                    capacity = 2*capacity;
            }
            arr[sizet++]=elem;
        }
        void pop_back(){
            sizet--;
        }
        void insert(iterator it,int size=1,int val=0){

        }
        int size(){
            return sizet;
        }
        void resize(int n){
        if(sizet < n){                  //if increasing the size
            for(int i=sizet;i<n;i++)
                arr[sizet++]=0;
        }
        else                            //decreasing the size
            sizet = n;
        }
        int at(int index){
            return arr[index];
        }
        int front(){
            return arr[0];
        }
        int back(){
            return arr[sizet];
        }

        ~Vector(){              //dtor
        delete arr[];
        }
    };

Edit: Resolved the naming conflict as was suggested.


texts:
I have elements that are generated dynamically on the screen. I have bars and diamonds.

The problem is I have hundreds of different types of bars or diamonds that need to be rendered on the screen. The bars and diamonds are tasks and milestones on a gantt chart. There are hundreds of different types and each type has to have a certain image attached to it for the background so they can be identified a little more easily.

Instead of writing a huge css file with background-image style I thought it would be  better to dynamically generate a css class with the background-image: url("~/relativepath/imageName") style.

Each one of these different types of elements have their own image file associated with them. Every element also has a Code property such as EX0blue. I was thinking of changing the image file names to match the code property, so when the css is generated I can give it a class name of the code and a relative path such as background-image: url("~/relativepath/code.jpg")

Is there a more optimal solution than what I am trying to implement?

Here is what I have so far

class CssGenerator {

    constructor() {
        this.relativeImagePath = '~/Content/images/activityTypes/';
    }

    createImageClass(code) {

        let style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = '.' + code +' { background-image: url(' + relativeImagePath + code + '.jpg); }';
        document.getElementsByTagName('head')[0].appendChild(style);
    }

}

Then after that I would add the class like this

gantt.templates.task_class=function(start, end, task){
 this.cssGenerator.createImageClass(task.code)
return task.code;
};

That return statement applies the string returned as a classname to the dynamic element. It's from the API


texts: Maintainable way to concatenate strings in JS and vendor prefixes

texts:
I am writing a jQuery plugin which, among other things, inserts a stylesheet in the head. Additionally, I also require vendor prefixes. A lot of values are derived from variables in the script, and I'm unsure how I can write this in a maintainable manner. Now I just list everything, but maybe there's a better way for vendor prefixing and overall structure of this script?

var bubble_in_ms = to_ms(args.bubble_in),
    opacity_out_ms = to_ms(args.opacity_out),
    text_in_ms = to_ms(args.text_in),
    bubble_color_rgb = rgbGenerator(args.bubble_color),
    bubble_css = "";

bubble_css += ".bubble {-webkit-animation: ";
bubble_css += "bubble-effect-in " + bubble_in_ms + "ms forwards,";
bubble_css += "bubble-effect-out " + opacity_out_ms + "ms ";
bubble_css += bubble_in_ms + "ms forwards;";
bubble_css += "animation: ";
bubble_css += "bubble-effect-in " + bubble_in_ms + "ms forwards,";
bubble_css += "bubble-effect-out " + opacity_out_ms + "ms ";
bubble_css += bubble_in_ms + "ms forwards;";
bubble_css += "background: ";
bubble_css += "rgba(" + bubble_color_rgb.r + "," + bubble_color_rgb.g + "," + bubble_color_rgb.b + ",";
bubble_css += args.bubble_opacity + ")";
bubble_css += "}";
bubble_css += ".bubble-wrap:after {-webkit-transition: ";
bubble_css += "opacity " + text_in_ms + "ms;";
bubble_css += "transition: ";
bubble_css += "opacity " + text_in_ms + "ms;";
bubble_css += "}";


texts: SICP - exercise 2.5 - representing pairs of nonnegative integers using only numbers and arithmetic operations

texts:
From SICP

Exercise 2.5: Show that we can represent pairs of nonnegative integers using only numbers and arithmetic operations if we represent the pair a and b as the integer that is the product 2^x*3^y. Give the corresponding definitions of the procedures cons, car, and cdr.

Please review my code.

(define (cons x y)
    (* (expt 2 x) (expt 3 y)))

I created repeat-divide to find x and y.

(define (repeat-divide x y)
    (if (> (remainder x y) 0)
        0
    (+ 1 (repeat-divide (/ x y) y))))

Selectors

(define (car p) (repeat-divide p 2))

(define (cdr p) (repeat-divide p 3))

I am not mathematically inclined, so I feel this solution might be extremely inefficient. How can I make this code better and more efficient?


texts: Matlab - Fastest way to parallel read many files line by line?

texts:
I have a few hundred files in a directory that I need to read in parallel. Namely, the files that have the same name, but different file extensions (.a and .b), but they're pretty much plain text files. The corresponding .a and .b files have the same number of lines. I need to read each line into cell arrays x and y until all each pair of files in the directory have been read. Currently, I'm doing it with a nested loop which works fine, but just takes too long. Is there any way to do this faster?

x = {};
y = {};
dir1 = dir( [ directory, filesep, '*', 'a'] );
dir2 = dir( [ directory, filesep, '*', 'b'] );
i = 1;
for i=1:length(dir1)
    fid1 = fopen([directory, filesep, dir1(i).name]);
    fid2 = fopen([directory, filesep, dir2(i).name]);
    sentence1 = fgetl(fid1);
    sentence2 = fgetl(fid2);
    while ischar(sentence)
        x{i} = sentence1;
        y{i} = sentence2;
        i = i+1;
        sentence1 = fgetl(fid1);
        sentence2 = fgetl(fid2);
    end
    fclose(fid1);
    fclose(fid2);
end


texts: Return "support count" for a given itemset after searching a large list of transactions

texts:
Not having found a satisfactory association rules implementation in Python, I set out to code my own. The code below runs reasonably quickly, but the groceries dataset used for testing is peanuts compared to the actual transactions dataset I'll be using in production (think hundreds of thousands of transactions). I'm looking for any performance improvements. Most of the objects iterated over for creating itemset frequencies are sets, but is there anything else in Python I should be thinking of, speed-wise, that's relevant to the code below? I think the final list comprehension maybe could use some improvement, but I haven't had any luck with multiprocessing.Pool yet.

import operator
from itertools import combinations
from bs4 import BeautifulSoup
import urllib2

# separate into support_count and support
def support_count(items):
    '''
    Let transactions be a list of transactions, n_transactions be the number of elements in that list, and X be an itemset.

    In pseudocode, support_count(X) - support count for an itemset of length 1:

    count = 0
    For t in transactions:
        if X is an element of t:
            count += 1
    return count

    support_count((X,Y)) - support count for an itemset of length two:
    count = 0
    For t in transactions:
        if X or Y is an element of t:
            count += 1
    return count
    '''
    # if str, then we know we're looking for the support of one item
    if type(items) is str:
        # if type(items) is str then we know we're searching
        # for support_count for one item and can use rows_indiv (list of sets of words)
        spec_rows = rows_indiv
        count = sum([1 for row in spec_rows if items in row])
    # if tuple, then we know we're looking for the support rule between two items
    elif type(items) is tuple:
        if items[0] == items[1]:
            return None
        else:
            # use rows_all_type_combos to satisfy or condition...
            # either the whole tuple is in a given transaction t, or we see if at least one of the elements is in t
            spec_rows = rows_all_type_combos
            # if any of the elements in items are in a row, count that row
            count = sum([1 for row in spec_rows if any([item in row for item in items])])
    return count

def vocab(tuples_set):
    return {word for t in tuples_set for word in t}

url = 'https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/groceries.csv'
open_url = urllib2.urlopen(url)
soup = BeautifulSoup(open_url).encode("ascii")

# list of strings
rows = [row for row in soup.split('\n')]

# global
n_rows = len(rows)

# set of all items in transactions
items = {item for trans in rows for item in trans.split(',')}

# create all possible combinations of elements in items
# should be factorial(len(items))/(factorial(r) * factorial(len(items) - r)) number of elements in pairs list below
pairs = [(a,b) for (a,b) in combinations(items, 2)]

#### two options ####
# One: one support function for tuples and string. support_master()
# used to look up tuples and individual items
rows = [set(row.split()) for row in rows]

# Two: keep one support function for tuples, and one support function for individuals.... support_tuple() and support_indiv()
# used for looking up tuples
rows_tuple = [set((a, b) for a in row for b in row) for row in rows]

# each row is set of elements... used for looking up quickly an individual word
# used for looking up individual items
rows_indiv = [set(a) for a in rows]

rows_all_type_combos = []
for row in rows_tuple:
    row_vocab = vocab(row)
    row_to_append = list(row)
    for word in row_vocab:
        row_to_append.append(word)
    rows_all_type_combos.append(row_to_append)

for idx, row in enumerate(rows_all_type_combos):
    rows_all_type_combos[idx] = set(rows_all_type_combos[idx])

# support for all pairs of items
res = [(p, support_count(p)) for p in pairs]


texts:
Is there a cleaner way to do the following?

friend_ids = [1,2,3,4,5]
friendIDsQuery = ""
friend_ids.each_with_index do |friend_id, index|
  friendIDsQuery += "SELECT id FROM test WHERE user_id = #{friend_id}"

  if index != friend_ids.size - 1
    friendIDsQuery += " INTERSECT "
  end
end

Basically I'm passing in an array of IDs, and building a custom INTERSECT query.


texts:
I have a model with a hierarchical structure like this:

Batch (root)
    Groups (List of groups owned by the Batch)
        Groupnames (Nameing of the Group in different Languages)
        Tags (List of Tags owned by the Group)
           Tagnames (Nameing of the Tag in different Languages)
           Logs (List of Logs owned by the Tag)

I want to display all logs with some fields from its parent Tag and parent Group plus the names in a selected Language. I have the following LINQ to select the data which produces the results I want, but queries the database 4 times per log.

        entities.LOGs.Select(log =>
            new
            {
                GroupType = log.TAG.GROUP.GROUPTYPE, //Additional Querry here
                Group = log.TAG.GROUP.GROUPNAMEs.First(gn => gn.LCID == 1031).GROUPNAME1, //Additional Querry here
                Tag = log.TAG.TAGNAMEs.First(tn => tn.LCID == 1031).TAGNAME1, //Additional Querry here
                Time = log.SERVERTIME,
                SourceTime = log.SOURCETIME,
                Information = log.INFORMATION,
                PreviousValue = log.OLDVALUE,
                Value = log.CURRENTVALUE,
                Unit = log.TAG.UNIT,//Additional Querry here
                Operator = log.OPERATOR
            }
        );

Is there a way to avoid these additional queries?

EDIT:
Here are the querries generated by the above code each subqerry is called 14 times

Main querry:

SELECT
    c."GROUPTYPE" AS COL1,
    b."GROUPID" AS COL2,
    a."TAGID" AS COL3,
    a."SERVERTIME" AS COL4,
    a."SOURCETIME" AS COL5,
    a."INFORMATION" AS COL6,
    a."OLDVALUE" AS COL7,
    a."CURRENTVALUE" AS COL8,
    b."UNIT" AS COL9,
    a."OPERATOR" AS COL10
FROM "LOGS" a
LEFT JOIN "TAGS" b
       ON (a."TAGID" = b."ID")
LEFT JOIN "GROUPS" c
       ON (b."GROUPID" = c."ID")

Subquerry group:

SELECT
    "BATCHID" AS COL1,
    "BATCHID" AS COL2,
    "GROUPTYPE" AS COL3,
    "IDENTIFIER" AS COL4,
    "SIGNATURE" AS COL5,
    "TAGCOUNT" AS COL6
FROM "GROUPS"
WHERE "ID" = CAST(@p0 AS INTEGER)

Subquerry tag:

SELECT
    "GROUPID" AS COL1,
    "GROUPID" AS COL2,
    "IDENTIFIER" AS COL3,
    "LOGCOUNT" AS COL4,
    "SIGNATURE" AS COL5,
    "UNIT" AS COL6
FROM "TAGS"
WHERE "ID" = CAST(@p0 AS INTEGER)

Subquerry groupnames:

SELECT
    b."GROUPID" AS COL1,
    b."LCID" AS COL2,
    b."GROUPID" AS COL3,
    b."GROUPNAME" AS COL4,
    b."SIGNATURE" AS COL5
FROM "GROUPS" a
LEFT JOIN "GROUPNAMES" b
       ON (a."ID" = b."GROUPID")
WHERE a."ID" = CAST(@p0 AS INTEGER)

Subquerry tagnames:

SELECT
    b."GROUPID" AS COL1,
    b."LCID" AS COL2,
    b."GROUPID" AS COL3,
    b."GROUPNAME" AS COL4,
    b."SIGNATURE" AS COL5
FROM "GROUPS" a
LEFT JOIN "GROUPNAMES" b
       ON (a."ID" = b."GROUPID")
WHERE a."ID" = CAST(@p0 AS INTEGER)


texts: QuickSort C++ Implmentation using Iterators #2

texts:
This is part 2 of the previous question asked which is at - QuickSort C++ Implementation using Iterators.

Based on Jerry Coffin's answer, I have made the changes except for using templates.

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

typedef std::vector<int>::iterator itr;

itr partition(itr left,itr right)
{

    itr i=left-1;

    itr it=left;

    while(it<right)
    {
        if(*it<*right)
        {
            using std::swap;

            std::advance(i,1);
            swap(*i,*it);
        }
        std::advance(it,1);
    }

    using std::swap;

    swap(*(i+1),*right);
    std::advance(i,1);
    return i;
}

void quicksort(itr left, itr right)
{
    if(std::distance(left,right)>=2)
    {
        itr q=partition(left,right);
        quicksort(left,q-1);
        quicksort(q+1,right);
    }
}

int main()
{
    std::vector<int> v={6,10,4,5,3,9,13,8};

    std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," "));
    std::cout<<std::endl;

    quicksort(std::begin(v),std::end(v)-1);

    std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," "));
    std::cout<<std::endl;

    return 0;
}


texts: Groups/Communities in Facebook snap dataset using networkx in python

texts:
I was using facebook dataset and making a graph on it. But was not been able to find different groups/communities in it. The code was using is this:

import networkx as nx
import matplotlib.pyplot as plt

g = nx.read_edgelist('facebook_combined.txt', create_using=nx.Graph(), nodetype=int)
print nx.info(g)

sp = nx.spring_layout(g)
nx.draw_networkx(g, pos=sp, with_labels=False, node_size=35)
# plt.axes('off')
plt.show()

The result i was getting is this:

But was not been able to find groups or communities in it.

The link to the dataset is here

The source to the is here


texts:
I have designed a simple state machine for the following diagram:

I am seeking improvements from the community. I especially don't like two things:

When calling performAction I wan't to send only the action without the callback.
And maybe remove the Action enum from the Engine class.

For simplicity everything is added in one .cpp file:

#include <iostream>
#include <utility>
#include <map>

typedef enum {
    Idle,
    Start,
    Stop,
    ChangeSpeed
}State;

class Engine
{
public:
    typedef enum {
        SetSpeed,
        Halt
    }Action;

    typedef void (Engine::*DoAction)(void);

    Engine() {
        std::cout << "Engine()" << std::endl;
    }
    void setSpeed() {
        std::cout << "Engine::setSpeed()" << std::endl;
    }
    void halt() {
        std::cout << "Engine::halt()" << std::endl;
    }
};

struct ActionCallback
{
    Engine::Action         action;
    Engine::DoAction       callback;
    ActionCallback(Engine::Action a, Engine::DoAction c):action(a), callback(c){}
};

struct StateAction {
    State state_;
    Engine::Action action_;
    StateAction(State state, Engine::Action action):state_(state), action_(action){}
    friend bool operator<(const StateAction& sta1, const StateAction& sta2) {
        return (sta1.state_ < sta2.state_) ||
        (sta1.state_ == sta2.state_ &&
         sta1.action_ < sta2.action_);
    }
};

class StateMachine {
public:
    typedef std::map<StateAction, State> StateDiagram;

    StateMachine(Engine &engine):engine_(engine) {
        stateDiag_[StateAction(Idle, Engine::SetSpeed)] = Start;
        stateDiag_[StateAction(Start, Engine::SetSpeed)] = ChangeSpeed;
        stateDiag_[StateAction(ChangeSpeed, Engine::SetSpeed)] = ChangeSpeed;
        stateDiag_[StateAction(ChangeSpeed, Engine::Halt)] = Stop;
        stateDiag_[StateAction(Start, Engine::Halt)] = Stop;
        //
        //  current state is initialized to Idle
        //
        currentState_ = Idle;
        std::cout << "State machine initial state: " << currentState_ << std::endl;
    }
    void performAction(ActionCallback actionCallback) {
        //
        //  The state machine "state" is: currentState
        //
        StateAction stateAction(currentState_, actionCallback.action);

        if (stateDiag_.find(stateAction) == stateDiag_.end()) {
            //
            //  Invalid state transition
            //
            std::cout << "You cannot perform action: "
            << actionCallback.action
            << " from state: "
            << currentState_
            << ", invalid transition!!!"
            << std::endl;
            return;
        }
        State oldState = currentState_;
        //
        //  Execute the action and update the state machine
        //
        (engine_.*actionCallback.callback)();
        currentState_ = stateDiag_[stateAction];

        std::cout << "State changed:\n\tOld state: " << oldState << "\n\tNew state: " << currentState_ << std::endl;
        //
        //  If current state is: Stop change state to: Idle
        //
        if (Stop == currentState_) {
            oldState = currentState_;
            currentState_ = Idle;
            std::cout << "State changed:\n\tOld state: " << oldState << "\n\tNew state: " << currentState_ << std::endl;
        }

    }
    State getInitialState() {
        return Idle;
    }

private:
    StateDiagram stateDiag_;
    State currentState_;
    Engine engine_;
};
int main()
{
    Engine engine;
    StateMachine stateMachine(engine);
    stateMachine.performAction(ActionCallback(Engine::SetSpeed, &Engine::setSpeed));
    stateMachine.performAction(ActionCallback(Engine::Halt, &Engine::halt));
    stateMachine.performAction(ActionCallback(Engine::Halt, &Engine::halt));
    return 0;
}


texts: Constructors for a class to represent a price in € or $

texts:
I'm using a method to make initialize the attributes of my objects, and this method is called from both constructor.

What it does is, if no correct currency has been passed, choose € by default.

I'm just wondering if what I've done below is correct in terms of design.

class Prix
{
    double valeur;
    string monnaie;

    public Prix(double valeur, string monnaie)
    {
        if (monnaie.Equals("€") || monnaie.Equals("$"))
        {
            this.valeur = valeur;
            this.monnaie = monnaie;
        }
        else
        {
            defaultConstr(valeur);
        }
    }

    public Prix(double valeur)
    {
        defaultConstr(valeur);
    }

    private void defaultConstr(double valeur)
    {
        this.valeur = valeur;
        this.monnaie = "€";
    }
}


texts:
I currently use the following code to asynchronously create a cimsession to a remote host, query the remote host, return multiple values and finally updated some textboxes on my WPF window. Just wanting to know if anyone can see any glaring issues (I'm new to all things async and c# :) ) or a better was of doing it:

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Management.Infrastructure;

//creating cimsession to remote host
CimSession session = await Task.Run(() =>
{
     return CimSession.Create(computerHostName);
});

//getting username, bootupstate, manufacturer and model
var GI1 = await Task.Run(() =>
{
    var results = new List<string>();
    IEnumerable<CimInstance> queryResults = session.QueryInstances(nameSpace, WQL, "SELECT Username, BootUpState, Manufacturer, Model FROM Win32_ComputerSystem");
    foreach(CimInstance i in queryResults)
    {
        results.Add(i.CimInstanceProperties["Username"].Value.ToString());
        results.Add(i.CimInstanceProperties["BootUpState"].Value.ToString());
        results.Add(i.CimInstanceProperties["Manufacturer"].Value.ToString());
        results.Add(i.CimInstanceProperties["Model"].Value.ToString());
    }
    //returning all the variables
    return results;
});
//adding username, bootupstate, manufacturer and model to their textboxes
GIUS.Text = GI1[0];
GIBS.Text = GI1[1];
GIMF.Text = GI1[2];
GIMD.Text = GI1[3];

Any suggestions or help is appreciated :)


texts: I need code review on using singleton to access local database

texts:
As I get to some performance issues in my app and find that I use database access in a bad way.
So I decided to move to singleton pattern.
I need someone to review this code and confirm me that I made a good database access via singleton pattern or I am doing something wrong:
This is the class:

@interface DataAccessController : NSObject{
    sqlite3 *databaseHandle;
}
+ (id)sharedManager;
-(void)initDatabase;
...
+ (id)sharedManager {
    static DataAccessController *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

- (id)init {
    if (self = [super init]) {
        [self initDatabase]; // open database connection
    }
    return self;
}
...

AppDelegate.m

DataAccessController *d = [DataAccessController sharedManager];

Usage through the application every time I need data I use:

DataAccessController *d = [DataAccessController sharedManager];
NSMutableArray* data = [d getAllRecordedUnits];


texts: Is my equivalent of Excel's "EOMONTH" function correct?

texts:
I am trying to create a function similar to Excel's EOMONTH function in C#. I have written the following, however, I am not entirely sure if the it achieves the equivalent functionality. Is my equivalent of Excel's "Eomonth" function correct and are my tests sufficient?

public static DateTime EOMonth(this DateTime dateTime, int months = 0)
{
    DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
    return firstDayOfTheMonth.AddMonths(1 + months).AddDays(-1);
}

Tests

[Test]
public void EOMonth_For5jan2011WithNoAddedMonths_ReturnsLastDayOfJan()
{
    var expectedDate = DateTime.Parse("31-Jan-2011");
    var currentDate = DateTime.Parse("5-Jan-2011");

    var result = currentDate.EOMonth();

    Assert.That(result, Is.EqualTo(expectedDate));
}

[Test]
public void EOMonth_For5jan2011With_1_AddedMonths_ReturnsLastDayOfFeb2011()
{
    var expectedDate = DateTime.Parse("28-Feb-2011");
    var currentDate = DateTime.Parse("5-Jan-2011");

    var result = currentDate.EOMonth(1);

    Assert.That(result, Is.EqualTo(expectedDate));
}

[Test]
public void EOMonth_For5jan2011WithNegative_1_AddedMonths_ReturnsLastDayOfFeb()
{
    var expectedDate = DateTime.Parse("31-Dec-2010");
    var currentDate = DateTime.Parse("5-Jan-2011");

    var result = currentDate.EOMonth(-1);

    Assert.That(result, Is.EqualTo(expectedDate));
}

 [Test]
 public void EOMonth_For28Feb2007_12_AddedMonths_ReturnsLastDayOfFeb2008()
 {
     var expectedDate = DateTime.Parse("29-Feb-2008");
     var currentDate = DateTime.Parse("28-Feb-2007");

     var result = currentDate.EOMonth(12);

     Assert.That(result, Is.EqualTo(expectedDate));
 }


texts: PHP code to insert phone number and IP address into a table if not already present

texts:
I am currently coding a PHP script that connects to a database and inserts a phone number and IP address if either item is not present in the table. I believe I have completed it and it is working but I want to make sure that I have used best practices in regards to security. I have read here about how to connect to MySQL and query it using a PDO and a prepared statement. I also read on Stack Overflow that had a fantastic explanation on how to do this. However, this is now a few years old.

I would like to know if my code follows current best practices and that it is secure against SQL injection.

if( isset($_POST['submit']))
{
//user data
$name = $_REQUEST['fullname'];
$numbers = $_REQUEST['number'];
$bedrooms = $_REQUEST['bedrooms'];
$date = $_REQUEST['date'];
$movingFrom = $_REQUEST['from-postcode'];
$movingTo = $_REQUEST['to-postcode'];
$typeOfJob = $_REQUEST['typeOfJob'];
$additionalInfo= $_REQUEST['message'];
$ip = $_SERVER['REMOTE_ADDR'];
$id= NULL;
//connection variables
$host='localhost';
$user='root';
$pass='';
$db='lookup';
$chset='utf8mb4';
$dns = "mysql:dbname=$db;host=$host;charset=$chset";
$options = [
PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES   => false,
];

try {
    //Try to create a new PDO object and pass the vars
    $connection = new PDO($dns, $user, $pass, $options);

    //prepare the database for data and exectue
    $stmt = $connection->prepare("SELECT id, numbers, ip FROM numbers");
    $stmt->execute();

    //Loop through table and check for numbers and ips
    //If present set var to true and break from loop
    $present = false;
    foreach ($stmt->fetchAll() as $k=>$v)
    {
        if($v['ip'] == $ip or $v['numbers'] == $numbers)
        $present = true;
        break;
    }
    //If data present I will be redirecting and informing user
    if($present)
    {
        //TODO: send to different pages
        echo "present";
    }
    //Else insert the data into the table
    else
    {
        $sql = "INSERT INTO numbers (id, numbers, ip) VALUES (NULL, '$numbers', '$ip')" ;
        $stmt = $connection->prepare($sql);

        $stmt->bindParam(':id', $id);
        $stmt->bindParam(':numbers', $numbers);
        $stmt->bindParam(':ip', $ip);
        $stmt->execute();
        echo "Woohoo";
    }
} catch (PDOException $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

There is more code in the if statement but it is irrelevant here as it is for the HTML.


texts: A service that returns paged Products with optional filtering

texts:
In order to better understand what the code does, I've captured this screencast.

Basically, I have a products page and a user can filter products.

CODE

Here is the Page action method on my ProductController:

public ActionResult Page(SearchViewModel search, int page = 1)
{
  var viewModel = _productService.GetPagedProducts(search, page);
  PopulateDropDownSelectListsForSearchVm(viewModel.Search);
  return View(viewModel);
}

That PopulateDropDownSelectListsForSearchVm is a helper method that sits within the controller.

Here is the implementation of the GetPagedProducts method on the IProductService interface:

public ProductPageViewModel GetPagedProducts(SearchViewModel search, int page = 1)
{
  int totalNumberOfProducts;
  if (HaveSearchTermsChanged(search)) page = 1;
  var products = _context.Products.Where(p => p.IsDeleted == search.ShowDeleted).AsQueryable();
  if (search.CategoryId != 0) products = products.Where(p => p.CategoryId == search.CategoryId);
  if (search.BrandId != 0) products = products.Where(p => p.BrandId == search.BrandId);
  if (search.QualityId != 0) products = products.Where(p => p.QualityId == search.QualityId);

  if (!string.IsNullOrEmpty(search.SearchTerm))
  {
    products = products
                .Where(p => p.Name == search.SearchTerm.Trim())
                .OrderBy(p => p.Name);
    totalNumberOfProducts = products.Count();
    products = products.Skip(_recordsPerPage * (page - 1)).Take(_recordsPerPage);
  }
  else
  {
    products = products.OrderBy(p => p.Name);
    totalNumberOfProducts = products.Count();
    products = products.Skip(_recordsPerPage * (page - 1)).Take(_recordsPerPage);
  }

  var productPageVm = new ProductPageViewModel
  {
    Products = ProductViewModelFactory.BuildListOfProductViewModels(products.ToList()),
    Pagination = new PaginationViewModel
    {
      CurrentPage = page,
      RecordsPerPage = _recordsPerPage,
      TotalRecords = totalNumberOfProducts
    },
    Search = search
  };

  TrackCurrentSearchTerm(productPageVm);
  return productPageVm;
}

Readability is really important to me, and right now it just doesn't feel readable. What can I do to improve the overall code, and have it be very readable?


texts:
I have this view vw_RFQPartVendor that's based on these two views vw_requestVendor and vw_vendorEmail and various other tables in SQL 2005.

I find that view vw_RFQPartVendor is a little slow, anyway to improve it?

vw_RFQPartVendor 83250 rows.

SELECT TOP (100) PERCENT
    dbo.SubconJobQ.Regno,
    CASE
        WHEN userName.userName IS NULL THEN UserAcc
        ELSE userName.userName
    END AS requesterName,
    CASE dbo.SubconJobQ.IsPara
        WHEN '1' THEN 'yes'
        ELSE 'no'
    END AS IsPara,
    CASE dbo.SubconJobQ.IsDxf
        WHEN '1' THEN 'yes'
        ELSE 'no'
    END AS IsDxf,
    CASE dbo.SubconJobQ.IsPdf
        WHEN '1' THEN 'yes'
        ELSE 'no'
    END AS IsPdf,
    dbo.SubconJobQ.Rec_date,
    dbo.SubconJobQ.Cov_date,
    dbo.SubconJobQ.Status,
    dbo.SubconJobQ.Remark,
    dbo.SubconJobQ.plant,
    dbo.SubconJobQ.doc_support,
    CASE
        WHEN approverName.userName IS NULL THEN approver
        ELSE approverName.userName
    END AS approverName,
    dbo.SubconJobQ.Comment,
    dbo.SubconJobQ.sync_status,
    dbo.SubconJobQ.ModuleNo,
    CASE dbo.SubconJobQ.req_type
        WHEN 'sq' THEN 'SubContracting Part'
        WHEN 'sd' THEN 'Resend Drawing'
        WHEN 'pq' THEN 'New Part'
        ELSE ''
    END AS req_type,
    dbo.SubconJobQ.projectNo,
    dbo.Reg_parts.Partno,
    dbo.Reg_parts.Quantity,
    dbo.Reg_parts.Part_desc,
    dbo.vw_requestVendor.vendorCode,
    dbo.vw_vendorEmail.vendorEmails
FROM dbo.SubconJobQ
LEFT OUTER JOIN dbo.Reg_parts
    ON dbo.SubconJobQ.Regno = dbo.Reg_parts.Regno
LEFT OUTER JOIN dbo.vw_requestVendor
    ON dbo.SubconJobQ.Regno = dbo.vw_requestVendor.Regno
LEFT OUTER JOIN dbo.vw_vendorEmail
    ON dbo.vw_requestVendor.vendorCode = dbo.vw_vendorEmail.vendor
    AND dbo.SubconJobQ.plant = dbo.vw_vendorEmail.plant
LEFT OUTER JOIN
    (
        SELECT
        user_acc,
        userName
        FROM dbo.t_user_auth
        GROUP BY    user_acc, userName
    ) AS userName
    ON userName.user_acc = dbo.SubconJobQ.UserAcc
LEFT OUTER JOIN
    (
        SELECT
        user_acc,
        userName
        FROM dbo.t_user_auth AS t_user_auth_1
        GROUP BY    user_acc, userName
    ) AS approverName
    ON approverName.user_acc = dbo.SubconJobQ.approver
ORDER BY dbo.SubconJobQ.Regno, dbo.Reg_parts.Partno, dbo.vw_requestVendor.vendorCode

vw_requestVendor

WITH temp(Regno, vendorCode, vendor) AS
    (
        SELECT
            Regno,
            LEFT(CAST(vendor AS NVARCHAR(MAX)), CHARINDEX(';', CAST(vendor AS NVARCHAR(MAX)) + ';') - 1) AS Expr1,
            STUFF(CAST(vendor AS NVARCHAR(MAX)), 1, CHARINDEX(';', CAST(vendor AS NVARCHAR(MAX)) + ';'), '') AS Expr2
        FROM dbo.SubconJobQ
        UNION ALL
        SELECT
            Regno,
            LEFT(CAST(vendor AS NVARCHAR(MAX)), CHARINDEX(';', CAST(vendor AS NVARCHAR(MAX)) + ';') - 1) AS Expr1,
            STUFF(CAST(vendor AS NVARCHAR(MAX)), 1, CHARINDEX(';', CAST(vendor AS NVARCHAR(MAX)) + ';'), '') AS Expr2
        FROM temp AS temp_2
        WHERE (vendor > '')
    )
SELECT TOP (100) PERCENT
    Regno,
    vendorCode
FROM temp AS temp_1
ORDER BY Regno, vendorCode

vw_vendorEmail

SELECT TOP 100 PERCENT
    *
FROM
(
    SELECT
    outerVendorEmail.comp_code,
    outerVendorEmail.plant,
    outerVendorEmail.vendor,
    STUFF
    (
        (
            SELECT ';' + LTRIM(RTRIM(innerVendorEmail.email))
            FROM t_vendor_email AS innerVendorEmail
            WHERE innerVendorEmail.status = 1
            AND innerVendorEmail.comp_code = outerVendorEmail.comp_code
            AND innerVendorEmail.plant = outerVendorEmail.plant
            AND innerVendorEmail.vendor = outerVendorEmail.vendor
            FOR XML PATH ('')
        ), 1, 1, ''
    ) AS [vendorEmails]
    FROM t_vendor_email AS outerVendorEmail
) AS vendorEmails
GROUP BY    vendorEmails.comp_code,
            vendorEmails.plant,
            vendorEmails.vendor,
            vendorEmails.vendorEmails
ORDER BY vendorEmails.comp_code, vendorEmails.plant, vendorEmails.vendor

Execution plan for vw_RFQPartVendor


texts: Improving JavaScript processing of Ruby Regex

texts:
I have a basic webapp that basically serves the same purpose as Rubular.com, but for gsub.

It runs some simple JavaScript, which POST's to a Sinatra app, which then updates page with the results.

index.haml

%h1 Gsubular
%p It's like Rubular! But for y'know...Gsub!

#gsub_div
  %h2 Gsub-ed String:
  #gsub_answer
  %form#gsub_form{:method=>"POST", :action=>"/"}
    %input#test_string{:name=>"test_string"}
    %span< .gsub!(
    %span< /
    %input#gsub_pattern_input{:name=>"pattern"}
    %span /,"
    %input#gsub_replacement_input{:name=>"replacement"}
    %span ")

The Sinatra POST handler:

# create
  post '/' do
    response = {}
    begin
      str = params[:test_string]
      regex_do_this = Regexp.new(params[:pattern])
      gsubbed_string = str.gsub!(regex_do_this, params[:replacement])
    rescue Exception => e
      response[:error] = e.message
      return response.to_json
    end
    response[:gsub_string] = []
    response[:gsub_string].push(gsubbed_string)
    response.to_json
  end

The JavaScript I reworked from someone else's JavaScript to just detect Regex looks like this:

$(function() {
  $("#gsub_pattern_input, #gsub_replacement_input, #test_string").bind('keyup', function(e){
    $.post('/', $(e.target).parents("form").serialize(), function(response){
      var results = eval("(" + response + ")");
      var result;
      $("#gsub_answer").empty();
      $("#gsub_error").empty();
      if(results.gsub_string){
        result = escapeHTMLEncode(results.gsub_string[0])
        if(result == "undefined")
          result = "no matches"
        if(result == "null")
          result = "[EMPTY STRING]"
        $("#gsub_answer").css('color', 'green');
        $("#gsub_answer").html(result);
      }
      else
        $("#gsub_answer").css('color', 'red');
        $("#gsub_answer").html(results.error);
    });
  })
})

function escapeHTMLEncode(str) {
 var div = document.createElement('div');
 var text = document.createTextNode(str);
 div.appendChild(text);
 return div.innerHTML;
}

// Hacky way of stopping enter from submitting...
$(document).ready(function() {
  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
});

I'm not a Javascript expert, but it looks ok to me in general. I know that evals are evil, so that needs fixing.

Can anyone give me some guidance on how to clean this up a bit?

The full Github repo is here.


texts:
I am implementing a music player for Windows 10, and I am in a bit of a pickle at the library phase.

I am scanning for all the files in a music library (plus optional folders the user selects) and I need to get their tags (only need the title and artist; don't need complex details).

The problem is performance. Getting the music properties cost a LOT if you have a large media library. At the moment I am doing the following:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    Task.Factory.StartNew(async () =>
    {
        try
        {
            var files = await StorageHelper.GetFiles(KnownFolders.MusicLibrary);
            List<LocalAudio> tempList = new List<LocalAudio>();
            foreach (var file in files)
            {
                MusicProperties properties = await file.Properties.GetMusicPropertiesAsync();

                var title = string.IsNullOrWhiteSpace(properties.Title) ? file.Name : properties.Title;
                var artist = string.IsNullOrWhiteSpace(properties.Artist) ? file.Path : properties.Artist;
                tempList.Add(new LocalAudio() { Title = title, FilePath = file.Path, Artist = artist, Duration = properties.Duration });
                if (tempList.Count > 50)
                {
                    await AddToLibrary(tempList);
                    tempList.Clear();
                }
            }
            await AddToLibrary(tempList);
            tempList.Clear();
        }
        catch (Exception ex)
        {
            //log exception here
        }
    });
}

private async Task AddToLibrary(List<LocalAudio> tempList)
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        foreach (var item in tempList)
        {
            this.LocalFiles.Add(item);
        }
    });
}

Where LocalAudio is my audio model and this.LocalFiles is an ObservableCollection. Also, the StorageHelper.GetFiles(StorageFolder) method returns an IEnumerable of StorageFiles (music files) (it's a recursive method to go in depth).

I am adding batches of 50 files on the dispatcher thread so I won't block the UI (I know, even if it is an async method running in a task that I started by fire-and-forget it still blocks the UI if I add them continuously...).

How can I optimize this better? Any idea is good. I was thinking to scan them once and save the music properties (title, artist and duration) in a file and read that file on application start and just sync the filename with the data I have in that file (a dictionary or something and read the file and keep it in memory). Not sure how good this idea is.


texts:
I am trying to compare any performance gain(s) from moving a current offset-based pagination endpoint to a cursor based one.

I have the following ruby code:

@limit = 25
@max_pages = 200

def query
  @q ||= MyDataBaseClass.where(status: "open").order(:id)
end

def offset_paginatiion
  # count is currently an implementation for this appraoch
  total = query.count
  ctr = 0
  page_size = 1
  while page_size < @max_pages
    query.limit(@limit).offset(ctr).load
    ctr += @limit
    page_size += 1
  end
end

def keyset_pagination
  current_page = query.limit(@limit)

  page_size = 1
  while true
    break if page_size >= @max_pages
    page_size += 1
    last_id = current_page.last.id
    current_page = query.where("id > ?", last_id).limit(@limit).load
  end
end

require 'benchmark/ips'

Benchmark.ips do |x|
  x.report("offset_pagination") { offset_paginatiion }
  x.report("keyset_pagination") { keyset_pagination }
  x.compare!
end

I've seeded the database with 1 million rows but you can see here I'm only trying to page up to 200 pages (or 5,000 results).

The two methods are returning the exact same data in the same order so they are functionally equivalent and after running the benchmark are performing nearly the same:

│Calculating -------------------------------------
│   offset_pagination      0.009  (± 0.0%) i/s -      1.000  in 110.100327s
│   keyset_pagination      0.009  (± 0.0%) i/s -      1.000  in 112.732942s
│
│Comparison:
│   offset_pagination:        0.0 i/s
│   keyset_pagination:        0.0 i/s - 1.02x  slower

I was under the impression performance (from numerous articles) was a benefit to moving to cursor pagination but have yet to been able to reproduce - what am I doing worng?


texts: Getting a valid datetime format from a string

texts:
I have this code in a class that gets a string, parses it, and makes some adjustments to get a valid datetime format from the string.

An input string for this function could be = "A0X031716506774"

import datetime
def _eventdatetime(self, string):
    base_date = datetime.datetime.strptime("1980-1-6 0:0", "%Y-%m-%d %H:%M")
    weeks = datetime.timedelta(weeks = int(string[5:9]))
    days = datetime.timedelta(days = int(string[9]))
    seconds = datetime.timedelta(seconds = int(string[10:15]))
    stamp = base_date + weeks + days + seconds

    adjustUTC = datetime.timedelta(hours = 3)
    stamp =  stamp - adjustUTC

    return str(stamp)

Total time: 0.0483931 s

How can I improve the performance for this? The third line seems to be the slowest.


texts: Safely handling memory allocation across DLL boundaries in Windows

texts:
I know that, in Windows, memory must be deallocated in the same module that allocated it.

I have 2 DLLs built with different C++ compilers (VS 6 and VS 2015). In the VS 2015 module, I have an exported function which returns a variable number of items (let's say of int type), so I need a variable-sized buffer.

I'm trying to automate as much as possible the use of the VS 2015 DLL from the VS 6 client, so I came up with this in the VS 2015 project:

/* dll.h */

#if EXPORTS  // EXPORTS is defined just in VC2015 project
#  define EXPORTED __declspec(dllexport)
#else
#  define EXPORTED __declspec(dllimport)
#endif

typedef int*(*Allocator_int__t)(size_t size);
int* allocator_int(size_t size);

#ifndef EXPORTS
inline int* allocator_int(size_t size){ return new int[size]; }
#endif

EXPORTED void cross_boundaries_int_buffer(int*& buffer, Allocator_int__t a = allocator_int);

and

/* dll.cpp */

EXPORTED void cross_boundaries_int_buffer(int*& buffer, Allocator_int__t a/* = allocator_int*/)
{
  buffer = a(10);
  buffer[0] = 0;
  /* ... */
  buffer[9] = 9;
}

This allows the VS 6 client DLL to do:

#include <dll.h>

void client_func()
{
  int *buffer = NULL;
  cross_boundaries_int_buffer(buffer); // default argument uses default allocator, **which is defined in client dll**
  /* use buffer elements as needed */
  delete[] buffer;
}

Is this a safe way to deal with the problem? Is there any better way?


texts:
I wanted to have a good review on my code I wrote today to read files in C++ line by line. The file can look something like this:

# Program configuration

network.port=30000 # The port the server uses
network.ip=127.0.0.1 # The IP the server uses

Notes:

Lines that start with # won't be processed
Lines that are empty won't be processed
All the data before the # on a line will be processed
The data will be split by =, before the = the key and after it the value

configuration.h:

#pragma once
#include <fstream>
#include <map>
#include <sstream>
class configuration
{
public:
    configuration(const char* path);
    ~configuration();
    std::string get_value(const char* key);
private:
    std::map<std::string, std::string> values;
    std::ifstream file;
};

configuration.cpp:

#include "configuration.h"
#include <Windows.h>
#include <iostream>
#include "logger.h"

configuration::configuration(const char* path)
{
    try
    {
        file.open(std::string(path), std::ios::in);
        values = std::map<std::string, std::string>();

        if (file.is_open())
        {
            std::string line;
            while (std::getline(file, line))
            {
                if (line.find("#") != 0 && !line.empty() && line.find("="))
                {
                    std::string part = line.substr(0, line.find("#"));
                    std::string key = part.substr(0, line.find("="));
                    std::string value = part.substr(line.find("=") + 1);
                    values.insert_or_assign(key, value);
                }
            }
        }
        else
        {
            logger::log_error("File %s is not open!", path);
        }
    }
    catch (const std::ios_base::failure& ex)
    {
        logger::log_error(ex.code().message().data());
    }
}

std::string configuration::get_value(const char* key)
{
    return values[key];
}

configuration::~configuration()
{
    file.close();
}

Usage would be:

configuration* config = new configuration("file.cnf");
config->get_value("network.port");


texts:
I'm basically looking for a general critique. I'm just starting out and would like to know if I should have done anything differently to make it faster or to make it much more readable. I don't know exactly what happened but I tried it with 14 iterations and it took about 3 minutes to complete. It crashed Unity at 15 and after a restart, completed 15 iterations in half a second. I have a feeling I should just cut the dragon list and add the vertices to the Line Renderer as they're generated but I feel like the way it is now might be easier to read.

Are there any more corners I could be cutting? I lost indentation on the body of the class when I pasted it into the codeblock.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HeighwayGenerator : MonoBehaviour {
private List<char> direction;
private List<Vector3> dragon;
private Stack <char> stack;
private LineRenderer line;
private float magnitude = 0.0f;

public int iterations = 4;

void Start () {
    direction = new List<char> ();
    stack = new Stack <char> ();
    dragon = new List<Vector3> ();
    line = GetComponent<LineRenderer> ();

    //generate turn list
    generateDirection (iterations);

    //set start direction. Vector3.forward is axis of rotation (0,0,1)
    transform.Rotate (Vector3.forward, (float)iterations * 45.0f);

    //find segment length of fully iterated list. magnitude of 0th iteration is 10 units from (-5,0) to (5,0)
    magnitude = 10.0f / (Mathf.Pow (Mathf.Sqrt (2), iterations));

    //generate vertices and fill dragon
    generateVertices ();

    //draw line
    line.SetVertexCount (dragon.Count);
    for (int i = 0; i < dragon.Count; i++) {
        line.SetPosition(i, dragon[i]);
    }
}

void generateDirection(int _iteration) {
    // Heighway dragon is generated from an initial direction and a list of turns.
    // This list is generated by:
    // 1.Pushing previous list into stack.
    // 2.Appending 'R'
    // 3.Appending reversed and 'negated?' previous list from stack.

    if (direction.Count == 0) {
        direction.Add ('R');
        _iteration -= 1;
    }
    if (_iteration <= 0) {
        return;
    }
    int _tempCount = direction.Count;
    direction.Add ('R');

    // Reverse and negate previous iteration
    for (int i = 0; i < _tempCount; i++) {
        stack.Push(direction[i]);
    }
    for (int i = 0; i < _tempCount; i++) {
        char _tempChar = stack.Pop ();
        if (_tempChar == 'R') {
            direction.Add ('L');
        }
        if (_tempChar == 'L') {
            direction.Add ('R');
        }
    }
    generateDirection (_iteration - 1);
}

void generateVertices() {
    //initial position
    dragon.Add (transform.position);
    //first translation
    transform.position += magnitude * transform.right;
    //second position
    dragon.Add (transform.position);

    // turn/translate/add -- transform.right is forward on XY plane. Vector3.forward is axis of rotation (0,0,1)
    for (int i = 0; i < direction.Count; i++) {
        if(direction[i] == 'R'){
            transform.Rotate (Vector3.forward, -90.0f);
            transform.position += magnitude * transform.right;
            dragon.Add (transform.position);
        }
        if (direction[i] == 'L') {
            transform.Rotate (Vector3.forward, 90.0f);
            transform.position += magnitude * transform.right;
            dragon.Add (transform.position);
        }
    }
}
}


texts: C# based IRC application (with WPF as UI)

texts:
I would like to know how I could improve my C# project. I'm not really sure whether or not I'm allowed to just post a link to the Google Code project here, so sorry if it's not allowed, I'll edit my question if someone tells me so.

http://code.google.com/p/simple-irc/source/browse/#svn%2Ftrunk

The reason I'm posting the entire project is because I would like to know if there's anything I can improve coding-wise (coding conventions I'm missing or misusing?), structural-wise (should anything be restructured, do I need to split it up in different classes etc?) and document-wise (how should I document my variables, methods, etc?).

Edit: Maybe I should explain a few things to save anyone who reads this some time. The project is split up in three "modules". The UI part of the application is in the "Source" folder, the core IRC library is in the "Library" folder and any plugins (that the UI module may load) are in the "Plugins" folder.

Edit2: I'll edit my question as a reply to the replies, this is a picture of the application: http://matthias.van-eeghem.com/data/simpleIRC.png

This is a small bit of code (as I apparently have to include that according to the FAQ):

/// <summary>
/// Called when the client receives the your hostName is event
/// </summary>
/// <param name="serverIndex">The server index</param>
/// <param name="hostName">The displayed hostName</param>
private void OnDisplayedHostReceived(int serverIndex, string hostName)
{
    string Output = "Your displayed hostName is: " + hostName + "\r";
    Core.ServerList[serverIndex].Logger.Log(Output);
    ServerWindow.List[serverIndex].SendText(Output);

    foreach (PluginInterface Plugin in MainWindow.PluginList) if(Plugin != null) Plugin.OnDisplayedHostReceived(serverIndex, hostName);
}

/// <summary>
/// Called when the server sends all the commands supported to the client
/// </summary>
/// <param name="serverIndex">The index of the server</param>
/// <param name="commandsSupported">The list of the commands supported</param>
private void OnCommandsSupportedReceived(int serverIndex, string commandsSupported)
{
    string Output = "Supported: " + commandsSupported + "\r";
    Core.ServerList[serverIndex].Logger.Log(Output);
    ServerWindow.List[serverIndex].SendText(Output);

    foreach (PluginInterface Plugin in MainWindow.PluginList) if(Plugin != null) Plugin.OnCommandsSupportedReceived(serverIndex, commandsSupported);
}

/// <summary>
/// Called when the client times out from the server
/// </summary>
/// <param name="serverIndex">The serverIndex</param>
private void OnClientTimeout(int serverIndex)
{
    Server Server = Core.ServerList[serverIndex];
    // Could not resolve hostName
    string Output = "* Lost connection to " + Server.Hostname + "\r";
    Core.ServerList[serverIndex].Logger.Log(Output);
    ServerWindow.List[serverIndex].SendText(Output);

    Output = "* Trying to reconnect..\r";
    Core.ServerList[serverIndex].Logger.Log(Output);
    ServerWindow.List[serverIndex].SendText(Output);

    // Loop through all channels
    for (int i = 0; i < Core.ChannelList.Count; i++)
    {
        Channel Channel = Core.ChannelList[i];
        if(Channel != null && Channel.Server != null)
        {
            if (Channel.Server.Index == serverIndex)
            {
                Output = "* Lost connection to " + Server.Hostname + "\r";
                Core.ServerList[serverIndex].Logger.Log(Output);
                ChannelWindow.List[i].SendText(Output);
            }
        }
    }

    // Try reconnecting
    Server.Connect();

    foreach (PluginInterface Plugin in MainWindow.PluginList) if(Plugin != null) Plugin.OnClientTimeout(serverIndex);
}

I'd love any feedback for this code specifically but I'd also appreciate any comments in regards to the entire project structure etc.


texts: Google Foobar Level 1 - Minion Task Schedule

texts:
I recently just stumbled upon a post regarding Google Foobar Level 2 and decided to try it out myself but starting from Level 1. The description and code are shown below.

Write a function called answer(data, n) that takes in a list of less
than 100 integers and a number n, and returns that same list but with all of the numbers that occur more than n times removed entirely. The returned list should retain the same ordering as the original list - you don't want to mix up those carefully planned shift rotations! For instance, if data was [5, 10, 15, 10, 7]
and n was 1, answer(data, n) would return the list [5, 15, 7] because 10 occurs
twice, and was thus removed from the list entirely.
Constrains:
Standard libraries are supported except for bz2, crypt, fcntl, mmap,
pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib.
Test Cases:
Inputs:
(int list) data = [1, 2, 3]
(int) n = 0
Output:
(int list) []
Inputs:
(int list) data = [1, 2, 2, 3, 3, 3, 4, 5, 5]
(int) n = 1
Output:
(int list) [1, 4]
Inputs:
(int list) data = [1, 2, 3]
(int) n = 6
Output:
(int list) [1, 2, 3]

Code:
def answer(data, n):
    if len(data) > 99:
        exit('List contains more than 100 elements')
    print('data:\n', data) #for error-checking

    count = dict()
    for i in data:
        count.setdefault(i, 0)
        count[i] += 1
    print('count:\n', count) #for error-checking

    for k, v in count.items():
        if v > n:
            for i in range(v):
                data.remove(k)

    return data

I decided to also test the code myself by using test cases and a new list containing random numbers
data1 = [1, 2, 3]
n1 = 0

data2 = [1, 2, 2, 3, 3, 3, 4, 5, 5]
n2 = 1

data3 = [1, 2, 3]
n3 = 6

data4 = [random.randint(1, 15) for i in range(0, 99)]
n4 = 6

ans1 = answer(data1, n1)
print('ans1:\n', ans1, '\n')

ans2 = answer(data2, n2)
print('ans2:\n', ans2, '\n')

ans3 = answer(data3, n3)
print('ans3:\n', ans3, '\n')

ans4 = answer(data4, n4)
print('ans4:\n', ans4, '\n')

Output:
data:
 [1, 2, 3]
count:
 {1: 1, 2: 1, 3: 1}
ans1:
 []

data:
 [1, 2, 2, 3, 3, 3, 4, 5, 5]
count:
 {1: 1, 2: 2, 3: 3, 4: 1, 5: 2}
ans2:
 [1, 4]

data:
 [1, 2, 3]
count:
 {1: 1, 2: 1, 3: 1}
ans3:
 [1, 2, 3]

data:
 [15, 9, 14, 10, 13, 8, 7, 2, 10, 3, 10, 11, 9, 9, 10, 7, 5, 11, 12, 14, 1, 5, 14, 15, 11, 2, 13, 2, 6, 1, 6, 8, 3, 15, 8, 6, 6, 13, 14, 14, 2, 14, 12, 6, 15, 4, 7, 5, 13, 8, 8, 4, 7, 15, 6, 1, 2, 11, 14, 6, 8, 4, 10, 4, 2, 9, 6, 14, 11, 12, 8, 15, 7, 15, 6, 1, 14, 8, 5, 14, 6, 10, 8, 1, 4, 15, 11, 9, 11, 10, 13, 13, 9, 7, 7, 4, 4, 12, 12]
count:
 {15: 8, 9: 6, 14: 10, 10: 7, 13: 6, 8: 9, 7: 7, 2: 6, 3: 2, 11: 7, 5: 4, 12: 5, 1: 5, 6: 10, 4: 7}
ans4:
 [9, 13, 2, 3, 9, 9, 5, 12, 1, 5, 2, 13, 2, 1, 3, 13, 2, 12, 5, 13, 1, 2, 2, 9, 12, 1, 5, 1, 9, 13, 13, 9, 12, 12]

Any input is appreciated. Thank you :-)


texts: For each element in a list, finding the element in a second list, that minimizes a distance function

texts:
Getting started:

For a given number x (say -5) I want to know which element of a list y (say [2, -6]), minimizes the value of distance function d (say the absolute value) which applied on x and all elements of y. My current solution to this problem is

x = -5
y = [2, -6]
def d(a1, a2):
    return abs(a1-a2) # more complicated in real application

min(y, key = lambda _: d(_, x)) # correctly returns -6

Next level: x is actually a list (say [1, 5, -10]), so I build a list comprehension on top my previous solution:

x = [1, 5, -10]
[min(y, key = lambda _: d(_, xindex)) for xindex in x] # correctly returns [2, 2, -6]

As this type of problem seems quite basic to me, I expect a simple solution for this problem (like argmin(list1=x, list2=y, function=d)). Here is a question where it is shown how a custom distance matrix can be build. But this alone (the step from the distance matrix to the argmins would also have to be done) seems too be more complicated than my code.

Overall my question is: is my code fine, or is there a nicer solution?


texts:
Code objective: Read a txt file, parse it into a matrix and count (and also print) the number of unique "islands" in the map formed by the matrix.

0 = Sea;
1 = Land;
Islands are orthogonal groups of land.

testfile.txt :

5 9
00000
00010
10000
01101
10000
00100
01001
10010
01101

Python code:

# Reads the file and creates the matrix map.
with open('testfile.txt') as f:
    w = int(f.read(1))
    f.read(1)
    h = int(f.read(1))
    f.read(1)
    map = [[2 for x in range(w)] for y in range(h)]
    for y in range(h):
        for x in range(w):
            map[y][x] = int(f.read(1))
        f.read(1)

# Swipes the matrix map after NEW land chunks.
def swipe():
    counter = 0
    for x in range(h):
        for y in range(w):
            if map[x][y] == 1:
                counter += 1
                landInSight(map, x, y, 99)
    print(counter)

# Recursive function to hide any land attached to a chunk already swiped.
def landInSight(m, h, w, c):
    if m[h][w] == 1:
        m[h][w] = c
        if w < len(m[0]) - 1: landInSight(m, h, w + 1, c)
        if h < len(m) - 1: landInSight(m, h + 1, w, c)
        if w > 0: landInSight(m, h, w - 1, c)
        if h > 0: landInSight(m, h - 1, w, c)

# Calls the swipe function.
swipe()

It is a very simple code, but I took way too long coding it. This is my first "program" using Python and although it works, it seems too rough. I am looking for any constructive inputs from Python people.


texts: Utility functions for processing tweets or texts

texts:
I have 4 functions that process texts or tweepy.models.Status object (this object stores text information, author information, retweets & likes information of a specific tweet).

The first function is filter_unique, which is used to exclude duplicate tweets from a list of tweets. The result is a generator.

The function clean_text, is used to remove punctuations, undefined characters, and whitespaces from a string.

The function split_texts has two inputs, the list of tweets/strings and the mode of the splits (naive=True means it only use text.split(), if set to False then it will clean the text first before splitting)

The function total_rts calculates the total number of retweets. string_inclusion='word' means that it will calculate the total retweets of tweets that contain 'word'.

Question: How to make this module more efficient, and perhaps advanced?

We can use the sample tweets testfile.npy which contains objects tweepy.models.Status.

import tweepy
import string

def filter_unique(tweets):

    uniques = set()

    for tweet in tweets:

        if not isinstance(tweet, tweepy.Status):
            raise TypeError('Each element must be of tweepy.Status object')

        try:
            tweet = tweet.retweeted_status
        except:
            pass

        if tweet.id not in uniques:

            uniques.add(tweet.id)

            yield tweet

def clean_text(text):

    punct = string.punctuation
    printable = string.printable
    whitespace = string.whitespace

    table = text.maketrans({key: None for key in printable})
    undef = set(text.translate(table))

    table = text.maketrans({key: None for key in undef})
    removed_undef = text.translate(table)

    table = text.maketrans({key: None for key in punct})
    cleaned = removed_undef.translate(table)

    table = text.maketrans({key: ' ' for key in whitespace})
    cleaned = cleaned.translate(table)

    return cleaned

def split_texts(texts, naive):

    if naive:
        for text in texts:
            yield text.split()
    else:
        for text in texts:
            yield clean_text(text).split()

def total_rts(tweets, string_inclusion = False, naive = True):

    result = 0

    if not string_inclusion :

        result = sum([tweet.retweet_count for tweet in tweets]);

    else:

        if naive:
            try:
                result = sum([tweet.retweet_count for tweet in tweets if string_inclusion in tweet.full_text.split()])
            except:
                result = sum([tweet.retweet_count for tweet in tweets if string_inclusion in tweet.text.split()])
        else:
            try:
                result = sum([tweet.retweet_count for tweet in tweets if string_inclusion in clean_text(tweet.full_text).split()])
            except:
                result = sum([tweet.retweet_count for tweet in tweets if string_inclusion in clean_text(tweet.text).split()])

    return result


texts: Encrypting a payload for transmission over HTTP. AES256 with PBKDF2

texts:
I've done some reading about implementing AES256 and deriving a key from a password. If I understand correctly:

I want to generate a new salt (for the key) and a new IV (for the encrypted message) for every new message.
It should also not be a problem sending the salt and the IV together with the message.

I decided to pack everything together in one byte array where the first 16 bytes is the salt, the next 16 bytes is the IV and the rest is the encrypted message. This with a key length of 256 bits and 20000 iterations for generating the key. I also encode the whole thing in Base64 for transmission.

Can this approach be improved? Knowing that I'm limited to 256 bytes for the complete message (salt+iv+message).

public class app {

    public static void main(String[] args) throws Exception {
        int iterations = 20000;
        int keyLength = 32;
        byte[] salt = getRandomBytes(16);
        byte[] iv = getRandomBytes(16);
        char[] password = "password_here".toCharArray();
        byte[] payload = "payload_here".getBytes();

        byte[] key = deriveKey(iterations, keyLength, salt, iv, password);
        byte[] encrypted = encrypt(iv, key, payload);
        byte[] bytes = concatBytes(salt, iv, encrypted);

        String output = Base64.encodeBytes(bytes);
        int outputLength = output.getBytes().length;
        System.out.println(outputLength + "\n" + output);
    }

    private static byte[] deriveKey(int iterations, int length, byte[] salt, byte[] iv, char[] password)
            throws InvalidKeySpecException, NoSuchAlgorithmException {
        PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, length * 8);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        byte[] key = skf.generateSecret(spec).getEncoded();
        return key;
    }

    private static byte[] getRandomBytes(int length) throws NoSuchAlgorithmException {
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        byte[] salt = new byte[length];
        sr.nextBytes(salt);
        return salt;
    }

    private static byte[] concatBytes(byte[]... arrays) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        for (byte[] array : arrays)
            outputStream.write(array);
        return outputStream.toByteArray();
    }

    private static byte[] encrypt(byte[] iv, byte[] key, byte[] text) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
            BadPaddingException {
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
        SecretKeySpec newKey = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
        return cipher.doFinal(text);
    }
}


texts:
I've created this thin wrapper class for a socket project that I'm working on.  Can someone review my class and give me some points on if I'm on right track?

#include <iostream>
#include <WinSock2.h>

const int MAX_RECV_LEN = 8096;
const int MAX_MSG_LEN= 1024;
const int PORT_NUM =  1200;

class Sokect
{

private:
    Sokect(){}
    void setSocketID(int socketFb){socketID = socketFb;}

    int portNum;
    int socketID;
    int blockFlag;
    int bindFlag;

public:
    Sokect(int);

    ~Sokect(){
        closesocket(socketID);
    }

    void setReuseAddr(int);
    void getReuseAddr(int);
    void setKeepAlive(int);
    void getKeepAlive(int);
    void setLingerOnOff(bool);
    void getLingerOnOff(bool);
    void setLingerSecond(int);
    void getLingerSecond(int);
    void setSocketBlockFlag(int);
    void getSocketBlockFlag(int);

    //size of send & Receive Buffer
    void  setSendBufferSize(int);
    void  getSendBufferSize(int);
    void  setReceiveBuffferSize(int);
    void  getReceiveBuffferSize(int);

    int getSocketID() {return socketID;}
    int getPortNum() {return portNum;}

    friend ostream& operator<<(ostream&,mySocket&);

    //get systems error
    void detectErrorOpenWinSocket(int*,string&);
    void detectErrorSetSocketOption(int*,string&);
    void detectErrorGetSocketOption(int*,string&);

};


texts:
I have a method I created which is a custom indexOf(). This code seems ugly to me so I was wondering if anyone could recommend a cleaner and perhaps faster implementation of my method.

public static int myIndexOf(char[] str, char[] substr, int index) {
    if (str == null) {
        throw new NullPointerException();
    }
    if (substr.length > str.length) {
        return -1;
    }
    if (substr.length == 0 && index >= str.length) {
        return str.length;
    }
    if (substr.length == 0 && index >= 0) {
        return index;
    }
    if (substr.length == 0 && index < 0) {
        return 0;
    }
    if (substr.length == 0 && str.length == 0 ) {
        return 0;
    }

    int[] found = boyerMoore(str, substr);
    for (int i : found) {
        if (i >= index) {
            return i;
        }
    }
    // can not found substring after index
    return -1;
}

// Don't necessarily need any modification for this method. But for those who are interested
Boyer Moore:

public static int[] boyerMoore(char[] str, char[] substr) {
    // index array of indexes of found substrings
    // initialize
    int indexCounter = 0;
    int[] subStringIndex = new int[str.length];
    for (int c = 0; c < str.length; c++) {
        subStringIndex[c] = -1;
    }
    // character set ASCII
    int characterSet = 256;
    // initialize
    int[] charSet = new int[characterSet];
    for (int a = 0; a < characterSet; a++) {
        charSet[a] = -1;
    }
    for (int b = 0; b < substr.length; b++) {
        charSet[substr[b]] = b;
    }

    int skip;
    for (int i = 0; i <= (str.length - substr.length); i += skip) {
        //System.out.print(i);
        skip = 0;
        for (int j = substr.length - 1; j >= 0; j--) {
            if (substr[j] != str[i + j]) {
                skip = Math.max(1, j - charSet[str[i + j]]);
                    break;
            }
        }

        if (skip == 0) {
            // increment next value in String
            skip++;
            // add found substring (beginning index)
            subStringIndex[indexCounter] = i;
            indexCounter++;
            // this last bit is used if Boyer Moore stops searching after first instance is found
            // return i;

        }
    }

    return  subStringIndex;
    // This bit is used when Boyer Moore is stops after the first instance in found
    // cannot be found
    // return str.length;
}


texts:
I had the following assignment for the Python 101 course I'm taking in my university.

'''
Modify the naive implementation (see below) of a sorting algorithm so that
the list to be sorted is modified *in place*.

Here is the idea:

1. Iterate over all indices i = 0, 1, 2, ... of the input list

2. Find the index j of the smallest element in the rest of the list (from i on)

3. Replace the elements at indices i and j

If we want to sort the list [5, 3, 1, 4, 8, 2, 7, 6], for instance, a run of
the algorithm would be:

i=0, j=2, list=[5, 3, 1, 4, 8, 2, 7, 6]
i=1, j=5, list=[1, 3, 5, 4, 8, 2, 7, 6]
i=2, j=5, list=[1, 2, 5, 4, 8, 3, 7, 6]
i=3, j=3, list=[1, 2, 3, 4, 8, 5, 7, 6]
i=4, j=5, list=[1, 2, 3, 4, 8, 5, 7, 6]
i=5, j=7, list=[1, 2, 3, 4, 5, 8, 7, 6]
i=6, j=6, list=[1, 2, 3, 4, 5, 6, 7, 8]
'''

In principle, the only two funcions I have to modify are "index_of_smallest_element" and "naive_sort_inplace".

def index_of_smallest_element(some_list):
    '''Returns the index of the smallest element in some_list'''
    guess=some_list[0]
    for element in some_list:
        if element < guess:
            guess=element
    return some_list.index(guess)

def naive_sort(some_list):
    sorted_list = []
    while len(some_list) > 0:
        # remove smallest element from some_list
        smallest_element = some_list.pop(index_of_smallest_element(some_list))
        # ... and append it to sorted_list
        sorted_list.append(smallest_element)
    return sorted_list

def naive_sort_inplace(some_list):
    def exchange(some_list,x,y):
        some_list[x],some_list[y]=some_list[y],some_list[x]

    for i in range(0,len(some_list)):
        remaining_list=some_list[i:(len(some_list)+1)]
        i_rest=index_of_smallest_element(remaining_list)
        j=i+i_rest
        print('i=',i,'j=',j,'list=',some_list)
        exchange(some_list,i,j)
    return some_list

if __name__ == '__main__':
    my_list = [5, 3, 1, 4, 8, 2, 7, 6]
    naive_sort_inplace(my_list)
    print('Test: ', my_list == [1, 2, 3, 4, 5, 6, 7, 8])

Although the code is fully functional, I'm wondering is there would be a better way to do it, especially the "naive_sort_inplace" function.


texts: Get K most frequent tokens from a token stream

texts:
This is an interview problem:

  Given a token stream, find the K most frequent tokens in real time (the order of these k tokens does not matter).

My approach:

Use a hash table (unordered_map in c++) to maintain the frequency of tokens.
Use a min heap to maintain the k most frequent tokens.
Update the hash table and the min heap dynamically.

I feel my code is not concise enough and I am not experienced using some new features in C++11. I would be very appreciative if anyone could any suggestions on the algorithm, coding style, object-oriented design, etc.

    class TokenCounter{
    public:
    void MaxKFrequent(int k) {
      std::unordered_map<std::string, TokenInfo::TokenInfoPtr> hash_table;
      std::deque<TokenInfo::TokenInfoPtr> min_heap;

      string token;
      while(std::cin >> token) {
        // update hashtable
        if(hash_table.find(token) == hash_table.end()) {
          hash_table[token] = TokenInfo::TokenInfoPtr(new TokenInfo(token));
        } else {
          hash_table[token]->increase_cnt();
        }

        // update min-heap
        TokenInfo::TokenInfoPtr& token_info = hash_table[token];
        // if already in the heap -> heaplify directly
        if(token_info->get_in_heap()) {
          std::make_heap(min_heap.begin(), min_heap.end(), TokenInfoCmp());
        } else {
        // if not in the heap
          if(min_heap.size() < k) {
          // if heap is not full -> insert into heap directly
            token_info->set_in_heap(true);
            min_heap.push_back(token_info);
            std::push_heap(min_heap.begin(), min_heap.end(), TokenInfoCmp());
          } else {
          // if heap is full
              // if count > heap top -> delete heap top & insert into heap
            if(token_info->get_cnt() > min_heap.front()->get_cnt()) {
              token_info->set_in_heap(true);
              min_heap.push_back(token_info);

              min_heap[0]->set_in_heap(false);
              min_heap.pop_front();

              std::make_heap(min_heap.begin(), min_heap.end(), TokenInfoCmp());
            }
          }
        }

        for(auto& token_ptr : min_heap) {
          std::cout << token_ptr->get_token() << " ";
        }
        std::cout << std::endl;
      }
    }

    private:
    class TokenInfo {
      public:
        using TokenInfoPtr = std::shared_ptr<TokenInfo>;
        TokenInfo(std::string token) : token_(token) {}

        int get_cnt() const {return cnt_;}
        void increase_cnt() {cnt_ += 1;}

        bool get_in_heap() const {return in_heap_;}
        void set_in_heap(bool in_heap) {in_heap_ = in_heap;}

        std::string get_token() const {return token_;}

      private:
        std::string token_;
        int cnt_ = 1;
        bool in_heap_ = false;
    };

    struct TokenInfoCmp {
      bool operator() (TokenInfo::TokenInfoPtr a, TokenInfo::TokenInfoPtr b) const {
        return a->get_cnt() > b->get_cnt();
      }
    };
  };


texts: Tkinter program that displays 28 numbers on the screen

texts:
I created a simple program that displays 28 numbers on the screen.

from Tkinter import *
import random

class Game(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent, background="white")
        self.parent = parent
        self.parent.title("Centered window")
        self.pack(fill=BOTH, expand=1)
        self.centerWindow()

    def centerWindow(self):

        w = 850
        h = 530

        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()

        x = (sw - w)/2
        y = (sh - h)/2
        self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))

def main():

    global tile_frame_column  # the labels that go on the screen of individual numbers
    tile_frame_column = {}

    global tiles_make_number  # dictionary of all the individual numbers
    tiles_make_number = {}

    global tile_frame_column_counter
    tile_frame_column_counter=0

    global tiles_make_number_counter
    tiles_make_number_counter=0

    global xpos
    xpos=35

    global ypos
    ypos=50

    tile_numbers = ['1', '2', '3', '4', '5', '6', '1', '2', '1', '2', '1', 'l', '1', '3', '4', '2', '1', '2', '1', '1', '2', '1', '1', '6', '4', '3', '2', '2']

    root = Tk()
    ex = Game(root)

    def add_number_to_screen():
        global tile_frame_column_counter
        global tiles_make_number_counter
        global tile_frame_column
        global xpos
        global ypos

        if not tile_numbers:
            return

        rand = random.choice(tile_numbers)
        tile_frame_column[tile_frame_column_counter] = Button(root, text=rand, font="Helvetica 16 bold")

        tile_frame_column[tile_frame_column_counter].place(x=xpos, y=ypos)
        tile_numbers.remove(rand)  # remove that tile from list of tiles
        xpos += 80
        if (len(tiles_make_number) % 7 == 0) & (len(tiles_make_number) > 0):
            xpos = 35
            ypos += 80
            tile_frame_column[tile_frame_column_counter].place(x=xpos, y=ypos)
            xpos += 80
        tiles_make_number[tiles_make_number_counter] = rand
        tile_frame_column_counter += 1
        tiles_make_number_counter += 1
        root.after(10, add_number_to_screen)

    root.after(10, add_number_to_screen)
    root.mainloop()

if __name__ == '__main__':
    main()

I want the main() function to be initialized, though. Whenever a game object is created I want it to automatically add the main function -- meaning add numbers to the screen. I think the way my code is now, when a game object is created it only creates the screen.

Any ideas?


texts:
I'm working on a Meteor application which integrates a user's contacts from external sources (Google in the case of this example). I'm currently writing the server side code to retrieve this data and send it to the client.

I figured using promises to do this made sense due to the asynchronous manner of requests. So I have getContacts which sends the request to the Google API, and processContacts which processes and formats the response data:

getContacts = function (accessToken) {
  return new Promise(function (resolve) {
    httpRequest.get({
      url: 'https://www.google.com/m8/feeds/contacts/default/full?alt=json',
      auth: {
        'bearer': accessToken
      },
      headers: {
        'GData-Version': 3.0
      },
    }, function (err, res, body) {
      resolve(body);
    });
  });
}

processContacts = function(googleContacts) {
  return new Promise(function(resolve) {
    const contacts    = JSON.parse(googleContacts).feed.entry;
    const allContacts = [];
    const groupedContacts = { conflicts: [], new: [] };
    ...
    ...
    resolve(groupedContacts);
  });
}

I have a Meteor method google.contacts.import which is called synchronously from the client, and because all of this is asynchronous I'm using a future to force the client to wait for the call to finish:

Meteor.methods({
  'google.integration.import'(orgId) {
    check(orgId, String);

    const user = Users.getOne(Meteor.userId());
    let fut = new Future();

    getContacts(user.services.google.accessToken)
    .then(processContacts)
    .then(function(contacts){
      fut.return(contacts);
    })
    .catch(function(err){
      console.log('error! :' + err);
    });

    return fut.wait();
  },
})

This all works fine, but it seems...messy or somewhat convoluted. I'm relatively new to Javascript and ES6 in particular so I feel I could definitely improve this. Am I correct to be using promises here? I guess they probably aren't necessary in the case of processContacts. I also realize this is lacking in terms of error checking/reporting.

Any help or guidance is appreciated!


texts:
I already know how to merge two linked list, but I needed to code a function that'll merge three already sorted linked lists into one.

I wrote a code and it is working fine, it takes care of null lists and so. I just don't know if it's efficient enough. What do you think? How can I improve this code?

typedef struct clist {
int num;
struct clist *next;
} clist;

clist *cmerge(clist *c1, clist *c2, clist *c3) {
// Input parameters are pointers to the first items of the lists, out parameter is the pointer to the first item of the merged list
clist *ret = NULL, *curr = NULL, *last = NULL;
int i=1,n;
while(i) {
    i = 0;
    n = (c1) ? c1->num : (c2) ? c2->num : (c3) ? c3->num : 0;
    if(c1 && c1->num <= n) {
        n = c1->num;
        i = 1;
    }
    if(c2 && c2->num <= n) {
        n = c2->num;
        i = 2;
    }
    if(c3 && c3->num <= n) {
        n = c3->num;
        i = 3;
    }
    switch(i) {
    case 1:
        curr = c1;
        c1 = c1->next;
        break;
    case 2:
        curr = c2;
        c2 = c2->next;
        break;
    case 3:
        curr = c3;
        c3 = c3->next;
        break;
    default :
        curr = NULL;
    }
    if (!ret) ret = curr;
    else last->next = curr;
    last = curr;
}
return ret;
}


texts:
I've got a small tomcat web application that's used to receive customer data from an iPad app. After receiving those information they will be saved on a local MySQL database and then sent to an SAP system.

I'm focusing on my "concept" of handling database connections since this is my first ever productive web app that makes intense use of the database. I was wondering if there is something wrong in the way I talk with the database or if this might be prone to problems in future.

I have a class Database that creates the connection to the database and then provides a PreparedStatement through a method getPreparedStatement(String sql) that can be filled with the SQL query. It looks like this:

public class Database {
    private Connection connection = null;

    private Config config = ConfigurationUtils.config;

    public Database() {
        try {
            // Load the MySQL driver
            Class.forName("com.mysql.jdbc.Driver");

            // Create the database connection
            this.connection = DriverManager.getConnection("jdbc:mysql://" + config.getDatabase().getHost() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabaseName() + "?user=" + config.getDatabase().getUser() + "&password=" + config.getDatabase().getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PreparedStatement getPreparedStatement(String sql) throws SQLException {
        return this.connection.prepareStatement(sql);
    }

    public void close() throws SQLException {
        this.connection.close();
    }
}

This Database class is used throughout the whole application (nearly every other class uses the Database class, some classes might even use them twice because multiple SQL queries have to be executed). The following example is the code of a method insertDealer() that's used to fill the database with master data about our dealerships:

    private void insertDealer(int dealer, String name1, String name2, String city, int zip) throws SQLException {
        String sql = "INSERT INTO dealer (id, name1, name2, city, zip) VALUES (?, ?, ?, ?, ?)";

        Database database = new Database();
        PreparedStatement preparedStatement = database.getPreparedStatement(sql);

        try {
            preparedStatement.setInt(1, dealer);
            preparedStatement.setString(2, name1);
            preparedStatement.setString(3, name2);
            preparedStatement.setString(4, city);
            preparedStatement.setInt(5, zip);

            preparedStatement.executeUpdate();
        } finally {
            preparedStatement.close();
            database.close();
        }
    }

Would this kind of Database access count as bad behaviour? Everything works actually like it should, I'm not experiencing any strange problems. Only thing I noticed that is when I stop my tomcat web app, I get this entry in the tomcat server log:

/webapp/statistics##1.0 appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.


texts: C# Generic Repository and Unit of Work Implementation for Northwind

texts:
I have created 3 projects to implement layered architecture design mainly the
 - Presentation Layer (Web)
 - Domain Model Layer
 - Infrastructure Layer

For simplicity, I have only two domain model entities: Customer.cs and Category.cs. In my domain model layer, I also put my repository interface (both custom and generic).

In Infrastructure layer, I have the dbcontext, Unit of Work (including its interface) and repository classes (which implement repository interface).

Here is my generic repository interface

public interface IRepositoryGeneric<TEntity> where TEntity : class
{
    // CRD
    void Add(TEntity entity);
    void Remove(TEntity entity);
    TEntity Get(object Id);
    IEnumerable<TEntity> GetAll();

    void Save();
}

Generic Repository class

public class RepositoryGeneric<T> : IRepositoryGeneric<T> where T : class
{
    protected readonly AppDbContext db;
    public RepositoryGeneric(AppDbContext db)
    {
        this.db = db;
    }

    public void Add(T entity)
    {
        throw new NotImplementedException();
    }

    public T Get(object Id)
    {
        throw new NotImplementedException();
    }

    public IEnumerable<T> GetAll()
    {
        return db.Set<T>().ToList();
    }

    public void Remove(T entity)
    {
        throw new NotImplementedException();
    }

    public void Save()
    {
        throw new NotImplementedException();
    }
}

Unit of Work Interface

public interface IUnitOfWork : IDisposable
{
    IRepositoryCustomer Customers { get; }
    IRepositoryCategory Categories { get; }

    int SaveChanges();
}

Unit of Work class

public class UnitOfWork : IUnitOfWork
{
    private readonly AppDbContext db;
    public UnitOfWork()
    {
        db = new AppDbContext();
    }

    private IRepositoryCustomer _Customers;
    public IRepositoryCustomer Customers
    {
        get
        {
            if (this._Customers == null)
            {
                this._Customers = new RepositoryCustomer(db);
            }
            return this._Customers;
        }
    }

    private IRepositoryCategory _Categories;
    public IRepositoryCategory Categories
    {
        get
        {
            if (_Categories == null)
            {
                _Categories = new RepositoryCategory(db);
            }
            return _Categories;
        }
    }

    public void Dispose()
    {
        db.Dispose();
    }

    public int SaveChanges()
    {
        return db.SaveChanges();
    }
}

Yeah, I have yet to add Application layer, which is the Web API layer.

Any comments on the structure of the whole solution? Any comments are welcome.

My whole solution source is available at my github - https://github.com/ngaisteve1/LayeredArchitectureDesignCSharp


texts: Pythonic way to count if a value is 'NaN' in a dictionary of dictionaries

texts:
I have a dictionary of dictionaries with several values. I want to count all the entries that do not contain 'NaN' (Not a Number in a string).

for each in enron_data:
    if enron_data[each]["salary"] !='NaN':
        counter += 1

    if enron_data[each]['email_address']!='NaN':
        valid_email +=1

I'm not familiar with list comprehensions, but I'm pretty sure there's might be a more pythonic way to achieve this.

Can anyone share pythonic advice?

Thank you


texts:
I have been working a number pyramid program. This is a question from Y. Daniel Laing's Introduction to programming using Python (which is based on Python 3.2). The question is from chapter 5, and it is number 5.19 in programming exercises portion of the chapter.  The question is stated as follows:

  Write a program that prompts the user to enter an integer from 1 to 15
  and displays it as the following sample run:

I don't have the image, but it is something like this:

  Enter the number of lines: 3

                          1
                       2  1  2
                    3  2  1  2  3

The solution I have come up with is this:

p = eval(input("Enter the number of lines: "))

num = 0
y = 1

while num < p:
    y = num

    line = str(y+1) + "  "

while y >= 1:

    y -= 1
    line += str(y+1) + "  "

while  y < num:

    line += str(y + 2) + "  "

    y +=1

print(format(line, "^80s"))

num +=1

If anyone has a better solution that would be simpler, please let me know.


texts: Minimum priority queue using Singly Linked LIst

texts:
I have written this code for Priority Queue using Singly Linked List. I want to improve this code. Is there any other method to write program for priority queue?

#include <iostream>

template<typename T>
class priorityQueue
{
  struct Node
  {
    int priority;
    T info;
    Node* next;
    Node(T item) : info(item), next(nullptr) {}
  };
  Node* front;

public:
  priorityQueue() : front(nullptr) {}
  ~priorityQueue()
  {
    Node *tmp = nullptr;
    while (front){
      tmp   = front;
      front = front->next;
      delete tmp;
    }
  front = nullptr;
  }

  priorityQueue(const priorityQueue<T> & pq)     = delete;
  priorityQueue& operator=(priorityQueue const&) = delete;

  void insert(T item, int priority)
  {
    Node* tmp;
    Node* node     = new Node(item);
    node->priority = priority;

    if(front == nullptr || priority < front->priority)
    {
      node->next = front;
      front = node;
    }
    else
    {
      tmp = front;
      while(tmp->next != nullptr && tmp->next->priority <= priority)
        tmp = tmp->next;

      node->next  = tmp->next;
      tmp->next   = node;
    }
  }

  void deleteItem(T item)
  {
    Node* find = search(item);
    Node* node = front;
    if(node == find)
      front = node->next;
    else
    {
      while(find != nullptr)
      {
        if(node->next == find)
        {
          node->next = find->next;
          delete find;
          return;
        }
        node = node->next;
      }
    }
  }

  friend std::ostream & operator<<(std::ostream & os, const priorityQueue<T> & pq)
  {
      pq.display(os);
      return os;
  }

private:
  Node *search(T item)
  {
    Node *node = front;
    while(node != nullptr)
    {
    if(node->info == item)
      return node;
    node = node->next;
    }
    std::cerr << "No such element in the list \n";
    return nullptr;
   }

   void display(std::ostream& out = std::cout) const
   {
     Node *node = front;
     out << "\nItem \tPriority \n";
     while(node != nullptr)
     {
       out << node->info << "\t" << node->priority <<"\n";
       node = node->next;
     }
    }
};

int main()
{
  priorityQueue<std::string> pq1;
  pq1.insert("code", 1);
  pq1.insert("sleep", 3);
  pq1.insert("food", 2);
  pq1.insert("date", 4);
  std::cout << pq1;
}