Efficiency of a linq query on a nested structure

It runs 4 queries for every question in the survey because of the line that calculates the percentage.

You're trying to retrieve data and make calculations at the same time. The issue stems from trying to do these two things. This is basically violating SRP.

Edit According to a coworker, this is not strictly an SRP violation, but it's very close to the spirit of what makes SRP important. I sort of see his point, but an in-depth discussion is a bit offtopic here.

Instead, try to only retrieve data at this point. There are other ways to do the calculation at a later stage:

Use an automated property
Perform the calculation on the in-memory objects

1. Only retrieve the needed information and store it in an appropriate spot.

We have to make sure that we retrieve all needed information that allows us to do the calculation at a later stage. Your percentage calculation relies on two pieces of information:

The result count of the current answer.
The combined result count of all answers that belong to the (parent) question.

(1) is already being retrieved (it's stored in SelectedCount).

(2) is not explicitly retrieved, but can already be derived by looking at the Answers property of a QuestionResultViewModel. In short, it can be found by calling myQuestionModel.Answers.Sum(x => x.SelectedCount)

So the first step is easy, simply omit the calculation:

// ...

Answers = from a in q.Answers
    select new AnswerResultViewModel
    {
        Text = a.Text,
        SelectedCount = a.Results.Count
    }

// ...

2. Option 1 - using an automated property.

I find this the cleanest solution, it forgoes the need to manually trigger a calculation. There are a few prerequisites here:

An AnswerResultViewModel needs to have a reference to its parent QuestionResultViewModel in order to access the total count. For the example, I will assume such a property exists by the name of Question.
If the calculation is expensive, you should reconsider using an automated property because it will be recalculated every time it is accessed. However, I don't consider your current example to be an expensive calculation.

The automated property, using an expression-bodied member:

public int SelectedPercentage =>
               (double)this.SelectedCount / this.Question.Answers.Sum(an => an.SelectedCount);

If you prefer a more traditional syntax:

public int SelectedPercentage
{
    get
    {
        return (double)this.SelectedCount / this.Question.Answers.Sum(an => an.SelectedCount);
    }
}

Minor changes:

Instead of .Select(lambda).Sum(), I simply used .Sum(lambda). It's slightly shorter and easier to read.
Instead of an.Results.Count, I used an.SelectedCount since it already contains the correct value.

3. Option 2 - Manual calculation.

I like this option less, but it's better in cases where the calculation is expensive and you want to avoid calculating the same value over and over.

After you've retrieved the value from the database, calculate the percentages.

If the Question navigational property exists:

foreach(var answer in srvm.Questions.SelectMany(q => q.Answers))
{
    answer.SelectedPercentage = (double)answer.SelectedCount / answer.Question.Answers.Sum(an => an.SelectedCount);
}

If there is no navigational property:

foreach(var question in srvm.Questions)
foreach(var answer in question.Answers)
{
    answer.SelectedPercentage = (double)answer.SelectedCount / question.Answers.Sum(an => an.SelectedCount);
}


Making an email text-parser generic using PredicateBuilder

First of all, you have one too many parentheses and are missing some variable declarations, so let's fix that up.

var emailText = "";
foreach (List<string> list in groups)
{
    IEnumerable<string> lines3 = list.Where(line => !line.StartsWith("Cc:", StringComparison.OrdinalIgnoreCase));
    lines3 = lines3.Where(line => !line.StartsWith("To:", StringComparison.OrdinalIgnoreCase));

    var result = lines3.Select((r, i) => new { line = r, index = i })
                    .Where(r =>
                        r.line.StartsWith("Regards", StringComparison.OrdinalIgnoreCase)
                        || r.line.StartsWith("Thanks", StringComparison.OrdinalIgnoreCase)
                        || r.line.ToLower().Contains("please do not forward")
                        )
                        .Select(r => new { LineNo = r.index }).OrderBy(r => r.LineNo);

    foreach (var item in result)
    {
        var lineNumber = item.LineNo;
        if (lineNumber > 0)
            emailText += String.Join(System.Environment.NewLine, lines3.Take(lineNumber - 1));
        else
            emailText += String.Join(System.Environment.NewLine, lines3);
        break;
    }
}

Now let's run this on

var groups = new List<List<string>>
{
    new List<string> { "Hello", "To: me", "Message body", "Regards", "me" }
};

From your problem description, I would expect the output to be Hello\nMessage body, but it's just Hello. So I'm going to assume the problem description is correct and the code is wrong.

Here are some other notes:

lines3 is not a good variable name.
If you find yourself concatenating a lot of strings, see if you can use a StringBuilder instead.
OrderBy line number is redundant, as the lines are already ordered by line number.

Let's start with a skeleton of what we want the code to look like:

var result = new StringBuilder();
foreach (List<string> email in emails)
{
    // TODO: Remove "to" and "cc" lines.
    // TODO: Remove everything after first "thanks", "regards", etc.
    result.Append(string.Join(Environment.NewLine, ???));
}

Let's handle the easy part first.

private static bool IsDestination(string line)
{
    return line.StartsWith("Cc:", StringComparison.OrdinalIgnoreCase) ||
           line.StartsWith("To:", StringComparison.OrdinalIgnoreCase);
}

var result = new StringBuilder();
foreach (List<string> email in emails)
{
    var filteredEmail = email.Where(line => !IsDestination(line));
    // TODO: Remove everything after first "thanks", "regards", etc.
    result.Append(string.Join(Environment.NewLine, filteredEmail));
}

Now you want a variable number of phrases to look for, and it seems that some will need to be at the start of the line, while others can be anywhere.

We can write the tests like this

var valedictionTests = new List<Predicate<string>>
{
    line => line.StartsWith("Regards", StringComparison.OrdinalIgnoreCase),
    line => line.StartsWith("Thanks", StringComparison.OrdinalIgnoreCase),
    line => line.IndexOf("Please do not forward", StringComparison.OrdinalIgnoreCase) != -1
};

Then you can add tests like this

valedictionTests.Add(line => line.StartsWith("Best,", StringComparison.OrdinalIgnoreCase));

And pass them in as a parameter

var result = new StringBuilder();
foreach (List<string> email in emails)
{
    var filteredEmail = email.Where(line => !IsDestination(line))
        .TakeWhile(line => !valedictionTests.Any(test => test(line)));
    result.Append(string.Join(Environment.NewLine, filteredEmail));
}


"From this, I see that JS joins the scope of a function with a function (recursively) called in itself."

No, it doesn't.  You created a global variable called "r", not a method variable, and so there is only one "r", and it is accessible to all your JavaScript code.

Thou shalt always use var to declare a variable.


Interface design

The current design of the validator class is not very intuitive.
Users of this class must call the check_for_symbol and check_length methods to validate an email address.
They would only know this by reading the implementation.
You could add documentation,
but it would still not be easy to use.
The best is when the usage and behavior is obvious.

I would imagine the design of an email validator something like this:

class EmailValidator:
    def validate(self, email):
        # ...

Where the validate method would raise exceptions depending on what failed (such as length, symbols).

With such outline, even without documentation,
when a user does help(EmailValidator) and sees a single validate method,
the usage is obvious, clear.

What's worse,
the check_email function is not part of the validator class.
As such, the validator doesn't encapsulate the validation logic.
That might be acceptable if the public API is the check_email and the class is the implementation detail.

Coding style

Use boolean expressions directly. For example, instead of this:

def check_for_symbol(self):
    if re.search("[@.]", self.email) is None:
        return False
    else:
        return True

You could write as:

def check_for_symbol(self):
    return re.search("[@.]", self.email) is not None

Also, instead of x is True, use simply x, and instead of x is False, use not x.

See more on this in the style guide.

Compatibility with Python 3

When using Python 2,
to future-proof yourself it's good to adopt a writing style compatible with Python 3. For example, instead of print 'something', write print('something').


Greasemonkey script to auto fill the values of a form on a specified page

While your code is generally easy to read and understand, I believe you have slightly over complicated things, there's no need to define an object with several methods to autocomplete a form.

Before I get to that though, let's take a look at your code.

Good work with encapsulating all of your code in an IIFE and forcing strict mode.
formDefaults is a decent name, I was able to guess almost exactly what it would contain when I first read it. However, since it also contains the page url, I would rename this to pages. urlPattern is also decent, though I expected it to be a regex due to the pattern suffix. valueSet on the other hand gives me absolutely no information about what it contains. I'd recommend renaming this to something like defaults.
The run method accepts a formDefaults parameter, but doesn't use it. It calls tryEachPattern and tryEachPattern uses the variable declared outside formFiller. If you take parameters, you should use them.
Using arrow functions doesn't actually help you here. Every single arrow function in this code takes a _this parameter. Arrow functions are supposed to make working with this easier, not more cumbersome! Just use regular functions if you need to pass a _this argument.
tryEachPattern is more complex than it needs to be. There is an Array.find method which can find a match in an array for you and returns undefined if the value was not found.
Don't explicitly compare boolean values with ===, in tryEachPattern you can just do if (matched) which is easier to read.
Don't assign a result to a variable just to return that variable. var matched = ...; return matched can just be written as return ....
Don't use String.match to test if a string contains another string. Use String.includes.
Prefer const and let to var. const is especially helpful as it tells readers that the assigned value will not be changed (though it could be mutated).
In fillForm, there's no need to write a bounded for loop. Prefer for...of pattern or the Array.forEach call to make it easier to immediately know you are looping over everything.
I don't see any reason that the click handler should call event.preventDefault(), the button won't cause any navigation so it shouldn't be necessary unless the page does something odd.
If the page is already including jQuery, it makes sense to use it here as it simplifies the creation of the auto fill button. Otherwise, it only takes like 4 more lines of code and I would recommend dropping the jQuery dependency to speed up your script.
jQuery provides a .click method as a shorter alternative to .on('click', ...
Destructuring is an amazing tool that can make code easier to read by giving immediate access to desired properties on an object.

At the start I mentioned that this was more complicated than it needed to be. Here is how I would write an equivalent script.

/*jshint esversion: 6 */
(function ($) {
    'use strict';

    const pages = [
        {
            url: '/profile/user',
            defaults: [
                { selector: '#username', value: 'bill' },
                { selector: '#lastname', value: 'miller' },
                { selector: '#auth_pin', value: '1234' },
            ]
        },
        {
            url: '/profile/company',
            defaults: [
                { selector: '#companyname', value: 'bill' },
                { selector: '#state', value: 'washington' },
            ]
        },
    ];

    const page = pages.find(({ url }) => location.href.includes(url));

    if (page) {
        const button = $('<button type="button" style="position: absolute; right: 10px; bottom: 10px">Auto Fill</button>');
        button.click(() => {
            for (const { selector, value } of page.defaults) {
                $(selector).val(value);
            }
        });
        $('body').append(button);
    }

})(jQuery);


Python loop to write all possible strings that can be formed from some letters

1. Speeding up the code

Here's the initial measurement:

$ python ./cr163578.py
('a', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('a', 'a', 'a', 'a', 'a', 'a', 'a', 'b')
('a', 'a', 'a', 'a', 'a', 'a', 'a', 'c')
... much output deleted ...
('h', 'h', 'h', 'h', 'h', 'h', 'h', 'h')
$ tail -1 txt_abcdefgh.txt
This took 122.10 seconds

Let's make some improvements:

Omit the print statement, which generates lots of useless output that is just slowing us down.
Use the with statement to open and close the file.
Omit the time measurement — we can use the time program to do the measurement.

Here's version 2:

import itertools
with open('txt_abcdefgh.txt', 'w') as f:
    for i in itertools.product('abcdefgh', repeat=8):
        f.write(i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + '\n')

This is about 8 times as fast as the original:

$ /usr/bin/time python cr163578.py
       16.73 real        16.45 user         0.23 sys

Now let's use the join method to build the output instead of lots of + operations. Here's version 3:

import itertools
with open('txt_abcdefgh.txt', 'w') as f:
    for i in itertools.product('abcdefgh', repeat=8):
        f.write(''.join(i) + '\n')

This is about 16 times as fast as the original:

$ /usr/bin/time python cr163578.py
        8.25 real         8.01 user         0.21 sys

And finally, let's avoid the need to append the newline to each line of output by using the writelines method of the file. Here's version 4:

import itertools
with open('txt_abcdefgh.txt', 'w') as f:
    f.writelines(map(''.join, itertools.product('abcdefgh', repeat=8)))

This is about 20 times as fast as the original:

$ /usr/bin/time python cr163578.py
        5.87 real         5.73 user         0.12 sys

2. Runtime complexity

If there are \$k\$ letters in your alphabet, and \$n\$ characters in the strings you are constructing, then there are \$k^n\$ strings altogther. Here \$k=8\$ and \$n=8\$ so there are \$8^8 = 16,777,216\$ possibilities, which take \$9 \times 8^8 = 150,994,944\$ bytes to store on disk.

Your aim is to have \$k=26\$ and \$n=26\$. But then there will be $$26^{26} = 6,156,119,580,207,157,310,796,674,288,400,203,776$$ possibilities, which can't possibly be generated or stored.

Whatever you are trying to do, you will have to find some other way to do it.


Put it in a separate function, and call it whenever you want to change it.
I'm not a fan of using an application-scope variable to store the currently selected frame. You could use something like listItems.index(listItems.filter(":visible")) to get the index of the currently selected frame. However, if you want to keep using your way, then please name it something better.

I made a few changes. Of note:

I set display: none on #slider li instead of using listItems.not(':first').hide(). This is because when I loaded it the first time, all the images were briefly shown. This will prevent them from being rendered initially.
I changed the interval into a timeout that's refreshed upon changing frames. This way, if I click on a dot 4.5 seconds after the frame changes, the slider won't change my frame after just 0.5 seconds.

Here's a fiddle.

I made a few more changes in an updated version. Specifically:

By convention, constants are named in uppercase.
By convention, jQuery variables are prefixed with $.
I got rid of the selectedFrame variable and replaced it with a getSelectedIndex() function.
I made the dots dynamically added.

Here's a fiddle.


Get a limit number to test all the prime numbers it contains in C

Personally, I would put FindPrime above main, to avoid having to declare it.
Only have one variable declaration per line. With more than one, it becomes harder to see where a variable comes from. It doesn't really matter with a short program, but will for more complex ones.
Your print statements contain text which contradict what the task says (contains rather than less than)
userInput needs to be validated - Make sure that the user has entered a number, and it's greater than 0.
As mentioned, it's better that a function does one thing, rather than multiple. So your FindPrime function should be responsible for finding one, and the caller should be responsible for printing it.
In FindPrime, the if statement contains a condition that's already been tested for in the for loop, so you can simplify the if condition by removing it.
Always use {} for if (and while, for etc) statements, even if they're single line. Sooner or later, you'll add another line and break things, causing a very subtle error. And in this example, I'd reverse the condition, since the normal case doesn't do anything.

if (number%numberTest != 0 )
{
    nextNumber++;
}

With regards to the algorithm, it can certainly be improved, but that may be out of your reach at the moment.


You're taking the task too literally. All the binary stuff is just for explanation, it's not meant to be implemented.

No time now to comment it... will do it later. Learn about bit operations in the meantime.

public static String encode(int num) {
    final int translated = num + 8192;
    final int lowSevenBits = translated &  0x007F; // 0b0000_0000_0111_1111
    final int highSevenBits = translated & 0x3F80; // 0b0011_1111_1000_0000
    final int composed = lowSevenBits + (highSevenBits << 1);
    return String.format("%04X", composed);
}

You also should always write a test:

public void test() {
    assertEquals("4000", encode(0));
    assertEquals("0000", encode(-8192));
    assertEquals("7F7F", encode(8191));
    assertEquals("5000", encode(2048));
    assertEquals("2000", encode(-4096));
}


Aggregate Pandas Columns on Geospacial Distance


Calculate time difference when using time only fields

You don't seem to require worrying about time zones or daylight savings. So if you just want a simple calculation to determine the duration between two times, then a simple function will work:

Function DurationDiff(startTime As Double, endTime As Double) As Double
    '--- assumes startTime is the start time in decimal hours (0.0 to 23.99)
    '            endTime   is the ending time in decimal hours
    '    returns duration between the times in decimal hours
    '        (accounts for time crossing midnight)
    If endTime < startTime Then
        '--- midnight crossing!
        endTime = endTime + 24
    End If
    DurationDiff = endTime - startTime
End Function

Running a quick test with this function gives you:

Sub DurationTest()
    Debug.Print DurationDiff(startTime:=9#, endTime:=17#) & " hours"
    Debug.Print DurationDiff(startTime:=23#, endTime:=3#) & " hours"
    Debug.Print DurationDiff(startTime:=11#, endTime:=15#) & " hours"
    Debug.Print DurationDiff(startTime:=15#, endTime:=3#) & " hours"
End Sub

With the output:

8 hours
4 hours
4 hours
12 hours

I think it then reduces your calculation to nothing more than

Me.TotalTime = Nz(DurationDiff([startTime], [endTime]))


The "of" in the name factorial_of is redundant and unconventional.

factorial_of 0 returns nil instead of the correct answer, which is 1.

def factorial(n)
  (1..n).inject(1, :*)
end


Simple prime factorization program in Haskell

First, a few stylistic points:

It's more common to write the div and mod functions in infix, as x `mod` y == 0, etc.
A parenthesized argument to a function is never written f(x).  It's always with a separating space like f (x), so you want primes (x `div` factor).
The infix operator $ is pretty much always both preceded and followed by a space.
Similarly, in the [x..y] syntax, the .. is normally not surrounded by any space, though when it is, it is both preceded and followed by space, so either [1,3..roof] or [1, 3 .. roof] would be more common than [1,3.. roof].

and some advice on functions:

even x is preferred to x `mod` 2 == 0
For positive values, 1 + truncate x can be replaced with ceiling x.  Technically, though, you don't need to check any numbers greater than the square root, so floor (sqrt $ fromIntegral x) works in your case.
Also for positive values, the function pair quot/rem is equivalent to div/mod and the former can compile to faster code on x86_64 (e.g., quot on Ints can be a single machine instruction), so many Haskellers will get in the habit of using them whenever the substitution works.  (Alternatively, this is a ridiculous micro-optimization here, so sticking with the div/mod pair is fine if it looks more natural.)

However, it might be even better to introduce a predicate:

divides :: (Integral a) => a -> a -> Bool
divides d n = n `rem` d == 0

so you can write y `divides` x in your list comprehension, and use the section (`divides` x) in your filter.

Finally, it's usual practice to provide type signatures that are more general than the ones you've provided.  Certainly, generalizing primes and isPrime to all integral types would be usual:

isPrime :: (Integral a) => a -> Bool
primes :: (Integral a) => a -> [a]

On the other hand, isLengthLessThan is probably fine the way it as, as it's usual to assume list lengths will fit in a 64-bit Int, for obvious reasons.

Now, with respect to your questions:

  Should isLengthLessThan be in a where under isPrime?

That's up to you.  A utility function that's used in only one function is often included in a where clause, though if the function seems "independently useful", it's sometimes kept at top-level.  In this small module, keeping it at top level seems reasonable.  In a bigger module, you might stick it in a where clause to reduce clutter.

Rearranging the functions can also help.  If you put primes at the top and then put utility functions after the functions that use them (e.g., primes, then isPrime, then isLengthLessThan), it can make it easier to follow the program's organization.

  Is isLengthLessThan's name too verbose?

I'd say so.  Maybe shorterThan?  Note that the character count is probably less of an issue than the word count -- shorterThan is simple to read and understand; isLengthLessThan belongs in a Java program as a method in the ListLengthCheckerFactory class.  But also, see below for how to eliminate this function entirely.

  What should I name my "primes" function?

I'd say factors -- I mean, it's obviously going to be the prime factorization, because what other factors function would make sense?

  Are the function type specifications called that or declarations?

According to the Haskell 2010 Report, they're called "type signatures", and the corresponding function definitions f x = ... are called "bindings".  Both type signatures and bindings are examples of "declarations", so isPrime :: Int -> Bool is a declaration, but so is isPrime x = ....  However, in informal speech, if you asked "should this binding have a type declaration?", everyone would know what you meant.

  Should I put the type specifications right before the function or should I put it at the start?

Almost universally in Haskell code, type signatures are placed immediately preceding the corresponding binding rather than collected together at the beginning of the module.  While collecting them together probably seems like a nice way of documenting the "interface" at the top of the module, it has the drawback that someone stumbling across the definition may assume there's no type signature (as signatures are often optional) and so never see it, and what's more the standard Haddock documentation tool is set up to have the signature and definition of each binding in the same place.

  Do specifications help speed or do they only help us by giving us better errors?

If the type signature matches what would be inferred by GHC if it was missing (which is often the most general possible type), then there is no effect on generated code.  Moreover, GHC is designed to generate efficient code even when highly generalized signatures are used, by specializing and inlining functions aggressively.  You could most likely drop your signatures, or use the signatures I suggested above with no effect on performance.

So, yes, type signatures are primarily useful for generating better error messages, occasionally for catching certain design errors, and -- when the code is finished -- for documenting the interface.

  Do parenthesis make compilation faster or do they only muddle code up?

Any effect of parentheses on compilation speed will be miniscule, and it's standard Haskell style to omit them except where they are necessary.

Anyway, incorporating the advice I gave above, my version of your program currently looks like the following.  Note that I turned on -Wall, which reminded me that your program doesn't actually need the modules it imports, and let me know that there was a type defaulting going on in the sqrt call that I thought should be made explicit.  I also ran hlint on it, which didn't have any hints for me.

{-# OPTIONS_GHC -Wall #-}

module MorePrimes where

factors :: (Integral a) => a -> [a]
factors x
    | isPrime x = [x]
    | otherwise = factor : factors (x `div` factor)
    where factor = head $ filter (`divides` x) [2..]

isPrime :: (Integral a) => a -> Bool
isPrime x
    | x == 2    = True
    | even x    = False
    | otherwise = shorterThan 2 [y | y <- [1,3..roof], y `divides` x]
    where roof = floor $ sqrt (fromIntegral x :: Double)

shorterThan :: Int -> [a] -> Bool
shorterThan 0 _      = False
shorterThan _ []     = True
shorterThan n (_:ys) = shorterThan (n-1) ys

divides :: (Integral a) => a -> a -> Bool
divides d n = n `rem` d == 0

Finally, some additional optimizations.

Your test in the third case of isPrime can be simplified.  Since 1 always divides x, you can replace the test with:

shorterThan 1 [y | y <- [3,5..roof], y `divides` x]

which is equivalent to:

null [y | y <- [3,5..roof], y `divides` x]

which would more usually be written:

not $ any (`divides` x) [1,3..roof]

allowing you to drop the definition of shorterThan entirely and use:

isPrime :: (Integral a) => a -> Bool
isPrime x
    | x == 2    = True
    | even x    = False
    | otherwise = not $ any (`divides` x) [3,5..roof]
    where roof = floor $ sqrt (fromIntegral x :: Double)

The other problem is that factors is doing extra work here -- it's using isPrime to check if x is prime using an algorithm that handles even numbers specially for efficiency, but then it turns around and does trial division by all numbers [2..] for composites.  Instead, you could replace isPrime with a function to find the first prime factor of x, like so:

firstPrime :: (Integral a) => a -> a
firstPrime x
    | even x    = 2
    | otherwise = head [y | y <- [3,5..roof]++[x], y `divides` x]
    where roof = floor $ sqrt (fromIntegral x :: Double)

This finds the first prime factor below roof, but throws in x itself as a last resort.  (There's an unnecessary check here that x divides itself, so we could improve this code to avoid that, but it might not be worth the trouble.)

Then, factors can grab this first prime factor, and see if it was x itself (in which case x is prime and we're done) or else look for more factors.  The complete code for this version looks like:

{-# OPTIONS_GHC -Wall #-}

module MorePrimes where

factors :: (Integral a) => a -> [a]
factors x = case firstPrime x of
    factor | factor == x -> [x]
           | otherwise   -> factor : factors (x `div` factor)

firstPrime :: (Integral a) => a -> a
firstPrime x
    | even x    = 2
    | otherwise = head [y | y <- [3,5..roof]++[x], y `divides` x]
    where roof = floor $ sqrt (fromIntegral x :: Double)

divides :: (Integral a) => a -> a -> Bool
divides d n = n `rem` d == 0

Obviously, further optimization is possible, as illustrated by @Gurkenglas's answer.


Finding cells in a 3D grid with free neighbors

It seems like your neighbors2 is a pure function. I think you could speed things up by memoizing the results:

def neighbors2(row, col, separate=False):
    global _neighbors2_memoized

    args = (row, col, separate)

    if not _neighbors2_memoized:
        _neighbors2_memoized = { } # Start with empty dict
    elif row_col_sep in _neighbors2_memoized:
        return _neighbors2_memoized[args]

    # rest of your code

    if separate:
        result = (rs, cs)
    else:
        result = idxs

    _neighbors2_memoized[args] = result
    return result

Of course, a little searching will yield hundreds of memoization decorators and the like for python.

Update

Also, I think what you are doing is looking for "layers" or "levels" at which the given cell has all its neighbors empty.

If so, consider recoding your check to use intersect1d(). The neighbors are going to be constant - it's the same neighbors at each level.

So make a similar candidates array for each neighbor, and treat it like a set: "this is the set of levels where neighbor (ij) is zero".

Then, compute the intersection of that set with the candidates set (which is just the set of levels where cell is zero).

This is basically a large and: cell is zero AND neighbor(ij) is zero AND ...

You can execute len(neighbors) numpy operations instead of looping in python.

def get_free_setwise(cell):
    """
    Return the indices of a a cell that are 0 and
    where all its neighbors are 0 for the same depth
    """
    neighbors = neighbors2(*cell, False)

    candidates = np.where(state[cell] == 0)[0]

    for n in neighbors:
        candidates = np.intersect1d(candidates, np.where(state[n] == 0)[0])
        if len(candidates) == 0:
            break

    return candidates

print(timeit.timeit("get_free((4, 4))", number=10000,
      setup="from __main__ import get_free"))
print(timeit.timeit("get_free_setwise((4, 4))", number=10000,
      setup="from __main__ import get_free_setwise"))

Output of this was (note: number=10,000):

0.8349439720041119
6.3053770850092405

About seven times slower. However, when I raised depth to 700, the times were almost equal, and when I raised depth to 7000, the times were

3.6607714939891594
2.716483567011892

So the numpy-centric version does better as depth increases (FWIW).

Update 2

Using bitwise ops I was able to get better performance:

# Neighborhood pre-computation for get_free5. This is a one-time thing - the
# neighborhoods will never change as long as the rows,cols stay the same.
# NOTE however that these arrays are TRANSPOSED, because that's what free5
# does.

neighborhood = []
for r in range(rows):
    neighborhood.append([])
    for c in range(cols):
        hood = np.zeros((rows, cols), dtype=np.bool_)
        hood[r,c] = 1
        for nb in neighbors2(r, c):
            hood[nb] = 1
        hood = hood.T # Transpose!
        # Now hood is all 1's for neighbors. Do this same computation
        # in the same fashion that the state-updater does.
        bytestr = np.packbits(hood).tobytes() + b'\0'
        uint = np.frombuffer(bytestr, dtype=np.uint64)
        neighborhood[r].append(uint[0])

neighborhood = np.array(neighborhood, dtype=np.uint64)
# Neighborhood[r,c] is a bitmask with all neighbors + (r,c) set to 1
# NOTE: +(r,c) - the neighborhood explicitly includes the center point,
# which is not true for the neighbors function. I am using this to test for
# the center ALSO being zero at the same time - just one pass through the
# state array.

def get_free5(cell, *, verbose=False):
    """
    Return the indices of a a cell that are 0 and
    where all its neighbors are 0 for the same depth
    """
    result = list(np.nonzero(state_asbits & neighborhood[cell] == 0)[0])

    # This if statement is used in my self-test code. Delete all this code
    # for production use.
    if verbose:
        orig = get_free(cell)
        missing = [n for n in orig if n not in result]
        extra = [n for n in result if n not in orig]
        print("Missing from result, found in orig:")
        for n in missing:
            print("orig: (transposed)", n)
            print(etats[n])
            print("bits: {:64b}".format(state_asbits[n]))
            print("mask: {:64b}".format(neighborhood[cell]))

        print("Found in result, not found in orig:")
        for n in extra:
            print("orig: (transposed)", n)
            print(etats[n])
            print("bits: {:64b}".format(state_asbits[n]))
            print("mask: {:64b}".format(neighborhood[cell]))

    return result

# Setup for get_free5(). This code has to be run whenever the state[]
# array changes or is computed. It's an alternate encoding of the state array
# so if you don't need state[] for anything except this check, you can get
# rid of it. Otherwise, you'll have to call this before you start calling
# get_free5()

# Transpose, to get all the values at the same depth in a contiguous area.
etats = state.T
bitplane = []
for d in range(state.shape[-1]):
    #bytestr = np.packbits(etats[d]).tobytes()
    #if len(bytestr) % 8 != 0:
    #    bytestr += b'\00' * (8 - len(bytestr) % 8)

    # Hard-coding for 7x7 - append one extra byte
    bytestr = np.packbits(etats[d]).tobytes() + b'\0'
    uint = np.frombuffer(bytestr, dtype=np.uint64)
    bitplane.append(uint[0])

# state_asbits[n] is an array of 64-bit numbers, holding bits for each of the
# 49 cells at depth=n. If the value in state[r,c,n] is 1, then the bit at
# state_asbits[n].get_bit(r,c) == 1. These values can be trivially checked
# using bitwise AND operations, with the mask values in the neighbors array.
state_asbits = np.array(bitplane)

This code runs faster than the original version, but that doesn't include the computations I have to do for the state_asbits array. I think that's pretty cheap, but I don't know how often you are updating it.

$ python test.py
original code 8.314752619000501
free5: bitwise 1.1757051760068862


the fox is black - a simple image scraper

Suggested:

#!/usr/bin/python

import os
import requests

from bs4 import BeautifulSoup as bs
from pathlib import Path
from shutil import copyfileobj

MAX_PAGES = 1  # Max. number of pages is 41
SAVE_DIRECTORY = Path('fox_backgrounds')
BASE_URL = 'http://www.thefoxisblack.com/category/the-desktop-wallpaper-project/page'
RESOLUTIONS = {
    '1280x800', '1440x900', '1680x1050', '1920x1200', '2560x1440',
    'iphone', 'iphone-5', 'iphone6', 'iphone-6-plus', 'iphone6plus',
    'ipad'
}

def fetch_url(url):
    return requests.get(url).text

def clip_part(href):
    return href.rpartition('/')[-1]

def save_image(href):
    part = clip_part(href)
    print(f'   Downloading: {part}')
    fn = SAVE_DIRECTORY / part
    with requests.get(href, stream=True) as response, \
         open(fn, 'wb') as output:
        copyfileobj(response.raw, output)

def get_images_from_page(url):
    html = fetch_url(url)
    soup = bs(html, 'html.parser')
    for link in soup.find_all('a', class_='btn_download'):
        href = link['href']
        if any(href.endswith(f'-{res}.jpg') for res in RESOLUTIONS):
            save_image(href)
        else:
            print(f'Unknown resolution {href}')

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

def get_backgrounds():
    make_dir()
    for page in range(1, MAX_PAGES+1):
        print(f'Fetching page {page}...')
        get_images_from_page(f'{BASE_URL}{page}')

def main():
    get_backgrounds()

if __name__ == '__main__':
    main()

Comments:

You initialized SAVE_DIRECTORY but then use it for creation and not file write
RESOLUTIONS should be a set, or maybe a tuple
clip_url is slightly misleading; it returns a URL part and not a whole URL
It's best if you have a prompt before downloading the index page; otherwise it hangs without the user knowing what's happening
Stream your content so that you don't use up memory for big files
Your RESOLUTIONS check is a little puzzling. Maybe it's a validation step? But if it's a validation step, you silently fail instead of printing a warning. Also, you keep iterating even after you've found the correct resolution. I rewrote this to just check the current resolution, and also be a little bit more careful about where it's seen in the filename.
range(0, ...) is redundant, but for your use case you're better off with range(1 anyway.
rpartition does basically the same thing as what you wrote, but doesn't require any fancy array slicing
Don't call clip_path twice

Edit

The following version makes sane use of generators so that the iteration functions only need to know about their iteration, and not the inner business logic.

Also, your resolution check needs to be case-insensitive for many of the files on the site; and the site has gif and png images as well as jpg. You were missing some resolutions and some alternate iPhone spellings. I don't think that it's worth doing a resolution check at all, especially given these edge cases, but I left it in.

#!/usr/bin/python

import os
import requests

from bs4 import BeautifulSoup as bs
from pathlib import Path
from shutil import copyfileobj

MAX_PAGES = 1  # Max. number of pages is 41
SAVE_DIRECTORY = Path('fox_backgrounds')
BASE_URL = 'http://www.thefoxisblack.com/category/the-desktop-wallpaper-project/page'
RESOLUTIONS = {
    '1280x800', '1440x900', '1680x1050', '1920x1200', '2560x1440', '3840x2400',
    'iphone', 'iphone5', 'iphone-5', 'iphone6', 'iphone-6-plus', 'iphone6plus', 'iphone6-plus',
    'ipad'
}

def clip_part(href):
    return href.rpartition('/')[-1]

def save_image(href):
    part = clip_part(href)
    print(f'   Downloading: {part}')
    fn = SAVE_DIRECTORY / part
    with requests.get(href, stream=True) as response, \
         open(fn, 'wb') as output:
        copyfileobj(response.raw, output)

def urls_from_page(url):
    soup = bs(requests.get(url).text, 'html.parser')
    for link in soup.find_all('a', class_='btn_download'):
        href = link['href']
        if any(href.lower().contains(f'-{res}.') for res in RESOLUTIONS):
            yield href
        else:
            print(f'Unknown resolution {href}')

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

def all_urls():
    for page in range(1, MAX_PAGES+1):
        print(f'Fetching page {page}...')
        yield from urls_from_page(f'{BASE_URL}{page}')

def main():
    make_dir()
    for url in all_urls():
        save_image(url)

if __name__ == '__main__':
    main()


Webscraping application that collects metal prices and converts them from USD to MXN real-time using BeautifulSoup

Check for failure
metals = requests.get('https://www.lme.com/').text

should be
response = requests.get('https://www.lme.com/')
response.raise_for_status()
metals = response.text

String literals don't need parens
date = datetime.datetime.now()
time = (f'{date.day}/{date.month}/{date.year} | {date.hour}:{date.minute}')

would only need parens if it spans multiple lines.
Also, date shouldn't be called date for a couple of reasons:

it shadows datetime.date, and
it has a time, not just a date.

Finally: try using strftime rather than extracting the time's individual components to render a string.
No intermediate dict
metal_data = dict(zip(names, prices))
for k, v in metal_data.items():

The only reason you'd want to do this is if you're deeply concerned that there are duplicates, which this would remove. If there are no duplicates, then simply
for k, v in zip(names, prices):

Combine format strings
    v = ("%.2f" % v)
    k = metal_translation[k]
    print(f'{k}: {v} $')

can be
print(f'{metal_translation[k]}: {v:.2f}')


Generate a random integer of length N with unique digits

Not quite one line, but here's a much simpler solution:

import random

def randN(n):
    assert n <= 10
    l = list(range(10)) # compat py2 & py3
    while l[0] == 0:
        random.shuffle(l)
    return int(''.join(str(d) for d in l[:n]))

Explanation:

The problem wants N unique digits. To get those, first make a list of all the digits, then shuffle them and cut off the unwanted digits (it should be noted that if you have many more than 10 digits, shuffling the whole lot would be expensive if you only want a handful).

To turn the list of digits into an integer, the shortest way is to stringify each digit and then join them into one string, and parse that back into an integer.

But wait! If the first digit is 0, the parsed string will be one digit shorter sometimes. So, earlier in the code, simply repeat the shuffle until some other digit comes first.

Since the initial order of the list has the 0 at the beginning, we don't need to do an extra shuffle before the loop.


Node.js put records to Kinesis with infinite retry strategy.

There is no obvious bug but;

You are defining functions both within the return structure and outside of it, which makes it hard to read/parse, just keep it clean and define everything outside of it
You are sending records in a function called putRecord, that is not good.
500 should probably be a constant retrieved with config, same thing for 200
Using numbers.random() sounds like a terrible idea, you should use a library that generates GUID's (https://www.npmjs.com/package/guid)
Why assign config.kinesis.streamName to streamName, just assign it straight in the structure. It is one less line of code, and the reader does not have to wonder where it comes from
I would have re-ordered the 500 check and the assignment:

records  = records.splice(0, 500);

let payloadRecords = {
  Records: records,
  StreamName: streamName
};

instead of

let payloadRecords = {
  Records: records,
 StreamName: streamName
};

if (records.length >= 500) {
  const pushingToKinesis = records.splice(0, 500);
  payloadRecords.Records = pushingToKinesis;
} else {
  records = []
}

personally I would even go for

//Make sure we never send more than 500 records, splice keeps the rest in records
let payloadRecords = {
  Records: records.splice(0, 500),
  StreamName: streamName
};

Finally, I am not a big fan of perpetually re-sending the data. If the data gets rejected because of data-content, then at some point if you reach 500 entries that will never be stored, your pipe will filled with junk and you will stop transmitting store-able data.


Re-use of read/write object , for multiple objects

Based on the comments, I think you're confused about actually saving/loading distinct ClubInfo objects. You can call writeObject and readObject multiple times, but you need to store those ClubInfo objects in separate files if you want to persist them.

What you could do is add a ClubInfo parameter to your Serialize method, then name the file you save based on the clubName. By doing this, you don't tie down your serialize method to one instance of ClubInfo...you can pass in any ClubInfo object and it will save it to a different output file:

public void Serialize(ClubInfo club) {

  String fileName = club.teamName + ".ser";

  try {
    FileOutputStream fileOut = new FileOutputStream("/home/cg/root/" + fileName);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);

    out.writeObject(club);
    out.close();
    fileOut.close();

    System.out.println("Serialized data is saved in C:/tmp/club.ser");

    } catch(IOException i) {
        i.printStackTrace();
    }
  }
}

This is close to your original code. A few things I would change:

Assuming you're using Java 7 or newer, use the try-with-resources feature so you don't explicitly have to call close() on your streams.
I would have the thread throw the IOException that's required instead of handling it inside the method itself. It makes the method cleaner and you can handle the exception in different ways, depending on where and how you call it.

Here's how it looks afterwards:

public void Serialize(ClubInfo club) throws IOException {

  String fileName = "/home/cg/root/" + club.teamName + ".ser";

  try (FileOutputStream fileOut = new FileOutputStream(fileName);
       ObjectOutputStream out = ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
    out.writeObject(club);
    System.out.println("Serialized data is saved in C:/tmp/club.ser");
    }
  }
}

And to call it:

case 2:
    Serialize saveclub1 = new Serialize();
    try {
      saveclub1.Serialize(club1);
    } catch(IOException e) {
      System.out.println("Error saving club!");
    }

    exit = true;
    break;

You can do the same-ish thing with Deserialize. I would pass in the teamName I want to search for, get a list of files from the directory I am persisting the objects to and look for the file name that contains the team name. Then call readObject and return it from the Deserialize method. The method signature would look like this:

public ClubInfo Deserialize(String teamName)

Then your method isn't tied to the single club file.

This should also lead you in the direction you should take in saving the game state. I'll leave that to you to figure the guts out, but that should get you in the right direction!


Handling optimistic concurrency violations

What optimistic concurrency usually does is update the row specifying the old value in the where clause:

var command = connection.CreateCommand();
command.CommandText = "Update Person SET Age = @age timeStamp = @newTimeStamp WHERE ID = @id AND timeStamp = @originalTimeStamp";
command.Parameters.Add("@age", 1111);
command.Parameters.Add("@newTimeStamp", DateTime.Now);
command.Parameters.Add("@id", 1);
command.Parameters.Add("@originalTimeStamp", originalTimeStamp);
int affected = command.ExecuteNonQuery();

if (affected == 0)
{
    throw new OptimisticConcurrencyException("Data was changed by someone else, please refresh and try again");
}

Also, please use parameters for your SQL queries!


A couple of small points.

Single letter identifiers and variable names are the devil incarnate. Replace r with something meaningful.
There's not much point in using a variable for col.DataType.ToString. You only use the variable dataType once, so it would be perfectly acceptable to get rid of a line of a code and do this instead.

Select Case col.DataType.ToString
    Case "System.DateTime"
        value = New DateTime

I also don't see much benefit to the value variable. You could just set row(col) = to whatever you would set value to.
There's no reason to explicitly check for Case "System.Decimal", "System.Int16", "System.Int32", "System.Int64". It's taken care of in the Else case.

Select Case col.DataType.ToString
    Case "System.DateTime"
        row(col) = New DateTime
    Case "System.String"
        row(col) = String.Empty
    Case "System.Boolean"
        row(col) = False
    Case Else
        row(col) = 0
End Select


Angular JS Switcharoo multiple select module

I really like your code, I only have 2 minor observations:

I understand the part where default has the default items. I am not sure that after initialization you should keep referring to the default variable ( from a naming perspective ), maybe call it selection instead of default ?
Related to the previous point, ng-if="default.indexOf(key) == -1" and ng-if="default.indexOf(key) > -1" only make sense during initialization. In my mind, it would be ideal to provide a isSelected( key ) that you can use for the ng-if. Not to mention that it will remove a > out of your HTML attribute and look better in Markup ;)


Some things I think would improve your code, which is quite correct:

Having variables for high and low, you shouldn't hard code their values in the opening print.
You should use // to make sure you are getting integer division.
You can write guess = (low + high) // 2 only once, if you place it as the first line inside the while loop.
When checking for pointer, you may want to first convert it to lower case, to make sure both h and H are understood.
Make your code conform to PEP8 on things like maximum line length.
Using the format method of str can make more clear what you are printing.

Putting it all together:

high, low = 100, 0

print('Please think of a number between {0} and {1}!'.format(low, high))

guessing = True
while guessing:
    guess = (low + high) // 2
    print('Is your secret number {0}?'.format(guess))
    pointer = raw_input("Enter 'h' to indicate the guess is too high. "
                        "Enter 'l' to indicate the guess is too low. "
                        "Enter 'c' to indicate I guessed correctly.").lower()
    if pointer == 'h' :
        high = guess
    elif pointer == 'l' :
        low = guess
    elif pointer == 'c':
        guessing = False
    else:
        print('Sorry, I did not understand your input.')

print('Game over. Your secret number was {0}.'.format(guess))


First of all, you can use a where binding to clear this up:

moves :: Player -> Board -> [Board]
moves p ((a,b,c),(d,e,f), (g,h,i)) = (moves' a ((s,b,c), (d,e,f), (g,h,i))) ++
                                     (moves' b ((a,s,c), (d,e,f), (g,h,i))) ++
                                     (moves' c ((a,b,s), (d,e,f), (g,h,i))) ++
                                     (moves' d ((a,b,c), (s,e,f), (g,h,i))) ++
                                     (moves' e ((a,b,c), (d,s,f), (g,h,i))) ++
                                     (moves' f ((a,b,c), (d,e,s), (g,h,i))) ++
                                     (moves' g ((a,b,c), (d,e,f), (s,h,i))) ++
                                     (moves' h ((a,b,c), (d,e,f), (g,s,i))) ++
                                     (moves' i ((a,b,c), (d,e,f), (g,h,s)))
    where
        s = symbol p

Next, I would recommend changing your Board type to use a flat list instead of nested tuples, it's a lot easier to work with:

type Board = [Field]

For whatever your Field type is (for future reference please include all relevant definitions in your questions on SO, this goes for any language).  Now you can write this a bit differently:

moves :: Player -> Board -> [Board]
moves p [a, b, c, d, e, f, g, h, i] = moves' a [s, b, c, d, e, f, g, h, i] ++
                                      moves' b [a, s, c, d, e, f, g, h, i] ++
                                      moves' c [a, b, s, d, e, f, g, h, i] ++
                                      moves' d [a, b, c, s, e, f, g, h, i] ++
                                      moves' e [a, b, c, d, s, f, g, h, i] ++
                                      moves' f [a, b, c, d, e, s, g, h, i] ++
                                      moves' g [a, b, c, d, e, f, s, h, i] ++
                                      moves' h [a, b, c, d, e, f, g, s, i] ++
                                      moves' i [a, b, c, d, e, f, g, h, s]
    where
        s = symbol p

Now I recognize a zipWith here between the first and second arguments to moves', although we'll need a concat after the zipWith is done:

moves :: Player -> Board -> [Board]
moves p board@[a, b, c, d, e, f, g, h, i]
    = concat
    $ zipWith moves'
        board
        [[s, b, c, d, e, f, g, h, i],
         [a, s, c, d, e, f, g, h, i],
         [a, b, s, d, e, f, g, h, i],
         [a, b, c, s, e, f, g, h, i],
         [a, b, c, d, s, f, g, h, i],
         [a, b, c, d, e, s, g, h, i],
         [a, b, c, d, e, f, s, h, i],
         [a, b, c, d, e, f, g, s, i],
         [a, b, c, d, e, f, g, h, s]]
    where
        s = symbol p

Now, this still leaves the big block of different boards.  There is definitely a pattern here.  If you have a replace function with the type replace :: Int -> a -> [a] -> [a] (I'll leave this as an exercise to the reader) then you could write this as

[replace 0 s board,
 replace 1 s board,
 replace 2 s board,
 replace 3 s board,
 replace 4 s board,
 replace 5 s board,
 replace 6 s board,
 replace 7 s board,
 replace 8 s board]

Or more simply

map (\i -> replace i s board) [0..8]

So your entire function becomes

moves p board
    = concat $ zipWith moves' board
    $ map (\i -> replace i s board) [0..8]
    where
        s = symbol p
        replace i a xs = undefined
        moves' B m = [m]
        moves' _ _ = []


Jamal's answer already covers some of this, but I wanted to provide slightly different reasons for some of his advice.

1. Return an std::string rather than a const std::string.

Returning an std::string makes your function more useful to a caller because it gives them more options.

They can do this:

std::string s1 = DNAStorage::get_block ("abc1234:blah") ;

Or this:

const std::string s1 = DNAStorage::get_block ("abc1234:blah") ;

Or even this:

const std::string &s1 = DNAStorage::get_block ("abc1234:blah") ;

Unless you are trying to force some kind of interface onto the caller, there isn't really any good reason (from a programming perspective) to return a const std::string.

2. Cut down on copying.

You copy a substring of name into buffer, then a substring of buffer into into temp, and then you copy temp into result. This is unnecessary. See section 4 of this post for an example that avoids all of this copying.

3. Avoid memory leaks and focus on exception safety.

You dynamically allocate memory for two character arrays. If you actually needed a buffer (which you don't), you could just do something like this:

std::string buffer (name.size (), ' ') ;

4. Here's an example using std::find() and iterators.

Edit: 200_success was kind enough to provide a more concise version:

std::string DNAStorage::get_block (const std::string &name)
{
    typedef std::string::const_iterator iter ;

    iter colon = std::find (name.begin (), name.end (), ':') ;

    if (colon == name.end ()) {
        return "";
    }

    iter start = colon ;

    while (start != name.begin () && std::isdigit (*(start - 1))) {
        --start ;
    }

    return std::string (start, colon) ;
}

Test cases:

int main ()
{
    const std::string s1 = DNAStorage::get_block ("abc1234:blah") ;
    const std::string s2 = DNAStorage::get_block ("5n63:124") ;
    const std::string s3 = DNAStorage::get_block ("nocolon") ;
    const std::string s4 = DNAStorage::get_block ("nonumber:ffff") ;
    const std::string s5 = DNAStorage::get_block ("5363:124") ;
    const std::string s6 = DNAStorage::get_block ("v32463:124") ;

    return 0 ;
}

5. Here's an example with std::reverse_iterator.

First we have to create a unary predicate for std::find_if.

bool is_not_digit (char c)
{
    return std::isdigit (c) == 0 ;
}

And here's how we can code the function:

std::string DNAStorage::get_block (const std::string &name)
{
    typedef std::string::const_iterator const_iterator ;
    const const_iterator colon_position = std::find (name.begin (), name.end (), ':') ;

    if (colon_position == name.end ()) {
        return "" ;
    }

    std::reverse_iterator <const_iterator> rstart (colon_position) ;
    std::reverse_iterator <const_iterator> rend = std::find_if (rstart, name.rend (), is_not_digit) ;

    std::string out (rstart, rend) ;
    std::reverse (out.begin (), out.end ()) ;
    return out ;
}


You could do:

String toString(){
    "${ass?.app?.appName ?: ''} ${step?.stepName ?: 'null'}"
}


Too much IO.
Text.Regex.Posix is too much for those simple tests. Why not any isUpper, any isLower, any isDigit?
Your program ignores --help and --with-symbols (have a look at OptParse-Applicative)
randomReplace has too many temporary structures (see below)
You use length too often. Unlike in PHP, it's quite slow (O(n) vs O(1)).

A short search for bottlenecks

  I'm guessing my bottleneck here ...

Don't guess, measure. Enable profiling to check where you're actually losing time:

$ stack install --profile random
$ stack install --profile regex-posix
$ stack exec -- ghc -O2 -prof -auto-all PWGen.hs
$ ./PWGen+RTS -s -p

If you don't use stack, make sure to enable profiling when you install the packages:

$ cabal sandbox init
$ cabal install -p random regex-posix
$ cabal exec -- ghc -O2 -prof -auto-all PWGen.hs
$ ./PWGen +RTS -s -p

This is actually fast, but not fast enough:

9,420,841,024 bytes allocated in the heap
   6,425,011,808 bytes copied during GC
       1,021,088 bytes maximum residency (2915 sample(s))
          59,672 bytes maximum slop
               4 MB total memory in use (0 MB lost due to fragmentation)

                                     Tot time (elapsed)  Avg pause  Max pause
  Gen  0     15199 colls,     0 par    1.844s   1.971s     0.0001s    0.0011s
  Gen  1      2915 colls,     0 par    1.188s   1.076s     0.0004s    0.0009s

  INIT    time    0.000s  (  0.001s elapsed)
  MUT     time    1.953s  (  2.001s elapsed)
  GC      time    3.031s  (  3.048s elapsed)
  RP      time    0.000s  (  0.000s elapsed)
  PROF    time    0.000s  (  0.000s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    5.000s  (  5.050s elapsed)

  %GC     time      60.6%  (60.4% elapsed)

  Alloc rate    4,823,470,604 bytes per MUT second

  Productivity  39.4% of total user, 39.0% of total elapsed

Note: I've changed passwordLength to 5000, since it was too quick with 500. However, the times above don't give enough information where you actually lose that time. That's what -p was for. PWGen.prof contains the following data:

COST CENTRE                        MODULE                     no.     entries  %time %alloc   %time %alloc

MAIN                               MAIN                        59           0    0.0    0.0   100.0  100.0
 main                              Main                       119           0    0.8    0.0    99.9  100.0
  generatePassword                 Main                       121           1    0.4    0.1    99.1  100.0
   randomReplace                   Main                       124        5730   69.5   62.9    98.7   99.9
    randomReplace.hash             Main                       125        5728   29.2   36.9    29.2   36.9
  main.helpOnly                    Main                       120           1    0.0    0.0     0.0    0.0

Almost all memory is allocated in randomReplace. After all, you break both the map and the list apart for every character in replacement. That's quite costly. Instead, let's try to stay on a single map as long as possible:

-- This is still not an idiomatic version, but better
randomReplace :: String -> String -> IO String
randomReplace rs subject = fmap (map snd . M.toList) $ go rs $ M.fromList $ zip [0..] subject
  where
    l = length subject
    go []     m = return m
    go (r:rs) m = do
      randomIndex <- randomRIO (0, l - 1) :: IO Int
      go rs $ M.insert randomIndex r m

What's the big difference? Well, we're not using length repeatedly, which is a big plus. length needs to traverse the whole list to get its result. Also, we're not switching between lists and maps all the time, which gets rid of all intermediate lists.

Running the benchmark again, we gain the following result:

  INIT    time    0.000s  (  0.001s elapsed)
  MUT     time    0.016s  (  0.021s elapsed)
  GC      time    0.016s  (  0.011s elapsed)
  RP      time    0.000s  (  0.000s elapsed)
  PROF    time    0.000s  (  0.000s elapsed)
  EXIT    time    0.000s  (  0.000s elapsed)
  Total   time    0.031s  (  0.033s elapsed)

  %GC     time      50.0%  (32.6% elapsed)

  Alloc rate    2,105,396,736 bytes per MUT second

  Productivity  50.0% of total user, 47.7% of total elapsed

                                                                                individual     inherited
COST CENTRE                        MODULE                     no.     entries  %time %alloc   %time %alloc

MAIN                               MAIN                        59           0    5.6    0.0   100.0  100.0
 main                              Main                       119           0    5.6    0.1    94.4   97.8
  generatePassword                 Main                       121           1   55.6   44.7    88.9   97.7
   randomReplace                   Main                       124           2    0.0   16.4    33.3   53.0
    randomReplace.l                Main                       126           2    0.0    0.0     0.0    0.0
    randomReplace.go               Main                       125        5521   33.3   36.6    33.3   36.6
  main.helpOnly                    Main                       120           1    0.0    0.0     0.0    0.0

Runtime is down to 0.03s from 5s. Keep in mind that this is for passwordLength = 5000. This is 0.6% of the original runtime. I can even crank passwordLength up to 100000 and it still takes only 0.7s total.

The reason here is (somewhat) simple. randomReplace had a bad asymptoticl complexity. If n is the length of subject and k is the length of rs, you get roughly:

k                    -- for every character in rs
  * ( n              -- get the length of subject (every time!)
      + n * log(n)   -- create the map
      + log (n)      -- insert a character at a random position
      + 2 * n        -- zip the list and zip it back
    )

Compare this to the new version:

k * (log (n))        -- for every character insert a character into a map
+ n                  -- get the length once(!)
+ n * log (n)        -- create a map once(!)
+ 2 * n              -- zip the map and zip it back

So this would be a way to improve your runtime tremendously.

Further places for improvement

generatePassword is too opaque, it's not clear what you're doing. Try to split it into several sections. For example, all those lists of symbols can be abstracted into

randomListOf :: Random g => Int -> (g, g) -> IO [g]
randomListOf n r = do
    l <- randomRIO (1, n)
    replicateM l $ randomRIO r

-- ...
generatePassword = do
    -- ...
    digits    <- randomListOf passwordLength ('0','9')
    uppercase <- randomListOf passwordLength ('A','Z')

However, you should try to make all your functions pure. That way, you can easily test them later.

Also, you can try to generate passwords that contain at least one digit. It's not possible with random by default, but you can probably achieve something similar to a modified Test.QuickCheck.frequency.


I believe your issue is related to the setInterval, you should at least try not to use it; instead use requestAnimationFrame.

for example here is a link:
http://paulirish.com/2011/requestanimationframe-for-smart-animating/

GL!


Bug

In the constructor, matchingRegex must be initialized before calling getAllFiles(root);, otherwise a NullPointerException is thrown if the root folder contains subfolders.

Design

Constructor

I agree with @holroy about the fact that there are too many things done in the constructor. Its job is too heavy, instead of just initialize some things. Moreover, it makes the FileFinder instance unusable for other parameters, e.g. to search in another folder or with another regex, one will need to instantiate a new FileFinder.

getAllFiles(File)

It should be named getAllMatchingFiles, because in the current implementation it returns only the files with names matching the regex and all the directories. By the way, it is not very clear why the directories are put there.

The implementation of this method is correct in general, but is coded entirely with before-Java-7 style. Indeed, java.nio would be a lot of help here.

First of all, there is FileVisitor interface. Using a SimpleFileVisitor prevents us from pre-calculating Map<String, File> files. It also solves the problem of files with same names in different folders:

public Collection<Path> findFiles(String fileName) throws IOException {
  final Collection<Path> foundFiles = new ArrayList<>();
  Files.walkFileTree(this.root.toPath(),
                     new SimpleFileVisitor<Path>() {
      @Override
      public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException {
        if (file.getFileName().toString().equals(fileName)) {
            foundFiles.add(file);
        }
        return FileVisitResult.CONTINUE;
      }
    });
  return foundFiles;
}

This implementation will return all the files having fileName. If you want to get them by regex, it's achieved with a little change in the if condition.

If we push a bit deeper and try to benefit from Java 8, it becomes really concise:

public Collection<Path> findFiles8(String fileName) throws IOException {
  return Files.find(this.root.toPath(),
                    Integer.MAX_VALUE, // NB! should be more reasonable :)
                    (file, attrs) -> {
         return Files.isRegularFile(file)
           && file.getFileName().toString().equals(fileName);
    }).collect(Collectors.toList());
}

In fact, Files.find(args) can do almost all the job of FileFinder...

copy(args)

I think that this method should be defined elsewhere, because it does not correspond to the functionality of FileFinder.

It's a good idea to use Files.copy() inside it (and even better would be instead of it). Since this is a public method, the validation of the arguments should be done on non-nullability of file and dest. The existence of the files will be checked in Files.copy(), with the respective exceptions thrown.

The target Path object can be created easier with dest.toPath().resolve(file.getName()), no need to instantiate a File.


Interface

To design functions that other programmers will find useful, it helps to draw inspiration from conventions established by existing string libraries.

In C, as in other languages with array indices numbered from 0, it is customary to use inclusive-exclusive ranges.  from would be the index of the first byte to delete; to would be the index of the first byte to keep.  That has the nice property that to - from indicates the number of characters to be deleted.  That convention would echo the String.substring(beginIndex, endIndex) function that Java offers, for example.

Another common convention in C would be to accept parameters (str, from, len) — the last parameter specifying the number of bytes to remove.

Finally, you should study Perl's splice function and JavaScript's array.splice(), which are generalized versions of this function.

Implementation

Starting from the top…

void strdev (char *str, long from, long to)

The name strdev doesn't mean anything to me.  I suggest splice_away.

    char *part1 = malloc(strlen(str) - strlen(&str[from]));

You call strlen() twice, when you don't need to call it at all.  malloc(from) will do.  Since strlen() works by visiting every byte in the string, you should avoid calling it carelessly.

    long i;

i is no longer used.  Prune the dead code.

    memmove(part1, str, from); // New copying

part1 is newly allocated memory, and is guaranteed not to overlap with the source string.  Therefore, memcpy() will do.

    sprintf(str, "%s%s", part1, &str[to+1]);

Whoa! Why are you overwriting the initial part of the string with the same characters?  You didn't need to do any of that previous work.

The whole function can be condensed down to

char *splice_away(char *str, int from, int to)
{
    memmove(str + from, str + to, 1 + strlen(str + to));
    return str;
}

I've used the inclusive-exclusive range convention mentioned above, and added return str for the caller's convenience.


Test whether a type T is among the types of a tuple

Since you're implementing a traits-like check, I would typically expect your traits class types to be derived from std::true_type and std::false_type. This is certainly not required, and overall this implementation doesn't bug me. I was slightly surprised to see std::tuple<> called out explicitly, but that doesn't seem to change the overall correctness in any way.

The biggest question I have is how exact you want your type matching to be. Do you require exact type matches like std::is_same<T, U> or usably similar types like std::is_convertible<T, U>? To be concrete, what should Is_in_tuple<decltype(0), std::tuple<unsigned int>>::value be? It would be great to have some unit tests that document this sort of edge case.

The only thing I see wrong with your implementation is by implication: either you must have included some form of using, either using namespace std; or using std::tuple;, or you are dependent on someone else having done so before including this header. While correct use of using is fine, particularly in implementation files or inside functions, I would strongly suggest avoiding it here where it will pollute (or require previous pollution of) the compilation unit of whatever code includes this.


Java function to rotate an image by 180 degrees

Your code is complicated and hard to read, say the least. So I'll start discussing dimensions of arrays in java, and ease into a re-implementation of your code. Some lessons to be learned is added to the end.

If you create a two-dimensional array in java, int[2][3] twoDim, and use Arrays.deepToString(twoDim) (from java.util.Arrays) to print it. It'll print something ala: [[0, 1, 2], [3, 4, 5]]. And twoDim[1][2] has the value 5. This I interpret that the first dimension is zero-based rows, and the second dimension is zero-based columns.

In your code you intermix x and y and use the width when calculating the y (which I read as height or rows). In addition you add an i and offset to the mix which makes everything very hard to read.

So I plain and simply gave up trying to understanding what your algorithm tries to do. The last two variables further add to the confusion with one top and the other leftBottom. Is the top to the right or left, and what happens when you start iterating?

Helper methods

In order to try figuring out what is really happening I made these three helper methods and variables:

final int height = 3;
final int width = 3;
int[][] imageMatrix = new int[height][width];

public void swap(int x1, int y1, int x2, int y2) {
  int tmp;
  tmp = imageMatrix[y1][x1];
  imageMatrix[y1][x1] = imageMatrix[y2][x2];
  imageMatrix[y2][x2] = tmp;
}

public void resetMatrix() {
  int k = 0;
  for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
      imageMatrix[row][col] = k++;
    }
  }
}

public void printMatrix(String title) {
  System.out.println(title);
  for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
      System.out.printf("%2d ", imageMatrix[row][col]);
    }
    System.out.println();
  }
  System.out.println();
}

This allowed me to have a slightly simpler structure to work with, and I can display the matrix whenever I feel like it.

On a square image of 3x3 I got the following output:

 Original
  0  1  2
  3  4  5
  6  7  8

 rotateImage180Deg
  8  7  6
  5  4  3
  2  1  0

I then proceeded to changed your algorithm using the swap helper method (and added some space to align swap coordinates), this gave this code:

public void rotateImage180DegII() {
     for (int x = 0; x < width / 2; x++) {

         int y = width - 1 - x;
         for (int i = x; i < y; i++) {
             swap(i, x,            y - i + x, y);
             swap(x, y - i + x,    y, i);
         }
     }
 }

Output was the same, as expected, and it does fail with the same (but more on that later). In the code above, you clearly see how strange
the usage of x and y becomes, as you intermix them freely.

Re-implementation

Finally I proceeded to re-implement the algorithm. My thought was to simply start at the upper left and swap with bottom right, and move through the entire row. Repeat for next row(s). After doing this I discovered that I had to cover a special case when there was an odd number of rows, so I added a second loop to handle that case. This resulted in the following code:

public void rotateImage180DegIII() {
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < (height / 2); y++) {
      swap(x, y,         width - 1 - x, height - 1 - y);
    }
  }

  if (height % 2 == 1) {
    int mid_y = height / 2;
    for (int x = 0; x < (width / 2); x++) {
      swap(x, mid_y,      width - 1 - x, mid_y);
    }
  }
}

Even without any comments, I think this code reads a lot clearer. The first double for loop reads as swap an element at x, y with an element at width - 1 - x, height - 1 - y. Repeat this for the entire x-column range, and half the height range.

With top left as 0, 0, this reads as swap an element from top left with an element at bottom right. Move to the right for the first element, and to the left for the second element. Repeat until row is swapped, and then repeat for the second (and second to last row).

Finally, if there is an odd number of rows, swap the far left element of the middle row with the far right element on the same row. Increase x and repeat until row is swapped.

Changing dimensions

This code actually handles various dimensions, whereas your original code gets ArrayIndexOutOfBoundsException whenever the second dimension is larger than the first. And it produces strange examples if the second dimension is less than the first.

Here is the output from a 3x3 run (the first line is the Arrays.deepToString(imageMatrix) output):

[[0, 1, 2], [3, 4, 5], [6, 7, 8]]

Original
 0  1  2
 3  4  5
 6  7  8

rotateImage180DegIII
 8  7  6
 5  4  3
 2  1  0

rotateImage180DegII
 8  7  6
 5  4  3
 2  1  0

Nicely rotated by both alternatives. Let's continue with an 5x3 run:

[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14]]

Original
 0  1  2
 3  4  5
 6  7  8
 9 10 11
12 13 14

rotateImage180DegIII
14 13 12
11 10  9
 8  7  6
 5  4  3
 2  1  0

rotateImage180DegII
 8  7  6
 5  4  3
 2  1  0
 9 10 11
12 13 14

I think my output make sense, but not yours. I'm not sure what is happening, or why. But it does look strange. Finally let us do a 3x5 run:

[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]

Original
 0  1  2  3  4
 5  6  7  8  9
10 11 12 13 14

rotateImage180DegIII
14 13 12 11 10
 9  8  7  6  5
 4  3  2  1  0

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
  at Main.swap(Main.java:13)
  at Main.rotateImage180DegII(Main.java:64)
  at Main.main(Main.java:95)
exited with non-zero status

Lessons to be learned

Work on the variable naming – Using x and y which have a very strong co-nation to coordinates, and intermixing them is bad. You loose control over what they indicate.
Perform wider tests – I don't think you've tested with alternating dimensions, as you then would have discovered the issues I point out in the Changing dimensions tests
Add helper methods – Using helper methods can both help in visualizing the process, and reduce the clutter so you can focus on the algorithm. Compare your original code with the code using swap. Just that is a nice improvement. Adding something like a printMatrix and resetMatrix can also be very helpful to understand what is happening.

Translating my alternate implementation to yours with your definition of imageMagic and RGBColor I trust you'll manage. Note that within the swap method the y coordinates needs to be in the first dimension, whilst the x coordinates are in the second dimension. In other words, keep your head straight on when handling dimension indexes.

Update: Changed to non-static methods, which purely was a result of laziness in my test setup based upon the static main() method, and not proper object initialization.


What do you mean optimal?

Your thing transforms the object lots of times, which is liable to not be very efficient, if that's what you meant.

Here's something that I hope is simple to read that does your task.

function sumObjects(obj1, obj2) {
    return Object.keys(obj2).reduce(
        (acc, currKey) => ({
        ...acc,
        [currKey]: acc[currKey] ? acc[currKey] + obj2[currKey] : obj2[currKey]
        }),
        obj1
    );
}

const result = dimensions.reduce(sumObjects);

The observation is that you just need to see how to do your task for two objects and the rest is just a reduce.

The most efficient thing will likely be just a for loop, like

const res = {};
for (const obj of dimensions) {
    for (const key in obj) {
        res[key] = res[key] ? res[key] + obj[key] : obj[key];
    }
}


From a short review;

You probably want to encapsulate if(matrix === null || matrix.length === 0) into an isValidMatrix function for re-use
I would drop the whole i/j thing and go for variables named after row and col
I would collect all the rows and columns with a 0 in one set of loops, and then clear the matrix based on the collected rows and columns in another set of loops.
let only declares in the block, so the for(i=0; i<matrix.length; i++){ creates a global i
I much prefer function setFlag(matrix, i, j) { over const setFlag = function(matrix, i, j) {

This is my current proposal, I did not test it, but you should get the gist of it;
//This does not catch everything, but it'll do
function isValidMatrix(matrix){
  return !(!matrix || !matrix.length);
}

function setZeroes(matrix) {

  if(!isValidMatrix(matrix))
    return [];

  const rows = [], cols = [];

  for(let row = 0; row < matrix.length; row++){
    for(let col = 0; col < matrix[row].length; col++){
      if(matrix[row][col] === 0){
        rows.push(row);
        cols.push(col);
      }
    }
  }

  for(let row = 0; row < matrix.length; row++){
    for(let col = 0; col < matrix[row].length; col++){
      if(rows.includes(row) || cols.includes(col)){
        matrix[row][col] = 0;
      }
    }
  }
}

For further refinement, rows and cols might/will get multiple times the same value, so to deal with that gracefully you should use Sets for that.


I'd prefer writing it like this:

dataSearcher = (element, identifier) ->
  return element if element.identifier is identifier
  for child in element.children
    found = dataSearcher child, identifier
    return found if found

Changes:

guard style instead of else (one less level of indentation)
postfix if (probably a question of style)
is instead of ==
shorter variable names


Instead of transforming everything to String using Show just for the purpose of hashing it, you should constraint element types to be Hashable instead:

getHashes :: Hashable a => BloomFilter -> a -> [Int]
getHashes bloomFilter elem =
  let seed    = hashSeed bloomFilter
      maxSize = m bloomFilter
  in  (`mod` maxSize) . abs . (`hashWithSalt` elem) . (seed +) <$> [1..(k bloomFilter)]

I'd also use maxSize and numFuns as the field names in BloomFilter and then use RecordWildCards:

getHashes :: Hashable a => BloomFilter -> a -> [Int]
getHashes BloomFilter{..} elem = map nthHash [1..numFuns]
  where
    nthHash n = abs (hashWithSalt (n + hashSeed) elem) `mod` maxSize

Or maybe even nicer:

getHashes :: Hashable a => BloomFilter -> a -> [Int]
getHashes BloomFilter{..} elem =
  [ abs (hashWithSalt (i + hashSeed) elem) `mod` maxSize  | i <- [1..numFuns] ]


Sub Fortune_parser()

The procedure is implicitly Public; I'd make it explicitly so. It's also using a rather uncommon casing style I don't even have a name for. In every single VBA type library out there[citation needed], public members are PascalCase.

Note that classes that expose public members that have underscores in their names, cannot be used with the Implements keyword; the underscore has a special meaning in VBA, and thus its use should be avoided in member names.

Dim driver As New WebDriver

If you had Rubberduck installed, this would pop an inspection result about driver being auto-instantiated. The way VBA treats those is rather unsettling (and frankly annoying) - essentially, you can't destroy an object that was declared with As New. Best split declaration and assignment into two distinct instructions:

Dim driver As WebDriver
Set driver = New WebDriver

I see you've avoided the classic beginner trap:

Dim posts As Object, post As Object, items As Object, item As Object

Well done! However posts is declared there, and then assigned 8 instructions later. post is used further down, and items+item are even further. I wouldn't declare all variables at the top of the procedure; by doing that it's easy to declare a variable and then forget about it, and then you're stuck with dangling declarations for things you're not using anywhere.

Consider:

Dim driver As WebDriver
Set driver = New WebDriver
driver.Start "chrome", "http://fortune.com/fortune500"
driver.Get "/list/"
driver.Wait 500

'...

Dim posts As Object
Set posts = driver.FindElementsByXPath("//li[contains(concat(' ', @class, ' '), ' small-12 ')]")

Dim post As Object
For Each post In posts
    '...
Next

That last loop is interesting. Where are i, item_links and x declared? Are you not using Option Explicit?

I think I would make a With block hold the reference to the driver object; that way I'm sure it's destroyed proper.

So scratch the part above about splitting declaration and assignment; don't even declare it, and wrap the whole procedure body with this:

With New WebDriver
    .Start "chrome", "http://fortune.com/fortune500"
    '...
    .Quit
End With

You're not consistent about parentheses; these ones are redundant:

driver.ExecuteScript ("window.scrollTo(0, document.body.scrollHeight);")

Notice the whitespace between the method name and the opening parens; these parentheses are telling VBA to take that string literal and pass it by value to the ExecuteScript method.

Should be:

driver.ExecuteScript "window.scrollTo(0, document.body.scrollHeight);"

That way this method call looks like every other method call you have, and doesn't force any argument to be passed ByVal.

There's some implicit code here:

Cells(x, 1) = item.FindElementByXPath(".//div[contains(@class,'small-7')]/p[@class='remove-bottom-margin']").Text
Cells(x, 2) = item.FindElementByXPath(".//div[contains(@class,'small-9')]/a").Attribute("href")

Unqualified Cells calls like these, implicitly refer to the ActiveSheet, which may or may not be set to a valid reference, and may or may not be the sheet you expect. You're also implicitly assigning to the default property of that Range object (the reference returned by Cells).

Explicit code would be:

ActiveSheet.Cells(x, 1).Value = item.FindElementByXPath(".//div[contains(@class,'small-7')]/p[@class='remove-bottom-margin']").Text
ActiveSheet.Cells(x, 2).Value = item.FindElementByXPath(".//div[contains(@class,'small-9')]/a").Attribute("href")

Your code will work fine, until the scraped website changes their CSS classes. I would extract the CSS class names from the string literals and into a local constant, e.g.:

Const CSS_CLASS_HREF As String = "small-9"

And then concatenate the constant into the xpath query:

item.FindElementByXPath(".//div[contains(@class,'" & CSS_CLASS_HREF & "')]/a").Attribute("href")

That way if/when the CSS changes, you don't need to dig up any specific instructions, you just update your CSS class names constants to mirror the website's changes.

Error handling is missing. What happens when the site is down? When FindElementByXPath returns Nothing? A flawless procedure would handle these sticky situations gracefully.


Generic Graph using Adjacency matrix - Java

You can simplify the mapping you are using to get from vertex to it's associated object. Instead of using a List<V>, which can only guarantee \$\mathcal{O}(n)\$ lookup time, you could just use an array.
Comments restating what's known should be removed, comments that can be made obsolete through refactoring should prompt that refactoring:

public class GraphAM<V> {
    private Map<V, Integer> vertexToIndex;
    private V[] indexToVertex;

    private int[][] adjacencyMatrix;
    private int index;

vertices (aka vertexToIndex can be eagerly initialized.
all private fields (apart from index) should be marked final.
Instead of passing numVertices to the constructor, you should consider passing a Collection<V>, which removes the need to deal with adding vertices in addEdge.
Finally instead of using a boolean[] for visited, a Set<V> is significantly more obvious. It might come with a minor performance penalty though.
Finally the search functions do not search for anything... they're just performing an exhaustive traversal and are accordingly useless in their current form...


Calculating the total of each column and display in footer of gridview in DataTable

#region column sum and show in footer

Before you start optimizing this block you should move it into a separate method. The region clearly indicates that it's a part of something bigger that is doing too much. I find it's pointless to try to optimize this as long as it's a region of something else.

Currently your method not only calculates the RowTotals but it also updates a grid-view. This should not be done by the same method because you cannot test whether the totals are calculated correctly. The only way to validate it is to look at the grid. This is very unreliable.

Basically it should look like this: you have two worker methods

int[] CalculateRowTotals(DataTable dataTable) { ... }

void UpdateTotalsGridView(int[] rowTotals) { ... }

and one method that combined the results of both:

void DisplayRowTotals(..)
{
    var rowTotals = CalcualteRowTotals(..);
    UpdateTotalsGridView(rowTotals);
}

About the code itself...

if(dt_.Rows.IndexOf(row) !=0)

It is not clear why you are doing this. A helper variable or method would be nice so that you can give it a meaning like I don't know isHeader or something that makes sense.


I agree with commenter @vnp, who says, "The question would greatly benefit from adding getMedianWord code directly. As posted, it is on the verge of being closed as hypothetical." All the interesting stuff is going on in getMedianWord.

TLDR: yep, your use of pthread_create and pthread_join looks fine.

However, I couldn't be sure of that without knowing the declaration of getMedianWord, for which I had to click through to frequency.c:

void *getMedianWord(void *vargp) {
    char *filename = *(char **) vargp;

Okay, this is fine.

However, you could do better by passing the char pointer itself as your void* argument, instead of passing the address of the pointer. That is, if you rewrote your getMedianWord function —

void *getMedianWord(void *vargp) {
    const char *filename = vargp;

— then you could rewrite your main loop correspondingly —

for (int i = 1; i < argc; ++i) {
    pthread_create(&tid[i], NULL, getMedianWord, argv[i]);
}

Notice that I've made two other cosmetic changes here. First, I put braces around every compound statement; see goto fail for why. Second, I took your complicated actual_args/i + 1 logic and turned it into an idiomatic loop running over the half-open range [1, argc). Use the most common idioms you can! It saves your reader some brain cells.

The functional change here is replacing &argv[...] with simply argv[...]. (The cast to void* is not required in C. And likewise the cast back from void* inside getMedianWord is not required in C, although it would be required if you wanted to port this code to C++.)

I won't very closely review the rest of getMedianWord because you didn't post it.

It is definitely not thread-safe, though; there's a data race on file_index. Look up the _Atomic keyword.

I notice that getMedianWord spends most of its time reading from files, which means that it's not likely to parallelize very well. You'll end up being bounded by the speed of your file-reading. Splitting up those reads into different threads doesn't achieve any speedup; in fact it probably wastes time because now you have to wait for a context-switch between each pair of reads. (OTOH, the files will be buffered at the stdio level and also probably brought into memory at the OS level, so maybe I'm wrong about its hurting. I think I'm right about its not helping, though.)

Try reading all the files into memory first, and then splitting up just the computational processing of those files into multiple threads.

In other words, think of "the filesystem" as a contended resource which will destroy your parallelism if everyone's trying to access it at once. (Also think of the malloc/free heap and stdout as contended resources. Looks like you're doing well on those fronts.)

Your WordArray word_array[MAX_WORDS]; is a variable-length array, which might blow up your (thread's) stack if MAX_WORDS is increased to, say, 2000000 / sizeof(WordArray) — that is, if MAX_WORDS * MAX_STRING >= 2000000. So watch out for that.


Your code can be simplified using a simple loop, eliminating most of the duplicated code:

def game_of_cups(zipcode, rules):

    total_points = 0

    for num, rule in enumerate(rules, 1):
        rule_passes = rule(zipcode)
        points = num + 4 if rule_passes else 0
        total_points += points
        print(f"Rule {num} got {points} points, so total is now {total_points}")

    print(f"{zipcode} got {total_points} points!")

You just need the appropriate rule functions, like:

def rule1(zipcode):
    return zipcode[0] == zipcode[-1]

def rule2(zipcode):
    a, b, c, d, e = map(int, zipcode)
    return b * 2 > a and c > min(b, d)

... etc ...

And then a list of rules to pass to the game:

rules = [ rule1, rule2, rule3, rule4, rule5, rule6, rule7 ]

Feel free to name the functions more appropriately; they don’t need to be named rule#.

Are you missing a rule?  You said there were 8 rules.

Your implementation of rule#2 doesn’t match the comment description of rule #2.  I think it should be b == a * 2, not b * 2 > a.


There's no need to check if array[0] > array[1] twice.

if (array[0] > array[1]) {
    max = array[0];
    secondmax = array[1];
} else {
    secondmax = array[0];
    max = array[1];
}

But if you really want to use the ternary operator here (? :), at least move them to the declarations, and move the assertion above.

It's a micro-optimization, but I would assign array[j] to a temporary variable inside the loop since it may potentially be accessed four times. Plus it is more readable

for(j=2;j<n;j++)
{
    int candidate = array[j];
    if (candidate > max) {
        secondmax = max;
        max = candidate;
    }
    else if (candidate > secondmax) {
        secondmax = candidate;
    }
}

Gotta say that I'm not a big fan of a single space for indentation or putting multiple statements on a single line. I've worked at a company where we used two before, and that was confusing. One is asking for trouble. And with 24+ inch monitors common, there's no need to be so frugal with your vertical screen real estate! :)

The assertion seems like a good choice, though you might consider returning a signaling value instead. It really depends on what callers will expect, and make sure you document your choice.


Copy directories while changing Unicode filenames to ASCII

My overall take was "Nice!". :)

Given your exchange with Patrick J S above (not to mention my lack of familiarity with the wrinkles discussed in the relevant RFCs) I'm not going to comment at all on the technical-compliance-with-standards aspects of your decoding logic.

Simplify Inline::Perl5 code

use Inline::Perl5;
my $p5 = Inline::Perl5.new;
$p5.use('Text::Unidecode');
.
.
sub transformfilename($filename) {
  return $p5.call('Text::Unidecode::unidecode', $filename);
}

That was old style. Write this way instead:

use Text::Unidecode:from<Perl5>;

sub transformfilename($filename) {
  return Text::Unidecode::unidecode($filename);
}

Don't repeat yourself?

sub transformdirname($dirname is copy) {
  $dirname.=subst( /:i \%(<[0..9A..F]>**2) /, { chr(:16(~$0)) }, :g );
  $dirname.=subst( /\//, '_', :g );
  return $dirname;
}

This has 'dirname' in the sub's name plus four mentions of $dirname. Sometimes repeats are valuable, perhaps making code easier to read or change, but in this case I think the reverse applies. I'd write:

sub transformdirname($_ is copy) {
  .=subst( /:i \%(<[0..9A..F]>**2) /, { chr(:16(~$0)) }, :g )
  .=subst( /\//, '_', :g )
}

transformfilename has a similar issue:

sub transformfilename($filename) {
  return $p5.call('Text::Unidecode::unidecode', $filename);
}

('filename' is written three times.) In addition it's a one line routine introducing the abstract "transform" and I'm going to assume the transformation is unlikely to change, so abstracting it out to a sub is questionable. In addition there's another several 'file's in the call so instead I'd elide the sub altogether and eliminate all the repeats of 'file'. We'll see that later in the final version where I've made some additional changes.

Comments

Imo good code means good comments. You've written good comments like:

sub transformdirname($dirname is copy) {
  $dirname.=subst( /:i \%(<[0..9A..F]>**2) /, { chr(:16(~$0)) }, :g ); # undo URL encoding
  $dirname.=subst( /\//, '_', :g ); # make it a valid UNIX filename
  return $dirname;
}

In Raku you can effortlessly turn such ordinary end-of-line comments (that start with #) into Pod declarator comments (that start with #| or #= and appear immediately before or after a declaration):

#| undo URL encoding and make it a valid UNIX filename
sub transformdirname($_ is copy) {
  .=subst( /:i \%(<[0..9A..F]>**2) /, { chr(:16(~$0)) }, :g );
  .=subst( /\//, '_', :g );
}

Now documentation tools can automatically extract documentation of the sub because this:

say &transformdirname.WHY;

now displays:

undo URL encoding and make it a valid UNIX filename

(Why is the method that extracts such doc called WHY? It's to remind folk that by far the most important thing to explain in code comments is WHY -- why the code has been written and/or why it's written the way it is.)

I note you've used MAIN:

sub MAIN($fromdir, $todir) {

As you know, this automatically generates a nice usage message. What you may not know is that this can be combined with the Pod declarator blocks discussed above to great effect. Checkout The Cool subset of MAIN for examples.

Final version

I've made many more changes to be the way I write things. As I wrote at the start, your original code looks nice and many coding issues are subjective.

use Text::Unidecode:from<Perl5>;

sub MAIN ($fromdir, $todir) {

  for $fromdir.dir {

    next unless .d;

    say my $tosubdir = "$todir/&transformdirname(.basename)";
    mkdir $tosubdir;

    for .dir {

      # make filename ASCII at all costs:
      my $tofile = "$tosubdir/{.basename.&Text::Unidecode::unidecode}";

      if $tofile.path.e { die "Will not overwrite $tofile" }

      copy $_, $tofile;

    }
  }
}

# undo URL encoding and make it a valid UNIX filename
sub transformdirname($_ is copy) {
  .=subst( /'%' (<xdigit>**2)/, { chr(:16(~$0)) }, :g )
  .=subst( /\//, '_', :g )
}


Unit Testing of parser method using pytest

I'm not familiar using py.test, but here are some comments I have after reading your code:

There are no comments - this would be helpful to the reader.
In text_parser, you have from tparse import TextParser. I would advise moving this import to the top of the file, for readability.
What is your code trying to do with these tests? What are you testing?


Using temporary file as backing object for a tarfile

You could try:

def __init__(self, path):
    self.tmp = tempfile.TemporaryFile()
    self.tarfile = tarfile.open( fileobj=self.tmp, mode='w:' )
    self.tarfile.add( path, '.' )
    self.tarfile.close()
    self.tmp.flush()
    self.tmp.seek(0)
    self.tarfile = tarfile.open( fileobj=self.tmp, mode='r:' )

tarfile() will take a fileobj in the constructor instead of a name, so as long as you don't close it, and instead just seek() to 0 when you want to re-read it, you should be good.


Listing employees whose employee number is divisible by some given number

Yes, there is.

AddEmployee.java:

private AddEmployees() {}

public static List<Employee> createListOfEmployees(long upperLimit) {
    return LongStream.range(0, upperLimit)
                     .mapToObj(Employee::new)
                     .collect(Collectors.toList());
}

public static List<Employee> getAllNumbersDividedByProvidedNumber(long upperLimit, int divideBy) {
    return createListOfEmployees(upperLimit).parallelStream()
            .filter(Employee -> Employee.getNumber() % divideBy == 0).collect(Collectors.toList());

}

Note that the constructor is declared private since it is useless in this context. Also, addListOfEmployees is not the best possible name; I used createListOfEmployees instead.

Also, since you are printing employees, it would be very nice if they have overriden the toString method.

All in all, I had this in mind:

Employee.java:

public class Employee {

    private final long number;

    public Employee() {
        this(0L);
    }

    public Employee(long number) {
        this.number = number;
    }

    public long getNumber() {
        return number;
    }

    public String toString() {
        return "[Employee ID: " + number + "]";
    }
}

AddEmployees.java:

import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

public class AddEmployees extends Employee {

    private AddEmployees() {}

    public static List<Employee> createListOfEmployees(long upperLimit) {
        return LongStream.range(0, upperLimit)
                         .mapToObj(Employee::new)
                         .collect(Collectors.toList());
    }

    public static List<Employee> getAllNumbersDividedByProvidedNumber(long upperLimit, int divideBy) {
        return createListOfEmployees(upperLimit).parallelStream()
                .filter(Employee -> Employee.getNumber() % divideBy == 0).collect(Collectors.toList());

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of employees: ");
        int numberOfEmployees = sc.nextInt();
        System.out.print("Enter the divisor: ");
        int divisor = sc.nextInt();

        getAllNumbersDividedByProvidedNumber(numberOfEmployees,
                                             divisor)
                .forEach(System.out::println);
    }
}

Additional advice

You can shorten your getEmployeeDividedBy a bit:

public static List<Employee> getEmployeeDividedBy(long upperLimit, long dividedby) {
    return LongStream.range(0, upperLimit)
                     .filter(l -> l % dividedby == 0)
                     .mapToObj(Employee::new)
                     .collect(Collectors.toList());
}

Note that I removed .collect(Collectors.toList()).stream() since it is not required. Also, I first filter, and map only after that since this prunes away some pointless computation.

Hope that helps.


When working with prices, then in my opinion it's usually a good idea to avoid floating point numbers. Binary rounding errors can easily strike unexpectedly any time.

Instead I'd suggest to work with integers (thus pennies) internally, and just add the decimal point for output.

An other unrelated point: I would move the hard-coded HTML in the script to the HTML document and only write the price.