What's wrong

for filename in $(ls -1 ./*); do

Ouch! As a general rule (with very few exceptions), do not use ls in scripts. What you wrote is almost equivalent to for filename in ./*; do, except that if there are any non-printable characters, whitespace characters, or \[?* in the file names, they will be mangled if you use ls. You don't need the ./ (except to ensure that the file name doesn't start with a -) but it doesn't hurt.

[[ $filename =~ _[0-9]\. ]]

could be written in a slightly simpler and more portable way: [[ $filename = *_[0-9].* ]]. And since you can use a simple glob pattern, you might as well not have iterated over all file names: for filename in *_[0-9].*; do. But there's a better way to express your script, to take advantage of BASH_REMATCH; see below.

echo $filename

Always put double quotes around variable and command substitutions. Exception: when you understand why you need to leave the double quotes off and why it's safe to do so. When the shell sees a variable substitution ($foo or ${foo}) or a command substitution (`foo` or $(foo)) outside double quotes, the result of the substitution undergoes word splitting and globbing (filename generation). That was one of the problems with $(ls -1 ./*) earlier. This should be echo "$filename".

In fact, it would be better printf "%s" "$filename", because echo itself performs expansions. In bash, unless you've set non-default options to enable backslash expansion, the only problem is that a few arguments beginning with a - look like option, and in this specific case the filename will begin with ./.

There is an edge case where your sed call won't work: if you have a file name that ends with a newline character. This doesn't happen in practice unless someone has made a mistake (like a rogue script or a bad copy-paste) or is deliberately trying to trick your script — so watch out in security contexts.

Incidentally, this is one of the few cases where it's safe to leave out the double quotes around a command substitution: in a variable assignment, there is an implicit pair of double quotes around the right-hand side, so new_filename=$(…) is equivalent to new_filename="$(…)". Note that this does not extend to export VARIABLE="$(value)", where the double quotes are necessary.

We now turn to your question about the use of sed. It is not necessary here; you can perform this substitution in bash. Bash has a pattern replacement parameter substitution feature, but it's limited to a constant replacement text. Bash also has a way to extract substrings from regexp matches with =~, through the BASH_REMATCH variable. After the match, ${BASH_REMATCH[0]} contains the portion of the string matched by the regexp, ${BASH_REMATCH[1]} contains the portion matched by the first parenthesized group and so on.

mv $filename $new_filename

Again, double quotes.

A working script

One possibility is to extract the text to replace from the regexp match, then perform a string replacement on it. Since I'm not using ./*, the file name may begin with a -, so I take care to use -- on the call to mv to ensure that the file name isn't seen as an option.

for filename in *; do
  if [[ $filename =~ _[0-9]\. ]]; then
    from=${BASH_REMATCH[0]}
    mv -- "$filename" "${filename//$from/${from/_/_0}}"
  fi
done

Another possibility is to match the whole file name as a regexp and splice a 0 into the bits. Note that this will behave differently in a corner case: if there are several occurrences of _[0-9]\., the code above replaces the first occurrence, while this replaces the last occurrence.

for filename in *; do
  if [[ $filename =~ ^(.*_)([0-9]\..*)$ ]]; then
    mv -- "$filename" "${BASH_REMATCH[1]}0${BASH_REMATCH[2]}"
  fi
done

You can also use other constructs and avoid regexps altogether, but it's more complicated. An advantage is that the script then works in all shells, not just bash.

for filename in *_[0-9].*; do
  digit=${filename%.${filename##*_[0-9].}}; digit=${digit##*.}
  mv -- "$filename" "${filename%%_[0-9].*}_0${digit}.${filename#*_[0-9].*}"
done

There are other methods to perform this kind of file renaming. If you're on Debian or derivative (including Ubuntu) or have the rename Perl script that's floating around (note that this is not the standard Linux rename utility):

rename 's/_([0-9]\.)/_0$1/' *_[0-9].*

Or with mmv:

mmv -x '*_[0-9].*' '#1_0#2.#3'

Or in zsh, after autoload zmv:

zmv '(*_)([0-9].*)' '${1}0$2'

Further reading

The rename and quoting tags on Unix Stack Exchange.


You are using function as definition for model and inside that function you are defined an interface to have access to those properties/methods. In complicated cases you would have inflexible usage between definition and public interface. Better solution would be to create definition for the class like this

var Model = function () {
   var self = this;
   self.selectedItems = ko.computed(function () {
        return _.filter(items(), function (item) {
            return item.active();
        });
    })
}

and then create an instance of it

var model = new Model();

because a new operator you will create new JS object and this object will be send as scope for current this content. This is commonly used patterns in order to follow OOJ see link here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript


Count reoccurring elements in multiple lists in Python

What you want is collections.Counter, and the Counter.most_common method.

A naive implementation could be:

import collections

lists = [list_1, list_2, list_3, list_4, list_5]
counter = collection.Counter(sum(lists))

for name, amount in counter.most_common():
    print('\'%s\' is in %s lists' % (name, amount))

This however will mean if list_1 is ['a'] * 5 it will say 'a' is in five lists when this isn't true. A dictionary comprehension is all that's needed for this building a dictionary from the lists, setting the value to 1. And you can take advantage of the fact collection.Counter has addition defined.

counter = sum(
    collection.Counter({name: 1 for name in names})
    for names in lists
)

Without using collections.Counter I would advise you use set. Firstly it removes the need for using np.unique and emphasis the fact that sorted is redundant, as sets are unordered.

It also means that making all is simple and reduces name in list_1 from an \$O(n)\$ operation to an \$O(1)\$ operation. Leading to faster code.

As shown earlier it's easier to work with lists, rather than a list of variable names and so lists is defined the same as above.

sets = [set(names) for names in lists]
all = sum(sets)

def in_how_many(value, sets):
    return sum(
        value in set_
        for set_ in sets
    )

No matter which of the above two solutions you use you still have a couple of other problems:

Don't put code in global scope, have a main function. This makes it harder to do bad things or mess up.
You should use if __name__ == '__main__' to only allow the code in main to run if it's the 'main' file.
In relation to the above two, notice that I pass sets to in_how_many. This is because you shouldn't rely on global scope as it makes reusing the same code harder to do.
Modulo formatting was deprecated for a while, because it's generally worse than str.format and is more susceptible to errors. I suggest using str.format or upgrading to Python 3.7 to take advantage of f-strings.
Import print_function form __future__ to make print a function. This makes it easier to upgrade to Python 3.

from __future__ import print_function
import collections
import functools
import operator

def main(lists):
    counter = functools.reduce(
        operator.add,
        (
            collections.Counter(set(names))
            for names in lists
        )
    )

    for name, amount in counter.most_common():
        print('{name!r} is in {amount} lists'.format(name=name, amount=amount))
        # print(f'{name!r} is in {amount} lists')  # Python 3.7 f-string.

if __name__ == '__main__':
    main(...)


As far as I can understand (I use Moq as an isolation framework and not rhino mocks) in your test you are testing not an implementation of your class FillTableRow, but a mock, generated by isolation framework. You should always use your real class as systam under test (obviously). You use stubs to provide context in which your system under tests works, just to emulate the system's environment. And you use mocks if the state of your system under test is not changing during your test and you need to be sure that correct methods of external classes has been called.
I recomend you to read "The Art of Unit Testing" book by Roy Osherove. It gives very good explanation of how and when you should use mocks and stubs.


Unfortunately, your isPrime function returns incorrect results for the squares of odd numbers.

This is a pretty common oversight though, so don't feel too bad.

A better implementation of isPrime looks more like this:

bool isPrime(int num) {
    if (num <= 3) {
        return num > 1;
    } else if (num % 2 == 0 || num % 3 == 0) {
        return false;
    } else {
        for (int i = 5; i * i <= num; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }
}

In the first if, we handle the special cases of 0 through 3 as well as all of the negative numbers.

In the second, we eliminate all of the multiples of 2 and 3.

Finally, in the catch-all else, we're handling everything else.

By starting at 5 and incrementing by 6, we're able to skip all of our multiples of 2 and 3 which we already eliminated.  So we're checking 2/3rds of the numbers your original implementation checks.

Moreover, because we're dealing with integers, i * i <= num is a bit better than i < sqrt(num) (which actually needs to be <=).


To answer your questions: Yes.

Your code could be boiled down to:

#!/bin/python
import sys
import os

os.system("sshpass -p VerySecrectPassword ssh pi@172.16.0.141" + " " .join(sys.argv[1:]))

Note that I also put the imports on two lines, as recommended by PEP8, Python's official style-guide.

But I would actually re-expand it to:

#!/bin/python
import sys
import os

data = {"user": "pi",
        "host": "172.16.0.141",
        "password": "VerySecret",
        "commands": " " .join(sys.argv[1:])}

command = "sshpass -p {password} ssh {user}@{host} {commands}"
os.system(command.format(**data))

This gives you some easier ways to modify the configuration. It would also be easier to implement reading this configuration from a file now.

Note that bash (or whatever shell you use) will not care about the trailing whitespace in case commands is the empty string.

The next step would be using a ssh library, like @Simon recommended in his comment.

One way to do it would be first setting up password-less login and then using this answer and a comment within it:

#!/bin/python
import sys
import paramiko

rpi = {"username": "pi",
       "hostname": "172.16.0.141"}
command = " " .join(sys.argv[1:])}

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(**rpi)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)

Here the ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) is needed because paramiko does not read the hosts file correctly.


Trim NA values from left and right of Rcpp::NumericVector

Code Review

Code is good, except the naming. There are some pieces which could be improved

Use auto for iterators, as exact iterator type is not important, but it's properties are. Iterator properties are usually inherited from container it came from.

Use better constructor, there seems to be one which accepts pair of iterators, and in this case, pair of pointers denoting contiguous range.

Better (?) approach

The problem seems to be somewhat specific case of trimming in general. Note that trimming from the right and trimming from the left are symmetric, and std::reverse_iterator "normalizes" trimming from right (great improvements thanks to @Toby).

template <typename BidirIterator,
          typename Predicate>
auto trim(BidirIterator first, BidirIterator last, Predicate predicate) {
    auto left_edge = std::find_if_not(first, last, predicate);
    auto right_edge = std::find_if_not(std::reverse_iterator(last),
                                       std::reverse_iterator(left_edge),
                                       predicate).base();

    return std::pair{left_edge, right_edge};
}

When reversing a range, first becomes last, and vice versa, that is why std::reverse_iterator arguments were swapped. There is also C++17 feature in play (template class argument deduction).

Full code:

#include <utility>
#include <iterator>
#include <algorithm>

template <typename BidirIterator,
          typename Predicate>
auto trim(BidirIterator first, BidirIterator last, Predicate predicate) {
    auto left_edge = std::find_if_not(first, last, predicate);
    auto right_edge = std::find_if_not(std::reverse_iterator(last),
                                       std::reverse_iterator(left_edge),
                                       predicate).base();

    return std::pair{left_edge, right_edge};
}

#include <vector>
#include <stdexcept>

int main() {
    std::vector<int> initial_values{2, 2, 3, 4, 5, 6};
    auto predicate = [](int x) { return x % 2 == 0; };
    auto [new_first, new_last] = trim(initial_values.begin(),
                                      initial_values.end(),
                                      predicate);
    std::vector<int> resulting_values(new_first, new_last);
    std::vector<int> correct_result{3, 4, 5};
    if (resulting_values != correct_result) {
        throw std::logic_error("incorrect trimming occured");
    }
}

Demo on Wandbox.

To make trimming a little bit easier to use, one might write something like this:

template <typename Container, typename Predicate>
Container trim_copy(Container&& container, Predicate predicate) {
    auto [new_first, new_last] = trim(container.begin(), container.end(),
                                      predicate);
    return Container(new_first, new_last);
}

and pass a lambda calling the NumericVector::is_na:

auto predicate = [](auto x) { return NumericVector::is_na(x); };

or may be pointer to the function directly.


You missed the specification.

  Find the sum of all the multiples of 3 or 5 below 1000.

You print all of the multiples of 3 or 5 below 1000. To meet the spec, you need to actually sum up all of the elements of ans.


Resets Rows to default values if column A is missing a value

A couple of things -

What is Range("A3:FS5002").Select doing? It seem superfluous. Be sure to avoid things like .Select - it just slows the code down by needing to fiddle with the spreadsheet while doing everything else behind the scenes. There's a good question on StackOverflow addressing this - how to avoid using select in vba.

You use ActiveWorkbook.Worksheets("Master") five times. Why not make it a variable like

Dim masterSheet As Worksheet
Set masterSheet = ThisWorkbook.Sheets("Master")

Then replace all those instances with the variable?

Speaking of variables, ws and ur aren't very descriptive and it costs nothing to make them mean something like dataSheet and dataSheetUsedRange.

Yes, you can use named ranges from the worksheet as named ranges in VBA. That's what you want to do and it's definitely possible and cleaner. Honestly making the ranges be strings that have to be passed to a range method is, pretty confusing. If you need to get the last row each time instead of changing it, you can use -

    Dim lastRow As Long
    lastRow = mySheet.Cells(Rows.Count, A).End(xlUp).Row

Now you can just use the column letters to create your ranges like -

    Set myUsedRange = mySheet.Range("A2:A" & lastRow)

and so on.

You could also use a union like this -

    Dim blankRange As Range
    set blankrange = application.Union(Range("A2:A" & lastRow), Range("B2:B" & lastRow), etc)

Or use the Union with your named ranges

    Set blankRange = Application.Union(Range("BlankB"), Range("BlankFtoG"), Range("BlankK"), Range("BlankMtoO"))

Using the Union method is only one of the ways to do it, say if you had each part of the range as a different name or selection.

Let's talk about named ranges. When you name a range you first select the range in the worksheet, so say you've selected the cells in

H2:I5002,J2:J5002,P2:U5002,Z2:Z5002,AC2:AD5002,AN2:AT5002,AX2:AX5002,AZ2:AZ5002,BE2:BF5002

Now you go up to the spot next to the formula bar and type in a name SelectColumns. Now you can use a formula like =SUM(SelectColumns) on the worksheet.

In VBA you can use the range as well, like this -

Set mySelectRange = dataSheet.Range("SelectColumns")

If, say, you still wanted to use a string then you wouldn't need the quotation marks

Dim rangeToSelect As String
rangeToSelect = "SelectColumns"
Set myRange = dataSheet.Range(rangeToSelect)

I think for some reason you've been misguided that you need to use strings for named ranges. Strings can be used, but if you already have the ranges named, just use the built in name. You can cut out a lot of the constants you have.

If you want to define a name in VBA you can like this -

Sub NameIt()
    Dim myRange As Range
    Set myRange = Range("A1:A5")
    myRange.Name = "SelectColumns"
End Sub

But, as you can see, that's a lot more work by typing it rather than selecting them with the mouse. Something like the NameIt above would only be useful if you needed to define the ranges every time the sub ran, say by using the lastRow from above. You'd need to clear all the names, then assign them again with the relative lastRow.

Here's an example of a non-contiguous named range name in a worksheet -


$allowloop = true;
while (($x < 99) && ($allowloop))
{
// More code was here. //
    if ((!$query) && ($time == 0))
    {
        $allowloop = false;
    } elseif (mysqli_num_rows($query) == 0) {
        usleep(200000);
        $x = $x + 1;
    } else {
        $allowloop = false;
    }
}

if (!$allowloop)
{

You don't need $allowloop.  You can replace these lines with

while ( $x < 99 ) {
// The code that was here stays the same. //
    if ( ! $query && ( 0 == $time ) ) {
        break;
    } elseif ( ! $query || ( 0 == $query->num_rows ) ) {
        usleep(200000);
        ++$x;
    } else {
        break;
    }
}

if ( $x < 99 ) {

I changed $x = $x + 1 to the more standard ++$x which does the same thing.

I also optimized your elseif so that it won't call mysqli_num_rows unnecessarily.  And changed it so that it uses the shorter object-oriented notation.

This hits the database every .2 seconds for almost twenty seconds.  Are you really in that much of a hurry?  Also, be careful of timeouts.  If your HTTP connection times out after five seconds, there's not much point in continuing to poll the database.

Note that if you have a lot of people chatting but no one saying anything, there will be a bunch of people doing this at once.  You may want to look into solutions that allow for sharing these results among multiple HTTP connections.  For example, something that caches the last two message times and returns a cached result for times between them (older results would be returned from the database).  APC would work for a single server or a shared memcache for multiple servers.

    $query = mysqli_query($con, 'SELECT * FROM chat_messages WHERE send_time > "'.$time.'"');

There's a security issue here.  Assuming $time is an integer, you should say,

    $query = mysqli_query($con, 'SELECT * FROM chat_messages WHERE send_time > "'.(int)$time.'"');

This avoids the possibility of SQL injection by someone who spoofs your AJAX call.

Or use mysqli_prepare (preferred).

    $statement = $con->stmt_init();
    if ($statement->prepare('SELECT * FROM chat_messages WHERE send_time > ?') ) {
        $statement->bind_param('i', $time);

Be careful about using SELECT *.  It's one thing if the three columns that you use are the only columns.  It's another thing entirely if you have a large text column that you don't need in the table.  There are some people who believe that you should never use * and always specify the columns.

Note that using * may also cause you to go to disk when you otherwise would have a covering index.  This means that even a small column can be a problem.

I don't like the name $con for the database connection.  It's too short to be obvious what it is.  Admittedly, $con is slightly more accurate than $db, but $db is accurate enough, reasonably standard, and more easily recognizable in my opinion.

You may want to replace send_time with chat_message_id as your check.  I.e. instead of checking the time, check for newer chat_message_id values.  Assuming this is an autoincrement primary key, you should already have a unique index on it.  You could put an index on send_time, but it will be a slightly less performant non-unique index.

This also avoids the problem with near simultaneous chat messages where you only have the first one and never look for the second because it has the same time.

if ($time == 0)
{
    $query = mysqli_query($con, 'SELECT * FROM (SELECT * FROM chat_messages WHERE send_time > "'.$time.'" ORDER BY chat_message_id DESC LIMIT 30) T1 ORDER BY chat_message_id ASC');

This seems more complicated than it needs to be.  We know that $time == 0, so why use $time in the query?  Are there negative times in the table?  Why not just

if ( 0 == $time ) {
    $query = $con->query('SELECT * FROM (SELECT * FROM chat_messages ORDER BY chat_message_id DESC LIMIT 30) T1 ORDER BY chat_message_id ASC');

Also switched to the shorter object-oriented notation.

Switched the order of the comparison to 0 == $time because this will throw a compile error if you leave off an = where $time = 0 will silently change the value of $time and evaluate as false.  Consistently using this order will therefore make it easier to catch that particular typo.

You switch between having the { on the same line and on a new line.  Either is a valid style, but please pick one and stick with it.  My preference is for on the same line, but both are common.

Scaling

My primary concern with this code is that it won't scale well.  You have every user in the chat polling to see if there is new data.  For ten users, this will probably work fine.  For a thousand users, this seems like it would put considerable load on the database.  Not so much from the queries as maintaining a thousand simultaneous database connections for twenty seconds.

As a general rule, a callback is better than polling.  You can generate the callback only when required whereas polling has to continually check.  PHP is probably the wrong language for implementing a callback model though.  You could add an additional layer between PHP and the database to fix this.  PHP would call your service, which would then only call the database once (or not at all if the service also manages sending a chat message) rather than once per client.

In that model, one database connection is sufficient regardless of the number of clients.  You'd only need multiple database connections if there were multiple chat channels, and even then they could share.  Some languages (e.g. Java) have connection pooling so as to share database resources transparently to the client.  PHP seems the wrong language to manage something like this, as it is optimized for HTTP request/reply connections.  PHP's not persistent, while this problem space is.

This will probably work for a small scale application, but if it grows, you'll likely have to dump the current method and swap it out for something more robust.


Freeing allocated memory: am I missing something?

The basic logic is that you only free dynamically allocated resources. Those that were created with malloc.

Hdr_t and body_t are automatically allocated and must therefore not be freed.

The most important point is that you might under no circumstances free a pointer without checking if it is not pointing to 0.

This following fragment will core:

void * ptr = 0;
free (ptr);

Therefore always do something like this:

if (ptr != 0)
    free(ptr)


In this small of a query I wouldn't alias anything.  It doesn't make it any more readable and you don't use those column aliases anywhere else in the query.

Don't do the reporting stuff on the SQL Server.  you can set the title of the column in the datagrid.  Don't do it here, it creates clutter if you try to do it in the SQL.

Whatever you are using to display this data is what you should use to give it a display title.

Always separate retrieving data from displaying data  it makes it easier to code.

I assume that you have all of your RegionID's Positive so you could change the query from

WHERE RegionID <> '0'

to

WHERE RegionID > 0

I hope that RegionID is an Integer in which case the ' are not needed because they make it a string.

Something else that I just noticed, must have been an addition when you edited your post.

WHERE TblA.PropertyID LIKE '%' + @PropertyID + '%'

I always think of an ID field being an integer, and if that is the case then you shouldn't (and CANNOT) use Wildcards or the LIKE operator on that column.  I really don't know how it is running like that.

the LIKE Operator uses a lot of performance as well, so let's say it works,  why would you want to do this in the first place, if you put in
1 for @PropertyID it will bring back records with a PropertyID of

1,11,111,1111,12,13,14,15,16,17,18,19,21,31,41,51,61,71,81,91,(100 <= x < 200)...

if you put in 13 you will get

13,113, 133,1413,1313,1334,1113,2132,1132, ...

I don't see this as being what you want to functionally happen in this query

You don't need this in the Query

ORDER BY TblA.PropertyID ASC

This should be done in the VB code as well.

This is more of a common coding practice type of thing (nitpick) all of your reserved words should be in all Caps

WHERE, JOIN, INNER, SELECT, LIKE, FROM, ORDER BY, AND, AS, ON


The problem with the current approach, as I'm sure you already know, is the very high number of DOM elements that the JavaScript engine is navigating through and assessing before the end of the process (bearing the 30,000 row scenario in mind).

And the jQuery selector function has a fair bit of work to do, checking every row element to see if it has the data attribute value or not, before it can even begin the highlighting. A taxing process. DOM manipulation is notoriously hard on performance.

My answer would be that the mapping between row and parent, which is what essentially needs to be calculated for each row when hovered, needs to be taken out of the DOM entirely so this calculation doesn't require any work with the DOM at all.

See my solution below, and this JSFiddle: https://jsfiddle.net/dawdg4sj/4

Ultimately, you'll see that on each row hover, the 'calculateHighlighting' function is called which performs the calculation "off-line" so to speak, away from the DOM (data attributes withdrawn) and instead uses the local 'map' object (row IDs on the left, parent row IDs on the right), and returned is a straight-forward array of objects each describing an element by ID and a class to add for highlighting purposes.

Bottom line, you need to be able to generate the row id/parent mapping outside the DOM and inside JavaScript instead. As long as you have this mapping information inside the DOM and wish to process 30,000 rows, it'll be a sluggish affair.

(Note: My solution uses function calls from the Lodash library for convenience, i.e. '.keys(.pick(...'. They are optional, keep or swap.)

Finally, and separately, I would express my surprise at such a large number of rows. Working with so many elements in a single view of an app via JavaScript is always going to lead to slow processing to one extent or another. But obviously so many rows could not be visible within the viewport all at once, so I would further suggest that part of the solution is to limit the highlighting to those within view and just outside only, and update on scroll, or even paginate the rows. It depends on the context of the project of course, but those are my supplementary thoughts.)

// ---
// New-look HTML
// ---

        test

        tes

        tes

        test

        tes

        tes

        test

// ---
// New-look JS
// ---

var map = { // row id -> parent row id
    1: 0,
    2: 1,
    3: 1,
    4: 3,
    5: 3,
    6: 1,
    7: 0
};
var highlightColours = 4;

// calculateHighlighting
// Description: Establish a straight-forward array of highlighting rules (classes to add to elements)
// e.g. [ {element: '#row-1', class: 'row-colour-1'}, {element: '#row-2', class: 'row-colour-4'} ... ]

function calculateHighlighting(rootElementID) {
    var result = [];
    var elementIDPrefix = '#row-';
    var elementColourClassPrefix = 'row-colour-';

    var parentRows = [];
    var id = rootElementID.split('-')[1]; // (e.g. '#row-1' -> 1)
    parentRows.push(id); // Start with the root (hovered) element

    var colourIndex = 1; // Keep track of highlight colours used

    while(parentRows.length > 0 || colourIndex <= highlightColours) { // While there are parent rows to highlight (or we run out of highlight colours)
        var childRows;
        for(var i = 0; i < parentRows.length; i++) {
            // Fetch related rows based on id/parent mapping (using lodash function, _.keys, for convenience)
            childRows = _.keys(_.pick(map, function(parentID) {
                return parentID == parentRows[i];
            }));
            if(childRows.length > 0) { // Highlight found parent rows
                for(var j = 0; j < childRows.length; j++) {
                    result.push({
                        element: elementIDPrefix + childRows[j],
                        class: elementColourClassPrefix + colourIndex
                    });
                }
            }
        }
        parentRows = childRows; // Just highlighted parent rows, become base for next parent search
        colourIndex++; // Next highlight colour
    }

    return result;
}

$('tr').on('mouseover', function () {
    var rowID = $(this).attr('id');
    var rowsToHighlight = calculateHighlighting(rowID);
    for(var i = 0; i < rowsToHighlight.length; i++) {
        $(rowsToHighlight[i].element).addClass(rowsToHighlight[i].class);
    }
});

$('tr').on('mouseleave', function () {
    $('tr').attr('class', '');
});

// ---
// New-look CSS
// ---
tr:hover {
    background-color: red;
}
.row-colour-1 {
    background-color: #1ba1e2;
}
.row-colour-2 {
    background-color: #75F7FF;
}
.row-colour-3 {
    background-color: #98E8E3;
}
.row-colour-4 {
    background-color: #B6DCE8;
}


Reading groups of files and concatenating them

Please note that I'm not sure if I'm reading your script correctly. I compared this to the original to see what's lost from how you called the functions. If I've misunderstood entirely please let me know.

In your original script one of your read_csv calls passed in a 'date_completed' key that you left out here to use one function for all files, but you can still get that information using a default value. Default values can be included in a function parameter list so even if they're not supplied, the variable will exist. In your case it would be good to have a value for parse_dates.

def readAndConcatFile(filename, daterange, parse_dates=True):
    df_list = []
    try:
        for date_range in (pd.Timestamp(startdate) + dt.timedelta(n) for n in range(dateRange(filename))):
            df = pd.read_csv(ftploc + filename + date_range.strftime(dateFormat(filename)) + ext, parse_dates = parse_dates)
            df_list.append(df)
        return pd.concat(df_list)
    except IOError:
        print('File does not exist: ', filename + date_range.strftime(dateFormat(filename)) + ext)

This means that in the absence of any value True will be passed to parse_dates as you're currently doing. However you could also pass specific parameters like you did previously.

nps = readAndConcatFile(npsname, daterange, ['call_date','date_completed'])
vas = readAndConcatFile(vasname, daterange, ['Call_date'])
fcr = readAndConcatFile(fcrname, daterange, ['call_time'])
aht = readAndConcatFile(ahtname, daterange)

However I noticed that you previously passed nothing at all to your call for aht whereas you're now passing True. If that's something you'd like to avoid, that's easy with a slight modification. When you want to use a default to make a parameter optional, set the default as None and then you can have a line where you test whether there was a parameter passed or not.

def readAndConcatFile(filename, daterange, parse_dates=None):
    df_list = []
    try:
        for date_range in (pd.Timestamp(startdate) + dt.timedelta(n) for n in range(dateRange(filename))):
            if parse_dates is None:
                df = pd.read_csv(ftploc + filename + date_range.strftime(dateFormat(filename)) + ext)
            else:
                df = pd.read_csv(ftploc + filename + date_range.strftime(dateFormat(filename)) + ext, parse_dates = parse_dates)
            df_list.append(df)
        return pd.concat(df_list)
    except IOError:
        print('File does not exist: ', filename + date_range.strftime(dateFormat(filename)) + ext)

This means you no longer have to pass True to parse_dates for aht just to have the function work.


Cleaner Way to Structure Asynchronous Code

Tips for JS code optimization:

Variable names

Give your variables meaningful names:

const r1 = await dialog.showOpenDialog(...) can be renamed to dirs or dirList (representing selected directories)
const r2 = await fsp.readFile('./config.json', 'utf8'), config = JSON.parse(r2), where r2 is supposed to be a config file path. Let's rename it to say const config_file = await fsp.readFile('./config.json', 'utf8'), config = JSON.parse(config_file)

Extract function:

Repeated expression <string>.slice(-1) === '\\' ? <string> : (<string> + '\\') can be extracted into a reusable routine (named or anonymous function):

var asDirPath = function(path) {
    return path.slice(-1) === '\\' ? path : (path + '\\');
};

Substitute algorithm:

The whole condition (combination of array.reduce and array.includes) can be easily substituted with a more efficient Array.some() approach (to check for any match between current directory path dir and config.archives file/dir paths):

for (const dir of dirList.filePaths){
    // Disregard user input if parent path or exact match already exists.
    if (config.archives.some(path => {
            return dir.indexOf(asDirPath(path)) === 0 || path === dir
    })) continue


This would be a start:

Sub bindTableToControl(table as DataTable, _
                       control, _
                       Optional textField As String = "Name", _
                       Optional valueField As String = "Name")

 control.DataSource = table;
 control.DataTextField = textField;
 control.DataValueField = valueField;
 control.DataBind();

End Sub

bindTableToControl( ddlModelValue , Session[SystemConstant.ModelSessionKey] );
bindTableToControl( ddlGroupValue, Session[SystemConstant.GroupSessionKey] );
bindTableToControl( ddlFitValue, Session[SystemConstant.FitSessionKey] );
bindTableToControl( ddlShellTypeValue, Session[SystemConstant.ShellTypeSessionKey] );
bindTableToControl( ddlDataTypeCodeValue, Session[SystemConstant.DataTypeCodeSessionKey] );

Basically I removed most of the repetition. I also removed all the Hungarian prefixes in the Sub.

As a side note, I wonder why SystemConstant is part of the Session, that does not seem memory efficient?


1. Review

The problem of putting the tickets in order is the same as that of constructing an Eulerian path on the directed graph whose edges correspond to the tickets. (An Eulerian path is a path in a graph that follows every edge exactly once.)

Since this is really a graph problem, it would make things clearer if the code used graph terminology rather than flight terminology. Thus: FlightStorage → Graph, start → vertex (or node), destination → neighbour, list → neighbours, flights → edges and so on.

The reason for using graph terminology (rather than flight ticket terminology) is that there is a body of computer science literature about graphs, and translating your problem into graph terminology helps you to see how your problem relates to this body of literature, and helps you discover and implement the best data structures and algorithms for your problem.
The classes OrderedDefaultDict and FlightStorage lack docstrings.
The list method returns an iterator (not a list) over the neighbours of a vertex. This means that in this code:

for destination in storage.list(start):
    storage.remove(start, destination)
    # ...

the loop is modifying the dictionary storage.storage at the same time as it is iterating over it. This is not guaranteed to work in Python (though it might happen to do so in some cases). For reliability you need to avoid this.
In the remove method there ought to be a check that self.storage[start][destination] is greater than zero, before decrementing it. This would catch attempts to remove non-existent edges from the graph.
The test not any(storage.list(start)) only works if the vertices of the graph are represented by objects that test as true. This prevents us from (say) representing vertices as integers starting at zero (because vertex zero will test as false).
In any case there is no need for the special case if not any(storage.list(start)) because that is already handled correctly — if start has no neighbours then there will be no iterations of the for loop.
Returning None when the graph has no Eulerian path is risky. It would be easy for caller to forget to check. It is better to raise an exception in an exceptional case like this.
When constructing the graph, it would be faster to sort the neighbours of each vertex, rather than sorting all the edges in one big list.
The examples can be written more tersely. Instead of:

[('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'A')]

write:

'AB AC BC CA'.split()

2. Algorithm

The problem of constructing an Eulerian path can be solved using Hierholzer's algorithm. This algorithm works by following edges until a dead end is reached. For example, if we are given this graph:

Then following edges starting at A reaches a dead end at F:

This initial step may leave some vertices which still have unused edges. We pick one of the vertices (for example, vertex E) and follow unused edges again until we reach a dead end. Necessarily this dead end must be where we started (otherwise there is no Eulerian path), and so we get a loop:

Continue to find loops until there are no more unused edges. In this example, if we pick vertex D then we get one more loop:

Now all the edges have been used, so we stitch together the result, by inserting each of the loops into the path we found at the first step, getting the Eulerian path:

In order to ensure that this procedure yields the lexicographically first path, we need to take some care about the order we choose edges and vertices. In particular, when following edges, we should choose the unused edge to the lexicographically earliest neighbour, and when choosing a vertex with unused edges, we should choose the last such vertex along the path constructed so far.

3. Implementation

This uses a linked list to maintain the path, so that we can efficiently insert new vertices into the middle of the path as we go along. The graph itself is represented as a defaultdict(list) mapping each vertex to a list of its neighbours. The neighbours for each vertex are sorted into reverse order, so that when we pop the list we get the lexicographically smallest neighbour.

from collections import defaultdict

class Link:
    "A link in a linked list."
    def __init__(self, vertex):
        self.vertex = vertex
        self.next = None

    def insert(self, link):
        "Insert link after self in the linked list and return link."
        link.next = self.next
        self.next = link
        return link

    def __repr__(self):
        return '{0.vertex!r}->{0.next!r}'.format(self)

def eulerian_path(start, edges):
    """Given an iterable of edges in a graph, return a list of the
    vertices in the lexicographically first Eulerian path in the graph
    starting at the start vertex. Edges must be represented as an
    iterables of two vertices, and a vertex may be any hashable
    object.

        >>> ''.join(eulerian_path('A', 'BC AB CD'.split()))
        'ABCD'

        >>> ''.join(eulerian_path('A', 'AB BC CA AC'.split()))
        'ABCAC'

    If the graph has no Eulerian path, raise ValueError.

        >>> ''.join(eulerian_path('A', 'AB CD'.split()))
        Traceback (most recent call last):
        ...
        ValueError: No Eulerian path

        >>> ''.join(eulerian_path('B', 'AB BC CA AC'.split()))
        Traceback (most recent call last):
        ...
        ValueError: No Eulerian path

    """
    # Build directed graph.
    graph = defaultdict(list)
    for v, w in edges:
        graph[v].append(w)

    # Sort neighbours of each vertex in reverse order.
    for neighbours in graph.values():
        neighbours.sort(reverse=True)

    start = Link(start)    # First vertex on the Eulerian path.
    unvisited = [start]    # Stack of vertices that might have unvisited edges.
    while unvisited:
        cur = loop = unvisited.pop()
        # Follow edges until a dead end is reached.
        neighbours = graph[cur.vertex]
        while neighbours:
            if len(neighbours) > 1:
                unvisited.append(cur)
            cur = cur.insert(Link(neighbours.pop()))
            neighbours = graph[cur.vertex]
        # Each dead end (except possibly the first) must complete a loop.
        if cur.vertex != loop.vertex and cur.next is not None:
            raise ValueError("No Eulerian path")

    # All edges must be traversed.
    if any(graph.values()):
        raise ValueError("No Eulerian path")

    # Reconstruct path from linked list.
    path = []
    while start is not None:
        path.append(start.vertex)
        start = start.next
    return path

4. Performance

Hierholzer's algorithm requires no backtracking, and solves the problem in time that's linear in the size of the graph. Whereas the code in the post potentially follows all simple paths in the graph, and in the worst cases takes time that's exponential in the size of the graph.

Here's one way to generate some test graphs that demonstrate the problem.

from itertools import product

def test_graph(n):
    "Generate edges in a test graph with n vertices."
    yield from product(range(2, n), repeat=2)
    yield 1, 2
    yield 1, n
    yield n, 1

The code in the post takes nearly 3 seconds to find an Eulerian path in the test graph with six vertices:

>>> from timeit import timeit
>>> timeit(lambda:reassemble_flights(1, test_graph(6)), number=1)
2.905132247833535

By comparison, eulerian_path is 30,000 times faster on the same graph:

>>> timeit(lambda:eulerian_path(1, test_graph(6)), number=1)
9.236903861165047e-05

(See this answer for an explanation of why following all simple paths in a graph leads to exponential runtime.)


User Registration compress to use less querry, CPU posible

Ok, a few things of note. Not really related to perfomance, but readability. To be fair though these are just details for better readability,  your code is not so bad.

This code is not expensive process wise, I was curious as to where $User_id comes from, because that is where the query you are worried about comes into play, but I trust your tables are properly indexed and output data is properly escaped.

On line 12-14,  I would put that php code up at top, all in a single php block.
This would separate the php from the html and make for better readability.

Also, same thing for the php block on line 21.  You call once again $current_user = wp_get_current_user(); , but you already have $current_user from line 15.

I would take this block to the top, and change the echo get_avatar() call to :

$avatar = "";
if ( ($current_user instanceof WP_User) ) {
  $avatar =  get_avatar( $current_user->user_email, 40 );
}

By initialising the variable (best practice), in your html "template code" below you can get away with just:

if ($avatar) { print $avatar; }

This habit leaves minimal php in the html and thus separates the logic from presentation.

I assume indentation was lost when you pasted, so I will not patronize you on this.

Hope this helps, happy coding.


One of the first useful things to take in consideration when developing in Python (whether you are a beginner or expert) is the PEP 8 that provides general rules how to write a code that can help other developers to read your code.

For example, from the naming conventions section you can read that variable names are written in snake_camel case. This means RandInt should be re-written as rand_int and ButtonDef() RandomColor() should be written button_def() and random_color() respectively.
In the imports section, you can read that we should avoid wild card imports. This means we should write import tkinter as tk instead of from tkinter import *

Names should be meaningful and express their purpose. This means ButtonDef(), which is rather a misleading name, could be named create_buttons() instead because you are creating 16 button widgets with that function. I would also rename RandomColor() by something like get_random_color() or pick_random_color() because a function does something, so it is better the name gets prefixed with a verb. Also, color is actually referencing several colors, so it should be named colors instead or, why not, colors_list because it consists of a list of colors.
General code formatting rules:

When affecting a value to a variable, we should leave one space character around both sides of the affectation symbol =. But this does not apply to tkinter when it comes to a widget's argument. Let me take an example to clarify this: btn = Button(window,command =lambda:[RandomColor()]) should be written btn = Button(window,command=lambda:[RandomColor()]) (changed ...command =lambda:... to ...command=lambda:...)
In this line: while j in range (4): we should remove the space in range (4): and write range(4): instead.

There are two main suggestions:

I would write your code in an OOP approach. No wonder that OOP and GUI coincided in birth historically.
Ideally, whenever we create widgets, we would keep a reference to them for later reuse or code invocation.  In your code, btn is overridden several 16 times so, by the end, it refers only to the last created button widget; this means you can not access easily ([winfo_children()][3] offers a way to access the child widgets of window but it is hard to have a friendly control on them)

When you have to deal with several widgets and manage them through different lines and columns, grid()  will make your life much easier than place().

Based on all the elements previously mentioned, and based on tkinter best practices article, I would re-rewrite your code as follows:

import tkinter as tk
import random

class ChangeButtonsColor(tk.Frame):

   def __init__(self, master):
       self.master = master
       tk.Frame.__init__(self, self.master)
       self.__colors = ["red","blue","green","yellow","black","purple","orange"]
       self.configure_gui()
       self.create_widgets()

   def configure_gui(self):
       self.master.title('Test')
       self.master.geometry('200x200')

   def create_widgets(self):
       self.create_buttons()

   def create_buttons(self):
       self.buttons = {}
       for i in range(4):
           for j in range(4):
               self.buttons['button{}'.format(4*i + j)] = tk.Button(self.master, bg=self.get_random_color())
               self.buttons['button{}'.format(4*i + j)].grid(row=i, column=j)
               self.buttons['button{}'.format(4*i + j)].config(width=3, height=3)
               self.buttons['button{}'.format(4*i + j)].config(command=self.change_buttons_color)

   def get_random_color(self):
       return random.choice(self.__colors)

   def change_buttons_color(self):
       for i in range(16):
           self.buttons['button{}'.format(i)].config(bg=self.get_random_color())

if __name__ == '__main__':
   root = tk.Tk()
   main_app =  ChangeButtonsColor(root)
   root.mainloop()

Note that in I created a dictionary to keep references to each button I created in create_buttons() function. The way to access each button is demonstrated in change_buttons_color()


A quick note on style, it's much more common in JavaScript code to have the opening curly bracket on the same line as the expression that precedes it, rather than on a new line.

var1, based on the code, indicates whether there has been a validation problem, and to keep the same logical flow, we could name it instead isInvalid for more clarity.

Along the same lines, missingVAL is never used; perhaps you meant to use that one instead of var1?

It's a good practice to use strict equality/inequality (=== and !==) instead of == and != in many cases, to avoid some unpredictable behavior that is otherwise caused by the JavaScript engine.

For instance:

1 == "1"  //true
1 === "1" //false
null == undefined  //true
null === undefined //false
"" == 0  // true
"" === 0 //false

Also, when comparing values to true or false, it's simpler to just let the truthiness or falseness of the value be the condition, such as:

// instead of this:
if (someValue == true && someOtherValue == false) { ... }
// simply do this: (note the use of not "!" operator)
if (someValue && !someOtherValue) { ... }

You are using the same validation routine 4 separate times; instead of copying the code, it's simpler to make one function that does the work then just call it 4 times.

Your validations do this:

Check that the input value is not false (empty, undefined or null)
If it fails, add a message into a string noting the validation failed
Make the validation flag variable true to indicate there is a validation problem.

Step (3) could reasonably be removed by using the validationMsg, since if there have been no validation failures by the end, it will still be an empty string.

We could write a function like this: (code comments added for explanation)

function validate(value, messageIfInvalid) {
    // null, undefined, 0 and "" evaluate to false
    if (!value) {
        // empty string evaluates to false and non-empty to true
        if (validationMsg) {
            // if not empty, add a space at the end
            validationMsg += " ";
        }
        // add the messageIfInvalid
        validationMsg += messageIfInvalid;
    }
}

This function introduces a side effect which is to modify the globally-scoped variable validationMsg, which makes it less predictable. It would be generally better to instead pass it the validationMsg as argument and then return the validationMsg with any applicable modifications made by the function, like so:

// also pass validationMsg as argument
function validate(value, validationMsg, messageIfInvalid) {
    if (!value) {
        if (validationMsg) {
            validationMsg += " ";
        }
        validationMsg += messageIfInvalid;
    }
    // return the validationMsg, whether or not it was modified
    return validationMsg;
}

Then at the very end of the script, we can check whether validationMsg is an empty string or not to find out if there have been any invalid inputs.

// this condition will be true unless validationMsg is an empty string
if (validationMsg) {
    scriptletContext.putGlobal("validationMsg", validationMsg);
    scriptletContext.putGlobal("missingVAL", true);
} else {
    scriptletContext.putGlobal("missingVAL", false);
}

There is one potential issue with this logic, in that if a value checked as invalid/false, but the messageIfInvalid argument was passed an empty string, then no message would be added to validationMsg. We can add another condition to address this special case and just add a generic validation message, like so:

if (!value) {
    if (!messageIfInvalid) {
        messageIfInvalid = "a value is missing";
    }
    if (validationMsg) {
        validationMsg += " ";
    }
    validationMsg += messageIfInvalid;
}

This is what it looks like with all the above applied.

host = scriptletContext.get("host");
user = scriptletContext.get("user");
serverId = scriptletContext.get("serverId");
runId = scriptletContext.get("runId");

function validate(value, validationMsg, messageIfInvalid) {
    if (!value) {
        if (!messageIfInvalid) {
            messageIfInvalid = "a value is missing";
        }
        if (validationMsg) {
            validationMsg += " ";
        }
        validationMsg += messageIfInvalid;
    }
    return validationMsg;
}

var validationMsg = "";

validationMessage = validate(host, validationMessage, "serverHostName (serverHostName) is missing.");
validationMessage = validate(user, validationMessage, "Requestor user name (requestorUserName) is missing.");
validationMessage = validate(serverId, validationMessage, "(vascoServerId) is missing.");
validationMessage = validate(runId, validationMessage, "OAC run type (runTypeId) is missing.");

if (validationMsg) {
    scriptletContext.putGlobal("validationMsg", validationMsg);
    scriptletContext.putGlobal("missingVAL", true);
} else {
    scriptletContext.putGlobal("missingVAL", false);
}


I think it really just comes down to opinion.

I think using ||= is better in this case, it's easier to glance at the code and see what it's intent is. When using the inline if or unless, I have to scan the whole line to know what is going on.

evaluation ||= Evaluation.find(@user.id_of_required_evaluation)

That said, I do like the inline if and unless with returns, since it reads nicely.

return false unless isSelected


Module with helper functions to initialize or reinitialize a Slick carousel

This approach seems fine. I did notice the following loop in Helpers. executeFunctionByName():

for (var i = 0; i < namespaces.length; i++) {
  context = context[namespaces[i]];
}
return context[func].apply(context, args);

Doesn't that just set context equal to the last element in namespaces? Why not just use a simple assignment without a loop?

The value for the timeout

 var timeout = 340;

Could be declared with all capitals as is idiomatic in JavaScript and many C-based languages, as well as a more descriptive name:

 const RESIZE_TIMEOUT = 340;

There are a few things that could be simplified.

In Carousel.init the resize callback could be simplified from

$(window).on('resize', Helpers.debounce(function() {
  applyForTheseQueries();
}, timeout));

To this:

$(window).on('resize', Helpers.debounce(applyForTheseQueries, timeout));

And if ecmascript-6 syntax is supported, then object initializer shorthand notation can be used - instead of :

return {
  init: init
}

just use:

return {
  init
}

And instead of

return {
  executeFunctionByName: executeFunctionByName,
  debounce: debounce
};

Simply to

return {
  executeFunctionByName,
  debounce
};

Another ecmascript-6 feature that could be used is the for...of loop. Instead of

for (var i = 0; i < namespaces.length; i++) {
  context = context[namespaces[i]];
}

Simplify this to

for (const namespace of namespaces) {
  context = context[namespace];
}


Check if an integer equals the sum of its digits to the fifth power

I'm not familiar with either Haskell or Python, but I'd like to challenge the way you're tackling this problem.

First of all, seven 9s will give you a sum of 7 * 95 = 413343. That's six digits, so searching up to one million (instead of ten million) would already be enough.

But we can do better. Instead of analyzing all million numbers, you can reduce that number by realizing that 123456 will give the same sum as 654321. The order of the digits doesn't matter. The sums you actually need to compute are the combinations with repetition; there are 'only' 5005 of them. Python has a standard function to list them in the itertools package, e.g. combinations_with_replacement('0123456789', 6).

When you have computed the sum of the combination, you need to sort its digits and check if they match the combination. If so, the sum is a true positive and can be added to the list.


Generic pattern argument but hardcoded assumptions
It's nice that you added a generic pattern argument, but your code depends on the pattern being "010" here:

      if (j == patternLen) {
        count++;
        chars[i+2] = '1';
      }

Here, you have hardcoded both a 2 and a '1'.  Your program would crash if the pattern were shorter than 3 characters and if i were the last index of the array.  Also, if the pattern were "101", setting the last character to '1' would not accomplish anything.
It would be better to just skip the rest of the pattern:
      if (j == patternLen) {
        count++;
        i += patternLen - 1;
      }


Wording

First, the terminology you chose seems a bit off.  I believe that you are writing this program from the point of view of an entrepreneur or investor in a business, looking to earn income by making computers available for training and general use.  Instead of "cost", you should be talking about price.

There are also some spelling mistakes ("Maximum" → "maxim", "number of computers" → "number of computer", and "month" → "mouth").

Misuse of variables

This program is basically a textual spreadsheet that accomplishes this (inputs in italic, outputs in bold):

As such, there are many variables to track, and therefore proper naming is critical to keep track of all of those numbers.  More importantly, you should not repurpose a variable midway through the program, as you have done here:

training = training * computer_training * open;

and

max = (max * 52 / 12);

Such reassignments prevent you from naming the variable appropriately, and from reasoning about the code.  In fact, the resulting confusion has led to a bug.  The assignment two = 0.2 * max is based on max as a weekly amount.  You subsequently reassigned max = (max * 52 / 12) as a monthly amount.  You then report two as a monthly amount:

System.out.println("estimated monthly income at mouth 1-3 \t:"+ two );

You would be better off with fewer variables, by eliminating variables two, etc.

Style

Nobody likes to read huge declaration blocks:

double training,p_h,computer_training,computers,open;

double max,two,thre,four,five;
double two2,thre3,four4,five5,p;

Instead, declare the variables at the assignment point.

You can make use of import static to avoid writing System.out everywhere.

import java.util.Scanner;
import static java.lang.System.out;

public class BusinessEstimator {
    public static void main (String args []) {
        try (Scanner input = new Scanner(System.in)) {
            out.print("please enter price of training people up on the computer p/h: £");
            double trainingPrice = input.nextDouble();

            out.print("please enter price to use the pc p/h for general use: £");
            double generalPrice = input.nextDouble();

            out.print("please enter number of computers in the shop for training: ");
            double trainingComputers = input.nextInt();

            out.print("please enter number of computers in the shop for general use: ");
            double generalComputers = input.nextInt();

            out.print("please enter hours open per week: ");
            double hoursPerWeek = input.nextDouble();

            double trainingWeeklyIncome = trainingPrice * trainingComputers * hoursPerWeek;
            double generalWeeklyIncome = generalPrice * generalComputers * hoursPerWeek;
            double maxWeeklyIncome = trainingWeeklyIncome + generalWeeklyIncome;
            double maxMonthlyIncome = maxWeeklyIncome * 52 / 12;

            double WAGES_PCT = 0.45;  // % of shop taking paid as wages

            out.println();

            out.println ("maximum monthly income from the computers £ "+ maxMonthlyIncome);
            out.println();

            out.println ("total income estimated over the next 2 years");
            out.println();

            out.println("estimated monthly income at month 1-3 \t:"+ 0.2 * maxMonthlyIncome);
            out.println("estimated monthly income at month 3-6 \t:"+ 0.3 * maxMonthlyIncome);
            out.println("estimated monthly income at month 6-12\t:"+ 0.4 * maxMonthlyIncome);
            out.println("estimated monthly income at year  1-2 \t:"+ 0.5 * maxMonthlyIncome);
            out.println();

            out.println("wages will be 45% of total income at the end of each month" );
            out.println();

            out.println("estimated monthly wages for month 1 - 3  £ "+ 0.2 * WAGES_PCT * maxMonthlyIncome);
            out.println("estimated monthly wages for month 4 - 6  £ "+ 0.3 * WAGES_PCT * maxMonthlyIncome);
            out.println("estimated monthly wages for month 7 - 12 £ "+ 0.4 * WAGES_PCT * maxMonthlyIncome);
            out.println("estimated monthly wages after the first year £"+ 0.5 * WAGES_PCT * maxMonthlyIncome);
        }
    }
}


You can try:

day = case self.type
  when 'fast'       then :fast_completion_day
  when 'super_fast' then :super_fast_completion_day
  when 'ludicrous'  then :ludicrous_completion_day
  else                   :budget_completion_day
end

self.value = Increment.first.max_hours * Incrementor.first.send(day)

From a friend that knows more than me:

prefix = %w[ fast super_fast ludicrous ].include?(self.type) ? self.type : "budget"
self.value = Increment.first.max_hours * Incrementor.first.send(:"#{prefix}_completion_day")

This is probably about as short and dry as you can get it. That said, I'm not sure that this is more clear/easy to read. I'd probably add a method to Incrementor like Incrementor#completion_day_for(type) that would take care of this logic for you.


I think your approach is problematic, in the following senses:

You use inheritance for genericity. There's no good reason for there to be an abstract base class for such operations. The behavior of your class is compile-time-determined by the binary operator you which to represent, and nothing else.
You use virtual methods, which really have no bearing on the problem at hand. That is, I don't see any reason why such operators need vtables.
You template on the binary operator's parameter and result types, rather than the operator itself, i.e. instead of

template <typename LHS, typename RHS, typename RET>

try

template <typename Function>

with std::return_type to obtain RET and so on.
Your infix operator implementation has state - and pointer state at that, which is difficult to optimize away - instead of just using temporary values which can get discarded. Don't be so stuck on imperative programming...

I'm pretty sure you could address all three of these issues with a more templated-oriented implementation; with an intermediate class for "infix operator after having gotten the LHS" (or after having gotten the RHS) which can then take the other operator. Possibly the CRTP is relevant here.

I'm being a bit brief and a bit philosophical since I'm strapped for time. If this is too vague for you (=OP), ask for clarification and I'll expand this answer later on.


Group list of words by occurrence / inverse index of strings

Prefer qualified imports for containers

The containers modules contain several functions that have the same names as their list counterpart. Therefore, they are usually included as qualified modules:

import           Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import           Data.Set (Set)
import qualified Data.Set as S

The types are imported unqualified to make the type signatures easier to read.

Next, it's a easier to exchange your types later if you provide a type synonym:

type MultiMap k v = Map k (Set v)
type WordMap      = MultiMap String Int

Now, let's have a look at your functions. zipWithIndex isn't optimal because it traverses xs twice. However the result of zip is as long as the shorter of the two lists. We can therefore simply write

zipWithIndex xs = zip [0..] xs

Note that you don't need a type signature on those local functions. Indeed, they can be misleading, because the a in zipWithIndex is not related to the a in the outer function.

Don't shadow library function names

group is a name that's already imported via Data.List. Since we now import our containers as qualified, we can simply provide our own singleton function to get rid of group:

singleton :: k -> v -> MultiMap k v
singleton k v = M.singleton k (S.singleton v)

Our makeInverseFunction would now look like this:

singleton :: k -> v -> MultiMap k v
singleton k v = M.singleton k (S.singleton v)

makeInverseIndex :: [String] -> WordMap
makeInverseIndex = foldr (M.unionWith S.union . uncurry insert) M.empty . zipWithIndex . L.map words
  where
    zipWithIndex = zip [0..]

    insert v = foldMap (flip singleton v)

Prefer functions that provide your functionality already

However, there's a function to convert a list of maps into a single map, , unionsWith:

makeInverseIndex :: [String] -> WordMap
makeInverseIndex = M.unionsWith S.union . concat . zipWith go [0..] . map words
  where
    go index ws = map (flip singleton index) ws

I admit that go is a bad name in that context. indexer might be a better one. By the way, we cannot use foldMap or mconcat here, since that wouldn't merge the map values.

Try to relax your type signatures*

orSearch is fine, although you could relax its type. Also, words is against a function name. You can use ws safely in this context.

orSearch :: (Ord k, Ord v) => [k] -> MultiMap k v -> Set v
orSearch ws =
  M.foldr S.union S.empty . pick ws

* unless it leads to performance problems or ambiguities

Prefer foldr1 instead of complicated start values

Now, andSearch is a little bit tricky. You use S.fromList [0..length index-1] in order to have a proper "zero" case. However, if the map is empty, the correct answer should be the empty set, not the complete, right?

So let's handle that case first with M.null index and then use foldr1 from Map's Foldable instance:

andSearch :: (Ord k, Ord v) => [k] -> MultiMap k v -> Set v
andSearch words index =
  | M.null sets = S.empty
  | otherwise   = foldr1 S.intersection sets
 where
   sets = pick words index

Other than that, well done. Keep in mind that there's a IntSet in Data.IntSet that might be more suitable for your use case.


You should have a look at Loop like a native!. Explicitly iterating over the indices is usually not the best way to do something in Python. By directly iterating over the string representation of n you won't even need the second argument (and you should remove it if the assignment/defined interface would not require it). This will help you a lot later, when you start using generators or other iterables which are not indexable.

In addition, you can use the built-in sum together with the fact that bools are integers with True == 1 and False == 0 and a generator expression to make your function a lot shorter:

def count_even_digits(n, n_digits):
    """Return the number of digits of n which are even.
    Second argument (number of digits) is unused.
    """
    return sum(int(d) % 2 == 0 for d in str(n))

I also added a docstring describing what the function does.

An alternative that does not use the casting to str is to make a digits function:

def digits(n):
    """Yields the digits of `n`, from smallest to largest."""
    while n > 10:
        n, d = divmod(n, 10)
        yield d
    yield n

def count_even_digits2(n, n_digits):
    return sum(d % 2 == 0 for d in digits(n))

Timing wise, these two solutions are both very fast, but the conversion to str stays faster, now tested up to 10**308. I also added the functions shown in the answer by @vurmux and the answer by @spyr03, which are even faster than these two solutions:

Interestingly the manual for loop is faster than the generator expression with sum.

For numbers with a lot of digits, it becomes a bit more complicated. This includes a function proposed in an answer to my question on SO about why the for loop is better than the sum. This function is even faster, but I would only recommend using it if you need that last bit of speed.

def count_even_digits_count_unrolled(n):
    s = str(n)
    return s.count("0") + s.count("2") + s.count("4") + s.count("6") + s.count("8")


I see some inconsistencies in your getCurrency.php code. There are many variables that are never declared within the functions, so I don't think that's going to work as expected.

I think the best approach would be to make a parser which looks for whatever currency code you need, regardless of the item order because that might change in the future. I mean, the most performance-expensive part of the job is to parse the whole XML into a SimpleXML structure which you're doing anyway, so looping through the items seaching for the right one wouldn't be much worse and your code will be more straight-forward and escalable.

On the other hand, you might also want to have some cache of the parsed XML to avoid re-parsing the same file on each request. But that depends on the variability of the XML feed. Maybe you can check with The Money Converter how often the feed is updated.


Create a dictionary which saves its data to file

bugs:

Your implementation of __setitem__() does not to add a line
if the key does not exist in the file yet.

other issues:

There is no need to create the file if it does not exist in
__init__(), as open(..., 'w') in __setattr__() will create
it for you.
It is gennerally not a good idea to print from a data class
(i.e. print("creating "+file)) as this makes the class unuseable for
programs that use stdout.
Iterating over a file in python automatically splits on lines so

lines = open(self.file).read().splitlines()
for index, line in enumerate(lines):

could be written as

for index, line in enumerate(open(self.file)):

(like you did in __init__())
If you are going to completly rewrite the file on every
__setitem__(), there is no point in reading the file first, as this
takes more time and is harder to read. here is an example
__setitem__():

def __setitem__(self,key,item):
    dict.__setitem__(self,key,item) #to avoid infinite loop

    with open(self.file, 'w') as save_file:
        for key, value in self.items():
            save_file.write("{} = {}\n".format(key, value))


Here is my take, dry some stuff.

You can glue each piece of code together to have a working script (I guess).

Keep track of your validations

var flags = {
    username: 1,
    password: 1,
    email: 1
};

jQuery seems to be included, so let's use it.
The code of the reg_username and reg_email seems to be similair, so let's DRY it into a function named validates

$('#reg_username').on('input', function () {
    validates('username', $(this).val());
});

$('#reg_email').on('input', function () {
    validates('email', $(this).val());
});

The code of reg_password can be shorten a little.

$('#reg_password').on('input', function () {
    flags.password = !isBlank($(this).val())
    handleButton();
});

Here is the validates function

function validates (type, value) {
    if (!isBlank(value)) {
        remoteCheck(type, value).done(remoteCallback);
    } else {
        flags[type] = 1;
        hideFeedback(type);
        handleButton();
    }
}

You can dryify emailCheck and usernameCheck in a single function named remoteCheck by passing email or username in arguments.

function remoteCheck (type, value) {
    return $.ajax({
        type: 'POST',
        url: 'check_availability.php',
        data: type + '=' + value
    });
}

And here is the dryed callback for remoteCheck

function remoteCallback (type, value) {
    flags[type] = parseInt(response);
    handleFeedback(type, flags[type])
    handleButton();
}

The feedback can also be handled with a single function

function handleFeedback (type, flag) {
    hideFeedback(type);

    if (flag) {
        $('#cross_' + type).fadeIn();
    } else {
        $('#tick_' + type).fadeIn();
    }
}

function hideFeedback (type) {
    $('#tick_' + type).hide();
    $('#cross_' + type).hide();
}

To improve readability we put the flags checker in a separated function (and it now can be used elsewhere)

function handleButton () {
    $('#register_btn').prop('disabled', !isValid());
}

function isValid() {
    return flags.username === 0 && flags.email === 0 && flags.password === 0
}

You can create an utility function to check if a string is empty or null which is used many times in this script.

function isBlank (string) {
    return string === null || string === ''
}


T-test Independence for a Pandas DataFrame

There may be a bug in your code. Look at these two lines:
        group1 = df.where(df[target] == 0).dropna()[column]
        group2 = df.where(df[target] == 1).dropna()[column]

What is happening here? First, the where method sets all rows in which your condition (df[target] == 0) is not true to NaN. Then, you drop the NaN values by using dropna(). The problem: The dropna() method removes all rows where at least one element is NaN, but this not only applies to the rows not meeting your condition, but also to other rows. For example, check out the deck column in the dataset, it has lots of rows with NaN values and all of them get completely removed (that is the entire row). This is not what you want, I guess.
My suggestion:
        group1 = df.loc[df[target]==0, column].dropna()
        group2 = df.loc[df[target]==1, column].dropna()

This code first extracts the column of interest and then removes the NaN values. Not the other way around. In this way, you avoid removing too many lines. In addition, I use loc to select the values, as this is the preferred pandas method, when you need to index rows and columns simultaneously, as opposed to chained indexing like df[idx1][idx2]. Fore more details see here:
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

A thing I just learned: As an alternative to using the dropna() method, you can set the nan_policy to 'omit' when calling the stats.ttest_ind function:
stats.ttest_ind(group1, group2, nan_policy='omit')


this.
There's a lot of this. references in your code.  Typically in Java you only use this. where you need to in order to disambiguate variables.  Consider removing them as they're only adding noise to your code.
always True
Your park methods all return boolean, even though they all always return true. If you want to maintain the same approach, consider changing them to void.
That said, I'd be tempted to merge the isPlaceFor and park logic together into a single tryPark method that checks if it can park and parks it if it can.  This seems like a connected operation.  Are you ever likely to want to know if you can park there without actually parking the car?
Naming / Constants
You fields are big,medium,small.  You use them to track the available spaces of that size, consider being a bit more verbose with the field names to make the meaning clearer.  It'll help when you add maxAvailableBigSpaces for example.
I'd also consider adding constants for the CAR sizes.  Whilst it may be obvious at the moment that 3 is a small car, it may not be as obvious in a week/months time.
delegating
When you park a small car, you're effectively checking for the small space, and if it's not there, trying to park it as a medium car, which if it's not possible you then try to park as a large car.  You could match this approach in your code, which simplifies the logic in each stage and removes some of your duplicate checks.
switch
Combining some of the above together and using a switch expression rather than the if/elseif/else combo means that you could end up with some code more like this:
public class ParkingSystem {
    private static final int BIG_CAR = 1;
    private static final int MEDIUM_CAR = 2;
    private static final int SMALL_CAR = 3;
    private int availableBigSpaces;
    private int availableMediumSpaces;
    private int availableSmallSpaces;

    public ParkingSystem(int bigSpaces, int mediumSpaces, int smallSpaces) {
        availableBigSpaces = bigSpaces;
        availableMediumSpaces = mediumSpaces;
        availableSmallSpaces = smallSpaces;
    }

    public boolean addCar(int car) {
        return switch (car) {
            case BIG_CAR -> tryParkBig();
            case MEDIUM_CAR -> tryParkMedium();
            case SMALL_CAR -> tryParkSmall();
            default -> false;
        };
    }

    private boolean tryParkBig() {
        if(availableBigSpaces > 0) {
            availableBigSpaces--;
            return true;
        }
        return false;
    }

    private boolean tryParkMedium() {
        if (availableMediumSpaces > 0) {
            availableMediumSpaces--;
            return true;
        }
        return tryParkBig();
    }

    private boolean tryParkSmall() {
        if (availableSmallSpaces > 0) {
            availableSmallSpaces--;
            return true;
        }
        return tryParkMedium();
    }
}

A simple array approach
An alternate approach, which is more concise, however I don't think is as clear to follow, would be to use the ordering of the car sizes and an array to handle the allocation.
public class ParkingSystem {
    private int availableSpaces[] = new int[3];

    public ParkingSystem(int bigSpaces, int mediumSpaces, int smallSpaces) {
        availableSpaces[2]=smallSpaces;
        availableSpaces[1]=mediumSpaces;
        availableSpaces[0]=bigSpaces;
    }

    public boolean addCar(int car) {
        if(car > 3) return false;
        for(var spaceToCheck = car-1;spaceToCheck>=0;spaceToCheck--) {
            if(availableSpaces[spaceToCheck] > 0) {
                availableSpaces[spaceToCheck]--;
                return true;
            }
        }
        return false;
    }
}


1)

Take advantage of javascript's truthy and falsey values.

Boolean(undefined); // => false
Boolean(null); // => false
Boolean(false); // => false
Boolean(0); // => false
Boolean(""); // => false
Boolean(NaN); // => false

Boolean(1); // => true
Boolean([1,2,3]); // => true
Boolean(function(){}); // => true

More here: http://james.padolsey.com/javascript/truthy-falsey/

if(monmpf == "") => if(!monmpf).

if(minutesCharset == true) => if(minutesCharset)

if(monhpf.length > 0) => if( monhpf.length )

2)

You can refer to the current element by using $(this).

$('#monmpf').keyup(function () {
// el refers to $("monmpf")
var el = $(this);

3)

if (monhpf.length > 0) is redundant since you already checked the string value with this, if(monmpf == "").

4)

You're using too many global variables, such as emptyField, invalid, green, red, orange, etc.
Try wrapping the variables inside a meaningful namespace, like

var COLORS = { red: "red", green: "green" };
var MESSAGE = { invalid: "Wrong", emptyField: "" };

5)

mondayErrors? I think you should rename this variable.

6)

Break up functions that are longer than 8-12 lines of code into smaller ones.

With all that said, here's what I came up with.

var getRequestObject = function (name, value) {
    var obj = {};
    switch (name) {
        case "monhpf":
            obj.url = 'functions/hoursCharset.php';
            obj.paramObj = {
                hoursPHP : value
            };
            break;
        case "monmpf":
            obj.url = 'functions/minutesCharset.php';
            obj.paramObj = {
                minutesPHP : value
            };
            break;
    }
    return obj;
};
var getPostFunc = function ($el) {
    return function (isCharset) {
        mondayErrors = !isCharset;
        if (isCharset) {
            $el.css('border-color', green);
            $('.mondayErrors').hide();
        } else {
            $el.css('border-color', orange);
            $('.mondayErrors').text(invalid).show();
        }
    };
};
$('#monhpf, #monmpf').keyup(function () {
    var $el = $(this),
        request,
        value = $el.val(),
        id = $el.attr('id');

    if (value) {
        request = getRequestObject(id, value);
        $.post(request.url, request.paramObj, getPostFunc($el));
    } else {
        $el.css('border-color', red);
        $('.mondayErrors').text(emptyField).show();
    }
});


Django's philosophy is Thin views, Fat models. This may be true for small applications, as application grows, it becomes extremely complex to manage all business actions in model layer.

One of the suggested approach is to have a separate layer to handle business actions. This can be similar to view, name it orchestrator or business_actions.

The responsibility of the layer is to do one business action which can touch multiple models. Say, register a new client means creating new user, new client, send notification, sync to external system. Having all these in multiple models is cumbersome.

The business action layer can be modelled as class or simple function which takes data and does all the relevant work.

Here is an example todo application which is designed to address these concerns.


I think you show a good understanding of F# as a language and functional programming in general as far as I can see. A couple of things though:

You use x as variable name on multiple levels of function definitions. It makes it hard to read.

IMO match .. with is a very readable construct for more than two matches. But for only two I prefer if-statements.

The check function/sequence is a kind of double sequence.
I would do it this way:

let primefactors target =
    let check ta = Seq.concat [seq { yield 2UL }; { 3UL .. 2UL .. uint64(ceil(sqrt(float(ta))))}]

and then call it like this:

let nextfactor x =
    match x with
    |1UL -> x
    |_ -> check x
          |> Seq.skipWhile(fun idx -> x % idx <> 0UL)
          |> getFirstOrDefault x

It will make the function much faster.

The getfactors - function is not tail-recursive, but can easily be changed to be:

let rec getfactors x factors =
    match nextfactor x  with
    |1UL -> factors
    // Here the factors list is handled before the recursive call.
    |factor -> getfactors (x / factor) (factor :: factors)


Your first function just gives an array of counts, without anyway to tell what each of them are counting.

Your second function is better, as the index shows what's being counted. You can use the index to see if it already exists, which means you don't have to sort your array.

if( a[arr[i]] )

This works fine if all values are positive integers. But what if you had strings? It would still work, but you couldn't loop through the result easily. You might also run into problems if you have values equal to existing array properties like length.

Instead you can use a Map, which is similar to the associative array in php.

function array_count_values(arr) {
    let a = new Map();
    for ( let i = 0; i < arr.length; i++ ) {
        if ( a.has(arr[i]) ) {
            a.set(arr[i], a.get(arr[i])+1)
        } else {
            a.set(arr[i], 1)
        }
    }
    return a;
}


Makefile for building classwork from markdown files and LaTeX

I have a similar script.  So from my experiences I would:

build the pdfs through latexmk

sometimes it needs more runs of pdflatex.  I found it too hard to get right in plain make.

have directories for work and artefacts.

I don't like to have temporary files among my carefully written texts. The risk of an accidental rm or overwrite is just always present.
Generally the idea is to split files into: pure input, files that are created.  And also these split into files that can be discarded and which should be archived (say they were handed in).

I found it useful to have a "work" directory for the dirty stuff.  It helps to give each document its own subfolder.  In latexmk I can use the -o parameter as -o work/($basename $<)/.  That keeps files reasonably separated.  Then I can also do parallel builds.

Stuff that needs to be kept can just be moved over with a rule to copying them.  Cleaning also gets much easier as you can just wipe the work folder empty.

make smaller projects

I would at least at the class level give them their separate makefile and directory structure. Personally I do it per each document.  Just imagine that after say a year you need to adjust something in the build - and then you end up debugging all your previous essays makefiles and/or text to not lose them.

Not makefile related - but useful is to have the texts in git repositories.   If you put all in one monolithic project the history is going to be very messy.  Much easier to follow on a per project basis.


I understand your concerns here. This is a messy situation, and Java (like most languages, I believe), makes it relatively hard to build up interdependencies during the bootstrapping of the classes, classloaders, etc.

What would be ideal is if you could have an Enum that allowed the following:

MessageProcessor processor = message.getMessageType().getProcessor();

but, that would require the MessageType to be constructed knowing what it's processor is.... but you need to map that on a case-by-case basis, based on the factory that supplies them.

What is certainly not ideal, is having public static methods that create new Maps each time they are called....

public static Map<MessageType, MessageProcessor> getProcessors() { ...

Why does it need to be public... what happens if it is called multiple times?

That's an ugly solution.

The ideal solution would be to construct the MessageProcessor at the same time as the MessageType, but, since that can't be done (as the MessageType is a parameter to the factory), I would instead recommend that you create a synchronized, or locked, or atomic method that is part of the Enum, and handles the situation for you. If your factory has the method:

public MessageProcessor getMessageProcessorFor(MessageType mType) {
    .....
}

then your enum could have a private setup like;

enum MessageType {
    TYPE1, TYPE2, ... , TYPE999;

    private final AtomicReference<MessageProcessor> processor = new AtomicReference<>();

    public MessageProcessor getProcessor() {
        MessageProcessor proc = processor.get();
        if (proc != null) {
            return proc;
        }
        proc = FactoryClass.getmessageProcessorFor(this);
        if (processor.compareAndSet(null, proc)) {
            return proc;
        }
        // there was a race condition, we lost, so use the winner
        return processor.get();
    }
}

This is a compromize I use quite often. It gives you an abstraction between the class-load initialization, and the runtime usage of the enum.

It has the following benefits:

you have a simpler run-time execution model.
your exceptions, if any, related to creating the MessageProcessor have better traces.
you do not have complicated circular dependencies at construct time.
you only create instances for types you use.

on the down-side, you have the following:

slight performance hit each time you call the method as it checks its state.
well, no 2, really.


Judging by the overall structure it looks quite good, I would change a few things though with respect to formatting:

Your public static void selectHeader() { line seems to be off, might've been due to putting the code here, you should remove 4 blanks.
Take as example this line:

wait.until(ExpectedConditions.visibilityOfElementLocated(By
       .xpath("//*[contains(@id, 'picture-und-0-select')]")));

I think it would read better as:

wait.until(ExpectedConditions.visibilityOfElementLocated(
       By.xpath("//*[contains(@id, 'picture-und-0-select')]")));

Note that now every argument is on one line.
Similar situations a few lines below.

Then onto the exception handling:

First I would change UnhandledAlertException alert to UnhandledAlertException alertEx, to denote that it is about an exception.
errorRecovery error = _TestSuite.errorRecovery confuses me to no ends, hence these suggestions:

_TestSuite should not have the underscore in front of it, I am still assuming it is a class though.
errorRecovery appears to be a class, which really should and needs to be upper-cased to prevent any confusion I had so far.

Your header_afbeelding_selecteer() method call is in Dutch and not in camelCase, it should be something like headerImageSelect().
Lastly, your ErrorRecovery error is not an error, it is an recovery or an errorRecovery, please name your variable like that.

I do not fully get what your error handling code does though, there also seems to be logic ongoing which is not neccessary, all I see that you are really using is the method call to headerImageSelect().

Therefore I suggest to add the following to your ErrorRecovery class:

public void handleRecovery(final Runnable methodToRun, final String exceptionMessage) throws UnhandledAlertException {
    if (getCount() == 0) {
        addCount();
        methodToRun.run();
    }
    else {
        resetCount();
        throw new UnhandledAletException(exceptionMessage);
    }
}

Which can then be called as:

errorRecovery.handleRecovery(new Runnable() {
    @Override
    public void run() {
        headerImageSelect();
    }, alertEx.toString());

Or if you are using Java 8, just:

errorRecovery.handleRecovery(() -> headerImageSelect(), alertEx.toString());

or even better:

errorRecovery.handleRecovery(this::headerImageSelect, alertEx.toString());

The design still seems weird to me though, which perhaps now is even more obvious, why does your error recovery method throw an UnhandledAlertException? Indicating that your error would need recovery again!


Extract the data from Dicom files in an time efficient way

Big data requires big time. But there might be ways to speed up your processing somewhat. For each DICOM file you:

open the file
read metadata
read an image
write the image as PNG in the current directory
move the PNG file to it's final destination

Some complex file formats benefit from being read and written on a local drive, the software assumes drive access is fast and memory is expensive, so it reads small bits of data scattered around the file instead of reading larger chunks and using only the small bits it needs. These file types can benefit from being copied over in their entirety to a local drive for reading. However, DICOM is not a complex file format, and DICOM files are commonly stored on networked drives, so I don't think this causes any specific problems.
However, writing PNG files might be a bit faster if done on a local drive. You create the file in the current directory (wherever that is), then move it from root_dirs to Destination_path. So I presume that root_dirs is the current directory? Instead, you could create the file in the temporary directory, then move it to its final destination like you do:
import tempfile
temp_dir = tempfile.gettempdir()  # expected to be on the local drive
# ...
name = dcm.AccessionNumber + '_' + str(counter) + '.png'
name = os.path.join(temp_dir, name)
imsave(name, img)
shutil.move(name,dest)

Next, skimage.io.imsave is very flexible, but it just calls functions in other libraries:

By default, the different plugins are tried (starting with imageio) until a suitable candidate is found. If not given and fname is a tiff file, the tifffile plugin will be used.

Note also that imageio further knows a lot of formats, including two different PNG implementations. So this writing function again does a lot of logic to find out what format to write the file in. And it might end up using the PIL implementation of PNG, which I hear is not very fast.
Therefore, it might be a bit more performant to use a different file writer. I would experiment with different ones, even a small difference in timing will accumulate over so many images. For example, with OpenCV you'd do:
import cv2
# ...
cv2.imwrite(name, img)

Finally, since dcm.pixel_array is a property, not a function, it is likely that dcm = pydicom.dcmread(i) reads in all the data. If you have files for which dcm.Modality == "XC" is not true, you've read in data unnecessarily. Consider looking for a DICOM reader that can read only the metadata. I don't know anything about pydicom, so can't comment on its speed.

Since this is Code Review, I'll give you some pointers towards better code:
Python people have strict rules they follow for code style (encoded in PEP 8). You break some of these rules, but I won't go into that, I think code style is personal and as long as you're consistent, you're OK.
Repeating code is bad though. It is easy to fix a bug in one place, and then forget it needs fixing in the other copy too. It causes there to be more code to read, making the code harder to understand. And it makes you type more, giving you more possibilities to make errors. The following bit of code is repeated, with only a trivial change:
counter = 1
# ...
        if os.path.isdir(dest):
            img = dcm.pixel_array
            name = dcm.AccessionNumber+'_'+str(counter)+'.png'
            counter+=1
            imsave(name,img)
            shutil.move(os.path.join(root_dirs,name),dest)
        else:
            os.mkdir(dest)
            img = dcm.pixel_array
            name = dcm.AccessionNumber+'_0.png'
            imsave(name,img)
            shutil.move(os.path.join(root_dirs,name),dest)

Instead, write:
counter = 0
# ...
        if !os.path.isdir(dest):
            os.mkdir(dest)
        img = dcm.pixel_array
        name = dcm.AccessionNumber+'_'+str(counter)+'.png'
        counter+=1
        imsave(name,img)
        shutil.move(os.path.join(root_dirs,name),dest)

You could also put the os.mkdir(dest) call outside the loop. Having less code inside the loop makes the loop faster.


First, some comments on your code:

An sqlite3 connection can be used as a context manager. This ensures that the statement is committed if it succeeds and rolled back in case of an exception. Unfortunately it does not also close the connection afterwards

with sqlite3.connect("test.sqlite") as connection, open("test_file.map") as f:
    connection.execute("""
        CREATE TABLE IF NOT EXISTS map (
            accession TEXT PRIMARY KEY,
            accession_version TEXT,
            taxid TEXT, gi TEXT
        )""")

    next(f)  # ignore header
    connection.executemany("INSERT INTO map VALUES (?, ?, ?, ?)", read_large_file(f))
connection.close()

You should separate your functions from the code calling it. The general layout for Python code is to first define your classes, then your functions and finally have a main block that is protected by a if __name__ == "__main__": guard to allow importing from the script without executing all the code.
open automatically opens a file in read-mode, if not specified otherwise.

That being said, if you have a billion lines, basically any approach is probably going to be slow. Here is an alternate approach using dask. It may or may not be faster, you will have to test it. The usage is very similar to pandas, except that the computations are only performed once committed to with the call to compute().

First, to install dask:

pip install dask[dataframe] --upgrade

Then for the actual usecase you mention, finding a specific gi in the table:

from dask import dataframe

df = dataframe.read_csv("test_file.map", sep="\t")
df[df.gi == 7184].compute()
#   accession accession.version  taxid    gi
# 0    V00184          V00184.1  44689  7184

In the call to dataframe.read_csv you can set it to read the file in blocks if needed, e.g. in 25MB chunks:

df = dataframe.read_csv("test_file.map", sep="\t", blocksize=25e6)


By counting the length of s2, you missed the point of this exercise.  Technically, you didn't call strlen(), but you emulated it, which is arguably even worse.  The point was to test your proficiency with pointers, and you failed this interview question.  (Why not count the length?  Because it's possible to do it without incurring the cost of that O(n) operation.  The only time you need to walk along the entire length of s2 is when a match is imminent.)

The strstr(3) documentation probably calls the arguments s1 and s2.  However, I'll borrow a rare piece of wisdom from the PHP documentation and call the arguments haystack and needle.

Don't use variables to control the execution flow.  Prefer to use more active means such as continue, break, and return.  If you have detected a match, what are you waiting for?  Celebrate your success with an immediate return!

Your imports are wrong.  The only library function you call is printf(), so you should #include <cstdio>.

Here's a solution.

#include <cstdio>

char* my_strstr(char *haystack, const char *needle) {
    if (haystack == NULL || needle == NULL) {
        return NULL;
    }

    for ( ; *haystack; haystack++) {
        // Is the needle at this point in the haystack?
        const char *h, *n;
        for (h = haystack, n = needle; *h && *n && (*h == *n); ++h, ++n) {
            // Match is progressing
        }
        if (*n == '\0') {
            // Found match!
            return haystack;
        }
        // Didn't match here.  Try again further along haystack.
    }
    return NULL;
}

const char* my_strstr(const char *haystack, const char *needle) {
    return my_strstr(const_cast<char *>(haystack), needle);
}

int main() {
    const char *str = "This is a strstr test";
    const char *search = "strstr";

    const char *result = my_strstr(str, search);

    if (result)
        printf("original: %s\n item: %s\n found: %s\n", str, search, result);
    return 0;
}