I think there is quite a lot that could be improved on in your approach.
My main piece of advice is to try and process each line in the data only once, since each line is independent you should be able to do this.
I'm not too familiar with pandas but it seems like there are two main areas of concern.

The section where you clean up the data and filter out all the bad emails, you create a mask by executing two regexs on each line and then read through and make copies of the data frame twice using the mask. At this point you have passed over every line in the data 3 times.

    df = df[0].str.strip(' \t"').str.split('[,|;: \t]+', 1, expand=True).rename(columns={0: 'email', 1: 'data'})
    mask = (df.email.str.contains(emailreg, regex=True, na=False)) & (~df.data.str.contains(asciireg, regex=True, na=False))
    df2 = df[~mask].copy()
    df = df[mask].copy()
    df2[['email', 'data']].to_csv("errorfile", sep=':', index=False, header=False, mode='a', compression='gzip')
    del df2
    del mask

The second section where you breakdown each email into a different file if it is valid. you go through every line in the dataframe for every possible starting letter, and copy the result over to process again. At this point you have gone through each line in the data about 40 times.

for x in "abcdefghijklmnopqrstuvwxyz0123456789":
    df2 = df[df.email.str.startswith(x)]
    if (df.email.size > 0):
        df2[['email', 'data']].to_csv(x, sep=':', index=False, header=False, mode='a')

Running cProfile on the code, when it just has to read one file with 6 lines in it produces this: 336691 function calls (328148 primitive calls) in 0.974 seconds. Nearly a second to just read and process 6 lines into different files is not good.
Rather than taking a pandas approach I have just written a pure python script that sketches out an alternative strategy. Doing the same test with cProfile produces 11228 function calls (11045 primitive calls) in 0.038 seconds. It might not fit your needs exactly but you could look at it for ideas about how to tweak your script.
import re
import logging

EMAIL_REGEX = r"^\w+(?:[-+.']\w+)*@\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*$"
OUTPUT_FILES = "abcdefghijklmnopqrstuvwxyz0123456789"

def configure_logging():
    """
    Configure a logger for each possible email start.
    """

    # TODO - Tweak the handlers, output formats and locations
    # to suit your needs

    error_handler = logging.FileHandler("error.log", mode="a")
    error_handler.setLevel(logging.ERROR)
    error_handler.setFormatter(logging.Formatter('%(message)s'))

    for entry in OUTPUT_FILES:
        logger = logging.getLogger(entry)
        handler = logging.FileHandler(f"{entry}.log", mode="a")
        handler.setFormatter(logging.Formatter('%(message)s'))
        handler.setLevel(logging.INFO)
        logger.addHandler(handler)
        logger.addHandler(error_handler)
        logger.setLevel(logging.INFO)

def gather_files():
    """
    Return all the log files that need to be processed.
    """
    # TODO - replace with your own logic to find files.
    return ["test_input.csv"]

def process_log_file(log_file_path):
    """
    For each line in the log file, process it once.
    """
    with open(log_file_path, "r") as log_file:
        for line in log_file:
            process_line(line)

def process_line(line):
    """
    Find the email and user from a line, test if the email is valid. Log the data
    to the appropriate place.
    """

    # TODO you may wish to change to logic
    # to decide if the line is valid or not.

    line = line.strip(' \t"\n')
    data = re.split(r'[,|;: \t]+', line, maxsplit=1)
    logger = logging.getLogger(data[0][0])
    if len(data) == 2 and re.match(EMAIL_REGEX, data[0]):
        logger.info(":".join(data))
    else:
        logger.error(line)

def main():
    """
    Processes each log file in turn.
    """
    for log_file_path in gather_files():
        process_log_file(log_file_path)

if __name__ == "__main__":
    configure_logging()
    main()

```


Generating all permutations of a sequence

Imports should always be at the top of the file. I would make an exception for those that are needed only if __name__ == '__main__', but in that case they should be within that block. So your code should look like:

import itertools

def permutation_lib(iterable):
   ...

def permutation(iterable):
    ...

def insert_at(iterable, idx, value):
    ...

if __name__ == '__main__':

    import unittest

    class TestPermutation(unittest.TestCase):
        ...

    unittest.main()

which also means that when you import from this script you don't get the test class, or have import unittest at the top with import itertools.

None of your functions/classes/methods have docstrings. Including these to explain what each one does, and even document the parameters, return values, etc. (I like the Google style for this) makes your code easier to follow.

You have one test method with two assertions covering three functions. Does this not seem odd to you? If that method fails, where do you look for the problem? I would suggest factoring out permutation_lib entirely (which also means you can move import itertools to the testing block) and testing insert_at on its own in a separate method.


First of all, please don't take what I am writting personnaly. Also, I am sure there would be something to argue with my proposals so feel free to tell me!

Let's begin with:

@RequestMapping(value = "/export_data")
public void downloadDataInCsv(
        @RequestParam("type") String type,
        @RequestParam("tID") String tableId,
        HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    if (type.equals("csv")) {
        List<UserInfo> list = userInfoDao.findById(tableId);
        ExportCsvUtil.downloadCsv(request, response, list);
    }
}

I hate using plain strings when doing some comparison especially when it looks like a real constant like csv

In order to remove the plain String, I usually use enum to do that.

Why not replace type.equals("csv") by something like type.equals(FileType.CSV.name())

Next, if we are looking here at this chunk:

List<UserInfo> list = userInfoDao.findById(tableId);
ExportCsvUtil.downloadCsv(request, response, list);

we see that your are proceding with ExportCsvUtil.downloadCsv() even if the list is empty. Should this be a normal behaviour of your application ? Maybe you should send a message telling the user that they were no data to retrieve ?

Last thing for downloadDataInCsv() method is that if your type isn't a csv file, it will return a HTTP code 200 with nothing. I think the client would be kind of confused with that. Maybe try to send a different message like I said before. (e.g: only CSV export is available at the moment)

Next chunk! :)

private void downloadCsv(HttpServletRequest request, HttpServletResponse response, List<UserInfo> list) throws IOException {
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=Table_Data.csv");
    response.setContentType("text/csv");
    response.setHeader(headerKey, headerValue);

    try (final CSVWriter writer = new CSVWriter(response.getWriter(), ",")) {

        writer.writeNext(new String[]{"User Id", "First Name", "Last Name", "Roll No", "Email ID", "Gender"});

        for (UserInfo entry: list) {
            // cast/convert to String where needed
            writer.writeNext(new String[]{entry.getUserId()+"", entry.getFirstName(), entry.getLastName(),entry.getRollNo(),entry.getEmail(),entry.getGender()});
        }
        writer.close();
    }
}

If you look at String headerValue = String.format("attachment; filename=Table_Data.csv");, you don't use String.format() capabilities at all! (if I am not mistaken)

String.format() allows you to do things like String.format("Hello %s", name)

You can replace this with an old plain String then.

response.setContentType("text/csv");, if you are using the enum like I said before, you could add a property contentType that allows you to do FileType.CSV.getContentType() in order to remove the hardcoded string.

writer.writeNext(new String[]{"User Id", "First Name", "Last Name", "Roll No", "Email ID", "Gender"});, since you know the header of the file, why not store is as a constant so it is not recreated every time you call the endpoint ?

Like so:

private static final String[] CSV_HEADER = new String[]{"User Id", "First Name", "Last Name", "Roll No", "Email ID", "Gender"});

I think you could do something about the for loop with some of Java 8 fanciness but I am not confident enough to write this without tools for testing.

Lastly, you don't have to do writer.close() since you are opening your file with a try with resource

Basically, CSVWriter class should implement AutoCloseable wich call close() method for you at the end of the try statement so you do not have to worry about this.

Documentation about this is here

Hope my answer helps !


Grouping orders by similarity (cluster) of their items

Faulty algorithm.  Any order would be 100% to an order of all items.

I real life a warehouse worker can only pick up so many items in a pass.

Take an order less than pass size and find the order or orders fully contained that get up to pass size.

After you cannot get up to pass size contained then normalize similarity with match / (size1 + size2).

A better measure of similarity is:

var ratio = 2 * (double)similarities / (minOrder.Items.Count + maxOrder.Items.Count);


The Common Lisp standard does not require implementations to eliminate tail calls, but this is an optimization supported by many implementations. It is the case for Lispworks. You can write your code so that it works portably with multiple implementations, provided you wrap your declarations as required for each one of them (e.g. #+sbcl). Otherwise,  you have to use an iterative approach, which is not hard to implement in your case.

Remarks

The code looks well-formatted, but consider using meaningful names:

Write tolerance instead of tol
Write line-min (?) or golden-section-search instead of linemin
x1, x2, xi1 and xi2 are a little hard to read. Granted, what you write is close to mathematical notations where variable names are short and written with indices, but maybe there are more interesting name to give (low, high?), especially when those names are used as keyword arguments.


The hard part of the challenge is already done for you by Collections.shuffle().  I'll focus on style, efficiency, and elegance.

In the main() function, scanner.nextLine().replace(" ", "") is a weird thing to do.  Would it be acceptable to interpret 5  0 as 50?  Perhaps you meant to do String.trim()?

Style

You have written a comment on every single line, which is too much.  Only beginner programmers are likely to want to state everything twice.  This comment, for example, is silly:

Collections.shuffle(temp); // Shuffles the list

The two lines above it aren't good either.  The // Checks if list is empty comment is noise.  You should not omit the "optional" braces.  You should pick a better variable name than temp.

if(temp.isEmpty()) // Checks if list is empty.
    temp = new ArrayList<>(Arrays.asList(pieces)); // Replaces the pieces in the list.

Efficiency

Repeated concatenation of strings using += is not recommended.  Concatenating n characters that way is O(n2), since each result has to be rebuilt from scratch.  Instead, use a StringBuilder (if you don't know the length of the final result) or a char[] (if you know exactly how long the final string will be).

Calling .remove(0) on an ArrayList is also not recommended.  To fill in the gap after removal, it has to copy over all of the subsequent elements.  Emptying an n-element ArrayList by repeatedly calling .remove(0) is O(n2).  Instead of removing the first element, you could remove the last element.  Better yet, just keep track of the index yourself, so that you don't have to remove anything, and you don't have to keep re-creating the list.

Elegance

A more elegant solution, I think, would be to generate an infinite stream, then take an excerpt from it of the desired length.  Such an infinite stream could actually be used as part of a real Tetris implementation.  Your Generator, on the other hand, has to be instantiated with a length limit (awkwardly named pieceNumber).

Suggested solution

This solution, using streams, produces an infinite sequence, then limits it to length 50.

You can easily tweak it to work like your interactive loop.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ShuffledStream {
    private ShuffledStream() {}   // Suppress the default constructor

    /**
     * Produces an infinite stream of the elements, reshuffling when
     * each batch is exhausted.
     */
    public static <T> Stream<T> stream(T[] elements) {
        return Stream.iterate(Arrays.asList(elements), list -> {
            Collections.shuffle(list = new ArrayList<T>(list));
            return list;
        }).skip(1)              // First batch is unshuffled; skip it
          .flatMap(List::stream);
    }

    public static void main(String[] args) {
        String fifty = ShuffledStream.stream("OISZLJT".split(""))
                                     .limit(50)
                                     .collect(Collectors.joining());
        System.out.println(fifty);
    }
}

Note that since this solution uses generics, you can reuse the same code to deal from 52-card decks.


Adding augmented maps to a vector based on regex text grabbing

Some things that stand out from your code:

(def next-appendage #()) is really (defn next-appendage [part done])
Do not use anonymous functions except for really really short functions. You next-appendage function is long for Clojure standards.
Instead of hash-map you can use the map literal directly `{:name (clojure.string/replace ,,,}
Even though it seems cool when you are starting Clojure, one liners are very hard to read. You already know that is "really ugly" :)
It is unusual to use loop. Most of the time you can replace it with a simpler map or similar
Do not use read-string, unless you know what you are doing

My version would look like:

(defn add-apendages [times part]
  (let [ ;; find the prefix and initial number
        [_ prefix number-str] (first (re-seq #"(.*)-([0-9]+)$" (:name part)))
         ;; parse the number, using Java interop
        initial-number (Integer/parseInt number-str)]
    ;; Build the list of results using a simple map operation
    (map (fn [n] {:name (str prefix "-" n)})
         (range initial-number (+ 1 initial-number times)))))


Please add a docstring.

Simplify_by_avg_weighted might be named points_near_centroid (or _center if you prefer).

Please initialize with L = math.inf, to clarify your intent.

Rather than a variable length list for [x,y,1], please store it as a 3-tuple (x, y, 1), or more naturally as a pair ((x, y), 1), where 1 is a count rather than a spatial coordinate.

    for i in xrange(len(points)):

This would more naturally be expressed as for pt in points:.

Now we come to the heart of the matter. It's a little hard to evaluate the code in the loop, as it seems to differ from your English language description, and it's hard to declare a "violates spec" bug when there's no docstring offering a spec.

When you assign x, y, they might better have names like x_centroid. And since z is not a spatial coordinate, it would more appropriately be named count. It's fine that you don't take square root, but d should have a name like dsq for distance squared.

To "return those points that are within delta of the mean position", does the interpreter even need to loop? It should suffice to compute centroid of the whole cloud (numpy does the looping), then select on distance (more numpy looping).


Error handling is indeed an issue in C with a multitude of potential handling mechanisms.  Good to see your take on it.

Allow for atomic error handling.  In addition to getting the error, it would be useful to have a test and clear to allow simpler error testing.  The point is that once upper level code tests positive for an error (the error is caught), the global test status should be immediately cleared (atomic) to allow for subsequent setting without a chance of intervening code messing up things.

bool err_test_and_clear(int *err) {
  // Simplistic code.  See also "C11 7.17 Atomics <stdatomic.h>"
  if (err) {
    *err = error;
  }
  bool e = error != ERR_OK;
  error = ERR_OK;
  return e;
}

// usage
foo();
int err;
if (err_test_and_clear(&err)) {
  foo_clean_up();  // code may cause an error
  fprintf(stderr, "Error: %d\n", err);  // report original error
}

vs.

// usage
foo();
if (ERR) {
  foo_clean_up(); // code may cause error - oops this should have been after below 2
  int err = err_get();
  err_set(ERR_OK);
  fprintf(stderr, "Error: %d\n", err);  // report maybe original error
}

Not a fan of ERR, which looks like a constant or object and it is not.  Perhaps make it look like a function. Or be emphatic that ERR_OK is 0.

// #define ERR (err_get() != ERR_OK)

// better
#define ERR() (err_get() != ERR_OK)

// or best no define, just use `if(err_get())`

If code like if (err_get() != ERR_OK) is preferred over if (err_get()), then code should use

// _Thread_local static int error;
_Thread_local static int error = ERR_OK;

Missing ,

enum err {
    ERR_OK    , // add ,
    ERR_SOME,
};

I'm leaning that _Thread_local static int error may benefit with being volatile.  Have to ponder that more.
For easier to read semantics, consider a clear function.  err_clear(); has more clarity than err_set(ERR_OK); ("setting the error" of "OK error").  Further ERR_OK looks contradictory.  Perhaps ERR_NONE?

void err_clear(void) {
  error = ERR_OK;
}

Mixed "types".  I'd expect both int or both enum err

             vvvvvvvv
void err_set(enum err e);
int err_get(void);
^^^


Two things I noticed/would recommend:

Cache jQuery variables such as $(this) and $('.fixMe') to prevent redundant lookups during each scroll event.
Implement an event debouncer/limiter, this is because the scroll event fires a lot more than you likely think it does. There are many utils/plugins to achieve this such as:

https://github.com/cowboy/jquery-throttle-debounce
http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
http://documentcloud.github.com/underscore/

I even wrote a very simple one: http://limit.gotsomething.com/. My site will show you why debouncing/limiting is useful.

Other than that looks good to me!

cheers,
Marc.


I don't know what you would use reduce() for to be honest. But you don't need to loop. We just have a simple arithmetic sequence with a straightforward formula:

$$\sum_{i=min}^{max} i = \frac{(max-min+1)(min+max)}2$$

We just need to pick out which one is larger:

function sumFrom(min, max) {
    return (max-min+1) * (min+max) / 2;
}

function sumAll(arr) {
    return sumFrom(Math.min(...arr), Math.max(...arr));
}

Or we could generalize to any arithmetic sequence:

function sumArithmetic(a1, n, d) {
    return n*(2*a1 + (n-1)*d) / 2;
}

function sumAll(arr) {
    var min = Math.min(...arr);
    return sumArithmetic(min, Math.max(...arr) - min + 1, 1);
}

I guess if you really want reduce() you'd do it this way (not using ES6 because I don't know how to get freecodecamp to use it):

function sumAll(arr) {
  var min = Math.min.apply(null, arr);
  var max = Math.max.apply(null, arr);

  return Array.apply(null, Array(max-min+1))   // get array of correct size
      .map(function(_, b) { return b+min; })   // change it to have correct values
      .reduce(function(a, b) { return a+b; }); // and sum it
}


First up, this is a nice project, and your implementation looks like it has the right basic concepts, but you're right, it could be done better.

There are two concepts you need to apply that will help you a lot...

Parent -> child relationships

The first is the concept of a parent/child relationship. You should consider that each object in your solar system has one or more child objects. The sun has the planets, the planets may have moons.

The logic of 'animating' your solar system is common to all objects. First, you move the object, then you move the dependent ones.

To do this, I extend the PointInSpace class to be:

public class ObjectInSpace extends PointInSpace {
    private final List<ObjectInSpace> dependents = new ArrayList<>();
    private final ObjectInSpace parent;

    public ObjectInSpace(ObjectInSpace parent, double distance, double angle, double diameter, String colour, double speed) {
        super(distance, angle, diameter, colour, speed);
        if (parent != null) {
            parent.addDependent(this);
        }
    }

    private void addDependent(ObjectInSpace kid) {
        // can only be called from constructor of kid instance
        dependents.add(kid);
    }

    public ObjectInSpace getParent() {
        return parent;
    }
    public List<ObjectInSpace> getDependents() {
        // return a copy, not the actual list.
        return new ArrayList<>(dependents);
    }
}

Right, now, with the above class, you can construct your solar system from the top down....

ObjectInSpace sun = new ObjectInSpace(null, .....);
....
ObjectInSpace earth = new ObjectInSpace(sun, ....);
....
ObjectInSpace moon = new ObjectInSpace(earth, ....);

Note that the only thing you need to remember is the sun, and all the other objects 'hang off' the sun. So, create a method:

public ObjectInSpace buildSolarSystem() {
    ObjectInSpace sun = .....
    ....
    return sun;
}

and then, in your code, you can just have:

ObjectInSpace solarsystem = buildSolarSystem();

Now, when you need to draw/move the solar system, you just need to visit every object, from the sun down, and process that object, then process in turn all of its children...

... which leads on to the second concept:

Recursion

The next concept to process is recursion. Consider the need to draw the solar system. All you need is a method:

public void draw(ObjectInSpace object) {
    // draw the object
    ....
    for (ObjectInSpace kid : object.getDependents()) {
        // recurse and draw the kid
        draw(kid);
    }
}

Something similar can be done to move the objects:

public void move(ObjectInSpace object) {
    // move the object
    if (object.getParent() != null) {
        // move the object relative to the parent.
    }
    ....
    for (ObjectInSpace kid : object.getDependents()) {
        // recurse and move the kid
        move(kid);
    }
}


Overall, even for a non-first-timer with SQL, this is a good query. The consistency and styling makes it very clear you're not a programming newbie.

To answer your concern about "under" versus "under or on", and 0 meaning an empty wiki - I totally agree. For me, 0 meaning empty is the only logical interpretation - empty strings have length 0 in every language I've come across. The way you've named your parameters (MaxSomething) implies clearly that the user is inputting the highest number that will be included in the results - and that's what they get. This is all perfectly clear and sensible to me, no need to worry.

Minor niggles

You don't need the BEGIN and END wrappers here. They don't hurt, but they're only required for control-of-flow blocks (usually, IF and WHILE constructs).

You don't need @LEN_MAX in the current version of the query.

For a "report" type query like this, I'd always prefer to have default ordering set by the query (an ORDER BY clause) so the results are guaranteed consistent.

The WHERE clause

Looking at this half a year on from your original post, I can see you've refined the WHERE clause yourself. At the time of my writing the WHERE clause on the linked SEDE query is:

WHERE
(@max_body_length >= 0 AND LEN(pWiki.body) <= @max_body_length)
OR (@max_excerpt_length >= 0 AND LEN(pExcerpt.body) <= @max_excerpt_length)

This is definitely a much clearer way to express your desired logic than the CASE statement in the original post.  For me, the reason it's clearer is that it maps quite easily to reading in plain English:

  Show me rows where the max body length is 0 or more and the length of the Wiki Body is less than or equal to that, or where...

I reckon we could go further though :)

Different operators can improve readability

This is a matter of opinion only, but if your aim is to make query logic easily readable in plain English, sometimes inverting your operators can help that.

@max_body_length >= 0

  "max body length is greater than or equal to 0, I mean 0 or more"

This is what I think when I read this code; I re-word it post-hoc to "0 or more" after understanding the meaning.

@max_body_length !< 0

  "max_body_length is not less than 0"

For me this reads more clearly first-pass, and doesn't need a post-hoc re-scan. It'll depend a lot on the reader and how familiar they are with ! as a negation operator (some people prefer <>).

An even shorter alternative

Shorter isn't always better: for complex logic, spelling it out step by step (more verbosely) as you have can be much clearer, because of the "mapping to plain English" property.

However, we could go shorter here using nullif() to equate your -1 default values with SQL NULL i.e. "unknown"/"not applicable":

WHERE
LEN(pWiki.body) <= nullif(@max_body_length,-1)
or LEN(pExcerpt.body) <= nullif(@max_excerpt_length,-1)

This takes advantage of the all-consuming nature of NULL: if I ask "is the length of this (known) thing less than an unknown number", the only possible answer can be "I don't know" - another NULL.

Then, because SQL NULL doesn't evaluate to true, the overall WHERE clause as a whole works out just as you intend.

However, the catch here (you probably spotted it) is that this only works for -1 as a default, not any negative input. Since negative inputs apart from -1 are meaningless in this query, you could validate this at the start of the query:

IF (@max_body_length<-1) or (@max_excerpt_length<-1)
    RAISERROR('Input parameters should be -1, 0 or positive integers',16,1)

Frankly this is probably overkill for a SEDE query but demonstrates the right approach for more generic application.

Ideally, one would actually make the default input for each parameter be NULL (rather than -1) and only permit 0 or positive input values: then you wouldn't need the nullif functions at all. However I'm not aware of a way to do that in SEDE.


Use of pointers with file-reading program

1. Introduction

You'll see that I made a lot of comments below, but if you're just starting to learn C, then your code is not at all bad. C is a tough language to write robust code in: it provides many many ways to go wrong, and even the most expert and experienced programmers don't always succeed in avoiding the traps and pitfalls.

I like your attention to error handling, that you check the result of realloc, that you remember to close your file handle, and that you use the symbols EXIT_SUCCESS and EXIT_FAILURE to make it clear what you mean.

2. Bugs

You don't check whether there were in fact any names in the file before printing the first of them. If the file was empty then this would result in undefined behaviour:

$ clang cr27281.c
$ cat /dev/null > names
$ ./a.out
Segmentation fault: 11

You don't check the result of fopen. If this call failed then you'd end up trying to read from the file handle NULL:

$ rm names
$ ./a.out
Segmentation fault: 11

3. Other comments on your code

If you need to return multiple values from a function, then why not use a structure? You could declare a structure like this:

typedef struct name_array_s {
    char **names;         /* Array of names, allocated with malloc. */
    size_t count;         /* Count of names in the 'name' array. */
} *name_array_t;

and then readFile could take a name_array_t as its first argument.
There's no comment or documentation explaining the interface to readFile. What is this function supposed to do and how are you supposed to call it? Imagine coming back to this code in a couple of years' time when you have forgotten all about it. Do you really want to have to reverse-engineer it to figure out what it does?
The variable names_size is an int, but it counts the number of entries in the names array and it can never legitimately be negative. And there are common platforms where INT_MAX is 2147483647, but where you have enough memory to store many more objects than that. So why not make names_size a size_t?
Similarly for the result of the function readFile.
Consider using abort() in your error handler instead of exit(EXIT_FAILURE). When running under the debugger, abort() drops you straight into the debugger prompt with a good chance of being able to find useful information in the backtrace, but exit() just quits the program, leaving you none the wiser.
It's a bit restrictive that the file has to be named names. Why not take the name on the command line in argv[1]?
This line:

if(error == ENOMEM) printf("size: %d\n", error);

has four problems! First, error messages should go to standard error, not (as here) standard output, so you need fprintf(stderr, ...). Second, you want size instead of error in the call to printf. Third, size is a size_t but you are using a %d format specifier which takes an int, so this results in undefined behaviour. Fourth, by putting the if and the printf on the same line, you've made it impossible (in most debuggers) to set a breakpoint on the printf. I would write:

if(error == ENOMEM)
    fprintf(stderr, "size: %lu\n", (unsigned long)size);

The C language allows you to combine the declaration of a variable and its initialization, so instead of writing the repetitive

char **names;
int names_size;
FILE *file;
int chars_read;
int i;

names = NULL;
file = NULL;
names_size = 0;
chars_read = 0;

you can write

char **names = NULL;
int names_size = 0;
FILE *file = NULL;
int chars_read = 0;
int i;

After making the change above, it becomes obvious that the variable i is unused. But shouldn't your compiler have complained about that? When writing a new program it's well worth turning on as many error-detecting features as you can, for example if you're using GCC it's a good idea to use a comprehensive set of flags like -ansi -pedantic -std=c99 -Wall -Werror.
The variable chars_read is also unused.
Having an assignment buried inside an expression seems error-prone and hard to understand (especially when broken across lines like this):

name = _realloc(name,
    sizeof(char) * (name_size =
    (name_size + 1) * chunk_size));

I prefer to keep the code simple:

name_size = (name_size + 1) * chunk_size;
name = _realloc(name, sizeof(char) * name_size));

sizeof(char) is defined to be 1 by the C standard, so you don't need it here (it doesn't do any harm, though).

  §6.5.3.4.3 When [sizeof is] applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

chunk_size is poorly named: it's not a size, it's an exponent that controls the exponential growth of the name buffer.

4. Performance

Your algorithm for allocation involves reallocating the names array each time a new line is reached in the file. This is wasteful, because each time this array is reallocated, the contents may have to be copied across to the new region of memory, potentially making it O(n2).

If your files are short then you may prefer not to worry about this, but after I did a simple rewrite of your readFile function to do less reallocation, I was able to get a speedup of more than 3× in test cases with many lines, like this one:

$ yes '' | head -10000000 | nl -ba > names
$ time ./names-old > /dev/null
real    0m4.136s
user    0m3.777s
sys     0m0.333s
$ time ./names-new > /dev/null
real    0m1.248s
user    0m0.946s
sys     0m0.287s


Constructing and initializing global objects

First, I would suggest against the Global name. Too generic. Next, I would suggest against putting it in the global scope. You can put the declaration inside jQuery's ready handler.

Another thing is to avoid the unnecessary "configuration". Sometimes, it's best if the code is straight-forward. The structure of a configuration is sometimes a roadblock. Takes up too much syntax but doesn't immediately tell you what the code does.

For instance, if I was looking at this code for the very first time, I would be wondering how button1 is related to this.button1, what button1 should be, when is init is called, etc. Once I get the hang of it, sure it works. But what if there's an architectural change that invalidates this style of configuration? What if you've done this to 1000 things? You can't easily pull out and rewrite the code.

Keep it simple.

$(function () {

  $("#button1").on('click', handler);
  $("#button2").on('click', handler);

});

On the other hand, if you have an app that is already starting to look complex, consider using a simple framework. There's Ractive, Vue and Riot which are small frameworks allow you to write self-contained portions of the site. This way, each "component" only houses functionality of that component, splitting up this "large number" into sections.


There isn't a whole lot here to comment on. I'll just mention a few things:

Technically, from my quick search of what a powerset is, this function should return sets. That seems petty, but unless it's documented to return a lazy list of lazy lists, users may try to treat the "subsets" as sets (like using them as functions). I'd finish this function off by mapping set over the list.
But to do that, you should rename your parameter, as you're shadowing the build-in set.
After doing the above two, it developed quite long lines and became nested. I'd add in some use of ->>, and put a few of the lines on the next line.

After that, I ended up with:

(defn powerset [base-set]
  (->> base-set
       (reduce
         (fn [xs x]
           (concat xs
                   (map #(cons x %) xs)))
         [()])

       (map set)))


Justifying a string followed by a number

You are vastly overcomplicating the solution.  The Scanner is perfectly capable of reading a string and an integer separately.  Then, it is a simple matter of calling System.out.printf() with the appropriate width specifications:

%-15s for a string, left-justified, with trailing spaces
%03d for an integer, zero-padded

Also, Scanner is AutoCloseable, so you should use a try-with-resources block instead of calling .close() on it manually.

import java.util.Scanner;

public class IOFormatting {
    public static void main(String[] args) {
        try (Scanner input = new Scanner(System.in)) {
            while (input.hasNext()) {
                System.out.printf("%-15s%03d%n", input.next(), input.nextInt());
            }
        }
    }
}

If you really wanted to do the formatting the hard way by inserting spaces and zeroes, I suggest editing each line using StringBuilder.insert() — your ArrayList<Character> is a rather unnatural way to edit a string.


Functions
It's good that you're thinking about how to capture code in functions, but you haven't particularly chosen the right code to move into functions.
This is somewhat trivial:
print("profit: " + str(profit), end = "\n")

and does not deserve its own function; simply write
print(f'profit: {profit}')

at the outer level. The same applies for output_subset, which does not need a loop and can be
    print(' '.join(item for item in subset))

Instead, something that does deserve to be in a separate function is your set of loops starting at for row, which can be translated into a generator; also note that 0 is the default start for range:
ProfitPair = Tuple[
    int,
    List[str],
]

def get_profits( ... variables needed for iteration ...) -> Iterable[ProfitPair]:
    for row in range(len(items)):
        for col in range(len(items) + 1):
            subset = items[row:col]
            for job_index in range(len(subset)):
                if items_starts[job_index] >= machine_starts[job_index]:
                    if items_ends[job_index] <= machine_ends[job_index]:
                        done_tasks[job_index] = done_tasks[job_index] + 1
            profit = 0
            for job_index in range(len(subset)):
                if tasks_to_complete[job_index] == done_tasks[job_index]:
                    profit += profits_for_job[job_index]

            yield (profit, subset)

Type hints
It's good that you've tried this out. subset:[str] should be subset: List[str].
Indexing
for row in range(0, len(items)):
    for col in range(0, len(items) + 1):
        subset = items[row:col]

seems strange to me. Based on your initialization, items is not a two-dimensional (nested) list - unless you count string indexing as the second dimension. row and col are thus somewhat misnamed, and are basically start and end.
In-place addition
done_tasks[job_index] = done_tasks[job_index] + 1

should be
done_tasks[job_index] += 1

Summation with generators
        profit = 0
        for job_index in range(0, len(subset)):
            if tasks_to_complete[job_index] == done_tasks[job_index]:
                profit = profit + profits_for_job[job_index]

can be
profit = sum(
    profits_for_job[job_index]
    for job_index in range(len(subset))
    if tasks_to_complete[job_index] == done_tasks[job_index]
)

This raises another point, though. Consider "rotating" your data structure so that, instead of multiple sequences where the same index in each correspond to a description of the same thing, e.g.
profits_for_job[job_index]
tasks_to_complete[job_index]
done_tasks[job_index]

instead have a sequence of @dataclasses with attributes:
job[job_index].profits
job[job_index].tasks_to_complete
job[job_index].tasks_done

Predicate combination
            if items_starts[job_index] >= machine_starts[job_index]:
                if items_ends[job_index] <= machine_ends[job_index]:
                    done_tasks[job_index] = done_tasks[job_index] + 1

can just be
if (
    items_starts[job_index] >= machine_starts[job_index] and
    items_ends[job_index] <= machine_ends[job_index]
):
    done_tasks[job_index] += 1


HackerEarth problem : number of occurrences of a number in the array

You can maintain a HashMap where key will be the number and value will be its frequency.


It's unfortunate that there are two reverse() functions that do similar things.  What is even more disconcerting is that they are inconsistent: main() is responsible for splitting a sentence into words, but reverse(List<String> words) joins them back up.  On the other hand, reverse(String word) doesn't work that way: it does both the splitting and joining.

Why not solve both problems consistently with one function?

import java.util.*;
import java.util.regex.Pattern;

public class Reverse {
    private Reverse() {}  // Suppress the default constructor

    /**
     * Splits the input on the delimiter, reverses the list, then joins the
     * results with the delimiter.
     */
    public static String reverse(String s, String delimiter) {
        List<String> list = Arrays.asList(s.split(Pattern.quote(delimiter)));
        Collections.reverse(list);
        return String.join(delimiter, list);
    }

    public static void main(String[] args) {
        System.out.print(">>> ");
        String input = new Scanner(System.in).nextLine();

        if (input.contains(" ")) {
            // Reverse words in a sentence
            System.out.println(reverse(input, " "));
        } else {
            // Reverse characters in a word
            System.out.println(reverse(input, ""));
        }
    }
}

Note that there is a technicality, if reverse() is exposed as a public function as I have done.  In that case, you would expect it to work for any delimiter, so the delimiter must be quoted such that String.split() treats it as a literal string rather than a regular expression.


There are many ways to test this. Mostly, though, it'd be nice to avoid having to create 50+ records, since it slows down your tests.

If you use a request spec, though, it's probably best to create 50+ records, since it's a high-level test, so you'll want to be close to the "real" usage scenario.

But you can cheat a little in other places. For instance, if you have the records-per-page number defined in a way that's configurable, you can set it to something lower in you pagination test (or you can set it globally for the test environment). For instance, if the per-page is set to 2, you only need to create 3 records to test pagination. That'll be a lot faster than creating 51 records.

If you're spec'ing the view itself, you can simply define the instance variables that'll trigger pagination links, and not bother with the actual records. Or you can use FactoryGirl.build_list to merely build the records and assign them to a view-accessible variable, without actually storing them in the database - again, faster.

You can also look into mocking and stubbing to avoid actually creating the records.

For your current code, You can do a couple of things, like:

describe "Companies" do
  describe "GET /companies", order: :defined do
    before(:all) { FactoryGirl.create_list :company, PER_PAGE }

    context "with few records" do
      it "does not paginate records" do
        visit "/companies"
        expect(page).to have_no_xpath("//*[@class='pagination']//a[text()='2']")
      end
    end

    context "with many records" do
      it "paginates records" do
        FactoryGirl.create :company

        visit "/companies"
        expect(page).to have_xpath("//*[@class='pagination']//a[text()='2']")
        find("//*[@class='pagination']//a[text()='2']").click
        expect(page.status_code).to eq(200)
      end
    end
  end
end

Changes I've made:

Using FactoryGirl.create_list to create a number of records at once.
Using a PER_PAGE constant, just in case it isn't 50. This could also be an ENV var, an instance variable, or simply hard-coded. But naming it helps document the code.
Using order: :defined to force the examples to be run in the order they're defined. This avoids the specs randomly failing because the 2nd test has been run before the first one.
I've change the visit path to /companies because that's what the spec is about. You used visit root_path, which no doubt worked fine, but the spec is about visiting /companies, so I find it nicer to keep it consistent.

You might also want to check that the correct records actually show up on the page. I.e. attempt to find the name of the 51st company within the rendered page, when you've gone to the 2nd page's path.

Lastly, you may want to add some specs for how the system should behave if you go say page 4, but there aren't enough records to show anything.

But again, I'd probably start with view/controller specs, before moving on to high-level request specs. Request specs are great because they test everything pretty close to actual usage. But that also makes them more complex, so the more you can check at a lower level, the better.


Client-server application for logging running processes

SERVER_1_PASS = os.getenv('PASS_1') or ""
SERVER_2_PASS = os.getenv('PASS_2') or ""

I don't think it's a good idea to pass secrets in the environment - that's too easily read by other processes.  Instead, prefer to hold them in a file that's accessible only by the user.  Since we're using SSH, we even already have such a file ($HOME/.ssh/config), though a better choice would be to use public-key authentication.

ssh.connect(SERVER_1, username='root', password=SERVER_1_PASS)

Ouch - why to we need to connect as root?  We should have a dedicated user for this, with the minimum level of capability to perform the task.
What does connect() do when SERVER_1 is the empty string (our default if not passed in environment)?  Is that what we want, or should we error out in that case?


I don't know any machine learning, however, there are some ways your code can be improved.
PEP8 is Python default style guide,
and so we use lower_case_with_underscores rather than mixedCase for most names.
The major exception to this is classes that use CapitalizedWords.

If we look at your variable names:

total_positive is incorrect, it's the sum of targets, so could be called total_targets.
total_negative is even more incorrect, it's the length minus the total.
sortedTargets is correct.
tprs seems like it means something well known, so it's a good name. Also the plural is great as it's a list.
predictedTruePositiveSofar is long, instead I'd move this into it's own function and then use it once in a loop.

And so I don't like most of your names.
Try to pick good names, like tprs, for all your variables.
Naming is hard, so write a concise description of the variable in the context of the function.
But if that's too big, you may want to make a new function.

The code is mostly ok, but I'd move the initialization of your lists to where they are used.
I'd also change your two sorted calls to a single one with two zips.
To do this you need to use the * operator, to expand a list to arguments.
Which is quite well explained here.
But in short, range(1, 10, 2) is the same as range(*[1, 10, 2]).
This simplifies the creation of sorted_scores and sorted_targets.

After this I'd make a function accumulate_truth.
This will yield the amount of truthy and falsy values.
And so can move the predicted_true_positive_so_far into it's own known function.
As naming it something useful will be hard in the current context.
I'd also change this function to iterate through the list,
rather than through a range and index,
and to remove the == 1 as that can change how the function works.
This is as bool([1]) is True, but [1] == 1 is False.
So your current implementation removes some truthy values.

To simplify the creation of tprs and fprs I'd change the creation of total_positive to be a float.
This changes total_negative to a float too, and makes all the items in tprs become floats too.
Alternately you can use __future__.division.

Finally try not to merge printing and doing work in a single function.
Instead you should return tprs and fprs, and print them in the __main__.

This can result in:

from __future__ import division

def accumulate_truth(iterable):
    true = 0
    false = 0
    for i in iterable:
        if i:
            true += 1
        else:
            false += 1
        yield true, false

def home_made_ROC(scores, targets):
    total_targets = sum(targets)
    length_sub_targets = len(targets) - total_targets

    scores, targets = zip(*sorted(zip(scores, targets), reverse=True))
    tprs = []
    fprs = []
    for true, false in accumulate_truth(targets):
        tprs.append(true / total_targets)
        fprs.append(false / length_sub_targets)

    return tprs, fprs

if __name__ == '__main__':
    tprs, fprs = home_made_ROC([0.1, 0.4, 0.35, 0.8], [0, 0, 1, 1])
    print tprs
    print fprs


General

I could work with this code, I only have a few remarks.

Globals

You should consider as a minimum to have your 4 globals in an object.

var player =
{
  spotifiedList : [],
  youtubeSongs : 0,
  spotifySongs : 0,
  spotifiedSongs : 0,
  playNext : 0
}

You could even add most of the functions to the player object

resetTimer

You should remove the console.log, which makes this a simple one liner. If you keep it ( function name neatly explains what it does ), make sure to call it from play() instead of doing a straight clearTimeout(playNext).

startProcess

You could consider caching the jQuery queries, maybe in the before proposed player object.

retrieveYoutubePlaylist

if (typeof data.feed.entry === 'undefined') {} else { is just wrong.

retrieveSpotify

Encoding onclick= is wrong as well.
Building HTML should be contained to separate functions.

parseTitle

I have no idea what that does, a few comments are needed there

play

I am curious, what does >> 0 get you ? Force it into an integer?


ASP.NET MVC website that display blogs from the database by AJAX

Putting your JavaScript alternative in <noscript> is exactly what <noscript> was meant for. There is nothing in the specs that would make this a bad idea.

I have to note that the derivation of rowsNumb in your code is terrible:

    var rowsNumb = 10;   //<- RowsNumb is 10
    var db = Database.Open("Northwind");
    rowsNumb = Request["lastRec"].AsInt() + 10; //No longer 10, but last request + 10
    var maxRecs = db.QueryValue("SELECT COUNT([Product Id]) FROM Products");
    var sql = @"SELECT * FROM Products ORDER BY [Product Id] OFFSET 0 ROWS FETCH NEXT @0 ROWS  ONLY";
    rowsNumb = (rowsNumb > maxRecs ? maxRecs : rowsNumb); //Possibly not last request + 10 ..
    var result = db.Query(sql, rowsNumb);

You could try something like this:

    var db = Database.Open("Northwind");
    var maxRecs = db.QueryValue("SELECT COUNT([Product Id]) FROM Products");
    var rowsNumb = Math.min( Request["lastRec"].AsInt() + 10 , maxRecs )
    var sql = @"SELECT * FROM Products ORDER BY [Product Id] OFFSET 0 ROWS FETCH NEXT @0 ROWS  ONLY";
    var result = db.Query(sql, rowsNumb);

I am also not 100% about your SQL statement, but given that it goes against Nortwind..


This is not a template class at all, but sort of a wrapper for file_exists() function. It does nothing of the templating business but just gets you a filename.

To make it a template class,

make it accept an array with all data used in the template
add a render() function that will extract data array into separate variables and then include the template file.
add some sort of auto-escaping feature (like going through the data array recursively and escaping all scalar values)
a possibility to return the rendered HTML instead of outputting it
add some facility to call a "master" template that will add the common site design to the certain page's template.

So a usage example would be

require_once 'Template.class.php';
$data = [
    'data' => $data,
    'results' => $results,
];
$template = new Template('path/to/template/file');
$template->render($data);

Quite off topic but consider a more tidy syntax for your template.

        <?php foreach($data as $row): ?>
          <?= $row['h'] ?>
          &nbsp;-&nbsp;
          <?= $row['a'] ?>

<?= $results[mt_rand(1,12)] ?>

        <?php endforeach ?>


Technically this is UB.
Writing a value to one member of a union, then reading from another member (that was not written to) of the union is undefined behavior.
You would be better off just casting to a char* and just reversing it.
swap(std::uint64_t& num)
{
    // I like the use of reinterpret_cast here
    // it makes sure that somebody reading the code notices
    // and takes special care to review what is happening.
    //
    char* first = reinterpret_cast<char*>(&num);
    char* last  = first + sizeof(num);

    // The advantage of reverse here:
    //
    // The standard library can have pre built optimizations
    // that will use hardware specific optimizations of this that
    // can convert it to a single instruction on some hardware.
    //
    std::reverse(first, last);
}

The question becomes are you doing this to support endianness?
If so then your code is only going to work on machines that have the same endianness as the platform you are currently working on.
Normally people want to swap from platform byte order to network byte order (or the reverse). Depending on architecture this is either a swap (as you have above) or a NO-OP or for some weird hardware something completely different.
To support this we have specific family of functions:
htonl()   // host to network long  (32 byte)
htons()   // host to network short (16 byte)

ntohl()   // network to host long  (32 byte)
ntohs()   // network to host short (16 byte)

These functions will do the appropriate conversion for your hardware.


If I understand correctly what you're doing, I think what you need is a UserControl that encapsulates some AddressBookCommand, ClearCommand, NextCommand and PreviousCommand commands.

Your main view would have 3 instances of that control, one for each field ("To", "CC", "BCC"), and each would have its own data context, some IList<Contact> I'd guess.

Also you're not showing your entire class so it's hard to tell, but since you mention MVVM I believe a lot of this code belongs in a ViewModel class, not in the View's code-behind; the XAML markup uses DataBinding markup extensions to bind visual element properties to properties in the ViewModel - the Window's DataContext property is set to the instance of the ViewModel class.

The reason I mention this, is because ideally in MVVM the View should only be caring for presentation concerns, which clearly Details.Recipients.Add(Recipient); isn't.


Please do not do this:

using namespace std;

I know every stupid shitty book about C++ does this. But don't. It works fine if your code is 10 lines long but anything larger it becomes a problem. So it is a really bad habit to start and breaking habits is hard so don't start it. Do yourself a favour and get into the habit of not using it. See Why is “using namespace std;” considered bad practice? for details.

This looks like it should be a constant:

int MaxRange= 50;

You should make it const. Then you can also use it as the size of the arrays below.

Declare one variable per line:

int i = 0, j = 0, k = 0, sum = 0;

The is one corner case where it does not do what you expect. But that's not the real reason for not doing this. Readability. And declare the variable as close to the point of use as possible. No point in declaring a variable that is not used until the bottom line of a program. Also loop variables should be declared as part of the loop so there affect does not bleed beyond the loop.

for(int loop = 0; loop < 5; ++loop)
{
}
// loop not valid here.

Also use better names. If you have a loop that covers multiple lines. Try and find all uses of the variable i. Your editor is going to hit a bunch of words in comments and other things. Which is a real pain when you are trying to fix code.

Why do you only initialize half of the mycompare. It may be valid but a comment on why would definately be in order.

int MaxRange= 50;
int mycompare[90]={0};
for(j=0;j <= MaxRange;j++){
    mycompare[j] = j;
}

After reading the code it seems that mycompare is not really needed.
Even in your loop to print out the values you don't use it.

This is an anti pattern for reading from a stream.
What happens if the read (operator>>) fails?

do
{
    theFile>>myint[i];
    // STUFF
}
while((myint[i-1] >=0) && (myint[i-1] <= MaxRange));

The correct patter for reading from a file is:
Note: You will find that nearly all programming languages work this way. It is a VERY common pattern. Put the read as part of the loop conditional to validate the read worked.

while(theFile>>myint[i])
{
    // Read worked so do work.
}

You can combine your other conditions:

while((theFile>>myint[i]) && (myint[i] >=0) && (myint[i] <= MaxRange))
{
    // Read worked and value in the correct range.
}

Try to always use {} around block statements. It will save your butt one day.

    for(j=0;j<=MaxRange;j++)
        for(k=0;k<=MaxRange;k++)

Prefer

    for(j=0;j<=MaxRange;j++)
    {
        for(k=0;k<=MaxRange;k++)
        {
        }
    }

Not sure what the outer loop is doing here.

    for(j=0;j<=MaxRange;j++)              // This line seems redundant.
        for(k=0;k<=MaxRange;k++)
        {
            j = k;                        // because of this line.
            if(myint[i] == mycompare[j])
                mycount[k] = mycount[k]+1;
        }

You may as well remove the outer loop.

Prefer not to use std::endl to mark end of line. Perfer '\n'. The difference is that std::endl puts the '\n' character on the stream then flushes the stream. If you don't need the flush do not use it. It makes the stream much more efficient not to use it.

cout<< "The Number\t\t Frequency"<<endl;
cout<<"------\t\t------"<<endl;

In C++ the return in main() is optional.
If you only return 0 then I would encourage you not put the return in the code. It is an indication that the program can not logically fail.

return 0;

Conclusion:

You need to learn how to use the built in types. This problem becomes trivial to do by using the std::map.

int main()
{
    std::map<int,int>   data;
    int  tmp;

    // Read the data
    ifstream theFile ("inputData.txt");
    while(theFile >> tmp)
    {
        ++data[tmp];
    }

    // Write the data
    for(auto loop = data.begin(); loop != data.end();++loop)
    {
         std::cout << loop->first << "\t\t" << loop->second << "\n";
    }
}


The basic aim is to put repeating parts in the function, while passing as a parameter anything that's different:

if (elementMissing(source, "afcd")) {
    return;
}

if (elementMissing(source, "rteirl")) {
    return;
}

bool elementMissing(source, element) {
    if (! elementExists(source, element)) {
        ppd << endl << "no " << element << " tag";
        cRep = ppd.str();
        cSuc = false;
        return true;
    } else {
        return false;
    }
}

I'm not sure what cRep and cSuc are supposed to be doing, so I can't help you with that part.


Finding largest prime factor from inputted number

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Problems
{

You can make this class static since it doesn't make sense to make instances of it.

    static class Program
    {

Local variables are usually not capitalized.

        static int FindFactor(Int64 userInput)
        {

The compiler can infer the type if you use var, so you won't have to repeat it.

            var largestFactor = 0L;
            var number = userInput;

            for (var i = 2; i < number; ++i)
            {

Use && instead of nested if-statements.

                if (number % i == 0 && IsPrimeFactor(i))
                {
                    largestFactor = i;
                }
            }

            return largestFactor;
        }

Usually you put a blank line between function definitions.

Functions returning Booleans are often called "IsBlaBla" instead of "BlaBlaCheck."

        static bool IsPrimeFactor(int number)
        {
            var primeNumberValidity = true;

            for(int i = 2; i < number; ++i)
            {
                if (primeNumberValidity && number % i == 0)
                {
                    primeNumberValidity = false;

You can exit the loop here because continuing is useless.

                    break;
                }
                else
                {
                    i = number;
                }
            }

            return primeNumberValidity;
        }

args is not used, so you can omit it.

        static void Main()
        {
            Console.Write("Hello, please enter a number to find the biggest prime factor of it: ");

            var userInput = Convert.ToInt64(Console.ReadLine());

            var answer = FindFactor(userInput);
            Console.WriteLine(answer);
            Console.ReadKey();
        }
    }
}

You can use LINQ to implement FindFactor in a functional fashion, rather than imperatively.

static int FindFactor(Int64 number) {
    return IEnumerable.Range(2, number - 2)
                      .Where(n => number % n == 0 && IsPrimeFactor(n))
                      .DefaultIfEmpty(0)
                      .Last();
}


Decomposing a matrix as a sum of two bitstrings

First thing I notice is that you initialize your lists to contain strings

array1 = [""] * len(M)
array2 = [""] * len(M)

but later assign integer values to these. This is misleading, and so I would instead do

array1 = [0] * len(M)
array2 = [0] * len(M)

This has the added benefit that we get one case for free- if we want the value to be zeros (value == 0) we don't have to do anything.

Another issue I see with these variables is that they are named arrayX, but are in fact lists. In general, its best to leave out the type in the variable name, and just provide a descriptive name for its purpose. I would name these something like bitsA and bitsB, which also follows suite in A/B vs 1/2 naming.

Moving into your for loop, I would first reorder these conditionals to check value in numerical order (zero case first, then one, etc.), however as mentioned before we can now skip case zero, and so with only two cases (one or two) its not as big of a deal in my opinion. There is still one problem though. In one conditional you check A>B and then in the other you check the exact opposite. This added redundancy is unnecessary, and if you later edit one to be A<B, neither case will cover A==B. For this reason, it should be removed.

Normally I argue for reducing nested if statements, but in this case it seems like a logical separation, since the two subcases for value == 1 really mirror each other and deserve distinguishing from the value == 2 case. I would rewrite the loop as:

for index, value in enumerate(M):
    if value == 2:
        array1[index] = 1
        array2[index] = 1
        A -= 1
        B -= 1

    elif value == 1:
        if A > B:
            array1[index] = 1
            array2[index] = 0
            A -= 1
        else:
            array1[index] = 0
            array2[index] = 1
            B -= 1

However, there is one other route we could take, which would remove the remaining repeated logic.

for index, value in enumerate(M):
    if value == 2 or value == 1 and A > B:
        array1[index] = 1
        A -= 1
    if value == 2 or value == 1 and A <= B:
        array2[index] = 1
        B -= 1

Note this does break that rule I just mentioned about not including both A > B and A <= B, because our if statements are independent- both can execute. It becomes a trade off of bug resistance vs reducing redundancy.


Check all permutations of generic type parameters

If this is a comment theme throughout your code I would say to make an IArgument interface that would manage your object parameters. But if this is the only time you are dealing with these obj parameters, I would say pass the arguments of IsSatisfied in as generic objects and let IsSatisfied worry about ordering the arguments by type:

    void IsSatisfied(params object[] objects)
    {
        // T1 obj1 = <find T1 in objects>;
        // T2 obj2 = <find T2 in objects>;
        // T3 obj3 = <find T3 in objects>;
        // continue with previous implementation of IsSatisfied
    }

You would need to deal with the case when more than one of the objs is the same type.

Overall I don't like how the Test() method smells. You're passing in obj1, obj2, obj3, and possibly even obj4, every time, but you may only use obj2. What are obj1 and obj3 in this case?


You might want to move out 1000, 3600, 24 to "constants" so that they're properly described. Something like:

var HOURS_A_DAY = 24;
var MILLISECONDS_A_SECOND = 1000;
var SECONDS_AN_HOUR = 3600;

In addition, you can name your variables properly so that they're easily understood. timeDiff could be timeDifference, diffDays could be daysDifference.

Creating date objects per iteration is costly. Try operating using milliseconds instead. Make data.date return milliseconds for date1. Then use Date.now() on date2. Also, if possible, avoid doing getTime() and getHours(). Try calculating them manually.

Next, Math.abs can be done using a bitwise operation. The same could be done for Math.ceil, just look around the internet. Here's an example of a bitwise Math.abs, where n is the number to absolutify.

return (n < 0) ? (~n + 1) : n

Bitwise isn't the solution to all math performance problems and highly depends on the JS engine running. Some bitwise operations are faster than their high-level counterparts, some the other way around. Highly depends on the combination, so try checking which ones work for you.

if (!week[day][hour]) week[day][hour] = 0;

Property access and condition evaluation can be costly. Move this out to the for-loop that builds your matrix. This has nothing to do with the data crunching. It's just initializing a hole in the array to 0.

Lastly, for a more beautiful code, let's make everything less redundant and more consistent-looking.

week[day][hour] += data.isNetAlive ? 1 : -5;


Speed up large calculation of intersections

I would use a somewhat different algorithm to accomplish this task, by instead making a dictionary of all trains that go to each stop, sorting those trains by arrival, and then taking all inorder pairs of trains at each stop.

The basic algorithm is like this:

from collections import defaultdict
from itertools import combinations

trains = {...}
departures = {...}

intersections = set()

stations = defaultdict(list)
for t, train in trains.items():
    for s in train:
        stations[s].append(t)

for station in stations.values():
    intersections.update(combinations(sorted(station, key=lambda t: departures[t]), 2))

(This version of the code vastly improved by @Veedrac)


I've started Tuples to LINQ and I'm not sure if it's an anti pattern

In general, I think this pattern makes sense, because it clearly expresses what the method returns and lets you easily do the computation you want to do with the result.

Some thoughts:

I don't see any reason why your extension methods should only work on a collection of Tuples and not on a single Tuple. Because of that, I would probably write extension methods that convert a single Tuple to IEnumerable:

public static IEnumerable<T> ToEnumerable<T>(this Tuple<T, T> tuple)
{
    yield return tuple.Item1;
    yield return tuple.Item2;
}

Since this would make the code you showed more complicated, it might make sense to also keep SelectMany(), but express it using ToEnumerable():

public static IEnumerable<T> SelectMany<T>(
    this IEnumerable<Tuple<T, T>> tuples)
{
    return tuples.SelectMany(t => t.ToEnumerable());
}

public static IEnumerable<T> SelectMany<T, U>(
    this IEnumerable<U> source, Func<U, Tuple<T, T>> selector)
{
    return source.SelectMany(x => selector(x).ToEnumerable());
}

It might make sense to create custom types that mean “pair (triple, …) of items of the same type”. As a side benefit, you wouldn't need custom ToEnumerable() or SelectMany() extensions, since those types could implement IEnumerable<T> directly.


One thing you can do is define an inductive predicate describing what it means for a number to be even. You could for instance write:

data isEven : ℕ → Set where
  ZisEven  : isEven 0
  SSisEven : {n : ℕ} → isEven n → isEven (2 + n)

You can then prove that your even function is sound: whenever it returns true (which is what Data.Bool's T means) then the input was indeed an even number.

even-sound : (n : ℕ) → T (even n) → isEven n
even-sound zero          prf = ZisEven
even-sound (suc zero)    ()
even-sound (suc (suc n)) prf = SSisEven (even-sound n prf)

Now, you can write the following test rather than your previous equality: even-sound generates the proof that isEven 12.

y : isEven 12
y = even-sound 12 _

If you want to be even more precise in the way you specify even, you can also prove that it is complete: whenever an input is even, it indeed returns true.

even-complete : (n : ℕ) → isEven n → T (even n)
even-complete 0             ZisEven        = _
even-complete (suc (suc n)) (SSisEven prf) = even-complete n prf

Now, the specification of Even-ness I have chosen is a bit arbitrary, you may want to prove that it is equivalent to other formulations.

First, we can describe evenness in terms of oddness and reciprocally using mutually defined predicates:

mutual

  data isOdd′ : ℕ → Set where
    OisOdd′    : isOdd′ 1
    SEvenIsOdd : {n : ℕ} → isEven′ n → isOdd′ (suc n)

  data isEven′ : ℕ → Set where
    ZisEven′   : isEven′ 0
    SOddIsEven : {n : ℕ} → isOdd′ n → isEven′ (suc n)

Or we can use divisibility by 2 as the fundamental notion:

open import Data.Nat.Divisibility
isEven′′ : (n : ℕ) → Set
isEven′′ n = 2 ∣ n

Edit: I have included the proofs that these definitions are equivalent in a gist in case you want to peek.


_pin overflow

You're relying on the caller to prevent a buffer overflow.  Consider if the calling code was this:

var sp = new StreamingPin("PinNumber");
var input = "p2gsvpinnumberPinNumbessfeER#VvsdinNumberPinNumberdfvlj4kF4wfV";
var lastIndex = -1;

while (!sp.IsCorrect)
{
    lastIndex++;
    Console.WriteLine(input[lastIndex]);
    sp.ProcessCharacter(input[lastIndex]);
}
sp.ProcessCharacter(input[lastIndex]);

Your ProcessCharacter method doesn't check that accessing _lastCorrect + 1 isn't going to overflow _pin.  This seems like the responsibility of the StreamingPin class.  I'd say, if they start adding letters after a valid pin, it should reset to not correct anymore (rather than throwing an exception that could give the caller information they might not know, such as how long the PIN is).

One way to achieve this would be to add a wrap to the increment:

var currentIndex = (_lastCorrect + 1) % _pin.Length;

readonly

Since you're not updating the pin after construction, I'd also consider making it readonly.


Thanks for sharing your code,

You should write code to adhere to the PEP8 standard, for the most part you've done that correctly, you have snake_case variable and function names, but you've put all your imports on one line.

import random, time, os

should become

import random
import time
import os

Give one line per import.

Your code lacks a main guard. If I was to import any functions or classes from your current code, your slot machine game would start! Generally you just want to wrap your main code in a conditional check that looks like

if __name__ == "__main__":
    main()

where main is a function that starts the execution of your program.

A pattern you use in your code is relying on a string value ("yes" or "no") to determine which function executes. This is standard enough and nothing really wrong with it, but I would recommend instead of checking for this string literals, to use boolean values instead which are taken from shorter helper functions. A quick example.

You offer the ability to save and load game state, this is a good thing. However, you save the state in a Human readable fashion - i.e. "Bob's Balance: 24". Unless you're relying on this format somewhere else or even just displaying it, you could just save a single number in a file. No need to record a name.

The main code does a lot, you could break this up into helper functions. Some possible function names that jump out are.

load_game, save_game, wants_to_save, wants_to_replay

A pattern you use in your code is relying on a string value ("yes" or "no") to determine which function executes. This is standard enough and nothing really wrong with it, but I would recommend instead of checking for this string literals, to use boolean values instead which are taken from shorter helper functions. A quick example.

Your current code for prompting for saving the game

    replay = input_verify("Do you want to play again?")
    if replay == "no":
        save = input_verify("Do you want to save your game?")
        if save == "yes":
            name = input("What is you name?")
            with open("One Armed Bandit.txt", "w") as f:
                f.write("{}'s Balance:\n{}".format(name, balance))
        break

I would create some helper functions that look like

def wants_to_replay():
    return input_verify("Do you want to play again?") == "yes"

def wants_to_save():
    return input_verify("Do you want to save your game?") == "yes"

def save():
    name = input("What is you name?")
    with open("One Armed Bandit.txt", "w") as f:
        f.write("{}'s Balance:\n{}".format(name, balance))

And then the code becomes

 if not wants_to_replay():
    if wants_to_save():
        name = input("What is you name?")
        with open("One Armed Bandit.txt", "w") as f:
           f.write("{}'s Balance:\n{}".format(name, balance))
    break

The presence of a global variable is a bit suspect. This usually indicates that maybe you should be using a class instead. Perhaps a SlotMachine object.

It might be nice to be able to instantiate a slot machine object and call a start method on it.

slot_machine = SlotMachine(saved_balance)
slot_machine.start()

this line,

a, b, c = [random.choice(weighted_fruits) for i in range(3)]

when there is an unused variable, you should indicate as such by naming it _
so this line becomes

a, b, c = [random.choice(weighted_fruits) for _ in range(3)]

But then a, b and c are not great variable names. They do have a small scope and are used immediately after, but there's no reason to not give them a better name.

Why not just call them all_fruit then in the next loop you can just use

for fruit in all_fruit:
    ...

In the next section you compare them to each other, you could simply then unpack these into variables like

fruit_a, fruit_b, fruit_c = all_fruit

I think single letter variable names (excluding _ and loop counters) should be avoided at all costs.

You currently have all the fruits values hard coded in, it might be worth while to extract that data into a file to make it easier to add/remove new ones. That's probably not a huge deal though and may have no real benefit.

I'm not sure if there's any cleaner way to achieve the sleeps without scattering the calls throughout the code. You could maybe use named constants for the duration, but there may not be much value in that either.

You have no docstrings in your code. The print statements act as a form of documentation however, and your variable names are (mostly) very good. But there may be some room for some useful comments.

Hopefully this review was useful for you!


Use of Regex Objects

If your intent is merely to validate that the input is a phone number or not, rather than trying to capture anything, you can use the static Regex.IsMatch function:

var isPhoneNumber = Regex.IsMatch(phoneNumber, "(1-)?\\p{N}{3}-\\p{N}{3}-\\p{N}{4}\\b");

That saves having to deal with creating a new object and deal with a MatchCollection

Regex Pattern

There are a few things I would change:

There is a simple character class for matching digits 0-9: \d.  \p{N} may be more appropriate if you need to match the entire Unicode number group rather than simple digits, but then I would not expect to see the explicit 1 at the start.
\b matches backspace characters, which seems a little odd in a phone number.  I would omit it.  If your phone number strings happen to have backspace characters at the end, it will still match, but I would not require it be there.
Given the backslashes necessary for character escapes, I would prefix your string with the @ symbol for readability

Also, as @miasbeck points out, if you want to match the entire input string, you need to prefix the pattern with ^ and suffix it with $.  They indicate that the match must start at the beginning and end of the string, respectively.

The end result would be something like the following:

@"^(1-)?\d{3}-\d{3}-\d{4}$"

Additionally, since there is no requirement that an area code is used for local numbers, so you may want to make it optional as well:

@"^((1-)?\d{3}-)?\d{3}-\d{4}$"

(note: The MSDN page on Regular Expression Language is always a good reference to check on when coming up with regex patterns in .NET code.)

String Formatting

Rather than concatenating the strings together, I would use a format string:

const string FMT = "{0} is {1} a valid US number.";

Additionally, instead of using "", I always tend towards string.Empty, as it is much more explicit about intent.  "" could easily be a mistake, but string.Empty is clear that you really do want an empty string.

You can then output your result as follows:

Console.WriteLine(FMT, phoneNumber, (isPhoneNumber ? string.Empty : "not "));

Bringing it Together

Taking all the suggestions together, you get the following:

static void Main(string[] args)
{
   const string FMT = "{0} is {1} a valid US number.";
   var phoneNumber= Console.ReadLine();
   var isPhoneNumber = Regex.IsMatch(phoneNumber, @"^((1-)?\d{3}-)?\d{3}-\d{4}$");
   Console.WriteLine(FMT, phoneNumber, (isPhoneNumber ? string.Empty : "not "));
}

(note: I switch back and forth between C# and F# , so I use var more often than most - feel free to keep the explicit type declarations if you really want)


I'll focus on the heart of your solution first, which is your readFile() method.  Here's how I would write it:

public static List<String> anagramsInFile(String word, File f)
        throws FileNotFoundException {
    char[] canonical = canonicalize(original);
    ArrayList<String> anagrams = new ArrayList<String>();
    try (Scanner s = new Scanner(f)) {
        while (s.hasNext()) {
            String candidate = s.next();
            if ( (candidate.length() == word.length()) &&
                 (Arrays.equals(canonical, canonicalize(candidate))) ) {
                anagrams.add(candidate);
            }
        }
    }
    return anagrams;
}

private static char[] canonicalize(String original) {
    char[] array = original.toCharArray();
    Arrays.sort(array);
    return array;
}

Points to note:

Naming is important.  handleFile() is vague; anagramsInFile() is meaningful.  Also, I find anag to be confusing parameter name.
Hard-coding the filename inside the method is a bad idea.
Unless the caller has a reason to require an ArrayList, your method signature should just commit to returning some kind of List.  Better yet, consider returning a Set.
You only have to canonicalize the original string once, not once per word in the file.
Canonicalization deserves a helper method, since you have to do it to both the original string and to the contents of the file.
Canonicalizing to a char[] avoids working character by character, since you can take advantage of String.toCharArray().  It's probably more efficient as well.
There's no point in maintaining k: it's just the size() of the returned list.  Maintaining k as a class variable is particularly egregious, as there's no reason for that count to be part of the state of the class.
If your blocks are so lengthy and deeply nested that you need comments on the closing braces, consider that a Bad Code Smell and address that issue instead.  (By the way, your braces were misaligned, which causes confusion that even your close-brace comments can't compensate for.)

Next, let's look at getOutPut().  Here's how I would write it:

private static String formatOutput(List<String> words) {
    StringBuilder out = new StringBuilder('[');
    int wordsPrinted = 0;
    Iterator<String> w = words.iterator();
    while (w.hasNext()) {
        out.append(w.next());
        if (w.hasNext()) {
           out.append((++wordsPrinted % 8 == 0) ? "\n" : ", ");
        }
    }
    return out.append(']').toString();
}

Points to note:

When concatenating so many strings, you really need to use a StringBuilder.  Repeated string + string would create a temporary string each time.
Your output is buggy: you drop every eighth word, replacing it with a newline.
Be careful with edge cases: your code crashes when words is empty.
Try to structure your code so it's obvious that certain properties hold true.  For example, in my solution, you can see that every word gets printed (because if .hasNext() is true, the next word will get appended).  You can also see that every word except the last word is followed by a delimiter (either newline or comma).
Avoid cryptically named variables like x and y.  In fact, you don't even use  y.
Naming your result wordz when there's another variable named words makes the code a pain to read.

Finally:

public static void main(String[] args) throws FileNotFoundException{
    String word = input("Enter the word or words you would like to be processed");
    List<String> anagrams = anagramsInFile(word, new File("words.txt"));
    display(String.format("The word %s has %d matches.  They are:\n\n%s",
                          word, anagrams.size(), formatOutput(anagrams)));
}

private static String input(String prompt) {
    return JOptionPane.showInputDialog(null, prompt);
}

private static void display(String message) {
    JOptionPane.showMessageDialog(null, message);
}

Observations:

I've decomposed the work differently.  There's input() and display(), which are analogous to each other, and encapsulate the Swing UI.  (If you ever want to convert the program to work in the text console, you just modify those functions.)
More importantly, you can now see at a glance what the whole program does just by looking at main().  Note that all string constants are there as well: the prompt, the filename, and the output.
Don't declare any more variables than you need to.  If possible, when you declare variables, assign them in the same statement.
Use String.format().


Sorting a list of names by greatest lexicographical order

You are using the sorted function's key parameter in a very odd way. As you have a function that takes a string and returns a value to sort on, you could implement this much more neatly as:

def answer(names):
    return sorted(names, key=value, reverse=True)

See this Python wiki article for more on sorting. In use:

>>> sorted(["annie", "bonnie", "liz"], key=value, reverse=True)
['bonnie', 'liz', 'annie']
>>> sorted(["abcdefg", "vi"], key=value, reverse=True)
['vi', 'abcdefg']

Note that this doesn't meet the requirements for breaking ties, though; as value('al') == value('cj') and sorted is stable, they stay in the input order:

>>> sorted(('cj', 'al'), key=value, reverse=True)
['cj', 'al']  # ok!
>>> sorted(('al', 'cj'), key=value, reverse=True)
['al', 'cj']  # oh...

Your current pre-sort fixes this, and could be incorporated here too:

>>> sorted(sorted(('al', 'cj'), reverse=True), key=value, reverse=True)
['cj', 'al']

To get around this without overcomplicating the code, value can return a tuple (score, string), so the string is used to break ties:

def value(name):
    """Evaluate and return value of the name."""
    return sum([ord(letter) - 96 for letter in name]), name
                                                   # ^ note

Now 'cj' is always preferred:

>>> sorted(('al', 'cj'), key=value, reverse=True)
['cj', 'al']
>>> sorted(('cj', 'al'), key=value, reverse=True)
['cj', 'al']

Beyond that, note that you should use docstrings rather than just comments:

def value(name):
    """Evaluate and return value of the name."""
    return sum([ord(letter) - 96 for letter in name])

Otherwise it looks pretty good: the code is well laid out, and follows the style guide. If anything, I'd say you have too many comments; they shouldn't explain things the reader could learn from the code, but why the code is written that way (if this is obvious, no comment is needed).


The docstring needs to be the first thing that appears in your function.

In functional programming, it is not proper to ever change the value of a variable.

If you know that you need integer division rather than floating-point division, use // instead of /.

A common idiom for recursion is

def recursive_function(input):
    if base_case_applies(input):
        return base_value
    else:
        return …(recursive_function(…))

Your recursive function would therefore be better expressed as:

def hailstone(n):
    """Print the terms of the 'hailstone sequence' from n to 1, and
    return the length of the sequence."""
    assert n > 0
    print(n)
    if n == 1:
        return 1
    elif n % 2 == 0:
        return 1 + hailstone(n // 2)
    else:
        return 1 + hailstone(3 * n + 1)

An even more functional programming approach, which admittedly looks horrible in Python and therefore isn't something I recommend, is to use a single expression for the recursion.

def hailstone(n):
    """Print the terms of the 'hailstone sequence' from n to 1, and
    return the length of the sequence."""
    assert n > 0
    print(n)
    return 1 if n == 1 else \
           1 + hailstone((n // 2) if n % 2 == 0 else (3 * n + 1))


Rest Controller in Spring Boot for Rock Paper Scissors REST API

You do not need ping within your controller. Check out Spring boot actuator
Each method having duplicated error handling. You can move it to @ControllerAdvice
Method names like playerPOST, playerDELETE aren't helpful. Consider this as a normal class and have normal method names like createPlayer, deletePlayer
You need not define produces attribute for each mapping, RestController defaults to JSON
Returning ok("") doesn't make sense. Should be either returning the player object, or use ResponseEntity.created() (201)
Why are you autowiring HttpServletRequest context?
Should use AllArgsConstructor - constructor based autowiring


Closest element in std map when discarding values

Consider replacing the while loop with two for loops that increment lower and upper iterators till they have a suitable value - and only afterwards setting closest - it might be doing more operations but it is more cache friendly.

Or simply create a new map2 with only valid values (simply filtrate unwanted).

Though, different situation have different preferred solutions - there is a trade-off which solution is better depending on number of undesirable values in map2 and relative sizes of map1 and map2.

I don't think that you can improve it beyond that, lest you have some extra knowledge you haven't shared. In certain cases, you might want to implement some completely different solutions that are not reliant on map.

About the code itself, there are just a few minor issues:

You don't take care of the case when lower_bound return an end, in this case program will crash or UB. Same problem inside the while scope.
There is no need to declare the four iterators outside the scope. Just declare them with auto when you instantiate them. Nobody cares to see std::map<int,float>::iterator.
replace setClosest with

  template<typename MapItr>
  auto getClosestMapIterator(const MapItr &upper, const MapItr & lower, int key ) -> const MapItr&
  {
     return abs( upper->first - key ) < abs( lower->first - key ) ? upper : lower;
  }

and adjust usage auto closest = getClosestMapIterator(upper, lower, key); Also you better use abs instead of fabs, as fabs is for floating point types, so you unnecessarily cast integers to double with fabs.
Inside while the line closest == upper ? upper++ : lower--; is confusing. Simply write if scope. Don't use the expression cond ? ret_on_true : ret_on_false for side-effects, only when you want to get the return value.


JavaScript function that composes other functions to create a master function

One little suggestion on your reduce version is to make the initial value not the first function's result. This way, if ever the arguments is empty functions[0](val) won't call a missing function and throw up. Also makes reduce call the callback for each item, which makes it less of a cognitive burden.

Also, arrow functions that have expressions as its body implicitly returns the expression's value. You can have something as tiny as the following:

const Compose = (...args) => v => args.reduce((p, c) => c(p), v)


Checking if a number is a palindrome and is incrementing

If we look at your four different functions, you should be able to see that two are simple and easy to read, this goes in line with Pythons ideas, "simple is better than complex" - zen of Python. This is good as it means that others can read and understand your code with ease. This is a benift to both you and others. It also means if you need to change the functionality slightly, it's simple and easy. With your optimised function they take time to read and understand. This is a trade off, either you have fast code that is hard to understand, or slow but easy to understand code. And so from a human point of view, we prefer slower code.

I guess you, like I, prefer fast code. This leads to problems. Say we are tasked to make a program, from day 1 we'll be using hard to read code to make something simple. This at first is ok, but then it effects the overall structure of the project. And slowly we've made a monster, a hard to read monster. This is why premature optimisations are bad.

Instead if you don't worry about speed from the beginning, then you don't fall into this problem. And when you need to increase the performance of your code you can then swap out the old slow code for new fast code, only when it's needed. Sure you have to write a function twice, but at least we don't need to write the entire program again.

There is also another benefit to using simple code. When you're implementing the optimisation you have a base function to time against. This is good as then you can easily tell if your code is an optimisation, or just added complexity. Your function for example show this, if you time them both against their 'simple' version, you'll find, you haven't optimised the code, you've only obfuscated the code.


Insert sort not working for large arrays

This would be a good opportunity to learn some performance features and limitations of the Java Virtual Machine, and the available libraries.

I want to take your code, go through a staged progression of optimizations that will improve the way the code performs, and, while it will not change the runtime 'complexity' \$O(n^2)\$ of the insertion sort, it will improve the actual runtime.

As Nick has suggested, your variable names are not very meaningful. He's suggested insertIndex and sortIndex. I can use those, they are good. So your code becomes:

public static void insertSort(int[] data) {
    int insertIndex, sortIndex;
    for (sortIndex = 1; sortIndex < data.length; sortIndex++) {
        int temp = data[sortIndex];
        insertIndex = sortIndex;
        while (insertIndex > 0 && data[insertIndex - 1] >= temp) {
            data[insertIndex] = data[insertIndex - 1];
            --insertIndex;
        }
        data[insertIndex] = temp;
    }
}

Now, I also changed arr to data, and I added space around the operators and between loop keywords and braces. But, the above, is really your code, reformatted.

After that neatening up, there is one more things that need to happen: Variable scopes. Variables should be as narrow as possible. The sortIndex should be declared as part of the for loop control system, and the insertIndex should be declared inside the loop.

This is the result:

public static void insertSort(int[] data) {

    for (int sortIndex = 1; sortIndex < data.length; sortIndex++) {
        int temp = data[sortIndex];
        int insertIndex = sortIndex;
        while (insertIndex > 0 && data[insertIndex - 1] >= temp) {
            data[insertIndex] = data[insertIndex - 1];
            --insertIndex;
        }
        data[insertIndex] = temp;
    }
}

Now, that's a good looking insertion sort.... but, can it go faster? Yes.

The insertion sort is basically an operation where you identify where the data belongs, and then you shift all the larger data to the right, and insert the value. The code above shifts the values one at a time. If you identify the position, and shift them all at once, using an optimized system, you get better results. First, an intermediate result:

public static void insertSort(int[] data) {

    for (int sortIndex = 1; sortIndex < data.length; sortIndex++) {
        int temp = data[sortIndex];
        int insertIndex = sortIndex;
        // find the insert point.
        while (insertIndex > 0 && data[insertIndex - 1] >= temp) {
            --insertIndex;
        }
        // shift the values
        for (int i = sortIndex; i > insertIndex; i--) {
            data[i] = data[i - 1];
        }
        data[insertIndex] = temp;
    }
}

Now, that may not look faster, and you're right, it is probably not faster, but, if we replace the second loop with:

public static void insertSort(int[] data) {

    for (int sortIndex = 1; sortIndex < data.length; sortIndex++) {
        int temp = data[sortIndex];
        int insertIndex = sortIndex;
        // find the insert point.
        while (insertIndex > 0 && data[insertIndex - 1] >= temp) {
            --insertIndex;
        }
        // shift the values
        System.arraycopy(data, insertIndex, data, insertIndex + 1, sortIndex - insertIndex);
        data[insertIndex] = temp;
    }
}

Now, we're cooking. How much, though?

Before we get there, I am going to suggest another change, which is to take the insides of the loop, and make it another function too, and also make some variables 'final', so the code looks like:

private static final void subInsertSort(final int[] data, final int sortIndex) {
    final int temp = data[sortIndex];
    int insertIndex = sortIndex - 1;
    while (insertIndex >= 0 && temp < data[insertIndex]) {
        insertIndex--;
    }
    insertIndex++;
    System.arraycopy(data, insertIndex, data, insertIndex + 1, sortIndex - insertIndex);
    data[insertIndex] = temp;
}

public static void insertSortSplit(final int[] data){

    for(int sortIndex = 1; sortIndex < data.length; sortIndex++) {
        subInsertSort(data, sortIndex);
    }
}

I know, you're going to ask "Why?"... let me get to that.

I took the liberty of writing a quick test, on my system, that looks like:

private static final long time(String name, Runnable torun) {
    long nanos = System.nanoTime();
    torun.run();
    long time = System.nanoTime() - nanos;
    System.out.printf("Ran %s in %.3fms%n", name, time / 1000000.0);
    return time;
}

Now, that's got some Java8 stuff in it, but, all it really does, is time how long it takes to run a function. If I have the following 3 functions though (all do the same thing, with different names:

public static void insertSortOrig(int[] data) {
    int insertIndex, sortIndex;
    for (sortIndex = 1; sortIndex < data.length; sortIndex++) {
        int temp = data[sortIndex];
        insertIndex = sortIndex;
        while (insertIndex > 0 && data[insertIndex - 1] >= temp) {
            data[insertIndex] = data[insertIndex - 1];
            --insertIndex;
        }
        data[insertIndex] = temp;
    }
}

public static void insertSortArrayCopy(int[] data) {

    for (int sortIndex = 1; sortIndex < data.length; sortIndex++) {
        int temp = data[sortIndex];
        int insertIndex = sortIndex;
        // find the insert point.
        while (insertIndex > 0 && data[insertIndex - 1] >= temp) {
            --insertIndex;
        }
        // shift the values
        System.arraycopy(data, insertIndex, data, insertIndex + 1, sortIndex - insertIndex);
        data[insertIndex] = temp;
    }
}

private static final void subInsertSort(final int[] data, final int sortIndex) {
    final int temp = data[sortIndex];
    int insertIndex = sortIndex - 1;
    while (insertIndex >= 0 && temp < data[insertIndex]) {
        insertIndex--;
    }
    insertIndex++;
    System.arraycopy(data, insertIndex, data, insertIndex + 1, sortIndex - insertIndex);
    data[insertIndex] = temp;
}

public static void insertSortSplit(final int[] data){

    for(int sortIndex = 1; sortIndex < data.length; sortIndex++) {
        subInsertSort(data, sortIndex);
    }
}

When I run the following test on those functions, I get interesting results:

private static void runAll(int size) {
    final int[] data = new int[size];
    final int[] rand = buildData(size);

    System.arraycopy(rand, 0, data, 0, data.length);
    time("ACopy " + size, () -> insertSortArrayCopy(data));
    checkData(data);

    System.arraycopy(rand, 0, data, 0, data.length);
    time("Split " + size, () -> insertSortSplit(data));
    checkData(data);

    System.arraycopy(rand, 0, data, 0, data.length);
    time("Orig " + size, () -> insertSortOrig(data));
    checkData(data);

}

The above test (I call it with 10000) runs the three functions, yours, the one with ArrayCopy, and the one that has the sort in two parts.

It does not matter what order I run them in, but they all take an array of 10,000 random values, and sort them. The Times I get are:

Ran ACopy 10000 in 16.418ms
Ran Split 10000 in 10.262ms
Ran Orig 10000 in 24.080ms

What is that saying? It's saying that I can make the code run more than twice as fast, without changing the algorithm.... Now, if you are running your code in about 10 hours for \$10^7\$ then my improvements will bring it to... maybe 3 hours? That's a big difference.... but, wait, there's more.

Why does the 'split' function work so much faster than the single function? The reason is that the inside function gets called 10,000 times, and the outside function gets called once. A method that is called once is not 'compiled' by the Java compiler, but is run in more of an interpreted fashion. By putting the inner code in a function, the compiler sees it running many times, and it 'hotspot' compiles it.... with lots of optimization.

We can use that to our advantage.... If I take lots of small datasets (say 1000  data sets between 1 and 1000 members large, and I run those functions for those data sets, and then I run it for bigger data sets, by the time the big data set is run, the methods are 'hot' and compiled, and the Java machine is said to be 'warmed up'. What sort of difference does it make?

Well, for each of the three sorts above, let's see, first, your code:

Ran TenK Cold in 24.166ms
Ran TenK Hot in 9.689ms
Ran Warmup in 103.707ms
Ran Sort 1 in 0.002ms
Ran Sort 10 in 0.000ms
Ran Sort 100 in 0.002ms
Ran Sort 1000 in 0.178ms
Ran Sort 10000 in 9.379ms
Ran Sort 100000 in 915.304ms

The first run, of 10000 values, took 24ms. After a warmup, it took 9.5ms... and as part of the increasing data sizes, it took 9.5ms again.

Using arraycopy, it becomes:

Ran TenK Cold in 16.085ms
Ran TenK Hot in 8.073ms
Ran Warmup in 93.131ms
Ran Sort 1 in 0.003ms
Ran Sort 10 in 0.001ms
Ran Sort 100 in 0.003ms
Ran Sort 1000 in 0.169ms
Ran Sort 10000 in 7.520ms
Ran Sort 100000 in 837.687ms

Even 'hot', that has saved about.... 20%.

Now, as the split function....:

Ran TenK Cold in 10.250ms
Ran TenK Hot in 5.979ms
Ran Warmup in 78.793ms
Ran Sort 1 in 0.002ms
Ran Sort 10 in 0.001ms
Ran Sort 100 in 0.003ms
Ran Sort 1000 in 0.081ms
Ran Sort 10000 in 6.224ms
Ran Sort 100000 in 698.875ms

Now it is down to about 6ms, so, that's 30% off the original.... that's 'hot'. The cold version is even better.

Now, what if I used the native sort in Java (Arrays.sort(int[]))?

Well, that's impressive:

Ran TenK Cold in 2.576ms
Ran TenK Hot in 2.013ms
Ran Warmup in 75.236ms
Ran Sort 1 in 0.003ms
Ran Sort 10 in 0.002ms
Ran Sort 100 in 0.019ms
Ran Sort 1000 in 0.155ms
Ran Sort 10000 in 0.500ms
Ran Sort 100000 in 4.884ms

See how much better it scales \$O(n \log{n})\$? When you have the appropriate algorithm for the job it makes a real difference.