texts: jQuery plugin to highlight text spans in various colors

texts:
I am trying to tackle two fairly massive concepts through JavaScript and jQuery, the first being object-oriented programming and the second being how to build robust modular plugins with an OOP approach. So far I've learned it's hard.

Here is an example plugin I built, that I somewhat based my much larger project on; any opinions you guys have on my approach would be awesome.

;(function($, window, document, undefined) {
  "use strict";

  $.fn.myPlugin = function(opts) {

  var settings = $.extend({},{ color  : 'blue', normal : 'black' }, opts);

  var proto  = {
    show : function () { $(this.self).css( "color", this.settings.color ); },
    hide : function () { $(this.self).css( "color", this.settings.normal); },
    log  : function () { console.log(this); }
  }

  return this.each(function(id, elem) {

    if (!$.data(elem, 'parent')) // Base parent object
      $.data(elem, 'parent', Object.create(proto, {
        'settings' : { value        : settings,
                       writable     : true,
                       enumerable   : true,
                       configurable : true
        }
      }));

      $(elem).children('span').each(function(idx, elmx) {

        if (!$.data(elmx, 'child')) // Child obj w/ delegation from parent
          $.data(elmx, 'child', Object.create($.data(elem, 'parent'), {
            'foo'  : { value        : 'bar',
                       writable     : true,
                       enumerable   : true,
                       configurable : true
            },

            'self' : { value        : elmx,
                       writable     : false,
                       enumerable   : true,
                       configurable : false
            }
          }));

        $(elmx).hover(
          function() {
            $.data(elmx, 'child').log();
            $.data(elmx, 'child').show();
          }, function() {
            $.data(elmx, 'child').hide();
          });
        });
      });
  };
}(jQuery, window, document));

Here is where the code is running, basically all it does is search for <span> tags inside of the <p> tags the plugin is instantiated on and highlights them with a given user setting or the defaults. It shows the use of delegation for inheritance and very basic event management (no callbacks as of yet)


texts:
As a follow up to a previous question, linked here, I have revised the code and developed what I believe to be a better solution.

In summary, the script should backup all files in a particular directory, and assign them to a particular zip file based on their modification dates. This should only occur for files modified yesterday and older, and skip files with the ".zip" extension.

Please let me know if their are any improvements that you would make? From testing the code, it has been working with no issues:

#!/bin/bash

yestdayend=$(date --date="yesterday" +"%Y-%m-%d 23:59:59")

path=/path/to/dir
filelist=$(find $path -maxdepth 1 ! \( -name "*.zip" \) -type f ! -newermt "$yestdayend")

for file in $filelist
do
        moddate=$(stat -c %y $file | cut -d " " -f 1)
        if zip -rv $path/"logbackup-"$moddate.zip $file; then
           rm $file
        fi
done


texts: Determine number of even, odd, and zero digits of a given integer

texts:
I have been programming in C++ for around a month now, just long enough, I figure, to develop bad habits. Can anyone point out where I'm making mistakes and offer topics I should investigate to understand why?

#include <iostream>
#include <cassert>
#include <sstream>
#include <string>

using namespace std;
string get_line();
int long get_integer();
int* flex(long int num);
void test_flex();

int main()
{
    //flex(get_integer());
    test_flex();
}

/*
 * ===  FUNCTION  =============================================================
 *         Name:  flex
 *  Description:  flex takes one integer argument and returns an then number
 *  of odd, even and zero digits.
 *  flex(111022) -> [3,2,1] i.e [odd,even,zero's]
 * ============================================================================
 */
    int*
flex (long int num)
{
    int res;
    int even = 0;
    int odd = 0;
    int zero = 0;

    while(num != 0){
        res = num % 10;
        if(res == 0){
            zero++;
        }
        else if(res % 2 == 0){
            even++;
        }
        else{
            odd++;
        }
        num = (num - (num % 10))/10;
    }
    int oez[3] = {odd,even,zero};
    return oez;
}       /* -----  end of function flex  ----- */

/*
 * ===  FUNCTION  =============================================================
 *         Name:  test_flex
 *  Description:  test suite for flex function
 * ============================================================================
 */
    void
test_flex ( )
{
    assert(flex(1)[0] == 1);
    assert(flex(1)[1] == 0);
    assert(flex(1)[2] == 0);

    assert(flex(112220)[0] == 2);
    assert(flex(112220)[1] == 3);
    assert(flex(112220)[2] == 1);

}       /* -----  end of function test_flex  ----- */

/*
 * ===  FUNCTION  ======================================================================
 *         Name:  get_integer
 *  Description:  checks if input is integer
 * =====================================================================================
 */
    long int
get_integer ()
{
    while(true){
        stringstream converter;
        converter << get_line();

        int long result;
        if(converter >> result){
            char remaining;
            if(converter >> remaining)
                cout << "Unexpected character: " << remaining << endl;
            else{
                return result;
            }
        } else
            cout << "Please enter an integer." << endl;

        cout << "Retry:" << endl;
    }
}       /* -----  end of function get_integer  ----- */

/*
 * ===  FUNCTION  ======================================================================
 *         Name:  get_line
 *  Description:  wraps getline
 * =====================================================================================
 */
    string
get_line()
{
    string result;
    getline(cin,result);
    return result;
}       /* -----  end of function get_line  ----- */


texts: Implementing carryless multiplication using normal multiplication

texts:
In carryless multiplication the partial products are XORed (ie addition without carry) together rather than added normally. The partial products themselves are still the product of a power of two (namely a bit extracted from one operand) and the other operand. For a multiplication by a power of two there is no difference between carryless multiplication and regular multiplication, so this step can be done with a normal multiplication. But then in JavaScript the question arises, which multiplication? It could be written like this:

function clmul_u32(a, b) {
    var prod = 0;
    while (a != 0) {
        prod ^= Math.imul(b, a & -a);
        a &= a - 1;
    }
    return prod;
}

Using Math.imul is correct by definition, so no problems there.

Or it could be written like this:

function clmul_u32(a, b) {
    var prod = 0;
    while (a != 0) {
        prod ^= b * (a & -a);
        a &= a - 1;
    }
    return prod;
}

This version avoids Math.imul, which I am often told to "avoid or polyfill", but it is less clear why this version should work. Multiplication by a power of two is actually safe, it cannot mangle the integer part of significand unlike multiplication by values that have simultaneously a low number of leading zeroes and a low number of trailing zeroes. I feel like if I used this version, I would have to write a long-winded comment justifying its correctness.

So which way should I go? Or perhaps there are better ways entirely?


texts:
PRIME1 is a CodeChef problem which states:

  Shridhar wants to generate some prime numbers for his cryptosystem.
  Help him! Your task is to generate all prime numbers between two given
  numbers.

  Input

  The first line contains t, the number of test cases (less then or
  equal to 10). Followed by t lines which contain two numbers m and n (1
  <= m <= n <= 1000000000, n-m<=100000) separated by a space.

  Output

  For every test case print all prime numbers p such that m <= p <= n,
  one number per line. Separate the answers for each test case by an
  empty line.

I have used segmented sieve and my code doesn't give TLE on any CodeChef IDE etc. yet I am getting a TLE. How can I optimise this code?

import math
max_n=10**9
m = int(math.sqrt(max_n))
arr = [1]*m
arr[0] = arr[1] = 0
#Calculate primes upto math.sqrt(max_n)
for x in xrange(m):
    for i in xrange(2,int(x**0.5)+1):
        if x%i==0:
            arr[x]=0
            break
list_prime=[]
for i,each in enumerate(arr):
    if each==1:
        list_prime.append(i)

#sieve b/w m and n
for _ in xrange(int(raw_input().strip())):
    m,n = map(int,raw_input().split())
    arr = range(m,n+1) if m>1 else range(2,n+1)
    for each in list_prime:
        firstfactor = (arr[0]/each)*each
        for x in xrange(firstfactor,n+1,each):
            try:
                if x!=each:
                    arr.remove(x)
            except:
                pass
    for each in arr:
        print each
    print


texts:
I'm writing a helper for asynchronous operations.
By default, winjs does not provide a way to wait x seconds before the execution of another function. So I decided to write a helper.

(function asyncOperation() {
    "use strict";

    // -------------------------------------------------------------------
    //   awaitTime(time_milliseconde)
    //  Description:
    //   Waits a specified amount of time.
    //  Returns:
    //   //
    //  Usage:
    //   foo.helpers.asyncOperation.awaitTime(1000).done(function(){
    //      //your logic
    //   });
    //
    // -------------------------------------------------------------------
    function awaitTime(milliseconds) {
        if(!milliseconds || +milliseconds) {
            // need to ensure that milliseconds can be used
            milliseconds = 0;
        }

        // create a new promise and use set time out to wait x milliseconds.
        return new WinJS.Promise(function (complete) {
            setTimeout(function () {
                // informe the promise that it ended.
                complete();
            }, milliseconds);
        });
    }

    WinJS.Namespace.define("foo.helpers.asyncOperation", {
        awaitTime: awaitTime
    });
})();

I want my code to be as safe as possible. That's why I'm using "+value" in my "if".
I saw that in some cases the performance of the setTimeout is not good.
Also, is it better to define the function and assign it to the namespace at the end? (http://msdn.microsoft.com/en-us/library/windows/apps/hh967793.aspx)

So, I want to know if my code can be refactored/safer.


texts: A lightweight CRUD, based on the Table Data Gateway pattern

texts:
In order to reduce the boilerplate code, I decided to write a lightweight database helper, based on the  Table Data Gateway pattern (or at least the way I understand it) with the aim of using it for a simple CRUD application.
Writing INSERT and UPDATE queries is always a nuisance, repeating all those ranks of column names, parameter names, binding parameters and such. So I decided to write a simple class that does all the job under the hood, and accepts/returns simple arrays, without going down the rabbit hole of creating a full featured ORM.
My aim was also to make this class as secure as possible, hence the table and field names are always hardcoded.
It consists of one prototype class,
abstract class BasicTableGateway
{
    protected $db;
    protected $table;
    protected $fields;
    protected $primary = 'id';

    public function __construct(\PDO $db)
    {
        $this->db = $db;
    }
    public function listBySQL(string $sql, array $params): array
    {
        $stmt = $this->db->prepare($sql);
        $stmt->execute($params);
        return $stmt->fetchAll();
    }
    public function list(int $page = 1, int $limit = 50): array
    {
        $page--;
        $offset = $page * $limit;
        $stmt = $this->db->prepare("SELECT * FROM `$this->table` LIMIT ?,?");
        $stmt->execute([$offset,$limit]);
        return $stmt->fetchAll();
    }
    public function read($id): ?array
    {
        $ret = $this->listBySQL("SELECT * FROM `$this->table` WHERE `$this->primary`=?", [$id]);
        return $ret ? reset($ret) : null;
    }
    public function update(array $data, int $id): void
    {
        $this->validate($data);

        $params = [];
        $set = "";
        foreach($data as $key => $value)
        {
            $set .= "`$key` = ?,";
            $params[] = $value;
        }
        $set = rtrim($set, ",");
        $params[] = $id;
        $sql = "UPDATE `$this->table` SET $set WHERE `$this->primary`=?";
        $this->db->prepare($sql)->execute($params);
    }
    public function insert($data): int
    {
        $this->validate($data);

        $fields = '`'.implode("`,`", array_keys($data)).'`';
        $placeholders = str_repeat('?,', count($data) - 1) . '?';

        $sql = "INSERT INTO `$this->table` ($fields) VALUES ($placeholders)";
        $this->db->prepare($sql)->execute(array_values($data));

        return $this->db->lastInsertId();

    }
    public function delete($id)
    {
        $sql = "DELETE FROM `$this->table` WHERE `$this->primary`=?";
        $this->db->prepare($sql)->execute([$id]);
    }
    protected function validate($data)
    {
        $diff = array_diff(array_keys($data), $this->fields);
        if ($diff) {
            throw new \InvalidArgumentException("Unknown field(s): ". implode($diff));
        }
    }
}

and children classes where related properties are set to operate certain tables:
class UserGateway extends BasicTableGateway {
    protected $table = 'users';
    protected $fields = ['email', 'password', 'name', 'birthday'];
}

which is then used like this
$data = [
    'email' => 'foo@bar.com',
    'password' => 'hash',
    'name' => 'Fooster',
];
$id = $userGateway->insert($data);
$user = $userGateway->read($id);
echo json_encode($user),PHP_EOL;

$userGateway->update(['name' => 'Wooster'], $id);
$user = $userGateway->read($id);
echo json_encode($user),PHP_EOL;

$users = $userGateway->list();
echo json_encode($users),PHP_EOL;

$userGateway->delete($id);

The code works all right for a simple CRUD but I'd like to know whether it can be improved or may be there are some critical faults I don't realize.


texts: Find height of a tree without using recursion

texts:
I want you to pick my code apart and give me some feedback on how I could make it better or simpler.

public int heightHelper(TreeNode node) {
    int height = -1;

    if (node == null) return height;

    final Queue<TreeNode> queue = new LinkedList<TreeNode>();
    queue.add(node);

    int currentLevelNodeCount = 1;
    int nextLevelNodeCount = 0;

    while (!queue.isEmpty()) {
        TreeNode current = queue.poll();
        currentLevelNodeCount--;

        if (current.left != null) { queue.add(current.left); nextLevelNodeCount++; }

        if (current.right != null)  { queue.add(current.right);  nextLevelNodeCount++; }

        // current level is done.
        if (currentLevelNodeCount == 0) {
            height++;
            currentLevelNodeCount = nextLevelNodeCount;
            nextLevelNodeCount = 0;
        }
    }
    return height;
}


texts: Image upload script more cleaner and secure using php 7 oop style.

texts:
I want to utilize namespaces and make the code on this file uploading script more secure, and cleaner looking, here is what i have so far

it uploads an image and inserts it into a database.

I want to be able to use namespaces within view files, without it looking messy.

i know i can use laravel blade etc, but for the sake of sharpening my php skills, i want to be able to do this from scratch.

Image.php

<?php

require 'Db.php';

class Image{

    private $dbh;
    private $connect;
    public $directory;
    public $uploadfile;

    public function __construct()
    {
        $this->dbh = new Db();
        $this->connect =  $this->dbh->connect();
        $this->directory = "uploads/";
    }

    public function upload_image($filename, $image_name)
    {

        $this->uploadfile = $this->directory . basename($filename);

        try{
            $stmt = $this->connect->prepare("INSERT INTO images (img, image_name) VALUES (:img, :imagename) ");
            $stmt->bindparam(':img', $this->uploadfile);
            $stmt->bindparam(':imagename', $image_name);
            $stmt->execute();
            return $stmt;
        }
        catch(PDOExeception $e)
        {
            echo $e->getMessage();
        }

    }

    public function get_image()
    {
        return $this->uploadfile;
    }

}

upload_image.php (view file)

    <form action="/ImgUpload.php" enctype="multipart/form-data" method="post">

            File input
            <input type="file" name="profile_img" class="form-control-file" id="exampleInputFile" aria-describedby="fileHelp">
            <input type="text" name="image_name" placeholder="Enter Name Of File" class="mt-3">
            This is some placeholder block-level help text for the above input. It's a bit lighter and easily wraps to a new line.
            <button type="submit" class="btn btn-primary">Submit</button>

        </form>

ImgUpload.php

<?php
session_start();
require_once 'Image.php';

if(isset($_FILES['profile_img'])){

    $image = new Image();

    $filename = $_FILES["profile_img"]["name"];
    $realname = "uploads/" . basename($filename);
    $directory = "/Applications/MAMP/htdocs/eliphp1/uploads/";

    $path = $directory . basename($filename);

    $image_name = $_POST['image_name'];

    if($image->upload_image($realname, $image_name)){

        move_uploaded_file($_FILES["profile_img"]["tmp_name"], $path);

        echo 'get_image().'">';

    };

};

?>


texts: Searching within multiple objects of the bucket

texts:
I have millions of files in the Google cloud storage's bucket. I want to search within files with a .index extension and retrieve the contents.

This is currently what I am doing, but the time required for overall process is large. Is there any better and faster way to do this?

class storage():
    ...
    self.indexFile = []
    self.indexFileIndex = []
    ...

    def get_content(self,param1,param2):
        c_n = []
        for value, index in zip(param1, param2):
            object_contents = StringIO.StringIO()
            srcObjURI = boto.storage_uri(value, self.storage)
            srcObjURI.get_key().get_file(object_contents)
            c_n.append(object_contents.getvalue())
            object_contents.close()
        return c_n

    def get_PATHs(self):
        paths=[]
        #pts=open("paths.txt","w")
        pts_log=open("paths_log.txt","a")
        pts_log.write("-"*20+time.ctime()+"-"*20+"\n")
        indexFileContents = self.get_content(self.indexFile, self.indexFileIndex)
        for c,d in zip(indexFileContents,self.indexFile):
            regx = r"(.*)\/" + r"(.*?)\|"
            patternPathList = re.compile(regx)
            for match in patternPathList.finditer(c):
                p=match.group(1).strip() + "/"+ match.group(2).strip()
                tst_exst=""
                if p in paths:
                    tst_exst="Already exist !"
                else:
                    tst_exst="Added to PATHs list"
                    paths.append(p)
                    #pts.write(p)
                    #pts.write("\n")
                pts_log.write("FROM : %s --> %s %s"%(d,p,tst_exst))
                pts_log.write("\n")
        #pts.close()
        pts_log.close()
        return paths

The files that I am trying to search varies from 200KB to 1MB and sometimes has Unicode characters.


texts: Turning JSON objects into custom NSObjects

texts:
Was advised to post this question here after posting to Stack Overflow. I'm pre-populating a UITableView app with data using a local JSON file. The JSON itself is very simple:

[
    {"Name":"Tennis Ball Launchers",
    "Type":"Forces"},

    {"Name":"Magnetic Hot Plates",
    "Type":"Magnetism"},

    {"Name":"Giant Lever",
    "Type":"Forces"},

    {"Name":"Music Wall",
    "Type":"Sound"},

    {"Name":"Water Bearing",
    "Type":"Forces"},

    {"Name":"Chaos Pendulums",
    "Type":"Chaos"}
]

The JSON objects will have more properties further down the line, this is just for starters (there will be around 200 entries by the end, with more properties). I don't imagine the structure getting any more complex, however. The user will not be adding or deleting entries in the app - the JSON is read only. I want to map these JSON objects into an array of custom NSObjects. Here is the code I am currently using in the initialisation of my data model:

    // JSON reading into array
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];

    NSError *error;

    NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

    if(error) {
        NSLog(@"Error reading file: %@", error.localizedDescription);
    }

    // Get JSON objects into initial array
    NSArray *rawExhibits = (NSArray *)[NSJSONSerialization JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL];

    NSMutableArray *myItems = [[NSMutableArray alloc] init];

    for (NSDictionary *object in rawExhibits) {
        NSString *objectName = object[@"Name"];
        NSString *objectType = object[@"Type"];

        // Create new exhibit object with data procured from JSON object
        W5BExhibit *exhibit = [[W5BExhibit alloc] initWithExhibitName:objectName exhibitType:objectType];

        [myItems addObject:exhibit];
    }

    self.allExhibits = [[NSArray alloc] initWithArray:myItems];

The table view can then refer to the allExhibits variable which is a property of a singleton. I'm very new at this so I'm canvassing for opinions.

I was also advised to consider using keyed archiving to persist the data instead of parsing the JSON each time the app is opened up from new.


texts: Transform a javascript object to an array based on value of each key in the original object

texts:
I have an object of ingredients to be added to a burger. Its structure is as follows:

Input

    ingredients: {
        salad: 1,
        cheese: 2,
        meat: 2,
    }

I want to transform it to an array of JSX elements. Each key in the original object has to be mapped to the number of JSX elements equivalent to the value of the ingredient in the ingredients objects. So the result array should look like so:

Output

[
    <BurgerIngredient type="salad" />,
    <BurgerIngredient type="cheese" />,
    <BurgerIngredient type="cheese" />,
    <BurgerIngredient type="meat" />,
    <BurgerIngredient type="meat" />,
]

My solution is as follows:

function getBurgerIngredients() {
    const ingredientNames = Object.keys(props.ingredients);
    const transformedIngredients = ingredientNames.map( (ingredientName) => {
        let burgerIngredientElements = [];
        for(let i=0; i<props.ingredients[ingredientName]; i++) {
            burgerIngredientElements.push(
                <BurgerIngredient type={ingredientName} />
            )
        }
        return [...burgerIngredientElements]
    });

    return transformedIngredients;
}

I'm aware that it's not the most elegant solution. Can anyone suggest any improvement(s)?


texts: Django query filter on count in one to many relation

texts:
I have the following models:

class Donation(models.Model):
    donation_amount = models.DecimalField(max_digits=9, decimal_places=2)
    donor_name = models.CharField(max_length=200)

class Charge(models.Model):
    donation = models.ForeignKey(Donation)
    error = models.TextField(null=True, blank=True)

And I have a complex query that should do one of the following:

  Get all donations or all donations which has at least one failed charge.

I came up with the following solution:

# get all donations with charge prefetched
all_donations = Donation.objects.all().prefetch_related('charge_set')

# sometimes filter for failed ones
if need_failed_charges:
  all_donations = all_donations.annotate(count=Count('charge')) \
            .filter(charge__error__isnull=False, count__gt=0)

Here I use the count to track the number of charges I have per donation, I filter them afterward to check if I have at least one.

I think there is a better way to express this, but as I'm still new to the django world I cannot see it.

What do you think?


texts:
I have 2 s being used for displaying the votes, which is up votes and down votes. There is no problem for the adding or removing votes, but the switching of votes is the issue (not really an issue).

The structure of the votes in the HTML is:

The values are retrieved from the database and I user innerHTML to insert the data between the  tags. Once the user clicks the , either the up_vote or the down_vote, the update request is sent through ajax and updates the vote counts, and once it is successfully executed, the value inside the  will be incremented by one, as well as the removing of vote, it will be decremented. And a voted class will be added once the user votes.

My JavaScript looks like this:

$("#up_vote, #down_vote").click(function(e) {
  var voteCount = document.getElementById(e.target.id).innerHTML;
  var action;
  var value;

  if ($("#up_vote").hasClass("voted") || $("#down_vote").hasClass("voted")) {
    if ($(this).hasClass("voted")) {
      action = "remove_vote";
      setValue();
    } else {
      action = "switch_vote";
      setValue();
    }
  } else {
    action = "add_vote";
    setValue();
  }

  function setValue() {
    if (e.target.id === "up_vote") {
      value = 1;
    } else if (e.target.id === "down_vote") {
      value = -1;
    }
  }

  $.ajax({
    method: "GET",
    url: "votes.php",
    data: , //it includes the user session id, the post id, and the action and values for doing the specific functions (i just didn't put it here, just giving the logic on what does my code looks like
    cache: false,
    success: function(msg) {
      if (msg === "vote_added") {
        document.getElementById.innerHTML = parseInt(voteCount) + 1;
        $("#" + e.target.id).addClass("voted");
      } else if (msg === "vote_removed") {
        // the same function of the vote_added message, but instead, it decrements the value and removes the "voted" class
      } else if (msg === "vote_switched") {
        // these is the part that i want to achieve, but coding it seems a bit long (just my thought)
      } else if (msg === "vote_exist") {
        // these is a, some sort of notifying the user that the he/she already votes the specific post
      }
    }
  })
})

As on what you see on the JavaScript, it sends an action and value (which will specify if it is up vote or down vote, which will be used to do specific functions inside the PHP. It will then echo out the message that will be used once the query is executed successfully.

(On the last condition, vote_exist, is used for security check. While I'm testing something out, removing the class="voted" (which notifies that the user already voted) inside Inspector window, clicking these back will increment the vote count again, which removes the restrictions, so I added a validation function inside the PHP before the user votes for doubled security check. Hope that these can help for others.)

My concern is about the 3rd condition, which is the switching of votes. For example, the user already up votes, and then he/she wants to down vote. There is no problem for the queries, just for the JavaScript part. I have an idea, though: by using the same functions inside the vote_added and vote_removed, where, when clicked, will target id will be incremented at the same time the other one will be decremented, also adds a voted class to the target id and removes the other one.

How can I do this in a shorter way?


texts: Recursive function and memorization to find minimum operations to transform n to 1

texts:
I'm a new, self-taught programmer working on the Google FooBar challenge.  I've submitted my answer (code at bottom) and it was accepted, but I'd like suggestions on how to improve my solution.

Challenge: return minimum number of operations to transform a positive integer to 1. Valid operations are: n+1, n-1, or n/2.

My solution
I started with a recursive function, but ran into a runtime error with very large numbers.  I added memorization using a global variable to store values that had already been computed, but this seems inelegant.  (I think it's discouraged to use global variables?)

Suggestions on how I might improve the below solution?

paths = {1:0, 2:1}

def shortest_path(num):
    if num in paths:
        return paths[num]

    if num % 2 == 0:
        paths[num] = 1 + shortest_path(num / 2)
    else:
        paths[num] = min(2 + shortest_path((num+1)/2),
                         2 + shortest_path((num-1)/2))
    return paths[num]

def answer(n):
    num = int(n)
    return shortest_path(num)

Test cases:

n = 15 --> 5
n = 293523 --> 25
n = 191948125412890124637565839228475657483920292872746575849397998765432345689031919481254128901246375658392284756574839202928727465758493979987654323456890319194812541289012463756583922847565748392029287274657584939799876543234568903 --> 1029

The input number can be up to 309 digits long hence the final test case


texts: Find a prefix of a query string in the values of a Map

texts:
I have a hash map which maps to some strings which serve as prefixes and are of small length (max length is 6):

Map<String, String> map = new HashMap<>();
map.put("codeA", "100");
map.put("codeB", "7");
map.put("codeC", "0012");
etc

This is fine so far, but I also need when provided an input string to actually break it into 2 parts if the string has a prefix that matches one of the values in my map.

What I do is:

boolean found = false;

String [] result;
for(Entry<String, String> e: map.entrySet()) {
   String code = e.getKey();
   String value = e.getValue();
   if(value.length >= inputString.length) continue;
   if(inputString.startsWith(value)) {
       result = new String[2];
       result[0] = value;
       result[1] = inputString.substring(value.length + 1);
       found = true;
       break;
     }
}
return result;

Could this be improved? Could I have been using some additional datastructure/API for this?

I am interested in an approach in Java 7 without any extra libs. The HashMap has ~400 entries and the input string 8-11 characters.

I would need a way to get the prefix having the code (hence the HashMap) and break the input string into the prefix and the rest part.


texts: Using shims to turn my integration tests into unit tests

texts:
I want to understand how to properly unit test a function such as this. The function doesn't return anything, but it does call other functions that themselves have already been unit tested. My understanding is that in order to properly unit test this method I will have to shim the dependant methods - otherwise this is just an integration test.

public void ResetColours(Colours colour)
{
    var colourInfo = colour.GetInfo();
    for (int i = 0; i < 9; ++i)
        this.SetColourInfo(i, colourInfo);
}

I'm having a hard time deciding on the right way to shim the methods. At the moment I am thinking of doing this - shim the GetInfo method so that it returns a hard coded value, then have the SetColourInfo shim remember the information that was passed in to make sure it matches.

[TestMethod]
public void ResetColours()
{
    Info[] colourInformation = new Info[9];

    using (ShimsContext.Create())
    {
        ShimColourExtensions.GetInfoColours = (Colours colour) => new Info("test");
        ShimFace.AllInstances.SetColourInfoIntInfo =
            (index, colourInfo) => colourInformation[index] = true;

        this.TestObject.ResetColours(Colours.Brown); //TestObject was initialized in my init method
        //ASSERT that hard coded GetInfo value was set into each array index
    }
}

Is this a valid unit test? Could I be doing anything better in situations like this? Should I even be writing a unit test for a method like this?


texts:
This is my second program in Python. I am aware that globals are a bad idea, but that is what I have learned so far in my course.

Right now the program is doing what it's supposed to do, my only concern is that it might be too long for nothing. I'm asking you to help me figure out where I could make it simpler.

import simplegui
import random
import math

#Globals, random guess and player's remaining guesses

num_range = 100
remaining_guesses = 7

# New game, range from 0 - 100 by default
# after the according button is pressed
# Reset the remaining guesses to 7

def range100():
    global num_range
    global remaining_guesses
    remaining_guesses = 7
    print "New game. Range is from 0 to 100"
    print "Number of remaining guesses is %i" %(remaining_guesses)
    print ""
    num_range = random.randrange(0, 101)
    return num_range

# Set game to a range of 0 - 1000
# after the according button is pressed
# Reset the remaining guesses to 10

def range1000():
    global num_range
    global remaining_guesses
    remaining_guesses = 10
    print "New game. Range is from 0 to 1000"
    print "Number of remaining guesses is %i" %(remaining_guesses)
    print ""
    num_range = random.randrange(0, 1001)
    return num_range

# Compare guess to random range
# Remove 1 to remaining guesses
# Display hint on the random range
# Restart the game if player is correct or out of guesses

def input_guess(guess):
    guess = float(guess)
    global remaining_guesses
    global num_range
    remaining_guesses -= 1
    print "Your guess was: %i" %(guess)
    print "Number of remaining guesses is %i" %(remaining_guesses)
    if (remaining_guesses == 0 and guess != num_range):
        print "The correct answer was %i" %(num_range)
        print "Let's try again!"
        print ""
        print ""
        range100()
    elif (remaining_guesses == 0 and guess == num_range):
        print "Correct!"
        print ""
        print ""
        range100()
    elif guess > num_range:
        print "Try a lower number!"
    elif guess < num_range:
        print "Try a higher number!"
    else:
        print "Correct!"
        print ""
        print ""
        range100()
    print ""
    return guess, remaining_guesses

# Create & start frame

f = simplegui.create_frame("Guess the number", 200, 200)

f.add_button("Range is [0, 100]", range100, 200)
f.add_button("Range is [0, 1000]", range1000, 200)
f.add_input('My label', input_guess, 50)
f.start()

# Start a new game

range100()


texts: One-by-one file deletion for a derived list of files

texts:
I have the following C# code for deleting files from a folder (and from its sub folders) excluding a list of files specified in an Excel file. The code works fine.

The deletion is happening one-by-one in a for loop. Is there a way to improve its performance by reducing the time taken for deletion?

class Program
{
    public static List<string> AllFilesStaticList = new List<string>();

    static void Main(string[] args)
    {

        string locationOfExcelList = @"G:\Test1\ExclusionList.xls";
        string sheetName = "List$";
        string sourceDirectory = @"G:\TFS2\";

        List<string> excludeList = Import_To_DataTable_FromExcel(locationOfExcelList, sheetName);
        ProcessAllDirectoryToGetFileList(sourceDirectory);

        //Get files to be deleted (Items that are not present in excludeList)
        List<string> eligibleListToProcess = AllFilesStaticList.Where(x => !excludeList.Any(y => x.Contains(y))).ToList();

        //Iterating two lists - need to be improved
        foreach (string fileEntry in eligibleListToProcess)
        {
            try
            {
                if (File.Exists(fileEntry))
                {
                    //foreach delete need to be made more efficient
                    File.Delete(fileEntry);
                    Console.WriteLine(fileEntry);
                }
            }
            catch
            {
                //Hide the exception for now
            }
        }
        Console.ReadLine();
    }

    // Process all files in the directory passed in, recurse on any directories
    // that are found, and process the files they contain.
    public static  void ProcessAllDirectoryToGetFileList(string targetDirectory)
    {
        // Process the list of files found in the directory.
        //Get files with their path
        string[] fileEntries = Directory.GetFiles(targetDirectory);
        foreach (string fileName in fileEntries)
        {
            AddFile(fileName);
        }

        // Recurse into subdirectories of this directory.
        string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach (string subdirectory in subdirectoryEntries)
        {
            ProcessAllDirectoryToGetFileList(subdirectory);
        }
    }

    public static void AddFile(string fileName)
    {
        //Console.WriteLine();
        AllFilesStaticList.Add(fileName);
    }

    private static List<string> Import_To_DataTable_FromExcel(string FilePath,string sheetName)
    {
        List<string> deploymentFiles = new List<string>();

        string conStr = null;
        conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text'";

        conStr = string.Format(conStr, FilePath);
        OleDbConnection connExcel = new OleDbConnection(conStr);
        OleDbCommand cmdExcel = new OleDbCommand();
        OleDbDataAdapter oda = new OleDbDataAdapter();

        cmdExcel.Connection = connExcel;

        connExcel.Open();
        DataTable dtExcelSchema = default(DataTable);
        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

        connExcel.Close();

        //Read Data from First Sheet
        connExcel.Open();
        cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
        oda.SelectCommand = cmdExcel;

        DataTable deploymentListTable = new DataTable("deploymentListTable");
        oda.Fill(deploymentListTable);
        connExcel.Close();

        foreach (DataRow row in deploymentListTable.Rows )
        {
            //Read third column from the excel sheet
            string cellFileName = row[2].ToString();
            if (!String.IsNullOrEmpty(cellFileName))
            {
                deploymentFiles.Add(cellFileName);
            }
        }

        return deploymentFiles;

    }

}


texts: Could this be considered a valid mergesort?

texts:
I tried to do a mergesort function for an assignment without importing merge from heapq:

def mergesort(a):
    if len(a) < 2:
        return a
    return mymerge(mergesort(a[len(a) // 2:]), mergesort(a[:len(a) // 2]))

def mymerge(list1, list2):
    for i in range(len(list1)):
        for x in range(len(list2)):
            if list1[0] < list2[x]:
                list2.insert(x, list1[0])
                list1.pop(0)
                break
            if list1[0] > list2[len(list2) - 1]:
                list2.append(list1[0])
                list1.pop(0)
                break
    return list2

Could this be considered a valid mergesort or does my function have flaws? Are there ways to improve the mymerge function?


texts:
I am using this script to get the admin pages via this query string ?page= once the admin is logged in.

Just needing a review, opinions

Function:

// GET PAGE FROM QUERY STRING
public function page_select($page) {
    $ext = '.php';
    $file = 'pages/'.$page.$ext;
    if(file_exists($file)) {
        return $file;
    }
    else {
        $file = 'pages/404'.$ext;
        return $file;
    }
}

Index.php

if(isset($_COOKIE["Cckiuas"]) && isset($_SESSION["loggedin"])) {
    if(isset($_GET['page'])) { $page = trim(htmlentities($_GET['page'])); }
    else { $page = 'dashboard'; }
    include($backend->get_base_url().INCLUDES."header.php");
    include($backend->page_select($page));
    include($backend->get_base_url().INCLUDES."footer.php");
}


texts:
What needs to be corrected, added, or subtracted here?

class mutexLocker
{
    private:
    /* Declaration of a Mutex variable `mutexA`. */
    pthread_mutex_t &mutexA;

    /* `mutexStatus` holds the return value of the function`pthread_mutex_lock `.
    This value has to be returned to the callee so we need to preserve it in a class
    variable. */
    int             mutexStatus;

    public:
    /* Constructor attempts to lock the desired mutex variable. */
    mutexLocker (pthread_mutex_t argMutexVariable)
    : mutexA (argMutexVariable)
    {
        /* Return value is needed in order to know whether the mutex has been
        successfully locked or not. */
        int mutexStatus = pthread_mutex_lock (&argMutexVariable);
    }

    /* Since the constructor can't return anything, we need to have a separate function
    which returns the status of the lock. */
    int getMutexLockStatus ()
    {
        return mutexStatus;
    }

    /* We may need this Mutex variable as an argument for `pthread_cond_wait()`*/
    pthread_mutex_t getLockedMutex ()
    {
        if (mutexStatus >= 0)
            return mutexA;
    }

    /* The destructor will get called automatically whereever the callee's scope ends, and
    will get the mutex unlocked. */
    ~mutexLocker ()
    {
        if (mutexStatus >= 0)
             pthread_mutex_unlock (&mutexA);
    }
};


texts: Select the Enum to classify a string based on the first word found

texts:
I have written a small function that will check a string to see if it contains a certain word.

Depending on the word that is found first, the function will return the correct Enum value that needs to be used further down the line of the program.

Would there be a better way to check a string to see if it contains a certain Enum value?
The thing that gives me a little alarm bell in my head is that i am checking the same string 5 times in this function.

Enum

 public enum QuestionIdentifier
{

    Wie,
    Wat,
    Waarom,
    Wanneer,
    Hoe,
    Andere

}

Function

private QuestionIdentifier IdentifyQuestion(string vQuestion)
    {
        //QuestionToLower
        string vQTL = vQuestion.ToLower();

        if (vQTL.Contains("wie"))
        {
            return QuestionIdentifier.Wie;

        }else if (vQTL.Contains("wat"))
        {
            return QuestionIdentifier.Wat;

        }else if (vQTL.Contains("waarom"))
        {
            return QuestionIdentifier.Waarom;

        }else if (vQTL.Contains("wanneer"))
        {
            return QuestionIdentifier.Wanneer;

        }else if (vQTL.Contains("hoe"))
        {
            return QuestionIdentifier.Hoe;

        }else
        {
            return QuestionIdentifier.Andere;
        }

    }


texts: Faster algorithm to tailor given mathematical expression

texts:
Is there a more optimized solution to solve the stated problem?

Given an array 'arr' of 'N' elements and a number 'M', find the least index 'z' at which the equation gets satisfied. [ ] is considered as floor().

Code:

counts=0
ans=0
while(ans==0):
    s=0
    for i in range(counts,len(arr)):
        s+=int(arr[i]/(i+1-counts))
        if(s>M):
            break
    if((i+1)==len(arr) and s<=M):
        print(counts)
        ans=1
    counts+=1

Explanation:

Check array from left to right. The first index that satisfies the condition is the answer. This is more optimized than considering from right to left.
If at any time during the calculation, 's' is deemed more than M, break the loop and consider the next. This is more optimized than calculating 's' completely.

Example:

INPUT:

N=3 M=3

arr=[1 2 3]

OUTPUT:

0

This would give the answer 0 since the 0th index contains the first element to satisfy the given relation.

Thanks in advance.


texts:
Today I've been working on this algorithm to sort a list. It works with duplicated numbers. What do you think about it? How can I improve it? Does it already exist? If it does, what is its name?

a = [1,1,5,3,2,3,4,3,99,99]  # The list I'm going to sort.
b = [None] * len(a)
x = 0
for number in a:
    for num in a:
        if number >= num:
            x += 1
    b[x-1] = number
    x = 0
b = b[::-1]
print(b)
for number in b:  #This part of the code is for the duplicated numbers.
    if number == None:
        print(b.index(number))
        b[b.index(number)] = b[b.index(number) - 1]
b = b[::-1]
print(b)


texts: Advent of Code 2018 Day 4 - Pythonic Sleepy Guards

texts:
Introduction

I'm using Advent of Code 2018 to learn Python better, interested in the new type support for Python 3.7, I decided to go with that.

Here is my solution to Advent of Code Day 4, both part 1 and 2. I'm returning the answers for both part 1 and 2 as a tuple.

Problem Description

The problem essentially boils down to: Given a timestamped unsorted list of events of guards beginning their shift, waking up and falling asleep, determine the following:

Part 1: Which guard is asleep the most and on which minute is that guard mostly asleep? Return guard id multiplied by the minute number.

Part 2: Which guard is most frequently asleep on the same minute? Again, return guard id multiplied by the minute number.

For full problem description, please see Advent of Code Day 4

Concerns

I'm a big fan of Java 8 Stream API and C# Linq, I kind of expected Python to be more like that. I'm not sure if the nested function calls like sorted(list(...)) or len(list(...)) are "Pythonic". Likewise, it feels like I should be able to use some reducer-like function calls instead of imperatively looping through stuff to find the most common sleeper of some kind. Or is the way I have written this code the Python way to do it?

Code

from dataclasses import dataclass
from datetime import datetime
from days import read_file
from enum import Enum
from collections import defaultdict, namedtuple
from statistics import mode
import statistics
import operator
import re

class EventType(Enum):
    STARTS_SHIFT = 1
    FALLS_ASLEEP = 2
    WAKES_UP = 3

@dataclass
class Event:
    time: datetime
    guard: int
    event: EventType

@dataclass
class GuardSleep:
    sleep_total: int
    last_sleep: int
    sleeps: list

    def add_sleeps(self, minute):
        for i in range(self.last_sleep, minute):
            self.sleeps.append(i)

def get_guard(line: str):
    if "Guard" in line:
        guard_id = re.search("Guard #(\\d+)", line)
        return int(guard_id.group(1))
    return -1

def event_type(line):
    if "begins shift" in line:
        return EventType.STARTS_SHIFT
    if "falls asleep" in line:
        return EventType.FALLS_ASLEEP
    if "wakes up" in line:
        return EventType.WAKES_UP
    raise Exception("Unknown line: " + line)

def day4() -> (int, int):
    events = sorted(list(Event(datetime.strptime(line[1:17], "%Y-%m-%d %H:%M"), get_guard(line), event_type(line)) for line in read_file(4)), key=operator.attrgetter("time"))

    guard = -1
    guardsleep = defaultdict(lambda: GuardSleep(0, 0, []))
    for event in events:
        if event.guard >= 0:
            guard = event.guard

        if event.event == EventType.FALLS_ASLEEP:
            guardsleep[guard].last_sleep = event.time.minute

        if event.event == EventType.WAKES_UP:
            guardsleep[guard].sleep_total += event.time.minute - guardsleep[guard].last_sleep
            guardsleep[guard].add_sleeps(event.time.minute)

    most_sleepy_guard_number = max(guardsleep, key=(lambda key: guardsleep[key].sleep_total))

    most_sleepy_guard = guardsleep[most_sleepy_guard_number]
    part1_result = most_sleepy_guard_number * mode(sorted(most_sleepy_guard.sleeps))

    # Part 2
    MostSleepy = namedtuple('MostCommon', ['id', 'minute', 'amount'])
    most_sleepy = MostSleepy(0, 0, 0)
    for k in guardsleep:
        current_guard = guardsleep[k]
        try:
            most_common_minute = mode(sorted(current_guard.sleeps))
            amount = len(list((m for m in current_guard.sleeps if m == most_common_minute)))
            if amount > most_sleepy.amount:
                most_sleepy = MostSleepy(k, most_common_minute, amount)
        except statistics.StatisticsError:
            print("No unique most common minute for " + str(k))

    return part1_result, most_sleepy.id * most_sleepy.minute

if __name__ == '__main__':
    print(day4())


texts:
I need to create unique nonces for cryptographic purposes in a library that I am writing.  If the nonces ever fail to be unique, the consequences may be up to and including remote execution of arbitrary code (by means of allowing injection of untrusted input into an unmarshalling operation).

While my particular application does not need to be thread-safe yet, I want the code to be thread-safe so that it can work in multithreaded applications without modification.  I also want it to be fairly efficient – more so than (say) using a global lock for every operation.

The code has not (yet) been tested, mostly because I have no idea how to write a test case for multi-threaded (and mostly lock-free) code.  It makes the assumption that all thread-local storage is initialized to zero when a new thread is created – otherwise, it will fail miserably.

The header file nonce.h:

#ifndef NONCE_H_INCLUDED
#define NONCE_H_INCLUDED NONCE_H_INCLUDED
#pragma once
#ifdef __cplusplus
extern "C" {
#elif 0
}
#endif

typedef struct {
   char nonce[12];
} nonce;

nonce generate_new_nonce(void);

#if 0
{
#elif defined __cplusplus
}
#endif // defined __cplusplus

#endif // !defined NONCE_H_INCLUDED

And the implementation file nonce.c:

#include "nonce.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define STATIC_ASSERT(x)                                \
   (0*sizeof(struct {                                   \
      int static_assertion_failed : 1 - 2*((int)!(x)); \
      int dummy: 31;                                    \
   }))

static __thread uint32_t local_counter;
static __thread uint64_t local_snapshot;
static uint64_t global_counter;
static uintptr_t lock;

static uint64_t get_new_snapshot(void);

#ifdef __cplusplus
extern "C"
#endif
nonce generate_new_nonce(void) {
   STATIC_ASSERT(sizeof(local_counter) == 4);
   STATIC_ASSERT(sizeof(local_snapshot) == 8);
   STATIC_ASSERT(sizeof(global_counter) == 8);
   nonce retval;
   if (0 == local_counter) {
      local_snapshot = get_new_snapshot();
   }
   memcpy(retval.nonce, &local_counter, sizeof local_counter);
   memcpy(retval.nonce + 4, &local_snapshot, sizeof local_snapshot);
   local_counter += 1;
   return retval;
}

static uint64_t get_new_snapshot(void) {
   while(__sync_lock_test_and_set(&lock, 1)) {}
   const uint64_t retval = global_counter;
   global_counter++;
   if (global_counter == 0) {
      /* Wraparound */
      abort();
   }
   __sync_lock_release(&lock);
   return retval;
}


texts: How can I make my resource mapping faster?

texts:
I have a Direct3D 11 application and recently I've started to implement a new feature on it, the UI(User Interface).
It seems to work well, but I'm having one problem with optimization when it comes to moving stuff on my window, currently I can create a 2D square with textures and a 300x300 resolution.I can drag it around my screen by calling a function that updates the square position on my screen.
bool Model::UpdateModel2D(ID3D11DeviceContext* pDeviceContext, short NewX, short NewY)

{
if ((m_PreviousX == NewX) || (m_PreviousY == NewY)) // detects if the UI position is the same as before
    return true;
D3D11_MAPPED_SUBRESOURCE ms;
ModelData* pNewData;
ZeroMemory(&ms, sizeof(D3D11_MAPPED_SUBRESOURCE));

ModelData pData[6]; //Builds two triangles in order to form a square.

pData[0].VertexData = XMFLOAT3(NewX, NewY, 0.0f);
pData[0].TextureCoords = XMFLOAT2(0.0f, 0.0f);

pData[1].VertexData = XMFLOAT3(NewX + 300.0f, NewY + 300.0f, 0.0f);
pData[1].TextureCoords = XMFLOAT2(1.0f, 1.0f);

pData[2].VertexData = XMFLOAT3(NewX, NewY + 300, 0.0f);
pData[2].TextureCoords = XMFLOAT2(0.0f, 1.0f);

pData[3].VertexData = XMFLOAT3(NewX, NewY, 0.0f);
pData[3].TextureCoords = XMFLOAT2(0.0f, 0.0f);

pData[4].VertexData = XMFLOAT3(NewX + 300.0f, NewY, 0.0f);
pData[4].TextureCoords = XMFLOAT2(1.0f, 0.0f);

pData[5].VertexData = XMFLOAT3(NewX + 300.0f, NewY + 300.0f, 0.0f);
pData[5].TextureCoords = XMFLOAT2(1.0f, 1.0f);

hr = pDeviceContext->Map(m_pVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &ms);
if (FAILED(hr))
    return false;

pNewData = (ModelData*)ms.pData;
memcpy(pNewData, pData, sizeof(ModelData) * m_NumVertices); //updates the position of the 2D square
pDeviceContext->Unmap(m_pVertexBuffer, 0);

return true;
}

To drag this square I have to left click on it and move my mouse, but this is an issue because my application gets too slow, and it looks like the longer I hold it on my mouse, the slower the application gets, until it gets released. If someone knows how to improve updating my buffers, please help me.


texts: Design pattern review: freeze/thaw (aka, "reversible builder" pattern)

texts:
OK, this question may seem a little strange at first; however I'd like to have your comments on it.

Background: I do Java. A lot. Java is a statically typed language, it has means to restrict visibility of instance variables, etc. And as such the builder pattern (see here) is quite adapted to it.

I took this pattern a step further. The builder pattern is a one-step process: once you .build(), you obtain an instance, preferrably immutable; but once you have the instance, there is no going back. The freeze/thaw pattern is a "reversible builder".

I implemented it in Java and this gives the following interfaces, which I use quite a lot in my own projects (one-sided discussion about this pattern here):

// Frozen
@Immutable
public interface Frozen<T extends Thawed<? extends Frozen<T>>>
{
    /**
     * Return a thawed representation of this frozen object.
     *
     * @return a thawed object
     */
    T thaw();
}

// Thawed
@NotThreadSafe
public interface Thawed<F extends Frozen<? extends Thawed<F>>>
{
    /**
     * Obtain a frozen representation of this thawed object
     *
     * @return a frozen, immutable object
     */
    F freeze();
}

This pattern means that if you have a frozen instance f, you can obtained a thawed instance of that instance by calling .thaw(), modify it (since .thaw() returns a builder) and freeze it again:

// In java, again
final F newFrozen = frozen.thaw().setX().setY().etc().freeze();

I view this pattern as beneficial for several reasons:

thawing an instance gives you a builder with that instance's full state (and you can thaw as many times as you want, in a thread safe manner since frozen instances are immutable by contract);
you get all the power of builders on the thawed side.

I use this pattern a lot; I find it useful. Do you? What would be your gripes against it?

Sample implementation of a simple pair:

public final class MyClass
    implements Frozen<Builder>
{
    final int value;

    private MyClass(final Builder builder)
    {
        value = builder.value;
    }

    public static Builder newBuilder()
    {
        return new Builder();
    }

    public int getValue()
    {
        return value;
    }

    @Override
    public Builder thaw()
    {
        return new Builder(this);
    }

    public static final class Builder
        implements Thawed<MyClass>
    {
        private int value;

        private Builder()
        {
        }

        private Builder(final MyClass myClass)
        {
            value = myClass.value;
        }

        public Builder withValue(final int value)
        {
            this.value = value;
        }

        @Override
        public MyClass freeze()
        {
            return new MyClass(this);
        }
    }
}

For more complex cases, I generally create both classes in the same package and make instance variables for both package visible (this leaves of course the responsibility on me that what I inject into the frozen part is actually immutable, but I deal with it ;)

EDIT Right now the methods are called .thaw() and .freeze(), to reflect the pattern's intents; do you think of better names?

EDIT 2 Objections from @bowmore: the SRP (Single Responsibility Principle) is violated; this is true: the frozen instance has the added responsibility that it must generate a pre-filled thawed instance. The suggestion here is to create an additional constructor/static factory method/method (pick your poison) on the builder class so that it be able to "swallow" the contents of the frozen instance.


texts: Convert rows with multiple values to row with single value

texts:
I have a dataframe with measurement data of different runs at same conditions. Each row contains both the constant conditions the experiment was conducted and all the results from the different runs.

Since I am not able to provide a real dataset, the code snippet provided below will generate some dummy data.

I was able to achieve the desired output, but my function transform_columns() seems to be unecessary complicated:

import pandas as pd
import numpy as np

np.random.seed(seed=1234)

df = pd.DataFrame(np.random.randint(0, 100, size=(100, 6)), columns=['constant 1', 'constant 2', 1, 2, 3, 4])

def transform_columns(data):
    factor_columns = []
    response_columns = []
    for col in data:
        if isinstance(col, int):
            response_columns.append(col)
        else:
            factor_columns.append(col)
    collected = []
    for index, row in data.iterrows():
        conditions = row.loc[factor_columns]
        data_values = row.loc[response_columns].dropna()
        for val in data_values:
            out = conditions.copy()
            out['value'] = val
            collected.append(out)
    df = pd.DataFrame(collected).reset_index(drop=True)
    return df

print(transform_columns(df))

Is there any Pythonic or Pandas way to do this nicely?


texts: Slow python code that automatically selects k for KNNG

texts:
This code is used to select K for a K-nearest neighbor graph [KNNG] from a dataset with an unknown number of centroids (which is not the same as K-means clustering).

Suppose that you have a dataset of observations stored in a data matrix X[n_samples, n_features] with each row being an observation or feature vector and each column being a feature. Now suppose you want to compute the (weighted) k-Neighbors graph for points in X using sklearn.neighbors.kneighbors_graph. The first step is to pick k.

This brute force method to pick k below is very slow when the sample dataset size becomes large (ie n_neighbors_max = 10000). The FOR loop is very slow. Any tips for how to speed it up?

def autoselect_K(X, n_neighbors_max, threshold):
    # get the pairwise euclidean distance between every observation
    D = sklearn.metrics.pairwise.euclidean_distances(X, X)
    chosen_k = n_neighbors_max
    for k in range(2, n_neighbors_max):
        k_avg = []
        # loop over each row in the distance matrix
        for row in D:
            # sort the row from smallest distance to largest distance
            sorted_row = numpy.sort(row)
            # calculate the mean of the smallest k+1 distances
            k_avg.append(numpy.mean(sorted_row[0:k]))
        # find the median of the averages
        kmedian_dist = numpy.median(k_avg)
        if kmedian_dist >= threshold:
            chosen_k = k
            break
    # return the number of nearest neighbors to use
    return chosen_k


texts: Function to generate radom alphanumeric cookies

texts:
I wrote this python function to generate random values to use as id for the cookies I have on my project.

These are some examples of what the return is:

'kck9874118vq{5470553602125615945684311372005'
'72195739g112py3472118v104h122z5038a100d118v912296336399110nu110n60041909'

That is the method:

def generateCookieID(userName):

    cookieId = ''
    for a in range(0,9):
        aux = randint(0,9999)
        if aux % 2==0:
            for i in range(1,4):
                letter = randint(97,123)
                if letter%2==1:
                    cookieId += chr(letter)
                else:
                    cookieId += str(letter)+chr(letter)
        cookieId += str(aux)
    cookieJar['name'] = userName
    cookieJar['id'] = cookieId
    return cookieId

It works, but is it appropriate to use this way?
What could I do better?


texts:
I'm growing a web server in Haskell that interfaces with TCP sockets using network. To read the HTTP header of the client's message, I use the following function:

import           Network.Socket          hiding ( recv )
import           Network.Socket.ByteString      ( recv )

import qualified Data.ByteString               as S
import           Data.ByteString.UTF8          as BSU ( fromString )

readHeader :: Socket -> IO S.ByteString
readHeader sock = go sock mempty
where
  go sock prevContent = do
    newContent <- recv sock maxNumberOfBytesToReceive
    let content = prevContent <> newContent
    -- Read the data from the socket unless it's empty which means
    -- that the client has closed its socket or we see an empty line
    -- which marks the end of the HTTP header
    if S.null newContent || emptyLine `S.isSuffixOf` content
      then return content
      else go sock content
  where
    -- Only read one byte at a time to avoid reading further than the
    -- empty line separating the HTTP header from the HTTP body
    maxNumberOfBytesToReceive = 1
    emptyLine                 = BSU.fromString "\r\n\r\n"

The function only reads a byte at a time since I want to be able to read the rest of the client's message from the socket afterwards. But I'm curious whether the function could be more efficient.

My first idea was to read more bytes (say 1024) from the socket, check if those bytes contain an empty line, save that part (the HTTP header) in a ByteString and put the bytes after the empty line back into the socket's buffer. But I'm not sure whether it's possible or wise to put data back into the socket's buffer.

I'm also interested in any other code improvements that come to your mind.

The whole project is here.

Start it with stack ghci and then run main. Send it an HTTP request with curl http://localhost:8080/


texts: C++/STL Prime tuples (twin, cousin, sexy) up to the given limit

texts:
As I'm mastering STL I wrote a solution for generating all the prime tuples of the form (p1,p2) where "twin" means p2-p1=2, "cousin" p2-p1=4 and "sexy" p2-p1=6.

I'm interested in all the comments about the efficiency and style of this solution.

I use the following subroutine from utilities_template.h to print vector container:

template <class C>
void printContainer(C v, std::string msg = "Container", bool rowWise = true) {
  std::cout << msg << " " << std::endl;
  for (auto it = v.cbegin(); it != v.cend(); ++it)
    std::cout << std::setw(3) << *it << (rowWise ? " " : "\n");
  std::cout << std::endl;
}

And here is my solution:

#include <iostream>
#include <iterator>
#include <list>
#include <string>
#include <vector>
#include "utilities_template.h"

enum primeClassification { twin = 2, cousin = 4, sexy = 6 };

void removeElementsInListDivisibleByP(int p, std::list<int>& l) {
  l.remove_if([p](int n) { return n % p == 0; });
}

std::vector<int> getAllPrimesLessN(int n, bool debugMode = false) {
  using namespace std;
  list<int> l;
  for (int i = 2; i < n + 1; i++) {
    l.push_back(i);
  }
  int min_element = 2;  //
  vector<int> primes = {min_element};
  while (min_element != l.back()) {
    removeElementsInListDivisibleByP(min_element, l);
    if (debugMode)
      printContainer(
          l, "After divisible by " + std::to_string(min_element) + " removed:");

    min_element = *std::min_element(l.cbegin(), l.cend());
    primes.push_back(min_element);
  }

  auto temp_it =
      adjacent_find(primes.cbegin(), primes.cend(),
                    [](const int& a, const int& b) { return (b - a) == 1; });

  printContainer(primes, "Primes up to " + std::to_string(n));

  return primes;
}

std::vector<std::pair<int, int> > formPrimePairs(primeClassification pClass,
                                                 std::vector<int>& primes) {
  using namespace std;

  std::vector<std::pair<int, int> > pairs;
  for (auto it = next(primes.cbegin(), 1); it != primes.cend(); it++) {
    if (*it - *std::prev(it, 1) == pClass) {
      auto this_pair = make_pair(*prev(it, 1), *it);
      pairs.push_back(this_pair);
    }
  }

  return pairs;
}

void s4PrimePairs() {
  using namespace std;
  int n = 0;
  cout << "Please enter the limit: ";
  cin >> n;

  vector<int> primes = getAllPrimesLessN(n);

  printVectorOfPairs(formPrimePairs(primeClassification::twin, primes),
                     "\nTwin pairs (delta=2): ", true);

  printVectorOfPairs(formPrimePairs(primeClassification::cousin, primes),
                     "\nCousin pairs(delta=4): ", true);

  printVectorOfPairs(formPrimePairs(primeClassification::sexy, primes),
                     "\nSexy pairs(delta=6): ", true);
}


texts: Reduce a string using grammar-like rules

texts:
I've got a program in Java which simplifies input string using grammar-like rules. Length of string is up to 200 and number of rules is up to 300. In big cases it works too slow, so I need some advice on time optimization of the code.

Example input: 4 4; a b a b; a b -> b; a b -> c; b a -> a; c c -> b

Program show all the simple chars that can be received from the input string a b a b using the rules. For this example it is b c. For string of length 155 and number of rules 92 the algorithm should work 0.05 seconds faster.

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {

    static int n, p;
    static String input;
    String[] textStr;
    static BufferedReader buff;
    static Map<String, Set<String>> cache = new HashMap();
    static Map<String, ArrayList<Character>> rules = new HashMap();

    public static void main(String[] args) {
        buff  = new BufferedReader(new InputStreamReader(System.in));
        try{
            String tem = buff.readLine();
            String textStr[] = tem.split("\\s+");
            n = Integer.parseInt(textStr[0]);
            p = Integer.parseInt(textStr[1]);
            tem = buff.readLine();
            input = tem.replaceAll("\\s+","");
            for(int i = 0; i < p; i++){
                tem = buff.readLine();
                textStr = tem.split("\\s+");
                Character cl = textStr[0].charAt(0);
                Character fa = textStr[1].charAt(0);
                Character re = textStr[2].charAt(0);
                if(!rules.containsKey(cl+" "+fa)){
                    rules.put(cl + " " + fa, new ArrayList());
                }
                rules.get(cl+" "+fa).add(re);
            }
            Set<String> temp = simplify(input);
            List<String> te = new ArrayList<>(temp);
            Collections.sort(te);
            for(int i = 0; i < temp.size(); i++){
                System.out.print(te.get(i)+" ");
            }
        }catch(IOException e){
            System.out.println("Error");
        }
        finally {
            try {
                buff.close();
            } catch (IOException e) {
                System.err.println("Error");
            }
        }
    }

    public static Set<String> simplify(final String in) {
        if(cache.containsKey(in)){
            return(cache.get(in));
        }
        Set<String> ret = new HashSet<>();
        if (in.length() == 1) {
            ret.add(in);
            return ret;
        }
        for (int i = 1; i < in.length(); i++) {
            String head = in.substring(0, i);
            String tail = in.substring(i);
            for (String c2 : simplify(tail)) {
                for (String c1 : simplify(head)) {
                    ArrayList<Character> rep = rules.get(c1+" "+ c2);
                    if (rep != null) {
                        for (Character c : rep) {
                            ret.add(c.toString());
                        }
                    }
                }
            }
        }
        cache.put(in, ret);
        return ret;
    }

}


texts:
I was experimenting with lists, sets, and finally maps when I spotted a pattern in my code to make it recursive. Now I haven't used recursion much in the past or at work and I was very excited to have something that does recursion.

I have this recursive method that takes in an Integer and returns a string representation of the Integer by reading two HashMaps.

For example representation(11) will be Eleven OR representation(89) will be Eighty-Nine, ect.

I have arranged to handle up to 999,999. Are there any improvements I could make to this current implementation to handle things more efficiently/speedily?

public String representation(Integer num) {
    StringBuffer numRep = new StringBuffer();
    StringBuffer hundred = new StringBuffer("Hundred");
    StringBuffer thousand = new StringBuffer("Thousand");
    //The case for anything less than 20; 0-19
    if (num < 20) {
        numRep.append(genNumMap.get(num));

    //The case for any integer less than 100; 20-99
    } else if (num < 100) {
        int temp = num % 10;
        if (temp == 0) {
            numRep.append(greaterNumMap.get(num));
        } else {
            numRep.append(greaterNumMap.get(num - temp) + "-"
                    + representation(temp));
        }
    //The case for any integer less than 1000, covers the hundreds; 100-999
    } else if (num < 1000) {
        int temp = num % 100;
        if (temp == 0) {
            numRep.append(representation(num / 100) + "-" + hundred);
        } else {
            numRep.append(representation((num - temp) / 100) + "-" + hundred
                    + "-" + representation(temp));
        }
    //The case for any integer less that one million, covers the thousands; 1,000 - 999,999
    } else if (num < 1000000) {
        int temp = num % 1000;
        if (temp == 0) {
            numRep.append(representation(num / 1000) + "-" + thousand);
        } else {
            numRep.append(representation((num - temp) / 1000) + "-" + thousand
                    + "-" + representation(temp));
        }
    } else if (num > 1000000){
        numRep.append("Sorry, this number is too large");
        JOptionPane.showMessageDialog(null, "Sorry, this number is too large\n"+"Integers 0 through 999,999 are exceptable.");
    }
    return numRep.toString();
}


texts: Inserting and updating data for a website monitoring app

texts:
I have created an app in which URLs stored in a database are pinged via curl to check their availability and the database is updated with the results.

Two tables are updated with each fresh ping of a website (Site -- the status recieved 200, 404 etc) and (History - time of each ping, time taken, result etc) From my understanding because I am using Doctrine I have to create an object each time and update the database one object at a time. Is there another way around this? This solution means for each site I have I must create two queries for each and this could quickly get out of hand.

About the Database

Site and History have a many to one relationship.  One Site can have many history entries but each history only has one site relating to it.

Is there a more efficient way of doing this?

Default Controller.php

<?php

namespace AppBundle\Controller;

use AppBundle\Controller\Base\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

use AppBundle\Entity\Site;
 use AppBundle\Entity\History;
use AppBundle\Entity\Status;

class DefaultController extends Controller
{

/**
 * @Route("/scan", name="homepage")
 */
public function indexAction()
{
    $allSites = $this->getSites(); // Returns sites repo
    $allStatuses = $this->getStatuses(); // returns all possible statusus 404, 200 etc

    $results = $this->getScanManager()->scanBatch($allSites); // scans all the sites and returns results array

    foreach ($results as $result) {

        $history = new History();
        $history->setSiteId($this->findSiteById($result['id'], $allSites)); // RETURNS THE CORRESPONDING SITE OBJECT - SINCE IT IS AN ID
        $history->setTime(date('h:i:s d-m-Y'));
        $history->setStatusId($this->findStatusById($result['status'], $allStatuses));//RETURNS THE CORRESPONDING STATUS OBJECT - SINCE IT IS AN ID
        $history->setResponseTime($result['speed_download']);

        $em = $this->getDoctrine()->getManager();
        $em->persist($history);
        $em->flush();

        $site = $this->findSiteById($result['id'], $allSites);
        $site->setSiteStatus($this->findStatusById($result['status'], $allStatuses));
        $site->setSitePingTime($result['speed_download']);
        $site->setSiteLastUpdate(date('h:i:s d-m-Y'));

        $em = $this->getDoctrine()->getManager();
        $em->persist($site);
        $em->flush();

    }

    return $this->render('default/index.html.twig');
}

}


texts: Print out numbers of increasing distance from center value in Python

texts:
I am trying to print out numbers by increasing positive and negative values around a center number, and I don't think I've found the best way. For example, say I want to find what number in a list my center value is closest to. I do that by adding one, see if it matches, then subtracting one from the original number and see if that matches, then add two from the original number and so on. I know there are other ways of finding the closest match but I'm not looking for that - I'm looking for the best way to print out numbers steadily getting farther from a center value. Here's my code:

def inc_val(center, i, neg_toggle):
    mult = 1
    if neg_toggle:
        neg_toggle = False
        mult = -1
    else:
        neg_toggle = True
        mult = 1
        i += 1
    val = center + (i * mult)
    return val, i, neg_toggle

i = 0
center = 24
val = center
neg_toggle = False
my_set = {30, 50, 90}
while val not in my_set:
    print(val)
    val, i, neg_toggle = inc_val(center, i, neg_toggle)

This prints out the following list, as desired:

24
25
23
26
22
27
21
28
20
29
19

To be clear, I'm interested in the incrementing numbers. The while not in my_set part is just to illustrate. I am not intentionally trying to re-invent anything, so if there's a built-in or simpler way to do this, please let me know.


texts:
I'm trying to write a class to handle the collecting and calculating of some data. My intention is to have the controller instantiate the Report object, and a very simple view display it. I figured this would keep the controller thin and keep logic out of the views.

class Report
  def initialize(year_term_ids = [])
    @year_term_ids = year_term_ids
  end

  def gpas
    {}.tap do |gpas|
      %i(average minimum maximum).each do |method|
        gpas[method] = student_terms.send(method, :term_gpa).to_f.round(2)
      end
    end
  end

  def students
    student_statuses.merge(
      count: student_terms.count,
      dismissed: student_terms.dismissed,
      on_probation: student_terms.on_probation
    )
  end

  def year_terms
    @year_terms ||= load_year_terms
  end

  def student_terms
    @student_terms ||= load_student_terms
  end

  private

  def load_student_terms
    StudentTerm.where(year_term: year_terms)
      .includes(:student)
  end

  def load_year_terms
    year_terms = YearTerm.includes(student_year_terms: [ :student ])

    if @year_term_ids.empty?
      year_terms
    else
      year_terms.where(id: @year_term_ids)
    end
  end

  def student_statuses
    symbolize_hash(student_terms.map(&:student).group_by(&:status))
  end

  def symbolize_hash(hash)
    hash.keys.each do |k|
      hash[k.underscore.to_sym] = hash.delete(k) if k.is_a?(String)
    end

    hash
  end
end


texts: Split a list using another list whose items are the split lengths

texts:
I want to split a list using another list which contains the lengths of each split.

E.g:

>>> print list(split_by_lengths(list('abcdefg'), [2,1]))
... [['a', 'b'], ['c'], ['d', 'e', 'f', 'g']]
>>> print list(split_by_lengths(list('abcdefg'), [2,2]))
... [['a', 'b'], ['c', 'd'], ['e', 'f', 'g']]
>>> print list(split_by_lengths(list('abcdefg'), [2,2,6]))
... [['a', 'b'], ['c', 'd'], ['e', 'f', 'g']]
>>> print list(split_by_lengths(list('abcdefg'), [1,10]))
... [['a'], ['b', 'c', 'd', 'e', 'f', 'g']]
>>> print list(split_by_lengths(list('abcdefg'), [2,2,6,5]))
... [['a', 'b'], ['c', 'd'], ['e', 'f', 'g']]

As you can notice, if the lengths list does not cover all the list I append the remaining elements as an additional sublist. Also, I want to avoid empty lists at the end in the cases that the lengths list produces more elements that are in the list to split.

I already have a function that works as I want:

def take(n, iterable):
    "Return first n items of the iterable as a list"
    return list(islice(iterable, n))

def split_by_lengths(list_, lens):
    li = iter(list_)
    for l in lens:
        elems = take(l,li)
        if not elems:
            break
        yield elems
    else:
        remaining = list(li)
        if remaining:
           yield remaining

But I wonder if there is a more pythonic way to write a function such that one.

Note: I grabbed take(n, iterable) from Itertools Recipes

Note: This question is a repost from a stackoverflow question


texts:
This code works as I intended, but I am definitely a JS noob (C++ background), so I'm not sure if this is the best way to do it (for example using .each instead of some kind of a for loop was completely new to me).

https://github.com/TaylorHuston/menuBreak/blob/master/menuBreak.js

function menuBreak() {

var windowWidth = $(window).width();

var menuLength = 0;

$("#navUl > li").each( function (index, element) {
    menuLength += $(this).width();
});

if( menuLength > windowWidth*.9 ) {
    var newWidth= menuLength*.7;
    $('#navUl').css({"maxWidth":newWidth});
} else {
    $('#navUl').css({"maxWidth":windowWidth});
}

}

And in my HTML, not sure if this is the 'best' way to call it

<script>
  $(document).ready(function() {
    menuBreak();
  });

  $(window).resize(function() {
    menuBreak();
  });
</script>


texts: Improving a shortcode replacement routine PHP and regex

texts:
I am using wordpress to allow a client to enter a curriculum vitae and have it output the correct and consistent html formatting:

So I have a plain text box custom field and am having them enter in the content in the following way:

[section "Solo Exhibitions"]
[year 2011]

Inner Space
January 11

Peter Pan
February 11

[section "Group Shows"]
[year 2009]

Group Show
January 09

Big Group Show
February 09

So on and so forth. I wrote the following to parse that text field and output the correct HTML formatting, but I think it is pretty brute force-- not very much finesse.

$cv = get('cv_text');
$cv = strip_tags($cv);

// Remove empty line breaks
$cv = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $cv);

// Explode line breaks
$cv = explode("\n", $cv);

$i = 0;

foreach($cv as $line) {
    if( preg_match('/\[section \"(.*)\"\]/', $line) ) {
        echo preg_replace('/\[section \"(.*)\"\]/', '
$1
', $line);
    } elseif( preg_match('/\[year (\d*)\]/', $line) ) {
        echo preg_replace('/\[year (\d*)\]/', '
$1
', $line);
    } else {
        // Count every two lines and wrap in a
 tag
        if($i === 1) {
            echo ''.$line.'
';
            $i = 0;
            continue;
        } else {
            echo '
'.$line.'

            ';
            $i++;
        }
    }
}

Can anyone recommend a way to improve my present code or present a better approach?

Thanks!


texts:
I'm trying to clean up this function to target individual #hdid. I'm also using $this, but with no success.

<script>
        $(function() {
            $(".delete").click(function() {
                $('#load').fadeIn();
                var commentContainer = $(this).parent();
               // var id = $this.(".delete").val();
               // var string = id ;
               //  console.log(id);

                $.ajax({
                    type: "POST",
                    url: "delete-class.php",
                    data: {hdid: $('#hdid').val()},
                    cache: false,
                    success: function(){
                        commentContainer.slideUp('slow', function() {$(this).remove();});
                        $('#load').fadeOut();
                        //console.log(string);
                    }
                });
                return false;
            });
        });
    </script>


texts: Computing the autocorrelation between 2D time series arrays in python

texts:
I have a list of 2D arrays which comes from a time evolution PDE (in \$(x, y, t)\$) that I solved numerically. There are \$k\$ arrays, which all have the same dimensions, and the arrays correspond to a solution field of the PDE at times \$t = 0, 0.1, \dots, 0.1k\$. I wrote a program that takes in this list and computes the autocorrelation between the arrays for varying lag times.

From the definition of the autocorrelation function for wide-sense stationary processes, we have

$$R(\tau) = \frac{\mathbb{E}[(X_{t} - \mu)(X_{t + \tau} - \mu)]}{\sigma^{2}} = \frac{1}{\sigma^{2}} \sum_{t = 0}^{k-\tau} (X_{t} - \mu)(X_{t + \tau} - \mu)$$

Here, \$X_{t}\$ represents our 2D array at time \$t\$ and \$\mu\$, \$\sigma\$ the average and standard deviation, respectively, of the entire list of 2D arrays. My code for this is as follows

import numpy as np
import matplotlib.pyplot as plt

### Data
# List of data arrays called 'Data'.

### Expectation
New_Data = []
O = np.ones((len(Data[0][0]),len(Data[0][1]))) # Resolution of data arrays.
A = sum(Data)/len(Data)                        # Average 2D array
M = np.mean(A)                                 # Average of average 2D array
S = np.std(A)                                  # Standard deviation of average 2D array

for i in range(len(Data)):
    New_Data.append((Data[i]-M*O)/S) # (X_t-mu)/sigma

### Autocorrelation for varying lags.
Count = 1
R = []
while Count < len(New_Data)//2: # Arbitrary choice for max lag time.
    Matrix_Multiply = []
    for j in range(len(New_Data)-Count):
        Matrix_Multiply.append(np.multiply(New_Data[j],New_Data[j+Count]))
    R.append(sum(Matrix_Multiply))
    Count = Count+1

Solution = []
for k in range(len(R)):
    Solution.append(np.mean(R[k]))

t = [0.1*k for k in range(1,len(Solution)+1)]

### Plotting
plt.xlabel('Lag time')
plt.ylabel('Matrix sum')
plt.title('Field autocorrelation over time')
plt.semilogy(t, Solution)
plt.savefig('I_hope_this_works.png')

I'm sure there are some redundant steps in there, so I was wondering if anyone can see how I could make the code cleaner? Also, if anyone knows how to apply pythons numba package to make the code faster, it would be greatly appreciated, as I haven't been able to figure it out from the documentation in the hyperlink.

EDIT

In response to the comment below, here is some arbitrary data to use

Data = []
n = 20
for k in range(n):
    Data.append(np.random.randint(n, size=(n,n)))

For my data, I have ~800 arrays of size 256x256. I don't think I'm able to upload that for anyone to use.

EDIT 2

I just realised that we don't need to keep the data in the R list, so we can remove it as well as the lines

Solution = []
    for k in range(len(R)):
        Solution.append(np.mean(R[k]))

and edit the while loop to

### Autocorrelation for varying lags.
Count = 1
Solution = []
while Count < len(New_Data)//2:                # Arbitrary choice for max lag time.
    Matrix_Multiply = []
    for j in range(len(New_Data)-Count):
        Matrix_Multiply.append(np.multiply(New_Data[j],New_Data[j+Count]))
    R = sum(Matrix_Multiply)
    Solution.append(np.mean(R))
    Count = Count+1

though it doesn't provide much of a markup in terms of speed.


texts: Counting characters inside of a textarea using Angular

texts:
I have a textarea and need to calculate how many characters have been typed into it.

Both of the following solutions work correctly—which would be preferable, and why? Is there an even better way to implement it?

Option A:

<textarea
   id="descriptionEducation"
   name="description"
   rows="5"
   [(ngModel)]="fruit.description"></textarea>

    You have written {{ fruit.description.length }} characters

Option B:

<textarea
   id="descriptionEducation"
   name="description"
   rows="5"
   [ngModel]="fruit.description" #descriptionFruit></textarea>

    You have written {{ descriptionFruit.value.length }} characters


texts: Check whether a number is positive, negative, or zero

texts:
I have a program which generates a number between 0 and 4 for both the player and the computer.  If the number is positive the player wins, and if it is negative the computer wins.

if dif == 1 or dif == 2 or dif == 3 or dif == 4:
    print 'Player wins!'
elif dif == -1 or dif == -2 or dif == -3 or dif == -4:
    print 'Computer wins!'
else:
    print 'Player and computer tie!'

How can I make the if and elif statments simpler?


texts:
What's the best way to approach generating a password using a secure prng? In Python, I could simply use os.urandom. Any suggestions on this?

 _ = require('underscore')

exports.gen = (n=10) ->
  throw new Error 'Not a number!' if typeof(n) isnt 'number'
  throw new Error 'Prefered (n): [10..128]' if n < 10 or n > 128
  chars = (String.fromCharCode(i) for i in [33..126]).join ''
  _.times(n, -> chars[(Math.random() * chars.length) | 0]).join ''


texts: Finding number of palindrome pairs in an array

texts:
I am trying to find the number of string pairs in an array that are palindromes.

import java.io.BufferedReader;
import java.io.InputStreamReader;

class TestClass {
public static void main(String args[] ) throws Exception {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();
    int N = Integer.parseInt(line);

    int count = 0;

    String s[] = new String[N];

    for (int i = 0; i < N; i++) {
        s[i] = br.readLine();
    }

    for (int i = 0; i < N; i++) {
        String str = s[i];

        for(int j=i; j<N; j++) {

            StringBuilder sb = new StringBuilder(s[j]);
            if(str.equals(sb.reverse().toString())){
                count++;
            }
        }
    }

    System.out.print(count);
}
}

Is there a more efficient way to do this?


texts: Class Module to wrap up classic ADO call to SQL-server

texts:
I do a lot of copying excel vba code related to classic ADO from workbook to workbook, so I've decided to add the code to a Class Module in a single utility workbook and then in all other books just reference the utility book and use instances of the ADO wrapper class.

The class module is called clDataAccess

I would like to know any obvious flaws in the coding:

Option Explicit

'::::::::::::::::::::
'::
'::  Class Variable Declarations
'::
'::::::::::::::::::::

Private mcnConnectString As String
Private mcnConnect As ADODB.Connection

'::::::::::::::::::::
'::
'::  on initialization open an ADO connection through to the server
'::
'::::::::::::::::::::
Private Sub Class_Initialize()

    mcnConnectString = _
        "PROVIDER=SQLOLEDB.1;" & _
        "P******D=*********;" & _
        "PERSIST SE**RITY INFO=****;" & _
        "USER ID=**********;" & _
        "INITIAL CATALOG=*******;" & _
        "DATA SOURCE=*****;" & _
        "USE PROCEDURE FOR PREPARE=*;" & _
        "AUTO TRANSLATE=****;" & _
        "CONNECT TIMEOUT=*;" & _
        "COMMAND TIMEMOUT=*" & _
        "PACKET SIZE=****;" & _
        "TAG WITH COLUMN COLLATION WHEN POSSIBLE=****"

    Call ensureClosedConn(mcnConnectString)

End Sub

'::::::::::::::::::::
'::
'::  connection string property
'::  user could need to change the connection
'::
'::::::::::::::::::::
Property Let connectionString(ByVal stConn As String)
    mcnConnectString = connectionString
End Property

':::::::::::::::::
'::
':: This procedure attempts to create a connection to
':: the database using the specified connection string.
'::
'::::::::::::::::::::
Private Function ensureClosedConn(ByVal sConnection As String) As Boolean

On Error GoTo ErrorHandler

    If (mcnConnect Is Nothing) Then
        Set mcnConnect = New ADODB.Connection
        With mcnConnect
            .connectionString = sConnection
            .CommandTimeout = 0
            .Open
            .Close
        End With
    Else
        If (mcnConnect.State And 1) = 1 Then mcnConnect.Close
    End If

Exit Function
ErrorHandler:
    Err.Raise 99999, , "Error occured inside routine ensureClosedConn of clDataAccess"
End Function

':::::::::::::::::
'::
':: Client may wish to receive the recordset
'::
'::::::::::::::::::::
Public Function cGetRecordset(ByVal sqlString As String) As ADODB.Recordset

    Call ensureClosed_PubConn
    mcnConnect.Open
    Set fRecordset = New ADODB.Recordset

    With fRecordset
        .Open _
            Source:=sqlString, _
            ActiveConnection:=mcnConnect, _
            CursorType:=adOpenForwardOnly, _
            LockType:=adLockReadOnly, _
            Options:=adCmdText
    End With

    Set cGetRecordset = fRecordset
End Function

':::::::::::::::::
'::
':: This procedure attempts to move some data
'::
'::::::::::::::::::::
Public Sub updateWorkSheet( _
    ByVal sqlString As String, _
    ByRef rngDest As Excel.Range _
    )

On Error GoTo ErrorHandler

    Call ensureClosed_PubConn
    mcnConnect.Open
    Dim r As ADODB.Recordset
    Set r = New ADODB.Recordset

    With r
        ':: Extract and copy the required records.
        .Open _
            Source:=sqlString, _
            ActiveConnection:=mcnConnect, _
            CursorType:=adOpenForwardOnly, _
            LockType:=adLockReadOnly, _
            Options:=adCmdText
        rngDest.CopyFromRecordset r
        .Close
    End With

    ':: drop the recordset and its connection
    If Not (r Is Nothing) Then
        If (r.State And 1) = 1 Then r.Close
        Set r.ActiveConnection = Nothing
        Set r = Nothing
    End If

ErrorHandler:
    Err.Raise 99999, , "Error occured inside routine updateWorkSheet of clDataAccess"
End Sub    'updateWorkSheet


texts:
Here is my code:

noTest=int(input())
reverse=[]
nums=[]
for i in range(noTest):
    nums.append(int(input()))

for num in nums:
    binary=bin(num)
    binNum=binary[2:]
    rev=binNum[::-1]
    rev=binary[:2]+rev+'0'*(32-len(rev))
    #print int(rev,2)
    reverse.append(int(rev,2))

for r in reverse:
    print(r)

Input:
The first line of input consists number of the test cases. Each test case
contains a single 32 bit integer.

Output:
Print the reverse of integer.

Input:00000000000000000000000000000001 =1
Output:10000000000000000000000000000000 =2147483648


texts: Displaying MySQL table data using Node.js

texts:
I'm trying Node.js for the first time, everything works, but i'm not sure that my approach is right, can you tell me if it's ok?

In this test, i've tried to get sample data from mysql db and print it on http response.

I didn't even understand how to call the res.end(); if i have many async tasks working ^^

//IMPORTS
var http = require('http');
var mysql = require('mysql');
//---------------------------------------------------

//HTTP RESPONSE
http.createServer(function (req, res) {
    fetchActors(res);
}).listen(8080);
//---------------------------------------------------

//MISCS

//Database connection string
var db = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "*********",
  database: "sakila"
});

//Connection to database
db.connect(function(err) {
  if (err) throw err;
  console.log("Connected to Database!");
});

//-----------------------------------------------------

//FUNCTIONS

//Exectues queries on declared db (it can be extended if you want to use more than one db)
function executeQuery(sql, cb) {
    db.query(sql, function (err, result, fields) {
        if (err) throw err;
        cb(result);
    });
}

//Prints actors table
function fetchActors(res){
        executeQuery("SELECT * FROM actor", function(result){
        res.write("
");
        res.write("
");
        for(var column in result[0]){
            res.write("" + column + "	");
        }
        res.write("
");
        for(var row in result){
            res.write("
");
            for(var column in result[row]){
                res.write("" + result[row][column] + "	");
            }
            res.write("
");
        }
        res.write("
");
    });
}


texts: Extract higher value of pair from JavaScript array

texts:
Say we have these data:

var mat = [
  {
    "id": "A-B",
    "x": "1",
    "y": "0",
    "weight": "100"
  },
  {
    "id": "A-C",
    "x": "2",
    "y": "0",
    "weight": "77"
  },
  {
    "id": "A-D",
    "x": "3",
    "y": "0",
    "weight": "95"
  },
  {
    "id": "A-E",
    "x": "4",
    "y": "0",
    "weight": "18"
  },
{
    "id": "B-C",
    "x": "1",
    "y": "2",
    "weight": "3"
  },
 {
    "id": "D-F",
    "x": "5",
    "y": "3",
    "weight": "18"
  },
{
    "id": "B-A",
    "x": "0",
    "y": "1",
    "weight": "10"
  },
  {
    "id": "C-A",
    "x": "0",
    "y": "2",
    "weight": "7"
  },
{
    "id": "C-B",
    "x": "2",
    "y": "1",
    "weight": "1"
  },
  {
    "id": "D-A",
    "x": "0",
    "y": "3",
    "weight": "5"
  },
  {
    "id": "E-A",
    "x": "0",
    "y": "4",
    "weight": "18"
  },
 {
    "id": "F-D",
    "x": "3",
    "y": "5",
    "weight": "1"
  }
]

My task is to compare each object with its counterpart — i.e. compare A-B with B-A etc.  Note that for each pair, the x value of one is the y value of the other, and the y of one is the x of the other.  I want to only keep elements that have the higher weight of their pair.  If a pair has the same value of weight - as you see with A-E and E-A then both are discarded.

I am  very unfamiliar with JavaScript.  This is how I achieved this, but I have to feel like there is a better (i.e. less code, faster) way of doing it.  Also apologies if my javascript terminology is wrong.

// First I stored variables as vectors
var x = mat.map(function(x) {return x.x;});
var y = mat.map(function(x) {return x.y;});
var weights = mat.map(function(x) {return x.weight;});

// create a sequence of indices the length of the original array
var seq = [];
for (var i = 0; i < x.length; ++i) seq.push(i)

The strategy is to find the indices where each value of x is matched in y and vise-versa.  I use the helper function below to find which index is true for both for all:

// This is a Function to find matches
Array.prototype.diff = function(arr2) {
    var ret = [];
    this.sort();
    arr2.sort();
    for(var i = 0; i < this.length; i += 1) {
        if(arr2.indexOf( this[i] ) > -1){
            ret.push( this[i] );
        }
    }
    return ret;
};

Here, I go step by step to find the index that matches (i.e. is the opposite id):

// create a sequence
var seq = [];
for (var i = 0; i < x.length; ++i) seq.push(i)

var seqindices = [];
for(var j=0; j < seq.length; j++) {

var indicesy = [];
for(var i=0; i<x.length;i++) {
    if (y[i] === x[j]) indicesy.push(i);
}

var indicesx = [];
for(var i=0; i<x.length;i++) {
    if (x[i] === y[j]) indicesx.push(i);
}

seqindices.push(indicesx.diff(indicesy));
}

// seqindices
// 6,7,9,10,8,11,0,1,4,2,3,5

Now I compare the weights of each against its matching index and only keep weights if greater, remove if equal to or lower.

var matres = [];
for(var i=0; i<seqindices.length;i++) {
    if (weights[i] > weights[seqindices[i]]) matres.push(mat[i]);
}

This gives the following result:

[
  {
    "id": "A-B",
    "x": "1",
    "y": "0",
    "weight": "100"
  },
  {
    "id": "A-C",
    "x": "2",
    "y": "0",
    "weight": "77"
  },
  {
    "id": "A-D",
    "x": "3",
    "y": "0",
    "weight": "95"
  },
  {
    "id": "B-C",
    "x": "1",
    "y": "2",
    "weight": "3"
  },
  {
    "id": "D-F",
    "x": "5",
    "y": "3",
    "weight": "18"
  }
]

I have to believe I have gone a really long way round to get this result.  Hopefully someone can advise a better way.


texts:
I'm working on some facial recognition scripts in python using the dlib library. dlib takes in a face and returns a tuple with floating point values representing the values for key points in the face. If the Euclidean distance between two faces data sets is less that .6 they are likely the same.

So, I had to implement the Euclidean distance calculation on my own. I found an SO post here that said to use numpy but I couldn't make the subtraction operation work between my tuples. Because this is facial recognition speed is important. If anyone can see a way to improve, please let me know.

Yes, it works :)

def euclidean_dist(data_x, data_y):
    if len(data_x) != len(data_y):
        raise Exception('Data sets must be the same dimension')
    dimensions = len(data_x)
    sum_dims = 0
    for dim in range(0, dimensions):
        sum_dims += (data_x[dim] - data_y[dim])**2

    return sqrt(sum_dims)