Producing excerpts from a file at specified positions within long lines (>1 M characters) I'm trying to read a file containing over 10.000 lines and each line contains an ID for an individual and a very long string composed of more than 1 million characters (numbers as example: 2200500200...205 until 1 million characters long).

I need to go through this long string and retrieve a subset based on flag information formed by 0 or 1s coming from a string called flagArray01String in the sample code, so I'm checking one string of the same length and printing the subset in a file.

I'm in doubt if I should convert the long strings (string from file and string composed of flags) to char and perform the loop after that.  I'm looking forward to receiving any comments that could help me obtaining a better performance.

Should I load all the strings in memory for some individuals?  Are there any other performance tricks?

Example of a sample file:

ID1 2020205000200200202020
ID2 2020205000200200202020
ID3 2020205000200200202020

...
ID10000 2020205000200200202020

Example of flag:
flag = 1000001111111111111100, where all the positions with 1´s should be retrieved and printed in a separated file.

if (IN_file.is_open())
{
    getline(IN_file, headerFileOriginal);
    while (IN_file >> idOriginal >> longString)
    {
        sizeTemp = longString.size();
        if (sizeTemp > 0)
            numbIdsOriginal++;
        for (int p = 0; p < flagArray01String.size(); p++)
        {
            if ((flagArray01String[p] == '1'))
            {
                subsetString = subsetString + longString[p];
            }
        }
        OUT_FILE << idOriginal << '\t' << subsetString << endl;
        subsetString = "";
    }
}
else
{
    myfileLog << "Problems to open file " << IN_file_StringName << endl;
    cout << "Problems to open file " << IN_file_StringName << endl;
}

IN_file.close();
OUT_FILE.flush();
OUT_FILE.close();

I see some things that may help you improve your program.

Don't abuse using namespace std

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.  It's not explicitly in your question, but the lack of std:: in the code leads me to suspect it.

Use consistent variable naming

We have variables named OUT_FILE, subsetString, IN_file_StringName with no discernable consistent style.  Pick a form, say, camel case such as subsetString and use it consistently.  Also, all caps names such as OUT_FILE should only be used for macros (and you probably don't really need macros much with modern C++).

Provide a complete interface

This is partly to do with getting good reviews, but also has a lot to do with good design.  The code as posted leaves a lot out.  It's not a complete function, and so reviewers have to guess as to what the missing variables are.  So in attempting to turn the code into a function which could be tested, I encapsulated it like this:

int original(std::ifstream &IN_file, std::ofstream &OUT_FILE,
            const std::string &IN_file_StringName, const std::string &flagArray01String)
{
    std::string headerFileOriginal;
    std::string idOriginal;
    std::string longString;
    int numbIdsOriginal{0};
    std::string subsetString;

    // original code here

    return numbIdsOriginal;
}

I actually deleted the line containing myfileLog for reasons I'll describe later.  Note that there are a number of variables passed in and a number which can be local to the function.

Don't store what you don't need

Because we don't have the full context, it's not clear if headerFileOriginal is ever used or is just discarded.  If the latter, I'd suggest this instead:

IN_file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Rethink error handling

As the code is currently written, the processing is done within a loop inside an is_open() call.  I'd refactor that to pull the processing into a separate function and write it like this:

std::ifstream in{inputFileName};
if (!in) {
    // log errors and quit
}
processMask(in, out, maskString);

All opening/closing/flushing should be done outside the function that does the line processing.

Prefer std::istream to std::ifstream

One could concievably get input from any kind of input stream and not just a file.  The same is true of output, so for that reason and to preserve maximum flexibility in the interface, I'd write the function with a prototype like this:

int processMask(std::istream &in, std::ostream &out, const std:string &maskString);

This allows for flexibility in input/output and also, by declaring the maskString asconst` makes clear that it will not be altered by the function.

Use a standard function

The standard template library has a lot of handy stuff in it, much of which is highly optimized for performance.  For that reason, it's often better to use an appropriate STL function rather than writing your own.  In this case, I'd suggest using std::copy_if.  Here's how a loop might be implemented using this:

while (in >> id >> longString)
{
    out << id << '\t';
    ++count;
    auto it{flagstring.cbegin()};
    auto end{flagstring.cend()};
    std::copy_if(longString.cbegin(), longString.cend(),
        std::ostream_iterator<char>(out),
        [&it,&end](const char){
            return it != end && *it++ == '1';
        });
    out << '\n';
}

This works by using a lambda as a predicate function.  It has the effect of copying each character from the input to the output as long as the condition is true, and the condition simply iterates through the flag string.

Don't use std::endl if you don't really need it

The difference between std::endl and '\n' is that '\n' just emits a newline character, while std::endl actually flushes the stream.  This can be time-consuming in a program with a lot of I/O and is rarely actually needed.  It's best to only use std::endl when you have some good reason to flush the stream and it's not very often needed for simple programs such as this one.  Avoiding the habit of using std::endl when '\n' will do will pay dividends in the future as you write more complex programs with more I/O and where performance needs to be maximized.

Don't explicitly flush the stream unless needed

When a std::ofstream iterator goes out of scope, the file is automatically flushed and closed by the destructor.  So for that reason, rather than explicitly calling flush() or close(), I'd recommend simply limiting the scope of the variable to only what is needed and rely on the destructor to do all of that.

Understand the limits of performance

I strongly suspect this program is I/O bound.  That is, I think it's very likely that almost all of the full duration required by this program is not in processing the data, but in simply reading and writing from files.  One simple test is to compare a simple line-at-a-time C++ copy function with the whole program.  The difference between the running of those two is likely to be an approximation of the maximum time you can trim by optimizing the algorithm.  I created a sample file with 10,000 data lines, each of which has a little over one million characters in the second field.  It takes about 1:03 (minutes:seconds) on my machine to process this 9.4 Gb file no matter what I do with for processing.


Implement a Shape abstract class To learn more about OOP, @nhgrif challenged me to implement a Shape abstract class (more details in code remarks below. Here is how I did it. Any and all advice appreciated!

Shape.java

/* nhgrif says:
 * Phrancis ready for inheritance/polymorphism?
 * Given the following abstract class:
 *
 *  public abstract class Shape {
 *      public abstract double area();
 *      public abstract double perimeter();
 *  }
 *
 * Implement a Circle, Triangle, and Rectangle class which extend the class Shape.
 * Ex: public class Circle extends Shape ... etc
 */

public abstract class Shape {
    public abstract double area();
    public abstract double perimeter();
}

Rectangle.java

public class Rectangle extends Shape {
    private final double width, length; //sides

    public Rectangle() {
        this(1,1);
    }
    public Rectangle(double width, double length) {
        this.width = width;
        this.length = length;
    }

    @Override
    public double area() {
        // A = w * l
        return width * length;
    }

    @Override
    public double perimeter() {
        // P = 2(w + l)
        return 2 * (width + length);
    }

}

Circle.java

public class Circle extends Shape {
    private final double radius;
    final double pi = Math.PI;

    public Circle() {
        this(1);
    }
    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        // A = π r^2
        return pi * Math.pow(radius, 2);
    }

    public double perimeter() {
        // P = 2πr
        return 2 * pi * radius;
    }
}

Triangle.java

public class Triangle extends Shape {
    private final double a, b, c; // sides

    public Triangle() {
        this(1,1,1);
    }
    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public double area() {
        // Heron's formula:
        // A = SquareRoot(s * (s - a) * (s - b) * (s - c))
        // where s = (a + b + c) / 2, or 1/2 of the perimeter of the triangle
        double s = (a + b + c) / 2;
        return Math.sqrt(s * (s - a) * (s - b) * (s - c));
    }

    @Override
    public double perimeter() {
        // P = a + b + c
        return a + b + c;
    }
}

And this is what I used to test it all, which it all works as intended:

TestShape.java

public class TestShape {
    public static void main(String[] args) {

        // Rectangle test
        double width = 5, length = 7;
        Shape rectangle = new Rectangle(width, length);
        System.out.println("Rectangle width: " + width + " and length: " + length
                + "\nResulting area: " + rectangle.area()
                + "\nResulting perimeter: " + rectangle.perimeter() + "\n");

        // Circle test
        double radius = 5;
        Shape circle = new Circle(radius);
        System.out.println("Circle radius: " + radius
            + "\nResulting Area: " + circle.area()
            + "\nResulting Perimeter: " + circle.perimeter() + "\n");

        // Triangle test
        double a = 5, b = 3, c = 4;
        Shape triangle = new Triangle(a,b,c);
        System.out.println("Triangle sides lengths: " + a + ", " + b + ", " + c
                + "\nResulting Area: " + triangle.area()
                + "\nResulting Perimeter: " + triangle.perimeter() + "\n");
    }
}

I am generally impressed with the consistency of the implementations, neatness, etc.

I have a comment about the basic premise. The Shape should not be an abstract class, but an interface. It has no concrete implementations for any methods, and making it an abstract class makes it hard to inherit from other places too. Consider this instead:

public interface Shape {
    public double area();
    public double perimeter();
}

In addition to that concern, the following are also things you should consider:

The pi field on your Circle class should be private. There's no need to re-expose an already public constant in another way.
I would avoid the 'unit' default constructors. They don't help with anything. When would someone want to call new Triangle() and not want to have the dimensions?
Your classes are missing a toString() method. These are useful for many reasons, especially debugging.
You don't have a plan for out-of-bound dimensions. What will you do with negative, NaN, or infinite input?


Beginner's Number Guessing Game I've started learning to program, and as the first program I wrote on my own I wanted to come up with a number guessing game - the below is what I got.
I'm mainly looking for a review of my Guess function, but I do have a few specific questions:

Does it make sense to have my loop variable as a function parameter?
I'm currently defining some variables (e.g. tries) outside of my function - is this an idiomatic way to do it?

import random
tries = [1]
def Guess(playing):
    number = random.randint(1,100)
    print(number)
    print("We are going to play high and low, the avabile numbers are from 1 to 100 included.")
    while playing == True:
        user_input = input("What is your number? ")
        if int(user_input) == number:
            print("You have won!")
            playing = False
        elif int(user_input) < number:
            print("You need to give a higher number")
            tries.append(1)
        elif int(user_input) > number:
            print("You need to give an lower number")
            tries.append(1)
        else:
            print("You have put an wrong number")
            playing = False
    print("It took you " + str(sum(tries)) + " turns to guess")
    still = input("Do you want to play again?")
    if still == "yes" or still == "YES" or still == "y" or still == "si":
        Guess(playing=True)
    else:
        playing=False

Guess(playing=True)

Overall you've done a good job with your code, there are a couple things I'd look out for :

You aren't paying for whitespace! What I mean is that it's important to let your
code breathe. Use some empty lines, it'll do wonder for your eyes when dealing with big chunks of code.
Why is tries an array? In my opinion, tries should represent the number of tries, so it should be a number!
Notice that you have for conditions, ==, <, > and... ? Say I ask you to give me a number that is neither equal, smaller or greater than 3, is there a possible option? What you should do instead is make sure the player input a number! Right now, if I was to answer "I don't know my numbers I'm just a baby" to "We are going to play high and low, the avabile numbers are from 1 to 100 included.", your program will crash! You should look into try/catch blocks to solve this problem, that could be a next thing to learn for you!
Say I need three tries on my first playthrough, then I start a new game (using "yes" when prompted) and I'm able to succeed on the first try, your code will tell me I've needed four tries to succeed. Can you find out why and how you can fix that?
You could simplify your loop using the break keyword, that's also something you should search for (while we're at it, try to understand the usage of break, return and continue. Those three keywords are pretty useful, although you probably already know about return).
What if I'm an english major and I can't stand making grammar mistakes (It's obviously not my case) and I write "Yes", instead of "yes" or "YES", when asked if I want to play again? What you could do is compare the lowered (or uppered) version of the input with what you want to check (ex. still.lower() == "yes").


How can I apply an Optional in which I want to call a method? I have this piece of code:
final ResultDO response = transformJsonToResponseDO(rawDataResponse, ResultDO.class);

return Optional.ofNullable(response)
            .map(results -> createPOIs(Collections.singletonList(response), providerResponse))
            .orElse(Collections.emptyList());

The important part is that: response is an object of type ResultDO. Within an Optional I want to call that method createPois(). Methot createPois wants a list of ResultDO, but there I will always have it only element because of that I wrapped in a Collections.singletonList().
As I wrote it, is it the right way, with that .map() ?
I don't want to use if(response != null) ...
Maybe it is worth to mention that createPois() return a list of some other types. A list of Points.

This looks mostly correct. IMO, should not use response in the map function, but results. I.e.
final ResultDO response = transformJsonToResponseDO(rawDataResponse, ResultDO.class);

return Optional.ofNullable(response)
            .map(results -> createPOIs(Collections.singletonList(results), providerResponse))
            .orElse(Collections.emptyList());

Even though they are the same in this case, typically this is how you are supposed to use Optional when you're not wrapping in #ofNullable.
Furthermore, this lets you put it all on one line:
return Optional.ofNullable(transformJsonToResponseDO(rawDataResponse, ResultDO.class))
            .map(results -> createPOIs(Collections.singletonList(results), providerResponse))
            .orElse(Collections.emptyList());


Finding the date 18 years ago Here's what I have:

require('../../src/utils/date-object-to-yyyymmdd');
var eighteenYearsAgo = new Date();
eighteenYearsAgo.setTime(eighteenYearsAgo.valueOf() - 18 * 365 * 24 * 60 * 60 * 1000);
eighteenYearsAgo = eighteenYearsAgo.yyyymmdd();

Basically, I am generate a date, setting it to 18 years ago (from today), and then converting it to yyyy-mm-dd format via an external function. I feel like this is quite verbose as is, but can't quite wrap my head around how to do it in a shorter way.

Why don't you use the setFullYear() method?

eighteenYearsAgo = eighteenYearsAgo.setFullYear(eighteenYearsAgo.getFullYear()-18);

Instead of using the yyymmdd() function, you can also use the Date.toISOString() method. This will also show the time... but you can get rid of it in several ways (split on "T" or use a regular expression).


Creating a concurrent/parallel ForEachAsync that can fail immediately or wait until enumeration is complete I needed an asynchronous parallel ForEach mechanism so I could enumerate an enumerable by N degrees of concurrency and process an action for each item.  Searching around, my code was modeled/merged from a few posts I found.

The tests I have been running are:

Run body() to completion
Inside the body(), do a CancellationTokenSource.Cancel()
Inside the body(), throw a OperationCancelledException()
Inside the body(), throw a NotSupportedException()

First, this points to a Stephen Toub post here.  I came up with combination of Toub's methods to have maxDegreeOfConcurrency and exception options (following bits from here)..

public static async Task ForEachAsync<T>( this IEnumerable<T> source, Func<T, Task> body, AsyncParallelOptions parallelOptions )
{
    ConcurrentBag<Exception> exceptions = null;

    var maxDegreeOfConcurrency = parallelOptions.MaxDegreeOfParallelism;

    // If they pass in a CancellationToken from caller of ForEachAsync need to create linked token source in case caller cancels, I want
    // ForEachAsync to cancel as well.  If they want to failImmediately, make a new CancellationTokenSource so I can stop processing partitions
    var cts = parallelOptions.CancellationToken != null ? CancellationTokenSource.CreateLinkedTokenSource( parallelOptions.CancellationToken ) :
              parallelOptions.FailImmediately ? new CancellationTokenSource() : null;

    var allDone = Task.WhenAll(
        from partition in Partitioner.Create( source ).GetPartitions( maxDegreeOfConcurrency )
        select Task.Run( async delegate
        {
            using ( partition )
            {
                while ( ( cts == null || !cts.IsCancellationRequested /* either from caller or failImmediately */ ) && partition.MoveNext() )
                {
                    await body( partition.Current )
                    .ContinueWith( t =>
                        {
                            // If body() threw an error, cancel if a CancellationTokenSource is present.
                            if ( t.IsFaulted )
                            {
                                if ( parallelOptions.FailImmediately )
                                {
                                    cts.Cancel();
                                }

                                // Always gather the exception to throw at the end
                                if ( exceptions == null ) exceptions = new ConcurrentBag<Exception>();

                                foreach ( var ex in t.Exception.InnerExceptions )
                                {
                                    exceptions.Add( ex );
                                }
                            }
                        }
                );
                }
            }
        }, cts.Token ) );

    // Wait until all finished (or errored out) and then return exceptions
    await allDone;

    // Question: allDone is never going to have IsCanceled or IsFaulted correct?  because await body() will swallow all exceptions?
    Console.WriteLine($"ForEachAsync Extension: Check Exceptions, {exceptions?.Count ?? 0} total, allDone.IsCanceled: {allDone.IsCanceled}, allDone.IsFaulted: {allDone.IsFaulted}, cts.IsCancellationRequested: {cts.IsCancellationRequested}" );

    if ( exceptions != null )
    {
        throw new AggregateException( exceptions );
    }
    else if ( cts.IsCancellationRequested )
    {
        throw new OperationCanceledException();
    }
}

CancaellationToken is a struct, not a class, so it can never be null. When people don't want to cancel stuff they just pass CancellationToken.None.
However note that If you change your cts assignment logic to something like parallelOptions.CancellationToken != CancellationToken.None you could hit a NullReferenceException when the cancellation token is None and FailImmediately is false. One simple way to make things work would be to just always assign cts = CancellationTokenSource.CreateLinkedTokenSource(parallelOptions.CancellationToken). Then you won't need the cts nullity check in the loop as well.
Instead of checking for IsCancellationRequested directly, it's a better practice to call cts.Token.ThrowIfCancellationRequested() - that way you will actually mark the task as canceled.
Instead of checking t.IsFaulted, it's a better practice to check if t.Exception != null.
Instead of iterating over t.Exception.InnerExceptions, you should iterate over t.Exception.Flatten().InnerExceptions`.
Creating the ConcurrentBag inside the task like you're doing now is not thread safe. I suggest just creating it upon declaration (instead of assigning null like you do now).
allDone issues:

allDone could be faulted if one of the body methods throw synchronously.
allDone would have IsCanceled set if cancellation was initiated either by the caller or by the FailImmediately logic
In either case, you won't get to the following line as await allDone would throw.

You don't need else after you throw new AggregateException.
If all you do is re-throw the exceptions at the end of the method, you could just await the Task.WhenAll and get the same behavior for free (without the Console.WriteLine though). To make this work, you'll have to re-throw the exception instead of adding its inner exceptions to the concurrent bag.


Python Socket programming chat - getting a handle on PEP conventions A while back I wrote a socket programmed chat in Python. This question isn't really about the program specifically, moreso on conventions of Python and my style of coding. I've only been using Python for a couple of months and any pointers would be appreciated.

Here's some code I'm curious about:

# in case of inappropriate arguments, remind user how to execute
if len(sys.argv) != 3:
    print("\nExecute the script as below:")
    print(" $ ./sock_client.py <IP Address> <Port>")
    print("For example: `./sock_server.py 127.0.0.1 8008`.")
    print("Additionally, ensure the socket server is live beforehand.\n")
    sys.exit(1)

In all of my Python projects so far, which all are ran from a terminal, I have a block of code like this at the top to ensure number of arguments, and if not, remind the user on how to execute. I don't know if this is good convention or not.

def send_user(m, u):
    """
    Sends a message to a single client.

    Parameter:
    m -- the message to send
    u -- the client to send to
    """
    for c in CLIENT_LIST:
        # only specific client/user
        if CLIENT_LIST[c] == u:
            c.send(bytes(m, "utf8"))

Here's an example docstring. Were I to have a return statement, it would look more like this, though:

def function(param1, param2):
    """
    What function does in a couple words.
    More detail if needed.

    Parameter:
    param1 -- some parameter
    param2 -- another parameter

    Return:
    -- some value

What is considered ideal convention for docstrings?

Here's the full repo: link.

Instead of always manually parsing sys.argv, you can use the standard library tool argparse. It automatically enforces the right usage and prints a nicely formatted usage if not. For your given example it would look like this:

import argparse

def ip_address(ip):
    # TODO: implement checking if IP is valid
    return ip

def parse_args(args=None):
    parser = argparse.ArgumentParser(description="Tool for server client connection.",
                                     epilog="Ensure the socket server is live before using this tool.")
    parser.add_argument("ip", type=ip_address, help="The IP address of the server")
    parser.add_argument("port", type=int, help="The port where the server listens.")
    return parser.parse_args(args)

I put it into a function so you can test it without having to run the whole code, by passing a list of strings to the function. It also allows setting the type of arguments, which can be any callable (so in the above example you need to have e.g a function called ip_address that can check if the argument is a valid IP address). The help messages generated by this example are like this:

$ python3 /tmp/sock_client.py 127.0.0.1
usage: sock_client.py [-h] ip port
sock_client.py: error: the following arguments are required: port

$ python3 /tmp/sock_client.py -h
usage: sock_client.py [-h] ip port

Tool for server client connection.

positional arguments:
  ip          The IP address of the server
  port        The port where the server listens.

optional arguments:
  -h, --help  show this help message and exit

Ensure the socket server is live before using this tool.

You can also add optional arguments, arguments with multiple parameters, mutually exclusive arguments/argument groups. Check out the documentation for more examples.

Instead of iterating only over the keys of a dictionary, iterate over the keys and values at the same time if you need both of them. Also, don't use too short names. No matter how nice your docstring is, it is always better if your function signature already tells you everything you need to know.

def send_user(message, user):
    """
    Sends a message to a single client.

    Parameter:
    message -- the message to send
    user    -- the client to send to
    """
    for client, user_name in CLIENT_LIST.items():
        # only specific client/user
        if user_name == user:
            client.send(bytes(message, "utf8"))

Also, don't lie in your docstrings. This function sends a message to every client with the right name (i.e. value in the dictionary)! You can just break after you have found the user to ensure there is only one, or adapt the docstring to the reality of the code. In either case, add tests to ensure that your code does what it should.

Calling what appears to be a dictionary CLIENT_LIST is one of the reasons why you should not include the type in the variable. Here it is very misleading and probably wrong! Just call them CLIENTS or ALL_CLIENTS or even USER_NAMES.

There are two widely used docstring conventions (that I know of, i.e. in scientific computing). The first is the Google style and the other one the numpy style. Your code seems to be closer to the latter, but as long as you are consistent and include all necessary information you should be fine.


Replacing each string of one or more blanks by a single blank K&R 1.9 exercise:

  Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

Is this correct and proper code? Could I have put c and inspace on a single line?

#include <stdio.h>

int main(void)
{
    /*initializes c and inspace. */
    int c;
    int inspace;

    /*sets inspace = 0*/
    inspace = 0;

    while((c = getchar()) != EOF)
    {
        if(c == ' ')
        {
            if(inspace ==0)
            {
                inspace = 1;
                putchar(c);
            }
        }

        if(c != ' ')
        {
            inspace = 0;
            putchar(c);
        }
    }

    return 0;
}

I am using the latest Ubuntu distro 64 bit.

Looks fine to me.  But I would replace

if (inspace == 0)

with

if (!inspace)

for stylistic reasons. And

if (c != ' ')

with

else

because there is no need to compare c again: you already know it isn't equal to a space because of the first if statement.

Also the code inside {...} after main should be indented over.


Negative Lookbehind Regex I have the following code which attempts to match all strings like "*SOMESTRING" (which can include numeric values), but not "*SOMESTRING*". For this I am using a negative lookahead as follows;*SEX and *AN01ZORA should match, \*PCCL\* should not match.

string s = "   if 'L,....' MDC = '13' Then " +
           "  if 'B,960.' SEX NOT = '2' AND *SEX NOT = '3' Then " +
           " DRG = 960Z (UNGROUPABLE) " +
           "    GoTo MDC FldErr " +
           "Else if 'B,N01.' SRG IN TABLE(*AN01ZORA) Then " +
           "  if '.,N01.' *PCCL* > 2 Then ";
Regex rr = new Regex(@"(?i)(?!\*\w+\*)\*\w+");
MatchCollection mc = rr.Matches(s);
foreach (Match m in mc)
    m.ToString().Dump();

  Output:
  *SEX
  *AN01ZORA

This seems to produce the correct output, but feels nasty and not correct. Is this right and what could I do to make the Regex better?

Your regex is overly complicated, I must admit. The negative lookahead is going to do a lot of work to identify all the negative cases before even looking for (nearly) positive matches.

I think the trick you are missing is the word-boundary anchor. Consider the following regex:

\*\w+\b

This looks for an asterisk, followed by characters, and then a (zero length) word-boundary. Now, both *SOME* and *SOME match that, since the \b happens before the asterisk. The negative lookahead would be useful after the word-boundary. Consider the following:

\*\w+\b(?!\*)

Look for *SOME where the SOME is a complete word not followed by an asterisk.

Here's a little demonstration ....

Edit: Note, there is no reason to add the case-insensitive switch ((?i)) because your regular expression has no specific case-based characters.


NullGuard for throwing more informative exceptions This small utility class is my solution for a more convenient way for checking against null. I also wanted to have more informative NullReferenceExceptions but I didn't want to pollute my code with null checks everywhere.
static class NullGuard
{
    public static T Check<T>(Expression<Func<T>> expr) where T : class
    {
        T value = expr.Compile()();
        if (value == null)
        {
            var message = CreateMessage(expr.Body);
            throw new NullReferenceException(message);
        }
        return value;
    }

    private static string CreateMessage(Expression expr)
    {
        return CreateMessage(expr as MemberExpression) ?? CreateMessage(expr as MethodCallExpression);
    }

    private static string CreateMessage(MemberExpression expr)
    {
        if (expr == null)
        {
            return null;
        }

        string instanceName = null;

        var constantExpression = expr.Expression as ConstantExpression;
        if(constantExpression != null)
        {
            instanceName = "this";
        }

        var memberExpression = expr.Expression as MemberExpression;
        if (memberExpression != null)
        {
            instanceName = memberExpression.Member.Name;
        }

        return CreateMessage(expr.Member.Name, expr.Member.MemberType.ToString(), instanceName);
    }

    private static string CreateMessage(MethodCallExpression expr)
    {
        string instanceName = null;
        var newExpression = expr.Object as NewExpression;
        if (newExpression != null)
        {
            instanceName = "new " + newExpression.Constructor.DeclaringType.Name;
        }

        var constantExpression = expr.Object as ConstantExpression;
        if (constantExpression != null)
        {
            instanceName = "this";
        }

        var memberExpression = expr.Object as MemberExpression;
        if (memberExpression != null)
        {
            instanceName = memberExpression.Member.Name;
        }
        return expr == null ? null : CreateMessage(expr.Method.Name, expr.Method.MemberType.ToString(), instanceName);
    }

    private static string CreateMessage(string memberName, string memberType, string declaringType)
    {
        var message = string.Format(
            "{0}.{1} (Instance.{2})",
            declaringType,
            memberName,
            memberType);
        return message;
    }
}

Its usage should be just on single methods or properties that must not be null but if they are, I want to know exactly where the null occurred:
class Foo
{
    public string Bar { get; set; }
    public string Baz() { return null; }
}

void Main()
{
    string abc = null;
    var foo = new Foo();

    //NullGuard.Check(() => abc);
    //NullGuard.Check(() => foo.Bar);
    //NullGuard.Check(() => foo.Baz());
    //NullGuard.Check(() => new Foo().Baz());
    NullGuard.Check(() => qux());
}

string qux() { return null; }

Result:

foo.Bar (Instance.Method)
foo.Baz (Instance.Property)
this.qux (Instance.Property)

Using a NullReferenceException for this seems wrong since no null reference has been dereferenced - it's capturing expressions which return null when they are not expected to. An InvalidOperationException might fit better (since you are performing an operation on an object which is in an invalid state) or even your own InvalidNullReturnException.
You can greatly simplify your message generation with:

var message = expr.Body.ToString();

This dumps the passed in expression as a string and you don't have to try and handle all kinds different expressions yourself.
Overall I would probably opt for aspect oriented programming (AOP) instead of littering my code with these NullGuard calls. There are a whole number of AOP frameworks around which usually work by using attributes you can decorate your methods and properties with. Based on these attributes arbitrary code can be injected - in your case you could inject the null checking and throwing code. This seems a much less intrusive way of achieving this kind of functionality greatly enhancing readability.


Computing factorials I am computing factorials as part of teaching myself C++. I am looking for feedback on efficiency, style and any bad practices I may be using.

#include <iostream>
#include <climits>

using namespace std;

int main()
{
    unsigned int number{ 0 };
    unsigned long long factorial{ 0 };
    cout << "The factorial of a non-negative integer n is denoted as n! (n factorial)...\n" <<
        "Example: 5! = 5x4x3x2x1 = 120\n\n";

    cout << "Enter a non-negative integer and we will compute the factorial: ";
    cin >> number;
    while (cin.rdstate() || number < 0 || number > LLONG_MAX)
    {
        cin.clear();
        cin.ignore(LLONG_MAX, '\n');//flush input buffer
        cout << "The data entered was invalid. Try again?\n\n" << endl;
        cout << "Enter a non-negative integer and we will compute the factorial: ";
        cin >> number;
    }
    if (number == 0 || number == 1)//since 0! and 1! = 1 there is no need to do a calculation
    {
        cout << "!" << number << " = 1" << endl;
    }
    else
    {
        factorial = number * (number - 1);
        for (size_t i = 2; i < number; ++i)
        {
            factorial *= (number - i);
        }
        cout << number << "! = " << factorial << endl;
    }
}

You should separate the computation code from the input/output code. As you have written it, the factorial computation code will never be reusable in any other program. To be specific, you should define an unsigned long long factorial(int n) function.

You have a formatting bug in the special case:

!0 = 1

In fact, you should structure your code to avoid the need for special cases altogether.

unsigned long long factorial(int n) {
    unsigned long long result = 1;
    while (n > 1) {
        result *= n--;
    }
    return result;
}


The CRUD interface I created for my new app. Creates, updates, deletes, reads and lists Here is the Interface I am using in my working app.

<?php

declare(strict_types=1);

namespace midorikocak\nano;

use Exception;

interface CrudInterface
{

    /**
     * @param array $data Array data of an Item.
     * @return array Array data of an Item with id.
     * @throws Exception if the data does not pass validate
     */
    public function create(array $data): array;

    /**
     * @param string $id
     * @return array Returns the array of New Item.
     * @throws Exception if something bad happens
     */
    public function read($id): array;

    /**
     * @param string $id Can be an integer or a string
     * @param array $data Accepts Array data of an Item with or without id.
     * @return array Returns the array of Updated Item.
     * @throws Exception if something bad happens
     * @todo Is it ok to have id in array data?
     */
    public function update(string $id, array $data): array;

    /**
     * @param string $id Can be an integer or a string
     * @return void Returns nothing. Simple Command Query separation.
     * @throws Exception if id not found or something bad happens
     * @todo Should create different exceptions for negative outcomes
     */
    public function delete(string $id): void;

    /**
     * @return array[] Should return an array of arrays that contain Item Data.
     */
    public function list(): array;

    /**
     * We have to validate data in every operation that receives item array.
     * @param array $data Accepts Array data of an Item with or without id.
     * @todo How can we be sure that this runs in every method that acceps item data?
     * @todo Maybe an abstract class?
     *
     * @return bool
     */
    public function validateData(array $data): bool;
}

I have some questions.

In update method, is it ok to have id in array data?
Should I create different exceptions for negative outcomes? Instead of returning false or null, when something is wrong, I prefer to throw exceptions.
How can we be sure that In validateData method runs in every method that accepts item data? Maybe an abstract class?
Are there any violations or can it be some better?

Ad 1) it Is ok to pass id in the Array data of you want to change the id. If you were asking if you can omit the id argument. Then yes but you Will never be able to change id And honestly it becomes more verbose to use. And honestly it Is quite premature to expect this on this generic level.

Ad 2) if "wrong" means empty id or database unreachable and similar, then yes, throw (or pass up) exceptions. But if it means object with given id not found then return null, it is not exceptional to ask for non-existent id, it just leads to a different result. But when database is unreachable, you are unable to say wheteher it exists or not and if it does what are its data and so here exception is appropriate.

Although many will argue here that exceptions are better. I think that exceptions are there to halt program (and do cleanup and notify someone) when something went really bad, not to be used to control flow of your program.

Ad 3) You have (at least) 2 options here:

Dont make it part of the interface. Let implementations take care of this. Could be abstrakt but in generál i advise against abstract classes as they sooner or later start cousing troubles when an implementation wants something done slightly differently And nobody thought about it when the abstract class was designed.

Or,

if it is / has to be part of the interface than it might better return error messages/codes instead of just bool. And the caller should be responsible for calling it first, if He does not call IT than the interface Is being used incorrectly And anything can happen, but that would be a dev's Mistake.

To provide an analogy, it would be like calling Iterator::current() before making sure Iterator::valid() returned true -> undefined behaviour, anything can happen, exception thrown, or nothing, or random element returned, or anything else - simply whatever was the most convenient for that implementation, anyway it is not important because at that point the interface is being misused and the code calling it is wrong and need to be fixed.

Calling it from inside the other method (valid from current, or validateData from create/update) just to make sure that the first method returns true and throw exception otherwise is also wrong because it will get called twice (redundantly) for whoever used the interface correctly.

Although many (especialy PHP) devs may argue that it is better to always throw exception to make sure the dev notices this as soon as possible. I personaly think that you should just write it into the interface's doc comments to state very explicitly that calling create/update without validating the data first is undefined behaviour. How many consumer classes will there be for CrudInterface anyway? One? Two? You make sure that these are correct and then everyone is going to use them, right? So...


Parser for Facebook friend list I have written some code in python in combination with selenium to parse all the names from facebook friend list. It was hard to manage the pop up notification and the process of scrolling to the end of that page. However, my scraper can do that successfully. I tried to do the whole thing very carefully. There are always rooms for improvement, though. here is the working code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)

def Facebook_Crawler(driver):

    driver.get('https://www.facebook.com/login.php?login_attempt=1&lwv=111')
    driver.find_element_by_xpath('//*[@id="email"]').send_keys('SomeEmail')
    driver.find_element_by_xpath('//*[@id="pass"]').send_keys('Password')
    driver.find_element_by_xpath('//*[@id="loginbutton"]').click()
    time.sleep(5)

    driver.find_element_by_xpath("//a[@id='findFriendsNav']").click()
    time.sleep(5)

    num_Of_links = -1
    num = 0

    while num != num_Of_links:

        num_Of_links = num
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(8)

        items = [post.text for post in driver.find_elements_by_xpath("//div[contains(@class,'friendBrowserNameTitle')]/a")]
        num = len(items)

    for item in items:
        print(item)

if __name__ == '__main__':
    driver = webdriver.Chrome(chrome_options = chrome_options)
    try:
        Facebook_Crawler(driver)
    finally:
        driver.quit()

I would improve the following things:

implement a "crawler" class that will store the "driver" instance as a variable and will also have a better separation of concerns - a separate method to log in, a separate method to get friends etc
stop using time.sleep() for waits with selenium - hardcoded time delays are seriously unreliable - use Explicit Waits instead
stop using XPaths - they are the least readable and the least reliable - use the fastest "by id" or the more concise "by css selector" locators

Improved code:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class FacebookCrawler:
    LOGIN_URL = 'https://www.facebook.com/login.php?login_attempt=1&lwv=111'

    def __init__(self, login, password):
        chrome_options = webdriver.ChromeOptions()
        prefs = {"profile.default_content_setting_values.notifications": 2}
        chrome_options.add_experimental_option("prefs", prefs)

        self.driver = webdriver.Chrome(chrome_options=chrome_options)
        self.wait = WebDriverWait(self.driver, 10)

        self.login(login, password)

    def login(self, login, password):
        self.driver.get(self.LOGIN_URL)

        # wait for the login page to load
        self.wait.until(EC.visibility_of_element_located((By.ID, "email")))

        self.driver.find_element_by_id('email').send_keys(login)
        self.driver.find_element_by_id('pass').send_keys(password)
        self.driver.find_element_by_id('loginbutton').click()

        # wait for the main page to load
        self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a#findFriendsNav")))

    def _get_friends_list(self):
        return self.driver.find_elements_by_css_selector(".friendBrowserNameTitle > a")

    def get_friends(self):
        # navigate to "friends" page
        self.driver.find_element_by_css_selector("a#findFriendsNav").click()

        # continuous scroll until no more new friends loaded
        num_of_loaded_friends = len(self._get_friends_list())
        while True:
            self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            try:
                self.wait.until(lambda driver: len(self._get_friends_list()) > num_of_loaded_friends)
                num_of_loaded_friends = len(self._get_friends_list())
            except TimeoutException:
                break  # no more friends loaded

        return [friend.text for friend in self._get_friends_list()]

if __name__ == '__main__':
    crawler = FacebookCrawler(login='login', password='password')

    for friend in crawler.get_friends():
        print(friend)

(not tested)


Optimisation XML handling within vb.net application I need to optimize this snippet to be faster:

Dim lst = (From t In DocElet.ChildNodes Select ID = t.item("ID").outerxml).Distinct().ToList()
       Parallel.For(0, lst.Count, Sub(i)
         Dim P As XmlElement = GetElement(lst(i))
       Dim ls = (From t In DocElet.ChildNodes Where t.item("ID").innerText = P.InnerText Select t)
         Parallel.ForEach(ls, Sub(D)
         Dim verif_date As String = D.Item("DAD").InnerText
        Sej.ID = D.Item("ID").InnerText
         End Sub)
            End Sub)

This is the XML structure:

<?xml version="1.0" encoding="utf-8"?>
<PatientData>
<Sejour><ID></ID><DAD></DAD></Sejour>
  </PatientData>

I'm asking how I can optimize my code because it takes a lot of time (50 sec) in the case where the list contains 20000 elements.

The indentation makes your snippet quite hard to read - the VB.NET syntax for inline delegates is fairly bulky (at least compared to C#), I'd give it more air; being able to read the code is step one.

Dim lst = (From t In DocElet.ChildNodes
           Select ID = t.item("ID").outerxml).Distinct()
                                             .ToList()

Parallel.For(0, lst.Count,
         Sub(i)
             Dim P As XmlElement = GetElement(lst(i))
             Dim ls = (From t In DocElet.ChildNodes
                       Where t.item("ID").innerText = P.InnerText
                       Select t)

             Parallel.ForEach(ls,
                      Sub(D)
                          Dim verif_date As String = D.Item("DAD").InnerText
                          Sej.ID = D.Item("ID").InnerText
                      End Sub)
         End Sub)

Ok. So you're running a Parallel.For, and inside that, you're running a Parallel.ForEach. That's quite a lot of overhead just there, it's likely that you're shooting yourself in the foot here, I'd first remove the inner Parallel.ForEach:

Parallel.For(0, lst.Count,
         Sub(i)
             Dim P As XmlElement = GetElement(lst(i))
             Dim ls = (From t In DocElet.ChildNodes
                       Where t.item("ID").innerText = P.InnerText
                       Select t)

             For Each foo In ls
                 Dim verif_date As String = D.Item("DAD").InnerText
                 Sej.ID = D.Item("ID").InnerText
             Next
         End Sub)

What's the purpose of verif_date? If it's not used, remove it:

             For Each foo In ls
                 Sej.ID = D.Item("ID").InnerText
             Next

Now the outer loop is making yet another selection:

             Dim ls = (From t In DocElet.ChildNodes
                       Where t.item("ID").innerText = P.InnerText
                       Select t)

I'd try to flatten that. I'm not very familiar with the LINQ syntax in VB.NET, but what you want is a SelectMany, where you'd select the child nodes where "ID"'s inner text matches the element's inner text for the node you're iterating - in other words the ls query gets outside the outer loop and into the lst query, so you only need a single loop (which can probably run in parallel).

If the XML is massive, it could be worth trying some PLINQ:

Dim lst = (From t In DocElet.ChildNodes
           Select ID = t.item("ID").outerxml)
          .Distinct()
          .AsParallel()
          .SelectMany(...)
          .ToList()

Now you can iterate lst with Parallel.ForEach (time it with a normal For Each first) and do your thing.

As an aside, I have to say your naming is inconsistent and hard to follow: Sub(i) looks good (Sub(index) would be better), Sub(D) doesn't. I realize VB.NET isn't case-sensitive, but if you're going to have a convention to make function parameters camelCase, by all means stick to it. lst would probably be better off as elements or whatever makes sense - use real, readable words for your identifiers, not just arbitrary abbreviations.


Simple C++ program to check win in TicTacToe Given a string of size 9 that represents a tic tac toe board, determine if X or O has won, or if the board is in an invalid state. Any other character aside from x, X, o, O represents an empty spot on the board.

Input: 012345678

Board:

0 1 2
3 4 5
6 7 8

TicTacToe.cpp

#include "tictactoe.h"
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>

using std::string;
using std::vector;

const int kBoardSize = 9;
const int kBoardLength = std::sqrt(kBoardSize);

TicTacToeState CheckTicTacToeBoard(std::string board) {
  if (board.size() != kBoardSize) {
    return TicTacToeState::InvalidInput;
  }

  std::transform(board.begin(), board.end(), board.begin(), ::tolower);
  size_t numberOfO = std::count(board.begin(), board.end(), 'o');
  size_t numberOfX = std::count(board.begin(), board.end(), 'x');

  if (numberOfO > numberOfX || numberOfX > numberOfO + 1) {
    return TicTacToeState::UnreachableState;
  }

  bool xWon = winDetection(board, 'x');
  bool oWon = winDetection(board, 'o');

  if (xWon && oWon) {
    return TicTacToeState::UnreachableState;
  } else if (xWon) {
    return TicTacToeState::Xwins;
  } else if (oWon) {
    return TicTacToeState::Owins;
  }
  return TicTacToeState::NoWinner;
}

bool winDetection(string board, char marker) {
  bool rowWin = false, colWin = false, rightDiagWin = true, leftDiagWin = true;

  for (int i{0}, rightDiagIndex{0}, leftDiagIndex{kBoardLength - 1};
       i < kBoardLength; ++i, rightDiagIndex += (kBoardLength + 1),
                         leftDiagIndex += (kBoardLength - 1)) {
    bool row = true, col = true;

    int rowIndex = kBoardLength * i, colIndex = i;
    for (int j{0}; j < kBoardLength; ++j) {
      col &= (board[colIndex] == marker);
      row &= (board[rowIndex] == marker);

      rowIndex++;
      colIndex += kBoardLength;
    }

    colWin |= col;
    rowWin |= row;

    rightDiagWin &= board[rightDiagIndex] == marker;
    leftDiagWin &= board[leftDiagIndex] == marker;
  }

  return (rowWin || colWin || rightDiagWin || leftDiagWin);
}

TicTacToe.h

#pragma once

#include <string>

enum TicTacToeState { UnreachableState, Xwins, Owins, NoWinner, InvalidInput };

TicTacToeState CheckTicTacToeBoard(std::string board);

bool winDetection(std::string board, char marker);

Computing a square-root is unnecessary; further, it only works when kBoardSize is a perfect square. Prefer

const int kBoardLength = 3;
const int kBoardSize = kBoardLength * kBoardLength;

Your test for unreachability is not complete. You verify that O has made no more moves than X, but you really need to check that the winner made the last move. If X won, then should have numberOfX = numberOfO + 1; if O won then should have numberOfO = numberOfX. It should be clear how to add these checks to your CheckTicTacToeBoard method.
A more overarching tension in this code is: should the board size really be a constant? The code is written so abstractly as to support any board size. But then the board size is fixed to a constant. I would choose one of the following directions to proceed in:

Commit to a board size of 9. In this case, I would recommend hard-coding the win conditions in winDetection. There are only 8 ways to win, so the resulting code would be a lot cleaner.
Allow arbitrary board sizes. In this case, you should accept a boardLength argument. Further, you will have to refine the notion of an UnreachableState. For example, in 5-by-5 tic tac toe, the following state is unreachable, your code would return Xwins:

x x x x x
o . o . o
x x x x x
o . o . o
o o x o o


Multiple lines of code in if statement without curly braces I have used multiple lines of code in one if condition without using curly braces, which is executing properly. So what is the main necessity of using curly braces in if statements for multiple lines?

 if (ggcCompareGrid.TopLevelTable != null)
            foreach (GridRow row in ggcCompareGrid.TopLevelTable.Rows)
            {
                if (row.DisplayElement.ParentGroup != null)
                    if (row.DisplayElement.ParentGroup.Category != null)
                        if ((int)row.DisplayElement.ParentGroup.Category == key && row.Record == null)
                        {
                            row.DisplayElement.ParentGroup.IsExpanded = false;
                            break;
                        }
            }

The problem with massively nested if statements without the { } is that it is a fragile structure. It is easy to come in and add a new statement and not realize whether it is or is not in one of the ifs or not. It also makes else statements harder to follow as not everyone is completely familiar with the precedence rules for else statements.

It works, it's entirely valid code - but it is bad style.

In this particular case you don't even need the nested ifs:

if (ggcCompareGrid.TopLevelTable != null)
{
        foreach (GridRow row in ggcCompareGrid.TopLevelTable.Rows)
        {
            if (row.DisplayElement.ParentGroup != null &&
                  row.DisplayElement.ParentGroup.Category != null &&
                  (int)row.DisplayElement.ParentGroup.Category == key &&
                   row.Record == null)
            {
                  row.DisplayElement.ParentGroup.IsExpanded = false;
                  break;
            }
        }
  }

You could also consider in this case having a private boolean function that does the comparison:

 private boolean checkParentGroup(ParentGroup group, int key, Record record) {
      return ((group != null) && (group.Category != null) &&
                  ((int)group.Category == key) &&
                   (record == null))
 }

if (ggcCompareGrid.TopLevelTable != null)
{
        foreach (GridRow row in ggcCompareGrid.TopLevelTable.Rows)
        {
            if (checkParentGroup(row.DisplayElement.ParentGroup, key, row.Record)
            {
                  row.DisplayElement.ParentGroup.IsExpanded = false;
                  break;
            }
        }
  }

This makes every part of the code simpler and clearer.

Obviously the checkParentGroup method should have a better name though, describing what it is actually checking.


Collect distances from 1 point to all others in a graph Problem:
I have a weighted graph. I want to get distance from definite point to all other points in the graph.(and then get path to them)
I used modified dijkstra algorithm
So, here is code:
var get_path = function(graph, a) {
// declaration
let cache, i, v, queue, node, links, root,
    j, link, c, n, d, max, w, L = graph.length;

// initialization
cache = Array(L);
i = L;
while (--i >= 0) {
    v = graph[i];
    cache[v.id] = {
        id: v.id,
        distance: Infinity,
        links: v.links,
        prev: null,
    };
}
root = cache[a];
root.distance = 0;
queue = [root];

// processing
i = 0;
while (i < queue.length) {
    node = queue[i];
    links = node.links;
    j = links.length;
    while (--j >= 0) {
        link = links[j];
        c = cache[link.id];
        d = node.distance + link.weight;
        if (d < c.distance) {
            c.prev = node;
            c.distance = d;
            queue.push(c);
        }
    }
    i++;
}

return cache;
}

Graph format is:
graph = [
  {
    id: 1,
    links: [
      {
        id: 2,
        weight: 1,
      },
      {
        id: 3,
        weight: 1,
      },
    ],
  },
  {
    id: 2,
    links: [
      {
        id: 1,
        weight: 1,
      },
      {
        id: 4,
        weight: 2,
      }
    ]
  },
  {
    id: 3,
    links: [
      {
        id: 1,
        weight: 1,
      },
      {
        id: 4,
        weight: 3,
      }
    ]
  },
  {
    id: 4,
    links: [
      {
        id: 2,
        weight: 2,
      },
      {
        id: 1,
        weight: 1,
      },
      {
        id: 3,
        weight: 3,
      },
      {
        id: 5,
        weight: 1,
      }
    ]
  },
  {
    id: 5,
    links: [
      {
        id: 4,
        weight: 1,
      }
    ]
  }
]

Performance:
In my PC this algorithm works with graph of ~160 vertices and ~350 edges about 0.03-0.06ms. But I need faster!
You can measure performance on your PC here
Question:
How to make this code (function get_path()) faster? Is it possible on JavaScript? If I should change the format of graph to make algorithm faster it's not a problem.

Or I exhausted possibilities of JavaScript?

Interesting question;
Naming

The names are on the whole terrible, it really blocks the reading flow, the only good 1 letter variable is i
Further evidence of the bad naming is that I only realized that n, w, and max are never used because of jshint.com
JavaScript follows lowerCamelCase so get_path -> getPath
In fact, even getPath would be a bad name, because you are not getting a path, getPaths could be better (you are getting more than 1 ), but getDistances or even getDistancesFromNode seems best

Declarations

You are mixing var and let in your code, I would stick to let and const

You are not declaring res at all (now it's a global) because this
var s_t = performance.now()
res = get_path(graph, 157)

should be
var s_t = performance.now(),
res = get_path(graph, 157)

Performance
This is already quite impressive, the only thing I noticed is that you process child nodes even if their child count is one (meaning really zero, since that 1 node must be the parent).
I assume the while loops are meant to be the fastest approach, however it seems that the fastest approach is now the most readable approach.
If you were allowed to just use/modify graph instead of cache it would be faster, of course the code reviewer would have told you that's not clean ;)
Comments
Comments should be meaningful, these comments are rather obvious.
All in all, if I had to maintain this, then I would rewrite with proper variable names but otherwise this is perfectly maintainable.

function getDistancesFromNode(graph, origin) {

  let cache, i, j, queue, node, links, root,
      link, nextNode, distance, nodeCount = graph.length;

  cache = Array(nodeCount);
  i = nodeCount;
  while (--i >= 0) {
      node = graph[i];
      cache[node.id] = {
          id: node.id,
          distance: Infinity,
          links: node.links,
          prev: null,
      };
  }
  root = cache[origin];
  root.distance = 0;
  queue = [root];

  i = 0;
  while (i < queue.length) {
      node = queue[i];
      links = node.links;
      j = links.length;
      while (--j >= 0) {
          link = links[j];
          nextNode = cache[link.id];
          distance = node.distance + nextNode.weight;
          if (distance < nextNode.distance && nextNode.links.length > 1) {
              nextNode.prev = node;
              nextNode.distance = d;
              queue.push(nextNode);
          }
      }
      i++;
  }
  return cache
};

var graph = [{"id":0,"links":[{"id":6,"weight":70.71067811865476},{"id":1,"weight":810}]},
{"id":1,"links":[{"id":0,"weight":810},{"id":2,"weight":70.71067811865476}]},
{"id":2,"links":[{"id":1,"weight":70.71067811865476},{"id":3,"weight":1592}]},
{"id":3,"links":[{"id":2,"weight":1592},{"id":5,"weight":70.71067811865476},{"id":153,"weight":205.87711451966396}]},
{"id":4,"links":[{"id":7,"weight":70.71067811865476},{"id":112,"weight":512.6115351930363},{"id":153,"weight":737.0774717197235}]},
{"id":5,"links":[{"id":3,"weight":70.71067811865476},{"id":153,"weight":135.6583919878893}]},
{"id":6,"links":[{"id":0,"weight":70.71067811865476},{"id":94,"weight":310.40008748620136}]},
{"id":7,"links":[{"id":4,"weight":70.71067811865476},{"id":94,"weight":1281.6150329914337}]},
{"id":8,"links":[{"id":9,"weight":55.43314410935759},{"id":27,"weight":394.91497146070554},{"id":89,"weight":249.25217498817833},{"id":17,"weight":37.97639596422426}]},
{"id":9,"links":[{"id":8,"weight":55.43314410935759},{"id":10,"weight":62.68381161480822},{"id":85,"weight":257.37611801643203},{"id":119,"weight":357.92609495024175}]},
{"id":10,"links":[{"id":9,"weight":62.68381161480822},{"id":11,"weight":22.00603386355706},{"id":43,"weight":325.893754212365},{"id":95,"weight":145.6349677143284},{"id":114,"weight":495.27154468751036},{"id":157,"weight":475.0410693030716},{"id":159,"weight":424.5428223515612},{"id":160,"weight":386.9207320551183},{"id":161,"weight":293.9650424785912}]},
{"id":11,"links":[{"id":10,"weight":22.00603386355706},{"id":12,"weight":40.754770931103174},{"id":66,"weight":338.44828019294425},{"id":154,"weight":277.7768610143829}]},
{"id":12,"links":[{"id":11,"weight":40.754770931103174},{"id":13,"weight":63.96396957495827},{"id":71,"weight":279.46200199816974},{"id":130,"weight":423.73430144247175},{"id":142,"weight":605.3109163530141}]},
{"id":13,"links":[{"id":12,"weight":63.96396957495827},{"id":14,"weight":42.685329113484585},{"id":22,"weight":267.044681207588},{"id":18,"weight":383.79570105328406},{"id":89,"weight":295.1151050396651},{"id":97,"weight":173.37684490280637},{"id":147,"weight":644.3394809974541}]},
{"id":14,"links":[{"id":13,"weight":42.685329113484585},{"id":15,"weight":45.39123913699872}]},
{"id":15,"links":[{"id":14,"weight":45.39123913699872},{"id":16,"weight":47.047036449194316},{"id":65,"weight":294.0887879580657},{"id":86,"weight":183.6721765115165},{"id":114,"weight":486.6941103104043},{"id":159,"weight":416.3689141062264},{"id":160,"weight":344.37793610826577}]},
{"id":16,"links":[{"id":15,"weight":47.047036449194316},{"id":17,"weight":25.7772200449739},{"id":43,"weight":298.08763602419685},{"id":39,"weight":354.1679638306291},{"id":70,"weight":371.37278825824006},{"id":74,"weight":670.2294043204714},{"id":132,"weight":378.87860482952715},{"id":157,"weight":434.2784913076533},{"id":161,"weight":262.12088923847034}]},
{"id":17,"links":[{"id":16,"weight":25.7772200449739},{"id":8,"weight":37.97639596422426},{"id":22,"weight":283.4489103653117},{"id":136,"weight":499.55002623505305},{"id":142,"weight":589.0762111107545}]},
{"id":18,"links":[{"id":13,"weight":383.79570105328406},{"id":19,"weight":68.56451044560333},{"id":27,"weight":31.90572098820839}]},
{"id":19,"links":[{"id":18,"weight":68.56451044560333},{"id":20,"weight":60.809531997686655},{"id":64,"weight":400.06011580087164},{"id":156,"weight":445.50275071828963}]},
{"id":20,"links":[{"id":19,"weight":60.809531997686655},{"id":21,"weight":41.7268040869492}]},
{"id":21,"links":[{"id":20,"weight":41.7268040869492},{"id":22,"weight":44.409786926805985},{"id":70,"weight":244.19177296204867},{"id":91,"weight":205.16309784978526},{"id":131,"weight":170.98157093493768}]},
{"id":22,"links":[{"id":13,"weight":267.044681207588},{"id":17,"weight":283.4489103653117},{"id":21,"weight":44.409786926805985},{"id":23,"weight":73.41351083187409},{"id":44,"weight":578.7057992684693},{"id":39,"weight":603.7781336207288},{"id":49,"weight":794.3115697606781},{"id":57,"weight":486.1389713865941},{"id":86,"weight":298.95911490661615},{"id":128,"weight":342.80891355908545},{"id":135,"weight":282.6431518625895},{"id":142,"weight":308.1360659862511},{"id":147,"weight":378.58967001309463},{"id":161,"weight":491.24332514971724}]},
{"id":23,"links":[{"id":22,"weight":73.41351083187409},{"id":24,"weight":47.156569853258866},{"id":63,"weight":299.1373917294431},{"id":156,"weight":424.0775352517563}]},
{"id":24,"links":[{"id":23,"weight":47.156569853258866},{"id":25,"weight":86.13825660873893},{"id":131,"weight":210.320160984807}]},
{"id":25,"links":[{"id":24,"weight":86.13825660873893},{"id":26,"weight":33.06353502569152}]},
{"id":26,"links":[{"id":25,"weight":33.06353502569152},{"id":27,"weight":67.91460112401641},{"id":57,"weight":540.3387174980588},{"id":92,"weight":228.93825991593803},{"id":120,"weight":412.75883815805963},{"id":128,"weight":418.9168740031914},{"id":142,"weight":333.7521131385038}]},
{"id":27,"links":[{"id":8,"weight":394.91497146070554},{"id":26,"weight":67.91460112401641},{"id":18,"weight":31.90572098820839},{"id":80,"weight":593.5258503155924},{"id":89,"weight":148.5928396302128},{"id":147,"weight":349.297544643658}]},
{"id":28,"links":[{"id":29,"weight":59.67018420401978},{"id":81,"weight":209.96278270854182},{"id":106,"weight":69.99034725665763},{"id":149,"weight":340.8623984131601},{"id":37,"weight":28.746317165572204}]},
{"id":29,"links":[{"id":28,"weight":59.67018420401978},{"id":30,"weight":23.113480906778186},{"id":60,"weight":155.8179847580466},{"id":77,"weight":297.5407757529884}]},
{"id":30,"links":[{"id":29,"weight":23.113480906778186},{"id":31,"weight":24.194433832360634},{"id":48,"weight":312.87236629082633}]},
{"id":31,"links":[{"id":30,"weight":24.194433832360634},{"id":32,"weight":95.03245812683691},{"id":52,"weight":306.82477443979093},{"id":107,"weight":78.85870804148841}]},
{"id":32,"links":[{"id":31,"weight":95.03245812683691},{"id":33,"weight":28.769841069179858},{"id":60,"weight":147.93878749367386}]},
{"id":33,"links":[{"id":32,"weight":28.769841069179858},{"id":34,"weight":26.884913041414087},{"id":149,"weight":324.73490720293273}]},
{"id":34,"links":[{"id":33,"weight":26.884913041414087},{"id":35,"weight":34.539644671799884},{"id":48,"weight":312.60280041119495},{"id":82,"weight":298.7966839326061}]},
{"id":35,"links":[{"id":34,"weight":34.539644671799884},{"id":36,"weight":47.88309910969351},{"id":52,"weight":298.6492567682363},{"id":78,"weight":270.88254329096384}]},
{"id":36,"links":[{"id":35,"weight":47.88309910969351},{"id":37,"weight":55.92799176338086},{"id":146,"weight":250.2794421195491}]},
{"id":37,"links":[{"id":36,"weight":55.92799176338086},{"id":28,"weight":28.746317165572204},{"id":101,"weight":174.53904785374436}]},
{"id":38,"links":[{"id":45,"weight":41.60346490016386},{"id":160,"weight":259.95392603739697}]},
{"id":39,"links":[{"id":16,"weight":354.1679638306291},{"id":22,"weight":603.7781336207288},{"id":40,"weight":28.56292626279466},{"id":136,"weight":827.1618749297187},{"id":142,"weight":911.9072364060776}]},
{"id":40,"links":[{"id":39,"weight":28.56292626279466},{"id":41,"weight":71.83348433366919},{"id":87,"weight":385.18415791555594}]},
{"id":41,"links":[{"id":40,"weight":71.83348433366919},{"id":42,"weight":25.66448755744104},{"id":84,"weight":301.86449922516306},{"id":119,"weight":304.09006218105435},{"id":160,"weight":191.66415098549578},{"id":161,"weight":42.17582986887332}]},
{"id":42,"links":[{"id":41,"weight":25.66448755744104},{"id":43,"weight":25.041564347271354},{"id":114,"weight":201.32515950767336},{"id":159,"weight":136.50001088374017}]},
{"id":43,"links":[{"id":10,"weight":325.893754212365},{"id":16,"weight":298.08763602419685},{"id":42,"weight":25.041564347271354},{"id":44,"weight":53.81505716045718},{"id":70,"weight":668.6142302142265},{"id":95,"weight":464.5912380083568},{"id":132,"weight":675.2576499747647},{"id":157,"weight":149.3858968154142},{"id":161,"weight":35.96703115932087}]},
{"id":44,"links":[{"id":22,"weight":578.7057992684693},{"id":43,"weight":53.81505716045718},{"id":45,"weight":41.40801209082323},{"id":57,"weight":1063.3996891527247},{"id":136,"weight":809.6193736796965}]},
{"id":45,"links":[{"id":44,"weight":41.40801209082323},{"id":38,"weight":41.60346490016386},{"id":86,"weight":326.14140573310647},{"id":83,"weight":413.3998108385815},{"id":118,"weight":301.0257064648056}]},
{"id":46,"links":[{"id":47,"weight":32.324323407861954},{"id":54,"weight":25.030634943141447}]},
{"id":47,"links":[{"id":46,"weight":32.324323407861954},{"id":48,"weight":74.97098636842881},{"id":150,"weight":482.148526864346}]},
{"id":48,"links":[{"id":30,"weight":312.87236629082633},{"id":34,"weight":312.60280041119495},{"id":47,"weight":74.97098636842881},{"id":49,"weight":51.584467579702824},{"id":61,"weight":244.27972531448285},{"id":106,"weight":336.640487491796}]},
{"id":49,"links":[{"id":22,"weight":794.3115697606781},{"id":48,"weight":51.584467579702824},{"id":57,"weight":308.50551001966323},{"id":128,"weight":451.6465055929891},{"id":135,"weight":511.844500792933}]},
{"id":50,"links":[{"id":51,"weight":49.512416392803104}]},
{"id":51,"links":[{"id":50,"weight":49.512416392803104},{"id":52,"weight":53.04787162699196}]},
{"id":52,"links":[{"id":31,"weight":306.82477443979093},{"id":35,"weight":298.6492567682363},{"id":51,"weight":53.04787162699196},{"id":53,"weight":76.43056418497244},{"id":82,"weight":547.120073731121},{"id":110,"weight":325.05226324305437},{"id":150,"weight":458.3924820628751}]},
{"id":53,"links":[{"id":52,"weight":76.43056418497244},{"id":54,"weight":20.039265191224455}]},
{"id":54,"links":[{"id":53,"weight":20.039265191224455},{"id":46,"weight":25.030634943141447},{"id":58,"weight":244.9260216552532},{"id":67,"weight":756.3463895463947},{"id":76,"weight":482.3227102069046},{"id":124,"weight":422.2533744531271}]},
{"id":55,"links":[{"id":56,"weight":58.460583239231624},{"id":102,"weight":115.82406310298205},{"id":125,"weight":152.5815324765102}]},
{"id":56,"links":[{"id":55,"weight":58.460583239231624},{"id":57,"weight":55.57519143003837}]},
{"id":57,"links":[{"id":22,"weight":486.1389713865941},{"id":26,"weight":540.3387174980588},{"id":44,"weight":1063.3996891527247},{"id":49,"weight":308.50551001966323},{"id":56,"weight":55.57519143003837},{"id":58,"weight":48.45767081071381},{"id":158,"weight":180.39992938539922},{"id":136,"weight":253.7803363149803},{"id":142,"weight":206.67779462996376}]},
{"id":58,"links":[{"id":54,"weight":244.9260216552532},{"id":57,"weight":48.45767081071381},{"id":59,"weight":24.092997373752972}]},
{"id":59,"links":[{"id":58,"weight":24.092997373752972},{"id":60,"weight":82.39211060953677},{"id":76,"weight":268.66979964854914},{"id":124,"weight":204.95995317424766}]},
{"id":60,"links":[{"id":29,"weight":155.8179847580466},{"id":32,"weight":147.93878749367386},{"id":59,"weight":82.39211060953677},{"id":61,"weight":56.8386682833888}]},
{"id":61,"links":[{"id":48,"weight":244.27972531448285},{"id":60,"weight":56.8386682833888},{"id":77,"weight":395.3342593777817},{"id":106,"weight":94.82870098367673}]},
{"id":62,"links":[{"id":63,"weight":50.239660941572126},{"id":138,"weight":456.67384076187767},{"id":71,"weight":38.53358308507445}]},
{"id":63,"links":[{"id":23,"weight":299.1373917294431},{"id":62,"weight":50.239660941572126},{"id":64,"weight":49.29188012559735}]},
{"id":64,"links":[{"id":19,"weight":400.06011580087164},{"id":63,"weight":49.29188012559735},{"id":65,"weight":34.9390920450358},{"id":91,"weight":484.70495246456426}]},
{"id":65,"links":[{"id":15,"weight":294.0887879580657},{"id":64,"weight":34.9390920450358},{"id":66,"weight":44.386933593419634},{"id":86,"weight":473.4945906187595},{"id":160,"weight":638.3194987663459}]},
{"id":66,"links":[{"id":11,"weight":338.44828019294425},{"id":65,"weight":44.386933593419634},{"id":67,"weight":46.35007638082747},{"id":97,"weight":354.99096425656126},{"id":155,"weight":98.46201480548129}]},
{"id":67,"links":[{"id":54,"weight":756.3463895463947},{"id":66,"weight":46.35007638082747},{"id":68,"weight":83.11118143775956},{"id":76,"weight":275.22916188389814},{"id":124,"weight":334.3275404295602}]},
{"id":68,"links":[{"id":67,"weight":83.11118143775956},{"id":69,"weight":61.82288775247714}]},
{"id":69,"links":[{"id":68,"weight":61.82288775247714},{"id":70,"weight":32.72720780632854}]},
{"id":70,"links":[{"id":16,"weight":371.37278825824006},{"id":21,"weight":244.19177296204867},{"id":43,"weight":668.6142302142265},{"id":69,"weight":32.72720780632854},{"id":71,"weight":82.01328044192185},{"id":74,"weight":299.3033974268629},{"id":86,"weight":467.8800531253375},{"id":114,"weight":795.7351820979673},{"id":131,"weight":74.14997079035552},{"id":157,"weight":798.2577975693512},{"id":159,"weight":728.6111329180097},{"id":161,"weight":632.6993752105095}]},
{"id":71,"links":[{"id":12,"weight":279.46200199816974},{"id":70,"weight":82.01328044192185},{"id":62,"weight":38.53358308507445},{"id":97,"weight":376.4367138297526},{"id":122,"weight":299.55665657848795},{"id":132,"weight":92.36209904758263},{"id":131,"weight":67.09223542194184},{"id":156,"weight":229.10251215735252}]},
{"id":72,"links":[{"id":73,"weight":22.44295867376786},{"id":124,"weight":96.7927637387651},{"id":76,"weight":30.383770443458943}]},
{"id":73,"links":[{"id":72,"weight":22.44295867376786}]},
{"id":74,"links":[{"id":16,"weight":670.2294043204714},{"id":70,"weight":299.3033974268629},{"id":75,"weight":41.39428112635902},{"id":121,"weight":140.7096972711017},{"id":132,"weight":291.38771611392053}]},
{"id":75,"links":[{"id":74,"weight":41.39428112635902},{"id":76,"weight":30.0627689482037}]},
{"id":76,"links":[{"id":54,"weight":482.3227102069046},{"id":59,"weight":268.66979964854914},{"id":67,"weight":275.22916188389814},{"id":75,"weight":30.0627689482037},{"id":72,"weight":30.383770443458943}]},
{"id":77,"links":[{"id":29,"weight":297.5407757529884},{"id":61,"weight":395.3342593777817},{"id":78,"weight":52.00968489404878},{"id":106,"weight":300.6735447670068},{"id":145,"weight":213.64883986492893}]},
{"id":78,"links":[{"id":35,"weight":270.88254329096384},{"id":77,"weight":52.00968489404878},{"id":79,"weight":44.23783407847146}]},
{"id":79,"links":[{"id":78,"weight":44.23783407847146},{"id":80,"weight":38.118361538425795}]},
{"id":80,"links":[{"id":27,"weight":593.5258503155924},{"id":79,"weight":38.118361538425795},{"id":81,"weight":88.22727131494065},{"id":92,"weight":748.610228678086},{"id":117,"weight":922.5836952812741},{"id":160,"weight":981.0925983047125}]},
{"id":81,"links":[{"id":28,"weight":209.96278270854182},{"id":80,"weight":88.22727131494065},{"id":82,"weight":65.51209274182104},{"id":101,"weight":323.12731048143235},{"id":147,"weight":350.10072558836436}]},
{"id":82,"links":[{"id":34,"weight":298.7966839326061},{"id":52,"weight":547.120073731121},{"id":81,"weight":65.51209274182104},{"id":111,"weight":269.9434631344396}]},
{"id":83,"links":[{"id":45,"weight":413.3998108385815},{"id":84,"weight":49.04899812555336},{"id":92,"weight":30.837464050468025}]},
{"id":84,"links":[{"id":41,"weight":301.86449922516306},{"id":83,"weight":49.04899812555336},{"id":85,"weight":63.71188304619065},{"id":161,"weight":262.445211274406}]},
{"id":85,"links":[{"id":9,"weight":257.37611801643203},{"id":84,"weight":63.71188304619065},{"id":86,"weight":49.09351203637654}]},
{"id":86,"links":[{"id":15,"weight":183.6721765115165},{"id":22,"weight":298.95911490661615},{"id":45,"weight":326.14140573310647},{"id":65,"weight":473.4945906187595},{"id":70,"weight":467.8800531253375},{"id":85,"weight":49.09351203637654},{"id":87,"weight":83.46480593410709},{"id":119,"weight":151.90228469087668},{"id":114,"weight":329.0079671927419},{"id":128,"weight":641.7308695509025},{"id":132,"weight":471.07620316344014},{"id":135,"weight":581.5450583764579},{"id":157,"weight":337.27659265236},{"id":159,"weight":264.5824947674739},{"id":160,"weight":170.91599639926625}]},
{"id":87,"links":[{"id":40,"weight":385.18415791555594},{"id":86,"weight":83.46480593410709},{"id":88,"weight":41.619088536378854},{"id":161,"weight":285.65519727136444}]},
{"id":88,"links":[{"id":87,"weight":41.619088536378854},{"id":89,"weight":19.022840656019405}]},
{"id":89,"links":[{"id":8,"weight":249.25217498817833},{"id":13,"weight":295.1151050396651},{"id":27,"weight":148.5928396302128},{"id":88,"weight":19.022840656019405},{"id":90,"weight":35.841603038774416}]},
{"id":90,"links":[{"id":89,"weight":35.841603038774416},{"id":91,"weight":32.398612793756314}]},
{"id":91,"links":[{"id":21,"weight":205.16309784978526},{"id":64,"weight":484.70495246456426},{"id":90,"weight":32.398612793756314},{"id":92,"weight":48.582975729764854}]},
{"id":92,"links":[{"id":26,"weight":228.93825991593803},{"id":80,"weight":748.610228678086},{"id":91,"weight":48.582975729764854},{"id":83,"weight":30.837464050468025},{"id":120,"weight":188.7591372909066},{"id":117,"weight":174.02812344524793},{"id":146,"weight":572.3906052533645},{"id":159,"weight":344.37896644057383},{"id":160,"weight":232.48237745520552}]},
{"id":93,"links":[{"id":97,"weight":102.0522575060609}]},
{"id":94,"links":[{"id":6,"weight":310.40008748620136},{"id":7,"weight":1281.6150329914337}]},
{"id":95,"links":[{"id":10,"weight":145.6349677143284},{"id":43,"weight":464.5912380083568},{"id":96,"weight":20.309465539051313},{"id":114,"weight":637.3889554757393},{"id":159,"weight":567.3380419121927},{"id":161,"weight":434.67294753052647}]},
{"id":96,"links":[{"id":95,"weight":20.309465539051313},{"id":97,"weight":63.213233743350074}]},
{"id":97,"links":[{"id":13,"weight":173.37684490280637},{"id":66,"weight":354.99096425656126},{"id":71,"weight":376.4367138297526},{"id":96,"weight":63.213233743350074},{"id":93,"weight":102.0522575060609},{"id":100,"weight":240.91712277511493},{"id":130,"weight":525.9007129529984},{"id":142,"weight":709.2888517760487},{"id":147,"weight":817.6275336689539},{"id":155,"weight":256.52958550596395}]},
{"id":98,"links":[{"id":99,"weight":38.26238414408713}]},
{"id":99,"links":[{"id":98,"weight":38.26238414408713},{"id":100,"weight":81.55030224358033}]},
{"id":100,"links":[{"id":97,"weight":240.91712277511493},{"id":99,"weight":81.55030224358033},{"id":130,"weight":766.6709881560315},{"id":142,"weight":949.697709689759}]},
{"id":101,"links":[{"id":37,"weight":174.53904785374436},{"id":81,"weight":323.12731048143235},{"id":102,"weight":47.30056869901373},{"id":143,"weight":92.85603330225935},{"id":109,"weight":48.22919045283958}]},
{"id":102,"links":[{"id":55,"weight":115.82406310298205},{"id":101,"weight":47.30056869901373},{"id":103,"weight":35.380667355785846},{"id":125,"weight":257.26089278078433},{"id":145,"weight":86.69660180432261}]},
{"id":103,"links":[{"id":102,"weight":35.380667355785846},{"id":104,"weight":43.58038875739012}]},
{"id":104,"links":[{"id":103,"weight":43.58038875739012}]},
{"id":105,"links":[{"id":106,"weight":49.00253118207074}]},
{"id":106,"links":[{"id":28,"weight":69.99034725665763},{"id":48,"weight":336.640487491796},{"id":61,"weight":94.82870098367673},{"id":77,"weight":300.6735447670068},{"id":105,"weight":49.00253118207074},{"id":107,"weight":27.405833077229865}]},
{"id":107,"links":[{"id":31,"weight":78.85870804148841},{"id":106,"weight":27.405833077229865},{"id":108,"weight":39.67467085048693},{"id":146,"weight":161.5285374896578}]},
{"id":108,"links":[{"id":107,"weight":39.67467085048693},{"id":109,"weight":28.034817769574357}]},
{"id":109,"links":[{"id":108,"weight":28.034817769574357},{"id":101,"weight":48.22919045283958}]},
{"id":110,"links":[{"id":52,"weight":325.05226324305437},{"id":111,"weight":25.375224080516592},{"id":150,"weight":133.5176916667986}]},
{"id":111,"links":[{"id":82,"weight":269.9434631344396},{"id":110,"weight":25.375224080516592}]},
{"id":112,"links":[{"id":4,"weight":512.6115351930363},{"id":113,"weight":83.28092580028589},{"id":153,"weight":239.32930481887956}]},
{"id":113,"links":[{"id":112,"weight":83.28092580028589}]},
{"id":114,"links":[{"id":10,"weight":495.27154468751036},{"id":15,"weight":486.6941103104043},{"id":42,"weight":201.32515950767336},{"id":70,"weight":795.7351820979673},{"id":86,"weight":329.0079671927419},{"id":95,"weight":637.3889554757393},{"id":115,"weight":101.77827438406392},{"id":132,"weight":799.5716987622106},{"id":159,"weight":71.39727162164735},{"id":161,"weight":202.98164149753293}]},
{"id":115,"links":[{"id":114,"weight":101.77827438406392},{"id":116,"weight":55.33658324913234},{"id":160,"weight":63.25551243883601}]},
{"id":116,"links":[{"id":115,"weight":55.33658324913234},{"id":117,"weight":64.34602362067791}]},
{"id":117,"links":[{"id":80,"weight":922.5836952812741},{"id":92,"weight":174.02812344524793},{"id":116,"weight":64.34602362067791},{"id":118,"weight":25.616725172113455},{"id":157,"weight":242.8128909397011},{"id":159,"weight":170.35193802012466},{"id":160,"weight":58.715883663506396}]},
{"id":118,"links":[{"id":45,"weight":301.0257064648056},{"id":117,"weight":25.616725172113455},{"id":119,"weight":49.34327971363866}]},
{"id":119,"links":[{"id":9,"weight":357.92609495024175},{"id":41,"weight":304.09006218105435},{"id":86,"weight":151.90228469087668},{"id":118,"weight":49.34327971363866},{"id":120,"weight":92.13543408276188},{"id":161,"weight":262.52409073917073}]},
{"id":120,"links":[{"id":26,"weight":412.75883815805963},{"id":92,"weight":188.7591372909066},{"id":119,"weight":92.13543408276188}]},
{"id":121,"links":[{"id":74,"weight":140.7096972711017},{"id":122,"weight":49.158380910815055}]},
{"id":122,"links":[{"id":71,"weight":299.55665657848795},{"id":121,"weight":49.158380910815055},{"id":123,"weight":22.072670207307805},{"id":132,"weight":207.55080569931508}]},
{"id":123,"links":[{"id":122,"weight":22.072670207307805},{"id":124,"weight":23.95147516157658}]},
{"id":124,"links":[{"id":54,"weight":422.2533744531271},{"id":59,"weight":204.95995317424766},{"id":67,"weight":334.3275404295602},{"id":72,"weight":96.7927637387651},{"id":123,"weight":23.95147516157658},{"id":125,"weight":37.10612238764236}]},
{"id":125,"links":[{"id":55,"weight":152.5815324765102},{"id":102,"weight":257.26089278078433},{"id":124,"weight":37.10612238764236},{"id":126,"weight":34.927378155183234},{"id":141,"weight":258.1518700205428}]},
{"id":126,"links":[{"id":125,"weight":34.927378155183234},{"id":127,"weight":32.98615477724716}]},
{"id":127,"links":[{"id":126,"weight":32.98615477724716},{"id":128,"weight":16.22742340952728}]},
{"id":128,"links":[{"id":22,"weight":342.80891355908545},{"id":26,"weight":418.9168740031914},{"id":49,"weight":451.6465055929891},{"id":86,"weight":641.7308695509025},{"id":127,"weight":16.22742340952728},{"id":158,"weight":35.94532649063341},{"id":135,"weight":60.199671727187834}]},
{"id":129,"links":[]},
{"id":130,"links":[{"id":12,"weight":423.73430144247175},{"id":97,"weight":525.9007129529984},{"id":100,"weight":766.6709881560315},{"id":131,"weight":89.63249969243708},{"id":137,"weight":44.67921793243463}]},
{"id":131,"links":[{"id":21,"weight":170.98157093493768},{"id":24,"weight":210.320160984807},{"id":70,"weight":74.14997079035552},{"id":71,"weight":67.09223542194184},{"id":130,"weight":89.63249969243708},{"id":132,"weight":70.44518620133896},{"id":142,"weight":273.78551731820147},{"id":138,"weight":351.5407196157602},{"id":156,"weight":296.14115910691856}]},
{"id":132,"links":[{"id":16,"weight":378.87860482952715},{"id":43,"weight":675.2576499747647},{"id":71,"weight":92.36209904758263},{"id":74,"weight":291.38771611392053},{"id":86,"weight":471.07620316344014},{"id":114,"weight":799.5716987622106},{"id":122,"weight":207.55080569931508},{"id":131,"weight":70.44518620133896},{"id":133,"weight":53.20257740632008},{"id":157,"weight":803.2044974295419},{"id":159,"weight":733.0042344492691},{"id":161,"weight":639.4014374495188}]},
{"id":133,"links":[{"id":132,"weight":53.20257740632008},{"id":134,"weight":50.32148162147291}]},
{"id":134,"links":[{"id":133,"weight":50.32148162147291}]},
{"id":135,"links":[{"id":22,"weight":282.6431518625895},{"id":49,"weight":511.844500792933},{"id":86,"weight":581.5450583764579},{"id":128,"weight":60.199671727187834},{"id":136,"weight":49.49620942581232}]},
{"id":136,"links":[{"id":17,"weight":499.55002623505305},{"id":39,"weight":827.1618749297187},{"id":44,"weight":809.6193736796965},{"id":57,"weight":253.7803363149803},{"id":135,"weight":49.49620942581232},{"id":137,"weight":31.43818529653531},{"id":161,"weight":721.2341683340406}]},
{"id":137,"links":[{"id":136,"weight":31.43818529653531},{"id":130,"weight":44.67921793243463}]},
{"id":138,"links":[{"id":62,"weight":456.67384076187767},{"id":131,"weight":351.5407196157602},{"id":139,"weight":23.777001819899976},{"id":156,"weight":645.5864796077194},{"id":147,"weight":60.21879922428856}]},
{"id":139,"links":[{"id":138,"weight":23.777001819899976},{"id":140,"weight":47.7270317027185}]},
{"id":140,"links":[{"id":139,"weight":47.7270317027185},{"id":141,"weight":23.039143895070726}]},
{"id":141,"links":[{"id":125,"weight":258.1518700205428},{"id":140,"weight":23.039143895070726},{"id":142,"weight":70.27931275561932}]},
{"id":142,"links":[{"id":12,"weight":605.3109163530141},{"id":17,"weight":589.0762111107545},{"id":22,"weight":308.1360659862511},{"id":26,"weight":333.7521131385038},{"id":39,"weight":911.9072364060776},{"id":57,"weight":206.67779462996376},{"id":97,"weight":709.2888517760487},{"id":100,"weight":949.697709689759},{"id":131,"weight":273.78551731820147},{"id":141,"weight":70.27931275561932},{"id":143,"weight":64.98383750211529}]},
{"id":143,"links":[{"id":101,"weight":92.85603330225935},{"id":142,"weight":64.98383750211529},{"id":144,"weight":30.097551026293797}]},
{"id":144,"links":[{"id":143,"weight":30.097551026293797},{"id":145,"weight":100.10632073495346}]},
{"id":145,"links":[{"id":77,"weight":213.64883986492893},{"id":102,"weight":86.69660180432261},{"id":144,"weight":100.10632073495346},{"id":146,"weight":43.76422694558446}]},
{"id":146,"links":[{"id":36,"weight":250.2794421195491},{"id":92,"weight":572.3906052533645},{"id":107,"weight":161.5285374896578},{"id":145,"weight":43.76422694558446},{"id":147,"weight":62.554721469410964}]},
{"id":147,"links":[{"id":13,"weight":644.3394809974541},{"id":22,"weight":378.58967001309463},{"id":27,"weight":349.297544643658},{"id":81,"weight":350.10072558836436},{"id":97,"weight":817.6275336689539},{"id":146,"weight":62.554721469410964},{"id":138,"weight":60.21879922428856}]},
{"id":148,"links":[{"id":153,"weight":83.09197814327331}]},
{"id":149,"links":[{"id":28,"weight":340.8623984131601},{"id":33,"weight":324.73490720293273},{"id":150,"weight":40.27917844217807}]},
{"id":150,"links":[{"id":47,"weight":482.148526864346},{"id":52,"weight":458.3924820628751},{"id":110,"weight":133.5176916667986},{"id":149,"weight":40.27917844217807}]},
{"id":151,"links":[{"id":152,"weight":16.05179269405632}]},
{"id":152,"links":[{"id":151,"weight":16.05179269405632},{"id":153,"weight":95.08995160516936}]},
{"id":153,"links":[{"id":3,"weight":205.87711451966396},{"id":4,"weight":737.0774717197235},{"id":5,"weight":135.6583919878893},{"id":112,"weight":239.32930481887956},{"id":152,"weight":95.08995160516936},{"id":148,"weight":83.09197814327331}]},
{"id":154,"links":[{"id":11,"weight":277.7768610143829},{"id":155,"weight":33.19341980748434}]},
{"id":155,"links":[{"id":66,"weight":98.46201480548129},{"id":97,"weight":256.52958550596395},{"id":154,"weight":33.19341980748434},{"id":156,"weight":58.397228717203404}]},
{"id":156,"links":[{"id":19,"weight":445.50275071828963},{"id":23,"weight":424.0775352517563},{"id":71,"weight":229.10251215735252},{"id":131,"weight":296.14115910691856},{"id":138,"weight":645.5864796077194},{"id":155,"weight":58.397228717203404}]},
{"id":157,"links":[{"id":10,"weight":475.0410693030716},{"id":16,"weight":434.2784913076533},{"id":43,"weight":149.3858968154142},{"id":70,"weight":798.2577975693512},{"id":86,"weight":337.27659265236},{"id":117,"weight":242.8128909397011},{"id":132,"weight":803.2044974295419}]},
{"id":158,"links":[{"id":57,"weight":180.39992938539922},{"id":128,"weight":35.94532649063341},{"id":160,"weight":44.1282092957909}]},
{"id":159,"links":[{"id":10,"weight":424.5428223515612},{"id":15,"weight":416.3689141062264},{"id":42,"weight":136.50001088374017},{"id":70,"weight":728.6111329180097},{"id":86,"weight":264.5824947674739},{"id":92,"weight":344.37896644057383},{"id":95,"weight":567.3380419121927},{"id":117,"weight":170.35193802012466},{"id":114,"weight":71.39727162164735},{"id":132,"weight":733.0042344492691},{"id":160,"weight":112.12052199347016},{"id":161,"weight":134.34656675925888}]},
{"id":160,"links":[{"id":10,"weight":386.9207320551183},{"id":15,"weight":344.37793610826577},{"id":38,"weight":259.95392603739697},{"id":41,"weight":191.66415098549578},{"id":65,"weight":638.3194987663459},{"id":80,"weight":981.0925983047125},{"id":86,"weight":170.91599639926625},{"id":92,"weight":232.48237745520552},{"id":117,"weight":58.715883663506396},{"id":115,"weight":63.25551243883601},{"id":158,"weight":44.1282092957909},{"id":159,"weight":112.12052199347016}]},
{"id":161,"links":[{"id":10,"weight":293.9650424785912},{"id":16,"weight":262.12088923847034},{"id":22,"weight":491.24332514971724},{"id":43,"weight":35.96703115932087},{"id":41,"weight":42.17582986887332},{"id":70,"weight":632.6993752105095},{"id":87,"weight":285.65519727136444},{"id":84,"weight":262.445211274406},{"id":95,"weight":434.67294753052647},{"id":119,"weight":262.52409073917073},{"id":114,"weight":202.98164149753293},{"id":132,"weight":639.4014374495188},{"id":136,"weight":721.2341683340406},{"id":159,"weight":134.34656675925888}]}]

var get_path = function(graph, a) {
// declaration
  let cache, i, v, queue, node, links, root,
      j, link, c, n, d, max, w, L = graph.length;

  // initialization
  cache = Array(L);
  i = L;
  while (--i >= 0) {
      v = graph[i];
      cache[v.id] = {
          id: v.id,
          distance: Infinity,
          links: v.links,
          prev: null,
      };
  }
  root = cache[a];
  root.distance = 0;
  queue = [root];

  // processing
  i = 0;
  while (i < queue.length) {
      node = queue[i];
      links = node.links;
      j = links.length;
      while (--j >= 0) {
          link = links[j];
          c = cache[link.id];
          d = node.distance + link.weight;
          if (d < c.distance) {
              c.prev = node;
              c.distance = d;
              queue.push(c);
          }
      }
      i++;
  }

  return cache
};

var times = [], newTimes = [];

for (let i=0; i < 1000; i++) {
  var s_t = performance.now();
  res = get_path(graph, 157);
  times.push(performance.now() - s_t);
}

for (let i=0; i < 1000; i++) {
  var s_t = performance.now();
  res = getDistancesFromNode(graph, 157);
  newTimes.push(performance.now() - s_t);
}

function average(nums) {
    return nums.reduce((a, b) => (a + b)) / nums.length;
}

console.log(average(times), 'ms')
console.log(average(newTimes), 'ms')


os.walk listing directory I use this script to get a list of all my photos in a folder and its subfolders. However, the program seems to be really slow.

There are 50,000 .jpg pictures in this folder and subfolders. I can reduce the for loops but the program still runs more or less at the same speed.

I am open to using lambda as well but would like to use Python's basic functions at the best possible speed. Can anyone suggest an improvement?

import os
from fnmatch import fnmatch
import sys

root = "C:\\Users\\Agartha\\Desktop\\photos"
pattern = "*.jpg"

with open("./files\\list.txt", "w") as a:
    for path, subdirs, files in os.walk(root):
       for filename in files:
            if fnmatch(filename,pattern):
                a.write(str(os.path.join(filename)) + '\n')

Here you do 4 things at the same time. Your are

Walking over all the files of the directory
Comparing the filename to a pattern
Formatting the filename
Appending the filename to a file

To find out what is the slower step, you should decouple this

def find_files(root):
    for path, subdirs, files in os.walk(root):
        for filename in files:
            yield filename

def filter_filename(files, pattern):
    for filename in files:
        if fnmatch(filename,pattern):
            yield filename

def format_filenames(files, root):
    for filename in files:
        yield str(os.path.join(filename)) + '\n'

def writelines(out_file, files):
    for filename in files:
        out_file.write(filename)
    # or:
    # out_file.writelines(files)

def main(root, pattern, filename_out):
    files = find_files(root)
    files_filtered = filter_filename(files, pattern)
    files_formatted = format_filenames(files, root)
    with open(filename_out, 'w') as out_file:
        writelines(out_file, files_formatted)

Then Afterwards, there are 4 things you can time individually to find out the largest culprit in time consumption and work on that one:

files = list(find_files(root))
files_filtered = list(filter_filename(files, pattern))
files_formatted = list(format_filenames(files, root))
with open(filename_out, 'w') as out_file: writelines(out_file, files_formatted)


Risk board game - best practices, private vs public variables and methods This is my first attempt at writing something in JavaScript besides a few Project Euler problems I did first. It's intended to be a library used client side and server side (both of which I know very little about).  I've read Crockford's Javascript: The Good Parts, but nothing else. I have the most current experience with Python.

I'd appreciate any feedback (especially style concerns), I know there's a terrifically large amount of stuff to address, so if that's overwhelming, I have these immediate concerns:

By using closure with my country object, am I wasting memory, and hence should be using prototypical inheritance?
My JSONify code seems redundant, I assume there's a more correct way to do this?
Mixing private instance variables like "countries" and public instance variables looks ugly.  Is that how it's supposed to be done?

Full code available, link is to the commit which brings that code up to date with what I've posted here. There's a larger object (250 lines) I would love to post if that's not excessive, but for now here's the smaller object. (50 lines)

var country = function(name, connected_to){
    // variables in constructor signature are private
    // These are private variables that weren't in the constructor signature
    var helper = function(player, num_troops){
        // These are public methods
        return {
            'get_name' : function(){return name},
            'get_troops' : function(){return num_troops;},
            'get_owner' : function(){return player;},
            'get_touching_names' : function(){return connected_to},
            'is_owned_by' : function(in_player){return in_player == player},
            'is_touching' : function(to){
                if (!(typeof to === 'string')){
                    to = to.get_name();
                }
                for (var i = 0; i < connected_to.length; i++){
                    if (connected_to[i] === to){
                        return true;
                    }
                }
                return false;
            },

            'set_state' : function(in_player, in_num_troops){
                player = in_player;
                num_troops = in_num_troops;
            },
            'toString' : function(){
                return '[country '+name+']';
            },
            'get_ascii' : function(){
                return name + ' owned by ' + player + ' with '+ num_troops +
                    '\n        connected to ' + connected_to;
            },
            'jsonify' : function(){
                var j = {
                    'name' : name,
                    'player' : player,
                    'num_troops' : num_troops,
                    'connected_to' : connected_to
                }
                return j;
            },
            'dejasonify' : function(j){
                name = j.name;
                player = j.player;
                num_troops = j.num_troops;
                connected_to = j.connected_to;
            }
        };
    };
    return helper(null, null)
};

If you stop worrying about "privates" everything becomes much cleaner.

By using closure with my "country" object am I wasting memory, and hence should be using prototypical inheritance?

The closure is a small performance hit. For a board game it won't matter. But if you decide not to use local variables in closures (AKA "privates") then you can get rid of the closure and use a constructor function / prototype.
One benefit of doing that is that it will give tools like eclipse, webkit inspector, etc. a clue as to how things are represented in your code so they can annotate stuff for you, provide pop-up help, autocompletion, things like that.

My jsonify code seems redundant, I assume there's a more correct way to do this?
Mixing private instance variables like "countries" and public instance variables looks ugly, is that how it's supposed to be done?

Getting rid of "privates" and making these all properties would be the easiest way to fix both of these issues. In that case the get_* functions could also go.
A few other points:

Usually javascript identifiers are written as camelCase, not underscore_separated.

The quotes around the property names in the object literal are not needed for character sequences that would otherwise be valid identifiers.

to is a confusing variable name, try to find something more descriptive. I changed it to country in the example below.

I would probably write it something like this:
function Country (name, connectedTo){
    this.name = name;
    this.connectedTo = connectedTo;
    // other constructor stuff...
}

Country.prototype = {

    // default properties

    numTroops: 0,

    player: null,

    // functions

    isOwnedBy: function(inPlayer){
        return inPlayer == this.player;
    },
    isTouching: function(country){
        // best to avoid typeof if possible
        if (country.name){
            country = country.name;
        }
        // this performs somewhat better than evaluating foo.length every iteration
        for (var i = 0, len = this.connectedTo.length; i < len; i++){
            if (this.connectedTo[i] === country){
                return true;
            }
        }
        return false;
    },
    setState: function(inPlayer, inNumTroops){
        this.player = inPlayer;
        this.numTroops = inNumTroops;
    },
    toString: function(){
        return '[country '+this.name+']';
    },
    getAscii: function(){
        return this.name + ' owned by ' + this.player + ' with '+ this.numTroops +
            '\n        connected to ' + this.connectedTo;
    }
};

If setState is only called once per object, you might consider making that code part of the constructor and getting rid of setState.