texts:
I want to force my compiler (C++ or perhaps CUDA) to unroll certain loops that it would rather not unroll. So, I've written the following:

template <size_t N> struct unroller {
    template <typename ForwardIterator, typename Function>
    static void unroll(const Function& f, ForwardIterator& it) {
        unroll<N/2, ForwardIterator, Function>(f, it);
        ++it;
        unroll<N-N/2, ForwardIterator, Function>(f, it);
    }
};
template <> struct unroller<0> {
    template <typename ForwardIterator, typename Function>
    static void unroll(const Function& f, ForwardIterator& it) { }
};

template <> struct unroller<1> {
    template <typename ForwardIterator, typename Function>
    static void unroll(const Function& f, ForwardIterator& it) { f(it); }
};

I can also manually write in the instances at sizes 2,3,4 or so to perhaps reduce the amount of work the compiler needs to do. However, I was thinking maybe I should be doing something involving template packs and index lists to really cut down on the amount of instantiation. What do you think?

Sample usage:

#include <iostream>

// unroller code here

int main()
{
    constexpr unsigned unroll_factor = 3;
    constexpr unsigned total_iterations = 12;

    static_assert(total_iterations % unroll_factor == 0,
        "Unroll factor must divide the number of iterations");

    auto f = [](int n) { std::cout << n << ' ' << std::flush; };
    std::cout << "no manual unrolling: ";
    for(int i = 0; i < total_iterations; i++) { f(i); }
    std::cout << std::endl << "manual unrolling x " << unroll_factor << ": ";
    for(int i = 0; i < total_iterations; i++) {
        unroller<unroll_factor>::unroll(f, i);
    }
    std::cout << std::endl;
    return 0;
}


texts: Test if two IDictionary objects contain the same values

texts:
I'm adding a function to my test library to assist in testing if two IDictionary objects contain the same keys/values.

I need the method to be generic and support a dictionary that has collections as values. So I thought I'd try to do it without using class types.

It appears to be working, but I would like to clarify that I didn't make any mistakes that would impact my tests later.

    /// <summary>
    /// Checks if two dictionaries contain the same values. Supports recursive
    /// dictionaries and collections as values.
    /// </summary>
    /// <param name="pExpect">Expected value</param>
    /// <param name="pActual">Actual value</param>
    // ReSharper disable CanBeReplacedWithTryCastAndCheckForNull
    public static void dictionary(IDictionary pExpect, IDictionary pActual)
    {
        Assert.IsNotNull(pExpect);
        Assert.IsNotNull(pActual);

        if (pExpect.Keys.Count != pActual.Keys.Count)
        {
            Assert.Fail("Expected {0} keys, but contains {1} keys.", pExpect.Keys.Count, pActual.Keys.Count);
        }

        object[] expectKeys = new object[pExpect.Keys.Count];
        object[] actualKeys = new object[pExpect.Keys.Count];

        pExpect.Keys.CopyTo(expectKeys, 0);
        pActual.Keys.CopyTo(actualKeys, 0);

        // check if the two key sets are the same
        CollectionAssert.AreEquivalent(expectKeys, actualKeys);

        for (int i = 0, c = expectKeys.Length; i < c; i++)
        {
            object expect = pExpect[expectKeys[i]];
            object actual = pActual[actualKeys[i]];

            // both can be null
            if (expect == null && actual == null)
            {
                continue;
            }

            // both must be assigned a value
            Assert.IsNotNull(expect);
            Assert.IsNotNull(actual);

            // must be same types
            Assert.AreEqual(expect.GetType(), actual.GetType());

            if (expect is IDictionary)
            {
                // support recursive dictionary checks
                dictionary((IDictionary)expect, (IDictionary)actual);
            }
            else if (expect is ICollection)
            {
                CollectionAssert.AreEquivalent((ICollection)expect, (ICollection)actual);
            }
            else
            {
                Assert.AreEqual(expect, actual);
            }
        }
    }

I am wondering if my last check Assert.AreEqual(expect, actual) will cover most remaining test cases.

EDIT: Fixed testing dictionaries that have keys in different orders.


texts:
Count the numbers without consecutive repeated digits

Asha loves mathematics a lot. She always spends her time by playing with digits. Suddenly, she is stuck with a hard problem, listed below. Help Asha to solve this magical problem of mathematics.

Given a number N (using any standard input method), compute how many integers between 1 and N, inclusive, do not contain two consecutive identical digits. For example 1223 should not be counted, but 121 should be counted.

Test Cases

7 => 7
1000 => 819
3456 => 2562
10000 => 7380

The code snippet that i submitted  for the above challenge was given below

f(int n)
{
    int i=1;
    int c=n;

    for(; i<=n; i++)
    {
        int m=-1;
        int x=i;

        while(x)
        {
            if (x % 10 == m)
            {
                c--;
                break;
            }
            m = x % 10;
            x /= 10;
       }
   }
   return c;
}

When I submitted the above code in the competition a few days ago I ranked the First. but yesterday I was at no 10-11-13 and now I am ranked 13 right now.

What was my mistake? What should I improve so that my rank will be the first in competitive programming?


texts:
The idea here is to implement a simple, threadsafe, observable collection that clients can bind to, whilst background threads can update. Changes in the contained items raise the CollectionChanged event.

Feedback appreciated:

public class ThreadSafeObservableCollection<T> : INotifyCollectionChanged
    where T: INotifyPropertyChanged
{
    private readonly Object _lock = new Object();
    private readonly Collection<T> _items = new Collection<T>();
    private ReadOnlyCollection<T> _readonlyItems;

    public ThreadSafeObservableCollection()
    {

    }

    public ThreadSafeObservableCollection(IEnumerable<T> items)
    {
        _items = new ObservableCollection<T>(items);
        foreach (T item in _items)
        {
            ((INotifyPropertyChanged) item).PropertyChanged += ItemPropertyChangedHandler;
        }
    }

    public ReadOnlyCollection<T> Items
    {
        get { return _readonlyItems ?? (_readonlyItems = new ReadOnlyCollection<T>(_items)); }
    }

    public virtual void Add(T obj)
    {
        lock(_lock)
        {
            ((INotifyPropertyChanged) obj).PropertyChanged += ItemPropertyChangedHandler;
            _items.Add(obj);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, obj));
        }
    }

    public virtual void Remove(T obj)
    {
        lock (_lock)
        {
            ((INotifyPropertyChanged) obj).PropertyChanged -= ItemPropertyChangedHandler;
            _items.Remove(obj);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, obj));
        }
    }

    #region INotify Members

    public event NotifyCollectionChangedEventHandler CollectionChanged;

    protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
    {
        var copy = CollectionChanged;
        if(copy != null)
        {
            CollectionChanged(this, args);
        }
    }

    private void ItemPropertyChangedHandler(object sender, PropertyChangedEventArgs e)
    {
        NotifyCollectionChangedEventArgs args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
        OnCollectionChanged(args);
    }

    #endregion
}

Update

In response to the comments about the ReadOnlyCollection, I have switch its implementation for a yield block that is locked (Using a ReaderWriterLock) for the length of the enumeration, like so:

    public IEnumerable<T> Items
    {
        get
        {
            _lock.AcquireReaderLock(_lockTimeout);
            try
            {
                foreach (T item in _items)
                {
                    yield return item;
                }
            }
            finally
            {
                _lock.ReleaseReaderLock();
            }
        }
    }

Any thoughts?


texts:
Anyone who has programmed in TI-Basic is likely to have written one of these programs. The concept for this program is simple.

The "ball" represented by a single point that originates from the upper left hand corner of the screen and bounces around the screen leaving a trail as it travels. The initial movement vector for the ball is (1,1), meaning one down, one right. During each pass through a loop, the "ball" is translated by the amount specified in the movement vector. When the ball would go of bounds, its movement vector is adjusted so that this does not happen. In short, it behaves like this:

except I increased the frame of the GIF.

I've reduced my code to a very short and clean snippet:

FnOff
PlotsOff
AxesOff
ClrDraw

{1,1→L₁
Repeat getKey
Ans→L₂
Pxl-Change(L₁(1),L₁(2
Ans+L₁→L₁
L₂*(1-2not(Ans and Ans≠{62,94
End

FnOn
PlotsOn
AxesOn
ClrDraw

What I would like to hear in responses can be enumerated in four points:

How can I reduce the size of this program?
How can I increase the speed of this program?
How can I reduce the numbers of variables used in this program?
How can I increase the clarity of this program without sacrificing any of the goals stated above?

People responding to this post should also keep in mind that this code was written for and tested on a TI-83 Plus calculator.


texts: Trim string of words based on character count

texts:
I needed a simple way to trim a string of words based on a max character count. I also needed to cut in-between words. Using WordPress, here is what I came up with, but I feel like it could be more efficient.

function count_words($content) {
    $partsArray = explode(' ', $content);
    return sizeof($partsArray);
}

function custom_length_excerpt_max_char($content, $max_chars_limit) {
    $count = 0;
    $word_limit;
    while (strlen($content) > $max_chars_limit) {
        $word_limit = count_words($content);
        $word_limit -= 1;
        $content = wp_trim_words($content, $word_limit, '');
        $count++;
        if ($count > 10) {
            break;
        }
    }
    if ($count > 0) {
        $content .= '&#8230;';
    }
    return $content;
}

I was originally using str_word_count to count the words but it was choking on HTML-encoded characters in the string.


texts:
I wrote this a while ago but wanted to see if I went about it the right way and if my brain is working correctly. I was thinking about these two projects the other day and I recently released the source. This game was originally for a Windows Form Game Jam so you have full context.

This code worked but I always felt there were inaccuracies in how it handled it. For example, there had been times a rock would clip through a ship.

private void CollisionDetection()
{
    foreach(PictureBox s in rocks)
    {
        //These tell the difference between, I'm not sure if this is all correct, its just what I came up with.
        int Xdifference = (s.Location.X + (s.Width / 2)) - (player.Location.X + (player.Width / 2));
        int Ydifference = (s.Location.Y + (s.Height/ 2)) - (player.Location.Y + (player.Height/ 2));

        //get the actual distance apart
        double dist = Math.Sqrt((Xdifference * Xdifference) + (Ydifference * Ydifference));

        //And finally, does the real comparison
        if(dist < ((s.Width / 2) + (player.Width / 2)) && damagable)
        {
            health -= 10;
            damagable = false;
            impactHit.Play();
        }
    }
}

Full file is found here and full source, here.


texts: Finding number pairs where the sum of their divisors are each other

texts:
Given a limit \$n\$, find all the pairs of values less than, or equal to n, which have the the property:

pair(a,b)

where a = sum of divisors of b
  and b = sum of divisors of a

I need to speed up my double for-loop but I don't see how to do it in this case.

def diviseurs_propres(n):
    t = [1]
    for i in range(2,n):
        if n%i == 0:
            t.extend([i])
    return t

def somme_diviseurs_propres(n):
    t = diviseurs_propres(n)
    S = sum(list(t))
    return S

def trouveur(n):
    t = []
    for i in range(0,n+1):
        for p in range(0,n+1):
            if p != i and somme_diviseurs_propres(i) == p and i == somme_diviseurs_propres(p):
             t.extend([p]) and t.extend([i])
    return t


texts:
I'm about a week into learning pdo.  I've created, with help from an example online, a database connection function.  I have a config file with all my info (username, pass, database, host) and a function inside a file called db.php.  Everything works great with the function below.

Though, I have to put dataConnect() on the top of every page that uses $dbo.  Since the function is included in my db.php file, I was hoping that the file was the only thing I needed to include on the page:  not both the db.php and dataConnect().

I've been searching online for a better way to do this function but most of the db examples are single use examples.

Also, having the variables from the config file as global in the function... I'm assuming is a bad thing.  I'm hoping someone can help or let me know that this is a good function.

require('config.php');

function dataConnect() {
try {
    global $dbo;
    global $dbname;
    global $dbuser;
    global $dbpass;

    $info['dbhost_name'] = $dbserver;
    $info['database'] = $dbname;
    $info['username'] = $dbuser;
    $info['password'] = $dbpass;

    $dbConnString = "mysql:host=" . $info['dbhost_name'] . "; dbname=" . $info['database'];

    $dbo = new PDO($dbConnString, $info['username'], $info['password'], array(PDO::ATTR_PERSISTENT => true));
    $dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
    $dbo->exec("SET CHARACTER SET utf8");
    }
    catch(PDOException $e) {
        header("Location:$basehttp/error");
    exit();
    }
}

Thanks for your help!


texts: Running a script recursively in subdirectories

texts:
I got tired of running the same command in multiple directories, so I thought "there has to be a way to make this easier". The commands I was running was mostly git status, git stash list, hg summary, mvn clean test, and so on and so on...

So what I did was to create a bash script that you can pass a parameter to. It will then run the same script or command in all subdirectories, until it finds a place where the script returns exit status 0 (meaning that it was run successfully), then it stops going deeper in those subdirectories.

As I am not that used to bash-scripting, I am wondering if there's anything I can improve here. I would also like you to comment on the usability of this script. You are also welcome to make feature-requests, bug reports, or pull requests (my favorite!) on my bash-recursive repository on GitHub.

The script is:

#!/bin/bash

RUN_SCRIPT=$1

recurseCheck() {
  local f
  for f in $1/* ; do
    local PREV_DIR=`pwd`
    if [ -d $f ]; then
      cd $f
      RESULT=`eval $RUN_SCRIPT`
      local RESULT_CODE=$?
      if [ $RESULT_CODE -eq 0 ]; then
        echo "$f"
        echo "$RESULT"
        echo ""
      else
        recurseCheck $f
      fi
      cd $PREV_DIR
    fi
  done;
}

START_DIR=`pwd`
recurseCheck $START_DIR

Usage example:

  recurse.sh "git status 2>&1"

Will output something like:

/home/zomis/gitstuff/a/Duga
On branch master
nothing to commit, working directory clean

/home/zomis/gitstuff/a/SudokuSharp
On branch master
nothing to commit, working directory clean

/home/zomis/gitstuff/c/Brainduck
On branch master
nothing to commit, working directory clean

/home/zomis/gitstuff/c/CodeReview-Shield
On branch master
nothing to commit, working directory clean

/home/zomis/gitstuff/c/SE-Scripts
On branch master
nothing to commit, working directory clean

The reason 2>&1 is there is because otherwise this, which goes to stderr would be printed:

fatal: Not a git repository (or any of the parent directories): .git

There is also an "example" directory on my GitHub repo which contains a script that I used a lot while testing, run ./recurse.sh /path/to/bash-recurse/example/filecheck.sh and it will output something like:

/home/zomis/bash-recurse/example/b/d
Running filecheck in /home/zomis/recursescript/example/b/d
Hello World
This is a recursive script

/home/zomis/bash-recurse/example/c
Running filecheck in /home/zomis/recursescript/example/c
OK in directory 'c'

Any comments welcome.


texts: Remove \n newline from a list of string dumped from github used for a Italian stopwords vocabulary

texts:
I have a set of Italian words in a txt file hosted on GitHub.
I use it as stopwords vocabulary for further NLP tasks.
When I dump from GitHub it returns me, obviously, also \n
Better or more efficient way to replace the special character compared with my working code?
import urllib.request

stopwords_link = "https://raw.githubusercontent.com/stopwords-iso/stopwords-it/master/stopwords-it.txt"
file_name = urllib.request.urlopen(stopwords_link)

list_nowords = [x.decode("utf-8") for x in file_name]
splitted = [x.split('\n')[0] for x in list_nowords]

As usual I am a self-taught and this is the best place where I can learn, discover new ways and aspects on how to code efficiently with Python


texts:
This is a follow up question for this question:

  Implementation of a dynamic list-like array

This is an implementation of an array that behaves like a list, granting me an easy and quick way to:

quickly add items
iterate over them without caring for empty spots in the array
quickly remove iterated items

After following user rolfl answer I revised my class to this

import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.stream.IntStream;

public class BucketArray<T> implements Iterable<T> {

    private final T[] mItems;
    private final int[] mSlots;
    private int mSlotsTop = -1;

    public final int size;

    private int mIteratedIndex = -1;

    @SuppressWarnings("unchecked")
    public BucketArray(Class<T> cast, int size) {
        this.size = size;
        mItems = (T[]) Array.newInstance(cast, size);
        mSlots = IntStream.range(0, size).toArray();
        mSlotsTop = size - 1;
    }

    public int add(T item) {
        if (item == null)
            return -1;
        if (mSlotsTop < 0)
            return -1;
        final int slot = popSlot();
        mItems[slot] = item;
        return slot;
    }

    public void addAll(T... items) {
        for (T item : items)
            add(item);
    }

    public T get(int i) {
        if (i < 0 || i >= size)
            throw new IndexOutOfBoundsException();
        return mItems[i];
    }

    public int getIteratedIndex() {
        return mIteratedIndex;
    }

    public boolean remove(int i) {
        if (i < 0 || i > size)
            return false;
        if (mItems[i] == null)
            return false;
        mItems[i] = null;
        pushSlot(i);
        return true;
    }

    public void remove(T item) {
        remove(indexOf(item));
    }

    public boolean removeIterated() {
        return remove(mIteratedIndex);
    }

    public boolean isFull() {
        return mSlotsTop == -1;
    }

    public boolean isEmpty() {
        return mSlotsTop == size - 1;
    }

    public int numFreeSlots() {
        return mSlotsTop + 1;
    }

    public int indexOf(T item) {
        for (int i = 0; i < size; i++)
            if (item == mItems[i])
                return i;
        return -1;
    }

    private int popSlot() {
        return mSlots[mSlotsTop--];
    }

    private void pushSlot(int s) {
        mSlots[++mSlotsTop] = s;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            private int index = -1;
            {
                if (mIteratedIndex != -1)
                    index = mIteratedIndex;
            }

            @Override
            public boolean hasNext() {
                do {
                    index++;
                    if (index >= size) {
                        mIteratedIndex = -1;
                        return false;
                    }
                } while (mItems[index] == null);
                return true;
            }

            @Override
            public T next() {
                mIteratedIndex = index;
                return mItems[index];
            }
        };
    }
}

Major changes:

add() method now ignores null and returns the index of the
inserted item so it can be used with get() and remove().
After some thought, the remove() method mostly used in the context of an iteration, so I added  removeIterated() method that removes the currently iterated item in the scope of an iteration being made.
Name of the class changed to BucketArray from BagArray because it sounds better.

Usage example in deleting duplicated integer values:

public static void main(String[] args) {
        BucketArray<Integer> mBucket = new BucketArray<Integer>(Integer.class,20);
        mBucket.addAll(10, 20, 30, 20, 20, 30, 10, 40);
        // bucket is: 40, 10, 30, 20, 20, 30, 20, 10
        for (int a : mBucket)
            for (int b : mBucket)
                if (a == b)
                    mBucket.removeIterated();
        // bucket is now: 40, 10, 30, 20
    }


texts:
I'm sure there's a better more 'jQuery' like way of writing this.  In particular the selector $(this).closest('div.login-box') being in there twice. Perhaps it's possible to use $(this)?

$('.close-box').click(function () {
        $(this).closest('div.login-box').slideToggle("400");
        if ($(this).closest('div.login-box').attr('id') == "login-forgot") {
            clearForgottenPasswordInputs();
        }
    });


texts:
I have a paid app on the app engine. At the moment, usually there is only one instance. The app has a download handler that sends an image after re-sizing it if it is too big. The problem is: if a web page contains many big images (60 or more) the app engine creates another instance. I'm trying to keep the app as cheap as possible and creating too many instances is probably not good for that.

Is there a way to improve this code? Or my concerns are just unfounded?

The application settings are:

automatic_scaling:
  min_idle_instances: 1
  max_pending_latency: 10s

The code:

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
  def get(self, resource):
    resource = str(urllib.unquote(resource))
    blob_info = blobstore.BlobInfo.get(resource)
    if not blob_info:
        pass
    else:
        data = blobstore.fetch_data(blob_info.key(), 0, 50000)
        img0 = images.Image(image_data=data)
        img = images.Image(blob_key=blob_info.key())
        if img0.width >= 800:
            img.resize(width=800)
            img = img.execute_transforms(output_encoding=images.JPEG)
            self.response.headers['Content-Type'] = 'image/jpeg'
            self.response.out.write(img)
        else:
            self.send_blob(blob_info)


texts:
I have coded a solution to build all valid permutations of parentheses.

My code is below.

I have a question on my code based on a comment by my PEP8 checker. It said that there was no need to include the line return anywhere in the code (initially I included one). The solution works but I have never had a case using recursion where I didn't have to use the return line in the base-case.

How come I don't need it here?

The following expression initially had a return statement but I was told this was obsolete.

    if number_open == number_pairs and number_closed == number_pairs:
        print(output)
        build_parentheses.counter += 1
        return

Python 3.7 code:

"""Module builds all valid permutations of n parentheses
For example: n = 3:
    ((()))
    (()())
    (())()
    ()(())
    ()()()
Total = 5
"""

def build_parentheses(number_pairs: int,
                      output="",
                      number_open=0,
                      number_closed=0)-> str:
    """The function that builds the parentheses. Output as a string:
        number_pairs: number of parentheses pairs user desired
        All other parameters are private
    """
    if number_open == number_pairs and number_closed == number_pairs:
        print(output)
        build_parentheses.counter += 1
    else:
        if number_open < number_pairs:
            output += "("
            build_parentheses(number_pairs, output, number_open + 1, number_closed)
            output = output[:-1]
        if number_closed < number_open and number_open:
            output += ")"
            build_parentheses(number_pairs, output, number_open, number_closed + 1)

if __name__ == "__main__":
    build_parentheses.counter = 0
    build_parentheses(5)
    print(f"=========\n{build_parentheses.counter} solutions")

By comparison, in this post I made, I did use the return statement.


texts: VBA macro - searches through a filtered table, stops when entry passes criteria

texts:
I have written several subs which are then called from a main sub. Individual subs run very quickly, most are instantaneous (the DoFind sub takes a few seconds to run due to the large amounts of data in the table) however when I run the main sub it takes up to a minute to execute. Any ideas/tips on why this is the case?

Note, I haven't had much experience with VBA (all has been learnt in the past week). There are other macros used, but they are not shown since even the test sub takes approximately 1 minute

Sub DoFind()

    Dim i As Long

    i = 1

        Do While Sheets("Temp").Cells(i, "A").Value <> Empty

                Dim BearingArray(6) As String

                BearingArray(0) = Sheets("Temp").Cells(i, "A").Value
                BearingArray(1) = Sheets("Temp").Cells(i, "B").Value
                BearingArray(2) = Sheets("Temp").Cells(i, "C").Value
                BearingArray(3) = Sheets("Temp").Cells(i, "D").Value
                BearingArray(4) = Sheets("Temp").Cells(i, "E").Value
                BearingArray(5) = Sheets("Temp").Cells(i, "F").Value
                BearingArray(6) = Sheets("Temp").Cells(i, "G").Value

                With Sheets("Calculations")
                    .Cells(17, "K").Value = BearingArray(0)
                    .Cells(19, "O").Value = BearingArray(1)
                    .Cells(20, "O").Value = BearingArray(2)
                    .Cells(23, "O").Value = BearingArray(3)
                    .Cells(22, "O").Value = BearingArray(4)
                    .Cells(26, "O").Value = BearingArray(5)
                    .Cells(17, "L").Value = BearingArray(6)
                End With

                i = i + 1

                    If Sheets("Calculations").Cells(17, "M").Value = "PASS" Then
        Exit Do
                    Else
                    End If
        Loop
                    If Sheets("Temp").Cells(i, "A").Value = Empty Then
                        MsgBox "No available bearing."

                    End If

End Sub

Sub Create_Sheet_Temp()

    ThisWorkbook.Sheets.Add
    ActiveSheet.Name = "Temp"

' This creates a new worksheet called "Temp"

End Sub

Sub Copy_Paste()

    Dim NewTable As ListObject
    Sheets("Calculations").Activate

    Set NewTable = Sheets("Calculations").ListObjects("Full_Bearings_List")

    NewTable.Range.SpecialCells(xlCellTypeVisible).Select
    NewTable.DataBodyRange.SpecialCells(xlCellTypeVisible).Copy
    Sheets("Temp").Range("A1").PasteSpecial xlPasteAll
    Application.CutCopyMode = False

        'This sub copies all visible cells from a filtered table and pastes them to the new sheet called "Temp"

End Sub

Sub test()
    Create_Sheet_Temp
    Copy_Paste
    DoFind

End Sub


texts: Determining if all strings are parsable to int

texts:
I have to loop through a list of strings and validate that all are parsable to int and not less than 1.  Also, if a string is not parsable but is null or whitespace, then it is OK and my validator should not fail.

Here's the code I have come up with which works fine but leads to method complexity:

public static bool ArePerPersonBILimitsInvalid(IEnumerable<string> bilimits)
        {
            return bilimits.Any(c =>
            {
                int d;
                if (!int.TryParse(c, out d))
                {
                    if (string.IsNullOrWhiteSpace(c))
                    {
                        return false;
                    }

                       return true;
                }

                if (d < 1)
                {
                    return true;
                }

                return false;
            });
        }


texts: Computing pairwise distance covariances for a symmetric matrix

texts:
I wrote a non-vectorized, non-parallel wrapper function for computing the pairwise distance covariances (Wikipedia) (original paper) in a data frame:

library(energy)

pairwise_dcor <- function (DF, progress = FALSE, parallel = FALSE) {
  stopifnot(is.data.frame(DF))
  v <- names(DF)
  n <- length(v)
  corr <- matrix(1, n, n, dimnames = list(v, v))
  for (i in seq_len(n)) {
    for (j in seq_len(i - 1)) {
      cc <- complete.cases(DF[[i]], DF[[j]])
      xi <- DF[cc, i]
      xj <- DF[cc, j]
      if (is.factor(xi) || is.character(xi)) {
        xi <- dist(model.matrix(~ xi - 1), "binary")
      }
      if (is.factor(xj) || is.character(xj)) {
        xj <- dist(model.matrix(~ xj - 1), "binary")
      }
      corr[i, j] <- dcor(xi, xj)
      corr[j, i] <- corr[i, j]
      }
    }
  corr
}

It's very slow:

# generate data that's similar to mine
test_data <- do.call(rbind, replicate(20, iris, simplify = FALSE))
test_data <- do.call(cbind, replicate(5, test_data, simplify = FALSE))

# run it (but i don't recommend it)
st <- system.time(
  dc <- pairwise_dcor(test_data)
)
st
#     user   system  elapsed
#  955.179  102.529 1075.781

This test data has a run time of several minutes. I need to repeat this operation several (10-20) times, and a few times on a much larger (like 3,000 x 100) data set, with missing values in most or all of the columns. I'd rather not spend most of the afternoon waiting for this code to run, since I'd like to be able to run it on-the-fly as I update my analysis.

It seems like it would be easy to improve its performance by parallelizing it. I know about the doParallel and foreach packages, but I'm not sure what the best way to use it would be and I've had mixed results with it in the past, with more cumbersome, less portable code and dubious performance improvements. For what it's worth, I'd like to run this on a quad-core MacBook Pro with 16 GB of RAM.

Also, am I missing out on other optimizations? For instance, the dist(model.matrix(~ xi - 1), "binary") line seems absurdly roundabout to me, but my brute-force reimplementation was substantially slower:

binary_jaccard <- function(x) {
  n <- length(x)
  d <- matrix(0, n, n)
  for (i in seq_len(n)) {
    for (j in seq_len(i - 1)) {
      d[i, j] <- as.numeric(x[i] != x[j])
      d[j, i] <- d[i, j]
    }
  }
  as.dist(d)
}

silly_version <- as.matrix(dist(model.matrix(~iris$Species - 1), "binary"))
slow_version <- binary_jaccard(iris$Species, FALSE)
all(silly_version == slow_version)
# TRUE

system.time(as.matrix(dist(model.matrix(~iris$Species - 1), "binary")))
#    user  system elapsed
#   0.002   0.000   0.003
system.time(binary_jaccard(iris$Species, FALSE))
#    user  system elapsed
#   0.988   0.071   1.061

Could I see improvements by using Rcpp?

Alternatively, would I be able to make significant improvements by switching to Python+NumPy+Pandas?


texts:
Problem: Implement merge sort in python. Don’t use Python’s built in sort or sorted. Assume your inputs will be sufficient for the memory you have.

How should I make this code better?

def merge_sort(list_sort):
    """splits the list in two parts until each part is left with one member"""

    if len(list_sort) ==  1:
        return list_sort

    if len(list_sort)>= 2:
        x= len(list_sort) / 2
        part_a = list_sort[:x]
        part_b = list_sort[x:]
        sorted_part_a = merge_sort(part_a)
        sorted_part_b = merge_sort(part_b)
        return merge(sorted_part_a, sorted_part_b)

def merge(left , right):
    """merges the two parts of list after sorting them"""
    sorted_list = []
    i = 0
    while left[:] and right[:] :
        if  left [i] > right [i]:
            sorted_list.append(right[i])
            right.remove(right[i])

        else :
            sorted_list.append(left[i])
            left.remove(left[i])

    if left[:]:
            sorted_list.extend(left[:])
    elif right[:] :
            sorted_list.extend(right[:])
    return sorted_list

details = [1, 127, 56, 2, 1, 5, 7, 9, 11, 65, 12, 24, 76, 87, 123, 65, 8,32, 86, 123, 67, 1, 67, 92, 72, 39, 49, 12, 98, 52, 45, 19, 37, 22, 1, 66, 943, 415, 21, 785, 12, 698, 26, 36, 18, 97, 0, 63, 25, 85, 24, 94, 150 ]
print "List to be sorted  = ", details
print "Sorted List = ", merge_sort(details)


texts: remove elements in one list according to the conditions on corresponding elements of another list

texts:
I have two lists that have the same length, lstA, lstB, I want to remove elements that are not good based on some conditions, but at the meantime, I also want to remove the elements in lstB that have the same position with the unwanted one in lstA. like below:

lstA = ['good','good2', '']
lstB = [1,2,3]

I want '' in lstA and 3 in lstB removed:

results:
lstA = ['good','good2']
lstB = [1,2]

Currently, I am doing this:

lstB = [lstB[i] for i in range(len(lstB)) if lstA[i] != '']
lstA = [st for st in lstA if st != '']

Want a more elegant and pythonic way of doing this


texts: Instantiating shapes using the Factory Design Pattern in Java

texts:
Trying to learn  the factory design pattern, came up with some code based on a Shape Factory (found this example here). Is this the right way to implement the factory pattern?

interface Shape{
    void draw();
}

class Rectangle implements Shape{

    @Override
    public void draw() {
        System.out.println(" Drawing A Rectangle!");

    }

}

class Circle implements Shape{

    @Override
    public void draw() {
        System.out.println(" Drawing A cIRCLE!");

    }

}

class Triangle implements Shape{

    @Override
    public void draw() {
        System.out.println(" Drawing A Triangle!");

    }

}

class ShapeFactory{

    public static Shape getShape(String shapeType)
    {
        Shape shape = null;
        switch(shapeType.toUpperCase()){

        case "CIRCLE":
        shape = new Circle();
        break;

        case "RECTANGLE":
            shape = new Rectangle();
            break;

         case "TRIANGLE":
            shape = new Triangle();
            break;
        }

            return shape;
    }

}

public class FactoryExample {

    public static void main(String[] args) {
        Shape s = ShapeFactory.getShape("rectangle");
         s.draw();
         s = ShapeFactory.getShape("triangle");
        s.draw();
    }

}


texts: Test if Excel is open without relying on catching an error

texts:
Getting a hook on an open instance of Excel is something I do all the time. I'm now questioning if I'm doing this the best way. I'm particularly concerned with the fact that I rely on the GetObject method to throw an error. I'd like to avoid it if possible.

I have a Utils module that contains a function that returns a brand new instance of excel and one that tries to get an open instance of excel. When I encounter Err.Number 429 in the GetExcelApp function, I call the CreateExcelApp function. Something I've done routinely for years suddenly feels dirty. There has to be a better way to do this.

Public Function GetExcelApp() As Excel.Application
' Returns open excel instance.
'   If it doesn't exist, creates one to return
On Error GoTo ErrHandler
Const ERR_APP_NOTRUNNING As Long = 429

    Set GetExcelApp = GetObject(, "Excel.Application")

CleanExit:
    Exit Function
ErrHandler:
    If Err.number = ERR_APP_NOTRUNNING Then
        Set GetExcelApp = CreateExcelApp
    Else
        ShowErrorMessageBox
    End If
End Function

Private Function CreateExcelApp() As Excel.Application
' Returns New instance of Excel
On Error GoTo ErrHandler

    Set CreateExcelApp = CreateObject("Excel.Application")

CleanExit:
    Exit Function
ErrHandler:
    ShowErrorMessageBox
End Function

Private Sub ShowErrorMessageBox()
    ErrHandler.messageBox "Utils", "GetExcelApp", vbCritical, "Unable to get an instance of Excel."
End Sub

This code does also rely on this subroutine in an external module named ErrHandler.

Public Sub messageBox(moduleName As String, procName As String, Optional style As VbMsgBoxStyle = vbCritical, Optional OptionalText As String = vbNullString)
    Dim messageText As String
    messageText = "Module: " & moduleName & vbCrLf & _
                "Procedure: " & procName & vbCrLf & _
                Err.Description

    If Not OptionalText = vbNullString Then
        messageText = messageText & vbCrLf & OptionalText
    End If

    MsgBox messageText, style, "Runtime Error: " & Err.number

End Sub

And here is a quick example of how the GetExcelApp function is used.

Public Sub Test()
    Dim xl As Excel.Application
    Set xl = GetExcelApp
    xl.Visible = True
End Sub

I also read the MSDN documentation, but they're using On Error Resume Next... That's not even remotely acceptable.


texts: A practical example of evenly distributing n lists into a single list

texts:
I had previously asked about how to evenly distribute the items in n lists into a single list and was referred to this question.

I made a practical example of my solution for this in VBA for Excel, since my application for this was resorting my Spotify lists which can be easily pasted into Excel for manipulation. Assumptions are that you have a headerless worksheet (wsSource) of songs with columns A, B, C representing Artist, Song, SpotifyURI respectively, a "Totals" worksheet (wsTotals) containing the sum of songs for each Artist from wsSource sorted in descending order, and a "Destination" worksheet where the new list will be created.

Here's a link to the sample file.

Could I get some suggestions to improve this? I was going to get rid of the totals worksheet and have this portion done in code, but I have to go and I wanted to go ahead and put this out there.

Sub WeaveSort()

Dim wb As Workbook
Dim wsDest As Worksheet
Dim wsSource As Worksheet
Dim wsTotals As Worksheet
Dim i As Integer
Dim iLast As Integer
Dim iDest As Integer
Dim iSource As Integer
Dim iOldRow As Integer
Dim iNewRow As Integer
Dim dDiff As Double
Dim dDiffSum As Double

    Set wb = ThisWorkbook
    Set wsTotals = wb.Worksheets("Totals")
    Set wsSource = wb.Worksheets("Source")
    Set wsDest = wb.Worksheets("Dest")
    iLast = wsTotals.Range("A1").End(xlDown).Row - 1

    For i = 2 To iLast
        iSource = wsTotals.Range("B" & i).Value
        iDest = wsDest.Range("A99999").End(xlUp).Row

        If i = 2 Then
            wsDest.Range("A1:C" & iSource).Value2 = wsSource.Range("A1:C" & iSource).Value2
            wsSource.Range("A1:C" & iSource).Delete (xlShiftUp)
            GoTo NextI
        End If

        dDiff = iDest / iSource
        dDiffSum = 0
        iNewRow = 0

        For iOldRow = 1 To iSource
            dDiff = iDest / iSource
            dDiffSum = dDiffSum + dDiff
            iNewRow = Round(dDiffSum, 0)
            wsSource.Rows(iOldRow).Copy
            wsDest.Rows(iNewRow).Insert xlShiftDown
            iDest = iDest + 1
        Next iOldRow

        wsSource.Range("A1:C" & iSource).Delete (xlShiftUp)
NextI:
    Next i

End Sub


texts: Turning nested jQuery Promises into flat code structure using .done() and .then()

texts:
This code uses jQuery promises in combination with callbacks does the following:

Saves Group UUID to server using storeGroupOnServer()
Creates a DOM element for the group using createGroupDomNode()
Creates child elements for each image, using createNewChildDomNode()

How can this be written more flat (avoiding the nesting), using .then()?

var groupPromise = groupInstance.promise();
var arrayOfFiles = groupInstance.files();

groupPromise.done(function(fileGroupInfo) {
    storeGroupOnServer(fileGroupInfo.uuid, function(groupid){

        createGroupDomNode(groupid, function(groupnode){

            $.each(arrayOfFiles, function(i, file) {
                file.done(function(fileInfo) {
                    createNewChildDomNode(fileInfo.cdnUrl, groupnode);
                });
            });
        });
    });
});

Step 1: storeGroupOnServer():

storeGroupOnServer = function(uuid, callback) {
    $.post('saveGroup.php', {uuid:uuid},
    function(data) {
        callback(data.groupid);
    },'json');
};

Step 2: createGroupDomNode():

createGroupDomNode = function(groupid, callback) {
    var cloner = $('#cloner');
    var newnode = cloner.clone(true);
    newnode.attr('id',groupid);
    newnode.removeClass('hide');
    cloner.after(newnode);
    callback(newnode);
}

Step 3: createNewChildDomNode():

function createNewChildDomNode(imgpath, groupdom){
    imgnode = $('<img/>').attr('src',imgpath);
    picnode = $('<picture/>').append(imgnode);
    $(groupdom).first().prepend(picnode);
}


texts: Python image sequence file concatenation

texts:
I need to iterate over a large (in the thousands) list of files in a network folder and "concatenate" any image sequences into just one entry with the range of images (first image, last image in sequence). I am calling this "sequence pruning" and I created the following code which does work, but it seems incredibly un-pythonic to me and runs slowly. I'm certain there is a MUCH better way to do this, so I am looking for review to help clean/speed this up.

To elaborate a bit further on the issue lets say I have this as a list of files for the input:

img.001.png
img.002.png
img.003.png
img_other.001.png
random_file.txt
yet another seq.0000.png
yet another seq.0001.png
yet another seq.0002.png
yet another seq.0021.png
yet another seq.0030.png

In the end I want to return something like this:

img.001.png, [1-3]
img_other.001.png
random_file.txt
yet another seq.0000.png, [0-30]

FYI I can safely assume that the image sequence number is always going to be a series of digits at the very end of the filename (before the extension of course). However I cannot assume that they will be perfectly sequential, as there are sometimes "gaps" between numbers.

Here's my current code, python 2.7:

import os

def split_padding(path):
    file, ext = os.path.splitext(path)
    pad_int = 0

    while file[pad_int * -1 - 1].isdigit():
        pad_int += 1

    if pad_int == 0:
        return file, '0', ext

    clean_file = file[0:pad_int * -1]
    padding = file[pad_int * -1:]

    return clean_file, padding, ext

def strip_padding(path):
    file, ext = os.path.splitext(path)

    while file[-1].isdigit():
        file = file[:-1]

    return file

def prune_files(paths):
    '''
    sequences get put into arrays like so:
    [x_folder, z_folder, [test_a.000.png, 0, 2], [test_b.000.tif, 0, 3], test_C.000.png]
    :return: [file1, file2, [first_file, seq_start, seq_end]]
    '''
    paths.sort(key=lambda s: s.lower()) # list has to be sorted for this to work

    # this odd bit of code turns all sequences into arrays of images.
    pruned_list = []
    switch = True
    for c, path in enumerate(paths):
        if c == 0:
            pruned_list.append(path)
            continue
        if not os.path.splitext(path)[1] in ['.png', '.tif', '.tiff', '.exr', '.jpg', '.jpeg']:
            pruned_list.append(path)
            continue

        test = paths[c-1]
        if strip_padding(path) == strip_padding(test):
            if switch:
                pruned_list[-1] = [pruned_list[-1]]
                switch = False
            pruned_list[-1].append(path)
        else:
            pruned_list.append(path)
            switch = True

    # so now lets convert that to the format we want to return
    for c, item in enumerate(pruned_list):
        if type(item) == list:
            pruned_list[c] = [item[0], int(split_padding(item[0])[1]), int(split_padding(item[-1])[1])]

    return pruned_list

if __name__ == "__main__":
    test_dir = "some directory"
    print prune_files([path for path in os.listdir(test_dir) if os.path.isfile(os.path.join(test_dir, path))])


texts: Caching data by using the result of first running operation

texts:
My code handles concurrent requests by waiting for the result of an already running operation. Requests for data may come in simultaneously with same/different credentials (including empty credentials).

For each unique set of credentials there can be at most one GetCurrentInternal call in progress, with the result from that one call returned to all queued waiters when it is ready.

private static readonly Credentials EmptyCredentials = new Credentials
{
    SqlCredentials = null,
    ExchangeCredentials = null,
};

public AgentMetadata GetCurrent(Credentials credentials)
{
    var agentMetadata = new Lazy<AgentMetadata>(() => GetCurrentInternal(credentials));

    var lazyMetadata = (Lazy<AgentMetadata>)MemoryCache.Default.AddOrGetExisting(
                       (credentials ?? EmptyMetadataCredentials).ToString(),
                       agentMetadata,
                       DateTime.Now.AddMinutes(5));

    try
    {
        return (lazyMetadata ?? agentMetadata).Value;
    }
    catch (InvalidOperationException ex)
    {
        _logger.ErrorException(ex, "An error occurs during getting full metadata");
        throw;
    }
}

Is it thread safe? Do I have a bugs? How to create a good key for the AddOrGetExisting?

Credentials its a DataContract that contains other DataContract like SqlCredentials, ExchangeCredentials. And they contains their own strings like user_name and password.


texts:
I was curious on what, if anything, I could be doing better. I have been spending the last few days working with Java and learning some basics and wanted to try this challenge and making it two classes. I found the scanner is sometimes strange and doesn't take input the first time.

import java.util.Scanner;
import java.util.Random;

public class rps {
    public static void main (String args[]){
        int input;
        int b = 1;
        Scanner sage = new Scanner(System.in);
        Random rnd = new Random();
    System.out.println("Rock Paper Scissors, by Sage!");
    System.out.println("Select 1, 2, 3, for Rock, Paper, Scissors");
    //Menu Present, pretty bad still

        while (b != 0){
        int rock = 1, paper = 2, scissors = 3;
        input = sage.nextInt();
        int randomNumber = rnd.nextInt(3-1+1)+1;

            if(randomNumber == rock){
                if(input == rock){
                    System.out.println("Rock vs. Rock, ITS A TIE!");
            }    else if(input == paper){
                    System.out.println("Rock vs. Paper! You win!" );
            }    else if(input == scissors){
                    System.out.println("Rock vs. Scissors! You lose!");
            }  //These blocks establish options if the computer got Rock

            else if(randomNumber == paper){
                if(input == rock){
                    System.out.println("Paper vs. Rock! You lose!");
            }   else if(input == scissors){
                    System.out.println("Paper vs. Scissors! You win!");
            }   else if(input == paper){
                    System.out.println("Paper vs. Paper! Its a tie!");
            } //These blocks establish the options if comp. got paper

            else if(randomNumber == scissors){
                if(input == rock){
                    System.out.println("Scissors vs. Rock! You win!");
            }   else if(input == scissors){
                    System.out.println("Scissors vs. Scissors, ITS A TIE!");
            }   else if(input == paper){
                    System.out.println("Scissors vs Paper! You lose!");
            } //These blocks establish if the computer got scissors.

            }
            }
            rps2 rps2Object = new rps2();
            rps2Object.rps2();

        }

    }
    }
}

Then this is the other class

import java.util.Scanner;

public class rps2 {
    public void rps2(){
        Scanner sage = new Scanner(System.in);
        int b;
        b = 1;
        System.out.println("Play again? Y(8), N(9)?");
        int yes= 8, no = 9;
        int input;
        input = sage.nextInt();

            if(input == yes){
                System.out.println("Rock,Paper,Scissors!");

            }else{
                System.out.println("Thanks for playing!");
            }

    }
}


texts:
I have this PHP function that adds custom user profile fields to WordPress. It saves the field through the WooCommerce form-edit-my-account.php file. I have a lot of repeated code, and would like to clean it up.

function save_sfirst_account_details( $user_ID ){
    $sfirst = ! empty( $_POST['sfirst'] ) ? wc_clean( $_POST['sfirst'] ) : '';
        update_user_meta( $user_ID, 'sfirst', $sfirst );
    }

function save_slast_account_details( $user_ID ){
    $slast = ! empty( $_POST['slast'] ) ? wc_clean( $_POST['slast'] ) : '';
    update_user_meta( $user_ID, 'slast', $slast );
}

function save_grade_account_details( $user_ID ){
    $grade = ! empty( $_POST['grade'] ) ? wc_clean( $_POST['grade'] ) : '';
    update_user_meta( $user_ID, 'grade', $grade);
}

function save_city_account_details( $user_ID ){
    $city = ! empty( $_POST['city'] ) ? wc_clean( $_POST['city'] ) : '';
    update_user_meta( $user_ID, 'city', $city );
}

function save_state_account_details( $user_ID ){
    $state = ! empty( $_POST['state'] ) ? wc_clean( $_POST['state'] ) : '';
    update_user_meta( $user_ID, 'state', $state);
}

function save_degree_account_details( $user_ID ){
    $degree = ! empty( $_POST['degree'] ) ? wc_clean( $_POST['degree'] ) : '';
    update_user_meta( $user_ID, 'degree', $degree );
}

function save_semail_account_details( $user_ID ){
    $semail = ! empty( $_POST['semail'] ) ? wc_clean( $_POST['semail'] ) : '';
    update_user_meta( $user_ID, 'semail', $semail );
}
add_action( 'woocommerce_save_account_details', 'save_student_account_details' );

Can anyone help me out with this?


texts:
Per a Reddit Daily Programming Challenge, I am performing a matrix transposition on a string using JavaScript.

Example input: 'The quick brown fox jumped over the lazy dog'

Output:

Tqbfjotld
hurouvhao
eioxmeezg
 cw pr y
 kn e
    d

While my code works, I wanted to know:

From a readability standpoint, would it be better to break the code up into smaller functions?
If there's anything in the code that can be simplified.

transposeString('The quick brown fox jumped over the lazy dog');

function transposeString(str) {

  if (str.indexOf(' ') === -1) {
    var map = Array.prototype.map;

    var output = map.call(str, function (char) {
        return char + '\n';
    });
    return output.join('');
  } else {
    var master = [],
        longestWord = '';

    words = str.split(' ');

    words.forEach(function(word) {
      if (word.length >= longestWord.length) longestWord = word;
    });

    longestWord.split('').forEach(function() {
      master.push([]);
    });

    var treatedWords = words.map(function (word) {
      if (word.length < longestWord.length) {
        var spaces = Array(longestWord.length - word.length + 1).join(' ');
        return word = word + spaces;
      } else {
        return word;
      }
    });

    treatedWords.forEach(function (word) {
      var n = 0;
      word.split('').forEach(function (char) {
        master[n].push(char);
        n++;
      });
    });

    master.forEach(function (arr) {
      console.log(arr.join(''));
    });

    return;
  }
}


texts:
I have written this recursive code to find all interleaves of two strings, where the order of strings are preserved. For example:

s1="abc" s2="1"
a1bc
ab1c
abc1

You can see that the order of s1 and s2 is preserved.

// Interleave_Two_Strings.cpp : Defines the entry point for the console application. //

#include "stdafx.h"
#include "iostream"
#include "vector"
#include "string"
using namespace std;
class Solution {
public:
    vector<string> res;
    void helper(string s1, string s2,string s3) {
        if (s1 == "" && s2 == "") {
            if(std::find(res.begin(),res.end(),s3)==res.end())
                res.push_back(s3);
            return;
        }
        if (s2 == "") {
            res.push_back(s3+s1);
            return;
        }
        if (s1 == "") {
            res.push_back(s3 + s2);
            return;
        }
        for (int i = 0; i < s1.size(); i++) {
            for (int j = 0; j < s2.size(); j++) {
                helper(s1.substr(i + 1, s1.size()),s2.substr(j+1,s2.size()),s3+s1.substr(0, i+1)+s2.substr(0,j+1));
            }
        }
    }
    void interleave(string s1, string s2) {
        if (s1==""|| s2=="")
            return;
        for (int i = 0; i < s2.size(); i++) {
            helper(s1, s2.substr(0, i+1),"");
        }
        res.pop_back();
    }
};
int main()
{
    Solution s;
    s.interleave("abc", "123");
    return 0;
}


texts:
I have started learning programming in ObjC from
"Objective-C Programming: The Big Nerd Ranch Guide"

In a task to exercise the learned stuff I've had to write a program what compares the the names in the file /usr/share/dict/words if there are same proper names (capitalized) and normal words (uncapitalized).

This is my program:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

    // Zähler für die Anzahl der Suchergebnisse
    int counter = 0;

    // Hier die Suchbegriffe eingeben
    NSString *search1 = @"Da";
    NSString *search2 = @"d";

    // Prüfung ob search1 und search2 identisch sind, wenn nicht eine Warnmeldung ausgeben.
    if ([search1 caseInsensitiveCompare:search2] != NSOrderedSame) {

        NSLog(@"Wenn du unterschiedliche Wörter suchst, wird die Suche lange dauern und zu keinem Ergebnis führen.");
        NSLog(@"Suche ist gestartet...");
        sleep(2);

    }   else {NSLog(@"Suche wird gestartet...");
        sleep(2);

    // Liest Datei als großen String ein (mögliche Fehler werden ausgeblendet
    NSString *namestring = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                    encoding:NSUncachedRead
                                                        error:NULL];

    // Zerlegt Datei in ein Array mit Strings
    NSArray *names = [namestring componentsSeparatedByString:@"\n"];

    // Suchalgorithmus
    // Geht das Array in der 1. Dimension einen String nach dem anderen durch
    for (NSString *n in names) {

        // Sucht nach dem String search1
        NSRange r = [n rangeOfString:search1 options:NSAnchoredSearch];

        // Wurde für search1 etwas gefunden?
        if (r.location != NSNotFound) {

            // Geht das Array in der 2. Dimension einen String nach dem anderen durch
            for (NSString *m in names) {

                // Sucht nach dem String search2
                NSRange q = [m rangeOfString:search2 options:NSAnchoredSearch];

                // Wurde für search2 etwas gefunden?
                if (q.location != NSNotFound) {
                    if ([n caseInsensitiveCompare:m] == NSOrderedSame) {
                        NSLog(@"%@ is equal to %@", n, m);

                        counter++;
                    }
                }
            }
        }
    }
    NSLog(@"Es wurden %i Suchergebnisse gefunden", counter);
    }
    NSLog(@"Es wurden %i Suchergebnisse gefunden", counter);
}
return 0;
}

As you can see, I'm German.

Is there a way to do the same but more elegantly?

It's not necessary to tell me the complete code. It's better to give me some tips on which way would be better.


texts: Count occurrences of a specific sequence in a list of many numbers

texts:
The script will work through each number from 0 - 10.
aList=[3,5,... over 10k numbers ...,2,2,6]

Let’s say, for example: findSquence01=[3,5] or findSquence02=[3,5,3]
count = 0
for i in range(len(data)-len(pattern)+1):
    tmp = [data[i], data [i+1]]

    try:
        for j in range(len(data)-i):
            print(i, i+j)
            if tmp[-1] != data[i+j+1]:
                tmp.append(data[i+j+1])

            if len(tmp) == len(pattern):
                print(tmp)
                break
    except:
        pass

    if tmp == pattern:
        count +=1

i- represents times of [3,5]
j- represents times of [3,5,3]

print("There is",i,"times",findSquence01)
print("There is",j,"times",findSquence02)

The output should look like:

There is 73 times [3,5]
There is 12 times [3,5,3]

How can I walk through the list and count those given sequences?


texts:
I'm learning F#, starting with baby steps. Is there a way to write this code in a more succint way, while keeping the same semantics? Maybe by using a Seq or something like that?

let rec repeatingPrompt predicate (message:string) =
    Console.WriteLine(message)
    let value = Console.ReadLine()
    if predicate value then
        value
    else
        repeatingPrompt predicate message


texts: Reading configuration variables in a Python project

texts:
I am new to python, in .NET we use web.config to store environment specific variables... I want to achieve the same for my python program (scraper)
I have created .env file at the root of the project and added the following variable to it:
CHROME_WEB_DRIVER=C:/webdrivers/chromedriver.exe

Note that I have added .env to gitignore as this file changes in different environments.
And I am using decouple package to use this setting from .env file in the constructor of the class:
import scrapy
from decouple import config
from python_json_config import ConfigBuilder
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

class MySpider(scrapy.Spider):
    name = 'my_spider'

    def __init__(self):
        # .env variables
        self.CHROME_WEB_DRIVER = config('CHROME_WEB_DRIVER')

        self.job_highlight_dict = {}
        self.wanted_organisations_dict = {}
        self.output_path = ""
        self.start_urls = []
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        self.driver = webdriver.Chrome(self.CHROME_WEB_DRIVER, chrome_options=chrome_options)

        builder = ConfigBuilder()
        config_values = builder.parse_config('config/config.json')
        self.start_urls = config_values.urls
        for o in config_values.organisations:
            self.wanted_organisations_dict[o["brandName"]] = o["organisationName"]
        self.output_path = config_values.output

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):

        # parse response

Note that I also have a config.json file, but this file contains configuration variables which are consistent across all environments.
This is how the config.json file looks like:
{
    "urls": [
        "https://www.site-to-scrape-1.com",
        "https://www.site-to-scrape-2.com"
    ],
    "organisations": [
        { "brandName" : "brand-1", "organisationName" : "organisation-1" },
        { "brandName" : "brand-2", "organisationName" : "organisation-2" }
    ],
    "output": "../path/to/s3/bucket"
}

I have not included all of spider class, but it is the main class of this project which crawls the given urls and then parse them. I am not sure if the constructor of the spider is the best place to read config.json and .env files?


texts: Pythonic data processing? Should I use an iterator?

texts:
In a data processing and analysis application I have a dataCleaner class that conducts a series of functions that help me to clean raw time series data.

One of the problems I see in my data is that they often include large gaps at the beginning and end. This is due to the occasional corruption of the timestamp, a behaviour that I cannot influence but can lead to arbitrary timings of individual data records.

Imagine an hourly dataset covering November 2011. Sometimes one of the timestamps is corrupted and may end up recording a date of January 2011. Since the data are sorted by date this puts a point at the beginning which needs to be removed. It is possible that this corruption can occur more than once in any given dataset. I need to detect and remove these outliers if they exist.

So I designed this function to trim off contiguous values at each end of the data if the time gap is considered large. My data arrive into this function in the form of two numpy arrays (timestamps and values) and must be filtered together.

@staticmethod
def _trimmed_ends(timestamps, values, big_gap = 60*60*24*7):
    """
    Uses timestamps array to identify and trim big gaps at either end of a dataset.
    The values array is trimmed to match.
    """
    keep = np.ones(len(timestamps), dtype=bool)
    big_gaps = (np.diff(timestamps) >= big_gap)
    n = (0, 0)
    for i in xrange(len(keep)):
        if big_gaps[i]:
            keep[i] = False
            n[0] += 1
        else:
            break

    for i in xrange(len(keep)):
        if big_gaps[::-1][i]:
            keep[::-1][i] = False
            n[1] += 1
        else:
            break

    if sum(n) > 0:
        logging.info("%i points trimmed (%i from beginning, %i from end)" % (sum(n), n[0], n[1]))
    else:
        logging.info("No points trimmed")
    return timestamps[keep], values[keep]

Is this a pythonic way to do this? I have been advised that I might want to convert this into an iterator but I'm not sure it that is possible, let alone desirable. As I understand it, I need to attack the array twice, once forwards and once backwards in order to achieve the desired result.


texts: Getting rid of goto without duplication in Go

texts:
Consider the following function:

func Listen(username string) (net.Listener, error) {
    path := filepath.Join(socketsDir, username)

    oldUmask := syscall.Umask(^0600)
    defer syscall.Umask(oldUmask)

    listener, err := net.Listen("unixpacket", path)
    if err != nil {
        return nil, err
    }

    passwd, err := passwd.Getpwnam(username)
    if err != nil {
        goto chownFailed
    }

    err = os.Chown(path, int(passwd.Uid), os.Getgid())
    if err != nil {
        goto chownFailed
    }

    return listener, err

chownFailed:
    listener.Close()
    return nil, err
}

I think this way of error handling is ugly. Is there a way to get rid of the goto without duplicating the call to listener.Close?


texts:
Just wanted to run through my PHP session handling and get some feedback and tips with regard to what is good (if anything!), what could be better and what is either plain wrong, or using out of date methods.

Just as a bit of background, I have only ever dabbled with PHP (and many years ago that was) and want to try and embrace the OOP side of it.

Every page that is required to be part of a session has the following at the top:

<?php
include_once 'Session.php';

$session = new Session();
$session->authenticate();
?>

Session.php is as follows:

class Session {
    const password = "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8";
    private $sessionInfo;

    public function __construct() {
        session_start();
        if (!isset($_SESSION['sessionInfo'])) {
            $sessionInfo = new SessionInfo();
            $_SESSION['sessionInfo'] = $sessionInfo;
        } else {
            $sessionInfo = $_SESSION['sessionInfo'];
        }
    }

    public function authenticate() {
        if (isset($_SESSION['loggedin'])) {
            if ($_SESSION['loggedin'] === true) {
                return;
            }
        }
        header('Location: login.php');
    }

    public function logIn($password) {
        if (sha1($password) === self::password) {
            $_SESSION['loggedin'] = true;
            header('Location: index.php');
        }
    }

    public function logOut() {
        unset($_SESSION['loggedin']);
        session_destroy();
        header('Location: login.php');
    }

    public function getSessionInfo() {
        return $this->sessionInfo;
    }
}

?>

SessionInfo is currently just a blank class, it's intended purpose is to act as an information store for the current session.

login.php:

<?php
include_once 'Session.php';
    if(isset($_POST['submit'])) {
        if(isset($_POST['password'])) {
            $session = new Session();
            $session->logIn($_POST['password']);
        }

    }
?>
<html>
    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
            <input type="password" name="password" />
            <input type="submit" name="submit" />
        </form>
    </body>
</html>

Do I need to call die() (or exit() seeing as they are identical) after a failed authenticate to terminate any page which may attempt to load? Or do I need to return a boolean and then terminate from the calling page?

Please disregard the use of SHA1 and the hashed password in the file, I will be extending this to use a MySQL background, and hope to incorporate the use of a salted hash. I will be using prepared statements, will this be sufficient to protect from SQL injection and allow me to not bother filtering $_POST[...]?

Also, I realise that the location headers may better be stored as a class constant in case I wish to change the locations.


texts: Extra parenthesis in if-clause vs. readability

texts:
For a while now, our team has been doing peer code reviews of all submissions. As we've learned, everyone has different styles and going forward, all of us see a need to converge at some kind of tribal/team style so that the code looks consistent and we don't spend time in code reviews arguing one style vs. another.

For most part we've been able to come up with compromises but few, such as this one, are turning out to be sticky:

if ((bIsPaused) || (runState != LnrState_Running))
{
    ....
}

vs.

if (bIsPaused || runState != LnrState_Running)
{
    ....
}

One one side, the argument is less characters -> less information to process -> more readable code.

On the other side, the argument is that extra parenthesis help to read the code.

I'm curious if this is a purely personal thing and the entire community split 50/50 on this (in which case, I'm probably going to be bashed for asking this question), or if you guys think one way is actually more preferred than the other.

UPDATE: removed spaces in the example code so the answers/comments just focus on existence/absence of extra parenthesis pairs.


texts:
I'm ashamed to ask such an elementary question but I tried to DRY up this template a couple ways and couldn't come up with anything I was satisfied with. Can you help?

  %ul.dropdown-menu(aria-labelledby="single-button" role="menu" uib-dropdown-menu)
    %li(role="menuitem")
      %a(href="/legal-forms/admin/document_templates/{{ documentTemplate.id }}/edit") Edit
    %li(role="menuitem")
      %a(href="/legal-forms/admin/document_templates/{{ documentTemplate.id }}/open_in_wizard") Create Document
    %li(role="menuitem")
      %a(href="/legal-forms/admin/document_templates/{{ documentTemplate.id }}/preview" document-template-preview="true" id="documentTemplate.id") Preview
    %li(role="menuitem" ng-hide="documentTemplate.parent_id")
      %a(href=true ng-click="copy(documentTemplate)") Copy
    %li(role="menuitem")
      %a(href=true ng-click="delete(documentTemplate)") Delete


texts: Faster way of replacing strings in large pandas dataframe with regex

texts:
I want to replace all instances of a location with just the location keyword itself, eg "Foxclore Road" with "road", "Chesture Avenue" with "avenue". The file is several GBs, with many million rows. Current working code, three methods:

startTime = time.time()
mergedAllCrimes['crime_location_approx'] = mergedAllCrimes.crime_location_approx.str.replace(r'.*(?i)road$', 'road')
endTime = time.time()
print(endTime - startTime)

startTime = time.time()
mergedAllCrimes.crime_location_approx = mergedAllCrimes.crime_location_approx.apply(lambda x: 'road' if 'road' in str.lower(x) else x)
endTime = time.time()
print(endTime - startTime)

startTime = time.time()
allCrimes.loc[allCrimes['crime_location_approx'].str.contains('Road', case=False), 'crime_location_approx'] = 'road'
endTime = time.time()
print(endTime - startTime)

my times are:

14.287408590316772
1.9554557800292969
5.129802942276001

respectively

Problem is, the second two methods (while faster), replace "Broadway" with "road", hence the need for a regex to search at the end of a string.

Is there any way to make the regex conditional method much faster? If I have a large list of replacements, it could end up taking a long time.


texts: Iterating over global keymap, removing all bindings to super keys

texts:
Background

Emacs on OS X by default binds several keys with the super modifier. I do not want these bindings as I switch between OS X and Windows frequently, and want a clean slate on both platforms to bind super keys for my own purposes.

Code

global-unset-all-super-key.el

(defun global-unset-all-super-key ()
  "Will unset any single key in global keymap that has the super
modifier."
  (let ((km (current-global-map)))
    (while km
      (let ((maybe-event (and (listp (car km))
                              (caar km))))
        (if (and (eventp maybe-event) ; Also filters out maybe-event
                                      ; when nil because (car km) was not a list.
                 (memq 'super (event-modifiers maybe-event)))
            (global-unset-key (vector maybe-event))))
      (setq km (cdr km)))))

(provide 'global-unset-all-super-key)

Lines added to init.el.

;; Remove default super bindings on Mac systems.
;; Do this early, before any mappings are added.
(when (string-equal system-type "darwin")
  (require 'global-unset-all-super-key)
  (global-unset-all-super-key))

Questions

Is this reasonably idiomatic lisp?
Are there elisp constructs that would simplify the implementation?
Are there elisp constructs that would make the implementation clearer?
And finally, is (while <list> <body> (setq <list> (car <list>))) the best that can be done to loop over a list taking each element in turn and doing something for side effects? map doesn't seem like a reasonable alternative because I'm not trying to build up a list to return. Something like Clojure's doseq.


texts:
This is a follow-on from a previous question I posted here.

I've got code here that works for what I want, but the problem is the loop takes ages to perform. I was wondering if anyone could follow this and tidy it up a bit for me.

Sub Refresh_Data()
    Application.CutCopyMode = False

    'Turns screen updating off to increase speed
    Application.ScreenUpdating = False

    'Get 'G/L Account numbers
    Sheet1 = "BW TB"

    Sheets(Sheet1).Activate
    Range("A1").Activate

    'Find last row - always named "Overall Result" in ColA
    Cells.Find(What:="Overall Result", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
    True, SearchFormat:=False).Activate

    'This looks up to row 25 (title row), but adjusts to only copy data from row 26 down to the penultimate row (the subtotal is not required)
    lastrow = Selection.Row - 1
    colno = Selection.Column
    firstrow = Selection.End(xlUp).Row + 1

    'CopyPaste loop

    'First sheet is titled "4020"
    i = Sheets("4020").Index

    'Due to all the sheet names being numeric. This is a slight workaround.
    'It basically runs the macro starting at the 4020 sheet and ending at the last sheet with a numeric sheets.
    'i.e. pastes values for all numbered tabs.
    Do While IsNumeric(Sheets(i).Name) = True

    'clear all formulae except first formulaic row (Row5)
    Sheets(i).Activate
    Range("A6").EntireRow.Select
    Range(Selection, Selection.Offset(1000, 0)).ClearContents

    'Copy G/L Account numbers from BW TB sheet to current sheet
    Sheets(BWTB).Activate
    Range(Cells(firstrow, colno), Cells(lastrow, colno)).Copy

    Sheets(i).Activate
    Range("a5").PasteSpecial xlPasteValues

    'Copy down formulae
    Range("B5:L5").Copy
    Range("B5:L5", Range("B5:L5").Offset(lastrow - firstrow, 0)).PasteSpecial xlPasteFormulas

    ActiveSheet.Calculate

    'Paste As Values
    Range("B6:L6", Range("B6:L6").Offset(lastrow - firstrow, 0)).Copy
    Range("B6").PasteSpecial xlPasteValues

    i = i + 1
    Loop
End Sub

The scenario is about 25 'numericlly named' sheets (eg 4020) for which I need to first clear (this is for a rolling document updated periodically with a differing number of rows required, the determine the number of rows, then copy-pastespecial data from an unformatted mastersheet (BW TB).

Apologies for the mess it's in.  I'm in the process of breaking up the code into more sub functions for easier reading.


texts: Parsing financials from a YAML file. Is this normal/idiomatic in Rust?

texts:
I wrote this Rust code to parse my financials from a YAML file and my main concern is the large match branches (although general code review is welcome; still a Rust beginner):
extern crate yaml_rust;
use yaml_rust::{Yaml, YamlLoader};

fn main() -> Result<(), std::io::Error> {
    let doc = std::fs::read_to_string("finances.yaml")?;
    let data = YamlLoader::load_from_str(&doc).unwrap();
    let doc = &data[0];

    let map = doc.as_hash().unwrap();

    for (k, v) in map.iter() {
        println!("{}: ", k.as_str().unwrap());
        let sum = unwrap_value(v);
        println!("== {}", sum);
        println!();
    }

    Ok(())
}

fn unwrap_value(v: &Yaml) -> f32 {
    match v {
        Yaml::Hash(v) => {
            let mut sum = 0.;
            for (k, vv) in v.iter() {
                match vv {
                    Yaml::String(_vv) => {
                        print!("\t\t {}: ", k.as_str().unwrap());
                    }
                    Yaml::Integer(_vv) => {
                        print!("\t\t {}: ", k.as_str().unwrap());
                    }
                    _ => {
                        println!("\t* {}:", k.as_str().unwrap());
                    }
                }
                sum += unwrap_value(vv);
            }
            sum
        }
        Yaml::Array(v) => {
            let mut sub_sum = 0.;
            for h in v.iter() {
                sub_sum += unwrap_value(h);
            }
            println!("\t= {}", sub_sum);
            sub_sum
        }
        Yaml::String(v) => {
            let tot: f32 = v
                .split("+")
                .fold(0., |sum, s| sum + s.trim().parse::<f32>().unwrap());
            println!("{}", tot);
            tot
        }
        Yaml::Integer(v) => {
            println!("{}", v);
            *v as f32
        }
        _ => 0.,
    }
}

Here's a sample YAML of the file I'm parsing:
---
'2021-01-01':
  apartment:
    - rent: 2750
  transportation:
    - uber: 87.69 + 55.36 + 26 + 42 + 42 + 34.92 + 25.76 + 42 + 42
    - bus: 12
'2021-02-01':
  apartment:
    - rent: 2750
  bills:
    - elctricity: 27
  transportation:
    - uber: 87.69 + 55.36 + 26 + 42 + 42 + 34.92 + 25.76 + 42 + 42
    - bus: 12

and this is the output for the example above:
2021-01-01:
        * apartment:
                 rent: 2750
        = 2750
        * transportation:
                 uber: 397.73
                 bus: 12
        = 409.73
== 3159.73

2021-02-01:
        * apartment:
                 rent: 2750
        = 2750
        * bills:
                 elctricity: 27
        = 27
        * transportation:
                 uber: 397.73
                 bus: 12
        = 409.73
== 3186.73


texts:
This an update to my last question.

I want to process multiple pages at once pulling URLs from tier_list in the crawl_web method. The problem I am having is crawl_web already has two threads to crawl and index simultaneously, and the .join() seems to prevent opening more than one page.

import urllib2, collections, threading, robotparser, urlparse, Queue
from bs4 import BeautifulSoup
q = Queue.Queue()

def get_page_links(page, crawled, to_crawl, next_tier, robot_set):
    #respect the robots.txt and returns all urls from page
    rp = robotparser.RobotFileParser()
    links = []
    uparse = urlparse.urlparse
    for link in page[1].find_all('a'):
        url = link.get('href')
        base_url = (uparse(page[0]).scheme + '://' + uparse(page[0]).netloc)
        if base_url not in robot_set:
            robot_set.add(base_url)
            rp.set_url(urlparse.urljoin(base_url,'robots.txt'))
            rp.read()
        abs_url = urlparse.urljoin(base_url, url)
        if rp.can_fetch('*', abs_url):
            links.append(abs_url)
    links = set(links)
    next_tier.extend(links)
    to_crawl.update(links)
    to_crawl.remove(page[0])
    crawled.add(page[0])

def get_page(url):
    #return page html from url
    try:
        return [url,BeautifulSoup(urllib2.urlopen(url).read())]
    except:
        return [url,BeautifulSoup("")]

def get_page_words(page, word_index):
    #pulls words off page to be indexed
    url = page[0]
    words = page[1].get_text()
    to_remove = '{}[]«#$%^&*._,1234567890+=<>/\()":;!?'
    bad_words = set(['http','www','com','https','gov','org','edu','imgs'])
    for i in to_remove:
        words = words.replace(i, ' ').lower()
    words = words.split()
    for word in words:
        if word in word_index:
            word_index[word].add(url)
        elif len(word) < 15 and word not in bad_words:
            word_index[word] = set([url])

def crawl_web(url, maxdepth):
    #controls crawl depth, updates url lists and word_index
    depth = 0
    robot_set = set()
    crawled = set()
    to_crawl = set([url])
    word_index = {}
    tier_list = [[url]]
    while depth < maxdepth:
        next_tier = []
        for url in tier_list[depth]:
            page = get_page(url)
            t1 = threading.Thread(target = get_page_links, args = (page,
                                      crawled, to_crawl, next_tier, robot_set))
            t2 = threading.Thread(target = get_page_words, args = (page,
                                                                   word_index))
            t1.start()
            t2.start()
            t1.join()
            t2.join()
        tier_list.append(next_tier)
        depth += 1
    return crawled, to_crawl, word_index, tier_list

def search_engine(target_string, word_index):
    #searches word_bank for words in string, returns urls words are found at
    targets = list(set(target_string.split()))
    result =[]
    for word in targets:
        if word in word_index:
            result += word_index[word]
    ans = collections.Counter(result).most_common(3)
    return ans

result = crawl_web('http://xkcd.com',2)
word_index = result[2]
print result[0]
print search_engine('starting blogs about', word_index)


texts: Calculating CRC using arbitrary polynomials

texts:
This is my implementation of calculating CRCs which supports arbitrary polynomials specified in different representations (according to the explanations given here and here).

My concerns are:

I don't like the weak typing (having to supply the numeric value and its size for both the data and the polynomial; possibility to mix up data and polynomial; "stringly typed" parameter for the polynomial representation).
I'm not sure if the bit manipulations are particularly efficient. Are there ways to make the calculations more efficient and/or simpler?
I have a feeling that the algorithm (in the crc function) could be expressed in a more functional way, as opposed to the quite imperative style I used (I sticked to it because I knew it was correct).

def get_bit(value, position):
    """Return the digit of the binary representation of the value at the given
    position, where position 0 is the LSB.

    >>> [get_bit(0b1101, position=i) for i in reversed(range(4))]
    [1, 1, 0, 1]
    """
    return (value >> position) & 1

def reverse_bits(value, bits):
    """Return the value obtained by reversing the binary representation.

    >>> '{:04b}'.format(reverse_bits(0b1101, 4))
    '1011'
    """
    return sum(2 ** i * get_bit(value, bits - i - 1)
               for i in range(bits))

def normalize_poly(value, bits, representation='reversed reciprocal'):
    """Convert different numerical representations of a polynomial to
    a canonical ('normal') representation.

    See https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Specification
    and https://en.wikipedia.org/wiki/Mathematics_of_cyclic_redundancy_checks#Polynomial_representations

    >>> '{:04b}'.format(normalize_poly(0b0011, 4, 'normal'))
    '0011'
    >>> '{:04b}'.format(normalize_poly(0b1010, 4, 'reversed'))
    '0101'
    >>> '{:04b}'.format(normalize_poly(0b1100, 4, 'reversed reciprocal'))
    '1001'
    """
    if representation == 'normal':
        return value % (2 ** bits)
    elif representation == 'reversed':
        return reverse_bits(value, bits)
    elif representation in ['reversed reciprocal', 'koopman']:
        return ((value << 1) % (2 ** bits)) + 1
    else:
        raise NotImplementedError(
            'Unknown representation: {}'.format(representation))

def crc(data, data_bits, poly, poly_bits, init='1'):
    """Calculate the CRC value of the data using the given polynomial in normal
    representation.

    >>> p = normalize_poly(value=0x62cc, bits=15)  # known as CRC-15-CAN
    >>> '{:04x}'.format(crc(data=0x00384c0, data_bits=25,
    ...                     poly=p, poly_bits=15))
    '007c'
    """
    if init == '1':
        reg = 2 ** poly_bits - 1
        assert '{:b}'.format(reg) == '1' * poly_bits
    else:
        reg = init

    for i in reversed(range(data_bits)):
        high_bit = get_bit(reg, poly_bits - 1) ^ get_bit(data, i)
        reg <<= 1
        if high_bit:
            reg ^= poly

    return reg % (2 ** poly_bits)


texts:
Consider the following two code examples:

1.

void foo(boolean cake) {
    if (!cake)
        return;

    // TODO some work.
}

2.

void bar(boolean cake) {
    if (cake)
        //TODO some work.
}

Which one is the most readable/ semantic correct? With the first example you avoid additional indentation on the below code.

Should one return right away when there is no cake or not?


texts: Sum the exponents of the prime factors of a number

texts:
My goal is to find the sum of the exponents of the prime factors of an integer.

I wrote the code below with the complexities specified here (I am not 100% sure of the complexities though):

Runtime complexity:

findPrimesSmaller: \$O(n \log(\log n))\$
exponentInDecomposition: \$O(n)\$
primeExponentsCount: \$O(n^2 \log(\log n))\$

Space Complexity:

findPrimesSmaller: \$O(\log N)\$ (?)
exponentInDecomposition: \$O(1)\$
primeExponentsCount: \$O(\log N)\$

import static org.junit.Assert.*;

import java.util.*;

import org.junit.*;

public class PrimeCount {
  // Given a number n and its associated prime numbers decomposition:
  // n = p1 ^ (alpha1) * p2 ^ (alpha2) * ... * pn ^ (alphan)
  // where all the pi's are prime numbers smaller than n
  // return the sum of the alphai's

  public int primeExponentsCount(int n) {
    // First generate the list of prime numbers smaller than n
    ArrayList<Integer> candidatePrimeNumbers = findPrimesSmaller(n);

    // initialize the sum
    int result = 0;

    for (int i : candidatePrimeNumbers) {
      result += exponentInDecomposition(i, n);
    }

    return result;
  }

  public ArrayList<Integer> findPrimesSmaller(int n) {

    int[] allNumbers = new int[n + 1];

    // Initialize the list with all the numbers smaller than n
    for (int i = 0; i <= n; i++) {
      allNumbers[i] = i;
    }

    // Apply a Sieves of Eratosthenes method to keep only the prime numbers
    for (int i = 2; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (j > 0 && j % i == 0) {
          allNumbers[j] = -1;
        }
      }
    }

    ArrayList<Integer> primeNumbers = new ArrayList<Integer>();
    for (int i = 2; i <= n; i++) { // Remove 0 and 1 from list => start counting at 2
      if (allNumbers[i] > 0) {
        primeNumbers.add(i);
      }
    }

    return primeNumbers;
  }

  public int exponentInDecomposition(int p, int n) {
    int result = 0;

    while (n % Math.pow(p, result) == 0 && n / Math.pow(p, result) >= 1) {
      result++;
    }
    result--;

    return result;
  }

  @Test
  public void test_exponentZeroDecomposition() {
    int a = exponentInDecomposition(3, 5);
    assertEquals(0, a);
  }

  @Test
  public void test_exponentNonZeroDecomposition() {
    int a = exponentInDecomposition(3, 9);
    assertEquals(2, a);
  }

  @Test
  public void primesSmaller10() {
    ArrayList<Integer> primesLessThan10 = findPrimesSmaller(10);
    int two = primesLessThan10.get(0);
    int three = primesLessThan10.get(1);
    int five = primesLessThan10.get(2);
    int seven = primesLessThan10.get(3);
    assertEquals(2, two);
    assertEquals(3, three);
    assertEquals(5, five);
    assertEquals(7, seven);
  }

  @Test
  public void testCount0() {
    int zero = primeExponentsCount(0);
    assertEquals(0, zero);
  }

  @Test
  public void testCount1() {
    int zero = primeExponentsCount(1);
    assertEquals(0, zero);
  }

  @Test
  public void testCount10() {
    int two = primeExponentsCount(10);
    assertEquals(2, two);
  }

  @Test
  public void testCount13() {
    int one = primeExponentsCount(13);
    assertEquals(1, one);
  }

  public static void main(String[] args) {
    PrimeCount e = new PrimeCount();
    e.test_exponentZeroDecomposition();
    e.test_exponentNonZeroDecomposition();
    e.primesSmaller10();
    e.testCount0();
    e.testCount1();
    e.testCount10();
    e.testCount13();

  }
}


texts:
In a bash script, I need to parse arguments that have the following form:

The main arguments can be thought of as being a single argument, but I do not want to force users to quote the entire thing, so when the argument contains spaces I must be able to handle multiple arguments.
One flag may be passed as -<flag> where <flag> can be an arbitrary word (without spaces)
Finally, an external command, including its own options and flags may be passed. If so, this should be separated by a double dash.

For example,

my_command test

should result in

"$inp" == "test"
"$flag" == ""
"$ext_command" == ""

and

my_command this is a test -new -- sed "s|a|b|"

should result in

"$inp" == "this is a test"
"$flag" == "new"
"$ext_command" == "sed \"s|a|b\""

I think the following script does what I want, but since it's my first bash script, I wanted to ask whether the script is idiomatic and whether I missed any border cases.

local inp=""
local flag=""
local ext_command=""
local count="1"
local started=""
for i
do
    count=$((count+1))
    if [[ "$i" == '--' ]]
    then
        ext_command="${@:count}"
        break
    else
        if [[ "$i" == -* ]];
        then
           flag=${i#*-}
        else
            if [ ! "$started" ]
            then
                inp="$i"
                started=1
            else
                inp="$inp $i"
            fi
        fi
    fi
done


texts: React hook to wrap useEffect and mimic React component class lifecycle calls

texts:
I wanted to make a React hook that allowed me to use the React Component Class lifecycle calls. So I could use the named version instead of just having useEffect.
For example, I want to be able to do this.
useEffectWithName.componentDidMount(callback);

Here is the code that I put together with help from the following question.
"https://stackoverflow.com/questions/53464595/how-to-use-componentwillmount-in-react-hooks".
My question is did I implement the React Class Lifecycle functions in a hook properly?
Or do you see an issue with how I did this.
Thanks in advance. Note I am using typescript.
import {
    useEffect,
    EffectCallback,
    memo,
    PropsWithChildren,
    FunctionComponent
} from 'react';

const useEffectWithName = (() => {
    const componentDidMount = (cb: EffectCallback) => {
        useEffect(cb, []);
    }

    const componentDidUpdate = (cb: EffectCallback) => {
        useEffect(cb);
    }

    interface shouldComponentUpdate<P> {
        Component: FunctionComponent<P>;
        condition: (
            prevProps: Readonly<PropsWithChildren<P>>,
            nextProps: Readonly<PropsWithChildren<P>>
        ) => boolean;
    }

    const shouldComponentUpdate = <P>({ Component, condition }: shouldComponentUpdate<P>) => {
        memo(Component, condition);
    }

    const componentWillUnmount = (cb: EffectCallback) => {
        useEffect(() => {
            return () => {
                cb()
            };
        }, []);
    }

    return {
        componentDidMount: componentDidMount,
        componentDidUpdate: componentDidUpdate,
        shouldComponentUpdate: shouldComponentUpdate,
        componentWillUnmount: componentWillUnmount
    };
})();

export default useEffectWithName;


texts: Countable and uncountable sets in .NET (set operators, demo)

texts:
Up-to-date version: IEnumerable and Predicate Sets in .NET (Complete, Explained).

Previously: Countable and uncountable sets in .NET (clean version).

Disclaimer: Please do not interpret my attempts to gain maximum from .NET as some kind of socially unacceptable behavior. You, probably, know someone who does it to his/her car. It is a very good way to learn and explore a hospital nearby :)

Update: Set operations *, - have been changed to &,| as they are more common for expressing set operations.

Reason to have sets: Just because it streamlines IEnumerable<T> filtering. I will probably define some kind of lambda language for set composition, but look how easy and natural it is to play with them in integration scenarios (online demo):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using static System.Console;
using static System.String;
using static Selection;

class Program
{
    static Set<double> Less(double max) =>
        new Set<double>(v => v < max);

    static Set<double> Bigger(double min) =>
        new Set<double>(v => v > min);

    static void Main(string[] args)
    {
        var soundings = Sounding.Fakes();

        // business layer – not happy; definitely need some lambda
        // language for composition
        Set<double> negative = Less(0);

        // @ t3chb0t look at the end of the line – this one is for you!
        // Also my first attempt to define pipe operator in C# :)
        Set<Point> shallow = negative & Bigger(-20) & Select((Point p) => p.Depth);
        Set<Point> squeezed = Bigger(100) & Select((Point p) => p.Pressure);
        Set<Point> outliers = negative & SelectMany((Point p) => p.Channels);

        Set<Sounding> deep = shallow & SelectMany((Sounding s) => s.Points);
        Set<Sounding> failed = (squeezed & shallow | outliers) & SelectMany((Sounding s) => s.Points);

        // reporting looks just good; and it is important as
        // it is usually very volatile
        Report("Job well done", soundings & !failed);
        Report("To do", soundings & failed);
        Report("Big upcoming expenses", soundings & failed & deep);
    }

    static void Report(string title, IEnumerable<object> args) =>
        WriteLine($"{title}: {Join(",", args)}");

}

Where new library code is:

static class Selection
{
    public static Selection<T, TResult> Select<T, TResult>(Func<T, TResult> selector) =>
        new Selection<T, TResult>(selector);

    public static Selection<T, TResult> SelectMany<T, TResult>(Func<T, IEnumerable<TResult>> selector) =>
        new Selection<T, TResult>(selector);
}

// represents some kind of data set, so intersection operator looks ok
class Selection<T, TResult>     {
    public static Set<T> operator &(Set<TResult> left, Selection<T, TResult> right) =>
        new Set<T>(r => left & right.Selector(r));

    public static Set<T> operator &(Selection<T, TResult> left, Set<TResult> right) =>
        new Set<T>(r => right & left.Selector(r));

    public Selection(Func<T, TResult> selector)
    {
        Selector = (T i) => new[] { selector(i) };
    }

    public Selection(Func<T, IEnumerable<TResult>> selector)
    {
        Selector = selector;
    }

    Func<T, IEnumerable<TResult>> Selector { get; }
}

P.S. It is not for production yet, just some kind of working 2 am experiment :)


texts: Passing C++ objects by reference for participants in a simulated battle

texts:
I just started watching C++ videos couple of days ago. I tried taking everything I learned about classes and passing by references vs values. The main question I what would like to know is this the correct way to change the values of private class members or is there a more 'professional' way of doing it. (Or standard).

Entity.cpp:

void Entity::playerAttack(Entity &Player, Entity &Enemy) {
    cout << "\nPlayer is Attacking!" << endl;
    cout << "Enemy's health was at: " << Enemy.getHealth() << endl;
    totalHealth = Enemy.getHealth() - Player.getDamage();
    Enemy.setHealth(totalHealth);

    if (Enemy.getHealth() <= 0) {
        Enemy.setHealth(0);
        cout << "\nEnemies is 0 or below" << endl;
        cout << "Players health is at: " << Player.getHealth() << endl;
    }
    else {
        cout << "Enemies health is now at: " << Enemy.getHealth() << endl;
    }
}

void Entity::setHealth(float x) {
    health = x;
}

void Entity::setDamage(float x) {
    damage = x;
}

float Entity::getDamage() {
    return (damage);
}

float Entity::getHealth() {
    return (health);
}

Entity.h:

#pragma once
class Entity
{
public:
    void setHealth(float x);
    void setDamage(float x);
    void playerAttack(Entity &Player, Entity &Enemy);
    float getHealth();
    float getDamage();

    Entity();
    ~Entity();

private:
    float health;
    float damage;
    float healAbility;
    float totalHealth;
};

source.cpp:

Entity constructE(float damage, float health, float regen) {
    Entity Entity;
    Entity.setDamage(damage);
    Entity.setHealth(health);
    Entity.setRegen(regen);

    return (Entity);
}

int main() {
    Entity Player = constructE(10, 100, 10);
    Entity Monster = constructE(5, 20, 5);
    Player.playerAttack(Player, Monster);
}


texts:
The idea is to have a data structure that holds any number of objects of any type. Objects can be retrieved, or removed permanently, at random. Also, it can be cleared (or emptied).

This class just provides the ability to select a random object from a collection, with the option of removing it.

public class Sack
{
    private List<object> objects;

    public Sack()
    {
        objects = new List<object>();
    }

    public void Add<T>(T obj)
    {
        objects.Add(obj);
    }

    public object Retrieve()
    {
        int numberObjects = objects.Count;

        Random rand = new Random();
        int selectedIndex = rand.Next(0, numberObjects);

        return objects[selectedIndex];
    }

    public object Remove()
    {
        int numberObjects = objects.Count;

        Random rand = new Random();
        int selectedIndex = rand.Next(0, numberObjects);

        object selectedObject = objects[selectedIndex];
        objects.RemoveAt(selectedIndex);

        return selectedObject;
    }

    public void Empty()
    {
        objects.Clear();
    }
}


texts:
I'd like to Select columns from Table 1 with a Row Filter, Select columns from Table 2 and join.

This works but it's a bit verbose. I was wondering if there is a cleaner way to write this.

SELECT * FROM
  ((SELECT JOB_NUM, ITEM_NUMBER, DATE_COMPLETED, QTY_COMPLETED, STD_USAGE, ACTUAL_USAGE FROM JOB_ROUTERS WHERE RESOURCE_CODE == "TDWELD") AS JOBS
 LEFT JOIN
(SELECT ITEM, DESCRIPTION FROM ITEM_ATTRIBUTE) as item
  ON JOBS.ITEM_NUMBER == ITEM.ITEM)


texts: Comparing two columns in two different rows

texts:
I want to go through each line of the a .csv file and compare to see if the first field of line 1 is the same as first field of next line and so on. If it finds a match then I would like to ignore those two lines that contains the same fields and keep the lines where there is no match.

Here is an example dataset (no_dup.txt):

Ac_Gene_ID  M_Gene_ID
ENSGMOG00000015632  ENSORLG00000010573
ENSGMOG00000015632  ENSORLG00000010585
ENSGMOG00000003747  ENSORLG00000006947
ENSGMOG00000003748  ENSORLG00000004636

Here is the output that I wanted:

Ac_Gene_ID  M_Gene_ID
ENSGMOG00000003747  ENSORLG00000006947
ENSGMOG00000003748  ENSORLG00000004636

Here is my code that works, but I want to see how it can be improved:

import sys

in_file = sys.argv[1]
out_file = sys.argv[2]

entries = {}
entries1 = {}

with open(in_file, 'r') as fh_in:
    for line in fh_in:
        if line.startswith('E'):
            line = line.strip()
            line = line.split()
            entry = line[0]
            if entry in entries:
                entries[entry].append(line)
            else:
                entries[entry] = [line]

with open('no_dup_out.txt', 'w') as fh_out:
    for kee, val in entries.iteritems():
        if len(val) == 1:
            fh_out.write("{} \n".format(val))

with open('no_dup_out.txt', 'r') as fh_in2:
    for line in fh_in2:
        line = line.strip()
        line = line.split()
        entry = line[1]
        if entry in entries1:
            entries1[entry].append(line)
        else:
            entries1[entry] = [line]

with open(out_file, 'w') as fh_out2:
     for kee, val in entries1.iteritems():
        if len(val) == 1:
            fh_out2.write("{} \n".format(val))

The output that I am getting:

[["[['ENSGMOG00000003747',", "'ENSORLG00000006947']]"]]
[["[['ENSGMOG00000003748',", "'ENSORLG00000004636']]"]]