texts: Advent of Code Day 3, how to optimize my ruby solution?

texts:
I have solved the 3rd day puzzle of Advent of Code but wanted some feedback on my solution. The puzzle here wants to find where two jumbled wires intersect closest to the origin.

Puzzle input is provided like so: R8,U5,L5,D3 representing the path of each string. Part A of the puzzle asks you to find the closest intersection to the origin calculated by Manhattan distance, and part B looks for the intersection that is the fastest path for both strings.

My solution was to store every point visited by each wire and use that path to then locate the closest or fastest intersection:

# frozen_string_literal: true

data = File.read('day3.txt').split("\n")
direction_code_arrays = data.map { |string_path| string_path.split(',') }

def calculate_path(direction_code_array)
  path = [[0, 0]]

  direction_code_array.each do |direction_code|
    direction = direction_code.slice(0, 1)
    number = direction_code.slice(1..-1).to_i

    while number.positive?
      prev_position = path.last
      new_position = Array.new(2, 0)
      new_position[0] = prev_position[0]
      new_position[1] = prev_position[1]

      case direction
      when 'R'
        new_position[0] += 1
      when 'L'
        new_position[0] -= 1
      when 'U'
        new_position[1] += 1
      when 'D'
        new_position[1] -= 1
      end

      number -= 1
      path << new_position
    end
  end
  path
end

def intersection_points(path1, path2)
  intersections = path1 & path2
  # origin at (0,0) should be excluded
  intersections.slice(1..-1)
end

def manhattan_distance(point)
  point[0].abs + point[1].abs
end

def number_steps_to_point(point, path1, path2)
  path1.find_index(point) + path2.find_index(point)
end

def shortest_manhattan_distance(path1, path2)
  shortest_distance = nil

  intersection_points = intersection_points(path1, path2)

  intersection_points.each do |intersection|
    if shortest_distance.nil? || manhattan_distance(intersection) < shortest_distance
      shortest_distance = manhattan_distance(intersection)
    end
  end
  shortest_distance
end

def fastest_intersection_point(path1, path2)
  fastest_point = nil

  intersection_points = intersection_points(path1, path2)

  intersection_points.each do |intersection|
    if fastest_point.nil? || number_steps_to_point(intersection, path1, path2) < fastest_point
      fastest_point = number_steps_to_point(intersection, path1, path2)
    end
  end
  fastest_point
end

string_1_path = calculate_path(direction_code_arrays[0])
string_2_path = calculate_path(direction_code_arrays[1])

print shortest_manhattan_distance(string_1_path, string_2_path)
print fastest_intersection_point(string_1_path, string_2_path)

To my understanding the time complexity of this solution is \$\mathcal{O}(n)\$, as each direction code in the array is iterated over once, while the space complexity is really poor as each code can generate hundreds of points in the path array. If anyone can explain the time and space complexity of my solution more clearly or offer any advice in optimizing I would very much appreciate it!


texts: Modify array of arrays to create custom array of objects

texts:
I have an array of arrays and I want to create an array of objects from it. There are custom properties to which I want to associate the sub-array values. I have the following array:

var testArray = [[1,5,10], [10,20,7], [11,10,25]];

I am modifying it to create the following

[{
    coordinates: [1, 5],
    reach: 10,
    power: 0
},
{
    coordinates: [10, 20],
    reach: 7,
    power: 0
},
{
    coordinates: [11, 10],
    reach: 25,
    power: 0
}]

For this I have written the following code.

function modifyArray(array) {
    return array.map(array => {
        return Object.assign({}, { coordinates: array.slice(0, 2), reach: array[2], power: 0 });
    });
}

I get the desired result. My question is if this is a good way to modify this type of data performance wise, since the function has two return statements. How can I improve on this code?


texts: Light Security Local-Machine Password Encryption

texts:
I have a program that makes a call to an SQL database. I cannot use SSH to connect to it (as far as I am aware) or any other more secure method, so Java's SQL connector was used. As the connection requires a username/password, and I want to store the user/pass for ease of making the connection. I knew I was not going to store the password in plain text, but I couldn't find a method of doing so in a secure manner in a platform-independent way in core Java, so I rolled my own (not advised in crypto, I know).

Below is the code that encrypts a message of any length with a key of any length.

private static String xor(String message, String key){
    if(message.isEmpty())
        return "";
    byte[] xorBlob=new byte[message.length()];
    long seed=0;
    for(char c: key.toCharArray())
        seed+=c;
    new Random(seed).nextBytes(xorBlob);
    char[] mArr=message.toCharArray();
    for(int i=0;i<mArr.length;i++)
        mArr[i]=(char)(mArr[i]^xorBlob[i]);
    return new String(mArr);
}

To put the above code into words:
It sums up all the char values of the given key into an int. That int is then used as the seed for the Random function, which then spits out an array of bytes the length of the message. The message and that byte array are then xor'd together.

It's rudimentary, but I figured for opt-in local storage of a password for the current user, it should suffice.

Is this a good method of doing so? Is there a way I can improve this code, for either more security or just better code to more fit industry standards?

EDIT:
Here's the code that calls the above function:

FileWriter writer=new FileWriter(config);
writer.write(String.format("username=%s%n", username));
writer.write(String.format("password=%s%n", xor(password, username)));


texts:
I am trying to obtain all possible ways to go from a city A to a city B on a given day by plane, with a maximum of 2 stops.

I have as input a list of dictionaries:

flights = [
        ...
        {'dep': 'FRA', 'arr': 'AMS', 'dep_dt': '2017-05-01 12:00:00', 'arr_dt': '2017-05-01 13:15:00', 'price': 100},
        {'dep': 'FRA', 'arr': 'CPH', 'dep_dt': '2017-05-01 10:00:00', 'arr_dt': '2017-05-01 12:00:00', 'price': 80},
        {'dep': 'FRA', 'arr': 'MAD', 'dep_dt': '2017-05-01 09:00:00', 'arr_dt': '2017-05-01 10:50:00', 'price': 30},
        {'dep': 'CPH', 'arr': 'AMS', 'dep_dt': '2017-05-01 15:00:00', 'arr_dt': '2017-05-01 16:30:00', 'price': 60},
        {'dep': 'CPH', 'arr': 'MAD', 'dep_dt': '2017-05-01 14:15:00', 'arr_dt': '2017-05-01 17:10:00', 'price': 70},
        {'dep': 'MAD', 'arr': 'AMS', 'dep_dt': '2017-05-01 19:00:00', 'arr_dt': '2017-05-01 21:40:00', 'price': 20},
       ...
    ]

With thousands of records between many cities on many dates.

Example

Let's say I want all possibilities to go from FRA to AMS. To find all possible trips, I've split the problem in 3:

Direct flights
Flights with 1 stopover
Flights with 2 stopovers

(I omit the date constraints below for simplicity)

In order to get the direct flights, I can do:

direct_flights = [f for f in flights if f['dep']=='FRA' and f['arr']=='AMS']

To get trips with one stopover, I've gone for a nested loop. Not optimal, but it works:

first_segment = [f for f in flights if f['dep']=='FRA']
second_segment = [f for f in flights if f['arr']=='AMS']

flights_w_1stop = []
for s1 in first_segment:
    for s2 in second_segment:
        if (s1['arr'] == s2['dep'] and
            s1['arr_dt'] < s2['dep_dt']):
            flights_w_1stop.append((s1, s2))

Now, to get all trips with two stopovers, I only could come up with a similar approach than above, but this makes two nested loops, which looks horrible and will be very slow when I have many flights.

first_segment = [f for f in flights if f['dep']=='FRA']
second_segment = [f for f in flights]
third_segment = [f for f in flights if f['arr']=='AMS']

flights_w_2stops = []
for s1 in first_segment:
    for s2 in second_segment:
        for s3 in third_segment:
            if (s1['arr'] == s2['dep'] and
                s2['arr'] == s3['dep'] and
                s1['arr_dt'] < s2['dep_dt'] and
                s2['arr_dt'] < s3['dep_dt']):
                flights_w_2stops.append((s1, s2, s3))

Finally, I would merge all 3 different lists with the same data structure and sort them either by price or by total duration.

Question

How could this be done more efficiently?

Note: I have read about graphs, but I don't think I will ever want to consider more than two stopovers. In addition, it would mean learning a new methodology. It is always good to learn, so if it is the best option I would invest the time. But if a similar thing can be done in another way (rearranging the nested loops somehow?), I would prefer so.

I found a similar question in SO and CodeReview, but I do not see how to apply that to my case.


texts:
This is my incoming data:

id      text    parent
--      ----    ------
0       0       NULL
1       1       0
11      11      1
2       2       0
21      21      2
211     211     21
22      22      2
3       3       0

Which I receive in a List<TreeNode>, where TreeNode is:

public class TreeNode
{
    public int id { get; set; }
    public string text { get; set; }
    public int? parent { get; set; }
}

To represent my tree,

0
|--1
|  |--11
|
|--2
|  |--21
|  |  |--211
|  |
|  |--22
|
|--3

I have this class:

public class Tree
{
    public int? id { get; set; }
    public string text { get; set; }
    public List<Tree> children { get; set; }
}

I build the tree from the flat list of nodes with this:

Tree tree = treeNodes.BuildTree();

This is my implementation of BuildTree:

public static Tree BuildTree(this List<TreeNode> nodes)
{
    // Create a NULL-root tree
    Tree root = new Tree();

    // Add nodes (sub-trees) to the tree
    foreach (TreeNode node in nodes)
    {
        // traverse tree, find the parent, add child
        root.TraverseAndAddNode(node);
    }

    // Return the NULL-root
    // If the tree has an actual root, it will be the first child of this root
    return root;
}

public static bool TraverseAndAddNode(this Tree root, TreeNode node)
{
    bool nodeAdded = false;

    // Check if the current root is the parent of the node to be added
    if (root.id == node.parent)
    {
        if (root.children == null)
            root.children = new List<Tree>();

        root.children.Add(new Tree()
        {
            id = node.id,
            text = node.text,
            children = null
        });

        nodeAdded = true;
    }
    // Check if the current root has children and recurse the search
    else if (root.children != null)
    {
        foreach (Tree tree in root.children)
        {
            nodeAdded = tree.TraverseAndAddNode(node);

            if (nodeAdded)
                break;
        }
    }
    // Node's parent is not in the current tree
    else
    {
        nodeAdded = false;
    }

    return nodeAdded;
}

I'm able to see a few problems here, such as the traversal code here is not efficient as it uses linear search, and the implementation would be better off done with a Dictionary, and also that I'm unnecessarily creating a NULL root for the lack of being able to dynamically identify the true root from the flat data.

How can I do better? Is there a way I can dynamically construct partial sub-trees as I parse the flat-data, and keep linking these partially constructed trees to the main tree and keep building on?

EDIT:
* Removed nullable id from TreeNode -- changed int? id to int id
* Updated incorrect original example showing 1.1 instead of 11

Added the following examples:

Case 1:
Multiple NULL parents (NULL root is acceptable):

id    text           parent            NULL
--    ----           ------            |
1     C:             NULL              |--C:
2     drivers        1                 |  |--drivers
3     oem            2        --->     |     |--oem
4     D:             NULL              |
5     data           2                 |--D:
6     E:             NULL              |  |--data
                                       |
                                       |--E:

Case 2:
Single NULL parent (NULL root is avoidable):

id    text           parent            NULL
--    ----           ------            |
0     My PC          NULL              |--My PC
1     C:             0                    |
2     drivers        1                    |--C:
3     oem            2        --->        |  |--drivers
4     D:             0                    |     |--oem
5     data           2                    |
6     E:             0                    |--D:
                                          |  |--data
                                          |
                                          |--E:


texts: Sum of squares of two largest of three numbers

texts:
Given the following problem (SICP Exercise 1.3):

  Define a procedure that takes three
  numbers as arguments and returns the
  sum of squares of the two largest
  numbers.

I wrote the following (somewhat clumsy) solution in Scheme.  How can I make it better?

(define (greatest-two a b c)
  (cond ((> a b) (cond ((> b c) (list a b))
                       (else (list a c))))
        ((> a c) (cond ((> c b) (list a c))
                       (else (list a b))))
        (else (list b c))))

(define (square x) (* x x))
(define (sum-of-squares a b) (+ (square a) (square b)))
(define (f a b c)
  (apply sum-of-squares (greatest-two a b c)))


texts: Invert the bits of a non-negative integer in Common Lisp (SBCL)

texts:
I'm doing code challenges to learn Common Lisp. I'm trying to invert all the bits in any given positive integer.

My current solution does it the math way, by recursing on a number, dividing it by two, and inverting the remainder before multiplying and adding back up:

(defun invert-bits (n)
    (if (> n 0)
        (+ (* (invert-bits (truncate (/ n 2))) 2)
           (if (= (rem n 2) 1) 0 1))
        0))

Is there a simpler way to do this using built-in functions?


texts:
I have a Notifications component in my React app, which continuously polls the server for new notifications. The app uses Redux for storing notifications. When a new notification is received, it is shown on screen. Here's what the code looks like:

import * as React from 'react'
import { connect } from 'react-redux'

interface ReduxProps {
    notifications: INotification[]
    fetchNotifications: () => any
}

interface IProps extends ReduxProps {}

class NotificationsWidget extends React.Component<IProps> {
    timer: number

    refreshNotifications = () => {
        this.props.fetchNotifications()
    }

    componentDidMount() {
        this.timer = window.setInterval(() => this.refreshNotifications(), 3000)
    }

    render() {
        // ... Show `this.props.notifications`.
    }
}

const mapStateToProps = (state: any) => ({
    notifications: state.notifications.items
})

export default connect(mapStateToProps, {fetchNotifications})(NotificationsWidget)

Is this the correct way to go about polling with a React/Redux app? Is there a better way to do polling, or is there an alternative way to push notifications to the browser without polling?


texts: Removing unneeded objects from an array before rendering

texts:
I'm making a function that adds a new object to an array and removes the oldest if it has reached a specific length. Click the button in the example below. All objects are drawn until its array reaches a length of 5 when the oldest is removed.
Is there a better, more processor and memory efficient way to do this instead of needlessly throwing the oldest entry at the garbage collector?

let c = document.getElementById("context");
let ctx = c.getContext("2d");
let particles = [];

function addParticle() {
  particles.push({
    x: Math.random() * 800,
    y: Math.random() * 500,
    width: Math.random() * 100 + 50,
    height: Math.random() * 100 + 50,
    color: '#' + Math.floor(Math.random() * 16777215).toString(16)
  });

  if (particles.length > 5) particles.shift();

  draw();
}

function draw() {
  ctx.clearRect(0, 0, 800, 500);

  for (let i = 0; i < particles.length; i++) {
    ctx.fillStyle = particles[i].color;
    ctx.fillRect(particles[i].x, particles[i].y, particles[i].width, particles[i].height);
  }
}
<button onclick="addParticle();">Add</button>
<canvas id="context" width="800" height="500"></canvas>


texts: Two Java methods for easy writing and reading bytes in files

texts:
I have these easy-to-use methods for reading and writing binary data in files.

FileTools.java:

package net.coderodde.file;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;

/**
 * This class contains utility methods for writing and reading binary data in
 * files.
 *
 * @author Rodion "rodde" Efremov
 * @version 1.6 (Feb 29, 2016)
 */
public class FileTools {

    /**
     * This method returns the byte array that represent the contents of
     * {@code file}.
     *
     * @param  file the file to read.
     * @return the array of bytes representing the contents of the input file.
     */
    public static byte[] readFile(File file)
    throws IOException, FileNotFoundException {
        Objects.requireNonNull(file, "The input file is null.");
        long size = file.length();
        checkSize(size);

        byte[] data;
        int bytesRead;

        try (FileInputStream stream = new FileInputStream(file)) {
            data = new byte[(int) size];
            bytesRead = stream.read(data);
        }

        if (bytesRead != size) {
            throw new IllegalStateException(
                    "File size and read count mismatch. File size: " +
                    size + ", bytes read: " + bytesRead);
        }

        return data;
    }

    /**
     * Writes the byte array {@code data} to the file {@code file}. After
     * successful operation of this method, the input file will contain exactly
     * the contents of the input data.
     *
     * @param file the file to write to.
     * @param data the data array to write.
     * @throws java.io.IOException           if file IO fails.
     * @throws java.io.FileNotFoundException if file does not exist.
     */
    public static void writeFile(File file, byte[] data)
    throws IOException, FileNotFoundException {
        Objects.requireNonNull(file, "The input file is null.");
        Objects.requireNonNull(data, "The input data to write is null.");

        try (BufferedOutputStream stream = new BufferedOutputStream(
                                           new FileOutputStream(file))) {
            stream.write(data);
        }
    }

    // This method ensures that file size is small enough to be represented
    // using a variable of type 'int'.
    private static final void checkSize(long size) {
        if (size > Integer.MAX_VALUE) {
            throw new IllegalStateException(
                    "The target file is too large: " + size + " bytes. " +
                    "Maximum allowed size is " + Integer.MAX_VALUE +
                    "bytes.");
        }
    }
}

FileToolsTest.java:

package net.coderodde.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import org.junit.Test;
import static org.junit.Assert.*;

public class FileToolsTest {

    private static final int ITERATIONS = 100;
    private static final int MAXIMUM_DATA_LENGTH = 20_000;

    @Test
    public void test() {
        long seed = System.nanoTime();
        Random random = new Random(seed);
        System.out.println("Seed = " + seed);

        for (int i = 0; i < ITERATIONS; ++i) {

            File file = new File("funky.txt");
            byte[] data = new byte[random.nextInt(MAXIMUM_DATA_LENGTH)];
            random.nextBytes(data);

            try {
                FileTools.writeFile(file, data);
                byte[] result = FileTools.readFile(file);
                assertTrue(Arrays.equals(result, data));
            } catch (FileNotFoundException ex) {
                fail(ex.getMessage());
            } catch (IOException ex) {
                fail(ex.getMessage());
            }

            file.delete();
        }
    }
}

Please, tell me anything that comes to mind.


texts:
I am interested in removing leading/trailing whitespace in a .csv file, and I was wondering if there's a better way to execute this:

with open("csv_file.csv", "rb") as infile:
    r = csv.DictReader(infile)
    fieldnames = r.fieldnames #Creates list of fieldnames

    for row in r:
        for f in fieldnames:
            row[f] = row[f].strip()

I am fine with using this method, but I was wondering if there's a way to circumvent using a nested for loop.


texts: Loading YouTube videos inside a bootstrap modal

texts:
The following code loads a YouTube video inside an iframe, where the tricky part is to sync the onYouTubeIframeAPIReady and on('shown.bs.modal') functions. The code works well in Firefox and decently in Chrome, but in Safari it is quite buggy. Any hints towards improvement would be appreciated.

jQuery( document ).ready(function() {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

jQuery("#video_modal").on('shown.bs.modal', function() {
    if(typeof player.playVideo == 'function') {
        player.playVideo();
    } else {
        var fn = function(){
            player.playVideo();
        };
        setTimeout(fn, 200);
    }
});
jQuery("#video_modal").on('hidden.bs.modal', function() {
    player.stopVideo();
});
});

var player;

function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
    videoId: 'zg8KE6bEP50',
    origin: 'http://rollnstroll.se',
    width: '640',
    height: '390'
});
}


texts:
This is my first time making a function just from reading about it on Wikipedia, so I'm sure there's room for a lot of improvement.

function fisherYates(str) {
  let result = '';
  for (let i = 0, len = str.length; i < len; i++) {
    let rand = (Math.random()*str.length)|0;
    result += str[rand];
    str = str.slice(0, rand) + str.slice(rand+1);
  }
  return result;
}


texts:
I knocked this up quickly to allow delayed Invoke on the dispatcher. Along with canceling the existing invocation and replacing with a new one, this allows throttling an action.

public static class DispatcherExtensions
{
    private static Dictionary<string, DispatcherTimer> timers = new Dictionary<string, DispatcherTimer>();
    private static object syncRoot = new object();

    public static void DelayInvoke(this Dispatcher dispatcher, string namedInvocation,
        Action action, TimeSpan delay, DispatcherPriority priority = DispatcherPriority.Normal)
    {
        lock (syncRoot)
        {
            DispatcherTimer timer = null;
            if (timers.TryGetValue(namedInvocation, out timer))
            {
                timer.Stop();
                timer.IsEnabled = false;
                timers.Remove(namedInvocation);
            }
            timer = new DispatcherTimer(priority,dispatcher)
            {
                Interval = delay
            };
            timer.Tick += (s, e)
                =>
                {
                    action();
                };
            timers.Add(namedInvocation, timer);
            timer.IsEnabled = true;

        }
    }
}


texts: Vectorization for temperature simulation

texts:
I'm new to Matlab and I would like to know tips in order to reduce the processing time of such program. The scheme of the actual code is very similar to typical C++ code and I would like to vectorize the for and if loops in order to decrease the processing time. The code is added just to make you know the overall idea of what I would like to obtain.

close all             %Tanca totes les finestres obertes
clear all             %Removes variables from memory
clc                   %Clears command window
tic                   %inicia comptador temps simulació

trans=fopen('trans_matlab.txt','wt')

%Physical properties and geometrical data input
Tair=25;
Tinitial=100;
h=10;
k=5;
qv=500;
thick1=0.05;
thick2=0.1;
density=1000;
Cp=800;

%Parameters imput
max_error=0.000001;   %Error màxim a partir del cual no hi ha canvis en els resultats
max_iter=1000000;
r=0.9;
N=10;                 %Nodes paret1
M=10;                 %Nodes paret2
Inc_x1=thick1/N;
Inc_x2=thick2/M;
num_t=23000;          %temps de realització
Inc_t=20;

%Initial map temperatures
t=1;
for i=1:(N+M+1)       %Hipòtesi inicial: tots els nodes tenen T i Ta = Tair
    T(i,t)=Tinitial;      %(N+M+1) pq no deixa començar a 0
    Ta(i,t)=Tinitial;
end

%GAUS SEIDEL
while t<num_t
    %New instant
    t0=t;
    t=t+1;
    for i=1:(N+M+1)
        Ta(i,t)=T(i,t0);
    end
    %Temperature calculation
    for j=0:max_iter
        for i=1:(N+M+1)
            if i==1
            b=0;
            c=k/Inc_x1;
            d=qv*Inc_x1/2;
            e=density*Cp*Inc_x1/(2*Inc_t);
            a=b+c+e;
            T(i,t)=(c.*Ta(i+1,t)+d+e.*T(i,t0))/a;
            end
            if (i>1)&(i<(N+1))
            b=k/Inc_x1;
            c=k/Inc_x1;
            d=qv*Inc_x1;
            e=density*Cp*Inc_x1/(Inc_t);
            a=b+c+e;
            T(i,t)=(b.*Ta(i-1,t)+c.*Ta(i+1,t)+d+e.*T(i,t0))/a;
            end
            if i==(N+1)
            b=k/Inc_x1;
            c=k/Inc_x2;
            d=qv*Inc_x1/2;
            e=density*Cp*Inc_x1/(2*Inc_t)+density*Cp*Inc_x2/(2*Inc_t);
            a=b+c+e;
            T(i,t)=(b.*Ta(i-1,t)+c.*Ta(i+1,t)+d+e.*T(i,t0))/a;
            end
            if (i>(N+1))&(i<(N+M+1))
            b=k/Inc_x2;
            c=k/Inc_x2;
            e=density*Cp*Inc_x2/(Inc_t);
            a=b+c+e;
            T(i,t)=(b.*Ta(i-1,t)+c.*Ta(i+1,t)+e.*T(i,t0))/a;
            end
            if i==(N+M+1)
            b=k/Inc_x2;
            c=h;
            e=density*Cp*Inc_x2/(Inc_t*2);
            a=b+c+e;
            T(i,t)=(b.*Ta(i-1,t)+h*Tair+e.*T(i,t0))/a;
            end
            %Error calculation-trobar l'error maxim de la matriu error(i,t)
            errort=0;
            error(i,t)=abs(T(i,t)-Ta(i,t));
            if error(i,t)>errort
            errort=error(i,t);
            else
            errort=errort;
            end
        end
        if errort>max_error
            for i=1:(N+M+1)
                Ta(i,t)=T(i,t).*r+Ta(i,t).*(1-r);
            end
        else
            break
        end
    end
    iter(t)=j;
end

toc
%Print Results
t=1;
fprintf(trans,'time\t T1\t TN+1\t TN+M+1\t iter\n');

while(t<=num_t)
time=t*Inc_t;
fprintf(trans,'%.2f\t %.2f\t %.2f\t %.2f\t %.2f\n',time,T(1,t),T(N+1,t),T(N+N+1,t),iter(t));
t=t+1;
end
fclose(trans);


texts: Avoiding multiple if conditions in a Car class

texts:
In this dummy structure, we have a race car manager class. this class contains a method named execute().  There is a List which contains all participating racing cars.

This is the Car class, the List's type is based off:

public Car {

    private boolean driverBeltOn;
    private boolean engineOn;

    public boolean engineReady() {
        return this.engineOn;
    }

    public boolean driverBeltOn() {
        return this.driverBeltOn;
    }

    public void warn(String message) {
        // Send warning to car
        // ....
        // .....  (<;D>-<;
        // ......
    }
}

The job of the execute method, is to loop through all cars, and make sure they are ready for the race, but if at-least one of the two conditions is false, then the car will not  participate in the race.

If one condition is false, then the car should be notified, about why it can't participate.

Basic solution:

public void execute() {
    for (Car car : carList) {
        if (car.engineOn()) {
            if (car.driverBeltOn()) {
                // Can go..
            }
            else {
                car.warn("Please wear a damn belt!");
            }
        }
        else {
            car.warn("How are you planning to participate in a race with an off engine??");
        }
    }
}

What is the best way to handle things like this, where you have to check multiple conditions, and each condition should do something if it's false in the check?

Example of a bad solution, because it's impossible to let the car know what's wrong:

public void execute() {
    for (Car car : carList) {
        if (car.engineOn() && car.driverBeltOn()) {
            // Can go..
        }
        else {
            car.warn("Something went wrong...");
        }
    }
}

Unless you add multiple checks inside the else block, which could be double work/bad practice.

What is the best practice do handle this? Or if inside if for this case is totally fine?


texts:
I've been working with the SerialPort class for a while, trying to figure out the best way to work with it, and especially adding support for Async-Await in C#. I also started before 4.5, so I didn't have access to the BaseStream member (so no BaseStream.ReadAsync()). I wanted to get some opinions on it before I started relying on it too heavily. Performance considerations and stability are high priority, but also figuring out if I'm reinventing the wheel.

The code is designed to wrap around a SerialPort to provide Async-Await operations. It does this by intercepting calls to Write and the SerialPort.DataRecieved events and relaying the data to a pair of Reactive Subjects that client code can subscribe to. Note this code requires the nuget package Rx-Main.

using System.IO.Ports;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
namespace Core.IO {
    public sealed class ComPort : IDisposable, INotifyPropertyChanged{
        private Subject<byte[]> _txDataSubject;
        private Subject<byte[]> _rxDataSubject;
        private SerialPort _port;

        public String PortName {
            get {
                return _port.PortName;
            }
            set {
                _port.PortName = value;
                RaisePropertyChanged("PortName");
                RaisePropertyChanged("IsOpen");
            }
        }
        public Boolean IsOpen {
            get {
                return _port.IsOpen;
            }
        }
        public IObservable<byte[]> DataTx {
            get {
                return _txDataSubject.AsObservable();
            }
        }
        public IObservable<byte[]> DataRx {
            get {
                return _rxDataSubject.AsObservable();
            }
        }
        public ComPort() {
            _port = new SerialPort();
            _txDataSubject = new Subject<byte[]>();
            _rxDataSubject = new Subject<byte[]>();
            _port.DataReceived+=(sender,args)=>{
#if DEBUG
                Console.WriteLine("({0}) RX on thread {1}",PortName, System.Threading.Thread.CurrentThread.ManagedThreadId);
#endif
                _rxDataSubject.OnNext(_port.Encoding.GetBytes(_port.ReadExisting()));
            };
        }
        public void Open() {
            _port.Open();
            RaisePropertyChanged("IsOpen");
        }
        public void Close() {
            _port.Close();
            RaisePropertyChanged("IsOpen");
        }
        private void RaisePropertyChanged(string propertyName) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public Boolean IsDisposed {
            get;
            private set;
        }
        public void Write(string data) {
            _txDataSubject.OnNext(_port.Encoding.GetBytes(data));
            _port.Write(data);
        }
        private void Dispose(bool isDisposing) {
            if (IsDisposed) return;
            if (isDisposing) {
                _port.Dispose();
                _txDataSubject.Dispose();
            }
            IsDisposed = true;
        }
        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        ~ComPort() {
            Dispose(false);
        }
    }
}


texts: Project Euler 1 - Multiples of 3 and 5 by Loki

texts:
Multiples of 3 and 5

  Multiples of 3 and 5
  Problem 1

  If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

  Find the sum of all the multiples of 3 or 5 below 1000.

Used the command line (bash):

(seq 0 3 999; seq 0 5 999) | sort | uniq | xargs | tr ' ' '+' | bc

For the range it seemed good enough:

> time (seq 0 3 999; seq 0 5 999) | sort | uniq | xargs | tr ' ' '+' | bc
233168

real    0m0.021s
user    0m0.019s
sys     0m0.019s


texts: In-place reversal of a singly linked list

texts:
As suggested in The 2nd Monitor, I'd like a review of this in-place reversal of a singly linked-list:

#include <iostream>

struct node {
    int data;
    node *next;
};

node *reverse(node *list) {
    node *prev = NULL;
    node *next;

    while (list) {
        next = list->next;
        list->next = prev;
        prev = list;
        list = next;
    }
    return prev;
}

void show_list(node *list) {
    while (list != NULL) {
        std::cout << list->data << ", ";
        list = list->next;
    }
}

int main() {
    node *list = NULL;

    for (int i=0; i<10; i++) {
        node *n = new node;
        n->next = list;
        n->data = i;
        list = n;
    }

    std::cout << "As built: ";
    show_list(list);

    list = reverse(list);

    std::cout << "Reversed: ";
    show_list(list);
    return 0;
}


texts: Simple use of mouse clicks to position a ball

texts:
Here's a simple program I wrote to teach myself the basics of using a mouse for a potential HTML5/JavaScript game. The thing that tripped me up was event.clientX and event.clientY don't return the correct values as they stand. One has to get the offsets of the canvas object, which in turn may be a child object of other objects whose offsets need to be obtained...

I found this a bit counter intuitive. If anyone has more elegant solutions, I'd be interested to learn about them.

var canvas, context, ball_pos;

var BALL_RADIUS = 15;

function start() {
    'use strict';
    canvas = document.getElementById('c');
    context = canvas.getContext('2d');
    ball_pos = [canvas.width / 2, canvas.height / 2];
}

function draw() {
    'use strict';

    // draw background
    context.fillStyle = "rgb(248,248,248)"; // off white
    context.fillRect(0, 0, canvas.width, canvas.height);

    // draw ball
    context.strokeStyle = 'black';
    context.lineWidth = 1;
    context.fillStyle = 'red';
    context.beginPath();
    context.arc(ball_pos[0], ball_pos[1], BALL_RADIUS, 0, Math.PI * 2, true);
    context.closePath();
    context.fill();
    context.stroke();
}

function animate() {
    'use strict';
    window.requestAnimationFrame(animate);
    draw();
}

function click(evt) {
    'use strict';
    var offset = [0, 0],
        obj = canvas;
    while (obj.offsetParent !== null) {
        offset[0] += obj.offsetLeft;
        offset[1] += obj.offsetTop;
        obj = obj.offsetParent;
    }
    ball_pos[0] = evt.clientX - offset[0];
    ball_pos[1] = evt.clientY - offset[1];
}

// register event handlers
window.onclick = click;

start();
animate();
<html>
<head>
  <meta charset="utf-8">
</head>
<body>
  <canvas id="c" width="600" height="400"></canvas>
</body>
</html>


texts: Get a number from a class of a HTML element

texts:
I've got an element with multiple classes in a page and I need to dynamically get the number at the end of a specific class name.

Example:

    ...

In particular I've to get "3" from gallery-columns-3.

I've made this function in Javascript/Jquery, but I think that there is a better solution.

//return the number of columns from gallery class
function get_gallery_columns($el){
    var class_arr = $el.attr('class').split(/\s+/).filter(function (el) {
        return el.includes("gallery-columns-");
    });

    if(class_arr.length < 1){
        return false;
    }

    return parseInt(class_arr[0].replace(/\D/g,''));
}

I wanted to do this to dynamically create the layout of Wordpress galleries.

$(document).ready(function() {

//galleries selector
var $galleries = $('div[class*=gallery-columns-]');
fix_gallery($galleries);

//responsive gallery on resize
$(window).on('resize', function(){
    fix_gallery($galleries);
});

//fixes galleries size
function fix_gallery($galleries){
    $galleries.each(function(index) {
        var $current_gall = $(this);
        var n_cols = get_gallery_columns($current_gall);

        if(n_cols === false){
            return;
        }

        //if there are columns, unwrap images from them
        $current_gall.find(".gallery-group").replaceWith(function() { return $(this).contents(); });

        //change columns on mobile/tablet
        if (window.matchMedia("(max-width: 640px)").matches && n_cols > 2) {
            n_cols = 2;
        } else if (window.matchMedia("(max-width: 800px)").matches && n_cols > 4) {
            n_cols = 4;
        } else if (window.matchMedia("(max-width: 980px)").matches && n_cols > 6) {
            n_cols = 6;
        }

        //gallery pictures selector
        var $pictures = $current_gall.find('.gallery-item');
        //number of pictures
        var pic_number = $pictures.length;

        for (var i = 0;i <= pic_number; i += n_cols){
            $pictures.filter(':eq('+i+'),:lt('+(i+n_cols)+'):gt('+i+')').wrapAll('
');
        }

        $pictures.width((100/n_cols) + '%');

    });
}

//return the number of columns from gallery class
function get_gallery_columns($el){
    var class_arr = $el.attr('class').split(/\s+/).filter(function (el) {
        return el.includes("gallery-columns-");
    });

    if(class_arr.length < 1){
        return false;
    }

    return parseInt(class_arr[0].replace(/\D/g,''));
}

});


texts:
I am calling a service outside of my control. My application must include a time out, so that if the call to the service takes too long, an appropriate time-out message is returned.

// Client connected to remote service
RemoteClient Client = new RemoteClient();

private async Task<MyResult> CheckItAsync(CheckRequest rqst, int timeout)
{
    // MyResult object gets some values populated on construction
    MyResult Result = new MyResult()

    // RemoteResponse auto generated from 'Add Service Reference'
    RemoteResponse response;

    try
    {
        // Perform the check and return the appropriate response.
        var task = Client.PerformCheckAsync(rqst);
        var res = await Task.WhenAny(task, Task.Delay(timeout));
        if (res == task)
        {
            // Task completed within time.
            response = task.GetAwaiter().GetResult();
            // Populate my result object with the necessary values
            Result.populate(response);
        }
        else
        {
            // Task timed out, populate result with timeout message
            Result.timedOut(rqst.RequestNumber);
        }

    }
    catch (Exception e)
    {
        // Something else happened, log exception and return failed result
        Elmah.ErrorSignal.FromCurrentContext().Raise(e);
        Result.failed(rqst.RequestNumber);
    }

    return Result;
}

I read that Task.WhenAny.. will still continue calling the service in the background even after the time out, creating a leak, but I am not sure how to stop the call as the remote service does not take a CancellationToken.

The code works fine and returns the correct responses when it times out and when it doesn't.

*Additional follow-up question, does this code call the remote service multiple times? (at both var task = ... and Task.WhenAny...)


texts: Beginner jQuery – Coding Fundamentals / Performance

texts:
Empty Textboxes, class="loc" display "(City, State \ Country)" using italics and silver.  Of course, Textboxes that do have data are displayed according to page defaults.

I noticed chaining and caching could be used.  There are inheritances issues, so I am not bothering with using .addClass instead of targeting with .css.

    $(function () {
        var loc = $('.loc');

        function desc() {
            var tar = $(this);
            if (tar.val().length == 0) {
                tar.val("(City, State \\ Country)").css({
                    "color": "silver",
                    "fontStyle": "italic"
                });
            };
        }

        function data() {
            var tar = $(this);
            if (tar.val() == "(City, State \\ Country)") {
                tar.val("").css({
                    "color": "black",
                    "fontStyle": "normal"
                });
            };
        }

        loc.each(desc).blur(desc).click(data);
    });

How far off base am I on understanding proper formatting in jQuery?


texts: Security flaws in smartphone application

texts:
And could you give me any security and optimization recommendations?

<?php

$datvariable = $_POST['comingover'];

$datvariable = htmlspecialchars($datvariable);

$datvariable = cleanPost($datvariable);

function cleanPost($item)
{ return mysql_real_escape_string(strip_tags(stripslashes($item))) }

$mysqliobject = new mysqli( 'localhost', 'myusername', 'mypassword', 'mydatabase');

if( mysqli_connect_errno( ) )   {   echo "Central SQL Access Malfunction";  exit(); }

$uploadquery = $mysqliobject->prepare(
"INSERT INTO ENTIRETABLE ( WORD, SOUNDCLICKCOUNT ) VALUES (?,'0')
      ON DUPLICATE KEY UPDATE SOUNDCLICKCOUNT = SOUNDCLICKCOUNT+1"
                                        );

$uploadquery->bind_param('s', $datvariable);

$uploadquery->execute();

$mysqliobject->close();

?>

I do not see any fatal error for now.

I plan to have a database that has about 10 tables, and each table will contain about 500 records.  Each column will have 50 characters at max.  Is MySQL good enough for that purpose?  It is for a smartphone application that helps students in East Asia learn the English language.


texts: LSTM Model - Validation Accuracy is not changing

texts:
I am working on classification problem, My input data is labels and output expected data is labels
Labels  Count
1      94481
0      65181
2      60448

I have made X, Y pairs by shifting the X and Y is changed to the categorical value
create X/y pairs
df1 = df['Data_Used']
df1 = concat([df1, df1.shift(1)], axis=1)
df1.dropna(inplace=True)

    X   Y
    2   1.0
    1   2.0
    1   1.0
    2   1.0
    2   2.0

values = df1.values

encoder = LabelEncoder()
test_labels = to_categorical(encoder.fit_transform(values[:,1]),num_classes=3)
train_X,test_X,train_y,test_y= train_test_split(values[:,0], test_labels,test_size = 0.30,random_state = 42)

print(train_X.shape)
print(train_y.shape)
print(test_X.shape)
print(test_y.shape)

(154076,)
(154076, 3)
(66033,)
(66033, 3)
Converting this to LSTM format
train_X = train_X.reshape(train_X.shape[0],1,1)
test_X = test_X.reshape(test_X.shape[0],1,1)

# configure network
n_batch = 1
n_epoch = 10
n_neurons = 100

ModelArchitecture
tf.keras.backend.clear_session()

model = tf.keras.models.Sequential([
    tf.keras.layers.LSTM(n_neurons, batch_input_shape=(n_batch, train_X.shape[1],train_X.shape[2]), stateful=True),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(100, activation = 'relu',kernel_regularizer=regularizers.l2(0.0001)),
    tf.keras.layers.Dense(3, activation='softmax')
])

model.summary()
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['acc'])
history = model.fit(train_X,train_y,validation_data=(test_X, test_y),epochs=n_epoch, batch_size=n_batch, verbose=1,shuffle= False)

Validation Accuracy is not Changing
Epoch 1/5
154076/154076 [==============================] - 356s 2ms/step - loss: 1.0844 - acc: 0.4269 - val_loss: 1.0814 - val_acc: 0.4310
Epoch 2/5
154076/154076 [==============================] - 354s 2ms/step - loss: 1.0853 - acc: 0.4256 - val_loss: 1.0813 - val_acc: 0.4310
Epoch 3/5
154076/154076 [==============================] - 355s 2ms/step - loss: 1.0861 - acc: 0.4246 - val_loss: 1.0814 - val_acc: 0.4310
Epoch 4/5
154076/154076 [==============================] - 356s 2ms/step - loss: 1.0874 - acc: 0.4228 - val_loss: 1.0825 - val_acc: 0.4310
Epoch 5/5
154076/154076 [==============================] - 353s 2ms/step - loss: 1.0887 - acc: 0.4208 - val_loss: 1.0828 - val_acc: 0.4310

What can be the changes to improve the model.


texts: Folding over a Rose Tree : Tree a -> [a]

texts:
I posted this question on StackOverflow.

Given this algebraic data type:

data Tree a = Node {
    rootLabel :: a,
    subForest :: [Tree a]
}  deriving (Show)

and function:

treeFold :: (a -> [b] -> b) -> Tree a -> b
treeFold f (Node x ts) = f x (map (treeFold' f) ts)

Write a function (using treeFold) with the signature: Tree a -> [a].

Given this example:

ghci> stringTree
Node {rootLabel = "foo",
      subForest = [Node {rootLabel = "bar",   subForest = []},
                   Node {rootLabel = "bippy", subForest = []},
                   Node {rootLabel = "baz", subForest = []} ]}

I came up with:

ghci> treeFold' (\x y -> x : (join y) ) stringTree
["foo","bar","bippy","baz"]

Please review this implementation.

I'm not sure if introducing join here (from Control.Monad) is overkill. Note that I don't have a good sense as to whether it's OK to casually use Monads - mainly using the right abstraction - not going overboard.


texts:
I wanted to see where I lined up with other users on tag badges (I wanted to count just bronze but figured I would start with all of them to start out with).

Here is the query that I ended up with:

SELECT
    Users.Id as [User Link]
    , Users.DisplayName
    , COUNT (Badges.Id) AS Number_of_Tag_Badges
FROM
    Badges
    INNER JOIN Tags ON Tags.TagName = Badges.Name
    INNER JOIN Users ON Users.Id = Badges.UserId
GROUP BY Users.DisplayName, Users.Id
ORDER BY Number_of_Tag_Badges DESC

What can be done to make this a better query?


texts: Avoid printing attributes of the nil object in Rails

texts:
Currently, when I'm wanting to print a value for my class I do the following...

<%= @person.team.name if @person.team.present? %>

This seems really redundant to me.  I've also have done...

<%= @person.team.name_display %>

where I've created a function for each attribute to kind of hide the first case.  It seems a little much though. Is there a more preferred way to do it such as...

<%= @person.team.name || "" %>


texts:
My program takes as first input the number of test cases, n. For the following n inputs, I output the smallest positive integer that has to be added to the input to make the input a numeric palindrome. If the input is already a palindrome the output shall be zero.

I have a problem with the performance, the algorithm is correct but I get get time limit exceeded. The input number is in the range \$[1, 1000000]\$.

#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;

extern bool isPal(const int &number) __attribute__((fastcall));

bool isPal(const int &number)
{
    char buffer[6];
    sprintf(buffer,"%i",number);
    const string x = buffer;
    for(int i=0, end = x.size()-1;i<end;i++,end--)
        if(x[i]!=x[end])
        return false;
    return true;
}

int main()
{
    int n, number, counter, t;
    scanf("%i",&n);
    while(n--)
    {
        counter = 0;
        scanf("%i",&number);
        t = number+counter;
        while(!isPal(t))
        {
            ++counter;
            t = number+counter;
        }
        printf("%i\n",counter);
    }
    return 0;
}

Please help me optimise the above code.


texts: Is this a good way to process SQLAlchemy's uniqueness constraint violation in Flask?

texts:
I'm using Flask along with Flask-SQLAlchemy and Flask-WTF.

I have a model:

class Trip(db.Model):
    id = db.Column(db.BigInteger, primary_key=True)
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'), index=True,
                          nullable=False)
    name = db.Column(db.String(2000), nullable=False)
    country_code = db.Column(db.String(2))
    slug = db.Column(db.String(2000), nullable=False)

    author = db.relationship('User',
                             backref=db.backref('trips',
                                                order_by=lambda: Trip.name))

    def __repr__(self):
        return f"<Trip {self.id} author_id={self.author_id} slug={self.slug}>"

db.Index('idx_trip_author_slug', Trip.author_id, Trip.slug, unique=True)

To create and update trips, I use class-based views like this:

class TripCUView(MethodView):
    methods = ('GET', 'POST')
    decorators = [user_required]
    title = 'Trip action'
    submit_text = 'Save'

    def dispatch_request(self, *args, **kwargs):
        self.model = self._instant_model()
        return super().dispatch_request(*args, **kwargs)

    def get(self):
        self.form = self._build_form()
        return self._default_render()

    def post(self):
        self.form = TripForm()
        if self.form.validate():
            try:
                self.form.populate_obj(self.model)
                db.session.add(self.model)
                db.session.commit()
                return redirect(url_for('.index'))
            except IntegrityError as e:
                db.session.rollback()
                if (e.orig.pgcode == pgerrorcodes.UNIQUE_VIOLATION):
                    self.form.errors.setdefault('slug', [])
                    self.form.errors['slug'].append('Already exists')
                    return self._default_render()
                else:
                    raise e
        return self._default_render()

    def _build_form(self):
        raise NotImplementedError

    def _instant_model(self) -> Trip:
        raise NotImplementedError

    def _default_render(self):
        return render_template('form.html', form=self.form,
                               title=self.title,
                               submit_text=self.submit_text)

class CreateTripView(TripCUView):
    title = 'Create trip'

    def _build_form(self):
        return TripForm()

    def _instant_model(self):
        return Trip(author=g.user)

class UpdateTripView(TripCUView):
    @property
    def title(self):
        return f"Updating {self.model.name}"

    def dispatch_request(self, slug):
        self.slug = slug
        return super().dispatch_request()

    def _build_form(self):
        return TripForm(obj=self.model)

    def _instant_model(self):
        return Trip.query\
                   .filter_by(slug=self.slug, author=g.user)\
                   .first_or_404()

trips.add_url_rule('/new', view_func=CreateTripView.as_view('new'))
trips.add_url_rule('/<slug>/update',
                   view_func=UpdateTripView.as_view('update'))

Basically, I override the post method on my MethodView subclass to check for integrity errors and translate them into form errors if they are indeed uniqueness violations.

Can it be done better?


texts: Picking the smallest value in a list using recursion

texts:
This is the code I wrote in order to find the smallest value in a list using recursion.However I feel like there would be a better way to do this?

def minimum(lst):
    """
    parameters : lst of type list
    return : the value of the smallest element in the lst
    """
    if len(lst) == 1:
        return lst[0]

    if lst[0] < lst[1]:
        lst.append(lst[0])
        return(minimum(lst[1:]))
    else:
        return(minimum(lst[1:]))


texts: Calculating amount of diet soda a dieter can drink without dying

texts:
The following question was taken from Absolute Java 5th ed. by Walter Savitch:

  A government research lab has concluded that an artificial sweetener commonly used in diet soda pop will cause death in laboratory mice. A friend of yours is desperate to lose weight but cannot give up soda pop. Your friend wants to know how much diet soda pop it is possible to drink without dying as a result. Write a program to supply the answer. The program has no input but does have defined constants for the following items: the amount of artificial sweetener needed to kill a mouse, the weight of the mouse, the starting weight of the dieter, and the desired weight of the dieter. To ensure the safety of your friend, be sure the program uses the weight at which the dieter will stop dieting, rather than the dieter’s current weight, to calculate how much soda pop the dieter can safely drink. You may use any reasonable values for these defined constants. Assume that diet soda contains 1/10th of 1% artificial sweetener. Use another named constant for this fraction. You may want to express the percent as thedouble  value  0.001.  (If your program turns out not to use a defined constant, you may remove that defined constant from your program.)

This is the code that I have written:

public class Question4 {

    private static final double AMOUNT_OF_ARTIFICIAL_SWEETENER_TO_KILL_MOUSE_IN_LITRE = 0.001;
    private static final double WEIGHT_OF_MOUSE_IN_GRAMS = 25;
    private static final double DESIRED_WEIGHT_OF_DIETER_IN_GRAMS = 65000;
    private static final double CONCENTRATION_OF_ARTIFICIAL_SWEETENER_IN_DIET_SODA = 0.001;

    public static void main(String[] args) {

        System.out.println(maxAmountOfDietSodaDieterCanDrinkInLitre()
                + " litres.");// 2600.00 litres
    }

    public static double maxAmountOfDietSodaAMouseCanDrinkInLitre() {
        return AMOUNT_OF_ARTIFICIAL_SWEETENER_TO_KILL_MOUSE_IN_LITRE
                / CONCENTRATION_OF_ARTIFICIAL_SWEETENER_IN_DIET_SODA;
    }

    public static double maxAmountOfDietSodaDieterCanDrinkInLitre() {
        return (maxAmountOfDietSodaAMouseCanDrinkInLitre() * DESIRED_WEIGHT_OF_DIETER_IN_GRAMS)
                / WEIGHT_OF_MOUSE_IN_GRAMS;
    }
}


texts: Python factory method with easy registry

texts:
My aim is to define a set of classes, each providing methods for comparing a particular type of file. My idea is to use some kind of factory method to instantiate the class based upon a string, which could allow new classes to be added easily. Then it would be simple to loop over a dictionary like:

files = {
    'csv': ('file1.csv', 'file2.csv'),
    'bin': ('file3.bin', 'file4.bin')
}

Here is what I have so far:

# results/__init__.py
class ResultDiffException(Exception):
    pass

class ResultDiff(object):
    """Base class that enables comparison of result files."""
    def __init__(self, path_test, path_ref):
        self.path_t = path_test
        self.path_r = path_ref

    def max(self):
        raise NotImplementedError('abstract method')

    def min(self):
        raise NotImplementedError('abstract method')

    def mean(self):
        raise NotImplementedError('abstract method')

# results/numeric.py
import numpy as np
from results import ResultDiff, ResultDiffException

class NumericArrayDiff(ResultDiff):

    def __init__(self, *args, **kwargs):
        super(NumericArrayDiff, self).__init__(*args, **kwargs)

        self.data_t = self._read_file(self.path_t)
        self.data_r = self._read_file(self.path_r)

        if self.data_t.shape != self.data_r.shape:
            raise ResultDiffException('Inconsistent array shape')

        np.seterr(divide='ignore', invalid='ignore')
        self.diff = (self.data_t - self.data_r) / self.data_r
        both_zero_ind = np.nonzero((self.data_t == 0) & (self.data_r == 0))
        self.diff[both_zero_ind] = 0

    def _read_file(self, path):
        return np.loadtxt(path, ndmin=1)

    def max(self):
        return np.amax(self.diff)

    def min(self):
        return np.amin(self.diff)

    def mean(self):
        return np.mean(self.diff)

class CsvDiff(NumericArrayDiff):

    def __init__(self, *args, **kwargs):
        super(CsvDiff, self).__init__(*args, **kwargs)

    def _read_file(self, path):
        return np.loadtxt(path, delimiter=',', ndmin=1)

class BinaryNumericArrayDiff(NumericArrayDiff):

    def __init__(self, *args, **kwargs):
        super(BinaryNumericArrayDiff, self).__init__(*args, **kwargs)

    def _read_file(self, path):
        return np.fromfile(path)

As you can see, the classes CsvDiff and BinaryNumericArrayDiff have only very minor changes with respect to NumericArrayDiff, which could potentially be refactored using constructor arguments. The problem is that different file types would then require different constructor syntax, which would complicate the factory pattern.

I did also consider providing @classmethods to NumericArrayDiff, which could be put in a dict in order to link to the file types. However, I'm hoping for a more natural way of registering these classes to the factory.

Any advice would be much appreciated.


texts: Construct a numpy array by repeating a 1 dimensional array sliced at different indices

texts:
I have a pandas dataframe (called base_mortality) with 1 column and n rows, which is of the following form:
    age   |   death_prob
---------------------------
    60    |   0.005925
    61    |   0.006656
    62    |   0.007474
    63    |   0.008387
    64    |   0.009405
    65    |   0.010539
    66    |   0.0118
    67    |   0.013201
    68    |   0.014756
    69    |   0.016477

age is the index and death_prob is the probability that a person who is a given age will die in the next year. I want to use these death probabilities to project the expected annuity payment that would be paid to an annuitant over the next t years.
Suppose I have 3 annuitants, whose names and ages are contained in a dictionary:
policy_holders = {'John' : 65, 'Mike': 67, 'Alan': 71}

Then I would want to construct a new dataframe whose index is time (rather than age) which has 3 columns (one for each annuitant) and t rows (one for each time step). Each column should specify the probability of death for each policy holder at that time step. For example:
         John      Mike      Alan
0    0.010539  0.013201  0.020486
1    0.011800  0.014756  0.022807
2    0.013201  0.016477  0.025365
3    0.014756  0.018382  0.028179
4    0.016477  0.020486  0.031269
..        ...       ...       ...
96   1.000000  1.000000  1.000000
97   1.000000  1.000000  1.000000
98   1.000000  1.000000  1.000000
99   1.000000  1.000000  1.000000
100  1.000000  1.000000  1.000000

At present, my code for doing this is as follows:
import pandas as pd

base_mortality = pd.read_csv('/Users/joshchapman/PycharmProjects/VectorisedAnnuityModel/venv/assumptions/base_mortality.csv', index_col=['x'])
policy_holders = {'John' : 65, 'Mike': 67, 'Alan': 71}

out = pd.DataFrame(index=range(0,101))

for name, age in policy_holders.items():
    out[name] = base_mortality.loc[age:].reset_index()['age']
out = out.fillna(1)
print(out)

However, my aim is to remove this loop and achieve this using vector operations (i.e. pandas and/or numpy functions). Any suggestions on how I might improve my code to work in this way would be great!


texts:
Write a method rarest that accepts a map whose keys are strings and
whose values are integers as a parameter and returns the integer value
that occurs the fewest times in the map. If there is a tie, return the
smaller integer value. If the map is empty, throw an exception.
For example, suppose the map contains mappings from students' names
(strings) to their ages (integers). Your method would return the least
frequently occurring age. Consider a map variable m containing the
following key/value pairs:
{Alyssa=22, Char=25, Dan=25, Jeff=20, Kasey=20, Kim=20, Mogran=25,
Ryan=25, Stef=22}
Three people are age 20 (Jeff, Kasey, and Kim), two
people are age 22 (Alyssa and Stef), and four people are age 25 (Char,
Dan, Mogran, and Ryan).
So a call of rarest(m) returns 22 because only
two people are that age. If there is a tie (two or more rarest ages
that occur the same number of times), return the youngest age among
them. For example, if we added another pair of Kelly=22 to the map
above, there would now be a tie of three people of age 20 (Jeff,
Kasey, Kim) and three people of age 22 (Alyssa, Kelly, Stef). So a
call of rarest(m) would now return 20 because 20 is the smaller of the
rarest values.

Here is the link to the question.
public int rarest(Map<String, Integer> m)throws Exception{
    if(m.size() == 0){
      throw new Exception();//throwing an exception as per the question
    }else{
    Integer count = null;
    //Creating a TreeMap to get the lowest age.
    TreeMap<Integer, Integer> t = new TreeMap<Integer, Integer>();
    for(Map.Entry<String, Integer> me : m.entrySet()){
        if(t.containsKey(me.getValue())) {
            count = t.get(me.getValue());
            t.put(me.getValue(), count+1);
        }else {
            count = 1;
            t.put(me.getValue(), count);
        }
      }

     //If there is tie I am comparing the frequencies
     int freq = t.get(t.firstKey());

     TreeSet<Integer> val = new TreeSet<Integer>();

     for(Integer i : t.keySet()){
        if(freq > t.get(i) ){
            freq = t.get(i);
            val.add(i);
        }
     }
        if(val.size() > 0){
           return val.first();
        }else{
           return t.firstKey();
        }
    }
}

It gives the correct results for the given test cases, but I feel there should be a better way to do this.


texts: Count number of registers in interval & location

texts:
Recently I asked how one could count the number of registers by the interval as answered in https://stackoverflow.com/questions/49240140/count-number-of-registers-in-interval.

The solution works great, but I had to adapt it to also take into account some localization key.

I did it through the following code:

def time_features(df, time_key, T, location_key, output_key):
    """
    Create features based on time such as: how many BDs are open in the same GRA at this moment (hour)?
    """
    from datetime import date
    assert np.issubdtype(df[time_key], np.datetime64)
    output = pd.DataFrame()

    grouped = df.groupby(location_key)
    for name, group in grouped:
        # initialize times registers open as 1, close as -1
        start_times = group.copy()
        start_times[time_key] = group[time_key]-pd.Timedelta(hours=T)
        start_times[output_key] =  1

        aux = group.copy()
        all_times = start_times.copy()
        aux[output_key] = -1
        all_times = all_times.append(aux, ignore_index=True)

        # sort by time and perform a cumulative sum to get opened registers
        # (subtract 1 since you don't want to include the current time as opened)
        all_times = all_times.sort_values(by=time_key)
        all_times[output_key] = all_times[output_key].cumsum() - 1

        # revert the index back to original order, and truncate closed times
        all_times = all_times.sort_index().iloc[:len(all_times)//2]
        output = output.append(all_times, ignore_index=True)
    return output

Output:

time    loc1    loc2
0   2013-01-01 12:56:00 1   "a"
1   2013-01-01 12:00:12 1   "b"
2   2013-01-01 10:34:28 2   "c"
3   2013-01-01 09:34:54 2   "c"
4   2013-01-01 08:34:55 3   "d"
5   2013-01-01 08:34:55 5   "d"
6   2013-01-01 16:35:19 4   "e"
7   2013-01-01 16:35:30 4   "e"

time_features(df, time_key='time', T=2, location_key='loc1', output_key='count')

This works great for small data, but for longer data (I using it with a file with 1 million rows) it takes "forever" to run. I wonder if I could optimize this computation somehow.


texts: Acquiring an ID from a newly inserted entry in an MSSQL database

texts:
In an MSSQL database, I have automatically increasing IDs for every entry.
Is it safe to acquire the ID of the newly inserted entry like this? I am worried that, if at the exact same time another entry gets inserted, I could end up with the wrong ID.

string query = "INSERT INTO subscriptions (polygon,...) VALUES (@polygon,...)";
                    SqlDataAdapter cmd = new SqlDataAdapter();
                    using (SqlCommand querySaveSubscription = new SqlCommand(query))
                    {
                        querySaveSubscription.Connection = openCon;
                        // Create Poly out of given geodata values
                        var point = SqlGeography.Point(Double.Parse(lat), Double.Parse(lon), 4326);
                        SqlGeography poly = point.BufferWithTolerance(Double.Parse(radius) * 1000, 0.01, true);

                        var param = new SqlParameter(@"polygon", poly);
                        param.UdtTypeName = "Geography";

                        querySaveSubscription.Parameters.Add(param);
        ...

                        openCon.Open();
                        querySaveSubscription.ExecuteNonQuery();

I have the following c# code directly after my INSERT INTO - query getting executed:

                    query = "SELECT ID FROM subscriptions ORDER BY ID DESC";
                    SqlCommand command = new SqlCommand(query, openCon);
                    SqlDataReader reader = command.ExecuteReader();
                    reader.Read();

                    int id = reader.GetInt32(0);

                    result.Add("success", "true");

                    result.Add("id", id.ToString());


texts: Function for approximating a sine signal

texts:
I want to approximate a sine signal with a parabola, which is given by the function:

$$f(x) = 0.4053 \cdot x \cdot (3.1415 - x) \space\text{for}\space 0 \le x \le 3.1415$$

In a first approach, I wrote the following code which works fine:

float sin1(int phase)
{
    if(phase % 1000 <= 500)
    {
    return(0.4053*2*3.1415*(phase % 1000)/1000*(3.1415 - 2*3.1415*(phase % 1000)/1000));
    }
    else
    {
    return(-0.4053*2*3.1415*(phase % 500)/1000*(3.1415 - 2*3.1415*(phase % 500)/1000));
    }
}

As I want to use this function on a microcontroller, I'm interested in how to optimize this code regarding memory consumption and performance.


texts: 'iplayer' style content accordion/slider

texts:
I was wondering if someone could have a look at this BBC iPlayer style content accordion/slider I've just made, and help me to get it working properly? I'm no expert in jQuery and I'm sure I haven't done this correctly, or if there are more efficient ways of doing it.

The problem seems to be the clicking.  It sometimes requires two clicks, and the sliding of the content, the left panel seems to re-size when animating.

Here is all of the code.

Snippet from jsfiddle:

$('#accordion > li.bg1 .heading').toggle(
    function () {
        $('#accordion > li.bg2').stop().animate({'width':'718px'},1000);
        $('#accordion > li.bg1').stop().animate({'width':'50px'},1000);
        $('#accordion > li.bg1 .heading').stop(true,true).fadeOut(1000);
        $('#accordion > li.bg1 .heading-side').stop(true,true).fadeIn(2000);
        $('a.see-more-p').css('display','none');
     },
    function () {
        $('#accordion > li.bg1').stop().animate({'width':'374px'},1000);
        $('#accordion > li.bg2').stop().animate({'width':'394px'},1000);
        $('#accordion > li.bg1 .heading-side').stop(true,true).fadeOut(1000);
        $('#accordion > li.bg1 .heading').stop(true,true).fadeIn(2000);
        $('a.see-more-p').css('display','block');
    }
 );
$('#accordion > li.bg2 .heading, a.see-more-p').toggle(
    // same as above
);
$('#accordion > li.bg1 .heading-side').toggle(
    // same as above
);


texts:
I'm new to C++ and while learning, I wrote a simple program that asks for a word, shuffle sthe characters and then asks another person to find the original word, given the shuffled letters.

I would like to know what is right and wrong in this code, what and where can I improve.

#include <iostream>

void shuffle(std::string& word);

int main(int argc, char *argv[])
{
    std::string word, shuffled, guess;
    int count(0);

    std::cout << "Word to guess: ";
    std::cin >> word;

    shuffled = word;
    shuffle(shuffled);

    do {
        std::cout << "Guess(" << shuffled << "): ";
        std::cin >> guess;
        count++;
        if (guess != word) {
            std::cout << "No." << std::endl;
        }
    } while (guess != word);

    std::cout << "Congratulation!" << std::endl;
    std::cout << "Word found in " << count
              << " guess" << (count>1?"es":"")
              << "." << std::endl;

    return 0;
}

void shuffle(std::string& word)
{
    srand(time(nullptr));
    for (unsigned int i(0); i < word.length(); ++i) {
        std::swap(word[i], word[rand() % word.length()]);
    }
}


texts: Directed Acyclic graph implementation in Ruby on Rails

texts:
I have an implementation for a graph node class that I'd like to have function as a directed acyclic graph.

The associations are roughly as follows

class Node
  has_many :parents,
           class_name: 'Edge',
           foreign_key: :child_id

  has_many :children,
           class_name: 'Edge',
           foreign_key: :parent_id
end

class Edge
  belongs_to :parent, class_name: 'Node'
  belongs_to :child, class_name: 'Node'

  has_many :edge_properties
end

Me and a co-worker of mine had a discussion about the proper way to do this has_many relationship. The rails paradigm seems to dictate that node.parents should return a Node::ActiveRecord_Relation. However, in this case, node.parents returns a list of edges, which results the ask for the node's parents into something of the sort:

Node.where(id: Edge.where(child: node))

The question is; is this a code smell? Can we do this relationship better where node.parents would return Node::ActiveRecord? If so, how would one access edge.edge_properties?


texts: Is this a correct implementation/interpretation of the "Birthday Problem"?

texts:
I recently wanted to determine the probability of generating a colliding string, given a character pool size, string length and number of strings to generate.  I was led to the Birthday Problem, and came up with this code in PHP:

function collision_probability($length, $count = 1) {

    $chars = '1234567890qwertyuiopasdfghjklzxcvbnm';

    $probability = 1.0;

    // Maximum number of combinations
    $combinations = pow(strlen($chars), $length);

    echo "$combinations combinations at $length characters long for $count tries\n";

    foreach(range(1,$count) as $i) {
        $probability = $probability * (1 - ($i/$combinations));
    }

    $probability = 1 - $probability;
    echo "A total probability their being collisions of ".($probability*100). "%\n";

    return $probability;

}

echo collision_probability(9, 3000000);

Given the echos, and the values you can see, am I interpreting this correctly?  Or am I missing something?


texts:
When I last did a lot of Java programming, I'd do this:

All classes in a package would be default (package) visibility.
Each package would have a public PackageNamePack factory class, e.g. BusinessPack.
And the package would have a number of interfaces.
The package factory would have methods like newBusiness(...) which would instantiate new BusinessImplWhatever objects, cast as a Business interface.

The factory class:

package io.eaternet.adapters.framework;

public class FrameworkPack {
    public static Business newBusiness(String name, String address, String city, String zipcode, String origKey) {
        return new BusinessImpl(name, address, city, zipcode, origKey);
    }

    public static Inspection newInspection(String origKey, String businessOrigKey, String score, String date) {
        return new InspectionImpl(origKey, businessOrigKey, score, date);
    }
}

An interface:

package io.eaternet.adapters.framework;

public interface Business {
    public String getName();
    public String getAddress();
    public String getCity();
    public String getZipcode();
    public String getOrigKey();
}

A package-visibility implementation class:

package io.eaternet.adapters.framework;

class BusinessImpl implements Business {
    String name, address, city, zipcode, origKey;

    BusinessImpl(String name, String address, String city, String zipcode, String origKey) {
        this.name = name;
        this.address = address;
        this.city = city;
        this.zipcode = zipcode;
        this.origKey = origKey;
    }

    public String getName() { return name; }
    public String getAddress() { return address; }
    public String getCity() { return city; }
    public String getZipcode() { return zipcode; }
    public String getOrigKey() { return origKey; }
}

I've started a new Java library which follows this pattern, and am wondering if it still make sense. This particular code is part of a CSV parser, which is why everything is a string.


texts: Async Task with multiple requests in android

texts:
I am using this AsyncTask class to update two different tables on SQL Server. So far this code works fine, I'm interested in a better and more sufficient code structure for this class, specially in doinbackground().

Is it okay to call multiple webservice methods in a single thread? Any other feedback?

    private class Update extends AsyncTask<Void, Void, Integer> {
    private final int FAILED_INVALID_RESPONSE = 0;
    private final int SUCCESS_GET_DATA = 1;
    ProgressDialog progress;
    private String _phoneno;
    private String _ticket;
    UpdateTicket(String phoneno,String ticket){
        _phoneno=phoneno;
        _ticket=ticket;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress = ProgressDialog.show(XYZ.this, "",
                "In Progress...", false);
    }

    @Override
    protected Integer doInBackground(Void... params) {
        method1(_phoneno);
        return  method2(_phoneno,_ticket);
    }
    @Override
    protected void onPostExecute(Integer result) {
        progress.dismiss();
        switch (result) {
            case FAILED_INVALID_RESPONSE:
                Toast.makeText(XYZ.this,"Please Check your Internet Connection.",Toast.LENGTH_SHORT).show();
                break;
            case SUCCESS_GET_DATA:
                Toast.makeText(XYZ.this, "Success!", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    int  method1(String phoneno,String tickets)
    {
        final  String methodname = "firstmethod";
        final  String NAMESPACE ="http://tempuri.org/";
        final  String URL="www.sampleurl.com";
        final  String SOAP_ACTION="http://tempuri.org/firstmethod";
        int success=0;
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        SoapObject request = new SoapObject(NAMESPACE, methodname);
        SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        request.addProperty("phoneno", phoneno);
        request.addProperty("tickets", tickets);
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try{
            androidHttpTransport.call(SOAP_ACTION,envelope);
            SoapObject response = (SoapObject) envelope.bodyIn;
        if(response!=null){
            success=1;
        }
        }
        catch (Exception e)
        {
            e.printStackTrace();

        }

        return success;
    }
    int  method2(String Phone) {
        final  String methodname = "secondmethod";
        final  String NAMESPACE ="http://tempuri.org/";
        final  String URL="www.sampleurl.com";
        final  String SOAP_ACTION="http://tempuri.org/secondmethod";
        int success=0;
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        SoapObject request = new SoapObject(NAMESPACE, methodname);
        SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        request.addProperty("phoneno", phoneno);
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try{
            androidHttpTransport.call(SOAP_ACTION,envelope);
            SoapObject response = (SoapObject) envelope.bodyIn;
        if(response!=null){
            success=1;
        }
        }
        catch (Exception e)
        {
            e.printStackTrace();

        }

        return success;
    }

}


texts: Multiple returns in a method or one return

texts:
I have a method which returns an Array. This array can contain values or can be empty. Here is the code:

function getCustomerIds() {
    //this declaration is at the top to inform about returning value of the method; I don't declare variables at the top
    var customerIds = [];
    var linkedContact = getLinkedContacts(this);
    if (linkedContact === null) {
        return [];
    }
    var linkedResource = getLinkedResources(linkedContact);
    if (linkedResource === null) {
        return [];
    }
    var linkedCustomersIds = getLinkedCustomers(linkedResource);
    return customerIds.concat(linkedCustomersIds);
}

I'm not sure if it should be return [] or return customerIds. The first one seems more explicit that the empty array is returned, while the second option seems more logical. What would be the best practice here?  Do you have some other suggestions re method structure?


texts: Select and delete objects from S3 container

texts:
GitLab saves files with the pattern 1530410429_2018_07_01_11.0.1_gitlab_backup.tar (epoch, date, version, fixed string) to an S3 container.

I wanted to create a Python module to manage those backups, particularly to delete old backups, but preserving a specified number of files for each version).

To this end I:

read S3 bucket contents and populate a list of dictionaries containing file name and an extracted version
extract a set of versions from the above list
iterate over each version and create a list of files to delete
iterate over the above result and delete the files from the bucket

It works, but I feel the data structures I used are not appropriate and not pythonic.

How could I simplify the data structures and possibly minimize the number of function calls.

import boto3

def get_set_of_versions(table_of_backups):
    set_of_versions = set()
    for backup in table_of_backups:
        set_of_versions.add(backup['gitlab_version'])
    return set_of_versions

def create_table_of_backups(list_of_backup_files):
    table_of_backups = [
        {
            "s3_object_name": filename,
            "gitlab_version": filename.split('_')[4]
        } for filename in list_of_backup_files
    ]
    return table_of_backups

def extract_list_of_backups(list_of_objects):
    list_of_backups = [s3_object.key for s3_object in list_of_objects if 'gitlab_backup' in s3_object.key]
    return list_of_backups

class GitlabBackups:
    def __init__(self, bucket_name):
        self.bucket_name = bucket_name

        self.s3 = boto3.resource('s3')
        self.bucket = self.s3.Bucket(bucket_name)
        self.list_of_backup_files = extract_list_of_backups(self.bucket.objects.all())
        self.table_of_backups = create_table_of_backups(self.list_of_backup_files)
        self.set_of_versions = get_set_of_versions(self.table_of_backups)

    def return_candidates_for_deletion_within_version(self, version, generations_to_keep=1):
        if generations_to_keep == 0:
            raise ValueError
        list_of_versioned_backups = [
            backup for backup in self.table_of_backups if backup['gitlab_version'] == version
        ]
        return [backup['s3_object_name'] for backup in list_of_versioned_backups[:-generations_to_keep]]

    def return_candidates_for_deletion(self, generations_to_keep=2):
        resulting_list = list()
        for version in self.set_of_versions:
            resulting_list.extend(self.return_candidates_for_deletion_within_version(version, generations_to_keep))
        return resulting_list

    def delete_old_backups(self, generations_to_keep=2):
        list_to_delete = self.return_candidates_for_deletion(generations_to_keep)
        for key in list_to_delete:
            self.s3.Object(self.bucket_name, key).delete()


texts: Check existence of a row then update a column

texts:
not too experienced with SQL, wondering if I can make this better... I am trying to check if a row exists and then add one to the frequency column if it does, if it does not exist it should return false...

The table is indexed by the column in the first select statement

I know about the risks of using formatting with public facing code, but this is for processing on a local machine only

    def check_word(self, word_name):
        self.c.execute('SELECT * FROM {tn} WHERE {cn} = """{wn}"""'.format(tn=self.table1, cn=self.column1, wn=word_name))
        exist = self.c.fetchall()
        if exist:
            new_freq = exist[0][2] + 1
            self.c.execute(
            'UPDATE {tn} SET {c2n}={en} WHERE {c1n}="""{word}"""'.format(tn=self.table1, c2n=self.column2,
                                                                         en=new_freq, c1n=self.column1,
                                                                         word=word_name))
            return True
        return False


texts:
I'm learning segment trees and their implementation. For the purpose, I tried solving the problem on CodeChef.

Full code here

My tree implementation is as follows:

#include <bits/stdc++.h>

#define _MAX (1<<17)
#define MAX (_MAX<<1)
using namespace std;

struct node
{
    int tails;
    int heads;
    void Merge(node &A,node &B) // Merge two nodes
    {
        tails=A.tails+B.tails;
        heads=A.heads+B.heads;
    }
    void CreateLeaf() // Creates a leaf
    {
        tails=1;
        heads=0;
    }
};

node ST[MAX];   // Segment tree ST

/* res is the node storing the result
   Information for range [l,r] is present in ST[idx]
   Query is required for range [u,v]  */

void Query(node &res,int l,int r,int u,int v,int idx)
{
    if(l==u && r==v)
        {
            res=ST[idx];
            return;
        }
    else
    {
        int mid=(l+r)/2;
        if(mid>=v)
            Query(res,l,mid,u,v,2*idx);
        else if(mid<u)
            Query(res,mid+1,r,u,v,2*idx+1);
        else{
            node left,right;
            Query(left,l,mid,u,mid,2*idx);    // left child is 2*idx
            Query(right,mid+1,r,mid+1,v,2*idx+1);   // right child is 2*idx+1
            res.Merge(left,right);
        }
    }
}

void init(int idx,int l,int r) // initialises the tree
{
    if(l==r)
        {
            ST[idx].CreateLeaf();
            return;
        }
    else
    {
        int mid=(l+r)/2;
        init(2*idx,l,mid);
        init(2*idx+1,mid+1,r);
        ST[idx].Merge(ST[2*idx],ST[2*idx+1]);
    }
}

/* Updates the value of a single node.
    When we flip the coins, no. of heads and tails get swapped */

void updatevalue(node &A)
{ swap(A.tails,A.heads); }

/* Updates the node ST[idx] and all the nodes descending the node ST[idx]
    ST[idx] stores information for range [l,r]  */

void update(int l,int r,int idx)
{
    if(l==r) // returns as soon as we reach a leaf node
        {
            updatevalue(ST[idx]);
            return;
        }
    else
    {
        updatevalue(ST[idx]);
        int mid=(l+r)/2;
        update(l,mid,2*idx); // Updates left child
        update(mid+1,r,2*idx+1); // Updates right child
    }
}

/* Range to be updated is [u,v]
    ST[idx] stores information for range [l,r] */

void UpdateRange(int l,int r,int u,int v,int idx)
{
   if(u<=l && r<=v)
         {
            update(l,r,idx);
                // As soon as required node is found, update the node and all its descendant
            return;
         }
    else
    {
        int mid=(l+r)/2;
        if(mid>=v)
            UpdateRange(l,mid,u,v,2*idx);
        else if(mid<u)
            UpdateRange(mid+1,r,u,v,2*idx+1);
        else
        {
            UpdateRange(l,mid,u,mid,2*idx);
            UpdateRange(mid+1,r,mid+1,v,2*idx+1);
        }
            ST[idx].Merge(ST[2*idx],ST[2*idx+1]);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,q,x,y,z;
    node res;
    cin>>n>>q;
    init(1,0,n-1); /* ST[1] stores information for [0,n-1] */
    while(q--)
    {
        cin>>x>>y>>z;
        if(x==1)
        {
            Query(res,0,n-1,y,z,1);
            cout<<res.heads<<"\n";
        }
        else
        {
           UpdateRange(0,n-1,y,z,1);
        }
    }
    return 0;
}

I've read many online sources and applied the segmented tree to the best of my knowledge, however my implementation is shown slow in accordance with time constraints for the problem. Please tell where my code is lacking. I feel my range update is a slow function but I can't find any method to speed it up as when we flip a coin, we have to update all ranges having that coin. Any better implementation for Range update or query is welcomed.


texts:
This came out on the spur of the moment, as a quick and dirty tool to have the job done.

This is a simple image scraper for immensely popular and legendary comic website inspired by a Python Easter Egg.

For those who don't know it, run your Python interpreter and type import antigravity and hit Enter. :)

As for the code below, I'd appreciate any feedback, particularly in regards to threading, as I'm new to this.

#!/usr/bin/python3

import os
import sys
import time
import threading
from pathlib import Path
from shutil import copyfileobj

import requests
from lxml import html

BASE_URL = "https://www.xkcd.com/"
ARCHIVE = "https://www.xkcd.com/archive"
SAVE_DIRECTORY = Path('xkcd_comics')
LOGO = """
       _           _
 tiny | |  image  | | downloader for
 __  _| | _____ __| |  ___ ___  _ __ ___
 \ \/ / |/ / __/ _` | / __/ _ \| '_ ` _ \
  >  <|   < (_| (_| || (_| (_) | | | | | |
 /_/\_\_|\_\___\__,_(_)___\___/|_| |_| |_|
 version 0.1
"""

def show_logo():
    print(LOGO)

def fetch_url(url: str) -> requests.Response:
    return requests.get(url)

def head_option(values: list) -> str:
    return next(iter(values), None)

def get_penultimate(url: str) -> int:
    page = fetch_url(url)
    tree = html.fromstring(page.content)
    newest_comic = head_option(
        tree.xpath('//*[@id="middleContainer"]/a[1]/@href'))
    return int(newest_comic.replace("/", ""))

def get_images_from_page(url: str) -> str:
    page = fetch_url(url)
    tree = html.fromstring(page.content)
    return head_option(tree.xpath('//*[@id="comic"]//img/@src'))

def get_number_of_pages(latest_comic: int) -> int:
    print(f"There are {latest_comic} comics.")
    print(f"How many do you want to download? Type 0 to exit.")
    while True:
        try:
            number_of_comics = int(input(">> "))
        except ValueError:
            print("Error: Expected a number. Try again.")
            continue
        if number_of_comics > latest_comic or number_of_comics < 0:
            print("Error: Incorrect number of comics. Try again.")
            continue
        elif number_of_comics == 0:
            sys.exit()
        return number_of_comics

def clip_url(img: str) -> str:
    return img.rpartition("/")[-1]

def make_dir():
    return os.makedirs(SAVE_DIRECTORY, exist_ok=True)

def save_image(img: str):
    comic_name = clip_url(img)
    print(f"Downloading: {comic_name}")
    f_name = SAVE_DIRECTORY / comic_name
    with requests.get("https:" + img, stream=True) as img, open(f_name, "wb") \
            as output:
        copyfileobj(img.raw, output)

def show_time(seconds: int) -> int:
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    time_elapsed = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
    return time_elapsed

def get_xkcd():
    show_logo()
    make_dir()

    collect_garbage = []
    latest_comic = get_penultimate(ARCHIVE)
    pages = get_number_of_pages(latest_comic)

    start = time.time()
    for page in reversed(range(latest_comic - pages + 1, latest_comic + 1)):
        print(f"Fetching page {page} out of {latest_comic}")
        try:
            url = get_images_from_page(f"{BASE_URL}{page}/")
            thread = threading.Thread(target=save_image, args=(url, ))
            thread.start()
        except (ValueError, AttributeError, requests.exceptions.MissingSchema):
            print(f"WARNING: Invalid comic image source url.")
            collect_garbage.append(f"{BASE_URL}{page}")
            continue
    thread.join()
    end = time.time()

    print(f"Downloaded {pages} comic(s) in {show_time(int(end - start))}.")

    if len(collect_garbage) > 0:
        print("However, was unable to download images for these pages:")
        print("\n".join(page for page in collect_garbage))

def main():
    get_xkcd()

if __name__ == '__main__':
    main()


texts: Should I be trying to imitate the control flow in this example R code?

texts:
I have heard that some patterns in R code are really anti-patterns and thus I am trying to imitate superior examples of R code.

I am confused about control flow.

I have been reviewing some R code from openintro.org;
the original exercise is explained at:

http://www.openintro.org/download.php?file=os2_lab_02A&referrer=/stat/labs.php

One of the very early examples deals with counting "hit" or "miss" streaks within a vector of "H" and "M" characters.  We consider a winning streak to be at least one H, and each streak ends in M.  So H M M H H M counts as a streak of 1, a streak of 0, a streak of 2, etc.

So, for example, we start with a sequence like kobe$basket:

  [1] "H" "M" "M" "H" "H" "M" "M" "M" "M" "H" "H" "H" "M" "H" "H" "M" "M" "H"
 [19] "H" "H" "M" "M" "H" "M" "H" "H" "H" "M" "M" "M" "M" "M" "M" "H" "M" "H"
 [37] "M" "M" "H" "H" "H" "H" "M" "H" "M" "M" "H" "M" "M" "H" "M" "M" "H" "M"
 [55] "H" "H" "M" "M" "H" "M" "H" "H" "M" "H" "M" "M" "M" "H" "M" "M" "M" "M"
 [73] "H" "M" "H" "M" "M" "H" "M" "M" "H" "H" "M" "M" "M" "M" "H" "H" "H" "M"
 [91] "M" "H" "M" "M" "H" "M" "H" "H" "M" "H" "M" "M" "H" "M" "M" "M" "H" "M"
[109] "H" "H" "H" "M" "H" "H" "H" "M" "H" "M" "H" "M" "M" "M" "M" "M" "M" "H"
[127] "M" "H" "M" "M" "M" "M" "H"

and if we call calc_streak on that, we get:

 [1] 1 0 2 0 0 0 3 2 0 3 0 1 3 0 0 0 0 0 1 1 0 4 1 0 1 0 1 0 1 2 0 1 2 1 0 0 1 0
[39] 0 0 1 1 0 1 0 2 0 0 0 3 0 1 0 1 2 1 0 1 0 0 1 3 3 1 1 0 0 0 0 0 1 1 0 0 0 1

The "1 0 2" of the answer indicates the streak of 1, the streak of 0, the streak of 2 mentioned above.

The following function is referred to as "calc_streak" but apparently that name doesn't show up in the code listing.

function(x){
y <- rep(0,length(x))
y[x == "H"] <- 1
y <- c(0, y, 0)
wz <- which(y == 0)
streak <- diff(wz) - 1
return(streak)
}

I think the first line makes a big vector of "0" values, just as long as the input, so in this case it should be 133 entries long.

I think the second line goes through that vector and rewrites the "0" value to "1" if the input has an "H."

The third line might be putting an extra "0" at the front and back of the vector.  I think it's in there to make the "diff" work correctly, so that the first winning streak shows up as a "1" value.

I think the object "wz" is a vector of integers. (I suspect this is a short vector, just containing the indices of the entries in x that satisfy the test.)

So first and foremost, if I am misunderstanding the way this example code works, please correct me.

But my primary question is - Should I regard the example R code as a good example of control flow and imitate it in my other programs?

Thanks.


texts: Extending Exceptions with module-specific prefix, intended for parsing error reporting

texts:
For one of my [WIP] project, I extended the dictionary class a lot with specific handlers, as such I determined that I wanted KeyError and TypeError exceptions to be specific to my project's parser. I extended the exception class like this:

Github Permalink

"""Exception classes
"""

class ValveException(Exception):
    """Base Exception
    """
    pass

class ValveKeyError(ValveException):
    """KeyError specific to map parsing
    """
    pass

class ValveTypeError(ValveException):
    """TypeError specific to map parsing
    """
    pass

Exceptions are raised by either the recursive parser loop, or by the extended dictionaries when they are given a key that is not whitlisted:

Permalink

if not datatype in self.datatypes:
    raise ValveKeyError(
        "Unknown datatype [ %s ]"
        % datatype
    )
...

Permalink

if not hasattr(self, vmf_key):
    raise ValveKeyError(
        'Key `%s` not allowed in %s'
        % (key, self._type())
    )
...

Permalink

if not value[0].allow_multiple:
    raise ValveTypeError(
        "Type %s does not allow allow multiple values"
        % (type(value[0]))
    )
...

Permalink

if any([
    value is None,
    not type(value)(allowedcontainer(value)) == value
]):
    # We do not get the same value, we cannont cast
    raise ValveTypeError(
        ("Illigal Type %s for key `%s`, "
        + "expected Type %s for value `%s`") % (
            type(value), key, allowedcontainer, value
        )
    )
...

And Lastly, the exception handler:

Permalink

except ValveException as err:
    self._parseerrors.append(
        'Ln %s: %s' % (self.i, err)
    )

Which works for me and I am content to leave it alone; This lets me except MyTypeError and except MyKeyError as to distinguish between a parsing problem, and a coding problem.

Is this distinction between Coding Fault, and Parsing Fault a good distinction to have? I don't know if I feel comfortable using the native KeyError and TypeErrors in place, not until I was absolutely sure that there were no ways they could be a coding fault, as that might cause exception routes to handle bad parser input handle a coding exception.

Thoughts and insights are welcome, as well as constructive criticism regarding the post's subject matter.

  [edit] Added Exceptions Raise calls (with links), as requested.

Note: The revision posted was non-stable, I have since published a revision that at least can run [d8bf9f18] Though it is widely irrelevant to the question posed in this thread.


texts:
I've been writing basic Python scripts for a while now to help process data or automate some task but I've decided I should start picking up unit testing and objective orientated programming (the vast majority of my scripts so far have been procedural).

As a starter I decided to follow along with Uncle Bob's bowling scoring kata to try and get my mind around TDD and the idea of writing the absolute minimal code at every step to either make the test go red or green (plus any refactoring steps).

As it's a bare bones example of following TDD the main program doesn't actually have an entry point other than via the tests.

Things that stand out to my beginner's eye:

There are a lot of selfs which look like a lot of visual clutter when I read through the code. Is there a better way of doing this? I think it's the density of them that really gets me so I wasn't sure if I could abstract some of them somehow?
unittest seems to have a lot of boilerplate. I had a play with nose a while back which seemed to strip a lot of that out but I thought it might be a good idea to start properly with unittest until I have a use case for anything that nose (or any other library) offers.

bowling_game.py

#!/usr/bin/env python

class Game:

  _rolls = [0] * 21
  _current_roll = 0

  def roll(self, pins):
    self._rolls[self._current_roll] = pins
    self._current_roll += 1

  def score(self):
    score = 0
    frame_index = 0
    for frame in range(0, 10):
      if self._is_strike(frame_index):
        score += 10 + self._strike_bonus(frame_index)
        frame_index += 1
      elif self._is_spare(frame_index):
        score += 10 + self._spare_bonus(frame_index)
        frame_index += 2
      else:
        score += self._rolls[frame_index] + self._rolls[frame_index + 1]
        frame_index += 2
    return score

  def _sum_of_balls_in_frame(self, frame_index):
    return self._rolls[frame_index] + self._rolls[frame_index + 1]

  def _spare_bonus(self, frame_index):
    return self._rolls[frame_index + 2]

  def _strike_bonus(self, frame_index):
    return self._rolls[frame_index + 1] + self._rolls[frame_index + 2]

  def _is_spare(self, frame_index):
    return self._rolls[frame_index] + self._rolls[frame_index + 1] == 10

  def _is_strike(self, frame_index):
    return self._rolls[frame_index] == 10

bowling_game_test.py

#!/usr/bin/env python

import unittest

from bowling_game import Game

class BowlingGameTest(unittest.TestCase):

  def setUp(self):
    self.g = Game()

  def roll_many(self, rolls, pins):
    for roll in range(0, rolls):
      self.g.roll(pins)

  def roll_spare(self):
    self.g.roll(5)
    self.g.roll(5)

  def roll_strike(self):
    self.g.roll(10)

  def test_gutter_game(self):
    rolls = 20
    pins = 0
    self.roll_many(rolls, pins)
    self.assertEquals(self.g.score(),0)

  def test_all_ones(self):
    rolls = 20
    pins = 1
    self.roll_many(rolls, pins)
    self.assertEquals(self.g.score(),20)

  def test_one_spare(self):
    self.roll_spare()
    self.g.roll(3)
    self.roll_many(17, 0)
    self.assertEquals(self.g.score(),16)

  def test_one_strike(self):
    self.roll_strike()
    self.g.roll(3)
    self.g.roll(4)
    self.roll_many(16, 0)
    self.assertEquals(self.g.score(),24)

  def test_perfect_game(self):
    self.roll_many(12, 10)
    self.assertEquals(self.g.score(),300)

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

The commit history is available on GitHub if anyone fancies taking a look at that to see where I might have gone better with the red -> green -> refactor cycle.


texts: Determining if someone has read and seen all the questions of a book

texts:
I am trying to evaluate if someone has read and seen all the questions of a book.

I am using PHP to average everything out after I get the data. I know MySQL can avg, but I can't seem to wrap my head around how I can average the box number of the questions (similar to spaced repetition learning) if they haven't seen every question.

How can this function be better? I feel there might be a more elegant way of doing it.

SELECT COUNT(s.id),
       (SELECT COUNT(u.id)
          FROM user_sections u
         WHERE u.chpt_id = ? && u.user_id = ?
       ),
       (SELECT COUNT(q.id)
          FROM question q
         WHERE q.chapter_id = ? && (q.module = 1 || q.module = ?)
       ),
       (SELECT COUNT(v.id)
          FROM user_questions v
         WHERE v.chpt_id = ? && u.user_id = ?
       ),
       (SELECT AVG(v.box)
          FROM user_questions v
         WHERE v.chapter_id = ? && u.user_id=?
       ),
       (SELECT AVG(m.$mkts_col)
          FROM mkts m
         WHERE m.chapter_id = ? && (m.module = 1 || m.module = ?)
       )
  FROM sections s
 WHERE s.chapter_id = ? && (s.module = 1 || s.module = ?)