Are 2 Django URLs pointing at 1 view unorthodox? I'm in the process of writing a private messaging system and I have 2 URLs which go to the same view (is that bad?). The logic behind this madness relates to the way user relationships are structured in our system...

     T
    / \___
   C1      \
   |        C2
   P1      /  \
          P2   P3

T = Trainer, C = Coach, P = Participant.

We define this structure in a pretty simple way:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    higher_up = models.ForeignKey('self', blank=True, null=True)
    # ...
    type = models.CharField(max_length=1, choices=TYPES, default='P')

And bam, We've got a DAG. Here's my 2 urls in urls.py:

# talk to lower_down
url(r'^pm/(?P<userprofile_pk>\d+)/$', 'private_message'),

# talk to higher_up
url(r'^pm/$', 'private_message'),

and the private_message view...

def private_message(request, userprofile_pk=''):
    me = request.user.userprofile
    src_pk = me.pk

    if userprofile_pk == '': # /pm/
        you = me.higher_up
        dest_pk = you.pk
    else: # userprofile_pk was not blank /pm/<num>
        you = UserProfile.objects.get(pk=userprofile_pk)
        dest_pk = you.pk

    # Are they allowed to talk?
    if me.is_assoc_with(you):
        return HttpResponseRedirect('/')

    feed_vars = {
        'dest_pk': dest_pk,
        'src_pk' : src_pk,
    }

    return render(request, 'talk/private_message.html', feed_vars)

The me.is_assoc_with(you) bit just checks where me and you are in the userprofile structure. In the above ASCII art diagram, P1 should not be allowed to talk with P2. T can talk to C1, C1 and C2 cannot talk. T and P3 can talk, etc.

Is this an ok pattern to have 2 URLs go to 1 view that handles logic based on the URL?

I do feel like I am reinventing the wheel here. I think there should be a more flexible approach with settings for who can talk to whom factored out of the business logic itself.

Why not separate the view functions?

# talk to lower_down
url(r'^pm/(?P<userprofile_pk>\d+)/$', 'private_message_down'),

# talk to higher_up
url(r'^pm/$', 'private_message_up'),

The view functions:

def private_message(src, dest):
    # Are they allowed to talk?
    if src.is_assoc_with(dest):
        return HttpResponseRedirect('/')

    feed_vars = {
        'dest_pk': dest.pk,
        'src_pk' : src.pk,
    }

    return render(request, 'talk/private_message.html', feed_vars)

def private_message_down(request, userprofile_pk):
    src = request.user.userprofile

    # TODO: profile actually exists?
    dest = UserProfile.objects.get(pk=userprofile_pk)

    return private_message(src, dest)

def private_message_up(request):
    src = request.user.userprofile
    dest = me.higher_up

    return private_message(src, dest)

I added a TODO for failure handling, I think you should wrap the .get lookup in a try-except UserProfile.DoesNotExist, for example:

from django.http import Http404

def private_message_down(request, userprofile_pk):
    src = request.user.userprofile

    try:
        dest = UserProfile.objects.get(pk=userprofile_pk)
    catch UserProfile.DoesNotExist:
        raise Http404

    return private_message(src, dest)

Also, you probably want to decorate private_message_* with @login_required (from django.contrib.auth.decorators import login_required).


Bash script to install vim configuration files For the first time ever I decided to write a bash script. It works fine, but I'm feeling that I didn't write the most efficient code in order for future extension of the script. I also don't really get a good feeling on the look of the code. Any suggestions on how to improve the script?

Purpose of the script:
To pull a vim configuration located on github and then copy the files to the home directory and create some directories needed by the vim configuration.

dirs=( "~/.vim/undo" "~/.vim/swap" "~/.vim/backup" )
function error_exit
{
  printf '[failed\n]'
  exit 1
}

printf '   vim_config by Roel0                \n'
printf '   version 1.0                       \n'

printf 'Updating vim configuration ... '
if !(git pull > /dev/null;) then
  error_exit
fi
printf '[OK]\n'

printf 'Installing vim configuration ... '
if !(cp -r ./.vim* ~/) then
  error_exit
fi
printf '[OK]\n'

printf 'Creating configured directories ... '
for i in "${dirs[@]}"
do
  if !(mkdir -p $i) then
    error_exit
  fi
done
printf '[OK]\n'

Shebang

You should declare what shell you require to make porting the script easy and not a nightmare of commands not being available and syntactical differences leading to bugs.

#!/bin/bash

Pull conflicts

The script does not account for problems when pull does funny things like conflicting. Also to prevent changes to the executable run through PATH modifications (whether intentional or not) you may want to consider using the absolute executable path.

Looping

I'd personally prefer the simpler loop instead of the string looping you do there.

for i in $dirs
do
  if !(mkdir -p $i) then
    error_exit
  fi
done

at least on my zsh this works as intended.


Find a service and update its last ping time I found those functions reviewing a C++14 codebase I am working on:

struct Service {
    Service(std::string id): id(id) {}
    virtual ~Service() {}

    std::string id;
    HeartbeatCheckpoint lastPing;
    HeartbeatCheckpoint lastPong;
};

std::map<std::string, Service> services;

bool Pacemaker::updatePong(const std::string& id) {
    auto found = services.find(id);
    if(found != services.end()) {
        found->second.lastPong = std::chrono::high_resolution_clock::now();
        return true;
    }
    return false;
}

bool Pacemaker::updatePing(const std::string& id) {
    auto found = services.find(id);
    if(found != services.end()) {
        found->second.lastPing = std::chrono::high_resolution_clock::now();
        return true;
    }
    return false;
}

I would like to refactor them creating a common function to remove code duplication, but I can't figure out how to pass the member variable (lastPing and lastPong).

Is there a better way to write this code?

The code as posted doesn't compile; I had to add some includes and definitions:

#include <chrono>
#include <map>
#include <string>

using HeartbeatCheckpoint = std::chrono::high_resolution_clock::time_point;

Having done that, the simplest refactoring of the functions would be to just pass a member pointer to specify the field you want to set in the found object:

bool update(const std::string& id, HeartbeatCheckpoint (Service::*field))
{
    auto found = services.find(id);
    if (found == services.end())
        return false;
    found->second.*field = std::chrono::high_resolution_clock::now();
    return true;
}

bool updatePing(const std::string& id) {
    return update(id, &Service::lastPing);
}

bool updatePong(const std::string& id) {
    return update(id, &Service::lastPong);
}

Alternatively, something I've used before is a small wrapper around find to give it an interface that can be tested inline:

namespace collection
{
    template<typename T>
    inline bool contains(const T& container, const typename T::key_type& key)
    {
        return container.find(key) != container.end();
    }
    template<typename T>
    inline bool contains(const T& container, const typename T::key_type& key, typename T::const_iterator& it)
    {
        return (it = container.find(key)) != container.end();
    }
    template<typename T>
    inline bool contains(T& container, const typename T::key_type& key, typename T::iterator& it)
    {
        return (it = container.find(key)) != container.end();
    }
    template<typename T>
    inline bool contains(const T& container, const typename T::key_type& key, typename T::mapped_type& result)
    {
        typename T::const_iterator it;
        return contains(container, key, it) && (result = it->second, true);
    }
}

This is then used slightly more readably (at the expense of being unfamiliar outside your team):

bool updatePing(const std::string& id) {
    std::map<std::string, Service>::iterator it;
    const bool found = collection::contains(services, id, it);
    if (found)
        it->second.lastPing = std::chrono::high_resolution_clock::now();
    return found;
}

It's arguably slightly neater if we assign to a pointer, rather than to an iterator:

namespace collection
{
    template<typename T, typename V>
    inline bool contains(const T& container, const typename T::key_type& key, const V *& result)
    {
        auto it = container.find(key);
        if ((it = container.find(key)) == container.end())
            return false;
        result = &it->second;
        return true;
    }
    template<typename T, typename V>
    inline bool contains(T& container, const typename T::key_type& key, V *& result)
    {
        auto it = container.find(key);
        if ((it = container.find(key)) == container.end())
            return false;
        result = &it->second;
        return true;
    }
}

bool updatePing(const std::string& id) {
    Service *service;
    const bool found = collection::contains(services, id, service);
    if (found)
        service->lastPing = std::chrono::high_resolution_clock::now();
    return found;
}

As you haven't shown us where these functions are used, we can't determine whether the return value is important or not.  If it's not required, we can easily use the Null Object pattern to provide a sink for the result:

Service& find_service_or_default(const std::string& id)
{
    static Service dummy{""};
    auto it = services.find(id);
    return (it == services.end()) ? dummy : it->second;
}

void updatePing(const std::string& id)
{
    find_service_or_default(id).lastPing = std::chrono::high_resolution_clock::now();
}

void updatePong(const std::string& id)
{
    find_service_or_default(id).lastPong = std::chrono::high_resolution_clock::now();
}

This is all assuming that you have more functions than just those two - if not, then the refactoring is unlikely to be an improvement, and the original (with side-by-side duplication of a clearly recognisable structure) is likely to be more readable.


Adding action listener to Swing JButton I'm aware that there are existing questions about JButtons, but I haven't found anything that's asking exactly what I'm trying to figure out.

I inherited a Java project that has a horrible structure; my job is essentially to go through and fix it. The project includes a GUI, which is where this question becomes relevant.

In the state that I inherited it, the GUI's buttons simply didn't work correctly. I don't have an exact way to describe it, but essentially the previous programmer neglected event-driven programming, and set up a bunch of busy-wait threads waiting for things to happen. It did not work.

In the course of trying to get the buttons to trigger the correct state change in the GUI, I completely reworked the buttons into this form:

JButton button = new JButton(new AbstractAction("A button") {
    @Override
    public void actionPerformed(ActionEvent ae) {
        doStuff();
    }
});

As far as I can tell, this works correctly; doStuff() is called, which handles the GUI's state transitions. This form is the result of struggling with different methods suggested at various places; this is the only one that I was able to get to work.

My specific question at this point is, firstly, is there a problem with using an AbstractAction in this form to listen for a button click, and secondly, is there a better way to do this? Most everyone uses button.addActionListener(), which is what I couldn't get to work correctly. If it's at all relevant, there are multiple buttons on some GUI states.

I'm using Java 7 for this. However, it wouldn't be a huge deal to switch to Java 8.

What you are already doing is a nice way of defining both the text for the button and the action for it at the same time.

As you're able to use Java 8, I would really recommend that you start using Java 8.

You might actually even want to consider migrating to JavaFX (that would probably require even more work though, but it could be a nice user interface upgrade).

One option, when using Java 8, is to do this:

JButton button = new JButton("A button");
button.addActionListener(this::doStuff);

private void doStuff(ActionEvent event) {
    ...
}

Or:

JButton button = new JButton("A button");
button.addActionListener(e -> this.doStuff());

private void doStuff() {
    ...
}

A different approach would be to do this:

JButton createButton(String text, Runnable action) {
    return new JButton(new AbstractAction(text) {
        @Override
        public void actionPerformed(ActionEvent ae) {
             action.run();
        }
    });
}

And then:

JButton button = createButton("A button", this::doStuff);
JButton button = createButton("Another button", () -> this.doStuff());


Raising an assertation error if string is not an email I created a class EmailParser and check if a supplied string is an email or not with
the help of the standard librarie's email module. If the supplied string is not an
email I raise an exception.

Is this a pythonic way to deal with inappropiate input values an assertation error?
How do you generally check input values and report inappropiate states and values?

code:

class EmailParser(object):

    def __init__(self, message):
        assert isinstance(message, basestring)

        parsed_message = email.message_from_string(message)

        if self._is_email(parsed_message):
            self.email_obj = parsed_message
        else:
            assert False, 'The supplied string is no email: %s' % parsed_message

    def __str__(self, *args, **kwargs):
        return self.email_obj.__str__()

    def _is_email(self, possible_email_obj):
        is_email = False

        if isinstance(possible_email_obj, basestring):
            is_email = False

        # I struggled a lot with this part - however, I came to the conclusion
        # to ask for forgiveness than for permission, see Zen of Python and
        # http://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python/610923#610923
       try:
           if '@' in possible_email_obj['To']:
               is_email = True

       except TypeError, e:
           is_email = False

       return is_email

class EmailParser(object):

    def __init__(self, message):
        assert isinstance(message, basestring)

In python, we typically don't check types. Basically, we prefer duck typing. Let the user pass whatever they want.

        parsed_message = email.message_from_string(message)

        if self._is_email(parsed_message):
            self.email_obj = parsed_message
        else:
            assert False, 'The supplied string is no email: %s' % parsed_message

As Janne mentioned, this is not a good use of an assertion.

    def __str__(self, *args, **kwargs):
        return self.email_obj.__str__()

You shouldn't call __str__ methods directly. Use the str() function.

    def _is_email(self, possible_email_obj):
        is_email = False

        if isinstance(possible_email_obj, basestring):
            is_email = False

This is useless, because the only possible outcome is to set is_email to False which it already is. Furthermore, you know this has to be a Message object since it was returned from message_from_string so why are you checking whether it's a string? You know its not.

        # I struggled a lot with this part - however, I came to the conclusion
        # to ask for forgiveness than for permission, see Zen of Python and
        # http://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python/610923#610923
       try:
           if '@' in possible_email_obj['To']:
               is_email = True

       except TypeError, e:
           is_email = False

In what circumstances can this possibly happen?

       return is_email

Don't needlessly set boolean flags. Just return True or False directly.

Better yet, raise an exception with details on what exactly is wrong rather than returning false and raising a generic exception later.


Fastest way to find if N numbers are even or odd I want to find if 'N' numbers are even or odd in the fastest way possible.

Here's the following code which produces the desired output, but I do not know how efficient it is.

int main() {
int N,x;
scanf("%d",&N);
while(N--){
   scanf("%d",&x);
    x&1 ? cout << "No" << endl : cout << "Yes" << endl;
}
return 0;

Can this code be improved in any way to get faster results?

Given that 99.9% of time is spent in scanf, cout and waiting for user input then there isn't much you can do about performance (but see notes).

What it may be improved is readability of your code. First of all your're mixing C style I/O (scanf) with C++ I/O (std::cout). Let's move to C++ std::cin.

Do not ever add using namespace std; (Why is “using namespace std” considered bad practice?)

In C++ there is no need to declare all variables at the top of your method.

I think ternary operator ?: is usually used for its result, not for side-effects of called code (here you're ignoring returned values). At first this usage is confusing for caller.

If you have a loop I think for is more clear (in intent) than while. Do not worry about extra variable, compiler will optimize it for you.

int main()
{
    int numbersToRead;
    std::cin >> numbersToRead;

    for (int i=0; i < numbersToRead; ++i)
    {
       int number;
       std::cin >> number;

       bool isEven = (number & 1) == 0;
       std::cout << (isEven  ? "Yes" : "No") << std::endl;
    }

    return 0;
}

This example is pretty small but for more complex code you may want to extract some functions:

int main()
{
    for (int i=readNumberOfValuesToRead(); i > 0; --i)
       std::cout << (isEven(readValue()) ? "No" : "Yes") << std::endl;

    return 0;
}

If you're already using Boost you may use boost::irange (let me also show that you may/should move formatting to a proper manipulator (for simplicity in this example I use standard one which prints true/false instead of Yes/No):

int main()
{
    std::cout << std::boolalpha;
    for (auto i : boost::irange(0, readNumberOfValuesToRead()))
       std::cout << isEven(readValue()) << std::endl;

    return 0;
}

I'd stop here (but read notes) for this code however if your scenario is (or it will be) more complex you will probably need to introduce more classes: ValuesReader base class (with a StandardInputValuesReader concrete class) to read values from an input stream and ValuesWriter (same...) to write output somewhere (using a ValueProcessor or ValueConverter and a custom stream manipulator?) Also remember that proper classes will make your life incredibly more easy during testing (because an abstract base class can be easily mocked while to read from std::cin you need much more code in your tests - and they can't be well-tested in isolation.)

EDIT

Few notes:

As Snowhawk04 noted I/O synchronization (between C and C++ functions) may slow down execution (or it may even not).
(number & 1) == 0 and (number % 2) == 0 seem to have the same performance (and generated assembly output), at least on GCC. Pick the one you feel more comfortable with.
Flushing output buffer (because of newline) may make it slower (but again consider that you're waiting user input). A different approach may help (concatenate results into std::string or std::stringstream and then write just once to std::cout). It's not a good approach if your application is interactive but it may be viable if it's intended to be used from command line with I/O redirection.

Just a proof of concept:

int main()
{
    std::ostringstream output;
    output << std::boolalpha;
    for (int i : boost::irange(0, readNumberOfValuesToRead(), 0))
        output << isEven(readValue()) << std::endl;

    std::cout << output.str();

    return 0;
}

Someone noted '\n' instead of std::endl will prevent flushing. '\n' has some performance benefits but you won't have a single flush because we're also reading from std::cin. The use of std::stringstream is the way to avoid flushing. Note that it is implementation dependent if this useless flush() will impact performance or not for std::cout (when std::cout.sync_with_stdio(false) is called.) In general I'd suggest to use it (measure...) but still stay on std::stringstream if you want to avoid multiple flushes (not for interactive use.)
I am just half joking but for a non-interactive use if you really need to squeeze maximum performance from this program then you may want to write it in C (in practice dropping C++ I/O and strings...)


Planetary Nomenclature I'm new when it comes to Java and programming in general, but I feel I have a decent grasp on a lot of the fundamentals.

I've created this "random planet generator" that is virtually useless aside from minimal entertainment, but I've created it to help solidify what I've been learning. I'm not asking for code, but for a direction with as much info as possible.

My random planet generator lists:

name:
temp:
size (circumference):
Density:
Length of Day:
Number of Moons:
Gravity:
If planet has rings:

My problem is, that for the name, it is horribly inefficient. Before I added the name field, I could generate millions of random planets in seconds. Now, I only set it to 50,000 planets and it still takes a minute or two to generate them all.

Here's the method I use to name the planets:

public void setName() throws FileNotFoundException {
    String prefix = "";
    String greek = "";
    int number;
    char suffix;
    Random rand = new Random();

    // randomly pull from list of star names
    int randName = rand.nextInt(300);
    File prefixFile = new File("StarNames.txt");
    Scanner inputFilePrefix = new Scanner(prefixFile);
    for (int i = 0; i <= randName; i++) {
        prefix = inputFilePrefix.nextLine();
    }
    inputFilePrefix.close();

    // randomly pull of list of Greek names
    int randGreek = rand.nextInt(11);
    File greekFile = new File("Greek.txt");
    Scanner inputFileGreek = new Scanner(greekFile);
    for (int i = 0; i <= randGreek; i++) {
        greek = inputFileGreek.nextLine();
    }
    inputFileGreek.close();

    // random number
    int randNum = rand.nextInt(1000);
    number = randNum;

    // random letter
    int randLetter = rand.nextInt(4);
    if (randLetter == 0) {
        suffix = 'a';
    } else if (randLetter == 1) {
        suffix = 'b';
    } else if (randLetter == 2) {
        suffix = 'c';
    } else {
        suffix = 'd';
    }

    // append to class name
    this.name = prefix + " " + greek + "-" + number + "" + suffix;

}

As you can tell, I have 2 text files. One for actual star names, and another for (what I think is) the Greek alphabet. It randomly picks a line to read up to and uses that name.
What could be a faster, more efficient way of doing this?

The name structure would essentially be something like:

 Name: Gemini ALPHA-229c

I have every planet generated added to an ArrayList one by one after all planets fields are created.

Any suggestions?

Review
You have posted one method for review, but that method has multiple responsibilities.
Good code follows the "Single Responsibility Principle". Your method does the following things:

it creates a random number generator
it generates a random number
it opens and reads data from a file and then closes it.
it generates another random number
it opens and reads data from another file, and then closes it.
it generates yet another random number
then another
it builds a string value from a number of components.
sets the value of the name field to this string

That is a lot of things happening in one method.
Additionally, your code has a number of magic numbers: 300 star names, 11 Greek letters, 1000 random values, and 4 letters. Each of these values are coded as magic numbers.
The File names are 'magic' too.
In that list, I can see the following bad practices:

two repeated items - selecting a random string from a file

a set method (setName) that does not take a value to set (setters should take a value, not generate a value)

You create a new Random instance each time

you repeatedly open/read/close multiple files.

you throw an IOException on a method which does not have a particularly IO-sounding name.

The above things should all be resolved.....
Alternative
Create a class that generates the names for you. Consider this class:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Random;

public class PlanetaryGenerator {

    private static final class RandomLine {
        private final List<String> lines;
        private final Random random = new Random();

        public RandomLine(String filename) throws IOException {
            this.lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8);
        }

        public String getRandom() {
            return lines.get(random.nextInt(lines.size()));
        }

    }

    private static final String[] SUFFIXES = {"a", "b", "c", "d"};

    private static final int MAXNUMBER = 1000;

    private final RandomLine stars;
    private final RandomLine greekletters;
    private final Random random;

    public PlanetaryGenerator(String starsfile, String greekfile) throws IOException {
        this.stars = new RandomLine(starsfile);
        this.greekletters = new RandomLine(greekfile);
        this.random = new Random();
    }

    public String generateName() {
        String prefix = stars.getRandom();
        String greek = greekletters.getRandom();
        int number = random.nextInt(MAXNUMBER);
        String suffix = SUFFIXES[random.nextInt(SUFFIXES.length)];

        // append to class name
        return String.format("%s %s-$d%s", prefix, greek, number, suffix);

    }
}

Then, in your existing code you can create a single instance of this, and just call it when you need a new name:
static PlanetaryGenerator ....

// get a new name:
String name = generator.generateName();


Project Euler 28 - Number Spiral Diagonals Project Euler problem 28

  Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

  $$\begin{array}{rrrrr}
\color{red}{21} &22 &23 &24 &\color{red}{25}\\
20 & \color{red}{7} & 8 & \color{red}{9} &10\\
19 & 6 & \color{red}{1} & 2 &11\\
18 & \color{red}{5} & 4 & \color{red}{3} &12\\
\color{red}{17} &16 &15 &14 &\color{red}{13}\\
\end{array}$$

  It can be verified that the sum of the numbers on the diagonals is 101.

  What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

I realized that the spiral was essentially an arithmetic sequence, so I based my code off of that.

from timeit import default_timer as timer
start = timer()

def spiral_diag_sum(n):
    if n < 1: return None
    elif n == 1: return 1
    elif n % 2 == 0: return None
    else:
        numbers = [1]
        while len(numbers) < (2*n - 1):
            increment = int(len(numbers) * 0.5 + 1.5)
            for p in range(4):
                numbers.append(numbers[-1] + increment)
    return sum(numbers)

ans = spiral_diag_sum(1001)
elapsed_time = (timer() - start) * 1000 # s --> ms
print "Found %d in %r ms." % (ans, elapsed_time)

from timeit import default_timer as timer

start = timer()

This line of code belongs with the code at the end of the program that times the call to spiral_diag_sum.

def spiral_diag_sum(n):

It's always a good idea to add a docstring. Several of the later Project Euler problems rely on finding a solution to earlier ones (though perhaps not this one), so you'll find yourself building up a library of useful code to reuse, in which case a good docstring makes it much easier to import.

    if n < 1: return None
    elif n == 1: return 1
    elif n % 2 == 0: return None
    else:

Checking that your arguments are sane is good. However, here you've got (in sequence) an error condition, a successful return, an error condition and (following the else) the main body of the function. It's better to separate the error conditions and handle them first.

You're also returning None as the error value, but you're not checking for that value after you call spiral_diag_sum at the end of the program. Add code to check that, or better yet, raise an exception here indicating the error. Since both the n < 1 and n % 2 == 0 clauses are checking the same argument's validity, you can combine them into one if-statement:

    if n < 1 or n % 2 == 0:
        raise ValueError("argument must be an odd-valued integer >= 1")

Resuming with the if n == 1: return 1 from the original code:

    if n == 1:
        return 1

Even when it's a short piece of code like here, I like to keep separate statements on their own line. Most Python code that you see will do that.

    else:
        numbers = [1]
        while len(numbers) < (2*n - 1):
            increment = int(len(numbers) * 0.5 + 1.5)
            for p in range(4):
                numbers.append(numbers[-1] + increment)
        return sum(numbers)

The formulas here need more thought than is required. Calculate and store the (2*n -1) formula in a variable with a descriptive name. The increment starts at 2 for the first row out from the center, and goes up by two for every subsequent row, so this whole thing could be:

        numbers_needed = 2 * n - 1
        increment = 2
        while len(numbers) < numbers_needed:
            for p in range(4):
                numbers.append(numbers[-1] + increment)
            increment += 2
    return sum(numbers)

This other answer already explains about the downsides of using a list to hold all the numbers and shows how to calculate the number at each corner. You can go even further and derive a mathematical formula for the total contributed by the four corners of an NxN square, then use that formula iterating from the central 1x1 square out to the final NxN.

   Hint: the last corner of an NxN square will have the value N2. The values of the other three corners can be derived from that value. The same formula also works for the central 1x1 square so you don't even need to have a special case for it.

ans = spiral_diag_sum(1001)
elapsed_time = (timer() - start) * 100 # s --> ms
print "Found %d in %r ms." % (ans, elapsed_time)

Following from some things I mentioned earlier: making this into a module that you can use in later problems, checking for a valid return value from spiral_diag_sum, and keeping all the timing code together:

if __name__ == "__main__":       # Allows standalone use or as a library module.
    start = timer()              # This code moved from above.
    ans = spiral_diag_sum(2)
    elapsed_time = (timer() - start) * 100 # s --> ms
    if ans:                      # Check for valid answer (or use try / except)
        print "Found %d in %r ms." % (ans, elapsed_time)
    else:
        print "No answer returned"


Join two arrays into a single one I put together a function to sort out values that I am getting from a curl request for different 'devices', it works as is, I am only wondering if there is an easier way to to this.

function convert($deviceName, $namesArray, $devicesArray) {
    $result = array();
    foreach ($devicesArray as $d) {
        $result [] = [
            'Device Type' => $deviceName,
            'Device' => $d[0],
            $namesArray[0] => $d[1],
            $namesArray[1] => $d[2],
            $namesArray[2] => $d[3],
            $namesArray[3] => $d[4],
            $namesArray[4] => $d[5],
            $namesArray[5] => $d[6],
            $namesArray[6] => $d[7],
            $namesArray[7] => $d[8],
            $namesArray[8] => $d[9],
            $namesArray[9] => $d[10]
        ];
    }
    return $result;
}

it takes in the following:

$namesArray = [ Operating System, Physical Size, Physical Size, Width, Height, Device-width, Px Per Inch, Px Density, Aspect Ratio, Popularity];

$devicesArray =
Array
(

    [0] => Array
            (
            [0] => iPhone 4 (4, 4S)
            [1] => iOS
            [2] => 3.5
            [3] => 8.9
            [4] => 640
            [5] => 960
            [6] => 320
            [7] => 326
            [8] => 200% XHDPI
            [9] => 2 : 3
            [10] => 8.8
            )

    [1] => Array
            (
            [0] => iPhone 5 (5c, 5s)
            [1] => iOS
            [2] => 4.0
            [3] => 10.0
            [4] => 640
            [5] => 1136
            [6] => 320
            [7] => 326
            [8] => 200% XHDPI
            [9] => 40 : 71
            [10] => 7.9
            )
)

 and $deviceName field which can be 'Phone', 'Tablet' or 'Monitor'.

This gives the output

[0] => Array
        (
            [Device Type] => Phone
            [Device] => iPhone 4 (4, 4S)
            [Operating System] => iOS
            [Physical Size] => 8.9
            [Width] => 640
            [Height] => 960
            [Device-width] => 320
            [Px Per Inch] => 326
            [Px Density] => 200% XHDPI
            [Aspect Ratio] => 2 : 3
            [Popularity] => 8.8
        )

    [1] => Array
        (
            [Device Type] => Phone
            [Device] => iPhone 5 (5c, 5s)
            [Operating System] => iOS
            [Physical Size] => 10.0
            [Width] => 640
            [Height] => 1136
            [Device-width] => 320
            [Px Per Inch] => 326
            [Px Density] => 200% XHDPI
            [Aspect Ratio] => 40 : 71
            [Popularity] => 7.9
        )

Change $devicesArray to $devices.  It is obvious enough that each device would contain an array of information.
Change $namesArray to $deviceFields it is more meaningful.
Change $deviceName to $deviceType. $deviceName seems to be misleading.
Use $device instead of $d in the foreach loop, it is more meaningful.
'Device' is a field and should be the first field in $deviceFields. This is required for the following improvement.
Use array functions to combine the arrays:
array('Device Type' => $deviceType]) + array_combine($deviceFields, $device).

The function then becomes:

function convert($deviceType, $deviceFields, $devices) {
    $result = [];

    foreach ($devices as $device) {
        $result[] =
            ['Device Type' => $deviceType] +
            array_combine($deviceFields, $device);
    }

    return $result;
}


Checking for existence of object by id, name, display name, or web id I have the following code:
public final boolean doesExistById(Long id) {
    return dataAccessObject.findById(id) != null;
}

public final boolean doesExistByName(String name) {
    return dataAccessObject.findByName(name) != null;
}

public final boolean doesExistByDisplayName(String displayName) {
    return dataAccessObject.findByDisplayName(displayName) != null;
}

public final boolean doesExistByWebId(String webId) {
    return dataAccessObject.findByWebId(webId) != null;
}

My Product class has properties id, name, displayName, wedId.
dataAccessObject.findBy____() returns an object of type Product, if it can be found in the data store, or null if it cannot.
I would like to reduce this chunk of code, if possible, because I have many objects that require the doesExist() pattern as above. The client code will only know one of these properties.

A possible solution I thought of would be to do this:
public final boolean doesExist(Long id, String name, String displayName, String webId) {..}

and then call it with null for unknown fields while using if statements to determine which field has a value. But is there another way that is more elegant?

One of the answers I got over was this, from user StriplingWarrior:

You are recognizing that the "does exist" part of all of these methods is exactly the same, and that makes you want to avoid repeating it, but the "ByXxx" part of these methods is completely different.
What you've got is far better than what you're thinking of doing. Please don't change your method signature to require callers to provide null values for all but one argument. That is highly error-prone, as it provides no compile-time errors for a variety of different ways that people might use the method signature incorrectly.
One thing you might want to consider doing is separating the "does exist" piece of this into its own mechanism:
public final Optional<MyObj> byId(Long id) {
    return Optional.ofNullable(dataAccessObject.findById(id));
}

So instead of saying service.doesExistById(123), you'd say service.byId(123).isPresent(). This represents the same meaning semantically, but it's broken into separate parts, which means you can reuse byId(), byName(), etc., for other purposes where you need the actual object, and not just to know whether it exists.

My ultimate question is, aside from the current solution, is there another way to refactor this code block so I don't have so much repeated similar code?

To me such a method

public final boolean doesExistById(Long id) {
    return dataAccessObject.findById(id) != null;
}

is already a code smell. What do you expect it to be used for. Something like

if (doesExistById(id)) {
    return getById(id);
} else {
    return new Something(id);
}

which means you're doing the lookup twice in the first branch. This is not too bad as there's caching somewhere, but it's strange. So I'd prefer to have no such method.

The other thing is that all it does id delegating to the dataAccessObject. Can't you use it directly. It looks like instead of calling

 dataAccessObject.findById(id)

you call

 somethingContainingTheDataAccessObject.findById(id)

Does it make sense? It might, but without more code we can't tell.

It looks like a typical XY problem. You're asking how to improve X (the repetitive doesExist* while having a problem Y (kept secret).

So all we can do is to tell you what not to do.

public final boolean doesExist(Long id, String name, String displayName, String webId) {..}

This could be a good idea if you needed a combined search often. But you're speaking about many methods and converting them into one method with many arguments is a step obviously in the wrong direction. It's much longer on the call site, much more error-prone and slightly less efficient. No gain.

public final Optional<MyObj> byId(Long id) {
    return Optional.ofNullable(dataAccessObject.findById(id));
}

My favorite rant says it all, though in this case it mightn't be that bad. You could use something like

public final Something getOrCreateById(Long id, Function<Long, Something> creator) {
    Something result = dataAccessObject.findById(id);
    return result != null ? result : creator.apply(id);
}

It wouldn't reduce the number of needed methods, but it could make them exactly what you need (remember, Y is kept secret).

With Java 8, it could get pretty compact.


Is there a better design pattern for a SDK? At work I decided it would be better to reverse engineer a SDK for one of our pieces of hardware. Because it is work related I can't share the exact code.. but I can give enough of a gist of the code with the following question in mind. Is there a design pattern that better reflects what I want to do?

I'm going to describe what the endgame is, then lay the code out with how to use it first, how it is implemented, and what I was thinking about for a change.

The end game is that the device I have communicates via RS232. I write a byte array to it to have it do things for me, it responds back with a similar structure. (The response is not of concern though). So what I have done is created a rather large factory class (not exactly a factory pattern, but I couldn't think of a better name). The code in small part looks similar to this

public class Command
{
    public ushort DeviceId{get;private set;}
    public byte CommandId {get; private set;}
    public byte[] Data {get; private set;}

    public Command(ushort deviceId, byte commandId, params byte[] data)
    {
        //assign to properties
    }
}
public static class CommandAdapter
{
    public static byte[] ToArray(this Command command)
    {
        //build byte array to match format needed
        return buffer;
    }
}

public class Device
{
    private ushort deviceId;
    private SerialPort devicePort;
    public Device(string comPort)
    {
        devicePort = new SerialPort(comPort, 115200);
        byte[] response = TransceiveCommand(CommandFactory.GetDeviceId());
        deviceId = BitConverter.ToUInt16(response, 0);
    }
    public byte[] FooBar()
    {
        byte mode = 1;
        byte option = 0x52;
        Command cmd = CommandFactory.DoFooBar(deviceId, mode, option)
        return TransceiveCommand(cmd);
    }

    private byte[] TransceiveCommand(Command cmd)
    {
        byte[] buffer = cmd.ToArray();
        devicePort.Write(buffer, 0, buffer.Length);
        byte[] response = new DeviceResponseParser.GetResponse(devicePort);
        return response;
    }
}

and I think that this code isn't so bad. it's the factory that I don't care much for.

public static class CommandFactory
{
    public static Command GetDeviceId()
    {
        return new Command(0, GET_DEVICE_CMD_ID);
    }

    public static Command DoFooBar(ushort deviceId, byte mode, byte option)
    {
        return new Command(deviceId, FOOBAR_CMD_ID, mode, option);
    }

    public static Command Another(ushort deviceId, byte[] data)
    {
        return new Command(deviceId, ANOTHER_CMD_ID, data);
    }

    public static Command SetSerialNumber(ushort deviceId, string newSerial)
    {
        byte[] array = new byte[newSerial.Length + 2]; //1 for 0x12, 1 for length
        array[0] = 0x12; //not sure what the 0x12 is, but it is required
        array[1] = (byte)(newSerial.Length); //+1 for null-termination
        for (int i = 0; i < newSerial.Length; i++) { array[i + 2] = (byte)newSerial[i]; }
        return new Command(deviceId, SET_SERIAL_CMD_ID, array);
    }
    //..~30 more commands

    private const byte GET_DEVICE_CMD_ID = 0x01;
    private const byte FOOBAR_CMD_ID = 0x02;
    //.. ~30 more command id's
}

In the end the commands are not going to change, so I've considered just using a byte array instead of a whole class for command. I don't like the idea of making an extension method for a byte array which makes me second guess doing that. Every command except for GetDeviceId takes in as it's first parameter the device id. So I wrote down on paper an idea about writing something like CommandFactory.CreateCommandFor(deviceId).FooBar()

or I've also thought about making a non-static Command factory pass in the deviceId and have it send back commands. Then I wouldn't have to pass in the device Id each time. Are there any design patterns that would better match what I'm trying to acheive?

I see this as a good design. As for now I would change only those two things and consider changing one more:

CommandAdapter seems to be unnecessary separation of logic here. I would just move it as instance method of Command (CommandAdapter is being removed),
Since Device represents low-level communication with hardware via RS232 - seems like something unmanaged going there and in fact SerialPort is IDisposable so - Device should be either:

public class Device : IDisposable
{
    private ushort deviceId;
    private SerialPort devicePort;
    public Device(string comPort)
    {
        devicePort = new SerialPort(comPort, 115200);
        // ...
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            devicePort.Dispose();
        }
    }

    //...
}

so now we have clear IDisposable semantics:

using (var device = new Device("COM1"))
{
    // ...
}

I am not sure whether Command is used somewhere else, but currently it seems to be a class that is created just to call ToArray on it. It may be inefficent, it depends how many times it is called etc. So consider reducing Command and pass byte[] representing command directly:

public class Command
{
    internal static byte[] Format(ushort deviceId, byte commandId, params byte[] data)
    {
        //build byte array to match format needed
        return new byte[] { };
    }
}

public static class CommandFactory
{
    public static byte[] GetDeviceId()
    {
        return Command.Format(0, GET_DEVICE_CMD_ID);
    }

    public static byte[] DoFooBar(ushort deviceId, byte mode, byte option)
    {
        return Command.Format(deviceId, FOOBAR_CMD_ID, mode, option);
    }

    // ...
}

public class Device : IDisposable
{
    // ...

    public byte[] FooBar()
    {
        byte mode = 1;
        byte option = 0x52;
        byte[] cmd = CommandFactory.DoFooBar(deviceId, mode, option);
        return TransceiveCommand(cmd);
    }

    private byte[] TransceiveCommand(byte[] cmd)
    {
        devicePort.Write(cmd, 0, cmd.Length);
        byte[] response = DeviceResponseParser.GetResponse(devicePort);
        return response;
    }
}

In this case you can of course merge Command.Format into CommandFactory. This makes much less sophisticated design but if Command do not any additional value - maybe worth consideration.


Neural Network Simulator with OpenMP I wrote a simple neural network simulator (the biophysical kind) from scratch, and was hoping to get some feedback on how I can speed things up, or any C++ / compilation best practices that I can improve on.

The code is at this repository.

Main Problem:

OpenMP doesn't seem to be conferring speedup.

The performance-critical section of the code is in src/networks/spikingnet.cpp, but for additional context, see the rest of the code in the repository.

#pragma omp parallel for
for (size_t li=0; li < nL; ++li) {
    SpikingLayer *layer = net->layers[li];
    Stim *stim = rs->stimuli[li];
    boolvec doSpike = stim->yield();
    conn_vec pre_arr = net->pre[li];
    updateLayer(layer, pre_arr, doSpike, t);
    recordSpikes(results.mutable_spikes(li), layer, i);
}

// update transmission & stdp
#pragma omp parallel for
for (size_t li=0; li < nL; ++li) {
    SpikingLayer *layer = net->layers[li];

    // transmission
    for (SpikingConnection *conn : net->post[li]) {
        for (SpikingSynapse* syn : conn->synapses) {
            updateTransmission(syn, layer->units[syn->s]);
        }
    }

    // STDP
    for (SpikingConnection *conn : net->pre[li]) {
        SpikingLayer *source = net->layers[conn->s];
        SpikingLayer *target = net->layers[conn->t];
        if (conn->stdp_enabled) {
            #pragma omp parallel for
            for (SpikingSynapse* syn : conn->synapses) {
                updateSTDP(syn, source->units[syn->s], target->units[syn->t]);
            }
        }
    } // end STDP

} // end for

GProf Trace

Architectural details:

The network consists of L=9 "layers", each with 100-900 units. There are on the order of L^2 "connections" (bundles of synapses between layers), and 2000 synapses per "connection" (synapses are sparse).

During each update cycle, all the layers (neurons) are updated (conditioned on connections), and then all the connections (synapses) are updated (conditioned on layers). That is to say, layer updating is independent conditioned on connections, and connection updating is independent conditioned on layers.

Given that there are so many neurons and synapses, the program naturally spends most of its time updating layers and synapses (line 133). I thought that using OpenMP over the per-layer loop, or even per-neuron/synapse, would really speed things up, but that does not seem to be the case at all.

If it's of any interest to the reader, my machine has a 4.0Ghz CPU with 8 cores. 12K steps run in approximately 30 seconds on either single-threaded or OpenMP-enabled builds.

I'm aware that I should be using smart pointers, but memory management is fairly straightforward in this simple simulator, so I chose to live dangerously.

General C++ programming style tips / pointing out my bad practices are also welcome!

It is generally advisable to parallelize loops at the absolute outermost level possible. Creating new OS threads, dividing up the relevant loop, and allocating data in the private address space for each thread all take up time, as does synchronization once the parallel task is over. Often, this overhead can outweigh any benefit that parallelism might have conferred in the first place. Whenever you use OpenMP to parallelize a task or loop, it has to be large enough to amortize the cost of spawning threads.

I would hoist as many parallel directives to the outermost loop as possible, and avoid having nested parallel directives. You may have to switch from using omp parallel for to just using omp parallel at the outermost level, then having some logic to explicitly decide which part of the data each thread works on depending on its thread number.

You'll have to be the judge of whether this is actually the problem or not; I only did a shallow read of your code, and the work that gets parallelized in the inner loops might actually be large enough that this is not the consideration at all.

Finally, there are alternatives to OpenMP for shared-memory parallel programming, e.g. Intel's Threading Building Blocks. OpenMP is pretty crude in the grand scheme of things, so you might find task-based parallelism easier or more fun to work with. You get to pretend like you have as many parallel threads of execution as you want, and the library manages farming out the tasks you define to the existing OS threads.


Method that returns an integer chosen by user Can I make this:

if (oneFour == 1 || oneFour == 2 || oneFour == 3 || oneFour == 4)

shorter, especially if I'm gonna need a wider range of integers?
Do I need to use a string _warning twice like I did?

static string _warning = "Please, enter a number from 1 to 4!";

static int oneToFour()
{
    int oneFour = 0;
    bool isError = true;
    while (isError)
    {
        try
        {

            oneFour = Convert.ToInt32(Console.ReadLine());
            if (oneFour == 1 || oneFour == 2 || oneFour == 3 || oneFour == 4)
                isError = false;
            else
                Console.WriteLine(_warning);
        }
        catch { Console.WriteLine(_warning); }
    }

    return oneFour;
}

You should start using comparison operators,

for a range from 1 to 4, use this:

if (oneFour >= 1 && oneFour <= 4)

This reads naturally as "if variable is more than or equal to 1 and variable is less then or equal to 4", which is the same thing as "if variable is in range 1 to 4".

Other improvements:

Instead of setting the isError value, you can return oneFour; directly.
Using the above advice, you can change your loop to while (true), eliminating your isError totally.
Using the above advice, you can make the scope for the oneFour variable smaller, which is a good thing (when possible).
Braces. Always use braces.

Resulting code will look something like this:

static int oneToFour()
{
    while (true)
    {
        try
        {

            int oneFour = Convert.ToInt32(Console.ReadLine());
            if (oneFour == 1 || oneFour == 2 || oneFour == 3 || oneFour == 4)
            {
                return oneFour;
            }
            else
            {
                Console.WriteLine(_warning);
            }
        }
        catch { Console.WriteLine(_warning); }
    }
}


Matlab - Fastest way to parallel read many files line by line? I have a few hundred files in a directory that I need to read in parallel. Namely, the files that have the same name, but different file extensions (.a and .b), but they're pretty much plain text files. The corresponding .a and .b files have the same number of lines. I need to read each line into cell arrays x and y until all each pair of files in the directory have been read. Currently, I'm doing it with a nested loop which works fine, but just takes too long. Is there any way to do this faster?

x = {};
y = {};
dir1 = dir( [ directory, filesep, '*', 'a'] );
dir2 = dir( [ directory, filesep, '*', 'b'] );
i = 1;
for i=1:length(dir1)
    fid1 = fopen([directory, filesep, dir1(i).name]);
    fid2 = fopen([directory, filesep, dir2(i).name]);
    sentence1 = fgetl(fid1);
    sentence2 = fgetl(fid2);
    while ischar(sentence)
        x{i} = sentence1;
        y{i} = sentence2;
        i = i+1;
        sentence1 = fgetl(fid1);
        sentence2 = fgetl(fid2);
    end
    fclose(fid1);
    fclose(fid2);
end

Performing this in parallel using PARFOR should be straightforward if you have the Parallel Computing Toolbox installed. Whether it goes any faster or not depends entirely on the size of your data, the speed of your disks etc. Here's how I would approach it:

function [x, y] = readOnePairOfFiles(name1, name2)
   x = {}; y = {};
   fid1 = fopen(name1); fid2 = fopen(name2);
   %# Read from fid1 and fid2, build up x and y
end

% Use PARFOR to invoke readOnePairOfFiles
x = {}; y = {};
parfor ii = 1:length(dir)
   name1 = fullfile(directory, dir1(ii).name);
   name2 = fullfile(directory, dir2(ii).name);
   [thisX, thisY] = readOnePairOfFiles(name1, name2);
   x = [x, thisX]; %# concatenation reduction to get x and y
   y = [y, thisY];
end


Casting base to derived class according to a type flag I have been writing an event class for my game engine and I came across to the following problem:

Is casting a base class object to a derived class object given a type flag a good programming design?

Let me give you the following simple example.  I have this enum holding the categories of the events:

enum DisplayEventType
{
    DET_UNSET,
    DET_WINDOW_EVENT,
    DET_KEYBOARD_EVENT,
    DET_MOUSE_EVENT,
    DET_JOYSTICK_EVENT
};

This is the base class of all the events:

typedef std::unique_ptr<DisplayEventData> DisplayEventDataMP;

class DisplayEvent
{
    private:

        // The type of the display event
        DisplayEventType mType;

        // The data of the event
        DisplayEventDataMP mEventData;

    public:

        // Constructor
        DisplayEvent(DisplayEventType type = DisplayEventType::DET_UNSET, DisplayEventDataMP eventData = std::unique_ptr<DisplayEventData>((DisplayEventData*) 0));

        // Retrieves the event type of the current event
        DisplayEventType GetType() const;

        // Sets the event type of the current event
        void SetType(DisplayEventType type);

        // Retrieves a raw pointer to the event data of the current event
        DisplayEventData* GetEventData() const;

        // Sets the event data of the current event
        void SetEventData(DisplayEventDataMP eventData);
};

This is the base class of the event data:

class DisplayEventData
{
    public:

        // Virtual destructor, for the deallocation of the interface implementors to work
        virtual ~DisplayEventData();
};

And these are the derived classes

class WindowEventData : public DisplayEventData
{
    // Window event data specific data and methods
};

class KeyboardEventData : public DisplayEventData
{
    // Keyboard event data specific data and methods
};

class MouseEventData : public DisplayEventData
{
    // Mouse event data specific data and methods
};

class JoystickEventData : public DisplayEventData
{
    // Joystick event specific data and methods
};

Some basic usage would be this:

// Poll for next event in the queue
DisplayEvent ev;
display->PollEvent(ev); // Fills ev with the needed data

if (ev.GetType() == DisplayEventType::DET_KEYBOARD_EVENT)
{
    KeyboardEventData* kev = static_cast<KeyboardEventData>(ev.GetEventData());
    // Do stuff
}

Is the above design viable/correct? If not what would a better alternative be in terms of object oriented or even generic programming?

Is casting a base class object to a derived class object given a type flag a good programming design?

Usually no.
It usually indicates a badly defined interface in your base class.

A better way to do it will depend on what you want you want to do with it. If you can define an abstract interface with virtual functions that would be the normal way of solving this problem.

But you seem to be using DisplayEventData as simply a bag of properties so maybe there is no generic interface. In which case I would go with a union (but that choice could change quickly based on other information).

struct DisplayEventData   // struct because it is a property bag not a real class.
{
     DisplayEventType   type;
     union
     {
            WindowEventData   win;
            KeyboardEventData key;
            MouseEventData    mouse;
            JoystickEventData joy;
     } data;
};

PS:

This is the wrong cast:

KeyboardEventData* kev = static_cast<KeyboardEventData>(ev.GetEventData());

You should be using dynamic_cast


Recognise numbers 1 to 6 in various notations I'm embarrassed I spent so much time on this seemingly simple but tricky coding exercise.
The task is this: input is various strings - digits, characters, non-English characters, e.g. some could be Cyrillic. The input must be split into words and then each word must be interpreted numerically if possible.
It should handle actual digits from 1 to 6 as valid numbers. All others should be printed as is.
Valid input includes positive, int or float type numbers 1 to 6, such as the following:

1,2,3,4,5,6
+1, +2, ... , +6
1.0, 2.0,... 6.0
any scientific notation that represent numbers 1 to 6.

if number is 1 - print "rule1" , 2- "rule2" etc.
Special case is if input number can be interpreted as 0, or any other variation of 0: .0 , 0.0000 or 0.e00. It prints all 6 - "rule1, rule2,.... rule6".
Invalid input includes negative numbers, they should be printed as-is:
-1 prints '-1', -2 prints -2.

any digits in scientific notation that are not in [1 .. 6] should be printed as is.
any single char or string, English and non-English (like dot) is printed as-is; 'nine' is printed 'nine' and "ывъхзъх" is printed "ывъхзъх".

Sample input:
1.0  03

 +5

 2.0e0
+.4e1
6.E+00
20e-1 2e1 2ва . 1e-307

expected output:
rule1
rule3
rule5
rule2
rule4
rule6
rule2
2e1
2ва
.
1e-307

Here is my code.  I'm looking to make it more elegant.
from math import modf
import fileinput

rule1 = 'rule1'
rule2 = 'rule2'
rule3 = 'rule3'
rule4 = 'rule4'
rule5 = 'rule5'
rule6 = 'rule6'

def print_rule(n):
    if n == 1:
        print(rule1)
    elif n == 2:
        print(rule2)
    elif n == 3:
        print(rule3)
    elif n == 4:
        print(rule4)
    elif n == 5:
        print(rule5)
    elif n == 6:
        print(rule6)

def print_all():
    print(rule1)
    print(rule2)
    print(rule3)
    print(rule4)
    print(rule5)
    print(rule6)

def get_num(s):
    try:
        s = int(s)

        if s == 0:
            print_all()

        elif s in [1, 2, 3, 4, 5, 6]:
            print_rule(s)
        elif s < 1 or s > 6:
            print(s)
    except ValueError:
        try:
            saved = s
            s = float(s)
            if s == 0:
                print_all()
            elif s < 1 or s > 6:
                print(saved)

            else:
                s = float(s)
                s = check_float(s)
                if s == 0:
                    print_all()
                elif s in [1, 2, 3, 4, 5, 6]:
                    print_rule(s)
                else:
                    print(s)

        except ValueError:
            print(s)

def check_float(n):
    frac, _ = modf(n)
    if frac != 0:
        return n
    else:
        return n

def test2():

    for line in fileinput.input():
        line = line.strip().split()

        for item in line:
            get_num(item)

if __name__ == "__main__":

    test2()

Your code for print all and print rule is needlessly verbose. You can convert the float into a integer and fraction part - like you did in check_float but never used. And then just check if the integer is in the bounds and that there is no fraction part.
def print_rule(number):
    integer, fraction = divmod(number, 0)
    if 1 <= integer <= 6 and not fraction:
        print(f"rule{int(number)}")

Print all can also just be a for loop.
def print_all():
    for number in range(1, 7):
        print(f"rule{number}")

The programming challenge is bad. The challenge is just complicated and handling 0 differently to 1-6 is just a gimmick. I'd suggest using a different site, because the challenge is figuring out the challenge is just poorly worded.
When you get a programming challenge that says "this is ok, this is ok, this is ok" and what is ok is constantly being expanded on then the recommended approach is normally to regex. However we can think of it in a more general way; split the code into three parts:

Extracting raw data.
You start with building a means to extract what you need from the input. If the challenge says not to allow something this is a problem for later.
This is normally done with regex but sometimes using plain Python can be advantageous.

Evaluate the raw data.
Here we're converting the raw data into something we can use.
Here this is just using float, but sometimes this can be a bit more hands on.

Validate the data.
Here you're coding the "if number == 0 ...". Since we've parsed the data this makes the code much simpler.

Your code bundles all this together.
The programming challenge can be reworded to be something far simpler.

Split the input by whitespace into segments.
for segment in TEXT.split():

Evaluate the number from scientific notation; if not possible output the segment.
try:
    number = float(segment)
except ValueError:
    print(segment)
else:
    # ...

If the evaluated number is 1, 2, 3, 4, 5 or 6 then output "rule{number}".
If the evaluated number is 0 output "rule1", "rule2", ... "rule6".
if number in range(0, 7):
    numbers = range(1, 7) if number == 0 else [int(number)]
    for number in numbers:
        print(f"rule{number}")

Otherwise output the segment.
print(segment)

Then the challenge just becomes as simple benign for loop with some horrible conditions. If value is this and value is this and value is this... And ultimately forces you to use the arrow anti-pattern to have clean code. Not a fan.
TEXT = """\
1.0  03

 +5

 2.0e0
+.4e1
6.E+00
20e-1 2e1 2ва . 1e-307\
"""

def main():
    for segment in TEXT.split():
        try:
            number = float(segment)
        except ValueError:
            pass
        else:
            if number in range(0, 7):
                numbers = range(1, 7) if number == 0 else [int(number)]
                for number in numbers:
                    print(f"rule{number}")
                continue
        print(segment)

if __name__ == "__main__":
    main()

If you think just calling float is cheating then you can manually extract and evaluate the number.
To extract we should turn to regex. We can build up from the items in the challenge description.

1,2,3 ,4,5,6

[0-6]

+1, +2, ... +6

\+?[0-6]

1.0, 2.0,... 6.0

\+?[0-6](?:\.0)?

Negative numbers should simply be printed as-is: -1 prints '-1'

[\-\+]?[0-6](?:\.0)?

any scientific notation that represent numbers 1 to 6.

Firstly we now need to accept any digit not just 0 and 0-6, because +.4e1 = 4.
And the numbers can be any length, .04e2 = 4.
[\-\+]?(?:\d*\.\d*|\d+)

Then we need to get scientific notation, which is just smacking an e in-between two numbers.
{number}(?:[eE]{number})?

[\-\+]?(?:\d*\.\d*|\d+)(?:[eE][\-\+]?(?:\d*\.\d*|\d+))?

Since we want to check if the segment is completely a number then we can check that the match starts at the start and ends at the end too. Or use re.fullmatch
^[\-\+]?(?:\d*\.\d*|\d+)(?:[eE][\-\+]?(?:\d*\.\d*|\d+))?$

Evaluating a number in scientific notation is simple as we split out the base and exponent. We then convert both to floats and then put them in the equation \$b10^e\$, where b is base and e is exponent. Defaulting the exponent to 0 if it doesn't exist.
def scientific_float(number):
    base, _, exponent = number.partition("e")
    return float(base) * 10**float(exponent or "0")

This automatically handles +\d, -\d, .\d and \d. if you think this is cheating then you can make your own float function.
In all this shows the three stages
import re

# Extract
NUMBER = "[\-\+]?(?:\d*\.\d*|\d+)"
SCIENTIFIC_NUMBER = f"{NUMBER}(?:[eE]{NUMBER})?"
NUMBER_MATCHER = re.compile(SCIENTIFIC_NUMBER)

def scientific_float(number):  # Evaluate
    base, _, exponent = number.partition("e")
    return float(base) * 10**float(exponent or "0")

def main():
    for segment in TEXT.split():  # Extract
        if None is not (match := NUMBER_MATCHER.fullmatch(segment)):  # Extract + Validate
            try:
                number = scientific_float(match.group(0))  # Evaluate
            except ValueError:
                pass
            else:
                if number in range(0, 7):
                    numbers = range(1, 7) if number == 0 else [int(number)]
                    for number in numbers:
                        print(f"rule{number}")
                    continue
        print(segment)

if __name__ == "__main__":
    main()


Using Timer with Backgroundworker to ensure the doWork method is called I have a windows forms application in which a backgroundworker is called again and again.
I need to avoid concurrent access of the code in dowork method for the backgroundWorker; but also need to ensure that the code in the dowork method is called; hence I cannot simply avoid running the backgroundworker altogether if it is busy.

The code is provided below with detailed comments:
The code as it is works nicely; Please evaluate the code and this way of achieving the intended; Please let me know of any potential problems,code smells, design flaws or better way of doing this. Any kind of comments, answers will be of great help.

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
 {
  public partial class Form1 : Form
   {
    // this timer calls bgWorker again and again after regular intervals
    System.Windows.Forms.Timer tmrCallBgWorker;

    // this is our worker
    BackgroundWorker bgWorker;

    // this is the timer to make sure that worker gets called
    System.Threading.Timer tmrEnsureWorkerGetsCalled;

    // object used for safe access
    object lockObject = new object();

    public Form1()
    {
        InitializeComponent();
        // this timer calls bgWorker again and again after regular intervals
        tmrCallBgWorker = new System.Windows.Forms.Timer();
        tmrCallBgWorker.Tick += new EventHandler(tmrCallBgWorker_Tick);
        tmrCallBgWorker.Interval = 100;

        // this is our worker
        bgWorker = new BackgroundWorker();
        // work happens in this method
        bgWorker.DoWork += new DoWorkEventHandler(bg_DoWork);
        bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);

    }

    void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Complete");
    }

    void bg_DoWork(object sender, DoWorkEventArgs e)
    {
        // does a job like writing to serial communication, webservices etc
        System.Threading.Thread.Sleep(100);
    }

    void tmrCallBgWorker_Tick(object sender, EventArgs e)
    {
        if (Monitor.TryEnter(lockObject))
        {
            try
            {
                // if bgworker is not busy the call the worker
                if (!bgWorker.IsBusy)
                    bgWorker.RunWorkerAsync();
            }
            finally
            {
                Monitor.Exit(lockObject);
            }

        }
        else
        {

            // as the bgworker is busy we will start a timer that will try to call the bgworker again after some time
            tmrEnsureWorkerGetsCalled = new System.Threading.Timer(new TimerCallback(tmrEnsureWorkerGetsCalled_Callback), null, 0, 10);

        }

    }

    void tmrEnsureWorkerGetsCalled_Callback(object obj)
    {
        // this timer was started as the bgworker was busy before now it will try to call the bgworker again
        if (Monitor.TryEnter(lockObject))
        {
            try
            {
                if (!bgWorker.IsBusy)
                    bgWorker.RunWorkerAsync();
            }
            finally
            {
                Monitor.Exit(lockObject);
            }
            tmrEnsureWorkerGetsCalled = null;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        tmrCallBgWorker.Start();
    }
  }
}

I needed something similar, in that I had code that was going to periodically check the database for new records and notify the user with baloon tips.  Checking the database every n-secs caused a pause in the UI that I didn't want the user to experience so I moved the procedure to a thread.

I had a System.Windows.Forms.Timer tick event, where the Thread was created something like (leaving out all the exception handling etc):

TIMER_TICK(object sender, EventArgs){
    Thread checkNow=new Thread(ProcessAutoCheck)
    checkNow.IsBackground=true;
    checkNow.Start();
    checkNow.Join(500);
}

Then in the ProcessAutoCheck method, I used the lock approach to prevent thread contention:

**lock**(lockObject){
{
....

The program worked as designed, and I didn't consume resources with threads.  It wasn't the fanciest solution.  I learned a lot from Threading in C# - By Joseph Albahari.  Complete book online.


Calculating distance with Euclidean, Manhattan, and Chebyshev in C# I am by no means proficient at being able to take mathematical concepts and converting them to code. I was wondering if you guys could look at it and make suggestions on how I could fix it or if my thinking is correct on these. I used a combination of the wikipedia pages for each of these as well as http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html to come up with my solutions.

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

namespace Algorithms
{
    public static class Heuristics
    {
        // implementation for integer based Manhattan Distance
        public static int ManhattanDistance(int x1, int x2, int y1, int y2)
        {
            return Math.Abs(x1 - x2) + Math.Abs(y1 - y2);
        }

        // implementation for floating-point  Manhattan Distance
        public static float ManhattanDistance(float x1, float x2, float y1, float y2)
        {
            return Math.Abs(x1 - x2) + Math.Abs(y1 - y2);
        }

        // implementation for integer based Euclidean Distance
        public static int EuclideanDistance(int x1, int x2, int y1, int y2)
        {
            int square = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
            return square;
        }

        // implementation for floating-point EuclideanDistance
        public static float EuclideanDistance(float x1, float x2, float y1, float y2)
        {
            float square = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
            return square;
        }

        // implementation for integer based Chebyshev Distance
        public static int ChebyshevDistance(int dx, int dy)
        {
            // not quite sure if the math is correct here
            return 1 * (dx + dy) + (1 - 2 * 1) * (dx - dy);
        }

        // implementation for floating-point Chebyshev Distance
        public static float ChebyshevDistance(float dx, float dy)
        {
            // not quite sure if the math is correct here
            return 1 * (dx + dy) + (1 - 2 * 1) * (dx - dy);
        }
    }
}

Let's start with some housekeeping. You're not using these using so they should be removed:

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

Your namespace is far too generic - it's much better than ConsoleApplication1 but you should use something like <Company>.<Product>.<Feature> in your real app.

Your method names should be verb phrases ManhattanDistance should be CalculateManhattanDistance (and likewise for the others).

Why does your ChebyshevDistance take different parameters to the others? That seems unfriendly in the API design.

You should add documentation comments rather than simple comments to explain what a method does.

e.g.

/// <summary>
/// Calculates the Manhattan distance between the two points.
/// </summary>
/// <param name="x1">The first x coordinate.</param>
/// <param name="x2">The second x coordinate.</param>
/// <param name="y1">The first y coordinate.</param>
/// <param name="y2">The second y coordinate.</param>
/// <returns>The Manhattan distance between (x1, y1) and (x2, y2)</returns>
public static int CalculateManhattanDistance(int x1, int x2, int y1, int y2)
{
    return Math.Abs(x1 - x2) + Math.Abs(y1 - y2);
}

I'd also consider swapping the order of the parameters so you specify x1, y1, x2, y2 or using Point and PointF instead.

Now that's out of the way... ManhattanDistance looks correct.

EuclideanDistance is missing the final square root. The article you've linked to explains why that's bad:

  I’ve seen several A* web pages recommend that you avoid the expensive square root in the Euclidean distance by just using distance-squared
  [snipped pseudocode]
  Do not do this! This definitely runs into the scale problem. The scale of g and h need to match, because you’re adding them together to form f. When A* computes f(n) = g(n) + h(n), the square of distance will be much higher than the cost g and you will end up with an overestimating heuristic. For longer distances, this will approach the extreme of g(n) not contributing to f(n), and A* will degrade into Greedy Best-First-Search.

According to the Chebyshev distance Wikipedia article, in the Cartesian plane it's simply:

max(|x2 - x1|, |y2 - y1|)

Making it:

public static int CalculateChebyshevDistance(int x1, int x2, int y1, int y2)
{
    return Math.Max(Math.Abs(x2 - x1), Math.Abs(y2 - y1));
}

Although, following the article you linked your result should be:

public static int CalculateChebyshevDistance(int x1, int x2, int y1, int y2)
{
    var dx = Math.Abs(x2 - x1);
    var dy = Math.Abs(y2 - y1);
    return (dx + dy) - Math.Min(dx, dy);
}

I can't see how to prove that they're equivalent... If anyone can, I'd be interested to see it!

They are obviously equivalent... If you add the dx and dy together and then subtract the smaller of the two you are obviously left with just the bigger value i.e. the max of dx and dy.


Covert html to pdf, calculate the vertical space to the end of the last page faster I'm working on a custom puppeteer script which converts dynamic generated HTML to an A4 PDF.
This script converts HTML to a near perfect A4 PDF file, the only issue I have is speed. Document conversion is anywhere between 1-10 minutes.

I have tracked the slowness to the part where we calculate how much vertical space needs to be added to the last page
in order to have a full page. If we don't do this the background we have will be clipped.

Next up the relevant code, which is called as followed;

node convert.js "{\"url\":\"file://$(pwd)/generated.html\",\"options\":{\"path\":\"$(pwd)/generated.pdf\"}}"

async function addPadding(page) {
    await page.emulateMedia('print');

    await page.evaluate(
        _ => {
            let container = document.querySelector('#padding-container');

            if (container)
                container.style.height = '1145px'
        }
    );

    return Promise.resolve()
}

function getPageAmount(buffer) {
    let index = buffer.indexOf('/Count ');
    let string = buffer.slice(index + '/Count '.length, index + '/Count '.length + 32).toString();
    return ~~string.trim().split(/\n|\/|\[|\>/)[0]
}

convert.js

(async _ => {
    const browser = await puppeteer.launch({
        headless: true,
        args: [
            isDocker ? '--no-sandbox' : ''
        ],
        defaultViewport: {
            width: 724,
            height: 1145 * 32
        }
    });
    const page  = await browser.newPage();

    await page.emulateMedia('print');
    await page.goto(request.url, { waitUntil: 'networkidle0' });
    await page.setViewport({ width: 724, height: 1145 });

    let buffer = await render(page);
    let originalPages = getPageAmount(buffer);
    let maxPasses = 3;
    let passes = 0;

    await addPadding(page);

    buffer = await render(page);

    while(getPageAmount(buffer) !== originalPages && passes < maxPasses) {
        await page.evaluate(
            _ => {
                let paddingContainer = document.querySelector('#padding-container');
                let value = ~~paddingContainer.style.height.replace('px', '');
                paddingContainer.style.height = `${value / 2}px`
            }
        );

        buffer = await render(page);

        if (getPageAmount(buffer) === originalPages) {
            while (getPageAmount(buffer) === originalPages) {
                await page.evaluate(
                    _ => {
                        let paddingContainer = document.querySelector('#padding-container');
                        let value = ~~paddingContainer.style.height.replace('px', '');
                        paddingContainer.style.height = `${value * 1.5}px`
                    }
                );

                buffer = await render(page)
            }
        }

        passes++
    }

    while(getPageAmount(buffer) !== originalPages) {
        await page.evaluate(
            _ => {
                let paddingContainer = document.querySelector('#padding-container');
                let value = ~~paddingContainer.style.height.replace('px', '');
                paddingContainer.style.height = `${value - 1}px`
            }
        );

        buffer = await render(page)
    }

    if (request.options.path)
        fs.writeFileSync(request.options.path, buffer);

    browser.close()
})();

I really would appreciate any feedback/help on this.

Ways to optimize:

Nested loops

You got nested while loops in convert.js script and that requires a particular attention.The outer while loop contains the construction:

...
if (getPageAmount(buffer) === originalPages) {
    while (getPageAmount(buffer) === originalPages) {
...

where if conditional redundantly checks the condition getPageAmount(buffer) === originalPages whereas the underling while loop would check the same condition by itself. Therefore, remove the redundant if "wrapper".

getPageAmount function

Deserves a separate attention (frequently invoked function).

'/Count '. Many-times hardcoded search string '/Count ' begs for extracting into a variable

let searchStr = '/Count ';

index + '/Count '.length. Duplicated expression points to a starting offset for input buffer slicing. Worth to be a variable:

let pos = buffer.indexOf(searchStr);
let startOffset = pos + searchStr.length;
let str = buffer.slice(startOffset, startOffset + 32).toString();

splitting a string by pattern and get the 1st chunk (~~string.trim().split(/\n|\/|\[|\>/)[0]). What it does is splitting the input string by regex pattern /\n|\/|\[|\>/ into array of substrings. Though it creates a new array of strings/chunks in memory - whereas we only need the 1st leftmost chunk .[0].Instead, a much more efficient way is to just find the position of the 1st occurrence of the pattern and slice the input string to that point.That's achievable with String.search + String.slice combination and will go smashingly faster compared to the initial approach.
Eventually the optimized function would look as:

function getPageAmount(buffer) {
    let searchStr = '/Count ',
        pos = buffer.indexOf(searchStr),
        startOffset = pos + searchStr.length;
    let str = buffer.slice(startOffset, startOffset + 32).toString();
    return ~~str.trim().slice(0, str.search(/[\n\/\[\>]/))
}

DOM tree scanning

The "hero" of this section is document.querySelector('#padding-container') which appears in many places within while loops and queries the current document for a specific tag/element.
Such DOM queries become an expensive operations if used frequently, moreover - in massive traversals. Depending on markup complexity and "amount" of traversal such repetative queries may make the processing +50% slower.The solution here is to extract the reference to an element into a top-level variable and reference it in all needed places.

# top-level variables
...
let passes = 0;
let paddingContainer = document.querySelector('#padding-container');

Extracting "padding container" height

Expression ~~paddingContainer.style.height.replace('px', '') is duplicated in many places and is candidate for Extract function technique.Could be even defined as unified function for getting height for the element passed as parameter:

function getElHeight(el):
    return ~~el.style.height.replace('px', '')

...

    ...
    _ => {
        let padding_height = getElHeight(paddingContainer);
        paddingContainer.style.height = `${padding_height * 1.5}px`
    }


This program asks users to rank an item and validates the rank # get rank which takes the users rank as an argument and determines
# if it is valid input

def getRank(x):
    # make a list of the acceptable choices
    choices = ['l','m','h']
    # convert the users rank to lowercase
    x = x.lower()
    # create a while True loop that only runs if x is not in the choices
    while True:
        if x not in choices:
            # remind the user of the valid choices
            print('Only enter a valid choices')
            print(choices[0:3])
            x = input('Please enter a rank: ')
            x = x.lower()
            # add an if statement to continue the loop if the users new input is still incorrect
            if x not in choices:
                continue
        # if everything is all good break from the loop
        else:
            break
    return x

def main():
    print('Please enter a ranking for the items')
    print('l for low,m for medium,h for high')
    print('')
    print('Please enter a rank for milk')
    # prompt the user to enter their rate
    userRate = input('Please enter a rank: ')
    # validate their input
    validRank = getRank(userRate)
    # assign the users rating to validRank as it will only hold a valid choice
    # continue doing this for each item
    milkRate = validRank
    print('Please enter a rank for cheese')
    userRate = input('Please enter a rank: ')
    validRank = getRank(userRate)
    cheeseRank = validRank
    print('Please enter a rank for eggs')
    userRate = input('Please enter a rank: ')
    validRank = getRank(userRate)
    eggRank = validRank
    print('')
    #output the users ratings for the items
    print('Your overall ratings are:')
    print('Milk:',milkRate)
    print('Cheese:',cheeseRank)
    print('Eggs:',eggRank)

main()

Hi everyone! I'm a first year student and I would really love some tips to better my logic here. The above program does exactly what I want it to do: Display an item to the user, ask the user for their rank, validate that their rank is one of 3 choices case insensitive. If its invalid it requests reinput until it is. While this code works, I know there are oppurtunities for me to write better logic here.
I am open to all critiques here! Would just love feedback from the community

Your getRank function could use a little flexibility. If the user inputs low, their input is rejected as incorrect. To solve this you can take the first character of the string passed. Since you index the string at 0, input like l will still work.
Your validation loop could also use some work. Since you're only checking if the users choice is in the valid input list, make the while loop represent that!
def getRank(choice: str) -> str:
    choices = ['l', 'm', 'h']
    choice = choice.lower()[0]
    while choice not in choices:
        print(f'Only enter a valid choice ({choices})')
        choice = input('Please enter a rank: ').lower()[0]
    return choice


C++ memset / memcpy / strcpy implementation performance In C++, I have made a basic memset / memcpy / strcpy implementation, yet I am worried that they are slower than the STL equivalent, as they are probably made in assembly, which I do not know. Is there any way of making these quicker, still using C++?

#define BYTE unsigned char

typedef BYTE byte;

void *__memset(void *_Dst, int _Val, UINT _Size)
{
    BYTE *buf = (BYTE *)_Dst;
    while (_Size--)
    {
        *buf++ = (BYTE)_Val;
    }
    return _Dst;
}

void *__memcpy(void *_Dst, const void *_Src, UINT _Size)
{
    BYTE *buf = (BYTE *)_Dst;
    BYTE *__Src = (BYTE *)_Src;
    while (_Size--)
    {
        *buf++ = *__Src++;
    }
    return _Dst;
}

char *__strcpy(char *_Dst, const char *_Src)
{
    while ((*_Dst++ = *_Src++) != '\0');
    return _Dst;
}

These are Runtime library functions, not STL (template library) functions.

The run-time library is arguably part of the language (so you can use the built-in library functions and still be "using C++").

It's possible that your compiler is able to generate these as intrinsic functions.

Things you can try to make your functions faster:

Use a compiler with a better optimizer
Copy 4 or 8 bytes at a time
If the buffers aren't aligned on a 4- or 8-byte boundary, copy 1 byte at a time until you come to a boundary alignment, and then copy 4 or 8 bytes at a time


Creating SQL queries for a MySQL database I am writing a PHP application that works with a MySQL database.  As I am fairly new to PHP I am looking for ways to improve my code.  Below are two functions, create_ct_query and create_ct_query2.  create_ct_query2 is the result of my refactoring create_ct_query.  I am fairly happy with it now, but am wondering how it looks to others.

<?php
// experiment with versions of create_ct_query

$lvb_id_ml=30;       // lvb id
$lvb_name_ml=100;    // competition name

$lvb_competitions_table =
  array ("table_name" => "lvb_competitions",
         "table_cols" =>
         array (array ('id', $lvb_id_ml),
                array ('name', $lvb_name_ml),
                array ('age', 'INT')),
         "primary_key" => array ('id', 'age'));

function varchar ($len) {
  return "VARCHAR(" . $len . ")";
}

function primary_key ($item) {
  return $item . " PRIMARY KEY";
}

// version currently in script
function create_ct_query ($tdesc) {
  $ret_val = "CREATE TABLE " . $tdesc['table_name'] . " ( ";

  foreach ($tdesc["table_cols"] as $row) {
    if (is_numeric ($row[1])) {
      $ret_val .= $row[0] . " " . varchar ($row[1]) . ", ";
    } else {
      $ret_val .= $row[0] . " " . $row[1] . ", ";
    }
  }

  // add primary key constraint
  $ret_val .= " CONSTRAINT pk_" . $tdesc["table_name"] .
    " PRIMARY KEY (";
  if (is_array ($tdesc['primary_key'])) {
    $ret_val .= implode (",", $tdesc['primary_key']);
  } elseif (is_string ($tdesc['primary_key'])) {
    $ret_val .= $tdesc["primary_key"];
  } else {
    die ("Primary key not of right type in tdesc for " .
         $tdesc['table_name'] . "\n");
  }
  $ret_val .= "))";

  return $ret_val;
}

function create_ct_query2 ($tdesc) {
  $ret_val = "CREATE TABLE " . $tdesc['table_name'] . " (";

  $ret_val .= cols_desc ($tdesc['table_cols']) . ", ";

  $ret_val .= primary_key_constraint ($tdesc['table_name'],
                                      $tdesc['primary_key']) . "))";

  return $ret_val;
}

// create the item description from the item
// Example:
// item_desc (array ('name', 10));
//    'name VARCHAR(10)'
// item_desc (array ('name', "INT"));
//    'name INT'
function item_desc ($item) {
  return " " . $item[0] . " " .
    (is_numeric($item[1]) ? varchar($item[1]) : $item[1]);
}

// create the column description part of table creation query
// Example:
// cols_desc (array (array ('id', 10), array ('id2', INT)));
//   'id VARCHAR(10), id2 INT'
function cols_desc ($table_cols) {
  return implode (",", array_map ('item_desc', $table_cols));
}

// create the primary key constraint part of the table creation query
// primary_key_constraint ('name', 'id');
//    CONSTRIANT pk_name PRIMARY KEY (id)
// primary_key_constraint ('name', array ('id', 'id2'));
//    CONSTRAINT pk_name PRIMARY_KEY (id, id2)
function primary_key_constraint ($table_name, $primary_key_desc)  {
  return " CONSTRAINT pk_" . $table_name . " PRIMARY KEY (" .
    (is_array($primary_key_desc) ?
     implode (",", $primary_key_desc) :
     $primary_key_desc);
}

echo create_ct_query ($lvb_competitions_table) . "\n\n";
echo create_ct_query2 ($lvb_competitions_table) . "\n\n";

Assuming you're protecting yourself from SQL Injection and the like, there are just a couple things that stand out to me:

It doesn't seem clear to me why the closing parenthesis for your primary key constraint is in the create_ct_query2() function rather than in the primary_key_constraint() function.
I believe the code would be far easier to read, test, and extend if it were built using objects rather than global functions.
Since you are new to PHP, I think it is worth mentioning that single quotes and double quotes are treated differently.  Knowing this, as a personal preference, I tend to use single quotes unless I intentionally need / want to use the added functionality of double quotes.
While related to refactoring, I believe my other suggestions start to delve more in to the readability of things like create_ct_query().  This starts to feel a bit nit-picky, and almost a matter of style.  For example, with create_ct_query(), it took me a minute to realize what "ct" meant.  Only after looking inside the function and seeing the string being created did I realize what it did.  I feel that something like "get_create_table_query()" or similar would be a bit more intuitive.

Regarding documentation, I have found Eclipse and other tools to utilize the following style of doc-blocks.  The @return is especially useful for code-completion of methods available on the returning object in my IDE.  Documentation tools tend to have built-in support for other tags like @author as well.

/**
 * Description of someMethod()
 *
 * @param int $someInt
 * @param ObjectName $someObj
 * @return AnotherObj
 */
public function someMethod($someInt, ObjectName $someObj)
{
    // ...

    return new AnotherObj();
}


Pascal Triangle implementation in javascript I have been given an exam to implement Pascal Triangle in JavaScript,
I have seen that it somehow similar on fibonacci sequence.
With that I have come up with the solution below.

function pascalTriangle(n){
  var data = [[1],[1,1]];
  var rowNum = n - 2;
  for (var i = 1; i <= rowNum; i++){
   var curr = i;
    data.push([1]);
    for (var x = 0; x< data[curr].length - 1; x++){
      var calcMid = data[curr][x] + data[curr][x + 1];
      data[curr+1].push(calcMid);
    }
    data[curr+1].push(1);
  }
  console.log(data);
}
pascalTriangle(5);

What I did is that I first initialize the 1st and 2nd row of the Pascal Triangle and then do the calculation for the middle with the idea from fibonacci sequence.

I feel that the solution the I have come up can be improved.

Minor style points and an alternative solution.

With these types of problems the first port of call is always Wikipedia to get as much info as you can before you start on creating a solution.

Two things stand out about the solution.

The rows are symmetrical, the right side is a mirror of the left.
There is a formula that lets you calculate a row which can help reduce the complexity.

Your code

There is nothing majorly wrong with the code, the following are just general style points

Do the output outside the function.
Use const for variables that do not change.
Declare variables with var at the top of the function or use let to declare variables in the block they are to be used.
In nested loops try to avoid double indexing by creating a reference to the inner array outside the inner loops.
The function is not complete, as it does not return the correct results for pascalTriangle(0) and pascalTriangle(1) a simple if statement can take care of that.
Don't duplicate variables for no reason. The variable i (out loop iteration counter) is immediately copied to a variable curr and neither are changed inside the loop.
When writing we put a space after a comma, this make it easier to read then without,thus illustrated. The same with code, put spaces after commas so that it does not all blend together. Same for operators where the convention is a space either side. eg data[curr+1] should be data[curr + 1] and data = [[1],[1,1]]; should be data = [[1], [1, 1]];
(minor point as the problem is simple and solution sort) You could use some better names for some of the variables.

Rewrite

Rewriting your function using the above points.

function pascalTriangle1(rowCount) {
    if (rowCount === 0) { return [] }
    if (rowCount === 1) { return [[1]] }
    const rows = [[1], [1, 1]];
    var prevRow = rows[1];
    for (let n = 1; n < rowCount - 1; n += 1) {
        const row = [1];
        let val = prevRow[0];
        for (let k = 0; k < prevRow.length - 1; k += 1) {
            row.push(val + (val = prevRow[k + 1]));
        }
        row.push(1);
        rows.push(row);
        prevRow = row;
    }
    return rows;
}

Alternative solution

The following reduces the number of calculations to about half by exploiting the symmetry of each row.

Further improvements can be made by using Float64Array and pre-calculating the triangle size, then indexing directly into the array, avoiding the expensive array creation left and right and concatenation for each row. But that would only show any real benefit for very large triangles, where he need to hold all the rows would be questionable.

const pascalTriangle = rowCount => {
    const createRow = rowIndex => {
        var k = 1, val = 1;
        const n = rowIndex + 1;
        const left = [val];
        const right = [val];
        while (k < n / 2) {  // only do half the row
            val = val * (n - k) / k++;
            left.push(val);
            right.unshift(val);
        }
        if (n % 2) { right.shift() }  // when n is odd remove first right item
        return [...left, ...right];
    }
    const rows = [];
    if (rowCount > 0) {
        let i = 1;
        rows.push([1]);
        while (i < rowCount) {  rows.push(createRow(i++)) }
    }
    return rows;
}


Reconstructing Matrix scattered over many MPI ranks I have a program that does some matrix computations using MPI (MPICH). Each rank has a slice of the matrix and does the computations on their slice to get a new slice of the matrix. Sometimes I need to synchronize the matrix on the manager for snapshotting, output, etc. so I need all ranks to send their current matrix slice back to the manager.

I've come up with a solution to do this, and it seems to work OK; but I wanted to know whether this is a good approach or if I'm doing something stupid since I've never worked with MPI before. One thing I'm particularly curious about is whether I'm deleting my requests vector correctly. Thanks in advance!

Here's the code:

#define __WORLD MPI_COMM_WORLD

// reconstructs the matrix on the manager node
void reconstructMatrix(double** current, const int COLS, const int ROWS,
                       const int MY_RANK, const int MPI_SIZE)
{
    auto requests = new std::vector<MPI_Request*>();
    requests->reserve(COLS);

    // Manager receives data from all ranks
    if (MY_RANK == 0)
    {
        // number of columns assigned to all ranks
        const auto SLICE_SIZE = COLS / MPI_SIZE;

        // receive from rank 1 to rank n-1
        for (auto src = 1; src < MPI_SIZE; src++)
        {
            for (auto i = (SLICE_SIZE * src); (i < COLS) && (i < SLICE_SIZE * (src + 1)); i++)
            {
                auto temp = new MPI_Request();
                MPI_Irecv(current[i], ROWS, MPI_DOUBLE, src, 1, __WORLD, temp);
                requests->push_back(temp);
            }
        }
    }
    else // Workers send data to manager
    {
        // index 0 and COLS + 1 are ghost columns
        for (auto i = 1; i <= COLS; i++)
        {
            auto temp = new MPI_Request();
            MPI_Isend(current[i], ROWS, MPI_DOUBLE, 0, 1, __WORLD, temp);
            requests->push_back(temp);
        }
    }
    // wait for sync to finish
    waitAll(requests);

    requests->clear();
    delete requests;
    requests = nullptr;
}

// waits for all MPI requests and deletes the vector
void waitAll(std::vector<MPI_Request*>*& requests)
{
    for (auto& req : *requests)
    {
        MPI_Status stat;
        MPI_Wait(req, &stat);
        if (stat.MPI_ERROR != MPI_SUCCESS)
        {
            std::cerr << "MPI Request failed!\n\tCode: " << stat.MPI_ERROR
                      << "\n\tFrom: " << stat.MPI_SOURCE;
        }
        delete req;
    }
}

Note: I've written a matrix multiplication benchmark program in C and MPI:https://github.com/thefangbear/matrix-mpi. It uses a row-major format to store the two matrices in 1D arrays for better cache coherency. It also supports both synchronous and asynchronous send/receive modes, tunable via a preprocessor macro, so you can take a look.

With that being said, let's turn to a few improvable parts in your code:

You don't have to use an array of pointers to represent the matrix format. You can use an 1D array with row-major/column-major format. If you use a "2D array" (array of pointers), each row/colum slice of your matrix are not guaranteed to be stored in consecutive locations.
Isend/Irecv lead to degraded performance. You can verify this via benchmarking. MPI sends/receives extra messages when asynchronous mode is used, which makes basic operations more expensive. And since you have blocking anyways somewhere in your code, ISend/IRecv do not make a huge difference.
It is common for worker nodes to send back processed slices of data to be merged and outputted in the master node. You can technically just invoke printf in a loop on each worker node to output its own slice of data (since mpirun redirects all stdout streams on each node to your terminal screen), but MPI does not guarantee the order of output.
If you want a more elegant semantics, you should checkout MPI_Scatterv/MPI_Gatherv scatter/gather functions: https://www.mpich.org/static/docs/v3.1/www3/MPI_Scatterv.html. (But I don't think you'll get significant performance gains by using them)