texts: Beautifulsoup and lxml (xpath) too slow with respect to regex when parsing HTML

texts:
I agree that using regex to parse HTML is not a good way, in particular I am worried about their fragility with respect to change in the HTML.

The problem is that any alternatives are really too slow.

I am parsing this web page which is quite big (3MB). Somewhere in the html there is:

<form class="form-inline"  id="exportform" action="/references" class="hidden">
    <input type="hidden" name="format" value="" />

    <input type="hidden" name="item_id" value="678860" />

    <input type="hidden" name="item_id" value="656680" />

    <input type="hidden" name="item_id" value="678846" />

    <input type="hidden" name="item_id" value="650319" />

    <input type="hidden" name="item_id" value="651504" />

    <input type="hidden" name="item_id" value="678849" />

    <input type="hidden" name="item_id" value="649821" />

    <input type="hidden" name="item_id" value="649835" />

    <input type="hidden" name="item_id" value="651512" />

    <input type="hidden" name="item_id" value="651510" />

</form>

I need to extract the 10 numbers in the value field. I know they are exactly 10.

I tried different approaches:

BeautifulSoup

from bs4 import BeautifulSoup

def soup(html):
    soup = BeautifulSoup(html, 'lxml')
    form = soup.find("form", {'id': 'exportform'})
    if not form:
        return []
    entries = form.find_all("input", {'name': 'item_id'})
    result = [entry['value'] for entry in entries]
    return result

Regex

import re
import itertools

def regex(html):
    r = re.compile(r'<input type="hidden" name="item_id" value="([0-9]+)">')

    s = r.finditer(html)
    result = []
    for ss in itertools.islice(s, 10):
        result.append(ss.group(1))
    return result

xpath

import lxml.html
from lxml import etree # same performance with this

def xpath(html):
    root = lxml.html.fromstring(html)
    result = root.xpath('//form[@class="form-inline"]/*[@name="item_id"]')
    return [r.attrib['value'] for r in result]

I get the same results from the three, but very different timing:

BeautifulSoup: 3.3
regex: 0.00013
xpath: 0.57

In the regex case I am able to use the trick that I know there are exactly 10 numbers to find, gaining a factor 10. But also without that trick the regex approach is 400 times faster than xpath.

In addition asking xpath to give me the first 10:

(//form[@class="form-inline"]/*[@name="item_id"])[position() < 11]

is not improving


texts:
I need to extract the dimensions (width and height) of a jpeg file for inserting in an html document. According to the JPEG image compression FAQ, cautions against what's commonly out there,

  Some people have recommended just searching for the byte pair
  representing SOFn, without paying attention to the marker block structure.
  This is unsafe because a prior marker might contain the SOFn pattern, either
  by chance or because it contains a JPEG-compressed thumbnail image.

It said to look up rdjpgcom.c, which is part of the open-source ImageMagick. It seems to agree with the advice on this answer. However, this file prints out jpeg comments, so I wrote one to specifically get the dimensions.

#include <stdlib.h> /* EXIT */
#include <stdio.h>  /* [f]printf fopen fclose fread */
#include <assert.h> /* assert */
#include <errno.h>  /* errno */

/** Attempt to read the size of a jpeg.
 @param[file] File that has been opened in binary mode and rewound; required.
 @param[width, height] Pointers that get overwritten on success; required.
 @return Success, otherwise `errno` will (probably) be set.
 @license <http://www.faqs.org/faqs/jpeg-faq/part1/> said to look up
 [rdjpgcom.c](https://github.com/ImageMagick/jpeg-turbo/blob/master/rdjpgcom.c),
 which has
 [IJG License](https://github.com/ImageMagick/jpeg-turbo/blob/master/README.ijg)
 and possibly related [ImageMagick](https://imagemagick.org/script/license.php).
 I used as a reference to write this function. */
static int jpeg_dim(FILE *const fp, unsigned *const width,
    unsigned *const height) {
    unsigned char f[8];
    unsigned skip;
    assert(fp && width && height);
    /* The start of the file has to be an `SOI`. */
    if(fread(f, 2, 1, fp) != 1) return 0;
    if(f[0] != 0xFF || f[1] != 0xD8) return errno = EDOM, 0;
    for( ; ; ) {
        /* Discard up until the last `0xFF`, then that is the marker type. */
        do { if(fread(f, 1, 1, fp) != 1) return 0; } while(f[0] != 0xFF);
        do { if(fread(f, 1, 1, fp) != 1) return 0; } while(f[0] == 0xFF);
        switch(f[0]) {
        case 0xC0: case 0xC1: case 0xC2: case 0xC3: case 0xC5: /* _sic_ */
        case 0xC6: case 0xC7: case 0xC9: /* _sic_ */ case 0xCA: case 0xCB:
        case 0xCD: /* _sic_ */ case 0xCE: case 0xCF:
            /* `SOF` markers. */
            if(fread(f, 8, 1, fp) != 1) return 0;
            if((skip = (f[0] << 8) | f[1]) != 8u + 3 * f[7])
                return errno = EDOM, 0;
            *width  = (f[5] << 8) | f[6];
            *height = (f[3] << 8) | f[4];
            return 1;
        case 0xD8: case 0xD9:
            /* Image data `SOS, EOI` without image size. */
            return errno = EDOM, 0;
        default:
            /* Skip the rest by reading it's size. */
            if(fread(f, 2, 1, fp) != 1) return 0;
            if((skip = (f[0] << 8) | f[1]) < 2) return errno = EDOM, 0;
            if(fseek(fp, skip - 2, SEEK_CUR) != 0) return 0;
        }
    }
}

int main(int argc, char **argv) {
    FILE *fp = 0;
    unsigned width, height;
    int success = EXIT_FAILURE;
    if(argc != 2 || !(fp = fopen(argv[1], "rb"))
        || !jpeg_dim(fp, &width, &height)) goto catch;
    printf("Jpeg width: %u, height: %u.\n", width, height);
    success = EXIT_SUCCESS;
    goto finally;
catch:
    if(errno) {
        perror("jpeg size");
    } else {
        fprintf(stderr, "Use with jpeg filename to see the size.\n");
    }
finally:
    if(fp) fclose(fp);
    return success;
}

It seems to work, but I'd like to review it to make sure that it's correct.


texts: Replace the content of a file by zero-bytes and keep the same filesize

texts:
Here is a little tool for Windows that replaces the content of a file with zero-bytes, keeping the same filesize. It works. The goal is to be sure that the old bytes-content of the file is no more recoverable on disk.

Question: how to be 100% sure that it replaces the content in-place and that it doesn't allocate new sectors on disk to write the zero bytes? (thus leaving the old content somewhere else, still possibly accessible, which would be a security hole!)

Note: I know that freewares exist for this, but I would like to try a simple implementation first

Note2: I know that some more advanced wiping techniques exist (random content and/or multi-pass, etc.), but for me zeroing the content is enough if at least I know that the zeroing is done in-place, i.e. if the new zero bytes replace the old content at the same location on disk (and not moved to another sectors on disk!)

Could you review the following code?

#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE *f;

    if (argc != 2) { fprintf (stderr, "Usage: %s <file>\n", argv[0]); exit(1); }

    if ((f = fopen(argv[1], "rb+")) == NULL) { perror("Error opening the file"); exit(1); }

    fseek(f, 0L, SEEK_END);
    unsigned long size = ftell(f);
    fseek(f, 0L, SEEK_SET);

    char buf[1024] = {0};
    unsigned long num = size / 1024;
    int remaining = size % 1024;

    for (unsigned long i = 0; i < num; i++)
        fwrite(buf, 1, 1024, f);

    fwrite(buf, 1, remaining, f);

    fclose(f);
    exit(0);
}


texts:
I have a Db table:

'aid' INTEGER,
'tid' INTEGER,

Which represents connection between activity and task, so multiple tasks could be connected to one activity using their ids:

aid tid
1   5
1   9
1   11

So activity with id=1 is connected with tasks with ids=5,9,11.

I load all this data during the launch of the app and put it in SparseArray, where keys of this array are activity ids (aid), and values of this array are arrays with task ids (long[]).

The function looks like this:

public final void setRawCursor(final Cursor cursor) {
        if (cursor != null) {
            long aid, aidPrev = INVALID;
            final FastLongKeySparseArray<long[]> map = mTaskPinMap.getMap();
            final FastLongStack stack = new FastLongStack(25);
            try {
                while (cursor.moveToNext()) {
                    aid = cursor.getLong(TaskPin.AID_);
                    if (aidPrev != INVALID
                            && aid != aidPrev) {
                        map.append(aidPrev, stack.toArray(new long[stack.size()]));
                        stack.clear();
                    }
                    stack.push(cursor.getLong(TaskPin.TID_));
                    aidPrev = aid;
                }
                if (aidPrev != INVALID) map.append(aidPrev, stack.toArray(new long[stack.size()]));
            } finally {
                cursor.close();
            }
        }
        mIsDataReady = true;
    }

My purpose to save as much memories as possible, and do not make pressure on GC. I use long[] instead of ArrayList because this data could be modified in rear cases, so it is not hard to add new ids to long[] by coping the array, and I think to save memory with such turn.

What do you think? Is it possible to eliminate put to the map after the loop?


texts: How to keep current HtmlElement active and change its properties?

texts:
I have a simple class that listens click and draws two points on the page.
If first point is not exist it is drawn. When second point is present it should be changed after each click.
I dislike this part of code:
this.point2.element.style.left = `${e.x}px`;
this.point2.element.style.top = `${e.y}px`;

Full code:
export class Kizda {
    private point1: { element: HTMLElement; pointMeter: Point };
    private point2: { element: HTMLElement; pointMeter: Point };

    getPoints() {
        return { point1: this.point1, point2: this.point2 };
    }

    action(e: MouseEvent): void {
        const point = this.getMouseMPoint(e);

        function createPoint(color: string): HTMLElement {
            let pointSize = 12;
            let pointElement = document.createElement('div');
            pointElement.style.position = 'absolute';
            pointElement.style.width = `${pointSize}px`;
            pointElement.style.height = `${pointSize}px`;
            pointElement.style.left = `${e.x}px`;
            pointElement.style.top = `${e.y}px`;
            pointElement.style.background = color;
            pointElement.style.borderRadius = '50%';

            document.body.append(pointElement);

            return pointElement;
        }

        if (!this.point1) {
            this.point1 = { pointMeter: point, element: createPoint('red') };
            return;
        }

        if (!this.point2) {
            this.point2 = { pointMeter: point, element: createPoint('green') };
            return;

        } else {
            this.point2.element.style.left = `${e.x}px`;
            this.point2.element.style.top = `${e.y}px`;
        }
    }


texts:
Is there a way I can make the code compact and with fewer ifs?

theData = GetData();
if (theData.Rows.Count > 0)
{
    MyModel = new CustomModel();
    dataSetRow = theData.Rows[0];
    if (theData.Columns.Contains("Column1"))
    {
        if ((!object.ReferenceEquals(dataSetRow["Column1"], DBNull.Value)))
        {
            MyModel.Column1 = Convert.ToString(dataSetRow["Column1"]);
        }
    }
    if (theData.Columns.Contains("Column2"))
    {
        if ((!object.ReferenceEquals(dataSetRow["Column2"], DBNull.Value)))
        {
            MyModel.Column2 = Convert.ToString(dataSetRow["Column2"]);
        }
    }
    if (theData.Columns.Contains("Column3"))
    {
        if ((!object.ReferenceEquals(dataSetRow["Column3"], DBNull.Value)))
        {
            MyModel.Column3 = Convert.ToString(dataSetRow["Column3"]);
        }
    }
    if (theData.Columns.Contains("Column4"))
    {
        if ((!object.ReferenceEquals(dataSetRow["Column4"], DBNull.Value)))
        {
            MyModel.Column4 = Convert.ToString(dataSetRow["Column4"]);
        }
    }


texts:
Story
Niñez friends from Kakao Elementary School meet a stream with stepping stones on an autumn picnic with Ms. Ryan and are about to cross over to the other side. Ms. Ryan made the following rules so that Niñez and his friends can safely cross the river:

Stepping stones are placed in a row, and the number of times a kid may step on a stone is written on each stone.
The number on the stone decreases by one each time a kid steps on them.
When the number of stepping stones reaches 0, you can no longer step on them.
A kid can jump, skipping several stones. The maximum number of stones that a kid may skip is given as a parameter k.
If a kid jumps, you always jump to the nearest stepping stone that isn't 0.

The friends of Niñez are on the left side of the stream, and they must arrive across the right side of the stream to be counted as crossing the stepping stone.
How many kids can cross the river, given an array of initial values on the stones and the parameter k?
Constraints

The number of Ninez friends who must cross the stepping stone is considered to be unlimited.
The stones array has a size of at least 1 and has a maximum length of 200,000.
The initial value of each element in the stones array is a natural number between 1 and 200,000,000.
k is a natural number greater than or equal to 1 and less than or equal to the length of the stones.

Code review question
How should i revise my code? this is Kakao intern code test. I used loop to change stone numbers when stepped and repeat many times . It returns how many people could cross the river.
Original challenge (in Korean)
Example
stones = [2, 4, 5, 3, 2, 1, 4, 2, 5, 1]
k=3

After the first kid crosses, the stones have the following values:
stones = [1, 3, 4, 2, 1, 0, 3, 1, 4, 0]

The second kid makes it, making two jumps:
stones = [0, 2, 3, 1, 0, 0, 2, 0, 3, 0]

The third kid makes it, making four jumps:
stones = [0, 1, 2, 0, 0, 0, 1, 0, 2, 0]

Now it takes a jump of 4 stones to get to the other side, which is larger than k = 3. So the answer for this input is three kids, or simply 3.
def solution(stones, k):
    num=len(stones)
    answer=0
    for count in range(max(stones)):
        stat=-1
        for i in range(num):
            if stat==i-1:
                if stones[i]==0:
                    for j in range(i+1,num):
                        if stones[j]==0:
                            if j-stat>=k:
                                return answer
                            else:
                                pass
                        elif stones[j]!=0:
                            stat=j
                            stones[j]-=1
                            break
                elif stones[i]!=0:
                    stat=i
                    stones[i]-=1
        if stat+k>=num:
            answer+=1
        elif stat+k<num:
            return answer
    return answer


texts:
I am currently study the basic data structure and trying to implement everything as I go. can anyone give me some feedback about class LinkedList how to make the code more elegant. any review are appreciated.

class Node(object):
    def __init__(self, data, n=None):
        self.val = data
        self.next = n

    def get(self):
        return self.data

    def set(self, data):
        self.val = data

    def get_next_node(self):
        return self.next

    def set_next_node(self, data):
        self.next.val = data

class SingleLinkedList(object):
    """
    Single Linked List object
    Args:
        data(Node): Node
    Attributes:
        head(Node): single LinkedList head
    """

    def __init__(self, data):
        self.head = data

    def __repr__(self):
        """
        :return:
        """
        cur = self.head
        s = ''
        while cur:
            s += f'{cur.val}->'
            cur = cur.next
        return s

    def __len__(self):
        cur, cnt = self.head, 0
        while cur:
            cnt += 1
            cur = cur.next
        return cnt

    def append(self, data):
        if not self.head:
            self.head = Node(data)
            return
        cur = self.head
        while cur.next: cur = cur.next
        cur.next = Node(data)

    def insert_before_key(self, key, data):
        cur = self.head
        prev = Node(None)
        while cur:
            if cur.val == key:
                node = Node(data)
                node.next = cur
                prev.next = node
                break
            prev = cur
            cur = cur.next
        self.head = prev.next

    def insert_after_key(self, key, data):
        cur = self.head
        while cur:
            if cur.val == key:
                node = Node(data)
                node.next = cur.next
                cur.next = node
            cur = cur.next

    def delete(self, key):
        if not self.head: return
        dummy = cur = self.head
        prev = None
        while cur:
            if cur.val == key and prev:
                prev.next = cur.next
                self.head = dummy
                return
            elif cur.val == key:
                self.head = cur.next
                return
            prev = cur
            cur = cur.next

    def search(self, key):
        cur = self.head
        while cur and cur.val != key:
            cur = cur.next
        return cur or None

    def reverse(self):
        cur = self.head
        prev = None
        while cur:
            nxt = cur.next
            cur.next = prev
            prev = cur
            cur = nxt
        self.head = prev


texts:
I'm compressing an array of strings in an Android app. Is there something you would suggest?

public static String compressArray(String[] data){
    String jsonData = Arrays.toString(data);

    try {
        String compressedData = compressString(jsonData);
        int compressedLength = compressedData.length();
        int normalLength = jsonData.length();
        return normalLength < compressedLength? jsonData:compressedData;
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }

}
public static String compressString(String srcTxt) throws IOException {
    ByteArrayOutputStream rstBao = new ByteArrayOutputStream();
    GZIPOutputStream zos = null;
    try {
        zos = new GZIPOutputStream(rstBao);
        zos.write(srcTxt.getBytes());
        zos.finish();

        byte[] bytes = rstBao.toByteArray();
        return Base64.encodeBase64String(bytes);

    }
    finally {
        zos.close();
        rstBao.close();

    }

}


texts: Implementing a temporary data context manager

texts:
I implemented a context manager to handle temporary files and directories.

"""Temporary data management."""

from contextlib import ExitStack
from tempfile import template, NamedTemporaryFile, TemporaryDirectory

__all__ = ['TemporaryDataContext']

class TemporaryDataContext(ExitStack):
    """A context manager for temporary files and folders."""

    def __init__(self, root=None, prefix=template):
        """Initializes the context manager with an
        optional temporary data root and prefix.
        """
        super().__init__()
        self.root = root
        self.prefix = prefix

    def mktemp(self, mode='w+b', encoding=None, suffix=''):
        """Creates and registers a named temporary
        file and returns the file object.
        """
        temp = self.enter_context(NamedTemporaryFile(
            suffix=suffix, prefix=self.prefix, dir=self.root))
        temp.file.close()
        return open(temp.name, mode=mode, encoding=encoding)

    def mkdtemp(self, suffix=''):
        """Creates and registers a temporary
        directory and returns its path.
        """
        return self.enter_context(TemporaryDirectory(
            suffix=suffix, prefix=self.prefix, dir=self.root))

It is being invoked by a data processing API to arbitrarily create temporary files and directories, that need to survive the entire processing chain and will be cleaned up at the end.

I am especially interested in optimizing the mktemp method, which seems awkward to me (though it does its job).


texts: Adding a mix of strings and string arrays to an array

texts:
Original question:

Function to split either a string array or string into a string array

I was asked why I didn't write multiple functions or something like that. The point of the function is to be a single one and to be able to receive an indefinite number of strings or string arrays and get the job done. Restrictions are not allowed.

The function I have written (creatively-named):

public string[] Add(string[] ogarr,object[] toadd)
{

    var results = new System.Collections.Generic.List<string>();
    results.AddRange(ogarr);

    for (int i = 0; i < toadd.Length; i++)
    {
        if (toadd[i].GetType() == typeof(string[]))
            results.AddRange((string[])toadd[i]);
        else
            results.Add((string)toadd[i]);
    }

        return results.ToArray();
}

I use this to test its functionality:

string[] temp = new string[]{"123","456","789"};

string[] thing1 = new string[]{"abc","def"};
string thing2 = "ghi";

object[] toadd = new object[] {thing1,thing2 };

temp = Add(temp,toadd);

temp is the original string array I want to add things to.
thing1 and thing2 are both things I want to add to that array, one after another, in the order of addition to the object[], obviously.

Disclaimer: I'm not trying to cut bytes; I want aesthetic code! This is my passion.


texts: Linked list abstraction using python arrays

texts:
Building linked list abstraction using class 'list' data model.

##################
#   Linked List  #
##################

#Representation - start
empty = 'empty'
def is_link(s):
    """ s is a linked list if it is empty or a (first, rest) pair."""
    return s == empty or (isinstance(s, list) and len(s) == 2 and is_link(s[1]))
#Constructor
def link(first, rest):
    """Construct a linked list from its first element and the rest."""
    assert is_link(rest), "rest must be a linked list"
    return [first, rest]
#Selector 1
def first(s):
    """Return a first element of a linked list s"""
    assert is_link(s), "first only applies to linked lists."
    assert s != empty, "empty linked list has no first element."
    return s[0]
#Selector 2
def rest(s):
    """Return the rest of the elements of a linked list s."""
    assert is_link(s), "rest only applies to linked lists."
    assert s != empty, "empty linked list has no rest."
    return s[1]

def isempty(s):
    return s == 'empty'

#Representation - end

###  +++ === Abstraction barrier === +++ ###

#Use(interface) -  start
def create_linked_list(first, rest):
    return link(first, rest)

def len_rlist(s):
    """Compute the length of the recursive list s"""
    def compute_length(s, length):
        if isempty(s):
            return length
        else:
            return compute_length(rest(s), length + 1)
    return compute_length(s, 0)

def getitem_rlist(s, i):
    """Return the element at index i of recursive list s"""
    if i == 1:
        return first(s)
    else:
        return getitem_rlist(rest(s), i-1)

def count(s, value):
    """Count the occurence of value in the list s """
    def count_occurence(s, value, count):
        if isempty(s):
           return count
        else:
           if first(s) == value:
              return count_occurence(rest(s), value, count + 1)
           else:
              return count_occurence(rest(s), value, count)
    return count_occurence(s, value, 0)

#Use - end

#Constructor and Selector constitutes Abstract Data Type that supports below invariants:
#If a recursive list s is constructed from a first element f and a recursive list r, then
#   • first(s) returns f, and
#   • rest(s) returns r, which must be a list or 'empty'.

#Code using this abstraction
Lst = empty
Lst = create_linked_list(4, Lst)   #  Representation [4, 'empty']
Lst = create_linked_list(3, Lst)   #  Representation [3, [4, 'empty']]
Lst = create_linked_list(1, Lst)   #  Representation [1, [3, [4, 'empty']]]
Lst = create_linked_list(1, Lst)   #  Representation [1, [1, [3, [4, 'empty']]]]
print(count(Lst, 1))

Intention is to build a single linked list abstraction that has no correctness issues.

Are there any limitations in building this abstraction?


texts: Speech Recognition Part 3: Training the Neural Network

texts:
The last part of my speech recognition series: finally training my network.  Here's the dataset I did it with (self-generated, small I know), and the code I used.

After running this code (takes about an hour on my Mac), I get a validation accuracy of roughly 30%... not spectacular.  Any ideas on how to improve the training speed, or the neural network's accuracy?  Any other suggestions in general?

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import numpy as np
import tflearn

def main():
    LABELED_DIR = 'labeled_data'
    width = 512
    height = 512
    classes = 26  # characters
    learning_rate = 0.0001
    batch_size = 25

    # load data
    print('Loading data')
    X, Y = tflearn.data_utils.image_preloader(LABELED_DIR, image_shape=(width, height), mode='folder', normalize=True, grayscale=True, categorical_labels=True, files_extension=None, filter_channel=False)
    X_shaped = np.squeeze(X)
    trainX, trainY = X_shaped, Y

    # Network building
    print('Building network')
    net = tflearn.input_data(shape=[None, width, height])
    net = tflearn.lstm(net, 128, dropout=0.8)
    net = tflearn.fully_connected(net, classes, activation='softmax')
    net = tflearn.regression(net, optimizer='adam', learning_rate=learning_rate, loss='categorical_crossentropy')

    model = tflearn.DNN(net, tensorboard_verbose=3)
    print('Training network')
    model.fit(trainX, trainY, validation_set=0.15, n_epoch=100, show_metric=True, batch_size=batch_size)
    model.save("tflearn.lstm.model")

if __name__ == '__main__':
    main()


texts: Refactored 2 functions into 1 - Verify UPC/EAN13 barcode

texts:
I refactored 2 seperate functions that I found here into a single function:

<?
    function isValidBarcode($barcode) {

        $barcode = trim($barcode);

        //If value is not numeric, it is not valid.
        if ((!is_numeric($barcode)) && ((strlen($barcode) == 12) || (strlen($barcode) == 13))) return false;

        //set empty variables
        $even_sum = $odd_sum = 0;

        //array key value to count to, doesn't include the last 2 numbers of barcode
        $countTo = strlen($barcode) - 2;

        //loop through every digit in barcode, count even and odds based on $countTo set before.
        for ($x = 0; $x <= $countTo; $x++) {
            if ($x % 2 == 0) {
                $even_sum += $barcode[$x];
            } else {
                $odd_sum += $barcode[$x];
            }
        }

        //Multiply even_sum by 3, and then add odd_sum to it.
        $total_sum   = $even_sum * 3 + $odd_sum;

        //get highest multiple of 10 of total_sum.
        $next_ten    = ceil($total_sum / 10) * 10;

        //Subtract highest multiple of 10 from total_sum to get check_digit.
        $check_digit = $next_ten - $total_sum;

        //if check_digit is equal to the last digit in the barcode, it is valid.
        if ($check_digit == $barcode[$countTo + 1]) return true;

        //if check_digit is not equal to the last digit in the barcode, it is invalid.
        return false;
    }

?>

But, I feel as though I can make this even better. I wanted some other opinions on this, and I wanted to see what other people could come up with in terms of a solution.

My main question is, how efficient is this code, and is it possible for me to make it more efficient?


texts:
I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time!
Problem

Given an array which consists of non-negative integers and an integer
m, you can split the array into m non-empty continuous subarrays.
Write an algorithm to minimize the largest sum among these m
subarrays.
Note:
If n is the length of array, assume the following constraints are
satisfied:
$$
  1 \le n \le 1000 \\
  1 \le m \le \min(50, n)
  $$
Example:
Input: nums = [7,2,5,10,8] m = 2
Output: 18
Explanation: There are four ways to split nums into two subarrays. The
best way is to split it into [7,2,5] and [10,8], where the largest sum
among the two subarrays is only 18.

Code
#include <vector>

class Solution {
public:
    static int splitArray(const std::vector<int> &nums, const int m) {
        size_t lo = 0;
        size_t hi = 0;

        for (int num : nums) {
            lo = std::max(lo, (size_t) num);
            hi += num;
        }

        while (lo < hi) {
            size_t mid = lo + (hi - lo) / 2;
            if (partitioning_is_valid(nums, m - 1, mid)) {
                hi = mid;
            } else {
                lo = mid + 1;
            }
        }

        return lo;
    }

private:
    static bool partitioning_is_valid(const std::vector<int> &nums, int partitions, size_t partition_max_sum) {
        size_t accumulate_curr_partition = 0;
        for (int num : nums) {
            if (num > partition_max_sum) {
                return false;
            } else if (accumulate_curr_partition + num <= partition_max_sum) {
                accumulate_curr_partition += num;
            } else {
                partitions--;
                accumulate_curr_partition = num;

                if (partitions < 0) {
                    return false;
                }
            }
        }

        return true;
    }

};

Reference
LeetCode has a template for answering questions. There is usually a class named Solution with one or more public functions which we are not allowed to rename. For this question, the template is:
class Solution {
public:
    int splitArray(vector<int>& nums, int m) {

    }
};

Problem

Solution

Discuss


texts: Creating an Excel sheet and appending namings for a matrix

texts:
I'm working on some VB.Net code to generate data sheets for giving user rights on a per folder level. I came up with this code but it's very slow and I feel like it could be improved.

Public Shared Function CreateExcel(groupOwnerOf, sourceLocation, Optional groupOwner = Nothing)

    Dim ini As New IniFile(Directory.GetCurrentDirectory() & "\settings.ini")
    For Each prog As Process In Process.GetProcesses
        If prog.ProcessName = "EXCEL" Then
            prog.Kill()
        End If
    Next
    Threading.Thread.Sleep(1000)

    Dim xlApp As Excel.Application = New Excel.Application()

    If xlApp Is Nothing Then
        Logging.LogMain("Excel is not properly installed, recommending (re)isntallation")
        Return Nothing
    End If

    Dim xlWorkbook As Excel.Workbook
    xlWorkbook = xlApp.Workbooks.Add()

    Dim xlWorksheet = xlWorkbook.Sheets("sheet1")

    xlWorksheet.Name = "rights"
    xlWorksheet.Cells(1, 1) = "Paths"

    Dim Colums As Integer = 2
    Dim Sheetcounter = 2
    Dim locationCount As Integer = 3
    Dim Row As Integer = 3
    Dim ColumsGroups As Integer = 2
    Dim saveLocation = ini.GetString("settings AC", "excelMailSave") & "\" & ini.GetString("settings AC", "excelMailName") & ".xlsx"
    Dim groupCounter = 1

    If groupOwner Is Nothing Then
        groupCounter = countGroupsOfOwner(Autocomply.DisplayADsPath.Text)
    Else
        Console.WriteLine("other")
        groupCounter = countGroupsOfOwner(groupOwner)
    End If

    Console.WriteLine(groupCounter)

    If My.Computer.FileSystem.FileExists(saveLocation) Then

        My.Computer.FileSystem.DeleteFile(saveLocation)
        xlWorkbook.SaveAs(saveLocation)

    End If

    'generating excel data
    xlWorksheet.Rows(1).Orientation = 90
    xlWorksheet.Rows(1).RowHeight = 12 * xlWorksheet.StandardHeight
    xlWorksheet.Rows(1).HorizontalAlignment = Excel.Constants.xlCenter
    Do While groupOwnerOf.read()

        Autocomply.ProgressBar1.Maximum = groupCounter
        Dim membersFromGroup = GetMembersFromGroup(sourceLocation, groupOwnerOf("groupName"))
        Do While membersFromGroup.read()

            For i = ColumsGroups To groupCounter + 1 Step +1

                If membersFromGroup("memberUser") = xlWorksheet.Cells(2, i - 1).Value Then
                    Console.WriteLine("false")
                    GoTo exitMemberFor
                End If

            Next

            Console.WriteLine(membersFromGroup("memberUser"))

            xlWorksheet.Cells(2, Colums) = membersFromGroup("memberUser")
            Colums += 1
            groupCounter += 1

exitMemberFor:

        Loop

        Dim locationsOfGroup = DataFetch.locationsOfGroup(sourceLocation, groupOwnerOf("groupName"))

        Do While locationsOfGroup.read()

            For i = locationCount To Row + 1 Step +1

                If locationsOfGroup("location") = xlWorksheet.Cells(i - 1, 1).Value Then
                    Console.WriteLine("false")
                    GoTo exitFor
                End If

            Next

            xlWorksheet.Cells(Row, 1) = locationsOfGroup("location")
            Console.WriteLine(xlWorksheet.Cells(Row, 1).Value)
            Row += 1
            locationCount += 1
exitFor:

        Loop

        Autocomply.ProgressBar1.Value += 1

    Loop

    Autocomply.ProgressBar1.Value = 0
    Autocomply.ProgressBar1.Maximum = locationCount

    For Row = 3 To locationCount Step +1

        For i = 2 To groupCounter + 1 Step +1

            Dim rights = getRightsForPath(sourceLocation, xlWorksheet.Cells(1, i).Value, xlWorksheet.Cells(Row, 1).Value)
            Do While rights.Read()

                xlWorksheet.Cells(Row, i) = rights("rights")
                Console.WriteLine(rights("rights"))

            Loop

        Next

        Autocomply.ProgressBar1.Value += 1

    Next

    Autocomply.ProgressBar1.Value = 0
    xlWorkbook.SaveAs(saveLocation)
    Return xlApp

End Function


texts: Optimizing and clarifying nested list appending

texts:
I have such part in my script, how can I optimize such parts?  I don't want to use many local variables.

size_variants = []
for size in sizes_list:
    size_variants.append({'size': size})

Also here:

variants_tmp = list()
all_color_list = list(get_all_colors())
[variants_tmp.extend(var) for var in all_color_list]


texts: Checking if a number is power of 2 or not

texts:
This is my solution in Java and I need your opinion:

public class Main {

  /**
   *
   */

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int number = in.nextInt();
    in.close();

    if (isPowerOfTwo(number)) {
      System.out.println("yes");
    } else {
      System.out.printf("no");
    }
  }

  private static boolean isPowerOfTwo(int number) {

    if (number % 2 != 0) {
      return false;
    } else {

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

        if (Math.pow(2, i) == number) return true;
      }
    }
    return false;
  }
}


texts: Getting a value from a database table dynamically

texts:
I am trying to create a forum using php in a MVC architecture.
This is my database setup:

Table: forum_categories

`forum_categories` (
`cat_id` INT(8) NOT NULL AUTO_INCREMENT,
`cat_title` VARCHAR(255) NOT NULL,
`cat_desc` TEXT NOT NULL,
PRIMARY KEY (`cat_id`),
UNIQUE KEY (`cat_title`)

Table: forum_topics

`forum_topics` (
`topic_id` INT(8) NOT NULL AUTO_INCREMENT,
`cat_id` INT(8) NOT NULL COMMENT 'foreign key with forum_categories table',
`user_id` INT(11) NOT NULL COMMENT 'foreign key with users table',
`topic_title` VARCHAR(255) NOT NULL,
`topic_desc` TEXT NOT NULL,
`topic_date` DATETIME DEFAULT NULL,
PRIMARY KEY (`topic_id`),
FOREIGN KEY (`cat_id`) REFERENCES forum_categories (`cat_id`) ON DELETE CASCADE ON UPDATE CASCADE

Example of the functionality, I would like to achieve:

  Category 1 has cat_id = 1
  Category 2 has cat_id = 2

  Topic 1 has cat_id = 1
  Topic 2 has cat_id = 2

Now when category 1 is selected I just want topic 1 to show.
If category 2 is selected I just want topic 2 to show.

The following code does that:

Controller

 /**
 * Show all the topics in the chosen category
 */
public function showForumTopicsCat1()
{
    $topic_model = $this->loadModel('Forum');
    $this->view->forum_topics = $topic_model->getForumTopics(1);
    $this->view->render('forum/viewTopics');
}

public function showForumTopicsCat2()
{
    $topic_model = $this->loadModel('Forum');
    $this->view->forum_topics = $topic_model->getForumTopics(2);
    $this->view->render('forum/viewTopics');
}

public function showForumTopicsCat3()
{
    $topic_model = $this->loadModel('Forum');
    $this->view->forum_topics = $topic_model->getForumTopics(3);
    $this->view->render('forum/viewTopics');
}

public function showForumTopicsCat4()
{
    $topic_model = $this->loadModel('Forum');
    $this->view->forum_topics = $topic_model->getForumTopics(4);
    $this->view->render('forum/viewTopics');
}

Model

/**
 * Gets an array that contains all the forum topics in the database.
 * Each array element is an object, containing a specific topic's data.
 * @return array All the forum topics
 */
public function getForumTopics($cat_id)
{
    $sql = 'SELECT * FROM forum_categories fc JOIN forum_topics ft ON fc.cat_id = ft.cat_id WHERE fc.cat_id = :cat_id ORDER BY ft.topic_date DESC';
    $query = $this->db->prepare($sql);
    $query->execute(array(':cat_id' => $cat_id));

    return $query->fetchAll();
}

View

<?php
    if ($this->forum_categories) {
            echo 'Category1';
            echo 'Category2';
            echo 'Category3';
            echo 'Category4';
    } else {
        echo '
There are no categories.
';
    }
?>

My Concern: Is there anyway to this more DRY and more reusable?


texts: Parse Bloomberg Excel/CSV with Pandas DataFrame

texts:
I retrieved  Bloomberg data using the Excel API.  In the typical fashion, the first row contains tickers in every fourth column, and the second row has the labels Date, PX_LAST, [Empty Column], Date, PX_LAST, etc.  The following rows have dates and last price.

   EHFI38 Index BBGID,                   , , EHFI139 Index BBGID,                    , ...
   Date              , PX_LAST           , , Date               , PX_LAST            , ...
   1999-12-31        , 100.0000          , , 1999-12-31         , 100.0000           , ...
   2000-01-31        , 100.1518          , , 2000-01-31         ,  98.6526           , ...
...

It seems that the proper data structure would be a DataFrame with dates as the index, and tickers as the column names.

    , Date,       EHFI38 Index BBGID, EHFI139 Index BBGID, EHFI139 Index BBGID, EHFI84 Index BBGID, ...
   0, 1999-12-31, 100.0000          , 100.0000           , 100.0000           , 100.0000, ...
   1, 2000-01-31, 100.1518          ,  98.6526           ,  98.6526           , 104.7575, ...
...

I wrote this code, which seems to work when I step through it, but I'm sure I'm not doing it well.  I'd like to learn how to do it better.

# IMPORT
import pandas as pd
import numpy as np
import datetime

# READ IN CSV FILES
# EHFI38 Index BBGID,                   , , EHFI139 Index BBGID,                    , ...
# Date              , PX_LAST           , , Date               , PX_LAST            , ...
# 1999-12-31        , 100.0000          , , 1999-12-31         , 100.0000           , ...
# 2000-01-31        , 100.1518          , , 2000-01-31         ,  98.6526           , ...
# ...
px = pd.read_csv('Book1.csv', sep=',', parse_dates=True)

# REMOVE EMPTY COLUMNS
px = px.dropna(axis=1, how='all')

# CONVERT TO ARRAYS
M = np.array(px)
C = np.array(px.columns)

# FIX UNNAMED COLUMNS IN C
for i in arange( len(C)/2 ) * 2:
    C[i+1] = C[i]

# CONVERT EXCEL DATES FUNCTION (THANKS JOHN MACHIN)
def xl2pydate(xldate, datemode):
    # datemode: 0 for 1900-based, 1 for 1904-based
    return (
        datetime.datetime(1899, 12, 30)
        + datetime.timedelta(days=xldate + 1462 * datemode)
    )

# CONVERT DATES THE UGLY WAY
# LOOP THROUGH 1,2, ... last row
for i in arange( len(M)-1 ) + 1:

    # LOOP THROUGH 0,2, ... last column-1
    for j in arange( len(M.T)/2 ) * 2:

        # CONVERT DATE & STORE
        if isinstance(M[i,j],str) and M[i,j].isdigit():
            M[i,j] = xl2pydate(int(M[i,j]), 0)
        else:
            M[i,j] = NaN

# RECOMBINE IN A DATAFRAME
df = pd.DataFrame(M[1:,:], columns=[C,M[0,:]])

# MERGE DATES
#  , Date,       EHFI38 Index BBGID, EHFI139 Index BBGID, EHFI139 Index BBGID, EHFI84 Index BBGID, ...
# 0, 1999-12-31, 100.0000          , 100.0000           , 100.0000           , 100.0000, ...
# 1, 2000-01-31, 100.1518          ,  98.6526           ,  98.6526           , 104.7575, ...
# ...

# LOOP 0,2,...,len-1
for i in arange( (len(df.T))/2 ) * 2:

    # GET A DATE, LAST_PX FOR A SINGLE TICKER
    b = df[df.columns[i:(i+2)]]

    # CHANGE COLUMN NAMES TO DATE, [TICKER]
    b.columns = (df.columns[i][1], df.columns[i][0])

    # COMBINE
    if i==0:
        a = b
    else:
        a = pd.merge(a.dropna(), b.dropna(), on='Date', how='outer')


texts: Count number of pairs of strings which on joining contain all vowels

texts:
Problem

I have \$N\$ words. For each valid \$i\$, word \$i\$ is described by a string \$D_i\$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'.

We check the number of strings \$D_i\$ and \$D_j\$ which, when concatenated into a new string \$M\$, contain all 5 vowels. What is the total number of (unordered) pairs of words such that when concatenated, they contain all the vowels?

Input

The first line of the input contains a single integer \$T\$ denoting the number of test cases. The description of \$T\$ test cases follows.
The first line of each test case contains a single integer \$N\$.
\$N\$ lines follow. For each valid \$i\$, the \$i^{th}\$ of these lines contains a single string \$D_i\$.

Output

For each test case, print a single line containing one integer - the number of concatenated words that contain all vowels.

Test Case

3
aaooaoaooa
uiieieiieieuuu
aeioooeeiiaiei

Output

2

Constraints

\$1≤T≤1,000\$
\$1≤N≤10^5\$
\$1≤|D_i|≤1,000\$ for each valid \$i\$
the sum of all |\$D_i\$| over all test cases does not exceed \$3⋅10^7\$

I have written some code but I want to optimize it:

#include<bits/stdc++.h>
using namespace std;

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    int n;
    cin>>n;
    vector<string> v;
    long long  con_s[n];
    for(int i=0;i<n;i++)
    {
      string s;
      cin>>s;
      con_s[i]=0;
      for(int j=0;j<s.length();j++)
      {
        con_s[i] = con_s[i] | (1<<(s[j]-'a'));
      }
    }
    long long complete = 0;
    complete = (1<<('a'-'a')) | (1<<('e'-'a')) | (1<<('i'-'a')) | (1<<('o'-'a')) | (1<<('u'-'a'));
    // cout<<complete;
    int count = 0;
    for(int i=0;i<n-1;i++)
    {
      for(int j=i+1;j<n;j++)
      {
        if((con_s[i] | con_s[j])==complete)count++;
      }
    }
    cout<<count<<"\n";
  }
  return 0;
}

Can you please suggest a more optimized solution...


texts: Passing uint64_t from C++ to R: Hilbert Mapping - xy2d function

texts:
I have been working with Rcpp to perform a forward and backward Hilbert Mapping.  Below is an implementation based on this code.

My application is in genomics and I may be dealing with enormous datasets, which necessitates the use of very large integers for indices, so I found this code for passing large integers to R using Rcpp and the bit64 R package and incorporated it after the for loop.

The xy2d() function works properly.  My interest is on your thought regarding the code AFTER the for loop, which prepared the result for passage back to R. Please let me know what you think  :)

#include <Rcpp.h>
using namespace Rcpp;
# include <bitset>
# include <cstdint>
# include <ctime>
# include <iomanip>
# include <iostream>
using namespace std;
//****************************************************************************80
// [[Rcpp::export]]
Rcpp::NumericVector xy2d ( int m, uint64_t x, uint64_t y )
//
//****************************************************************************80
{
  uint64_t d = 0;
  uint64_t n;
  int rx;
  int ry;
  uint64_t s;

  n = i4_power ( 2, m );

  if ( x > n - 1 || y > n - 1) {
    throw std::range_error("Neither x nor y may be larger than (2^m - 1)\n");
  }

  for ( s = n / 2; s > 0; s = s / 2 )
  {
    rx = ( x & s ) > 0;
    ry = ( y & s ) > 0;
    d = d + s * s * ( ( 3 * rx ) ^ ry );
    rot ( s, x, y, rx, ry );
  }

  std::vector<uint64_t> v;
  v.push_back(d);
  //v[0] = d

  size_t len = v.size();
  Rcpp::NumericVector nn(len);         // storage vehicle we return them in

  // transfers values 'keeping bits' but changing type
  // using reinterpret_cast would get us a warning
  std::memcpy(&(nn[0]), &(v[0]), len * sizeof(uint64_t));

  nn.attr("class") = "integer64";
  return nn;
}

This post will be followed up shortly with another post regarding the rot() function, and well as the reverse d2xy() function


texts:
Below is a piece of code which takes a list of IPs, pings the hosts and returns (prints) the list for "Alive" and "Dead". Any feedback on what can be done better is welcomed, mainly, speed and reliability! Would you accept it as production code? What about try/except blocks and logging?

import subprocess
from multiprocessing import Pool, cpu_count

hosts = ('127.0.0.1', 'google.com', '127.0.0.2', '10.1.1.1')

def ping(host):
    ret = subprocess.call(['ping', '-c', '5', '-W', '3', host], stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
    return ret == 0

host_list = {"Alive":[], "Dead":[]}

host_stat = {True: host_list["Alive"], False: host_list["Dead"]}

p = Pool(cpu_count())
[host_stat[success].append(host) for host, success in zip(hosts, p.map(ping, hosts))]
print host_stat


texts:
I'm developing a middleware for my client and it's found that there are some delay when executing the process. We are currently investigating what's the exact causes of the issue, from architecture design to coding.

Here is part of the script we are calling, which is working, and I'm just wondering if it's optimized and properly written.

public string getResponse(System.Type type, Object input, string taskName, string method)
{
    string response = string.Empty;

    try
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(type);
        MemoryStream mem = new MemoryStream();
        ser.WriteObject(mem, input);
        string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);

        using (WebClient webClient = new WebClient())
        {
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;

            logger.Info("Sending [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "]");

            response = webClient.UploadString(string.Format(Configuration.getTargetURI(), taskName), method, data);

            logger.Info("Response of API[" + string.Format(Configuration.getTargetURI(), taskName) + "] successfully retrieved");
            logger.Debug("Response: " + response);
        }
    }
    catch (Exception ex)
    {
        logger.Info("Failed to invoke [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "], Error: " + ex.Message);
        logger.Error(ex.Message + ex.StackTrace);

        throw ex;
    }

    return response;
}

public string getResponseNoInput(string taskName, string method)
{
    string response = string.Empty;

    try
    {
        WebRequest req = WebRequest.Create(String.Format(Configuration.getTargetURI(), taskName));
        req.Method = method;
        req.ContentType = "application/json; charset=utf-8";

        logger.Info("Sending [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "]");

        HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
        using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
        {
            response = sr.ReadToEnd();
        }

        logger.Info("Response of API[" + string.Format(Configuration.getTargetURI(), taskName) + "] successfully retrieved");
        logger.Debug("Response: " + response);
    }
    catch(Exception ex)
    {
        logger.Info("Failed to invoke [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "], Error: " + ex.Message);
        logger.Error(ex.Message + ex.StackTrace);

        throw ex;
    }

    return response;
}


texts:
I have implemented a Non Reentrant Lock. I want to know if this has any mistakes, race conditions etc. I am aware of the fact that existing libraries have to be used (instead of writing our own), but this is just to see if I am understanding the java concurrency correctly. Any feedback is appreciated.

public class MyLock {
private boolean isLocked = false;
private long owner = -1;
private static String TAG = "MyLock: ";

public synchronized void Lock() throws InterruptedException, IllegalStateException {
if(owner == Thread.currentThread().getId()) {
    throw new IllegalStateException("Lock already acquired. " +
                                    "This lock is not reentrant");
} else {
    while(isLocked == true) {
        System.out.println(TAG+"Waiting for Lock, Tid = " +
                Thread.currentThread().getId());
        wait();
    }
}

isLocked = true;
owner = Thread.currentThread().getId();
System.out.println(TAG+"Lock Acquired: Owner = " + owner);
}

public synchronized void Unlock() throws IllegalStateException {
if(!isLocked || owner != Thread.currentThread().getId()) {
    throw new IllegalStateException("Only Owner can Unlock the lock");
} else {
    System.out.println(TAG+"Unlocking: Owner = " + owner);
    owner = -1;
    isLocked = false;
    notify();
}
}


texts: Functions that converts day of year to month and day and reverse with error checking

texts:
There is no error checking in day_of_year or month_day. remedy this defect.

Here is the solution:

int day_of_year(unsigned int year, unsigned int month, int day) {
    int leap, i;

    leap = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));

    if(((month >= 1) && (month <= 12)) && ((day >= 1) && (day <= daytab[leap][month]))) {
        for(i = 1; i < month; i++) {
            day += daytab[leap][i];
        }

        return day;
    }

    return -1;

}

void month_day(unsigned int year, unsigned int yearday, int *pmonth, int *pday) {
    int leap, i;

    leap = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
    if((leap == 1 && (yearday >= 1 && yearday <= 366)) || (leap == 0 && (yearday >= 1 && yearday <= 366))) {

        for(i = 1; yearday > daytab[leap][i]; i++) {
          yearday -= daytab[leap][i];
        }
        *pday = yearday;
        *pmonth = i;
    }
    else {
        printf("error: the yearday is invalid");
    }
}

In the day_of_year's case I have to check if the yearday is a valide one. 1 <= yearday <= (365 || 366). I changed the parameters type to unsigned, because a day can't be negative nor a year.

In the month_day's case I check if the month is a valid one, it should be 1 <= month <= 12. After this, I check if the day belongs to a valid interval.

This exercise can be found in K&R2 at page 126.


texts: Gather list of all IDs ordered by submission date and ID by parsing file path

texts:
This code is to gather a sorted list of IDs (first by ID, then by date) recursively from a directory that submitted a particular type of file (that, as you can tell, includes the word "Magic") and then outputs the IDs paired with the modified date (the closest proxy I have for the submission date) to a CSV.  (Where I then dedupe by ID to get the earliest submission for each ID/find average number of unique IDs submitted a month over the year/ the total submitted for the year):

Get-ChildItem -Recurse -Include *Magic* |
Where-Object {$_.lastWriteTime -gt '01/01/2018' -AND $_.lastWriteTime -lt '12/31/2018'} |
Select-Object -Property Directory, lastWriteTime |
ForEach-Object -Begin {
     $holder=@{}
     $results=@()
} -Process {
    $holder.id= [regex]::match($_.Directory,'(\d{8})')
    $holder.modifiedDate = Get-Date  $_.lastWriteTime -Format 'yyyy/MM/dd'
    $results+=[pscustomobject]$holder
} -End {
    $results
    }  |
Sort-Object -Property @{Expression = "id"; Descending = $True;}, @{Expression = "modifiedDate"; Descending = $False} |
Export-csv C:\Users\REDACTED\Desktop\REDACTED.csv

This is my first attempt at writing something in powershell purely using the command prompt (which is to say, not scripting in the ISE as if it were Ruby using ifs and so forth) How could I have done this better and should I have organized the pipeline differently? I tried to avoid using variables like would have in the past and focused purely on piping the data I was interested in from beginning to end.

EDIT

Based on the comment I got I made these changes. I wasn't able to get rid of all the pipelines and I haven't been able to test if it's faster or not, but I have a feeling even this version has some garbage that could go.

$sourceFiles = Get-ChildItem -Recurse -Include *Magic*
$filteredFiles = @()
ForEach($file in $sourceFiles){
    If($file.LastWriteTime.Year -eq 2018){
        $filteredFiles+=[PSCustomObject]@{
            Agency = [regex]::match($file.Directory,'(\d{8})')
            ModifiedDate = Get-Date  $file.lastWriteTime -Format 'yyyy/MM/dd'
        }
    }
}
$filteredfiles | Sort-Object -Property @{Expression = "Agency"; Descending =
$True;}, @{Expression = "ModifiedDate"; Descending = $False} |
Export-csv C:\Users\REDACTED\Desktop\REDACTED.csv -NoTypeInformation


texts: Calculate the difference between the elements in a list

texts:
So I have two lists which have the same size. One is called roadS and the second one is called simTime. My aim is to calculate the difference between two indexes which lie next to each other and divide them with the difference of the other list. On the picture above you see what I mean.

This is my current function:

public List<Double> getSpeedS(List<Double> simTime, List<Double> roadS){
    List<Double>  speedS = new ArrayList<>();
    for(int idx = 1; idx < roadS.size();idx++){
        double curSpeedS = (roadS.get(idx)-roadS.get(idx-1))/(simTime.get(idx)-simTime.get(idx-1));
        speedS.add(curSpeedS);
    }
    return speedS;
}

Now although the function is quite short, I aim to find a better or faster solution.

For eg. in phyton you could you pantas library and do this:

player.df['speedS'] = player.df.roadS.diff().shift(-1) / player.df.simTime.diff().shift(-1)

My aim is to solve this problem more efficient. Does Java or another library maybe provide such functions where I can for calculate the differences


texts:
I don't want any feedback on the regexes as I know what needs to be updated here.  Also, don't need any feedback on naming conventions.

I'm looking for feedback on the structure and correctness of the class.

/***************************************************************************************************
**SForm - validates and manipulates form data
*/
    var SForm = $A.Class.create({
        Name: 'SForm',
         S: {
            domain:    /:\/\/(www\.)?([\.a-zA-Z0-9\-]+)/,
            url:       /:\/\/(www\.)?[\x00-\x7F]{1,1800}\.[\x00-\x7F]{1,200}/,
            email:     /\S{1,64}@[\x00-\x7F]{1,255}\.[\x00-\x7F]{1,255}/,
            tweet:     /\S{1,40}/,
            title:     /\S{1,32}/,
            name:      /\S{1,64}/,
            pass:      /\S{6,20}/,
            pre_url:   /(http:)|(https:)\/\//,
            full:      /\S+/,
            google:    'https://plus.google.com/_/favicon?domain='
        },
        constructor : function (form_elements) {
            this.form = {};
            $A.someKey(form_elements, function (val) {
                if (val.type !== 'checkbox') {
                    this.form[val.name] = val.value;
                } else if (val.type === 'checkbox') {
                    this.form[val.name] = val.checked;
                }
            }, this);
        },
        get: function (key) {
            return this.form[key];
        },
        set: function (key, value) {
            this.form[key] = value;
        },
        getObj: function () {
            return this.form;
        },
        checkField: function (key) {
            return this.S[key].test(this.form[key]);
        },
        checkFull: function () {
            var key;
            for (key in this.form) {

                // if it is not a boolean and it is not full
                if (!$A.isBoolean(this.form[key]) && !this.S.full.test(this.form[key]) ) {
                    return false;
                }
            }
            return true;
        },
        checkFullAM: function () {
            var key;
            for (key in this.form) {

                // if the fields is empty and it is not the tag field
                if (key !== "tag" && !this.S.full.test(this.form[key])) {
                    return false;
                }

                // if no tag is set, set one by default
                if (key === "tag" && !this.S.full.test(this.form[key])) {
                    this.form[key] = "no-tag";
                }
            }
            return true;
        },
        addURLPrefix: function () {

            // if there is no prefix, add http by default
            if (!this.S.pre_url.test(this.form.url)) {
                this.form.url = 'http://' + this.form.url;
            }
        },
        setDomain: function () {
            var domain = this.form.url.match(this.S.domain);
            if (domain) {
                this.form.domain = domain[2];
            }
        },
        setFaviconTemp: function () {
            this.form.favicon = this.S.google + this.form.domain;
        }
    }, 'constructor');


texts:
I know there is a better way to store data.  What is the most concise way to simplify this script?

from random import randint
from sys import exit
import os

os.system('clear')
print "Welcome to the dice rolling simulator!"
raw_input("Press enter to begin.")

total = 0

completed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

def roll():
    os.system('clear')
    die1 = randint(1, 6)
    die2 = randint(1, 6)
    global total
    total = die1 + die2
    storetotal()
    print "Die 1: %d \nDie 2: %d \nTotal: %d." % (die1, die2, total)
    print "\n\nRoll again?"
    roll_again = raw_input("Press enter to roll again, type 'stats' to view scores, or 'quit' to exit.\n> ")
    if roll_again == "":
        roll()
    elif roll_again == "stats":
        stats()
    elif roll_again == "quit":
        exit(0)
    else:
        print "I don't know what that means so you get to roll again."
        raw_input("> ")
        roll()

def stats():
    global total
    print "2s: %d \n3s: %d \n4s: %d \n5s: %d \n6s: %d \n7s: %d \n8s: %d" % (completed[0],
                                                    completed[1], completed[2], completed[3],
                                                    completed[4], completed[5], completed[6])
    print "9s: %d \n10s: %d \n11s: %d \n12s: %d""" % (completed[7], completed[8],
                                                    completed[9], completed[10])
    raw_input("")
    roll()

def storetotal():
    if total == 2:
        completed[0] += 1
    elif total == 3:
        completed[1] += 1
    elif total == 4:
        completed[2] += 1
    elif total == 5:
        completed[3] += 1
    elif total == 6:
        completed[4] += 1
    elif total == 7:
        completed[5] += 1
    elif total == 8:
        completed[6] += 1
    elif total == 9:
        completed[7] += 1
    elif total == 10:
        completed[8] += 1
    elif total == 11:
        completed[9] += 1
    else:
        completed[10] += 1

roll()


texts:
The script below should open a file, read and reverse every line, then overwrite the source file.

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

// reverses the order of characters in every line of SRC file

void validate_cmdline_args(int argc, char **argv) {
  if(argc != 2) {
    printf("Usage: %s SRC\n", argv[0]);
    exit(1);
  }
}

void reverse_string(char *str) {
  char *end = strchr(str, (int)'\0') - 1;
  char tmp;
  while(str < end) {
    tmp = *str;
    *str++ = *end;
    *end-- = tmp;
  }
}

void process_line(char *output_content, char *line) {
  reverse_string(line);
  strcat(output_content, line);
  strcat(output_content, "\n");
}

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

  char* file_content = NULL;
  char* output_content = NULL;
  char *file_content_free_pointer = NULL;

  int file_descriptor;
  if((file_descriptor = open(argv[1], O_RDONLY)) == -1)
    goto errno_error;

  struct stat file_stat;
  if(fstat(file_descriptor, &file_stat) == -1)
    goto errno_error;

  if((file_content = malloc((size_t)file_stat.st_size+1)) == NULL)
    goto errno_error;
  file_content_free_pointer = file_content;

  if(read(file_descriptor, file_content, (size_t)file_stat.st_size) == -1)
    goto errno_error;
  file_content[(int)file_stat.st_size] = '\0';

  if(close(file_descriptor) == -1)
    goto errno_error;

  if((output_content = malloc((size_t)file_stat.st_size+2)) == NULL)
    goto errno_error;
  output_content[0] = '\0';

  char* delim = "\n";
  char* line = NULL;

  while((line = strsep(&file_content, delim)))
    process_line(output_content, line);

  if((file_descriptor = open(argv[1], O_WRONLY | O_TRUNC)) == -1)
    goto errno_error;

  if(write(file_descriptor, output_content, (size_t)file_stat.st_size) == -1)
    goto errno_error;
  if(close(file_descriptor) == -1)
    goto errno_error;

  free(file_content_free_pointer);
  free(output_content);

  return 0;
errno_error:
  perror(NULL);
  free(file_content_free_pointer);
  free(output_content);
  return 1;
}

It's solely for fun and practicing Linux system programming, so I've decided not to divide it into smaller functions.

Here's what I find thought-provoking:

instead of reopening a file I'd prefer to open it in such a way that it becomes truncated just before writing,
this constant error-checking is a madness - can you propose a nicer solution?
I find this flow, with almost every single line beginning with if ... != NULL/-1 etc. a bit distracting
I'm not 100% sure if the code is devoid of off-by-one errors (ok, now I'm sure).


texts:
I have been practising Python Practice questions and came across this question:

Write a Python program to add 'ing' at the end of a given string
(length should be at least 3).
If the given string already ends with 'ing' then add 'ly' instead.
If the string length of the given string is less than 3, leave it
unchanged.   Sample String : 'abc'
Expected Result : 'abcing'
Sample String : 'string'
Expected Result : 'stringly'

Here's my code:
string = input()
if len(string) < 3:
  print(string)
elif string[-3:] == 'ing':
  print(string + 'ly')
else:
  print(string + 'ing')

However, something about this code seems wrong: though it works, fine I feel it smells bad. I can't pinpoint why.


texts: Creating an insert at front function for a circular doubly linked list

texts:
I have only been working with C for a couple of months and I think I have a pretty good function to  insert a node in the front of the circular doubly linked list.  I have tested in adding several nodes to it, but would like some feedback.

struct used in functions:

typedef struct TCB_t
{
    struct TCB_t * next;
    struct TCB_t * previous;
    ucontext_t context;
    int val;
}TCB_t;

Here is the function that calls the add_to_list() function:

void start_thread(void (*function)(void), int *arg)
{
    printf("In main: creating thread\n");
    struct stack * stackP = (struct stack*)malloc(8192);
    tcb = (struct TCB_t *)malloc(sizeof(struct TCB_t));
    init_TCB (tcb, function, stackP,  8192);
    tcb->val = iter;
    iter++;
    add_to_list( &ptr, &tcb);
}

This is the function in which I am not sure if I am adding the nodes correctly:

void add_to_list( struct TCB_t **ptrBlock, struct TCB_t **addQ)
{
    (*addQ)->val = iter;
    iter++;

    if(!*ptrBlock)
    {
        *ptrBlock = *addQ;
        (*ptrBlock)->next= *ptrBlock;
        (*ptrBlock)->previous = *ptrBlock;
    }
    else
    {
        (*ptrBlock)->previous->next = *addQ;
        (*addQ)->previous = (*ptrBlock)->previous;
        (*addQ)->next = *ptrBlock;
        (*ptrBlock)->previous = *addQ;
    }
}


texts:
I have a sort function that needed to sort in a certain way. That position was sorted by mobile-top first, then empty position followed by mobile-bottom. Then on these I needed to sort by their priority, with priority 1, 2 and then blank.

  widgetInfo = [
['box1','mobile-bottom',''],
['box2','mobile-top','priority-1'],
['box3','mobile-top',''],
['box3','mobile-top',''],
['box4','mobile-bottom',''],
['box2','mobile-top','priority-1'],
['box5','mobile-top','priority-2'],
['box6','','']],
 ['box5','mobile-top','priority-2'];

var newArray = widgetInfo.sort(function(a, b){

  if(a[1] == 'mobile-top'){
    if(b[1] == 'mobile-bottom'){
      return -1;
    }
    else if(b[1] == 'mobile-top'){

      if(a[2] == 'priority-1'){

        if(b[2] == 'priority-1'){
          return 0;
        }
        else if(b[2] == 'priority-2'){
          return -1;
        }
        else{
          return -1;
        }
      }

      else if(a[2] == 'priority-2'){

        if(b[2] == 'priority-1'){
          return 1;
        }
        else if(b[2] == 'priority-2'){
          return 0;
        }
        else{
          return -1;
        }
      }

      else{
        return 1;
      }

    }
    else{
      return -1;
    }
  }
  if(a[1] == 'mobile-bottom'){
   if(b[1] == 'mobile-top'){
     return 1;
   }
    else if(b[1] == 'mobile-bottom'){
     return 0;
   }
    else{
      return 1;
    }
  }

});

It seems like one heck of a lot of code to do this and I'm certain there will be better ways. Any suggestions?


texts: String pattern matching - method needs improving

texts:
I'm trying to build simple pattern matching in a string.

ie., Given a template of: "Hello ${1}" and an input of "Hello world", I'd like to be able to return a map of 1='world'.

This test demonstrates what I'm trying to acheive:

@Test
public void detectsTokenValues()
{
    Properties properties = formatter.getTokenValues("${1}/andThen/${2}/followedBy/${3}","start/andThen/middle/followedBy/end");
    assertThat((String) properties.get("1"), equalTo("start"));
    assertThat((String) properties.get("2"), equalTo("middle"));
    assertThat((String) properties.get("3"), equalTo("end"));
}

Here's my implementation.  It works, but I feel like it's convoluted, and I suspect there's Regex or other JDK native features I could be using to make it simpler.

// Matches ${0} - ${9999} etc.,  But not ${variblesWithWords}
private static final Pattern NUMERIC_VARIABLES = Pattern.compile("\\$\\{([0-9]+?)\\}");

Properties getTokenValues(String template, String input) {
    Properties properties = new Properties();
    Matcher matcher = NUMERIC_VARIABLES.matcher(sourcestring);
    final int variableWrapperLength = "${}".length();
    int accumulativeOffset = 0;
    while (matcher.find())
    {
        String variableName = matcher.group(1);
        String value = null;
        if (matcher.end() >= template.length())
        {
            value = input.substring(matcher.start() + accumulativeOffset);
        } else {
            // Find the character after the variable in the template.
            // This serves as the end-delimeter in the input string.
            String endDelimiter = template.substring(matcher.end(),matcher.end()+1);
            int variableEndIndex = findEndDelimiterIndex(input, matcher.start() + 1,
                    endDelimiter);
            value = input.substring(matcher.start() + accumulativeOffset, variableEndIndex);

            // Adjust the accumulative offset to account for the difference
            // in lengths between the variableName (eg., ${1})
            // and the the actual value (eg., 'foo')
            accumulativeOffset += value.length() - (variableWrapperLength + variableName.length());
        }

        if (value != null)
            properties.put(variableName, value);
    }
    return properties;
}

private int findEndDelimiterIndex(String input, int fromIndex,
        String endDelimiter) {

    // Find the first instance of the delimiter in the input string
    // that occurs after the start of our  matches variable.
    if (fromIndex > input.length())
    {
        return Integer.MAX_VALUE;
    }
    int variableEndIndex = input.indexOf(endDelimiter,fromIndex);
    return variableEndIndex;
}

How can I improve this method?


texts:
I am learning Haskell programming language mainly from this source. And there I have encouraged with "an elegant" realization of the quicksort sorting algorithm (the Quick, sort! section). Here it is:
Book's implementation
quicksort :: ( Ord a ) = > [ a ] -> [ a ]
quicksort [] = []
quicksort ( x : xs ) =
    let smallerSorted = quicksort [ a | a <- xs , a <= x ]
        biggerSorted = quicksort [ a | a <- xs , a > x ]
    in smallerSorted ++ [ x ] ++ biggerSorted

The problem with this implementation (as I think) is doubled number of comparisons inside the let binding every time. Isn't the fact that those of a's that already in the smallerSorted list cannot be in the biggerSorted list, so we don't need to compare x with them anymore.
In order to "improve" the above approach I have written my own implementation, where I use auxiliary local function split that splits a list into two parts: less than (or equal to) x and greater than x.
My implementation
quick_sort :: (Ord a) => [a] -> [a]
quick_sort []       = [] --edge condition
quick_sort (x : xs) =    --general condition
    let (lt, gt) = split x xs
    in  (quick_sort lt) ++ [x] ++ (quick_sort gt)
    where
        --split function is used to split a list
        --into two sublists: one for elements
        --less or equal (lt) than some value - x
        --and one for those that greater (gt) than x
        --NOTE: split function is also recursive
        split x [] = ([], []) --edge condition
        split x (h : hs)      --general condition
            | h <= x    =
                let (lt, gt) = split x hs
                in ( (h : lt), gt )
            | otherwise =
                let (lt, gt) = split x hs
                in ( lt, (h : gt) )

P.S.
For a while I am not able to compare two approaches, but on the worst cases (when list to sort is already sorted) it seems that my implementation is a bit slower.


texts: Simple bank-account application using composition

texts:
I'm trying to use composition to design an application that works with different types of bank-accounts. Instead of deriving from a Account Class I want to have separate account classes that are created with appropriate behaviours with a factory. For the example below can I please get some comments on:

Overall design decision
Testability
As i'm adding more behaviours/features does it look sustainable to keep passing objects in constructor?

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

using namespace std;

class Balance {
public:
  Balance() {}
  Balance(double amount, std::string currency)
      : d_amount(amount), d_currency(currency) {}
  double amount() const { return d_amount; }
  std::string currency() const { return d_currency; }

private:
  double d_amount;
  std::string d_currency;
};

std::ostream &operator<<(std::ostream &os, const Balance &balance) {
  os << "Balance->" << balance.amount() << ":Currency->" << balance.currency();
  return os;
}

class InterestRate {
private:
  double d_interest_rate;

public:
  virtual double interest_rate() const = 0;
};

class CurrentAccountInterestRate : public InterestRate {
public:
  double interest_rate() const override { return 0.5; }
};

class SavingsAccountInterestRate : public InterestRate {
public:
  double interest_rate() const override { return 2.0; }
};

std::ostream &operator<<(std::ostream &os, const InterestRate &interest_rate) {
  os << "Interest-Rate->" << interest_rate.interest_rate() << std::endl;
  return os;
}

class CurrentAccount {
public:
  CurrentAccount(const Balance &balance,
                 std::unique_ptr<InterestRate> interest_rate)
      : d_balance(balance), d_interest_rate(std::move(interest_rate)) {}

  void print_balance() { std::cout << d_balance << ":" << *d_interest_rate; }

private:
  Balance d_balance;
  std::unique_ptr<InterestRate> d_interest_rate;
};

class SavingsAccount {
public:
  SavingsAccount(const Balance &balance,
                 std::unique_ptr<InterestRate> interest_rate)
      : d_balance(balance), d_interest_rate(std::move(interest_rate)) {}

  void print_balance() { std::cout << d_balance << ":" << *d_interest_rate; }

private:
  Balance d_balance;
  std::unique_ptr<InterestRate> d_interest_rate;
};

class AccountFactory {
public:
  static CurrentAccount create_current_account(double amount,
                                               const std::string &currency) {
    std::unique_ptr<InterestRate> currentAccountInterestRate =
        std::make_unique<CurrentAccountInterestRate>();
    CurrentAccount ca{Balance{amount, currency},
                      std::move(currentAccountInterestRate)};
    return ca;
  }

  static SavingsAccount create_savings_account(double amount,
                                               const std::string &currency) {
    std::unique_ptr<InterestRate> savingsAccountInterestRate =
        std::make_unique<SavingsAccountInterestRate>();
    SavingsAccount sa{Balance{amount, currency},
                      std::move(savingsAccountInterestRate)};
    return sa;
  }
};

int main() {
  CurrentAccount ca = AccountFactory::create_current_account(20.5, "USD");
  ca.print_balance();
  SavingsAccount sa = AccountFactory::create_savings_account(40.5, "GBP");
  sa.print_balance();
  return 0;
}


texts:
I want to produce a Stream<Pixel> from a Pixel[][]. My code for this is extremely verbose so I suspect I am doing something bad.

Please provide me some advice on how to make my code shorter and cleaner.

private Stream<Pixel> stream() {
    Iterable<Pixel> iterable = new Iterable<Pixel>() {
        @Override
        public Iterator<Pixel> iterator() {
            return new Iterator<Pixel>() {
                private int row = 0;
                private int col = 0;

                @Override
                public boolean hasNext() {
                    return row < height && col < width;
                }

                @Override
                public Pixel next() {
                    Pixel pixel = pixels[row][col];

                    if (col == width - 1) {
                        col = 0;
                        row++;
                    } else {
                        col++;
                    }

                    return pixel;
                }

                @Override
                public void remove() {
                    throw new RuntimeException("nope");
                }
            };
        }
    };

    return StreamSupport.stream(iterable.spliterator(), false);
}

For example, in Python I could do something like:

def stream():
    for row in pixels:
        for pixel in row:
            yield pixel

Which is certainly much more concise and understandable.


texts:
I'm trying to calculate with SUMIFS and COUNTIFS some data.

The output sheet looks like this:

I've hidden some irrelevant or sensitive data to make it easy to go to the point.

My code runs an array on this worksheet and calculates per each day and user some KPIs I need from this worksheet:

The calculation is pretty simple, but it impacts heavily on the performance:

Option Explicit
Sub Test()

    With Hoja4
        Dim arrTotal As Variant: arrTotal = .UsedRange.Value
        Dim Fecha As Double
        Dim i As Long
        For i = 2 To UBound(arrTotal)
            Fecha = arrTotal(i, 1)
            arrTotal(i, 13) = CalculaLlamadas(CStr(arrTotal(i, 2)), Fecha, 1, 4)
            arrTotal(i, 14) = CalculaFCR(CStr(arrTotal(i, 2)), Fecha, 1, 4)
            arrTotal(i, 15) = CalculaLlamadas(CStr(arrTotal(i, 2)), Fecha, 3, 5)
            arrTotal(i, 16) = CalculaFCR(CStr(arrTotal(i, 2)), Fecha, 3, 5)
            arrTotal(i, 17) = CalculaLlamadas(CStr(arrTotal(i, 2)), Fecha, 30, 6)
            arrTotal(i, 18) = CalculaFCR(CStr(arrTotal(i, 2)), Fecha, 30, 6)
        Next i
        .UsedRange.Value = arrTotal
    End With

End Sub
Private Function CalculaFCR(Agente As String, Fecha As Double, Dias As Byte, Columna As Byte) As Long

    Dim FechaFin As Double: FechaFin = Fecha + Dias
    With Hoja37
        CalculaFCR = Application.SumIfs(.Columns(Columna), .Columns(1), Agente, _
            .Columns(2), ">=" & Fecha, .Columns(2), "<=" & FechaFin)
    End With

End Function
Private Function CalculaLlamadas(Agente As String, Fecha As Double, Dias As Byte, Columna As Byte) As Long

    Dim FechaFin As Double: FechaFin = Fecha + Dias
    With Hoja37
        CalculaLlamadas = Application.CountIfs(.Range("A:A"), Agente, _
            .Range("B:B"), ">=" & Fecha, .Columns(2), "<=" & FechaFin)
    End With
    CalculaLlamadas = LLamadas

End Function

As you can see on the code, the logic is pretty easy calculate how many times an user shows between 2 dates, and get the sum of a column for these dates.

I can't figure out how to make this quicker, I've also tried using an array to loop through it instead using the countif and the sumif, but it takes even longer because the input sheet has 40k (1 and half month of data) rows and its likely to be increased with days to come.

Any suggestions on how to improve the performance for this task?


texts: Logger to syslog with error_log fallback

texts:
Below is an implementation of a typical Logger interface that uses syslog() as the default means to store messages and falls back to error_log() if that does not work.  This is the first time I've used syslog and just want to ensure that my code is logical and there isn't something about syslog that I'm not aware of that could come back and bite me.  The code does work, messages are appearing in syslog as appropriate.

Project github repo
Source code for Logger interface
Source code for extended class

<?php

/**
 * @file
 * @brief Holds a class that implements error logging to an OS or system logger.
 */

namespace SprayFire\Logger;

/**
 * @brief A SprayFire.Logger.Log implementation that will attempt to log a message
 * to syslog and as a fallback will log the message to whatever option is set in
 * the error_log php.ini directive.
 *
 * @uses SprayFire.Logger.Log
 * @uses SprayFire.Core.CoreObject
 */
class SystemLogger extends \SprayFire\Core\CoreObject implements \SprayFire\Logger\Log {

    /**
     * @brief A flag to tell if fallback should be used.
     *
     * @property $syslogOpened
     */
    protected $syslogOpened = false;

    /**
     * @param $syslogIdent
     */
    public function __construct($syslogIdent = 'SprayFire') {
        $this->syslogOpened = \openlog($syslogIdent, \LOG_NDELAY, \LOG_USER);
    }

    /**
     * @param $syslogSeverity The timestamp for the \a $message being logged
     * @param $message The message to be logged
     * @return true if the message was logged, false if not
     */
    public function log($syslogSeverity, $message) {
        $loggedtoSyslog = $this->logToSyslog($syslogSeverity, $message);
        if (!$loggedtoSyslog) {
            return $this->logToErrorLog($message);
        }
        return $loggedToSyslog;
    }

    /**
     * @param $syslogSeverity The severity of the syslog message to store
     * @param $message The message to store in syslog
     * @return true if the message was logged into syslog, false if it wasn't
     * @see http://www.php.net/syslog
     */
    protected function logToSyslog($syslogSeverity, $message) {
        if ($this->syslogOpened) {
            return \syslog($syslogSeverity, $message);
        }
        return false;
    }

    /**
     * @param $message The message to log with error_log
     * @return true if logged, false if not
     * @see http://php.net/manual/en/function.error-log.php
     */
    protected function logToErrorLog($message) {
        $message = \date('M-d-Y H:i:s') . ' := ' . $message;
        return \error_log($message);
    }

    /**
     * @brief Ensures that the syslog is properly closed if it was opened.
     */
    public function __destruct() {
        if ($this->syslogOpened) {
            \closelog();
        }
    }

}

One question that I would have is how to unit test this?  How would I disable syslog to ensure the fallback is called?  Or is there some other way to do it?


texts:
I've noticed a tendency towards two styles of conditionally executing methods:

Conditional before the call

def foo(thing)
  puts thing
end

if thing
  foo(thing)
end

Conditional within the call

def foo(thing)
  return unless thing
  puts thing
end

foo(thing)

Pros of 1:

Method is concise and easier to read
Method can assume input is valid
Obvious what's going on in calling code

Pros of 2:

Caller doesn't need to check condition every time the method is invoked
Calling code is cleaner and easier to read
Conditional is centralised in one location and not duplicated (DRY!)

Anecdotally, I've noticed that most people tend to lean towards the first style. Lately I've found my self leaning towards the second style, for the pros I've given above.

Thoughts? Any other advantages/disadvantages I've missed? Any other styles?


texts:
Here is my implementation of Huffman encoding using min-heap approach mentioned in this Wikipedia page. I'm looking for advice, using Python 2.7.

import heapq
from collections import defaultdict
class TreeNode:
    def __init__(self, name, left, right, value):
        self.name = name
        self.left = left
        self.right = right
        self.value = value
    def __cmp__(self, other):
        return cmp(self.value, other.value)
    @staticmethod
    def merge_tree(root1, root2):
        return TreeNode(root1.name+"_"+root2.name, root1, root2, root1.value + root2.value)
    @staticmethod
    def print_tree(root):
        if not root:
            return
        buf = []
        buf.append(root)
        layer_count = 1
        next_layer_count = 0
        layer_result = []
        while len(buf) > 0:
            node = buf.pop(0)
            layer_result.append(str(node.name))
            layer_count -= 1
            if node.left:
                buf.append(node.left)
                next_layer_count += 1
            if node.right:
                buf.append(node.right)
                next_layer_count += 1
            if layer_count == 0:
                print '\t'.join(layer_result)
                layer_result = []
                layer_count = next_layer_count
                next_layer_count = 0
    @staticmethod
    def encode(root, prefix, result):
        if not root:
            return
        # left encode 0, right encode 1
        if not root.left and not root.right:
            result[root.name] = prefix
        TreeNode.encode(root.left, prefix+'0', result)
        TreeNode.encode(root.right, prefix+'1', result)

if __name__ == "__main__":
    n1 = TreeNode('a1', None, None, 0.4)
    n2 = TreeNode('a2', None, None, 0.35)
    n3 = TreeNode('a3', None, None, 0.2)
    n4 = TreeNode('a4', None, None, 0.05)
    h = [n1, n2, n3, n4]
    heapq.heapify(h)
    while len(h) > 1:
        c1 = heapq.heappop(h)
        c2 = heapq.heappop(h)
        heapq.heappush(h, TreeNode.merge_tree(c1, c2))

    TreeNode.print_tree(h[0])
    result = defaultdict(str)
    TreeNode.encode(h[0],'', result)
    print result


texts: Checking if a value is in the RGB range and then increment/decrement

texts:
Is there a better way to check if the variable x is in the 0-255 range and:

if x is less than 255, add colVel to it
if x equals 255 minus colVel from it.

My implementation, in Python 3.8.
if 0 <= x < colVel:
    flag = True
if (255-colVel) < x < 255:
    x = 255
if (255-colVel) <= x <= 255:
    flag = False
if flag == True:
    x += colVel
if flag == False:
    x -= colVel

This section of code is a function that creates iridescence while an object moves. This is all of my code, and I'm happy for it to be reviewed.
import pygame
import random
pygame.init()

win = pygame.display.set_mode((800,600))

pygame.display.set_caption('Chachi')
x = 50
y = 50
width = 2
height = 2
vel = 5
colVel = 10
rf = True
gf = True
bf = True
running = True

def irid(r, g, b, rFlag=True, gFlag=True, bFlag=True):
        def rgbRange(x, flag):
                if 0<= x< colVel:
                        x = random.randint(1, 100)
                        flag = True
                if (255-colVel) < x < 255:
                        x = 255
                if (255-colVel) <= x <= 255:
                        flag = False
                if flag==True:
                        x += colVel
                if flag==False:
                        x -= colVel
                return x, flag
        r, rFlag = rgbRange(r, rFlag)
        g, gFlag = rgbRange(g, gFlag)
        b, bFlag = rgbRange(b, bFlag)
        return r, g, b, rFlag, gFlag, bFlag
r = irid(0, 255, 0)[0]
g = irid(0, 255, 0)[1]
b = irid(0, 255, 0)[2]

while running:
    pygame.time.delay(1)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        x -= vel
        r, g, b, rf, gf, bf = irid(r, g, b, rf, gf, bf)

    if keys[pygame.K_RIGHT]:
        x += vel
        r, g, b, rf, gf, bf = irid(r, g, b, rf, gf, bf)
    if keys[pygame.K_UP]:
        y -= vel
        r, g, b, rf, gf, bf = irid(r, g, b, rf, gf, bf)
    if keys[pygame.K_DOWN]:
        y += vel
        r, g, b, rf, gf, bf = irid(r, g, b, rf, gf, bf)

    pygame.draw.rect(win, (r, g, b), (x, y, width, height) )
    pygame.display.update()

pygame.quit()


texts: Pin an array in memory and construct a Bitmap using that buffer

texts:
I'm using some resources in the code below to pin an array in memory and construct a Bitmap using that buffer. I'm not sure if I'm handling all of the resourced in the proper way.

I'm interested if the way I'm handling the GCHandle and other resources in the constructor in a safe manner, so there are no resource leaks.

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;

namespace Model
{
    public class FastImage
    {
        private const int BytesPerPixel = 4;
        private readonly byte[] _buffer;
        private const int ExtraSpace = 0;

        public int Width { get; set; }
        public int Height { get; set; }

        public FastImage(int width, int height)
        {
            Width = width;
            Height = height;
            _buffer = new byte[Width * Height * BytesPerPixel];
        }

        public FastImage(Bitmap from)
        {
            Width = from.Width;
            Height = from.Height;
            _buffer = new byte[Width * Height * BytesPerPixel];
            GCHandle handle = GCHandle.Alloc(_buffer);
            try
            {
                var address = Marshal.UnsafeAddrOfPinnedArrayElement(_buffer, 0);
                var stride = BytesPerPixel * Width;
                var pixelFormat = PixelFormat.Format32bppArgb;
                using (var fastBitmap = new Bitmap(Width, Height, stride, pixelFormat, address))
                {
                    using (var fastBitmapGraphics = Graphics.FromImage(fastBitmap))
                        fastBitmapGraphics.DrawImageUnscaled(from, 0, 0);
                }
            }
            finally
            {
                handle.Free();
            }
        }

        public Color GetPixel(int x, int y)
        {
            var i = y * BytesPerPixel + x;
            return Color.FromArgb(_buffer[i], _buffer[i + 1], _buffer[i + 2], _buffer[i + 3]);
        }

        public void SetPixel(int x, int y, Color color)
        {
            var i = y * BytesPerPixel + x;
            _buffer[i] = color.A;
            _buffer[i + 1] = color.R;
            _buffer[i + 2] = color.G;
            _buffer[i + 3] = color.B;
        }

        public void Save(string filename)
        {
            GCHandle handle = GCHandle.Alloc(_buffer);
            try
            {
                var address = Marshal.UnsafeAddrOfPinnedArrayElement(_buffer, 0);
                var stride = BytesPerPixel * Width;
                var pixelFormat = PixelFormat.Format32bppArgb;
                using (var fastBitmap = new Bitmap(Width, Height, stride, pixelFormat, address))
                {
                    fastBitmap.Save(filename);
                }
            }
            finally
            {
                handle.Free();
            }
        }

        public void Save(MemoryStream stream, ImageFormat format)
        {
            GCHandle handle = GCHandle.Alloc(_buffer);
            try
            {
                var address = Marshal.UnsafeAddrOfPinnedArrayElement(_buffer, 0);
                var stride = BytesPerPixel * Width;
                var pixelFormat = PixelFormat.Format32bppArgb;
                using (var fastBitmap = new Bitmap(Width, Height, stride, pixelFormat, address))
                {
                    fastBitmap.Save(stream, format);
                }
            }
            finally
            {
                handle.Free();
            }
        }
    }
}


texts: Scan first and last name from user, then format the name as last, first initial

texts:
Reads the user's first and last name (an entire line as a single string), then prints the last name followed by a comma and the first initial. Ensure the code re-prompts until the user types a name that is at least 5 letters total in length and has at least one space in it. For example:

Type your name: Jessica Miller
Your name is: Miller, J.

A few questions I have about my code:

Is there a difference between breaking out of a loop with a break statement, and meeting the condition for correctName?
Did I make my program do unnecessary things? I'd like to answer this for myself, but I may've made it unnecessarily complex, or even buggy, due to my thought process. For instance, can I check if there's a space in my first conditional, where I check if the length of the line is greater than or equal to five?

Any feedback is welcome!
Scanner console = new Scanner(System.in);
boolean correctName = false;

while(correctName != true) {
    System.out.print("Type your name: ");
    String line = console.nextLine();
    if(line.length() >= 5) {
        StringTokenizer lineTokens = new StringTokenizer(line);
        while(lineTokens.hasMoreTokens()) {
            if(lineTokens.countTokens() >= 2) {
                String first = lineTokens.nextToken();
                String last = lineTokens.nextToken();
                String name = (last + ", " + first.substring(0, 1) + ".");
                System.out.println("Your name is: " + name);
                correctName = true;
            } else {
                String checkSpace = lineTokens.nextToken();
                for(int i = 0; i < checkSpace.length(); i++) {
                    if(checkSpace.charAt(i) == ' ') {
                        break;
                    } else {
                        System.out.println("Error, must be at least 5 chars with a space.");
                        break;
                    }
                }
           }
        }
    } else {
        System.out.println("Error, must be at least 5 chars with a space.");
    }
}


texts: Rendering explosion of bouncing balls on a canvas

texts:
I'm making a particle engine that can spawn new explosions while keeping particles from previous ones active (until they despawn).

The code works well on its own but within the game, two or more active explosions at once causes stutters, especially on laptops that are a few years old. I'm wondering if there's something inherently inefficient with my code, or if any of the coding practices used are bad.

In in the code snippet below, you can click anywhere within the canvas element to see the particle effect. Let me know if there's something that's unclear.

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var explosions = [];
var mouseX;
var mouseY;

canvas.addEventListener('mousemove', setMouse, false);

canvas.addEventListener('click', function() {
    explosions.push(new explosion(mouseX, mouseY));
}, false);

function loop() {
    ctx.clearRect(0, 0, 500, 500);
    drawExplosion();
    requestAnimationFrame(loop);
}

loop();

function drawExplosion() {
    if (explosions.length === 0) {
        return;
    }

    for (let i = 0; i < explosions.length; i++) {
        const explosion = explosions[i];
        const projectiles = explosion.projectiles;

        if (projectiles.length === 0) {
            explosions.splice(i, 1);
            return;
        }

        const projectilesRemove = projectiles.slice();

        for (let ii = 0; ii < projectiles.length; ii++) {
            const projectile = projectiles[ii];

            // remove projectile if radius is below 0
            if (projectile.radius < 0) {
                projectilesRemove.splice(ii, 1);
                continue;
            }

            // draw
            ctx.beginPath();
            ctx.arc(projectile.x, projectile.y, projectile.radius, Math.PI * 2, 0, false);
            ctx.closePath();
            ctx.fillStyle = 'hsl(' + projectile.h + ',' + projectile.s + '%,' + projectile.l + '%)';
            ctx.fill();

            // update
            projectile.x -= projectile.vx * 1;
            projectile.y -= projectile.vy * 1;
            projectile.radius -= 0.02;

            // collisions
            if (projectile.x > 500) {
                projectile.x = 500;
                projectile.vx *= -1;
            }

            if (projectile.x < 0) {
                projectile.x = 0;
                projectile.vx *= -1;
            }

            if (projectile.y > 500) {
                projectile.y = 500;
                projectile.vy *= -1;
            }

            if (projectile.y < 0) {
                projectile.y = 0;
                projectile.vy *= -1;
            }
        }

        explosion.projectiles = projectilesRemove;
    }
}

function explosion(x, y) {
    this.projectiles = [];

    for (let i = 0; i < 100; i++) {
        this.projectiles.push(
            new projectile(x, y)
        );
    }
}

function projectile(x, y) {
    this.x = x;
    this.y = y;
    this.radius = 2 + Math.random() * 4;
    this.vx = -10 + Math.random() * 20;
    this.vy = -10 + Math.random() * 20;
    this.h = 200;
    this.s = Math.floor((Math.random() * 100) + 70);
    this.l = Math.floor((Math.random() * 70) + 30);
}

function setMouse(e) {
    mouseX = e.offsetX;
    mouseY = e.offsetY;
}
<!DOCTYPE html>
<html>
<body>
<canvas id="canvas" width="500" height="500" style="border:1px solid #000;"></canvas>
</body>
</html>


texts:
This inserts into 3 tables really quickly. My SELECT at the bottom appears to take an age, I assumed it would be fairly quick but is not. Is the XML path the right way to go?

DECLARE @TEAMMEMBER TABLE
        (CAREPROVIDEROID BIGINT, Team VARCHAR(MAX))
DECLARE @MAINSPECIALTY TABLE
        (CAREPROVIDEROID BIGINT, MainSpecialty VARCHAR(MAX))
DECLARE @TREATSPECIALTY TABLE
        (CAREPROVIDEROID BIGINT, TreatSpecialty VARCHAR(MAX))

INSERT INTO @TEAMMEMBER
    SELECT
    TEAMMEM.CAREPROVIDEROID as CAREPROVIDEROID,
    CAST(REPLACE(TEAM.NAME,'&','AND')+'('+TEAM.IDENTIFIER+')' AS NVARCHAR(MAX)) as Team
    FROM LZO_TEAMMEMBER TEAMMEM
    INNER JOIN LZO_TEAM TEAM
    ON TEAM.OID = TEAMMEM.TEAMOID
    AND TEAM.STATUS = 'A'
    --ORDER BY TEAMMEM.CAREPROVIDEROID

INSERT INTO @MAINSPECIALTY
    SELECT USERRP.USERSOID as USERSOIDMAINSPECIALTY,
    CAST(s.NAME +'('+s.MAINIDENTIFIER+')' AS NVARCHAR(MAX)) as MainSpec
    FROM LZO_USERROLEPROFILE USERRP

    INNER JOIN LZO_ROLEPROFILEENTERPRISEOBJECT RPEO
    ON USERRP.ROLEPROFILEOID = RPEO.ROLEPROFILEOID
    AND 'SPECIALTY' = RPEO.IDENTIFYINGTYPE
    AND RPEO.STATUS = 'A'
    INNER JOIN LZO_Specialty s
    ON s.OID = RPEO.IdentifyingOID and s.SPETYCode = 'CC_CPSPE' --Specialty
    AND RPEO.STATUS = 'A'
    WHERE 1=1
    AND USERRP.STATUS='A'
    --ORDER BY USERRP.USERSOID

INSERT INTO @TREATSPECIALTY
    SELECT
    USERRP.USERSOID as USERSOIDTREATSPECIALTY,
    CAST(s.NAME +'('+s.MAINIDENTIFIER+')' AS NVARCHAR(MAX)) as TreatSpec
    FROM LZO_USERROLEPROFILE USERRP
    INNER JOIN LZO_ROLEPROFILEENTERPRISEOBJECT RPEO
    ON USERRP.ROLEPROFILEOID = RPEO.ROLEPROFILEOID
    AND 'SPECIALTY' = RPEO.IDENTIFYINGTYPE
    AND RPEO.STATUS = 'A'
    INNER JOIN LZO_Specialty s
    ON s.OID = RPEO.IdentifyingOID and s.SPETYCode = 'CC_TRTFN' --Treatment Function
    AND RPEO.STATUS = 'A'
    WHERE 1=1
    AND USERRP.STATUS='A'
    --ORDER BY USERRP.USERSOID

Select OID,
       (SELECT sub.Team + ', ' AS [data()]
            from @TEAMMEMBER as sub
            Where sub.CAREPROVIDEROID = LZO_USERS.OID
            Order by sub.Team asc
                  FOR XML PATH('')
        ) as [Teams]

FROM LZO_USERS WHERE STATUS = 'A'


texts:
This code will take as output any string (long, empty, with spaces...) and will return its reverse. Can I improve this algorithm so that it can be faster?

Right now its complexity is \$O(n)\$.

def reverse(stri):
    output = ''
    length = len(stri)
    while length > 0:
        output += stri[-1]
        stri, length = (stri[0:length - 1], length - 1)
    return output