Reusable methods

Notice that you have several methods that essentially do the same thing. Log and both writeCustomErrorLog methods follow the same approach:

StreamWriter sw = null;
try
{
    sw = new StreamWriter("some file path", true);
    sw.WriteLine("some string");
    sw.Flush();
    sw.Close();
catch (Exception ex)
{
    throw ex;
}

This can easily be put into a reusable method:

private void WriteMessageToFile(string filepath, string message)
{
    StreamWriter sw = null;
    try
    {
        sw = new StreamWriter(filepath, true);
        sw.WriteLine(message);
        sw.Flush();
        sw.Close();
    catch (Exception ex)
    {
        throw ex;
    }
}

And then your methods become much simpler and less repetitive:

public void writeCustomErrorLog(string location, string message,string fileName)
{
    string filePath = location + "\\" + fileName +".txt";
    string logMessage = DateTime.Now.ToString() + " : " + "\n" + message;

    WriteMessageToFile(filePath, logMessage);
}

Catching and throwing

try
{
   // ...
}
catch (Exception ex)
{
    throw ex;
}

Catching an exception should be done when you want to handle the exception.
Throwing an exception should be done when you don't want to handle the exception (or raise a new exception)

There is no point to catching an exception, only to then throw it. That is essentially like queueing at a supermarket checkout when you don't have any items you want to buy. There's no point to doing so.

The try/catch can be removed altogether in these cases. Note that the try/catch could be useful if you actually did something with it:

try
{
   // ...
}
catch (Exception ex)
{
    Trace.Write("Exception occurred here!");

    throw;
}

Note the different between throw ex; and throw;.

throw ex; resets the stacktrace to where you call throw ex;. This effectively removes deeper stack trace information
throw; retains the stack trace of the exception as it was initially raised. This does not remove data.

Throwing an existing exception is rarely a good idea.

Hardcoding

AppDomain.CurrentDomain.BaseDirectory + "\\Log.txt"
AppDomain.CurrentDomain.BaseDirectory + "\\PassiveLog.txt"

Don't hardcode your location, nor your filename.

You intend to use this in future projects. It's not unforeseeable that you're going to want to decide where to put the file, or what you should name it (especially if you want to use more than one log file in the same application).

Also, I have no idea what a "passive log" is. The name isn't very descriptive.

String concatenation

DateTime.Now.ToString() + " : " + "\r\nSource " + ex.Source.ToString().Trim() +
"\r\nMessage : " + ex.Message.ToString().Trim() +
"\r\nInner Exceptions: " + Convert.ToString(ex.InnerException) +
"\r\nException thrown from: " + getExceptionGeneratedMethod(ex) +
"\r\nLine Number: " + getExceptionGeneratedLineNumber(ex) +
"\r\nStack Trace: " + ex.StackTrace.ToString()

Do not concatenate strings with +.

Strings are immutable. That means that you can't change a string, you can only create a new string (and the old one will be discarded.

Let's take a simple example:

"a" + "b" + "c" + "d"

These concatenations are done step by step. Look at the needed memory allocation:

Create a 1 character string (a)
Create a 1 character string (b)
Create a 2 character string (a+b)
Create a 1 character string (c)
Create a 3 character string (ab+c)
Create a 1 character string (d)
Create a 4 character string (abc+d)

To concatenate 4 strings, you're had to allocate 7 strings.
For a 4 character result, you've effectively allocated 13 characters' worth of strings.

This problem quickly grows out of proportion and leads to bad performance, massive memory usages, and eventual OutOfMemoryExceptions being thrown. Furthermore, avoiding this is very simple:

Option 1 - String.Format

This is more useful for cases where you are replacing a fixed set of placeholders.

string result = String.Format("{0} is the father of {1}", nameOfFather, nameOfChild);

Note that interpolated strings are a fairly recent addition that simplify the syntax:

string result = $"{nameOfFather} is the father of {nameOfChild}";

Option 2 - StringBuilder

This is more useful for cases where you dynamically generate a string, instead of replacing a fixed set of placeholders:

StringBuilder sb = new StringBuilder();

sb.Append(nameOfFather);
sb.Append(" is the father of ");
sb.Append(nameOfChild);

string result = sb.ToString();

I repeat again: Do not concatenate strings with +. Especially since interpolated strings have been added; there's no reasonable "simply syntax" argument to using + concatenation.

  Addendum

  Piedar made a really interesting remark in the comments. Apparently, string + string concatenation is now converted to String.Concat
   by the compiler. This effectively negates the issue I'm pointing at.

  However, I still urge you to not do + concatenation on big string anyway, from a readability perspective. value + " seconds" is readably enough, but you're pasting a lot of different things together and the code starts looking bulky and ugly.
  However, this is a style (and, in extreme cases, readability) argument, not a technical one.

Statics

Everything you've listed is static. Why?

This issue is strongly related to why you hardcoded your values. You think that your single log is a catch-all for any future application's needs and you'll never need to tweak it or change it. You're effectively making it so that a future consumer of your library has no configuration options.

This is inherently bad design. This is equivalent to Word being able to save files in a single (hardcoded) directory, or Firefox telling you what your homepage should be without alowing you to change it.

Good applications (and libraries) give control to the user, they don't tell the user what to do.
Note: there is nothing wrong with providing a default behavior, but you need to give your user the option of changing the default behavior if they so choose.

Separation of concerns

Other than not allowing the user to change key settings, you're also handling too much here. Your one class is handling many responsibilities:

Deciding where to put the file
Accessing and writing to the file
Deciding how to format the log messages

These things should ideally be split into separate classes.

I understand that the need for separating this into classes is not that great at the moment. However, once you implement the other suggested changes (configuration options), your code will quickly grow to a size where the need for separation becomes more apparent.

Ideally, this is a better logging approach:

LogFile allows you to create a log with a specific configuration (filename, location)
ExceptionLogMessageFormatter takes an exception and turns it into a log message (string).
Logger takes a pre-formatted string message and writes it to a pre-configured logfile.

You're currently only logging exceptions, but the need for separation becomes more apparent when you want to also log other objects. If you want to e.g. log an object of type Person, you'll create a separate PersonLogMessageFormatter class, but you will be able to reuse the same LogFile class (a different file with different settings, but the same class in code!) and Logger class (which logs the string message to this different log).

Don't reinvent the wheel!

This is the most important lesson to draw here. However, it does come with a caveat, which I will mention at the end.

What you've built here is a fairly simple tool, for a problem that is not new to the field of software development. (That's not an insult - in case you interpreted it as such).
Odds are that someone is already going to have created a library to handle this exact job. And they have:

NLog
Log4Net
Elmah

These tools are very well developed, offer an incredibly amount of configuratbility, and are able to handle fringe cases that you probably haven't even thought of but will eventually have to implement a solution for.

Personally, I'm a big fan of NLog. I've never come across a need for logging that could not be handled by NLog. I like how it works, and I find that it gives me a perfect balance of good default behavior and exceedingly configurable custom behavior if I need it.

If you were going to write a story about a world based on medieval fantasy with magic and multiple humanoid races; I would suggest that you watch e.g. the Lord of the Rings movies (or read the books of course).
Even if your story is going to be different, it gives you a nice overview of the ins and outs of your story's setting, pitfalls to avoid, how to create interesting an immersive plots, ...

Similarly, if you want to write a logging framework, I suggest that you first try working with existing tools.
Even if your tool is going to do something new; seeing the existing framework gives you a good overview of what features you're going to need to add to your tool to make it user friendly, and it helps you draw the line on how much configuration options you need to add to it to fulfill most needs.

  Caveat
  However, writing your own logging framework from scratch (with no knowledge of existing frameworks) can be rewarding in and of itself as a training exercise.

  Based on your code, I surmise that you are a beginner, at least to creating reusable libraries. So if writing this library benefits you for training purposes, don't let my review stop you.

  But if your only goal is to create a functionally usable tool, then I do suggest first looking for existing solutions, either to already solve your problem or even just to get some inspiration/ideas.


Public Enum comparisonOperator
    '/ Used [[ ]] because the outer set escape the inner brackets for the Enum list, which will then escape the operator when chosen as a value. E.G. they will appear as "Operator = [<]" in code.
    [[<]] = 1
    [[<=]] = 2
    [[=]] = 3
    [[>=]] = 4
    [[>]] = 5
End Enum

Why not just name them and comply with the language's rules for user identifiers? Also, Enum constants should be enumerated: let the compiler assign the underlying values:

Public Enum ComparisonOperator
    EqualTo = 0
    LessThan
    LessThanOrEqualTo
    GreaterThanOrEqualTo
    GreaterThan
End Enum

There should be a sensible default (0) value; I think I'd make EqualTo that default.

The operators in Case Is = ... are redundant, but the magic numbers in the Case expressions should be the enum named constants. The underlying values should be abstracted away behind the enum - the underlying values should be meaningless.

Consider:

    Case ComparisonOperator.LessThan
    isTrue = sourceValue < comparisonValue

    Case ComparisonOperator.LessThanOrEqualTo
    isTrue = sourceValue <= comparisonValue

    Case ComparisonOperator.EqualTo
    isTrue = sourceValue = comparisonValue

    Case ComparisonOperator.GreaterThanOrEqualTo
    isTrue = sourceValue >= comparisonValue

    Case ComparisonOperator.GreaterThan
    isTrue = sourceValue > comparisonValue

That way nothing breaks if the enum members are reordered.


/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="o">Item to be cached</param>
/// <param name="key">Name of item</param>
public static void Add<T>(T o, string key)

I think this signature would be more useful like this:

/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs.
/// </summary>
/// <typeparam name="T">Type of cached item (inferred from usage).</typeparam>
/// <param name="key">A string used to uniquely identify the added value.</param>
/// <param name="value">Item/value to be cached.</param>
public static void Add<T>(string key, T value)

This is misleading:

/// <summary>
/// Remove item from cache
/// </summary>
/// <param name="key">Name of cached item</param>
public static void Clear(string key)
{
    HttpContext.Current.Cache.Remove(key);
}

Clear is usually a parameterless method that clears a container's contents. This method should be named Remove.

/// <summary>
/// Check for item in cache
/// </summary>
/// <param name="key">Name of cached item</param>
/// <returns></returns>
public static bool Exists(string key)
{
    return HttpContext.Current.Cache[key] != null;
}

"Check for item in cache" doesn't really say what's going on. I realize it's trivially inferred from the method's name, but "Checks for existence of specified key in cache." would be totally unambiguous.

public static bool Get<T>(string key, out T value)

I don't like the implementation of that method very much, there are too many exit points, some being redundant. Consider something like this:

public static bool TryGet<T>(string key, out T value)
{
    bool result;

    try
    {
        if (Exists(key))
        {
            value =  (T)HttpContext.Current.Cache[key];
            result = true;
        }
    }
    catch(InvalidCastException)
    {
        value = default(T);
    }

    return result;
}


Handlers for checkboxes to select various map features

you only have a difference in two places: query and attribute of dockwidget you are going to check against. so you can pass them as parameters to your score function.

def score(self, query, check_attr):
    opp_selectedLayerIndex = self.dockwidget.areaOpporunityMap_combo.currentText()
    opp_sel_layer = QgsMapLayerRegistry.instance().mapLayersByName(str(opp_selectedLayerIndex))[0]
    # Select all features where "Score" = NULL
    expr = QgsExpression(query)
    it = opp_sel_layer.getFeatures(QgsFeatureRequest(expr))
    ids = [i.id() for i in it]

    checkbox = self.dockwidget.getattr(check_attr)

    if checkbox.isChecked():
        opp_sel_layer.select(ids)
    else:
        opp_sel_layer.deselect(ids)


Display element in a grid and conditionally add to redux or remove if not selected

First of all, there's way too much going on here for an inline function. Let's seperate that out. Also, the splicing can be replaced with the more expressive filter.
const addIfMissing = (array, item) =>
    array.includes(item) ? array : array.concat([item]);

const without = (array, exclude) =>
    array.filter(x=> x!==exclude);

const onGridSelectorChange = (categoryId, isSelected) => {
    favorites[categoryId] = isSelected;
    const categoryIds = Object.values(onboarding.favoriteCategories);
    const newCategoryIds = isSelected ?
        addIfMissing(categoryIds,categoryId) :
        without(categoryIds,categoryId);

    dispatchData({ [DATA_KEY.FAVORITE_CATEGORIES]: newCategoryIds})

}


Marionette.js Mediator ('global' commands vs app controller)

I have the exact same quandary (and found this question googling for answers). On first read through my opinion was that triggering events on the modules themselves was the best approach on the basis that this loosens any coupling between the modules and other components. However, having given it more thought it seems that this is exactly what the Wreqr interface has been designed for, so actually my approach would be to use the aggregator to handle events. I’d be interested to know which approach you decide to take.


Use nconc instead of append for speed in

(setf merged-list (append merged-list (list a)))

Note that you need setf with nconc only if merged-list is nil.
Use pop: instead of

(let ((a (first lower)))
  (setf merged-list (append merged-list (list a)))
  (setf lower (rest lower))))

you can write

(setf merged-list (nconc merged-list (list (pop lower))))
The combination of let and loop creates extra indentation for no good reason. You can use with clause in loop to create bindings instead, or use the do macro.
Usually predicates are named with a -p suffix, so I suggest that you rename your small-list to small-list-p.
Please fix line breaks and indentation in the third cond clause.


Finding the difference in time between two events for a specific ID

Some general suggestions:

Run the code through pycodestyle to get several suggestions for writing idiomatic Python:

  indentation is not a multiple of four

  missing whitespace after ','

  multiple spaces after operator

  line too long

  do not use bare except' [sic]

  block comment should start with '# '

Don't use single letter variables - it makes it very hard to maintain the code.
Don't shadow common names like time - if you ever need the time module in the code you'll have to rename the import or your variable.
You have unnecessary outer parens in the time = … line.
Indexing by a non-trivial expression is confusing. Assign x.id == j to a variable first so the maintainer knows what it means.
feature_final is accessed like a global. You should instead pass it in.
If you're processing a huge list you might want to try returning an iterable instead of a list - that way other code can start processing the output before it's complete.


Cross product to find Lorentz force in C++

There are a few things here that could improve your code (whether the code becomes more succinct or not is a matter of debate, but it will definitely become more readable):

Do not use using namespace std, it is considered bad practice as it can lead to subtle name resolution errors and makes the code (in my opinion) more difficult to read because you now have globals, for example cout and cin in your program which are not easily recognizable.
You do not need to define all variables at the top of each function, and, in fact, should favor to declare your variables as locally as possible, because this helps people keep track of what is currently important while reading your code. Nobody wants to keep track of several different, obscure variables whose meaning and usage is only introduced several lines deeper down.
It is completely unclear to me why you wrote double F = (0.0, 0.0, 0.0); which is equivalent to double F = 0.0; but very obscure.
Even more obscure, why do you even have a variable named F if you never use it? Remove it.
Rethink operator spacing. Ultimately, this issue comes don to personal preference, but please consider adding a space before and after each binary operator. Lines such as i_X= (v[1]*B[2])-(v[2]*B[1]); are much harder to parse quickly and correctly than i_X = (v[1] * B[2]) - (v[2] * B[1]);.
Do not use std::endl unless you have a good reason to, as it not only creates a line break but also flushes the stream, which can be detrimental to performance. Although it is unlikely to matter in your case, you might find yourself working with situations where it does matter, so you should make a habit out of preferring '\n'.
Concatenate adjacent string literals when writing to std::cout. You could get away with fewer calls to operator<< if you wrote cout << "F =  " << q * i_X << "i + " << q * j_X << "j + " << q * k_X << "k\n"; instead of cout << "F =  "<< q* i_X << "i" << " + " << q* j_X << "j" << " + " << q* k_X << "k" << endl;.
Omit return 0;. The compiler automatically inserts it for you (although only in main).
You do no input checking at all. If a user enters anything that can not be parsed as a double by cin you will be running into undefined behavior because of uninitialized variables. Depending on your use case, this might be fine, but if you ever intend to have somebody else use this program, you should add some checks to make sure you actually receive valid input.
You should consider letting the user input all three values of a vector on the same line, not only to make your code a little more succinct, but also to make it a little more user-friendly.


Given hexadecimal rgb color, find the closest predefined color

Fixing and improving the short regex handling

This won't work as intended:

hex = hex.replace(shortRegEx, function(full, r, g, b) {
  return [r, r, g, g, b, b].join();
});

The default separator of join is a comma, so this will join the hexadecimal digits by commas, when you want empty string: .join("")

In any case, instead of the anonymous function + array + joining,
it will be simpler to use the capture groups of the regex in the replacement string:

hex = hex.replace(shortRegEx, "$1$1$2$2$3$3");

Don't repeat yourself

Instead of repeating parseInt(x, 16) multiple times here:

  r: parseInt(rgbArray[1], 16),
  g: parseInt(rgbArray[2], 16),
  b: parseInt(rgbArray[3], 16)

It would be better to create a function for it.

The same goes for this snippet in the distance calculation:

    Math.pow((rgbObj.r - currentColor.rgb.r), 2) +
    Math.pow((rgbObj.g - currentColor.rgb.g), 2) +
    Math.pow((rgbObj.b - currentColor.rgb.b), 2)

When defining the static colors here,
it's not obvious that the rgb values are correct:

  { color: 'red',    hex: '#EA4235', rgb: { r: 234, g: 66,  b: 53  } },
  { color: 'green',  hex: '#34A853', rgb: { r: 512  g: 168, b: 83  } },
  { color: 'blue',   hex: '#4285F4', rgb: { r: 66,  g: 133, b: 244 } },
  { color: 'yellow', hex: '#FBBC05', rgb: { r: 251, g: 188, b: 5   } }

You could make it obvious by reusing the hexToRgb function to create the rgb values.


Python one-liner to print permutations of a string

Even though the code fits a single line, this is not Pythonic. Just because Python allows to put a lot of things on a single line because of list comprehensions, ternary operators and other syntactic sugar, it does not mean you have to try making your code as short as possible. Remember the Zen of Python, in particular:

  Explicit is better than implicit.

  Flat is better than nested.

  Readability counts.

  If the implementation is hard to explain, it's a bad idea.

Expand the solution into multiple lines using meaningful variable names and comments:

def permute(c, s=""):
    """Returns all possible permutations of a string c."""
    if len(s) == len(c):  # base case - returns a single variation of a string - string itself
        return [s]
    else:
        variations = [s[0:i] + c[len(s)] + s[i:] for i in range(len(s) + 1)]

        # get all permutations for each variation of a string
        permutations = [permute(c, e) for e in variations]

        # flatten a list of lists
        return [x for y in permutations for x in y]

print(permute("omer"))


Few suggestions:

Input validation: there is no input validation for win_size and padding. If win_size is -3 the exception says ValueError: Stride must satisfy 0 < stride <= -3.. If padding is a string, numpy throws an exception.

Type hints: consider adding typing to provide more info to the caller.

f-Strings: depending on the version of Python you use, the exception message can be slightly simplified.
From:
if not (0 < stride <= win_size):
    fmt = 'Stride must satisfy 0 < stride <= %d.'
    raise ValueError(fmt % win_size)

To:
if not 0 < stride <= win_size:
    raise ValueError(f'Stride must satisfy 0 < stride <= {win_size}.')

Duplication: the statement shape = n_windows, win_size seems duplicated and can be simplified.
From:
if padding is not None:
    n_windows = ceil(n_elems / stride)
    shape = n_windows, win_size
    A = np.pad(A, (0, n_windows * win_size - n_elems),
             constant_values = padding)
else:
    n_windows = floor(n_elems / stride)
    shape = n_windows, win_size

To:
if padding is not None:
    n_windows = ceil(n_elems / stride)
    A = np.pad(A, (0, n_windows * win_size - n_elems),
                   constant_values=padding)
else:
    n_windows = floor(n_elems / stride)
shape = n_windows, win_size

Warning: FYI on the doc of np.lib.stride_tricks.as_strided there is a warning that says This function has to be used with extreme care, see notes.. Not sure if it applies to your use case but consider checking it out.


After sitting on this for... 8 hours, I realized singular() can, and probably should, use the replaceLast() method, to encourage code re-use at a very small expense of efficiency (which isn't the goal here anyways):

private static String singular(ChronoUnit unit) {
    return replaceLast(unit.toString(), "s", "");
}


For lines like

if ('Who').lower() in sent_split or 'Who' in sent_split:

you have some extra brackets and can do some manual constant folding, to get

if 'who' in sent_split or 'Who' in sent_split:

Perhaps you meant

if 'who' in read_str.lower().split():

?

Assuming so, this would simplify the main part to

if  'who' in read_str.lower().split()::
    result = 'person'
    break
elif 'where' in read_str.lower().split(): and 'what' not in read_str.lower():
    result = 'location'
    break
elif 'why' in read_str.lower().split()::
    result = 'reason'
    break
elif 'how many' in read_str.lower():
    result = 'count'
    break
elif 'when' in read_str.lower().split(): and 'what' not in read_str.lower():
    result = 'time'
    break
elif 'how' in read_str.lower().split(): and 'how long' not in read_str.lower() and 'how much' not in read_str.lower():
    result = 'manner'
    break

This can be encoded in a data structure:

# Give this a better name
def process(read):
    in_notin_result = (
        ('who',      [],       'person'),
        ('where',    ['what'], 'location'),
        ('why',      [],       'reason'),
        ('how many', [],       'count'),
        ('when',     ['what'], 'time'),
        ('how',      ['how long', 'how much'], 'manner'),
    )

    words = read.split()

    for in_sent, notin_read, result in in_notin_result:
        if in_sent in words and all(disallowed not in read for disallowed in notin_read):
            return result

    return None

This says that each if is of the form

word which must be in read.split()
some words (0, 1 or 2) which must not be in read
a result value

We put that in the data structure and loop over it:

for in_sent, notin_read, result in in_notin_result

We check the in_sent in words and that disallowed not in read for every disallowed in the words which must not be in read. If this matches, we return result, otherwise we go to the next iteration of the loop.

This can be simplified:

elif sub_link in links_list2:
    check_yn = verify_yesno(post_script_link_list)
    if check_yn == 1:
        result = 'Yes/No'
        break
    elif check_yn == 0:
        result = 'noresult'
        break
    break

to

elif sub_link in links_list2:
    check_yn = verify_yesno(post_script_link_list)
    result = 'Yes/No' if check_yn else 'noresult'
    break

Note further that

('How long').lower() not in read_str and 'How long' not in read_str and ('How much').lower() not in read_str and 'How much' not in read_str:

will always be True because of the first filter agains bad_strings.

I would short-circuit the return instead of breaking. It seems to me that you want to return noresult on no result, but you default to ''. If you just default to 'noresult this would be easier.

for linking in post_script_link_list:
    sub_link = re.search('\((.*?)\)', linking).group(1)

    if sub_link in ['Wq','Ws','Wi','Wd']:
        process_result = process(read_str)
        if process_result is not None:
            return process_result

    elif sub_link in ['Qd']:
        return 'Yes/No' if verify_yesno(post_script_link_list) else 'noresult'

return 'noresult'

but if you do really want to distinguish these cases you'll need to use your original variant:

result = ''
for linking in post_script_link_list:
    ...

    else:
        result = 'noresult'

return result

It's not clear because I don't know what the code is meant to do.

This is what I've ended with. I don't know if it works:

# Give this a better name
def process(read):
    in_notin_result = (
        ('who',      [],       'person'),
        ('where',    ['what'], 'location'),
        ('why',      [],       'reason'),
        ('how many', [],       'count'),
        ('when',     ['what'], 'time'),
        ('how',      [],       'manner'),
    )
    words = read.split()
    for in_sent, notin_read, result in in_notin_result:
        if in_sent in words and all(disallowed not in read for disallowed in notin_read):
            return result
    return None

def process_link(post_script_link_list, read_str):
    read_str = read_str.lower()

    for item in ('how long', 'how much'):
        if item in read_str:
            return

    for linking in post_script_link_list:
        sub_link = re.search('\((.*?)\)', linking).group(1)

        if sub_link in ['Wq','Ws','Wi','Wd']:
            process_result = process(read_str)
            if process_result is not None:
                return process_result

        elif sub_link in ['Qd']:
            return 'Yes/No' if verify_yesno(post_script_link_list) else 'noresult'

    return 'noresult'


For something like this, numpy is probably unbeatable in terms of performance, since its underlying functions are implemented in C.

In your case the implementation would be rather easy:

import numpy as np

def random_hyperrectangular_matrix(dimensions, min_dim_size, max_dim_size):
    shape = np.random.randint(min_dim_size, max_dim_size + 1, dimensions)
    return np.random.randint(0, 101, shape)

Note the +1 on the upper limits, since np.random.randint excludes the upper bound, whereas random.randint includes it.

Note also that dimensions is in this case limited to be less than or equal to 32 (because of limits in the internal implementation). Your algorithm will only fail once the recursion depth reaches the limit (1000 by default). But if you need more than 32 dimensions, you are probably doing something wrong, anyways...

This might also make the rest of your program (which you don't show) easier, because operations can act on each element of the array, or only certain dimensions.

With min_dim_size, max_dim_size = 2, 2 (the lowest practical numbers, and the same to eliminate one source of randomness in the timings) this consistently improves the timing (note the logarithmic scale on the y-axis):

Especially noteworthy is that at low dimensions, the numpy approach is basically constant in time. For higher dimensions it scales the same as your approach, but consistently lower.


Displaying Video file from storage with MediaStore in android Java

There are no big issues with your method, but I noticed something that can be improved:
Variable declaration
In Java is not common to declare the variables at the beginning of the method:
Uri uri;
Cursor cursor;
VideoAdapter videoAdapter;
int column_index_data,thum;

String absolutePathThumb;

Declare and initialize variables in the same place, it's easier to read and maintain.
By the way, videoAdapter is declared but never used, that's another reason why you should avoid doing this.
Query results not used
Here you configure the projection:
String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME, MediaStore.Video.Media._ID,MediaStore.Video.Thumbnails.DATA};

But after the query you never use the results for MediaStore.Video.Media.BUCKET_DISPLAY_NAME and MediaStore.Video.Media._ID.
Close the resources
The Cursor object which is returned from the query is a resouce that needs to be closed:
cursor = getApplicationContext().getContentResolver().query(uri,projection,null,null,orderBy);

To close it automatically use the try-with-resources statement:
try (Cursor cursor = getApplicationContext().getContentResolver().query(uri,projection,null,null,orderBy)) {
// ...
}

Camelcase instead of underscore
From the Java Naming Convention:

Methods should be verbs, in mixed case with the first letter
lowercase, with the first letter of each internal word capitalized.

So instead of videoModel.setVideo_uri() use videoModel.setVideoUri() or even better videoModel.setUri(). Similarly for the other methods.
Duplicated assignment
videoModel.setDuration(duration) is called three times. Compute the value of duration first and add it to videoModel only once.
Ternary operator instead of if-else
Sometime using the ternary operator is better for readability. For example instead of:
if (extension!=null){
    videoModel.setVideo_extension(extension);
}else {
    videoModel.setVideo_extension("mp4");
}

You can get the same result with:
String extension = extension != null ? extension : "mp4";
videoModel.setVideo_extension(extension);

Documentation
In the link you posted there is an example that is very similar to your use case. Most of my suggestions are already mentioned there, read it carefully.


Loop for finding proper grouping brackets

The most common approach to bracket matching is using a stack.

Unmatched brackets you encounter are pushed onto the stack. When encountering a matching bracket pop the stack (and verify if it's the correct type of bracket, maybe not needed in your case). If the stack is empty after finding a match, the last match was the position you were looking for.

This approach, as you can see, also immediately allows for bracket matching validation, and can handle different types of brackets in the same String

To boot, the code is simpler.

Note

When looking for a stack implementation in Java, you may come across java.util.Stack; don't use that, use java.util.ArrayDeque instead, as the Stack javadoc itself suggests.


Remove duplicate elements from array along with element using Java 1.7

Use the right tools

You are on the right track to use Collections.frequency, as it does a lot of the plumbing for you

Start simple

The basic idea is to create a new list, unique that will contain the numbers that are unique.

This is the piece of code that will do all the hard work (but is not optimized yet!).

ArrayList<Integer> uniqueNumbers = new ArrayList<>();

for (Integer number : numbers) {
    if (Collections.frequency(numbers, number) == 1) {
        uniqueNumbers.add(number);
    }
}

Now you only need to convert your input array to an numbers ArrayList and convert the resulting uniqueNumbers list back to an array.

Then optimize

If you care about performance, you will probably want to skip number that you have already checked and know that they are non unique. You can do this with a Set<Integer>.

Set<Integer>  nonUniqueNumbers   = new HashSet<>();
ArrayList<Integer> uniqueNumbers = new ArrayList<>();

for (Integer number : numbers) {

    if (!nonUniqueNumbers.contains(number)) {
        if (Collections.frequency(numbers, number) == 1) {
            uniqueNumbers.add(number);
        }
        else {
            nonUniqueNumbers.add(number)
        }
    }
}

More room for performance

To increase the performance, you only want to scan each input element once; this can for example be done like this:

public static Collection<Integer> getUniques( Collection<Integer> input )
{
    Set<Integer> uniques = new HashSet<>();
    Set<Integer> nonUniques = new HashSet<>();
    for ( Integer i : input )
    {
        //if i is in the nonUnique set, we already have it twice,
        //no need to do anything with it anymore
        if ( nonUniques.contains( i ) )
        {
            continue;
        }
        else
        {
            //either we never saw i, or we have seen it once.
            //if we saw it once, it will be in the uniques.
            if ( uniques.contains( i ) )
            {
                //if it was in the uniques , this is the second time we encountered it.
                //remove it from the uninques and put it in the nonUniques
                uniques.remove( i );
                nonUniques.add( i );
            }
            else
            {
                //if we never saw i, it should be in the uniques set (for now)
                uniques.add( i );
            }
        }
    }
    return uniques;
}


MapReduce program for finding low value hashes

From PEP 8 :

  Imports are always put at the top of the file, just after any module
  comments and docstrings, and before module globals and constants.

Edit : Ok, from your comment, it seems like this is a requirement for mincemeat.

The default value for the third argument of range is 1 so you don't need to provide it.

You shouldn't use magic numbers. Especially when they have so many digits and are so tedious to read/compare.

It makes the following snippet a bit awkward:

for i in range(v*1000000, ((v+1)*1000000), 1):

Just trying to understand how many iterations there will be is a bit of a pain.
I'd much rather read :

nb_nounces = 1000000
for i in range(v*nb_nounces, (v+1)*nb_nounces):

There is no point in having :

    else:
       pass

nonces = [i for i in range(0, 40)]
datasource = dict(enumerate(nonces))
s.datasource = datasource

can be much more concisely written :

s.datasource = {i:i for i in xrange(40)}

using dict comprehension and xrange.

for i in range(0, len(container)): is usually an antipattern in Python. This usually corresponds to something that can be written with straight-forward iteration. In your case, I guess (but I haven't tested), you could just do : for key,value in results.iteritems().


Threadsafe get method on queue that draws values from other queues?

Code Style

Java Code Style puts the open-brace at the end of the line, not the start of the next line. For example, you have:

            if(i == nextQueue)
            {

but that should be:

            if(i == nextQueue) {

Variable conventions

i as a variable name is a great idea, if the variable is the control integer in a for loop. In your case, I presume it is short for 'item', or something, but, a Queue, being called i is unconventional.

As it happens, the letter q is perfect as a substitute....

Now, your nextQueue variable is actually the lastQueue odd.

Function extraction

With synchronization, return-balues from methods are often a great help for readibility. Consider this code you have:

// Work on a copy of memberQueues
List<Queue<T>> memberQueuesCopy;

synchronized(memberQueues)
{ memberQueuesCopy = new ArrayList<Queue<T>>(memberQueues); }

Which should really be written as:

// Work on a copy of memberQueues
List<Queue<T>> memberQueuesCopy;

synchronized(memberQueues) {
    memberQueuesCopy = new ArrayList<Queue<T>>(memberQueues);
}

would be even better if written as:

private final List<Queue<T>> copyQueues() {
    synchronized(memberQueues) {
        return new ArrayList<Queue<T>>(memberQueues);
    }
}

and then:

// Work on a copy of memberQueues
List<Queue<T>> memberQueuesCopy = copyQueues();

Bugs

There are three bugs I should point out:

NoSuchElementException if memberQeues is empty:

  Queue nextQueue = memberQueuesCopy.get(memberQueuesCopy.size() - 1);

(and bug 3) NullPointerException if any of the queues are empty (in some combinations) (one bug on iComparable, the other on nextValueComparable):

T nextValue = nextQueue.peek();
Comparable nextValueComparable = keyGetter.getMember(nextValue);

  ....

    T iValue = i.peek();
    Comparable iComparable = keyGetter.getMember(iValue);

  ....

    if(nextValue == iValue && nextValueComparable.equals(iComparable))

  ....

    if(iComparable.compareTo(nextValueComparable) < 0)


ES6 Javascript (React) code that maps and filters JSON data

You can use a for..of loop to avoid using .map() twice and Set

const files = Object.values(gist.files);
const unique_tags = [];

for (let file of files) {
  let tag = file.language ? file.language : 'Plain Text';
  if (unique_tags.indexOf(<Tag value={tag}/>) === -1) {
    unique_tags = [...unique_tags, <Tag value={tag}/>]
  }
}


Increase performance of string matching function

You're just looking to find any textStatement's that exist in Library.Statements? A HashSet will make easy work of this:

IEnumerable<string> splitInputStatements = SplitStatements(data);
HashSet<string> inputStatements = new HashSet<string>(splitInputStatements);

// If you're using Library.Statements in this way many times, it might just
// be a good idea to keep all of them cached in a HashSet to begin with so
// you don't have to build this.
IEnumerable<string> libraryStatementTexts = Library.Statements.Select(s => s.FullText);
HashSet<string> libraryStatements = new HashSet<string>(libraryStatementTexts);

// That's it, this is your result:
var matches = inputStatements.Intersect(libraryStatements);

This site has some handy notes for when to use collections of certain types.

Alternatively, you can skip making inputStatements a HashSet and continue using the IEnumerable directly like so::

inputStatements.Where(is => libraryStatements.Contains(is));


I was not able to find this question on SO, so let me try to give an answer here:

A general idea:

Depending on the nature of the data your are synchronizing, it might be better that the server sends out a synchronization signal and the clients synchronize to the data received from the server.

Specifics to the code of the question

I see two problems in the slot function acceptConnection() according to the documentation:

the function nextPendingConnection() may return 0. This may lead to a problem with the following connect statement. You should check for 0 response.
nextPendingConnection() will not return the exact same connection for which the signal was sent, it just returns "the next one". This may lead to a problem when multiple connections are made at the same time, or your code cannot handle one of them in time. It might be better that you handle all pending connections in a loop.

With these additions your code would perhaps look like this:

void Server::acceptConnection()
{
   while(client = server.nextPendingConnection()) //client is a QTcpSocket*
   {
      connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
   }
}

This brings us directly to the next problem: the client variable - I assume that this is a member variable of the Server class - will be overwritten for each new connection.

The Better approach is to follow the advice in the documentation and override incomingConnection(). We could subclass QTcpSocket and implement the handleConnection() functions from there. The documentation describes what the base class function normally does.


for starters, have it output valid html....so don't put a div and an h1 into your head...
also align is not a valid attribute. you should run your markup output through a validator


Speed up performance of nested for loops to get all related format of all Authors' Books

If you would use a debugger or something like a django-debug-toolbar, you'd see that you are actually issuing a lot of queries - to get all authors, for each author to get all the books, for each book to get all the formats.

There is a specific tool in Django to solve this kind of problem of getting the related sets - prefetch_related() and select_related(). Replace:

authors = Author.objects.all()  # 1 query
for author in authors:
    books = author.book_set.all() # 1 query per each author

with:

# only 1 query
authors = Author.objects.prefetch_related('books').all()
for author in authors:
    books = author.books.all()

See also this nice article with related examples:

Solving Performance Problems in the Django ORM

For the multiple "prefetch" levels, please see:

prefetch_related for multiple Levels

You can also select only the columns you actually need - use values_list.


At the bottom is a new version, addressing most of the issues you're wondering about. But let us give a review to explain the changes:

Intermixing functions and code – In your on_touch_up you are intermixing code with the new functions. That is not good. You can introduce functions within a functions, but if you do so, then do it at the top of the function, so that it is very clear where you actual function body starts, and where the other functions are defined.
Alternative to function within function – There does however exist alternatives to those recursive function definition, and I'll present two lambda's and partial functions:

functools.partial – The other inside function you had was the call to line_expand(), and this would be nicer, in my opinion, if you put in the class scope. This does however need for more information to be passed, i.e. the touch data structure, so the definition for it to be a valid function is:

def _line_expand(self, touch, dt):

However, this is also used as a callback, so it should only take that one parameter, i.e. dt. To accomodate both these demands, you can use functools.partial to prefill a parameter like this:

growclock = Clock.schedule_interval(partial(self._line_expand, touch), 0.01)

lambda – In some cases, like a callback which will actually only do that one thing, you could use lambda's which is kind of a fake function for one expression.

Clock.schedule_once(lambda dt: Clock.unschedule(growclock), 1)

These two tricks allows you to remove both of the inner functions in your original on_touch_up(), and the full alternate version is presented below as alt_on_touch_up()
Combine the animation sequence into a method – Instead of having to always combine the two operations you could make a method which does the animation for you, with the given interval time, and the end time. This is presented below in the animate() function. Now the kind of 'messy' Clock.schedule stuff is hidden away in that method.
Alternate ways to change a tuple – A tuple in Python is immutable, or unchangeable. You have to replace it, but how you replace it is another story. In the new _line_expand() I'll show two alternate methods, one which lifts out the values from the tuple to dedicated values, and one which makes an alias for the touch.ud['ellipse'] part, which makes the changes a little easier on the eye.
Comments are good! – Try working on giving some comments or docstrings on your code, as it will help you and others understand what happens.

So without further ado, here is the revamped version of class Test:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Rectangle, Line, Ellipse
from kivy.clock import Clock

from functools import partial

class Test(Widget):

    def _line_expand(self, touch, dt):
        """Expands the size of the ellipse, and width of line"""

        # Extract values in front of setting new
        width, height = touch.ud['ellipse'].size
        touch.ud['ellipse'].size = (width +1, height +1)

        # Create an alias for later use
        ellipse = touch.ud['ellipse']
        ellipse.pos = (ellipse.pos[0] - 0.5,
                       ellipse.pos[1] - 0.5)

        # ellipse.size = (ellipse.size[0] + 1, ellipse.size[1] +1)

        touch.ud['line'].width += 0.5

    def on_touch_down(self, touch):
        """Clear canvas, and create an ellipse and line starting at touch down point."""
        with self.canvas:
            Color(0, 0, 0)
            Rectangle(pos=(0, 0), size=(self.width, self.height))
            Color(1, 1, 1)
            touch.ud['ellipse'] = Ellipse(pos=(touch.x - 10, touch.y - 10), size=[20, 20])
            touch.ud['line'] = Line(points=[touch.x, touch.y], width=10)

    def on_touch_move(self, touch):
        """Add points to the line when dragging mouse"""
        touch.ud['line'].points += [touch.x, touch.y]

    def on_touch_up(self, touch):
        """When releasing touch, call animation to expand line and ellipse."""

        self.animate(partial(self._line_expand, touch), 0.01, 1)

    def alt_on_touch_up(self, touch):
        """First rewrite of on_touch_up."""

        growclock = Clock.schedule_interval(partial(self._line_expand, touch), 0.01)
        Clock.schedule_once(lambda dt: Clock.unschedule(growclock), 1)

    def animate(self, animate_func, interval_time, end_time):
        """Schedule animate_func every interval_time for end_time seconds."""

        animation = Clock.schedule_interval(animate_func, interval_time)
        Clock.schedule_once(lambda dt: Clock.unschedule(animation), end_time)

The TestApp and main call code is untouched, so keep them as they were. Hope this gives some hint on how to avoid or make it cleaner when using inner functions.


PHP : Custom trim function instead existing ltrim() or rtrim()

I wonder if you did a quick Google search before rolling your own solution. Despite being implemented via different languages, they all give the same general regex solution.

Regex to remove all
 tags from beginning and end of a string in javascript
Remove
 tags from the beginning and end of a string
Remove BR tag from the beginning and end of a string

As you have discovered, trim() functions treat the 2nd parameter as a "character mask", this means that it will greedily remove any consecutive characters from the beginning/end of the string from that "list of characters" (
 is the same as rb><).

If you need a utility function and require the same passed-in parameters:

function trimString($input, $find, $replacement = ' ', $type = 'ltrim') {
    $patterns = [
        'ltrim' => '^(?:' . preg_quote($find, '/') . ')+',
        'rtrim' => '(?:' . preg_quote($find, '/') . ')+$'
    ];
    $pattern = $patterns[$type] ?? implode('|', $patterns);
    return preg_replace('/' . $pattern . '/i', $replacement, $input);
}

If you want to be able to pass replacement strings which contain regex to the custom function like
, then you can remove the preg_quote() calls entirely or set a flag/argument in the custim function which determines whether the quoting call should be implemented.  This means that the responsiblity of quoting any special characters is transferred from the custom function to whatever layer is calling the custom function.

If you want to use trim() before or after the execution of this custom function, that makes perfect sense too -- removing whitespace characters is exactly what it is mean to do.

If your requirements keep extending and you have the intention of removing HTML tags from a valid HTML document, then regex is probably not the right path to go down.  Regex is DOM-unaware and will be vulnerable to breakage in fringe case scenarios -- a legitimate DOM parser would be my strong recommendation in that case.


Countdown module that hides an element when a specified date is reached

My notes:

There is no need to create a function that calls check, you can just pass check
There is no need for function hide, you only call it once, and it only has 1 line of code, I would inline that function
Very minor, but there is no need to declare endingDate inside check, I would declare it once outside of the function
You are using a revealing pattern, I would use a simple self executing pattern. On the whole if you expose 1 function that you will run only once, use a self executing function.
Once the date is passed you will be hiding that element non-stop every second, that sounds like overkill
I abhor anonymous functions, choose a meaningful name, and you can spare a line of comment ;)

My approach with these notes would be

    (function HideElementWhenTimeComes (){
      var $el = $('#element'),
          endingDate = new Date("July 25, 2016 11:06:00"),
          intervalID;

      function check() {
        var currentDate = new Date();
            if (currentDate.getTime() >= endingDate.getTime()) {
              $el.hide();
              clearInterval(intervalID);
            }
      }

      intervalID = setInterval(check , 1000 );

    })();


double matrix[dimension][dimension];

double array[dimension * dimension];

double matrix[dimension][dimension];

    double subMatrix[dimension - 1][dimension -1];

    double array2[(dimension - 1) * (dimension - 1)];

None of those are legal C++, because dimension isn't a constant-expression.

We have a signed/unsigned comparison here:

    for(int k = 0; k < str.length(); k++) {

We can easily eliminate the compiler warning by using a more appropriate type:

    for (std::size_t k = 0;  k < str.length();  ++k) {

But a better fix, given we only use k to index str, is to use a range-based loop:

    for (auto const c: str) {

When using operator>> on a stream, we must always check that it succeeded, before we depend on the result:

std::size_t dimension;
std::cin >> dimension;
if (!std::cin) {
    std::cerr << "Input failed\n";
    return EXIT_FAILURE;   // needs <cstdlib>
}
if (dimension == 0) {
    std::cout << "1\n";    // empty matrix determinant
}


It's not only possible but also a great example of how functional programming can make code much simpler and readable:

  public Map<Integer,List<String>> mapLengthToMatchingStrings(List<String> strings){
        return strings.stream().distinct().collect(Collectors.groupingBy(String::length));
    }

(The distinct part is optional, regarding whether you want duplicates in the lists)


Your function modifies the get/put position of the buffer.  I think this is a bug, and it makes benchmarks misleading.  Try calling extract twice in a row -- you'll get different results.  You set the put position after calling sgetn but you should have set the get position.  This should be intuitive.  After a read, you "undo" your reading.  Fix the bug with something like this:

string extract() {
    streambuf& buffer = *str_buffer.rdbuf();
    vector<char> sequence(str_buffer.tellp());
    auto g = str_buffer.tellg();
    buffer.sgetn(sequence.data(), str_buffer.tellp());
    str_buffer.seekg(g); // seekg to the original position
    // as far as I can work out, buffer.pubseekpos is not necessary here?
    return string(ALL(sequence));
}

Your function still does not compute the same thing as str.  str returns the entire buffer.  Your function returns from the input position to the output position.  These are different -- try calling str_buffer.seekg(3) before comparing the output of the two functions if you want to see for yourself.  You could seekg(0) before you call sgetn:

    auto g = str_buffer.tellg();
    str_buffer.seekg(0);
    buffer.sgetn(sequence.data(), str_buffer.tellp());
    str_buffer.seekg(g);

I think this implementation is now correct enough to benchmark.  I tried the updated version vs str on http://quick-bench.com.  I used clang-9 and libstdc++(GNU).  I tried a stringstream with 1000 characters and also 10,000 characters.

This implementation is about 130 times slower than str with no optimizations and with -O1/Clang, and it is and 3.5 times slower with -O2/Clang.

How can you speed up your function?  Well the easiest way is to use str!  But let's make some small improvements.  You copy the buffer into a vector<char> and then copy the vector into a string.  What if we only have one string?

string extractOneString() {
    std::string sequence;
    sequence.resize(str_buffer.tellp());
    auto g = str_buffer.tellg();
    str_buffer.seekg(0);
    str_buffer.rdbuf()->sgetn(sequence.data(), str_buffer.tellp());
    str_buffer.seekg(g);
    return sequence;
}

Now the unoptimized version is 3.5 times slower -- just like the optimized version before.  As it turns out, the Clang's -O2 was already making this change for you.

Still, 3 times slower is not good.

This is maybe not obvious, but if you look at the assembly (perhaps using http://godbolt.org), you'll notice that resize calls memset to set the new buffer to zero.  That's not necessary since you're about to overwrite the buffer anyway.  There's no easy way to get around this (unless you count using str).  I think this contributes to the 3x slowdown (not sure and I don't think it's worth confirming).

A few notes about your code:

Don't use using namespace std.  People have written about this in lots of places so I won't here.
The ALL macro is ugly and unnecessary.  If you cannot stand to write iterators, look into the new ranges library.

It's worth learning sooner rather than later not to spend time optimizing the wrong thing.  You wrote this code and then (I) measured it.  Next time, measure your code first and then decide whether it's worth changing.  This has the added advantage that you're already set up to measure the code post change.

I think it's also worth focusing on the basic semantics of your program before worrying too much about measured performance.  An experienced C++ dev would see right away that your extract function has a unnecessary copies and would most likely fix that before profiling.  Learning to avoid copies and other idioms will help you avoid having to measure every little thing.


The important part
They want something below O(n). Using map is a good idea but actually you should find the cycles and return the right position on the cycle instead of computing line by line.
Spoiler (possible solution)
Change:
            if tuple(cells) in step_map:
                return  step_map[tuple(cells)]

to:
            if tuple(cells) in step_map:
                cycle = list()
                head = tuple(cells)
                cycle.append(head)
                previous = head
                while True:
                    next_node = tuple(step_map[previous])
                    if next_node == head:
                        return list(cycle[(N - k) % len(cycle)])
                    cycle.append(next_node)
                    previous = next_node

Old edit - Some small improvements
There are some O(m) operations multiple times...
For example:
 tmp = [0] + tmp + [0]

Python operation for that is O(m). Therefore, your solution is O(nm).
step_map[tuple(cells)] = tmp

this is also O(m).


Forget everything but the instatiator, constructor and property, you don't call new static; you call new self(), then it jumps into the constructor, the way I do singleton is:

class Foo
{
    private static $init;

    private function __construct() {  }

    public static function init()
    {
        return static::$init = (
          null === static::$init ? new self() : static::$init
        );
    }
}

$foo = Foo::init();

This makes it so when new self() is called, it jumps to the constructor and assigns self::$init to really be an instance of the class


find_index for first occurrence of pattern in text

Python is a duck typed language, so it's not considered normal to assert instance types. Instead simply assume that the user passed a string, and let the program fail if the input can't be processed sensibly.

You calculate the length of text and pattern potentially many times during a run. Unless you plan on modifying them during the run you can save the results to a variable and reuse that.

You can get rid of if text_index + i < len(text) by changing your while statement to take into account the length of the pattern.

Apropos runtime: I'm purposefully not commenting on more advanced algorithms for text searching, mainly because I don't memorize things I have literally never had to use.


Security

XSS

You might want to consider HTML encoding the database values such as product name, description, price, etc.

It may or may not be needed currently, but generally, you might want more than one type of user in the future. You may have a site admin, a sales manager, data entry people who add new products, and so on.

If you do not encode these values, less privileged users could escalate their privileges via XSS.

CSRF

You also do not have any CSRF protection. This means that an attacker can add products if a logged in user with the right to add products visits an attacker-controlled website.

As you are also open to XSS, an attacker can gain XSS via adding products.

SQL Injection

Escaping is really not the preferred way to defend against SQL Injection. It's certainly a lot better than nothing, and if used correctly will be secure, but in practice, prepared statements result in better and more secure code and are really the only correct way to handle SQL Injections.

Structure

Your code is generally quite readable, but it would be even more readable if you would separate code into classes and functions. This would also make adding new functionality a lot easier

For later exercises, you might want to look into MVC. But even for a small example script, you might want to at least separate the database connection , so in case the credentials change, you do not have to change them all over the place, but just in one configuration file.

Functions would help make your code even more readable. You might consider functions such as selectProductById($connection, $id), insertProduct($connection, $name, $description, $price, $quantity), showProducts($products), showProductForm etc. You might also consider extracting those methods in a separate Product class (or a Product class for the database interactions, and a ProductView class for the displaying functions).

Misc

why are all your inputs of type text? Quantity and Price should be numbers, allowing a user to enter anything makes for bad usability. In practice, description should also likely be a textarea.
having an empty if is bad for readability. Just negate the question and only have the else case.
regarding usability: Think about adding at least some kind of feedback after a successful add.


User-defined function to split a 1D array into 2D array

"receive a 1D array and split it into a 2D array" - it's not true, it receives a 2d array of which 2nd dimension is 1. It won't work receiving an 1D or transposed 2D array. (it probably serves your purpose, just be clear with the description to avoid confusion in the future.

If ArraySize Mod MaxRow > 0 Then MaxCols = MaxCols + 1

Although this is correct syntax it's generally a bad practice to write IF in one line.

You can simplify your calculations:

y = x Mod MaxRow
If y = 0 Then y = MaxRow

=> y = ( (x - 1) Mod MaxRow) + 1

z = x \ MaxRow
If x Mod MaxRow = 0 Then z = z - 1

=> z = (x-1) \ MaxRow

(These will not work correctly for 0 index, however in your case arrays copied from Range always have index starting from 1.


Von Neumann extractor to remove bias from pseudorandom array

Your code looks quite clean and I have just a few observations, mainly regarding readability.

First it lacks comments, that is especially important in your case because the matter is not that common. In your question you separately gave all needed information to understand what and how it is processed, but these explanations should be included in the code, even if in a less detailed fashion.

Naming conventions respect standards but I'd suggest a different choice about what you named "bit". Technically speaking you're not managing bits but numbers, even if they only take values of 0 or 1: so at a first view it's a bit (!) confusing and you'd better using something like "digit" or "val", or somewhat else not too much related to a (here falsy) javascript type.

Yet about this fact that data are not really bits I was surprised you're using a bitwise operator in return (a ^ b ? a : null);. Maybe I'm missing some subtility but for what I can understand this XOR can be simply expressed as return (a != b ? a : null);.
Though I'm not pretty sure, it might be less detrimental to performance.

The only real fault against best practices is in your for (var i = 0; i + 1 < randomArray.length; i += 2): this way randomArray.length is evaluated for each step. The right way is for (var i = 0, l = randomArray.length; i + 1 < l; i += 2), so evaluating randomArray.length only once.

Last observation, you may save some variables and reduce the code length each time a variable is used only once, like here:

var a = arr[i];
var b = arr[i + 1];
return (a ^ b ? a : null);

that can be written:

var a = arr[i];
return (a ^ arr[i + 1] ? a : null);

(note that I only dropped the b var: also dropping a would cause arr[i] to be evaluated twice)


No need to peek ahead for the loop condition. Especially not AFTER incrementing it already. Coincidence that this function was not part of the test suite?

void printList(node* head)
{
  if (head == NULL) return;

  while(head)
  {
    printf("%d, ", head->val);
    head = head->next;
  };

  printf("\n");
}

Too much redundant code:

void clean(node* head)
{
  while (head)
  {
    node* curr = head;
    head = head->next;
    free(curr);
  }
}

In void insert(node* head, int val), no need to branch inside the loop. Do it step by step. First find the tail, then append.

void removeIf(node** head, int val) would become already much simpler if it did just add a temporary head to the list, as the special handling for removing a chain at the head wouldn't be necessary in that case. Finally destroy the temporary head again, and overwrite head unconditionally.

In int size(node* root), why call it root all of a sudden and no longer head?

Also, unnecessary lookahead and branching again:

int size(node* head)
{
  int sz = 0;
  while (head)
  {
    ++sz;
    head = head->next;
  }
  return sz;
}

In terms of code style only one remark, and that is on the location of the * when working with pointer types.

I know, placing it next to the type feels more natural, but it's semantically inaccurate, and it will trip you the first time you attempt to declare multiple variables at once. Placing it where there compiler would sort it helps preventing these mistakes.


Using dapper.net and repository pattern in Azure websites

Always program to abstraction, meaning it is always preferred to write your code against interface or abstract class rather than specific implementation.  So, IStoreRepository  would be a good idea.
If you are not going to ever change your implementation, do not use abstract or interfaces. Make it simple and readable.
If you are considering using mapper and all, use DI container too so that you don't have to do this:

var db = new StoreRepository();
// creating instance like this is not a good idea, rather inject the dependencies

Same thing goes for your SQL helper class too.  Inject as a dependency in your repository.
If you use EF/nhibernate, then your code will not be repeating as all code will have the same kind of code.  You write a generic base class for them and pass the type of data you want to retrieve it from the database.  But in case of hand-written SQL, that could not be done that easily as you need to write different SQL for different type. Again, that is your choice.
It is good to use async await for managing an SQL connection so you don't just block the currently-working thread. Overhead is small in overall picture.


I had a quick look at the github repo. I'd highly recommend splitting your project into multiple files as this will make things easier to manage. You might also want to consider writing some unit tests.

Design comments:
The use of global in createParticles is a bit concerning, it looks like you want to have some sort of ParticleManager class (or similar) that will explicitly manage the particles. This will be much easier to maintain than keeping a list in global scope. Things like gBest and bestIndex really work better when they are in an appropriate management class.

From looking at the Github code it looks like you have a bunch of different functions that manipulate a global list of particles. By creating a class that stores the list of particles plist and has methods that operate on it you will save yourself a lot of headaches when dealing with the data. This encapsulation will help you keep maintaining the code simple. So to answer your question, definitely make createParticles a method of a class. For example if you have a problem with the data in your current design you first have to search through a whole bunch of different functions to see which ones could possibly have side effects that modified your data before you can be sure you have fixed your problem. By having the dedicated particle management class you immediately know exactly which functions could be responsible and you immediately know where they are.

Python specific comments:
You really should get in the habit of using docstrings. For example:

class Particle:
    """This class models a particle in the system, it does a,b,c...."""

def createParticles():
    """This function creates the particle objects used in the system"""

You have some really long string lines, Python will let you concatenate these strings. So instead of:

if k > 0:
      return '  i: '+str(self.index)+'\n  x: '+str(self.x)+'\n  y: '+str(self.y)+'\nv_x: '+str(self.velocity_x)+'\nv_y: '+str(self.velocity_y)+'\n  b: '+str(self.pBest[0])+'\n  l: '+str(self.lBest)+'\n'

you could instead do:

if k > 0:
       return('  i: '+str(self.index)+'\n'
              '  x: '+str(self.x)+'\n'
              '  y: '+str(self.y)+'\n'
              'v_x: '+str(self.velocity_x)+'\n'
              'v_y: '+str(self.velocity_y)+'\n'
              '  b: '+str(self.pBest[0])+'\n'
              '  l: '+str(self.lBest)+'\n')

Which is easier to read already. However you can go further here by using string formatting. Additionally there's duplicated code in building the strings for returning, just make the first string and if K > 0 just append what you need in that case.

So the complete __str__ implementation I might go with looks something like this:

def __str__(self):
    """Creates string representation of particle"""
    ret = """  i: {self.index!s}
  x: {self.x!s}
  y: {self.y!s}
v_x: {self.velocity_x!s}
v_y: {self.velocity_y!s}
  b: {self.pBest[0]!s}""".format(**locals())

    if k > 0:
        return ret+'  l: '+str(self.lBest)+'\n'
    else:
        return ret


Checking for violation of primary key in a DataTable

According to the .NET Naming Guidelines, parameter names should be written in lower Camel case and be descriptive.

Private Sub PrimaryKeyCheck(Of ColumnType)(table As DataTable, columnName As String)

  "Is it possible to remove the double for each?"

Yes! There's a handy method in the List<T> class named ForEach.  The provided action will be invoked for each element in the list.

I advice you to not use the name i in this context as this name is widely used as an abbreviation for index (usually a number). A better name is r or row. And g could be renamed to group.

You should also move the magic string "Error, Violating PK" to a constant.

Const errorMessage As String = "Error, Violating PK"

table _
    .AsEnumerable() _
    .Where(Function(r) r.RowState <> DataRowState.Deleted) _
    .GroupBy(Function(r) r.Field(Of ColumnType)(columnName)) _
    .Where(Function(g) g.Count() > 1) _
    .ToList() _
    .ForEach(Sub(g) g.ToList().ForEach(Sub(r) r.SetColumnError(columnName, errorMessage)))


Fast way to calculate Standing Wave modes in NumPy

A few things to consider:

Loops in python are very inefficient. To optimize performance, when
it is possible to vectorize code, you should do so.
Avoid calculating the same thing multiple times. For example, the term $$ cos(\omega_{n,m}t + \phi_{n,m}) $$ does not depend on x and y, so there is no need to calculate it during every iteration of the loop.
The number of calculations you are performing is huge. Nx * Ny * Nt * n_max * m_max is roughly 191 billion iterations, and on each of those iterations, multiple calculations are performed. I am not sure about the reason for using so many points on your x,y,t grid, but for adequate visualization, much fewer are required. If the goal is numerical precision, I would recommend checking out SciPy's interp2D function. What you could do is calculate a smaller number of grid points, and if you need to sample a value that is not on the grid, you can use the 2d interpolation function.

I have altered your code to vectorize it as much as possible. I did not completely vectorize out the loops, because my laptop does not have RAM capacity in the triple digits of gigabytes.

Reducing the number of grid points in the X and Y directions results in a huge speedup, and the resulting plot is shown after the code.

import numpy as np

#Nx = 384
#Ny = 384
#Nt = 360

Nx = 38
Ny = 37
Nt = 360

#########################################################
# function to calculate standing wave (original code had error in amp line)
#########################################################
def s_wave(lx, ly, xx, yy, tt, pp, max_modes):
    tot_amp = 0    # Initialize total amplitude to 0
    Mm      = 1000 # a constant relevant to my problem
    p_idx   = 0    # The index for accessing the phase noise

    for m in range (0,max_modes):
        for n in range (0,max_modes):
            #############################
            # Calculating Omega
            #############################
            omega1 = n*np.pi/(lx*Mm)
            omega2 = m*np.pi/(ly*Mm)

            omega  = np.sqrt(omega1**2 + omega2**2)

            #############################
            # Calculating Amplitude
            #############################
            amp1   = np.sin(n*np.pi*xx/lx)
            amp2   = np.sin(m*np.pi*yy/ly)
            amp3   = np.cos(omega*tt - pp[p_idx])

            amp    = amp1 * amp2 * amp3

            #############################
            # Update total amplitude
            #############################
            tot_amp = tot_amp + amp
            p_idx   = p_idx + 1

    return tot_amp

def s_wave_opt(lx, ly, xx, yy, tt, pp, max_n, max_m):
    tot_amp = 0    # Initialize total amplitude to 0
    Mm      = 1000 # a constant relevant to my problem

    #############################
    # Initialize total amplitudes at all points to 0
    #############################
    A_total = np.zeros([len(xx), len(yy), len(tt)])

    #############################
    # Calculate omega
    #############################
    n,m     = np.mgrid[1:max_n+1:1, 1:max_m+1:1]
    omega_n = n * np.pi / ( lx * Mm )
    omega_m = m * np.pi / ( ly * Mm )
    omega   = np.sqrt(omega_n**2 + omega_m**2)

    #############################
    # Vactorize calculation of term3
    #############################
    term3 = np.multiply.outer(t,omega)
    term3 = np.add(pp,term3)
    term3 = np.cos(term3)

    #############################
    # Calculating A[x,y,:]
    #############################
    nr     = np.arange(1,max_n+1)
    term1  = np.multiply.outer(xx, nr*np.pi/(lx*Mm))
    term1  = np.sin(term1)

    mr     = np.arange(1,max_m+1)
    term2  = np.multiply.outer(yy, mr*np.pi/(ly*Mm))
    term2  = np.sin(term2)

    for y_idx in range(len(yy)):
        xy  = np.multiply.outer(term1, term2[y_idx])
        xyt = np.multiply(xy[:,None,:,:],term3[None,:,:,:])
        tot = np.sum(xyt, axis=(2,3))
        A_total[:,y_idx,:] = tot

    return A_total

# building grid points in x and y direction
Lx = 6.144 # Length of the Box along each axis
Ly = 6.144
T = 10 # delta_T
x = np.linspace(-Lx/2, Lx/2, Nx)
y = np.linspace(-Ly/2, Ly/2, Ny)
t = np.linspace(0, (Nt-1)*T, Nt)

modes_n = 60
modes_m = 61
phase2  = np.random.uniform(0,2*np.pi, [modes_n,modes_m])
ustand  = s_wave_opt(Lx, Ly, x, y, t, phase2, modes_n, modes_m)

The resulting plot was too large to attach as a gif, but attached is a screenshot of one frame:

The code to produce the gif plot:

##################################################
# Plotting the results
##################################################
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt

import os
if not os.path.isdir("./images"):
    os.system("mkdir images")

for i in range(Nt):
    fig = plt.figure()
    ax  = plt.axes(projection='3d')
    X,Y = np.meshgrid(y,x)
    ax.plot_surface(X, Y, ustand[:,:,i])

    ax.set_xlim3d(x.min(), x.max())
    ax.set_ylim3d(y.min(), y.max())
    ax.set_zlim3d(ustand.min(), ustand.max())
    ax.set_title("Standing Wave at Time {}".format(t[i]))
    plt.savefig("./images/frame{}.png".format(i))
    plt.close()

os.system("ffmpeg -i ./images/frame%d.png -vf palettegen -y paletter.png && ffmpeg -framerate 20 -loop 0 -i ./images/frame%d.png -i paletter.png -lavfi paletteuse -y plot.gif")

The code runs in about 4 minutes. Roughly 1 minute are the calculations, and the rest is time spent creating the graph.


Finding peak point in an array by using divide and conquer approach

The code will crash with an out of bounds exception if given a zero-length array.

You are playing a little loose with the array endpoints.  If you get to mid=0, and it turns out that arr[mid] > arr[mid+1], then you will check arr[mid] > arr[mid-1], which will be reading arr[-1] which is the element at the other end of the array.  Fortunately, if arr[0] > arr[1], then the array must be monotonically decreasing, and the last value will be the smallest, but relying on that is tricky behaviour.

Your code is recursively executing return search(...), Python does not do tail-recursion-optimization.  This could could easily be re-written to use a loop, which would be faster and doesn’t require any additional stack space.  For example, something like:

while low < high:
    mid = low + (high - low) // 2

    if arr[mid-1] < arr[mid] < arr[mid+1]:
        return arr[mid]

    if arr[mid] > arr[mid+1]:
        high = mid
    else:
        low = mid + 1

return arr[low]

You search(mid+1, high). Why don’t you search(low, mid-1)?  Seems asymmetrical.

Minor nit: You algorithm will fail if the array can have equal elements, such as 5, 50, 10, 10, 10, 10, 5.  It will find “10” at the mid-point and on both sides, at which point it will drift to the larger indexes, and eventually declare “10” as the maximum.


Some quick comments:

At least as written, your code doesn’t work for me. I created the following file:

1

and passing it to your script got the following error:

Enter name of a file of numbers (include the whole path): numbers2.txt
Traceback (most recent call last):
  File "parsenumbers.py", line 37, in <module>
    main()
  File "parsenumbers.py", line 20, in main
    oddNumber.write(number + '\n')
TypeError: unsupported operand type(s) for +: 'float' and 'str'

I’m using Python 3.4.3 on OS X.
I fixed that (fix hidden for you to work it out yourself):

   Cast number to str(number) before writing.

Then I created a longer file:

1
2
3
4
5

And ran the script again.  Still broken:

# odd.txt
1.0

# even.txt
(Empty)

It’s also strange that 1 has become 1.0 in the output file. I’d expect it to be carried through unchanged – what’s happened?

Once you’ve got the code working, some style comments:
* Run your code through a PEP 8 linter. This will catch style issues like variable names, whitespace, etc.

Everything is wrapped up in one massive main() function. That makes this code difficult to test and reuse – it would be better if you had separate functions, each of which took a list of numbers, and would do different things.

e.g. one function gets the sum of the negatives, another the positives, another to write the evens to a file.
Rather than calling main() at the top-level, put it in an if __name__ == '__main__' block. This means it only runs if the script is called directly – and other people can import from your script without side effects.
Use with open(filename) as f:, not f = open(filename) … f.close(). This is more idiomatic, and safer – you can’t leave files open accidentally. (You never actually close even.txt or odd.txt after you’ve opened them!)
None of your comments tell me anything I couldn’t get from reading the code.  They should explain why the code is written this way – how does it relate to the problem it’s trying to solve – not what it’s doing.  And as a sidebar, it’s really annoying if comments don’t line up with the code they’re describing.
Use better variable names. You have count and total, but it’s not obvious what these correspond to.  How about negative_count and positive_total – otherwise it sounds like you’re getting a count/total for the entire set.
Your code assumes that zero is negative, and will count it as such. That’s somewhat counter-intuitive: instinct tells me that zero is neither positive nor negative, but there are times when it’s more complicated.

This would be a really good spot for a comment: if it’s not completely ambiguous, state what choice you’re making (in plain English), and why you’ve chosen to do it this way.


Sure it can!

Steps I see to make it clean:

Create a class, define state attrs:

private

attr_reader :output, :code_pointer, :data_pointer, :input_pointer

define initialize method

def initialize(args = {})
  @code = args[:code]
  @input = args[:input]
  @output = ''
  @code_pointer, @data_pointer, @input_pointer = 0, 0, 0
end

define whens as a methods and give it right names: next_cell, prev_cell, increment, decrement, write_byte, read_byte and so on
define constant for configuration:

OPERATORS = {
  '>' => :next_cell,
  '<' => :prev_cell,
  '+' => :increment,
  ...
}

define method to evaluate code:

def evaluate_code
  code.each { |operator| public_send OPERATORS[operator] }
end

Finally instantiate object of your class and call evaluate_code

BrainLuck.new(code: code, input: input).evaluate_code

I think you'll love your object oriented result.


Avoid similar variable names
There's line.product_id.id, line_id and line.id. This is very confusing. Try to avoid having variable names that are too close to each other.
Simplifying the code
You can indeed simplify the code somewhat. First, you want to find out if there are multiple lines that reference the same product. To do that, you want to have a dict that maps from product IDs to a list of lines. The easiest way to do this is to use a defaultdict(list):
from collections import defaultdict

def check_for_duplicates(self):
    product_lines = defaultdict(list)

    # Build a map of which lines reference which products
    for line in picking.move_lines:
        product_lines[line.product_id.id].append(line.id)

Then you can go over that map to check if any product occurs on more than one line:
    # Find duplicates
    for product, lines in product_lines.items():
        if len(lines) > 1:
            # Report lines with duplicate products here
            ...

I'm not sure how to improve on the part that checks for duplicate lots, I don't know what type self.env[...] is and what the browse() function does, but perhaps the above code will help you simplify that as well.


Not yet implemented

It's unsafe to just have a comment like this:

// will be implemented later

At least add a TODO (so that an IDE can collect all of these):

// TODO will be implemented later

Or better yet, throw an exception:

throw new Exception('Not implemented');

If your code gets bigger and bigger, it's easy to overlook one or two of your original comments, and then your data will not be filtered correctly.

if-else return true-false

Again, whenever you have an if/else statement that only returns true/false, just return the statement inside the if. So for example

if( $pass != $cPass )
    return false;
else
    return true;

would become

return $pass === $cPass;

Notice also the === instead of ==.

The same goes for this:

if( $this->errorFlag == 0 ){
    return true;
}
else{
    return false;
}

write it like this instead:

return $this->errorFlag == 0;

End-of-Function comments

I think that these only clutter up the code, I would get rid of them.

Inside a function, // endif type comments are a sign of bad code. If you have so many if-statements that you cannot see where each ends, you probably have too many. Try to get rid of some or extract some functionality into a separate method.

Switch or if-else

Since you asked: I think that the switch looks better than an if-else.

Error Flag

You never set this to 0, is this intentional?

Also, you set this flag always directly after calling setErrorMsg. Just move $this->errorFlag = 1; inside setErrorMsg, so you don't have to manage it.

If you would change it to a boolean value, it would also be easier to handle.

Unnecessary local variables

You do this very often:

$msg = $key . VALIDATION_ERR ;
$this->setErrorMsg($msg);

As you don't need msg afterwards, just call setErrorMsg directly:

$this->setErrorMsg($key . VALIDATION_ERR);

Deeply nested checkRequiredData function

You have too many if-else in your checkRequiredData function which makes it really hard to read.

One way to solve this is to pull the checks to the top of the method like this:

function checkRequiredData($dataArray, $requiredFields = NULL) {
    if (count($dataArray) <= 0) {
        $msg = "data array is empty";
        $this->setErrorMsg($msg);
        $this->errorFlag = 1;
        return false;
    }

    if ($requiredFields == NULL) {
        foreach ($dataArray as $key => $val) {
            if (empty($val)) {
                $msg = $key . REQUIRED_ERR;
                $this->setErrorMsg($msg);
                $this->errorFlag = 1;
            }
        }
        return $this->errorFlag == 0;
    }

    foreach ($dataArray as $key => $val) {
        if (array_key_exists($key, $requiredFields)) {
            $format = $requiredFields[$key];
            if (!empty($val) && $format != 'none') {
                if (!($this->dataFormatCheck($val, $format))) {
                    $msg = $key . VALIDATION_ERR;
                    $this->setErrorMsg($msg);
                    $this->errorFlag = 1;
                }
            } else {
                $msg = $key . REQUIRED_ERR;
                $this->setErrorMsg($msg);
                $this->errorFlag = 1;
            }
        }
    }
    return $this->errorFlag == 0;
}

Confusing nested ifs

This code is a bit confusing (it's not that easy to see when there will be no error):

        if (!empty($val) && $format != 'none') {
            if (!($this->dataFormatCheck($val, $format))) {
                $msg = $key . VALIDATION_ERR;
                $this->setErrorMsg($msg);
                $this->errorFlag = 1;
            }
        } else {
            $msg = $key . REQUIRED_ERR;
            $this->setErrorMsg($msg);
            $this->errorFlag = 1;
        }

This might be better:

        if (empty($val) || $format == 'none') {
            $msg = $key . REQUIRED_ERR;
            $this->setErrorMsg($msg);
            $this->errorFlag = 1;
        } else if (!$this->dataFormatCheck($val, $format)) {
            $msg = $key . VALIDATION_ERR;
            $this->setErrorMsg($msg);
            $this->errorFlag = 1;
        } // else: everything is fine

Final Code

If you follow all these suggestions, your checkRequiredData function would look like this:

function checkRequiredData($dataArray, $requiredFields = NULL) {
    if (count($dataArray) <= 0) {
        $this->setErrorMsg("data array is empty");
        return false;
    }

    if ($requiredFields == NULL) {
        foreach ($dataArray as $key => $val) {
            if (empty($val)) {
                $this->setErrorMsg($key . VALIDATION_ERR);
            }
        }
        return $this->hasError;
    }

    foreach ($dataArray as $key => $val) {
        if (array_key_exists($key, $requiredFields)) {
            $format = $requiredFields[$key];
            if (empty($val) || $format == 'none') {
                $this->setErrorMsg($key . REQUIRED_ERR);
            } else if (!$this->dataFormatCheck($val, $format)) {
                $this->setErrorMsg($key . VALIDATION_ERR);
            } // else: everything is fine
        }
    }
    return $this->hasError;
}

public function setErrorMsg( $msg ){
    $this->hasError = true; // set error flag here, use boolean instead of int for easier usage
    array_push( $this->errorMsg, $msg );
}

Which is a lot shorter and a lot easier to understand. It would be best to write some unit tests to verify that it actually does the exact same thing, as this is a major rewrite.


Don't split reading from same file into different processes

I haven't used Pool my self, but it seems like you're a splitting the file read over 30 different processes, where each reads 30 lines each? If that is correct, you should seriously consider a different split tactic, as that will throttle your IO seriously. You'll have 30 different processess trying to read from 30 different places in the file at the same time.

A better tactic would be to send each file to a different process, and then let that process handle that file completely.

Choose number of processes wisely

Another caveat would be the number of processes you use. You create 30 processes, but as long as you don't have a 30 actual processors available you wont see any major performance gain using this number of processes.

Back in the days using various Unix based operating systems, we did compilations in batches and the general rule was that we would aim for approx 4 times the number of processors we had available. In other words on a quad-processor, we would aim for 16 processes. Any more and we started seeing congestion due to interprocess issues and IO related performance bottlenecks.

Shorten the distance from file to server

Another speedup can be found if you are able to avoid network traffic. That is if you are able to run this script directly on the Kafka server, so that you can use loopback addresses and local connections instead of using the IP network.

Establish a baseline

Not so much a performance suggestion, but do you have good baselines for how long it takes to do a typical run using only a single process? This can be helpful, when you start dividing the load according to other metrics, to compare when you reach a threshold regarding what each server/client should do.


Simple JDBC Query By Example (QBE) for Spring JDBC Template

Be careful of SQL injections.

Assuming that the Order object is sanitized, then the only suggestion I'd make is to make a class for your order query and extract the logic from your method into that class.

A quick example would be that:

public class OrderQuery {

    private List<String> clauses = new ArrayList<>();
    private List<Object> values = new ArrayList<>();

    public OrderQuery(Order order) {
        setOrderId(order.getOrderID());
        // ...
    }

    private void setOrderId(String orderId) {
        if (orderId != null) {
            clauses.add("order_id = ?");
            values.add(orderId);
        }
    }

    // ...

    public List<Object> getValues() {
        return values;
    }

    @Override
    public String toString() {
        StringBuilder sql = new StringBuilder();
        sql.append("select * from orders ");
        if (!clauses.isEmpty()) {
            sql.append("where ");
        }
        String whereClause = clauses.stream().collect(Collectors.joining(" and "));
        sql.append(whereClause);
        return sql.toString()
    }
}

And to call it from your now much less clustered method:

public List<Order> orderQuery(Order order) {
    OrderQuery orderQuery = new OrderQuery(order)
    List<Order> result = jdbcTemplate.query(orderQuery.toString(), orderRowMapper, orderQuery.getValues().toArray());
    return result;
}

But it could be further optimized by using something like builder pattern.


A Scala script for beautify scientific notation strings from Excel

The code is OK, it looks quite readable, but still some things may be cleaned a bit.

Encapsulation

Members are public by default, so some of them should be hidden, since they do not seem (and do not need) to be used from outside: private val superscriptMap, private def dealWithSign.

Don't Use Mutable Collections

Scala favors immutability and using things from scala.collection.mutable should be justified. There is a simple way here to bypass the mutability by concatenation of two maps:

private val superScriptImmutable = Map(
  '0' -> '\u2070',
  '1' -> '\u00b9',
  '2' -> '\u00b2',
  '3' -> '\u00b3'
) ++ ('4' to '9').zip('\u2074' to '\u2079').toMap

Use Matchers

if.. else if.. else are too much from imperative style; matchers should be used instead:

private def dealWithSign2(s: String): ExponentTuple = {
  def dropZeros(str : String) = str.dropWhile(_ == '0')
  s.head match {
    case '+' => (1, dropZeros(s.tail))
    case '-' => (-1, dropZeros(s.tail))
    case _ => (1, s)
  }
}

Misc

There should be a general check for input data that the args contain numbers. I've tried to pass "test" string to snBeautify and it returned java.util.NoSuchElementException: key not found: s, which is not explicit from the user's point of view.

ExponentTuple type does not seem to be used elsewhere except in the optional return type declaration in dealWithSign function. The type and the declaration can be removed without any harm.

In snBeautify() functions, s1 and s2 should be renamed according to their meaning, something like original and beautified. I'd also suggest to extract clipboard operations into dedicated functions, for example:

private def getFromClipboard = {
  val clipboard = Toolkit.getDefaultToolkit.getSystemClipboard
  clipboard.getData(DataFlavor.stringFlavor).asInstanceOf[String]
}


Invalid input

toRomans(21463);

There we go. I broke your function. Sorry.

Even though the Roman Numerals Kata specifies that only numbers between 1 and 3000 needs to be returned correctly, I think it is better to handle numbers outside the working range in a specific way, such as throwing an exception.

Return, do not log

Imagine that I am writing a program and I want to use your method to count how many unique characters there is in the resulting string.

console.log(n + " --> " + result);

This doesn't let me do that, really. It would be much better to do:

return result;

And then I can log, show an alert, or count the number of unique characters after calling the function. This is how it is properly done in programming.

console.log(toRomans(56)); is more clear what the code is doing than simply toRomans(56);

Returning and breaking

In your switch statement, you don't need both return and break. When calling return, the method ends, so further statements will not be executed. Your breaks are dead code that will never be called.

I would also return ""; in the default case as well, which lets 0 be handled together with default.

function conversor(digit, letterSet) {
    switch(digit) {
        case "1":
            return letterSet[0];
        case "2":
            return letterSet[0] + letterSet[0];
        case "3":
            return letterSet[0] + letterSet[0] + letterSet[0];
        case "4":
            return letterSet[0] + letterSet[1];
        case "5":
            return letterSet[1];
        case "6":
            return letterSet[1] + letterSet[0];
        case "7":
            return letterSet[1] + letterSet[0] + letterSet[0];
        case "8":
            return letterSet[1] + letterSet[0] + letterSet[0] + letterSet[0];
        case "9":
            return letterSet[0] + letterSet[2];
        default:
            return "";
        }
}

I believe the conversor function can be further optimized, but I'll leave as-is for now.


Custom setInterval to run function synchronously

Not much to review but
A few points
Too many arguments. You have a lot of arguments that could actually be passed as  just the handler, and if you make the timeout callback one declared in the customSetInterval (rather than call customSetInterval) you can close over some of the variables and thus would not have to pass them (and keep them protected).
The variable isContinue could be removed as it is only used once and may be better as
if (func(data)) {
    handler.timerId = setTimeout(...

The first call to customSetInterval executes the function immediately, this is not what setTimeout does. On the first pass should it not set a timeout rather than execute?
Memory leaks?
That would be up to the functions being called. Your timeout will eventually release any references that are held pending the timeout so you are not causing a leak. There could be memory problems (not a leak just running low) if there are a lot of timeouts and the delay between them is long, but not a real concern.
Timing issues.
You set the timeout after you call the function and do not account for the time spent running the function. The timeouts will always be late and any error in time will accumulate with each call by the amount of time the func takes to execute (and any other page blocking events).
The way to deal with this is to record the start time and adjust the timeout to be at the correct interval.
That then introduces the issue of catching up to late calls. There are any number of ways the page can blocked making your calls is late. If late do you still make the call or skip it. If you make the call and the function you call is taking longer than the interval do you create a stack of pending calls?
A quick rewrite
As a suggestion only to demonstrate the use of closure to keep stats on the timeout and keeping track of the next scheduled time out, adjusting the time to keep things on time if possible (if more than half a interval behind the timeout is skipped) (though I did not test that)

function customSetInterval(handler) {
  var id, interval, count;
  const startTime = performance.now();

  // this returns the time till next interval. If late will drop calls.
  function getNextCallTime() {
    var nextCallIn = (startTime + interval * (count + 1)) - performance.now();
    if (nextCallIn < -interval / 2) { // to late drop the call
      count = Math.floor((performance.now() - startTime) / interval) + 1;
      nextCallIn = (startTime + interval * count) - performance.now();
    }
    return nextCallIn;
  }

  // This handles the callback
  function execute(handler) {
    if (handler.func(handler)) {
      count += 1;
      id = setTimeout(execute, getNextCallTime(), handler);
    }
  }
  handler = Object.assign({
    interval: 1000,
    stop() {
      return clearTimeout(id)
    },
  }, handler);
  count = 0;
  interval = handler.interval;
  id = setTimeout(execute, getNextCallTime(), handler);
  return handler;
}

// Tests as examples how to.

function run(handler) {
  log("Tick for " + handler.data.name);
  handler.data.counter -= 1;
  if (handler.data.counter === 0) {
    log("Run complete for " + handler.data.name, "red");
    return false;
  }
  return true;
}

function killNumThree(handler) {
  log("Assasin Killing number 3");
  handler.data.handleFor3.stop();
  log("Run complete for " + handler.data.name, "red");
  log("No more intervals expected!", "red");
}

// every second ten times
customSetInterval({
  func: run,
  interval: 1000,
  data: {
    counter: 10,
    name: "Number ONE"
  }
});

// every half second 10 times
customSetInterval({
  func: run,
  interval: 500,
  data: {
    counter: 10,
    name: "Number Two"
  }
});

// every 10th second 20 times
customSetInterval({
  func: run,
  interval: 100,
  data: {
    counter: 20,
    name: "Number Two and a half"
  }
});

// every 2.5 seconds 1000 times
const num3Handle = customSetInterval({
  func: run,
  interval: 2500,
  data: {
    counter: 1000,
    name: "Number Three"
  }
});

// once at 11 seconds and stops number 3
customSetInterval({
  func: killNumThree,
  interval: 11000,
  data: {
    name: "Assasin",
    handleFor3: num3Handle
  }
});

var last;

function log(data, col = "black") {
  var div = document.createElement("div");
  div.textContent = data;
  div.style.color = col;
  if (!last) {
    document.body.appendChild(div);
  } else {
    document.body.insertBefore(div, last);
  }
  last = div;
}


Summing different tables values limiting the results

You might try this:

select
  sum(amount) amount
from(
  select
    least(sum(amount),10000) amount
  from (
    select date,
           200 amount
    from   a
    union all
    select date,
           500 amount
    from   b) list_of_all
  group by
    date(date)) list_by_day


For the LEFT case, you're checking that an index is above the low value of zero; for the RIGHT case, you're checking that it's below the high value of field.size.  You can't really reconcile these except by combining them into a method that checks both boundaries - and there's nothing wrong with that.  Now you've got

if (inBounds(theIndex) && isEmptyCell(theIndex) && ...)
    Some(theIndex)

That ... is questionable to me because you seem to be comparing a different index (clickedIndex or clickedIndex + 1) depending on LEFT or RIGHT.

I think there are errors here that you need to reconcile before going too far down the path of duplication elimination.