SE Data Explorer Query: Average score for questions and answers, by tag

Your code is, for the most part, consistently styled, and predictably structured, which is nice. I am not a fan of the comma-on-the-new-line style of continuation. I prefer the comma at the end. But, at least you are consistent with this.

One inconsistency which I see is that for smaller selects you have multiple selected columns on a single line.

SELECT Tags.Id, Tags.TagName, Alias.Id AS AliasId, Alias.TagName
    ....

To be consistent with the other selects, as much as these are short statements, you should maintain consistency:

SELECT Tags.Id
     , Tags.TagName
     , Alias.Id AS AliasId
     , Alias.TagName
   ....

There is only one example of it in your code, but I really dislike multiple conditions on a single line:

WHERE SourceTagName=TagName AND ApprovedByUserId IS NOT NULL

This really should be:

WHERE SourceTagName=TagName
  AND ApprovedByUserId IS NOT NULL

The UNION condition is also very buried in there, and it has too much indentation ;-)

Finally, white-space is cheap in SQL, I really don't like the space-less arg=val expressions, spacing them is easy arg = val.

Right, that's the nit-picky stuff done.

Talking about the UNION, the statement could be rewritten as:

SELECT IsNull(Main.TagName, Alias.TagName) AS TagName
     , IsNull(Main.Id,      Alias.Id     ) AS Id
     , Alias.Id                            AS AliasId
    FROM Tags AS Alias
    LEFT JOIN TagSynonyms
            ON SourceTagName = Alias.TagName
    LEFT JOIN Tags AS Main
            ON TargetTagName = Main.TagName

I prefer the more compact result, although it is debatable as to whether the "Outer" Join is a better solution than the UNION. Outer joins can lead to confusion. So can UNIONS and I prefer the outer join.

In the above query I have removed the Alias TagName since it is not used outside the sub-select.

I also found the final 'assimilation' of all the data in the final select to have too many casts and data conversions. The actual logic was lost in the detail. I moved the complicating CASTS to the previous with clauses.

Finally, the indexed ORDER BY has caught a number of people out in the past, as it is too easy to change columns and suddenly the data ordering is off, even though the query still runs. I always recommend named-column ordering.

Putting this all together, I have the SQL:

WITH CanonicalTags AS (
    SELECT IsNull(Main.TagName, Alias.TagName) AS TagName
         , IsNull(Main.Id,      Alias.Id     ) AS Id
--         , Alias.TagName                       AS AliasName
         , Alias.Id                            AS AliasId
        FROM Tags AS Alias
            LEFT JOIN TagSynonyms
                ON SourceTagName = Alias.TagName
            LEFT JOIN Tags AS Main
                ON TargetTagName = Main.TagName
), TagQuestionStats AS (
    SELECT Tags.Id AS Id
         , Tags.TagName AS TagName
         , CAST(AVG(CAST(Questions.Score AS DECIMAL)) AS DECIMAL(8,2)) AS AvgScore
         , CAST(STDEV(Questions.Score) AS DECIMAL(8,2))  AS StdDevScore
         , CAST(COUNT(Questions.Id) AS DECIMAL(8,2))  AS N
        FROM CanonicalTags AS Tags
            INNER JOIN PostTags
                ON PostTags.TagId=Tags.AliasId
            INNER JOIN Posts AS Questions
                ON Questions.Id=PostTags.PostId
        GROUP BY Tags.Id, Tags.TagName
), TagAnswerStats AS (
    SELECT Tags.Id AS Id
         , Tags.TagName AS TagName
         , CAST(AVG(CAST(Answers.Score AS DECIMAL)) AS DECIMAL(8,2)) AS AvgScore
         , CAST(STDEV(Answers.Score) AS DECIMAL(8,2)) AS StdDevScore
         , CAST(COUNT(Answers.Id) AS DECIMAL(8,2)) AS N
        FROM CanonicalTags AS Tags
            INNER JOIN PostTags
                ON PostTags.TagId=Tags.AliasId
            INNER JOIN Posts AS Questions
                ON Questions.Id=PostTags.PostId
        INNER JOIN Posts AS Answers
            ON Answers.ParentId=Questions.Id
        WHERE
            Answers.PostTypeId=2
        GROUP BY Tags.Id, Tags.TagName
)
SELECT '<' + QStats.TagName + '>' AS [TagName]
     , QStats.AvgScore AS [Avg Qst Score]
     , QStats.StdDevScore AS [Qst Score Std Dev]
     , QStats.N AS [# Qst]
     , AStats.AvgScore AS [Avg Ans Score]
     , AStats.StdDevScore AS [Ans Score Std Dev]
     , AStats.N AS [# Ans]
     , ROUND(AStats.N / QStats.N, 2) AS [Avg # Ans/Qst]
    FROM
        TagQuestionStats AS QStats
            LEFT OUTER JOIN TagAnswerStats AS AStats
                ON AStats.Id=QStats.Id
    ORDER BY [Avg Ans Score] DESC
           , [Avg Qst Score] DESC
           , [# Ans]
           , [# Qst]
           , [TagName];

This query supports this fork of your original question


Reading String data into a Tree like nested grid structure (like Sudoku Board)

Consider the polymorphic function:

axis :: [a] -> Axis a
axis [x,y,z] = Axis x y z

You want to apply this at each of the four "list levels" of the output [[[[a]]]] of nestList.  So, if you have:

lst4 : [[[[Char]]]]
lst4 = nestList "295743861431865927876192543387459216612387495549216738763534189928671354154938672"

you want to write:

axis lst4 :: Axis [[[Char]]]

to replace the outermost list with an Axis, then you want to fmap axis over the Axis:

fmap axis . axis $ lst4 :: Axis (Axis [[Char]])

to replace the second-level list with an Axis, then you want to do a double-fmap (fmap axis):

fmap (fmap axis) . fmap axis . axis $ lst4 :: Axis (Axis (Axis [Char]))

to replace the third-level list, and finally the innermost list:

fmap (fmap (fmap axis)) . fmap (fmap axis) . fmap axis . axis $ lst4 :: Axis (Axis (Axis (Axis Char)))

So, you actually have:

axify :: [[[[a]]]] -> Board a
axify = fmap (fmap (fmap axis)) . fmap (fmap axis) . fmap axis . axis

Alternatively, instead of using the functor instance for Axis, you could use the functor instance for lists, starting from the inside out:

axify = axis . map axis . map (map axis) . map (map (map axis))


Don't know if this is affecting your performance, but one thing to check is that you are using an up-to-date version of awk. In particular, the version of awk that ships on MacOS is very slow. You may get dramatic difference by installing the latest GNU gawk. I've measured order of magnitude performance improvements by upgrading. (On MacOS, Homebrew and MacPorts are two popular package managers that can install the latest version.)


Testing whether a number is nearly a multiple of 10

I would suggest you to go further in  hard coding those values:
I mean I would write:

def is_near_ten(num):
   return (num % 10) in [0, 1, 2, 8, 9]

You can see visually the difference (less instructions) and you can confirm that:

>>> import timeit
>>> timeit.timeit('(128 % 10) in [0, 1, 2, 8, 9]')
0.23481132300003082
>>> timeit.timeit('(128 % 10) in range(8, 10) or (128 % 10) in range(0, 3)')
1.3901589560000502

You can notice that further hard coding those values as suggested makes your function more than 5 times faster.


If you want to prevent the whole payload from being loaded into memory, you’ll very likely need to send a chunked http response as well. Otherwise I can’t see how the payload doesn’t need to be loaded into RAM completely at some stage.

Looking at the documentation for ServletResponse.html#getWriter(), it seems like the PrintWriter just accumulates all writes in memory until it is finally flushed (and the http response sent).

Another point to consider is that logService (whose code you did not supply) should of course not just load the file and then stream its lines, but needs to really stream the file from disk. Otherwise you’ll not have won anything.

Finally a small tip: In place of an inline lambda you can more concisely write stream.forEach(writer::println) in this case. :)

edit: Service looks fine, but you’re reading the whole file twice – once to find number of lines, and then a second time to get the actual output. (And the number of lines might even have changed in the meantime.) I can’t think of a good solution to this, sadly.

Btw, again I recommend using String::trim and HtmlUtils::htmlEscape as somewhat more elegant. You can also extract the third function into a private String addHtmlFormatting(String escapedError) and then reference it like this::addHtmlFormatting.


Minutia
A table called V ... really? Columns Column1 and Column2 .... why are you using example tables? In your setup you called it Table but in the code you called it V...
Normally we require that code is real code, not 'example' code. In this case, it seems your actual code is 'real', just the tables it works on is not.
IDENTITY columns
IDENTITY columns are generally there for one reason only, to create a unique field that is fast to use (insert/index/join/search).
These properties are guaranteed in all uses of the field, including concurrent modification of the data (two processes inserting data at the same time will each generate unique ID's still - guaranteed)
You want to have the added use for the column: to ensure there are no 'gaps' in the sequence. This is not what IDENTITY columns are for.
IDENTITY columns, for performance reasons, generally allocate a 'pool' of numbers to each process that's adding data, and as each number is used from the pool, the pool shrinks. If the pool is no longer needed, then the pool is 'returned' to the system and another process can continue with it.  If two processes are inserting data at the same time you will often get gaps between the numbers because the pools are different. Also, if there's a "hard crash" on the system, the pool is often completely 'lost', and you could have large gaps in the ranges.
This is the compromise that IDENTITY columns make. They sacrifice sequentialness for performance and concurrency.
Solution
There are two solutions, the easy one, and the hard one.
The easy one is to accept that there will be gaps, and move on. I strongly recommend that you revisit your requirements, and see if you can just use the IDENTITY column and move on.
The hard solution is to build your own system for number generation (although it will still not be perfect... if someone deletes a record, you will still have gaps).
Your solution is too simple, and also too slow. It is, essentially, broken.
If two processes run at the same time, they will allocate the same numbers for records, and you will have duplicate insert situations in to your table. This is because you do not have any transaction handling for your statements... they are all independent. You need to have a table-lock on the table so that no other process can add records while you are finding the max value. The lock is needed for the entire process of finding, and deciding, and inserting the value. You will need to read up on "with holdlock" for your select statements otherwise the lock will not be maintained: See the HOLDLOCK and other documentation on transactions.
Then, after the manipulation is done, you can commit the transaction.
Your performance will suck because you will be locking a lot of data, etc.
The better solution is to have a separate ID table for your key field. A simple table like:
create table VTableKey (
    nextid integer not null
)

insert into VTableKey values (1)

Now, with that table, you can:

begin a transaction
query-with-update-lock and update just that one record
insert into the V table
commit the transaction

In that way, you maintain the 'atomicity' of the transaction - the value only changes on a successful insert. I would consider using a stored procedure to encapsulate that logic.
Note that you are still vulnerable to someone deleting records, you will still end up with gaps.


Looking up a hostname for a given path and client ID

Just don't duplicate it:

Map<Integer, Integer> intermediate = mapping.get(path);
if (intermediate != null) {
    Integer localId = intermediate.get(clientId);
    if (localId != null) {
        return getHostname(path, localId);
    }
}

// log error here
return null;

Alternatively, you could throw the above into a helper function:

private Integer getLocalId(String path, int clientId)
{
    Map<Integer, Integer> intermediate = mapping.get(path);
    if (intermediate != null) {
        return intermediate.get(clientId);
    }
    return null;
}

And then have just the one check:

private String getAddress(final String path, final int clientId) {
    Integer localId = getLocalId(path, clientId);
    if (localId == null) {
        // error
        return null;
    }

    return getHostname(path, localId);
}


This is not a secure system.

I haven't actually read your code, but unless you have a team of at least 10 security researchers who have been doing nothing other than testing this encryption scheme, you should not use it in production code. You should be using a crypto library built by someone that knows what they are doing. I would recommend cryptography. Docs are here https://cryptography.io/en/latest/. Good luck


The conventional naming for arguments in C# is camelCase:

  AccountLogin _AccCred

looks better as

AccountLogin accountLogin

I wonder if this: DatabaseCredentials?.DatabaseName is of any use? if DatabaseCredentials is null what database can you then connect to?

I would throw an exception if DatabaseCredentials == null. This applies probably to AccountCredentials too.

String concatenations are a rather inefficient way to  build strings:

instead of

string ConnInfo = "server=" + DatabaseCredentials.Server + ";" +
                              "user=" + AccountCredentials.Username + ";" +
                              "database=" + DatabaseCredentials.DatabaseName +
                              "port=" + DatabaseCredentials.Port +
                              "password=" + AccountCredentials.Password;

use either

StringBuilder

or

String.Format("server={0};user={1};...", DatabaseCredentials.Server, AccountCredentials.Username,...);

or

string interpolation:

$"server={DatabaseCredentials.Server};user={AccountCredentials.Username};..."

MySqlConnection implements IDisposable. Therefore your wrapper should do that as well, so you can dispose the _connection object, and the client can then use your wrapper like this:

using (DatabaseConnection dc = new DatabaseConnection(...))
{
  // Use the connection...
}

The try-catch-statement in the Connect() method is of no use to the client because, if the connection fails, it is not signaled to the client. Either rethrow the exception, throw a new exception of your own or simply ignore any exceptions here and let the client handle them.

in

public void Close()
{
    _connection.Close();
}

you should test for _connection == null.


Getting all number from a String like this "1,2,5-9"

Well, here's my version that's benchmarked across different browsers. Might need some improvement (currently finding shortcuts for the isNumber), but works as marketed:

function getNumbers(stringNumbers) {

    //personal preference, but I got this handy tip from the internet that

    //if you had assignments, better if they are individually var'ed
    var nums = [];
    var entries = stringNumbers.split(',');
    var length = entries.length;

    //for variabes that don't, comma separated
    var i, entry, low, high, range;

    for (i = 0; i < length; i++) {
        entry = entries[i];

        //shortcut for testing a -1
        if (!~entry.indexOf('-')) {
            //absence of dash, must be a number
            //force to a number using +
            nums.push(+entry);
        } else {
            //presence of dash, must be range
            range = entry.split('-');

            //force to numbers
            low = +range[0];
            high = +range[1];

            //XOR swap, no need for an additional variable. still 3 steps though
            //http://en.wikipedia.org/wiki/XOR_swap_algorithm
            if(high < low){
              low = low ^ high;
              high = low ^ high;
              low = low ^ high;
            }

            //push for every number starting from low
            while (low <= high) {
                nums.push(low++);
            }
        }
    }

    //edit to sort list at the end
    return nums.sort(function (a, b) {
        return a - b;
    });
}

Here's another version, which is slightly slower (the V2) since I added the additional number checks which the one above didn't but more conformant to the specification of the question.

var getNumbers = (function () {

    //we create a closure so as not to expose some of our utility functions

    function isNumber(n) {
        //we check isFinite first since it will weed out most of the non-numbers
        //like mixed numbers and strings, which parseFloat happily accepts
        return isFinite(n) && !isNaN(parseFloat(n));
    }

    //let's get this one out as well
    //the simple sort() wouldn't work so instead we provide a sorter
    function sorterFunction(a, b) {
        return a - b;
    }

    //getNumbers should be this function
    return function (stringNumbers) {

        //variable declaration format is personal preference
        //but I prefer having declarations with assignments have individual vars
        //while those that have no assignments as comma separated
        var i, range, low, high, entry;

        //an added bonus, " and ' are the same in JS, but order still applies
        //I prefer to use ' since it's cleaner
        var entries = stringNumbers.split(',');
        var length = entries.length;
        var nums = [];

        for (i = 0; i < length; ++i) {
            entry = entries[i];

            if (isNumber(entry)) {
                //we check if the entry itself is a number. If it is, then we push it directly.
                //an additinal advantage is that negative numbers are valid
                nums.push(+entry);
            } else {

                //if not a number, probably it had the - and not being a negative number
                //only here do we split after we determined that the entry isn't a number
                range = entry.split('-');

                //check if what we split are both numbers, else skip
                if (!isNumber(range[0]) || !isNumber(range[1])) continue;

                //force both to be numbers
                low = +range[0];
                high = +range[1];

                //since we are dealing with numbers, we could do an XOR swap
                //which is a swap that doesn't need a third variable
                //http://en.wikipedia.org/wiki/XOR_swap_algorithm
                if (high < low) {
                    low = low ^ high;
                    high = low ^ high;
                    low = low ^ high;
                }

                //from low, we push up to high
                while (low <= high) {
                    nums.push(low++);
                }
            }
        }
        return nums.sort(sorterFunction);
    }
}());


Just for the fun of it, here's a one-liner using LINQ.

Dim names = (From i In {0, 35, 70} Where i < input.Length Select input.Substring(i, Math.Min(35, (input.Length - i))))

Example

Dim names As String() = (From i In {0, 35, 70} Where i < input.Length Select input.Substring(i, Math.Min(35, (input.Length - i)))).ToArray()
Dim var1 As String = If((names.Length > 0), names(0), String.Empty)
Dim var2 As String = If((names.Length > 1), names(1), String.Empty)
Dim var3 As String = If((names.Length > 2), names(2), String.Empty)


Well, you effectively have 3 nested loops including the generator comprehension, which you can reduce to two just by simplifying:

def get_products(string):
    for i in itertools.count(0):
        for product in itertools.product(string, repeat=i):
            yield product

Or if you are intent on putting it in a single line, use chain:

products_generator = itertools.chain.from_iterable(itertools.product("ABC", repeat=i)
                                                   for i in itertools.count(0))


Headers and namespaces

#include "bits/stdc++.h"

That's not a standard C++ header; prefer to use standard headers so that others can use your code.

using namespace std;

Bringing all names in from a namespace is problematic; namespace std particularly so. See Why is “using namespace std” considered bad practice?.

Used but not included: <cstdio>, <vector>,  <list>.  I'd recommend you stick to one of <cstdio> and <iostream>.

Function signature

std::vector<int> topologicalsort(std::vector<std::list<int> > adjacencylist,
                                 int indegree[], int v,
                                 int e)

Why do you require e?  It's never used.

Passing indegree as a pointer and length is very C-style; prefer a C++ range (e.g. a pair of iterators, or a standard container).

There's no need to pass adjacencylist by value: a const-ref is more appropriate.

Result vector

Instead of creating v+1 elements in the result vector and having to keep track of our insert position with k, we can create it empty and push_back each result as we find it.  We can avoid reallocation by using reserve().

Looping over elements

Instead of the error prone iterator-based loop, prefer to use range-based for when applicable:

    for (int i: adjacencylist[x]) {
        if (--indegree[i] == 0)
            indegree0.push_back(i);
    }

Variable-length arrays

This is a non-standard extension:

int indegree[v+1];

It's not clear why you ignore the 0th element of these arrays; that probably warrants a comment.

Cleaned code

#include <iostream>
#include <list>
#include <vector>

std::vector<int> topologicalsort(const std::vector<std::list<int>>& adjacencylist,
                                 std::vector<int> indegree)
{
    std::vector<int> ans;
    ans.reserve(indegree.size()+1);
    //list to store vertices with indegree 0
    std::list<int> indegree0;
    for (size_t i = 1;  i < indegree.size();  i++) {
        if (indegree[i] == 0)
            indegree0.push_back(i);
    }
    while (!indegree0.empty()) {
        const int x = indegree0.front();
        indegree0.pop_front();
        ans.push_back(x);
        for (const int i: adjacencylist[x]) {
            if (--indegree[i] == 0)
                indegree0.push_back(i);
        }
    }
    return ans;
}

int main()
{
    int v, e;
    std::cin >> v >> e;
    std::vector<std::list<int>> adjacencylist(v+1);
    std::vector<int> indegree(v+1);
    for (int i = 1;  i <= e;  i++) {
        int v1, v2;
        std::cin >> v1 >> v2;
        adjacencylist[v1].push_back(v2);
        ++indegree[v2];
    }
    for (int i = 1;  i <= v;  i++) {
        std::cout << "adjacencylist[" << i << "%d] :";
        for (int j: adjacencylist[i]) {
            std::cout << j << "  ";
        }
        std::cout << std::endl;
    }
    auto const& ans = topologicalsort(adjacencylist, std::move(indegree));
    for (int i: ans)
        std::cout << i << "  ";
}


Calculate the first passage time of a sequence consisting of prices

else-after-continue
This:
  if -threshold < cum_return < threshold:
     # this possibility will probably occur the most frequently
     continue
  elif cum_return > threshold:
     times.append(cum_return)
     up_threshold += threshold
     down_threshold += threshold
  else:
     times.append(cum_return)
     up_threshold -= threshold
     down_threshold -= threshold

can be
  if -threshold < cum_return < threshold:
     # this possibility will probably occur the most frequently
     continue

  times.append(cum_return)

  if cum_return > threshold:
     up_threshold += threshold
     down_threshold += threshold
  else:
     up_threshold -= threshold
     down_threshold -= threshold


Excel VBA Sub Routine is extremely Slow deleting a Range

COUNTIF() is slow.  A few COUNTIF() statements in a worksheet can be okay, but thousands of them will result in a glacial spreadsheet.

Here is a way to replace your COUNTIF() statements:

Sort Column A.
In Column B, have each cell check if the corresponding cell in column A is equal to the cell above it in column A.  If no, put 1 in column B's cell.  If yes, set column B's cell equal to 1 + the value in the cell above it in column B.
In Column C, have each cell check if the corresponding cell in column A is equal to the cell below it in column A.  If yes, let the cell in column C be an empty string.  If no, set column C's cell equal to column B's cell.
(Optional)  In Column D, have each cell check if the corresponding cell in column C is an empty string.  If no, set column D's cell equal to column C's cell.  If yes, set column D's cell equal to the cell below it in column D.


Matching a checkbox caption to a cell's value

Anywhere there is repetition, there is a function screaming to be born. What is not clear from your code is whether all the same cells are changed (i.e. eRow is the same in all 30 cases) but I will assume this is the case.

Sub HandleCheckBox(SomeText as String, OtherText as String) 'no need to return anything
    Dim tempBool as Boolean ' lousy name, but this is just to shorten code
    Dim eRow as Range
    Set eRow = Sheet3.Cells(Rows.Count, 6).End(xlUp).EntireRow.Columns.Offset(1,0)
    tempBool = CheckBox29.Value
    eRow(6) = TextBox1.Value
    eRow(7) = ComboBox1.Value
    eRow(8) = IIf(tempBool, CheckBox29.Value, "Sales Call")
    eRow(9) = ComboBox3.Value
    eRow(10) = TextBox2.Value
    eRow(11) = TextBox3.Value
    eRow(12) = TextBox4.Value
    eRow(13) = ComboBox4.Value
    eRow(14) = IIf(tempBool And CheckBox1.Value, "Yes", "No")
    eRow(15) = IIf(tempBool, ComboBox6.Value, "-")
    eRow(16) = IIf(tempBool, ComboBox7.Value, "-")
    eRow(17) = IIf(tempBool And CheckBox2.Value, "Yes", "No")
    eRow(18) = IIf(tempBool, ComboBox8.Value, "-")
    eRow(19) = IIf(tempBool, TextBox5.Value, "-")
    eRow(20) = IIf(tempBool, TextBox6.Value, "-")
    eRow(21) = IIf(tempBool, TextBox7.Value, "-")
    eRow(22) = IIf(tempBool, TextBox8.Value, "-")
    eRow(23) = ComboBox5.Value
    eRow(24) = SomeText
    eRow(25) = OtherText
    eRow(26) = IIf(tempBool, TextBox9.Value, "-")
    eRow(27) = IIf(tempBool, TextBox10.Value, "-")
End Sub

Now all that is left is to call it.

You could do this in a series of calls

'[... Your main sub ...]
If Checkbox3.Value then HandleCheckBox CheckBox3.Caption, TextBox11.Caption
If Checkbox4.Value then HandleCheckBox CheckBox4.Caption, TextBox12.Caption
'[... Rest of code ...]

Or perhaps in a series of handlers

Private Sub CheckBox3_Change()
    If Checkbox3.Value then HandleCheckBox CheckBox3.Caption, TextBox11.Caption
End Sub

Or perhaps in a loop

For j = 1 to 30
   cbNumber = j + 2
   tbNumber = j + 10
   If Controls("Checkbox" & cbNumber).Value then HandleCheckBox Controls("Checkbox" & cbNumber).Caption, Controls("TextBox" & tbNumber).Caption
Next j


TL;DR:

use memoization to speed up calculations
if you drive for performance use quot instead of div
if possible, try to define your sequences without complicated functions
use even n instead of n `rem` 2 == 0 or odd n instead of n `rem` 2 == 1

Type annotations and documentation

You use both type annotations as well as documentation. Great. There are only two small drawbacks:

The documentation strings sometimes miss highlighting, e.g. [1, -1, 2, -2,...] in pentagonals. Cross-references would also be great, e.g. pentagonals could point to nthPentagonal in its documentation.
Type annotations for workers (the local bindings) only contribute noise, as their type will be inferred by its surrounding function's types.

For example

integerStream :: [Integer]
integerStream = map integerOrdering [1 .. ]
    where
    integerOrdering :: Integer -> Integer
    integerOrdering n
        | n `rem` 2 == 0 = (n `div` 2) * (-1)
        | otherwise = (n `div` 2) + 1

doesn't need the second type annotation:

integerStream :: [Integer]
integerStream = map integerOrdering [1 .. ]
    where
    integerOrdering n
        | n `rem` 2 == 0 = (n `div` 2) * (-1)
        | otherwise = (n `div` 2) + 1

Convey ideas in code as direct as possible

Above, we use n `rem` 2 == 0 to check whether a number is even. However, there is already a function for that: even. It immediately tells us the purpose of the expression, so let's use that:

integerStream :: [Integer]
integerStream = map integerOrdering [1 .. ]
    where
    integerOrdering n
        | even n    = (n `div` 2) * (-1)
        | otherwise = (n `div` 2) + 1

Use quotRem if you need both the result of rem and quot

integerStream is a special case, though: as we need both the reminder as well as div's result, we can use divMod or quotRem, e.g.

integerStream :: [Integer]
integerStream = map integerOrdering [1 .. ]
    where
    integerOrdering n
        | r         = -q
        | otherwise = q + 1
       where
        (q, r) = n `quotRem` 2

Use simpler code where applicable

We stay at integerStream. As the documentation tells us, we want to have a bijection from the sequence of natural numbers to a sequence of all integers.

While the canonical mapping is indeed

$$
f(n) =
\begin{cases}
\frac{n-1}{2}+1& \text{if } n \text{ is odd}\\
-\frac{n}{2}& \text{otherwise}
\end{cases}
$$
we don't need to use that definition in our code. Instead, we can use

$$
n_1,-n_1,n_2,-n_2,\ldots.
$$

integerStream :: [Integer]
integerStream = go 1
    where
      go n = n : -n : go (n + 1)

Or, if you don't want to write it explicitly

integerStream :: [Integer]
integerStream = concatMap mirror [1..]
    where
      mirror n = [n, -n]

Both variants have the charm that no division is necessary in the computation of your list.

Use cycle for repeating lists

While the methods above arguably break down to personal preference, additiveStream can benefit from cycle:

additiveStream :: [Integer]
additiveStream = cycle [1, 1, -1, -1]

Generalize functions where applicable

delta can be written for any  type that is an instance of Num an Eq, so lets generalize:

delta :: (Eq n, Num n) => n -> n -> n
delta n i
    | n == i    = n
    | otherwise = 0


Compute shortest distance between point and a rectangle

Bug

If either x == x_min or x == x_max, your function will return 0 even if the point is outside of the rectangle.  This is because none of your if statements included the case where x == x_min or x == x_max, so the flow falls through to the end of the function where you return 0.

Extraneous variable

Your function has a variable r that is set but never used.  You should remove that variable so that lines like this:

        return r = x_min - x;

can turn into this:

        return x_min - x;

Extraneous third comparison

Consider this chain of three if statements:

    if(y < y_min){
        return r = sqrt( sqr(x_min-x) + sqr(y_min-y) );
    }else if(y > y_max){
        return r = sqrt( sqr(x_min-x) + sqr(y_max-y) );
    }else if((y >= y_min) && (y <= y_max)){
        return r = x_min - x;
    }

The point can either be above the rectangle, below the rectangle, or within the rectangle.  If the point is neither above nor below the rectangle, then it must be within the rectangle.  Therefore, the third comparison can be removed, like this:

    if(y < y_min){
        return r = sqrt( sqr(x_min-x) + sqr(y_min-y) );
    }else if(y > y_max){
        return r = sqrt( sqr(x_min-x) + sqr(y_max-y) );
    }else{
        return r = x_min - x;
    }

Rewrite

In this rewrite, I defined a HYPOT macro to simplify the expression that you use 4 times.  I also modified the indentation so that each case would be at the same indentation level.  Lastly, I reordered the if statements so the order would be "below, inside, above" instead of "below, above, inside", although the order really doesn't matter.  This rewrite was a tiny bit faster (2%) than the original code, possibly due to removing the extraneous third comparisons mentioned above.

#define HYPOT(x, y) sqrt(sqr(x) + sqr(y))

double distanceBoxParticle2D(double x, double y, double x_min, double y_min,
        double x_max, double y_max)
{
    if (x < x_min) {
        if (y < y_min)
            return HYPOT(x_min-x, y_min-y);
        else if (y <= y_max)
            return x_min - x;
        else
            return HYPOT(x_min-x, y_max-y);
    } else if (x <= x_max) {
        if (y < y_min)
            return y_min - y;
        else if (y <= y_max)
            return 0;
        else
            return y - y_max;
    } else {
        if (y < y_min)
            return HYPOT(x_max-x, y_min-y);
        else if (y <= y_max)
            return x - x_max;
        else
            return HYPOT(x_max-x, y_max-y);
    }
}

Here is a more concise variant you might prefer:

double distanceBoxParticle2D(double x, double y, double x_min, double y_min,
        double x_max, double y_max)
{
    if (x < x_min) {
        if (y <  y_min) return HYPOT(x_min-x, y_min-y);
        if (y <= y_max) return x_min - x;
                        return HYPOT(x_min-x, y_max-y);
    } else if (x <= x_max) {
        if (y <  y_min) return y_min - y;
        if (y <= y_max) return 0;
                        return y - y_max;
    } else {
        if (y <  y_min) return HYPOT(x_max-x, y_min-y);
        if (y <= y_max) return x - x_max;
                        return HYPOT(x_max-x, y_max-y);
    }
}


You don't need to prefix variables with $ inside $(( ... )),
this will work just as well:

idle_secs=$(( minutes * 60 + seconds ))  # Screen timeout in seconds
idle_ms=$(( idle_secs * 1000 ))

In many places you pipe to a grep and then pipe to awk.
Note that awk can perform many of the functions of grep,
and using one process (awk) instead of two (grep + awk) would be more optimal.

Here are some examples. Instead of:

... | egrep '(Width|Height):' | awk '{print $NF}'

This is equivalent:

... | awk '/(Width|Height):/ {print $NF}'

Instead of:

... | grep -m1 dimensions | awk '{print $2}'

This is equivalent:

... | awk '/dimensions/ {print $2; exit}'

And so on, similarly at other places too, you get the idea.

Another common pattern I see at many places:

echo "$br >= 0" | bc -l

That is, using echo to pass a string to a command.
A better way is using here-strings, like this:

bc -l <<< "$br >= 0"

This long line appears at multiple places, sometimes with minor variations:

# If it was interrupted during dimming, reset brightness
xrandr --output $screen --brightness $brightness

It would be better to move this to a function,
for example reset_brightness,
to reduce duplication of logic.


Merge two lists in Java and sort them using Object property and another condition

Your problem statement is not very clear.  As I understand it, you want to have a combined sorted list but interleave elements from list1 and list2 whenever the age is the same.

Here's a simple implementation of that logic.  I used java 8 streams to sort lists and put them in ArrayDeques.

import static java.util.stream.Collectors.toCollection;
import static java.util.Comparator.comparingInt;

static<T> List<T> interleaveSort(List<T> list1, List<T> list2, Comparator<T> cmp) {
    // sort the lists and set them up as queues
    Deque<T> x = list1.stream().sorted(cmp).collect(toCollection(ArrayDeque::new));
    Deque<T> y = list2.stream().sorted(cmp).collect(toCollection(ArrayDeque::new));

    List<T> result = new ArrayList<>(x.size() + y.size());

    while (!x.isEmpty() && !y.isEmpty()) {
        if (cmp.compare(x.peek(), y.peek()) > 0) {
            result.add(y.poll());
        } else {
            // whenever we pick from X, swap queues so next time we will favor the other list
            result.add(x.poll());
            Deque<T> t = x;
            x = y;
            y = t;
        }
    }
    // since the while loop terminates when one of the queues is empty
    // we should add the left-over elements
    result.addAll(x);
    result.addAll(y);

    return result;
}

Then, in your main method:

List<Person> allList=interleaveSort(list1, list2, comparing(Person::getAge).reversed());


Least Recently Used Cache Daily Coding Practice

Your code reads very much like you have written some Java and are trying to translate it to Python. You need to "drink the Kool-Aid" and start embracing idiomatic Python, instead.

First, start by noting that storing None as a value is a legitimate thing to do in Python. So the idea of returning None to mean "no key found" is weak. A better approach would be to raise an exception -- probably a KeyError.

However, since it's part of your problem statement, we can ignore that issue (except to note that your problem statement likely came from Java or C++ or someplace like that).

In your set() method:

def set(self,key,value):
    if key in self.vals:
        del self.vals[key]
        self.vals[key] = value
    else:
        if(len(self.vals) < self.max_size):
            self.vals[key] = value
        else:
            del self.vals[list(self.vals)[0]]
            self.vals[key] = value

You can overwrite items in a dictionary directly. Just assign to them:

if key in self.vals:
    self.vals[key] = value

And your missing-key code is redundant. You assign value in both branches of the if/else statement. Just refactor the assignment down:

if len(self.vals) >= self.max_size:
    del self.vals[list(self.vals)[0]]

self.vals[key] = value

Once you do that, you realize that the assignment is also present in the upper (key-found) condition, and you can refactor it again:

def set(self, key, value):
    vals = self.vals
    if key in vals and len(vals) >= self.max_size:
        del vals[list(vals)[0]]
    vals[key] = value

In your get() method:

You are doing things pretty much spot-on, here, but you are depending on dictionaries to preserve insertion order. That is a somewhat new feature in python (added as an implementation detail in 3.6, and guaranteed in 3.7) that won't work in 3.5. You should at least comment this, and probably should add code at the top of your module to verify the version of python.

On the other hand: there is a collections.OrderedDict in the standard library that has been around for a while, and which even includes an example of building an LRU cache in the documentation. Just sayin'. ;-)


(this answer is advice and not a real code review, but I can't comment)

I discourage to use swing for your GUI. Java is replacing swing with JavaFX.
But that's not the only reason why I should use JavaFX. The most important reason is that with JavaFX it is easier to follow the MVC pattern.

A very good JavaFx and MVC tutorial I really recommend and wich shows the whole worflow and structure:
link


Basic login script for educational purposes

First, all of your indentation is half indentation: you two-spaces instead of four.

Most IDEs have proper spacing if you press TAB.

Naming:

You have variables need re-naming:

$_SESSION['log']: a log is a regular or systematic record of incidents or observations, not whether you've logged in, which has entirely different meaning, also the same applies to the following variables, log is a completely different thing than the state it represents: loggedIn or signedIn would be much better.
$pass, $_SESSION['pass'], $user and $_SESSION['user']: it's really not worth it to sacrifice four extra characters here and there, for the sake of simplification. add word and name to the end of those.

Security:

You SHOULD not be saving the password to a session variable, there are ways to break into sessions, and therefore, is an insecure way to save them longer than needed; use unset($_SESSION['password']) to remove them after processing.

Failed Globals:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $errors = array();

You use the $errors variable outside of the if statement, which is a no-no. Declare the variable up there with session_start().

You shouldn't be manually providing the name of the PHP form, but, rather using $_SERVER['PHP_SELF'] instead:

<form method="post">

into:

<form action=<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?> method="post">

  if (count($errors) > 0) {
    foreach($errors as $e) {
      echo $e . '
';
    }
  }

count($errors) > 0 can be simplified to: count($errors)
count($errors) can simplified to: $errors
$errors can simplified to: '' (Nothing)

Explicitly checking the length of $errors is redundant, when you have a for or foreach loop.

If there's \$0\$ elements, the foreach won't trigger.

foreach($errors as $e) {
    echo $e . '
';
}

In fact, in this instance, foreach is redundant in itself.

Using a neat function called implode, you can join an array together, without needing to iterate over its elements explicitly.

echo implode('
', $errors);

Actually,
 isn't really the best way to display multiple errors.

Consider using
 instead.

    <?php echo implode ('

', $errors); ?>

That certainly slimmed down, didn't it? When displaying content built from variables or arrays, make sure you consider how you choose to return them to the client.

        Username
        <input name="user" type="text">

        Password
        <input name="pass" type="password">

A few points to make about this block:

Why are you using
? It seems silly, and pointless. Even using
 would be pointless.
In the <input> you can use the attribute placeholder, which as the name suggests, lets you set a placeholder to show an example password, or the name of the field. Personally, I'd ditch the labels, and put username and password as placeholders. As @KevinBrown pointed out in the comments: screen readers need lables, and without them, it can cause issues, however, I'd still set placeholders.

    Username
    <input name="user" type="text">

    Password
    <input name="pass" type="password">

Into:

<input name="user" placeholder="username" type="text">
<input name="pass" placeholder="password" type="password">

You can individually space them with CSS (Recommended method),
 + inline, or
.

if (isset($_SESSION['log']) && !empty($_SESSION['log'])) {

$_SESSION['log'] is stored as a boolean, not a variable. Meaning, you can simply test it, without the need for functions like isset and !empty.

if ($_SESSION['loggedIn']){

Finally, the usage of $errors is a little superfluous.

$errors is used as an array, implying multiple errors will be added. However, only one $error should be added to the array, meaning a variable would be better.

Well, that would be the case, if you added a break, or return in the empty() check, but instead, you check if it's empty, add an error.

Then proceed to run through the rest of the code before comparing them. You already know if they're empty, they won't match the username and password given by the server, so why continue the script?

First, initialise $error (Note the lack of s) as a variable.
Then, break the function if they're empty.
Finally, return $error as a variable.

(I do realise this nullifies my previous point about displaying the errors, but, nonetheless, if you were to display errors, that's the way to do it.)


Your current code allocates several objects on the heap even though it doesn't need to. A string, a character array and several character objects.

Puzzles like this can often be solved not by inspecting each bit individually but by processing them in parallel. An alternative approach is:

To find the binary gap of n:

Discard all trailing zeros.
As long as n does not consist of 1s only:

Combine n with n shifted to the right by one place.

The number of repetitions is the length of the largest gap.

Taking 1000010001000 as an example:

1000010001   after 0 steps
1100011001   after 1 step
1110011101   after 2 steps
1111011111   after 3 steps
1111111111   after 4 steps

It took 4 steps, therefore the binary gap is 4.

In Java, this code becomes:

public static int binaryGap(int n) {
    n >>>= Integer.numberOfTrailingZeros(n);
    int steps = 0;
    while ((n & (n + 1)) != 0) {
        n |= n >>> 1;
        steps++;
    }
    return steps;
}

As said in the above description, this code runs in \$\mathcal O(\text{gap})\$, which is a bit better than \$\mathcal O(\text{bits})\$.

It might be worth making it run in \$\mathcal O(\log_2 \text{gap})\$ by first taking 16 steps at once, then 8, then 4, then 2, then 1. If that's possible at all. For 32 bits it's entirely possible to compare all results of the optimized version against the simple code shown above. But then I guess the code will become much more complicated.


You could avoid the is_active variable by using the lock in a non-blocking manner:

def try_to_do(self, yes_we_can_fn, no_we_cannot_fn):
    if self.locker.acquire(False):
        try:
            yes_we_can_fn()
        finally:
            self.locker.release()
    else:
        no_we_cannot_fn()


Disclaimer: I'm a Rust beginner with a background in C and C++. Now that I've lost my credibility, let's have a look at your code.

Use rustfmt, clippy and rustc

There are (at least) three helpful tools when one writes Rust code: rustfmt, clippy and the compiler rustc itself.

Rustfmt provides a common ground

The compiler doesn't yield any warnings, but rustfmt will change your code.
As many Rust developers use rustfmt (or cargo fmt, which calls the former), you should also make a habit of using it. That way others will feel right at home if they look at your code, and the same will hold for you when you look at theirs.

Clippy's warnings

clippy on the other hand will catch common mistakes. In this case its mostly the redundant returns in your idx functions:

fn get_transpose_table(keyword: String) -> Vec<usize> {
    keyword
        .chars()
        .map(|character| char_to_alpha_idx(character))
        .collect()
}

fn char_to_alpha_idx(character: char) -> usize {
    "abcdefghijklmnopqrstuvwxyz"
        .find(character)
        .expect("Bad char for char_to_alpha_idx")
}

fn alpha_idx_to_char(loc: usize) -> char {
    "abcdefghijklmnopqrstuvwxyz"
        .chars()
        .nth(loc)
        .expect("Bad loc for alpha_idx_to_char")
}

There is no need to return here. Next, you never consume the String in get_transpose_table, so we could borrow it instead with &String. But in the end, we never use any String-specific functionality, therefore we can just use &str.

While we're at it, map(|x| func(x)) is just map(func), so let's change that as-well:

fn get_transpose_table(keyword: &str) -> Vec<usize> {
    keyword.chars().map(char_to_alpha_idx).collect()
}

All those small improvements were reported by clippy.

Use T::with_capacity if you know how many elements you will have

For your main algorithm, you should use String::with_capacity if you know the number of characters you want to store:

let mut output = String::with_capacity(input.len());

Nothing else in your code changes.

Closures and readability

Apart from better names (message instead of body, body_index or message_ index instead of input_index) that's all we can do with this algorithm. We could transform it into a closure:

let output: String = message
    .char_indices()
    .map(|(index, message_char)| {
        let transpose_distance = transpose_table[index % transpose_table.len()];
        let transposed_char_idx = char_to_alpha_idx(message_char) + transpose_distance;
        alpha_idx_to_char(transposed_char_idx % 26)
    })
    .collect();

but we only removed mutability. So let's get back to the drawing table. Why do we need index? Because we want to know which usize we should use from the transpose_table. We would like to traverse a cycle of those values at the same time as our original message.

That's exactly what cycle() and zip() are for:

let output: String = transpose_table
    .iter()
    .cycle()
    .zip(message.chars())
    .map(|(transpose_distance, message_char)| {
        let transposed_char_idx = char_to_alpha_idx(message_char) + transpose_distance;
        alpha_idx_to_char(transposed_char_idx % 26)
    })
    .collect();

Whether that's easier to understand than your original variant is left to you. However, we can also get rid of the intermediate Vec:

let output: String = keyword
    .chars()
    .map(char_to_alpha_idx)
    .cycle()
    .zip(message.chars())
    .map(|(transpose_distance, message_char)| {
        let transposed_char_idx = char_to_alpha_idx(message_char) + transpose_distance;
        alpha_idx_to_char(transposed_char_idx % 26)
    })
    .collect();

This is a handful, but it works exactly as your previous variant.

Encapsulate functionality in functions

Now, my main critique point is that you don't provide a single function for your cipher. It's hard to test your functionality at the moment, so we better add one and some documentation:

/// Applies [The Alphabet Cipher] given by `keyword` to `message`.
///
/// Both `message` and `keyword` must only contain lowercase alphabetic ASCII
/// characters.
///
/// # Panics
/// Panics if any non-lowercase or non-alphabetic ASCII character is encountered.
///
/// # Examples
/// The character 'a' in the keyword won't change the input:
/// ```
/// assert_eq!(alphabet_cipher("aaaaa", "hello"), "hello");
/// ```
///
/// They keyword gets cycled if its  shorter than the message:
/// ```
/// assert_eq!(alphabet_cipher("a", "hello"), "hello");
/// assert_eq!(alphabet_cipher("b", "hello"), "ifmmp");
/// assert_eq!(alphabet_cipher("ab","hello"), "hflmo");
/// assert_eq!(alphabet_cipher("ba","hello"), "iemlp");
/// ```
///
/// [The Alphabet Cipher]: https://en.wikipedia.org/wiki/The_Alphabet_Cipher
fn alphabet_cipher(keyword: &str, message: &str) -> String {
    keyword
        .chars()
        .map(char_to_alpha_idx)
        .cycle()
        .zip(message.chars())
        .map(|(offset, message_char)| {
            let index = char_to_alpha_idx(message_char) + offset;
            alpha_idx_to_char(index % 26)
        })
        .collect()
}

The code snippets in the comments also get tested by cargo test, but we can also add some more tests to check that our functions work, for example:

#[test]
fn alphabet_cipher_rot_a_to_z() {
    assert_eq!(
        alphabet_cipher("abcdefghijklmnopqrstuvwxyz", "aaaaaaaaaaaaaaaaaaaaaaaaaa"),
        "abcdefghijklmnopqrstuvwxyz"
    );
}

There are more tests one can come up with, but those are left as an exercise.

User friendly interface

You use unwrap() quite a lot. However, that leaves a user with unhelpful error messages:

$ ./cypher test
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', ...

$ ./cypher test test12345
thread 'main' panicked at 'Bad char for char_to_alpha_idx', ...

Those error messages aren't that helpful. They're fine for toy programs, but you should view unwrap as "I know that this will be Ok or Some, and it's fine to panic if it isn't, because I don't know how to continue". However, even in those cases, the end user should know how to continue. Keep that in mind when you write larger programs.


A function to convert FASTA .txt to a dictionary

Here are my suggestions:

It's a good practice to use a with statement to handle the opening and closing of files for you. Not only does it keep you from having to worry about closing every file that you open, it also simplifies your code.
You might consider taking the file I/O part out of your function altogether, thereby simplifying what your function does. Your function would still work exactly the same way, you would just be passing it the actual FASTA text (including linebreaks) as a string, rather than a filename. Your function call could like something like this:

with open("some_fasta.txt", "r") as f:
    txt = f.readlines()
    d = parse_fasta(txt, "this_is_a_key_id")

(I also took the liberty of renaming the function parse_fasta so as to better describe what it does.)
A simpler way to update a dictionary entry is dictionary["key"] = "new value" (as opposed to dictionary.update({"key": "new value"}).
Instead of adding all of the keys and values to the dictonary, and then going through them one by one and deleting them or replacing escape characters, you could simplify things by validating the entries before adding them to the dictionary. You can also use str.translate() as a simpler way to remove any escape characters from the string. Conveniently, you can specify the escape characters as a string, allowing you to represent them more concisely (see my revision at the end -- I managed to get your argument list down to one line!)

"fix\n\r\n\r\t me".translate(None, escape_chars)
#=> "fix me"

(I would also rename replace_id_list to escape_chars.)
You might consider changing if key_id not in line: to simply else:, to better reflect that your code either does one thing or the other, depending on whether or not key_id is in line.
Instead of updating the value of value_line and then updating the value in the dictionary to that updated value, you can just update the value in the dictionary directly.
The way your code works currently, a KeyError will be thrown if the first line doesn't contain the given key_id. A simple way to fix this would be to initially (before the for loop) define key_line to be something like "key_id not found" -- that way any lines at the beginning of the file that do not contain the key_id will be collected as the value of that key. A better solution would involve exception handling.
Another possible problem with your function is that you are deleting the entry for a key altogether if any subsequent "value" line has less than the minimum required length of characters. This might be the behavior you want, but I have a hunch that what you really want to do is to ignore any lines that are not of the minimum required length. I've taken that approach with my revision below.
As you currently have it, you are checking the length of each line, including any escape characters. You should check after removing the escape characters.
You should also remove the escape characters from the key lines.

Here is your code with my suggested revisions:

def parse_fasta(txt, key_id, escape_chars="\r\n\w\s\t\v\f", min_len=1):
    """Returns a dictionary representation of FASTA text. Every line containing the
       supplied key_id is added to the dictionary as a key, with every subsequent
       line until the next key_id line as its value."""
    d = dict()
    key = "key_id not found"
    for line in txt:
        line = line.translate(None, escape_chars)
        if key_id in line:
            key = line
            d[key] = ""
        else:
            if len(line) >= min_len:
                d[key] += line   # (or optionally, d[key] += line + "\n"
                                 # if you want to keep the lines separate)
    return d


You have inconsistent bracing style. For example, in your onsubmit function, you start the brace on the same line as the function signature:

form.onsubmit = function(e) { // <=======

But, in this if/else conditional, you are starting the brace on the next line:

else
{ // <========

Choose one and stick with it: it makes your code a lot more clean. Personally, I like to use the first version.

Here, you are checking to make sure that the form is not blank:

if (form.costOfGas.value == '' ||
  form.vehicleMPG.value == '' ||
  form.numMiles.value == '')
{

However, there is a much, much easier way to do this: using the required HTML attribute (scroll down for the "required" section)

By adding a blank required attribute to each of the input elements of your form, you can safely remove this chunk of code from your JavaScript; the user will no longer be able to submit the form unless they have entered data in each field.

Here, you are using the + operator to convert a string to a number:

var userCostOfGas = +form.costOfGas.value;

While this is a neat little JavaScript trick, it looks like it's more for if you were creating this code for a code golf: it is very unclear as to what this is doing.

To make this more clear, use the parseInt function:

var userCostOfGas = parseInt(form.costOfGas.value, 10);

It looks to me that, since you are attempting to turn the form input fields from a string into a number, that you are using input fields of the type text.

By doing it this way, you are forcing yourself to take that extra step of converting the input to a number type. It would a lot easier if you set the type attribute of your input fields to "number".

That way, when you go to get the value of the input fields, they will already be of the correct type.

Good job using toFixed on the output: it makes the formatting of numbers very clean.

You have a few magic numbers in your code, especially in the calcCost function:

var dailyCost = (costOfGas / vehicleMPG) * (numMiles * 2);
var weeklyCost = dailyCost * 5;
var monthlyCost = weeklyCost * 4;

Where did the 2 come from? What about the 5? And the 4?

To make your code more readable and understandable, you should declare constants at the top of your code, and then use the constants in place of these mysterious numbers.

Here, I've created a few for you:

var DAYS_IN_WEEK = 5;
var WEEKS_IN_MONTH = 4;

I wasn't able to figure out where the 2 came from.

Then, going back to the code snippet, you would use these constant variables like this:

var weeklyCost = dailyCost * DAYS_IN_WEEK;
var monthlyCost = weeklyCost * WEEKS_IN_MONTH;


Implementation of Conway's Game Of Life on Android

General isues

Your code doesn't follow the single responsibility principle. The class WorldSimulation is responsible for updating the game and creating the graphical representation. If you followed a naming convention where the name of a class describes it's functionality it would have to be WorldSimulationAndPaint.

Instead of passing the canvas and paint to the game engine, you should try to follow the MVC pattern. I have once implemented a Conway's game of life too. I had a timer that was responsible for controlling the updates. The timer sent an event to the game engine telling it "please advance the game state by one frame". When the game engine had done that, the game engine sent an event to the user interface telling it "here's the lates game state, please update the user interface."

Once you have removed the responsibility of drawing from WorldSimulation you can remove the cellWidth and cellHeight fields.

The events can be transmitted by setting up each component as a listener to the relevant source or with an event bus (such as Google Guava).

Field visibility

All the fields in WorldSimulation are visible to other classes in the package. The fields of a class should be as closely guarded as possible so they should be private unless a there is a very good reason to do otherwise. That way you guarantee that the state of the object can only be changed by the object itself. It also forces you to concentrate on the interface your class provides to others.

The livingNeighbours method is not supposed to be accessed from outside the class, so it should be private too.

Magic numbers

What does 255 mean? Instead of using a literal integer every time you should define it as a constant whose name describes the meaning of the value and use the constant instead of the literal 255.

private static final int STATUS_ALIVE = 255;
private static final int STATUS_DEAD = 0;

Duplicated code

The drawRect call is identical in both branches of the if statement. You can take them both out and replace them with a single call after the if statement.

canvas.drawRect(left-cellWidth,top-cellHeight,right,bottom,paint);

Misleading naming

In livingNeighbours you count number of cells, but the value is collected in a variable named alive. In spoken language "alive" has only two values (alive or dead). Thus it sugests that the field would be a boolean (as you have done in the setCell method), while it is in fact an integer. It should be named livingNeighbourCount.

Misleading method signature

The alive parameter suggest that one could set a cell to be either living or dead. The implementation however only sets it to a living state. Either fix the code to handle the parameter or remove it and rename the method to setCellAlive.

public void setCell(int i, int j, boolean alive)
{
    next[i][j] =  255;
}

Rules

You're not really implementing Conway's Game of Life. :)


Find the most frequent number in an array

Please add a /** */ javadoc sentence, clarifying what happens in e.g. the case where { 5, 5, 6, 6 } is passed in.

The name findMostFrequentNumber() seems a bit verbose, when mode() or findMode() would suffice.

Consider numbers instead of givenArray. In any event, "array" is redundant with int[].

Consider eliding the null check. Caller gets what he deserves if the contract is "caller shall provide an array of numbers". NPE is appropriate if caller fails to do that. Or throw your own exception. Returning zero is clearly inappropriate, as caller cannot distinguish between an input of null and an input of { 5, 6, 0, 0, 0, 5 }.

In numberOccuranceMap, the "map" is redundant with the Map declaration. (Also, typo: occurrence.) Consider a "to" name for your maps, such as numToOccurences or numToCount.

    int mostFrequentOccurance = 0;

This sounds like it is simply modeCount, and the mostFrequentNumber we will return is simply mode.

            numberOccuranceMap.put(number, numberOccuranceMap.get(number) + 1);

Your get() call could specify a 2nd argument default value of zero, which would obviate the need for the containsKey() test.

        if (numberOccuranceMap.get(number) > mostFrequentOccurance) {
            mostFrequentNumber = number;
            mostFrequentOccurance = numberOccuranceMap.get(number);
        }

Here we find the answer to the javadoc question above: last one wins!


Rails controller to manage EC2 instances

I see two things wrong with this controller.

It's too 'fat'. You're doing way too much in one controller.
It's not RESTful.

In this case, those two problems are much the same. Without reading too much into the code, it looks like you could break that controller up into 3 controllers, UsageController, InstanceController, Instance::PowerController. I'm not certain about the last one, you may find the Instance's power state not to be a RESTful resource.

Here's an example: https://stackoverflow.com/documentation/ruby-on-rails/4373/method-on-a-controller-that-s-not-part-of-the-default-five#t=201608110000585779419

As well though, I'd say the building of the hashes should be encapsulated by a model.


Congrats on tackling cryptopals! These are great exercises and very interesting to learn new languages and techniques.

Syntax

Remove the type annotations

In most cases in OCaml, you don't need any type annotations. So instead of:

let make_result (input : xor_input) c : result =

It's more usual to write:

let make_result input c =

The reason is that for exposed functions, the type will be in the .mli file, and for local functions, it's only one key binding away in your editor thanks to merlin.

Pipelines

The pipe operator is powerful and works well in some cases like your main function, but combined with partial application and infix operators, it can become hard to read.

I'd rewrite:

fun l -> int_of_char l |> (lxor) c |> char_of_int

As:

fun l -> char_of_int (c lxor (int_of_char l))

Pattern tricks

In places like this:

let (Xor_input input) =

You can omit the parens and just write:

let Xor_input input =

You can also perform several layers of pattern matching in one step. So rather than writing

let sort_results { score = first; _ } { score = second; _ } =
  let (Score first_val) = first in
  let (Score second_val) = second in

You can write:

let sort_results { score = Score first_val; _ } { score = Score second_val; _ } =

Record shorthand syntax

When building a record, parts that look like label = label can be written just label. So you can rename variables and change:

let make_result input c =
  let output = xor c input in
  { test_char = char_of_int c; score = make_ranking output; xor_input = input; xor_output = output }

To:

let make_result xor_input c =
  let xor_output = xor c xor_input in
  { test_char = char_of_int c; score = make_ranking output; xor_input; xor_output }

Type design

I don't think that the xor_input and xor_output bring a lot here. You're probably be as OK with plain strings.

Other remarks

You are sorting a whole list just to get the minimal element. There is probably a function to do that in core (and it will be linear).

Instead of fprintf stdout, you can just use printf.

That's it! Have a nice journey learning OCaml and please post other examples on this site :)


What do you think about this?

var Request = {
    version: 1.0, //not needed but i like versioning things
    xproxy: function(type, url, data, callback, timeout, headers, contentType)
    {
        if (!timeout || timeout <= 0) { timeout = 15000; }
        $.ajax(
        {
            url: url,
            type: type,
            data: data,
            timeout: timeout,
            contentType: contentType,
            success:function(data)
            {
                if (callback != undefined) { callback(data); }
            },
            error:function(data)
            {
                if (callback != undefined) { callback(data); }
            },
            beforeSend: function(xhr)
            {
                //headers is a list with two items
                if(headers)
                {
                    xhr.setRequestHeader('secret-key', headers[0]);
                    xhr.setRequestHeader('api-key', headers[1]);
                }
            }
        });
    }
};

<script type="text/javascript">
var contentType = "applicaiton/json";
var url = "http://api.lastfm.com/get/data/";
var timeout = 1000*5; //five seconds
var requestType = "POST"; //GET, POST, DELETE, PUT

var header = [];
header.push("unique-guid");
header.push("23903820983");

var data = "{\"username\":\"james\"}"; //you should really deserialize this w/ a function
function callback(data)
{
   //do logic here
}
Request.xproxy(requestType, url, data, callback, timeout, header, contentType);
</script>


RXJava operate on exiting List instead of creating new

You're just abusing the library here. What you're doing can be done significantly more succinctly in standard java API:
public void sortDishes(int categoryId, List<Dish> dishes) {
    final Set<Integer> categories = new HashSet<Integer>();
    categories.add(categoryId);
    final Integer[] subCategories = dbInteractor.getSubCategories(categoryId);
    if (subCategories != null) {
        categories.addAll(Arrays.asList(subCategories));
    }

    final List<Dish> sortedDishes = dishes.stream()
        .filter(d -> categoryHelper.getCategories(d.getCategories())
                         .stream().anyMatch(categories::contains))
        .sort()
        .collect(Collectors.toList());
}

Note that this corrects a typo in getCategorys.

Further notes
Your dbInteractor's API could use a rework. I generally advocate against returning null from APIs that return a collection of elements, whether that's an Array or an actual Collection subclass.
I strongly suggest you consider returning new Integer[0] or even Collections.emptyList() because this will greatly simplify calling code by guaranteeing you're not returning null.
I also don't understand why the method is typed void. Either you actually modify the List you've received or you return a new List.


Reading set of large log files line by line and count how many hostnames appear on each line

So I think you can improve the efficiency of your code like this.

First, as I said in one comment, you can replace:

s=len(line.split('|'))
a=line.split('|')[s-3]

by

a=line.split('|')[-3]

as no need to know the total length of a list to get the third element from the end.

Second, assigning a then b with a value take some times, you can do it in one line:

a=line.split('|')[-3]
b=a.split('/')[0]

becomes

b=line.split('|')[-3].split('/')[0]

Third, I'm not sure len can equal to None, maybe you wanted to check for 0, but if our code run like this, I would say that:

if len(b.split('.'))==None:
    ''
else:
    b=b.split('.')[0]

is not useful so you can calculate directly the final b with:

b=line.split('|')[-3].split('/')[0].split('.')[0]

Forth, because actually you don't need to assign b anymore, you can append the value into rows directly, such as:

rows=[]
for line in f:
    rows.append(line.split('|')[-3].split('/')[0].split('.')[0])

or as a list comprehension:

rows = [line.split('|')[-3].split('/')[0].split('.')[0] for line in f]

Fifth, again, you create df_temp to use it once and then delete it, you can append directly into df_final such as:

df_temp= pd.DataFrame(columns=['hostname'],data=rows)
df_final=df_final.append(df_temp,ignore_index=True)
del df_temp

is better this way:

df_final=df_final.append(pd.DataFrame(columns=['hostname'],data=rows),
                         ignore_index=True)

Ultimately, rows is not necessary anymore, so all the code from the line with ... until the line del ... can be written:

with gzip.open(tt,'rt') as f:
    df_final=df_final.append(pd.DataFrame(columns=['hostname'],
                                          data=[line.split('|')[-3].split('/')[0].split('.')[0] for line in f]),
                             ignore_index=True)

So far, I think we saved some time, but I know that appening dataframe in a loop is not the best practice, especially because you need to assign again df_final each time. It's better to add all the dataframes that you want to append together in a list, and then use pd.concat outside of the loop. Your code becomes:

list_final = []
for file in listi:
    tt=os.path.join(directory,file)
    with gzip.open(tt,'rt') as f:
        list_final.append(pd.DataFrame(columns=['hostname'],
                                       data=[line.split('|')[-3].split('/')[0].split('.')[0]
                                             for line in f]))
df_final = (pd.concat(list_final,ignore_index=True)
              .groupby(["hostname"]).size().reset_index(name="Topic_Count"))

Timing

I create one file with around 3 millions of rows, running you method was 8.9 seconds while mine was 5.8 (a gain more than 30%). I run the code on a listi containg 10 of this file, and your method gave more than 91 seconds (a bit more than stricly 10 times the method with one file) while mine was about 57 seconds (a bit less than 10 times the method for just one file).

I don't know about all the multiprocessing or serializing calculations in Python, but it may be a good option too.


Short code in Python Rock Paper Scissors

I'll just go over some small improvements I noticed you can make.
my_win = 0
my_loss = 0
my_tie = 0

def game():

    global my_tie, my_loss, my_win

Global variables should be avoided whenever possible. The only reason I can see that these are globals is because of results() but you never actually call results(). It's probably better to just leave these as local variables.
var = {'scissors':(0,1,0),
       'paper':(0,0,1),
       'rock':(1,0,0)}

for k, v in var.items():
    if k == user:
        one = int(v.index(1))
    if k == computer:
        two = int(v.index(1))

A couple of comments here. First, you don't make use of the fact that your values in your dictionary are tuples instead of just ints. You can just use 0, 1, and 2 as values. Second, you're not taking advantage of the thing that dictionaries are best at: indexing. An improvement here would be:
var = {'scissors':1,
       'paper':2,
       'rock':0}

one = var[user]
two = var[computer]

Next:
if one < two:
    print(f"USER with {user} - WIN!")
    my_win += 1
elif one == two:
    print("==TIE==")
    my_tie += 1
else:
    print(f"COMPUTER with {computer} - WIN!")
    my_loss += 1

As I said in the comments, I don't think this is functionally correct. Either combination of the user and the computer choosing paper and rock will cause the rock to win. One of many fixes you could do is to add and abs(one - two) == 1 as a condition to your first if statement.
def results():

    print ("You win %d times!" % my_win)
    print ("You lose %d times!" % my_loss)
    print ("You tie %d times!" % my_tie)

This function is defined inside your game() function and is never called. Because your variables are local (as I suggested), you might want to put wins, losses, and ties as arguments for this function. You also don't have a way to play the game more than once, making this function pretty unhelpful. I'll leave adding a way to play multiple games to you since that's more of a code upgrade than a code review.
Here is the complete code with all my proposed changes:
import random

def game():
    my_win = 0
    my_loss = 0
    my_tie = 0

    var = {'scissors':1,
           'paper':2,
           'rock':0}

    user = input("Please choice scissors, paper or rock: ")
    while user not in ['scissors', 'paper', 'rock']:
            user = input("Please choice scissors, paper or rock: ")
    computer = random.choice(['scissors','paper','rock'])

    print(f"USER - {user} \nCOMPUTER - {computer}")

    one = var[user]
    two = var[computer]

    if one < two and abs(one - two) == 1:
        print(f"USER with {user} - WIN!")
        my_win += 1
    elif one == two:
        print("==TIE==")
        my_tie += 1
    else:
        print(f"COMPUTER with {computer} - WIN!")
        my_loss += 1

    results(my_win, my_loss, my_tie)

def results(win, loss, tie):

    print ("You win %d times!" % win)
    print ("You lose %d times!" % loss)
    print ("You tie %d times!" % tie)

if __name__ == "__main__":
    game()


Inside of your copyArray you should not have the cnt variable separate from the for loop, you should just include it in the declaration. The name of that Method should be in PascalCase not it camelCase.  You should also give operators some space (ie [end - start])

so this:

   private int[] copyArray(int[] A, int start, int end)
    {

        int[] result = new int[end-start];

        int cnt = 0;
        for (int i = start; i < end; i++)
        {
            result[cnt] = A[i];
            cnt++;
        }

        return result;
    }

would become this

private int[] CopyArray(int[] A, int start, int end)
{
    int[] result = new int[end - start];
    for (int i = start, cnt = 0; i < end; i++, cnt++)
    {
        result[cnt] = A[i];
    }
    return result;
}

you can remove the cnt++ from inside the loop and just add it to the for loop declaration.

Variables should be camelCase

 int[] LeftArray = copyArray(data,0,length);
 int[] RightArray = copyArray(data, length,data.Length);

These should be

 int[] leftArray = CopyArray(data,0,length);
 int[] rightArray = CopyArray(data, length,data.Length);

And

public int[] merge(int[] right, int[] left)

should be

public int[] Merge(int[] right, int[] left)

Because Methods should always be in PascalCase

And

int cntright = 0;
int cntleft = 0;

should be

int cntRight = 0;
int cntLeft = 0;

for the same reason


Portfolio object made of Asset classes and Securities

I've got a problem with your SecuritiesBasketType protocol and its extension.  The problem is best highlighted by taking a look at the multiple classes you've implemented to conform to this protocol.

class AssetClass : SecuritiesBasketType{

    var name : String = ""
    var securities = [SecurityType]()
    required init(){}
}

class WatchList: SecuritiesBasketType{

    var name : String = ""
    var securities = [SecurityType]()
    required init(){}
}

There are a couple of problems here.  First, why do we need two classes that are identical in every way except name?  We don't.

I understand that we will want assets that we actually own as well as assets that we're watching to help us make future decisions, but making different classes like this simply does not make sense.  Additionally, this name property makes relatively little sense, and having a zero-argument constructor plus var properties is problematic as far as I'm concerned.

But we must have the zero argument constructor because of the problem with the protocol.  We have conformed to the SecuritiesBasketType, which has a zero-argument initializer as part of its conformance.  This means that everything that conforms to this protocol as well as things that inherit from that must implement this zero-argument constructor with the required keyword.

In some cases, a required initializer makes sense.  Like, for example, with the NSCoding protocol (more reading).  But here, we've only required this zero argument constructor so that we can add two other constructors in the protocol extension.

But WHY?

What you have here is a protocol that should be a class.  You've done all of the work in the protocol and protocol extension, and it has resulted in very weird things.  It doesn't make much sense to subclass anything that conforms to this protocol and it doesn't make much sense to create more than one class that conforms to this protocol.  And when those two things are true (and they are here), then it doesn't make much sense to have the protocol at all.  We should instead have a class that looks something like this:

class SecuritiesBasket {
    let name: String
    var securities: [SecurityType]

    init(withName name: String, securities: [SecurityType]) {
        self.name = name
        self.securities = securities
    }

    func addSecurity(security: SecurityType) {
        securities.append(security)
    }
}

Now the SecuritiesBasket class does everything we wanted out of the SecuritiesBasketType protocol, but it does it without forcing any required initializer (which severely dictates a lot of the things you do with subclasses from that point).

In Swift, we want to adopt a protocol-oriented approach to programming.  Protocols allow us to do some powerful things.  But with that said, we need to use the right tool for the right job.

We use protocols to allow multiple implementations of the same interface.  The most classic example is with UITableView and its delegate/datasource properties.  All of the UI logic of the table view exists within UIKit, yet with just a handful of methods in the protocols, the table view can adapt to display whatever information you need it to display.


Java methods to add and authenticate users in MySQL

Funny coincidence, I just created the solution for your problem (but in PHP). The common thing here is the one-time use of a prepared statement, the difference is in the query string and arguments, so what you need is a function that accepts a query string, an argument determining what execution function to use (executeQuery() or executeUpdate()) and the parameters for the query:

// note: QueryType is an enum you should define
public static ResultSet query(String sql, QueryType queryType, Object... queryArgs){
    // ...
}

Include the try-catch blocks in this function too of course, and return null when according to queryType there should be no results.

Note: I have no experience with database interaction with Java, so if there are only 2 different statement execution functions then you can use a boolean argument instead of the enum QueryType.

Then your code could look like this:

public static boolean checkLogin(String username, String password) {
    String sql = "select * from users where username = ? and password = ? ";
    ResultSet result = query(sql, QueryType.RETURN_RESULT, username, password);
    return result.next(); // true if User exists
}

A note about your query string: I find it more convenient to use CAPS for keywords and lowercase text for names, it highlights the structure of the query.


Segmented Sieve of Eratosthenes in Scala

There is a lot of overlap between the concept of windowed Sieve of Eratosthenes and that of segmented one, which is why the two are often confuddled.

A windowed sieve is rigged such that it can sieve a window, that is a range of numbers in the interval [m, n] (in SPOJ PRIME1/PRINT parlance), without having to sieve all the numbers before the start of the interval as well. The main trick here is to use modulo operations to find where the crossing-off sequence for a prime first intersects with the sieve window.

You have already implemented this, but I'm showing it again in pseudo code that makes the logic clearer. It is for an odds-only sieve, which is why there is a division by 2 at the end of the offset calculation, which transforms the starting point from the realm of numbers into the realm of odds-only bit indices.

internal static ushort[] small_odd_primes; // up to sqrt(MAX_N)
internal static bool[] sieve = new bool[1 << 15];

internal static void sieve_primes_between (int m, int n)
{
    // ...

    int last_prime_index = Array.BinarySearch(small_odd_primes, (ushort)Math.Sqrt(n));
    if (last_prime_index < 0)
        last_prime_index = -last_prime_index - 2;

    for (int i = 0; i <= last_prime_index; ++i)
    {
        int prime = small_odd_primes[i];
        int start = prime * prime;
        int stride = prime << 1;

        if (start < m)
            start = (stride - 1) - (m - start - 1) % stride;
        else
            start -= m;

        offset[i] = start >> 1;
    }

    // ...

Of course, this not real pseudocode; it is my PRIME1/PRINT submission from my 'learn C#' tour of the world (fastest PRINT submission in C# and third fastest overall).

The modulo logic is a bit more complicated than what you did, for two reasons. First, I start crossing off at the square of a prime because all lower multiples will already have been crossed off during the runs for smaller primes. The effect minuscule, so don't worry about it. The second reason is that I wanted to minimise the number of branches necessary. Biassing the modulo operation by -1 makes it yield directly the desired result in the range 0 to stride-1, instead of giving 1 to stride where the last value has to be corrected to 0. Again, the effect is minor, but I wanted a clear, correct rendering of the windowed sieve offset logic on record.

Now, on to segmented operation. The purpose here is not to avoid the sieving of unwanted numbers as it is for a windowed sieve. The purpose is to sieve the desired range in small batches that do not exceed the size of the L1 cache of the target CPU (typically 32 KiB). For large sieve windows this can speed up things by orders of magnitude.

    for (int bits_to_sieve = ((n - m) >> 1) + 1; bits_to_sieve > 0; )
    {
        int window_bits = Math.Min(bits_to_sieve, sieve.Length);

        Array.Clear(sieve, 0, window_bits);

        for (int i = first_sieve_prime_index; i <= last_prime_index; ++i)
        {
            int p = small_odd_primes[i], j = offset[i];

            for ( ; j < window_bits; j += p)
                sieve[j] = true;

            offset[i] = j - window_bits;
        }

        append_sieved_numbers_to_result_vector(m, window_bits);

        m += window_bits << 1;
        bits_to_sieve -= window_bits;
    }

I think this makes the core idea of the segmented sieve very clear. In this case I'm reusing the sieve segment over and over, which is why I have to subtract window_bits before stashing an offset back in its slot. The same logic can also be used to process a bigger sieve in small, L1-sized stripes (but there's no advantage to that for PRIME1 and PRINT).

append_sieved_numbers_to_result_vector() just goes through the sieved range and pulls out the primes; I'm not showing it because it is actually twice as long as the sieve code (with mod 6 stepping and unrolling and all that) and still it takes twice as long as the actual sieving. It's core logic is quite simple, though:

    for (int i = 0; i < window_bits; ++i)
        if (!sieve[i])
            result[result_size++] = m + (i << 1);

In my submission I'm blasting a precomputed bit pattern over the sieve instead of calling Array.Clear(), in order to avoid the many iterations necessary for crossing off the smallest primes. However, the effect is not major and nothing to worry about.

What you do have to worry about is efficient extraction of the primes from the sieve, and I'd definitely recommend using an odds-only sieve in order to halve your workload in one fell swoop.

You'll also have to worry - a lot - about efficient output of the sieved primes. My submission used custom conversion code that rendered the numbers directly into the output buffer, digit by digit, in-place without any copying/moving. Using C#'s lame builtin I/O would have added at least a second to the timing, threatening TLE. See SPOJ's INOUTEST for the 'fast bulk output of numbers' thing. There are a lot of problems on SPOJ where fast bulk output of numbers comes in handy, and where the output time can dominate the total timing by a factor of 100 or so if you use the slow builtin I/O that comes with most languages. My first ETF submission got TLE, and after replacing the output code - not the actual processing related to the task - it clocked 0.030 vs. the time limit of 0.161, a nice big reserve.

I cannot comment on Scala's mutable.BitSet but I can assure you that its equivalents in C++ (std::vector<bool>) and in C# (System.Collections.BitArray) are slow as molasses and slow things down by an order of magnitude.

Custom code using a byte array and manual bit indexing (sieve[j>>3] |= 1 << (j & 7)) still takes twice as long as the plain bool[] sieve. It can be made about 33% faster than bool[] by copious unrolling, but the effort required is on the same order of magnitude as that for implementing a mod 30 wheel. To give you an impression, here is one of eight functions, each of which sieves a different residue class mod 8:

static void sieve_residue_class_1 ()
{
    var pao = primes_and_offsets[1];

    for (int i = 0, e = pao.Length; i < e; ++i)
    {
        int p = pao[i].prime, j = pao[i].offset, dj = p << 3;

        if (j + dj + dj <= window_bits)
        {
            int b = j >> 3, db = p >> 3;

            switch (j & 7)
            {
                case 4:  sieve[b] |= 1 << 4;  j += p;  b += db + 0;  goto case 7;
                case 7:  sieve[b] |= 1 << 7;  j += p;  b += db + 1;  goto case 2;
                case 2:  sieve[b] |= 1 << 2;  j += p;  b += db + 0;  goto case 5;
                case 5:  sieve[b] |= 1 << 5;  j += p;  b += db + 1;  goto case 0;
                case 0:  sieve[b] |= 1 << 0;  j += p;  b += db + 0;  goto case 3;
                case 3:  sieve[b] |= 1 << 3;  j += p;  b += db + 0;  goto case 6;
                case 6:  sieve[b] |= 1 << 6;  j += p;  b += db + 1;  break;
            }

            for (int next_j = j + dj; next_j <= window_bits; j = next_j, next_j += dj, b += p)
            {
                sieve[b + 0 * db + 0] |= 1 << 1;
                sieve[b + 1 * db + 0] |= 1 << 4;
                sieve[b + 2 * db + 0] |= 1 << 7;
                sieve[b + 3 * db + 1] |= 1 << 2;
                sieve[b + 4 * db + 1] |= 1 << 5;
                sieve[b + 5 * db + 2] |= 1 << 0;
                sieve[b + 6 * db + 2] |= 1 << 3;
                sieve[b + 7 * db + 2] |= 1 << 6;
            }
        }

        for ( ; j < window_bits; j += p)
            sieve[j >> 3] |= (byte)(1 << (j & 7));

        pao[i].offset = j - window_bits;
    }
}

The effort just isn't worth it in most cases, and it exceeds the source code size limits for most tasks involving primes. Without such gargantuan efforts, a simple bool[] always wins. At least in C#, C++ and Delphi.

In C# I realised significant performance gains from allocating buffers (sieve, presieve pattern and so on) once in the constructor and using them over and over, instead of allocating and discarding as needed. Of course, this goes against the grain of languages like Scala but that's the reality of tasks like PRIME1 and language runtimes.

Moral: keep things simple and your caches hot.


Ordinary Data Processing Task in Haskell: Vague Misgivings

recursive part of minBal is mapM_
multiple get is better to refactor into one
get - process - put part of minBal is modify
state monad is not necessary - you can use plain foldr instead of mapM_ and modify
runningBal is better to implement using scanr. Then zip running balances and source list and foldr the resulting list of tuples
use minimum to find the minimum

Here is a more idiomatic approach to minBal:

runningBal' = scanl1 (+) . map amt

rb l = zip (runningBal' l) l

mb l = minimumBy (compare `on` fst) l

minDay = mb . rb

minBal' l = DtdBal {
    runningBal = fst $ last runningBals,
    lowDt = dt $ snd md,
    lowBal = fst md }
    where
        runningBals = rb l
        md = minDay l

You can inline all the small functions for the final code but that's the order I created them in. Note that rb is calculated twice, but I was not sure if you really need the final balance. In your algorithm it was a byproduct.

main can be rewritten too:

main' = (parseFromFile sched >=> process >=> print) "escrow.txt" where
    process (Right l) = return $ minBal' l
    process (Left _) = error "Oops"

Basically, putStrLn . show is print and composition is your friend. >=> is monadic variant of .. It's advantage over >>= is associativity - it lets you refactor your code more easily.

Here is a shorter version of minBal' which also saves a few calculations, but last is still O(N):

minBal' l = DtdBal {
    runningBal = last runningBals,
    lowDt = dt $ snd md,
    lowBal = fst md }
    where
        md = minimumBy (compare `on` fst) $ zip runningBals l
        runningBals = scanl1 (+) $ map amt l

Let me know how this works for very large files.


Finding the nearest agents to a customer using IComparable

var agents = db.GetAgents();
if(agents == null || agents.Count == 0)
{
    Console.WriteLine("No Results");
}

You should exit your application/code block when you detect an invalid state. Here even if "No Results" is printed, the app will still run into a NullReferenceException or IndexOutOfRangeException in the next few steps.  Also, talking of IndexOutOfRangeException, there is no check against if there is at least 5 agents in your db.

Instead of creating an IComparable, you can just use linq to sort directly. But, since in your case, you also need to print out the distance (the value used for sorting), we will need to create an anonymous class to hold it:

var agents = db.GetAgents();
if(agents == null || agents.Count == 0)
{
    Console.WriteLine("No Results");
    return;
}

var nearestAgents = agents
    .Select(x => new
    {
        x.AgentId,
        DistanceToCustomer = ws.GetDistanceMeters(x.Latitude, x.Longitude, customerLatitude, customerLongitude)
    })
    .OrderBy(x => x.DistanceToCustomer);
foreach (var agent in nearestAgents.Take(5))
{
    Console.WriteLine($"agent_id:  {agent.AgentId}  distance: {agent.DistanceToCustomer} m");
}

The .Take(5) ensures that only 5 agent will be printed out, or less.


Generating all size-k consecutive subsets in Haskell

Each evaluation of length list will take time proportional to the length of the list, which will slow this down.

We know that there will be length xs - n + 1 sublists of xs of length n. We can use this to make the code much faster.

sublists n xs = take (length xs - n + 1) $ sublists' n xs
    where sublists' _ [] = [[]]
          sublists' n xs@(_:rest) = take n xs : sublists' n rest

For comparison, with

main = print $ sum $ map length $ sublists 2 [1 .. 100000]

I get the following timings on my machine, compiled with -O.

Original version:

$ time ./orig
199998

real    0m14.890s
user    0m0.015s
sys     0m0.015s

This version:

$ time ./new
199998

real    0m0.065s
user    0m0.015s
sys     0m0.015s


Search for a value in tuple of lists/vectors

find :: Integer -> SeqV -> Maybe Integer
find n (a,b,c) | elem n a || elem n b || elem n c = Just n
find _ _ = Nothing

There's also (<|>) :: Maybe a -> Maybe a -> Maybe a and asum :: [Maybe a] -> Maybe a, but if you simplified your question and find above doesn't unsimplify, post the whole code and there might be an unreasonably short answer again. I also have the feeling wherever you use find could be simplified further since find only really returns a boolean.


When speeding up code, you need to measure the performance of the code on a standard test case, and then try many experiments focussing on the hot spots (the bits of code that are executed most often and take the majority of the time).

So the first thing to do is to refactor the code so that it is easily measurable.

def test1(key='a', target='3141592653'):
    """Return a string whose MD5 hexdigest starts with target. The search
    starts at the string key.

    """
    target_len = len(target)
    starttime = calendar.timegm(time.gmtime())
    while True:
        now = calendar.timegm(time.gmtime())
        if now - starttime > 30*60:
            starttime = now
        if str(hashlib.md5(key.encode('utf-8')).hexdigest())[0:target_len] == target:
            return key
        strip_zs = key.rstrip('z')
        if strip_zs:
            key = strip_zs[:-1] + chr(ord(strip_zs[-1]) + 1) + 'a' * (len(key) - len(strip_zs))
        else:
            key = 'a' * (len(key) + 1)

I've stripped the file-reading and writing code, and the exception handling, because it's not important for testing performance, and I've inlined the code for next_string so that we can improve that along with the rest of the code.

The refactored code is designed to be easy to measure — you can pass different values for target to get different levels of difficulty. Here's my chosen test case:

>>> from timeit import timeit
>>> timeit(lambda:test1(target='000000'), number=1)
10.682760871015489

The first thing we can try is to skip the code for periodic saving — if the KeyboardInterrupt handling is reliable, then there's no need for a second mechanism to save your progress.

def test2(key='a', target='3141592653'):
    """Return a string whose MD5 hexdigest starts with target. The search
    starts at the string key.

    """
    target_len = len(target)
    while True:
        if str(hashlib.md5(key.encode('utf-8')).hexdigest())[0:target_len] == target:
            return key
        strip_zs = key.rstrip('z')
        if strip_zs:
            key = strip_zs[:-1] + chr(ord(strip_zs[-1]) + 1) + 'a' * (len(key) - len(strip_zs))
        else:
            key = 'a' * (len(key) + 1)

This is about twice as fast as test1:

>>> timeit(lambda:test2(target='000000'), number=1)
5.272445386042818

This means that about half the time in test1 was being spent in calendar.timegm and time.gmtime!

The next thing to try is to avoid the encoding and decoding steps. The only characters you use are lower-case "a" to "z", and these all encode to UTF-8 as single bytes. So we could maintain a bytearray instead of a string.

def test3(key='a', target='3141592653'):
    """Return a string whose MD5 hexdigest starts with target. The search
    starts at the string key.

    """
    key = bytearray(key.encode('ascii'))
    key_indexes = range(len(key) - 1, -1, -1)
    md5 = hashlib.md5
    a, z = b'az'
    while True:
        if md5(key).hexdigest().startswith(target):
            return key.decode('ascii')
        for i in key_indexes:
            key[i] += 1
            if key[i] > z:
                key[i] = a
                if i == 0:
                    key.append(a)
                    key_indexes = range(len(key) - 1, -1, -1)
            else:
                break

Note that I've also cached hashlib.md5 in a local variable to avoid having to look it up on each iteration.

This is about three times as fast as test1:

>>> timeit(lambda:test3(target='000000'), number=1)
3.3292323709465563

Instead of calling the hexdigest method, we could call digest instead:

import binascii

def test4(key='a', target='3141592653'):
    """Return a string whose MD5 hexdigest starts with target. The search
    starts at the string key.

    """
    key = bytearray(key.encode('ascii'))
    key_indexes = range(len(key) - 1, -1, -1)
    target_digest = binascii.unhexlify(target)
    md5 = hashlib.md5
    a, z = b'az'
    while True:
        if md5(key).digest().startswith(target_digest):
            return key.decode('ascii')
        for i in key_indexes:
            key[i] += 1
            if key[i] > z:
                key[i] = a
                if i == 0:
                    key.append(a)
                    key_indexes = range(len(key) - 1, -1, -1)
            else:
                break

(Note that this imposes the limitation that the target has an even number of hexadecimal digits.)

This gives a small further improvement:

>>> timeit(lambda:test4(target='000000'), number=1)
3.105254566995427

That's the limit of piecewise improvements that I can spot, so it doesn't look as though this approach is going to get you anywhere near to 12 digits. We expect the time taken to grow by about 16 times for each additional hexadecimal digit, so the expected runtime of test4(target='314159265358') would be well over a year.


Get client information with GET request in GO API

If you have m fields and n names then you are iterating O(m * n) times. For all fields and names, circa 60 * 60 = 3600 times. Only loop O(m) times:
for field, _ := range m {
    if !names[field] {
        delete(m, field)
    }
}

With some improvements:
package main

import (
    "encoding/json"
    "fmt"
    "regexp"
    "strings"
)

var fieldNameRegex = regexp.MustCompile(`^\w+$`)

func structToMap(client interface{}, fieldNames []string) (map[string]interface{}, error) {
    names := make(map[string]bool, len(fieldNames))
    for _, name := range fieldNames {
        name = strings.TrimSpace(name)
        if !fieldNameRegex.MatchString(name) {
            return nil, fmt.Errorf("Invalid field name: `%s`", name)
        }
        name = strings.ToLower(name)
        names[name] = true
    }

    j, err := json.Marshal(client)
    if err != nil {
        return nil, err
    }
    m := map[string]interface{}{}
    err = json.Unmarshal(j, &m)
    if err != nil {
        return nil, err
    }
    for field, _ := range m {
        if !names[field] {
            delete(m, field)
        }
    }
    if len(m) != len(names) {
        return nil, fmt.Errorf("field(s) not found")
    }
    return m, nil
}

type Client struct {
    AltAddress string `json:"alt_address"`
    Firstname  string `json:"firstname"`
    Lastname   string `json:"lastname"`
}

func main() {
    client := Client{
        AltAddress: "HalLo",
        Lastname:   "smith",
    }

    m, err := structToMap(client, []string{"alt_address", "firstname"})
    if err != nil {
        fmt.Println("error1:", err)
        return
    }
    j, err := json.Marshal(m)
    if err != nil {
        fmt.Println("error2:", err)
        return
    }
    fmt.Println(string(j))
}

Playground: https://play.golang.org/p/Qf8bdygEcVT
{"alt_address":"HalLo","firstname":""}