texts: NodeJS script to dump data from Redis to Elasticsearch

texts:
I have about a million records in Redis which I want to dump into Elasticsearch periodically. I just want to make sure that my script is decent enough in terms of speed and no memory leaks.

'use strict';

const redis = require('redis');
const bluebird = require('bluebird');
const request = require('request');
const elasticsearch = require('elasticsearch');
const fs = require('fs');
const _ = require('lodash');
const async = require('async');
const sh = require('shorthash');
const sleep = require('sleep');
const config = require('../config');

bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);

let client = redis.createClient({
  host: config.redis.url,
  port: config.redis.port
});

let ES = elasticsearch.Client({
  host: config.elasticsearch.url,
  requestTimeout: 30000000
});

var keys = fs.readFileSync('no-keys').toString().split('\n');
keys = keys.filter((e) => e);

let chunkedKeys = _.chunk(keys, 1000);
console.log('We have ' + chunkedKeys.length + ' keys');

_.each(chunkedKeys, (chunkedKey) => {
  client.mget(chunkedKey, (mgetError, replies) => {
    if (mgetError) {
      console.error(mgetError);
    }
    console.log('MGET complete from Redis');
    console.log('We have ' + replies.length + ' documents');
    async.mapLimit(replies, 5, (reply, callback) => {
      try {
        let content = JSON.parse(reply);
        let k = sh.unique(content.url);

        let body = [{index: {_index: config.elasticsearch.index, _type: 'article', _id: k, _timestamp: (new Date()).toISOString() }}];

        body.push(content);
        callback(null, body);
      } catch(e) {
        console.error(e);
        callback(e, []);
      }
    }, (err, results) => {
      if(err) {
        console.log(err);
      }
      let mergedResult = _.flatten(results.filter((e) => e));

      console.log('Export complete with ' + mergedResult.length);

      ES.bulk({body: mergedResult}, () => {
        console.log('Import complete');
      });
    });
  });
});


texts:
I need to find the substrings within my array. If I have an array: ["abc", "abcd", "abcde", "xyz"], my method should return the array members: abc, abcd, abcde as each is a substring or a superstring of the other, but it should exclude "xyz" as it is not related to other strings in any way.

function find_substrings(arr) {
    var res = [];
    for (var i=0; i<arr.length; i++) {
        for (var j=0; j<arr.length; j++) {
            if (i !== j && (arr[i].indexOf(arr[j]) > -1 || arr[j].indexOf(arr[i]) > -1)) {
                res.push(arr[i]);
                break;
            }
        }
    }
    return res;
}
var arr = ["abc", "abcd", "abcde", "xyz"];
console.log(find_substrings(arr));

Here my code is of \$O(n^2)\$ complexity, as it's iterating twice over the complete array. Is there any optimal solution?


texts: Simplify complex hash-table manipulations in Common Lisp

texts:
I'm trying to write a simple triplestore in Common Lisp that will store triples in the form subject-predicate-object. The code is inspired by the book "Programming the Semantic Web".

(defvar triplestore-graph (make-hash-table))

(defun triplestore-init ()
  (puthash :spo (make-hash-table) triplestore-graph)
  (puthash :pos (make-hash-table) triplestore-graph)
  (puthash :osp (make-hash-table) triplestore-graph))

(defun triplestore-add (s p o)
  (triplestore-add-to-index :spo s p o)
  (triplestore-add-to-index :pos p o s)
  (triplestore-add-to-index :osp o s p))

(defun triplestore-add-to-index (index a b c)
  (let ((index (gethash index triplestore-graph)))
    (cond ((not (gethash a index))
           (let ((tmp (make-hash-table)))
             (puthash a
                      (progn
                        (puthash b
                                 (list c)
                                 tmp)
                        tmp)
                      index)))
          ((not (gethash b (gethash a index)))
           (puthash b
                    (list c)
                    (gethash a index)))
          (t
           (nconc (list c)
                  (gethash b
                           (gethash a index)))))))

Here (puthash k v table) is defined as (setf (gethash k table) v). The code above is roughly equivalent to the Python code:

graph = {}

def init(graph):
    graph['_spo'] = {}
    graph['_pos'] = {}
    graph['_osp'] = {}

def add(graph, (s, p, o)):
    _addToIndex(graph['_spo'], s, p, o)
    _addToIndex(graph['_pos'], p, o, s)
    _addToIndex(graph['_osp'], o, s, p)

def _addToIndex(index, a, b, c):
    if a not in index:
        index[a] = {b: set([c])}
    elif b not in index[a]:
        index[a][b] = set([c])
    else:
        index[a][b].add(c)

How can i improve readability and elegantness of the Common Lisp code? I'm mainly concerned that operations with hash-tables are verbose comparing to Python's equivalents. Are there macros for hash-table handling, for example?


texts: Is there a way to shorten a set of conditionals like this?

texts:
I have a long set of IF statements that basically set a null object Value to 0, I feel that because i am doing the same action each time their has to be a simple way to make this allot shorter. It just looks like something I wouldn't normally see in code.

My code looks like this:

if ($data->sl1 === NULL){ $data->sl1 = "0";}
if ($data->sl2 === NULL){ $data->sl2 = "0";}
if ($data->sl3 === NULL){ $data->sl3 = "0";}
if ($data->sl4 === NULL){ $data->sl4 = "0";}
if ($data->sl5 === NULL){ $data->sl5 = "0";}
if ($data->sl6 === NULL){ $data->sl6 = "0";}
if ($data->sl7 === NULL){ $data->sl7 = "0";}
if ($data->sl8 === NULL){ $data->sl8 = "0";}
if ($data->sl9 === NULL){ $data->sl9 = "0";}
if ($data->sl10 === NULL){ $data->sl10 = "0";}
if ($data->sl11 === NULL){ $data->sl11 = "0";}
if ($data->sl12 === NULL){ $data->sl12 = "0";}

if ($data->sn1 === NULL){ $data->sn1 = "0";}
if ($data->sn2 === NULL){ $data->sn2 = "0";}
if ($data->sn3 === NULL){ $data->sn3 = "0";}
if ($data->sn4 === NULL){ $data->sn4 = "0";}
if ($data->sn5 === NULL){ $data->sn5 = "0";}
if ($data->sn6 === NULL){ $data->sn6 = "0";}
if ($data->sn7 === NULL){ $data->sn7 = "0";}
if ($data->sn8 === NULL){ $data->sn8 = "0";}
if ($data->sn9 === NULL){ $data->sn9 = "0";}
if ($data->sn10 === NULL){ $data->sn10 = "0";}
if ($data->sn11 === NULL){ $data->sn11 = "0";}
if ($data->sn12 === NULL){ $data->sn12 = "0";}

if ($data->sr1 === NULL){ $data->sr1 = "0";}
if ($data->sr2 === NULL){ $data->sr2 = "0";}
if ($data->sr3 === NULL){ $data->sr3 = "0";}
if ($data->sr4 === NULL){ $data->sr4 = "0";}
if ($data->sr5 === NULL){ $data->sr5 = "0";}
if ($data->sr6 === NULL){ $data->sr6 = "0";}
if ($data->sr7 === NULL){ $data->sr7 = "0";}
if ($data->sr8 === NULL){ $data->sr8 = "0";}
if ($data->sr9 === NULL){ $data->sr9 = "0";}
if ($data->sr10 === NULL){ $data->sr10 = "0";}
if ($data->sr11 === NULL){ $data->sr11 = "0";}
if ($data->sr12 === NULL){ $data->sr12 = "0";}

One way I know could work would simply be to iterate through the entire object with a simple foreach like this:

foreach ($data as $item) {
    if ($item === NULL){ $item = "0";}
}

But let's say I don't want to change every single null object to 0.  Do I have any other option?

I am using PHP in my example, but I am curious to hear answers for other languages as well.


texts:
I have created this method to do all my WebRequests.

Generally my question is, if this could be improved in any way.
As a side question, I would like to know if it be better to create methods for each type (PUT, POST, GET, UPDATE, DELETE) of a request separately?

    /// <summary>
    /// Test-Method
    /// </summary>
    /// <param name="method">Json Content String</param>
    /// <param name="postData"></param>
    public static T CreateRequest<T>(string url, ICredentials cred, string method, string postData = null) where T : class
    {
        string responseJson = string.Empty;
        try
        {
            //Do request:
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = method.ToUpper();//Just in case ToUpper();
            request.ContentType = "application/json";

            if ((method == "PUT" || method == "POST") && postData != null)
            {
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(postData);
                    streamWriter.Flush();
                }
            }

            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream()))
            {
                responseJson = reader.ReadToEnd();
            }
        }
        catch (Exception ex)
        {
            Debug.Fail($"WebRequest failed: {ex.Message}");
            return null;
        }

        try
        {
            return JsonConvert.DeserializeObject<T>(responseJson);
        }
        catch (Exception ex)
        {
            Debug.Fail($"Error parsing JSON: {ex.Message}");
            return null;
        }
    }

Note:

credentials excluded for this purpose
don't focus on exceptionhandling- I left that out too


texts: Improve performance by excluding multiple conditions and List

texts:
This method works correctly and verifies whether a user has permissions to view or edit the first parameter (value). I think it is not the best implementation, because there are too many conditions and the List object isn't necessary. How to improve it?

protected override Exception GetValidationException( object value, string name )
{
  List<Entity> entities = value is int ? new List<Entity>( 1 ) { EntityCache.Entity.GetEntity( (int) value ) } :
                          value is Guid ? new List<Entity>( 1 ) { EntityCache.Entity.GetEntityByGuid( (Guid) value ) } :
                          value is int[] ? (value as int[]).Select( i => EntityCache.Entity.GetEntity( i ) ).ToList() :
                          value is Entity ? new List<Entity>( 1 ) { value as Entity } :
                          value is Entity[] ? (value as Entity[]).ToList() :
                          null;

  if( entities == null )
    throw new ArgumentException( string.Format( RI.UnknownArgumentType, name, value, value.GetType().Name ) );

  bool hasPermission = (from one in entities
                        select EntityPermissionCache.PermissionChecker( one.Id, one.EntityOrganizationId ) &&
                               _checkEditInsteadOfView ? EntityTypePermissionCache.PermissionChecker( true, one.TypeId ) : true)
                       .All( a => a );

  return hasPermission ? null : new AccessViolationException( RI.AccessDenied );
}


texts: Intentional performance degradation through cache and branch-prediction misses

texts:
I am experimenting with performance evaluation. From the hardware architecture point of view, two main factors are cache misses and incorrect prediction of conditional branches which can degrade the performance of an application.

I have considered a small application for each case:

Cache Misses:

In this application, I intentionally want to write such a piece of code that would cause a worst case scenario w.r.t. cache (i.e., induce cache misses). A cache miss can be a read miss or a write miss, so I have written the code for both.

Read Misses:

     int n = 32767;
     int arr[n];

     for (int i = 0; i < n ; i++) {
         int ri = rand() % n;
         int var=arr[ri];
     }

Write Misses:

     int n = 32767;
     int arr[n];

     for (int i = 0; i < n ; i++) {
         int ri = rand() % n;
         arr[ri] = ri;
     }

Branch Incorrect Predictions:

Here, I intentionally want to write such a piece of code that would cause a worst case scenario w.r.t. branch prediction (i.e., induce incorrect predictions). So, it will make the branch predictor fail badly. The code is as following: Source

    const unsigned arraySize = 1024;
    int data[arraySize];

    for (unsigned c = 0; c < arraySize; ++c){
        data[c] = std::rand() % 256;
    }
    long sum = 0;
    for (int i = 0; i < 1024; ++i) {
         // Primary loop
         for (int c = 0; c < arraySize; ++c) {
             if (data[c] >= 128)
                 sum += data[c];
         }
    }

My question:

  Can I make any improvements to these source codes?

(Since a flaw left at this level can make all my experimental results flawed.)

  What are other possible codes that would have similar behavior(cache misses and branch incorrect predictions)?

(Maybe, a couple of other possible ways to do the same thing but with different apparent code.)

  Last but not least; Is there any other potential factor (other than cache misses and branch incorrect predictions) that can cause such a huge performance degradation?

An example source code to demonstrate its effect on the performance.

Note: With regard to the third question, I can name factors e.g., interrupts, context switches etc., however, I am considering real-time paradigm where such events would presumably be controlled.

P.S. I know three different question should be asked in three different posts, nonetheless, the second question is an addendum to the first question and the third question alone will be dubious without the whole context.

Thanks.

EDIT: I am using gem5-simulator in my experiments. Just out of my mind, based on my previous studies and experiences, a brief description of the simulator is: It is a cycle-accurate simulator with the facility to configure customized platforms. The execution traces provided by this simulator does include the exact counts on most of the expected statistics (e.g., cache misses, branch incorrect predicted etc). The generation of these traces does not cause overhead on the actual execution of an application. It also provides overhead free instrumentation facility to added further counters in the traces as per user needs.

Nevertheless, @vnp: Adjoining thanks, I am getting the counts and considering the cache configurations.


texts: Removing all stopwords from a list of words

texts:
What is the fastest Pythonic way to remove all stopwords from a list of words in a document? Right now I am using a list comprehension that contains a for loop.

from nltk.corpus import stopwords

''' Push stopwords to a list '''
stop = stopwords.words('english')
Document = ' Some huge text .......................... '
''' Tokenize the doc '''
words = nltk.word_tokenize(Document)
''' Comparing two lists '''
stopwordsfree_words = [word for word in words if word not in stop]

Is there a faster way to do this?


texts: Path from the root to a given node in binary tree

texts:
Based on the solution provided in this question , I have written non-recursive Java code to find the path form to a given element in a binary tree.  This code is working for all the elements except root.

Is this the correct implementation of the algorithm provided?
Please check and suggest the required changes for the code to look more elegant.

public void findPathFromRoot(Node rootNode,int key)
{
    Node temp = rootNode;
    Stack<Node> nodeStack = new Stack<Node>();
    nodeStack.push(rootNode);
    boolean present = true;

    Node topNode = rootNode;
    Node topNodeLeft,topNodeRight;

    while(true)
    {
        //three temp variables for top,left of top,right of top
        topNode = nodeStack.peek();
        topNodeLeft = topNode.lnode;
        topNodeRight = topNode.rnode;

    //if left of top is not null, push to stack. Check if the push values is equla to the required key
    //If it is the require element break
    if(topNodeLeft != null )
    {
        nodeStack.push(topNodeLeft);
        if(topNodeLeft.value == key)
            break;
    }

    //if right of top is not null, push to stack. Check if the push values is equal to the required key
    //If it is the require element break
    else if(topNodeRight != null)
    {
        nodeStack.push(topNodeRight);
        if(topNodeRight.value == key)
            break;
    }

    //If both the childs are null(ie leaf node) , pop the elements in stack, till the top node has some right child.
    else
    {
        Node lastPoppedNode;

        //pop the elements in stack, till the top node has some right child.
        do{
            lastPoppedNode = nodeStack.pop();
            topNode = nodeStack.peek();
        }while(topNode.rnode==null);

        //Now the top node would be the node with some right child
         topNodeRight = topNode.rnode;

         //Push that right child
        nodeStack.push(topNodeRight);

        //Check if the push values is equal to the required key
        //If it is the require element break
        if(topNodeRight.value == key )
        {
            break;
        }

        //To terminate the loop if no element is found....
        //it checks if the popped element is getting added again
        if( lastPoppedNode == topNodeRight)
        {
            present = false;
            break;
        }
    }
    }

    //Printing based on the boolean variable.
    if(present)
    {
    for(Node tempNode : nodeStack)
    {
        System.out.println(tempNode.value);
    }
    }
    else
    {
        System.out.println("Element is not present in the tree");
    }

}


texts: correctGuess() and incorrectGuess() functions for a quiz

texts:
In my JavaScript quiz, I have two functions below which are not DRY. What i want to do is to cut it, so everything below var = text would be used only once not twice.

My concept is to enclose these two functions in bigger function (e.g. guess()) and keep the trimmed correctGuess() and incorrectGuess() within it.

Now here's the question: how can I call such nested function as described above from outside scope. I was thinking about something like: guess().correctGuess() which is obviously wrong but I wanted to share a concept.

Additionally, when e.g. correctGuess() would be called, is rest of the commands within our main guess() function would be executed?

Fiddle with full code

function correctGuess(i) {

    totalScore++;
    questionNumber++;

    var text = "Correct!";

    var updatePage = ['
' +
        '
' + text + '
' +
        '
Total Score: ' + totalScore + '

'
    ];

    mainContent[html](updatePage);

    $('#answerDiv')[fadeIn]("slow");
    $('#answerDiv').append('<button id="nextButton">Next Question</button>');

    $('#nextButton').on('click', function() {
        if (questionNumber == allQuestions.length && totalScore <= 4) {
            results()
        } else {
            question(questionNumber)
        }
    })
};

function incorrectGuess(i) {
    totalScore--;
    questionNumber++;

    var text = "Wrong!";

    var updatePage = ['
' +
        '
' + text + '
' +
        '
Total Score: ' + totalScore + '

'
    ];

    mainContent[html](updatePage);

    $('#answerDiv')[fadeIn]("slow");
    $('#answerDiv').append('<button id="nextButton">Next Question</button>');

    $('#nextButton').on('click', function() {
        if (questionNumber == allQuestions.length && totalScore <= 4) {
            results();
        } else {
            question(questionNumber);
        }

    });

};


texts: Iterating an Array Object & Assigning the Attributes Values to those that Match the Condition

texts:
Is there any approaches to reduce the below coding snippet in Angular 2+ with few lines of code by making use of ECMA Script or by some other ways
this.testCenterAccomodations.forEach((x: { utilityCode: string; isChecked: boolean; isSelected: boolean; }) => {
    if (x.utilityCode === 'PSDQRA') {
        if (this.voiceOverLanguageChecked) {
            x.isChecked = true;
            x.isSelected = true;
        } else {
            x.isChecked = false;
            x.isSelected = false;
        }
    }
});

Looping an Array of objects, we are trying to find the object with the column named utilityCode having PSDQRA value and setting those particular object's properties of isChecked & isSelected to be true  iff, this.voiceOverLanguageChecked boolean value is true, otherwise set those properties to false.
The above snippets is simpler enough to read & understand. But is it possible for someone to achieve the same piece of logic with few lines of code by making use of map, filter, reduce, lambda and rest..


texts: Movie search layout tweaks with cookie persistence

texts:
This works as intended, but was wondering if there is a more efficient way of coding this.

if (window.location.href.indexOf('/search/') >= 0) {
    switch ($.cookie("Layout")) {
        case "Poster":
            $("link:first").attr("href", "../css/list.css");
            break;
        case "Description":
            $("link:first").attr("href", "../css/desc.css");
    }
    $(function () {
        $(".Poster").click(function () {
            $("link:first").attr("href", "../css/list.css");
            $.cookie("Movie_Layout", "Poster", {
                expires: 365,
                path: '/media/search',
                secure: true
            });
            $('.content-genre, .searchresults').hide().delay(75).fadeIn(275);
            window.scrollTo(0, 0);
        });
        $(".Description").click(function () {
            $("link:first").attr("href", "../css/desc.css");
            $.cookie("Movie_Layout", "Description", {
                expires: 365,
                path: '/media/search',
                secure: true
            });
            $('.content-genre, .searchresults').hide().delay(75).fadeIn(275);
            window.scrollTo(0, 0);
        });
    });
} else {
    switch ($.cookie("Layout")) {
        case "Poster":
            $("link:first").attr("href", "css/list.css");
            break;
        case "Description":
            $("link:first").attr("href", "css/desc.css");
    }
    $(function () {
        $(".Poster").click(function () {
            $("link:first").attr("href", "css/list.css");
            $.cookie("Movie_Layout", "Poster", {
                expires: 365,
                path: '/media',
                secure: true
            });
            $('.content-genre, .searchresults').hide().delay(75).fadeIn(275);
            window.scrollTo(0, 0);
        });
        $(".Description").click(function () {
            $("link:first").attr("href", "css/desc.css");
            $.cookie("Movie_Layout", "Description", {
                expires: 365,
                path: '/media',
                secure: true
            });
            $('.content-genre, .searchresults').hide().delay(75).fadeIn(275);
            window.scrollTo(0, 0);
        });
    });
}


texts: String.IndexOfAny(string, string[]) (lowest position for group of needles in haystack)

texts:
This is a follow-up to this question with bug fixes, question and code improvements from @dfhwze, @PieterWitvoet, @HenrikHansen, @t3chb0t. I am still hoping for an improved approach or algorithm rather than micro-optimizations to the code.

.Net provides String.IndexOfAny(string, char[]) but not String.IndexOfAny(string, string[]).

The existing built-in String.IndexOfAny takes an array of char and returns the lowest position of any one char from the array in a passed in string or -1 if none are found.

My extension takes a string to search s and an array of strings to find targets and returns the lowest position found of any member of targets in s or -1 if none are found.

A naive implementation (using LINQ) isn't particularly efficient:

public static int IndexOfAny1(this string s, params string[] targets) =>
    targets.Select(t => s.IndexOf(t)).Where(p => p >= 0).DefaultIfEmpty(-1).Min();

My improved implementation tracks the current candidate position and restricts future searches to be before that candidate position:

public static int IndexOfAny2(this string s, params string[] targets) => s.IndexOfAny3(StringComparison.Ordinal, targets);
public static int IndexOfAny2(this string s, StringComparison sc, params string[] targets) {
    if (targets == null || targets.Length == 0)
        return -1;

    int index = s.IndexOf(targets[0], sc);
    var sLen = s.Length;

    for (int j1 = 1; j1 < targets.Length; ++j1) {
        var target = targets[j1];
        var targetIndex = s.IndexOf(target, 0, index >= 0 ? Math.Min(sLen, index + target.Length) : sLen, sc);

        if (targetIndex >= 0 && (index == -1 || targetIndex < index)) {
            index = targetIndex;
            if (index == 0) // once you're at the beginning, can't be any less
                break;
        }
    }

    return index;
}

This runs up to two times faster.

Sample code to test the two methods:

Console.WriteLine($"IndexOfAny1 should be 8: {"foo bar baz".IndexOfAny1("barz", "baz")}");
Console.WriteLine($"IndexOfAny1 should be 0: {"aabbccddeeffgghh".IndexOfAny1("bbb", "hh", "aa")}");
Console.WriteLine($"IndexOfAny1 should be 0: {"abc".IndexOfAny1("c", "abc")}");

Console.WriteLine($"IndexOfAny2 should be 8: {"foo bar baz".IndexOfAny2("barz", "baz")}");
Console.WriteLine($"IndexOfAny2 should be 0: {"aabbccddeeffgghh".IndexOfAny2("bbb", "hh", "aa")}");
Console.WriteLine($"IndexOfAny2 should be 0: {"abc".IndexOfAny2("c", "abc")}");

Is there a better algorithm or another way to make this faster?

PS Test harness for testing random possibilities:

var r = new Random();

var sb = new StringBuilder();
for (int j1 = 0; j1 < r.Next(80,160); ++j1)
    sb.Append((char)('0'+r.Next(0, 26+52)));
var s = sb.ToString();

var listTargets = new List<string>();
for (int j1 = 0; j1 < r.Next(5, 10); ++j1)
    if (r.NextDouble() < 0.8) {
        var tLen = r.Next(4, Math.Min(s.Length - 4, 10));
        var beginPos = r.Next(0, s.Length - tLen);
        listTargets.Add(s.Substring(beginPos, tLen));
    }
    else {
        sb.Clear();
        for (int j2 = 0; j2 < r.Next(5, 10); ++j2)
            sb.Append((char)('0'+r.Next(0, 26+52)));
        listTargets.Add(sb.ToString());
    }

var targets = listTargets.ToArray();
if (s.IndexOfAny1(targets) != s.IndexOfAny2(targets))
    Console.WriteLine($"Fail on {s} containing {String.Join(",", targets)}");


texts:
So for practice with Javascript, I wrote this lightweight widget factory which is similar in functionality (okay, that's a bit of a stretch) to jQuery's UI Widget factory. I was hoping I could get some pointers as far as idiom usage and also if there are any glaring problems with the below code.

var _CreateWidget = function(namespace, implementation) {
    _Widget = {
        _create: function() {},
        _destroy: function() {},
        _set: function() {},
        _get: function() {},
        options: {},
        context: undefined,
        namespace: namespace,
        apply: function(element) {
            this.context = element;
            element[namespace] = this;
            this._create();
        }
    };

    for (var item in implementation) {
        if (implementation.hasOwnProperty(item)) {
            _Widget[item] = implementation[item];
        }
    }

    return _Widget;
}

Widget = function(namespace, implementation) {
    return function(element) {
        var instance = _CreateWidget(namespace, implementation);
        instance.apply(element);
        return instance
    };
}

An example widget would be defined as follows:

//Example widget
Page = Widget("page", {
    _create: function() {
        //initialization code here
    },
    _destroy: function() {

    },
    _set: function() {

    },
    _get: function() {

    },
    request: function() {
        //custom function
    },
    options: {
        url: ''
    }
});

And the widget would be applied to an element like so:

myElement = document.getElementById("myUniqueId");
appliedPageReference = Page(myElement);

myElement.page.request(); //One way to call the custom request() function;
appliedPageReference.request(); //Another way to call the custom request() function;


texts: Scrabble algorithm review and performance suggestions

texts:
After finishing Project Euler 24, I decided to make a scrabble type of application using /usr/lib/dict as my dictionary what I cat that into a word file. It does take a few seconds, and the dictionary selection isn't that great.

Is there any way I could make it faster, more effective, and with a better dictionary source?

public class Scrabble {

    public static ArrayList<String> numbers = new ArrayList<String>();
    public static ArrayList<String> numbers2 = new ArrayList<String>() {
    };

    public static void main(String[] args) {
        Scanner dict = null;

        try {
            dict = new Scanner(new File("words.txt"));
        } catch (FileNotFoundException ex) {
        }

        while (dict.hasNextLine()) {

            numbers.add(dict.nextLine());
        }

        String n = "gojy";//random text here

        rearrange("", n);
        LinkedHashSet<String> listToSet = new LinkedHashSet<String>(numbers2);
        ArrayList<String> listWithoutDuplicates = new ArrayList<String>(listToSet);
        for (int i = 0; i < listWithoutDuplicates.size(); i++) {

            if (numbers.contains(listWithoutDuplicates.get(i))) {
                System.out.println(listWithoutDuplicates.get(i));
            }
        }

    }

    public static void rearrange(
            String q, String w) {
        if (w.length() <= 1) {
            String k = q + w;
            numbers2.add(k);//full word
            numbers2.add(q);//smaller combination to get words with less letters. doesn't work too well
        } else {

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

                String c = w.substring(0, i)
                        + w.substring(i + 1);

                rearrange(q
                        + w.charAt(i), c);

            }
        }
    }
}


texts: Computing tangent space basis vectors for an arbitrary mesh

texts:
This is more like a share and a request than a question. I converted Eric Lengyel's code, which calculates tangents of a mesh for the purpose of texturing and normal mapping, to support SIMD. For this I used DirectXMath library from windows SDK. I don't know if this is actually faster than regular floating point version (didn't test it yet). So, my request is: please let me know if you have any suggestions as to optimize the code even further.

void CalculateTangentsAndNormals( vector<Vertex>& verts, vector <U32>& idx )
{
    // Computing Tangent Space Basis Vectors for an Arbitrary Mesh
    //  http://www.terathon.com/code/tangent.html
    //

    const XMVECTOR NullVector = XMLoadFloat4A(&XMFLOAT4A(0.f, 0.f, 0.f, 0.f));
    const XMVECTOR W_Null = XMLoadFloat4A(&XMFLOAT4A(1.f, 1.f, 1.f, 0.f));
    const XMVECTOR signature = XMLoadFloat4A(&XMFLOAT4A(1.f,-1.f,-1.f,1.f));
    XMMATRIX st(NullVector, NullVector, NullVector, NullVector);

    vector <XMVECTOR> vTangents, vBitangents;
    const U32 NumberOfVertices = (U32)verts.size();

    vTangents.resize( NumberOfVertices , NullVector);
    vBitangents.resize( NumberOfVertices, NullVector);

    const U32 NumberOfIndices = (U32)idx.size();
    for ( U32 i = 0; i < NumberOfIndices; ++i )
    {
        const U32 i0 = idx[i];
        const U32 i1 = idx[++i];
        const U32 i2 = idx[++i];

        const XMVECTOR v0 = XMLoadFloat3A(&verts[i0].pos);
        const XMVECTOR v1 = XMLoadFloat3A(&verts[i1].pos);
        const XMVECTOR v2 = XMLoadFloat3A(&verts[i2].pos);

        const XMVECTOR e0 = XMVectorMultiply( v1 - v0, W_Null);
        const XMVECTOR e1 = XMVectorMultiply( v2 - v0, W_Null);

        const XMMATRIX e01( e0, e1, NullVector, NullVector);
        /*      | e0.x e0.y e0.z 0 |
                | e1.x e1.y e1.z 0 |
        e01 =   | 0    0    0    0 |
                | 0    0    0    0 |
        */
        const XMVECTOR t0 = XMLoadFloat2A(&verts[i0].tex);
        const XMVECTOR t1 = XMLoadFloat2A(&verts[i1].tex);
        const XMVECTOR t2 = XMLoadFloat2A(&verts[i2].tex);

        XMVECTOR s = XMVectorMergeXY(t1 - t0, t2 - t0); // s = (t1x - t0x, t2x - t0x, t1y - t0y, t2y - t0y)

        XMFLOAT4A d;
        XMStoreFloat4A(&d, s);

        const float DetInv = 1.0f/( d.x * d.w - d.z * d.y );

        s *= DetInv;
        s = XMVectorMultiply(s, signature); // s = (sx, -sy, -sz, sw)

        const U32 *const pSrc = reinterpret_cast<const U32 *const>(&s);
        U32* pDst = reinterpret_cast<U32*>(&st.r[0]);
        pDst[0] = pSrc[3];  // _00 = sw
        pDst[1] = pSrc[2];  // _01 = -sz
        pDst = reinterpret_cast<U32*>(&st.r[1]);
        pDst[0] = pSrc[1];  // _10 = -sy
        pDst[1] = pSrc[0];  // _11 = sz

        /*      | sw  -sz 0 0 |
                | -sy sx  0 0 |
        st =    | 0    0  0 0 |*DetInv
                | 0    0  0 0 |
        */

        const XMMATRIX uv( XMMatrixMultiply(st, e01) );

        vTangents[i0] += uv.r[0];
        vTangents[i1] += uv.r[0];
        vTangents[i2] += uv.r[0];

        vBitangents[i0] += uv.r[1];
        vBitangents[i1] += uv.r[1];
        vBitangents[i2] += uv.r[1];

    }
    // orthogonalize normals and tangents
    for ( U32 i = 0; i < NumberOfVertices; ++i )
    {
        const XMVECTOR normal = XMVector3Normalize(XMLoadFloat3A(&verts[i].normal));

        // normalize tangents and orthogonalize TBN
        XMVECTOR n0 = XMVector3Normalize( vTangents[i] - XMVector3Dot( normal, vTangents[i] ) * normal );
        //calculate handedness
        const XMVECTOR n1 = XMVector3Cross( normal, vTangents[i] );
        const float w = (XMVector3Less(XMVector3Dot(n1, vBitangents[i] ), NullVector)) ? -1.f : 1.f;

        n0 = XMVectorSetW(n0, w);
        XMStoreFloat4A(&verts[i].tangent, n0);
    }
}


texts:
I started learning JavaScript a week ago, and I made a sorting function on my own. It does work, but I need reviews and how to write a better one.

Art

Mobile

Education

Games

Magazines

Sports

var list = document.getElementById("list");
var myList = list.getElementsByTagName("li");
var a = [];
for (var i = 0; i < myList.length; i++) {
    a[i] = myList[i].innerHTML;
}
a.sort();
for (var i = 0; i < myList.length; i++) {
    myList[i].innerHTML = a[i];
}

Output:

  Art
  Education
  Games
  Magazines
  Mobile
  Sports


texts: Sorting an int[] array with an Insertion Sort

texts:
I'm currently learning about different sorting algorithms and after reading on the concept of the insertion sort of how it's done I've tried to implement it by myself before seeing how it was implemented in the source from which I'm learning.

This was my implementation:

void insertionSort(int arr[])
{
    for(int i = 1; i < SIZE; i++)
    {
        for(int j = i; j > 0 && arr[j] < arr[j - 1]; j--)
        {
            int swap = arr[j];
            arr[j] = arr[j - 1];
            arr[j - 1] = swap;
        }
    }
}

Now I think I've implemented it correctly (not talking about efficient or anything just the algorithm itself), but when I saw the way it was implemented in the course I'm seeing I had a bit of concerns about my implementation because they're really different and I just wanted to make sure my implementation is really an insertion sort algorithm implementation and I didn't done anything else.

Here's the implementation by the source I'm learning from:

void insertionSort(int arr[])
{
  for(int i = 1;, i < SIZE; i++)
  {
      int value = arr[i];
      int j = i - 1;
      int done = 0;
      do
      {
          if (arr[j] > value)
          {
              arr[j + 1] = arr[j];
              j--;
              if (j < 0)
                  done = 1;
          }
          else
          {
              done = 1;
          }
      } while(!done)
      arr[j + 1] = value;
  }
}

Is my implementation of insertion sort correct ?

Also I would appreciate if you could compare my implementation to the one made by the source I'm learning from, any efficiency differences or any other thing you think would worth mentioning.


texts:
I have tried to work out the Frog Jump task on Codility. The algorithm for solving this is rather simple but the maximum score I am able to achieve is 55% (also 44% with modulus operations).

  A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater
  than or equal to Y. The small frog always jumps a fixed distance, D.

  Count the minimal number of jumps that the small frog must perform to reach its target.

  Write a function: class Solution { public int solution(int X, int Y, int D); } that, given three integers X, Y and D, returns the minimal
  number of jumps from position X to a position equal to or greater than
  Y.

public static int solution(int X, int Y, int D)
{
    int count = 0;

    //55%
    if (X == Y)
    {
        return 0;
    }
    else
    {
        while (X < Y)
        {
            X += D;
            count++;
        }
        return count;
    }
}

I have read in a place that switch statements can outperform if conditions but couldn't find a way to make a switch statement out of this, also tried to get the answer recursively, but was only able to put down the base case..

Appreciate hints, help, suggestions at what should I look for to be optimal with this method.


texts: Add data into lists at runtime, from a database

texts:
Presentation:

I am trying to write classes to store data from a database.

The data consists in objects, let's call them "Areas". Each area has a name and possibly 'child' areas:

public class Area {
    public string Name;
    public List<Area> SubAreas = new List<Area>();
}

The program reads data from the database (out of the scope of this review), and creates a new Data object to store this data.

The data is returned as a 2D array of stings:
[ [areaName, parentAreaName], ...]

With the following assumptions:

The parent of a root-level area is String.Empty
The parent item is before (in the array) any child item (-> no recurrence needed).

Code:

public static void Main()
    {
        //This comes from the DB
        string[,] areas = new string[4,2] {{"1",""}, {"11","1"},{"12","1"},{"2",""}};

        new Data(areas);
    }

public class Data {
    public List<Area> Areas = new List<Area> ();

    public Data(string[,] areasArray)
    {
        for (int i = 0; i < areasArray.GetLength(0); i++) {
            Area loopSubArea;
            if (areasArray[i,1] != "") {
                loopSubArea = Areas.Where(a => a.Name == areasArray[i,1]).First();
                loopSubArea.SubAreas.Add(new Area() {Name = areasArray[i,0]});
            } else {
                Areas.Add(new Area() {Name = areasArray[i,0]});
            }
        }
    }
}

The full working code is also available in a Fiddle

My concerns:

I initialize the lists during the object creation, meaning that I have possibly a lot of empty Lists around. Is that an issue?
I feel like the loop part could be improved, but I have not been able to think of anything better
I am a beginner in C#, any general advice is of course welcome.


texts:
The following code achieves what I require, reading 10000 rows at a time:

var query = repository.Subscribers.ToList().Skip(numberToSkip).Take(numberInBatch);

Is there an alternative which will make this faster?

Here's the full code:

protected static string ContactUrl = BaseUrl + "/api/contact";

protected static int NumberInBatch = 10000;
protected static int NumberToSkip = 10000;
protected static int NumberToCountUpto = 100000;

public static string GetSubscribers(int numberToSkip, int numberInBatch)
    {
        using (var repository = new SubscriberEntities())
        {
            var index = 0;
            string output = null;
            var query = repository.Subscribers.ToList().Skip(numberToSkip).Take(numberInBatch);

            foreach (var subscriber in query)
            {
                var telephone = ContactHelper.GetTelephoneFromEmail(subscriber.EmailAddress);

                string requestCreateContact = RequestHelper.CreateContact(subscriber.EmailAddress, telephone);
                if (index > 0) output = output + ",";
                output = output + (requestCreateContact);

            }
            return output;
        }
    }

    //Batch Create
    public static void SendBatchCreate()
    {

        for (int i = NumberToSkip; i < NumberToCountUpto; i += NumberInBatch)
        {
            var requestCreateMultipleContacts = GetSubscribersAndCreateContacts(i, NumberInBatch);

            RequestHelper.SubmitPostRequest(requestCreateMultipleContacts, ContactUrl);
        }
        Console.ReadKey();
    }


texts:
I wrote some code for my question about deleting temp files that weren't deleted on exit before and would like to get some feedback if I forgot some details or got something wrong.

private static final String PROJECTNAME = "projectname";
private static final String LOCKNAME = "lock_ThisIsAVeryLongFilenameNoOtherApplicationWillUse.lock";

private static void initTempDir(Path tempPath) throws IOException {
    final Path tempFolder = Files.createTempDirectory(tempPath, PROJECTNAME);
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            DeepDelete.deepDelete(tempFolder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }));

    Timer timer = new Timer(true);
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            Path lockFile = tempFolder.resolve(LOCKNAME);
            try {
                Files.write(lockFile, ByteBuffer.allocate(Long.BYTES).putLong(System.currentTimeMillis()).array());
            } catch (IOException e) {
                System.err.println("Failed to secure avaliability of temp folder, exiting!");
                e.printStackTrace();
                System.exit(-1);
            }
        }

    }, 60_000, 60_000);
    // Make first write manually to be sure the file has been written when
    // old temp files get deleted. Otherwise, the current temp file not
    // having a time stamp (yet) would be classified as old and removed.
    Files.write(tempFolder.resolve(LOCKNAME),
            ByteBuffer.allocate(Long.BYTES).putLong(System.currentTimeMillis()).array());

    long currentTime = System.currentTimeMillis();
    Files.list(tempFolder.getParent())//
            .filter(Files::isDirectory)//
            .filter(p -> p.getFileName().toString().startsWith(PROJECTNAME))//
            .filter(p -> {
                Path lock = p.resolve(LOCKNAME);
                if (!Files.isRegularFile(lock))
                    return true;
                try {
                    return ByteBuffer.wrap(Files.readAllBytes(lock)).getLong() < currentTime - 60_000 * 5;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            })//
            .forEach(p -> {
                try {
                    DeepDelete.deepDelete(p);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
}

The method takes in the path to the default temp directory and first initializes the own temp folder with the hardcoded prefix PROJECTNAME. The folder has a file whose name is in the constant LOCKNAME in it. Each minute, the current time is written in this file. In the end of initialization, each folder in the temp directory with the same prefix and with a lock file older than five minutes is considered a leftover from a crash and gets deleted.


texts:
I'm attempting to rewrite the classic snake game in c++. What I am inquiring about is my implementation for a 2D collision detection function. I am utilizing SFML to create the window and shapes. The two objects that I am checking are squares of the same size. I would really appreciate any feedback that is given, as I'm still a beginner when it comes to c++.

This function works as intended. I'm looking for performance improvements, and general c++ practices that I should follow. If I need to add any more code, please let me know.

Game.h

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>

class Game {

    // Some code omitted for brevity //

    private:
        sf::RectangleShape enemy;
        sf::RectangleShape player;

    public:
        const bool isColliding(sf::RectangleShape player, sf::RectangleShape enemy) const;

    // Some code omitted for brevity //

};

Game.cpp

/*
    Determines if the player is colliding with the enemy.

    @param sf::RectangleShape player - Player object.
    @param sf::RectangleShape enemy - Enemy object.

    @return bool - True if colliding, False otherwise.
*/
const bool Game::isColliding(sf::RectangleShape player, sf::RectangleShape enemy) const {

    sf::Vector2f playerPosition = this->player.getPosition();
    sf::Vector2f enemyPosition = this->enemy.getPosition();

    float playerWidth = this->player.getSize().x;
    float playerHeight = this->player.getSize().y;

    return (
        ((playerPosition.x + playerWidth) > enemyPosition.x && (playerPosition.x - playerWidth) < enemyPosition.x) &&
        ((playerPosition.y + playerHeight) > enemyPosition.y && (playerPosition.y - playerHeight) < enemyPosition.y)
    );

}


texts: Is there an easier way to find the index of the first letter in a string?

texts:
I have a function that returns the index of the first alphabet letter found in a string, or -1 if no letter is found.  It seems like there should be a way to do this with extension methods instead of looping, but I can't think of one:

int findFirstLetter(string str)
{
 for(int ctr=0;ctr<str.Length;ctr++)
 {
  if (Char.IsLetter(str[ctr]))
  {
   return ctr;
  }
 }
 return -1;
}


texts:
/*
 * Power set is just set of all subsets for given set.
 * It includes all subsets (with empty set).
 * It's well-known that there are 2N elements in this set, where N is count of elements in original set.

 * To build power set, following thing can be used:

 * Create a loop, which iterates all integers from 0 till 2^N-1
 * Proceed to binary representation for each integer
 * Each binary representation is a set of N bits (for lesser numbers, add leading zeros).
 * Each bit corresponds, if the certain set member is included in current subset.
 */
import java.util.NoSuchElementException;
import java.util.BitSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.List;
import java.util.ArrayList;

public class PowerSet<E> implements Iterator<Set<E>>, Iterable<Set<E>> {
    private final E[] ary;
    private final int subsets;
    private int i;

    public PowerSet(Set<E> set) {
        ary = (E[])set.toArray();
        subsets = (int)Math.pow(2, ary.length) - 1;
    }

    public Iterator<Set<E>> iterator() {
        return this;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Cannot remove()!");
    }

    @Override
    public boolean hasNext() {
        return i <= subsets;
    }

    @Override
    public Set<E> next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        Set<E> subset = new TreeSet<E>();
        BitSet bitSet = BitSet.valueOf(new long[] { i });
        // get the index of truth values.
        for (int e = bitSet.nextSetBit(0); e != -1; e = bitSet.nextSetBit(e + 1)) {
            subset.add(ary[e]);
        }
        i++;
        return subset;
    }

    // Unit Test
    public static void main(String[] args) {
        Set<Integer> numbers = new TreeSet<Integer>();
        for (int i = 1; i < 5; i++) {
            numbers.add(i);
        }
        int count = 0;
        PowerSet<Integer> pSet = new PowerSet<Integer>(numbers);
        for (Set<Integer> subset : pSet) {
            //System.out.println(subset);
            count++;
        }
        assert count ==  (int)Math.pow(2, numbers.size()): "Total number of elements should be 2^N";
    }
}

The code is running fine. I am trying to learn assert for unit testing the code as well. The main issue is the elements seem to come out of order. Is there any way to maintain lexicographical order?


texts:
I have JSON data:

var data = [ { dat: 'timestamp', val: 10 }, { dat: 'timestamp', val: 20}, (...)]

Data contains objects with timestamp and value, can be more than one in  month.

I need to sum values from objects which have the same month and year.

First I use each function to add additional field cat which contains formatted date in format MMM'YY for example: Sep'15, after that I group the result by this cat field. This result I map to objects with properties name and value. Values are summed by using reduce function on plucked values - val from each grouped result. I'm using underscore.

var withCategories = _.each(data, function (elem) {
                            elem.cat = moment(elem.dat).format("MMM[']YY");
                        });

var groupedResult = _.groupBy(withCategories , 'cat');

var finalResult = _.map(groupedResult, function (elems) {

                            var valuesArray = _.pluck(elems, 'val'); // from group
                            return {
                                name: elems[0] ? elems[0].cat : '',
                                value: _.reduce(valuesArray, function (memo, num) {
                                    return memo + num;
                                }, 0)
                            }
                        });


texts:
I'm working on this valid palindrome problem. Any advice on code bug, better idea for low algorithm execution time complexity, code style, etc. are highly appreciated.

  Problem

  Given a string, determine if it is a palindrome, considering only
  alphanumeric characters and ignoring cases.

  Example

  "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not
  a palindrome.

  Note

  Have you consider that the string might be empty? This is a good
  question to ask during an interview. For the purpose of this problem,
  we define empty string as valid palindrome.

Source Code

def check_valid(source):
    i = 0
    j = len(source)-1
    source =source.lower()
    while i <= j:
        while i<=j and not ('a'<=source[i]<='z'):
            i+=1
        while i<=j and not ('a'<=source[j]<='z'):
            j-=1
        if i<=j:
            if source[i] != source[j]:
                return False
            else:
                i+=1
                j-=1
        if i > j:
            return True

if __name__ == "__main__":
    print check_valid('A man, a plan, a canal: Panama') # return True
    print check_valid('race a car') # return False


texts: How can this python regex file finder be improved?

texts:
I've just spent a good hour or so developing a python script that hunts down and removes .php files depending on the regex expression the situation was a client has got several sites and got hacked recently and as a result a large portion of 8 character random php files have created, so in order to remove them all I made a python script to hunt and find the files whilst avvoiding the files needed also which are 8 characters.
Target:
n1qv0iop.php
e7wo0sb8.php
sdt24g9o.php
9ktrhstu.php
2cvgc7qa.php
nlrgnwzt.php
pyj67osr.php
Script:
import os
import re

rootdir = "/ROOTDIRECTORY"
regex = re.compile('^((((?!absolute|abstract|accounts|activate|activity|advanced|archives|autoload|autoplay|basiccss|bookmark|calendar|captions|category|checkbox|comments|compiled|conntest|controls|counters|courierb|courieri|creative|cronview|database|defaults|disabled|document|dropcaps|elements|embedded|error404|external|facebook|features|fieldset|flamingo|flipcard|freedoms|frontend|getcombo|getnodes|groupped|gzdecode|homepage|icongrid|iconlist|igniteup|importer|includer|launcher|lazyload|licenses|lightbox|location|magazine|mailpoet|manifest|mappings|myticket|optimize|packager|password|phpthumb|platform|pointers|products|progress|promobox|radiotab|redirect|register|renderer|repeater|required|response|revision|richtext|rollback|rollover|security|services|settings|sidebars|sitemaps|switcher|taxonomy|template|testmail|textarea|thankyou|timeline|tracking|upgrader|upgrades|variable|videobox|whatsnew|payments|donation|checkout|readmore|overview|tooltips|purchase|endpoint|firewood|pagepost|jwplayer|analytic|builders|bulkedit|carousel|delivery|featured|feedback|fontello|fontpack|headline|honeypot|messages|metadata|position|registry|sendaway|sections|singular|siblings|subpages|tabgroup|thickbox|turfmail|webfonts|networks|htaccess))[a-z0-9]{8})).php$')

for root, dirs, files in os.walk(rootdir):
  for file in files:
    if regex.match(file):
   print(os.path.join(root,file))
   #os.remove(os.path.join(root,file))

   print "END";

My question is can this be improved? is there a better way to write the regex pattern?just looking for advice :)


texts: Creating a function when user clicks on element to toggle another element

texts:
I use a lot of "when user clicks on element then another element will be displayed as visible/hidden... aka toggle".

So I'm trying to make a general function to abide the DRY principal.

Before jumping into the code, I just wanted to state that there are still bugs. I just want to know before continuing if writing a function like the one below is necessary or if I should just ignore the DRY in this case and have lots of separate toggle functions. Or if it's necessary, would there be a more efficient method?

JSFiddle

*Brief Description of Code

-There are 4 parameters

The first two are mandatory, the last two are optional
The index parameter is a string of numbers ex. "1.0","1"
The reason I need the last two parameters is because I have elements that can't be easily
targeted. For example, they don't have an #id. So to grab those generic elements, I use getElementsByTagName
Then I'll use the index parameter to get the element in the array.
index parameter: The number before '.' is the index of the clickElement
and the number after '.' is the index of the toggleElement. clickElement = 1. toggleElement = 0.

function toggleClick(clickElement, toggleElement, condition, index) {
    var toggleIndex;
    var clickIndex;

    if(condition === false){
        if(index.indexOf('.') != -1){
            //indexOf returns -1 if '.' is not found.
            // Checks to see if two index is entered
            indexArray = index.split('.');
            clickIndex = indexArray[0];
            toggleIndex = indexArray[1];
            clickElement = document.getElementsByTagName(clickElement)[clickIndex];
            toggleElement = document.getElementsByTagName(toggleElement)[toggleIndex];
        }else{
            //If there is no '.', that means only one index was entered.
            // By function requirement, it should be the index of clickElement

            clickElement = document.getElementsByTagName(clickElement)[index];
        }
    }
    $(clickElement).click(function(){
            $(toggleElement).toggle();
        });
    }

This is one of my first functions, and so I need some guidance on whether I'm writing bad code. Yes, this might be broad because there may be many different ways to write this function.  But I just need to know if there's a more efficient way or if my way is okay.


texts:
Here is the text of the problem:

  There are 5 brands of cameras sold on your website – Nikon, Canon, Sigma, Fujifilm and Sony. Each of these brands sell different cameras at distinct prices. For simplicity, each brand is represented by an integer between 1 to 5. Your website has q shoppers each of whom has a list of brands they are willing to buy from. Based on every user's buying history, you know that they are most likely to buy the kth cheapest camera amongst the brands they like. Your task involves finding this kth cheapest camera for each shopper.

  Given a list of brands and the prices of their cameras, the list of shoppers and their preferences, for each shopper, determine the kth cheapest camera. If no such camera exists, print -1.

  Input Format

  The first line contains integer n denoting the total number of cameras being sold. The second line contains an array of n integers describing the brands of the cameras. The third line contains an array of n integers describing the prices of those particular cameras.

  After that, q queries describing the shoppers' preferences follow. A single query is described in 3 lines.

  d represents the number of brands the shopper will buy from.
  d space separated integers in ascending order denoting the index of the brands.
  kth cheapest camera to display.

  Constraints

  1 ≤ n ≤ 105
  1 ≤ q ≤ 105
  1 ≤ pricesi ≤ 109
  1 ≤ brandsi ≤ 5
  1 ≤ lj ≤ 5
  1 ≤ k ≤ 5 * 105
  all pricesi are distinct

  Output Format

  Output exactly q lines. In the jth of them print a single integer denoting the price of the camera lens the jth shopper will buy. If no such lens exists, print -1.

Here is my solution:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> brands;
    for (int i = 0; i < n; i++){
        int tmp;
        cin >> tmp;
        brands.push_back(tmp);
    }

    unordered_map<int, vector<int>> dict;
    for (int i = 0; i < n; i++){
        auto it = dict.find(brands[i]);
        if( it == dict.end() ) {
        dict.insert({brands[i], vector<int>()});
        }
    }
    for (int i = 0; i < n; i++){
        int tmp;
        cin >> tmp;
        dict[brands[i]].push_back(tmp);
        }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++){
        int number_of_brands;
        cin >> number_of_brands;
        vector<int> indexes;
        for (int i = 0; i < number_of_brands; i++){
            int tmp;
            cin >> tmp;
            indexes.push_back(tmp);
            }
        int k;
        cin >> k;
        vector<int> potential_cameras;
        for (auto i : indexes){
            for (auto x : dict[i]){
                potential_cameras.push_back(x);
                }
            }
        sort(potential_cameras.begin(), potential_cameras.end());
        if (potential_cameras.size() >= k){
            cout << potential_cameras[k - 1] << '\n';
            }
        else {
            cout << -1 << '\n';
        }
    }
    return 0;
}

This algorithm times out for every single one of the bigger sized test cases. It is one of my very first attempt at writing C++ code, so please be merciless with any bad habit or code smell you can spot, but in this specific case I am curious to know what the main issue is, and if I could have used more suitable data structures than the ones I chose.


texts: Reading image files asynchronously and displaying them on the page

texts:
I am trying to display images from a folder in my project using the "fs" module with NodeJS. I need to read all images in a directory and run them in a loop for them to be displayed. I was able to do it but I'm not sure if this is the proper or good way to do it.

I inserted my readdir (Reading Files Asynchronously) inside my Homepage route.
The images were displayed but I have no one to ask if I did it the proper way.

const express = require('express');
const router = express.Router();
const fs = require('fs');

router.get('/', function(){
    fs.readdir('./assets/images/', (err, files) => {
        if(err) {
            throw err;
        }
        res.render('home', {
            files: files
        });
    });
});

module.exports = router;


texts:
I'm trying to implement Dijkstra's algorithm in Python, but the issue is that Python doesn't have support for key-based heaps out of the box, so applying relaxation step of the classical algorithm presented in CLRS becomes cumbersome.

My idea is to keep maintaining current total distance and then pushing frontier nodes with it. It seems to work both with the basic examples I can think of and pass codeforce's 20-C: Dijkstra?, which literally just tests algorithm implementation without need for modifications.

However, I don't have enough experience to reason about correctness and I can't figure out if my implementation is actually correct. Please take a look at my implementation and see if you can spot any problems?

from heapq import heappop, heappush

def dijkstra(g, s, f):
    h = [(0,s,None)]
    visited, prev = set(), {}

    while len(h) > 0:
        d,v,p = heappop(h)
        if v not in visited:
            visited.add(v)
            prev[v] = p
            if v == f:
                break
            for adj, adj_d in g[v]:
                if adj not in visited:
                    heappush(h, (adj_d + d, adj, v))

    path = [f]
    while f in prev and prev[f] != None:
        f = prev[f]
        path.append(f)
    return path[::-1], d

if __name__ == '__main__':
    g = {'s':[('v',1),('w',4)],'v':[('w',2),('t',6)],'w':[('t',3)],'t':[]}
    print(dijkstra(g, 's', 't') == (['s', 'v', 'w', 't'], 6))
    print(dijkstra(g, 's', 'e') == (['e'], 7))


texts: Website monitor - if site is down, kill the process, contact admin and restart server

texts:
My Python-based web server (Tornado module) is pretty complex and keeps going down (seems like usually in the middle of the night), so here's a bash script that kills the process, restarts and emails and txt messages me:

#!/bin/bash
NOTIFYEMAIL=<my_email_address>
SMSEMAIL=<cell_phone_number>@<carrier> # ie. pm.sprint.com
SENDEREMAIL=admin@loclahost
SERVER=http://the_website.com/
PAUSE=60
FAILED=0
DEBUG=0

while true
do
/usr/bin/curl -sSf --max-time 10 $SERVER > /dev/null 2>&1
CS=$?
# For debugging purposes
if [ $DEBUG -eq 1 ]
then
    echo "STATUS = $CS"
    echo "FAILED = $FAILED"
    if [ $CS -ne 0 ]
    then
    echo "$SERVER is down"

    elif [ $CS -eq 0 ]
    then
    echo "$SERVER is up"
    fi
fi

# If the server is down and no alert is sent - alert
if [ $CS -ne 0 ] && [ $FAILED -eq 0 ]
then
    PID=`ps -eaf | grep 'python -m fore.server' | grep -v grep | awk '{print $2}'`
    FAILED=1
    if [ $DEBUG -eq 1 ]
    then
    echo "$SERVER failed"
    if [[ "" !=  "$PID" ]]
    then
        echo "killing $PID"
        kill -9 $PID
        echo "Starting the server."
        python -m fore.server
    else
        echo "No Python Process Running Locally"
    fi
    fi
    if [ $DEBUG = 0 ]
    then
    echo "$SERVER failed"
    PID=`ps -eaf | grep 'python -m fore.server' | grep -v grep | awk '{print $2}'`
    if [[ "" !=  "$PID" ]]
    then
        kill -9 $PID
        echo "$SERVER went down $(date)" | /usr/bin/mail -s "$SERVER went down" "$SENDEREMAIL" "$SMSEMAIL"
        echo "$SERVER went down $(date)" | /usr/bin/mail -s "$SERVER went down" "$SENDEREMAIL" "$NOTIFYEMAIL"
        python -m fore.server
    else
        echo "No Python Process Running Locally"
    fi
    fi

# If the server is back up and no alert is sent - alert
elif [ $CS -eq 0 ] && [ $FAILED -eq 1 ]
then
    FAILED=0
    if [ $DEBUG -eq 1 ]
    then
    echo "$SERVER is back up"
    fi
    if [ $DEBUG = 0 ]
    then
    echo "$SERVER is back up $(date)" | /usr/bin/mail -s "$SERVER is back up again" "$SENDEREMAIL" "$SMSEMAIL"
    echo "$SERVER is back up $(date)" | /usr/bin/mail -s "$SERVER is back up again" "$SENDEREMAIL" "$NOTIFYEMAIL"
    fi
fi
sleep $PAUSE
done

I have to execute my server with sudo in order to use port :80 so I am executing the script like:

sudo ./outage.sh

Of course, warnings, suggestions, improvements are welcome.

One improvement I'm interested in implementing is to iterate through a list of servers in order to keep track of multiple web sites.


texts: How to Unit Test a Thread which has an infinite loop?

texts:
I wanted to be sure the thread being tested would complete its job by the Assert statement of the unit test, but I couldn't be sure bc of the way I designed the threads. I designed them to run forever in the background, performing a task - logging, assembling, or disassembling - at regular timed intervals. What I ended up doing was sleeping the test thread for a bit to ensure the thread being tested had enough time to complete its task. I have no idea whether this is the "right" way to handle this situation, and I'd appreciate any feedback!

@Test
// Tests whether thread removes completed tasks from in_progress and returns
// processors to available
public void testOne() {

    t.complete = true;
    d.start();

    try {
        Thread.sleep(20);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Assert.assertEquals(cs.size(), 3);
    Assert.assertEquals(inProgressJobs.size(), 0);

}

Full code: https://github.com/dandelion1234/Map-Reduce


texts: Java implementation of the caesar-cipher

texts:
I've wrote a little program that encrypts text by using the caesar-cipher.
Also it contains a little GUI, created by using swing.
Here's the full code:

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Dimension;
public class caesar {

    public static void main(String[] args) {
        String field, text;

        field = JOptionPane.showInputDialog("Please enter text:");
        field = field.replaceAll("[^a-zA-Z]+", "");
        field = field.toUpperCase();

        int shift;

        String shift_String = JOptionPane.showInputDialog("Please enter shift to the right:");
        shift = Integer.parseInt(shift_String);

        String d = JOptionPane.showInputDialog("Encrypt (1) or decrypt (2):");
        int decision = Integer.parseInt(d);

        String out;

        if(decision==1) {
            out = encrypt(field, shift);
            JTextArea msg = new JTextArea(out);
            msg.setLineWrap(true);
            msg.setWrapStyleWord(true);
            JScrollPane scrollPane = new JScrollPane(msg);
            scrollPane.setPreferredSize(new Dimension(300,300));
            JOptionPane.showMessageDialog(null, scrollPane);
        }
        if(decision==2) {
            out = decrypt(field, shift);
            JTextArea msg = new JTextArea(out);
            msg.setLineWrap(true);
            msg.setWrapStyleWord(true);
            JScrollPane scrollPane = new JScrollPane(msg);
            scrollPane.setPreferredSize(new Dimension(300,300));
            JOptionPane.showMessageDialog(null, scrollPane);
        }
    }

    //Encryption
    public static String encrypt(String text, int n) {
        int x = 0;
        int y = 0;
        String out = ""; //Empty string for result.
        while (x < text.length()) {
            if (text.charAt(x) > 64 && text.charAt(x) < 91) {
                if (text.charAt(x) + n > 90) {
                    y = 26;
                }
                out = out + (char) (text.charAt(x) + n - y);
            } else {
                out = out + text.charAt(x);
            }
            x++;
            y = 0;
        }
        return out;
    }

    //Decryption
    public static String decrypt(String text, int n) {
        int x = 0;
        int y = 0;
        String out = ""; //Empty string for result.
        while (x < text.length()) {
            if (text.charAt(x) > 64 && text.charAt(x) < 91) {
                if (text.charAt(x)-n < 65) {
                    y = 26;
                }
                out = out + (char) (text.charAt(x) - n + y);
            } else {
                out = out + text.charAt(x);
            }
            x++;
            y = 0;
        }
        return out;
    }
}

My question now is: How to improve this code?

I mean, it does what it is supposed to do, but it's not really great code.


texts: Export class instances to a string (for sharing, backup) and importing them

texts:
I have a class (Patient) with many instances.. I need a way to share the instances with other users, so I thought of converting the instance into a string and sharing it, then reconverting it into an instance

From instance to string

func ToString(pt: Patient) -> String {
    let myarray: [String] = [
        pt.id,
        pt.name,
        String(pt.age),
        pt.address,
        pt.tele,
        pt.notes,
        dateToString(pt.date),
        pt.history,
        pt.diagnosis,
        (pt.investigations.map {$0.name + "{|}" + $0.value}).joinWithSeparator("{|X|}"),
        (pt.treatments.map {$0.name + "{|}" + $0.dose}).joinWithSeparator("{|X|}"),
        (pt.images.map {$0.imagePath + "{|}" + $0.caption}).joinWithSeparator("{|X|}")
    ]

    return myarray.joinWithSeparator("{|NEWLINE|}")
}

From string to instance

func FromString(mystring: String) {
    let pt = Patient()

    let ptarray = mystring.componentsSeparatedByString("{|NEWLINE|}")
    let ixarray = ptarray[9].componentsSeparatedByString("{|X|}")
    let rxarray = ptarray[10].componentsSeparatedByString("{|X|}")
    let photoarray = ptarray[11].componentsSeparatedByString("{|X|}")
    let ixcarray = ixarray.map { Investigation(name: $0.componentsSeparatedByString("{|}")[0], value: $0.componentsSeparatedByString("{|}")[1]) }
    let rxcarray = rxarray.map { Treatment(name: $0.componentsSeparatedByString("{|}")[0], dose: $0.componentsSeparatedByString("{|}")[1]) }
    let photocarray = photoarray.map { Pic(imagePath: $0.componentsSeparatedByString("{|}")[0], caption: $0.componentsSeparatedByString("{|}")[1]) }

    pt.id = ptarray[0]
    pt.name = ptarray[1]
    pt.age = Int(ptarray[2]) ?? -1
    pt.address = ptarray[3]
    pt.tele = ptarray[4]
    pt.notes = ptarray[5]
    pt.date = stringToDate(ptarray[6])
    pt.history = ptarray[7]
    pt.diagnosis = ptarray[8]

    pt.savePatient()

    _ = ixcarray.map { x in pt.addInvestigation(x) }
    _ = rxcarray.map { x in pt.addTreatment(x) }
    _ = photocarray.map { x in pt.addImage(x) }
}

Notes:

investigation, treatment, and images are array of classes with two properties
I used "|" as a separator because I disabled it in my app so the user can't interfere with my functions
I use Realm in my app for data persistance
My specific concern is performance issues because thousands of instances may be shared at once


texts: Bash script to simplify Git deployment workflow

texts:
In my company I follow the following workflow. (I think it can be defined a kind of 'Continuous Integration'.)

Workflow:

We have 3 fixed branches (staging/master/production)
Every push to staging or production automatically deploy to staging/production server.

We use git in the following way:
(suppose I'm working on the new functionality 'A')

I create a new branch from Master (ex. branch 'A')
If I want to show my changes to the client I merge A to staging and push it.
once finish my work on A I merge back A on Master and push master
to send changes live I merge Master in Production branch and push production

I find quite boring/time consuming to git checkout ... git pull .. bla bla every time I have to deploy live/staging my work.

So I have produced the following bash script, the aim is to simplify the process of deploying branch A into a single command deploy.sh live A

#!/bin/bash
function merge() {
    SOURCE=$1
    DEST=$2

    echo ''
    echo "--- Merging $SOURCE with $DEST ---"
    echo "--> Checkout $DEST ..."
    git checkout $DEST

    echo "--> Pull $DEST ..."
    git pull --ff-only origin $DEST
    if [ $? -ne 0 ]
    then
      echo "ERROR: Could not PULL"
     return 1
    fi

    echo "--> Merging $SOURCE with $DEST ..."
    git merge --ff-only $SOURCE --no-edit  # --ff-only trigger errors if merge/pull is not possible
    if [ $? -ne 0 ]
    then
      echo "ERROR: Could not MERGE"
      return 1
    fi

    echo "--> Push $DEST ..."
    git push origin $DEST
    return 0
}

function deploy() {
    MODE=$1
    SOURCE_BRANCH=$2

    echo ''
    echo "### START ###"
    echo ''
    echo "--- Pull changes from Master ---"
    git checkout $SOURCE_BRANCH
    git pull --progress --no-edit --no-stat -v --progress origin master

    merge $SOURCE_BRANCH 'staging'
    status=$?
    if [ $status -ne 0 ]
    then
      echo "ERROR: STEP 1" >&2
      exit 1
    fi

    if [ $MODE = "live" ]
    then

        merge $SOURCE_BRANCH 'master'
        status=$?
        if [ $status -ne 0 ]
        then
          echo "ERROR: STEP 2"
          return 1
        fi

        merge 'master' 'production'
        status=$?
        if [ $status -ne 0 ]
        then
          echo "ERROR: STEP 3"
          return 1
        fi
    fi

    echo ''
    echo "### END ###"
    echo ''
}

MODE=$1;
SOURCE_BRANCH=$2;

if [ -z "$MODE"  -o -z "$SOURCE_BRANCH" ]
    then
        echo "Usage:"
        echo ""
        echo  "MODE BRANCH_NAME (MODE: live|staging)"
    else
        if git show-ref --verify --quiet "refs/heads/$SOURCE_BRANCH";
        # if [ `git branch --list $SOURCE_BRANCH ` ]
            then
                deploy $MODE $SOURCE_BRANCH
        else
            echo ''
            echo "Error: Branch $SOURCE_BRANCH not found"
        fi
fi

The question:

I'm quite newby both on GIT and BASH scripting.
So I would like to know if the above workflow/script is ok ? Any recommendation is welcome.
(I'm happy to share this as resource.)


texts: Check the validity of a South African ID number using the Luhn Algorithm

texts:
import java.math.BigInteger;

/**
 * Created by lungisani on 2017/02/25.
 */
public class Luhn {
    public Boolean getIdentitySummation(BigInteger identities){
        String identify = String.valueOf(identities);
        int sumOdd = 0, sumEven = 0, _doubled;
        int summation;
        try {
            String identity = String.valueOf(identify);
            int length = identity.length() - 1;
            char[] chars = identity.toCharArray();
            String check = identity.substring(12);
            for(int x  = 0; x < length; x++){
                if(x % 2 == 0 ){
                        String numString = String.valueOf(chars[x]);
                        int numbers = Integer.valueOf(numString);
                        sumOdd += numbers;
                }else if(x % 2 != 0){
                        String numString = String.valueOf(chars[x]);
                        int numbers = Integer.valueOf(numString);
                        int doubled = numbers * 2;
                        if(doubled > 9){
                            _doubled = doubled - 9;
                        }else{
                            _doubled = doubled;
                        }
                        sumEven += _doubled;
                }
            }
            summation = sumOdd + sumEven;
            int checksum = Integer.valueOf(check);
            if((summation * 9) % 10 == checksum)
                return Boolean.TRUE;
            else
                return Boolean.FALSE;
        } catch (Exception e) {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }
}


texts: Binary tree max sum level - better design?

texts:
I have written some code for finding a level in a binary tree, having a maximum number of elements. I have a few questions:

Is it a good design? I have used 2 queues but the total sum of elements both queues store will be less than n. So I think it should be OK.
Can there be a better design?

    public class MaxSumLevel {
    public static int findLevel(BinaryTreeNode root) {
        Queue mainQ = new Queue();
        Queue tempQ = new Queue();
        int maxlevel = 0;
        int maxVal = 0;
        int tempSum = 0;
        int tempLevel = 0;
        if (root != null) {
            mainQ.enqueue(root);
            maxlevel = 1;
            tempLevel = 1;
            maxVal = root.getData();
        }
        while ( !mainQ.isEmpty()) {
            BinaryTreeNode head = (BinaryTreeNode) mainQ.dequeue();
            BinaryTreeNode left = head.getLeft();
            BinaryTreeNode right = head.getRight();
            if (left != null) {
                tempQ.enqueue(left);
                tempSum = tempSum + left.getData();
            }
            if (right != null) {
                tempQ.enqueue(right);
                tempSum = tempSum + right.getData();
            }
            if (mainQ.isEmpty()) {
                mainQ = tempQ;
                tempQ = new Queue();
                tempLevel ++;
                if (tempSum > maxVal) {
                    maxVal = tempSum;
                    maxlevel = tempLevel;
                    tempSum = 0;
                }
            }
        }
        return maxlevel;
    }
}


texts:
I would like to search a String, and for a certain word, I would like to change only the first 3 letters

For example:

  This is a java institute which insures that you are in that institute. ins!

I want to change the first 3 letter of institute (ins) to JSE, so the result will be:

  This is a java JSEtitute which insures that you are in that JSEtitute. ins!

I have the following, but it is NOT doing the job perfectly:

public class ExistanceAndReplace {

public static void main(String[] args) {

String s = "This is a java institute of insurance and insu.";

if (s.contains("institute")) {
    String s1 = s.replaceFirst("ins", "JSS");
    System.out.println(s1);

}else{
    System.out.println("not found!");
}

}

}

Any suggestions?


texts: Generic java configuration class with type safety

texts:
In the first code snippet I am creating a generic class to hold configuration details.  I am currently using it as a way to pass run time configuration options to plugins in a generic manner.  The second snippet is a helper class that utilizes the class to provide the public methods for use which helps guarantee type safety.

Questions

How is my use of generics?  I feel like there is a better way to handle this.
Is there a better design pattern to be used here?

DefaultConfiguration

import java.util.HashMap;
import java.util.Map;

public class DefaultConfiguration implements Configuration {
    private final Map<String, Setting> options;

    public DefaultConfiguration() {
        this.options = new HashMap<String, Setting>();
    }

    @Override
    public <T> T get(String option, Class<T> type) {
        Setting setting = options.get(option);
        if (setting != null) {
            return type.cast(setting.getSetting(type));
        }
        return null;
    }

    @Override
    public <T> void set(String option, T setting, Class<T> type) {
        options.put(option, new Setting<T>(setting, type));
    }

    @Override
    public void set(String option, Object setting) {
        options.put(option, new Setting(setting, setting.getClass()));
    }

    class Setting<W> {
        private final Class<? super W> type;
        private final W setting;

        public Setting(W setting, Class<? super W> type) {
            this.type = type;
            this.setting = setting;
        }

        public W getSetting(Class<? super W> type) {
            if (type.isAssignableFrom(this.type)) {
                return setting;
            }
            return null;
        }
    }
}

MyConfiguration

public class MyConfiguration extends DefaultConfiguration {
    public String getSettingA() {
        return super.get("settingA", String.class);
    }

    public void setSettingA(String settingA) {
        super.set("settingA", settingA, String.class);
    }

    public Integer getSettingB() {
        return super.get("settingB", Integer.class);
    }

    public void setSettingB() {
        super.set("settingB", settingB, Integer.class);
    }
}

Updated Classes

import java.util.HashMap;

public class Configuration
    extends HashMap<String, Object>
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Override
    public <T> T get(String option, Class<T> type)
    {
        Object obj = get(option);
        if (obj == null) {
            return null;
        }
        try {
            return type.cast(obj);
        } catch (ClassCastException ex) {
            throw new ConfigurationException(ex);
        }
    }
}

public class MyConfiguration
    extends Configuration
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private static final String SETTING_A = "settingA";
    private static final String SETTING_B = "settingB";

    public String getSettingA()
    {
        return get(SETTING_A, String.class);
    }

    public void setSettingA(String settingA)
    {
        put(SETTING_A, settingA);
    }

    public Integer getSettingB()
    {
        return get(SETTING_B, Integer.class);
    }

    public void setSettingB()
    {
        put(SETTING_B, settingB);
    }
}


texts: Top 2 most common elements inside array in Python

texts:
I got this interview problem from leetcode for top k frequent elements, https://leetcode.com/problems/top-k-frequent-elements/description/ But I decide to simplify the problem just most common elements to make it simpler to solve.  If the 3 item has the same frequentcy, then it's okay to return all 3.

"""Given an array, return an array of the top 2 most elements. // Input: [1, 2, 3, 4, 5, 3, 4] | Output: [3, 4] // Input: [2, 2, 3, 2, 4, 3, 4] | Output: [2,4]"""

import collections

def top2Frequent(nums):
    """
    :type nums: List[int]
    :type k: int
    :rtype: List[int]
    """
    if len(nums) < 2:
        return []
    freq = {}
    for num in nums:
        if num in freq:
            freq[num] = freq[num] + 1
        else:
            freq[num] = 1

    bucket = collections.defaultdict(list)
    for key in freq:
        f = freq[key]
        bucket[f].append(key)
    res = []
    count = len(nums)  # the upper limit for res
    while len(res) < 2:
        if bucket[count]:
            res += bucket[count]
        count -= 1
    return res

nums = [1, 4, 3, 4, 5, 3, 4]  # [4,3]
solution = top2Frequent(nums)
print(solution) # [4,3]

https://gist.github.com/Jeffchiucp/2e733e57476bd697bc430cdf48f6e180

I am also tried to solve this problem without using any python library built in collections.


texts: Implement pattern match on FindUsersByName

texts:
I would like some feedback on the code below. And how would I Implement pattern match on find users in role so that exact match not required?

// MembershipProvider.FindUsersByName
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
    MembershipUserCollection users = new MembershipUserCollection();

    try
    {
        Profile.MembershipMapper memberMapper = new MembershipMapper();
        List<Profile.Membership> recs = (List<Profile.Membership>)memberMapper.GetMembershipsByUsername(_memberUtil.GetApplicationId(), usernameToMatch, pageIndex, pageSize, out totalRecords);

        foreach (Profile.Membership rec in recs)
        {
            users.Add(GetUserFromModel(rec, usernameToMatch));
        }
    }
    catch (Exception ex)
    {
        Exception e = CheckEventLog(ex, "FindUsersByName");
        throw e;
    }

    return users;
}


texts:
There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be \$O(log (m+n))\$.

You may assume nums1 and nums2 cannot be both empty.

  nums1 = [1, 3] nums2 = [2]

  The median is 2.0

def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:

        concat = sorted(nums1 + nums2)
        median = concat[len(concat) //2] if len(concat)%2 != 0 else (concat[len(concat) //2]  \
                            +concat[((len(concat))//2)-1])/2
        return median

I want to make this faster.


texts: Efficiently generate large file to create SQL table and insert sample user data

texts:
Here's a function I use to generate a 2.5 gig SQL dump file for testing. It works but it takes a long time. How can I make it more efficient?

const crypto = require("crypto");
const fs = require('fs');
const os = require('os');
const path = require('path');

(async function main(){
    var dumpfile = await generate_large_dump();
    console.log("\nDump created: ", dumpfile);
})();

function generate_large_dump(){
    return new Promise(async (resolve, reject)=>{
        var total_bytes = 0;
        const target_bytes = 2.5 * 1e+9;
        const target_file = path.join(os.tmpdir(), 'large_dump.sql');

        const writerStream = fs.createWriteStream(target_file, {flags: 'w'});
        writerStream.on('error', console.error);

        const age = ()=>Math.floor(Math.random() * (95 - 18 + 1)) + 18;
        const name = ()=>crypto.randomBytes(16).toString("hex");

        const write = str => new Promise(resolve=>{
            total_bytes += Buffer.byteLength(str, 'utf8');
            writerStream.write(str, resolve);
            var pct = Math.min(100, Math.floor(total_bytes / target_bytes * 10000)/100);
            process.stdout.clearLine();
            process.stdout.cursorTo(0);
            process.stdout.write(pct+"% complete");
        });

        var create_sql = "CREATE TABLE `sample_table` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(250) NOT NULL, `age` int(11) NOT NULL, `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`));\n";
        await write(create_sql);

        while(total_bytes < target_bytes) await write("INSERT INTO `sampe_table` (`name`, `age`) VALUES ('"+name()+"', '"+age()+"');\n");

        writerStream.end();
        process.stdout.write("\n");
        resolve(target_file);
    });
}


texts: Extract script body or script src for internal and external JS scripts respectively

texts:
I have written a function func getScriptsFromUrl that accepts a URL and creates two files with script body and script src for internal and external JS scripts respectively.

But since I am totally new to Golang, I doubt if the code is of a high quality, so will highly appreciate if you

examined my code for compatibility with common Golang standards
Checked if the used business logic is appropriate
Provided my with any piece of advice, etc.

package main

import (
    "github.com/PuerkitoBio/goquery"
    "log"
    "net/http"
    "os"
)

func checkError(err error, errorMsg string) {
    if err != nil {
        log.Printf("%s: %s\n", errorMsg, err)
        return
    }
}

func getScriptsFromUrl(url string) {
    res, err := http.Get(url)
    checkError(err,"Failed to access the provided URL")
    defer res.Body.Close()

    doc, err := goquery.NewDocumentFromReader(res.Body)
    checkError(err,"Error loading HTTP response body")

        extScriptsFile, err := os.OpenFile("external_scripts.txt", os.O_WRONLY|os.O_CREATE, 02)
    intScriptsFile, err := os.OpenFile("internal_scripts.txt", os.O_WRONLY|os.O_CREATE, 02)
    defer extScriptsFile.Close()
    defer intScriptsFile.Close()

    scripts := doc.Find("script")
    for i := range scripts.Nodes {
        script := scripts.Eq(i)
        href, srcExists := script.Attr("src")
        if srcExists {
            extScriptsFile.WriteString(href + "\n")
        } else {
            source, err := script.Html()
            checkError(err,"Error getting the script's content")
            intScriptsFile.WriteString(source + "\n")
        }
    }
}

func main(){
    getScriptsFromUrl("https://www.your-site.com")
}


texts: Modular Angular structure with Browserify

texts:
I am trying to create a basic angular template where all files are sorted into a modular structure and referenced using the browserify require method.

I am very new to browserify and the CommonJS-type import system.

Here is a look at the initial structure before I go into details of the file contents:

So to start I have the app folder which contains all the Angular JS files.

"bundle.js" is the output from browserify. This is what will be referenced from the index.html file.

In this I have the app.module.js file which is the main angular app file. This is the file which browserify uses as the start point.

So far "app.module.js" contains the following:

'use strict';

var angular = require('angular');
require('./components/');

var app = angular.module('app', [require('ui-router'), 'app.home', 'app.other']);
app.config(require("./app.routes"));
app.constant('VERSION', require('../../package.json').version);

app.routes.js:

module.exports = function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/");
}

Then in each sub folder I have an index.js which is used by browserify when the require is called for that folder.

app/components/index.js just references all the components:

'use strict';
require("./home/");
require("./other/");

app/components/home/index.js:

module.exports = require("angular")
    .module("app.home", [])
    .config(require("./home.routes"))
    .controller("homeController", require("./home.controller"));

home.controller.js:

module.exports = function($scope)
{
    $scope.message = "Loaded home controller!";
};

home.routes.js:

module.exports = function ($stateProvider, $urlRouterProvider) {
    $stateProvider
    .state('home', {
        url: "/",
        views: {
            "": {
                templateUrl: "app/components/home/home.view.html",
                controller: "homeController"
            }
        }
    })
}

home.view.html:

<section>
    {{message}}
</section>

"home.service.js" is empty and not used, but you get the idea.

The structure is the same for the other folder.


texts: "Factory" which produces a "connection" object

texts:
I am a newbie in C# and in OOP in general. I decided to create a class that should give out a "connection" object. I would like your advice and critique on improving it.

public class Connection_Factory
{

  private string Last_Connection_Trying   = "";
  private string Last_Connection_Success = "";
  public OracleConnection Build(string connection_string)
  {
    Last_Connection_Trying = connection_string;
    return new OracleConnection(connection_string);
  }

  public OracleConnection Build()
  {
    return Build(Last_Connection_Success);
  }

  public void Mark_Connection_as_Successful(string connection_string)
  {
    Last_Connection_Success = connection_string;
  }

  public void Mark_Connection_as_Successful()
  {
    Last_Connection_Success = Last_Connection_Trying;
  }

}


texts: Selecting results as a comma-separated string

texts:
The below code is working correctly, however, I am not sure if the combination of while, foreach and implode is really necessary here or if the same can be achieved much simpler.

What I do here is fetching some data (text) from a db and the only thing I need is the results as a comma separated string.

Now even though this is working I was wondering if there is a direct way to get the results as a comma separated string since the arrays here are not needed for anything else.

$stmt = $conn->prepare("SELECT " . $languageFrm . " FROM TranslationsMain WHERE location LIKE ? ORDER BY sortOrder, " . $languageFrm);
$stmt->bind_param("s", $location);
$stmt->execute();
$result = $stmt->get_result();
$strTranslations = array();
while($arrTranslations = $result->fetch_assoc()){
    $translations[] = $arrTranslations;
}
foreach($translations as $translation){
    array_push($strTranslations, $translation[$languageFrm]);
}
echo implode(",", $strTranslations);


texts:
I've been looking for a while for the right code to encrypt/decrypt files with AES in vb.net. I created the code below. Is this the right way doing AES encryption/decryption nowadays?

Public Function EncryptFile(ByVal plainFilePath As String, ByVal encryptedFilePath As String, ByVal EncryptionKey As String, ByVal SaltValue As String) As String
    Dim passwordIterations As Integer = 1000
    Dim initVector As String = "Bla3212EWMND12es"
    Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
    Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(SaltValue)
    Dim k1 As New Rfc2898DeriveBytes(EncryptionKey, saltValueBytes, passwordIterations)
    Dim symmetricKey As New RijndaelManaged()
    symmetricKey.Mode = CipherMode.CBC
    Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(k1.GetBytes(16), initVectorBytes)

    Using plain As FileStream = File.Open(plainFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
        Using encrypted As FileStream = File.Open(encryptedFilePath, FileMode.Create, FileAccess.Write, FileShare.None)
            Using cs As CryptoStream = New CryptoStream(encrypted, encryptor, CryptoStreamMode.Write)
                plain.CopyTo(cs)
            End Using
        End Using
    End Using
End Function

Public Function DecryptFile(ByVal plainFilePath As String, ByVal encryptedFilePath As String, ByVal DecryptionKey As String, ByVal SaltValue As String) As String
    Dim passwordIterations As Integer = 1000
    Dim initVector As String = "Bla3212EWMND12es"
    Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)
    Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(SaltValue)
    Dim k1 As New Rfc2898DeriveBytes(DecryptionKey, saltValueBytes, passwordIterations)
    Dim symmetricKey As New RijndaelManaged()
    symmetricKey.Mode = CipherMode.CBC
    Dim decryptor As ICryptoTransform = symmetricKey.CreateDecryptor(k1.GetBytes(16), initVectorBytes)

    Using plain As FileStream = File.Open(plainFilePath, FileMode.Create, FileAccess.Write, FileShare.None)
        Using encrypted As FileStream = File.Open(encryptedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
            Using cs As CryptoStream = New CryptoStream(plain, decryptor, CryptoStreamMode.Write)
                encrypted.CopyTo(cs)
            End Using
        End Using
    End Using
End Function


texts:
Could you review it and said if it's written good and/or is better way to do that?

My algorithm is:

Input roomNumber
   If length of roomNumber is 8 set goToRoom state Enabled and color 0xFFFFFFFF
   Else set goToRoom state Disabled and color 0xBBFFFFFF

Code of that algorithm is:

public class Join_room_screen extends Activity {

    EditText numberRoom;
    Button goToRoom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.joinroom);

        numberRoom = (EditText) findViewById(R.id.roomNumber);
        goToRoom = (Button) findViewById(R.id.goToRoom);

        TextWatcher watcher = new LocalTextWatcher();
        goToRoom.addTextChangedListener(watcher);
        updateButtonState();
    }

    void updateButtonState() {
        boolean enabled = checkEditText(numberRoom);
        if (enabled) {
            goToRoom.setBackgroundColor(0xFFFFFFFF);
            goToRoom.setEnabled(enabled);
        } else {
            goToRoom.setBackgroundColor(0xBBFFFFFF);
            goToRoom.setEnabled(false);
        }
    }

    private boolean checkEditText(EditText edit) {
        return ((edit.getText().toString()).length() == 8 );
    }

    private class LocalTextWatcher implements TextWatcher {
        public void afterTextChanged(Editable s) {
            updateButtonState();
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    }
}


texts: SELECT from "vertical" table and display on "horizontal" table

texts:
I have this table in my db:

table
----------
cityID
value
parameterID

The table looks like this:

cityID     value      parameterID
500        30         1
500        60         2
500        7          3
600        25         1
600        50         2
600        6          3
....       ...        .....

where 1,2, 3 stands for temperature, humidity and cm of rain.

I need to display this data this way:

   city       temp        humidity       rain
   500        30           60            7
   600        25           50            6

What I did was this (after SELECT * FROM table):

 /*retrieve all values and put them in loooong arrays */

while($row = $result->fetch_assoc()) {
$selectcityID_array[] = (isset ($row['cityID']) ? $row['cityID'] : "");
$selectparameterID_array[] = (isset ($row['parameterID']) ? $row['parameterID'] : "");
$selectvalue_array[] = (isset ($row['value']) ? $row['value'] : "");
 }
/* remove doubles from cities and parameters*/
$selectcityID_array_unique = array_values (array_unique($selectcityID_array) );
$selectparameterID_array_unique = array_values (array_unique($selectparameterID_array) );

/*chunk the values do that I have an "horizontal" array for every city*/
$value_array_chunked = array_chunk($selectvalue_array, count($selectparameterID_array_unique));

for ($i=0; $i < count($selectcityID _array_unique); ++$i){ /*for every city*/
   for ($i_content=0; $i_content < count($selectparameterID_array_unique); ++$i_content){ /*and for every parameter*/
        echo $ value_array_chunked[$i][$i_content];
        echo ' ';
     }

     echo '<br>';

}

It works, but is this the only (and best) way to do it?


texts: Randomized Quick Sort Implementation in C++

texts:
I just started learning C++, and I'm taking an algorithms course on MIT Open Course Ware to get a better understanding of basic CS and the best practices in C++. Any comments on the structure of the algorithm (optimizations, etc) or best practices in C++ would be greatly appreciated!

#include <iterator>
#include <random>

namespace sort
{

    namespace detail
    {
        template <typename ForwardIter, typename Compare>
        ForwardIter randomized_partition(ForwardIter start, ForwardIter end, Compare less_than)
        {
            static std::default_random_engine gen;
            std::uniform_int_distribution<long> rand_index(0, std::distance(start, end) - 1);

            ForwardIter pivot = start + rand_index(gen);
            std::iter_swap(start, pivot);
            pivot = start;

            ForwardIter less_than_pivot = start, greater_than_pivot = start + 1;
            while (greater_than_pivot != end)
            {
                if ( less_than(*greater_than_pivot, *pivot) ) {
                    ++less_than_pivot;
                    std::iter_swap(less_than_pivot, greater_than_pivot);
                }
                ++greater_than_pivot;
            }

            std::iter_swap(pivot, less_than_pivot);
            pivot = less_than_pivot;
            return pivot;
        }
    }

    template <typename ForwardIter, typename Compare = std::less<typename std::iterator_traits<ForwardIter>::value_type>>
    void randomized_quick_sort(ForwardIter start, ForwardIter end, Compare less_than = Compare())
    {
        if (std::distance(start, end) > 1)
        {
            ForwardIter pivot = sort::detail::randomized_partition(start, end, less_than);
            randomized_quick_sort(start, pivot, less_than);
            randomized_quick_sort(pivot + 1, end, less_than);
        }
    }

}