Design patterns: choosing the right handler depending on data type Suppose we have an application which process packets from somewhere. We should choose right handler for the packet depending on packet type and also we know that it might be necessary to change algorithm of handlers and add new handlers in the future.

The question is: how can I write maintainable code?

Here is my draft. I'm sure there should be patterns for this and similar tasks. I want to resolve not only my small task but read deep about design and patterns which developers usually use in this case. What do you recommend for me?

public class RequestHandler
{
    Dictionary<string, IDocumentProcessor> _handlers;

    public Dictionary<string, IDocumentProcessor>  Handlers { get { return _handlers; } }

    public RequestHandler(IDictionary<string, IDocumentProcessor> handlers)
    {
        _handlers = handlers.ToDictionary(key => key.Key, val => val.Value);
    }

    public void Handle(Document document)
    {
        IDocumentProcessor handler;
        handler = Handlers.ContainsKey(document.Type)? Handlers[document.Type] : null;

        if(handler == null) throw new Exception();

        Task task = Task.Factory.StartNew( () => handler.Process(document) );
    }
}

public interface IDocumentProcessor
{
    void Process(Document document);
}

public class DocumentAProcessor:IDocumentProcessor
{
    public void Process(Document document)
    {

    }
}

public class DocumentBProcessor:IDocumentProcessor
{
    public void Process(Document document)
    {

    }
}

Sorry this took so long to post, it got a lot longer then I expected it to be.

Since you are already programming off of interfaces, implementing the MEF framework should be relatively simple, provide the expansion that you need, and allow you to add in new IDocumentProcessor classes without recompiling the entire program. You may/will need to modify some aspects of your code, but overall it will remain unchanged.

MEF works by importing and exporting classes based upon interfaces and metadata tied to those classes. You can think of it like the children toys where you put the round blocks in the round holes, the square blocks in the square holes, etc (Note that is overly simplified).

Using your case for example, you would want to export your:

public class DocumentAProcessor:IDocumentProcessor
{
    ....
}

To do so, you would need to add the following to your code file:

[Export(typeof(IDocumentProcessor))]
public class DocumentAProcessor:IDocumentProcessor
{
    ....
}

The Export attribute means that you want to export that class and import it into a variable marked with the Import attribute (I'll get to that in a second). The typeof(IDocumentProcessor) informs what type you are exporting so that it can be matched with an Import expecting that same type (round peg in a round hole).

Additionaly, you can add MetadataAttributes to the classes that you are exporting that give additional information about the class. To do so will take a little more work then adding the export class but taking your example, we would want to create something similar to:

public interface IDocumentProcessorMetadata
{
    string DocumentType { get; }
}

[MetadataAttribute]
public class DocumentProcessorMetadata : Attribute, IDocumentProcessorMetadata
{
    public DocumentProcessorMetadata(string documentType)
    {
        DocumentType = documentType;
    }

    public string DocumentType { get; set; }
}

This will create a simple Metadata attribute that you can attach to your class that will contain the DocumentType that the class processes. You can have this metadata attribute hold any number of items simply by adding new properties to IDocumentProcessorMetadata and DocumentProcessorMetadata. In my system for example, I use this to hold a GUID (has to be as a string), a simple name that can be used for lookups and flags for various things. To use the DocumentProcessorMetadata attribute, you would modify your class(es) to look like:

[Export(typeof(IDocumentProcessor))]
[DocumentProcessorMetadata("DocumentType1")]
public class DocumentAProcessor:IDocumentProcessor
{
    ....
}

[Export(typeof(IDocumentProcessor))]
[DocumentProcessorMetadata("DocumentType2")]
public class DocumentBProcessor:IDocumentProcessor
{
    ....
}

That is it for the Export, now for the Import. The import works in pretty much the same way as the export. You put an [ImportMany(typeof(IDocumentProcessor))] attribute above the variable that the Exports will import into, and that is pretty much it (The ImportMany attribute imports more than 1 instance of the class, while the Import attribute will only import the first instance that is encountered). However, you will need to import to an IEnumberable<Lazy<IDocumentProcessor,IDocumentProcessorMetadata>> variable instead of the Dictionary<string,IDocumentProcessor> that you were using before. You can keep the dictionary that you had, but you will need to populate it another way (I'll provide an example later). What the Lazy<T1,T2> class allows is for the classes to be imported into the variable, but not actually created until they are needed. That way, if you have 5 document processors but in the life of the program you only ever use 2, you save the overhead of creating those 3 that you never used. That said, your import would look like this:

[ImportMany(typeof(IDocumentProcessor))]
IEnumberable<Lazy<IDocumentProcessor,IDocumentProcessorMetadata>> _processors;

To get the processor that you need, you would iterate through the _processors (or you could use linq) to find the one that handles the correct Document.Type:

public void Handle(Document document)
{
    IDocumentProcessor handler;
    //note I keep the dictionary
    //if you use TryGetValue, it is only one trip to the dictionary instead of two
    Handlers.TryGetValue(document.Type, out handler);

    if(handler == null)
    {
        foreach(var processor in _processors)
        {
            if(processor.Metadata.DocumentType == document.Type)
            {
                handler = processor.Value;
                //add the processor to the dictionary so it can be easily found later
                Handlers.Add(document.Type, processor.Value);
                break;
            }
        }
        //If it gets to this point, the processor was not found and an exception should be thrown
        throw new Exception();
    }
    Task task = Task.Factory.StartNew( () => handler.Process(document) );
}

The last piece of the puzzle is the code that will actually connect everything together. I usually have this in it's own class, in the core dll of whatever application is using it. Essentially, this class handles what is called a CompositionContainer that allows all the Exports to be tied to the Imports. You add what are called AssemblyCatelogs that contain a single assembly or you can add a DirectoryCatelog that will include any files in the directory. In your case, I would use an AssemblyCatelog so include all of your Imports (this is assuming that they are in the same assembly as the code containing the CompositionContainer) and a single DirectoryCatelog that will hold all of the IDocumentProcessor classes. If everything is in the same assembly, you can get away with just the AssemblyCatelog but I would still include the DirectoryCatelog incase you wanted to add new IDocumentProcessor classes later. The class would look something like this:

public class MEFManager
{
    private CompositionContainer _container;

    public MEFManager()
    {
        //AggregateCatalog allows multiple catalogs to be added to the CompositionContainer
        var catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
        catalog.Catalogs.Add(new DirectoryCatalog(Path.Combine("BaseDirectory","PathToAssemblies"));

        _container = new CompositionContainer(catalog);

        try
        {
            //This line of code is what ties everything together.
            this._container.ComposeParts(this);
        }
        catch (CompositionException ex)
        {
            ....
        }
    }
}

I would create this class as start up, just keeping it tucked away somewhere. If you want to get really fancy, you can add a FileSystemWatcher on the directory that you specified so if you add a new file at runtime, it calls a function that automatically loads the new assembly and ties all of the Imports and Exports together.

That is pretty much it. This allows you to create an easily extendable program, if you need to add a DocumentCProcessor you would create the class in its own assembly and add the file to the directory that you specified in the MEFManager. The next time the program starts, the MEFManager will find the files and automatically import them to where they need to be.

Surprisingly, the MSDN documentation on MEF is fairly good and can be found here.


My selection sort implementation please review my code on selection sort algorithm:

selectionSort();

function selectionSort(){
    var numArray = [4,34,2,5,12];
    var MIN = 0;
    var totalNum = numArray.length,
        temp = 0,
        next_position = 0

    for(var i = 0; i < totalNum; i++){
        MIN = numArray[i];

        next_position = i + 1;
        for(var j = next_position; j < totalNum; j++){

           if(numArray[j] < MIN){
              temp = MIN;
              MIN = numArray[j];
              numArray[j] = temp;
           }
        }

        numArray[i] = MIN;
    }

    console.log(numArray);
}

After running, the output is:

[ 2, 4, 5, 12, 34 ]

Here's what I would add if I were doing this as a peer review.

Nested For Loops:

Nested for loops are hard to read. As an alternative, split the nested loop into a separate, aptly named function, if possible. Perhaps a function that returns the next item to be swapped with? You would take your nested for loop and move it to a function that would return what is currently temp if it is there. Then, if there was a return value, you would swap the values. You could even destructure them instead of storing the previous value in a temporary variable: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Also, by moving the for loop into a different function, you only have to do this numArray[i] = MIN; if necessary.

Naming:

Your variables are lowercase, lower_case, camelCase, and UPPERCASE. I suggest standardizing on one. For one thing, MIN looks like a constant to me (from a PHP background), but it is being changed every time in the for loop. Also, what about improving the names: totalNum is unclear to me. Without looking beyond the naming is that an accumulator that stores the sum of all the items? temp could be previousItem.

Other Items:

It's typically recommended to declare variables at the top of the function. j is declared numerous times.
You initialize MIN at 0, but then reset it inside of the for loop. Why not just declare MIN and not set a value.

A completely different approach:

Finally, I wondered about using Javascript's Array.forEach(). The following is a completely different perspective, but I thought I would throw it out there for you to consider:

selectionSort();

function selectionSort(){
    var originalArray = [4, 34, 2, 5, 12],
        sortedArray = [],
        lowerSortedIndex;

  originalArray.forEach(function(item) {
    // Arrow function with implied return:
    lowerSortedIndex = sortedArray.findIndex((nextItem) => nextItem >= item);

    if (lowerSortedIndex >= 0) {
      sortedArray.splice(lowerSortedIndex, 0, item);
    } else {
      sortedArray[sortedArray.length] = item;
    }
  });

    console.log(sortedArray);
}

The arrow function could be re-written as:
function(nextItem) { return nextItem >= item; })

I wrote it here if you want to play with it: http://codepen.io/bassplayer7/pen/oBxmbW


Sieve of eratosthenes: My optimized version I wrote a simple prime number program that checks if a number is prime or not using sieve of eratosthenes, I also wrote an optimized version that only generates the sieve_arr when the val exceeds the length of sieve_arr.
sieve.py
# sieve.py

from array import array
from math import sqrt

def sieve(n=80):
    """Sieve of Erathosthenes"""

    sieve_arr = array('i', [True for _ in range(n+1)])
    sieve_arr_length = len(sieve_arr)

    for i in range(2, int(sqrt(sieve_arr_length)) + 1):
        if sieve_arr[i]:
            for j in range(i + i, n + 1, i):
                sieve_arr[j] = False;

    return sieve_arr

prime.py
# prime.py

import timeit
from array import array
from sieve import sieve

sieve_arr = array('i')

def isprime(val):
    global sieve_arr
    if val >= len(sieve_arr):
        sieve_arr = sieve(val * 2)

    return sieve_arr[val]

def isprime2(val):
    return sieve(val)[val]

if __name__ == '__main__':
    TEST1 = '''
from prime import isprime
for i in range(1, 10001):
    isprime(i)
    '''

    TEST2 = '''
from prime import isprime2
for i in range(1, 10001):
    isprime2(i)
    '''

    tt1 = timeit.repeat(stmt=TEST2, repeat = 5, number=1)
    print('prime Time taken: ', min(tt1))
    tt2 = timeit.repeat(stmt=TEST1, repeat = 5, number=1)
    print('Optimized prime Time taken: ', min(tt2))

I ran the test and had the following results
prime Time taken:  9.446519022000302
Optimized prime Time taken:  0.002043106000201078

Note: I didn't check for 0 and 1 in isprime() and isprime2() which aren't prime number, I wrote the code with performance in mind.

Built-in functions
As mentioned by Marc, Since Python 3.8 you can use math.isqrt.  This is faster.  It is also guaranteed to be accurate when the size of the argument exceeds the number of bits of mantissa in a float.
Type coercion
sieve_arr = array('i', [True for _ in range(n+1)])

Your array is declared to hold 'i' values: integers.  But you are trying to storing boolean values in the array.  This necessitates an unnecessary conversion from True and False to 1 and 0.
Underused variable
The value sieve_arr_length is used once.  It effectively holds n + 1.
In fact, sieve_arr_length = n + 1 might be a more efficient way to initialize it, rather than asking the array for its length.  Then instead of recomputing n+1 each time the j loop starts, you could simply use sieve_arr_length.
Special case even numbers
Every even number greater than 2 is not prime.  There is no need to check for these.  You could simply start your i loop at 3, instead of 2, and go up by twos to check only the odd numbers greater than 1.
Starting the j loop at i+i, or i*2 is an optimization, but not enough of one.  By the time you're checking 13, you've already eliminated 3*13, 5*13, 7*13, and 11*13 because you've eliminated multiples of smaller primes.  You can simply start at i*i.
Plus, if you're only checking the odd prime candidates (because you've special-cased the even), you don't need to eliminate 14*13, 16*13, 18*13 and so on, because those are guaranteed to be even. Your step size can be 2*i, and eliminate half of the redundant assignments.
    # Odd prime candidate sieve ...
    for i in range(3, isqrt(sieve_arr_length) + 1, 2):
        if sieve_arr[i]:
            for j in range(i * i, sieve_arr_length, 2 * i):
                sieve_arr[j] = 0;

Memory
The array('i', ...) uses 4 or 8 (depending on architecture) bytes per element in the array.  RootTwo's solution uses a list structure, instead of the array.  This will increase the amount of storage required for the sieve, since a list must store a pointer in each list element.
Using a bytearray instead of an array('i', ...) would reduce your memory usage, down to 1 byte per sieve element, allowing you to maintain a larger sieve.
def ajneufeld_bytearray(n=80):
    """Sieve of Erathosthenes"""

    prime = bytearray((0, 1)) * ((n + 1) // 2)
    prime[1] = 0
    prime[2] = 1
    for i in range(3, isqrt(n) + 1, 2):
        if prime[i]:
            prime[i*i::2*i] = bytes(len(range(i*i, n, 2*i)))

    return prime

You can further pack 8 flags into each byte, dramatically decreasing the size of your sieve even further, but this bit packing would be complex to implement yourself.  Fortunately it has already been done as a nice package: bitarray.
bitarray implementation
The bitarray package has nice bitslice assignment operators.  RootTwo's slice assignment:
sieve_arr[i*i::i] = [False]*len(range(i*i, len(sieve_arr), i))

is much easier with bitarray.  The equivalent statement would be:
sieve_arr[i*i::i] = False

assigning False to each bit in the slice.
from bitarray import bitarray
from math import isqrt

def ajneufeld(n=80):
    """Sieve of Erathosthenes"""

    prime = bitarray(n)
    prime.setall(False)
    prime[3::2] = True

    prime[2] = True
    for candidate in range(3, isqrt(n) + 1, 2):
        if prime[candidate]:
            prime[candidate*candidate::2*candidate] = False

    return prime

Test code:
import matplotlib.pyplot as plt
from timeit import timeit
from sys import getsizeof

if __name__ == '__main__':

    candidates = (theprogrammer, roottwo, ajneufeld_bytearray, ajneufeld_bitarray)
    limits = [10, 30, 100, 300, 1000, 3000, 10_000, 30_000, 100_000, 300_000]

    print("Memory usage:")
    for candidate in candidates:
        result = candidate(100_000)
        print(f"  {candidate.__name__:19} {getsizeof(result):6}")

    fig, ax = plt.subplots()

    for candidate in candidates:
        times = [ timeit(lambda: candidate(limit), number=1) for limit in limits ]
        ax.plot(limits, times, '-+', label=candidate.__name__)

    ax.legend()
    plt.xscale('log')
    plt.yscale('log')
    plt.show()

Output:

Memory usage:
  theprogrammer       400068
  roottwo             800056
  ajneufeld_bytearray 100057
  ajneufeld_bitarray   12564


Finding happy numbers This is a practice question:

  A happy number is a number defined by the following process:

  Starting with any positive integer, replace the number by the sum of
  the squares of its digits, and repeat the process until the number
  equals 1 (where it will stay), or it loops endlessly in a cycle which
  does not include 1. Those numbers for which this process ends in 1 are
  happy numbers.

Example: 19 is a happy number

\$1^2 + 9^2 = 82\$

\$8^2 + 2^2 = 68\$

\$6^2 + 8^2 = 100\$

\$1^2 + 0^2 + 0^2 = 1\$

Program below solves this problem:

public static boolean isHappy(int n) {
   int sum=0;
   Set<Integer> seenSet = new HashSet<Integer>();
    while (n > 0) {
        int x = n%10;
        sum+=x*x;
        n=n/10;

        if (n == 0 && sum != 1) {
            n=sum;
            sum=0;
            seenSet.add(n);
        } else if (n== 0 && sum == 1 ) {
            return true;
        }
            else if (seenSet.contains(n)) {
            return false;
        }
    }

    return true;
}

I'm looking for any comments on how to improve the code, but a specific focus on whether the way the method returns from multiple places would be appreciated - the multiple return statements concern me.

The repeated n == 0 conditions are not pretty. It would be better to turn that part into a nested condition to avoid the duplication. It will also help simplifying the two conditions on sum.

In addition, there is one more comparison with 0 of n: in the loop condition. That's unnecessary, because n will never become 0. Only if the input is 0, but then the current implementation will return true, which is wrong, since 0 is not a happy number. As such, the loop can be while (true) infinite loop, and your could replace the last return statement with throw new IllegalStateException("whaaaat? How did you get here? ") or similar.

The overall logic would be easier to follow if you separate the summing of the digits from the method exit control. You could add a helper function that returns the sum of the squares of digits. Based on that result you could decide to return a value or continue for another round.

You can replace n=n/10 with n /= 10.

The formatting is messy too. Try the auto reformat feature of your favorite IDE.


Generating Pascal's triangle I am trying to generate Pascal's triangle table in Python 3. I started to like using this following pattern when dealing with 2D arrays.

Generate a list of valid indices
Use a function that works with those indices in a list comprehension. (may produce side effects)

I wonder, is this pattern pythonic and how common is it in practice?

def pascal_triangle(level):
    def compute(table, row, col):
        if row == 0:
            table.append([])
        if col == 0 or row == col:
            table[row].append(1)
        elif col <= row:
            table[row].append(table[row-1][col] + table[row-1][col-1])
    table = []
    valid_idxs = [(r,c) for r in range(level) for c in range(level)]
    [compute(table, r, c) for r, c in valid_idxs]
    print(table)

In :

table = []
valid_idxs = [(r,c) for r in range(level) for c in range(level)]
[compute(table, r, c) for r, c in valid_idxs]
print(table)

the line [compute(table, r, c) for r, c in valid_idxs] definitly shouldn't be a list comprehension as we are building a list that we do not use. If it is side-effects we are interested in, we might as well use a for.

table = []
valid_idxs = [(r,c) for r in range(level) for c in range(level)]
for r, c in valid_idxs:
    compute(table, r, c)
print(table)

Once, this is done, it seems quite clear that the first list comprehension is not really required neither as it could be :

table = []
for r in range(level):
    for c in range(level):
        compute(table, r, c)
print(table)

This being said, even though it looked like a good idea to use a function to extract common code, the responsabilities are not clearly defined making things a bit hard to track. Let's put the body of the function back in the loop and try to see what can be improved :

def pascal_triangle(level):
    table = []
    for r in range(level):
        for c in range(level):
            if r == 0:
                table.append([])
            if c == 0 or r == c:
                table[r].append(1)
            elif c <= r:
                table[r].append(table[r-1][c] + table[r-1][c-1])
    print(table)

First thing to improve : print the content of table before or after the table.append([]) : isn't it a bit weird to build all lists as we go through the c variable ? Also, the first list created is non-empty but the other one are. Let's make the process clearer and add an empty list at each r iteration.

def pascal_triangle(level):
    table = []
    for r in range(level):
        print(table) # look how the tables gets populated now : isn't it cool ?
        table.append([])
        for c in range(level):
            if c == 0 or r == c:
                table[r].append(1)
            elif c <= r:
                table[r].append(table[r-1][c] + table[r-1][c-1])
    print(table)

Let's go one step further, please note that the deeper loop will not do anything if c > r (this wasn't true before previous change but it is now correct). We can easily remove this pointless iterations (and the pointless check) :

def pascal_triangle(level):
    table = []
    for r in range(level):
        print(table)
        table.append([])
        for c in range(r+1):
            if c == 0 or r == c:
                table[r].append(1)
            else:
                assert(c<=r)
                table[r].append(table[r-1][c] + table[r-1][c-1])
    print(table)

Now, it's time for the special trick : we could remove the test if c == 0 by performing this before the loop. Also, we could't remove the if c == r by performing this after the loop (but we need to check that this would have happened which is when r>0).

def pascal_triangle(level):
    table = []
    for r in range(level):
        print(table)
        table.append([])
        table[r].append(1)
        for c in range(1, r):
            table[r].append(table[r-1][c] + table[r-1][c-1])
        if r:
            table[r].append(1)
    print(table)

Now, we can try to be more fancy : instead of calling table[r] every time : let's introduce a line variable and use it :

def pascal_triangle(level):
    table = []
    for r in range(level):
        line = []
        line.append(1)
        for c in range(1, r):
            line.append(table[r-1][c] + table[r-1][c-1])
        if r:
            line.append(1)
        table.append(line)
    print(table)

We can see that this looks a lot like a list comprehension scenario and indeed, we can use it :

def pascal_triangle(level):
    table = []
    for r in range(level):
        table.append([1] + [table[r-1][c] + table[r-1][c-1] for c in range(1, r)] + ([1] if r else []))
    print(table)


Displaying API results inside a WordPress page template I've created a WordPress page template that calls an API using Httpful and formats the results. This is my first time using APIs on a page, so I'd like to know if this is the right direction to go in.

The code below uses information from a members API to receive the member's information and display their basic information along with a single upcoming class.

page-members.php

<?php
/**
 * The template for displaying a list of members.
 *
 * @package themename
 */

get_header();

// call from api using basic auth
require_once 'inc/httpful.phar';
$uri = "https://api.example.com/v1/accounts/KEY/members_list";
$response = \Httpful\Request::get($uri)
    ->addHeader('Authorization', 'Basic KEY')
    ->expects('application/json')
    ->send();
?>

        <?php
        $defaultImage = get_field('logo', 'option');

        // loop through api response for each member
        foreach($response->body->members as $member) :
            $memberID = $member->id;

            // if member has no id, skip member
            if (empty($memberID)) :
                continue;
            endif;

            // if member has no image, use default logo
            $memberImage   = (!empty($member->image)) ? $member->image : $defaultImage;
            $memberName    = $member->name;
            $memberTagline = $member->tagline;

            // loop through upcoming classes for each member
            foreach($member->upcoming_classes as $class) :
                $classID = $class->id;

                // if class has no id, skip class
                if (empty($classID)) :
                    continue;
                endif;

                $className = $class->name;
                $classDate = $class->date;
                $classTime = $class->start_time;
            ?>

                <a href="<?php echo site_url() . '/member/?MemberID=' . $memberID; ?>">
                    <div class="members-image" style="background: url('<?php echo $memberImage; ?>') no-repeat top center / cover;">

<?php echo $memberName; ?>

<?php echo $memberTagline; ?>

                        <?php echo $className; ?>

                        <?php echo date('m/d', strtotime($classDate)); ?> — <?php echo date('g:i a', strtotime($classTime)); ?>

                    <a class="button" href="<?php echo site_url() . '/reserve/?classID=' . $classID; ?>">Book

        <?php
        // end class loop by breaking on first upcoming class with an id
        break; endforeach; endforeach;
        ?>

<?php get_footer(); ?>

Is there anything I can do to improve this single template?

I see three things

I'm not too terribly familiar with custom wordpress tempaltes, but I'm fairly certain you shouldn avoid doing the API logic in the view. You should call the API in a controller and pass the result to the view, if at all possible. This is a standard separation of concerns.

Additionally, you shouldn't assume the API call will work. If they're server or dns has issues, or they make a breaking change to their API, you should handle it gracefully and inform the user that there are issues. Right now it appears foreach($response->body->members as $member) would fail if the API failed.

Finally, it's a good idea to keep the "Basic KEY" in a config or something, so you don't need to touch the code to update the key.


Socket.io wrapper for an app with ReactJS components I am upgrading a traditional jQuery based app by converting a few parts of it into ReactJs components and use Socket.io for the data transfer medium.

There are a few React components spread across the page. The React components will receive updates from the socket connection in their componentDidMount method.

For example:

componentDidMount: function(){
    socket.on('connect', function(data){....});
    socket.on('anotherEvent', function(data){....})
}

Now, the thing is that the React components will be scattered across the page and will not be under a single app structure, and they will be subscribing to the same socket events to stay in sync.

For this, I wrote the below Socket.io wrapper, this way multiple subscriptions for the same events can be avoided but all the interested React components can be notified on an event update.

window.socketEvents = (function(){
    var socket = io.connect("http://localhost:3000"),
        events = {},                    //events structure = {'event_name':subscribers:[callback functions], isSubscribed:true}
        callOnConnect = [];

    var _register = function(event, callback){
        events[event] = events[event] || {};
        events[event].subscribers = events[event].subscribers || [];
        events[event].subscribers.push(callback);
    };

    var _subscribe = function(event){
        console.log('Subscribingto: ', event);
        socket.on(event, function(data){    // notifiy the subscribers of this event by firing their callbacks.
            console.log('received evt: ', data);
            for(var cb=0;cb<events[event].subscribers.length;cb++){
                events[event].subscribers[cb](data);
            };
        });
        events[event].isSubscribed = true;
    };

    var _emit = function(event, data, onConnect){
        console.log('_emit called for: ', event);
        socket.emit(event, data);
        if(onConnect){
            callOnConnect.push(function(){
                socket.emit(event, data);
            });
        }
    };

    var _listen = function(event, callback){
        _register(event, callback);
        if(!events[event].isSubscribed){
            _subscribe(event);
        };
    };

    setTimeout(function(){  // register a little late to avoid multiple callbacks.
        socket.on('connect', function(){
            console.log('connect fired');
            for(var i=0;i<callOnConnect.length;i++){
                callOnConnect[i]();
            }
        });
    },1000);

    return {
        listen: _listen,
        emit: _emit
    }

})();

React example with the new wrapper:

In ReactClassOne:

componentDidMount: function(){
    socketEvents.emit('join_service', {name:'Some name'}, true); // the third boolean param states that should the event be emitted if the socket disconnects and then reconnects.
    socketEvents.listen('anotherEvent', function(data){...})
}

In ReactClassTwo:

componentDidMount: function(){
    socketEvents.emit('join_service', {name:'Some name'}, true);
    socketEvents.listen('anotherEvent', function(data){...})
}

The code is currently working fine during the preliminary development but I am worried about the timeout in the socketEvents. So, I would like this to be reviewed.

It looks like you've written the code that already exists.

  this way multiple subscriptions for the same events can be avoided but all the interested React components can be notified on an event update

Why would you want to avoid this? There is no overhead in a function call. In fact, this is exactly how Socket.io client works (based on Emitter component):

Emitter.prototype.on =
Emitter.prototype.addEventListener = function(event, fn){
  this._callbacks = this._callbacks || {};
  (this._callbacks['$' + event] = this._callbacks['$' + event] || [])
    .push(fn);
  return this;
};

Then there is this hack. If the client will connect in less than a second, you'll miss 'connect' event:

setTimeout(function(){  // register a little late to avoid multiple callbacks.
    socket.on('connect', function(){
        console.log('connect fired');
        for(var i=0;i<callOnConnect.length;i++){
            callOnConnect[i]();
        }
    });
},1000);

Last, it's a good practice to unsubscribe from events in componentWillUnmount. Your wrapper doesn't allow this.


Retrieve the index of the first element using a predicate I want to change the legacy C-style code by using stl:

for (posEmptyItem = startAt; strlen(collection[posEmptyItem]) > 10; posEmptyItem++) {}
std::cout << posEmptyItem << std::endl;

This code seems a bit hard to read. Anyway to do better?

auto it = std::find_if(collection + startAt,
                       collection + COLLECTION_SIZE,
                       [](const char* line) { return strlen(line) <= 10; });
int idx = std::distance(collection, it);

Below a complete example:

#include <cstring>
#include <iostream>
#include <algorithm>

#define COLLECTION_SIZE      250

int main()
{
    const char* collection[COLLECTION_SIZE]{ "time11,time2,time3",
                                       "time12,time2,time3",
                                       "time13,time2,time3",
                                       "time14,time2,time3",
                                       "time15,time2,time3",
                                       "x\n",
                                       "" };
    auto startAt = 2;
    int posEmptyItem;

    // legacy code
    for (posEmptyItem = startAt; strlen(collection[posEmptyItem]) > 10; posEmptyItem++) {}
    std::cout << posEmptyItem << std::endl;

    // replace the loop to search an index by calling to standard library
    auto it = std::find_if(collection + startAt,
                           collection + COLLECTION_SIZE,
                           [](const char* line) { return strlen(line) <= 10; });
    posEmptyItem = std::distance(collection, it);
    std::cout << posEmptyItem << std::endl;

    return 0;
}

For "easier readability" you could extern the lambda expression form the find_if():

auto pred = [](const char* line) { return strlen(line) <= 10; };
auto it = std::find_if(collection + startAt,
                       collection + COLLECTION_SIZE,
                       pred);

Also make use of std::begin() and std::end():

auto it = std::find_if(std::begin(collection) + startAt,
                       std::end(collection),
                       pred);

At least (but probably not last), don't use raw arrays. Rather change collection to a std::array:

std::array<const char*,COLLECTION_SIZE> collection
    { "time11,time2,time3"
    , "time12,time2,time3"
    , "time13,time2,time3"
    , "time14,time2,time3"
    , "time15,time2,time3"
    , "x\n"
    , ""
    };
// Note my formatting style above, which makes it easier to extend the array

Here's the fully refactored code:

#include <cstring>
#include <iostream>
#include <algorithm>
#include <array>

const size_t COLLECTION_SIZE = 250; // rather use a const variable than a macro

int main()
{
    std::array<const char*,COLLECTION_SIZE> collection
        { "time11,time2,time3"
        , "time12,time2,time3"
        , "time13,time2,time3"
        , "time14,time2,time3"
        , "time15,time2,time3"
        , "x\n"
        , ""
    };

    size_t startAt = 2; // care about the correct type. auto would leave you with int

    // replace the loop to search an index by calling to standard library
    auto pred = [](const char* line) { return strlen(line) <= 10; };
    auto it = std::find_if(std::begin(collection) + startAt,
                           std::end(collection),
                           pred);
    auto posEmptyItem = std::distance(std::begin(collection), it);
    std::cout << posEmptyItem << std::endl;

    return 0;
}

See Live Demo


Allowing users to subscribe to categories I am working on a Rails 3 application, and am having some trouble with my user/edit view. In this application, Users can subscribe to Categories. Users and Categories have a has_and_belongs_to_many relationship through a join table (categories_users).

In my user/edit view, I need to display a list of checkboxes for the various categories so the user can edit his subscriptions. I am using the following code to achieve this:

  <% Category.roots.order('name ASC').each do |category| %>

      <li class="parent <%= category.name %>">
        <%= check_box_tag "user[category_ids][]", category.id, @user.categories.include?(category) %> <%= category.name %>

      <ul id="children-<%= category.id %>" class="children-list">
        <% category.children.order('name ASC').each do |child| %>
          <li class="child <%= child.name %>">
            <%= check_box_tag "user[category_ids][]", child.id, @user.categories.include?(child) %> <%= child.name %>

        <% end %>

  <% end %>

The Category model uses the ancestry gem for hierarchy, which is there the roots and children methods come from. Roots returns all the parent categories, and children returns all of the sub-categories for a given parent.

The really problematic part of this code seems to be @user.categories.include?(child) which is running 81 separate queries (one for each child category). This line is used to check if the category is currently subscribed to by the user -- if so, the checkbox is marked checked. Is there a better/faster way to accomplish this?

For reference, here is the join table's schema definition - I tried to put an index on the foreign keys thinking it would speed things up, but this is still very slow compared to the rest of my app.

create_table "categories_users", :id => false, :force => true do |t|
  t.integer "category_id"
  t.integer "user_id"
end

add_index "categories_users", ["category_id"], :name => "index_categories_users_on_category_id"
add_index "categories_users", ["user_id"], :name => "index_categories_users_on_user_id"

When you load the user, you can include the categories using eager loading.

@user = User.includes(:categories).find(params[:id])

or

@user = User.find(params[:id])
@categories = @user.categories.all


Query to find users from where? Based on my question here, I have updated my query to take user input so it can be a generic query:

DECLARE @location AS nvarchar(max) = ##location:string##;
SELECT u.Id [User Link], u.Reputation, u.Location
FROM Users u
WHERE lower(u.Location) LIKE lower('%' + @location + '%')
ORDER BY 'Reputation' DESC

Is there something I should be doing differently, or is this good?

This looks fine, the only thing I would say is @location is being declared as nvarchar(max). I can only assume that u.Location is also nvarchar(max). If it isn't then set it to be the same size. nvarchar max will have a slight performance hit to it.


Calculate course module credits from individual assessment scores I'm currently working on a grade calculator for a university (professionally), and I am wondering if anyone can suggest an improvement to the following query? Here is my database:

The query returns the following table:

SELECT
        `course`.`title` AS `course_title`,
        `yearGroup`.`id` AS `year_group_id`,
        `yearGroupLookup`.`yearGroup` AS `year_group`,
        `yearGroup`.`max_credits`,
    TRUNCATE
        (
            COALESCE(
                SUM(
                    `assessment`.`percentage_achieved` *(
                        `assessment`.`percentage_weighting` / 100
                    ) /(
                    SELECT
                        COUNT(*)
                    FROM
                        `module`
                    WHERE
                        `yearGroup`.`id` = `module`.`year_group_id`
                )
                ),
                0
            ),
            2
        ) AS `grade`,
    TRUNCATE
        (
            COALESCE(
                SUM(
                    `assessment`.`percentage_achieved` *(
                        `assessment`.`percentage_weighting` / 100
                    ) /(
                    SELECT
                        COUNT(*)
                    FROM
                        `module`
                    WHERE
                        `yearGroup`.`id` = `module`.`year_group_id`
                )
                ),
                0
            ) / 100 * `yearGroup`.`max_credits`,
            2
        ) AS `credit_achieved`
    FROM
        `assessment`
    RIGHT JOIN `module` ON `module`.`id` = `assessment`.`module_id`
    RIGHT JOIN `yearGroup` ON `yearGroup`.`id` = `module`.`year_group_id`
    RIGHT JOIN `course` ON `course`.`id` = `yearGroup`.`course_id`
    RIGHT JOIN `yearGroupLookup` ON `yearGroup`.`yearLookup_id` = `yearGroupLookup`.`id`
    WHERE
        `yearGroup`.`course_id` = 1
    GROUP BY
        `yearGroup`.`id`

Minor stuff: your backticks are distracting, and unnecessary in most (all?) cases. I'd delete them.
Your use of TRUNCATE seems like a formatting concern and not a query concern. I'd expect that a query like this just return a full, floating-point number, and leave it up to the application as to whether rounding or truncation needs to be performed.


Outputting a large list to a web client Awhile back, I wrote a quick and dirty webapp to read the remote server's log files, without a user needing to ssh in to view it via command line. It's meant to be a quick and dirty tool since we don't have anything like logstash available.

I am rewriting in Java 8 and have used the Streaming API to remove about 60% of the existing code.

What I need reviewed is the following function:

@RequestMapping(value="logLines")
public void getLog(@RequestParam String logName, @RequestParam Integer logSize, HttpServletResponse response) throws Exception {

    log.debug("Entered getLog(). log: {}, logSize: {}", logName, logSize);

    PrintWriter writer = response.getWriter();
    // stream a line at a time to try to minimize memory usage
    Stream<String> stream = logService.getLogStream(logName, logSize);
    stream.forEach(p -> {
        writer.println(p);
    });

}

The previous version of the logService.getLogStream() method returned a List<String> which would have at least 1000 entries (and likely more). This version is meant to be optimized to avoid the memory overhead of a large List implementation. Also, it is meant so that the garbage collector can clean up used entries.

Am I really getting the benefits here that I want?

Here's the service method being used. There's some rudimentary HTML processing stuff being done to the lines read from the text file, which, if this wasn't quick and dirty code, I'd find a more elegant way to do.

/* JAVA 8 CODE */
@Override
public Stream<String> getLogStream(String logName, int lines) throws Exception {

    log.debug("Entered getLogStream()");

    final String filepath = properties.getProperty(logName);
    Path path = Paths.get(filepath);
    if (Files.exists(path)) {
        long countOfLines = Files.lines(path).count();
        Stream<String> result = Files.lines(path)
                .skip(Math.max(0, countOfLines - lines))
                .map(p -> p.trim())
                .map(p -> HtmlUtils.htmlEscape(p))          // Spring Util to escape the string for HTML since I'm already using Spring
                .map(p -> {
                    if (p.startsWith("at ")) {
                        return "" + p + "
\r\n";
                    } else {
                        return p + "
";
                    }
                });

        return result;
    } else {
        throw new IOException(String.format("Log file %s not found!", logName));
    }
}

If you want to prevent the whole payload from being loaded into memory, you’ll very likely need to send a chunked http response as well. Otherwise I can’t see how the payload doesn’t need to be loaded into RAM completely at some stage.

Looking at the documentation for ServletResponse.html#getWriter(), it seems like the PrintWriter just accumulates all writes in memory until it is finally flushed (and the http response sent).

Another point to consider is that logService (whose code you did not supply) should of course not just load the file and then stream its lines, but needs to really stream the file from disk. Otherwise you’ll not have won anything.

Finally a small tip: In place of an inline lambda you can more concisely write stream.forEach(writer::println) in this case. :)

edit: Service looks fine, but you’re reading the whole file twice – once to find number of lines, and then a second time to get the actual output. (And the number of lines might even have changed in the meantime.) I can’t think of a good solution to this, sadly.

Btw, again I recommend using String::trim and HtmlUtils::htmlEscape as somewhat more elegant. You can also extract the third function into a private String addHtmlFormatting(String escapedError) and then reference it like this::addHtmlFormatting.


Get string distance for a pseudo fuzzy search We are using the following for evaluating strings for a pseudo fuzzy search. Curious if anything sticks out as needing optimization

    function getEditDistance(a, b) {
    if (a.length == 0) return b.length;
    if (b.length == 0) return a.length;

    var matrix = [];
    var i;
    for (i = 0; i <= b.length; i++) {
        matrix[i] = [i];
    }

    var j;
    for (j = 0; j <= a.length; j++) {
        matrix[0][j] = j;
    }

    for (i = 1; i <= b.length; i++) {
        for (j = 1; j <= a.length; j++) {
            if (b.charAt(i - 1) == a.charAt(j - 1)) {
                matrix[i][j] = matrix[i - 1][j - 1];
            } else {
                matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1,
                    Math.min(matrix[i][j - 1] + 1,
                        matrix[i - 1][j] + 1));
            }
        }
    }

    return matrix[b.length][a.length];
};

The code becomes 25% faster if we extract values not changed in the loops to variables, but of course it's observable only on large number of repetitions or long strings as tested in Chrome.

function getEditDistance(a, b) {
    var lenA = a.length, lenB = b.length;
    if (lenA == 0) return lenB;
    if (lenB == 0) return lenA;

    var matrix = [];
    var i;
    for (i = 0; i <= lenB; i++) {
        matrix[i] = [i];
    }

    var j, m0 = matrix[0];
    for (j = 0; j <= lenA; j++) {
        m0[j] = j;
    }

    for (i = 1; i <= lenB; i++) {
        var m = matrix[i], mPrev = matrix[i - 1];
        var bChar = b.charAt(i - 1);
        for (j = 1; j <= lenA; j++) {
            if (bChar == a.charAt(j - 1)) {
                m[j] = mPrev[j - 1];
            } else {
                m[j] = Math.min(mPrev[j - 1] + 1, Math.min(m[j - 1] + 1, mPrev[j] + 1));
            }
        }
    }

    return matrix[lenB][lenA];
}

P.S. Replacing s.charAt(i) with s[i] provides no tangible speedup and is not cross-browser.


Reducing time limit for prime number program I have to create a program in which t sets of m and n will be given by the user. For each set, we have to generate all prime numbers between m and n.  This code is successfully executed with the correct output, but I have to reduce the time limit for a large data range.

#include<math.h>
#include<stdio.h>
int isPrime(int k);
int j;
int isPrime(int k)
 {
  for(j=2; j<=(int)sqrt(k); j++)
    {
      if(k%j==0)
      return 0;
    }
  return 1;
}

int main()
{
   int u,i,t,m,n,k;
   scanf("%d", &t);
   while(t--)
       {
         scanf("%d %d", &m, &n);

         for(i=m;i<=n;i++)
             {
                  u = isPrime(i);
                  if (u == 1)
                  printf("%d\n", i);
             }
       }
   return 0;
}

For this SPOJ problem with (1 <= m <= n <= 1000000000, n-m<=100000) limits you will have to use an offset sieve of Eratosthenes.

Since n <= 10^9, to sieve any range under n it is sufficient to have just the primes below its square root, 31622, pre-calculated. There are only 3401 such primes, the biggest being 31607.

So, A. pre-calculate the 3401 primes below 31622.

Since n-m<=100000, you can B. have one array of 100000 booleans preallocated, and sieve it by those primes, as in sieve of Eratosthenes, for each pair m,n.

As an easy optimization, use that sieve segment to represent just odds, without evens: an entry at position i will represent odd(m) + 2*i where odd(m) is the lowest odd number not below m in value. Sieve this segment by odd primes only. No even number above 2 is ever a prime:

for i = 3, 5 ... √n in primes:
   for j = i^2, i^2+2*i, i^2+4*i ...   inside the segment  m ... n:
      A{j} := false


Removing undesired points from a mesh I have a 3D data set made of a certain number of points (x,y,z) that cover a certain region of space. Using the scatteredInterpolant object I can interpolate this data set over a grid to produce a rectangular mesh. Note that the mesh may extend to regions that are not defined by the data set; in fact, after the mesh generation I need to remove the part of the mesh that is extrapolated away from the data set (replacing its values with NaN, for example) in order to retain only the mesh generated between the data points.

I came up with the following MATLAB script to solve this problem in a very naive way. Considering a single mesh point (xq,yq), I evaluate the minimum distance between this point and the data set; if this distance is greater than a certain threshold, then the corresponding interpolated value (zq) is set to NaN.

%% Data set (x,y,z)
x = [3 3 3 4 4 4 4 4 5 5 5 5 5]';
y = [1 2 3 0 1 2 3 4 0 1 2 3 4]';
z = [.5 .505 .51 .51 .51 .51 .51 .515 .535 .528 .53 .53 .53]';

%% Interpolant
F = scatteredInterpolant(x,y,z,'natural');

%% Mesh generation (xq,yq,zq)
delta = 0.5;
ti = 0:delta:5;
si = 0:delta:4;
[xq,yq] = meshgrid(ti,si);
zq = F(xq,yq);

%% Replacing undesired values with NaN
thresh = 1;
n = length(ti) * length(si);
m = length(x);
xqcol = reshape(xq,[n,1]);
yqcol = reshape(yq,[n,1]);
zqcol = reshape(zq,[n,1]);
tab = [xqcol yqcol zqcol];

for i = 1:n
   dmin = 10^32;
   for k = 1:m
      diffx = tab(i,1) - x(k);
      diffy = tab(i,2) - y(k);
      d = sqrt(diffx^2 + diffy^2);
      if d < dmin
         dmin = d;
      end
   end
   if dmin >= thresh
      tab(i,3) = NaN;
   end
end

zqwork = tab(:,3);
zq2 = reshape(zqwork,[size(zq,1),size(zq,2)]);

%% Plotting
figure
plot3(x,y,z,'.r','MarkerSize',10);   % data set
grid on; axis([0 5 0 4 0.46 0.54]);
hold on;
% mesh(xq,yq,zq); view(3);           % full mesh
mesh(xq,yq,zq2); view(3);            % mesh with undesired points removed

This code gets the job done (to my utter surprise, since I am not really proficient with Matlab!). Here you can find a full mesh, and here the same mesh after the undesired points are removed. In both pictures the red dots represent the initial data set the mesh is interpolated from.

My main concern here is that while this code works fine with a limited number of points in the starting data set, it gets incredibly cumbersome when the number of data points is in the order of hundreds of thousands, which is sadly my case. I can let it be if necessary, but I feel like I should make the code a bit more efficient, since I may have to run it many times. Do you have any suggestion on how to improve its efficiency? Any input will be greatly appreciated.

I am using MATLAB 2017a.

MATLAB's documentation about the scatterInterpolant function actually mentions in passing how one would do this in https://www.mathworks.com/help/matlab/ref/scatteredinterpolant-object.html#bvkm1tv-4. In short, you would set F.ExtrapolationMethod to None so that F returns NaN for points that do not lie inside the convex hull of the points defined by x and y. This way, you can avoid writing for loops and a bunch of reshape commands, which can take a while for large vectors and matrices.

x = [3 3 3 4 4 4 4 4 5 5 5 5 5]';
y = [1 2 3 0 1 2 3 4 0 1 2 3 4]';
z = [.5 .505 .51 .51 .51 .51 .51 .515 .535 .528 .53 .53 .53]';

F = scatteredInterpolant(x,y,z,'natural');

delta = 0.5;
ti = 0:delta:5;
si = 0:delta:4;
[xq,yq] = meshgrid(ti,si);
zq = F(xq,yq);

figure
hold on
mesh(xq,yq,zq)
plot3(x,y,z,'r.','MarkerSize',10)
hold off
view(3)
axis([0 5 0 4 0.46 0.54])

F.ExtrapolationMethod = 'none';
zqe = F(xq,yq);

figure
hold on
mesh(xq,yq,zqe)
plot3(x,y,z,'r.','MarkerSize',10)
hold off
view(3)
axis([0 5 0 4 0.46 0.54])

Plot of all points, including extrapolated ones:

Plot of points inside convex hull, excluding extrapolated ones:


Pandas Filtering based on the length of the same kind variables in a column In my real case I have a set of time series related to different IDs stored in a single DataFrame
Some are composed by 400 samples, some by 1000 samples, some by 2000.
They are stored in the same df and:
I would like to drop all the IDs made up of time series shorter than a custom length.
I wrote the following code, but I think is very ugly and inefficient.
import pandas as pd
import numpy as np

dict={"samples":[1,2,3,4,5,6,7,8,9],"id":["a","b","c","b","b","b","c","c","c"]}
df=pd.DataFrame(dict)

df_id=pd.DataFrame()

for i in set(df.id):
  df_filtered=df[df.id==i]
  len_id=len(df_filtered.samples)

  if len_id>3: #3 is just a random choice for this example
    df_id=df_id.append(df_filtered)
print(df_id)

Output:
   samples id
2        3  c
6        7  c
7        8  c
8        9  c
1        2  b
3        4  b
4        5  b
5        6  b

How to improve it in a more Pythonic way?
Thanks

Good answer by Juho. Another option is a groupby-filter:
df.groupby('id').filter(lambda group: len(group) > 3)

#    samples id
# 1        2  b
# 2        3  c
# 3        4  b
# 4        5  b
# 5        6  b
# 6        7  c
# 7        8  c
# 8        9  c

To match your output order exactly, add a descending id sort: .sort_values('id', ascending=False)


Identifying surface events happening at specific time intervals Here is some code I wrote to surface Mint.com transactions that occur at monthly intervals, in order to identify subscriptions I may be paying for without realizing it.

I'd like to have some friends try this out as well, but Pandas is not a very common package for non-Python users, and it's requirement is keeping a lot of people from trying this out.

I'm looking for any advice to improve the code, or better recognize the 'monthly interval' piece (without Pandas would be even better).

import sys
from datetime import datetime

import pandas as pd

MINT_DATE_FORMAT = "%m/%d/%Y"
MINT_MERCH_COLUMN = "Description"
MINT_DATE_COLUMN = "Date"
MINT_AMT_COLUMN = "Amount"

def parse_date(datestring, format):
  return datetime.strptime(datestring,  format)

def calculate_date_diff(list):
  diffs = []
  for i in range(0,len(list)-1):
    diffs.append(diff_month(parse_date(list[i], MINT_DATE_FORMAT),
    parse_date(list[i+1], MINT_DATE_FORMAT)))
  return diffs

def diff_month(d1, d2):
  return(d1.year - d2.year)*12 + d1.month - d2.month

def unique_merchants(df, merch_column = MINT_MERCH_COLUMN):
  return df[merch_column].unique().tolist()

def merchant_price_txn_list(df, merchant_name,  txn_amount,
                      merch_column = MINT_MERCH_COLUMN, date_column = MINT_DATE_COLUMN,
                      amt_column = MINT_AMT_COLUMN):
  return df[(df[merch_column] == merchant_name) & (df[amt_column] == txn_amount)][date_column].tolist()

def unique_pricepoints(df, merchant_name,
                      merch_column = MINT_MERCH_COLUMN, amt_column = MINT_AMT_COLUMN):
    return df[(df[merch_column] == merchant_name)][amt_column].unique().tolist()

def main(csv):
  foo = pd.read_csv(csv)
  print("reading csv")
  print("Read rows:", len(foo.index))
  for merch in unique_merchants(foo):
    for pricepoint in unique_pricepoints(foo, merch):
      unique_mo_diffs = set(calculate_date_diff(
                         merchant_price_txn_list(foo, merch, pricepoint)))
      if len(merchant_price_txn_list(foo, merch, pricepoint)) < 3:
        pass
      elif unique_mo_diffs != {1}:
        pass
      else:
        date_list = merchant_price_txn_list(foo, merch, pricepoint)
        print merch, "\n", "$", pricepoint, "\n", date_list, "\n"

if __name__ == '__main__':
  txn_csv = sys.argv[1]
  main(txn_csv)

With the caveat that I am not familiar with either the Mint.com format or Pandas, I can make a few suggestions:

The Python convention is to indent by 4 spaces, and wrap lines at 79 characters. It’s not a hard requirement, but it would be less jarring for me to read your code. And use one space after commas, it’s easier to read.
There aren’t any comments or docstrings to help me understand your code, which will hinder anybody trying to review or understand it.
It’s not clear to me why you define the parse_date() function. It has the same argument signature as datetime.strptime(), but now somebody familiar with the datetime module will find it harder to read your code. It would be better to replace all calls to this function with datetime.strptime().
In the calculate_diff_list function:

Don’t use list as a parameter. Overriding builtins is a bad idea, and can lead to subtle bugs.
You don’t say if you’re using Python 3 or Python 2.x; if the latter, use xrange() instead. It’s faster and more memory efficient.

In the diff_month function, it would be good to provide a docstring explaining exactly what you mean by “difference in months”, and how this function should be used. You should also specify the ordering, because it’s not necessarily obvious.

For example, consider 31 Dec 2014 and 1 Jan 2015. Are they a month apart or not? Your function returns 1 and -1 (!) depending on the ordering.
In unique_merchants(), you could make a couple of tweaks:

Default keyword arguments don’t have spaces around the equals sign.
If you could drop the pandas requirement, this could be reduced to set(df[merch_column]). You only need an iterable, not a list. This goes for the other functions along here (which also need some docstrings and comments, really).

As far as I can tell, the only bit of functionality that relies on pandas is foo = pd.read_csv(csv). (foo and csv are terrible variable names.) Have a look and see if the builtin csv module can be used here, then you might be able to simplify some more of the code.


Editing  values through a textbox with jquery I've written a script that will allow me to enter a serial number for each received product, in-line in a table. First you enter the amount received, which increases the size of the <select> to that amount. Then you can enter new serial numbers through the s/n text box, submitting with TAB or Return.

Additionally, selecting one of the options (empty or filled) will allow you to edit that specific option - otherwise, the next empty line is taken when submitting a serial number. It also checks if the entered serial number already exists, which disallows entering it. This works with multiple products (each tbody is a product "row").

I'm looking for any tips to improve style, best practices and performance. Thanks in advance.

Here's a working JSFiddle (I've translated the comments, but classes etc. are still Dutch):
http://jsfiddle.net/iv00w/7wds214d/

My HTML code:

                Amount: <input type="text" value="0" />

            omschrijving etc.

            Enter s/n:

                <input type="text" />

                <select size="0"></select>

And my Javascript:

$(".AantalOntvangen input").each(function () {
    $(this).keyup(function (event) {
        var aantal = $(this).val();

        // find the select box that holds the s/n
        var serieNrBox = $(this).parent().parent().nextAll().last().find("select");

        // resize it
        serieNrBox.prop("size", aantal);

        // append new options until it matches the size
        while (serieNrBox.children().length < aantal) {
            serieNrBox.append($("<option></option>"));
        }

        // select the first option
        $(serieNrBox).children().first().prop("selected", true);
    });
});

$(".SerieNr input").each(function () {
    $(this).keydown(function (event) {
        if (event.which == 9 || event.which == 13) {
            var serieNrBox = $(this).parent().parent().next().find("select");
            var selected = $("option:selected", serieNrBox);
            var emptyOptions = serieNrBox.find("option")
                .filter(function () {
                return !this.value || $.trim(this.value).length === 0;
            });
            var serienr = $(this).val();
            var exists = false;

            // the s/n has to be unique
            serieNrBox.children().each(function () {
                if ($(this).val() == serienr) {
                    // error
                    exists = true;
                }
            });

            // if it is unique, update the selected, or next empty, option
            if (exists === false) {
                if (selected.val().length > 0) {
                    emptyOptions.first().prop("selected", true);
                } else {
                    emptyOptions.next().first().prop("selected", true);
                }

                selected.text(serienr);
                selected.val(serienr);

                $(this).val("").focus();
            }
        }
    });
});

$(".SerieNr select").each(function () {
    $(this).change(function () {
        // update the textbox
        var selected = $("option:selected", this);
        var serieNr = $(this).parent().parent().prev().find("input");
        serieNr.val(selected.val()).focus().select();
    });
});

First: Rename your variables etc. to English. This is probably why you have not received an answer for one of the most common topics in CR.

Bad Relative Traversal

This is bad practice:

var serieNrBox = $(this).parent().parent().nextAll().last().find("select");
//...
var serieNrBox = $(this).parent().parent().next().find("select");
//...
var serieNr = $(this).parent().parent().prev().find("input");

If you traverse the document tree like this you will need to do shotgun surgery each time anything changes in the DOM. For example, think what would have to change in the above code, if you (or the next developer who maintains this) has to add another select in the same cell as the current select.

Do not traverse DOM to like this to find an element. Give each element you need to find a class name and find relative to the container.

Better Organization

I understand you do this to support multiple product tbodys. You can instead do a:

$("#BestelRegels").find(".BestelRegel").each(function(index, orderRule) {
    //You can locate things relative to orderRule now...

which follows the logical organization of the document.

Unnecessary each

each function bodies with a single function call to $(this) is unnecessary:

$(".SerieNr select").each(function () {
    $(this).change(function () {

is equivalent to:

$(".SerieNr select").change(function () {

Applying these you get:

function alreadyExists(serieNrBox, serienr) {
    return serieNrBox.children().is(function () {
        return $(this).val() === serienr;
    });
}

$("#BestelRegels").find(".BestelRegel").each(function(index, orderRule) {

   // give these elements unique (within tbody) class names instead
   var serieNrBox = $("select", orderRule);
   var txtSerieNr = $(".SerieNr input", orderRule);
   var txtNumberReceived = $(".AantalOntvangen input", orderRule);

    txtNumberReceived.keyup(function (event) {
        var aantal = $(this).val();

        // resize it
        serieNrBox.prop("size", aantal);

        // append new options until it matches the size
        while (serieNrBox.children().length < aantal) {
            serieNrBox.append($("<option></option>"));
        }

        // select the first option
        serieNrBox.children().first().prop("selected", true);
    });

    txtSerieNr.keydown(function (event) {
        if (event.which !== 9 && event.which !== 13) return;

        var serienr = $(this).val();

        // the s/n has to be unique
        if (alreadyExists(serieNrBox, serienr)) return;

        // update the selected, or next empty, option
        var selected = $("option:selected", serieNrBox);

        selected.text(serienr);
        selected.val(serienr);

        var emptyOptions = serieNrBox.find("option")
            .filter(function () {
            return !this.value || $.trim(this.value).length === 0;
        });

        (selected.val().length > 0
            ? emptyOptions
            : emptyOptions.next())
                .first()
                .prop("selected", true);

        $(this).val("").focus();
    });

    serieNrBox.change(function () {
        // update the textbox
        var selected = $("option:selected", this);
        txtSerieNr.val(selected.val()).focus().select();
    });
});

Notes:

References to parent and children are gone (except for
serieNrBox.children() which is quite safe).
keydown handler is simplified.
You can check if any element in a selection satisfies some condition
using is.


Jaccard distance between strings in Rust The Jaccard distance between two sets is the size of their intersection divided by the size of their union. For example, the distance between {1, 2, 3} and {2, 3, 4} is 2 ({2,3}) / 4 ({1,2,3,4}) = 0.5.

The Jaccard distance can be used for string similarity by slicing a string into word or character n-grams. For example, with 2-character n-grams:

"Pear" vs "Peach"
`{"Pe", "ea", "ar"}` vs `{"Pe", "ea", "ac", "ch"}` = 2/5 = 0.4

I've written an implementation in Rust as an attempt to learn something about the language.

extern crate unidecode;
use unidecode::unidecode;

use std::collections::HashSet;

fn normalize(s: &str) -> String {
    unidecode(s).to_lowercase()
}

fn shingles(s: String) -> HashSet<String> {
    let mut xs = HashSet::new();
    let it = s.chars();
    let mut pk = it.peekable();

    loop {
        let cur = pk.next();
        let nxt = pk.peek();
        match (cur, nxt) {
            (Some(i), Some(nxt_i)) => {
                xs.insert(format!("{}{}", i, nxt_i));
            }
            _ => { break }
        }
    }
    return xs
}

// Intersection of the sets divided by the size of the union of the
// sets.
fn jaccard_distance(s1: String, s2: String) -> f64 {
    let s1_shingles = shingles(s1);
    let s2_shingles = shingles(s2);
    s1_shingles.len() as f64;
    let inter: HashSet<_> = s1_shingles.intersection(&s2_shingles).collect();
    let union: HashSet<_> = s1_shingles.union(&s2_shingles).collect();
    (inter.len() as f64) / (union.len() as f64)
}

fn comp_and_print(s1: &str, s2: &str) {
    let normal_s1 = normalize(s1);
    let normal_s2 = normalize(s2);
    println!("'{}' vs '{}' ... \t {}", s1, s2,
             jaccard_distance(normal_s1, normal_s2));
}

fn main() {
    comp_and_print("Apple sauce", "Apple Trees");
    comp_and_print("Apple pie", "Grandma's Apple pie");
    comp_and_print("Pear", "Peach");
}

The shingles method seems particularly bad. I'd like it to be more flexible (for 3 or 4 character n-grams, for instance) and the use of format! rather than join on a collection like I'd do in Ruby is ugly.

edit

Here's Ruby code for shingles since I can express what I mean easier in Ruby. My Rust implementation only handles n = 2 because I had trouble figuring out how to make it dynamic.

def shingles(str, n = 2)
  (0..(str.length - n)).map { |idx| str[idx..idx+(n-1)] }.to_set
end

Let's start by removing unused code, the whole normalize function and the line s1_shingles.len() as f64.

Then, in order to actually be able to call your functions easily, we change all String arguments into &str. You usually want to accept &str as more types can provide it. In this case, string literals are the big example.

Let's do shingles in a big bang!

fn shingles(s: &str) -> HashSet<String> {
    let chars: Vec<_> = s.chars().collect();
    chars.windows(2).map(|w| w.iter().cloned().collect()).collect()
}

We start by getting all the characters once into a vector. Now that we have a slice (through the Vec), we can use windows, which gives overlapping views into the slice. We then convert each window into an iterator of char using cloned and then into a string using collect. We convert  the overall iterator into a HashSet, also using collect. Look at that type inference go!

Note that now we have a parameter to windows that allows you to extend to arbitrary-sized n-grams!

The other optimization is to avoid collecting into a collection if we just want the count of elements:

let inter = s1_shingles.intersection(&s2_shingles).count();
let union = s1_shingles.union(&s2_shingles).count();

All together, it now looks like:

use std::collections::HashSet;

fn shingles(s: &str) -> HashSet<String> {
    let chars: Vec<_> = s.chars().collect();
    chars.windows(2).map(|w| w.iter().cloned().collect()).collect()
}

fn jaccard_distance(s1: &str, s2: &str) -> f64 {
    let s1_shingles = shingles(s1);
    let s2_shingles = shingles(s2);
    let inter = s1_shingles.intersection(&s2_shingles).count();
    let union = s1_shingles.union(&s2_shingles).count();
    (inter as f64) / (union as f64)
}

fn main() {
    println!("{}", jaccard_distance("Pear", "Peach"));
}

Edit

And of course now I see that if I were to scroll down a bit, I'd see that you do use normalize. Oops! You should still accept a &str, the only change you need to make is to take a reference to the Strings:

println!("'{}' vs '{}' ... \t {}", s1, s2,
         jaccard_distance(&normal_s1, &normal_s2));


Copying List Elements To Another List And Changing Them I'm a beginner python programmer..I recently started reading a python book called: "Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming"...And I found an interesting challenge...The  challenge is to copy the elements of the list to another list and change the elements to make them become lowercase...I solved the challenge by myself and wrote two versions for it...

Here is my first attempt:

#Method1: Using the slice method
currentUsers = ['MrSplash', 'MrRobot', 'Peter1234', 'Steve', 'James']
newUsers = ['Davidpeterson', 'Elliot345', 'STEVE', 'mrRobOt', 'NickB3ns0N']

currentUsersLower = currentUsers[:]
for i in range(0, 5):
    currentUsersLower[i] = currentUsersLower[i].lower()

And here is my second attempt:

#Method2: Using a normal for loop
currentUsers = ['MrSplash', 'MrRobot', 'Peter1234', 'Steve', 'James']
newUsers = ['Davidpeterson', 'Elliot345', 'STEVE', 'mrRobOt', 'NickB3ns0N']

currentUsersLower = []
for user in currentUsers:
    currentUsersLower.append(user.lower())

My first question is this: Imagine we are working with around a million elements here. Which of these methods is efficient?

I define efficient as:

Easy to maintain and read
Performance-wise(Because let's face it, a million elements requires a lot of performance here)

My second question is this: Are there better ways to do this task than the ones I presented here?
If so please share them as an answer here

Note: The code works on both Python 3 and 2.

Your second version is better than your first version, for two reasons:

All else being equal, building a new list is IMO easier to reason about than changing a list you're iterating over.
You're iterating using for...in rather than iterating by index using a range (and the range in your first example is hardcoded rather than being over the len of the list, which is doubly bad).

The most efficient/pythonic way is to use the comprehension syntax:

currentUsersLower = [user.lower() for user in currentUsers]

(Also, standard Python style would be to name these current_users and current_users_lower!)


Controller for classrooms and students The actions I'm wondering about are members, add_student, and remove_student.  They work just fine, but they aren't RESTful and it feels like they should be handled (perhaps) by the StudentsController.

Note that members and show are the same. The only difference is that the members.html.erb view iterates over and displays @classroom.students while show.html.erb displays info about the classroom itself.

class ClassroomsController < ApplicationController

  # GET /classrooms
  def index
    @classrooms = Classroom.all.order('name ASC')
  end

  # GET /classrooms/1
  def show
    @classroom = Classroom.find(params[:id])

    authorize @classroom
  end

  # GET /classrooms/1/members
  def members
    @classroom = Classroom.find(params[:id])

    authorize @classroom
  end

  # GET /classrooms/new
  def new
    @classroom = Classroom.new
    @classroom.build_avatar

    authorize @classroom
  end

  # GET /classrooms/1/edit
  def edit
    @classroom = Classroom.find(params[:id])
    @classroom.build_avatar unless @classroom.avatar

    authorize @classroom
  end

  # POST /classrooms
  def create
    @classroom = Classroom.new(classroom_params)

    authorize @classroom

    if @classroom.save
      redirect_to classrooms_path, notice: 'Classroom was successfully created.'
    else
      render action: "new"
    end
  end

  # PUT /classrooms/1
  def update
    @classroom = Classroom.find(params[:id])

    authorize @classroom

    if @classroom.update(classroom_params)
      redirect_to edit_classroom_path(@classroom), notice: 'Classroom was successfully updated.'
    else
      render action: "edit"
    end
  end

  def add_student
    @classroom = Classroom.find(params[:id])
    @student = Student.find(params[:classroom][:student_ids][0])

    authorize @classroom

    @classroom.students << @student

    respond_to do |format|
      @count = @classroom.students.count
      format.js
    end
  end

  def remove_student
    @classroom = Classroom.find(params[:id])
    @student = Student.find(params[:classroom][:student_ids][0])

    authorize @classroom

    @classroom.students.delete(@student)

    respond_to do |format|
      @count = @classroom.students.count
      format.js
    end
  end

  # DELETE /classrooms/1
  def destroy
    @classroom = Classroom.find(params[:id])

    authorize @classroom

    @classroom.destroy

    redirect_to classrooms_path
  end

  private

  def classroom_params
    params.require(:classroom).permit(:name, :description, avatar_attributes: [:id, :avatar, :_destroy],
      :student_ids => [])
  end

end

Here are the models:

# student.rb
class Student < ActiveRecord::Base
  has_many :classroom_memberships, :dependent => :destroy, :inverse_of => :student
  has_many :classrooms, :through => :classroom_memberships
end

# classroom.rb
class Classroom < ActiveRecord::Base
  has_many :classroom_memberships, :inverse_of => :classroom, :dependent => :destroy
  has_many :munchkins, :through => :classroom_memberships
end

The actions I'm wondering about are members, add_student, and remove_student. They work just fine, but they aren't RESTful [...]

True, they're not part the usual CRUD actions that can be expressed with HTTP verbs. But then neither are the default new or edit actions, so it's not like going beyond CRUD is automatically illegal.

Even so, associations do always present a bit of challenge. Are you adding the student to the class, or the class to the student?

However, in this case it appears that you already have a join model: ClassroomMembership. So going back to the CRUD actions, consider what you're creating when you call add_student: A membership.

So what you might want to do is simply make a ClassroomMembershipsController, since that's the resource you're manipulating. Said controller can then have the usual RESTful CRUD actions, keeping StudentsController or ClassroomController pretty clean.

This is just an idea, though. Again, if it's simpler to keep your actions on the ClassroomController then do that. But conceptually what you're doing is manipulating the association model.


Mockingbird c++ mocking framework I have developed Mockingbird a mocking framework for c++, it depends on function injection, here is the code from the repository:
/*
 *  Mockingbird is a simple and a powerful mocking framework for c++.
 *  Copyright (c) Mouaz Chamieh April 2021.
 *  Distributed under the MIT License. Please refer to the LICENSE file at:
 *  https://github.com/muazsh/Mockingbird
 */

#pragma once
#ifndef MOCKINGBIRD
#define MOCKINGBIRD

#define START_MOCK(MockingClass, MockedClass)\
class MockingClass : public MockedClass {\
 public:

#define INJECTION_SET(FuncName, Substitute)\
private:\
template<class T>\
class FuncName##Class {\
public:\
FuncName##Class():m_func((T)Substitute){}\
T m_func;\
};\
FuncName##Class<decltype(Substitute)> m_##FuncName##Class;\
mutable int m_##FuncName##CallCounter = 0;\
public:\
void Inject##FuncName(decltype(Substitute) sub){\
m_##FuncName##Class.m_func = sub;}\
int Get##FuncName##CallCounter(){return m_##FuncName##CallCounter;}

#define FUNCTION(FuncName,ReturnType, Signature, Substitute, .../*signature variables*/)\
INJECTION_SET(FuncName, Substitute)\
ReturnType FuncName Signature override\
{ m_##FuncName##CallCounter++;\
return m_##FuncName##Class.m_func(__VA_ARGS__);}

#define CONST_FUNCTION(FuncName,ReturnType, Signature, Substitute, .../*signature variables*/)\
INJECTION_SET(FuncName, Substitute)\
ReturnType FuncName Signature const override\
{ m_##FuncName##CallCounter++;\
return m_##FuncName##Class.m_func(__VA_ARGS__);}

#define OVERLOAD_INJECTION_SET(FuncName, Substitute, overloadedMethodNumber)\
private:\
FuncName##Class<decltype(Substitute)> m_##FuncName##Class##overloadedMethodNumber;\
mutable int m_##FuncName##overloadedMethodNumber##CallCounter = 0;\
public:\
void Inject##FuncName(decltype(Substitute) sub){\
m_##FuncName##Class##overloadedMethodNumber.m_func = sub;}\
int Get##FuncName##overloadedMethodNumber##CallCounter(){return m_##FuncName##CallCounter;}

#define FUNCTION_OVERLOADING(FuncName,ReturnType, Signature, Substitute, overloadedMethodNumber, .../*signature variables*/)\
OVERLOAD_INJECTION_SET(FuncName, Substitute, overloadedMethodNumber)\
ReturnType FuncName Signature override\
{ m_##FuncName##overloadedMethodNumber##CallCounter++;\
return m_##FuncName##Class##overloadedMethodNumber.m_func(__VA_ARGS__);}

#define CONST_FUNCTION_OVERLOADING(FuncName,ReturnType, Signature, Substitute, overloadedMethodNumber, .../*signature variables*/)\
OVERLOAD_INJECTION_SET(FuncName, Substitute, overloadedMethodNumber)\
ReturnType FuncName Signature const override\
{ m_##FuncName##overloadedMethodNumber##CallCounter++;\
return m_##FuncName##Class##overloadedMethodNumber.m_func(__VA_ARGS__);}

#define END_MOCK(MockingClass)\
};

#endif // !MOCKINGBIRD

and here is an example on how it works:
Assume you have the following class Foo and you need to mock:
struct MyStruct{
    int x, y;
};

class Foo{
public:
    virtual const MyStruct CreateMyStruct(int x, int y) { return MyStruct{ x,y }; }
};

Then you write a testing fixture only once for the whole project:
const MyStruct CreateMyStructDummy(int x, int y) { return MyStruct{0,0}; }

START_MOCK(FooMock, Foo)
FUNCTION(CreateMyStruct, const MyStruct, (int x, int y), &CreateMyStructDummy, x, y)
END_MOCK(FooMock)

Then in the tests you write the desired substitute for example:
const MyStruct CreateMyStructSubstitute(int x, int y) { return MyStruct{ x + 10, y + 10 }; }

and inject it like:
FooMock fooMock;
fooMock.InjectCreateMyStruct(CreateMyStructSubstitute); // Mocking function injection.
auto created = fooMock.CreateMyStruct(5,5);
EXPECT_EQ(created.x, 15);
EXPECT_EQ(created.y, 15);

The code is in the file Mockingbird.hpp and totally depends on macros, it is short and straightforward, I ask for reviewing the code on github here, thanks.

You make the code really hard to read:
#define INJECTION_SET(FuncName, Substitute)\
private:\
template<class T>\
class FuncName##Class {\
public:\
FuncName##Class():m_func((T)Substitute){}\
T m_func;\
};\
FuncName##Class<decltype(Substitute)> m_##FuncName##Class;\
mutable int m_##FuncName##CallCounter = 0;\
public:\
void Inject##FuncName(decltype(Substitute) sub){\
m_##FuncName##Class.m_func = sub;}\
int Get##FuncName##CallCounter(){return m_##FuncName##CallCounter;}

just because it is a  macro does not mean it needs to be unreadable. You can still format it nicely for the maintainer.
#define INJECTION_SET(FuncName, Substitute)                         \
                                                                    \
    private:                                                        \
    template<class T>                                               \
    class FuncName##Class                                           \
    {                                                               \
        public:                                                     \
            FuncName##Class():m_func((T)Substitute){}               \
            T m_func;                                               \
    };                                                              \
                                                                    \
    FuncName##Class<decltype(Substitute)> m_##FuncName##Class;      \
                                                                    \
    mutable int m_##FuncName##CallCounter = 0;                      \
    public:                                                         \
    void Inject##FuncName(decltype(Substitute) sub)                 \
    {                                                               \
        m_##FuncName##Class.m_func = sub;                           \
    }                                                               \
    int Get##FuncName##CallCounter()                                \
    {                                                               \
        return m_##FuncName##CallCounter;                           \
    }


Forcing SSL and WWW using .htaccess I would like to know if this code in .htaccess for forcing SSL and WWW in URL is correct, because with another code I usually get redirect loop, e.g. RewriteCond %{HTTPS} !=on and now it works like a charm (suspiciously). Also, is it possible to write it better/simpler?

# Force to SSL
RewriteCond %{HTTP:HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Force to WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

What you're doing with RewriteCond %{HTTP:HTTPS} !1 is checking whether the HTTPS header is present in the request and if it is, then the value is not 1.

  %{HTTP:header}, where header can be any HTTP MIME-header name,
  can always be used to obtain the value of a header sent in the HTTP
  request. Example: %{HTTP:Proxy-Connection} is the value of the HTTP
  header Proxy-Connection:.

Though, I think it'd be easier to use %{HTTPS} instead.

  %{HTTPS}

  Will contain the text "on" if the connection is using SSL/TLS, or
  "off" otherwise. (This variable can be safely used regardless of
  whether or not mod_ssl is loaded).

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]

Also, you are not using QSA flag. This might cause in undesired behaviour.


CodeAbbey #21 Array Counters I'm trying to learn some Haskell by working my way through the CodeAbbey problems. I really struggled to find a nice functional (but still efficient) way to solve problem #21: Array Counters.

The problem is, given a list of M numbers between 1 and N (an input parameter), to count the number of occurrences of each number. The question strongly suggests the imperative approach of using an array of counters and incrementing the counter at the relevant index.

Example:

Input:
10 3
3 2 1 2 3 1 1 1 1 3

Expected output:
5 2 3

My code:

count :: Int -> [Int] -> [Int]
count n = go (replicate n 0)
  where
    go cs []       = cs
    go cs (x : xs) = go (incList x cs) xs

incList :: Int -> [Int] -> [Int]
incList x cs = (take (x - 1) cs) ++ [cs!!(x - 1) + 1] ++ (drop x cs)

main :: IO ()
main = do line <- getLine
          let [m, n] = map (read :: String -> Int) $ words line
          line <- getLine
          let list = map (read :: String -> Int) $ words line
          putStrLn $ unwords $ map show $ count n list

You could use a Map, from the Data.Map module to count occurrences of the numbers:

import qualified Data.Map.Strict as M

count :: [Int] -> M.Map Int Int
count xs = M.fromListWith (+) $ zip xs (repeat 1)

This way you only iterate once through the list of pairs (number, 1) and sum the 1s of each different number.

Since we don't have mutation in Haskell, you can often use a Map as a replacement for a dictionary in imperative languages (or arrays).


Multiple Interfaces modifying contract I have a problem with my interface-design. I especially ask for help with the overloaded compute-Method.

For a calculator I work with these Interfaces:

public interface ICalculator{
    public double compute(String expression) throws IllegalArgumentException;
}

public interface IParameterizedCalculator extends ICalculator {
    public double setParameter(String parameter, double value) throws IllegalArgumentException;

    public double getParameter(String parameter) throws IllegalArgumentException;

    // IllegalArgumentException when expression is null or malformed
    // IllegalStateException when a parameter in the expression was not set prior to this method
    double compute(String expression) throws IllegalArgumentException, IllegalStateException;
}

public interface IPersistentCalculator extends ICalculator {
    // IllegalStateException when no expression is stored
    double compute() throws IllegalStateException;

    void store(String expression) throws IllegalArgumentException;
}

IParameterizedCalculator adds an Exception to compute. As the previously stateless ICalculator suddenly became stateful I see no way than to add this exception.
IPersistantCalculator doesn't modify the original compute(String), but overloads it with compute(), which also has a state and therefore throws an IllegalStateException (but not necessarily an IllegalArgumentException, as the only way to store an expression is through the store(String)-Method and I don't care if someone hacks my code via reflection or debugger). However this IllegalStateException has a different reason (another "state" if you want).

I have three questions now:

How do I properly extend the throws-clause to indicate that my derived methods will throw more than their parent?
How do I communicate this in a class MyCalculator implements IPersistentCalculator, IParameterizedCalculator (which only exposes a single compute(String)-method)?
How do I communicate that the MyCalculator.compute()-Method may throw IllegalStateException when a parameterized expression has been stored?

I think if you continue this path you violate the "interface seggregation principle". Try to separate the interfaces. Going with your semantics I expect interfaces like ParameterAware or Storable. Then you won't get into such a semantical trouble.

As you have less semantical problems with your interfaces  you will face other challenges that have to do with the implementing classes and your algorithms using and working with these seggregated interfaces. But you cannot escape it and the effort and it is justified.


Iterate attributes of property, typecast and store info if found Im constructing metadata about a class. With reflection i iterate over the class properties, and those properties have attributes (name, validation etc). When iterating those attributes, i typecast to some specific attribute-types, and if successful store the relevant attribute information. Im looking to do this in a more elegant way since there is much code repetition at this point. In part the typecast checks (attibute as Attributetype), and also the check if Validate is null, and then new-ing it up is repetitive (Validate needs to be null if there are no validation attributes). I expect the number of attribute-types to grow

Ideal would be to define what attribute-types im looking for, and if found execute relevant code.

foreach (Attribute attribute in property.GetCustomAttributes(true))
{

    //Info attribute
    DisplayAttribute display = attribute as DisplayAttribute;
    if (display != null)
    {
        Name =display.Name;
        Group = display.GroupName;
        Description = display.Description;
    }

    //Validation attributes
    MinLengthAttribute min = attribute as MinLengthAttribute;
    if (min != null)
    {
        if (Validate == null) Validate = new MetaValidate();
        Validate.MinLength = min.Length;
    }

    MaxLengthAttribute max = attribute as MaxLengthAttribute;
    if (max != null)
    {
        if (Validate == null) Validate = new MetaValidate();
        Validate.MaxLength = max.Length;
    }

    RegularExpressionAttribute regex = attribute as RegularExpressionAttribute;
    if (regex != null)
    {
        if (Validate == null) Validate = new MetaValidate();
        Validate.Regex = regex.Pattern;
        Validate.RegexError = regex.ErrorMessage;
    }
}

Seems like ive found a great solution, my entire loop can be rewritten as this:

InvokeOnAttr<DisplayAttribute>(property, a => {
    Name = a.Name;
    Group = a.GroupName;
    Description = a.Description;
});

InvokeOnAttr<MinLengthAttribute>(property, a => {
    Validate.MinLength = a.Length;
});

InvokeOnAttr<MaxLengthAttribute>(property, a => {
    Validate.MaxLength = a.Length;
});

InvokeOnAttr<RegularExpressionAttribute>(property, a => {
    Validate.Regex = a.Pattern;
    Validate.RegexError = a.ErrorMessage;
});

If this method also is availible in the class (it takes a property, finds the attribute (of type TAttr provided), and if found executes the passed in action on it:

public void InvokeOnAttr<TAttr>(PropertyInfo p, Action<TAttr> action) where TAttr : Attribute {
    TAttr attr = p.GetCustomAttribute<TAttr>();
    if (attr is ValidationAttribute)
    {
        if (Validate == null) Validate = new MetaValidate();
    }
    if (attr != null) {
        action.Invoke(attr);
    }
}


Rewrite 2 repeating With-blocks Got the following piece of code:

          If boolFlag Then
              With offer.Person1
                entity.Birthdate = .BirthDate

                entity.FirstName = .FirstGivenName
                entity.LastName = .FamilyName

                entity.Street = .StreetName
                entity.HousNr = .HouseNumberIdentifier
                entity.BoxNr = .BoxNumberIdentifier
                entity.PostalCode = .PostalCode
                entity.Municipality = .CityName
            End With
        Else
            With offer.Person2
                entity.Birthdate = .BirthDate

                entity.FirstName = .FirstGivenName
                entity.LastName = .FamilyName

                entity.Street = .StreetName
                entity.HousNr = .HouseNumberIdentifier
                entity.BoxNr = .BoxNumberIdentifier
                entity.PostalCode = .PostalCode
                entity.Municipality = .CityName
            End With
        End If

Objects Person1 and Person2 are of different types, and those types do not share a common Interface / class. (And that can't be changed)

How do I avoid the duplicate code inside the With-blocks?

Unless you can make them share a common interface, it is not desirable to eliminate the code duplication -- best case (vb.net late binding on Object), trades your code size for the compilers.  Doing the work yourself, simply means more code for you.

That said, where the code duplication is, is something you might want to control.  Extract each assignment out into a similarly named method, and call the appropriate one.

Sub UpdateEntityFromPerson(ByVal entity as Entity, ByVal person as Person1)
    With person
        entity.Birthdate = .BirthDate

        entity.FirstName = .FirstGivenName
        entity.LastName = .FamilyName

        entity.Street = .StreetName
        entity.HousNr = .HouseNumberIdentifier
        entity.BoxNr = .BoxNumberIdentifier
        entity.PostalCode = .PostalCode
        entity.Municipality = .CityName
    End With
End Sub

Sub UpdateEntityFromPerson(ByVal entity as Entity, ByVal person as Person2)
    With Person
        entity.Birthdate = .BirthDate

        entity.FirstName = .FirstGivenName
        entity.LastName = .FamilyName

        entity.Street = .StreetName
        entity.HousNr = .HouseNumberIdentifier
        entity.BoxNr = .BoxNumberIdentifier
        entity.PostalCode = .PostalCode
        entity.Municipality = .CityName
    End With
End Sub

        If boolFlag Then
            UpdateEntityFromPerson(entity, offer.person1)
        Else
            UpdateEntityFromPerson(entity, offer.person2)
        End If

All of the code is still duplicated, but it type safe, clear, and easy to use and understand.  Of course, the best thing to do is probably to have an entity constructor that takes one or the other, or a builder that does so, but that depends upon other factors, outside your snippet...


Merging two files into one .CSV I'm relatively new to Python scripting. I made a script that takes two raw data files and merges them into one CSV file, but it takes a long time to complete.  Are there any logic problems in this code?

input_fileVms = open( 'vms.csv', 'rb')

if site == 'eg':
    output_merge = open('mergedFileEG.csv', 'wb')
elif site== 'fm':
    output_merge = open('mergedFileFM.csv', 'wb')

dataVms = csv.reader(input_fileVms)
writerMerge = csv.writer(output_merge,quoting=csv.QUOTE_ALL)

for lineVM in dataVms:
    input_fileUsers = open( 'users.csv', 'rb')
    dataUsers = csv.reader(input_fileUsers)
    new_line = strip(lineVM)
    OSFile = open('OS.csv', 'rb')
    OS = csv.reader(OSFile)
    i = 0
    for user in dataUsers:
        if i < 4:
            i = i + 1
        else:
            user = strip(user)
            if len(user) >= 4:

                idLine = str(user[1])

                if idLine in new_line:

                    for OS in OS:
                        system = OS[0]
                        if system in new_line:

                            if len(user) < 5:
                                writerMerge.writerow((str(new_line[2] + ',' + user[2] + "," + "Missing"+ ',' +OS[1])).split(','))
                            else:
                                writerMerge.writerow((str(new_line[2] + ',' + user[2] + "," +user[4]+ ',' +OS[1])).split(','))

            input_fileUsers.close()

You should never do this:

for OS in OS:

Your loop variable "OS" is the same name as the iterator you're looping over.  That is very likely to cause you problems.  In some simple cases it will work, but makes for very unreadable code.

Secondly, for every line in the first file you are reading the ENTIRE second file!  So you read the second file as many times as there are lines in the first.  You need to separate the loops, read the first file (I like using input_fileVms.readlines() to put everything in a list of strings) and then read the second file.  Once you have the contents of the two files in data structures, then you can merge them.  Unless your files are in the 1GB range, this is what I would recommend.  If you are just concatenating the files, you don't need csv.reader() because there is no reason to split out the strings and get the individual fields.