Printing out a tree set using pattern matching in Scala

Some ideas:

Make the trait sealed.
combine doesn't need to be a nested function, this only makes it harder to read and causes problems with shadowed variables. Instead you could move it to an accompanying object, or make it a separate method such as:

sealed trait IntSet {
    ...
    private def combine(withRight: IntSet): IntSet;
}

for EmptySet it will just return withRight, for a NonEmptySet it will be

 def combine(withRight: IntSet): IntSet =
    NonEmptySet(elem, left.combine(right), withRight);

toString is somewhat complicated, and can suffer from performance problems due to repeatedly concatenating strings together. Instead I'd suggest you to implement Traversable. Not only this will make your class much more useful, it's implementation will be cleaner, because it won't be cluttered by operating with strings. And then you'll already have mkString which you can use as

override def toString: String = mkString("{", ",", "}")


Print the value of the fractions correct to 3 decimal places.

You didn't really do this part. You're just printing the full float. You can use str.format() to print to three places. With this syntax:

print "{:.3f}".format(count_positives / N)

The .3f syntax tells Python to print the first three digits only. Note that str.format will actually round the value, not just truncate it:

"{:.3f}".format(1.3449)
>>> '1.345'

Though, since you're just printing the value by itself, you can use the format builtin where you pass the string and the format as a string.

format(count_positives / N, '.3f')

You don't need a filter to count the zeros. Lists have a built in count function which will return the number of times a particular object occurs in it. Like this:

count_zeros = ary.count(0)


size and i in reverse look superficial. Consider
void reverse(int A[], int l, int r) {
    while (l < r) {
        pswap(&A[l++], &A[r--]);
    }
}

The outer loop looks very strange. Its body is executed exactly once, when the longest decreasing interval is detected. Consider lifting the body out of the loop.
As a side point, I don't see the point of struct {int i; int j}. It is initialized to {r-1, r}, and at each iteration both members are decremented. z.i is always equal to  z.j - 1. Keeping just one int is enough. Consider
  int j = r; // j will point to the highest peak of the interval [i,r].
  // this loop will find the first increasing [i,j] from r to l.
  while (j > l) {
      if (A[j-1] < A[j]) {
          break;
      }
      --j;
  }

  if (j > l) {
      int k = r;
      // find the first A[k] where A[k] > A[j-1].
      while (!(A[j-1] < A[k])) {
          --k;
      }
      pswap(&A[j-1], &A[k]);
      reverse(A, j, r);
      return true;
  }

  reverse(A, j, r);
  return false;

It already looks cleaner, but let's get going. An obvious next step is to get rid of the duplicate calls to reverse:
  if (j > l) {
      ....
      pswap(....);
  }
  reverse (A, j, r);
  return j > l;

No naked loops please. If you felt compelled to put comments like
      // find the maximal decreasing interval [j,r]
      // find the first A[k] where A[k] > A[z.i].

you admit that you failed to express the intentions in the code. Consider refactoring these loops into functions. Consider
  int j = find_decreasing_interval(A, l, r);
  if (l < j) {
      int k = lower_bound(A, j, r, A[j-1]);
      pswap(&A[j-1], &A[k]);
  }
  reverse((A, j, r);
  return j > l;


You're doing a good job at indenting your code, well done! But then why would you need these?

            }   // end if
        }   // end foreach(s)
    }   // end foreach(t)
}   // end foreach(r)

Now, the signature:

private static void GetResults(ref ObservableCollection<string> resultTitles, ref string[] query, ref ObservableCollection<int> weight)

"void GetResults"... "void Get". And then the two ref parameters...

  and passes the query and two empty ObservableCollections to this function.

query doesn't need to be passed by reference, doesn't need to be specifically an array, and if I had to use ref parameters I'd put it first in the signature:

private static void GetResults(IEnumerable<string> query, ref ObservableCollection<string> resultTitles, ref ObservableCollection<int> weight)

Now, the method doesn't care that our collections are observable - it only needs to know it's some kind of ICollection<T>... but there's a smell there nonetheless: passing ref parameters instead of returning a collection, doesn't feel quite natural.

I believe, because it's a private method, that whatever you're passing in as weight could be promoted to a private field if it's used anywhere else in the class, or moved to the GetResults scope where it's used.


Find the kth prime and all the primes below it

Before we talk about efficiency, let's talk about something very important: readability. It took me an unreasonably long time to understand what it was your code was doing, due to lots of single-letter variable names, confusing flow control, and odd choices for functions.

Checking for primality

Let's start with f. f is apparently determining the primality of n, so let's call it is_prime. Furthermore, it's not actually determining the primality of n, it's determining the primality of 2n+1. That's too much for one function - let's change it's responsibility to take the number it tests. The way you're doing it (taking the % against every prime) is a good algorithm, but then you're multiplying the mods together. That doesn't make sense as an operation. You don't care about the product, you care if any of them are zero! And as soon as one of them is zero, you can return False.

Here's a clearer function:

def is_prime(n):
    for p in primes:
        if n%p == 0:
            return False
    return True

Or if you're generator inclined:

def is_prime(n):
    return not any(n%p == 0 for p in primes)

Main Loop

We need to find k primes. You are keeping track of this by comparing k to n and occasionally incrementing k. That is confusing. Since we're already keeping track of all the primes, the termination condition should be:

len(primes) >= k

And then we can handle iteration by the odd numbers with itertools.count():

num_primes = input()
primes = [2]

def is_prime(n): ...

for n in itertools.count(start=3, step=2):
    if is_prime(n):
        primes.append(n)

        if len(primes) >= num_primes:
            break

This program is far easier to understand.

Efficiency

We could do better in terms of efficiency. At a first approach, we're checking all the odd numbers but we can drop the multiples of 3 earlier by simply writing our own generator that spits out 3, 5, and then alternates adding 2 and 4. That's a quick optimization that let's us keep the same algorithm.

Better would be to use an entirely different, and very well-known algorithm: the Sieve of Eratosthenes. It is a much better algorithm for this problem.


As 200_success pointed out, you have not implemented a sieve. See Wikipedia's pseudo code for the Sieve of Eratosthenes.

You need to check if reading in a number was actually successful:

if (!(std::cin >> number)) {
    std::cerr << "Invalid number provided" << std::endl;
    return EXIT_FAILURE;
}

(Note that you would need cstdlib for EXIT_FAILURE.)

number is a bad name. It's an int--of course it's a number. What is it's meaning? Get rid of CAP, and name number something more meaningful like maxNumber (or ideally something better -- I'm having a naming mental block).

You should only ever use bare dynamic arrays if you are implementing a container or some other low level structure. They are non-exception safe, and you have to remember to clean up the memory. Just use a pre-allocated vector, and you can retain the same performance (or maybe even better in this relatively rare circumstance).

std::vector<bool> prime(CAP, true);

Note that std::vector<bool> has a specialization that uses packing to save space at the cost of a few extra cycles and some rather odd semantics (you actually get a proxy object from the vector rather than a direct reference to the element).

As Jerry Coffin noted, this task is almost certainly memory constrained rather than CPU, so the higher throughput of bit-based bools should actually provide faster performance.

Also, note that as a bonus on top of automatic memory management, you get to remove your initializing loop.

Though in toy programs it doesn't matter, using namespace std; is considered harmful, and it's a bad habit to form. Instead, use using std::cout;, using std::endl;, etc to only import certain symbols (and do it inside of a function, not at the global level).

It's not technically wrong, but it's much, much more common to use a space between #include and the file:

#include <iostream>

//if %i == 0 mark false this comment says the exact same thing the code does. Either make it much more meaningful (// mark the number non-prime if it is divisible by i), or--better in this situation--just remove it.

Interactive programs should be avoided if at all possible. They cannot be chained with other commands in a scripted fashion, and they are prone to user error. Instead, when you're only accepting one or a few simple user inputs, just use arguments to the program (i.e. use argv to get the maximum number rather than using std::cin).

In C++, if a return value is not specified in main, it is assumed to be EXIT_SUCCESS (0). Because of this, I like to omit return values in main when it's not possible for the program to result in a non successful return. Seeing a return in main makes me immediately wonder if it can fail. (Your program actually should be able to fail, so I would keep the return, you just should also have some error checking on the input reading).


Regardless of whether the function is actually correct or not, there are some issues that could be improved:

I would expect a function that is supposed to determine whether a relation \$R\$ over a set \$A\$ is reflexive to take two arguments:

The relation \$R\$,
and the set \$A\$.

Providing only the set \$\{(a, b) | a, b \in A, R(a,b)\}\$ and deducing \$A\$ from it is insufficient, because there could be elements in \$A\$ which are not related to any other element. This could lead to a false positive result.
I would expect the function to only return a boolean value, and to have no side effects like printing its result. Leave that to the caller.
The docstring of the function does not explain well enough what it expects as its argument, and does not explain at all what the function does.
a and y are poorly named. What is their meaning?
Instead of creating a and y a as lists to which is appended (needlessly checking if elements are already contained), only to convert them to sets at the end, why don't you just create them as sets in the first place and add to them?
Parentheses are not needed around conditions in if statements:

if a1 == a2:
if a1 not in a:
if set(a) == set(y):

(also technically not around the returned tuples)

return result, True
return [], False

This comment confuses me: What is a0?

if (a1 not in a):
    a.append(a1) #contains list of a0

You better just remove the comment.


Error handling for a simple 'fetch some data, then save the data' Node function

In my opinion it is much better to place the saveData call inside the first try-catch block.

This avoids calling saveData with an undefined value in case an exception occurs in calls to fetch or data.json, and gives the json variable a clean scope.

Depending on the granularity needed for error handling I would prefer to surround

await saveData(json);

with try-catch if necessary. However, a switch statement could also be a natural approach if you have a clear distinction on the exceptions thrown (e.g. error numbers).


Python web crawler counting words in all pages in a domain

It's readable and gets the job done as a script, but there is some redundancy in your if cases. Here's a streamlined version:

import requests
from urllib.request import urlopen
from bs4 import BeautifulSoup

base_url = "https://independencerpgs.com/"
html = urlopen(base_url)
bsObj = BeautifulSoup(html.read(), features="html.parser");

# Sets handle duplicate items automatically
urls=set()
for link in bsObj.find_all('a'):
    urls.add(link.get('href'))
print(urls)

words=0
for url in urls:
    # Having the logic this way saves 4 spaces indent on all the logic
    if url in ["NULL", "_blank", "None", None, "NoneType", base_url]:
        continue

    # The == base_url case is handled in the above `if`
    if url[0] == "/":
        specific_url = base_url + url # requests.get does not care about the num of '/'
    else:
        specific_url = url

    r = requests.get(specific_url)
    soup = BeautifulSoup(r.text, features="html.parser")
    for script in soup(["script", "style"]):
        # Use clear rather than extract
        script.clear()
    # text is text you don't need to preprocess it just yet.
    text = soup.get_text()
    print(f"{specific_url}: {len(text)} words")
    words += len(text)

print(f"Total: {words} words")

Depending on what you intend to do with that code, you might want to put it into functions or even create a class around it too.


Some notes:

From the moment you write hash[n] += 1, your solution is not functional anymore.
each_with_object is also imperative, for FP you should use reduce.
reduce works well with linked lists, not so well for generating new arrays. In any case, reduce is a generic abstraction, there is a more specific one to the problem at hand: Enumerable#chunk.

That's how I'd write it:

xs.chunk(&:itself).map { |y, ys| ys.size == 1 ? y : {y => ys.size} }


The guessing takes too long because picking a random number is not the best idea.
For example if the number to guess is 100,
and the computer is really really unlucky,
it might pick the numbers 1, 2, 3, 4, 5, 6, ... and take 100 guesses!

A better way is to halve the intervals.
That is, if you know the number must be between 1 and 100, then pick 50.
If that's too high, and so you know the number is between 1 and 50,
then pick 25. And so on.

So, changing one line could make a huge difference:

    // randNum = rand.nextInt(upperLimit - lowerLimit + 1) + lowerLimit;
    randNum = (upperLimit + lowerLimit + 1) / 2;

This is called a binary search.
As @cbojar pointed out in comments,
since binary search takes \$\log_2(n)\$ tests where \$n\$ is the total number of objects, and since \$\log_2(100) = 6.643856\$,
that means it's guaranteed that this algorithm will find the correct number in at most 7 steps.

The string literals "th", "tl", "y" that control the flow of the program are buried inside the code.
If you read this program a month from now,
it might not be obvious how it works until you read the details.
Something that would help with that,
and a good practice is to extract these string literals to constants,
for example:

private static final String TOO_HIGH_MARKER = "th";
private static final String TOO_LOW_MARKER = "tl";
private static final String CORRECT_MARKER = "y";

// ...

public static void main(String[] args) {
    // ...

    do {
        // ...

        if (myAnswer.equals(TOO_LOW_MARKER)) {  //too low
            lowerLimit = randNum + 1;
        } else if (myAnswer.equals(TOO_HIGH_MARKER)) { // too high
            upperLimit = randNum - 1;
        }

        // ...
    } while (!myAnswer.equals(CORRECT_MARKER));

Now all these magical strings are easy to see near the top.
Should you decide to use different values,
you could just edit them right at the top, simple and clear.

UPDATE

As an extra touch,
as @Tom pointed out in a comment,
it might be a good / interesting idea to make these equals comparisons a bit more fault tolerant by making them case insensitive by changing to equalsIgnoreCase, for example:

        if (myAnswer.equalsIgnoreCase(TOO_LOW_MARKER)) {  //too low

This is of course less efficient than a simple equals,
so don't use it excessively everywhere,
only when justified.

These initializers are redundant:

int randNum = 0;
String myAnswer = "";

Because, these variables will always be assigned before they are used.
As such, you can remove the initializers:

int randNum;
String myAnswer;

It's a cosmetic thing,
but your formatting doesn't follow the standard in a few places.

Instead of this:

if(myAnswer.equals("tl")) {  //too low
    lowerLimit = randNum + 1;
}
else if(myAnswer.equals("th")) { // too high
    upperLimit = randNum - 1;
}

It should be like this:

if (myAnswer.equals("tl")) {  //too low
    lowerLimit = randNum + 1;
} else if (myAnswer.equals("th")) { // too high
    upperLimit = randNum - 1;
}

And instead of this:

}while(!myAnswer.equals("y"));

It should be like this:

} while (!myAnswer.equals("y"));

Again, these are minor issues, but as your programs get larger and larger these little details can add up and seriously hurt readability.
Use editor environments like Eclipse or IntelliJ or Netbeans that can automatically reformat your code following the standard.


When comparing against "yes" or "no", you should consider using equalsIgnoreCase so that answers like YES can be accepted. (Perhaps the player is excited to play?)
startGame: I cannot tell what the x argument is.  It should be renamed.
gamePrize: Rather than hard-coding the 1-6 figures in your switch statement, you should make each one a constant (ex. VAL_ORANGE).
result: I would not know that you arguments are the number of occurrences of each type.  These should probably be renamed.
result: You currently have 1 cherry as winning $7 and 3 as winning $2.  This appears to be a bug.


One way to speed up the lookups is to use TryGetValue() instead of ContainsKey() and then the indexer. So, for example:

if (hashs1[i].ContainsKey(filenamef))
{
    sw.Write(hashs1[i][filenamef]);
}

would become:

string value;
if (hashs1[i].TryGetValue(filenamef, out value))
{
    sw.Write(value);
}

But it's hard to tell how much would that help you.

Another thing that might help you would be combining all the hashes into one that contains object that contains all the inner hashes. Something like:

class Hashes // or struct?
{
    public Dictionary<string, string> hash1 { get; set; }
    public Dictionary<string, string> hash2 { get; set; }
    public Dictionary<string, string> hash3 { get; set; }
    public Dictionary<string, string> hash4 { get; set; }
}

…

var mainHash = new Dictionary<int, Hashes>(f.Count + 2);

Combining the two approaches lowers the number of potential lookups per iteration from 16 to 5.

Also, unrelated to performance, but you should name your variables by their contents, not implementation. I think hash1 is a very bad name.


Best way to locate Find on List with reflection

When searching for methods using reflection I found that the most future-proof way is by searching exactly for the method, so not only the Count() is correct but I would also have checked both what is this parameter type and what is the return type of the function.


Python Pandas Dataframe code takes too long to finish

You could do the entire thing in pandas and numpy as their Cython implementation will be much faster than iterating over objects in Python.

First simulate some data,

import pandas as pd, numpy as np
from datetime import datetime

num_samples = 50000
user_ids = np.arange(num_samples)
dates = pd.date_range("2012-01-01","2015-01-01")
start_dates = dates[np.random.randint(0,len(dates),50000)]

data = pd.DataFrame(data={"user_id": user_ids, "start_date":start_dates})
data["end_date"] = data.start_date.apply(lambda x: x + pd.DateOffset(days=np.random.randint(0,300)))

which results in data:

start_date  user_id   end_date
2013-12-15        0   2014-09-24
2013-12-13        1   2014-01-17
2014-08-29        2   2015-03-25
2014-04-13        3   2015-01-04
2014-01-21        4   2014-06-22

We need an output dataframe so that we don't have to painfully iterate over each date one by one:

output = pd.DataFrame({"date":pd.date_range(data.start_date.min(), datetime.now())})

and now define your functions to calculate the churn rate and apply it over the entire data frame:

def get_users_from_date(df, date):
    return df[(date >= df.start_date) & (date <= df.end_date)].user_id

def calc_churn(df, date):
    users_before = get_users_from_date(df,date-pd.DateOffset(months=1))
    users_now = get_users_from_date(df,date)

    if len(users_before):
            return len(np.setdiff1d(users_before,users_now)) / len(users_before)
    else:
            return 0

output["churn"] = output.date.apply(lambda x: calc_churn(data,x))

This finishes for me in just:

  5.42 s ± 65.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Since we are using np.setdiff1d to find the difference, we don't need to unique each subset of user ids beforehand. Finding the unique subsets in get_user_from_date then passing them in, even with assume_unique=True, gets a time of:

  5.98 s ± 75 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

while using Python set difference instead of numpy arrays gets a time of:

  7.5 s ± 114 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

and finally, combining Python sets with list comprehensions, similar to your current implementation never finished running.

The main bottleneck is in comparing the sets of user ids, so doing that in either numpy or pandas instead of iterating over Python objects will improve performance.

On a different note, your code looks good and is really easy to read but you should use snake case instead of camel case to fit Python convention.


Blog markup

The site title should be a h1.

Use an ul for the navigation links. Or, if you don’t want that, use at least a div element for each link, or a textual separator between each link.

The page title (+ the content under its scope) needs be enclosed in a sectioning element (probably section in this case). Also, don’t include the page title in the site’s header.

The main element should also contain the page title.

Each post excerpt should get its own article (as a general rule: don’t use headings in list items (e.g. li) unless they have a sectioning content parent).

You may use the time element for the dates.

Which leads to (excluding anything not related to the outline):

Freundliche Artikel

We can't stop here

Introduction anyone?

Post markup

Again, use the site heading as h1 (as a child of body, without any sectioning content element parents).

Again, use ul for the navigation links etc.

You may use the time element for the dates.

Your footer contains information about the whole page (body). So if one of the articles is written by a different author, the article should get its own footer.

Some further explanations from the comments:

(a) Why the site name should be bodys h1

Please see my answer to a different question. The last example (HTML + the corresponding outline) applies to your case.

In short: When you don’t use a heading for the site name, your main content heading will be used as the heading for the whole body. Everything else on this page will be in scope of this heading. But there are things on your page which shouldn’t be in its scope, for example your navigation.

In your current HTML, the nav (which clearly represents navigation for the whole site) would be in the scope of "Freundliche Artikel" (while your nav might be friendly, it’s clearly not an article ;-)).

(b) Why the logo can be used for (a)

(You need to agree with (a) first.)

Headings are not for text only. A logo typically represents the site name (blog name, company name, whatever). It doesn’t matter if the logo includes a textual representation or if it’s graphics only. In both cases, the alt text would be the textual site name, as this is what the logo stands for.

(c) Why you can’t use sectioning elements (for your main content) when your main content is not included in a sectioning element and you have a site heading

Let’s take this (wrong) example:

Artikelschmiede

Freundliche Artikel

We can't stop here

Introduction anyone?

The outline is:

1. Artikelschmiede
  1.1 Freundliche Artikel
  1.2 We can't stop here
  1.3 Introduction anyone?

But the outline should be:

1. Artikelschmiede
  1.1 Freundliche Artikel
    2.1 We can't stop here
    2.2 Introduction anyone?

So to get the correct outline, use a sectioning element for the whole main content (typically section or article):

Artikelschmiede

Freundliche Artikel

We can't stop here

Introduction anyone?


Porting functional programming code to Linq

I've hardly written a line of Haskell in my life... but hopefully that isn't too important since you're happy with plain old concrete types here.

As Adriano Repetti said in his comment, working with IEnumerable<T> rather than List<T> - or pretty much anything else - allows for lazy evaluation: your Prepend extension method is a beautiful example of this already. Most implementations of IEnumerable are nice enough to not break when you try to use them in funky ways through LINQ, so don't worry about that. If you are worried about that, then just .ToArray() the input (or use an IReadOnlyList<T>) so that you know you can trust the input.

Doing a quick job of replacing all those lists with IEnumerable<T>s and removing the calls to .ToList() gives this:

private static IEnumerable<IEnumerable<T>> FilterND<T>(Func<T, IEnumerable<bool>> preds, IEnumerable<T> xs)
{
    if (xs.Any())
    {
        return
            preds(xs.First()).SelectMany(b =>
                FilterND(preds, xs.Skip(1)).SelectMany(ys =>
                    b
                        ? new [] { ys.Prepend(head) }
                        : new [] { ys }
                    )
                );
    }
    else
    {
        return new [] { Enumerable.Empty<T>() };
    }
}

Note that I use arrays for 'throw away' fixed-length enumerables: they will be (marginally) faster, and provide a much tidier syntax. Enumerable.Empty<T>() is used rather than new T[0] or new T[] { } just because it is absolutely clear that I just want something empty.

Prepend is lazy, so is SelectMany: this whole package is now lazy. You can kind-of ratify this by putting a print line in Prepend, and writing the following code:

var ps = PowersetNess.PowerSet(new[] { 1, 2, 3 });

int si = 0;
foreach (var s in ps)
{
    Console.WriteLine(si++ + "\t" + string.Join(", ", s));
}

Without the print in Prepend the output will look like this:

0       1, 2, 3
1       1, 2
2       1, 3
3       1
4       2, 3
5       2
6       3
7

With the prepend, we can see that the calls are interspersed everywhere, which implies that something is being lazy.

Prepend!
Prepend!
Prepend!
0       1, 2, 3
Prepend!
Prepend!
1       1, 2
Prepend!
Prepend!
2       1, 3
Prepend!
3       1
Prepend!
Prepend!
4       2, 3
Prepend!
5       2
Prepend!
6       3
7

Naturally, if you insert those .ToList() calls back in, then you will find all the Prepend!s at the top, because you are forcing evaluation and caching.

Now indulge me while I pull out a couple of variables and rename b to includeHead...

private static IEnumerable<IEnumerable<T>> FilterND<T>(Func<T, IEnumerable<bool>> preds, IEnumerable<T> xs)
{
    if (xs.Any())
    {
        T head = xs.First();
        IEnumerable<T> tail = xs.Skip(1);

        return
            preds(head).SelectMany(includeHead =>
                FilterND(preds, tail).SelectMany(ys =>
                    includeHead
                        ? new [] { ys.Prepend(head) }
                        : new [] { ys }
                    )
                );
    }
    else
    {
        // just the empty set
        return new [] { Enumerable.Empty<T>() };
    }
}

Perhaps it is somehow clearer in Haskell to use >>= rather than a classic map, but the inner SelectMany can just be a Select, which will improve performance and reduce clutter rather.

private static IEnumerable<IEnumerable<T>> FilterND<T>(Func<T, IEnumerable<bool>> preds, IEnumerable<T> xs)
{
    if (xs.Any())
    {
        T head = xs.First();
        IEnumerable<T> tail = xs.Skip(1);

        return
            preds(head).SelectMany(includeHead =>
                FilterND(preds, tail).Select(ys =>
                    includeHead
                        ? Cons(head, ys)
                        : ys
                    )
                );
    }
    else
    {
        // just the empty set
        return new [] { Enumerable.Empty<T>() };
    }
}

You can also externalise the includeHead check, which will of course reduce overhead, and (I think) makes the whole method much more understandable.

private static IEnumerable<IEnumerable<T>> FilterND<T>(Func<T, IEnumerable<bool>> preds, IEnumerable<T> xs)
{
    if (xs.Any())
    {
        T head = xs.First();
        IEnumerable<T> tail = xs.Skip(1);

        return
            preds(head).SelectMany(includeHead =>
                includeHead
                    ? FilterND(preds, tail).Select(ys => Cons(head, ys))
                    : FilterND(preds, tail)
                );
    }
    else
    {
        // just the empty set
        return new [] { Enumerable.Empty<T>() };
    }
}

To answer your question 2: you don't need or want any out, ref, or in here. You could write a Decons method with out params to cleanly extract the head and tail of a list (shown below for fun), but it's probably not worth it, and doesn't have the same typing gaurentees as proper pattern matching.

public static bool Decons<T>(this IEnumerable<T> list, out T head, out IEnumerable<T> tail)
{
    if (list.Any())
    {
        head = list.First();
        tail = list.Skip(1);
        return true;
    }
    else
    {
        head = default(T);
        tail = null;
        return false;
    }
}

If I was using such a method in 'real' code it would be called TryDeconstruct or something. The pattern matching feature that are slowly being fed into C# may one day allow code looks like if (vs is (head, tail)), but I don't keep on top of that stuff.

Here is a (in my opinion very readable) version which uses a pair of nested foreach loops, and the Decons method.

private static IEnumerable<IEnumerable<T>> FilterND<T>(Func<T, IEnumerable<bool>> preds, IEnumerable<T> xs)
{
    if (xs.Decons(out var head, out var tail))
    {
        foreach (var includeHead in preds(head))
        {
            foreach (var partialSubSet in FilterND(preds, tail))
            {
                if (includeHead)
                    yield return partialSubSet.Prepend(head);
                else
                    yield return partialSubSet;
            }
        }
    }
    else
    {
        // just the empty set
        yield return Enumerable.Empty<T>();
    }
}

I hope that flood of C# was of some interest!

One final thing: you can tidy the definition of preds a bit (should it be predicates?) by using a local function.

Func<T, List<bool>> preds = (x => new List<bool> { true, false });
bool[] predicates(T x) => new bool[] { true, false };

This is just a matter of taste really (slightly surprised you didn't go with return FilterND<T>(x => new bool[] { true, false }, xs);). You could reduce the number of allocations by creating a single bool[] and always returning it, if performance is a serious concern, but this is the wrong place to be looking for a performance advantage.

P.S.: thanks for posting this, I had a lot of fun with it


When one property is calculated from another

The C# property model allows external classes to inspect (or set) a given member as though it were a public 'field', and the implementation details are left to the property's accessor and mutator.  In your case, you want to expose TotalCost and hide the implementation details about how it is derived.  And your code reflects best practices.

Following the comment from Clockwork-Muse, your implementation can be made more elegant by...

    public decimal TotalCost
    {
        get { return CostOfFood * NumberOfPeople; }
    }

This avoids the calculation penalty for setting either of the calculation ingredients and performs the calculation only when called upon to do so.  It's also a bit more readable and transparent.  In this particular case, there's no need for an asymmetric mutator, so it's been removed.


Function to format numbers with thousand separators and decimals

You can use default parameters to automatically assign a value if none is given.

function neat(numToFormat, thousandSep = ".", decimalSep = ",")

You should start by checking if the input is valid, instead of doing it at the end. It would also be better to throw an error, since it's not easy to tell from the result if the formatting succeeded or not.

if (typeof numToFormat == 'number') {
  if(Number.isNaN(numToFormat) || !Number.isFinite(numToFormat)) {
    throw new Error("Invalid number");
  }
}
else if(typeof numToFormat == 'string') {
  if(!/^\d+((,|\.)\d+)?$/.test(numToFormat)) {
    throw new Error("Invalid string");
  }
}
else {
  throw new Error("Can only use number or string");
}

It's not parseFloat() which is imprecise, but the numbers themselves. JavaScript uses IEEE 754, which is 64-bit floating point numbers, with 53 bits used for precision. The number you are testing against is the highest safe integer, which is 2^53-1 and can be gotten from Number.MAX_SAFE_INTEGER. But your result could still be imprecise with numbers this large, since you are not only using integers but also 2 decimal places.

With string inputs you can get around this by not converting it to a number at all. Since the result is also a string, you can keep it a string all the way through. For number inputs you can only work with the precision you've been given. Any imprecision should be handled by the code calling this function beforehand.

You will need to update your parseInput function to avoid parseFloat. Just cut of any extra decimals, or add them if they are missing. If you want to round the number it gets a bit harder, but it's still possible.

Your placeThousands function can be done a little simpler. There are several ways to do it, but I would cut the string into an array of chunks and then join them together. I would also make it easier to change the chunk size by putting it in a variable.

const chunkSize = 3;
let chunks = [];
let start = wholesPart.length % chunkSize;
if(start != 0) {
    chunks.push(wholesPart.slice(0, start));
}
for(let i = start; i < wholesPart.length; i+= chunkSize) {
    chunks.push(wholesPart.slice(i, i + chunkSize));
}
return chunks.join(thousandSep)

When I saw fstChunk the first thing I thought was 'fast chunk', but I guess it's supposed to be 'first chunk'. There is no reason make things less clear just to save 2 characters.

You don't need to check the length of wholesPart, placeThousands can handle short strings.

return placeThousands(wholesPart) + decimalPart;


A JavaScript class, which finalises the HTML of each page of my NodeJS (Express) website

Ways of improving/optimizations:

What fixApostrophes and fixDashes functions actually try to do is replacing a specific punctuation chars with respective HTML entities.
Instead of those numerous horrifying while loops - a more optimized, concise and extendable approach would be to:

compose a predefined replacement map (where keys are search patterns and values - respective entities values):

const replaceMap = {"``": "&ldquo;", "''": "&rdquo;", "`": "&lsquo;",
                    "'": "&rsquo;", "---": "&mdash;", "--": "&ndash;"};

perform all the replacements at once
with String.replace function based on combined regex pattern:

input = input.replace(new RegExp(Object.keys(replaceMap).join('|'), 'g'), function(m){
    return replaceMap[m] || m;
});

where Object.keys(replaceMap).join('|') is used to compose regex alternation group from replaceMap keys like ''|'|---|--
the former 2 functions can be conceptually combined into a single function called, say punctToEntities ("punctuations to entities")

Eventually, the Finaliser class would look as:

const replaceMap = {"``": "&ldquo;", "''": "&rdquo;", "`": "&lsquo;",
                    "'": "&rsquo;", "---": "&mdash;", "--": "&ndash;"};

class Finaliser
{
    constructor()
    {

    }

    static punctToEntities(input) {
    /** Converts punctuation chars to respective HTML entities **/
        input = input.replace(new RegExp(Object.keys(replaceMap).join('|'), 'g'), function(m){
            return replaceMap[m] || m;
        });

        return input;
    }

    // Render, and deliver the page to the browser.
    protoRender(req, res, view, properties) {
        var date = new Date();

        properties.footstamp = date.toISOString();
        res.render(view, properties, function(err, html){
            if (html === undefined) {
                res.render(view, properties);
            } else {
                html = Finaliser.punctToEntities(html);
                res.send(html);
            }
        });
    }
}


Good job in general, mad props for proper use of cout vs cerr.

I don't see any "errors" per se, but I do see a few things I personally would have done a bit differently.

Opinion: Not a big fan of std::exit() in anything but main()

A function is supposed to be decontextualized, and having a "will crash the whole program if it fails" as part of the function's contract is just too aggressive in my opinion.

Personally, I'd rather just throw an exception instead. It accomplishes the same thing, and it lets users of the function choose how to handle failures.

Opinion: maybe have a separate prompt for failure?

I would personally explain to the user why he is being asked the same question twice when the input fails to match either criteria.

Opinion: std::transform is overkill here

This is a personal bias, as I find in-place usage of std::transform uncomfortable at best, since a range-based for does as good a job in a much more legible manner.

Specifically: Having to read the third parameter to understand that it's an inplace transform is just unnecessary cognitive load. I find the following easier to parse:

for(auto& x : line) {
  x = std::tolower(static_cast<unsigned char>(x));
}


That regex will not properly validate the format of an email address. Many web sites get it wrong when validating email addresses.
According to RFC5322, the local part of an address (the part before the @) can contain one or more text strings separated by .s.  The test strings can include letters, numbers, and any of these characters: ! # $ % & ' * + - / = ? ^ _ { | } ~ " or a back tick.  Alternatively, the local part can be a quoted string.  There can be comments in () and possibly whitespace in some places.
That's just for the local part using only US-ASCII characters.  There are other RFCs that describe the domain (after the @) and addresses with non-ASCII (e.g., Unicode) characters.


You can always translate loops into a tail-recursive function.  Then all you need to do is pass state around as function parameters instead of mutating it in place.  IIRC, Scala supports tail-call optimization of direct recursion with the appropriate annotation.  That should be good enough in this case.

You actually have done this for BFS_r.  You basically just need to do the same thing with the rest of the code.

But first, you're using a lot of Lists that would probably be better off as Sets or Maps.  Also, you're keeping track of the nodes that have been visited (globalVisits).  I would keep track of the portion of the graph that has not been visited instead -- at least with a tail-recursive solution, since we'll have to get rid of the flatMap.

The first thing is to replace the flatMap with a tail-recursive function:

@tailrec
def loop(unvisited: Map[String, List[String]], accumulator: List[List[String]]): List[List[String]] = {
  if unvisited.empty {
    // We're done => return the accumulator
    return acc
  }
  else{
    // Get the next seed node
    // (Since maps are typically unordered, head is an arbitrary node
    //  that hasn't been visited yet.)
    val seed = unvisited.head

    // Find the component connected to seed
    component = BFS_r(List(seed), List(List(seed)).get.flatten)

    // Iterate, with new unvisited map and component list
    // The new unvisited will have all the nodes in component removed
    // The new accumulator has component added
    return loop(unvisited -- component, component :: accumulator)
}

Then kick it off with:

return loop(adjMap, List())

I haven't done a lot of work with Scala yet, so I can't say this is 100% valid Scala or will work at all.  But hopefully the intention is clear.  One important to note is that Map and List here are the immutable variants (i.e., from scala.collections.immutable rather than scala.collections.mutable).


newsUrl → newsURL, same for idsUrl.
const syncInterval = 30 * time.Minute is more readable. You can also convert everything that represents a time into a time.Duration or a time.Time, rather than deal with Unix timestamps. There are functions in the time package that do what you want to do (time.Date, time.Since, etc.).
About that, why does Fetch need to do complicated things with times, checking when was the last time it was called, etc.? Shouldn't the caller take care of this with a simple time.Ticker instead? It feels like this complexity shouldn't be there.
HackerNewsService is a weird name for an implementation. HackerNewsFetcher maybe?
Your postgres package isn't very idiomatic, which makes the callers do weird things to use it. For example, why do you have a SettingsRepository struct that doesn't contain anything? Why not simply have GetSettings as a package-level function? Same for the other empty structs of this package (HackerNewsRepository…).
It's more idiomatic to test for len(myString) == 0 rather than myString == "".
I'm not exactly sure what you're doing with lastSyncTime (this is pretty complex). Maybe add a comment for the line lastSyncTime = time.Now().Unix() - syncInterval - 1 or refactor this part?
Talking about refactor, Fetch() is way too long. Split it in sub-functions.
No need for an else after log.Fatal.
Rather than:

// fetch each news
log.Println("hacker news: " + fmt.Sprintf(newsUrl, id))

why not:
    log.Println("fetching from hacker news", fmt.Sprintf(newsUrl, i))
Using log.Fatal everywhere makes your code not very resilient. Maybe it's worth looking at "what can fail for transient reasons" (typically, database transactions…), and re-try the thing that failed a few times with exponential backoff.
There's lots of whitespace in the "what stores the data" part, which isn't very idiomatic. Empty lines in functions aren't really frequent.


Given two int values, return the one closer to 10

You have a lot of redundant code in here:

public static int Closer(int a, int b)
{
    int calcA = a - 10;
    int calcB = b - 10;
    if (Math.Abs(calcA) == Math.Abs(calcB))
    {
        return 0;
    }
    else if ((a >= 10 || b >= 10) && a < b )
    {
        return a;
    }
    else if ((a >= 10 || b >= 10) && b < a)
    {
        return b;
    }
    else if (calcB > calcA && Math.Abs(a)!= Math.Abs(b))
    {
        return b;
    }
    else if ((calcA < 0 || calcB < 0) && (calcA > calcB && Math.Abs(a) != Math.Abs(b)))
    {
        return a;
    }
    else return Closer(a,b);
}

First of all, you can figure everything out if you know the distance between the values of a, b, and 10.

int calcA = Math.Abs(a - 10);
int calcB = Math.Abs(b - 10);

You have principle in this section right, but it can be simplified a bit if you set calcA and calcB to use the absolute difference:

if (calcA == calcB)
{
    return 0;
}

Now, you just need to apply the same principle to the other values:

if (calcA < calcB)
{
    return a;
}

return b;    // we already established that the values are not the same, and that "a" is farther from 10 than "b"

Here is our new method:

public static int Closer(int a, int b)
{
    int calcA = Math.Abs(a - 10);
    int calcB = Math.Abs(b - 10);

    if (calcA == calcB)
    {
        return 0;
    }

    if (calcA < calcB)
    {
        return a;
    }

    return b;
}

But, 10 is a magic number in there, and magic numbers are never good.  Why don't you create a const value to store the number that you are checking the passed values against?

public static int Closer(int a, int b)
{
    const int compareValue = 10;
    int calcA = Math.Abs(a - compareValue);
    int calcB = Math.Abs(b - compareValue);

    if (calcA == calcB)
    {
        return 0;
    }

    if (calcA < calcB)
    {
        return a;
    }

    return b;
}

Now, you can change your method as much as you want, and you only have the magic number 10 in one place, so it is easy to change your method to compare the values against a different number.

Edit:

Thanks to Eric Lippert, I have fixed two bugs.  Eric found the bug that int.MinValue != int.MaxValue, so calling Math.Abs(int.MinValue) crashes.  If you pass a value that equals int.MinValue + compareValue, the program will crash.  While fixing this, I found another bug in which the wrong value will be returned if you call the function with two values that are less than int.MinValue + compareValue because they wrap around in the call value - compareValue.  Both of these bugs are now fixed for the compare value of 10, still working on a generic patch:

public static int Closer(int a, int b)
{
    const int compareValue = 10;

    while (a <= int.MinValue + compareValue || b <= int.MinValue + compareValue)
    {
        if (a <= int.MinValue + compareValue)
        {
            b = AdjustVal(b, compareValue);
            a++;
        }
        if (b <= int.MinValue + compareValue)
        {
            a = AdjustVal(a, compareValue);
            b++;
        }
    }

    int calcA = Math.Abs(a - compareValue);
    int calcB = Math.Abs(b - compareValue);

    if (calcA == calcB)
    {
        return 0;
    }

    if (calcA < calcB)
    {
        return a;
    }

    return b;
}

Edit 2:

Eric Lippert says we should do the arithmetic in longs. Of course, this doesn't work when we take input as long too.

public static int Closer(int a, int b)
{
    const int compareValue = 10;

    long calcA = Math.Abs((long)a - compareValue);
    long calcB = Math.Abs((long)b - compareValue);

    if (calcA == calcB)
    {
        return 0;
    }

    if (calcA < calcB)
    {
        return a;
    }

    return b;
}


Read from DataInputStream (Socket connection)

Too late for OP but anyway...

Seems that you read from socket continuously when data is available. If there is not incoming data in socket for some reason you can wait maximum 10 seconds (it's just an exemplary number) and then you terminate the program.

Some kind of loop is required anyway (because you read as much as possible). The loop can be hidden with aid of Java's Timer, but it will not make the code more clean.

Please consider the following example (I played with files instead of sockets, but from data stream's point of view they expose the same interface):

public class DataInputStreamWithTimeoutExample {
    public static final int TIMEOUT = 10;

    public static void main(String args[]) throws IOException, ExecutionException, InterruptedException {
        final DataInputStream reader =
                new DataInputStream(new FileInputStream("/home/me/Temp/java_example/in.txt"));
        final DataOutputStream writer =
                new DataOutputStream(new FileOutputStream("/home/me/Temp/java_example/out.txt"));

        final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        final Future<Void> future = singleThreadExecutor.submit(new Courier(reader, writer));

        try {
            System.out.println("Started...");
            future.get(TIMEOUT, TimeUnit.SECONDS);
            System.out.println("Finished.");
        } catch (TimeoutException e) {
            System.out.println("Timeout has gone!");

            // TIMEOUT seconds are passed, but infinite loop is not finished.
            // Lets send `interrupt` message to it.
            future.cancel(true);
            singleThreadExecutor.shutdown();
        }
    }
}

class Courier implements Callable<Void> {
    public static final int BUFFER_SIZE = 1024;

    private final DataInputStream reader;
    private final DataOutputStream writer;

    public Courier(final DataInputStream reader, final DataOutputStream writer) {
        this.reader = reader;
        this.writer = writer;
    }

    @Override
    public Void call() throws Exception {
        while (true) {
            int availableBytes = reader.available();
            if (availableBytes > 0) {
                final byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead = reader.read(buffer);
                writer.write(buffer, 0, bytesRead);
            }
            // We need analyze whether someone decided
            // to interrupt the infinite loop.
            if (Thread.interrupted()) {
                writer.flush();
                writer.close();
                System.out.println("Terminated.");
                return null;
            }
        }
    }
}

We analyze status of the thread inside infinite loop because without such check it will be impossible to interrupt the loop outside it.

You can ask Where is the gain?

We wiped out System.currentTimeMillis()... stuff. Java's ExecutorService does calculations of time for us.
Timeout is set outside the loop (please note that there are not any references to TIMEOUT constant inside Courier class). So Courier class can be re-used in another program (but don't forget that you need to send interrupt message to Courier with aid of future.cancel(true); and singleThreadExecutor.shutdown();, otherwise the program will never end).

PS while working on this answer I used awesome answers:

"How to timeout a thread"
"How to Stop long duration execution task ( e.g. Infinite Loop) execution inside a Executor Service"


Checking if a number is Armstrong number or not

In addition to the points made by justjofe, I also have a few suggestions.

You might consider making the method armstrong(int) return a boolean instead of a String, and then using the returned boolean to construct the output of your program. That way, you separate the logic of determining whether a number is an Armstrong number from the program's interface. In this case, a better name for the method might be "isArmstrong" instead of "armstrong":

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    // ...

    if (isArmstrong(in.nextInt())) {
        System.out.println("armstrong number");
    } else {
        System.out.println("not armstrong number");
    }
}

private static boolean isArmstrong(int number) {
    // ...
    return sum == oldNumber;
}

And now about the code that checks a number for being an Armstrong number itself. I think assigning a new value to the method parameter number is confusing, because it forces you to introduce a local variable that serves no other purpose than to store the original value of the method parameter. So why not do it the other way around, using the variable declared inside the method for the loop and leaving the method parameter number assigned to the original value?

private static boolean isArmstrong(int number) {
    double sum = 0.0;
    int n = number;

    while (n > 0) {
        int temp = n % 10;
        n = n / 10;
        sum = sum + Math.pow(temp, 3);
    }

    return sum == number;
}

After this change, the while loop combined with the declaration of n (or whatever you want to call it) actually lends itself to being expressed as a for loop instead:

private static boolean isArmstrong(int number) {
    double sum = 0.0;

    for (int n = number; n > 0; n = n / 10) {
        int temp = n % 10;
        sum = sum + Math.pow(temp, 3);
    }

    return sum == number;
}

I would now even go so far as to skip the introduction of the temp variable and just invoke Math.pow(n % 10, 3), since you only need to evaluate n % 10 once per loop iteration, but that is purely a matter of taste.

Finally, some comments on points mentioned by justjofe:

First, about closing the scanner: Be aware that calling close() on the Scanner will also call System.in.close() which might not be desirable. There are several threads concerning this at stackoverflow, and one answer says this:

  Generally speaking, the code that creates a resource is also responsible for closing it. System.in was not instantiated by by your code, but by the VM. So in this case it's safe to not close the Scanner […]

However, this doesn't address the issue of garbage collection with regards to the Scanner object raised by justjofe, and I don't know enough to give specific advice here, so I'll just make you aware of this issue.
Second, a small heads-up on compound assignment operators such as += and /=: While it might appear that

sum = sum + Math.pow(temp, 3); and
sum += Math.pow(temp, 3);

are equivalent, this is actually not the case. If you make sum an int instead of a double, the second statement will compile, however, the first one will not. The reason for this is that the second statement is actually equivalent to

sum = (int) (sum + Math.pow(temp, 3));

(as stated here in the Java Language Specification). Since Math.pow(double, double) returns a double, the result needs to be cast to an int before being assigned to an int variable. The compound assignment operator does this for you, but when using an ordinary =, you have to do the casting yourself.


I think there is this code organization issue - you have a class named Customer, but it, aside from the .name attribute, consists of credit-card related logic only.

I would also pass the obtained attributes to the class constructor instead of asking for them inside it:

def __init__(self, name, post_code, card_date, card_code):
    self.name = name
    self.post_code = post_code
    self.card_date = card_date
    self.card_code = card_code

It is a little bit cleaner to do this way since now our class is more generic, it is agnostic of where the attributes are coming from.

Some other code-style related notes:

consistent naming: rename postcode to post_code
revise the quality and necessity of comments: there is probably not much sense in having a comment # Constructor
you can simplify the way you return a boolean result from your methods. For instance, you can replace:

if datetime.date.today() < card:
    return True
else:
    return False

with:

return datetime.date.today() < card

And, it's worth mentioning that, generally speaking, if you doing this for production, you should not be reinventing the wheel and switch to a more mature, well-used and tested package like pycard.


Data set with many variables in Python, many indented dictionaries?


Project Euler #549: Divisibility of factorials

Caveat: at the moment I have neither the time nor the knowledge for solving Problem Euler #549 properly - all I can offer is more effective ways of applying brute force.

The biggest obstacle to solving this problem by brute force using factorials is the sheer size of the numbers involved. Stirling's approximation says that 100000000! has more than 2 * 10^9 bits; with a good MP library like GMP such numbers might be just barely tractable but it's going to be painfully slow.

Moreover, these factorials would need to be factored, which under the circumstances would boil down to trial division by up to pi(10^8) = 5,761,455 primes - and this would give a whole new meaning to the phrase 'painfully slow'.

A much better way is to avoid factorials entirely, working (almost) exlusively in the realm of factors. The most important insight - on which all solutions presented here are based - is this:

s(p^k * m) = max(s(p^k), s(m)) if gcd(p, m) == 1

In other words (and formulated without recursion): given the factorisation of a number n, the value s(n) is the maximum s(p^k) over all distinct prime factors p.

This gives a viable recipe for computing s(n), since factorisation of small integers is trivial and the computation of s(p^k) is tractable. Here's a rendering of the latter that's not very mathematical but easy to verify and code (using C# for clarity, since I don't speak Python well enough):

// computes s(p^e) for p prime
static int s_for_prime_power (int p, int e)
{
    int k = 0;

    while (e > p)
    {
        k += p;
        e -= p + 1;

        for (int t = k; (t /= p) % p == 0; )
            --e;
    }

    return (k + Math.Max(0 , e)) * p;
}

With the help of that function the computation of s() is easily formulated:

static int s (int n)
{
    return s(factorisation(n));
}

static int s (List<int> factors, int lo = 0)
{
    int p = factors[lo], hi = lo;

    while (++hi < factors.Count && factors[hi] == p)
        ;

    int result = s_for_prime_power(p, hi - lo);

    if (hi < factors.Count)
        result = Math.Max(result, s(factors, hi));

    return result;
}

This function implements the recursion shown earlier: it determines the power of the least prime factor (by counting the number of occurrences), computes the s value for that, and then returns the maximum of that value and the s value for the rest.

I'm glossing over some unimportant things - like factorisation and so on - since Python has everything that's needed; the C# file is Euler0549_simple.linq on PasteBin. This is a script for use with the (free) LINQPad with which I do almost all of my algorithmic prototyping and Eulering/SPOJing etc. But the file is also valid .cs that can be compiled and run unchanged with Mono, for example.

This code performs suprisingly well (considering), which is nice because that makes it easier to test more advanced solutions:

# benching 'Euler0549_simple' ...

S(       10):                 39        0,0 ms
S(      100):               2012        0,0 ms
S(     1000):             136817        0,1 ms
S(    10000):           10125843        1,3 ms
S(   100000):          793183093       16,1 ms
S(  1000000):        64938007616      216,5 ms
S( 10000000):      5494373412573    3.538,4 ms
S(100000000):    #45a57e1327a02a   66.756,3 ms

In the last row some hex digits from the MD5 of the decimal result are printed, in order to avoid showing the actual result. The increase in time is higher than linear because the factorisations get longer as numbers get bigger. One major point of inefficiency here is the fact that numbers are enumerated as numbers and then factored, instead of enumerating factorisations directly.

There is a type an algorithm that effectively enumerates an initial segment of natural numbers in the form of least factor decompositions p * m where p is not greater than the least prime factor of m; see Linear time Euler's Totient Function calculation for example.

This algorithm is ideally suited for computing tables of functions where

f(p * m) = g(f(p), f(m))

which is - almost - the case here. 'Almost' because we need a decomposition of the form p^k * m with gcd(p, m) == 1; however, for a first draft this can be arranged by the simple expedient of dividing p out of m if necessary.

static int[] s;

static long E549_via_lpf_decomposition (int limit)
{
    int small_prime_limit = (int)Math.Sqrt(limit);
    var small_primes = new List<int>();
    int m = 2;

    s = new int[limit + 1];

    for (int half = limit / 2; m <= half; ++m)
    {
        if (s[m] == 0)  // prime
        {
            s[m] = m;

            if (m <= small_prime_limit)
                small_primes.Add(m);
        }

        int s_m = s[m], threshold = limit / m;

        foreach (int p in small_primes)
        {
            if (p > threshold)  // i.e. if (p * m > limit)
                break;

            if (m % p != 0)  // not a factor of m yet
            {
                // p is a new least factor -> s(p) < s(m) -> s(p * m) = s(m)
                s[p * m] = s_m;
            }
            else // p is already a (least) factor of m
            {
                int e = 2, q = m;

                while ((q /= p) % p == 0)
                    ++e;

                s[p * m] = Math.Max(s_for_prime_power(p, e), s[q]);
                break;
            }
        }
    }

    for ( ; m <= limit; ++m)
        if (s[m] == 0)
            s[m] = m;

    long sum = 0;
    for (int i = 2; i <= limit; ++i)
        sum += s[i];

    return sum;
}

This can be made more efficient in various ways but I think in this form it nicely shows the logic of the algorithm. The timings are much more linear in n, since the algorithm itself is linear. Only s_for_prime_power() and my fumbling with factoring out powers of the lpf add a tiny logarithmic influence on top:

# benching 'Euler0549_lpf_decomp' ...

S(       10):                 39        0,0 ms
S(      100):               2012        0,0 ms
S(     1000):             136817        0,0 ms
S(    10000):           10125843        0,3 ms
S(   100000):          793183093        1,9 ms
S(  1000000):        64938007616       19,5 ms
S( 10000000):      5494373412573      196,1 ms
S(100000000):    #45a57e1327a02a    1.990,3 ms

Although this is based on raw firepower instead of mathematics, it is almost decent already... Another option would be a modified Sieve of Eratosthenes (using the s[] vector like a sieve, instead of of a bit vector). Or, the devious application of advanced mathematics, but that is well beyond my capabilities ATM. ;-)


Return all words which have their reverse present in a string

I have observed a few things that may help you improve your code.

Use library functions

One of the things the code does is to separate words in the input.  However, this could be done more easily by using a std::stringstream:

stringstream in(test);
string word;
while (in >> word) {
    // test each word
}

Use more whitespace to enhance readability of the code

Instead of crowding things together like this:

cout<<"( "<<rev<<","<<temp<<" ), ";

most people find it more easily readable if you use more space:

cout << "( " << rev << ", " << word << " ) ";

Only print words once

Right now, with this input string:

string test="Sachin tendulkar is si is the best tseb eht nihcaS si";

The word "is" gets reported twice.

  {( is,si ), ( best,tseb ), ( the,eht ), ( Sachin,nihcaS ), ( is,si ), }

One way to handle that is to note when each word is actually reported and to only do it once.  Here's one way to do that:

stringstream in(test);
enum state { DETECTED=1, REPORTED };
unordered_map <string, state> binMap;

string word;
cout << "{";
while (in >> word) {
    string rev{word.rbegin(), word.rend()};
    if (binMap[rev] == DETECTED && binMap[word] != REPORTED) {
        cout << "( " << rev << ", " << word << " ) ";
        binMap[word] = REPORTED;
    } else {
        binMap[word] = DETECTED;
    }
}
cout << "}\n";


For starters using Python 3 or at least being compatible with it would
be a good idea.  Meaning that all the print calls should be using the
function form, i.e. print(...).

Next, take a look at PEP8
and use four spaces for indentation.

Parsing command line arguments is a bit nicer with
argparse, but since
you're already done with it it might not make too much sense to rewrite
that part.

There's duplicated for the "--all" and "--list" cases that would be
better served with a common function.

The colour definitions can be taken from some module, e.g. take a look
at termcolor or
colorama.

Lastly the for c in range(len(data)): will always be a pattern that
should be replaced with direct iteration, i.e. for x in data:.  The
line is also way to long to read it easily.


Calculate age given the birth date in the format YYYYMMDD

Non standard date string YYYYMMDD
As pointed out in the comments "Looks quite reasonable..." and as such there is not too much to review...
Well apart from the fact that new Date("19671031") is not a valid date The date string as parsed by Date must be a valid date strings. (I did not spot this until halfway through the review)
Ignoring the fact your code does not work as a simple oversight and continuing on as initially started...
...I will therefor nit pick, and I do stress the point this is just nit picking for sake of a review.
Constants
Constants as const The named vars today, birthDate, m can all be const. In fact the whole thing can be rewritten to only use constants.
Names
Names are too verbose. The general rule is to use the scope to infer meaning, for names to preference 1 word over 2, 2 words over 3, etc... Short words rather than long. Common abbreviations instead of full words.

birthDate can be birth as date is inferred
dateString can be dateStr using the common abbreviation for string
today could be now. In context today is the most accurate however nothing is lost if now is used.
m does not work as month. The value represents a quantitative query (How many months?) and as such months or better yet the abbreviated mths... eww ... drop the s for mth

Rewrite A
And then I realize it does not work
function getAge(dateStr) {
    const birth = new Date(dateStr);
    const now = new Date();
    const mth = now.getMonth() - birth.getMonth();
    const adjust = mth < 0 || (mth === 0 && now.getDate() < birth.getDate()) ? 1 : 0;
    return now.getFullYear() - birth.getFullYear() - adjust;
}

Responsibility
A general rule for functions is that they should only do one thing. Your function has two tasks, parse the date string and calculate the age.
As the date string is likely an un-trusted source (eg user input) and as such requires vetting which is outside the functions responsibility (inferred by the name getAge) the function should accept a Date rather than a string to parse.
This solves the problem of incorrectly formatted date string.
Rewrite B
As the birth date is now an argument the simple name birth does not hold enough meaning to be clear so it is changed back to birthDate
function getAge(birthDate) {
    const now = new Date();
    const mth = now.getMonth() - birthDate.getMonth();
    const adjust = mth < 0 || (mth === 0 && now.getDate() < birthDate.getDate()) ? 1 : 0;
    return now.getFullYear() - birthDate.getFullYear() - adjust;
}

Parsing and vetting
The task of vetting and parsing the date string can now be handed to a more appropriate function.
The function assumes that the format is YYYYMMDD and returns a date only if the Date accepts the year, month, and day extracted from the string, that the date is earlier than now. It will return an age that fits within the Date object range (age of historical births)
If the date is invalid it can be said to be undefined and as such the function returns a Date or undefined.
function parseBirthDate(dateStr) {
    const birth = new Date(dateStr.slice(0,4), dateStr.slice(4,6), dateStr.slice(6,8)););
    const since = Date.now() - birth.getTime();
    return isNaN(since) || since < 0 ? undefined : birth;
}

Using the two functions to get an age.
const birth = parseBirthDate("19671031");
const age = birth ? getAge(birth) : undefined;

Testing
A simple manual test snippet accepts a string input

function getAge(birthDate) {
    const now = new Date();
    const mth = now.getMonth() - birthDate.getMonth();
    const adjust = mth < 0 || (mth === 0 && now.getDate() < birthDate.getDate()) ? 1 : 0;
    return now.getFullYear() - birthDate.getFullYear() - adjust;
}
function parseBirthDate(dateStr) {
    const birth = new Date(dateStr.slice(0,4), dateStr.slice(4,6), dateStr.slice(6,8));
    const since = Date.now() - birth.getTime();
    return isNaN(since) || since < 0 ? undefined : birth;
}
birthDate.addEventListener("change", () => {
    const birth = parseBirthDate(birthDate.value);
    const age = birth ? getAge(birth) : undefined;
    ageDisplay.textContent = age !== undefined ?
        "Age: " + age + " years old" :
        "Invalid birth date!";
})

Enter birth date:
<input type="text" id="birthDate"  maxlength="8" placeholder = "YYYYMMDD" size="8">


Divide the last element in an array of arrays among the remaining elements

Based on your constraints, which are:

Single loop variable
No restarting of iteration over large arrays

You could calculate your looping step sizes first, then go over the array only once.

  const mushedArr = [];
  const remainderSize = arr.length % size
  const numberOfChunks = Math.floor(arr.length / size);
  let remainderStepSize = Math.floor(remainderSize / numberOfChunks);

I'm going to define the remainder with negative index slicing, so there has to be a guard against calculating a remainder even when division is exact.

let remainder = remainderSize ? arr.slice(-remainderSize) : []

Second edge case is where the remainder is smaller than the chunk size, which would cause my step size to evaluate to 0 and not loop through the remainder properly.

if(remainderStepSize === 0) remainderStepSize ++;

Finally, loop over the array and the remainder:

 let i = 0;
  while(i < numberOfChunks){
    let remainderPortion = remainder
        .slice(i*remainderStepSize, i*remainderStepSize+remainderStepSize);

    let arrayPortion = arr.slice(i*size, i*size+size);

    mushedArr.push([...arrayPortion, ...remainderPortion]);
    i++;
  };
  return mushedArr;
};

Variable names here could be shorter, and the slice can be simplified to
something like arr.slice(size*i, size*(i+1), but the idea is to loop over the array and copy the items in sizes that are equal to the chunk size.

Testing for both inputs in your question yielded:

Calling function with input: [A,B,C,D,E,F,G,H,I,J,K,L,M,N]
 chunk size: 5
[ [ 'A', 'B', 'C', 'D', 'E', 'K', 'L' ],
  [ 'F', 'G', 'H', 'I', 'J', 'M', 'N' ] ]
Calling function with input: [A,B,C,D,E,F,G,H]
 chunk size: 3
[ [ 'A', 'B', 'C', 'G' ], [ 'D', 'E', 'F', 'H' ] ]


Binary Search Tree insert while keeping track of parent for node to be added

Instead of if (n != NULL) and then indenting the whole code of function, it is better to write:
if (n == NULL) { return NULL; }
The line int cmp = (n->val < x) ? -1 : (n->val > x); is very hard to read.
I suggest finding a better name to cmp - for example isGreater and changing its type to boolean.


Version 1 uses insert(0, ...) which has to move over all the characters already in the StringBuilder's buffer, so is unnecessarily wasting time.

A more efficient construct would be:

StringBuilder().append(DOUBLE_QUOTE_CHAR)
               .append(escapeJava(query))
               .append(DOUBLE_QUOTE_CHAR)
               .toString();

Version 2 uses StringBuilder under the hood, to construct "%c" + escapeJava(query) + "%c", so your belief the second version is faster may be in error.

Which leads us to the most efficient, most readable version:

DOUBLE_QUOTE_CHAR + escapeJava(query) + DOUBLE_QUOTE_CHAR;


I disagree with rolfl's assertion that, "There is no practical way to validate an e-mail address by regex alone."

He is correct—as illustrated by the somewhat infamous SO answer he linked to—that it's impractical to validate any RFC-5322-compliant email addresses because, to quote the HTML5 spec:

  RFC-5322 … defines a syntax for e-mail addresses that is simultaneously too strict (before the '@' character), too vague (after the '@' character), and too lax (allowing comments, whitespace characters, and quoted strings in manners unfamiliar to most users) to be of practical use here.

...not to mention that there are now literally thousands of TLDs.

But it's absolutely practical to come up with a very good compromise, which is exactly what the writers of the HTML5 spec did. To that end, the spec provides this PCRE:

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

This regular expression will yield false positives (rejections) for technically valid email addresses, including ones with spaces and comments (yes, there is a syntax for putting comments in an email address), but practically all email addresses in use in the real world will pass.

As rolfl suggests, the only perfect way to validate an email address is to send an email to it and see if it's received—not all syntactically valid email addresses actually exist, after all—but that doesn't mean we can't use a pretty good validation to give the user useful feedback in the client, and for that we can look to the very smart people who did the work for us when they wrote the HTML5 spec.

As to your second question:

  Also is there a difference between this method and just setting the type="email" in the HTML form?

Sure. Using type="email" makes the browser do the validation for you. That's obvious, I know, but it has a number of implications, some good and some not-necessarily-good. Among them:

The browser might not do it. It might not support automatic validations, or the user might have disabled it. This limitation could be overcome with a polyfill, however.
In browsers that support it, it just works, and you don't have to write any code, which is pretty nice.
Currently there's no widely-supported way to customize the way browsers show HTML5 validation errors, so you can't control how it'll look to the user.

I'm sure there are others, but you get the idea.

My personal recommendation is that unless you have a good reason not to, use type="email" along with a polyfill (if you have users with older browsers). Very smart people wrote the HTML5 spec, and very smart people wrote the browsers that implement it. Unless you know something that they don't, use the solutions they've already built.


I guess I might as well then.  To prefix that with:  It looks fine as is,
there's a few details that could be improved, although those also depend
on your team and style guide.  Also read it in the context of a bigger
project, not everything always applies, especially if it's one-off code,
etc.
I'm gonna assume (since the formatting is a bit off) that you're using
gofmt or goimports already, if you don't, check them out, it helps
immensely.
For the User struct, even if the JSON name is lastname, consider the
Go-side name to be LastName to keep in line with normal style guides.
Depending on whether you need it the JSON annotation could also use an
omitempty, but that depends on your external service of course.
For the HandleLambda loop a few things stand out:

for idx := range event.Records might as well be
for idx, record := event.Records, no need have an extra line to get
the actual item.  And if you're never using it,
for _, record := event.Records even.
Errors are ignored?  Consider logging them if don't error our
immediately.
user := make(map ...) I'd usually make that a literal to avoid
repeating myself even more.
userRecords could perhaps already be pre-allocated to the right size. Then there'd be no need for append at all.  Or, like here, if you still need to use continue, pre-allocating the maximum length it could take, so it definitely wouldn't have to
be resized might also be a good idea.  Usually this also doesn't matter that much if you don't have a
lot of entries, but it's good to keep in mind for situations where it
does matter.
Also consider making the to-map conversion a new method on User, or
a helper function, you might want to test it or reuse in other places.
repository looks like a global variable, that's usually not the best
idea.  Consider making that a parameter for HandleLambda, or making
HandleLambda a method and put the repository as one of its
members.  It will help with testability in the end to not have
dependencies scattered around like that.

type User struct {
    Name     string `json:"name"`
    LastName string `json:"lastname"`
}

func (u *User) toMap() map[string]interface{} {
    return map[string]interface{}{
        "1": u.Name,
        "2": u.LastName,
    }
}

func HandleLambda(event events.SQSEvent) {
    userRecords := make([]map[string]interface{}, 0, len(event.Records))

    for _, record := range event.Records {
        ev, err := event.NewEvent(record.Body)
        if err != nil {
            continue
        }

        user, err := repository.NewUser(ev)
        if err != nil {
            continue
        }

        userRecords = append(userRecords, user.toMap())
    }

    uJSON, _ := json.Marshal(userRecords)
    // I send this to external service
}


Generating a list of menus with AngularJS

Simple answer, no it's not OK this way, you are totally misusing the angular.js purpose.

Anugular.js is a framework in which follows a modular pattern, it was designed by the time web wasn't so rich as nowadays so lots of things in the framework are native behavior today's days, it starts always with a module, like so:

angular.module('MyApp', []);

As you have your module declared, it can be triggered by the ng-app="MyApp" directive, which bootstraps your module in the element you embedded the ng-app attribute into (i.e., it starts an angular.js app in that element) or you can bootstrap it manually using angular.bootstrap(document, ['MyApp']) when the DOM is ready.

I've made an acceptable way of using angular.js with your example and you can check this in the example bellow.

angular.module('MyApp', [])
  .controller('MainCtrl', function($scope) {
    $scope.links = [
      { ref: "./link1.html", content: "First link of the menu" },
      { ref: "./link2.html", content: "Second link of the menu" },
      { ref: "./link3.html", content: "Third link of the menu" }
    ];
  });

       {{ x.content }}

However, the modern way of doing modern web apps these days is using the concept of WebComponents, concept in which angular.js supports since version 1.5.0.

In the snippet bellow, I provided the component approach of your example.

  Disclaimer: You seem not to have understood angular.js' concept properly, so, I don't know where you've been studying angular.js, but I really recommend the angular.js guide which covers pretty much everything you need to know about concepts, architecture, features, etc.

angular.module('MyApp', [])
  .component('app', {
    template: `

           {{ x.content }}

    `,
    controller: function() {
      this.links = [{
          ref: "./link1.html",
          content: "First link of the menu"
        },
        {
          ref: "./link2.html",
          content: "Second link of the menu"
        },
        {
          ref: "./link3.html",
          content: "Third link of the menu"
        }
      ];
    }
  });

  <app />


md5 usage may be okay on small projects, but it's generally considered weak and shouldn't be used. As of the MD5 wikipedia page:

  The security of the MD5 hash function is severely compromised.

and http://us1.php.net/md5

  It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm.

So perhaps you should look into PHP's other hashing methods, say hash() itself. You can read more here: http://us1.php.net/manual/en/function.hash.php

Important Edit:

I read a little bit more and I am incorrect! The hash method is okay. It's not great like bcrypt! I highly suggest you read both of these and understand them, I'm sure it will come in handy later on! https://security.stackexchange.com/questions/17421/how-to-store-salt and https://security.stackexchange.com/questions/211/how-to-securely-hash-passwords. And directly relating to PHP, http://us1.php.net/manual/en/function.password-hash.php


Is this a fair use for a factory pattern?

Rather than doing it that way (separate methods for separate configurations) you should pass in the "configuration" to a single method and let that one handle it.

class AcmeFactory
{
    public static function create($config)
    {
        $crud           = new Crud;
        $productTypes   = new ProductTypes($crud);
        $productImages  = new ProductImages($crud);
        $product        = null;
        if($config == "anvil")
        {
            $product = new Anvils($crud, $productImages);
        }
        else if($config == "jetpack")
        {
            $product = new JetPacks($crud, $productImages);
        }
        $productManager = new ProductManager($productTypes, $product);

        return $productManager;
    }
}

You can of course extract the choice of product into a separate (private) function again ;)

then you'd use this as follows:

$anvil = AcmeFactory::create("anvil");
$anvil->create($weight);
$anvil->display($id);
...

I am not that good with PHP, but I think you should somehow limit what you allow to be passed into your create function. In Java I'd solve this using an enum, but as I said, I don't know enough php :(

Apart from that, I have a minor nitpick too:

Be consistent in your use of whitespaces before parentheses. Compare:

$productImages  = new ProductImages($crud);
$product        = new Anvils ($crud, $productImages);


How to simplify code, which adds spaces and commas if field value is not empty?

Firstly, I'd create a helper function for the != "" check:

function isEmptyString(value) {
    if (value == "") {
        return true;
    }
    return false;
}

It would make the code easier to read.

Then I'd extract out a function for sturctures like ($("#dob-dd").val() != "" ? $("#dob-dd").val() : "xx") + ".":

function wrapDateOfBirth(id) {
    return ($(id).val() != "" ? $(id).val() : "xx") + ".";
}

and structures like searchDetails != "" ? " "+$("#first-name").val() : $("#first-name").val();:

function wrapSearchDetails($value, $id) {
    return $value != "" ? " " + $($id).val() : $($id).val();
}

They would remove some duplication.

Furthermore, the wrapSearchDetails function could be written like this too:

function wrapSearchDetails(value, id) {
    return (isEmptyString(value) ? "" : " ") + $(id).val();
}

The wrapDateOfBirth function also could be simplified:

function wrapDateOfBirth(id) {
    var value = $(id).val();
    return (isEmptyString(value) ? "xx" : value) + ".";
}

Finally, a getDefaultIfEmpty function also could improve the readability of the wrapDateOfBirth function:

function getDefaultIfEmpty(value, defaultValue) {
    if (isEmptyString(value)) {
        return defaultValue;
    }
    return value;
}

function wrapDateOfBirth(id) {
    var value = $(id).val();
    return getDefaultIfEmpty(value, "xx") + ".";
}

(I have not tested the code, please feel free to edit if you find any typo/etc.)


Here I’m wondering about a couple of things.
Firstly, if I’m using requests.Session correctly.

Yes, you are. In one of my other reviews, using requests.Session in the same way for iterating over a paginated API almost halved the total execution time.
I did some quick testing by downloading the last 7 pages (pages 1625-1631) for "2020-10-23" and it did marginally better than making requests with requests.get:

requests.get: 23.2 seconds
requests.Session: 17.7 seconds

Secondly, if splitting code into two functions instead of one was a good idea.

I think it's fine to have it split into two functions. That said, I do have some comments about the responsibilities and interface of fetch and how to better take advantage of your usages of yield and yield from down below.

Overall the code looks clean and is easy to read. Here's how I think it can be improved:

I think all the low-level details of how to issue requests to the API should be abstracted away from the caller of fetch. That is, fetch's function signature should look something like this:
def fetch(
    session: requests.Session,
    start_date: date,
    end_date: date,
    starting_page: int = 0,
    page_size: int = 100,
) -> Iterator[pd.DataFrame]:
    pass

So now creating an appropriate params would be fetch's responsibility, not fetch_all's. Notice also that start_date and end_date are of type datetime.date, not str. Similarly, fetch_all should not have to be concerned with what date string serialization format the API accepts; this is fetch's responsibility.

Within fetch, instead of maintaining variables next_page and last_page on each request, I think it would be better to calculate the total number of pages (n) only once with the first request (page k), then use a for loop for pages k+1..n-1:
def to_dataframe(json_data: Dict[str, Any], page: int) -> pd.DataFrame:
    return pd.json_normalize(json_data["transactions"]).assign(page=page)

def fetch(
    session: requests.Session,
    start_date: date,
    end_date: date,
    starting_page: int = 0,
    page_size: int = 100,
) -> Iterator[pd.DataFrame]:
    params = {
        "startdate": start_date.isoformat(),
        "enddate": end_date.isoformat(),
        "page": starting_page,
        "pageSize": page_size,
    }

    data = session.get(URL, params=params).json()
    page_count = math.ceil(data["count"] / data["pageSize"])
    last_page = page_count - 1
    if starting_page > last_page:
        return
    print(f"{starting_page} / {last_page}")
    yield to_dataframe(data, starting_page)

    for page in range(starting_page + 1, page_count):
        params["page"] = page
        data = session.get(URL, params=params).json()
        print(f"{page} / {last_page}")
        yield to_dataframe(data, page)

The tradeoff here is that there's a small duplication of code because the first request is handled a little differently, but now we've delegated responsibility of page number iteration to the for loop.

I recommend adding an event hook to the session object so that it always calls raise_for_status() on the response object. This ensures that all requests made with the session raise requests.HTTPError if the server gives us a 4xx or 5xx response, and prevents us from converting an error response's .json() data into a dataframe:
session.hooks["response"].append(
    lambda r, *args, **kwargs: r.raise_for_status()
)

Currently the program is combining all dataframes in memory before exporting it to a CSV file. To take advantage of fetch_all being an Iterator[pd.DataFrame], I think it would be better to write each dataframe to the CSV immediately, so we don't need to hold it in memory any longer than necessary:
output_path = Path(f"data/{DATE}.csv")
output_path.unlink(missing_ok=True)
data = fetch_all()
for i, dataframe in enumerate(data):
    write_header = True if i == 0 else False
    dataframe.to_csv(
        output_path, header=write_header, index=False, mode="a"
    )

Refactored version:
#!/usr/bin/env python3

import math
from datetime import date, timedelta
from pathlib import Path
from typing import Any, Dict, Iterator

import pandas as pd  # type: ignore
import requests

# # This is what I'd normally use, but since there would be no data today,
# # I assign specific date myself
# DATE = date.today() - timedelta(days=1)
DATE = date.fromisoformat("2020-10-23")
URL = "https://spending.gov.ua/portal-api/v2/api/transactions/page/"

def to_dataframe(json_data: Dict[str, Any], page: int) -> pd.DataFrame:
    return pd.json_normalize(json_data["transactions"]).assign(page=page)

def fetch(
    session: requests.Session,
    start_date: date,
    end_date: date,
    starting_page: int = 0,
    page_size: int = 100,
) -> Iterator[pd.DataFrame]:
    params = {
        "startdate": start_date.isoformat(),
        "enddate": end_date.isoformat(),
        "page": starting_page,
        "pageSize": page_size,
    }

    data = session.get(URL, params=params).json()
    page_count = math.ceil(data["count"] / data["pageSize"])
    last_page = page_count - 1
    if starting_page > last_page:
        return
    print(f"{starting_page} / {last_page}")
    yield to_dataframe(data, starting_page)

    for page in range(starting_page + 1, page_count):
        params["page"] = page
        data = session.get(URL, params=params).json()
        print(f"{page} / {last_page}")
        yield to_dataframe(data, page)

def fetch_all() -> Iterator[pd.DataFrame]:
    with requests.Session() as session:
        session.hooks["response"].append(
            lambda r, *args, **kwargs: r.raise_for_status()
        )
        yield from fetch(session, start_date=DATE, end_date=DATE)

if __name__ == "__main__":
    output_path = Path(f"data/{DATE}.csv")
    output_path.unlink(missing_ok=True)
    data = fetch_all()
    for i, dataframe in enumerate(data):
        write_header = True if i == 0 else False
        dataframe.to_csv(
            output_path, header=write_header, index=False, mode="a"
        )


Why does removing the else{} case cause the compiler to give a warning for it?

The compiler will indeed issue a warning if a value-returning function doesn't return a value for every path.  Ignoring this warning could then cause undefined behavior.

If you intend for bSearch() to return -1 on failure, then you can put just one return -1; at the very end of the function and remove the elses that return -1.


There is a much simpler method in Python to do an insertion sort using the
bisect.insort method

import bisect
array = [1,3,5,7]
bisect.insort_left(array, 2)

>>[1, 2, 3, 5, 7]

With regard to your coding style, I would recommend using the if __name__ == "__main__" guard.

You can also just say for i in range(n) with no need to say (0, n)

You can also use list-comprehension to build the list:

import bisect
def insertion_sort(thelist):
    new_list = []
    for item in thelist:
        bisect.insort_left(new_list, item)
    return new_list

if __name__ == "__main__":
    n = int(input("Enter number of elements: "))
    lst = [input("Please enter a number: ") for i in range(n)]
    new_list = insertion_sort(lst)
    print(*new_list, sep = ", ")


Inserting multiple elements at known locations in a vector

We need to include <algorithm> for std::sort and <cassert> for assert.

void print(std::vector<T> &x): Since we don't modify the vector, this should take a const& (and use a const& internally in the range-based for loop).

void reorder(std::vector<T> &v, std::vector<uint32_t> &order): Again, order can be a const&.

We should use std::size_t for indices into a vector, not uint32_t or size_t.

If we need a signed type for dealing with index differences, we should use std::ptrdiff_t, not int. It might be easier to count forwards, and then calculate the indices we need inside the loop though.

There's no point in std::moveing a char.

Rather than inserting elements in-place, we can create a new vector and copy the elements over, selecting from the appropriate source at each index. This doesn't require any more memory than calling x.resize().
e.g.:
{
    std::vector<char> x = {'H', 'e', 'l', 'l', 'W', 'o', 'l', 'd'};
    std::vector<char> values = {'r', 'o'};
    std::vector<size_t> positions = {6, 4};

    auto indices = sort_indices(positions);
    reorder(positions, indices);
    reorder(values, indices);

    auto size = x.size() + values.size();

    auto y = std::vector<char>();
    y.reserve(size);

    auto v = std::size_t{ 0 }; // index into x
    auto p = std::size_t{ 0 }; // index into positions / values

    for (auto i = std::size_t{ 0 }; i != size; ) // index into y
    {
        if (v == positions[p])
        {
            y.push_back(values[p]);
            ++p;
            ++i;
        }
        else
        {
            y.push_back(x[v]);
            ++v;
            ++i;
        }
    }

    print(y);
}

Since we expect the x vector to be much larger than the values to be inserted, we could copy larger ranges from x in the else branch above, something like:
            auto beg = v;
            auto end = (p == positions.size() ? x.size() : positions[p]);

            y.insert(y.end(), x.begin() + beg, x.begin() + end);
            v = end;
            i += (beg - end);

(Warning: code not properly tested).


Minimum difference between numbers in a file

You need to make sure you always close resources that can be closed. That means either a try-with-resources block or a try-finally block. You can’t just leave close outside one of these constructs because if an exception gets thrown the close method might not get called.
Arguably, parsing the distances is easier to read using the stream API. You might not yet have learned that part of the language yet, in which case there’s nothing wrong with your loop except that inFile is never closed. Oh, no, it is, but waaaay too late. Keep the scope of the scanner as tight as possible.
Likewise, the code for finding the minimum distance can be written as a stream. Credit to @AnkitSont.
Variable names are really, really important. They should describe the information being pointed to. l is a terrible variable name because it doesn’t tell us anything. list would be a slight improvement, but distances would be much better.
Along the same lines, there’s no reason to cut short variable name lengths. sc instead of scanner doesn’t save you anything, and it makes it harder on the reader because they have to go dig up what an sc is. minDist, diff and minDiffcan and should all be expanded out, and minDist might be better named safeDistance.
Declare variables where they’re first used, and limit their scope as much as possible. This reduces the cognitive load on the reader. minDist can be declared right before your output. It could arguably also be a constant in your class (private static final double declared at the class level). diff can be declared inside the for loop.
Even though the language allows curly braces to be optional for one-line blocks, it is widely regarded best practice that you always include curly braces. To do otherwise is inviting problems later when your code is modified.
Be careful with conditional checks. All of your code will work correctly if there are zero distances in the file until you hit if (l.size() != 1). You really want to check if (l.size() > 1), right?
Your catch block isn’t really doing anything. Uncaught exceptions would effectively be handled the same way - execution would terminate and the stack trace would be logged to System.err.
It's nice to use final when you can to indicate that a variable isn't going to be reassigned. This also reduces the cognitive load of the reader.

If you were to apply all these changes to your code, it might look something like:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.stream.IntStream;

public final class Main {

    public static void main(final String[] args)
            throws IOException {

        final String filename;
        System.out.print("Please enter the name of the input file: ");
        try (final Scanner scanner = new Scanner(System.in)) {
            filename = scanner.nextLine();
        }

        final double[] distances =
                Files
                    .lines(Paths.get(filename))
                    .mapToDouble((line) -> Double.parseDouble(line.split(",")[1]))
                    .sorted()
                    .toArray();

        final double minimumDifference =
                IntStream
                    .range(1, distances.length)
                    .mapToDouble(i -> distances[i] - distances[i - 1])
                    .min()
                    .getAsDouble();

        final double safeDistance = 5;
        if (minimumDifference < safeDistance) {
            System.out.println("The satellites are not in safe orbits.");
        } else {
            System.out.println("The satellites are in safe orbits.");
        }

        if (distances.length > 1) {
            System.out.println("The minimum distance between orbits (km): " + minimumDifference);
        }
    }

}


Generics

Since you asked: Sure, generics would be nice. Right now, your Tree would be better named IntBinarySearchTree, as it only accepts integers.

Mutable

  Should I make it mutable? Is it good? Why?

No, mutable objects are not good. Very often, they are necessary (because the data changes after the object is created), but if you can make an object immutable, you should do so (because it's easier to handle if you know that it doesn't change).

In this case, I think that it is ok that it is mutable. You could change it, but the resulting code wouldn't be all that pretty, and it would get even worse when you implement more functionality.

Formatting

You are violating a lot of Java style conventions (opening { should be on same line, as should else; whitespace around ==, >, =; etc). If you paste your code into any IDE, it can format it according to conventions.

By convention, the order of elements in a class is: fields, constructor, methods. I would go with this order, so readers can easily find them.

private Node class

As your Node class is never used on its own, and never outside of the Tree class, it should be a private class inside Tree.

Node constructor

It's always a bit ugly to pass null to a constructor. I would create an additional constructor that only takes data as input.

compareTo

Your compareto method can be replaced with return this.data - o.getData();. It's a bit less readable, but it is how it's done most of the time, so readers will be used to it. You might also want to check if the passed node is not null, and if it is handle it accordingly.

insert

You should move the code for inserting the first root node to insertData. It just complicates insert.

I would also rename root1 to currentRoot.

And I would write a toString method for the tree to test it. Right now, your code doesn't seem quite right (you are doing root1.setLeftChild in both cases).


That depends on how confident you are that neither of these cases will be true:

Another tag like  will be introduced that you will want to leave unfiltered
No nested tags will appear within the tag

An example of the last would be:

<UserPassword><for name="boy">Sue</for></UserPassword>

It seems your assumptions are safe, but if you want to be certain, go with

self.request.gsub! /\<UserPassword\>.+?\</UserPassword\>/gm, '<UserPassword>[FILTERED]</UserPassword>'

By eliminating the greediness of the regex, you can match the exact tag you mean and reduce the assumptions that will come back and bite you.