Your instincts are basically right that maybe a dictionary of some sort could help. Let's look at some key lines of your code.

foreach (var l in saniLines)
{
    Line line = new Line(l, path, lineNo);
    string[] words = l.Split(' ');
    foreach (var word in words)
    {
        Word w = new Word();
        w.WordStr = word;

        if (Words.Contains(w, new WordComparer())) //Set already contains the word
        {
            Word wordToAdd = (from wors in Words where wors.WordStr.ToLower() == w.WordStr.ToLower() select wors).First();
            if (!wordToAdd.Lines.Contains(line))
                wordToAdd.Lines.Add(line);

I see 5 nested loops here. If you step through the code, you'll see them, too. The outer 2 are obvious, you've coded them explicitly. But stepping through the code will reveal the looping being done at Words.Contains. The Linq query is also an abstracted loop. And wordToAdd.Lines.Contains is also going to be a loop. You're going to pay a high price for these.

Profiling will always help. But I would suggest perhaps changing Words to some sort of IDictionary<string, Word>, where you can replace

if (Words.Contains(w, new WordComparer())) //Set already contains the word
{
    Word wordToAdd = (from wors in Words where wors.WordStr.ToLower() == w.WordStr.ToLower() select wors).First();

With something more like

if (Words.ContainsKey(w.WordStr))
{
    Word wordToAdd = Words[w.WordStr];
    // ...
}

And you could also change Lines into a HashSet<Line> instead of a list. That should improve the performance of

if (!wordToAdd.Lines.Contains(line))
    wordToAdd.Lines.Add(line);

In fact, HashSet<T>.Add(T val) could be used by itself, without the call to Contains, as Add returns a bool indicating if the addition could be performed. If a matching value is already in the set, it simply returns false without modifying its contents.

Try these changes, measure the performance before and after and see where you are.

Unrelated, but I also recommend that you wrap your StreamReader in a using () { } block so that the resource is properly disposed when you're through with it.


Thanks for providing the link, it helped a lot in understanding what you were after. I will provide two versions for you to consider. The first one is algorithmically similar to yours, except written a little more in the "R style". The second one will make a more efficient use of memory since that seems to be important too.

I will also assume that the input file in args[[1]] is like the one in the link, i.e. contains lines like the two below:

3 5 10
2 7 15

Version 1:

args <- commandArgs(trailingOnly = TRUE)
input.file <- args[[1]]
test.cases <- strsplit(readLines(input.file, warn=FALSE), ' ')
for (test in test.cases) {
   stopifnot(length(test) == 3L)
   XYN <- as.integer(test)
   X <- XYN[1]
   Y <- XYN[2]
   N <- XYN[3]
   i <- seq_len(N)
   is.divisible.by.X <- (i %% X) == 0L
   is.divisible.by.Y <- (i %% Y) == 0L
   out <- as.character(i)
   out[is.divisible.by.X & is.divisible.by.Y] <- "FB"
   out[is.divisible.by.X] <- "F"
   out[is.divisible.by.Y] <- "B"
   cat(out, sep = " ")
   cat("\n")
}

Some noticeable improvements are:

some assumption checking with stopifnot
the use of variable names that are closer to the problem write-up (X, Y, N) or more descriptive, e.g., is.divisible.by.X
the use of %% for finding if a number is divisible by X or Y
vectorization (notice how one of your for loops is gone), which should make your code faster
The use of seq_len(N) instead of 1:N is more robust in the case when N is zero.

Version 2:

args <- commandArgs(trailingOnly = TRUE)
input.file <- args[[1]]
file.handle <- file(input.file, open = "r")
repeat {
   line <- readLines(file.handle, n = 1)
   if (length(line) == 0L) break  # end of file
   test <- strsplit(line, " ")[[1]]
   XYN <- as.integer(test)
   X <- XYN[1]
   Y <- XYN[2]
   N <- XYN[3]
   i <- 1L
   while (i <= N) {
      is.divisible.by.X <- (i %% X) == 0L
      is.divisible.by.Y <- (i %% Y) == 0L
      out <- if (is.divisible.by.X & is.divisible.by.Y) "FB" else
             if (is.divisible.by.X) cat("F") else
             if (is.divisible.by.Y) cat("B") else as.character(i)
      cat(out, "")
      i <- i + 1L
   }
   cat("\n")
}
close(file.handle)

How is that different from version 1? Here we are careful to read the file and process it line-by-line instead of reading the whole file in memory. Also, when processing each line, we are careful not to generate the seq_len(N) vector in memory and pre-compute the output for all the values. Instead, we are counting from 1 to N and processing each value one at a time.

Because version 2 uses a while loop in place of the vectorization happening in version 1, it will be slower. But it will use a lot less memory, especially if the file contains a large number of rows or large values for N, both things could make version 1 fail.

I hope this helps. Let me know if you have questions.


Run different methods in background threads without duplication

Extending Thread is an 'anti-pattern' in Java. The right way to do this work is to create an instance of a Runnable, and use the runnable as a constructor to a Thread instance.

Thread t = new Thread(new Runnable() {
    public void run() {
        /*
         * Do something
         */
    }
});

t.start();

But, and this is a big "but", in Java, in general, the practice of creating and using threads like this is being replaced by frameworks that manage the threads in a better way. In regular Java, you should be using the Executor, ExecutorService, and Executors class in the concurrent package.

As for android-specific use cases, you should read up on the threading models in Android paying special attention to the AsyncTask in Android dev kit

Update: more about the Thread anti-pattern

Jon Skeet says so ... ;-)  and most people agree. What you have is something that needs to be done. That is what you need to implement, the way to do it. The place to run that something is on a thread. You do not implement the thread, you implement the task. There are two different concepts here: what needs to be done, and where it needs to be done. You are only implementing one of those.

Update: alternative solution

To reduce the logic required for managing the thread the code runs on, I would do one of two things:

simply always use a different thread. Simply remove the if-condition and do all the processing in a Runnable's run method.
Have a common utility method that looks something like:

private static final ExecutorService THREADPOOL = Executors.cachedThreadPool();

public static void runButNotOn(Runnable toRun, Thread notOn) {
    if (Thread.currentThread() == notOn) {
        THREADPOOL.submit(toRun);
    } else {
        toRun.run();
    }
}

and then you can call it in your method with:

private static void processImplementation(final Context context, final String itemId) {
     // ... the actual work.
}

public static void processItem(final Context context, final String itemId) {
    Runnable task = new Runnable(){
        public void run() {
            processImplementation(context, itemId);
        }
    };
    runButNotOn(task, Looper.getMainLooper().getThread());
}


Extension methods to get direct reports from user

Why are you ToListing your collections when you are returning IEnumerable<T>? Unless you have a good reason (rare) then leave them as IEnumerable

GetAsDirectoryEntry seems redundant - and in fact hides the fact that it could be null, whereas somebody calling as directly will know that null is possible. I note when you call it you do not test for null.

In general I prefer the lamba inputs as short names escpecially when the scope is so small, so do

   .Where( dn=>!dn.IsNullOrWhiteSpace())

instead of

   .Where(
        directReportDistinguishedName =>
            !directReportDistinguishedName.IsNullOrWhiteSpace())


Wrong Objects

You've made objects, but you haven't really made the right ones.  Your Fizz, Buzz, FizzBuzz, and Element classes are really just wrappers for a string, which is already a class.

Object oriented programming is not always the best solution for a problem.  FizzBuzz itself is frankly very trivial and almost any object based approach is a needlessly complicated solution.  With that said, perhaps we can redefine the problem to make it a better fit for an object oriented solution.

New problem definition

Consider this modified problem: As input you will receive a list.  Each row (a rule) will have one or more numbers (divisors) and a text substitution.  After parsing the list, for every number 1..100 you will either print the number or a substitution for the number.  For each rule, if a number is divisible by all of the divisors, you will use the substitution.  In the event of a tie, use the substitution from the rule with the most divisors.  If there is still a ties use the substitution for the rule with the smallest individual divisor.  For example, given "3 6 apple" and "2 9 banana" and "18 candle", they all tie for an input of 18, but "2 9 banana" has the smallest divisor, 2.

Example Input:
3 fizz
5 buzz
3 5 fizzbuzz
3 6 apple
2 9 banana
18 candle
2 3 4 dog

New Classes

Now there is opportunity for new classes, perhaps Parser that reads the rules/substitutions, Rule which contains a list of divisors, the substitution, and perhaps it supports the comparison operation <=> so that the rules can automatically resolve ties.  There could also be an Engine class for iterating through 1..100 and applying the rules.  There are a couple of different ways you could define the classes or even the problem here. In general though, OO solutions to trivial problems aren't a good fit.


Typesafe scanf-like function with variadic templates

So here's what I'm hoping to find out:
  Is this code idiomatic modern C++?

Pretty much.
You use recursion to handle the multiple arguments. But a better technique may be to use the compiler to build objects to handle each argument. Unfortunately the way you have to defined the interface makes this a bit hard.
If we re-consider the interface to make it like C-scanf.

template<typename T>
int parser(char const*& format, char const*& value, T& value)
{
     // Parse format and value.
     // convert the first % in format into value and save in value.
     // return 1 if value was correctly parsed otherwise 0
}

template<typename... Args)
int tscanf(char const* format, char const* value, Args&... args)
{
    // This will call parser() for each argument.
    // Each of the parameters inside {} are guaranteed to be constructed
    // left to right.
    // Because the compiler knows the number of arguments you
    // get a fixed size array that can be used to count the
    // number of parameters that were actually found.
    auto count[] = {parser(format, value, args)...};

    // Probably need a check for unmatched format parts.

    // The return the number of format parameters matched.
    return std::accumulate(std::begin(count), std::end(count), 0);
}

  Am I overlooking useful features in the standard library that would make things easier?

I don't think there is anything you absolutely need.

  Should I be using pointers in this way, or am I thinking too much in C?

NO. Absolutely do not use pointers like that.

Prefer to use references. This way you don't need to check for null. You also express your intent much more clearly so there is no confusion on the ownership of the pointer.

  Should I consider another approach entirely, like tscanf(format, paramname1, paramname2) >> param1 >> param2?

There have been a couple of attempts like this. boost::format springs to mind. They used % rather than >>. But personally I don't see a need for this. BUT big advancements have come from trying the non obvious (things I would not think of). So please experiment.


// package name should be lower case
package HorseRace;

public interface FinishingLine {

    public void arrive(Horse h);
}

package HorseRace;

import java.util.Vector;

public class FinishingLineImpl implements FinishingLine {

    // Vector is discouraged, unless you are using an ancient version of java
    // use Collections.synchronizedList(new ArrayList()) instead
    // variable should start with a lowercase letter, i.e. camelCase
    Vector<String> Ranking = new Vector<String>(10);

    public FinishingLineImpl(){}
    @Override
    public void arrive(Horse h) {
        // TODO Auto-generated method stub
        Ranking.add(h.getId());
    }

    // override toString instead
    public void print()
    {
        // magic numbers are evil; use ranking.size() instead
        for (int i = 0; i < 10 ; i++)
        {
            System.out.println("Place " + (i + 1)+ " is : " + Ranking.get(i));
        }
    }
}

package HorseRace;

public interface Horse {

    public void run();
    // change String to Object so you can use anything for the ID
    public String getId();
}

package HorseRace;
import java.util.Random;

public class HorseImpl implements Horse,Runnable {

    // see previous comment regarding camelCase
    int _distance;
    String _id;
    FinishingLine _f;

    public HorseImpl(String id, FinishingLine f)
    {
        _id = id;
        _distance = 0;
        _f = f;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        Random rand = new Random();

        int n = rand.nextInt(10) + 1;

        while (_distance < 100)
        {
            try
            {
                // why sleep?
                // why not create and increment a timeElapsed instance variable?
                // preferrably an AtomicInteger
                // then calculate ranking after the race, e.g. sort by elapsed time
                Thread.sleep(n);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                // break out?
            }
            _distance += 1;
        }

            _f.arrive(this);
    }

    @Override
    public String getId() {
        return _id;
    }
}

package HorseRace;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        // I suggest calculating nThreads as cpu count * 2
        // @see Runtime.getRuntime().availableProcessors();
        // please use descriptive variable names
        ExecutorService e = Executors.newFixedThreadPool(10);
        FinishingLineImpl f = new FinishingLineImpl();

        // another magic number
        for (int i = 0; i<10 ; i++)
        {
            System.out.println("Creating a horse with id = " + (i+1));
            // see comment regarding id type
            String strI = Integer.toString(i+1);
            HorseImpl h = new HorseImpl(strI,f);
            e.execute(h);
        }

        e.shutdown();
        // 16 hours? really?
        e.awaitTermination(60000, TimeUnit.SECONDS);

        // change to System.out.println(f);
        f.print();
    }
}


A basic Object Orientated REPL Implementation

Looks quite good to me! There's some things I think you could improve:

When a user hits Ctrl-C, the program flow is suddenly interrupted. The call to sys.exit should use 1 as a status code to indicate that something went wrong.
The REPL prompt is currently hardcoded, but you could easily change Reple's signature to allow a custom prompt:

class Reple(object):
    def __init__(self, evaluator=DEFAULT_EVALUATOR, prompt=PS1):
        self.prompt = prompt

On that note, PS1 is a bit ambiguous, maybe rename it to DEFAULT_PROMPT.
This is more of a design argument, but in my opinion it would make more sense to have run call evaluate, making evaluator superfluous; in pseudocode:

while true
    read from stdin
    evaluate(expression)

And in Python:

class Reple(object):
    ...

    def run(self):
        while 1:
            try:
                expression = input(self.prompt)
            except KeyboardInterrupt:
                sys.exit(1)
            else:
                self.evaluate(expression=expression)

    def evaluate(self, expression):
        raise NotImplementedError("This method must be implemented in a derived class")

You can use abc.ABCMeta together with abc.abstractmethod to provide 'truly' abstract methods:

import abc

class Reple(object, metaclass=abc.ABCMeta):
    ...

    @abc.abstractmethod
    def evaluate(self, expression):
        return

And here's a Python 2 version:

import abc

class Reple(object):
    __metaclass__ = abc.ABCMeta
    ...

    @abc.abstractmethod
    def evaluate(self, expression):
        return

You don't have any docstrings, so it's hard to tell what certain methods do or how they should be implemented by subclasses of Reple.


Implementations and analysis of my python login system (~80 lines)

When entering passwords, you may want to opt for using the getpass library. It allows you (if the console supports it) to hide passwords as they're being typed. You'd use getpass.getpass just as you are using input to ask for the password. The only difference is, the password won't show in the console as it's typed.

You're using recursion in get_password in order to ask the user again. This isn't a good idea. If the user fails too many times, your program can crash. Just use a loop:
def get_password():
    while True:
        password = input('Enter password: ')
        if len(password) >= 8:
            confirm_password = input('Confirm password: ')
            if password == confirm_password:
                credentials.append(password)
                return
            else:
                print('Password does not match with confirmation\n\n')
        else:
            print('Password too short (min 8 characters)\n\n')

I'm looping until correct input is entered, then I'm just returning from within the infinite loop.
And the same goes for the rest of your functions. Recursion is great when used properly, but it carries more problems than most other looping methods. Use it in cases where it's appropriate (like when the problem itself is recursive), not just for general looping.

add_credentials would be safer if it used a with block to close the file for you:
def add_credentials_to_database():
    credentials_file = os.path.join(database_path, f'{credentials[0]}.txt')
    with open(credentials_file, 'w') as file:
        file.write(credentials[1])
    print('Account created successfully\n\n')
    initialize()

Now, even if an error were to happen half-way through the function, there's a greater chance that the file would be closed. Making a practice of using with also prevents you from forgetting to add a call to file.close().

At the top you have:
credentials = []
database_path = "C:\\Users\\filip\\PycharmProjects\\database"
database = os.listdir(database_path)

A few things to note:

credentials shouldn't be a global here. Global mutable states (which credentials is) tend to complicate code and make it harder to understand. In this set up, I would probably make credentials a local variable of initialize, have get_password and the like to return the data instead of adding it to the list directly, and manually pass that data to add_credentials_to_database.

database_path is a module-level constant, and as such should be in upper-case like DATABASE_PATH according to PEP8.

Arguably database is also (effectively) constant, so it should also be in upper-case.

In initialize, I would get rid of the int conversion of choose_option. All the call to int is doing is allowing for a ValueError to be raised if the user enters in a dumb menu option like "a". Just use the raw string value and compare against it:
choose_option = input('0. Exit\n1. Login\n2. Sign up\n\nOption: ')
if choose_option == "1":
    . . .

If you actually needed it as an integer to do math on it or something, it would be different. It's simply an "indicator value" though, so the type of choose_option doesn't really matter, and keeping it as a string has less problems.

And my favorite projects that I still do whenever I learn a new language are:

A naïve, bruteforce prime number finder (and then maybe a sieve). Fairly basic.
Conway's Game of Life. Harder, and has the potential to turn into a large project.
A Mandelbrot Set image generator. Even harder, and usually becomes a larger project.


Turning an Integer into an array of ints

I don't think your algorithm can be improved in speed. I'd propose a different implementation, though, a more shorter recursive function:

def reversed_digits(num, base: 10)
  quotient, remainder = num.divmod(base)
  [remainder] + (quotient == 0 ? [] : reversed_digits(quotient, base: base))
end


Sticking to a working proxy generated by a rotating proxy script

Do not recurse if the iterative solution is readily available. In Python it is particularly very important: Python does not optimize tail recursion, and there is a serious chance to hit the stack limit.

For example, make_request should look like

while True:
    try:
        res = requests.get(url, proxies=proxy, timeout=10)
    except Exception:
        continue
    print("proxy used in requests:",proxy)
    if res.status_code!=200:
        continue
    soup = BeautifulSoup(res.text, "lxml")
    return [urljoin(base_url,item.get("href")) for item in soup.select(".summary .question-hyperlink")]

Similarly, start_script shall also be converted into a loop. As a side benefit, there would be no need for the very alarming usable_proxy and cbool globals.
You shall not blindly retry on res.status_code!=200. Some status codes (e.g. a 400 family) guarantee that you will get the same error over and over again, resulting in the infinite loop.

Ditto for exceptions.


By defining r = round . sqrt . fromIntegral, newBoard fits on the screen. positions doesn't appear to be used in newboard's first case.

Half the code disappears if we interpret Cell as ((Int, Int), Int).

newBoard's first case can be pushed one recursion call deeper, mapping [] to [] instead of [x] to the current right hand side.

[_] ++ _ should be simplified as _ : _.

positions is only used once, therefore I inline it.

type Cell = ((Int, Int), Int) -- (position, state)

genPositions :: Int -> [(Int, Int)]
genPositions x = [ (a,b) | a <- [0..(x-1)], b <- [0..(x-1)] ]

r = round . sqrt . fromIntegral

newBoard :: Int -> [Int] -> [Cell]
newBoard i [] = []
newBoard i (x : xs) = (genPositions (r i)!!(i - 1 - length xs), x) : newBoard i xs

Successive elements of the list returned by genPositions and xs are zipped together; zip captures this pattern. i is now not needed in its non-rooted form and I recommend changing the interface to take N as an argument instead. Non-square arguments can currently crash !! anyway. genPositions is only used once, therefore I inline it.

type Cell = ((Int, Int), Int) -- (position, state)

newBoard :: Int -> [Int] -> [Cell]
newBoard n = zip $ liftA2 (,) [0..n-1] [0..n-1]

For type Board = Array (Int, Int) Int, Data.Array allows newBoard n = listArray ((0,0),(n-1,n-1)).


There is no need for the default clause in Schedule.worker.
Memory side, it is better to create a time.Timer object and call Timer.Reset & Timer.Stop on it.
No need for the done variable, we can just return when we are done.

func (s *Schedule) worker(c chan Task, i int) {
    t := time.NewTimer(s.idle * time.Second)
    for {
        select {
        case task := <-c:
            task.Execute()
            if !t.Stop() {
                <-t.C
            }
            t.Reset(s.idle * time.Second)
        case <-t.C:
            s.Lock()
            close(c)
            delete(s.queues, i)
            s.Unlock()
            return
         }
    }
}

It is not advisable to close a channel from a receiver. Better to close the channel when you know that there are no more tasks and have the worker wait for that.

func (s *Schedule) worker(c chan Task, i int) {
    for task := range c {
        task.Execute()
    }
}


JavaScript function to get DOM elements by any attribute

querySelectorAll

First off, if you're only dealing with relatively modern browsers (basically anything above IE7), you can use querySelectorAll, which is the fastest and easiest method to go about this:

document.querySelectorAll('[' + attrib + ']');

Here's the fiddle: http://jsfiddle.net/rc6Pq/

Sizzle

If you're stuck having to support IE7 and below, then you might as well just include the Sizzle selector engine, since you're bound to be using some additional selectors in the future. Once you include the Sizzle script in your page, you could then just use it in a similar fashion to the native querySelectorAll:

Sizzle('[' + attrib + ']');

Here's the fiddle: http://jsfiddle.net/rc6Pq/1/

jQuery

If you're already using jQuery on the page, you don't have to use Sizzle separately, since jQuery has Sizzle incorporated within it. If that's the case, just use this:

$('[' + attrib + ']').get();

Here's the fiddle: http://jsfiddle.net/rc6Pq/2/


Phone number extraction with regular expressions

Interesting proposal! I could hardly read the expression that you've been working with, so I started from scratch and have built the following:

\D?(\d{0,3}?)\D{0,2}(\d{3})?\D{0,2}(\d{3})\D?(\d{4})$

I tried to take out some of the conditional and capturing groups to clear it up. So we can split this into four part:

\D?(\d{0,3}?)

Is there anything that isn't a number? it's probably a + or something so we'll make sure not to capture that. Then we look for a country code. You had only a check for a single digit country code, and if that's what you want, fine. However, know that some nations have codes more than one character in length.
\D{0,2}(\d{3})?

Is there a dash or a parenthesis? Or both? If so, count those out. Is there an area code? If so, capture it.
\D{0,2}(\d{3})

Same as the previous section, except we're expecting to find those 3 digits, as they're necessary.
\D?(\d{4})$

We expect only one non-digit character between the 3 digit and 4 digit sections. We need to pull the matching to the right with the $. This way we make sure to get the very last digit in the phone number.

I'm sure there are many ways to do this, and this is only my interpretation. Have you tried Googling this to see if there's an established expression?


Banking: Calculate the Capital Requirement to calculate Risk-Weighted Assets

The function name is very nice. The docstring adds nothing to that. Consider describing one or more arguments, especially if there are tricky details about their validity. Definitely add a URL describing the math approach.

Sorry, I didn't notice how the wikipedia page on Capital_requirement relates to the details of the formula.

Overall the code is clear. If you really wanted to delete the b1 temp variable for some reason you might write this two-liner:

return ((lgd * stat.norm.cdf(a1) - lgd * pd)
        * (1 + ((tenor - tsub) * madj)) / (1- madjcoeff * madj))

(I suppose lgd could be factored out, if desired.) In any event, the a1 temp definitely helps with readability, you'll want to hang on to that. Something to keep in mind is that subexpressions can raise exceptions. If you're concerned that might happen, it may be useful to break out several temp variables, each on its own line, so the stack trace will pinpoint where things went south.

tsub is a perfectly nice concise name to choose, but please add a comment:

tsub = 2.5  # tenor subtrahend

Not sure how "constant" these two are, but you might allow callers to change them:

def capital_requirement(r, lgd, pd, madj, tenor, madjcoeff=1.5, tsub=2.5):

It's pretty clear that stat.norm.ppf(0.999) is not implementing the .99 given in your formula.

You might validate r:

assert 0 < r < 1

If out-of-range values sneak in, be aware that math.sqrt() will raise ValueError, which you probably want, while ** 0.5 will return an imaginary root.


Well, here is how I'd write that subroutine. I thoroughly commented the code to explain my choices.

use strict;
use warnings;
use autodie;     # automatic error handling for "open" and other builtins
use IO::Handle;  # object oriented syntax for filehandles

# DEBUG is a compile-time constant initialized by an environment variable.
# If it is false, the debug statements will get optimized away.
use constant DEBUG => $ENV{DEBUG};

# the log file handle and debug file handle
# Here, I opened both to STDERR
open my $lfh, '>&', STDERR;
open my $dfh, '>&', STDERR;

# close the $lfh file handle regardless of how the script terminates.
END {
    no autodie;
    close $lfh or warn "Unable to close the log file handle: $!";
}

# the main part of our script
{
    my $filename = "config.txt";
    # "read_config_file" is a function that takes one argument and returns one value.
    my $config = read_config_file($filename);
    # if the check fails, exit this script with error code "1".
    # the default error code (zero) is considered a success.
    ui_check_config_ok(\*STDOUT, $config, $filename) or exit 1;

    # now we can do stuff with the $config, e.g.
    my $usr = $config->{username};
}

# Name your variables in snake_case, not with camelCase.
# This function *only* parses the config file.
# It does not interact with the user.
# Such separation of concern makes your code easier to maintain:
# every function should do one thing only.
sub read_config_file {
    # Subroutines can take an argument list.
    # We unpack it like this:
    my ($filename) = @_;

    $lfh->say("Reading config file $filename");
    DEBUG and $dfh->say("Opening config file $filename");

    my %known_keys = map { $_ => 1 } qw/
        username password subnet webserver imgFullSize imgThumSize
    /;

    # we wills store the config in this hash
    my %config;

    open my $fh, "<", $filename;

    # don't slurp the file, we loop over it line by line
    LINE:
    while (<$fh>) {
        # trim the line
        s{\A\s+}{};
        s{\s+\z}{};

        DEBUG and $dfh->say("Reading line: $_");
        if (/^#/) {
            DEBUG and $dfh->say("Line is comment, skipping.");
            next LINE;
        }

        # * We use the /x flag on regexes to be able to structure the regex with spaces
        #   they won't match, they're just decoration
        # * instead of assigning to an array, we assign to a list of scalars.
        #   If the "split" fails, the last one should be "undef"
        # * We only accept one "=" per line, and produce 2 fragments max. So
        #       foo = bar = baz
        #   will produce $key="foo", $value="bar = baz".
        my ($key, $value) = split m{\s* [=] \s*}x, $_, 2;
        if (not defined $value) {
            DEBUG and $dfh->say("Ignoring config error: no key = value pair found on line: $_");
            next LINE;
        }

        DEBUG and $dfh->say("Value $key:$value");
        $config{$key} = $value;

        # a sanity check – you don't do anything on unknown keys
        if (not exists $known_keys{$key}) {
            $lfh->say("The config provided the unknown key $key, ignoring.")
        }
    }

    # another sanity check – did the user forget to specify some properties?
    for my $key (keys %known_keys) {
        if (not exists $config{$key}) {
            $lfh->say("The config didn't provide a key $key, ignoring.")
        }
    }

    return \%config;
}

# another subroutine that provides the user interface to ask whether the data is correct
# It will return a truth value whether the config was OK.
sub ui_check_config_ok {
    # Here, we see an argument list with three values
    my ($ui_fh, $config, $filename) = @_;

    $ui_fh->say($_) for
        "*********************************",
        "*Config imported",
        "*********************************",
        "*username    :$config->{username}",
        "*password    :$config->{password}",
        "*subnet      :$config->{subnet}",
        "*webserver   :$config->{webserver}",
        "*imgFullSize :$config->{imgFullSize}",
        "*imgThumSize :$config->{imgThumSize}",
        "*********************************";
    $ui_fh->print("Is this above correct?[Y]:");
    if (<STDIN> =~ /^\s*Y/) {
        $lfh->say("The user pressed Y. Config is good.");
        return 1;
    }

    $lfh->say("Config file contains false information, please correct.");
    $lfh->say("Terminating script");
    $ui_fh->say("The script will now terminate.");
    $ui_fh->say("Please amend the file $filename and rerun the script.");
    $ui_fh->say("Press any key to terminate...");
    <>;
    return 0;
}

Note how indentation made this code much easier to read than the dump you provided. Please use indentation!

I actually do not generally use IO::Handle, but it makes stuff more obvious when having more than one file handle. The autodie module is a bit inflexible, but I highly encourage it when you are still new with Perl, as it provides sensible defaults. You do not manually have to close file handles if they are variables declared with my – they will get closed automatically, and it's highly unlikely that closing a file will fail (file handles are not only used for physical files, but also for other things like pipes, where the close can fail).


Sieve of Sundaram for Project Euler 7: Python implementation slower than C++ and R

TL;DR: Just use PyPy; it gets you to about 10x the time of C++. If you really want to use CPython, a lot of clever optimizations (not algorithm changes) gets you as fast as PyPy and then using Numpy gets you close to C++ (2x the time).

The first thing of note is that your Python code is broken:

    if m > len(marked):
        break

Remember that Python is 0-indexed. What about when m == len(marked)?

So that's the first thing to fix. Going from the top, I'd do the
translation so:

from math import log

n = 10001                              n <- 10001
maximum = n * (log(n) + log(log(n)))   max <- n*(log(n) + log(log(n)))
marked = [False] * int(maximum // 2)   marked <- vector(mode="logical", length=max/2)
for i in range(1, len(marked)+1):      for (i in 1:length(marked)) {
    for j in range(1, i+1):              for (j in 1:i) {
        m = i + j + 2*i*j                  m <- i + j + 2*i*j
        if m > len(marked): break          if (m > length(marked)) break
        marked[m-1] = True                 marked[m] <- T
                                         }
                                       }
count = 1                              count <- 1
for i in range(1, len(marked)+1):      for (i in 1:length(marked)) {
    if not marked[i-1]: count += 1       if (!marked[i]) count <- count + 1
    if count == n:                       if (count == n) {
        print(2*i + 1)                     print(2*i + 1)
        break                              break
                                         }
                                       }

This is as direct a mapping as possible; instead of changing the comparison
I just shifted the index when indexing. This isn't idiomatic, but it's direct.
It's largely the same as your code, but it's correct. This matters when we go to
larger N, where your code fails. It's also Python 3 compatible simply by using
print with brackets.

Let's time them:

$ time Rscript r.r
[1] 104743
Rscript r.r  1.59s user 0.78s system 99% cpu 2.375 total

$ time python2 p.py
104743
python2 p.py  12.88s user 0.00s system 100% cpu 12.873 total

$ time python3 p.py
104743
python3 p.py  0.16s user 0.00s system 98% cpu 0.163 total

$ # A faster, very compatible Python interpreter
$ time pypy p.py
104743
pypy p.py  0.04s user 0.01s system 98% cpu 0.051 total

$ time pypy3 p.py
104743
pypy3 p.py  0.05s user 0.01s system 99% cpu 0.054 total

So there we have it. Python is over an order of magnitude faster on 75% of
interpreters, and under an order of magnitude slower in the worst case...

But why is it so slow with python2? line_profiler is a good utility:

import line_profiler
profiler = line_profiler.LineProfiler()

def main():
    # ... the code ...

profiler.enable()
profiler.add_function(main)
main()
profiler.print_stats()

Giving:

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           def main():
     5         1          271    271.0      0.0      from math import log
     6
     7         1            1      1.0      0.0      n = 10001
     8         1           22     22.0      0.0      maximum = n * (log(n) + log(log(n)))
     9         1          373    373.0      0.0      marked = [False] * int(maximum // 2)
    10     57160        37122      0.6      0.3      for i in range(1, len(marked)+1):
    11    188523      8444200     44.8     62.9          for j in range(1, i+1):
    12    188355       118260      0.6      0.9              m = i + j + 2*i*j
    13    188355      4672922     24.8     34.8              if m > len(marked): break
    14    131364        75962      0.6      0.6              marked[m-1] = True
    15
    16
    17         1            1      1.0      0.0      count = 1
    18     52371        25618      0.5      0.2      for i in range(1, len(marked)+1):
    19     52371        28393      0.5      0.2          if not marked[i-1]: count += 1
    20     52371        26003      0.5      0.2          if count == n:
    21         1           60     60.0      0.0              print(2*i + 1)
    22         1          329    329.0      0.0              break

So our most likely offending line is:

11    188523      8444200     44.8     62.9          for j in range(1, i+1):

On Python 2 there is both range and xrange. Using

for i in range(...):

will generate a list of numbers and then loop over them, whereas

for i in xrange(...):

will just increment i as wanted each time. This is often much faster. So let's add:

# Set range to xrange on Python 2
try:
    range = xrange
except NameError:
    pass

to the top of the script and retime:

$ time python2 p.py
104743
python2 p.py  0.09s user 0.00s system 96% cpu 0.090 total

$ time python3 p.py
104743
python3 p.py  0.16s user 0.00s system 99% cpu 0.161 total

$ time pypy p.py
104743
pypy p.py  0.04s user 0.00s system 93% cpu 0.050 total

$ time pypy3 p.py
104743
pypy3 p.py  0.04s user 0.01s system 99% cpu 0.054 total

Yup. CPython is much improved.

So how would we make the code good? First you can transform the i+j+2ij
calculation to directly use a stepping range:

step = 2*i + 1
for m in range(i+step-1, i+i*step, step):
    if m >= len(marked):
      break

    marked[m] = True

Then you can just do a slice assignment:

for i in range(1, len(marked)+1):
    step = 2*i + 1
    marked[i+step-1::step] = [True] * len(marked[i+step-1::step])

and you can immediately see that you can stop when...

    if i+step-1 >= len(marked):
        break

This leads to calculating the bound in the for loop itself.
step = 2*i + 1, so i+step-1 = i+(2*i+1)-1 = 3*i, so we should
stop before len(marked)//3:

for i in range(1, len(marked)//3):
    step = 2*i + 1
    marked[i+step-1::step] = [True] * len(marked[i+step-1::step])

The count part can use enumerate:

count = 1
for i, mark in enumerate(marked, 1):
    if not mark:
        count += 1

    if count == n:
        print(2*i + 1)
        break

I suggest inverting the True/False in the array to make this simpler,
and then you can just do count += mark. Why? Well, it lets us use
itertools.accumulate on Python 3:

for i, count in enumerate(accumulate(marked), 1):
    if count+1 == n:
        print(2*i + 1)
        break

You might also think len(marked[i+step-1::step]) is wasteful, since
it's generating a new array each time: it is!
It's still really fast, but Jaime's solution is better... except it
involves risky math steps. How could we get the best of both?

Well, remember how in Python 3 range is a fancy "pseudo-list" and
doesn't build a real list? Python 3's range can be sliced, too.
We just need to build a range the same length as the list and slice
that instead. This is actually slower on CPython (the one invoked
as python3) for normal-sized lists because CPython's list slices
are really fast, but on PyPy the overheads of the range version
are optimized out:

from itertools import accumulate
from math import log

n = 1000001
maximum = n * (log(n) + log(log(n)))
marked = [True] * int(maximum // 2)

slice_proxy = range(len(marked))
for i in range(1, len(marked)//3):
    step = 2*i + 1
    marked[i+step-1::step] = [False] * len(slice_proxy[i+step-1::step])

for i, count in enumerate(accumulate(marked), 1):
    if count+1 == n:
        print(2*i + 1)
        break

(Note the much larger n)

Times without slice_proxy:

$ time python3 p.py
15485867
python3 p.py  5.57s user 0.01s system 100% cpu 5.576 total

$ time pypy3 p.py
15485867
pypy3 p.py  3.58s user 0.08s system 100% cpu 3.654 total

Times with slice_proxy:

$ time python3 p.py
15485867
python3 p.py  5.94s user 0.02s system 100% cpu 5.949 total

$ time pypy3 p.py
15485867
pypy3 p.py  2.52s user 0.06s system 100% cpu 2.585 total

Then just wrap the whole thing in a main function, because local
variables are faster than global ones:

from itertools import accumulate
from math import log

def main():
    # ...

main()

Times:

$ time python3 p.py
15485867
python3 p.py  4.74s user 0.02s system 100% cpu 4.754 total

$ time pypy3 p.py
15485867
pypy3 p.py  2.40s user 0.06s system 100% cpu 2.456 total

But wait! We dropped the end point!

for i in range(1, len(marked)//3):
    step  = 2*i + 1
    start = i+step-1
    stop  = 2*i*(i+1)
    marked[start:stop:step] = [False] * len(slice_proxy[start:stop:step])

This is important on PyPy; on CPython it's about the same speed:

$ time python3 p.py
15485867
python3 p.py  4.88s user 0.01s system 100% cpu 4.890 total

$ time pypy3 p.py
15485867
pypy3 p.py  1.44s user 0.02s system 99% cpu 1.461 total

It's not really faster on CPython because the number of things you do tends to be more important
than what you do, simply from interpreter overhead.

We know it's way faster on CPython, but how does this compare to the original version on PyPy
(once stuck inside a main function)?

from math import log

def main():
    n = 1000001
    maximum = n * (log(n) + log(log(n)))
    marked = [False] * int(maximum // 2)

    for i in range(1, len(marked)+1):
        for j in range(1, i+1):
            m = i + j + 2*i*j

            if m > len(marked):
              break

            marked[m-1] = True

    count = 1
    for i in range(1, len(marked)+1):
        if not marked[i-1]:
            count += 1

        if count == n:
            print(2*i + 1)
            break

main()

Times:

$ time pypy p.py
15485867
pypy p.py  0.83s user 0.02s system 99% cpu 0.851 total

$ time pypy3 p.py
15485867
pypy3 p.py  0.78s user 0.01s system 99% cpu 0.790 total

Interestingly, the original is significantly faster on PyPy. This implies
that unless you want a code base optimized for both CPython and PyPy, some
of these optimizations (like slice_proxy) just aren't needed.

But, eh, what if this isn't fast enough?

Eh?

Well, let's quickly draw the things we're setting. T are the parts we set. . is the part we would set
if not for the :end: optimization.

index:  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  i=1         T        .        .        .        .        .        .        .        .        .
  i=2                  T              T              .              .              .              .
  i=3                           T                    T                    T                    .
  i=4                                    T                          T                          T
  i=5                                             T                                T
  i=6                                                      T                                      T
  i=7                                                               T
  i=8                                                                        T
  i=9                                                                                T
  i=10                                                                                T
  i=11                                                                                T

We're doing i=1 then i=2 then i=3, etc. From i² ≈ len(marked) onwards, though, there aren't many
things set each run. We end up moving back-and-forth in the array a lot, spending more time "seeking" in
memory than actually setting items. Consider, though, this decomposition:

index:  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  i=1         1        2        3        4        5        6        7        8        9       10
  i=2                  1              2              3              4              5              6
  i=3                           1                    2                    3                    4
  i=4                                    1                          2                          3
  i=5                                             1                                2
  i=6                                                      1                                      2
  i=7                                                               1
  i=8                                                                        1
  i=9                                                                                1
  i=10                                                                                1
  i=11                                                                                1

For low numbers, 1, 2, 3, ... this hits a lot of those later values. We can then split the work into
an upper part:

  i=1         T        .        .        .        .        .        .        .        .        .
  i=2                  T              T              .              .              .              .
  i=3                           T                    T                    T                    .
  i=4                                    T                          T                          T
  i=5                                             T                                T

and a lower part:

  i=6                                                      1                                      2
  i=7                                                               1
  i=8                                                                        1
  i=9                                                                                1
  i=10                                                                                1
  i=11                                                                                1

This sets the same values, but does so in a different order.

Here's the code:

from itertools import accumulate
from math import log

def main():
    n = 1000001
    maximum = n * (log(n) + log(log(n)))
    marked = [True] * int(maximum // 2)

    slice_proxy = range(len(marked))
    for i in range(1, int(len(marked)**0.5)):
        step  = 2*i + 1
        start = 3*i
        stop  = 2*i*(i+1)
        marked[start:stop:step] = [False] * len(slice_proxy[start:stop:step])

    i += 1
    start_step  = 2*i + 1
    start_start = 3*i
    step = 3
    for start in range(start_start, len(marked), start_step):
        marked[start::step] = [False] * len(slice_proxy[start::step])
        step += 2

    for i, count in enumerate(accumulate(marked), 1):
        if count+1 == n:
            print(2*i + 1)
            break

main()

How fast is it?

$ time python3 p.py
15485867
python3 p.py  1.53s user 0.01s system 99% cpu 1.538 total

$ time pypy3 p.py
15485867
pypy3 p.py  1.26s user 0.06s system 100% cpu 1.319 total

CPython improves a lot with this, although PyPy doesn't really improve. This makes sense: you do a lot
fewer operations but each operation is more complicated. Since CPython has a really fast implementation
under the hood but a really slow interpreter, fewer operations is important. PyPy, however, is bound
by the size of the operations themselves.

Of course, for PyPy you'd want to modify the "original" code:

from math import log

def main():
    n = 1000001
    maximum = n * (log(n) + log(log(n)))
    marked = [True] * int(maximum // 2)

    for i in range(1, int(len(marked)**0.5)):
        step  = 2*i + 1
        start = 3*i
        stop  = 2*i*(i+1)

        if stop > len(marked):
            stop = len(marked)

        for m in range(start, stop, step):
            marked[m] = False

    i += 1
    start_step  = 2*i + 1
    start_start = 3*i
    step = 3
    for start in range(start_start, len(marked), start_step):
        for m in range(start, len(marked), step):
            marked[m] = False

        step += 2

    count = 1
    for i in range(1, len(marked)+1):
        if marked[i-1]:
            count += 1

        if count == n:
            print(2*i + 1)
            break

main()

Which gives

$ time pypy p.py
15485867
pypy p.py  0.65s user 0.02s system 99% cpu 0.672 total

$ time pypy3 p.py
15485867
pypy3 p.py  0.61s user 0.01s system 99% cpu 0.625 total

The difference is obviously much less, but it exists. This is probably
due to improvements in memory locality. It actually turns out that just
terminating the range with len(marked)//3 gets you almost to this
point, so the optimization, IMHO, isn't worth it.

Sadly, CPython still trails the first version on PyPy. Janne Karila gave a good suggestion in the comments for speeding up CPython. The key part is to use itertools (or filter) to apply a filter and islice to do the counting. The large advantage comes from the iteration and counting being done inside C routines.

I went with compress from Python 3.1 and Janne Karila's use of islice to get the wanted element:

primes = compress(range(3, int(maximum), 2), marked)
print(next(islice(primes, n-2, None)))

Timings are really good:

$ time python3 p.py
15485867
python3 p.py  0.58s user 0.01s system 99% cpu 0.598 total

This is (negligibly) faster than PyPy!

But, eh, but... but...

What if this isn't fast enough?

Eh?

Well, that's what Numpy is for. Numpy is Python's fast array library.

from math import log
import numpy

def main():

    n = 1000001
    maximum = n * (log(n) + log(log(n)))
    marked = numpy.ones(int(maximum // 2), dtype=bool)

    for i in range(1, int(len(marked)**0.5)):
        step  = 2*i + 1
        start = 3*i
        stop  = 2*i*(i+1)
        marked[start:stop:step] = False

    i += 1
    start_step  = 2*i + 1
    start_start = 3*i
    step = 3
    for start in range(start_start, len(marked), start_step):
        marked[start::step] = False
        step += 2

    accumulated = numpy.add.accumulate(marked)
    [where] = numpy.where(accumulated+1 == n)
    print(2 * where[0] + 3)

main()

This also works on Python 2. Unfortunately PyPy doesn't (yet)
have an easy Numpy port. Even so, the times:

$ time python3 p.py
15485867
python3 p.py  0.23s user 0.02s system 99% cpu 0.252 total

$ time python2 p.py
15485867
python2 p.py  0.17s user 0.02s system 98% cpu 0.196 total

C++ with this N takes:

$ g++ -O3 c.cpp -o c; time ./c
15485867
./c  0.06s user 0.00s system 96% cpu 0.065 total

Neat, but we're still not done. One slow part is

accumulated = numpy.add.accumulate(marked)
[where] = numpy.where(accumulated+1 == n)
print(2 * where[0] + 3)

It turns out that Numpy's count_nonzero is really fast, so let's try capitalizing on that with what amounts to a binary search. There are similarities to Quickselect:

# I can't think of a good name...
def count_nonzero_sorted(array, count):
    # We always assume the count is achievable
    if array.shape[0] == 1:
        return 0

    # Split the array in two
    midpoint = len(array) // 2
    first_half = array[:midpoint]
    second_half = array[midpoint:]

    first_counts = numpy.count_nonzero(first_half)

    # If the counts in the first half is greater
    # than the wanted index, check that half
    if first_counts >= count:
        return count_nonzero_sorted(first_half, count)

    # else check the other half and offset the result
    return midpoint + count_nonzero_sorted(second_half, count-first_counts)

This gives

# compress sieve
where = count_nonzero_sorted(marked, n-1)
print(2 * where + 3)

and timings of just

$ time python2 p.py
15485867
python2 p.py  0.10s user 0.01s system 97% cpu 0.116 total

$ time python3 p.py
15485867
python3 p.py  0.16s user 0.01s system 99% cpu 0.167 total

On Python 2 that's half the speed of C++! Vectorization is a wonderful thing.

It would be interesting how many of these optimizations can apply to R.
A preliminary guess puts the time required for the original R code to run as about 7 hours.

What should you take away from this?

PyPy is really, really fast. The original code (after the
bug is fixed) takes only 0.7-0.9 seconds, or a tenth the speed of C++.

PyPy thus ends up, by my guess, about 10000 times as fast
as the implementation of R that I have available (for this task).
Optimizing PyPy is possible but it's hard; the optimized PyPy was
only a little faster than the original PyPy code at 0.6-0.7s.
This is because the JIT gives you most optimizations for free.
CPython has sharp corners (range vs xrange) but Python 3 takes
some of these problems away by default. Once you handle this, CPython
is an order of magnitude faster than R on this code.
CPython can be forced to go fast if you know what you are doing.
When forced it became a touch faster than PyPy.
CPython is ridiculously fast when Numpy is used; getting to half the speed
of C++.

I thought to check how a traditional sieve compares against this:

import sys
from itertools import compress, islice
from math import log

def main():
    n = 1000001
    maximum = int(n * (log(n) + log(log(n))))
    maxidx = maximum//2
    sieve = [True] * maxidx # 2, 3, 5, 7... might be prime

    j = 0
    for i in range(1, int(maxidx**0.5)+1):
        j += 4*i

        if sieve[i]:
            step = 2*i + 1
            sieve[j::step] = [False] * -(-(maxidx-j) // step)

    # compress sieve
    print(next(islice(compress(range(1, maximum, 2), sieve), n-1, None)))

main()

and the PyPy version:

import sys
from itertools import compress, islice
from math import log

def main():
    n = 1000001
    maximum = int(n * (log(n) + log(log(n))))
    maxidx = maximum//2
    sieve = [True] * maxidx # 2, 3, 5, 7... might be prime

    j = 0
    for i in range(1, int(maxidx**0.5)+1):
        j += 4*i

        if sieve[i]:
            for k in range(j, maxidx, 2*i + 1):
                sieve[k] = False

    # compress sieve
    count = 1
    for i in range(1, maxidx):
        count += sieve[i]

        if count == n:
            print(2*i + 1)
            break

main()

and the Numpy version:

import numpy
from math import log

def count_nonzero_sorted(array, count):
    # We always assume the count is achievable
    if array.shape[0] == 1:
        return 0

    midpoint = len(array) // 2
    first_half = array[:midpoint]
    second_half = array[midpoint:]

    first_counts = numpy.count_nonzero(first_half)

    if first_counts >= count:
        return count_nonzero_sorted(first_half, count)

    return midpoint + count_nonzero_sorted(second_half, count-first_counts)

def main():
    n = 1000001
    maximum = int(n * (log(n) + log(log(n))))
    maxidx = maximum//2
    sieve = numpy.ones(maxidx, dtype=bool) # 2, 3, 5, 7... might be prime

    j = 0
    for i in range(1, int(maxidx**0.5)+1):
        j += 4*i

        if sieve[i]:
            sieve[j::2*i + 1] = False

    # compress sieve
    where = count_nonzero_sorted(sieve, n)
    print(2 * where + 1)

main()

Timings:

$ time python3 p.py
15485867
python3 p.py  0.40s user 0.01s system 99% cpu 0.415 total

$ time pypy3 p.py
15485867
pypy3 p.py  0.36s user 0.01s system 99% cpu 0.368 total

$ # Numpy versions
$ time python2 p.py
15485867
python2 p.py  0.07s user 0.01s system 97% cpu 0.075 total

$ time python3 p.py
15485867
python3 p.py  0.12s user 0.00s system 99% cpu 0.124 total

Sorry, Sieve of Sundaram, you're not that great after all. :P

And, heck, that Python 2 version is way too fast.

Note: I made a mistake previously in comparing vector<bool> to vector<int>; I should have compared it to vector<uint8_t> instead. It turns out that the difference is actually relatively minor (30-40%) and although significant it isn't nearly the factor-of-4 difference from before.


Try to avoid nested if and for loops
The structure of your code is a bunch of nested for and if-elif-else statements. Try to find a way to compact those by restructuring your code or logic. Some examples:
No need to use elif if else suffices.
if stones[i]==0:
    # code
elif stones[i]!=0:
    # more code

Can be rewritten as:
if stones[i]==0:
    # code
else:  # you can place a comment here like: stones[i]!=0
    # more code

Combine if statements with and or or:
if stat==i-1:
    if stones[i]==0:

Can be rewritten as:
if stat==i-1 and stones[i]==0:

No need to state and else if it does nothing:
else:
    pass

Can be omitted.
Try to use Python code style PEP8 and meaningfull variable naming
See PEP8. Some examples in your code that can be improved for consistent readability by other Pythonistas:
Use spaces around operators and comparisons. These (unrelated) lines:
num=len(stones)
range(i+1,num)
if j-stat>=k:
    pass

Becomes:
num = len(stones)  # spaces around '='
range(i+1, num)    # space after ',' in function arguments
if j - stat >= k:  # spaces around '-' and '>='
    pass

Use meaningfull function and variable names

solution() -> num_kids_crossing_river()
answer -> num_kids or simply kids
i -> current_stone
k -> max_leap
Etc.

Use docstrings and comments to clarify your code
Document your code. Place documentation below your function definition (a "docstring") like this:
def num_kids_crossing_river(stones, max_leap)
""" Return the number of kids that can pass the array of stones.
    <<some more text here; like in the text from the exercise>>

    Args:
        stones (list(int)): initial values on the stones.
        max_leap (int): maximum number of stones a kid can jump over/skip.

    Returns:
        int: the number of kids that can cross the river.

Use comments to document what you are doing, as far as it isn't clear from the code itself. Document the algorithm/idea and not the code itself.
For example:
# Maximum number of iterations is the maximum number on the stones
for count in range(max(stones)):
    etc.

Most importantly: rethink the problem
All of the above can be used to improve your code and coding style, but it does not really optimise your code. One important step in coding is to first think about what you are about to code and how to code it.
If you look at the example, you see that the numbers in the stone array decrease, until there are k or more zero's next to eachother.
So an other way to formulate the problem is: how many times can I subtract 1 from an array until I have k or more zero's next to eachother?
Now suppose k = 0 (no jumps). And stones = [5, 6, 7]. You can see/imagine that the number of kids that can pass is 5.
Now suppose k = 1 and the same stones. You can see/imagine that the number of kids that can pass is 6. The 5 becomes 0 first, but you can skip it. Next, the 6 becomes 0.
What we can do, is to iterate over the stones, and check each "window" of k stones. And check the maximum value in that window. After that number of steps, that window is all 0 and the game stops. So we need to find the window that first gets to 0.
So now we can rephrase the problem again, more in mathematical terms: what is the minimum value of the maximum value of all sequential windows of stones with length k?
Illustrated with the example give in the problem statement: k = 3; stones = [2, 4, 5, 3, 2, 1, 4, 2, 5, 1]
[2, 4, 5, 3, 2, 1, 4, 2, 5, 1]
+-------+                      # maximum = 5 for this window
   +-------+                   # maximum = 5 for this window
      +-------+                # maximum = 5 for this window
         +-------+             # maximum = 3 for this window
            +-------+          # maximum = 4 for this window
               +-------+       # maximum = 4 for this window
                  +-------+    # maximum = 5 for this window
                     +-------+ # maximum = 5 for this window

Now the minimum value of all those maximum values is 3, which is the answer.
You can see that the value of 3 exactly appears in the piece that goes 0 first and where the fourth kid is stuck.
Here, I give you two implementations. One uses basic Python concepts, like your code. The second uses the (intermediate level Python concept) list comprehensions.

def min_max_of_subsection(array, k):
    """ Returns the minimum value of the maximum value of each subsection of length k in array.

    Args:
        array (list): an array of values (can be anything that has an order)
        k (int): length of the sub section

    Returns:
        value from array
    """
    max_subsection = []
    for array_index in range(len(array) - k + 1):
       max_subsection.append(max(array[array_index:array_index + k]))
    return min(max_subsection)

def min_max_of_subsection2(array, k):
    """ Returns the minimum value of the maximum value of each subsection of length k in array.

    Args:
        array (list): an array of values (can be anything that has an order)
        k (int): length of the sub section

    Returns:
        value from array
    """
    return min(max(array[idx:idx+k]) for idx in range(len(array) - k + 1))

print(min_max_of_subsection([2, 4, 5, 3, 2, 1, 4, 2, 5, 1], 3))
print(min_max_of_subsection2([2, 4, 5, 3, 2, 1, 4, 2, 5, 1], 3))

Try it online!


Exercise to fetch data from JSON files using ES6 promises

There are lots of things you can do:

Use a parameterized function to create the XHR promises, since your three functions are identical other than what they're requesting
Use Promise.all, since none of the requests relies on information from previous requests and they can run in parallel
Move the JSON parsing into the functions retrieving the data, rather than making the consumer of them do it

And a bugfix, I think: The response text is this.responseText, not this.response, in the XHR load callback.

That would let us replace all of this:

const commentsPromise = new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.onload = function() {
        resolve(this.response);
    };
    xhr.onerror = reject;
    xhr.open('GET', root + 'comments');
    xhr.send();
});

const usersPromise = new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.onload = function() {
        resolve(this.response);
    };
    xhr.onerror = reject;
    xhr.open('GET', root + 'users');
    xhr.send();
});

const postsPromise = new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.onload = function() {
        resolve(this.response);
    };
    xhr.onerror = reject;
    xhr.open('GET', root + 'posts');
    xhr.send();
});

// Posts
postsPromise
    .then(data => {
        const posts = JSON.parse(data); // Contains our posts

        usersPromise
            .then(data => {
                const users = JSON.parse(data); // Contains our users
                commentsPromise
                    .then(data => {
                        const comments = JSON.parse(data);
                        displayPosts(posts, users, comments);
                    });
            });
    }).catch(error => { console.log(error); });

with this:

const requestJSON = objType => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.onload = function() {
            try {
                resolve(JSON.parse(this.responseText));
            }
            catch (e) {
                reject(e);
            }
        };
        xhr.onerror = reject;
        xhr.open('GET', root + objType);
        xhr.send();
    });
};

Promise.all([requestJSON('posts'), requestJSON('users'), requestJSON('comments')])
    .then(results => {
        displayPosts(...results);
    })
    .catch(error => {
        console.log(error);
    });

So shorter, simpler, and more centralized for debugging, not repeating yourself, etc.

If you want the requests done in series instead of in parallel even though they don't have to be, just change the Promise.all part to:

requestJSON('posts')
    .then(posts => requestJSON('users')
        .then(users => requestJSON('comments')
            .then(comments => {
                displayPosts(posts, users, comments);
            })
        )
    )
    .catch(error => {
        console.log(error);
    });

(I tried to keep that in line with your line break/indentation style...)

Final result (with parallel requests):

'use strict';
const root = 'https://jsonplaceholder.typicode.com/';
const postList = document.querySelector('#post-list');
console.clear();

const requestJSON = objType => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.onload = function() {
            try {
                resolve(JSON.parse(this.responseText));
            }
            catch (e) {
                reject(e);
            }
        };
        xhr.onerror = reject;
        xhr.open('GET', root + objType);
        xhr.send();
    });
};

Promise.all([requestJSON('posts'), requestJSON('users'), requestJSON('comments')])
    .then(results => {
        displayPosts(...results);
    })
    .catch(error => {
        console.log(error);
    });

// No changes from here on out...

function displayPosts(posts ,users, comments) {
    let output = '';
    posts.map(post => {
        output += `

 ${displayUserName(users, post)}

 ${post.title}

 ${post.body}

 ${displayCommentAmount(comments, post)} comments

        `;
    });
    postList.innerHTML = output;
}

function displayUserName(users, post) {
    return users.map(user => {
        if (user.id === post.userId) {
            return user.name;
        }
    }).join('');
};

function displayCommentAmount(comments, post) {
     const commentArray = comments.filter(comment => {
        return comment.postId === post.id;
    });
    return commentArray.length;
}


Suggested refactorings of this Ruby time-to-string method?

Courtesy of the clever helper method from this link:
https://stackoverflow.com/questions/4136248/how-to-generate-a-human-readable-time-range-using-ruby-on-rails

def humanize secs
  [[60, :seconds], [60, :minutes], [24, :hours], [1000, :days]].map{ |count, name|
    if secs > 0
      secs, n = secs.divmod(count)
      "#{n.to_i} #{name}"
    end
  }.compact.reverse.join(' ')
end

def time_to_alarm_in_words time
  #AN EMPTY STRING WE WILL BE ADDING TO
  time_in_words = ''

  #THE TIME TO THE ALARM IN SECONDS
  total_seconds = (time - Time.now).to_i

  time_in_words = humanize total_seconds

  #RETURN THE RESULT
  return time_in_words + " in the future"
end


Review the vimrc for a Python programmer

I like to add the following:

" Allow easy use of hidden buffers.
" This allows you to move away from a buffer without saving
set hidden

" Turn search highlighting on
set hlsearch

" Turn on spelling
" This auto spell checks comments not code (so very cool)
set spell

" tabstop:          Width of tab character
" expandtab:        When on uses space instead of tabs
" softtabstop:      Fine tunes the amount of white space to be added
" shiftwidth        Determines the amount of whitespace to add in normal mode
set tabstop     =4
set softtabstop =4
set shiftwidth  =4
set expandtab


Outlook VBA-Macro for to check mails concerning recipients and attachments

For multiple address, why not have a collection of emails?

Public Function GetSendToList () As Collection

    Dim emailList As Collection
    Set emailList = New Collection

    emailList.Add "main.clerk@smth.com"
    emailList.Add "deputy.clerk@smth.com"
    emailList.Add etc.

    Set GetSendToList = emailList

End Function

And then:

Dim emailList as Collection
Set emailList = GetSendToList

...
...
...

For i = 1 to emailList.Count
    emailToCheck = emailList(i)
    '/ code to check the email against your message
Next i

This means that you can:

Keep your hard-coded list away from your business logic, making both
cleaner and easier to find
Have just one place you go to to add/remove emails from your list, rather than wading through a sub to find it.
Have just one (validate email) loop, so you don't have to repeat your code for each one.

In my projects, I often have a separate module for "Manually-Set Values" but that's just personal preference.


you can merge

 var item_html="";

 item_html += "

	";

to

var item_html="

	";

and i also cant see the id "tablearea" in your html.
UPDATE:
by your fiddle link that tablearea is the id of a div and are you sure that you want to use $('#tablearea').append(item_html); because it will append to div everytimes whenever your ajax method called.

so better that you use

$('#tablearea').html(item_html);


First off, be sure you're compiling with optimizations enabled.

In multiply_matrices you don't need to explicitly fill your matrix with zeros, as the vector constructor will do that.

In the kuramoto_exe constructor, the member initializer list has the member variables in the wrong order.  They will be constructed in the order they are declared, so N will be constructed before coupling despite its appearing later in the list.  While not an issue here, it can be if there are dependencies among the member variables, and some compilers will issue a warning when you do this.

In your matrix multiply, accumulate the result to a local variable, then store the result in mult[i][j].  This avoids the potential of multiple index calculations.

When calculating the Jacobian, the diagonal members are the negative of the sum of the horizontal members.  You're doing twice as much work as you need to.  Accumulate the sum as you process the row (skipping the diagonal element) then store the diagonal element when you're done with the row (Jac[i][i] = -rowsum;).

When you're doing the initial reshaping (filling the Y matrix), it is probably better to put the i loop on the outside, so that your writes are sequential.  It is easier for the CPU to handle nonsequential reads then nonsequential writes.

Rather than using a vector of vectors, consider creating a matrix class that stores all elements contiguously (in one vector) and computes the appropriate index.  This avoids the double lookup you currently have.


To avoid conflict with any other libraries, wrap your code using the jQuery function, and additionally call jQuery.noConflict();

The only conflict you may have is if somebody else imported an object named jQuery into the global namespace.

Reference: http://docs.jquery.com/Using_jQuery_with_Other_Libraries

Example:

jQuery.noConflict();
jQuery(document).ready(function($){
 //This is a jQuery function
 $('.myClass');
});


Parsing data from XML using Ruby and Nokogiri

Notes:

Use functional Enumerable#map instead of imperative pattern obj = [] +  Enumerable#each + Array#<<.
Use Kernel#Hash to build a hash from its pairs. Note that this method is very ugly and some prefer a more OOP approach, in that case check Enumerable#mash from Facets.
Use nokogiri_node.text.
Arguments in blocks can be unpacked.

I'd write:

require 'nokogiri'
require 'facets'

def get_training_queue(xml_data)
  xml_data.xpath("//row").map do |skill_in_queue|
    skill_in_queue.attributes.mash do |name, attribute|
      [name.to_sym, attribute.text]
    end
  end
end

All those eachs and in-place updates show that you think in imperative terms instead of functional, check this wiki page.


The logic of trying to write or else log.Fatal is duplicated in the two ServeHTTP implementations. And the log message looks copy pasted, because it's the same for both the String and *Struct versions, for example:

func (s *Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if _, err := fmt.Fprint(w, s.Greeting, s.Punct, s.Who); err != nil {
        log.Fatal("Error in ServerHTTP of String.")
    }
}

You could extract the common logic to a function, for example:

func writeOrElseLogFatal(writer func() (int, error), msg string) {
    if _, err := writer(); err != nil {
        log.Fatalf("Error in ServeHTTP of %s", msg)
    }
}

And then call it like this, for example for *Struct:

func (s *Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    writer := func() (int, error) {
        return fmt.Fprint(w, s.Greeting, s.Punct, s.Who)
    }
    writeOrElseLogFatal(writer, "Struct")
}

Or you could make things simpler by not logging at all in these functions:

func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, s)
}

func (s *Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, s.Greeting, s.Punct, s.Who)
}


Not a performance suggestion, but you can make use of the fact that an empty collection is Falsey.

if len(s) == 0:

Is functionally the same as just:

if not s:

And similarly,

while len(s) > 0:

Can be just:

while s:

Relevant PEP entry (search for "For sequences" under the linked heading).


Working with group of checkboxes in MS Excel

Let's address your question first, then we'll talk about some other things that can be improved.

  This is a lot of work to be done as you need to create a subroutine for each checkbox control.

Well, yes and no. You do need to create an event routine for each control, but there's no reason to repeat all of that code over and over inside each of them. Extract the logic into a paramaterized method. Then, your event procedures will look something like this.

Private Sub CheckBox21_Click()
    HandleCheckBoxClick() 'we'll get to the parameters later
End Sub

Private Sub CheckBox22_Click()
    HandleCheckBoxClick()
End Sub

Which isn't really so much work, is it?

However, to make this work cleanly, we'll need to define a simple data structre and populate it with appropriate values for high, medium, and low. I've used a Type here, but it might be worth creating a full blown class for down the road.

Private Type TStatus
    Name As String
    Abbreviation As String
    Color As Long
End Type

You'll want to store your three different statuses at the module level and create them at an appropriate time. Perhaps on the sheet activate event. Otherwise, you'll need to create them on the fly each time the procedure is run. I'm not sure which way I'd go, but the important part is abstracting the data right now.

Private high As TStatus
Private medium As TStatus
Private low As TStatus

Private Sub InitStatusVariables()
    high.Name = "High"
    high.Abbreviation = "H"
    high.Color = RGB(217, 0, 0) 'Red

    medium.Name = "Medium"
    medium.Abbreviation = "M"
    medium.Color = RGB(255, 204, 0) 'Orange

    low.Name = "Low"
    low.Abbreviation = "L"
    low.Color = RGB(153, 204, 0) ' Green
End Sub

So now, we can go ahead and write that HandleCheckBoxClick method. We'll pass in the actual checkbox control along with our predefined true state and false state.

Private Sub HandleCheckBoxClick(cntrl As CheckBox, trueState As TStatus, falseState As TStatus)
    If cntrl.Value = True Then
        Range("i6", "m6").Value = trueState.Name
        Range("r3").Value = trueState.Abbreviation
        Range("i6", "m6").Interior.Color = trueState.Color
        Range("r3").Interior.Color = trueState.Color

    Else
        cntrl.Value = False
        Range("i6", "m6").Value = falseState.Name
        Range("r3").Value = falseState
        Range("i6", "m6").Interior.Color = falseState.Color
        Range("r3").Interior.Color = falseState.Color
    End If
End Sub

Awesome, but now the duplication inside of the method has become really obvious, but thankfully, easier to deal with. So let's refactor again.

Private Sub HandleCheckBoxClick(cntrl As CheckBox, trueState As TStatus, falseState As TStatus)

    Dim state As TStatus
    If cntrl.Value = True Then
        state = trueState
    Else
        cntrl.Value = False
        state = falseState
    End If

    Range("i6", "m6").Value = state.Name
    Range("r3").Value = state.Abbreviation
    Range("i6", "m6").Interior.Color = state.Color
    Range("r3").Interior.Color = state.Color

End Sub

Note that in your full code, you will likely need to also pass the appropriate ranges into this method as well. Also, I'm not sure if you need this line of code, so I've left it's equivalent in the above method.

Else
CheckBox22.Value = False

Why set the value of the checkbox to False if it's not True? Shouldn't it already be false? Or can the value of a checkbox be "null"? I can't remember, but it's probably worth a comment if you're doing this to make sure the value is false instead of Empty.

This feels like an abuse of the With statement to me.

With Me.CheckBox21
If .Value Then ClearGroup .GroupName, .Name
End With

Why the With statement here? I don't see a real need for it. Maybe it's personal preference, but I think this version of it is nicer.

If Me.CheckBox21.Value = True Then
    ClearGroup Me.Checkbox21.GroupName, Me.CheckBox21.Name
End If

Even better, instead of passing strings to the ClearGroup method, pass the whole CheckBox control.

 If Me.CheckBox21.Value = True Then ClearGroup Me.CheckBox21

There ya go. One line, nice and neat. It would also remove the need to check the type of the control within the method, because you already know it's a checkbox. It has to be, or you get a type mismatch error.

Your indentation is inconsistent. You do very well in some places, but very poorly in others.

Good!

    For Each ole In Me.OLEObjects
        If TypeName(ole.Object) = "CheckBox" Then
            If ole.Object.GroupName = sGroup And ole.Name <> sName Then
                ole.Object.Value = False
            End If
        End If
    Next ole

Bad!

Private Sub CheckBox21_Click()
    If CheckBox21.Value = True Then
    Range("i6", "m6").Value = "High"
    Range("r3").Value = "H"
    Range("i6", "m6").Interior.Color = RGB(217, 0, 0)
    Range("r3").Interior.Color = RGB(217, 0, 0)

    Else
    CheckBox21.Value = False
    Range("i6", "m6").Value = "Low"
    Range("r3").Value = "L"
    Range("i6", "m6").Interior.Color = RGB(153, 204, 0)
    Range("r3").Interior.Color = RGB(153, 204, 0)
    End If

End Sub

Everything inside of an If block should be indented one level.

This is a bit harsh, but I'm not sure how else to put it... Your control names suck. CheckBox21, CheckBox22, CheckBox23... It goes on and on and on doesn't it?

Take the time to give these meaningful names. Yes, it's going to take a while to name all of those checkboxes, but you'll thank yourself later, trust me.

Nifty little program you have there. I think you'll do well if you read up a bit about "Abstract Data Structures" and "Abstraction". All in all you write pretty neat and readable code. Those are good things.


Symlinking shared libraries from a platform-specific directory

Seems like you can do the if statement to set a variable to the first part of the path, then put all the file copies at the end after that.

(Warning: My bash is kind of bad. Haven't used it for a while. I think this is how it's done though... Might need quotes around the libpath assignments?)

# this is to get PNG and JPEG support working in PIL on Ubuntu
if [ -d /usr/lib/x86_64-linux-gnu ] ; then # 64 bit ubuntu
    libpath=/usr/lib/x86_64-linux-gnu
fi

if [ -d /usr/lib/i386-linux-gnu/ ] ; then # 32 bit ubuntu
    libpath=/usr/lib/i386-linux-gnu
fi

if [ -d /usr/lib64/ ] ; then # Redhat
    libpath=/usr/lib64
fi

sudo ln -s $libpath/libfreetype.so /usr/lib/
sudo ln -s $libpath/libz.so /usr/lib/
sudo ln -s $libpath/libjpeg.so /usr/lib/


head can never be NULL

ListNode *getNextElement(ListNode *head){
    while ((head != NULL)                      // This can't be NULL
                                               // because you test before calling.
          && (head->next != NULL)
          && (head->data == head->next->data)){
        head = head->next;
    }

    // If head can be NULL then you should also be checking here.
    // Otherwise this may fail.
    return head->next;
}

Small notes:

ListNode *deleteDuplicates(ListNode *head) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function

    // Replace with for(;;)
    for(ListNode* cur = head; cur != NULL; cur = cur->next){
        // Move declaration of next here.
        // It is not used outside the loop so why pollute
        ListNode *next = getNextElement(cur);

        // May want to make sure you don't leak.
        ListNode *old  = cur->next;
        while(old != next)
        {
            ListNode* toFree = old;  // you can probably put this logic in getNext()
            old = old->next;
            free(toFree);
        }
        cur->next = next;
    }
    return head;
}


Remove item after a specified item from a linked list

if(first == null) { return;}
    else {
        Node c = first;
        while(c!=null){
            if(c.item == x && c.next!=null) {
                c.next = c.next.next;
            }
            c = c.next;
        }
    }

This could be written more briefly as

    for (Node c = first; c != null; c = c.next) {
        if (c.item == x && c.next != null) {
            c.next = c.next.next;
            return;
        }
    }

You don't need the first if, as the code has the same effect without it.

A for loop better captures the iterative properties here.

If we return immediately after doing our task, we save some iterations.

Another alternative would be

    if (first == null) {
        return;
    }

    for (Node current = first; current.next != null; current = current.next) {
        if (current.item == needle) {
            current.next = current.next.next;
            return;
        }
    }

I would prefer not to put everything on the same line.  This would be even more true if there was an else.

Since we return, we don't need an else.  This has the same effect without it, and we save the extra indentation.

I find current more descriptive than c.  Not a huge deal for a looping variable.

Since we know that first is not null, we can go ahead and check the next.  This saves us an iteration if we're looking for the last item.  This also saves having to check next later.

I find needle (meaning something for which we're searching) more descriptive than x.  That's a common idiom, but you can say target, etc. if that doesn't work for you.

You might consider returning the value that you remove.

            double value = current.next.item;
            current.next = current.next.next;
            return value;

This is common with remove operations.  You have the value right there at the time.  And this saves having to traverse the list twice to get the item and then remove it.  Of course, perhaps your usage patterns never have you doing that.


Unit test for a function that adds users

A unit test is split in 3 steps. Arrange-Act-Assert. Usually, to make it clear what is being tested, you write as a comment in which step you're at.

[TestMethod]
public void TestAddUser()
{
    //Arrange
    AccountAccess accountActions = new AccountAccess();
    Account testUser = new Account("abe",1000m);

    //Act
    accountActions.Add(testUser);

    //Assert
    Account retrievedUser = accountActions.GetAccount("abe");
    Assert.AreEqual("abe", retrievedUser.name, "Added user does not have corret name: 'abe' - '" + retrievedUser.name + "'");
    Assert.AreEqual(1000m, retrievedUser.limit, "Added user does not have corret limit: '1000' - '" + retrievedUser.limit + "'");
}

Now, to make your test as good as possible, it needs to be as small as possible but still crystal clear about what you're testing.

What are your Assert testing? If the name and limits are equals.
What do you want your `Assert to test? That the same account that was added is returned. That both accounts are equal.

It's not a big difference, but what if one day the equality of an Account is based on an Id? You might need to refactor this test because the assertions would be false. Ex : If I add twice the same Account with the same name and limit, which is supposed to be the one that is returned?

What I mean is, your test should look like this :

[TestMethod]
public void AddUser()
{
    //Arrange
    AccountAccess accountActions = new AccountAccess();
    Account expected = new Account("abe",1000m);

    //Act
    accountActions.Add(new Account("abe",1000m));

    //Assert
    Account actual = accountActions.GetAccount("abe");
    Assert.AreEqual(expected, actual);
}

Now, this test might fail, if so, it's because your Account class doesn't override the Equal and GetHashCode methods. It probably should as they both  define the identity of your object.

Let's get back to my last example. What is different from your test?

Expected/Actual : This way, I know what are the expected results (I have a variable for that) and what was actually returned (I also have a variable for that!)
Only one assertion. It's a best practice, you can't always achieve one assertion, but that should be your goal. This way, it's easy for anyone to understand what your test... tests. It's now very clear I'm testing that it's the same account that is returned, because internally Assert.AreEqual will use the Equal property of your class. And it seems reasonable that the Account class should know if it's equal to another much better than your unit test.

Now, if you don't want to override the equals, there's another solution for you. Use of constants

Ex :

[TestMethod]
public void AddUser()
{
    //Arrange
    const string accountName = "abe";
    const decimal accountLimit = 1000m;
    AccountAccess accountActions = new AccountAccess();
    Account testUser = new Account(accountName,accountLimit);

    //Act
    accountActions.Add(testUser);

    //Assert
    Account retrievedUser = accountActions.GetAccount(accountName);
    Assert.AreEqual(accountName, retrievedUser.name);
    Assert.AreEqual(accountLimit, retrievedUser.limit);
}

This way, there's no confusion about "abe" being everywhere, heck, I don't even need to know what is inside the accountName variable, I just need to know it is equals to the retrieved one because I used it to retrieve the account. Actually, the use of constants should be heavily considered in my first example too!!

Note : The string messages, aren't necessary. The default assertion message for Assert.AreEqual will be very clear that the actual value isn't equal to the expected one. And your test name shouldn't include Test. You know it's a test, there's the attribute juste above :) (You should take a look at @Konrad's comment about naming!)


SpringBoot application to send notification via phone numbers on SMS

PhoneService class
In my opinion, the PhoneService should be converted to an interface exposing the methods.
public interface PhoneService {
   void sendNotificationByPhoneNumbers(String title, String message, List<String> phoneNumbers);
}

Then, the PhoneServiceImpl class can implement the interface PhoneService.
@Slf4j
@Service
public class PhoneServiceImpl implements PhoneService {

   @Value("${notificationService.url}")
   String url;

   @Override
   public void sendNotificationByPhoneNumbers(String title, String message, List<String> phoneNumbers) {
      PhoneContext phoneContext = new PhoneContext(new SendPhoneByPhoneNumbers(url));
      phoneContext.notify(title, message, phoneNumbers);
   }
}

Those’s changes will allow you to use inheritance and make the code easier to refactor if, you want to add any new phone services (new implementations). The old implementation was, in my opinion, a bit confusing and did not add any advantages that I can see.
Prefer Constructor-based Dependency Injection / Setter-based Dependency Injection over Field Injection
In my opinion, the Constructor Dependency Injection should be preferred over the Field Injection since it makes the testing easier and more natural (you can pass the mocks directly into the constructor) without using hardcore ways to inject the dependencies.
Also, even in Spring documentation, the Field Injection is not even mentioned as a way to inject and lots of people don’t recommend it.
Never expose the variables directly in classes
In my opinion, it's a bad habit to have public variables in classes, since you don’t control your own data. Try to hide them by adding the private / protected keyword before them and use getters / setters to change the state. This will give you more control over the class own data.


Get item from database by ID and get all items from database

First some general information. Your code currently solves a specific problem of the "object relational impedance mismatch" in a specific way. Consider to minimize the effort and complexity to handle this mismatch by using an object-relational mapping framework. Here is the thing: Every single step of code improvement code will lead towards an implementation of an more generalized object-relational mapper anyway. But that will be a hard way to go. Every answer given will be one of those:

Write your own OR-Mapper
Use an existing OR-Mapper
Develop your code towards an OR-Mapper
Ignore the problem through adding specific code that makes your solution more inflexible

I don't think that 1. or 2. will be covered here. I will ignore 4 because we are here to improve code. Therefore I will only provide some steps of 3.

The first step to remove redundancy is to identifiy the parts that need to be flexible. In your case you may need a new class of objects that holds following information:

table column name (unitid)
the type of the table column (NpgsqlDbType.Integer)
value to query (id)
the placeholder within the query (":id")

So what you have built then is something like a QueryParameter for a specific table (average_hdop).

Now you can generalize your parameter of the query-method:

private List<Tuple<double, DateTime>> GetCarHdops(List<QueryParameter> queryParams)

With this information you will be able to built your where-clause dynamically through string concatenation. This will be some effort and error prone. Furthermore you are currently restricted to one conjunction. You have to decide between "and" or "or". And make sure to omit the where-clause in the whole if the queryParams are empty.

The parameter will be passed to the method "QueryDatabase" as well. There you can built your "NpgsqlCommand" dynamically.

This is only a suggestion to develop the code in the "right" direction. As you already asked to avoid redundancy on this way you also gain flexibility.

The flexibility now ends at the level where you want to built queries with "and" and "or". If you want to do that you must have somethig like the Criteria-API in JPA (Java). But as I said I won't cover that in this answer.

One other thing you should think about: db.OpenConnection(); and db.CloseConnection(); are perfect candidates be extracted to a template. (see https://en.wikipedia.org/wiki/Template_method_pattern)


Reverse character order of substrings delineated by white space chars

First, NullReferenceException is used by the CLR, and it's the default exception for  nullity in most cases. The ArgumentNullException used to prevent NullReferenceException from thrown, and gives more meaningful exception to your code.
So, in your current code, if you pass a null, the thrown exception message would be something like
Object reference not set to an instance of an object

. This would not give you any hint on what's going on your code, so if you used
throw new ArgumentNullException(nameof(InitialValue));

this would give you something like :
Value cannot be null. (Parameter 'InitialValue')

isn't clearer to point out the issue ?
Another point is that you don't need to throw an exception all the time as a way to handle your code. Only throw exception when it's critical to the application. As exceptions like a Stop sign to the application, whenever it's thrown, it stops the application from running the code. if the code is related to other dependencies such as required class, extensions, databases, ..etc. Then, this code should throw an exception if it leaks the dependency requirements.
In your case, it's not critical to pass null, yes it would break the code, but also it's not required to stop the code just for this, as you can simply return null to the caller. Same thing if Empty string or Whitespace passed. because you
so if you do this :
if(string.IsNullOrWhitespace(value)) { return value; }

would be enough and better than thrown exception for this case.
For the methods ReverseOnlyWords and Reverse I don't see what was the reason behind splitting them?. It should be one method that does the process. having two methods is confusing, because it's required to us (or any developer) to read both methods to understand the logic and to know which to use ! of course, splitting methods can be useful if there are independent procedures inside the method that can be reused or simply outside the main scope of the method, but not in your current code.
Using StringBuilder is a good practice. However, you only need one, the other one is unnecessary. Also, you don't need ToString() when appending char in StringBuilder
this line :
if (c == ' ' || c == '.' || c == ',') { ... }

punctuations can't be handled like this, because if you keep it that way, you will miss other punctuations. if we assumed you'll process English context, then you only covered 3 out of 14 punctuations that I know of. What about other languages? So Instead, use the built in char.IsPunctuation which covers most of the punctuations that in UnicodeCategory.

Complexity? Would the complexity be? I feel like it is O(n) since it
will basically look at almost every char twice.

You're using 3 loops, so your complexity is \$O(N^2)\$. though, it can be simplified to two loops (one for the words, the other for the characters), and one StringBuilder is enough. However, the overall code can be written as follow :
public string Reverse(string value, char separator)
{
    if(string.IsNullOrEmpty(value)) { return value; } // just return the value, leave the handling to the caller.

    var words = value.Split(separator); // split it to words by spaces

    // initiate a string builder with a fixed size based on the original string size.
    // setting the capacity would avoid oversized allocation.
    var resultBuilder = new StringBuilder(value.Length);

    // iterate over words
    for(int x=0; x < words.Length; x++)
    {
        // store the tailing punctuation
        char? punctuation = null;
        // iterate over characters in reverse
        for(int c = words[x].Length - 1; c >= 0; c--)
        {
            var current = words[x][c];

            if(char.IsPunctuation(current))
            {
                if(c == 0) // for leading punctuation
                {
                    // get the first poistion of the current word
                    var index = resultBuilder.ToString().Length - (words[x].Length - 1);

                    // insert the leading punctuation to the first poition (its correct poistion)
                    resultBuilder.Insert(index, current);
                }
                else
                {
                    // store tailing punctuation to insert it afterward
                    punctuation = current;
                }

            }
            else
            {
                // everything else, just append
                resultBuilder.Append(current);
            }

      }

        if(punctuation != null)
        {
            // insert tailing punctuation
            resultBuilder.Append(punctuation);
            punctuation = null; //reset
        }

        resultBuilder.Append(separator);
    }

    return resultBuilder.ToString();
}

this is just a revision of your code in its simplest form possible (at least to my knowledge) and it's one way process (process each word and character once). Using pointers would not increase the overall performance to the point where it would be recommended!. Array.Reverse also can be used, but still might add more memory allocation and slow down the performance specially with large strings.
Other missing part that needs to be counted when dealing with strings is Unicode which in some cases invalidate the results if not handled correctly.
UPDATE :
Here is another version that uses one loop, and two StringBuilder (one stores the results, and one for the processing).
public static string ReverseString(string value , char separator)
{
    if(string.IsNullOrEmpty(value)) { return value; }

    var tempBuilder = new StringBuilder(value.Length);

    var resultBuilder = new StringBuilder(value.Length);

    var isCompleted = false;

    for(int x = 0, index = value.Length - 1; index >= 0; index--, x++)
    {
        var current = value[index];

        if(current == separator)
        {
            isCompleted = true;
        }
        else
        {
            tempBuilder.Append(current);

            if(index == 0)
            {
                isCompleted = true;
            }
        }

        if(isCompleted)
        {
            // handle the lead and tail punctuations
            if(char.IsPunctuation(tempBuilder[0]) && char.IsPunctuation(tempBuilder[tempBuilder.Length - 1]))
            {
                var tail = tempBuilder[0];
                var lead = tempBuilder[tempBuilder.Length - 1];
                tempBuilder.Remove(0 , 1);
                tempBuilder.Remove(tempBuilder.Length - 1 , 1);
                tempBuilder.Insert(0 , lead);
                tempBuilder.Append(tail);
            }
            else if(char.IsPunctuation(tempBuilder[0]))
            {
                tempBuilder.Append(tempBuilder[0]);
                tempBuilder.Remove(0 , 1);
            }
            else if(char.IsPunctuation(tempBuilder[tempBuilder.Length - 1]))
            {
                tempBuilder.Insert(0 , tempBuilder[0]);
                tempBuilder.Remove(tempBuilder.Length - 1 , 1);
            }

            //store the results
            resultBuilder.Insert(0 , separator);
            resultBuilder.Insert(0 , tempBuilder);

            //reset
            tempBuilder.Clear();
            x = 0;
            isCompleted = false;

        }
    }

    return resultBuilder.ToString();
}

it may need more work, but I thought it would be useful to share it. It still lakes the Unicode handling though.


Memory efficient way to merge multiple Pandas DataFrames

The issue was resolved, there was a duplication in keys in more than two DF, which was leading to memory usage increase.
Thanks.