texts:
This code hides a message in an image by altering the blue values in the top left part. This is just for fun as I think it would be easy for a cryptographer to find and decrypt this.

Original

With message We will meet at London bridge at dawn

As you can (cannot) see, as long as the message is short enough, the naked eye has difficulty noticing the difference. Zooming in the top left corner will reveal the trick though.

from PIL import Image
from itertools import zip_longest
import doctest

def read_and_write_after_tranformation(infile, outfile, transformation):
    # Based on a StackOverflow answer.
    im = Image.open(infile)
    list_of_pixels = list(im.getdata())
    im2 = Image.new(im.mode, im.size)
    im2.putdata(transformation(list_of_pixels))
    im2.save(outfile)

def read_rgb(infile):
    return list(Image.open(infile).getdata())

def steganographic_encrypt(message, rgb_tuples):
    """
    Encodes a message in the blue values of the pixels on the top left corner of the image.

    >>> steganographic_encrypt("Hello, World!", [(a, b, c, luminosity) for a, b, c, luminosity in zip(range(12, 30), range(20, 48), range(100, 128), range(100, 128))])
    [(12, 20, 72, 100), (13, 21, 101, 101), (14, 22, 108, 102), (15, 23, 108, 103), (16, 24, 111, 104), (17, 25, 44, 105), (18, 26, 32, 106), (19, 27, 87, 107), (20, 28, 111, 108), (21, 29, 114, 109), (22, 30, 108, 110), (23, 31, 100, 111), (24, 32, 33, 112), (25, 33, 113, 113), (26, 34, 114, 114), (27, 35, 115, 115), (28, 36, 116, 116), (29, 37, 117, 117)]
    """
    return [(red, green, blue if ascii_val is None else ascii_val, luminosity)
              for (red, green, blue, luminosity), ascii_val in zip_longest(rgb_tuples, map(ord, message))]

def steganographic_decrypt(rgb_tuples):
    """
    Decodes a message from the blue values of the pixels on the top left corner of the image.

    >>> steganographic_decrypt([(12, 20, 72, 100), (13, 21, 101, 101), (14, 22, 108, 102), (15, 23, 108, 103), (16, 24, 111, 104), (17, 25, 44, 105), (18, 26, 32, 106), (19, 27, 87, 107), (20, 28, 111, 108), (21, 29, 114, 109), (22, 30, 108, 110), (23, 31, 100, 111), (24, 32, 33, 112), (25, 33, 113, 113), (26, 34, 114, 114), (27, 35, 115, 115), (28, 36, 116, 116), (29, 37, 117, 117)])
    'Hello, World!qrstu'
    """
    return ''.join([chr(blue) for (red, green, blue, luminosity) in rgb_tuples])

if __name__ == "__main__":
    doctest.testmod()
    read_and_write_after_tranformation(
        "cc.png",
        "cc2.png",
        lambda image: steganographic_encrypt("We will meet at London bridge at dawn", image)
    )

    print(steganographic_decrypt(read_rgb("cc2.png"))[0:100])


texts: Python [Discord.py] - Sports Fixture Bot

texts:
I have created a simple bot using Discord.py which outputs the contents of text files which contain scraped sports fixtures and channel data (scraped data is received from other small scripts I created but haven't included here).
discordbot.py
import discord
import os
from dotenv import load_dotenv

client = discord.Client()

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

client = discord.Client()

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(
        f'{client.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )

@client.event
async def on_message(message):
    id = client.get_guild(73194****229982)

    if str(message.channel) == "boxingmma":
        if message.content.find("!boxing") != -1:

            with open('/home/brendan/Desktop/boxingtest.txt', 'r') as file:
                msg = file.read(2000).strip()
                while len(msg) > 0:
                    await message.author.send(msg)
                    msg = file.read(2000).strip()

    if str(message.channel) == "football":
        if message.content.find("!english") != -1:

            with open('/home/brendan/Desktop/splittest.txt', 'r') as file:
                msg = file.read(2000).strip()
                while len(msg) > 0:
                    await message.author.send(msg)
                    msg = file.read(2000).strip()

    if str(message.channel) == "football":
        if message.content.find("!scottish") != -1:

            with open('/home/brendan/Desktop/testing2.txt', 'r') as file:
                msg = file.read(2000).strip()
                while len(msg) > 0:
                    await message.author.send(msg)
                    msg = file.read(2000).strip()

client.run("NzMxOTQ4Mzk5NzY2NjY3NDg2.XwuPsw.iN****-e2yDnRS_uWqff43Thvqw")

The bot is running fine and when commands are run on the relevant discord channel a DM is sent to the user who sent the request to the bot. The biggest issue I have with this code is the repetitive code in the on_message function. The only difference in the three different double if statements is the channel name e.g. Football or Boxing, the command e.g. !scottish, !english, !boxing and the filepath to the scraped data.
I am curious if anyone knows of anyways this could be improved or a way for the repetitive code to be reused? I tried to pass these as positional arguments from another function, for example:
def callall():
    on_message(channel="football", command="!english", filepath="/home/brendan/Desktop/test.txt")

but it appears that after researching this it is not possible to pass arguments to on_message in discord.py
Any suggestions on how the repetitive code can be resued, or ways in which the general program flow can be improved would be much appreciated.
If this would be better over at Stackoverflow instead please let me know as well.
Thanks all.


texts:
The following function accepts a list of Topic entities, retrieved from a database using LINQ-to-Entities. Each Topic has an Id,  Title and ParentId.

I want to populate an ASP.NET TreeView control, and so the function is creating a hierarchy of the Topics based on their ParentId. If a Topic has no parent, its ParentId is null and I put it under the 'root'.

public TreeNode GenerateTopicsTree(List<Topic> topics) {
  var s = new Stack<TreeNodeAndId>();
  var root = new TreeNode("Topics", "0");
  s.Push(new TreeNodeAndId { Id = null, TreeNode = root });
  while (s.Count > 0) {
    var node = s.Peek();
    var current = topics.FirstOrDefault(o => o.ParentId == node.Id);
    if (current == null) {
      s.Pop();
      continue;
    }
    var child = new TreeNode(current.Title, current.Id.ToString());
    node.TreeNode.ChildNodes.Add(child);
    s.Push(new TreeNodeAndId { Id = current.Id, TreeNode = child });
    topics.Remove(current);
  }
  return root;
}

struct TreeNodeAndId
{
  public TreeNode TreeNode;
  public int? Id;
}

Any improvements?


texts:
I'm new at groovy, and this is my first time writing a database connection script. It may seem like example code, but it is more like test code. It works as posted, and I'm planning to use this as a model for a larger business application, so I want to make sure I do it right.

Are there any groovy features that it could benefit from? Did I miss any edge cases, or anything else I could improve?

import groovy.sql.Sql

def dbUrl      = "jdbc:postgresql://localhost/GroovyTest"
def dbUser     = "Phrancis"
def dbPassword = "test"
def dbDriver   = "org.postgresql.Driver"

def sql = Sql.newInstance(dbUrl, dbUser, dbPassword, dbDriver)

println "Sql Instance: " + sql

sql.execute """SET SEARCH_PATH TO groovy_test;"""

sql.execute """
        START TRANSACTION;
        DROP TABLE IF EXISTS test;
        CREATE TABLE test (
            id SERIAL,
            string TEXT,
            number INTEGER,
            decimal NUMERIC,
            datetime TIMESTAMP
            );
        COMMIT;"""

def params1 = ['''');DROP TABLE test;--''', 42, 3.14159, 'NOW()']
def params2 = ['Hello, World!', 99999999, 0.1209823098234, '2015-06-25']

sql.execute """
        START TRANSACTION;
        INSERT INTO test (string, number, decimal, datetime)
            VALUES (?, ?, ?, CAST(? AS TIMESTAMP));
        COMMIT;""", params1

sql.execute """
        START TRANSACTION;
        INSERT INTO test (string, number, decimal, datetime)
            VALUES (?, ?, ?, CAST(? AS TIMESTAMP));
        COMMIT;""", params2

sql.eachRow("SELECT * FROM test;") { row ->
    println """
The row Id is: ${row.id}
The string is: ${row.string}
The number is: ${row.number}
The decimal is: ${row.decimal}
The date-time is: ${row.datetime}"""
}
sql.close()

Here is the stdout after this runs:

Sql Instance: groovy.sql.Sql@385c9627

The row Id is: 1
The string is: ');DROP TABLE test;--
The number is: 42
The decimal is: 3.14159
The date-time is: 2015-06-25 19:49:25.603991

The row Id is: 2
The string is: Hello, World!
The number is: 99999999
The decimal is: 0.1209823098234
The date-time is: 2015-06-25 00:00:00.0

Process finished with exit code 0


texts: Capturing the positions of "start" and "end" markers in a multi-line string

texts:
Is there any better (and shorter) way to get index of a regex match in Python?

import re
sstring = """
this is dummy text
which starts with nothing
and ends with something
"""

starts = re.finditer('start[s]?', sstring)
ends = re.finditer('end[s]?', sstring)

for m in starts:
    print (m.start())

for m in ends:
    print (m.end())

For me, there is only one starts and endsmatch in the string.


texts: Setting some element widths, disabling a button, and initializing some date fields

texts:
Does anyone have suggestions on how to refactor this?

$(document).ready(function () {

        $("td,select,th").css("min-width", "150px");

        $("#cancel").button({
            disabled: true
        });
        $(".group-32 input").datepicker();
        $(".group-6 input").datepicker();

        $("#0LevelId").change(function () {
            var currentYear = (new Date).getFullYear();
            $("#0EffectiveDate").val("01/01/" + (currentYear + 1));
        });

        $("#1LevelId").change(function () {
            var currentYear = (new Date).getFullYear();
            $("#1EffectiveDate").val("01/01/" + (currentYear + 1));
        });

        $("#3LevelId").change(function () {
            var currentYear = (new Date).getFullYear();
            $("#3EffectiveDate").val("01/01/" + (currentYear + 1));
        });
    });


texts:
I've written some PHP code that connects to a database and saves input entered by the user at our signup page. I would like some feedback before I present this code to my colleagues, as I intend on using this code for our website:

Security
Use of PDO vs other connection methods
Variable Naming
PHP Conventions
HTML Conventions
Anything else

sign_up.html

<!DOCTYPE html>

        <meta charset="UTF-8">
        <title>Sign Up</title>
        <link rel="stylesheet" type="text/css" href="styles/main.css">
        <link rel="stylesheet" type="text/css" href="styles/dropdown.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

            <form action="sign_up.php" method="post">
                <input id="form_input"
                       type="text"
                       placeholder="First Name"
                       name="firstname">

                <input id="form_input"
                       type="text"
                       placeholder="Last Name"
                       name="lastname">

                <input id="form_input"
                       type="text"
                       placeholder="Age"
                       name="age">

                <input id="form_input"
                       type="text"
                       placeholder="Email"
                       name="email">

                <input id="form_input"
                       type="password"
                       placeholder="Password"
                       name="password">

                <button id="signup_button" type="submit" disabled>Sign Up</button>
            </form>

sign_up.php

<?php

include 'config/database.php';

try {

    $conn = new PDO("mysql:host=$DB_HOST;dbname=Database", $DB_USER, $DB_PASSWORD);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $firstname = filter_var($_POST["firstname"], FILTER_SANITIZE_STRING);
    $lastname = filter_var($_POST["lastname"], FILTER_SANITIZE_STRING);
    $email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
    $password = filter_var($_POST["password"], FILTER_SANITIZE_STRING);

    #No need to hash name and email, can be used to check duplicates
    $password_hash = password_hash($password, PASSWORD_DEFAULT);

    $stmt = $conn->prepare("INSERT INTO Users (FirstName, LastName, Email, Password) VALUES (:firstname, :lastname, :email, :password)");
    $stmt->bindParam(':firstname', $firstname);
    $stmt->bindParam(':lastname', $lastname);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':password', $password_hash);
    $stmt->execute();

    header("Location: signed_up.html");

} catch(PDOException $e) {

    echo "Connection failed: " . $e->getMessage();

}

?>


texts: Presenting and passing data to a modal view controller without using prepare(for:sender:) method

texts:
I am using a toolbar button to present a modal view controller (in which I let the user export data as a PDF file). The main section of my app is a UITableViewController subclass embedded in a UINavigationController.

Here is a schematic of my layout.

The modal itself is embedded in a UINavigationController as I need it to have a bottom toolbar. It also has a transparent background and is presented using .overCurrentContext, so the main screen of the user's data blurs underneath.

I found that to get it to float over everything else (including the navigation bar etc), I had to present it from the UINavigationController (otherwise the main navigation bar and toolbar appeared on top of it).
The problem with this is that the UITableViewController method prepare(for:sender:) is not called.

I call the segue to the modal view controller like this (from the UITableViewController subclass):

// User taps EXPORT button
@objc func exportButtonTapped(_ sender: UIBarButtonItem) {
    self.navigationController?.performSegue(withIdentifier: "showExport", sender: nil)
}

In order to transfer the array of user data to the modal view controller, I have called the following code in the modal view controller:

override func viewDidLoad() {
    super.viewDidLoad()
    // Get data from array in main table view controller
    let masterNav = navigationController?.presentingViewController as! UINavigationController
    let myTableVC = masterNav.topViewController as! MyTableViewController
    self.userData = myTableVC.userData // This is of type: [MyObject]
}

The data is then rendered to a PDF (using HTML templating) in the modal view controller's viewWillAppear() method. This works as expected.

However, I have some concerns about doing it this way:

Is it guaranteed that viewDidLoad() will finish before viewWillAppear() is called? Will an even a larger data set be available for rendering as PDF in viewWillAppear()?
Is it acceptable to present modally from the UINavigationController?
Should I be subclassing the main UINavigationController and using its prepare(for:sender:) method (if this is even an option)?
In the performSegue(withIdentifier:sender:) method, does the sender argument make any difference?
Is it preferable to use present() rather than a segue?

I would of course be grateful for any other advice or refinements to the code. It seems to work as expected but I just want to make sure I following best practice as far as possible.


texts:
I am attempting to follow the "Let's build a compiler" tutorials, in order to build a basic language compiler for a custom assembly language.

Currently the specs of my compiler are:

  Accepts a single positive integer of any length as input
  Accepts two single digit numbers† with a "+" or "-" in between them as input
  If the program was just an integer, then generate assembly to push it to the stack
  If the program contained addition or subtraction, then generate assembly to find the sum, and push it to the stack

In the future, I would like to expand the compiler, by adding other operators, logic, variables, and more. So the question is, how can I improve my compiler in such a way, that it will be easier to expand, and add new features to in future?

Here is the code:

def runProgram(program):
    if "+" in program:
        index = program.index("+")
        try:
            return "PUSH " + program[index-1] + "\nPUSH " + program[index+1] + "\nADD"
        except:
            #"INVALID DIGITS FOR ADDITION"
            return "FAIL"
    elif "-" in program:
        index = program.index("-")
        try:
            return "PUSH " + program[index-1] + "\nPUSH " + program[index+1] + "\nSUB"
        except:
            #"INVALID DIGITS FOR SUBTRACTION"
            return "FAIL"
    else:
        try:
            int(program)
        except:
            #INVALID PROGRAM NOT INTERGER
            return "FAIL"
        return "PUSH " + program

testPrograms = ["3", "19", "5+2", "5-2", "birds", "12+23"]
for x in testPrograms:
    print(x + ":\n" + runProgram(x))

Output in this test:

3:
PUSH 3
19:
PUSH 19
5+2:
PUSH 5
PUSH 2
ADD
5-2:
PUSH 5
PUSH 2
SUB
birds:
FAIL
12+23:
PUSH 2
PUSH 2
ADD
Press any key to continue . . .

† NOTE: The program will just add the two digits on either side of the operand


texts: Reading and classifying lines from a file

texts:
I'm new to C which I'm learning in university now, and I'm not sure if the following is considered in C good practices or not.

For an assignment in a simple Classification problem I wrote the following code:

int train(FILE *file, const dim dim, const int m, vector *linearSeparator)
{
    size_t len = 0;
    char *line;
    bool stop = false;

    Sample *sample = NULL;
    for(int i = 0; i < m && getline(&line, &len, file) != -1 && !stop; i++)
    {
        if (strToSample(line, SEPARATOR, dim, sample) != EXIT_SUCCESS ||
            calibrateLinearSeparator(sample, linearSeparator, dim) != EXIT_SUCCESS)
        {
            printError(TRAINING, "Failed processing line");
            stop = true;
        }
        free(line);
        freeSample(sample);
    }
    line = NULL;
    return stop ? EXIT_FAILURE : EXIT_SUCCESS;
}

The functions:

strToSample gets a char* line and constructs a Sample object out of it, setting the *sample pointer. It returns EXIT_SUCCESS if succeeded and an error code if failed.
calibrateLinearSeparator uses the sample set by the previous function to manipulate the linearSeparator object (vector - a custom struct to represent a double[]). This function too returns EXIT_SUCCESS if succeeded and an error code if failed.

Both these functions print details about their internal errors and at this level I'm printing in what sample id that internal error occurred.

My question is:

Is this way of writing the if statement a good practice? Is it readable? Should I do it differently?

It feels as if these functions are having side effects. They get pointers to set but are used in an if statement to check if they succeeded in a way that order matters.

The idea behind writing it this way is that if either of these functions fail I want to print an error and not continue the loop. If the first one fails then the second is not performed (which is what I want). In any case of success or failure I want to free the allocated memory.
Is it a good practice in C to write the for condition the way I did? Having 3 conditions in it? (checking i, trying to bring next line and !stop?) It seems to me that this helps making the code cleaner as there are less "details" lines (of increasing i++ for example if the loop was a while loop on getLine)


texts: leetcode 230: kth smallest element in a Binary Search Tree

texts:
Here is my implementation: the goal of the algorithm is to find the kth smallest element in a BST.

class Solution(object):
    def kthSmallest(self, root, k):
        """
        :type root: TreeNode
        :type k: int
        :rtype: int
        """
        count = 1
        stack = []
        while True:
            if root:
                #keep traversing until we hit leftmost node in tree
                stack.append(root)
                root = root.left
            else:
                curr = stack.pop()
                if count == k:
                    return curr.val
                count+=1
                root = curr.right

I am having a hard time analyzing the complexity. The solution states how it is O(k+h) for both time and space, where h is the height of tree. For example, I do not understand why the space complexity isn't just O(h), assuming h is the height of the entire tree. Once you find the smallest item, its nodes right subtree could contain be of height>=height previously found between the smallest node and the root, so shouldn't the space be O(h*k), where h is the max depth between a subtrees leftmost child(smallest value) and its root?


texts:
My Objective is to create a fixed column array object of size 4 columns so that I can form a 2d array in the following format which I can use to print to a pdf later. The solution is working fine.
But need help in figuring out if there is a better way to implement this.

let input = [{
  id:123,
  quantity: 4,
  value: "xxxx"
},
{
  id:234,
  quantity: 11,
  value: "xxxx"
},
{
  id:345,
  quantity: 1,
  value: "xxxx"
}]

output = [
    [ objHeader, objValue, objValue, objValue ],
    [ objValue, null, null, null ],
    [ objHeader1, objValue1, objValue1, objValue1 ],
    [ objValue1, objValue1, objValue1, objValue1 ],
    [ objValue1, objValue1, objValue1, objValue1 ],
    [ objHeader2, objValue2, null, null ]
]

where objHeader = {
    id:123,
    header:true,
    quantity: 4,
    value: xxxx
}
objValue = {
    id:123,
    header:false,
    quantity: 4,
    value: xxxx
}

objValue is basically input[0].id repeated by the factor present in quantity

objValue1 is input[1].id repeated by the factor present in quantity and so on

Code that is working now

let input = [
  {
    id: 123,
    quantity: 4,
    value: "x1xxx"
  },
  {
    id: 234,
    quantity: 11,
    value: "x2xxx"
  },
  {
    id: 345,
    quantity: 1,
    value: "x3xxx"
  }
];

class RowRecord {
  constructor(rowsize = 4) {
    this.items = Array(rowsize).fill(null);
    this.rowSize = rowsize;
  }

  push(item) {
    const currentSize = this.size();
    // console.log(currentSize);
    if (currentSize < this.rowSize) {
      // replace 1 items with null based on index
      this.items.splice(currentSize, 1, item);
    }
  }

  get() {
    return this.items;
  }

  size() {
    return this.items.filter(item => item !== null).length;
  }
}

function getRecords() {
  const records = [];
  let rows = new RowRecord(); // fill default
  // create records
  for (let i = 0; i < input.length; i++) {
    // check for pending rows
    if (i !== 0 && rows.size() > 0) {
      records.push(rows);
    }
    // initiate new row
    rows = new RowRecord();
    const item = input[i];
    const quantity = parseInt(item.quantity, 10);
    const value = item.value;
    // +1 for printing the label for each product
    for (let j = 0; j <= quantity; j++) {
      if (j === 0) {
        // title label
        rows.push({
          id: item.id,
          header: true,
          quantity,
          value
        });
      } else {
        if (rows.size() === 4) {
          records.push(rows);
          rows = new RowRecord();
        }
        rows.push({
          id: item.id,
          header: false,
          quantity,
          value
        });
      }
    }
  }
  // push any pending rows
  records.push(rows);
  return records;
}

console.log(getRecords());


texts: Toggle between two options in an object?

texts:
I have just written this code for toggling between two possible states of an object.

// This can't change, and only ever has two states
const MODES = {
  ON: 'ON',
  OFF: 'OFF'
}

// this is my method
const toggleMode = currentMode => {
  const modes = Object.values(MODES);
  const newMode = modes.find(mode => mode !== currentMode);
  return newMode
};

// toggle state
const firstState = MODES.ON;
console.log(firstState);
const secondState = toggleMode(firstState);
console.log(secondState);
const thirdState = toggleMode(secondState)
console.log(thirdState)

Is this a good way of toggling between the two states? I'm concerned it's less readable than the verbose if one state, then pick the other, else pick the current state. But this way also means that so long as we know there's only two states, then nothing needs to be hardcoded in the function doing the toggling.


texts:
I came up with the following for a blocking and non-blocking queue implementation. Please suggest any changes/improvements/gotchas.

Blocking queue:

public class BlockingQueue<T> {
    List<T> buffer;
    private static final int MAX_QUEUE_SIZE = 100;

    public BlockingQueue() {
        buffer = new ArrayList<T>();
    }

    public void enqueue(T value) throws InterruptedException {
        synchronized(buffer) {
            while(buffer.size() >= MAX_QUEUE_SIZE) {
                buffer.wait();
            }
            buffer.add(value);
            buffer.notifyAll();
        }
    }

    public T dequeue() throws InterruptedException {
        synchronized(buffer) {
            while(buffer.size() < 1) {
                buffer.wait();
            }
            T value = buffer.remove(0);
            buffer.notifyAll();
            return value;
        }
    }

}

Non-blocking queue:

public class NonBlockingQueue<T> {
    List<T> buffer;
    private static final int MAX_QUEUE_SIZE = 100;
    private AtomicBoolean mutex = new AtomicBoolean(false);

    public NonBlockingQueue() {
        buffer = new ArrayList<T>();
    }

    public void enqueue(T value) throws InterruptedException {
        while(true) {
            while(!mutex.compareAndSet(false, true)) {
                Thread.sleep(100);
            }
            if(buffer.size() < MAX_QUEUE_SIZE) {
                buffer.add(value);
                mutex.set(false);
                return;
            } else {
                mutex.set(false);
            }
        }

    }

    public T dequeue() throws InterruptedException {
        T value = null;
        while(true) {
            while(!mutex.compareAndSet(false, true)) {
                Thread.sleep(100);
            }
            if(buffer.size() > 0) {
                value = buffer.remove(0);
                mutex.set(false);
                return value;
            } else {
                mutex.set(false);
            }
        }
    }
}

I think by non-blocking it means that the thread should not be blocked, so I used the sleep method. Please let me know if it has any issues.


texts: Is this powershell code to install the linux sl command to PowerShell good?

texts:
I have the following code in my project that installs linux sl on Windows using PowerShell. Is there anything you'd improve about this?

param([String]$Profile, [Switch]$Help, [Switch]$Force)

$ErrorMessages = @()

$Payload = "
# <Code inserted by Install-Steam-Locomotive>
function Steam-Locomotive {wsl sl -e}
function Steam-Locomotive-Force {wsl sl}
# </>
"

$HelpText = "This script helps you use the tremendous `"sl`" program in Windows PowerShell.
Simply download the .ps1 file and execute it.
If the script finishes successfully, you can type Steam-Locomotive in PS to start the interruptable `"sl -e`".
Use Steam-Locomotive-Force to prevent interruption (`"sl`").
Example:
PS C:\Users\User\Downloads\> .\Install-Steam-Locomotive.ps1 -Profile $PROFILE
"

if ($Help) {
  Write-Output $HelpText
  exit
}

if (!(Get-Command -Name "wsl" -CommandType "Application" -ErrorAction SilentlyContinue)) {
  $ErrorMessages += @("You don't have WSL installed. Cannot continue.")
}

if (!$Profile) {
  $ErrorMessages += @("Please supply your profile location under -Profile. Cannot continue.")
  if (Get-Content $Profile -ErrorAction SilentlyContinue | Select-String "Steam-Locomotive") {
    $ErrorMessages += @("Your profile seems to already contain something called `"Steam-Locomotive`". Use -Force to continue anyways.")
  }
}

if ((Get-Command -Name "Steam-Locomotive" -ErrorAction SilentlyContinue) -and !$Force) {
  $ErrorMessages += @("It seems a command named `"Steam-Locomotive`" is already installed. Use -Force to continue anyways.")
}

if ($ErrorMessages) {
  Write-Output $ErrorMessages
  exit
}

if (!(wsl command -v sl)) {
  Write-Output "First, install SL."
  wsl sudo apt install sl
}

if (wsl command -v sl) {
  Add-Content $Profile $Payload
  Write-Output "Done!"
}

if (!(wsl command -v sl)) {
  Write-Output "Failed installing wsl."
  exit
}

Write-Output "Success!"

EDIT: I had an error in this script. The fixed version is up on the project page.


texts: Store all entries in a single array from Wufoo api

texts:
What I'm doing

Using the v3 of the Wufoo API to retrieve all entries and store in a single array ($all_results). This is necessary because of the limit 100 entries limit that Wufoo has when retrieving results from their api. Also store the results to file we don't get slow responses from the api.

My question

I'm wondering how I could have done this better. This code is working for me, but I want to get better.

<?php

function wufoo_entries() {
    // Setup
    $account = "ACCOUNT_NAME";
    $form = "FORM_NAME";
    $api_key = "YOUR-API-KEY";
    $offset = 0;
    $limit = 100;
    $check_int = 15 * 60; // 15 minutes
    $file_time = 'time_stamp.txt';
    $file_results = 'results.txt';

    // Check for the last update
    $time = time();
    $time_stamp = file_get_contents($file_time);
    $refresh = ($check_int < ($time - $time_stamp)) ? TRUE : FALSE;

    // if true run functions and save the results and time to file else pull the results from file
    if ($refresh) {
        // Initialize our results array
        $all_results = array();

        // How many entries are there?
        $api_uri = "https://$account.wufoo.com/api/v3/forms/$form/entries/count.json";
        $count = wufoo_api($api_key, $account, $form, $offset, $limit, $api_uri);
        $counted = $count['EntryCount'];

        // Call the api as many times as needed
        for ($offset = 0; $offset <= $counted - 1; $offset = $offset + $limit) {
            $results = wufoo_api($api_key, $account, $form, $offset, $limit);
            // Limit the number of times we put results into the $all_results array to make sure we don't end up with blanks
            $limit = (($offset + $limit) > $counted) ? $limit - (($offset + $limit) - $counted) : $limit;

            // Iterate through the Entries array to push each entry into a new array
            for ($result = 0; $result <= $limit - 1; $result++) {
                array_push($all_results, $results['Entries'][$result]);
            }
        }

        // Save the new results (as json) and time to file
        $all_results_file = json_encode($all_results);
        file_put_contents($file_results, $all_results_file);
        file_put_contents($file_time, $time);
        return $all_results;
    }

    else {
        // Pull the results array from file
        $all_results = file_get_contents($file_results);
        $all_results = json_decode($all_results, true);
        return $all_results;
    }
}

function wufoo_api($api_key, $account, $form, $offset, $limit, $api_uri = NULL) {
    // Check to see if it's asking for the the entries or the total count
    $api_url = ($api_uri) ? $api_uri : 'https://' . $account . '.wufoo.com/api/v3/forms/' . $form . '/entries.json?pageStart=' . $offset . '&pageSize=' . $limit;

    $curl = curl_init($api_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERPWD, $api_key . ':footastic');
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_USERAGENT, 'Wufoo Sample Code');
    $response = curl_exec($curl);
    $resultStatus = curl_getinfo($curl);

    if ($resultStatus['http_code'] == 200) {
        $results = json_decode($response, true);
        return $results;
    } else {
        $results = 'Call Failed '.print_r($resultStatus, true);
        return $results;
    }
}

// Show me some results and make sure it works
echo "<pre>";
print_r(wufoo_entries());
echo "</pre>";

?>


texts: Export an Excel file using Laravel-Excel

texts:
I have this piece of code that is used to export an Excel file using Laravel-Excel. But I have doubts about it for the long run. Can the server handle it if there are a hundred requests?

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Arr;

class DailyGeneralSheetExport implements FromCollection, WithHeadings
{
    use Exportable;

    public function __construct(array $request, int $diid, int $igid, str $date)
    {
        $this->request = $request;
        $this->diid = $diid;
        $this->igid = $igid;
        $this->date = $date;
    }

    public function collection()
    {
        $dt = Carbon::parse($this->date);
        $endDt = Carbon::parse($this->date)->lastOfMonth();
        $period = CarbonPeriod::create($this->date, $endDt);
        $ipid = array_pluck($this->request, 'ip_id');
        $inidArr = array_pluck($this->request, 'in_id');
        $inid = array_unique($inidArr);
        $collection = [];
        foreach ($period as $key => $eachDate) {
            $reading = DB::table('instrument')
                    ->where('iv_inid', $inid)
                    ->whereIn('iv_ipid', $ipid)
                    ->whereDate('iv_date', $eachDate)
                    ->whereIn('iv_status', ['N', 'Y', 'A'])
                    ->orderBy('iv_date')->get()->toArray();
            $row = array($eachDate->format('d/m/Y'));
            foreach ($reading as $columnKey => $columnValue) {
                array_push($row, $columnValue->iv_reading);
            }
            array_push($collection, $row);
        }

        return collect($collection);
    }

    /**
     * @return array
     */
    public function headings(): array
    {
        $sheet_header = Arr::pluck($this->request, 'ip_label');

        return Arr::prepend($sheet_header, 'Date');
    }
}

As you can see I have to create an array for one month and this makes me worry if this will impact the performance. And for some reason, I feel I wrote code that is not elegant as Laravel does. How do I optimize it?


texts: Verifying the Entity Data Model schema against a production database

texts:
We have an entity data model (database first) for a legacy product we are migrating to .Net from Delphi. We recently deployed to a server where some of the tables had been modified and columns deleted. The results were catastrophic. I'm working on a way of validating the EDM schema against the live SQL server database.

public Validation(ADbContext db)
{
    _connectionString = db.Database.Connection.ConnectionString;
}

private readonly string _connectionString;

public ILookup<string, List<string>> Run()
{
    // A tolerance to deal with Entity Framework renaming
    var modelValidation = GetModelProperties<ADbContext>(tolerance);
    var isValid = !modelValidation.Any(v => v.Any(x => x.Count > 0));
    if (!isValid)
        Logger.Activity(BuildMessage(modelValidation));
    return modelValidation;
}

public string BuildMessage(ILookup<string, List<string>> modelValidation)
{
    // build a message to be logged
}

public List<string> GetMissingColumns(IEnumerable<string> props, IEnumerable<string> columns, int tolerance)
{
    // compare whether the entity properties have corresponding columns in the database
    var missing = props.Where(p => !columns.Any(c => p.StartsWith(c) && Math.Abs(c.Length - p.Length) <= tolerance)).ToList();
    return missing;
}

public string[] GetSQLColumnNames(Type t)
{
    SqlConnection connection = new SqlConnection(_connectionString);
    var table = t.Name;
    DynamicParameters dparams = new DynamicParameters();
    dparams.Add("Table", table);
    var query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @Table ";
    // Using dapper to retrieve list of columns from that table
    List<string> columns = connection.Query<string>(query, dparams).ToList();
    return columns.ToArray();
}

static string[] GetEntityPropertyNames(Type t)
{
    var properties = t.GetProperties(BindingFlags.Instance | BindingFlags.Public)
            .Where(p => p.CanRead && !p.PropertyType.FullName.Contains("My.Namespace") && !p.PropertyType.FullName.Contains("Collection"))
            .Select(p => p.Name)
            .ToArray();
    // these conditions excludes navigation properties: !p.PropertyType.FullName.Contains("My.Namespace") && !p.PropertyType.FullName.Contains("Collection")
    return properties;
}

ILookup<string, List<string>> GetModelProperties<T>(int tolerance, T source = default(T))
{
    var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)
            .Where(p => p.PropertyType.IsGenericType)
            .Select(p => p.PropertyType.GetGenericArguments()[0])
            .Select(p => new
            {
                Entity = p.Name,
                Properties = GetEntityPropertyNames(p),
                Columns = GetSQLColumnNames(p),
            })
            .ToArray();

    return properties.ToLookup(p => p.Entity, p => GetMissingColumns(p.Properties, p.Columns, tolerance));
}

Is there a better way?
Does Entity Framework already provide a mechanism for achieving this?
Would this be better in the data layer or in another assembly?
Is adding Dapper (just for this purpose) overkill, does it add unnecessary complexity to the project?

Update
I've created a Github repository with a library I've been using that implements this. To minimise dependencies, I've used reflection in favour of Entity specific features.


texts: Filling text boxes with player names received from an array

texts:
I'm currently trying to make the code of an app I'm developing a bit more efficient and easier to read. Basically what this does is after the user taps with two fingers (gesture) it retrieves a previously saved array from NSUserDefaults of player names, and fills in the 6 text boxes (tagged 6-11) on a table view with these names. If there isn't an existing array it'll use a default set of names.

I'm trying to reduce the length of a long for/if statement.  Any ideas for simplifying this code would be appreciated.

if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        NSMutableArray *names = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"nameArray"]];
        for (int i = 0; i <= 5; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            UITableViewCell *cell = [playerTable cellForRowAtIndexPath:indexPath];

            for (UIView *view in cell.contentView.subviews) {
                if ([view isKindOfClass:[UITextField class]]) {
                    UITextField *txtField = (UITextField *)view;
                    if (txtField.tag == 6) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:0]; }
                        else {
                            txtField.text = @"Peter";
                        }
                    }
                    if (txtField.tag == 7) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:1]; }
                        else {
                            txtField.text = @"Julia";
                        }
                    }
                    if (txtField.tag == 8) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:2]; }
                        else {
                            txtField.text = @"Durgan";
                        }
                    }
                    if (txtField.tag == 9) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:3]; }
                        else {
                            txtField.text = @"Bob";
                        }
                    }
                    if (txtField.tag == 10) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:4]; }
                        else {
                            txtField.text = @"Iseland";
                        }
                    }
                    if (txtField.tag == 11) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:5]; }
                        else {
                            txtField.text = @"Player";
                        }
                    }
                }
            }
        }
        [self saveNames];
    }

Edit: I've received an answer elsewhere and it greatly reduces the length as well as making it more readable:

NSArray *defaultNames = @[@"Peter", @"Julia",...];
int offsetIndex = 6;
BOOl needCustomNames = [[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"];
for (UIView *view in cell.contentView.subviews)
{
    if ([view isKindOfClass:[UITextField class]])
    {
        UITextField *txtField = (UITextField *)view;
        int index = [txtField tag]-offsetIndex;
        if (txtField.tag >= 6 && txtField.tag <= 11)
        {
            if (needCustomNames)
                txtField.text = [names objectAtIndex:index];
            else
                txtField.text = [defaultNames objectAtIndex:index];
        }
    }
}


texts:
After finishing up finals, I wasn't really working on coding for a while. I decided to write a card game to get back into the programming scene.  I'm just wondering if there's anything that I missed or could be improved upon.

import random

suit_choices = ("Diamonds", "Hearts", "Clubs", "Spades")
suit_picked = random.choice(suit_choices)

number_picked = random.randint(2,9)

while True:
    print suit_choices
    user_card = raw_input('\nEnter a suit to pick from. Your choices are listed above. ').title()

    if user_card == suit_picked:
        print '\nYou guessed the suit type correct!'
        user_number = input('\nNow pick a number card (2-9) ')
        if user_number == number_picked:
            print '\n You guessed the card number correct!'
            break
        else:
            print '\nShucks. Try again.'
            continue
    else:
        print '\nOH NO'
        continue


texts:
I was trying to figure out what a minimal ECS might look like in Lua; here's what I came up with. This is based very loosely on darkf/microecs.

local function createSystem(components, process)
  local function handle(entity)
    for i, component in ipairs(components) do
      if entity[component] == nil then return end
    end
    process(entity)
  end

  return function(entities)
    for i, entity in ipairs(entities) do
      handle(entity)
    end
  end
end

local function addComponent(entity, component, instance)
  local meta = { __index = component }
  entity[component] = setmetatable(instance or {}, meta)
  return entity
end

local function createEntity(instance)
  local meta = { __index = { addComponent = addComponent } }
  return setmetatable(instance or {}, meta)
end

return {
  createEntity = createEntity,
  addComponent = addComponent,
  createSystem = createSystem,
}

Here's how you could use it:

local ecs = require 'ecs'

local thing = ecs.createEntity()

local positionComponent = { x = 0, y = 0 }
local velocityComponent = { vx = 0, vy = 0 }

thing:addComponent(positionComponent, { x = 50, y = 60 })
thing:addComponent(velocityComponent, { vx = 5, vy = 6 })

local velocitySystem = ecs.createSystem(
  { positionComponent, velocityComponent },
  function (entity)
    local pos = entity[positionComponent]
    local vel = entity[velocityComponent]
    pos.x = pos.x + vel.vx
    pos.y = pos.y + vel.vy
  end)

Interestingly, the entities and components aren't nearly as important as the systems. The createEntity function only exists so that you can use addComponent in an object-oriented way, but you can just as easily write it like this:

local thing = {}
-- ...
ecs.addComponent(thing, positionComponent, { x = 50, y = 60 })
ecs.addComponent(thing, velocityComponent, { vx = 5, vy = 6 })

You can also ignore addComponent if you don't care about the "safety" of having "typed" components.

local thing = {
  position = { x = 50, y = 60 },
  velocity = { vx = 5, vy = 6 },
}

local velocitySystem = ecs.createSystem(
  { 'position', 'velocity' },
  function (entity)
    local pos = entity.position
    local vel = entity.velocity
    pos.x = pos.x + vel.vx
    pos.y = pos.y + vel.vy
  end)

This means systems are useful on arbitrary data structures, with the table representing the entity and its properties representing components. Keying properties with strings like this seems more idiomatic from a Lua standpoint.

To have a system process entities, simply call the function returned from createSystem, passing it a table of entities to process. Only the entities having the components specified in the first argument to createSystem will be processed.

function tick()
   velocitySystem({ thing, anotherThing })
   anotherSystem({ thing, somethingElse })
end

All comments are welcome, but I'm more interested in hearing about the implementation itself than stylistic choices (like whether I should have used _ in place of i, or 4 space indents instead of 2).


texts: jQuery .with() to allow better chaining and flow

texts:
What are your guys thoughts on the following jQuery plugin?

$.fn.with = function( fn ) {
    // Verify function
    if( jQuery.isFunction( fn ) ) {
        var temp = fn.call(this);

        // Check fn for valid response
        if( temp instanceof jQuery ) {
            return temp;
        }
    }
    return this;
};

I believe this method to very useful as it allows for better chaining, thus increasing flow. Before developing this method, I found myself creating a lot of temporary variables just to preform a conditional test.

Examples would be in cases where .hasClass and .is() are used:

// Example From: http://api.jquery.com/is/

$("ul").click( function( event ) {
    var $target = $(event.target);

    if ( $target.is("li") ) {
        $target.css("background-color", "red");
    }
});

However, using the proposed .with() method, the code can become cleaner:

$("ul").click( function( event ) {
    $(event.target).with(function() {
        if( this.is("li") ) {
            this.css("background-color", "red");
        }
    })
});

Note: If the function processed by .with() returns an instance of jQuery it, instead of the calling instance, will be returned into the chain.


texts:
Is there a faster, more efficient way to read a text file than this implementation?

Taking into account phones capabilities:

    dictionary = new ArrayList<String>();
    long start = System.currentTimeMillis();
    int count = 0;
    try{
        InputStream inputStream = context.getAssets().open("words.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String word;
        while((word = bufferedReader.readLine()) != null){
            dictionary.add(word);
            count++;
        }
        inputStream.close();
        inputStreamReader.close();
        bufferedReader.close();

    }catch(IOException e){
        e.printStackTrace();
    }
    long end = System.currentTimeMillis();
    double t = ((end - start) / 1000.0);
    System.out.println("Time to read database from file " + count + " items " + t + " seconds");

Output:

  Time to read database from file 272403 items 1.112 seconds

Update:
After taking @rolfl advice into account and doing a little more digging this is what I came up with. Any further advice or a tidy up would be very welcome

    dictionary = new ArrayList<>(300000);
    long start = System.currentTimeMillis();
    InputStream inputStream = null;
    try{
        inputStream = context.getAssets().open("words.txt");
    }catch(IOException e){
        e.printStackTrace();
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] buff = new byte[1048576];
    try{
        for(int i; (i = inputStream.read(buff)) != -1; ){
            byteArrayOutputStream.write(buff, 0, i);
        }
    }catch(IOException ex){
        ex.printStackTrace();
    }
    String[] contents = byteArrayOutputStream.toString().split("\n");
    for(int i = 0; i < contents.length; i++){
        dictionary.add(contents[i]);
    }
    long end = System.currentTimeMillis();
    double t = ((end - start) / 1000.0);
    System.out.println("Time to read database from file " + dictionary.size() + " items " + t + " seconds");

Output:

  Time to read database from file 272403 items 0.708 seconds


texts: Interpreting strings with C-style escapes

texts:
I am working on a compiler that does a pretty heavy work on strings in order to get them correctly displayed. So far I have an algorithm that transforms a given string like (In plaintext):

"This is a \"string\""

After matching the complete token I get something like the following (C string):

"This is a \\\"string\\\""

And the same goes for hexadecimal literals and common escape characters (\r \n \t \b), the code I've came is this one:

#include <stdint.h>

int is_hex(int c) {
  return (is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}

static char get_nibble(char T) {
  if (T >= 'A' && T <= 'F')
    return (T - 'a') + 10;
  else if (T >= 'a' && T <= 'f')
    return (T - 'A') + 10;
  return (T - '0');
}

char *post_string(char *in, uint32_t *len, const char **err) {
  if (!in || !len)
    return NULL;

  char *out = in, *tmp = in; uint32_t _len = 0; const char *_err = NULL;
  char esc = 0;
  while (in && *in && !_err) {
    if (*in == '\\') {
      ++in;
      switch (*in) {
        case '"': esc = '"'; ++in; break;
        case 'r': esc = '\r'; ++in; break;
        case 'n': esc = '\n'; ++in; break;
        case 't': esc = '\t'; ++in; break;
        case 'b': esc = '\b'; ++in; break;
        case 'x':
        case 'X': {
          ++in;
          if (!is_hex(*in) || !is_hex(in[1]))
            _err = "Invalid character on hexadecimal escape.";
          if (_err)
            break;
          esc = ((get_nibble(*in) << 4) | (get_nibble(in[1]))) & 0xff; in += 2;
        } break;
        case '\'': esc = '\''; ++in; break;
        case '\\': esc = '\\'; ++in; break;
        case 0:
        default:
          _err = "Unexpected '\\' with no escape sequence."; break;
      }
      if (!_err)
        tmp[_len] = esc, ++_len;
    }
    else {
      tmp[_len] = *in; ++in, ++_len;
    }
  }

  out[_len] = 0;  /* Set the end of string. */
  *len = _len;    /* Set the final string length. */
  if (err)        /* Set error message back. */
    *err = _err;
  return out;
}

  On above implementation I did make sure that a null terminated string is passed to the implementation.

There are no requirements but would be great if no allocation gets done on this step, also a standard implementation should be achieved (Not sure if mine is standard though).

Below is a test main:

#include <stdlib.h>

int main(void) {
  char t[] = "This is a \\\"string\\\"";
  uint32_t len = 0;
  printf("Before: %s\n", t);
  post_string(t, &len, NULL);
  printf("After : %s\n", t);
  return 0;
}

How can I improve this code?


texts: Productivity/Timer app that uses twilio to send SMS-reminders

texts:
I wrote a timer app in python using both twilio and the timer module; sends an sms to my phone after a certain amount of time has passed. While it works without a doubt, I wanted to gather some ideas/opinions on how to improve my program for efficiency, readability, etc...

I'm open to all suggestions.

import time
from twilio.rest import Client

client = Client("KEY", "KEY")

work_time = 20 * 60
break_time = 5 * 60

def _timer(workState = True):
    messageBody = ["Time to work", "Time to break"]
    if workState == True:
        client.messages.create(to="MYNUMBER", from_="TWILIONUMBER", body="Time To Work!")
        time.sleep(work_time)
        _timer(not workState)
    else:
        client.messages.create(to="MYNUMBER", from_="TWILIONUMBER", body="Time To Break!")
        time.sleep(break_time)
        _timer(not workState)
_timer(False)

Please note: Several values have been changed for privacy sake (e.g. MYNUMBER, TWILIONUMBER, KEY)


texts:
Description:

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

Note: A leaf is a node with no children.

Leetcode

Code:

class Solution {
    private List<List<Integer>> paths = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        traverse(root, sum, new ArrayList<Integer>());
        return paths;
    }

    private void traverse(TreeNode root, int sum, ArrayList<Integer> path) {
        if (root != null) {
            path.add(root.val);

            if (root.left == null && root.right == null && sum == root.val) {
                paths.add((ArrayList) path.clone());
            }

            traverse(root.left,  sum - root.val, path);
            traverse(root.right, sum - root.val, path);

            path.remove(path.size() - 1);
        }
    }
}


texts:
I'm using a document store that stores files and folders in a database and can output the contents (or part of the contents) as a serie of nested objects, like this:

var a = [
  {
    id: 0,
    children: [
      {
        id: 1,
        parent: 0,
        children: [
          {
            id: 2,
            children: []
          }
        ]
      }, {
        id: 3,
        parent: 0,
        children: []
      }, {
        id: 4,
        parent: 0,
        children: [
          {
            id: 5,
            parent: 4,
            children: [
              {
                id: 6,
                parent: 5,
                children: []
              }, {
                id: 7,
                parent: 5,
                children: []
              }, {
                id: 8,
                parent: 5,
                children: []
              }
            ]
          }
        ]
      }
    ]
  }
]

I'm displaying this and allowing users to interact with it in the browser using the HTML5 drag and drop API. When a user drags an element and drops it into another I want to change the object's position in the data. All I have available to use is the object to be moved and the destination object.

I've written a function that handles the move. It works by:

Finding the item to move, the original parent and the new destination.
Removing the item from the parent's children array.
Updating the parent on the item to move.
Pushing the item to the destinations children array.

Here's the code

function findItem (array, id) {
  var result
  array.some(item => {
    if (item.id === id) {
      result = item
      return true
    }
    if (item.children.length) {
      let subResult = findItem(item.children, id)
      if (subResult) {
        result = subResult
      }
    }
  })
  return result
}

function move (itemId, destinationId) {
  let itemToMove, parentItem, destinationItem
  itemToMove = findItem(a, itemId)
  parentItem = findItem(a, itemToMove.parent)
  destinationItem = findItem(a, destinationId)

  // Remove the item from the the parents childen
  var index
  parentItem.children.find((child, idx) => {
    if (child.id === itemToMove.id) {
      index = idx
      return true
    }
  })
  parentItem.children.splice(index, 1)

  // Update the parent of the item to move
  itemToMove.parent = destinationId

  // Push it to the new parent
  destinationItem.children.push(itemToMove)
}

move(3, 1)

Is there a better way of doing this?


texts:
Based on this question

Thought it was a nice puzzle

Main idea: How to randomly assign shifts to employees based on these restrictions:

As per the O.P.:

  Randomly write to cells "1" or "0"
  Employees' total shifts in a month can't be more than 7 and less than 4
  According to a few criteria like "Vacation" or "Definite shift demands"
  One employee per one day

This is my setup:

Link to current workbook

Note: Open to improvements

Code:

Option Explicit

Sub AssignAllShifts()

    Dim targetSheet As Worksheet

    Dim availableRange As Range
    Dim employeeRange As Range
    Dim previousRange As Range
    Dim emptyRange As Range

    Dim lastRow As Long
    Dim counter As Long

    Set targetSheet = ThisWorkbook.Worksheets("tablo")

    lastRow = targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Row

    ' For dev...
    targetSheet.Range("B2:AE" & lastRow).ClearContents
    targetSheet.Range("C2").Value = "V"
    targetSheet.Range("I2").Value = "V"
    targetSheet.Range("N2").Value = "V"
    targetSheet.Range("D3").Value = "V"
    targetSheet.Range("E3").Value = "V"
    targetSheet.Range("F4").Value = "V"

    For counter = 2 To lastRow

        ' Get days without shifts assigned
        Set availableRange = nonEmptyColsInRange(Range("B2:AE" & lastRow))

        Set employeeRange = Intersect(targetSheet.Range("B" & counter & ":AE" & counter), availableRange)

        ' Init random func
        Randomize

        AssignShiftsInRange employeeRange

    Next counter

End Sub

Private Sub AssignShiftsInRange(ByVal availableRange As Range)

    Dim cellAddrArray As Variant
    Dim randomCellPos As Variant

    Dim totalShifts As Long

    Dim counter As Long

    ' Get random value to assign to employee
    totalShifts = Int((7 - 4 + 1) * Rnd + 4)

    ' Store range cells' addresses in array
    cellAddrArray = rangeAddrToArray(availableRange)

    ' Get x num of cells positions in employe range
    randomCellPos = randomNumsToArray(totalShifts, 1, availableRange.Cells.Count)

    ' Choose x random cells addresses
    For counter = 0 To UBound(randomCellPos)

        ' Assign 1 to cell address
        availableRange.Parent.Range(cellAddrArray(randomCellPos(counter))).Value = 1

    Next counter

End Sub

Private Function nonEmptyColsInRange(ByVal sourceRange As Range) As Range
    Dim column As Range
    Dim result As Range

    For Each column In sourceRange.Columns

        If column.SpecialCells(xlCellTypeBlanks).Count = column.Cells.Count Then
            If result Is Nothing Then
                Set result = column
            Else
                Set result = Union(result, column)
            End If
        End If

    Next column

    Set nonEmptyColsInRange = result

End Function

Private Function rangeAddrToArray(ByVal sourceRange As Range) As Variant

    Dim cell As Range
    Dim result As Variant
    Dim counter As Long

    ReDim result(sourceRange.Cells.Count)

    For Each cell In sourceRange.Cells
        result(counter) = cell.Address
        counter = counter + 1
    Next cell

    rangeAddrToArray = result

End Function

Private Function randomNumsToArray(ByVal totalNums As Long, ByVal lowerLimit As Long, ByVal upperLimit As Long) As Variant

    Dim result As Variant
    Dim arrayPosition As Long
    Dim randomNum As Long

    ReDim result(totalNums - 1)

    If totalNums >= upperLimit Then
        Err.Raise 5, , "Minimum shifts is greater than available slots"
    End If

    Do
        randomNum = Int((upperLimit - lowerLimit) * Rnd + lowerLimit)
        If Not IsNumberInArray(randomNum, result) Then
            result(arrayPosition) = randomNum
            arrayPosition = arrayPosition + 1
        End If
    Loop While arrayPosition <= UBound(result)

    randomNumsToArray = result

End Function

Private Function IsNumberInArray(ByVal randomNumber As Long, ByVal targetArray As Variant) As Boolean

    IsNumberInArray = Not IsError(Application.Match(randomNumber, targetArray, 0))

End Function


texts: Use of concurrency to generate combinations

texts:
I have the following package to generate combinations from a slice of bytes:

//Package combinations generates combinations from a slice.
package combinations

//CombIter generates combinations of bytes of given length from a
//list of elements. It returns values by a channel outC
//An empty list indicates that no further combinations are available
func CombIter(elements []byte, length int, outC chan []byte) {
    for i := 0; i < len(elements); i++ {
        if length == 1 {
            outC <- []byte{elements[i]}
        } else {
            inC := make(chan []byte)
            go combIter(elements[i+1:], length-1, inC)

            for next := <-inC; len(next) != 0; next = <-inC {
                outC<- append([]byte{elements[i]}, next...)
            }
        }
    }
    outC <- []byte{}
}

//Comb collects combinations of length k from a byte slice l.
//It returns a slice containing (len(l) C k) slices all of length k
func Comb(l []byte, k int) [][]byte {
    c := make(chan []byte)
    go combIter(l, k, c)
    o := [][]byte{}
    for next := <-c; len(next) != 0; {
        o = append(o, next)
        next = <-c
    }
    return o
}

I test it with

func TestComb(t *testing.T)
    actual := Comb([]byte{'a', 'b', 'c', 'd'}, 2)
    expected := [][]byte{{'a', 'b'}, {'a', 'c'}, {'a', 'd'}, {'b', 'c'}, {'b', 'd'}, {'c', 'd'}}
    if !reflect.DeepEqual(actual, expected) {
        t.Errorf("Got  %v\nWant %v", actual, expected)
    }
}

The algorithm is an adaption of a python program: https://stackoverflow.com/a/2837693/2709093

I am particularly concerned with the use of concurrency, and the possibility of creating a disruptive number of goroutines. Is this a useful application of concurrency?


texts: Convert Tail-Call Recursive Code to Loop

texts:
So, I'm in the midst of writing a programming language interpreter in Python, and one of the problems I'm concerned about is that since I've been using a lot of recursions to implement the interpreter, I'll get a stack overflow if the size of the code is too large. For example, I have a piece of code that evaluates an and operation. Since the language I'm implementing is a logic programming language (like Prolog), it needs to try out all the options, so it consumes a generator and loops over it looking at the rest of the and operation. Behold:

def evaluate_and(and_clauses, binds={}):
    if and_clauses == []:
        yield binds
    else:
        head, *tail = and_clauses
        possible = evaluate(head, binds)
        for p in possible:
            yield from evaluate_and(tail, p)

The issue with this is that if the and operation is buried deep within the program, and the call stack is already large, then if the and has too many clauses the interpreter will crash.

If anyone could suggest a way to make this not possible, that would be amazing!

PS: I have an even bigger problem, which is that since recursion is the only way to do looping in a logic programming language if you loop more than a 100 times in my language, it creates a stack overflow in my interpreter.


texts: Improving time complexity of cheapest travel route algorithm

texts:
Below is the code to find the cheapest route for a passenger. stationsUnexplored is a Queue to which stations are added and examined. Once we are done with a station, it is removed from Queue and added to a Set stationsExplored. The purpose of stationsExplored is to avoid queuing of stations more than once. Before adding a station to stationsUnexplored I check if it has been explored earlier i.e  !stationsExplored.contains(adjacentStation.getKey(). Or it is already present in the queue i.e  !stationsUnexplored.contains(adjacentStation.getKey()). First contains() is on a Set, so it has time complexity of \$\mathcal{O}(1)\$. But second contains() is called on a Queue hence the time complexity is \$\mathcal{O}(N)\$.

Is there a way to get rid of the second check on queue or improve it to \$\mathcal{O}(1)\$?

 Queue<Integer> stationsUnexplored = new ArrayDeque<>();
 Set<Integer> stationsExplored = new HashSet<>();
 stationsUnexplored.add(1);

 while (!stationsUnexplored.isEmpty()) {
     int currentStation = stationsUnexplored.remove();
     Map<Integer, Integer> stations = stationsNFares.get(currentStation); // returns neighboring stations.
     if (stations != null) {
        Iterator<Map.Entry<Integer, Integer>> stationIterator = stations.entrySet().iterator();
        while (stationIterator.hasNext()) {
           Map.Entry<Integer, Integer> adjacentStation = stationIterator.next();

           // code for computing cheapest fare for current adjacent station.....

           if (!stationsExplored.contains(adjacentStation.getKey()) && !stationsUnexplored.contains(adjacentStation.getKey())) { //O(N)
              stationsUnexplored.add(adjacentStation.getKey());
           }
        }
     }
     stationsExplored.add(currentStation);
 }


texts: Searching in an array in less than O(n) time

texts:
I have an array where each element is either one less or one greater than the preceding element \$\{x_i = x_{i-1} \pm 1\}\$. I wish to find an element in it in less than \$O(n)\$ time. I've implemented it like this:

public int searchArray(int[] arr, int i, int elem) {

    if (i > arr.length - 1 || i < 0) {
        return -1;
    }

    if (arr[i] == elem) {
            return i;

    } else {
            int diff = Math.abs(elem - arr[i]);
            int index = searchArray(arr, i + diff, elem);
            if (index == -1) {
                index = searchArray(arr, i - diff, elem);
                if (index == -1) {
                    return -1;
                }
            }
            return index;
    }
}

And calling it like:

int[] arr = {2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 6, 5, 4, 3, 4};
int index = searchArray(arr, 0, 3);

It is working fine, but can it be improved? Specifically, is there a way to do it iteratively? And is the current algorithm less than \$O(n)\$. I guess it is, but I'm not sure.


texts: Rails - updating and deleting exactly 2 children of the parent model

texts:
Requirements

A Phrase can have many Translations, which are in turn a Phrase; thus, the relationship of Translation is self-referential. A single Translation is essentially a 1-to-1 mapping of two Phrases (what I call source_phrase and destination_phrase), though the Translation model as a whole is actually an M-N self-referential model (like a User following or blocking another User).

When a Translation is updated, both Phrases must be successfully validated and updated. If either of the two fails, the Translation update fails.

When a Translation is deleted, the source/destination phrase should only be deleted if it is not part of another Translation (if translations.count == 0, essentially). Here's my trimmed-down code to represent this relationship and update/delete actions:

Phrase

class Phrase < ActiveRecord::Base

  has_many :translations,
           class_name: "Translation",
           foreign_key: "source_id"
  has_many :source_phrases,
           through: :translations
  has_many :inverse_translations,
           class_name: "Translation",
           foreign_key: "destination_id"
  has_many :destination_phrases,
           through: :inverse_translations
end

Translation

class Translation < ActiveRecord::Base

  belongs_to :source_phrase,
             class_name: "Phrase",
             foreign_key: "source_id"
  belongs_to :destination_phrase,
             class_name: "Phrase",
             foreign_key: "destination_id"

  around_destroy :destroy_orphaned_phrases

  # Both source and destination must successfully update
  def update_source_and_destination_phrases(source, destination)
    Translation.transaction do
      source.save
      destination.save
    end
  end

  # Destroy orphaned phrases if they do not have any other translations
  def destroy_orphaned_phrases
    source_phrase = self.source_phrase
    destination_phrase = self.destination_phrase
    yield # executes the translation DELETE
    if source_phrase.translations.count == 0
      source_phrase.destroy
    end
    if destination_phrase.inverse_translations.count == 0
      destination_phrase.destroy
    end
  end
end

TranslationsController

def update

    @src = params[:translation][:source_phrase]
    @dst = params[:translation][:destination_phrase]

    @source_phrase = Phrase.find(@src[:id])
    @destination_phrase = Phrase.find(@dst[:id])

    # Assign attributes in controller before the transaction save in the model
    @source_phrase.assign_attributes(language: @src[:language], text: @src[:text])
    @destination_phrase.assign_attributes(language: @dst[:language], text: @dst[:text])

    if @translation.update_source_and_destination_phrases(@source_phrase, @destination_phrase)
      render json: @translation, serializer: CompleteTranslationSerializer
    else
      render json: @translation.errors, status: :unprocessable_entity
    end
end

def destroy

    @translation.destroy
end

Assume

That I am setting my variables correctly, that data is being validated appropriately, and that my request/response data/headers are fine.

What I need to know

Is this a good way of doing the update/delete when two child objects need to be handled by the parent?
Are my code style, variable names, etc. okay?

I've been developing in Rails for about two years part-time, but I've never had this exact requirement and I can't find any similar situations in open source/online, so I'm hoping that someone here will have some insight!


texts: Bag data structure (a set that can store multiple copies of items)

texts:
I am trying to make my class a bit cleaner, especially some of its methods like Count, __add__, remove and __iter__. I feel like I could use cleaner code on the above methods and get the same results.

Bag class represents and defines methods, operators, and an iterator for the Bag class. Bags are similar to sets, and have similar operations but unlike sets they can store multiple copies of items. Store the information in bags as dictionaries whose elements are keys whose associated values are the number of times the key occurs in the bag.

from collections import defaultdict
from goody import type_as_str
import copy
from collections import Counter

class Bag:

     def __init__(self, items = []):
         self.bag_value = defaultdict(int)
         for item in items:
             self.bag_value[item] += 1

     def __repr__(self):
         bag_list = []
         for item, count in self.bag_value.items():
             bag_list.extend(list(item*count))
         return 'Bag(' + str(bag_list) + ')'

     def __str__(self):
         return 'Bag(' + ','.join(str(item) + '[' + str(count) + ']' for item, count in self.bag_value.items()) + ')'

     def __len__(self):
         bag_len = 0
         for value in self.bag_value:
             bag_len += self.bag_value[value]
         return bag_len

     def unique(self):
         return len(self.bag_value)

      def __contains__(self, item):
          return item in self.bag_value

      def count(self, item):
          if item in self.bag_value:
             return self.bag_value[item]
          else:
             return 0

      def add(self, new):
          self.bag_value[new] += 1

      def __add__(self,right):
          mergedbag = Bag()
          mergedbag.bag_value = copy.copy(self.bag_value)
          for item in right.bag_value.keys():
              mergedbag.bag_value[item] += right.bag_value[item]
          return mergedbag

      def remove(self, item):
          if item in self.bag_value:
              if self.bag_value[item] == 1:
                  del self.bag_value[item]
              else:
                  self.bag_value[item] -= 1
          else:
              raise ValueError(str(item) + ' not in bag')

      def __eq__(self, right):
          if type(right) is not Bag:
              raise TypeError('Cannot compare Bag with' + type_as_str(right) + '. Can only compare Bag with Bag')
          else:
              return (Counter(self.bag_value)==Counter(right))

      def __ne__(self, right):
          return not self.__eq__(right)

      def __generate__(self, bag_value):
          for item in self.bag_value:
              for value in range(self.bag_value[item]):
                  yield item

      def __iter__(self):
          return self.__generate__(self.bag_value)

if __name__ == '__main__':
#     bag = Bag(['d','a','b','d','c','b','d'])
#     bag2 = Bag(['d','a','b','d','c','b','d'])
#     bag3 = Bag(['d','a','b','d','c','b'])
#     print(bag == bag2)
#     print(bag == bag3)
#     print(bag != bag2)
#     print(bag != bag3)
    import driver
    driver.driver()


texts: Better to use string concatenation using std::string::operator+() or std::string::reserve() and std::string::append()?

texts:
I'm doing some code review and I saw something like the following:

std::ifstream dataFile(filename);
if (dataFile.is_open())
{
    std::deque<std::string> lines;
    std::string line, line_batch;
    while (std::getline(dataFile, line))
    {
        if (!line.empty())
            lines.push_back(line);
    }
    dataFile.close();

    for (size_t i = 0; i < lines.size();)
    {
        if (i < MAX_BATCH_SIZE)
        {
            line_batch += "{" + lines.at(i) + "},";
            i++;
        }

I'm thinking that having many small strings (1000 lines equating to around 3000 strings) that will be concatenated together, that it would be beneficial to calculate the final string size ahead of time, reserve it and then do a concatenation using string::append as this would reduce fragmentation, reallocation and increase speed (the last one I don't think would be that big of a deal).

A read in line would, at a guess, be between 10-1000 characters.  Max size of line_batch should be under 20MB. The application can get pretty big during execution.  Big enough to force us to move from a 32 to a 64 bit memory scheme.  The amount of times that this will happen depends on the user's environment, but if I were to guess, maybe between 0 and 100 times in a session.  Could be more.

I'm a little on the fence on this.  Am I being too nit picky?  Is the added complexity worth it?  If it matters, we're compiling with VS2019, though I don't think we're using it as a fully compliant c++17 compiler.


texts:
I wrote a piece of code to generate a list of \$n\$ Happy Numbers, would love to see how I can write it better. More information below.

#a happy number is defined is found by taking a number, and adding the sum of its digits, and repeating the steps to add the sum of square
#of the resultant digits until we reach 1, if the result never reaches 1 then it is not a happy number

# example: number = 7
# 7^2 = 49
# 4^2 + 9^2 = 16+81 = 97
# 9^2 + 7^2 = 81 + 49 = 130
# 1^2 + 3^2 + 0 = 1+9 = 10
# 1^2 + 0 = 1 HAPPY number
#defining a emtpy list that will be populate as soon as we encounter a happy number
happy_numbers = []
sqdict={str(i):i**2 for i in range(10)}

#lets define our adder
def adder3(num):
    return sum(sqdict[i] for i in str(num))

#now we need to take a number and keep adding until we get a 1 or the loop keeps repeating
# lets define a function for that

def happynum (num, counter):
    i_sum = adder3(num) # storing the value from the adder fucntion to i_sum
    if i_sum == 1: # check for happy number
        happy_numbers.append(number)
        # print("The given number {} is a happy number.".format(number))
        return 1
    else: # we continue to keep splitting and adding until we reach 1 or attain infinite loop
        counter +=1
        if counter > 50:
             # print("The number {} isnt happy :(.".format(number))
            return False
        else:
            happynum(i_sum,counter)

counter = 0 # initializing a counter to keep keep track of the infinite loop if it does reach that.
# user_num = 7
for i in range (0, 1000):
    number = i
    happynum(number, counter)
print(happy_numbers[10])

The code above is working well for what I am trying to achieve. However, I tried defining the counter as a global variable before all the methods, but when called in the method I was not able to increment. It threw aa error "local variable referenced before assignment".

Additionally, I am not sure using a for loop in the end to generate 1000 Happy Numbers is an efficient way, it seems somewhat limiting. Would appreciate some help on this.


texts:
I am trying to design an exception hierarchy for logging errors.
These logs will be used only by developers and not by users.
This is a base class that I came up with.

#ifndef TSTRING_TYPEDEF
#define TSTRING_TYPEDEF
    typedef std::basic_string <TCHAR> tstring ;
#endif
struct BaseException // Derive from some standard exception
{
BaseException () ;
BaseException (const tstring &strWhat) ;
BaseException (const tstring &strWhat, const unsigned long ulErrorcode) ;

// ...Omitted functions here

void AddDetail (const tstring &strWhat) ;
void AddDetail (const tstring &strWhat, const unsigned long ulErrorcode) ;
tstring GetErrorString () const ;

private:
bool m_bValidErrorCode ;
std::vector <tstring> m_vecDetails ;
unsigned long m_ulErrorCode ;
};

The idea is that an inner function will not always have all the necessary details needed to give useful debugging information, so an inner function will load up an exception with whatever details it can and then throw the exception. The exception will be caught where it can be handled and more information may be added.

The error code will usually just be whatever ::GetLastError() returns (if applicable).

Please note that I won't be catching/wrapping bad alloc calls with this, so I'm not worried about making heap allocations with string and vector.

My questions:

I've been searching online for exception class design, and haven't found anything like this. This worries me. Is there something wrong with designing exceptions this way?
Are there any considerations to think about when deriving from one of the standard exceptions? Or should I just derive from std::exception and not worry about?
One problem with this design is that the format of the string is dependent on this class. Is there any good way to decouple this?

For example, let's a say we have files like this:

MainProcess.h

class MainProcess
{
public:
    // ctors, dtor, functions, etc...
private:
    Config m_config ;
    // ...
};

Config.h

class Config
{
public:
    // ctors, dtor, etc...
std::string ReadValue (const tstring &strFile, const tstring &strSection,
    const tstring &strKey, const bool bRequired = true,
    const tstring &strDefault = _T ("")) const ;

void WriteValue (const tstring &strFile, const tstring &strSection,
    const tstring &strKey, const tstring &strValue) const ;
};

class ConfigReadException // Derive from something
{
// ctors, dtor, etc...
private:
tstring strFile ;
tstring strSection ;
tstring strKey ;
tstring strDefault ;
};

class ConfigWriteException // Derive from something
{
// ctors, dtor, etc...
private:
tstring strFile ;
tstring strSection ;
tstring strKey ;
tstring strValue ;
};

In this case, these ConfigExceptions provide only relevant data and no specific format. But if we do this, then we may end up having to catch many different kinds of exceptions that would clutter our code.

Is it a better practice to have exception classes similar to BaseException or is it better to have exception classes to similar to ConfigReadException and ConfigWriteException, or is there a middle ground?


texts: Transforming an array: take the square root of each perfect square, else square the number

texts:
SquareOrSquareRoot should get an array of integers and return a new array. If the number at index i is a "square" number the returned array at index i should have its square root. If the original number is not a "square" number then the returned array in index i should be the number squared.

Coming from Python (where this could be done in a single line using list comprehension or using map), I find it very odd that this is probably one of the shortest ways to achieve that in Go (please prove me wrong).

package main

import (
    "fmt"
    "math"
)

func SquareOrSquareRoot(arr []int) []int{
    arr_to_return := make([]int, len(arr))
    for index, value := range arr {
        val_sqrt := math.Sqrt(float64(value))
        if val_sqrt == math.Trunc(val_sqrt) {
            arr_to_return[index] = int(val_sqrt)
        } else {
            arr_to_return[index] = value * value
        }
    }

    return arr_to_return
}

func main() {
    arr := []int{100, 101, 5, 5, 1, 1}
    fmt.Println(SquareOrSquareRoot(arr))
    // [10 10201 25 25 1 1]
}


texts:
I've got a flat array of < 1000 items and I need to find the indices of a smaller tuple/array within the array.  The tuple to find can be a varying length (generally between 2 and 5 items, order is important).

This is my initial naive implementation.  My main concerns are:

This seems like a CS 101 problem, so I'm pretty sure I'm overdoing it.
Readability.  I can break this down into smaller methods, but it's essentially a utility function in a much larger class.  I guess I could extract it into its own class, but that feels like overkill as well.  As-is, it's just too long for me to grok the whole thing in one pass.

public int[] findIndices(final Object[] toSearch, final Object[] toFind)
{
    Object first = toFind[0];

    List<Integer> possibleStartIndices = new ArrayList<Integer>();
    int keyListSize = toFind.length;
    for (int i = 0; i <= toSearch.length - keyListSize; i++)
    {
        if (first.equals(toSearch[i]))
        {
            possibleStartIndices.add(i);
        }
    }

    int[] indices = new int[0];
    for (int startIndex : possibleStartIndices)
    {
        int endIndex = startIndex + keyListSize;
        Object[] possibleMatch = Arrays.copyOfRange(toSearch, startIndex, endIndex);
        if (Arrays.equals(toFind, possibleMatch)) {
            indices = toIndexArray(startIndex, endIndex);
            break;
        }
    }
    return indices;
}

private int[] toIndexArray(final int startIndex, final int endIndex)
{
    int[] indices = new int[endIndex - startIndex];
    for (int i = 0; i < indices.length; i++)
        indices[i] = startIndex + i;

    return indices;
}


texts:
A small program I use as my "hello world" of streaming libraries is the following:

For each line of stdin
Interpret the line as a file-name
If the file represented by the file-name has changed
... (including the possibility of being removed or added)
... Then output the file-name

This works well because you can draw a nice data-flow diagram that should convert easily to pipe-like libraries because of the data-plumbing style algorithm...

However, I usually have some issues with this seemingly straight-forward exercise when it comes to splitting and re-joining a stream of values.

The "Machines" library by Edward Kmett has been the cleanest implementation I've made so-far, but is no exception to me encountering implementation issues (hopefully just due to my misunderstandings).

Gist

I've linked to my solution as a gist as well as appended to the end of this question as a code-block and welcome all code reviews, but I would especially like help with the following:

Identifying existing functionality in the 'machines' libraries that I can remove from my implementation.
Finding an exiting replacement or improvement for my splitZipWith function
Removing the IO dependency from as many of my machines as possible

{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleContexts #-}

module UniqHash (main, uniqHash) where

import Data.Machine
import Control.Monad          (when)
import System.Directory       (doesFileExist)
import Control.Category       (Category)
import Data.Digest.Pure.MD5   (MD5Digest, hash')
import Control.Monad.IO.Class (liftIO)

import qualified Data.Map        as M
import qualified Data.ByteString as B

--- UniqHash

main :: IO ()
main = runT_ $ stdinMachine ~> uniqHash ~> stdoutMachine

uniqHash :: ProcessT IO String String
uniqHash = splitZip echo (fileExists ~> md5machine)
        ~> construct (lookupM M.empty)

--- Helpers

stdinMachine :: SourceT IO String
stdinMachine = construct $ liftIO getContents >>= mapM_ yield . lines

stdoutMachine :: ProcessT IO String ()
stdoutMachine = repeatedly $ await >>= liftIO . putStrLn

fileExists :: ProcessT IO String (Maybe String)
fileExists = repeatedly $ do
  fn <- await
  ex <- liftIO $ doesFileExist fn

  yield $ if ex then Just fn
                else Nothing

md5machine :: ProcessT IO (Maybe String) (Maybe MD5Digest)
md5machine = repeatedly $ await >>= liftIO . fileMD5 >>= yield
  where
  fileMD5 :: Maybe String -> IO (Maybe MD5Digest)
  fileMD5 (Just fileName) = fmap (Just . hash') (B.readFile fileName)
  fileMD5  Nothing        = return Nothing

lookupM :: (Category k, Ord o, Eq a) => M.Map o a -> PlanT (k (o, a)) o m ()
lookupM m = do
  (fileName, hashVal) <- await

  case M.lookup fileName m of
       Nothing -> yield fileName
       Just v  -> when (hashVal /= v) (yield fileName)

  lookupM (M.insert fileName hashVal m)

-- Possibly exist in libraries?

double :: MachineT IO (Is o) o
double = repeatedly $ do
  i <- await
  yield i
  yield i

splitZipWith :: ProcessT IO a a' -> ProcessT IO a b' -> TeeT IO a' b' c -> MachineT IO (Is a) c
splitZipWith a b o = double ~> fit cappedT (tee a b o)
  where
  -- Redefined since this is private...
  cappedT :: T a a b -> Is a b
  cappedT R = Refl
  cappedT L = Refl

splitZip :: ProcessT IO a a' -> ProcessT IO a b' -> MachineT IO (Is a) (a', b')
splitZip a b = splitZipWith a b tpair

tpair :: MachineT IO (T t t1) (t, t1)
tpair = repeatedly $ do
  fn <- awaits L
  md <- awaits R
  yield (fn,md)


texts: Fetching data from a MySQL database using Promise

texts:
I have written JavaScript code for fetching data from a MySQL database. I have used connection pooling and Promise API.

var mySql = require("mysql");
var Promise = require("promise");

var pool = mySql.createPool({
  host : "localhost",
  user : "root",
  password : "rahul",
  database : "testDb", //schema
  connectionLimit : 13, // at a time 13 connection be created in pool
});

function getDepartments(){
  return new Promise(fn);

  function fn(resolve,reject){
    pool.getConnection(function(err,con){
        if(err){
            return reject(err);
        }else{
            con.query("select * from departmentTbl",function(err,rows){
                if(err){
                    return reject(err);
                }else{
                    con.release(); // releasing connection to pool
                    return resolve(rows);
                }
            });
        }
      }); // getConnection
    }// fn
 } // getDepartments

getDepartments().then(function(rows){
  console.log(rows);
}).catch(function(err){
  console.log(err);
}).done(function(){
  pool.end();   // closing all connections in pool
});

Can I further improve the code? I have only used one Promise. Can I use multiple promises to avoid callback hell?


texts:
I have a csv parsing method in my Windows App that reads headers and information from the subsequent lines. I am not sure if I am doing in an efficient way. Please comment.

public List<(string IPN, string FPTech)> GetIPNFPTechFromCSV(string filePath)
    {
        List<(string, string)> IPNFPTechPairs = new List<(string, string)>();
        using (StreamReader csvfileReader = new StreamReader(filePath))
        {
            List<string> headings = new List<string>();
            if (HasHeaderRow)
            {
                headings = LoadFieldNamesFromHeaderRow(csvfileReader.ReadLine());
                if (headings.Count != 2)
                    return null;
                else
                {
                    int IPNPos = headings.IndexOf("PartNumber");
                    int FPPos = headings.IndexOf("FPTech");
                    if (IPNPos != -1 && FPPos != -1)
                    {
                        string line;
                        while ((line = csvfileReader.ReadLine()) != null)
                        {
                            var IPNFPTechPair = (IPN: line.Split(Delimiter)[IPNPos], FPTech: line.Split(Delimiter)[FPPos]);
                            IPNFPTechPairs.Add(IPNFPTechPair);
                        }
                    }
                    else
                        return null;
                }
            }
            else
                return null;
        }
        return IPNFPTechPairs;
    }

In the caller I am doing

var InputPairs = inputcsvfile.GetIPNFPTechFromCSV(txt_filePath.Text);

            if (InputPairs == null)
            {
                MessageBox.Show("Please make sure input file is in valid format with PartNumber,FPTech Headers", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

To me, it looks like I am returning nulls from too many paths in my csv parsing method. Please let me know how can I improve this code. I am also not sure how do we efficiently log or report eceptions though out the Windows App. Any ideas are welcome please.

Sample Input CSV File

FPTech,PartNumber
TK0,H19873-001
TK1,H19873-001
TK2,H19872-001
TK1,H19874-001
TK0,H19872-001

The input .csv should be in this format and the FPTech and PartNumber Headers can interchange and sometime smissing. When missing, any of these I want to report to user.


texts: The Palindromic Odometer Puzzler: A Programmatic Solution

texts:
That is a programmatic solution to a puzzler from Car Talk, its summary is:

  "I noticed that the last 4 digits were palindromic. I drove a mile,
  and the last 5 were palindromic. I drove another mile and the middle 4
  were palindromic, and the ends were not involved. And then one mile
  later, all 6 digits were palindromic."

def is_palindrome(string):
    """Finds if a string is a plaindrome or not.

       string: str

       returns: bool
       """
    return string == string[::-1]

def find_puzzle_number():
    """Finds the number which was first seen.

       prints: int
       """
    guess = 100000
    # the odometer shows only 6 numbers,
    # therefore 999999 is the last number to test.
    while guess < 1000000:
        if is_palindrome(str(guess)[2:]):  # last 4 numbers.
            if is_palindrome(str(guess + 1)[1:]):  # last 5 numbers.
                if is_palindrome(str(guess + 2)[1:5]):  # middle 4 numbers.
                    if is_palindrome(str(guess + 3)):
                        print(guess)

        guess = guess + 1

print('These are the possible odometer readings: ')
find_puzzle_number()

Notes:

I'm a hobbyist and a beginner.
I don't only want to optimize the code, but also my skills and style. Therefore any notes, advice and suggestions aren't only welcomed, but also encouraged!


texts: Pandas calculation speed of stock beta on many dataframes

texts:
I have many (4000+) CSVs of stock data (Date, Open, High, Low, Close) which I import into individual Pandas dataframes to perform analysis. I am new to Python and want to calculate a rolling 12month beta for each stock, I found a post to calculate rolling beta (Python pandas calculate rolling stock beta using rolling apply to groupby object in vectorized fashion) however when used in my code below takes over 2.5 hours! Considering I can run the exact same calculations in SQL tables in under 3 minutes this is too slow.

How can I improve the performance of my below code to match that of SQL? I understand Pandas/Python has that capability. My current method loops over each row which I know slows performance but I am unaware of any aggregate way to perform a rolling window beta calculation on a dataframe.

Note: the first 2 steps of loading the CSVs into individual dataframes and calculating daily returns only takes ~20seconds. All my CSV dataframes are stored in the dictionary called FilesLoaded with names such as XAO.

import pandas as pd, numpy as np
import datetime
import ntpath
pd.set_option('precision',10)  #Set the Decimal Point precision to DISPLAY
start_time=datetime.datetime.now()

MarketIndex = 'XAO'
period = 250
MinBetaPeriod = period
# ***********************************************************************************************
# CALC RETURNS
# ***********************************************************************************************
for File in FilesLoaded:
    FilesLoaded[File]['Return'] = FilesLoaded[File]['Close'].pct_change()
# ***********************************************************************************************
# CALC BETA
# ***********************************************************************************************
def calc_beta(df):
    np_array = df.values
    m = np_array[:,0] # market returns are column zero from numpy array
    s = np_array[:,1] # stock returns are column one from numpy array
    covariance = np.cov(s,m) # Calculate covariance between stock and market
    beta = covariance[0,1]/covariance[1,1]
    return beta

#Build Custom "Rolling_Apply" function
def rolling_apply(df, period, func, min_periods=None):
    if min_periods is None:
        min_periods = period
    result = pd.Series(np.nan, index=df.index)
    for i in range(1, len(df)+1):
        sub_df = df.iloc[max(i-period, 0):i,:]
        if len(sub_df) >= min_periods:
            idx = sub_df.index[-1]
            result[idx] = func(sub_df)
    return result

#Create empty BETA dataframe with same index as RETURNS dataframe
df_join = pd.DataFrame(index=FilesLoaded[MarketIndex].index)
df_join['market'] = FilesLoaded[MarketIndex]['Return']
df_join['stock'] = np.nan

for File in FilesLoaded:
    df_join['stock'].update(FilesLoaded[File]['Return'])
    df_join  = df_join.replace(np.inf, np.nan) #get rid of infinite values "inf" (SQL won't take "Inf")
    df_join  = df_join.replace(-np.inf, np.nan)#get rid of infinite values "inf" (SQL won't take "Inf")
    df_join  = df_join.fillna(0) #get rid of the NaNs in the return data
    FilesLoaded[File]['Beta'] = rolling_apply(df_join[['market','stock']], period, calc_beta, min_periods = MinBetaPeriod)

# ***********************************************************************************************
# CLEAN-UP
# ***********************************************************************************************
print('Run-time: {0}'.format(datetime.datetime.now() - start_time))


texts:
I have been learning PHP and wanted to see if I could make a very simple slot machine game.  Everything works, but I'm sure this is not the best way to do this. Please let me know how you would do this and what I can do to improve.

<?php

$num1 = rand(1, 5);
$num2 = rand(1, 5);
$num3 = rand(1, 5);

$result = $num1.' | '.$num2.' | '.$num3;

// Read points from file
$filename = 'points.txt';
$handle = fopen($filename, 'r');
$current = fread($handle, filesize($filename));
fclose($handle);

if ($num1 == $num2 && $num2 == $num3) {
$status = '<big>You are a winner!</big>';

$add_points = $current + 10;

// Add points to file
$handle = fopen($filename, 'w');
$current_points = fwrite($handle, $add_points);
fclose($handle);

} else {
$status = 'please try again!';
}

?>

<title>Slot Machine Game!</title>

<big><?php echo $result; ?></big>

<big><?php echo $status; ?></big>

<big><?php echo 'You have '.$current.' points!'; ?></big>


texts: Finding the maximum number in a 2D vector

texts:
Several people who work with Rust have said that if you find yourself using a lot of clones, you're probably doing something wrong. I'm trying to rewrite some code I wrote to find the maximum number in a 2D vector to use RefCells for the inner vector due to the fact that (as much as I hate it) I need interior mutability on the 2D vec for my physics engine. However, all my code related to this now needs a ton of clone calls. I'm using the "maximum integer" part of my codebase just because its the shortest and easiest to explain piece. Here is my code:

let max_row = |row: &RefCell<Vec<Unit>>| -> Option<usize> {
    row.clone()
       .into_inner()
       .iter()
       .fold(None, |max, unit: &Unit| match max {
        None => Some(unit.tiles.len()),
        el @ Some(_) => {
            if el.unwrap() < unit.tiles.len() {
                Some(unit.tiles.len())
            } else {
                el
            }
        }
    })
};

// find the highest height in this list of rows. Same as above, but uses above. (:
let toplevel = world.map
                    .iter()
                    .fold(None, |max, vec| match max {
    None => max_row(vec),
    r @ Some(_) => {
        match max_row(vec) {
            None => r,
            z @ Some(_) => {
                if r.unwrap() < z.unwrap() { z } else { r }
            }
        }
    }
});

This is a tad ugly, but the worst part is the use of .clone and .into_inner right in a row after sending it as a pointer. For some reason, it just feels icky, probably because I did that as a fix, not how I originally was hoping it could turn out. I have only a mediocre understanding of borrowing, which could be the problem.

To clarify the code, here is Unit:

#[derive(Clone)]
pub struct Unit {
  pub tiles: Vec<Tile>,
}

// And a basic version of Tile

#[derive(Copy, Clone)]
pub enum Tile {
  Wall,
  Floor,
}

And world is just a Vec<RefCell<Vec<Unit>>>.

I really hope there is a better way, and if there is, could you explain it in such a way that I could apply it to other ugly areas of my codebase where this is also a problem? For instance, I have another area where I need to use a vector from inside a closure, but also need to modify it later, so in the closure I am cloning it, which seems like an ugly solution.