texts:
I'm doing a challenge on TalentBuddy that requires you to find the powers of multiple array pairs. Since I got the solution right, but not fast enough, I decided I'd ask for help here.

def fast_power(a, b)
  pairs = a.zip(b)
  pairs.each {|p| puts (p[0] ** p[1]) % 4211}
end

a1 = [long array with 5000 elements]
b1 = [Long array with 5000 elements]

Each element is a number between 0 and 50000.

I have to calculate the value of every element in the first array to the power of every corresponding element in the second array and then modulo that by 4211.

I ran benchmark tests on the data set and here is what I got

14.907000    0.734000    15.641000  (16.818161)

It was supposed to finish in under 2 seconds, so perhaps my algorithm is the issue.

Problem

  Searching for a string in a large data store by simply comparing
  characters would take ages or a huge amount of computing power. We
  need a way to do it fast!

  The first step to building a fast search algorithm is to encode the
  data in a format that makes search efficient.

  A critical operation in the encoding process which we’ll get familiar
  with a bit later is the exponentiation of integer numbers.

  Given a list of pairs of integers (a,b)

  Your task is to

  Write a function that prints to the standard output (stdout) for each
  pair the result of (ab) modulo 4211 (one per line)

  Note that your function will receive the following arguments:

  a - which is an array of integers giving the first element (a) of each pair

  b - which is an array of integers giving the second element (b) of each pair

  The ith pair is defined by a[i] and b[i].

  Data constraints

  The length of a, b arrays will not exceed 10000 elements of a, b
  arrays are integer numbers in the [0, 50000]

  Efficiency constraints

  Your function is expected to print the requested result and return in
  less than 2 seconds make sure you don't use any methods that do
  significant work for you (e.g. pow() in Python)


texts: Alien Numbers - how Scala-ish is my solution?

texts:
I'm trying to solve an old GCJ. It's a very simple puzzle, but I'm trying to sharpen my Scala-fu.

Basically, you're getting a list of triple number srcLanguage dstLanguage, where number is an integer given in the numeral system of srcLanguage. You should translate it to the numeral system of dstLanguage.

A numeral system is simply a string of all possible digits, in ascending order. The decimal numeral system is represented by 0123456789, the binary numeral system is 01, and the hexadecimal one 0123456789ABCDEF.

For example:

3 0123456789 01 -> 11
3 0123       AB -> BB

Here's how I implemented it in Scala:

case class Langs(num:String,srcLang:String,dstLang:String)
object Langs {def fromLine(line:String):Langs = {
    val ar = line.split(" ");return Langs(ar(0),ar(1),ar(2))}}
object Translate {
  def lang2int(lang:String,num:String):Long = {
    var b = BigDecimal(0)
    val dmap = (lang.toList.zipWithIndex).toMap
    val digitsList = num map dmap
    val valueList = digitsList.reverse.zipWithIndex map (
        x => x._1 -> math.pow(dmap.size,x._2))
    return valueList.map(x=>x._1*x._2).sum.toLong
  }
  def int2lang(lang:String,_num:Long):String = {
    var num = _num
    val dmap = (lang zip (0.toLong to lang.size)).map(_.swap).toMap
    val sb = StringBuilder.newBuilder
    while (num > 0) {
        sb.append(dmap(num % dmap.size))
        num = num/dmap.size
    }
    sb.reverse.toString
  }
  def lang2lang(l:Langs):String = int2lang(l.dstLang,lang2int(l.srcLang,l.num))
}

object mymain {
  def main(args : Array[String]) : Unit = {
    val s = "A-large-practice"
    val basef = new java.io.FileInputStream("~/Downloads/"+s+".in")
    val f = new java.util.Scanner(basef)
    val out = new java.io.FileWriter(s+".out")
    val n = f.nextInt
    f.nextLine
    for (i <- 1 to n) {
      val nl = f.nextLine
      val l = Langs.fromLine(nl)
      out.write("Case #"+i+": "+Translate.lang2lang(l)+"\n")
    }
    out.close
  }
}


texts:
I'm not really sure if the following solution is the best fit, and I would like to know best practices when manipulating state and/or Ajax isomorphic fetch requests, in this case, in intervals.

  Users Module

import { createAction, handleActions } from 'redux-actions'
import { api } from '../utils/api'

export const REPORT_POSITION_INTERVAL = 'REPORT_POSITION_INTERVAL'
export const CLEAR_POSITION_INTERVAL = 'CLEAR_POSITION_INTERVAL'
export const POSITION_UPDATED = 'POSITION_UPDATED'

export const positionUpdated = createAction(POSITION_UPDATED, (value) => value)
export const setReportPositionInterval = createAction(REPORT_POSITION_INTERVAL, (interval) => interval)
export const clearPositionInterval = createAction(CLEAR_POSITION_INTERVAL)

export const reportPosition = () => {
  return (dispatch, getState) => {
    if (!navigator.geolocation) {
      return
    }

    function success (position) {
      var latitude = position.coords.latitude
      var longitude = position.coords.longitude

      if (getState().user.userId !== null && getState().user.currentCheckup !== null) {
        var data = {
          lat: latitude,
          lng: longitude,
          user_id: getState().user.userId,
          vehicle_id: getState().user.currentCheckup.vehicle_id
        }
        api(dispatch, getState, '/api/positions/add', 'POST', data, positionUpdated)
      }
    }

    function error () {
      alert('error')
    }

    if (getState().user.currentCheckup !== null) {
      var options = {enableHighAccuracy: true}
      navigator.geolocation.getCurrentPosition(success, error, options)
    }
  }
}

export const reportPositionPeriodically = () => {
  return (dispatch, getState) => {
    dispatch(setReportPositionInterval(setInterval(() => {
      dispatch(reportPosition())
    }, 10000)))
  }
}

export const actions = {
  reportPositionPeriodically,
  clearPositionInterval
}

export default handleActions({
  [REPORT_POSITION_INTERVAL]: (state, { payload }) => {
    return {
      ...state, reportPositionInterval: payload
    }
  },
  [CLEAR_POSITION_INTERVAL]: (state, { payload }) => {
    var clearedInterval = clearInterval(state.reportPositionInterval)
    return {
      ...state, reportPositionInterval: clearedInterval
    }
  },
  [POSITION_UPDATED]: (state, { payload }) => {
    return state
  }
}, {reportPositionInterval: null})

The reportPositionPeriodically action is called from the view.

The reportPositionPeriodically() passes along a setInterval arrow function to setReportPositionInterval which keeps the interval in the state and runs reportPosition().

reportPosition calls a function named api() imported from utils.

  Utils API

import fetch from 'isomorphic-fetch'
import config from 'config'
export const api = (dispatch, getState, endpoint, method, data, success)
...
fetch(config.api_host + endpoint + query, params).then((response) => {
    dispatch(loaderActions.finished())
    return response.json()
}).then((value) => {
    if (value.message) {
        dispatch(loaderActions.fetchError(value.message))
    }
    if ((value.code !== undefined && value.code >= 200 && value.code <= 300) || (value.success !== undefined && value.success)) {
        dispatch(success(value))
    }
})

To clear the timer and stop the fetch requests, I perform a call to clearPositionInterval() which runs clearInterval on the interval in the state.

I find redux very hard and I'm having a hard time finding out how to do things properly.


texts:
I am interested in printing all numbers that do not have a match in an array.
Example: 1,3,4,5,3,4,5,5,6 result 1,5,6
Please review my solution bellow. What would be a much better way for this? Any input on how to improve this is welcome

public static Integer[] outputSinglePair(Integer[] numbers){
        if(numbers == null)
            throw new IllegalArgumentException();

        Arrays.sort(numbers);
        ArrayList<Integer> result = new ArrayList<Integer>();
        for(int i = 0; i < numbers.length - 1; ){
            if(numbers[i] != numbers[i + 1]){
                result.add(numbers[i]);
                i++;
            }
            else
                i+=2;
            if(i == numbers.length - 1)result.add(numbers[i]);//we hit last element of array which is unpaired
        }
        return result.toArray(new Integer[0]);
    }


texts: Traversing through single large array to convert single array into array of arrays as per condition

texts:
I have a huge fileList array where many elements of array start from sub-** pattern. I want to group these files belonging to same sub-**(for e.g.: files which start with sub-01) into one subject specific array. Thus after creating all these sub specific array they can be pushed to a large array nifti_fileList.
I have written following code in JavaScript with very crude logic:

        subFilelist = [];
    var previous_sub = null;
    var capture = null;
    var file_no = 0

    async.eachOfLimit(fileList, 200, function (file, key, cb) {
        testfile = file.relativePath;
        sub_capture_re = /sub-[a-zA-Z0-9]*/gi;
        var current_sub = testfile.match(sub_capture_re)
        if (current_sub) {
            if((file_no === 0)){
                subFilelist.push(testfile)
                file_no = file_no + 1;
                previous_sub = current_sub[0];
            }

            if ((current_sub[0] === previous_sub) && (file_no > 0)){
                previous_sub = current_sub[0];
                subFilelist.push(file.relativePath)
            } else {
                nifti_fileList.push(subFilelist)
                subFilelist = [];
                file_no =0
            }
        }
        process.nextTick(cb);
    });
    console.log(nifti_fileList);

Sample input:

fileList = [/sub-09/func/sub-09_task-genInstrAv_run-05_physio.tsv.gz,
/sub-10/anat/sub-10_T1w.json,
/sub-10/anat/sub-10_T1w.nii.gz,
/sub-10/fmap/sub-10_magnitude1.nii.gz,
/sub-10/fmap/sub-10_magnitude2.nii.gz,
/sub-10/fmap/sub-10_phasediff.json,
/sub-10/fmap/sub-10_phasediff.nii.gz,
/sub-11/anat/sub-11_T1w.nii.gz]

nifti_fileList = [[/sub-09/func/sub-09_task-genInstrAv_run-05_physio.tsv.gz],
    [/sub-10/anat/sub-10_T1w.json,
    /sub-10/anat/sub-10_T1w.nii.gz,
    /sub-10/fmap/sub-10_magnitude1.nii.gz,
    /sub-10/fmap/sub-10_magnitude2.nii.gz,
    /sub-10/fmap/sub-10_phasediff.json,
    /sub-10/fmap/sub-10_phasediff.nii.gz],
    [/sub-11/anat/sub-11_T1w.nii.gz]]

Can this be made more efficient?


texts: Get The Base Name of an Elixir Template File

texts:
I've got the following Elixir code:

defmodule T do
    @eex_extension "eex"
    @file_separator "."
    def get_base_file_name_from_template_name(template_file_name) do
        [base, extension, @eex_extension] = String.split(template_file_name,@file_separator)
        base <> @file_separator <> extension
    end
end

It works like this:

iex(3)> f = T.get_base_file_name_from_template_name("index.html.eex")
"index.html"

1.) Is there a built-in library function I'm missing that would do this?

2.) Is there a built-in attribute for the eex file extension?

3.) Is there an OS built-in value for the "." BTW, I think file separator is not a great name for that but I can't come up with a better name.

Thoughts, comments, suggestions would be greatly appreciated. Even better yet if this is a built-in function in one of the Elixir libs and I've missed it a pointer to it would be most welcome.


texts: Output CSS class based on day-of-the-week match

texts:
The goal is to loop through an array of days-of-the-week (array values are stored as strings, eg: 'Mon, Tues, Weds', etc) and output some HTML when today's day is encountered.

The file is a CMS's .tpl file, so the PHP sits directly alongside the HTML.

My current, working code is:

<?php
 $dayofweek = date('w');

 foreach ($dataarray['openingDays'] as $day_key => $opening_day):
   $i++;
   //if $i is equal to 7 make it 0 to match array
   $i = ($i == 7 ? 0 : $i);
   //If today's date matches $i
   if ($dayofweek == $i):
?>

<?php else: ?>

<?php
  endif;
  print $opening_day;
?>

<?php endforeach; ?>

This is one of the first times I've used ternary and would like to know if it is done correctly. I am also concerned about not initializing $i and would like to know if that is acceptable.


texts: Determining which vehicle Rob wants to buy

texts:
Rob is looking to buy a used pickup truck. He will spend a maximum of
  $10,000 but would like to spend as little as possible. He will spend
  up to $3000 for a 2000 or older model, $5000 for a 2001-05 model,
  $7000 for a 2006-09 model, $9000 for a 2010 or newer model. Rob will
  not buy a red truck but he will pay 20% more for a white truck.

  Given the information above and using C# or VB.net, write a function
  using nested if/else statements that receives the following variables:

d_Price decimal

s_Color string

i_Year integer

  ...and returns a Boolean value to determine whether a truck is one Rob
  wants to buy.

I don't think is a good approach. How can this be improved?

 //Function to decide if the car is what Rob wants
static bool Rob_Wants(decimal d_Price, string s_Color, int i_Year)
{
    decimal [] max_array = {3000, 5000, 7000, 9000};  //max prices

    //if/else to check the condition
    if (s_Color == "Red" || d_Price>10000)
 return false;
    else if (s_Color == "White")
    {
        for (int i = 0; i < max_array.Length; i++)
            max_array[i] = max_array[i]+((max_array[i] * 20) / 100);
    }
    if (d_Price <= max_array[0] && i_Year >= 2000)
        return true;
    else if (d_Price <= max_array[1] && i_Year >= 2001)
        return true;
    else if (d_Price <= max_array[2] && i_Year >= 2006)
        return true;
    else if (d_Price <= max_array[3] && i_Year >= 2010)
        return true;
    else return false;
}


texts:
I am trying to optimize inserts from a returned JSON which contains data from multiple tables on the remote server. My code is working, but I just want to figure out a way for it to work better. I need the structure to be very dynamic (i.e. fetching the table names from JSON then looping through to complete the inserts into SQLite). This is so if a table is added in the future, I will not have to change any code.

private boolean prepReturnedData(String newString) throws JSONException {
     long errCheck = 0; // tracks valid db inserts.

     JSONArray columns = new JSONArray();

     JSONObject jsonObj = new JSONObject(newString);
     ArrayList<String> tables = new ArrayList<String>();

     Iterator<?> tableKeys = jsonObj.keys();

     while (tableKeys.hasNext()) {
         tables.add(tableKeys.next().toString());
     }
     for (String table : tables) { // Loop through table names.
         columns = jsonObj.getJSONArray(table);
         for (int index = 0; index < columns.length(); index++) { // Loop through Array of Entries

             ContentValues cv = new ContentValues(); // Figure out where to put this.

             Iterator<?> iter = columns.getJSONObject(index).keys(); // Creates an Iterator containing Column names.
             JSONObject newObj = columns.getJSONObject(index); // Creates a new JSONObject for retrieving column values.

             while (iter.hasNext()) { // Iterates to get Column names.
                 String tColumn = iter.next().toString(); // Stores column name, for retrieval of column value.

                 cv.put(tColumn, newObj.get(tColumn).toString());

             } // end column iterator loop.

             String status = null;
             if (table == "Seizures") {
                 status = "timestamp";
             } else {
                 status = "name";
             }
             try {
                 SQLiteDatabase db = this.getWritableDatabase();
                 errCheck = db.replace(table, status, cv);

                 if (db.isOpen()); {db.close(); };
             } catch (Exception e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
         } // end entry loop
     } // end table loop
     if (errCheck >= 0) {
         return true;
     } else {
         return false;
     }
}

This function is within a DatabaseHelper class.


texts:
In a few weeks I need to give a workshop about JavaScript to colleagues. They do not have much experience with JavaScript so I will explain the basics of JavaScript. At the end of the day I want them to make a small game with the aspects learned during the day.

I was thinking of Rock Paper Scissors. Below you can find the final code for this game:

var RockPaperScissors = (function () {
    var t = 'Tie',
        c = 'Computer wins',
        p = 'Player wins',
        winningMap = [
            [t, c, p],
            [p, t, c],
            [c, p, t]
        ],
        choices = ["Rock", "Paper", "Scissors"];

    var getComputerChoice = function () {
        return Math.floor(Math.random() * 3);
    };

    var getWinner = function (playerChoice, computerChoice) {
        return winningMap[playerChoice][computerChoice];
    };

    function RockPaperScissors(playerChoice) {
        this.playerChoice = playerChoice;
        this.computerChoice = getComputerChoice();
        this.winner = getWinner(this.playerChoice, this.computerChoice);
    }

    RockPaperScissors.prototype.toString = function () {
        return this.winner + " [Computer: " + choices[this.computerChoice] + ", Player: " + choices[this.playerChoice] + "]";
    };

    return RockPaperScissors;

})();

This code is executed everytime a select input box changes (see this as the user input):

$('#choice').on('change', function () {
    var $input = $(this);

    if ($input.val() === '-1') return;

    var rockPaperScissors = new RockPaperScissors($input.val());

    $('#message').html(rockPaperScissors.toString());
});

I've also set up a Fiddle.

Do you think this code is clear for beginners? Is this too advanced? I'm not sure if I should introduce them to prototyping or not.


texts: Finding longest substring containing k distinct characters

texts:
Below is my method to find the longest substring having k distinct characters.
I have used Set to keep track of distinct characters.

public static int maxLengthSubstring(String str,int k) {
    int i=0;
    int j=i;
    int max=0;
    //use set to keep track of distinct characters
    Set<Integer> set=new TreeSet<>();
    char arr[] = str.toCharArray();
    int len = arr.length;
    while(i<len && j<len){
        int c=arr[j];
        set.add(c);
        //if the number of distinct characters exceed the given limit
        //then compute the length of substring containing them
        if(set.size()>k){
            int substrLen=Math.abs(i-j);
            if(substrLen>max){
                //update the length of maximum substring found
                max=substrLen;
            }
            //move to the next character in the array
                i=i+1;
                j=i;
            //clear the set containing previously found distinct characters
                set.clear();
                continue;
            }
        //if set contains the exact number for distinct characters
        //then store the length of the substring and move to next iteration
        if(set.size()==k){
            int substrLen = Math.abs(i-(j+1));
            if(substrLen>max){
                //update the length of maximum substring found
                max=substrLen;
            }
        }
        j++;
    }
    return max;
}

The maximum length of input string can be up to million characters. The above method works fine for smaller strings but slows down if input string is large. How can I tweak my logic to improve running time for large strings?

A few test cases:

Input:

str="zxzxzxzxzx cvcvcvcvcvcvcvcvcv"
k=2
Output=18

Input:

str="aaaaaaaa abcabc aaaabbbbaaaabbbbbbabaa"
k=3
Output=23 (longest substring in this case is " aaaabbbbaaaabbbbbbabaa" including space character)


texts:
Lately I have been learning about serialization so I decided to write a little helper class for my application to make it easy to use this feature in multiple places. I actually mixed some generics in the code and want to hear your feedback on my overall approach.

SerializationService.cs

public class SerializationService
{
    /// <summary>
    /// This method Serializes the given Obj into an XML file.
    /// </summary>
    /// <param name="myObject">The obj to be serialized.</param>
    /// <param name="fileFullPath">The file name with the full
    /// path for the output serialized XML file.</param>
    public static void SerializeToFile(Object myObject, string fileFullPath)
    {
        var xmlDoc = new XmlDocument();
        var xmlSerializer = new XmlSerializer(myObject.GetType());
        using (var fs = new FileStream(fileFullPath, FileMode.Create))
        {
            var writer = new XmlTextWriter(fs, Encoding.Unicode);
            xmlSerializer.Serialize(writer, myObject);
            writer.Close();
        }
    }

    /// <summary>
    /// This method deserializes the XML file into an Obj.
    /// </summary>
    /// <param name="fuleFullPath">The XML file to read.</param>
    /// <returns>Deserialized obj of a type T.</returns>
    public static T DeserializeToObj<T>(string fuleFullPath)
    {
        var xmlSerializer = new XmlSerializer(typeof(T));
        using (var reader = new FileStream(fuleFullPath, FileMode.Open))
        {
            return (T) xmlSerializer.Deserialize(reader);
        }
    }
}

And here is how I use it in my code:

Serialization

SerializationService.SerializeToFile(
    _partRepository.GetParts().Where(p => p.IsIgnored).ToList(),
    fileFullPath);

De-serialization

var ignoredParts = SerializationService.DeserializeToObj<List<Part>>(ignoredPartsXml);


texts: Applying option if property is true or not set at all

texts:
I have a case where I want to apply a format if a property is true or if it is not set at all. Here is what I have:

var item = {};
item.name = "Option 1";

if (item.addTags==true || item.addTags==null) {
   // user wants to add tags. if not set then default is to add tags
}
else {
   // property is set and is not true. do not add tags
}

Is there any potential issues with doing it this way?


texts: Callback of setTimeout function when performing synchronous actions

texts:
Inside a function I'm calling playgame service. Once I've obtained the needed data (gameBeingPlayed), I need to show a 3..2..1 counter and then start a game.

chooseGameService.playGame(gameDefinitionID)
                .then(function () {

                    vm.container.gameBeingPlayed = chooseGameService.gameBeingPlayed;

                    showGame();

                    counter(3);

                });

 function counter(count) {
        vm.container.counterVisible = true;

        var countDownFrom = count;
        var timer = setInterval(function() { handleTimer(count); }, 1000);

        function handleTimer() {
            if(count === 0) {
                clearInterval(timer);
                //
                initGame();
                vm.container.counterVisible = false;
            } else {
                $('#gameCount').text(count);
                count--;
            }
        }
    }

The things that bothers me is that I had to move the next two lines from the service (promise) callback to the counter in order to execute the code synchronously.

initGame();
vm.container.counterVisible = false;

It works, but it seems semantically wrong for me that initGame() and visibility change is inside the counter function instead of inside the callback of the service (which is inside the controller).

Is there any way I could keep these lines inside the .then() of the controller instead of inside the counter function?


texts: Stored procedure to implement a search filter

texts:
I've implemented a stored procedure for a search process SQL Server 2008. I'm not sure if everything I created is correct. Also I'm wondering if this code has any leaks or vulnerability on SQL Injection. Here is my stored procedure:

USE [TestDB]
GO
/****** Object:  StoredProcedure [dbo].[Search_Dictionary]    Script Date: 08/09/2018 19:17:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Search_Dictionary]
  @FilterBy int = NULL,
  @Name varchar(50) = NULL,
  @Code char(2) = NULL
AS
BEGIN
    SET NOCOUNT ON;

    SELECT RecID, Status, Code, Name
    FROM Dictionary
    WHERE
        (@FilterBy = 1 AND Name LIKE '%'+@Name+'%')
        OR
        (@FilterBy = 2 AND Code = @Code)
        OR
        (@FilterBy = 3 AND @Name IS NULL AND @Code IS NULL);
END

Here is example on how I call this procedure:

EXEC Search_Dictionary @FilterBy = 1, @Name = "Grant", @Code = NULL;

I just want to prevent, if for example Filter By is 2 that should search query by Code column returns any result if user pass word Grant. In that case should return 0 records.

Also if anyone have any suggestions on how to improve the code please let me know.


texts:
I've recently been trying to learn more rust and I have just finished chapter 3 of the Rust book, which has a list of projects to do to practice.

I have tried to write a program that prints the 12 Days of Christmas and I would like to know if it is idiomatic or if any major improvementsc could be made.

Thanks in advance.

const LYRICS: [&str; 12] = [
    "Twelve drummers drumming",
    "Eleven pipers piping",
    "Ten lords a-leaping",
    "Nine ladies dancing",
    "Eight maids a-milking",
    "Seven swans a-swimming",
    "Six geese a-laying",
    "Five golden rings",
    "Four calling birds",
    "Three french hens",
    "Two turtle doves, and",
    "A partridge in a pear tree",
];

fn gen_lyrics(day: i32) -> String {
    let ordinal = match day {
        1 => "1st".to_string(),
        2 => "2nd".to_string(),
        3 => "3rd".to_string(),
        _ => day.to_string() + "th",
    };

    let mut start = format!(
        "On the {} day of Christmas, my true love gave to me:\n",
        ordinal
    );

    for i in (0..day).rev() {
        start.push_str(&LYRICS[11 - (i as usize)]);

        if i != 0 {
            start.push_str("\n");
        }
    }

    start
}

fn main() {
    for i in 1..13 {
        println!("{}\n", gen_lyrics(i))
    }
}


texts: Performance impact of using the universal selector in CSS

texts:
I am using the universal selector in CSS:

#containerDiv.containerTable tr td *
{
max-width:240px;
}

But Visual Studio suggests:

  "Performance: Never use the universal selector. It has a big negative
  impact on browser rendering"

Will this have such a big impact? If so how can I overcome this?
I have used * before and I have not seen any noticeable impact.


texts: Am I wrapping synchronous code in ktor/Kotlin coroutine correctly?

texts:
I'm playing with ktor right now. If I understand this correctly, code in ktor's handlers need to be as asynchronous as possible. So I was trying to figure out how do you write synchronous (potentially expensive) code in a Kotlin coroutine. Here's what I've written:

    install(DefaultHeaders)
    install(CallLogging) {
        level = Level.INFO
    }
    install(Routing) {
       // More routes here

        get("/hello") {
            val key = async {
                // Emulating an expensive synchronous call by doing lots of calculations
                log.info("Calculating key")
                var sb = StringBuilder(32)
                for (i in 0..15) {
                    var sym: Byte = (Math.random() * 255).toByte()
                    for(j in 1..10000000) {
                        sym = (sym + Math.random() * 40).toByte()
                    }
                    sb.append(String.format("%02X", sym))
                }

                log.info("Ended key calc")

                sb.toString()
            }

            call.respondText("Hello again! Your key is ${key.await()}", ContentType.Text.Plain)
        }
    }
}

I'm wrapping my potentially expensive synchronous code in async to get a Deferred and then await it.

I think it works because I'm able to do simultaneous request to /hello and other routes.

Is this the correct way to do it? Do I even need to bother, or can I write synchronous code in ktor handlers? Thank you.


texts: Concurrent queue with pthread implementation

texts:
I'm writing a multi-threaded queue implemented with pthreads. Since I came up with the code based on several tutorials from Internet, I would like to make sure there are no logic errors in my code:

template <typename T>
class ThreadQueue {
public:
    ThreadQueue() {
        pthread_mutex_init(&m_qmtx, NULL);
        pthread_cond_init(&m_condv, NULL);
    }

    ~ThreadQueue() {
        pthread_mutex_lock(&m_qmtx);
        m_queue.clear();
        pthread_mutex_unlock(&m_qmtx);
    }

    void push(T t_data) {
        pthread_mutex_lock(&m_qmtx);
        m_queue.push_back(t_data);
        pthread_mutex_unlock(&m_qmtx);

        pthread_cond_signal(&m_condv);
    }

    T front() {
        T ret;
        pthread_mutex_lock(&m_qmtx);
        while (m_queue.empty()) {
            pthread_cond_wait(&m_condv, &m_qmtx);
        }
        ret = m_queue.front();
        pthread_mutex_unlock(&m_qmtx);
        return ret;
    }

    void pop() {
        pthread_mutex_lock(&m_qmtx);
        if (!m_queue.empty())
            m_queue.pop_front();
        pthread_mutex_unlock(&m_qmtx);
    }

private:
    std::deque<T> m_queue;
    pthread_mutex_t m_qmtx;
    pthread_cond_t m_condv;
};


texts: isRTL.coffee library to determine if a text is of right-to-left direction

texts:
I just wrote this tiny library called isRTL.coffee to determine the direction of the text. Is there any better way of doing this?

rtlChars = '\u0600-\u06FF' # Arabic - Range

rtlChars += '\u0750-\u077F' # Arabic Supplement - Range

rtlChars += '\uFB50-\uFDFF' # Arabic Presentation Forms-A - Range

rtlChars += '\uFE70-\uFEFF' # Arabic Presentation Forms-B - Range

reRTL = new RegExp "^[#{rtlChars}]"

window.isRTL = (value) ->
    if value.match reRTL
        true
    else
        false


texts: Better code for converting a char to its UTF-8 percent encoding representation?

texts:
This is working code for a URI template (RFC 6570) implementation; when the character to render is not within a specific character set, it is needed to grab the UTF-8 representation of that character and encode each byte as a percent-encoded sequence.

The question is whether something more efficient than this exists?

private static String encodeChar(final char c)
{
    final String tmp = new String(new char[] { c });
    final byte[] bytes = tmp.getBytes(Charset.forName("UTF-8"));
    final StringBuilder sb = new StringBuilder();
    for (final byte b: bytes)
        sb.append('%').append(UnsignedBytes.toString(b, 16));
    return sb.toString();
}

Note: UnsignedBytes.toString() is from Guava.


texts: Efficiently find all the Pythagorean triplets where all numbers less than 1000

texts:
How can I make this code run faster:
for a in range(1,1001):
    for b in range(1, 1001):
        for c in range(1, 1001):
            if pow(a, 2) + pow(b, 2) == pow(c, 2):
                print(str(a) + "," + str(b) + "," + str(c))


texts: Image hosting site image downloader using requests and BeautifulSoup

texts:
I went about this the way I have because Selenium is slow and inconvenient (opens browser) and I was unable to find the href attribute in the link element using BeautifulSoup. Html included below. On the downside, this appears to only ever find 25 images.

#! python3

# Saves photos to file from flickr.com using specified search term

import bs4
import logging
import os
import re
import requests
import shutil
import sys
import time

def find_link(element):
    """Finds link using a regular expression"""

    link_regex = re.compile(r"//c\d+.staticflickr.com/\d+/\d+/\w+\.jpg")
    # dictionary of element attributes
    element_attr_dict = element.attrs

    # get list of element attribute values wherein image link lies
    attr_list = [element_attr_dict[key] for key in element_attr_dict.keys()]
    attr_string = ""

    # link exists within a string list element
    for element in attr_list:
        if type(element) == str:
            attr_string += element
    match = link_regex.search(attr_string)
    if match:
        link = "https:" + match.group()
    else:
        link = None
    return link

def main():
    """Downloads specified type/number of 'flickr' images to folder

    Takes three command line arguments: "filename, search_term, number_
    images". Saves images to folder. Number of images saved based upon
    number requested by user or number found during search. Whichever is
    lower.

    Arguments:
    search_term   -- search image site using this term
    number_images -- maximum number of images to save to folder
    """

    try:
        search_term, number_images = sys.argv[1:]
        number_images = int(number_images)
    except ValueError:
        print("Something went wrong. Command line input must be of \
format: 'filename searchterm numberimages'")
        return

    links = []
    # make folder to store photos and name using search term
    html_path = r"C:\Users\Dave\Desktop\2016Coding\AutomateBoring\11" + \
                r"-WebScraping\flickrhtml.txt"
    path = \
    r"C:\Users\Dave\Desktop\2016Coding\AutomateBoring\11-WebScraping" + \
    r"\gimages\requests"
    folder_path = os.path.join(path, search_term)
    if os.path.exists(folder_path):
        shutil.rmtree(folder_path)
    os.makedirs(folder_path)

    print("Finding photos...")

    # get links to photos
    res = requests.get("https://www.flickr.com/search/?text=" + search_term)
    res.raise_for_status()
    soup = bs4.BeautifulSoup(res.text, "html.parser")
    found_elems = soup.select(".photo-list-photo-view")

    # incase number found images < requested
    number_save_images = min(number_images, len(found_elems))

    print("Found {} images".format(number_save_images))

    for found_elem in found_elems[:number_save_images]:
        link = find_link(found_elem)
        links.append(link)

    # write images to file
    print("Writing images to folder...")
    for image_link in links:
        basename = os.path.basename(image_link)
        save_file_name = os.path.join(folder_path, basename)
        res = requests.get(image_link)
        res.raise_for_status()

        with open(save_file_name, "wb") as f:
            for chunk in res.iter_content(100000):
                f.write(chunk)

    print("Images saved at: {}".format(folder_path))
    print("*****Done*****")

if __name__ == "__main__":
    main()


texts:
I'm new to the programming and I want to ask a question about my linkedlist stack implementation if it's correct and if it meets the requirements of the linkedlist stack implementation. If there is any suggestion I'm glad to hear...

public class DynamicStack {
  private class Node {
     private Object item;
     private Node next;

     Node(Object item){
      this.item = item;
      this.next = null;
     }

     Node(Object item, Node nextNode){
      this.item = item;
      this.next  = nextNode;
     }
  }

 private int count;
 private Node top;

 public DynamicStack() {
   this.top = null;
   this.count = 0;
 }

 public void push(Object item) {
   if(top == null) {
      top = new Node(item);
   }else {
      Node newNode = new Node(item,top);
      top = newNode;
   }
   count++;
 }

 public Object peek() {
   if(top == null) {
      throw new NoSuchElementException("Underflow Exception");
   }else {
      return top.item;
   }
 }
 public Object pop() {
   Node currentNode = top;
   if(top == null) {
      throw new NoSuchElementException("Underflow Exception");
   }else {
      Node nextNode = top.next;
      top = null;
      top = nextNode;
   }
   count--;
   return currentNode.item;
 }


texts:
Where should you store state and how should it be managed in a ClojureScript application? Take the following code for example - it's a "game" where you travel either down or left.

(ns cljsfiddle)

; initialise the state
(set! (.-x js/window) 0)
(set! (.-y js/window) 0)

; state modifiers
(defn inc-x! [value] (set! (.-x js/window) (inc (.-x js/window))))
(defn inc-y! [value] (set! (.-y js/window) (inc (.-y js/window))))

(.addEventListener js/window "keyup"
    (fn [event]
      (let [key-code (.-which event)]
       (cond
        (= key-code 49) (inc-x!)
        (= key-code 50) (inc-y!)))
      (print (.-x js/window) (.-y js/window))))

I'm transitioning to ClojureScript from JavaScript and I realize this is written in a fairly JavaScript-y way. In addition, any advice on how to rewrite this in a more Clojure-esque way is welcomed.


texts: Select multiple rows using multiple data attribute

texts:
I am hiding rows for given ids using this code:

var idArray = ['A123','B456']; // getting from web services
for (var i=0;i<idArray.length;i++) {
   var id = idArray[i];
   $("#eTable tr[data-empid=" + id + "]").hide();
}

HTML (Just for example doesn't needs improvement)

        Peter
        Vogel
        Principal

        Jan
        Vogel
        General Manager

        Jason
        van de Velde
        Company nurse

How can we improve this code as it is selecting same table again and again to get row one by one instead of getting them once?


texts: Using/reusing the SQL Command for several Inserts in the SQL Transaction

texts:
I'm using the code below to save a record in my database. I was thinking that I am creating new instances of the SQL Command object and fear that I might be bloating the system memory and not doing proper garbage collection for unused objects. Can anyone point out which parts of this code I can improve?

By the way, this is the first time I've used SQL Transactions.

Private Sub SaveRecord()
    Dim type As Integer = 0

    Try
        Using dbConn As New SqlConnection(strConnStr)
            Try
                dbConn.Open()

                _params = New List(Of SqlParameter)

                ' Register record
                If formMode = 1 Then
                    dbCmd = dbConn.CreateCommand
                    dbTran = dbConn.BeginTransaction(IsolationLevel.ReadCommitted, "RBT")

                    If OrganismID = -1 Then
                        dbCmd = New SqlCommand("spNewZEntity", dbConn, dbTran)
                        dbCmd.CommandType = CommandType.StoredProcedure
                        AssignZEntityParam()

                        ZEntityID = dbCmd.ExecuteScalar
                        If ZEntityID = Nothing Then Throw New Exception("Failed to Generate ZEntityID")
                    End If

                    dbCmd = New SqlCommand("spNewXEntity", dbConn, dbTran)
                    dbCmd.CommandType = CommandType.StoredProcedure

                    AssignXEntityParam()
                    XEntityID = dbCmd.ExecuteScalar()

                    If XEntityID = Nothing Then Throw New Exception("Failed to Generate XEntityID")

                    For Each AZ In _AZItems
                        If AZ.IDAZ = -1 Then
                            dbCmd = New SqlCommand("spNewAZ", dbConn, dbTran)
                            dbCmd.CommandType = CommandType.StoredProcedure

                            _params.Clear()
                            dbCmd.Parameters.AddWithValue("@AZName", AZ.nameAZ)
                            AZID = dbCmd.ExecuteScalar()

                            If AZID = Nothing Then Throw New Exception("Failed to Generate AZID")
                            type = 1
                        Else
                            type = 2
                        End If

                        dbCmd = New SqlCommand("spNewISO", dbConn, dbTran)
                        dbCmd.CommandType = CommandType.StoredProcedure

                        AssignCreateISOParam(type, AZ)
                        dbCmd.ExecuteNonQuery()
                    Next

                    dbTran.Commit()
                    dbConn.Close()

                    MsgBox("Done.", MsgBoxStyle.Information)
                End If

                flagSaved = True
                Dispose()
            Catch ex As Exception
                CreateLog("SaveRecord", ex.Message, ex)

                Try
                    MsgBox("Attempting to rollback changes to database...", MsgBoxStyle.Information, emParsing)

                    If Not dbTran.Connection Is Nothing Then
                        dbTran.Rollback("RBT")
                        dbTran.Dispose()

                        MsgBox("Rollback successful.", MsgBoxStyle.Information, emParsing)
                    Else
                        MsgBox("Failed to rollback changes. Please contact your system administrator.",
                               MsgBoxStyle.Exclamation, emParsing)
                    End If
                Catch ex2 As SqlException
                    CreateLog("SaveRecord/Rollback Failed", ex2.Message, ex2)
                    MsgBox("Failed to rollback changes. Please contact your system administrator.", MsgBoxStyle.Exclamation, "Rollback Failed")
                End Try
            Finally
                dbCmd.Dispose()
            End Try
        End Using
    Catch ex As Exception
        CreateLog("SaveRecord", ex.Message, ex)
    End Try
End Sub


texts: Is my login function secure ? how to improve it?

texts:
Using cakePHP, I would love to have your point of view about this large source code, i am not sure if its enought secure. sha1() will be removed with another script.
I found this large script can be optimized but how ?

i have many times

$this->Session->setFlash("You hav to complete each fiedls", "error");

and

$this->Request->redirect(SITE . "users/account");

I wonder if I can optmize this also:

class UsersController extends Controller {
    function account($Req){
        if(isset($Req->post->login)){
            $login = addslashes($Req->post->login);
            $password = sha1(addslashes($Req->post->password));
            $pass_confirm = sha1(addslashes($Req->post->pass_confirm));
            $email = addslashes($Req->post->email);
            $signature = addslashes($Req->post->signature);

            if(empty($login) || empty($email)){
                $this->Session->setFlash("You hav to complete each fiedls", "error");
                $this->Request->redirect(SITE . "users/account");
            }
            elseif($pass_confirm != $password) {
                $this->Session->setFlash("You gave two differents password", "error");
                $Req->redirect(SITE . "users/account");
            }

            $this->loadModel("Users");

            $dispoLogin = $this->Users->findCount(array(
                "login" => $login
            ));
            if($dispoLogin === 0){
                $this->Session->setFlash("The login is already use by someone else", "error");
                $this->Request->redirect(SITE . "users/account");
            }

            $dispoEmail = $this->Users->findCount(array(
                "email" => $email
            ));
            if($dispoEmail === 0){
                $this->Session->setFlash("Email adress already use by someone else", "error");
                $this->Request->redirect(SITE . "users/account");
            }

            if(empty($password)){
                $q = $this->Users->findFirst(array(
                    "fields" => "password",
                    "conditions" => array(
                        "id" => $this->User->id
                    )
                ));
                $password = sha1($q->password);
            }

            $this->Users->save(array(
                "id" => $this->User->id,
                "login" => $login,
                "password" => $password,
                "email" => $email,
                "signature" => $signature
            ));
            $this->user->setData(array(
                "login" => $login,
                "password" => $password,
                "email" => $email,
                "signature" => $signature
            ));

            $this->Session->setFlash("Your profile page is updated");
            $this->Request->redirect(SITE);
        }
    }
}


texts:
Here is code that will output maven coordinates from given pom.xml file. How can I remove boilerplate (possibly convert it to xml picklers)?

import Control.Lens ((&))
import Text.XML.HXT.Core
import System.Environment (getArgs)
import Text.XML.HXT.Arrow.XmlArrow
import Control.Monad (liftM)
import Data.Maybe
import Control.Applicative

main = do
  (filename:_) <- getArgs
  _groupId <- extractOne filename $ project >>> groupId
  _artifactId <- extractOne filename $ project >>> artifactId
  _version <- extractOne filename $ project >>> version
  _parentGroupId <- extractOne filename $ project >>> parent >>> groupId
  _parentArtifactId <- extractOne filename $ project >>> parent >>> artifactId
  _parentVersion <- extractOne filename $ project >>> parent >>> version
  let coordinate = (,,) <$> _groupId <*> _artifactId <*> _version
      parentCoordinate = (,,) <$> _parentGroupId <*> _parentArtifactId <*> _parentVersion
      in do
        print coordinate
        print parentCoordinate

extract :: String -> IOSArrow XmlTree a -> IO [a]
extract filename cat = runX $ readDocument [withValidate no] filename >>> cat

extractOne :: String -> IOSArrow XmlTree a -> IO (Maybe a)
extractOne filename cat = extract filename cat >>= return . listToMaybe

version :: ArrowXml a => a XmlTree String
version = getChildren >>> hasName "version" >>> getChildren >>> getText

artifactId :: ArrowXml a => a XmlTree String
artifactId = getChildren >>> hasName "artifactId" >>> getChildren >>> getText

groupId :: ArrowXml a => a XmlTree String
groupId = getChildren >>> hasName "groupId" >>> getChildren >>> getText

parent :: ArrowXml a => a XmlTree XmlTree
parent = getChildren >>> hasName "parent"

project :: ArrowXml a => a XmlTree XmlTree
project = getChildren >>> hasName "project"


texts: SQL Query over ODBC is slow with DISTINCT

texts:
Is there any way to simplify this query? It's being run against a Progress Database via an ODBC connection.

The CustomerSalesperson table has a CustomerID, a SalespersonID, and a CreateDate. Each CustomerID could have many records, each with a different SalespersonID. I'd like to get a list of the SalespersonIDs with the most recent CreateDate for each CustomerID. This is what I'm doing currently:

SELECT DISTINCT cs.CustomerID, cs.SalespersonID
FROM PUB.CustomerSalesperson cs
INNER JOIN
  (SELECT CustomerID, MAX(CreateDate) AS MaxDate
  FROM PUB.CustomerSalesperson
  GROUP BY CustomerID) csmax
ON cs.CustomerID = csmax.CustomerID
WHERE csmax.MaxDate = cs.CreateDate

It take a little longer to return than I would expect. When I remove the DISTINCT, it goes a lot faster, but it returns many duplicates as a result of other fields in the table not which aren't relevant to the query.


texts: Placement of jQuery function that toggles p tags

texts:
I have a page with lots of jquery.  There are lots of functions inside:

$( document ).ready(function() {
//lots of functions
});

After that "document ready" snippet closes, I have a function written like this:

$(function(){
        $('#number-of-coins input[type=radio]').change(function () {
            var numcoins=$("input[name=numbercoins]:checked").val();
            if (numcoins == "2") {
                $( "p.two" ).show();
                $( "p.three" ).hide();
            } else if (numcoins == "3") {
                $( "p.two" ).hide();
                $( "p.three" ).show();
            } else {
                $( "p.two" ).hide();
                $( "p.three" ).hide();
            }
        });
    });

What this does is not important, but it does work properly. My question is, would it be more efficient to take this section below (without the "function" wrapper) and move it into the "document ready" snippet?  Or does it not matter either way?  I did that as a test and it worked, so it would seem to me that every function should just go inside of the "document ready" snippet, since it's slightly less code not having to write the function wrapper each time.  Is that accurate or does it depend?

 $('#number-of-coins input[type=radio]').change(function () {
            var numcoins=$("input[name=numbercoins]:checked").val();
            if (numcoins == "2") {
                $( "p.two" ).show();
                $( "p.three" ).hide();
            } else if (numcoins == "3") {
                $( "p.two" ).hide();
                $( "p.three" ).show();
            } else {
                $( "p.two" ).hide();
                $( "p.three" ).hide();
            }
        });


texts: Deleting values from std::vector of different types

texts:
I need to make a vector of different types and be able to delete the values (please ignore anything else but deleting the values for now). Is the next code safe to delete the values?

#include <memory>
#include <vector>
#include <string>

using MyPtr = std::unique_ptr<void, void(*)(void*)>;

template<typename T>
MyPtr MakePtr(const T& val)
{
    return MyPtr(new T(val), [](void* ptr){delete static_cast<T*>(ptr);});
}

int main()
{
    std::vector<MyPtr> ptrs;

    ptrs.push_back(MakePtr(1));
    ptrs.push_back(MakePtr(1.1));
    ptrs.push_back(MakePtr(1.f));
    ptrs.push_back(MakePtr(std::string("1")));

    return 0;
}


texts: Generate all permutations of a list in Python

texts:
This is my solution to the "Permutations" problem from Leetcode:

  Given a collection of distinct numbers, return all possible permutations.

I know this is a common routine that can be done much faster using itertools.permutations but I want to write it to learn the algorithm itself.

I would like any general feedback on the implementation of the recursive algorithm. But I am also curious whether I should use an instance helper method as it is now, or make the helper method a class or static method. If someone can show me how to re-write the helper method as a class or static method that would be appreciated.

class Solution(object):
    def permute(self, nums):
        res = []
        self._permuteHelper( nums, 0, res)
        return res

    def _permuteHelper(self, nums, start, results):  #helper method
            if start >= len(nums):
                results.append(nums[:])
            else:
                for i in range(start, len(nums)):
                    nums[i], nums[start] = nums[start], nums[i]
                    self._permuteHelper(nums, start +1, results)
                    nums[start], nums[i] = nums[i], nums[start]

s = Solution()
nums = [1, 2, 3]
print s.permute(nums)

There are a lot of suggestions in the comments and answers about using library functions. For programming challenge, I am not interested in copying and pasting library functions. Yes, this is re-inventing the wheel if you want to call it that!


texts: Performance and Readability Improvements for HTML Parser with BeautifulSoup

texts:
This function takes as an argument a json file (could contain anything in json format, since I scrap hundreds of random pages) and returns a list of dictionaries where a URL is mapped to its corresponding headers, based on the extraction of headers using beautifulsoup and a regex pattern.

I'm looking for suggestions regarding performance readability and clarity.

Following  my 1st iteration I improved my code and here is the result:

import json
from tqdm import tqdm
import re
from bs4 import BeautifulSoup
import csv
import string

"Load HTML body, and fetch headers"
def get_headers_from_json(local_path):
    """
    The function takes a json file with html_body and returns a list of headers.
    It parses the titles, based on tags starting with 'h' + num.
    """
    data = json.loads(open(local_path).read())
    pattern = re.compile(r"^h[0-9]$")
    headers_urls = []
    printable = set(string.printable)
    for dict in tqdm(data):
        headers = []
        for val in dict.values():
            soup = BeautifulSoup(val, 'html.parser')
            url = dict.values()[0]
        for element in soup.find_all(pattern):
            element = element.get_text().strip().encode('utf-8')
            element = filter(lambda word: word in printable, element)
            headers.append(element)
        cleaned_data = {"url": url, "headers": headers}
        headers_urls.append(cleaned_data)
    return headers_urls

Example of json input:

[["<body class=\" catalog-category-view categorypath-sale-html category-sale\">\n<script type=\"text/javascript\">\n//<![CDATA[\nif (typeof(Varien.searchForm) !== 'undefined') {\n    Varien.searchForm.prototype._selectAutocompleteItem = function(element) {\n        var link = element.down();\n        if (link && link.tagName == 'A') {\n            setLocation(link.href);\n        } else {\n            if (element.title){\n                this.field.value = element.title;\n            }\n            this.form.submit();\n        }\n    };\n    Varien.searchForm.prototype.initAutocomplete = function(url, destinationElement) {\n        new Ajax.Autocompleter(\n            this.field,\n            destinationElement,\n            url,\n            {\n                paramName: this.field.name,\n                method: 'get',\n                minChars: 2,\n                frequency: .3,\n                updateElement: this._selectAutocompleteItem.bind(this),\n                onShow : function(element, update) {\n                    if(!update.style.position || update.style.position=='absolute') {\n                        update.style.position = 'absolute';\n                        Position.clone(element, update, {\n                            setHeight: false,\n                            offsetTop: element.offsetHeight\n                        });\n                    }\n                    Effect.Appear(update,{duration:0});\n                }\n\n            }\n        );\n    };\n    Autocompleter.Base.prototype.markPrevious = function() {\n        if (this.index > 0) {\n            this.index--;\n        } else {\n            this.index = this.entryCount - 1;\n        }\n        var entry = this.getEntry(this.index);\n        if (entry.select('a').length === 0) {\n            this.markPrevious(); // Ignore items that don't have link\n        }\n    };\n    Autocompleter.Base.prototype.markNext = function() {\n        if (this.index < this.entryCount - 1) {\n


texts: Recursively insert a node in an ordered Linked List

texts:
I've written this and it works great. Is there any shorter or simpler way to achieve the same result without distinguish between cases?

Node* recursive_ordered_insert(Node* head, int val)
{
    //special case: first elem is null
    if (!head)
    {
        head = create_node(val);

        return head;
    }

    //special case 2: end of list
    if (!head->next)
    {
        head->next = create_node(val);

        return head->next;
    }

    //base case
    if (head->next && head->next->data > val)
    {
        Node* newNode = create_node(val);
        newNode->next = head->next;

        head->next = newNode;

        return newNode;
    }

    return recursive_ordered_insert(head->next, val);
}


texts: Binance.Net getting historical data from start date to end date loop optimization

texts:
I'm using Binance.Net and I wanted to get historical candle data from start date to end date which is useful for backtesting. They don't really provide that functionality, so I have to do it myself. Their API allows me to specify startTime (start date) and endTime (end date) and limit (cannot exceed 1000). I'm reading candles until I reach all of them between start date and end date, because of that limit.

Two test cases (read the comments right next to first and last):

I added them to the question so you can get the idea behind that REST point.

BinanceClient client = new BinanceClient(new BinanceClientOptions()
{
    ApiCredentials = new ApiCredentials(configuration["BinanceSettings:ApiKey"], configuration["BinanceSettings:SecretKey"]),
    AutoTimestamp = true,
    AutoTimestampRecalculationInterval = TimeSpan.FromMinutes(30)
});

var candles = _client.GetKlines(symbol: "TRXUSDT", interval: KlineInterval.ThirtyMinutes, startTime: new DateTime(2018, 06, 06), endTime: new DateTime(2020, 06, 16), limit: 1000).Data.SkipLast(1).ToList();

// Test case 1
var first = candles.First().OpenTime; // 6/11/2018 11:30:00 AM. This doesn't match the specified "startTime" because Binance doesn't have data before the returned date
var last = candles.Last().OpenTime; // 7/2/2018 6:00:00 PM. This doesn't match the specified "endTime" because the limit is 1000, which means it cuts the result when it reaches 1000.

// Test case 2
var candles = _client.GetKlines(symbol: "TRXUSDT", interval: KlineInterval.ThirtyMinutes, startTime: new DateTime(2020, 06, 06), endTime: new DateTime(2020, 06, 15), limit: 1000).Data.SkipLast(1).ToList();
var first = candles.First().OpenTime; // 6/6/2020 12:00:00 AM - fine
var last = candles.Last().OpenTime; // 6/8/2020 8:00:00 AM - That's the latest data, because the specified "endTime" is greater than the current date and time.

Something very important:

That loop's only responsibility is when there are more than 1000 candles between a given start date and end date. For example if the start date is 2018-01-01 and end date: 2020-01-01 (2 years), assuming the interval remains 30 minutes (KlineInterval.ThirtyMinutes), there are going to be way more than 1000 candles, which means a single call to client.GetKlines(...) won't make it. It will return the first 1000 candles but not all of them. If the period between start date and end date is very short, we might end up with less than 1000 candles returned by client.GetKlines(...), which is fine. It might be better if I add a check to the loop (execute it only if the first call to client.GetKlines(...) returns 1000 candles; if less - do nothing).

The actual question:

I believe my code works fine and covers both test cases from above. Can you suggest me what to improve for better visual and performance look? A parallel loop would be a way to add multithreading in order to execute it faster.

// Input data
DateTime StartDate = new DateTime(2018, 06, 04);
DateTime EndDate = new DateTime(2019, 01, 15);

// Logic that has to be improved
var interval2 = KlineInterval.OneDay;
var candles = _client.GetKlines(symbol, interval2, startTime: StartDate, endTime: EndDate, limit: 1000).Data.ToList();
var firstCandle = candles.First();
var lastCandle = candles.Last();

while (true)
{
    var candles2 = _client.GetKlines(symbol, interval2, startTime: lastCandle.OpenTime, endTime: EndDate, limit: 1000).Data.ToList();

    firstCandle = candles.First();
    lastCandle = candles.Last();

    // This check prevents re-adding already existent data
    if (candles2.Last().OpenTime != lastCandle.OpenTime)
        candles.AddRange(candles2);

    if (EndDate == new DateTime(lastCandle.OpenTime.Year, lastCandle.OpenTime.Month, lastCandle.OpenTime.Day) || EndDate >= DateTime.Now)
        break;
}

foreach (var candle in candles)
{
    Console.WriteLine(candle.OpenTime);
}


texts:
I created a singleton class for managing sound effects on Android. This class will only be instanced and loaded once at the beginning, and each activity will use the loaded songs.

I don't know either if this is the good approach for Singleton, nor if this is the good way to play sounds in Android. This is working like a charm, and I'm wondering about the resource utilizations.

public class SoundManager {

    private static SoundManager mInstance;

    private SoundPool mSoundPool;

    private HashMap<Integer, Integer> mSoundPoolMap;

    private Vector<Integer> mAvailibleSounds = new Vector<Integer>();
    private Vector<Integer> mKillSoundQueue = new Vector<Integer>();

    private Handler mHandler = new Handler();
    private boolean mMuted = false;

    private static final int MAX_STREAMS = 2;
    private static final int KILL_AFTER = 3000;

    public static final int SOUND_SELECT = 0;
    public static final int SOUND_LOCKED = 1;

    @SuppressLint("UseSparseArrays")
    private SoundManager(Context context) {
        mSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
        mSoundPoolMap = new HashMap<Integer, Integer>();

        loadSounds(context);
    }

    public static SoundManager getInstance(Context context){
        if(mInstance == null){
            mInstance = new SoundManager(context);
            Log.d("SPARTA", "Instanciation");
        }
        return mInstance;
    }

    /**
    * Load all sounds and put them in their respective keys.
    * @param context
    */
    private void loadSounds(Context context){
        addSound(context, SOUND_SELECT, R.raw.metallic_knock);
        addSound(context, SOUND_LOCKED, R.raw.licorice);
    }

    /**
    * Put the sounds to their correspondig keys in sound pool.
    * @param context
    * @param key
    * @param soundID
    */
    public void addSound(Context context, int key, int soundID) {
        mAvailibleSounds.add(key);
        mSoundPoolMap.put(key, mSoundPool.load(context, soundID, 1));
    }

    /**
    * Find sound with the key and play it
    * @param key
    */
    public void playSound(int key) {
        if(mMuted)
        return;

        //If we have the sound
        if(mAvailibleSounds.contains(key)) {

            //We play it
            int soundId = mSoundPool.play(mSoundPoolMap.get(key), 1, 1, 1, 0, 1f);

            mKillSoundQueue.add(soundId);

            //And schedule the current sound to stop after set milliseconds
            mHandler.postDelayed(new Runnable() {
                public void run() {
                    if (!mKillSoundQueue.isEmpty()) {
                        mSoundPool.stop(mKillSoundQueue.firstElement());
                    }
                }
            }, KILL_AFTER);
        }
    }

    /**
    * Initialize the control stream with the activity to music
    * @param activity
    */
    public static void initStreamTypeMedia(Activity activity){
        activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    }

    /**
    * Is sound muted
    * @param muted
    */
    public void setMuted(boolean muted) {
        this.mMuted = muted;
    }
}


texts:
I made this class to handle any min max integer value and return a random number in the entered parameter range. I tested it to work with all combinations(I think). But is this totally wrong or written with unnecessary amounts of code? Are there obvious Java convention violations or obvious redundancy in this?

I was wondering if it was possible to do the same without instantiating Random, since I have read that object instantiation is more resource demanding than method invoking, like invoking Math.random(). I just couldn't make that work, unfortunately, as I didn't save the strange non-working code that that ended with.

I tried the solution here. However, I don't understand what the rand() part is, which is too bad since it seemed really simple with just one line of code.

abstract class RandomInteger {
     static int randomNumber;

     public static int returnRandomIntRange(int start, int end){

         if(end < start){
             throw new IllegalArgumentException("Start cannot exceed End.");
         } else if(end == 0 && start == 0){
             throw new IllegalArgumentException("Start and End can't both be 0.");
         } else if(end == start){
             throw new IllegalArgumentException("Start and End can't be the same.");

         } else if(end >= 0 && start <= 0){
             Random random=new Random();
             int range;
             range = end - start + 1;
             System.out.println("aEnd > 0 && aStart < 0. 1range is: " + range);
             randomNumber=(random.nextInt(range))+(start);
             System.out.println("aEnd > 0 && aStart >= 0. invoked. 1randomNumber is: " + randomNumber);

         } else if(end > 0 && start > 0){
             Random randomGenerator=new Random();
             int range;
             range = end - start + 1;
             System.out.println("aEnd > 0 && aStart >= 0. invoked. 2range is: " + range);
             randomNumber = randomGenerator.nextInt(range) + start;
             System.out.println("aEnd > 0 && aStart >= 0. invoked. 2randomNumber is: " + randomNumber);

         } else if(end < 0 && start < 0){
             Random randomGenerator=new Random();
             int range;
             range = (start - end -1) * -1;
             System.out.println("aEnd <= 0 && aStart < 0. invoked. 3range is: " + range);
             randomNumber = ((randomGenerator.nextInt(range)+ start));
             System.out.println("aEnd <= 0 && aStart < 0. invoked. 3randomNumber is: " + randomNumber);
         }

         return randomNumber;
     }

}


texts:
I was working on Haskell Problem 21:

  Insert an element at a given position into a list.

  Example:

* (insert-at 'alfa '(a b c d) 2)
(A ALFA B C D)

I have two different solutions:

--Problem 21 (also not tail recursive)
insertAt :: a -> [a] -> Int -> [a]
insertAt v [] n = [v]
insertAt v arr 1 = (v:arr)
insertAt v (x:xs) n = (x:(insertAt v xs $ n - 1))

--Tail recursive version
insertAtB :: a -> [a] -> Int -> [a]
insertAtB v arr n = loop v n arr []
    where loop v n [] acc = (myReverse acc) ++ [v]
          loop v 1 arr acc = (myReverse acc) ++ [v] ++ arr
          loop v n (x:xs) acc = loop v (n - 1) xs (x:acc)

Which is better stylistically? Which is better in terms of runtime and memory use?

How can you determine what order of arguments is the best for currying? Is it only based on your own usage or is there a better way to determine this?


texts: Remove duplicates from array of non-equatable objects

texts:
I have an array of non-Equatable objects. I want to remove all the duplicates from the array.

class SearchResult { // non-equatable
    var index: Int!
    var score: Int!

    init(_ index: Int, score: Int = 0) {
        self.index = index
        self.score = score
    }
}
var searchResults: [SearchResult] = [SearchResult(0), SearchResult(1), SearchResult(1), SearchResult(2), SearchResult(2)]

searchResults.map { $0.index } gives us: 0, 1, 1, 2, 2

Currently, I do so by mapping the objects' property index, which is equatable, since it's an Int. This way, I can remove the duplicates from the indices array:

let withDupes: [SearchResult] = searchResults
let indices = withDupes.map { $0.index }.removingDuplicates()

For reference, .removingDuplicates() comes from a post on StackOverflow

Note: this is the part I want to be reviewed.

Then, I get the object with the corresponding index by looping through the indices array and add it to the noDupes array.

var noDupes: [SearchResult] = []
indices.forEach { (index) in
    guard let notADupe = (withDupes.filter { $0.index == index }).first else { return }
    noDupes.append(notADupe)
}

noDupes.map { $0.index } now is: 0, 1, 2

Now, this code works, but it will be executed very often. Since I feel like this is not a very efficient way to remove the duplicates, I fear a performance drop.
How can I improve and / or simplify my code and still successfully remove the duplicates from the array?


texts:
Scenario

Currently filtering through a list of ~3100 rows of data presented to the end-user in the form of an index (think, list of car models).

This is done in such a way that every item is available in a long list to the end-user however they have the possibility to filter down their results into something more relevant to them.

I have gone through three variations of my filtering functionality which has greatly improved, however the fact that my jQuery skills lack experience, was wondering whether or not my implementation can be improved in the following ways:

Readability
Performance

var lis = $('#some-list .some-entry');
var list = $.makeArray(lis.map(function(k, v) {
    return $(v).text().toLowerCase();
}));

var filterDelay = (function() {
    var timer = 0;
    return function(callback, ms) {
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
})();

$('#filter').keyup(function() {
    var userInput = $(this).val();

    filterDelay(function() {
        var count = 0;
        lis.each(function(index, value) {
            if (list[index].indexOf(userInput) >= 0) {
                count++;
                $(value).show();
            } else {
                $(value).hide();
            }
        });

        if (count == 1) {
            $("#filter-results").text(count + " Result");
        } else {
            $("#filter-results").text(count + " Results");
        }
    }, 250);
});

The reason for the callback functionality is so that jQuery/JS does not start filtering through ~3100 rows of data every time the user presses a key. Instead, we want to wait for 250ms and then fall back to our filtering functionality afterwards.

This greatly helps the user-experience and is overall less stressing on the browser.

Question

My previous examples used toggle(bool) rather than show() or hide() to add or remove a row from the list (when filtered). Are there any inherent differences in terms of performance between the two?

Additionally, I am looking to implement a loading animation when:

The user is typing
The filter has starting filtering

And the removing it when the filtering is complete. Thus, would it make sense to toggle the loading animation, or add/remove it from the DOM respectively?

Toggle it off would mean that the animation is still occurring in the background regardless whether or not any filtering is being made. Adding it or removing to the DOM however will have its own performance drop.

What would one recommend? I don't want to make it seem like it's for micro-optimization purposes but simply looking to implement my filter functionality in a neater way for possible future re-use.

Update(s)

Added load animations by combining Bootstrap and jQuery like so:

$('#filter').keyup(function() {
    //...
    $('#filter-icon-load').removeClass('hidden');
    //...
    $('#filter-icon-load').addClass('hidden');
    //...
});


texts: Generating all variations of M*N matrix containing boolean values

texts:
I've implemented an algorithm that generates all variations of a matrix of size \$M*N\$. My approach is that I see a matrix as a list where the position of elements can be calculated from the position in the matrix. By doing that I can reduce the problem into generating all variations of a list of length \$M*N\$.

The algorithm computes the variations recursively. I start with an empty list and pass it to my function. The list is copied. The copied list gets a 0 and the original list gets a 1 added. Then I call the function recursively with each of both resulting lists. The process is repeated until the length \$M*N\$ is reached. At this point, I have the set of all variations (size = \$2^{M*N}\$).

Now I can compute the arrays from those variations and compose the resulting matrices.

Here is the implementation:

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

class Main {

    static void addVariations(Deque<int[]> stack, int[] variation, int index) {
        if (index >= 0) {
            // clone for next recursion
            int[] variationClone = variation.clone();

            // one gets 0, the other 1 at index
            variation[index] = 0;
            variationClone[index] = 1;

            // next recursion
            addVariations(stack, variation, index - 1);
            addVariations(stack, variationClone, index - 1);
        }
        else {
            stack.push(variation);
        }
    }

    static Deque<int[][]> getVariations(int M, int N) {
        int variationLength = M*N;

        // get all variations that the matrices are base on
        // there are n^r, 2^variationLength of them
        Deque<int[]> variations = new ArrayDeque<>();
        addVariations(variations, new int[variationLength], variationLength - 1);

        // container for resulting matrices
        Deque<int[][]> variationMatrices = new ArrayDeque<>();

        // for each matrix
        for (int i = variations.size() - 1; i >= 0 ; i--) {
            int[][] matrix = new int[N][M];
            int[] variation = variations.pop();

            // for each row add part of variation
            for (int j = 0; j < matrix.length; j++) {
                matrix[j] = Arrays.copyOfRange(variation, j*M, (j + 1)*M);
            }

            // and push the matrix to result
            variationMatrices.push(matrix);
        }
        return variationMatrices;
    }

    public static void main(String[] args) {
        int N = 2, M = 2;
        Deque<int[][]> variations = getVariations(N, M);

        variations.forEach(v -> {
            System.out.println("----");
            for (int i = 0; i < v.length; i++) {
                System.out.println(Arrays.toString(v[i]));
            }
            System.out.println("----");
        });
    }
}

I'd be very happy to get tipps on how to improve the code. I'm especially interested in style and readability of my code.

The expected output is the set of variations. Each variation is a matrix containing only 0s and 1s. Each matrix is represented as a 2-dimensional array where each row is an array.

So e.g. the output for a \$2*2\$ matrix is supposed to be:

[[0, 0], [0, 0]],
[[1, 0], [0, 0]],
[[0, 1], [0, 0]],
[[1, 1], [0, 0]],

[[0, 0], [1, 0]],
[[1, 0], [1, 0]],
[[0, 1], [1, 0]],
[[1, 1], [1, 0]],

[[0, 0], [0, 1]],
[[1, 0], [0, 1]],
[[0, 1], [0, 1]],
[[1, 1], [0, 1]],

[[0, 0], [1, 1]],
[[1, 0], [1, 1]],
[[0, 1], [1, 1]],
[[1, 1], [1, 1]]

The order doesn't matter.


texts: Calling PL/SQL functions using JDBC in a Spring clone

texts:
I started to modify some old code and ended up creating a sort of spring jdbc framework.
I know just the most popular design pattern, so I was wondering if my design was good.
For example, to call store procedures, I created an interface PlsqlParameter, to hold in and out parameter and cycle them.
But for example, InPlsqlParameter is forced to implements getItemFromStatement, with no return (because it's not an output).

How can I improve my design?
My project on github

the main method:

public void genericProcedure(Connection conn, String functionName, PlsqlParameter ... queryParams)  throws Exception
{
    CallableStatement cstm = null;

    String query = getQueryProcedure(functionName, queryParams).toString();
    boolean paramEmpty = queryParams.length==1 && queryParams[0]==null;

    try
    {
        cstm = conn.prepareCall(query);
        int i=1;
        if (!paramEmpty)
        {
            for (PlsqlParameter ob:queryParams)
            {
                ob.setParameter(i++, cstm);
            }
        }

        cstm.execute();
        if (!paramEmpty)
        {
            i=1;
            for (PlsqlParameter ob:queryParams)
            {
                ob.getItemFromStatement(i++, cstm);
            }
        }
    }
    catch (Exception e)
    {
        traceErrorLog(e, query, (Object[])queryParams);
        throw e;

    } finally
    {
        if (cstm!=null)
        {
            cstm.close();
        }
    }
}

getItemFromStatement return null:

public class InPlsqlParameter <T> implements PlsqlParameter {

    ItemContainer<T> item;

    public InPlsqlParameter() {
        super();
    }

    public InPlsqlParameter(T item) {
        super();
        this.item = new SingleItem<T>(item);
    }

    public InPlsqlParameter(ItemContainer<T> item) {
        super();
        this.item = item;
    }

    @Override
    public void setParameter(int parameterIndex, CallableStatement cstm) throws SQLException {
        cstm.setObject(parameterIndex, item!=null ? item.getObject() : null);
    }

    @Override
    public ItemContainer<?> getItemFromStatement(int parameterIndex,
            CallableStatement cstm) throws Exception {
        return null;
    }

    @Override
    public String toString() {
        return item!=null ? item.toString() : "";
    }

}

This is ok:

public class OutPlsqlParameter implements PlsqlParameter {

    ItemContainer<?> item;

    public OutPlsqlParameter(ItemContainer<?> item) {
        super();
        this.item = item;
    }

    @Override
    public void setParameter(int parameterIndex, CallableStatement cstm) throws SQLException {
        item.setOutParameter(parameterIndex, cstm);
    }

    @Override
    public ItemContainer<?> getItemFromStatement(int parameterIndex, CallableStatement cstm) throws Exception
    {
        return item.getObjectFromStatement(parameterIndex, cstm);
    }

    @Override
    public String toString() {
        return item!=null ? item.toString() : "";
    }

}


texts:
Is storing array values inside of a closure considered to be bad practice?  I having been trying to decide if I should just use a standard object with key value pair.  This is not the method I am using to populate arrays, but this does show how i create functions to store values.

Also, I'm wondering if this is technically a closure.

http://jsfiddle.net/canoeBoy/sn3wgs0w/

<input type="button" onclick="alert(Test1(0,0,'array'));
alert(Test2(0,0,'array'));
alert(Test3(0,0,'array'));">

function makeClosures(){
    var nameArr=["Test1","Test2","Test3","Test4","Test5","Test6","Test7"];
    for(var i=0;i<nameArr.length;i+=1){
        var nam=nameArr[i];
        window[nam]=funcsRun(nam,0,1);
    }
}

function funcsRun(nam,index,action){
    var a=nam;
    var arr=[];

    return function(value,index,action) {
        if(action=='array'){

        }
        else if(action=='copyEntireArray'){
            arr=value;
        }
        else if(action=='resetArray'){
            arr=[];
        }
        else if(action=='copyToIndex'){
            arr[index]=value;
        }

        else if(action=='indexValue'){
            return arr[index];
        }
        else if(action=='push'){
            arr.push(value);
        }
        return arr;
    };
};

makeClosures();

for(i=0;i<100;i+=1){
   Test1(Math.floor((Math.random() * 100) + 1),i,'copyToIndex');
   Test2(Math.floor((Math.random() * 100) + 1),i,'copyToIndex');
   Test3(Math.floor((Math.random() * 100) + 1),i,'copyToIndex');

}


texts:
I have something like a shop. It is limited to a checkout page, which will send an email. Nothing big, but a nice thing to learn more OOP.

Almost every time I develop new classes I am wondering how to organize the flow of depending methods. Here is an example, a first draft of what I have:

<?php

// Outside the class:
if (!empty($cms->request->post('add_to_cart'))) {
    $cart->addStrt();
}

OPTION A

// OPTION A: Build a "method chain" and move from one to the other, to the next …
Class Cart extends Shop {

    // This is the entry point and starts the "add process"
    public function addStart() {
        $this->checkQuantity(); // if qty is between min/max
        return false;
    }
    // Step 2
    public function checkQuantity() {
        if ($quantity < $max && $quantity > $min) {
            $this->checkInStock(); // move on to the next step
        }
        return false;
    }
    // Step 3
    public function checkInStock() {
        if ($availabe >= $qty_in_basket) {
            $this->addToCart(); // move on to the final step
        }
        return false;
    }
    // Step 4
    public function addToCart() {
        // Final step, all requirements ok
        $product_id = $this->request->post->product_id;
        $cart = $this->getCart();
        $product = $shop->getProduct($product_id);

        $cart->items->add($product);
        $added = $cart->save();

        return $added;
    }
}

OPTION B

// OPTION B: Have a single "add" method and check all requirements, sanitize, etc from a single method
Class Cart extends Shop {

    public function add() {
        $continue = $this->checkQuantity(); // if qty is between min/max
        if ($continue == false) return false;

        $continue = $this->checkInStock(); // if product still available
        if ($continue == false) return false;

        // Perhaps more checks, sanitize, etc.

        // Everythiing mus be ok, put item into cart
        $product_id = $this->request->post->product_id;
        $cart = $this->getCart();
        $product = $shop->getProduct($product_id);

        $cart->items->add($product);
        $added = $cart->save();

        return $added;
    }
}

I have read tutorials on some OOP patterns but can't seem to find which fits to my question. While Option B seems to be cleaner (to understand/extend) one thing I always read is never put multiple tasks into one method. Every method should only handle one specific task. But how/where do I control the flow?

Is this "Encapsulation" or "Decorator pattern" or "Dependency Injection"? I have no idea what I should google.


texts: CodingBat warm up: conditionally prepending "not" to a string

texts:
I solved this CodingBat problem:

  Given a string, return a new string where "not " has been added to the front. However, if the string already begins with "not", return the string unchanged. Note: use .equals() to compare 2 strings.

notString("candy") → "not candy"
notString("x") → "not x"
notString("not bad") → "not bad"

Here is the solution I came up with:

public String notString(String str) {
  if(str.indexOf("not") == 0){
    return str;
  }
  return "not " + str;
}

I was wondering how it compared to their solution:

public String notString(String str) {
  if (str.length() >= 3 && str.substring(0, 3).equals("not")) {
    return str;
  }

  return "not " + str;
}

Obviously neither is "wrong" but I was just wondering (in the long run) which has better syntax / more efficient code?


texts:
Here is the Euler problem referenced, it says:

  The n\$^{th}\$ term of the sequence of triangle numbers is given by, t\$_n\$ =
  ½n(n+1); so the first ten triangle numbers are:

  1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

  By converting each letter in a word to a number corresponding to its
  alphabetical position and adding these values we form a word value.
  For example, the word value for SKY is 19 + 11 + 25 = 55 = t\$_{10}\$. If the
  word value is a triangle number then we shall call the word a triangle
  word.

  Using words.txt (right click and 'Save Link/Target As...'), a 16K text
  file containing nearly two-thousand common English words, how many are
  triangle words?

My solution is as follows:

import re
import string

#generate a key to map the letters in our strings to
letters = string.ascii_uppercase
letters_map = {letter: idx for idx, letter in enumerate(string.ascii_uppercase, start=1)}

#open the file and parse on quoted values
with open('words.txt') as f:
    words = sorted(re.findall(r'"(.*?)"', f.read()))

#max word score can only be Z*len(n), recall 'Z' : 26 in letters_map
max_word_score = len(max(words, key=len))*26

#generate a list of triangle_numbers to search for using max_score as upper bound
n = 1
triangle_numbers = []
while 1/2.0*n*(n+1) <= max_word_score:
    triangle_numbers.append(1/2.0*n*(n+1))
    n += 1

#finally use a nested for loop to return number of triangle numbers in our now summed words
count = 0
for word in words:
    if sum(letters_map[char] for char in word) in triangle_numbers:
        count += 1

%%timeit
100 loops, best of 3: 3.67 ms per loop

I'm curious if there are any ways I can rewrite this with similar or better performance so that it is more pythonic. I'm curious about a few things more specifically:

1) Is it possible to rewrite this better using itertools.ifilter and itertools.imap using letters_map and the filter being the list of triangle_numbers, to greater affect than simply using a nested for loop and an if statement to filter?

2) Is my while construction extremely not pythonic? It felt awkward writing it the way it was and I wasn't sure what a better way of operating under that framework would be (needing to count up to some arbitrary ceiling, in this case max_word_score.)

3) Should I be getting in the habit of wrapping these items in mini-functions in order to make it more readable/replicable? Would that end up slowing runtime down? What sort of effect would that have in general.

4) Any other pythonizations I can use to better effect?


texts: Looping to update article codes on one server based on queries on another server

texts:
I've read somewhere on Stack Overflow that doing queries in a loop is very inefficient. It will hammer your SQL server and make your script very slow.

Sample code:

// Connect to SQL Server 1
$query = "SELECT * FROM
articles
";

$resource = mysql_query($query);
$articles = array();
while $record = mysql_fetch_assoc($resource) {
    $articles[] = $record;
}

// The part that bothers me:
// (Note: this is executed on a different sql server)
foreach ($articles as $article) {
    // Connect to SQL server 2
    $sQuery = "SELECT artcode FROM articles WHERE id='".$article['id']."'";
    $sResource = mysql_query($query);
    if (mysql_num_rows($sResource) == 1) {
        $data = mysql_fetch_assoc($sResource);
        // Connect to SQL Server 1 again ...
        // This will be executed on the first sql server again
        $uQuery = "UPDATE articles SET artcode='".$data['artcode']."' WHERE id='".$article['id']."' LIMIT 1";
        $uResource = mysql_query($query);
    }
}

How would I go and make this code more efficient. (In best case scenario avoiding doing the query in a loop.)


texts:
I have applied some optimizations like storing only odd values
and starting to mark off from square of the number.

Can it be optimized further?

bool * isPrime = new bool [n/2];

for (int i=0; i < n/2; ++i)
    isPrime [i] = true;

cout<< 2 << "\n";

for (int i = 3; i < n; i += 2) {

    if (isPrime [i / 2]) {

        cout<< i << "\n";

        for (int j = i * i; j < n; j += 2 * i)
            isPrime [j / 2] = false;
    }
}


texts: SSH rate limiting using Puppet firewall module

texts:
I used to do simple SSH rate limiting using an UFW Puppet module. To get more flexible blocking I've changed to using the official firewall module instead. Are there any significant issues with the following?

In manifests/host.pp:

include ssh_server
include my_firewall
[…]
resources { 'firewall':
  purge => true,
}

Firewall {
  before  => Class['my_firewall::post'],
  require => Class['my_firewall::pre'],
}

In modules/my_firewall/manifests/init.pp:

class my_firewall {
  include my_firewall::pre
  include my_firewall::post

  package { 'ufw':
    ensure => absent,
  }
}

In modules/my_firewall/manifests/pre.pp:

class my_firewall::pre {
  Firewall {
    require => undef,
  }
  # Default firewall rules
  firewall { '000 accept all icmp':
    proto  => 'icmp',
    action => 'accept',
  } ->
  firewall { '001 accept all to lo interface':
    proto   => all,
    iniface => lo,
    action  => accept,
  } ->
  firewall { '002 reject local traffic not on loopback interface':
    iniface     => '! lo',
    proto       => all,
    destination => '127.0.0.1/8',
    action      => reject,
  } ->
  firewall { '003 accept related established rules':
    proto  => all,
    state  => ['RELATED', 'ESTABLISHED'],
    action => accept,
  }
}

In modules/my_firewall/manifests/post.pp:

class my_firewall::post {
  firewall { '999 drop all':
    proto  => 'all',
    action => 'drop',
    before => undef,
  }
}

In modules/ssh_server/manifests/init.pp:

firewall { '200 limit incoming SSH connections to 6 per minute':
  dport     => 22,
  proto     => tcp,
  recent    => update,
  rseconds  => 60,
  rhitcount => 6,
  rname     => 'SSH',
  rsource   => true,
  action    => drop,
} ->
firewall { '201 allow incoming SSH connections':
  dport   => 22,
  proto   => tcp,
  recent  => set,
  rname   => 'SSH',
  rsource => true,
  action  => accept,
}


texts: Needs code review for class implementation

texts:
could you please review the code for the implementation of the class (link: General review of Robot class)

require_relative 'robot'
class Game

  def initialize
    # initialize values
    @commands = %w{PLACE MOVE LEFT RIGHT REPORT}
    @facings = %w{NORTH SOUTH EAST WEST}
    @pos_X, @pos_Y = nil, nil
    @facing, @robot, @r = nil, nil, nil
    command = get_initial_command
    setup(command)
  end

  def setup(command)
    params = command.gsub(/[PLACE]/, '').split(',')
    if (params[0] != nil && params[1] != nil && params[2] != nil)
      @pos_X  = params[0].to_i
      @pos_Y  = params[1].to_i
      @facing = params[2]
      @r = Robot.new(@pos_X, @pos_Y, @facing)
      if is_valid_position
        @robot = @r
      else
        Game.new
      end
      command = get_commands
    else
      Game.new
    end
  end

  def get_initial_command
    command = gets.strip
    if command.include?("PLACE")
      return command
    else
      command = get_initial_command
    end
  end

  def get_commands
    is_command = false
    command = gets.strip
    @commands.each do |c|
      if command.include?(c)
        is_command = true
        break
      end
    end
    if is_command
      translate_command(command)
      command = get_commands
      return command
    else
      command = get_commands
    end
  end

  def translate_command(command)
    case command
    when command.include?('PLACE')
      puts 'place the command'
      setup(command)
    when 'MOVE'
      @robot.move if is_valid_move
    when 'RIGHT'
      @robot.right
    when 'LEFT'
      @robot.left
    when 'REPORT'
      @robot.report
    when 'EXIT'
      Kernel.abort(false)
    end
  end

  def is_valid_move
    if ((@robot.instance_variable_get("@pos_X") == 0 && @robot.instance_variable_get("@facing") == 'SOUTH') ||
        (@robot.instance_variable_get("@pos_X") == 5 && @robot.instance_variable_get("@facing") == 'NORTH') ||
        (@robot.instance_variable_get("@pos_Y") == 0 && @robot.instance_variable_get("@facing") == 'WEST') ||
        (@robot.instance_variable_get("@pos_Y") == 5 && @robot.instance_variable_get("@facing") == 'EAST'))
          return false
    else
      return true
    end
  end

  def is_valid_position
    is_proper_facing = false
    @facings.each do |f|
      if f.include?(@r.instance_variable_get("@facing"))
        is_proper_facing = true
        break
      end
    end
    if (@r.instance_variable_get("@pos_X") < 0 || @r.instance_variable_get("@pos_X") > 5 ||
      @r.instance_variable_get("@pos_Y") < 0 || @r.instance_variable_get("@pos_Y") > 5 || !is_proper_facing)
      return false
    else
      return true
    end
  end
end

a = Game.new


texts: Perform additional tests before waiting for an event

texts:
I was looking for a way to perform additional tests before waiting for an event:

if everything_is_alright():
    event.wait()

But this is not thread-safe. A correct version would be:

with lock:
    wait = everything_is_alright() and not even.is_set()
if wait:
    event.wait()

The problem is that the lock have to be acquired everytime the event is set/cleared. So I thought about:

with event:
    if everything_is_alright():
        event.wait()

where event has an internal lock that would be released before it actually starts waiting. Since it looks very much like a Condition, I sub-classed both Condition and Event to create this object:

from threading import _Condition, _Event

# Lock Event
class LockEvent(_Condition, _Event):
    """Event that can be locked to perform additional test."""

    def __init__(self):
        """Initialize the event."""
        _Condition.__init__(self)
        _Event.__init__(self)

    def wait(self, timeout=None):
        """Wait for the event to be set."""
        with self:
            if not self.is_set():
                _Condition.wait(self, timeout)
            return self.is_set()

    def set(self):
        """Set the event."""
        with self:
            _Event.set(self)
            self.notify()

    def clear(self):
        """Clear the event."""
        with self:
            _Event.clear(self)

This is what the documentation says about _Condition.wait:

  When the underlying lock is an RLock, it is not released using its release() method, since this may not actually unlock the lock when it was acquired multiple times recursively. Instead, an internal interface of the RLock class is used, which really unlocks it even when it has been recursively acquired  several times. Another internal interface is then used to restore the recursion level when the lock is reacquired.

So far the code is working properly, but:

am I missing a race condition?
is there a simpler solution using the standard threading objects?


texts:
I have two very similar methods, which make HTTP requests, the difference is that one makes PUT and another GET. Is there any proper Ruby way not to repeat the setup code and not to pass the flag parameter?

def notify_client(url, params)
  uri = URI.parse(url)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = !Rails.env.development?
  req = Net::HTTP::Patch.new(uri.path)
  req.body = {data: {attributes: params}}.to_json
  res = https.request(req)
  puts "Response #{res.code} #{res.message}: #{res.body}"
end

def notify_vendor(url, params)
  uri = URI.parse(url)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = !Rails.env.development?
  req = Net::HTTP::Get.new(uri.path)
  req.body = {data: {attributes: params}}.to_json
  res = https.request(req)
  puts "Response #{res.code} #{res.message}: #{res.body}"
end


texts:
I have a Morse Code translator, which can translate text to Morse code and vice-versa. Note that for the two files that the first three lines are generated by the Code::Blocks IDE.

translator.h

#ifndef SPLIT_H_INCLUDED
#define SPLIT_H_INCLUDED
#endif // SPLIT_H_INCLUDED

#include <iostream>
#include <map>
#include <string>
#include "split.h"

std::string SPACE (" ");
std::map<char, std::string> charMap;
std::map<std::string, char> morseMap;
bool hasInit = false;

void init()
{
    charMap['a'] = ".-";
    charMap['b'] = "-...";
    charMap['c'] = "-.-.";
    charMap['d'] = "-..";
    charMap['e'] = ".";
    charMap['f'] = "..-.";
    charMap['g'] = "--.";
    charMap['h'] = "....";
    charMap['i'] = "..";
    charMap['j'] = ".---";
    charMap['k'] = "-.-";
    charMap['l'] = ".-..";
    charMap['m'] = "--";
    charMap['n'] = "-.";
    charMap['o'] = "---";
    charMap['p'] = ".--.";
    charMap['q'] = "--.-";
    charMap['r'] = ".-.";
    charMap['s'] = "...";
    charMap['t'] = "-";
    charMap['u'] = "..-";
    charMap['v'] = "...-";
    charMap['w'] = ".--";
    charMap['x'] = "-..-";
    charMap['y'] = "-.--";
    charMap['z'] = "--..";
    charMap['0'] = "-----";
    charMap['1'] = ".----";
    charMap['2'] = "..---";
    charMap['3'] = "...--";
    charMap['4'] = "....-";
    charMap['5'] = ".....";
    charMap['6'] = "-....";
    charMap['7'] = "--...";
    charMap['8'] = "---..";
    charMap['9'] = "----.";
    charMap['.'] = ".-.-.-";
    charMap[','] = "--..--";
    charMap['?'] = "..--..";
    charMap[' '] = "/";
    for (std::map<char, std::string>::iterator i = charMap.begin(); i != charMap.end(); i++) {
        morseMap[i -> second] = i -> first;
    }
}

std::string translateToMorse(std::string plainText)
{
    if (!hasInit) {
        init();
    }
    std::string result;
    for (int i = 0; plainText[i]; i++) {
        result.append(charMap[plainText[i]]);
        result.append(SPACE);
    }
    return result;
}

std::string translateFromMorse(std::string morseText)
{
    if (!hasInit) {
        init();
    }
    std::string result;
    std::vector<std::string> morse = split(morseText, SPACE[0]);
    for (std::vector<std::string>::iterator i = morse.begin(); i != morse.end(); i++) {
        result.push_back(morseMap[*i]);
    }
    return result;
}

split.h

#ifndef SPLIT_H_INCLUDED
#define SPLIT_H_INCLUDED
#endif // SPLIT_H_INCLUDED

#include <string>
#include <sstream>
#include <vector>

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

Concerns:

Are the three generated lines necessary and are they used correctly?
Is there a way to simplify the code?


texts:
I have written some code to suspend someone's enrollment to a course. A course may have multiple presentations.

module Enrolments
  class Suspend
    attr_reader :enrolment, :responses, :comment, :user_id
    attr_writer :event_service, :suspend_enrolment_presentation_service

    def initialize
      @responses = []
    end

    def call(enrolment, user_id, comment)
      @enrolment = enrolment
      @user_id = user_id
      @comment = comment

      suspend_enrolment_presentations
      if all_enrolment_presentations_suspended?
        suspend_enrolment
        raise_suspended_enrolment_event
        create_enrolment_comment
      end
    end

    private

    attr_reader :response

    def suspend_enrolment_presentations
      enrolment.enrolment_presentations.each do |ep|
        @responses << suspend_enrolment_presentation_service.call(ep)
      end
    end

    def all_enrolment_presentations_suspended?
      @responses.all?{ |r| r[:success] }
    end

    def suspend_enrolment
      enrolment.status = 'Suspended'
      enrolment.save
    end

    def raise_suspended_enrolment_event
      event_service.call(enrolment)
    end

    def create_enrolment_comment
      EnrolmentComment.create(comment: comment, user_id: user_id, enrolment_id: enrolment.id)
    end

    def event_service
      @event_service ||= Events::Outgoing::Enrolments::SuspendedEnrolment.new
    end

    def suspend_enrolment_presentation_service
      @suspend_enrolment_presentation_service ||= Enrolments::EnrolmentPresentations::Suspend.new
    end
  end
end

The code is fairly straight forward. It has a couple of dependencies that are injected via attr_writers when testing, otherwise they are instantiated when accessed.

I have heard/read a lot of issues regarding callbacks. The problem with this code is if I ever want to just suspend an enrollment and not raise an event or create a comment, I can't. Some ways to get around this are with the decorator pattern or I guess I could compose and object of all the services and manage everything in there.

I really strive for SRP and good design in my code. I would really appreciate any input on how I could improve this code. Should I be extracting the code to a higher level of abstraction and dealing with it one level up? What if running one piece of code depends on another passing? If I was to extract this out it would land up in my controller which is as high as I can go, unless I then extracted that into a form object.