I see these areas where your code could be improved:

processFiles can be expressed very simply using mapM_ from Control.Monad:

processFiles :: String -> [String] -> IO ()
processFiles pattern = mapM_ (processFile pattern)

All your functions are in the IO monad. This goes a bit against Haskell's philosophy to keep side effects to minimum.
parseLines requires the whole file to be read into the memory. This could be solved by using lazy IO, but I'd strongly discourage you from doing so.

One possibility to solve 2. and 3. is to use conduits. This may seem as somewhat complex subject, but the idea is actually very intuitive. A conduit is something that reads input a produces output, using some particular monad. This allows to break your program into very small, reusable components, each doing a single particular task. This makes it easier to debug, test and maintain.

For example, your code could be refactored as follows. (First some required imports.)

import Control.Monad
import Control.Monad.IO.Class
import Data.ByteString (unpack)
import Data.Conduit
import qualified Data.Conduit.Binary as C
import qualified Data.Conduit.List as C
import Data.List (isInfixOf)
import System.Environment (getArgs)
import System.Directory (doesFileExist)
import System.IO

sourceFileLines :: (MonadResource m) => FilePath -> Source m String
sourceFileLines file = bracketP (openFile file ReadMode) hClose loop
  where
    loop h = do
        eof <- liftIO (hIsEOF h)
        unless eof (liftIO (hGetLine h) >>= yield >> loop h)

This function takes a file name and creates a Source - a conduit that takes no input, but produces output. It reads a file line by line and sends each line down the pipeline using yield. Using bracketP we ensure that the file will get closed no matter what happens to the pipeline.

numLines :: (Monad m) => Conduit a m (Int, a)
numLines = C.scanl step 1
  where
    step x n = (n + 1, (n, x))

This component built using scanl is very simple. It just sends its input to the output, and keeps the count along the way. Notice that this conduit doesn't need any IO, it works with any monad.

Now it's easy to filter a stream of numbered lines with a pattern:

parseLines :: (Monad m) => String -> Conduit String m (Int, String)
parseLines pattern = numLines =$= C.filter f
  where
    f (_, x) = isInfixOf pattern x

This function fuses two conduits together. The first one numbers lines, the second filters them according to the pattern.

printMatch :: (MonadIO m) => Sink (Int, String) m ()
printMatch = C.mapM_ (\(n, x) -> liftIO $ putStrLn $ show n ++ ": " ++ x)

In printMatch we separate the logic that prints out the output. For each pair it receives it prints the line number and its content.

Combining and running these conduits is then easy:

runResourceT $ sourceFileLines file $= parseLines pattern $$ printMatch

(runResourceT is needed because of bracketP.) So the rest of the program would look like

processFile :: String -> String -> IO ()
processFile _ [] = return ()
processFile pattern file = do
    exists <- doesFileExist file
    if not exists
        then putStrLn $ file ++ ": file does not exists"
        else do
            putStrLn file
            runResourceT $ sourceFileLines file $= parseLines pattern $$ printMatch

processFiles :: String -> [String] -> IO ()
processFiles pattern = mapM_ (processFile pattern)

main = do
    args <- getArgs
    processFiles (head args) (tail args)


C function to capitalize first word in a sentence, handles arbitrary spaces

Declare the toupper function
The easiest way to provide a prototype is
#include <ctype.h>

Consider matching all whitespace
At present, we don't capitalize a word at the beginning of a line.  We could deal with this by using isspace() instead of testing for equality with ' '.  Also, we might want to consider a word to start when a letter immediately follows punctuation (such as ", / or -).
Bug: if the string ends with a space
Consider the string "  " and walk through.
We loop while (*s && *s == ' ') until s points at the terminating NUL character.  Now we upcase that NUL (which is harmless), and increment s again.  That's not good.
We need
 if (!*s) { break; }

before line 6.
An alternative approach
Instead of changing between two different loops for whitespace and non-whitespace runs, we can write a single loop if we use a variable to remember what we've most recently seen; I'll call it prev.  Start off with it set to a space, so that we'll capitalise the first letter of the string.  The code then looks like this:
#include <ctype.h>

void capitalize_string(char *s)
{
    for (int prev = ' ';  *s;  ++s) {
        int current = (unsigned char)*s;
        if (!isalnum(prev) && isalpha(current)) {
            *s = (char)toupper(current);
        }
        prev = current;
    }
}

Note that the <ctype.h> functions all accept (and return) an int that represents an unsigned char version of a character - on platforms where plain char is signed, passing one directly can be undefined behaviour.
Here's a short test program so we can give command-line arguments to try out:
#include <stdio.h>
int main(int argc, char **argv)
{
    for (int i = 1;  i < argc;  ++i) {
        capitalize_string(argv[i]);
        printf("%s\n", argv[i]);
    }
}

A more sophisticated test allows us to check a suite of examples every time we make a change to the code, and allows us to be explicit about our expectations:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check(const char *input, const char *expected)
{
    char *s = malloc(strlen(input)+1);
    if (!s) {
        perror("malloc");
        return 1;
    }
    strcpy(s, input);

    int err_count = 0;
    capitalize_string(s);
    if (strcmp(s, expected)) {
        fprintf(stderr, "*** Input \"%s\"; expected \"%s\"; got \"%s\"\n",
                input, expected, s);
        ++err_count;
    }

    free(s);
    return err_count;
}

int main()
{
    return check("", "")
        +  check("123", "123")
        +  check("ab", "Ab")
        +  check("AB", "AB")
        +  check("ab cd", "Ab Cd")
        +  /* add more tests here */
        +  check("harley-davidson", "Harley-Davidson")
        ;
}


If you're truly only looking for images, why not parse any src attributes you can find? As it stands, all of the provided Regular Expressions will fail on local images, as well as on dynamically generated images, such as:

<img src="/local/images/get_profile_pic.php?id=12345" title="John Doe" />

This is perfectly valid, and none of the other expressions would capture them. Generally, for parsing HTML, you're better of traversing it with something like an XML reader, or a DOM builder, depending on how well the HTML is formed.

Ben Nadel has a pretty good blog article on working with badly formed HTML and translating into an XML document. Once you have an XML document, you can unleash the power of XPath to do some very nice searching.

If, however, you just want a quick and dirty RegEx to get the job done, I'd recommend actually using the underlying Java Regular Expression library, as it's probably more efficient when matching multiple items over a large document.

The main RegEx I'll be using is as follows:

<img\s+[^>]*?src=("|')([^"']+)\1

Which looks for the src attribute in img tags. You can vary this as you see fit.

<cfset html = variables.getDocument /> <!--- your HTML --->
<cfset pattern = CreateObject("java","java.util.regex.Pattern").compile('(?i)<img\s+[^>]*?src=("|')([^"']+)\1') />
<cfset matcher = pattern.matcher(html) />

<!--- loop through the matches --->
<cfloop condition="matcher.find()">
    <cfset src = matcher.group(2) />
</cfloop>

UPDATE :
Here's an explanation of the pattern I chose:

<img           - Literal string "<img", match the opening tag
\s+            - Match one or more whitespace characters, so <img\t is valid
[^>]*?         - Lazily match any character that is not a '>' while looking for the next literal string
src=           - Literal string "src="
("|')          - Match either a single or a double quote, both are valid in HTML
([^"']+)       - Match anything that isn't a single or double quote. Note: You *could* use [^\1] here, however this way the match will reject malformed HTML attributes that have mismatched quotes
\1             - Match the value of the first group (either a single or double quote)


Calculating the potential of a matrix of size (424600, 55)

Your code loops N x M x N times, where N is currently 424600 and M is 55. I don't think it's going to finish.

I tried a couple of variations of your code, and the results are below. The version marked "original" is pretty true to your original code, except that I returned the results instead of storing them in a file. (So I could compare them against the original version.)

The version marked "fortran" is an attempt to change the internal arrangement of data in the array, to try to get better cache performance out of consecutive reads. It didn't work, but I may not have done it right, or I maybe should have done it on the numpyier version. (It's late, I'm tired, sorry.)

The "cached" version is the original code, but I tried to cache everything I could to eliminate repeated reads of the same items. I also tried to eliminate repeated sub-expressions, and I eliminated the sigma variable, since it was 1 and never changed.

The "numpyier" version uses numpy operations in the inner loop. There may be a way to do the same thing with the columns, doing a 2-d computation and eliminating the middle loop as well. Someone better at numpy than I am may be able to provide that. At any rate, this version does not produce quite the same results, but they're close and it runs a lot faster.

import sys
import csv
from timeit import timeit

import numpy as np
#from normalization import normalized_matrix
#MAKE_ROWS = 424600
MAKE_ROWS = 424
MAKE_COLS = 55
normalized_matrix = np.random.rand(MAKE_ROWS, MAKE_COLS)

def do_original():
    print(f"Original method", file=sys.stderr)
    n = len(normalized_matrix)
    sigma = 1
    #potential_writer = csv.writer(open('pot_orig.csv', 'w'))
    writer = []

    for l in range(n):
        elem = []
        for j in range(len(normalized_matrix[0])):
            GWF = 0
            PotSecPart = 0
            for i in range(n):
                if (i!=l):
                    PotSecPart = (PotSecPart + (((normalized_matrix[l][j] - normalized_matrix[i][j])**2)*(np.exp((-(normalized_matrix[l][j] - normalized_matrix[i][j])**2)/(2*(sigma**2))))))
                    GWF = GWF + (np.exp((-(normalized_matrix[l][j] - normalized_matrix[i][j])**2)/(2*(sigma**2))))
                    PotFirPart = (2*(sigma**2))* GWF
            elem.append(np.round(PotSecPart/PotFirPart, 2))
        #potential_writer.writerows([elem])
        writer.append(elem)
    return writer

def do_cached():
    print(f"Cached method", file=sys.stderr)
    num_rows = len(normalized_matrix)
    num_cols = len(normalized_matrix[0])

    sigma = 1
    #potential_writer = csv.writer(open('pot_orig.csv', 'w'))
    writer = []

    exp = np.exp

    for l in range(num_rows):
        elem = []
        for j in range(num_cols):
            GWF = 0
            PotSecPart = 0

            nm_lj = normalized_matrix[l][j]

            for i in range(num_rows):
                if i != l:
                    nm_ij = normalized_matrix[i][j]
                    diff_sq = (nm_lj - nm_ij) ** 2
                    exp_diff_sq_2 = exp(-diff_sq/2)

                    PotSecPart += diff_sq * exp_diff_sq_2
                    GWF += exp_diff_sq_2
                    PotFirPart = 2 * GWF
            elem.append(np.round(PotSecPart/PotFirPart, 2))
        #potential_writer.writerows([elem])
        writer.append(elem)
    return writer

def do_fortran():
    print(f"Fortran (cached) method", file=sys.stderr)
    matrix = normalized_matrix
    shape = matrix.shape
    matrix = np.reshape(matrix, shape, order='F')
    num_rows, num_cols = shape

    sigma = 1
    #potential_writer = csv.writer(open('pot_orig.csv', 'w'))
    writer = []

    exp = np.exp

    for l in range(num_rows):
        elem = []
        for j in range(num_cols):
            GWF = 0
            PotSecPart = 0

            nm_lj = matrix[l][j]

            for i in range(num_rows):
                if i != l:
                    nm_ij = matrix[i][j]
                    diff_sq = (nm_lj - nm_ij) ** 2
                    exp_diff_sq_2 = exp(-diff_sq/2)

                    PotSecPart += diff_sq * exp_diff_sq_2
                    GWF += exp_diff_sq_2
                    PotFirPart = 2 * GWF
            elem.append(np.round(PotSecPart/PotFirPart, 2))
        #potential_writer.writerows([elem])
        writer.append(elem)
    return writer

def do_numpyier():
    print(f"Numpy-ier method", file=sys.stderr)
    matrix = normalized_matrix
    num_rows, num_cols = normalized_matrix.shape

    sigma = 1
    #potential_writer = csv.writer(open('pot_orig.csv', 'w'))
    writer = []

    exp = np.exp

    for l in range(num_rows):
        elem = []
        for j in range(num_cols):
            GWF = 0
            PotSecPart = 0

            nm_lj = normalized_matrix[l][j]

            diff_sq = (nm_lj - matrix[:, j]) ** 2
            np.delete(diff_sq, l)
            exp_diff_sq_2 = exp(-diff_sq / 2)
            np.delete(exp_diff_sq_2, l)

            PotSecPart = np.sum(diff_sq * exp_diff_sq_2)
            GWF = np.sum(exp_diff_sq_2)
            PotFirPart = 2 * GWF

            elem.append(np.round(PotSecPart/PotFirPart, 2))
        #potential_writer.writerows([elem])
        writer.append(elem)
    return writer

if __name__ == '__main__':
    #orig = do_original()
    #print(timeit('do_original()', globals=globals(), number=1))
    #cached = do_cached()
    #assert cached == orig
    print(timeit('do_cached()', globals=globals(), number=1))
    #fortran = do_fortran()
    #assert fortran == orig
    #print(timeit('do_fortran()', globals=globals(), number=1))
    #numpyier = do_numpyier()
    #assert numpyier == cached
    print(timeit('do_numpyier()', globals=globals(), number=1))

The cached version was a little better than 2x faster than the original version, when I ran it on a 42x55 array. After a few passes, I stopped running the original.

The fortran version surprised me, because I expected to see better performance. Its results were consistently a smidge faster (.01-.02 sec) than the cached version, but that's it.

The numpyier version uses numpy operations to do the individual element calculations. This effectively replaces the inner loop with C.

Please Note: The numpyier version gets different results than your python-only version. The differences are typically +/- 0.01 in your rounded output. But they're different, and I haven't looked into why.

I did most of my work at a shape of 42 x 55. That was in the 3-second range for me, about what I was willing to run repeatedly. After I got the numpyier version sort-of working, and satisfied myself that the differences were small, I ran it again with a shape of 424 x 55. That's 10 times bigger, which means 100x slower since this is an \$n^2\$ operation. The results were:

Cached method
114.699519728
Numpy-ier method
4.552203466999998

This seems intuitively correct: the numpy-ier version eliminates the python inner loop in favor of C looping operations, reducing the runtime by a factor of 424. The results are 23x faster, which is on the order of 100x.

Note that this suggests that increasing the shape by a factor of 1000 (424600 instead of 424) would increase the runtime by somewhere between 1,000x and 1,000,000x. The numpyier version would require somewhere between 4000 seconds (1 hr, 20 mins) and 4,000,000 seconds (50 days).

I am working on a 10+ year old, 32-bit laptop. Your mileage is very likely going to be better.


Your solution looks ok to me. But let's understand better how this sort method actually works.

The insertion sort, works as it follows: always maintains a sorted sublist in the lower positions of the list. Each new item is then "inserted" back into the previous sublist such that the sorted sublist is one item larger. See below how insertion sorting process works. The shaded items represent the ordered sublists as the algorithm makes each pass.

(source: interactivepython.org)

We begin by assuming that a list with one item (position 00) is already sorted. On each pass, one for each item 1 through n−1, the current item is checked against those in the already sorted sublist. As we look back into the already sorted sublist, we shift those items that are greater to the right. When we reach a smaller item or the end of the sublist, the current item can be inserted.

(source: interactivepython.org)

Above, a sorted sublist of five items consisting of 17, 26, 54, 77, and 93 exists. We want to insert 31 back into the already sorted items. The first comparison against 93 causes 93 to be shifted to the right. 77 and 54 are also shifted. When the item 26 is encountered, the shifting process stops and 31 is placed in the open position. Now we have a sorted sublist of six items.

With this being said, I'd go with this implementation:

def insertionSort(lst):
    for index in range(1, len(lst)):

        currentvalue = lst[index]
        position = index

        while position > 0 and lst[position - 1] > currentvalue:
            lst[position] = lst[position - 1]
            position = position - 1

        lst[position] = currentvalue

lst = [54, 26, 93, 17, 77, 31, 44, 55, 20]
insertionSort(lst)
print(lst)


Forming a dynamic dictionary based on searched key and values pair using RAW logic and not modules of python

Your code is off to a good start, but here are some suggestions either
for modest improvements or ways to simplify and clarify things.
The built-in pathlib library
is often the easiest and most robust way to work with file paths.
Scripts with hard-coded file paths tend to be inflexible. For example,
to review your code I had to edit it right out of the gate, because
my computer lacks your directory structure. During your own development and
debugging of a script, you might not want to operate on the real
file every time (using a tiny replica is often easier). All of
which is to say that you are better off using default paths with
the option of command-line overrides. This adjustment also positions
you well for the future: if the needs of a script increase, you
can easily add support for more command-line arguments or options.
I would encourage you to embrace the time-tested best-practice of putting
nearly all substantive code inside of function or class definitions. It's an
easy change to make and the greater isolation tends to increase flexibility.
For example, in the suggested edits below, which still stick pretty closely to
your original code, the algorithmic complexity of parsing has been extracted
into a narrowly tailored function that knows nothing about the program's larger
concerns. It doesn't have to know about paths, file reading, etc. Instead, it
takes any iterable containing lines of relevant data. That makes it easier to debug
and test, because you can call it separately with just a list of strings.
The edits below also provide a short illustration of how even fairly brief
comments can add context and help the reader understand both purpose and the
general logic. Some of the comments are like organizational sign posts: for
example, "Setup" or "Parse". They make it easier to scan code visually to get
your bearings. Other comments can help a reader by explain the gist of the
algorithm intuitively (for example, the two comments in the inner loop). Notice
that the comments do not mimic the code; rather, they help to organize it and
throw additional light on it.
At a minimum, I would encourage you to adopt consistent variable naming
conventions. Sometimes you follow the practice of appending data-type suffixes
to names; other times not. That's not a practice I've ever found valuable, but
opinions differ. Naming variables, functions, and classes is difficult because
there are often competing priorities: clarity, brevity, ease of typing, ease of
reading, need to compare/contrast similar things, and so forth. In the edits
below, some of the names below are conventional. For example, unless I need to
have multiple files open at once, I always use fh for "file handle". Same
thing for args in a main() function. Other names are fairly explicit and
human-readable: default, file_name, file_path, line, and word are
examples in this bucket. And other names are less strong: for example, data
and vals. In some contexts, a generic name is appropriate (for example, a
general-purpose function that performs a general operation on some input). In
your case, I suspect that more substantive names exist, but only you know them.
from sys import argv
from pathlib import Path

def main(args):
    # Get file path to the input data file.
    default = 'backup/file2dict.result-soa1'
    file_name = args[0] if args else default
    file_path = Path.home() / file_name

    # Parse.
    with open(file_path) as fh:
        data = parse_data(fh)

    # Report.
    print(data)

def parse_data(lines):
    # Setup.
    data = {}
    vals = []
    key = None
    marker = 'ml'

    # Parse.
    for line in lines:
        for word in line.split():
            if marker in word:
                # Store current VALS; and setup new KEY and VALS.
                if key:
                    data[key] = vals
                    vals = []
                key = word
            else:
                # Or just store with current VALS.
                vals.append(word)

    # Wrap-up and return.
    data[key] = vals
    return data

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


Chained constructors.
I find it interesting that you described the single-parameter constructor as your "default" one, and the others as "extras". It's usually the other way around: the constructor that initializes all immutable fields is the "main" one, and any other constructor that works off default values would be "convenience" constructors.

public NumLoopThread(int sleepTime, String threadName, int numberLoops) {
    this.sleepTime = sleepTime;
    this.threadName = threadName;
    this.numLoops = numberLoops;
    thread = new Thread(this, threadName);
    thread.start();
}

Take out numberLoops, you get this one:
public NumLoopThread(int sleepTime, String threadName) {
    // pasta
}

Take out sleepTime, you get this one:
public NumLoopThread(String threadName) {
    // pasta
}

Looks like threadName should be the first parameter in all 3 constructors, for consistency:
public NumLoopThread(String threadName, int sleepTime) {
    // pasta
}

public NumLoopThread(String threadName, int sleepTime, int numberLoops) {
    this.sleepTime = sleepTime;
    this.threadName = threadName;
    this.numLoops = numberLoops;
    thread = new Thread(this, threadName);
    thread.start();
}

That way when you look at your overloads, they all look like they naturally extend each other, in some sort of chain.
You already have your defaults:

private int sleepTime = 400;
private int numLoops = 10;

Consider making them static final, and single-purpose:
private static final int defaultSleepTime = 400;
private static final int defaultNumLoops = 10;

And since the actual fields are only ever assigned in a constructor, they should be marked final, too:
private final int sleepTime;
private final int numLoops;

Use this(); as the first instruction in the body, to "chain" the constructors:
public NumLoopThread(String threadName, int sleepTime) {
    this(threadName, sleepTime, defaultNumLoops);
}

public NumLoopThread(String threadName) {
    this(threadName, defaultSleepTime, defaultNumLoops);
}

Voilà, no more pasta!

That said I'm not sure about start()ing the thread in the constructor; it makes your run method a bit misleading. In general it is not a good idea to pass your 'this' reference to to a thread and start it during construction. The thread could then start with an unfinished object. Generally you should leave the starting of the thread to a separate method. (ref)


You could start with Formatting your code so that it is readable, I got this far

SELECT `str_id`
    FROM (`rest_tablestr`)
    WHERE str_id NOT IN (
        SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(t.bookingsection_id, ",", n.n), ",", -1) value
            FROM rest_restaurantbooking t
                CROSS JOIN (
                    SELECT a.N + b.N * 10 + 1 n
                        FROM (
                            SELECT 0 AS N
                            UNION ALL
                            SELECT 1
                            UNION ALL
                            SELECT 2
                            UNION ALL
                            SELECT 3
                            UNION ALL
                            SELECT 4
                            UNION ALL
                            SELECT 5
                            UNION ALL
                            SELECT 6
                            UNION ALL
                            SELECT 7
                            UNION ALL
                            SELECT 8
                            UNION ALL
                            SELECT 9) a
                            ,
                        (
                            SELECT 0 AS N
                            UNION ALL
                            SELECT 1
                            UNION ALL
                            SELECT 2
                            UNION ALL
                            SELECT 3
                            UNION ALL
                            SELECT 4
                            UNION ALL
                            SELECT 5
                            UNION ALL
                            SELECT 6
                            UNION ALL
                            SELECT 7
                            UNION ALL
                            SELECT 8
                            UNION ALL SELECT 9) b
                        ORDER BY n ) n
                WHERE n.n <= 1 + (LENGTH(t.bookingsection_id) - LENGTH(REPLACE(t.bookingsection_id, ",", "")))
                    AND t.res_id = 21
                    AND t.booking_status not in ("cancelled","departed","noshow")
                    AND ((t.bookingstart_time <= "2015-06-12 19:45:00" AND t.bookingend_time >= "2015-06-12 22:15:00")
                        OR (t.bookingend_time >= "2015-06-12 19:45:00" AND t.bookingend_time <= "2015-06-12 22:15:00")
                        OR (t.bookingstart_time >= "2015-06-12 19:45:00" AND t.bookingstart_time <= "2015-06-12 22:15:00")
                        OR (t.bookingstart_time >= "2015-06-12 19:45:00" AND t.bookingend_time <= "2015-06-12 22:15:00"))
    ) AND `res_id` = '21'
    AND `area_id` = '28'
    AND  `wait_table` = 'no'
    AND `availability` = 'yes';

Indentation in SQL is important so that you know where you are when you are debugging the code.


The biggest problem you currently have is keeping the passwords in plain text - use a one-way irreversible hash function, like argon2 or bcrypt.

Going even further, you should not be doing it yourself and instead trust something stable that have already proved itself of being mature and safe - like passlib library.

If you know that user authentication part is going to become more complicated, think about using a specialized User Directory like LDAP or Active Directory - there is a third-party ldap package that you can use for your client implementation.

Also, when a password is entered, the input should be hidden - there is a getpass module specifically for this kind of use case. Also, look into getuser() function for the login prompt.

There are also different third-party libraries, like click or cement that might help in making building the CLI apps easier. Explore the other CLI libraries at the awesome-python list.


Can not clearing a local java.util.Vector cause a memory leak?

Because complete is used and is referenced entirely within the context of the method update(), it will become unreferenced after the method returns.

This will eventually cause it to be garbage collected.

After it has been garbage collected, some or all of the elements that belong to it will also become unreferenced and then garbage collected.

So technically, no, it is not necessary to call complete.clear().

On the other hand, getting into the habit of insuring that things are properly cleaned up can be important in other circumstances. As such, it would not be wrong to add the clear() call to the end of the method. To do so, or not do so could be either a stylistic choice, or something to consider addressing in a coding standards document. I would probably add it myself.


One dimension cellular automation in Python

ncells.index(cells) is linear. You could treat cells as a binary number and use it as an index in a code array. Not sure whether the access in constant time compensates the cost of the conversion with an 8-rule though.


Script for initialising i18n for a Flask site using pybabel and goslate

Nice script! Found it very helpful to get started and test flask babel and see first translations.

A few "bugs" I discovered for your consideration

Any string with a ' will cause problems
Same with other special characters, e.g. ()

The resulting po file mixes entries later on and generates strings like:

#: dedri/templates/contact.html:29
msgid "Your email"
msgstr "Aboutmsgstr \"about (modificado por Raimundo)\" #: dedri / templates / about.html:            13 msgid \"Aquí añadimos unas pocas palabras acerca de cada uno de nosotros.\" msgstr \"Aquí añadimos Unas Palabras Sobre CADA UNO de Nosotros here.\" #: Dedri / templates / contact.html: 29 msgid \"Your email\""


Algorithm that parses through input of points and finds distance

Choose a better name than calc(). Something like distance() or euclidean_distance().
There is no need for the variable temp, just write
for i in line.split(",")
The for i in temp loop can be written more simply as:

li.extend(i.split() for i in temp)

Remove all commented out code that you don't immediately need.
The for index in range(len(li)-1): doesn't need to be in the with statement, as it is not using filestream.
res can be calculated as part of the for i in temp: loop and avoid li altogether.


I am not sure how hard may be for a newbie to take on functional programming, but if you are interested in new ways of programming, check it out. A more specific article on Ruby: FP with Ruby. If you think in terms of expressions (what things are) instead of statements (update, insert, delete, ...), code is more declarative (and usually shorter). I'll use no regexps to show an alternative approach to the existing answer:
def separate_comma(number)
  reverse_digits = number.to_s.chars.reverse
  reverse_digits.each_slice(3).map(&:join).join(",").reverse
end

If you want to support decimals:
def separate_comma(number)
  whole, decimal = number.to_s.split(".")
  whole_with_commas = whole.chars.to_a.reverse.each_slice(3).map(&:join).join(",").reverse
  [whole_with_commas, decimal].compact.join(".")
end


Parsing hex and decimal numbers in C++11


Your question doesn't give enough information about exactly what you want to do to know if this might work in your case.  The most important answer is simple though:  "don't do that; use a web framework instead".  Session management should almost always be done in the standard way of your framework; then you won't have to do anything other than check if your framework thinks your user is authenticated.

Let's now look at the specifics of the code.  You have used a cryptographic PRNG.  That should be fine.  You used 32 bytes = 256 bits of random data.  This should be no problem (128 bits is normally enough against brute force attack; 256 is overkill, which is likely good in this case)  your main fears should probably be seeding and leaking bits.

Seeding: the PRNG documentation says that if you don't give a random seed the PRNG will be unseeded.  In real life it seems that it actually does get seeded from /dev/random.  I deeply don't like this and would probably seed it explicitly.  Better would be discuss with the author and send a patch to the documentation for the library so that it promises to do what it actually does.  I don't have an OS without /dev/random to test on, but I think your function simply won't work there (which is good because it's safe).

Leaking bits1:  If the attacker can find a situation where response time depends on the contents there might be a problem (e.g. force regeneration of the session).  Looking up about timing attacks on Ironclad I don't think it's resistant (see e.g. https://www.mail-archive.com/pro@common-lisp.net/msg00977.html).  You need to check or protect against this.  If the contents of the message are true then you should probably switch to a better library such as LibreSSL when it comes out (possibly GNU TLS?).  If someone attempts to use the function on e.g. Windows I would simply exit with a warning.

Leaking bits2: your output is constant size but includes some special characters. If the attacker can find a situation where your message length depends on the authentication token then you have a problem.  Using Base64 encoding (a second time) should be fine.  Using maximal HTML encoding for your tokens will not be (n.b. normal HTML would be fine; but some encoders go too far).

Leaking bits3: Going further, there are standard attacks against SSL/TLS which I think you should consider; BEAST, CRIME and BREACH which leak bits https://en.wikipedia.org/wiki/Transport_Layer_Security#CRIME_and_BREACH_attacks.  Increasing the size of your random token beyond 256 bits might marginally protect against such things.  However "In the current form, the exploit uses JavaScript and needs 6 requests to extract one byte of data" (https://community.qualys.com/blogs/securitylabs/2012/09/14/crime-information-leakage-attack-against-ssltls) so I think you may find that effort would be better invested in turning off compression on all sites and user web browsers.  Personally I would go for 384 bytes just to be more conservative than the rest ;-)

Summary: probably your function is basically the same one used in many frameworks.  Because of the risk of timing attacks and bit leakage I wouldn't personally want to release it for public use until I understood the whole surrounding situation of usage much better than I currently do.

(edited lots since I misread bytes as bits in the first response)


Bifurcating recursive calculation with redundant calculations

One more option is to not recurse at all, but to instead compute the sequence until we get the desired term. This not only avoids the original algorithm's exponential complexity; it also avoids the at least linear (in terms of counting values) memory complexity of memoization. (Recursion/memoization can also run into implementation-specific issues such as recursion depth errors or memory errors, which are not a concern with the code below.)
def T(n):
    if n<=0: return 1
    # Set a, b to T(-1), T(0)
    a, b = 1, 1
    for i in range(n):
        # Change a, b from T(i-1), T(i) to T(i), T(i+1)
        a, b = b, b+i*a
    # i=n-1 set b to T(n)
    return b


I'd format this code slightly differently:

return _formulaSheetSchema.column(columnName).get()
  .formula()
  .computeReferencedColums(getSheetName()).stream()
  .collect(groupingBy(ColumnRef::sheetName,
    LinkedHashMap::new,
    collectingAndThen(toSet(), set -> set.stream()
      .map(ColumnRef::columnName).collect(toSet())));

Also the downstream operation you use here is wasteful, because it collects the set and then restreams it. Instead use the mapping collector:

  .collect(groupingBy(ColumnRef::sheetName,
    mapping(ColumnRef::columnName, toSet()));

Sidenote: I personally prefer using 2-space indents across the board, but this formatting works with 4-space indents just as well...

Additionally the _-prefix for instance variables is something I dislike, but so long as you're consistent about it, that should be fine.

I also dislike that you're not checking whether the column actually exists, before accessing it's formula.


Safety

The whole point of the smart pointers is that you give up ownership of the pointer; so it (the smart pointer) will delete it if something goes wrong. Unfortunately your smart pointer fails this first test.

SharedPtr(T* rawObj)
{
    m_Rc = new RCObject<T>(rawObj);  // if this throws you have
                                     // leaked your object the one
                                     // thing a smart pointer should
                                     // never do.
    m_Rc->ref();
}

If that new throws you leak the pointer that was given to you.

Probably the best way to handle this is the initializer try catch:

SharedPtr(T* rawObj)
try
    : m_Rc(new RCObject<T>(rawObj))
{
    m_Rc->ref();
}
catch(...)
{
    delete rawObj;
    throw;
}

Rule of Three

Your RCObject does not obey the rule of three. Now this is a special case as it should only be used by SharedPtr and I can't see a wrong usage. But to protect against future misuse you should make this (RCObject) a private member of SharedPtr.

Though you have the three methods you require for SharedPtr to implement the rule of three you don't actually do it. Su you leak m_Rc in a couple of situations.

{
    SharedPtr  ac;
}//  Leaked the m_Rc

SharedPtr  ac1;
SharedPtr  ac2;
ac1 = ac2;  // leaked m_Rc from ac1.

RefCount Issue

I don't like setting the Count to zero in RCObject constructors as that requires you to call ref() after creating the object to correctly initialize it.

RCObject() :rawObj(nullptr), count(0){}

The whole point of the constructor is to put the object into a valid state so it can immediately be used.

And here is the bug caused by that misuse.

SharedPtr()
{
    m_Rc = new RCObject<T>();
    // You forgot to increment the ref count here.
    // When this shared pointer is destroyed the count will go to -1
    // not zero.

    // Currently this is not causing an issue because your class is
    // so simple but this breaks the invariant that everybody thinks
    // you have and thus as the class is expanded it will cause an
    // error at some point.
}

Pointless Test

Don't see the point in the test.

T* get()
{
    if (rawObj != nullptr) {
        return rawObj;
    }
    return nullptr;
}

if the pointer is null you will return nullptr. Why not just return the pointer?

Learn to use RAII

void ref()
{
    m_Mutx.lock();
    count++;
    m_Mutx.unlock();
}

Any time you see the pattern:

SetUp
Work
TearDown

You should think RAII and wrap it. It may seem usless; but as your class expands it will ensure that your class stays exception safe. Also there are so many helper classes out there that it is not really that much work.

void ref()
{
    std::lock_guard<std::mutex> lock(m_Mutx);
    count++;
}

Stop using leading underscore in identifiers

RCObject(T* _rawObj):rawObj(_rawObj)
{}

The rules for a leading underscore are non trivial and most people don't know them. So even if you do most other people will get confused and panicky.

The case where you use them it is not required anyway:

// This is perfectly valid.
// Does what you expect and quite normal in C++ code.
RCObject(T* rawObj)
    : rawObj(rawObj)
{}

Other Stuff.

There are a whole bunch of other stuff you should do take a look at these three articles I wrote:

Smart-Pointer - Unique Pointer
Smart-Pointer - Shared Pointer
Smart-Pointer - Constructors


This SQL, for what it does, is neat, and concise. There is no obvious place where any optimizations can be made. With the data size as small as it is, there is no reason to recommend indexes, or other improvements.

The only glaring issue has been pointed out already: There are tools other than SQL that are designed to do these types of heavy computation with performance that your database just won't beat.

Apart from anything else, the log functions are notoriously hard to get right, and there are multiple 'standards' for implementation.

Consider math libraries designed for data and computations such as this.


Fetch a department's total, Its percentage of earnings for that site and its percentage of earnings across the company

Adding the same GROUP BY in the 2nd Select is unneccessary, the data is already unique.

I prefer writing this kind of logic (where you need multiple aggregation levels) as a single Select using Analytical Functions, i.e. Group Sums:

WITH AllSites AS
 (  -- calculate the percentages for all sites
    SELECT DeptName
        ,DeptNo
        ,SUM(Value) AS SalesValue
        ,SiteNo
    -- Analytical Functions are calculated after GROUP BY/HAVING,
    -- thus you can nest the aggregated Sum in the Group Sum
        ,SUM(Value) / SUM(SUM(Value)) OVER (PARTITION BY SiteNo) AS SitePercent
        ,SUM(Value) / SUM(SUM(Value)) OVER () AS CompanyPercent
    FROM PLUSalesDetailExtended
    WHERE DATE BETWEEN @SessionDateFrom AND @SessionDateTo
    AND TermNo = 0

    GROUP BY DeptName, DeptNo, SiteNo
 )
-- return only one site
SELECT * FROM AllSites
WHERE SiteNo = @SiteNo

If this is more or less efficient than your current approach depends mainly on he number of sites, at least it's simpler (for me) and easy to extend to multiple/all sites.


MySQL update with PHP; MySQLi prepared statement in loop

I'll stick to reviewing your PHP code, as it doesn't look like it's recieved the attention it deserves!

I'll do what I can to find faults, but I won't be testing anything. Alright, we ready?

Nice! First line and we've found something! What kind file is functions.php!? Filenames should be one of the clearest names in your project. If you can't come up with a good filename, then that's a sign you need to break apart the contents within.
I'm assuming the initial conditional empty() is for testing only? I hope so, because automatically updating data on page load is a disaster waiting to happen. Send a couple hundreds requests to a page that's doing resource heavy database access and you'll happily be sitting in a DoS attack.
It's a little hard to tell, but it looks like those variable sets in that conditional are longer than any desired line length. It makes it much harder for the reader to effienctly read your code if they have to do a lot of horizontal scrolling.

You can reduce this a couple ways. In this case, break down your array over multiple lines:

$_POST['UPDATE_HEADERS'] = array(
        'table' => 'polisy_oc',
        'id' => 'numer_polisy_oc',
        'id_value' => '72451'
    );

This way is substantially easier to read in my opinion.

For other really long lines, you may be able to put data in variables, and then reference those variables.
@200_success had a good point: UpdateRecord isn't a very good name. Although it's hard to rename this class because it really doesn't have a single responsibility. That in itself contradicts the principles of SOLID. You might look towards making a Record class.
__construct($db_connection) should really be __construct(Database $db_connection) to better implement dependency injection practices.
I'd avoid automatically calling update() from the constructor. Make update() public and call it where the object is initialized.
If update() were in a Record class, that'd be OK. But here, I don't think the name is right. For all I know, this could really be updating the record updater!

If you don't change the class name, I suggest this be a method called updateRecord().
Where do we get $_SESSION from? The user. And what's our golden rule? Never trust the user. I think it's a bad idea to check the POST data against session data. Make a concrete white-list somewhere. Either that, or eliminate the need to dynamically reference a table. I'm sure with enough whiteboard space you'll be able to figure it out!
Avoid using the default $key and $value. Give some more descriptive variable names.
What is checkColumn()?

Alright! One file down, now the next?

Database is a very vague name. You got the noun part down, but seeing something like this would kill me!
Something like $connection doesn't need to be public. I'd make that private/protected and encapsulate it.
Add a visibility to your constructor.
Your constructor has too much business logic in it! It should be for constructing the object, nothing more. Break the logic into other methods.
Oh goodness! I do not like how you're setting up the database via the session! That is asking for a lot of trouble! Why can't the credentials be hardcoded? Or maybe put in a config file and access that way? The user has your database credentials!
Your DBAL shouldn't have any type of business logic in it what-so-ever. It defeats the point of having it in a separate class.

And lastly, you mention your code bottlenecking, but you're not sure why. Profile your code, get some concrete numbers, and then work from there. Is it that foreach that's causing the lag, or it say the query? You'll never know unless you test it!

In the comments a couple questions were asked. I'll do my best to answer them:

  functions.php is a file which contains database class and functions that are used by more than one class; shouldn't I use such resolution?

It's vague. What kind of functions are in this file? What kind of functions should go in this file? When is it appropriate to have a new class, versus just add functions into here. It's a problem of extending functional programming along side OO programming.

  Making a Record class would mean that it would have methods like updateClient(), insertClient() etc. inside, which I thought would make connecting app to database harder - wouldn't it be simpler for an app just to invoke desired class which is in separate .php file?

Those methods you list might actually be a better fit in a Client class. But having CRUD operations in a single class is not a bad thing. It keeps all "record" things together, and all "client" things together.

  Why adding class name in __construct parameters implements better dependency injection?

It ensures that the type hinted class is being introduced into the object. If I say __construct(Database $db), I know that a database will be passed. It also helps inheritance efforts. I could code something like __construct(API $api), and then if I have UserAPI and ServiceAPI classes, both of which extend API, then I can pass either of those, too.

  in update class, I check provided table and column with [the $_SESSION global] white-lists, why is it bad?

You don't have control over the session. It's so easy to take advantage of vulnerable sessions, it just takes the right mind. Here's just the a beginner's answer to session hijacking/fixation.

  I want an user of app to be the user of the database either (docs.oracle.com/cd/B28359_01/network.111/b28531/…), that's why I've come up with creating connection by $_SESSION.

From the Oracle link you provided, right off the bat I'll give you text from that page:

  You can store password credentials for connecting to a database by using a client-side Oracle wallet. An Oracle wallet is a secure software container that stores the authentication and signing credentials needed for a user to log in.

I point this out because of several reason:

Oracle isn't advocating PHP sessions. They want you to use their secure software to authenticate. These are two different realms here!
Application users should really only need to be database users if you have that much data. I don't see any reason to do this if you are the average web developer. If you are scaling up to this approach, you will want a team. Things will become very messy if each user get's their own set of databases!

  How can I connect to database by mysqli using password hashes?

You can't. Not hashes anyways. Know the differences between hashing, encrypting, etc.

You may need to rethink your program architecture before getting into this. I don't have the experience to tell you how it should be done.


please check my user session login system and tell me what can be done to make it better

Let's start with the SQL injection vulnerability in your code in

mysql_query("SELECT * FROM users WHERE    username='$login_username' AND password=SHA1('$login_password')") or die("unable to connect to msql server: " . msql_error()) ;

In this piece of code you put the user defined $login_username straight to a SQL query. I see that there's a function called security2 that does something to username but I cannot be sure that it executes mysql_real_escape_string that would prevent SQL injection. If it does not, then a malevolent user might be tempted to enter a user name that may corrupt your database.

Poor Bobby Tables might cause problems here .

Rest of the thoughts are (at least at first) in random order

Uou should probably migrate from mysql_ to mysqli_ functions. mysqli is the MySQL Improved Extension for PHP that is supposed to work better and do much more.

In PHP, it's almost always better to use === than == to test equality.

You could try to be consistent with the use of ''and "".

The code would benefit if you would define more functions to partition the logic. Maybe even define some classes.


Returning a description based on a minimum and a maximum value

Perhaps instead of this convoluted branching, you could simply handle the three cases: whether there is a maximum and a non-zero minimum, a maximum and a minimum of 0 or there is no maximum.

private static Dictionary<int, string> humanReadableNumbers = new Dictionary<int,string>()
    {
        {0, "zero"},
        {1, "one"},
        {2, "two"},
        //etc...
    };

public static string Execute(int minimum)
{
    return string.Format("{0} or more", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(humanReadableNumbers[minimum]));
}

public static string Execute(int minimum, int maximum)
{
    return minimum == 0 ? string.Format("Less than {0}", humanReadableNumbers[maximum]) : string.Format("From {0} to {1}", humanReadableNumbers[minimum], humanReadableNumbers[maximum-1]);
}

With this method, adding extra numbers is very simple. I've additionally chosen to split the method into two methods, one for no maximum, and one for a maximum. I feel it shows the different use-cases better, but if it is too much work to refactor all uses of it, this will work instead:

public static string Execute(int minimum, int? maximum)
{
    if(maximum.HasValue())
    {
        return string.Format("{0} or more", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(humanReadableNumbers[minimum]))
    }
    else
    {
        return minimum == 0 ?
            string.Format("Less than {0}", humanReadableNumbers[maximum]) :
            string.Format("From {0} to {1}", humanReadableNumbers[minimum], humanReadableNumbers[maximum-1]);
    }
}


It's not bad already (save for the weird formatting of parameters, what's with commas at the beginning of the line? : ) But that's a matter of taste, and beyond the point).

You could return to the trick you used in your previous implementation to avoid breaking the chain and get rid of value reassignments by pushing includeBlanks into the predicate, like so:

values = values.Where(v => includeBlanks || v != string.Empty);

We've cut on verbosity, but it's debatable to me whether it improves readability.

It's harder to achieve the same thing for Distinct, although you could pull it through if you added a helper extension:

private static IEnumerable<T> If(this IEnumerable<T> enumerable, bool condition, IEnumerable<T> ifMet)
{
    return condition ? ifMet : enumerable;
}

Then you could turn all of it into a single statement:

return String.Join(
    between,
    values
        .Where(v => includeBlanks || v != string.Empty)
        .If(excludeDuplicates, values.Distinct())
        .Select(v => begin + v + end);

Is it worth it, or over-engineered? This is subjective and a matter of taste. Some find verbosity more readable, others prefer terseness/expressiveness.

I would let both solutions slide in a peer review, although creating a single-use extension method (If) looks like a minor code smell.

Another way to avoid repetitive overwriting of values, which you're uncomfortable with, would be to introduce temporary variables:

var blanksHandled = includeBlanks ? values : values.Where(v => v != String.Empty);
var duplicatesHandled = excludeDuplicates ? blanksHandled.Distinct() : blanksHandled;
var withAffixes = duplicatesHandled.Select(v => begin + v + end);

We're not overwriting values now, but when I look at it, I feel it sacrificed too much readability just for that, and what's worse, it got error-prone in process: you now have to remember to always use the last variable so as not to skip a step.

If another parameter was to be added, there's a risk of a bug slipping in:

var blanksHandled = includeBlanks ? values : values.Where(v => v != String.Empty);
var duplicatesHandled = excludeDuplicates ? blanksHandled.Distinct() : blanksHandled;
// oops! will everyone spot right away what went wrong here?
var dirtyWordsHandled = safeMode ? blanksHandled.Except(dirtyWords) : blanksHandled;
var withAffixes = duplicatesHandled.Select(v => begin + v + end);

The last trick I can think of would be to convert these conditionals into Func<IEnumerable<T>, IEnumerable<T>> instances, and pass them to the LINQ chain.

I haven't got a C# IDE on this computer, so this code could be syntactically incorrect, but you get the idea:

Func<IEnumerable<String>, IEnumerable<String>> handleDuplicates = (entries) => excludeDuplicates ? entries.Distinct() : entries;
// etc.

And use them as selectors:

values
   .Select(handleBlanks)
   .Select(handleDuplicates)
   // ...

Again, is it worth it? Sometimes the clever thing is not to be too clever, and keeping it simple beats trying to make it pretty.


In generateSorted, in every iteration of the outer for loop, you walk over the data array elements until you find the right position to insert. In the worst case, this could be very inefficient. You can improve the performance using binary search:

public int[] generateSorted(final int length, final int minVal, final int maxVal) {
    List<Integer> data = new ArrayList<>(length);

    for (int i = 0; i < length; i++) {
        int rndNum = getRandomVal(minVal, maxVal);
        int insertionPoint = Collections.binarySearch(data, rndNum);
        data.add(insertionPoint > -1 ? insertionPoint : - insertionPoint - 1, rndNum);
    }

    return data.stream().mapToInt(i -> i).toArray();
}

Some other improvements I did here:

Changed i -> { return i; } to the lambda expression i -> i
Generate an array of length, instead of length + 1 as your version
Declare data as List instead of ArrayList

I would also shorten getRandomVal like this:

private int getRandomVal(int min, int max) {
    return min + rand.nextInt(max - min + 1);
}

Finally, I would make the whole thing testable, something like this:

class GenRandomArray {

    private final Random rand;

    public GenRandomArray() {
        rand = new Random();
    }

    public GenRandomArray(int seed) {
        rand = new Random(seed);
    }

    public int[] generateSorted(final int length, final int minVal, final int maxVal) {
        List<Integer> data = new ArrayList<>(length);

        for (int i = 0; i < length; i++) {
            int rndNum = getRandomVal(minVal, maxVal);
            int insertionPoint = Collections.binarySearch(data, rndNum);
            data.add(insertionPoint > -1 ? insertionPoint : -insertionPoint - 1, rndNum);
        }

        return data.stream().mapToInt(i -> i).toArray();
    }

    private int getRandomVal(int min, int max) {
        return min + rand.nextInt(max - min + 1);
    }
}

public class GenRandomArrayTest {
    @Test
    public void testGen10_Between_0_and_100() {
        assertArrayEquals(new int[]{5, 9, 42, 54, 67, 72, 82, 84, 93, 98},
                new GenRandomArray(0).generateSorted(10, 0, 100));
    }
}

About Collections.binarySearch

The javadoc says:

  returns: the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size() if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

If the value is greater than -1, it's the index of the item we searched for. In other words the random number we generated is already in the list. We can insert it this duplicate right there.

If the value is -1 or smaller, the item is not yet in the list, and the returned \$x\$ is such that \$-x - 1\$ is the right place to insert the item to keep the elements ordered.

It becomes clearer to see if you print out the random number, the returned \$x\$ and the content of the list, for example when using \$seed = 0\$ and \$(10, 0, 10)\$ as the params of the generator:

0 : -1 : []
6 : -2 : [0]
8 : -3 : [0, 6]
5 : -2 : [0, 6, 8]
6 : 2 : [0, 5, 6, 8]
2 : -2 : [0, 5, 6, 6, 8]
2 : 1 : [0, 2, 5, 6, 6, 8]
6 : 5 : [0, 2, 2, 5, 6, 6, 8]
2 : 1 : [0, 2, 2, 5, 6, 6, 6, 8]
0 : 0 : [0, 2, 2, 2, 5, 6, 6, 6, 8]

For example:

When inserting 8, it doesn't exist in the list, the returned \$x = -3\$, so we should insert at position \$-x -1 = 3 - 1 = 2\$.
When inserting 2 for the second time, the returned \$x = 1\$, so we should insert at position 2.

Conclusion

It's not worth trying to keep the list sorted. The binary search makes it faster than the original implementation, but keep in mind that the insertion is costly, as every time you insert something, the rest of the array has to be copied. And you cannot compensate for that by using LinkedList instead of ArrayList, because efficient binary search requires a random access list, with a LinkedList you would lose efficiency due to the traversal of elements.

Overall, this will be slower than first adding all the elements and then later sorting with Arrays.sort. So do it that way instead.

public int[] generateSorted(final int length, final int minVal, final int maxVal) {
    List<Integer> data = new ArrayList<>(length);

    for (int i = 0; i < length; i++) {
        data.add(getRandomVal(minVal, maxVal));
    }
    Collections.sort(data);

    return data.stream().mapToInt(i -> i).toArray();
}


Using built-ins

You can use built-in comparators instead of creating your own. For example

final Iterator<Collection<T>> allLists = stream.sorted(
      (l1, l2) -> l1.size() - l2.size()
).iterator();

can be written more simply using the comparingInt comparator, which compares elements according to the result of applying the given function (returning an int) to each element. In this case, you could have:

final Iterator<Collection<T>> allLists =
    stream.sorted(Comparator.comparingInt(Collection::size)).iterator();

The function returning the integer to compare is written as a method-reference refering to Collection.size(). Also, it gets rid of the comparison by subtracting the two int values, which has a corner-case.

Bug if the stream is empty

There is a bug in the current method if the given stream is empty. This is because of

final Set<T> result = new HashSet<>(allLists.next());

which unconditionally invokes next() on the iterator of the stream. You can call intersect(Stream.empty()) to verify it; it will throw a NoSuchElementException. In the case of an empty stream, the method should return an empty collection instead.

Better generics

With the current signature of

<T> Collection<T> intersect(Stream<Collection<T>> stream)

the issue is that passing for example a Stream<List<Integer>> will not compile. To verify this, you can have

intersect(Arrays.asList(Arrays.asList(1)).stream())

Instead, we can introduce a second generic type C for the collection with the following signature:

<T, C extends Collection<T>> Collection<T> intersect(Stream<C> stream)

This makes sure that you can pass any collection for the elements of the input stream. With such a signature, you can verify that the above compilation error is not there anymore.

Huge performance improvement

If you're not dealing with sets, the retainAll process is very inefficient:

result.retainAll(allLists.next());

Since this method checks to see if the collection given as parameter doesn't contain the elements of this collection (in order to remove them), using this on a List is O(n), making the whole operation O(n²). Instead, pass a new HashSet:

result.retainAll(new HashSet<>(allLists.next()));

Since the contains operation is constant time for sets, this will always be O(n), and, thus, a lot faster (at the expense of more memory).

Why sort?

The comment in your code says that the sorting step is used as an optimization, to ensure that shorter collections comes first. Intrigued, I made a benchmark comparing the code with and without sorting. It applied the two methods to a stream having 1000 and 10.000 elements where each inner collections had 100 and 1.000 elements. The elements chosen were random integers. Here are the results (Windows 10 x64, JDK 1.8.0_102, i5, 2.90 GHz):

Benchmark                 (lengthOfEach)  (totalLength)  Mode  Cnt    Score   Error  Units
StreamTest.intersect                 100           1000  avgt   30    1,757 ± 0,069  ms/op
StreamTest.intersect                 100          10000  avgt   30   18,876 ± 0,954  ms/op
StreamTest.intersect                1000           1000  avgt   30   17,287 ± 0,378  ms/op
StreamTest.intersect                1000          10000  avgt   30  177,633 ± 7,043  ms/op
StreamTest.intersectSort             100           1000  avgt   30    1,805 ± 0,080  ms/op
StreamTest.intersectSort             100          10000  avgt   30   18,434 ± 0,621  ms/op
StreamTest.intersectSort            1000           1000  avgt   30   19,472 ± 0,981  ms/op
StreamTest.intersectSort            1000          10000  avgt   30  184,440 ± 5,380  ms/op

For the values tested, this shows that there is really no measurable difference between the two, so I'd just get rid of this sorting.

Code of benchmark for completeness:

@Warmup(iterations = 10, time = 700, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 700, timeUnit = TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(3)
public class StreamTest {

    public static <T, U extends Collection<T>> Collection<T> intersectSort(Stream<U> stream) {
        final Iterator<U> allLists = stream.sorted(Comparator.comparingInt(Collection::size)).iterator();

        if (!allLists.hasNext()) return Collections.emptySet();

        final Set<T> result = new HashSet<>(allLists.next());
        while (allLists.hasNext()) {
            result.retainAll(new HashSet<>(allLists.next()));
        }
        return result;
    }

    public static <T, C extends Collection<T>> Collection<T> intersect(Stream<C> stream) {
        final Iterator<C> allLists = stream.iterator();

        if (!allLists.hasNext()) return Collections.emptySet();

        final Set<T> result = new HashSet<>(allLists.next());
        while (allLists.hasNext()) {
            result.retainAll(new HashSet<>(allLists.next()));
        }
        return result;
    }

    @State(Scope.Benchmark)
    public static class Container {

        @Param({ "100", "1000" })
        private int totalLength;

        @Param({ "1000", "5000" })
        private int lengthOfEach;

        private List<List<Integer>> list;
        private Stream<List<Integer>> stream;

        @Setup(Level.Trial)
        public void setUp() {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            list = rnd.ints(totalLength).mapToObj(i -> rnd.ints(lengthOfEach).boxed().collect(Collectors.toList())).collect(Collectors.toList());
        }

        @Setup(Level.Invocation)
        public void makeStream() {
            stream = list.stream();
        }

    }

    @Benchmark
    public Collection<Integer> intersectSort(Container a) {
        return intersectSort(a.stream);
    }

    @Benchmark
    public Collection<Integer> intersect(Container a) {
        return intersect(a.stream);
    }

}


for with range and len

This is one of the most common things to be seen in python code to someone that isn't very familiar to python, however it's not Pythonic and almost always the wrong way to do it.

Firstly if you are not using the index, so there is no need to iterate base on the index. Consequently this can be replaced with a normal for over a collection, in your case, a string.

So this:

for i in range(len(word)):
    if not curr.contains_key(word[i]):
        curr.put(word[i])
    curr = curr.get(word[i])

Should become this:

for letter in word:
    if not curr.contains_key(letter):
        curr.put(letter)
    curr = curr.get(letter)

Note that the index was not necessary at all, and the code actually becomes clearer.

Even in cases where the index of the element is needed for something, you can use enumerate which gives you a tuple for each element, holding both the index and the element:

for index, letter in enumerate(word):

Which is still more readable and remains Pythonic

Keys stored in array

Even though it is reasonable to store keys in a array it is actually simpler to use a dictionary. Doing so avoids having to convert a character to value in the range 0-26 and still makes it simple to check whether it exists.

So instead of doing self.children = [0]*26 you would do self.children = {}. Most of the TrieNode methods would have to be rewritten accordingly:

def put(self, ch):
    self.children[ch] = TrieNode()

def get(self, ch):
    return self.children[ch]

def contains_key(self, ch):
    return ch in self.children

Note that they are all so small and simple that i would go as far as removing them and using curr.children directly in both in insert, search and startsWith.

i.e (insert):

curr = self.root
for letter in word:
    if letter not in curr.children:
        curr.children[letter] = TrieNode()
    curr = curr.children[letter]

While using curr.children makes sense in this simple example, it may not be the case if there is a lot more logic in TrieNode. Each case is unique in a way, and what may be fine in one scenario may be bad in another.

The same can be said about the is_end method that merely returns end. Following this last step, you could go a step further and also use end directly as there isn't much to encapsulate. On top of that only Trie uses TrieNode.

On a side note, in cases where you need a getter, you should use the @property decorator which would be far more appropriate and Pythonic.

camelCase

I know the methods stubs were provided in the problem description, however, camelCase is not the typical naming convention for python  methods. The preferred style for this is snake_case, and as such, startsWith should be renamed to starts_with.

Feel free to take a look at the related PEP, PEP 8 -- Style Guide for Python Code


Your code is mostly right. primeCounter is a misleadingly named variable, since it counts non-primes. And, similarly, primes isn't an array of primes, but rather an array of flags indicating prime-ness. It would be better to name this one isPrime.

There is an Arrays.fill method that you may want to use for clarity:

Arrays.fill(isPrime, true);

And it may be easier to simply add the primes as you go though, and return an ArrayList<int>:

/*
 * Calculate primes up to num using Sieve of Eratosthenes algorithm
 */
ArrayList<int> primes = new ArrayList<int>();
for (int i = 2; i <= num; i++) {
    if (isPrime[i]) {
        primes.add(i);
        for (int k = i * i; k <= num; k += i) {
            isPrime[k] = false;
        }
    }
}
return primes;

The other advantage of ArrayList is that you don't have to have a separate index to keep track of where in your array you're inserting into - let the container do its job!


How to deal with interface inheritance and common properties?

What I would do is to use inheritance, but have only setter in IWritableItem:

public interface IReadableItem<TKey>
{
    TKey Key { get; }
}

public interface IWritableItem<TKey> : IReadableItem<TKey>
{
    new TKey Key { set; }
}

This still works as expected:

class Foo<T> : IWritableItem<T>
{
    public T Key { get; set; }
}

It also has the advantage that you can't implement the getter for IWritableItem differently from the getter for IReadableItem.


It's hard to comment when the original question was very vague, the Original-OP (O-OP) failed to answer questions, different interpretations were made by the various respondants, and there's (currently) no accepted answer. Even worse, someone saw fit to remove the jQuery tag, implying that only a CSS solution would suffice - a view that did not come from the O-OP.

None of that was your fault.

The worst feature of this answer (and possibly others) is that the paradigm is reliant on a particular font size - change it and the step-formation falls apart.

You might consider something like the version below, which goes a long was to fixing that particular issue, and tidies up the code considerably :

$('document').ready(function() {
    function makeEqual($container, dashes, n) {
        var $step, scale = {}, i;

        // Create a single step and proactively measure its width and height in the target container.
        // IIFE allows working member `dashString` to be GC'd.
        (function(dashString) {
            $step = $("
").html(dashString + "

" + dashString).css({'position': 'absolute', 'width': 'auto', 'white-space': 'nowrap'}).appendTo($container);
            scale.w = $step.width();
            scale.h = $step.height();
            $step.remove();
        })(Array.apply(null, new Array(dashes)).map(function() {return '- '}).join('')); // pass in the required string of dashes and spaces

        // create staircases - descending and ascending in the same loop
        for(i=0; i<(n-1); i++) {
            $step.clone().css({ 'left': i * scale.w, 'top': i * scale.h }).appendTo($container); // descending steps
            $step.clone().css({ 'left': (2 * n - i) * scale.w, 'top': i * scale.h }).appendTo($container); // ascending steps
        }
        // create bridge-piece
        for(i=0; i<3; i++) {
            $step.clone().css({ 'left': (n - 1 + i) * scale.w, 'top': (n - 1) * scale.h }).appendTo($container); // bridge-piece
        }
    }
    makeEqual($("#main").empty(), 5, 3);
});

DEMO

Notes :

no ids and no rediscovery of freshly appended DOM elements.
no need for the dashStructure() function - instead, positioned clones of the prototype $step are appended.
all steps are absolutely positioned on the same bases using left and top; the vagaries of mixed natural flow and relative positioning are avoided.
the number of assignments is greatly reduced.
consistent indentation!!


Not much to do there, maybe use with ... as and inline everything:

from itertools import product

with open('myfile', 'w') as file:
    for p in product('ABCDEFGHIJKLMNOPQRSTUVWXYZ', repeat=8):
        file.write("".join(p) + "\n")

I'm sure there is a clever way to generate the characters as well. Edit: ah yes, you could replace 'A...Z' with map(chr, range(65, 91)) although
that seems rather cryptic.

Now why would you want that though? It seems that using that generator and
staying in Python would be more than enough instead of generating a static
file.


Clean Code / OOP on Monty Hall Simulation Implementation

.. review .. mainly on the aspects of OOP and CleanCode

To Some of Your Thoughts

  usage of integer for doors - i think that's not so fancy...

Additional to a Door class we could add classes for Player, ShowMaster, and MontyHall too.

  should I break up the code more? maybe one method for removing one door by the showmaster?

This job is be done when we split up the code with classes into different logical units.

  too much comments (tell don't ask)

Comments are add all not bad but when you group your code in a method you all ready see semi-independent logic. In Robert C. Martins words: “Don’t Use a Comment When You Can Use a Function or a Variable”

Review

Bad news first.. I see no oop at all in your code.. An indicator for non-oop code is the heavy use off static and no own data-types. With other words: You write a huge procedural algorithm.

Find Objects

My first step would be to wrap this method into a class called MontyHall and give it a public method caluclateChance. I would avoid the get-prefix because it is not a getter.

After that, we can search for cohesion and abstractions. Your comments helps us very well!

The comment //the player selects one of three doors shows us that we could create a Player. After that the ShowMaster wants //.. remove one goat (but not the selected door). Than our //player changes door if he wants to.

We should think again about a Door. Currently, the doors are represented as a list of integers. But in the domain of Monty Hill, a door is something that hides a price and has a number.

class Door {
    private int number;
    private Price price;

    // all-args constructor..

    // hashcode and equals-method..
}

Code Smell

Magic Number

The magic number 3 appears two times in your code:

random.nextInt(3)
if (..., currentDoor < 3; ..)

The 3 represents the number of doors and can be replaced by int NUMBER_OF_DOORS = doors.size(), or you can hide it in an object, which would be the preferred way.

Flag Argument

The method getChanceForPlay takes as second argument a Boolean value, which creates uses if (doChange) { /*...*/} to create a branch in your method. The downside of this flag is that we need to test each branch when writing a unit test (but ok.. in this we have only on possible branch).

This flag has a logical cohesion to a Player. Let's say a Player is an abstract data type and there are two possible types of players: StraightPlayer and ChangePlayer. With polymorphism we can pass both players and call player.chooseDoor. While the StraightPlayer chooses the same door as before and ChangePlayer changes his/her selection.

Code

// will follow


I see a few issues with your class:

Issue 1

This property is open so anyone can set it:

public SqlConnection sqlconn { get; set; }

You probably want to make the setter private so only you can set it from within the class and it can be accessed from outside.

Issue 2

The property name should be SqlCon because the convention is to use Pascal naming in C#.

Issue 3

You have this in your constructor: OpenConn(); which means as soon as someone creates an instance of your class, you open the connection but what if the user of your class does not want to open the connection yet?

Issue 4

Why are you making the assumption that the user of you class will be using windows forms? What if I use your class from a console application, how will MessageBox.Show work?

catch(Exception ex){
    MessageBox.Show(ex.Message);
}

Issue 5

What if the user of your class forgets to close the connection?

Issue 6

What are you getting from this class? I will not use your class because the one in .NET is much better and easier to use. I can use it like this:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    // Do my work
}

So if I am on your team and you tell me to use your class, which is a wrapper, first question I would ask you is: Why? And then if I find out that as soon as I create an instance of your class, you are opening database connections, I will not be very happy.

Patterns

Patterns such as Single Responsibility Pattern (actually it's a principle but beyond the point) and many other patterns that are there should be used by programmers who have a black belt, or at least a few belts, in programming. If someone is learning programming, they should not even bother with patterns. Patterns are the cool thing and with names like Singleton, Liskov Substitution Principle, Memento Pattern and other fancy names, I have to admit they are so tempting. People throw these terms around because they want to sound smart but do not give into this temptation. People want to use them instead of just creating a plain solid class.

Many people on StackOverflow ask questions such as "Why do people give so much attention to data structures, will I ever need to create one?" My answer to this question is simple: Yes, you will create them every single time you create a class. Just because you are creating a class called DatabaseConnection or Employee does not mean they are not data structures. They are. They are encapsulating data and you better make sure no one puts the state of your class into a bad state: like you have the sqlconn property public.

Forget Patterns for now

Do not worry about patterns for now. Just study data structures and learn how much effort is put into making the data structure so it is never in a bad state. Pay attention to how the operations are defined so users can interact with the data, add data, manipulate data, delete data and what it does when someone tries something they should not be allowed. In other words, when someone tries something the are not allowed does the data structure show a message box? Does it write to the console? Does it write to a file?

At the end of the day, in C# that is and many other OO languages, data structures are just good solid classes. If you know how to make your own ArrayList class, which is an array that can grow and shrink, and you can make it in a way that the class will never be in a bad state, regardless of how irresponsible or clever the programmer who is using your class is, then you will know how to make good solid classes.

Then you can start looking at patterns.

I am sorry if I sound harsh in any way, it is just because I want so speak plainly and I think this is the best way to get the message across.


Recursive linear search - branch style and layout

How can I make the JavaScript more idiomatic?

By using indexOf:

var containsA = function(sentence) {
  return sentence.indexOf('A') != -1;
};

As your update specified that you want to use recursion, let's see what we can do.

Cleaning up, we can remove the else (and the extraneous semi-colon).

var containsA = function(sentence) {
  if (sentence.length === 0) {
    return false;
  }
  return sentence[0] === 'A' || containsA(sentence.substr(1));
};

Or you can write it more succinctly, but I would say at the cost of readability

var containsA = function(sentence) {
  return sentence.length > 0 &&
    (sentence[0] === 'A' || containsA(sentence.substr(1)));
};

Now we could end up creating a lot of substrings. To avoid this, we can pass an index into the string

var containsA = function(sentence, i) {
  i = i || 0;
  if (i >= sentence.length) {
    return false;
  }
  return sentence[i] === 'A' || containsA(sentence, i + 1);
};

Or

var containsA = function(sentence, i) {
  i = i || 0;
  return i < sentence.length &&
    (sentence[i] === 'A' || containsA(sentence, i + 1));
}

In case this is not homework or a thought exercise...

Don't do this. By using recursion (and substr) to solve this, you're simultaneously making your code less efficient, in terms of both time and memory, and less readable. As @Guffa pointed out, it may even blow the stack.

The function containsA has no reason to exist. Just use indexOf where appropriate.


Java Slick StateBasedGame managing resources across states

Overall, separate your code, make it modular. Store state where it needs to be stored, but keep it separate, and expose the minimum amount of functionality that you have to.

I would highly recommend that you read the book Effective Java, and then read it again. Feedback below, separate thoughts separated by . . .

. . .

First, don't use ints for storing flags, use enums.

public static int CHOSEN_GAME_STATE;
public static final int MAINMENU = 0;
public static final int LOBBY = 3;
public static final int SINGLEPLAYERDUNGEON = 1;
public static final int MULTIPLAYERDUNGEON = 2;

What does (MAINMENU - LOBBY * SINGLEPLAYERDUNGEON / MULTIPLAYERDUNGEON) mean? It's nonsense, but it's a valid expression in your API.

Instead, it would be much better annotated as:

enum GameState {
    MAIN_MENU, LOBBY, SINGLEPLAYER_DUNGEON, MULTIPLAYER_DUNGEON
}
private static GameState sCurrentGameState = GameState.MAIN_MENU;

. . .

With

public static HashMap<String, Image> IMAGES = new HashMap<String, Image>();

You have no control over who modifies your images. Instead I'd make your images stored and loaded via their own structure, something like:

class ImageCache {
    private HashMap<String, Image> mImages = new HashMap<String, Image>();

    public Image getImage(String imageName) {
        Image result = mImages.get(imageName); //returns null if image not present.
        if (result == null) {
             result = Image.loadFromFile(imageName);
             mImages.put(imageName, result);
        }
        return result;
    }

    public boolean imageCached(String imageName) {
        return sImages.contains(imageName);
    }
}

This will give you a lot of things. First, let's say that you have too many images to store in memory, now you can easily implement an LRU cache or something to load on demand. And better yet, so long as getImage and imageCached work as they did before, this will all be hidden from the people using the ImageCache.

. . .

Instead of instantiating factories, I would recommend that you use static factories. So instead of (assuming MobFactory returns Mob objects):

public static MobFactory MOB_FACTORY = new MobFactory();

You would have something like:

class Mob {
    //Private ctor means that nobody can instantiate this directly.
    private Mob(String name, int hp, int mp) { ... }

    //Create the Ability, give it to the entity
    public static Mob create(String name, int hp, int mp) {
        assert hp > 0 : "Tried to create a mob with " + hp + " HP!";
        assert mp >= 0 : "Tried to create a mob with " + mp + " MP!";
        Mob mob = new Mob(name, hp, mp);
        //optionally manipulate the mob further
        mob.giveAbility(Ability.DANCE);
        //...
        return mob;
    }
}

You could be even more clever, and roll your templates into this, so that they are entirely hidden from the user.

. . .

Don't use Vector, it is a concurrent data structure. Since it doesn't look like you are using concurrency, stick with ArrayList.

. . .

I find it's useful to have a static settings class for things like MAX_MESSAGE_LENGTH. That way it's easy to figure out where constants are defined, and to save/load them from disk.
. . .


"String Enum" implementation - comments?

A few things don't add up for me regarding your usage.  You want compile time absolution, but dynamic values while stating the critical nature of its use.

One option:
     You can just take care of this by having objects with inheritance structures and loading your "dynamic" values from an application configuration or db.  Just have 3 layers of inheritance 1) StringEnum 2) Enum Declaration (like System638) and 3) actual values.  Just wire up the inheritance.  Mostly likely though, interfaces would be more appropriate for restrictions at the 1 & 2 layers.

Recommended (assuming the distributed smell of the use case):
     Build your functional class and bind it to a single DB table that ensures the integrity you are looking for.  Just cache the values in memory for performance needs.

Recommended (given the critical nature of its use)
     Find a different way to design the system so that you can fully embed what you need in code.  For example, a web service.  Even if you need a local presence you can utilize a web service to control the pieces you need.


Getting the Twitter account from various input formats

You can use a ruby case statement to check for different regex matches.  If a match is found, it is by definition not nil, so it removes the need for a nil check as well.

twitters.each do |twitter|
  case twitter
    # for the url
    when /twitter.com\/([^\/.]*)$/
      puts $1

    # for @ beginning
    when /^@([^\/.]*)$/
      puts $1

    # if we arrive, we haven't found any coincidence
    else
      puts twitter
  end
end

When ruby does a regex match, any capture groups are assigned to the global variables $1, $2, $3, and so on.  See this question on StackOverflow for more details about the mechanic.

Note on your regex: If you want the capture groups to actually contain anything, you should change the asterisk to a plus: ([^\/.]+).  Without the plus, "www.twitter.com/" will be captured by one of the regex.  With the plus, that string will fall into the default case.


The way it's phrased, this isn't really a Code Review question. Still, I'll try to treat it as one.

But first, to answer your question: Yes, I would imagine you could just remove the call to clearRect as long as the background image you're drawing is entirely opaque.

(incidentally, you should just be able to try it out and see quicker than asking question on a Stack Exchange site: Just comment out the line and see what happens.)

However, in code review terms, I have to wonder why you're drawing the image on every update. If the background image doesn't constantly change, you could just consider keeping the clearRect call, but setting the background image as a regular CSS background on the canvas element. Then you don't need to worry about drawing the background yourself; it'll just be there, behind the canvas element.

Also, what's with the while loop? You've just written a complicated equivalent to a for-loop. Given its purpose here, a for loop would by far be the most straightforward thing to use:

for(var i = 0, l = objects.length ; i < l ; i++) {
  objects[i].redraw();
}

While you're at it, please give your code some room to breathe. Apart from indentation, I don't see a single space anywhere in your code. It's all squished together, which makes it unnecessarilyhardtoread.

And you're missing a semi-colon in the while loop. JS will still run your code, but I see no reason to rely on JS's automatic semi-colon insertion. Especially not on the one line that must work to avoid an infinite loop.


Remove code duplication inside of a loop preserving performance

I find this line hard to read:

bool merged{false}; AABB a{mSource.back()}; mSource.pop_back();

Please split variables up 1 per line.
Yes the new syntax allows {} for list initialization. But these are not lists so it seems confusing to me (this one is more personal bias so feel free to ignore).

bool   merged(false);
AABB   a(mSource.back());
mSource.pop_back();

Pass large parameters by const reference if you can.
It saves a copy and you don't seem to need the copy (since you are not modifying the value (Note the eraseRemove() call has no effect externally and does not effect the rest of the code)).

vector<AABB> getMergedAABBSLeft(vector<AABB> const&   mSource)
                                     //     ^^^^^^^^

Since your four functions are identical apart from one method call you could write a generic version and pass that method as a parameter:

vector<AABB> getMergedAABBSLeft(vector<AABB> mSource)
{    return getMergedAABBSGeneric(mSource, &AABB::getRight);
}


Does this open up the possibility for any buggy behavior that I don't know about?

It's not likely to. The if statement is fine, though it can be made cleaner:

Rather than selecting the element twice (once to check to see if it exists, another time to call addEventListener on it), save it in a variable:
const button = document.getElementById('button01');
if (button) {
  button.addEventListener('click', newThing);
}

Or, if you're writing JS which gets transpiled for production (which, in a professional or larger project, you really should be), use optional chaining:
document.getElementById('button01')?.addEventListener('click', newThing);

But the fundamental issue remains - the HTML layout is completely disconnected from the JavaScript. Having to check to see if a button exists in the first place is something that ideally shouldn't be an issue to worry about, at least in a larger or more professional project. What if you had not 1, but 3 or 5 or 10 elements with handlers on different pages, all of which may or may not exist? The codebase would be more difficult to maintain than it should be.
There are a few solutions for this:

One option is to have a separate

where thingSelection.js adds the event listener. But that'll require a separate request to the server, which may be a problem on a large page on HTTP 1.1 - if you have lots of different scripts like this, the sheer number of parallel requests could slow things down, especially for users with high latency. (The HTTP/2 protocol doesn't have issues with additional connections to the same server IIRC)
(You could also inline the script, eg </form>but I prefer putting scripts in separate files to permit caching)

But if it were me, I'd strongly prefer to fully integrate the creation of the HTML with the event listeners for that HTML, to make it completely inconceivable of one existing without the other, using a framework. For example, with React, you could do something along the lines of:

const Things = () => {
    const [item, setItem] = useState('');
    const [selectedOption, setSelectedOption] = useState('foo');
    const clickHandler = (e) => {
        const fn = options[selectedOption];
        if (fn) {
          setItem(fn());
        }
    };
    return (

            <select value={selectedOption} onChange={e => setSelectedOption(e.currentTarget.value)}>
                <option value="foo">foo</option>
                <option value="bar">bar</option>
            </select>
            <button onClick={clickHandler}>click</button>
            <output>{item}</output>

    );
};

const options = {
  foo: () => 'foo',
  bar: () => 'bar',
};

const Things = () => {
    const [item, setItem] = React.useState('');
    const [selectedOption, setSelectedOption] = React.useState('foo');
    const clickHandler = (e) => {
        const fn = options[selectedOption];
        if (fn) {
          setItem(fn());
        }
    };
    return (

            <select value={selectedOption} onChange={e => setSelectedOption(e.currentTarget.value)}>
                <option value="foo">foo</option>
                <option value="bar">bar</option>
            </select>
            <button onClick={clickHandler}>click</button>
            <output>{item}</output>

    );
};
ReactDOM.render(<Things />, document.querySelector('.react'));

Then when you're on a page where the Things are needed, you just render <Things />. With this approach, the HTML its associated JS handlers are all completely self-contained in the Things. There's no need to use selectors that may have unintentional collisions. (For example, going with your original code of document.getElementById('button01'), what if some other completely separate section of the HTML on one of the pages had an element which accidentally used the same ID? Then you'll have problems.)
Using a framework like this is admittedly a lot to learn, but it makes codebases significantly more maintainable. It's well worth it for medium-size or larger projects, IMO.

On a different note, your current code could also be improved a bit:
Prefer selectors Selector strings are usually more easily understood and more flexible than other methods of selecting elements (like getElementsByTagName and document.forms.someFormName.someFormElement). The selector string that matches an element will line up with the CSS selector that styles the element. For example, I'd replace:
document.forms.thingSelection2.type.value

with
document.querySelector('#thingSelection2 [name=type]').value

and
const output = document.getElementsByTagName('output')[0];

with
const output = document.querySelector('output');

(no need to select a collection when you only need the single matching element)
Save the value Instead of selecting and extracting the value twice, write DRY code; put it into a variable:
const { value } = document.querySelector('#thingSelection2 [name=type]');
if (value in options) { // Or, use `!options[value]
  return false;
}


Codility's stonewall 100% solution - different paths such as if,else if,etc, do the same thing

Some of these branches can be eliminated. Let's see:

if (heightsStack.Count > 0)
{
  if (heightsStack.Peek() < H[height])
  {
    blocksCounter++;
    heightsStack.Push(H[height]);
  }
}
else
{
  blocksCounter++;
  heightsStack.Push(H[height]);
}

Here, there are two conditions where we execute the two common lines: heightsStack.Count <= 0 or heightsStack.Peek() < H[height]. Since Count  can't be negative, let's rewrite it as such:

if (heightsStack.Count == 0 || heightsStack.Peek() < H[height])
{
  blocksCounter++;
  heightsStack.Push(H[height]);
}

if (heightsStack.Count > 0)
{
  while (heightsStack.Count > 0 && heightsStack.Peek() > H[height])
  {

The first if-statement is redundant. If heightStack.Count > 0 is false, the while statement will be false as well. We would immediately fall through and end up at the if-statement in the section above. This if-statement would evaluate to true, since heightStack.Count == 0 still. And it would execute the two common lines.

This would leave the body of the for-loop as follows:

while (heightsStack.Count > 0 && heightsStack.Peek() > H[height])
{
  heightsStack.Pop();
}
if (heightsStack.Count == 0 || heightsStack.Peek() < H[height])
{
  blocksCounter++;
  heightsStack.Push(H[height]);
}

Note, that we only have one branch with the two common lines.

There's one last thing. You're using a for-loop, and only using the looped variable to index directly. Instead, consider using a foreach loop:

public int solution(int[] H)
{
  Stack<int> heightsStack = new Stack<int>();
  int blocksCounter = 0;

  foreach(int height in H)
  {
    while (heightsStack.Count > 0 && heightsStack.Peek() > height)
    {
      heightsStack.Pop();
    }
    if (heightsStack.Count == 0 || heightsStack.Peek() < height)
    {
      blocksCounter++;
      heightsStack.Push(height);
    }
  }
  return blocksCounter;
}

As far as algorithm goes, you do what I would have come up with, so I have nothing more to say about that.


More elegant array multiplication than multiple concat statements

Here's one that uses call and manually concat a

var a = ['a','b','c'], b = Array.prototype.concat.call(a,a,a,a);

If you wanted an easy way to variably add a, just change i:

for(var a=["a","b","c"],b=[],i=4;i--;) b.push.apply(b,a);


Multidimensional array of objects that retrieves the id, title, and smallest box art URL for every video

Arrow Functions and ternaries

I'll start by saying that you can simplify syntactically using ternaries and ES6 Arrow functions without losing readability. These will make the code look smaller and simpler, despite being the same solution.

It would look like this:

const arr1 = movieLists.map(outterItem =>
    outterItem.videos.map(innerItem =>
        [innerItem.boxarts
            .reduce((acc, currentValue) => acc.width * acc.height < currentValue.width * currentValue.height ?
                acc :
                currentValue
            )
        ]
        .map(lastItem =>
            ({
                id: innerItem.id,
                title: innerItem.title,
                boxart: lastItem.url
            })
        )
    )
)

Naming

The names you gave in to some variables/parameters aren't very good and make the code somewhat hard to read.

For instance in the maps:

.map(function (outterItem) {
    return outterItem.videos
        .map(function (innerItem) {

Both outterItem and innerItem are too vague to actually mean anything. And you can't really know what they hold unless you take a another look at the data. The outterItem is the first level object of the array, which is a sort of category object, while the innerItem would be each video of the videos array.

Solution

The solution itself seems overly complex for the problem at hand, because you do a double map, to a reduce, to a final map. And then you flatten the result with reduce. Then flatten is needed because the generated structure is deeper than you needed it to be.

A way simpler solution would be:

2 for loops to run through each video
Find the smallest box art in the video with reduce
Add the desired object to the resulting array with push

This avoids the need to flatten anything.

Following this logic, the whole code would become:

const finalVideos = []
for (let category of movieLists){
    for (let video of category.videos){
        const smallestBoxArt = video.boxarts.reduce((acc, curr) => acc.width * acc.height < curr.width * curr.height ? acc : curr)
        finalVideos.push({
            id: video.id,
            title: video.title,
            boxart: smallestBoxArt.url
        });
    }
}

console.log(finalVideos)

If you feel the reduce line as a bit too long or just repeats the area code too much, you can even create a simple function for it:

const area = obj => obj.width * obj.height;

And change the reduce to use it:

const smallestBoxArt = video.boxarts.reduce((acc, curr)=>area(acc) < area(curr) ? acc:curr)


Parsing a tabular log file into a hashmap

This is indeed way too complex. In fact I think there's enough responsibilities in this one method to be spread over a few classes.

managing the history file
parsing history records
processing history records
counting lines
logging

These points may map to simply a helper method or a separate class.

Try organizing this behavior in several levels of abstraction. Try checking the existence of the file prior to tryig to parse its contents. Making a class that models the content of one record will definitely help. It'll make a good home for some of the validation and parsing logic. Don't be afraid to make private helper methods. They can greatly improve readability and lower complexity per method significantly.

The top level version of this method could look like this :

public ExchangeHistory loadHistory(String path) {
    List<HistoryRecord> records = new ArrayList<>();
    for (String line : new LineIterable(getHistoryReader(path))) {
        records.add(HistoryRecord.fromTokenizedString(line));
    }
    return ExchangeHistory.fromHistoryRecords(nonNulls(records));
}

In which

getHistoryReader(path) will fetch or create the file and wrap it as a reader.
LineIterable abstracts reading the BufferedReader to an Iterable (enabling the advanced for loop)
HistoryRecord parses the tokens of a line, and can be queried for the parsed data.

fromTokenizedString(line) returns a valid HistoryRecord or null

nonNulls() filters nulls out of a List
ExchangeHistory.fromHistoryRecords() creates the ExchangeHistory based on a List/Iterable of HistoryRecords

As for logging, it always clutters code. Your error logging currently seems to be a placeholder for decent exception handling. The other logging appears to be used for debugging. I like AOP to do logging, since it allows you to focus on the code, and the logging can be coded separately.