There are a number of issues in this very short program.
Unless there are coding standards that require otherwise, it is better to use typedef to define types rather than #define.
You are calling standard C libraries when you call printf(), scanf(), memmove() and strlen() unless you have written all of these functions on your own and linking in some sort of special manner.
It is not a good idea to redefine size_t at any point since it is defined by the system header files and may be different on different platforms (the Windows version of size_t is different than the Fedora Linux version of size_t for example).
The code contains a possible buffer overflow when performing the scanf(). The array ch is only 50 characters. This can lead to undefined behavior.
You don't need a long long for an array that is a maximum of 50 characters.


First of all, you could have less updates if you passed all values you want to set at once:

Product::where('id', '=', $key) ->update(['selected' => '0', 'amount' => '0', 'group' => '0']);

Secondly, you check in many places in your code if value is different than 0 and save the value, but if later if it is equal to 0 you save 0 in the database - you could just simply save the value in the database and skip the checks.

This, including some other enhancements, should work for you:

$data = [];
foreach ($checkedAmounts as $productId => $value)  {
  $data[$productId]['amount'] = $value;
}

foreach ($checkedGroups as $productId => $value) {
  $data[$productId]['group'] = $value;
}

foreach ($checkboxes as $productId => $value) {
  $data[$productId]['selected'] = boolval($value);
}

foreach ($data as $productId => $updates) {
  Product::whereId($productId)->update($updates);
}


A program to represent a coin amount using the smallest number of coins

Don't invent error messages. If strtol sets errno, use perror() to print a precise reason for a failure.
Flat is better than nested. I recommend to handle argc < 2 at the beginning, to remove one level of indentations of the main algorithm. Along the same line, there is no need to else once if branch does return.
Testing for duplicated denomination is easier after they are sorted.
while (amount >= denominations[i]) loop is a long way to say

denominationCount = amount / denomination[i];
amount %= denomination[i];

You may want to break the for (i = 0; i < count; i++) early, as soon as amount reaches 0.
Qudos for sizeof(*denominations).
The algorithm may fail to find a solution.


You don't need multiple instances. In fact, HttpClient is designed specifically to persist and be used for multiple requests (see: HttpClient.DefaultRequestHeaders).

There's already a lot written about this so I'll redirect you to those resources:

Do HttpClient and HttpClientHandler have to be disposed?
Is HttpClient safe to use concurrently?
What is the overhead of creating a new HttpClient per call in a WebAPI client?


Analyzing pair by pair DNA sequences with for loops in Python

I notice something small, well you know the corner cases. You know that a DNA string will yield a score of that is the same as length. Obviously ^^. So don't check those cases.

You make 4 iterations in your code. While only one is interesting.

A big improvement in runtime could be to change:

def build_matrix(sequences, N):
    # in : list of lists
    # out : matrix of scores between each pair of sequences
    matrix = np.zeros((N, N))
    for i in range(N):
        for j in range(i, N):
            matrix[i][j] = calculate_distance(sequences[i], sequences[j])
    return matrix

to this:

def build_matrix(sequences, N):
    matrix = np.zeros((N, N))
    for i in range(N):
        for j in range(i+1, N):
            matrix[i][j] = calculate_distance(sequences[i], sequences[j])
    return matrix

If you could load your data as:

def get_test_arr():
    parts = ['0', 'A', 'T', 'C', 'G']
    return ["".join(
        sorted([random.choice(parts) for _ in range(2)]))
            for _ in range(5*10**5)]

you could make them np arrays as:

a = np.array(get_test_arr(), dtype=object)
b = np.array(get_test_arr(), dtype=object)

and have your distance calculation as:

def calculate_distance1(genom1, genom2):
    score = 0
    for g1, g2 in zip(genom1, genom2):
        if (g1[0] in g2) or (g1[1] in g2):
            score += 2 if g1 == g2 else 1
    return score

In the problem you've provided each induvidual pairs order does not seem to matter, so load them as such. If you do that, you don't have to sort the in the calculation function.

If you don't, you'll resort them later. Again and again. Them more samples you use, there more time is spent doing this.

Times:

Your calculation function

0.575332 s for one loop in 100 iterations.

The my solution

0.10583 s for one loop in 100 iterations.


I want to get rid of unneccessary repetiton of cycles

DRY = Don't Repeat Yourself.
If you have repeating parts of code, something went wrong then. Also you may use Linq.
Consider this example.
static void Main(string[] args)
{
    string[] lines = File.ReadAllLines(@"C:\temp\andmed2");
    List<int> numbersList = lines.Select(line => int.Parse(line)).OrderByDescending(x => x).ToList();
    int sum = numbersList.Aggregate(0, (x, y) => x + y);
    int numberOfStacks = numbersList.Count;

    List<int> repeatedBarrelSizes = numbersList;
    int count;
    do
    {
        count = 0;
        List<int> result = new List<int>();
        for (int i = 0; i < repeatedBarrelSizes.Count - 1; i++)
        {
            if (repeatedBarrelSizes[i] > repeatedBarrelSizes[i + 1])
                count++;
            else if (repeatedBarrelSizes[i] == repeatedBarrelSizes[i + 1])
                result.Add(repeatedBarrelSizes[i + 1]);
        }
        repeatedBarrelSizes = result;
        numberOfStacks -= count;
    } while (count > 0);

    Console.WriteLine(string.Join(" ", repeatedBarrelSizes));

    Console.WriteLine("Sum: " + sum);
    Console.WriteLine("Number of barrels: " + numbersList.Count);
    Console.WriteLine("Stacks: " + numberOfStacks);
}

I'm sorry but I can't test it because I have no source file with numbers to compare the results.
Total: two loops.


Very regularly when dealing with asynchronous stuff (among other things) in iOS development, we have to make a decision between three tools.

Closures (blocks, callbacks, etc)
NSNotificationCenter
The delegate pattern.

Which one is right for you depends on the scenario.  It won't always be the same one.  As a general rule, we can use the number of objects we want to respond to the result as a guideline for which pattern to use.

Closures (blocks, callbacks, etc.) are good for when we want zero objects tied to waiting on the result.  We just pass the closure and forget about it.  0 objects.
NSNotificationCenter is a good approach when we want any number of unknown objects to be able to respond to the result.  The thing working on producing the result knows nothing about these objects, it just creates a notification when it's complete.  0-many objects.
The delegate pattern is good when we want exactly one object to respond to the result.  Using the delegate pattern, the delegate and the worker object know quite a bit about each other.  The worker object will define a protocol of set methods that its delegate must implement or can optionally implement, and the worker object can call any of these.  1 object.

It looks here like we most likely want the delegate pattern for our scenario, given that our generateRefreshToken method takes a custom notification string.  But I will cover all three options lightly (and perhaps after picking one you for sure want to use, you can improve your code and post another question where we cover that topic in detail).

For all of these scenarios, we need to consider the same information--what do we need to pass to our completion handling code?

Some indication of success vs failure.
If failure, details on the failure.
If success, the results.

Closures (blocks, callbacks, etc)
This is basically the approach that joekim's answer takes.  Our method, generateRefreshToken just needs to take a single argument, a completion block.  That completion block should be defined as a closure that accepts arguments that convey the information we outlined above, and return void.  joekim's answer outlines a very good approach to this, but how you implement it specifically is up to you.  Frequently these sorts of closures will simply take an NSData or NSDictionary object to represent the data (or perhaps just a regular Swift Dictionary) and an NSError optional to indicate success or failure (and hold failure details).

NSNotificationCenter
This is effectively the pattern you're currently using.  But remember, I described NSNotificationCenter as being for when you want zero to many objects responding to completion.  As such, when we use NSNotificationCenter, we probably should be accepting a string parameter to tell us the name of the notification.  Instead, the file that declares this class should probably also expose two String constants that the object will use for its notification.  One of these notifications is the "success" notification, and the other is the "error" notification.  The "userInfo" passed with the "success" notification is simply the necessary data we want to collect, and the "userInfo" passed with the "failure" notification simply contains the error.
The reason that I recommend this approach (two notifications) is simply because NSNotificationCenter allows observers a lot of control and flexibility.
If I want to use a single method to observe both methods and just check to see what's in user info, I can.
If I want to use two methods and separate my error handling logic from my success handling logic, I can.
If I want to ignore failure (or success), I can.  If I want to have one object handle success and another handle failure, I can.

The Delegate Pattern
This is the pattern I actually think is most appropriate for your scenario.  For this pattern, we must start by declaring a protocol that defines the methods we'll be calling on our delegate.  Something like this might work:
@objc protocol GenerateTokenDelegate {
    func tokenGeneratorDidSucceed(tokenGenerator: DeviceInfo)
    func tokenGeneratorDidFail(tokenGenerator: DeviceInfo)
}

Of course, this approach means we must declare our protocol as @objc, and our class DeviceInfo, must also be an Objective-C class (make it inherit from NSObject), and we must do this so we can hold a weak reference to our delegate.  If we hold a strong reference to our delegate (which will typically already have a strong reference to us), we risk creating a retain cycle.  The delegated object should hold a weak reference to its delegate.
So now we need to add a weak reference to our delegate to our class:
class DeviceInfo:NSObject{
    weak var delegate: GenerateTokenDelegate?
    // the rest of our class definition
}

Now, when we instantiate our class, we set its delegate property equal to the object we want it to call back to (which conforms to GenerateTokenDelegate protocol), and when we've completed our asynchronous code, we call back to it as such:
self.delegate?.tokenGeneratorDidFail(self)

And obviously, we can set our protocol up to define our methods in any such way and take any set of arguments (though we'll always want to pass self as the first argument, typically).
We can also declare optional methods.  Suppose we maybe don't want to force the delegate to handle errors.  We can let the "didFail" method be an optional part of the protocol.
optional func tokenGeneratorDidFail(tokenGenerator: DeviceInfo)

And now we'd call it as such:
self.delegate?.tokenGeneratorDidFail?(self)

Notice both sets of question marks.  The delegate may or may not be nil, and even if it's not nil, it may or may not implement the method we're trying to call.


Pair up elements of a sequence without overlap

Here's another way:

[| 1; 2; 3; 4; 5; 6; 7; 8; 9; 10|]
|> Seq.chunkBySize 2
|> Seq.choose (function [|x; y|] -> Some (x, y) | _ -> None)

So, chunkBySize will create a sequence like seq [ [|1; 2|]; [|3; 4|]; .. ]

Then choose will try to match each element of the sequence with an array of two elements, everything else will be filtered out, which accounts for a trailing array of a single element if the input length is not even.

It could be even shorter by changing the last line to:

|> Seq.collect Array.pairwise

Which would map pairwise to the each element, transforming for instance [|1;2|] to [|(1;2)|] and it will return [||] when there are less than two elements. So this will return a sequence of an array but since I'm using collect it will concatenate all elements. Since single elements were transformed to empty arrays it will concatenate nothing in those cases, if you think about it this is similar to the mechanics of choose.


Count the frequency of integers in an array

Names

All-caps names are typically reserved for macros. They don't seem to me to make much sense for const variables. In fact, they only make minimal sense for object-like macros--they were originally used for function-like macros as kind of a warning that you should be cautious about passing an argument with side-effects, because they might happen more than once.

Minimize Magic

I'd typically try to keep the magic numbers to a minimum. For example, instead of defining FREQUENCYSIZE by itself, I'd probably do something like this:

const int lower_bound = 80;
count int upper_bound = 85;
const int frequency_size = upper_bound - lower_bound + 1;

Separation of Concerns

I'd at least consider separating validating data from reading the data. I'd prefer to have a function on the general order of:

bool valid(int val) {
    return val >= lower_bound && val < upper_bound;
}

Class Usage

We have a number of different things related to reading and working with numbers in a specified range. It might be worth considering wrapping those bits and pieces into a coherent class for dealing with a value in a range, and let the outside world create and use objects of that class.

template <class T, T lower_bound, T upper_bound>
class bounded {
public:
    static bool valid(T val) { return val >= lower_bound && val < upper_bound; }

    friend std::istream &operator>>(std::istream &is, bounded &b) {
        T val;
        is >> val;
        if (valid(val))
            b.val = val;
        else
            is.setstate(std::ios_base::failbit);
        return is;
    }

    friend std::ostream &operator<<(std::ostream &os, bounded const &b) {
        return os << b.val;
    }

    size_t index() { return size_t(val - lower_bound); }

    static constexpr size_t range() { return upper_bound - lower_bound + 1; }

private:
    T val;
};

That let's us simplify the rest of the code a bit, something on this general order:

int main() {
    using integer = bounded<int, 80, 85>;

    std::array<integer, 10> inputs;
    std::array<size_t, integer::range()> freqs {};

    for (integer &i : inputs) {
        std::cin >> i;
        ++freqs[i.index()];
    }

    for (auto freq : freqs)
        std::cout << freq << "\n";
}

Technically, this doesn't meet the requirements as-is (e.g., it doesn't print out a prompt to tell the user to enter data), but I think it gives at least some idea of a direction things could go.


Using bucket sort to rank students based on their marks

Since your functions have no explicit return types (and surely you've compiled this), you must be implementing this under an older C standard that allowed for implicit return types.

Regardless, I would recommend adding explicit return types to your functions:

main() should return an int as it is supposed to return an integer value based on the program's execution outcome (either 0 for success or 1 for failure).  However, if it will always execute successfully, then you can omit the return 0 at the end.
The other functions should return void as they're not already returning anything.

You may typedef your structs to avoid having to type struct elsewhere.
You're inconsistently using snake_case naming and all-lowercase.  The latter is not very readable as compound words should be separated.  Since you're already using snake_case, just use that for all of your variable and function naming here.
marks should probably just be mark as it's just a single variable.  But if a student is supposed to have more than one, then marks should be an array.  Also, must this be an integer type, or can a student have a decimal value as a mark?  This would of course depend on how you want to implement this grading system.
The variable j declared in bucket_sort() is unused, so it should be removed.  You should have your compiler warnings up high so that you'll be told when this occurs.
You should add whitespace between operators for readability.  Keeping lines shorter would not be of much benefit as you should try not to have so much code on one line.

For instance, one of your for loops:

for(i=0;i<n;i++)

would look like this:

for (i = 0; i < n; i++)

And your array:

struct student array[5]={"1,one","2,two","2,two","4,four","1,one"};

should have its elements separated:

struct student array[5] = { "1,one", "2,two", "2,two", "4,four", "1,one" };

I'd recommend having a macro for the number of students:

#define NUM_STUDENTS 5

You can use this macro for the array size and anything else, and in case you'll ever need to change this number, this will be done in just one place.

In addition, bucket_sort() will no longer need to take a size.

student array[NUM_STUDENTS] = { "1,one","2,two","2,two","4,four","1,one" };
bucket_sort(array);


You could avoid typing +1 and -1 in a few places by using inclusive-exclusive ranges, like 200_success nicely explained answering to your other question.
isSortedArray should call isSorted instead of repeating the same code.
Instead of math.floor(lo + (hi-lo) / 2) you could use integer division (lo + hi) // 2 so you don't need to import math.
Passing around aux just to have a place for temporary data in merge seems like an unnecessary complication. Instead you could just take two slices from a and merge them back into a.


Disposing SQL objects inside another using block

Yes, you can do this, and the garbage collector will do the job well. This is very close to your original code (snippet 1), but notice the parameter clear instructions.

using (var sqlConnection = new SqlConnection(ConnectionString))
{
   sqlConnection.Open();

   using (var command = new SqlCommand("select...", sqlConnection))
   {
       command.ExecuteNonQuery();
       command.Parameters.Clear();  // <-- IMPORTANT BEFORE RUNNING NEXT COMMAND

       command.CommandText = @"another query...";
       command.ExecuteNonQuery();
       command.Parameters.Clear();  // <-- IMPORTANT BEFORE RUNNING NEXT COMMAND

       // ...etc
   }
}


No

app.post('/register'...

Has a callback and is non-blocking within Node

db.users.save(...

Has a callback and is non-blocking within Node (but will block on your database level, which is normal)

db.users.findOne(...

Has a callback and is non-blocking within Node (but will block on your database level, which is normal)

Your code looks pretty normal. MongoDB has a global lock across an entire mongoDB server daemon, so this is the only level that your current code will block. It will not block on your Node.js level for your concurrent users.


You stated you are stuck with SQL Server 2000, which sucks; but with that in mind, here are my thoughts. Bear in mind, I'm not a SQL Server specialist, and other answers may provide a more appropriate solution.

Good things

Your SQL syntax & indentation are great. There are a few keywords that are in different case, but overall I think you are good. Your logic is easy to follow. There are a lot of missing ; statement terminators but SQL Server is very permissive. I recommend you always use terminators. Also, your single letter aliases could be improved. I will include that in my code below the next section.

But now, I must address your...

Approach

What you are essentially doing is a pivot. SQL Server 2000 is an outdated product, and there are much more appropriate solutions available with newer versions, which require neither a CURSOR nor dynamic SQL. That said, I strongly question the usefulness of the result data set.

If you're querying only a few days, sure; but, imagine a query like that over a year or longer. You would be stuck with 365+ columns of data to look through rows for each product. It would be much more useful if the data was not pivoted, i.e., have a column for products and a column for dates. It is much easier to graph using Excel and such. And pivoting the data is a trivial task in Excel.

SQL really sucks at looping, and a cursor does exactly that which sucks. My opinion is that you would be better off just getting the data and not locking down your database server while this cursor is working to rearrange how the data is displayed to pivot it. Here is an example query using Northwind:

  USE Northwind;

  DECLARE @startdate DATETIME;
  DECLARE @enddate DATETIME;
  SET @startdate = '1998-01-01';
  SET @enddate = '1998-06-01';

  SELECT
      ord.orderdate,
      prod.ProductName,
      COUNT(ordDetail.Quantity) AS [Qty Sold]
  FROM [Northwind].[dbo].[Orders] AS ord
    JOIN [order details] AS ordDetail
       ON ordDetail.orderid = ord.orderid
    JOIN Products AS prod
       ON prod.ProductID = ordDetail.ProductID
  WHERE ord.OrderDate >= @startdate
    AND ord.OrderDate < @enddate
  GROUP BY [Qty Sold],
      ord.orderdate,
      prod.ProductName;

And just let the user pivot and manipulate it as they see fit. The purpose of a database system is to manage data, not manage how data is presented.


Shadowing
Your date parameter to the constructor of News is poorly-named, because it shadows the built-in date that you've imported from datetime.
Super
Your call to Article.__init__() should use super() instead.
Data classes
Article can just be a @dataclass with its explicit __init__ removed.
Static methods
publish_article doesn't make sense as a static method. In your invocations, you're always calling format_text() on a child instance, then passing that to a static method on the parent. Instead:

Define format_text(self) -> str: raise NotImplementedError() on Article to declare it abstract
Change publish_article(self) to simply file.write(self.format_text())

Backslash continuation
Change this:
    return f"{self.title}{(self.line_width - len(self.title)) * '-'}\n"\
           f"{self.text} \n"\
           f"{self.city}, {self.date.strftime('%d/%m/%Y %H:%M:%S')} \n"\
           f"{'-'*self.line_width}\n\n\n"

to drop the backslashes and use parens instead:
    return (
        f"{self.title}{(self.line_width - len(self.title)) * '-'}\n"
        f"{self.text} \n"
        f"{self.city}, {self.date.strftime('%d/%m/%Y %H:%M:%S')} \n"
        f"{'-'*self.line_width}\n\n\n"
    )

Date parsing
This is evil:
    day, month, year = map(int, self.end_date.split('/'))

Instead, you should be using an actual parsing method out datetime to get you a date instance; then referring to its members. Since you're looking for DD/MM/YEAR, this will be:
end_date = datetime.strptime(self.end_date, '%d/%m/%Y').date()
# Use end_date.day, end_date.month, end_date.year

That said, if you're at all able, drop that date format like a sack of rotten potatoes. YYYY-mm-dd is sortable and unambiguous.


Writing a program to read a maximum of 99 elements in an iteration from a list

First things first, I feel the biggest problem about your code is that your comments lie.

/**
 * i>   Read a List Input,
 * ii>  Call a Function with maximum of 99 elements in one iteration Eg:(0..98; 99..198; 199..297; .....)
 *
 *
 */
public class OptimizeReadingList

There is no List to be seen, and I am not sure what is meant by 'call a function'. Javadocs generally explain what the class/method is supposed to be used for, and they hardly talk about their underlying/reliant methods (except when the @see literal is used). At the very most, sometimes Javadocs cover "implementation notes" or "API notes" highlighting the caveats of such an implementation, but that's all to it.

// Should Call a function printElements with start_index and end_index
for(int i = 0,ele = (read_max_size-1); i<input_list_size ; ){

Should it, should it not? Why so? Even for general Javadoc comments, it sounds weird to be suggesting something so specific... if you decide to rename your method down the road, you'll have to update this comment, which becomes cumbersome in the long run.

// Increment Operations for the next elements(maybe another 99 or less than 99)
i=(ele+1);

Let's start printing batches of 98 instead. You'll have to manually update refrences to 99 throughout your code. Comments should document the 'why' and not the 'how', hence your comments describing how you are selecting maybe the next 99, or less than 99, is usually not recommended.

// Consider that this method can read only a maximum of 99 elements in range.
private static void printElements(int i, int ele) {

I'm only highlighting the method declaration, but nowhere in the method says that a maximum of 99 elements will be read. If I call this as printElements(0, 0);, I will only get a newline output. If I call this as printElements(9000, 10000);, I will probably get an ArrayIndexOutOfBoundsException exception (since you have hard-coded your test array as 107 elements).

So how can I actually achieve what I need?

You say you need a List, so let's see if there are any methods that can help you out with...

  List<E> subList(int fromIndex, int toIndex)

  Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

private static final int BATCH_SIZE = 99;

private static <T> void splitProcess(List<T> list) {
    splitProcess(list, BATCH_SIZE);
}

private static <T> void splitProcess(List<T> list, int batchSize) {
    if (batchSize < 1) {
        throw new IllegalArgumentException("Batch size must not be less than 1.");
    }
    for (int start = 0, end; start < (end = Math.min(list.size(), start + batchSize));
            start = end) {
        System.out.println(list.subList(start, end));
    }
}

Define your desired batch size as BATCH_SIZE = 99.
Have a 1-argument splitProcess(List) method that calls the 'fuller' splitProcess(List, int) method with BATCH_SIZE as the default second argument.
Do some validation for batchSize.
Construct a for-loop with:

two variables representing the start (inclusive) and end (exclusive) points
looping while start < list.size(), and
increment them accordingly.

Then you can call list.subList(start, end), and use it in the way you require.

Javadocs shall be left as an exercise for the reader...


C++11 Blocking connection pool with auto release

This is not what I would normally call an Object Pool (in the traditional sense of the term). That term I would use to refer to a pool of memory that returns "Uninitialized" memory (via a pointer) usually via an allocator so that a new operation could build the object (and the memory is returned after it is deleted).

I do like the term borrow() though as that is an accurate description of what is happening. You are temporarily giving ownership of objects that can be returned (in potentially a new state).

Your ownership semantics are wrong though. You return a unique_ptr<> indicating that ownership has been transferred to the borrower. But that is not actually true. The "pool" actually retains ownership as it calls the destructor on the object when it is destroyed (ONLY the owner of an object should try and delete it (reclaim the resource)).

Also if the "pool" is destroyed (thus destroying all the owned pointers); then the "borrowers" are left holding unqiue_ptr<> to objects that no longer exist (bad point 1). But when they do release them the "deleter" tries to put them back into a "pool" that no longer exists (bad point 2).

As it stands this code is just ill-advised.
You need to think some more about your ownership semantics.


I don't know about "more efficient", but here are some hints and tips that will help you clean up the code and make it more readable:

Style remarks

My style checker asks for at least 2 spaces before an inline comment (lines 20-21). I don't think it's a bad idea.
According to PEP8, you should keep your lines shorter than 80 characters (lines 24, 25 and 49).
This line is not very clear.

information = 'ENT: ' + str('{0:.2f}'.format(comp)) + ' SMA: ' + str('{0:.2f}'.format(smasumm)) + ' LB: ' + str(
 '{0:.2f}'.format(smasumm - framsd)) + ' UB: ' + str('{0:.2f}'.format(smasumm + framsd))

It would be a lot clearer to rewrite it like this:

'ENT: {0:.2f} SMA: {0:.2f} LB: {0:.2f} UB: {0:.2f}'.format(
    comp, smasumm, smasumm - framsd, smasumm + framsd)

Avoid one letter variable names (and other cryptic names like smasumm,  because they're not descriptive and it's not immediately obvious what they represent.
The lack of comments makes it difficult to follow what you're trying to do. You should use docstrings to write a small paragraph that provides an explanation of what you're trying to do.
Again according to PEP8:

  Imports should be grouped in the following order:

  standard library imports
  related third party imports
  local application/library specific imports

So you want to replace your imports with this:

from collections import deque
import datetime
import math
import subprocess

import numpy as np
from PIL import Image
from PIL import ImageChops
from PIL import ImageDraw
from PIL import ImageFont

Why do you alias numpy as np? It's not very readable. I'd rather read numpy in full letters each time.


Maintaining encapsulation seems to promote redundant classes

What I see here is a great scenario for composition!

What you say is that you're annoyed because CreateManualShipmentRequest already has the shipment informations. Alright so, why doesn't CreateManualShipmentRequest contains ShipmentRequestInfo instead of repeating all the information?

public class ShipmentRequestInfo
{
    public Address Sender { get; set; }
    public Address Recipient { get; set; }
    public PackageSizeAndWeight Package { get; set; }
    public Payor Payor { get; set; }

    public List<string> SpecialServices { get; set; }
    public decimal InsuredValue { get; set; }
    public string ServiceType { get; set; }
}

public class CreateManualShipmentRequest
{
    public ShipmentRequestInfo ShipmentRequestInfo { get; set; }

    public string ServiceProvider { get; set; }

    public string LabelImageFormat { get; set; }
    public bool TestMode { get; set; }
}

You're right that having redundant code is a code smell, but creating another class to keep encapsulation is most of the time a pretty good thing to do.

I wanted to address the comment stating that you could use interfaces. I feel like it's not a bad solution, but I don't think it's the right one.

Implementing an interface is kind of a "is a" relationship.

But, could you say : "CreateManualShipmentRequest is a ShipmentRequestInfo? I don't think so. Though, saying "CreateManualShipmentRequest contains ShipmentRequestInfo" seems pretty logical to me!

Sure, readability and serialization are good points. For the serialization, well.. APIs are good, you'll have to write a little bit more code to deal with that. Readability itself isn't that affected. But are those points valuable than :

Not having redundant code
Passing the wrong object to a method
Implementing an interface that is misleading

? I feel like you should use composition.


Algorithm for competing cells of 0s and 1s

EDIT: Thanks to @benrg pointing out a bug of the previous algorithm. I have revised the algorithm and moved it to the second part since the explanation is long.

While the other answer focuses more on coding style, this answer will focus more on performance.

Implementation Improvements

I will show some ways to improve the performance of the code in the original post.

The use of group is unnecessary in the for-loop. Also note that if a house has a missing adjacent neighbour, its next state will be the same as the existing neighbour. So the loop can be improved as follows.

for i in range(len(in_states)):
    if i == 0:
        out_state = in_states[1]
    elif i == len(in_states) - 1:
        out_state = in_states[i - 1]
    else:
        out_state = in_states[i - 1] == in_states[i + 1]
    new_state.append(out_state)

It is usually more efficient to use list comprehensions rather than explicit for-loops to construct lists in Python. Here, you need to construct a list where: (1) the first element is in_states[1]; (2) the last element is in_states[-2]; (3) all other elements are in_states[i - 1] == in_states[i + 1]. In this case, it is possible to use a list comprehension to construct a list for (3) and then add the first and last elements.

new_states = [in_states[i-1] == in_states[i+1] for i in range(1, len(in_states) - 1)]
new_states.insert(in_states[1], 0)
new_states.append(in_states[-2])

However, insertion at the beginning of a list requires to update the entire list. A better way to construct the list is to use extend with a generator expression:

new_states = [in_states[1]]
new_states.extend(in_states[i-1] == in_states[i+1] for i in range(1, len(in_states) - 1))
new_states.append(in_states[-2])

An even better approach is to use the unpack operator * with a generator expression. This approach is more concise and also has the best performance.

# state_gen is a generator expression for computing new_states[1:-1]
state_gen = (in_states[i-1] == in_states[i+1] for i in range(1, len(in_states) - 1))
new_states = [in_states[1], *state_gen, in_states[-2]]

Note that it is possible to unpack multiple iterators / generator expressions into the same list like this:

new_states = [*it1, *it2, *it3]

Note that if it1 and it3 are already lists, unpacking will make another copy so it could be less efficient than extending it1 with it2 and it3, if the size of it1 is large.

Algorithmic Improvement

Here I show how to improve the algorithm for more general inputs (i.e. a varying number of houses). The naive solution updates the house states for each day. In order to improve it, one needs to find a connection between the input states \$s_0\$ and the states \$s_n\$ after some days \$n\$ for a direct computation.

Let \$s_k[d]\$ be the state of the house at index \$d\$ on day \$k\$ and \$H\$ be the total number of houses. We first extend the initial state sequence \$s_0\$ into an auxiliary sequence \$s_0'\$ of length \$H'=2H+2\$ based on the following:

$$
s_0'[d]=\left\{\begin{array}{ll}
s_0[d] & d\in[0, H) \\
0      & d=H, 2H + 1\\
s_0[2H-d] & d\in(H,2H] \\
\end{array}\right.\label{df1}\tag{1}
$$

The sequence \$s_k'\$ is updated based on the following recurrence, where \$\oplus\$ and \$\%\$ are the exclusive-or and modulo operations, respectively:
$$
s_{k+1}'[d] = s_k'[(d-1)\%H']\oplus s_k'[(d+1)\%H']\label{df2}\tag{2}
$$

Using two basic properties of \$\oplus\$: \$a\oplus a = 0\$ and \$a\oplus 0 = a\$, the relationship (\ref{df1}) can be proved to hold on any day \$k\$ by induction:

$$s_{k+1}'[d] = \left\{
\begin{array}{ll}
s_k'[1]\oplus s_k'[H'-1] = s_k'[1] = s_k[1] = s_{k+1}[0] & d = 0 \\
s_k'[d-1]\oplus s_k'[d+1] = s_k[d-1]\oplus s_k[d+1]=s_{k+1}[d] & d\in(0,H) \\
s_k'[H-1]\oplus s_k'[H+1] = s_k[H-1]\oplus s_k[H-1] = 0 & d = H \\
s_k'[2H-(d-1)]\oplus s_k'[2H-(d+1)] \\
\quad = s_k[2H-(d-1)]\oplus s_k[2H-(d+1)] = s_{k+1}[2H-d] & d\in(H,2H) \\
s_k'[2H-1]\oplus s_k'[2H+1] = s_k'[2H-1] = s_k[1] = s_{k+1}[0] & d = 2H \\
s_k'[2H]\oplus s_k'[0] = s_k[0]\oplus s_k[0] = 0 & d = 2H+1
\end{array}\right.
$$

We can then verify the following property of \$s_k'\$
$$
\begin{eqnarray}
s_{k+1}'[d] & = & s_k'[(d-1)\%H'] \oplus s_k'[(d+1)\%H'] & \\
s_{k+2}'[d] & = & s_{k+1}[(d-1)\%H'] \oplus s_{k+1}[(d+1)\%H'] \\
       & = & s_k[(d-2)\%H'] \oplus s_k[d] \oplus s_k[d] \oplus s_k[(d+2)\%H'] \\
       & = & s_k[(d-2)\%H'] \oplus s_k[(d+2)\%H'] \\
s_{k+4}'[d] & = & s_{k+2}'[(d-2)\%H'] \oplus s_{k+2}'[(d+2)\%H'] \\
       & = & s_k'[(d-4)\%H'] \oplus s_k'[d] \oplus s_k'[d] \oplus s_k'[(d+4)\%H'] \\
       & = & s_k'[(d-4)\%H'] \oplus s_k'[(d+4)\%H'] \\
\ldots & \\
s_{k+2^m}'[d] & = & s_k'[(d-2^m)\%H'] \oplus s_k'[(d+2^m)\%H']  \label{f1} \tag{3}
\end{eqnarray}
$$

Based on the recurrence (\ref{f1}), one can directly compute \$s_{k+2^m}'\$ from \$s_k'\$ and skip all the intermediate computations. We can also substitute \$s_k'\$ with \$s_k\$ in (\ref{f1}), leading to the following computations:

$$
\begin{eqnarray}
d_1' & = & (d-2^m)\%H' & \qquad d_2' & = & (d+2^m)\%H' \\
d_1 & = & \min(d_1',2H-d_1') & \qquad d_2 & = & \min(d_2', 2H-d_2') \\
a_1 & = & \left\{\begin{array}{ll}
s_k[d_1] & d_1 \in [0, L) \\
0 & \text{Otherwise} \\
\end{array}\right. &
\qquad a_2 & = & \left\{\begin{array}{ll}
s_k[d_2] & d_2 \in [0, L) \\
0 & \text{Otherwise} \\
\end{array}\right. \\
& & & s_{k+2^m}[d] & = & a_1 \oplus a_2 \label{f2}\tag{4}
\end{eqnarray}
$$

Note that since the sequence \$\{2^i\%H'\}_{i=0}^{+\infty}\$ has no more than \$H'\$ states, it is guaranteed that \$\{s_{k+2^i}\}_{i=0}^{+\infty}\$ has a cycle. More formally, there exists some \$c>0\$ such that \$s_{k+2^{a+c}}=s_{k+2^a}\$ holds for every \$a\$ that is greater than certain threshold. Based on (\ref{f1}) and (\ref{f2}), this entails either \$H'|2^{a+c}-2^a\$ or \$H'|2^{a+c}+2^a\$ holds. If \$H'\$ is factorized into \$2^r\cdot m\$ where \$m\$ is odd, we can see that \$a\geq r\$ must hold for either of the divisibilty. That is to say, if we start from day \$2^r\$ and find the next \$t\$ such that \$H'|2^t-2^r\$ or \$H'|2^t+2^r\$, then \$s_{k+2^t}=s_{k+2^r}\$ holds for every \$k\$. This leads to the following algorithm:

Input: \$H\$ houses with initial states \$s_0\$, number of days \$n\$
Output: House states \$s_n\$ after \$n\$ days
Step 1: Let \$H'\leftarrow 2H+2\$, find the maximal \$r\$ such that \$2^r\mid H'\$
Step 2: If \$n\leq 2^r\$, go to Step 5.
Step 3: Find the minimal \$t, t>r\$ such that either \$H'|2^t-2^r\$ or \$H'|2^t+2^r\$ holds.
Step 4: \$n\leftarrow (n-2^r)\%(2^t-2^r)+2^r\$
Step 5: Divide \$n\$ into a power-2 sum \$2^{b_0}+2^{b_1}+\ldots+2^{b_u}\$ and calculate \$s_n\$ based on (\ref{f2})

As an example, if there are \$H=8\$ houses, \$H'=18=2^1\cdot 9\$. So \$r=1\$. We can find \$t=4\$ is the minimal number such that \$18\mid 2^4+2=18\$. Therefore \$s_{k+2}=s_{k+2^4}\$ holds for every \$k\geq 0\$. So we reduce any \$n>2\$ to \$(n-2)\%14 + 2\$, and then apply Step 5 of the algorithm to get \$s_n\$.

Based on the above analysis, every \$n\$ can be reduced to a number between \$[0, 2^t)\$ and \$s_n\$ can be computed within \$\min(t, \log n)\$ steps using the recurrence (\ref{f2}). So the ultimate time complexity of the algorithm is \$\Theta(H'\cdot \min(t, \log n))=\Theta(H\cdot\min(m,\log n))=\Theta(\min(H^2,H\log n))\$. This is much better than the naive algorithm which has a time complexity of \$\Theta(H\cdot n)\$.


Print all interleavings of two given strings

Your algorithm is correct, as you explained.  However, I didn't find the code easy to understand.

Staying within the spirit of your solution, I've cleaned it up a bit:

Renamed s2 → s1 (because s2 coming first is confusing), result → s2.
Renamed depth → i1, start → i2 to emphasize their relationship with s1 and s2.
Swapped the order of the last two parameters for parallelism.
Provided defaults for the last two parameters to make main() prettier.
Eliminated the variable i, because using i2 directly is clearer.
Used an early return to reduce a level of indentation.

Simultaneously tracking all of those substitutions is tricky, but here is the result:

void interleaving(const string& s1, string s2, int i1=0, int i2=0)
{
    if (i1 == s1.size())
    {
        cout << s2 << endl;
        return;
    }

    // Transfer the first character of s1 into s2
    // anywhere after the last previously inserted
    // character from s1.
    for ( ; i2 <= s2.size(); i2++)
    {
        s2.insert(i2, 1, s1[i1]);
        interleaving(s1, s2, i1 + 1, i2 + 1);
        s2.erase(i2, 1);        // Undo the insert() above
    }
}

Since passing the second parameter by value would cause it to be copied unnecessarily on each recursive call, you might want to change it to be passed by reference instead.

static void interleaving_helper(const string& s1, string &s2, int i1, int i2)
{
    if (i1 == s1.size())
    {
        cout << s2 << endl;
        return;
    }

    for ( ; i2 <= s2.size(); i2++)
    {
        s2.insert(i2, 1, s1[i1]);
        interleaving_helper(s1, s2, i1 + 1, i2 + 1);
        s2.erase(i2, 1);        // Undo the insert above
    }
}

void interleaving(const string& s1, string s2, int i1=0, int i2=0)
{
    interleaving_helper(s1, s2, i1, i2);
}


Python Firewall-Connection-Event Filter too slow

The main performance bottleneck is probably the fact that you keep your already seen IPs in a list. Doing x in list is \$\mathcal{O}(n)\$, so the more IPs there are, the slower this will get. Another factor is that you check if new_client_IP not in client_IPs but then add IP(new_client_IP).strNormal() to the list. So even if you come across the same IP again, it will still not be in the list, since that class probably produces something different than the line (which will have a trailing newline, for example) (and if it does not produce anything different, why have this step at all?) and so you do this possibly expensive step again.

Instead just save them in a set, for which in is \$\mathcal{O}(n)\$. And even better, if you add an already existing element to a set it does not care.

client_IPs = set()
client_IPs_size = 0

def read_clients(path_unique_ips):
    """Check if the file has changed size.
    If it has, extract all IPs from it and update the global variables.
    """
    global client_IPs_size
    current size = os.stat(path_unique_ips).st_size
    if current_size != client_IPs_size:
        client_IPs_size = os.stat(path_unique_ips).st_size
        global client_IPs
        with open(path_unique_ips, "r") as client_IPs_file:
            client_IPs = {IP(line).strNormal() for line in client_IPs_file}

Note that I used with to ensure the file is properly closed, even in an event of an exception. I also used a set as stated above.

I also iterate over the file directly instead of first reading all lines into memory. The whole thing fits nicely inside a set comprehension. I also added a docstring to document what this function does.

Modifying global objects can quickly lead to hard to read code and should be avoided if possible. Here this is a bit harder to avoid, because process_and_filter depends on the global state always being correct at that specific point in the loop. Another way around this would be to use class to keep this global state and make the other functions methods of that class. This might be a bit overkill, though, so I leave that implementation to you if you feel it is necessary.

def read_events(path_sophos_to_customer):
    """Every 0.1 seconds check if a new non-blank line has been added to the end of the file.
    If there has, yield it.

    Read the file containing the list of IPs again if it has changed.
    """
    with open(path_sophos_to_customer, 'r') as connection_event_to_customer:
        connection_event_to_customer.seek(0, 2)   # seek to the end
        while True:
            new_event = connection_event_to_customer.readline()
            if not new_event:
                time.sleep(0.1)
                continue
            yield new_event

            # file size of unique IP File changed, this will read the IPs again
            read_clients(PATH_UNIQUE_IPS)

Since the check for a changed client_IPs is now inside read_clients the if clause is not needed anymore.

I also removed the check if the file is readable. Usually you want your code to either fail loudly (with an exception) if something happens (like a file not being readable) or you catch that exception, do something about it and log that it happened. Having it just silently fail by bypassing it with that if clause does not help you at all.

For the splitting of the events into two logs, I would open both files only once at the beginning. I would also extract the IP from the event (since you have not shown us an example of what an event looks like, I will have to guess and say you could use for example re.search(r'\b(?:\d{1,3}\.){3}\d{1,3}\b', new_event).group()). This moves this from having to iterate over all IPs every time to iterating over the string of new_event, which is hopefully shorter.

def process_and_filter(my_events, match_log, no_match_log):
    """Get events in generator-object from function read_events
    and write either to matched or no match log file.
    """
    with open(match_log, 'a+') as match, open(no_match_log, 'a+') as no_match:
        for new_event in my_events:
            print(new_event)
            ip = extract_ip(new_event)
            file = match if ip in client_IPs else no_match
            file.write(new_event)

With this the calling code becomes:

if __name__ == '__main__':
    new_events = read_events(PATH_SOPHOS_TO_CUSTOMER, MATCH_LOG, NO_MATCH_LOG)
    process_and_filter(new_events)

Note that Python's official style-guide, PEP8, recommends using ALL_CAPS for global constants.


break (or better yet return)

Instead of doing done = False/True. You could just use break and get rid of done altogether. But even better than that, just return.

This:

        if self.keys[ind] == key:
            val = self.vals[ind]
            done = True

Becomes:

        if self.keys[ind] == key:
            return self.vals[ind] # Value found!

This:

            if ind == start_ind:
                done = True

            elif self.keys[ind] == None:
                done = True

Becomes:

            if ind == start_ind: # Not found!
                return

            elif self.keys[ind] == None: # Not found!
                return

(You can return None if you want).

This gets rid of the val variable (and done)


Below is refactoring code you can use:

def set_new_password
  @user = User.find_by(password_reset_token: params[:password_reset_token]) if params[:password_reset_token].present?

  unless user
    render json: {
       errors: ["Password reset not found. Please request a new password reset!"]
    }, status: 404 and return
  end

  if params[:password] != params[:password_confirmation]
    render json: { errors: ["Your password confirmation doesn't match"] }, status: :unprocessable_entity

  elsif @user.password_reset_sent_at < 2.hours.ago
    render json: {
      errors: ["Your password reset expired. Please require a new password reset from the app."]
    }, status: :unprocessable_entity

  else
    updated_params = user_params.merge(password_reset_token: nil, password_reset_sent_at: nil)

    @user.update(updated_params) ?
      render :json => {:success => true} :
      render :json => {:success => false}
  end
end

I also suggest to move validation password confirmation match with password to model using standard ActiveRecord validation: http://guides.rubyonrails.org/active_record_validations.html#confirmation


Function to erase a character in a string

I am not a c programmer, and I know short, cryptic function and variable names are used in c for historical reasons, but I'll still comment that I don't like it one bit.  I'd name the function removeCharacter.

Given that the function is only intended to be used on c-strings, is the len parameter actually necessary?  Can't we eliminate that parameter and get the len in the same way your context code demonstrates calculating a c-string's length?

void removeCharacter(char string[], size_t index) {
     memmove(string+index, string + index + 1, strlen(string) - pos + 1);
}

Now we can actually justify the function wrapping the memmove.  It makes it easier to use because all we have to do now is pass our string and the index of the character we want removed.

Given the performance implications of moving memory like this, it might be a great idea to offer a companion function which allows the user to remove a range of characters.

void removeSubstring(char string[], size_t index, size_t length) {
    memmove(string+index, string+index+length, strlen(string)-index+1);
}

I think it'd certainly be worthwhile to have a Range struct which has an index and length member, and simply pass this struct as the second argument instead of the index and length arguments.

And of course, it's important to be sure your code is well documented so that users are clear on exactly what they're getting with the function.

Also, this seems like a pretty easy thing to unit tests for, so be sure to do that.


Conditionally set strings if files exist

I see a number of things that may help you improve your code.

Use named constants

There are a number of constant strings which are used repeatedly.  If they're used more than once, it's probably a sign that you should use named constants instead.

constexpr static char[] JPG_EXT{".jpg"};
constexpr static char[] PNG_EXT{".png"};

Consolidate tests in compound if statements

Right now the code is basically this:

if ((test.contains("1.jpg") || test.contains("1.png")) && (maxP == false)){
    // do things
}
else if ((test.contains("1.jpg") || test.contains("1.png")) && (maxP == true)){
    // do other things
}

This can be simplified like this:

if ((test.contains("1.jpg") || test.contains("1.png")) {
    if (maxP) {
        // do other things
    } else {  // maxP must be false
        // do things
    }
}

Consolidate code where only the data changes

The code currently has repeated patterns like this:

if (QFile().exists(basepath + smax + ".jpg")){
    p_left = basepath + smax + ".jpg";
}

It then repeats, but with slightly different data.  That suggests further consolidation:

if ((test.contains("1.jpg") || test.contains("1.png")) {
    QString leftbase, rightbase;
    if (maxP) {
        gpage = max;
        leftbase = basepath + smax;
        rightbase = basepath + sinc;
    } else {
        page++;
        leftbase = basepath + spage;
        rightbase = basepath + inc;
    }
    add = 1;
    if (QFile(leftbase + JPG_EXT).exists()) {
        p_left = leftbase + JPG_EXT;
    } else if (QFile(leftbase + PNG_EXT).exists()) {
        p_left = leftbase + PNG_EXT;
    }
    if(QFile(basepath + QString::number(page + 1) + JPG_EXT).exists()){
        p_right = rightbase + JPG_EXT;
    } else if(QFile(basepath + QString::number(page + 1) + PNG_EXT).exists(){
        p_right = rightbase + PNG_EXT;
    }
}

Use a function for repeated actions

If you find yourself writing similar code multiple times, it might be a sign that you could use a function.  In this code, I'd write this:

void setIfJpgOrPngExists(const QString& querybase, const QString& answerbase,
        QString &target)
{
    if (QFile(querybase + JPG_EXT).exists()) {
        target = answerbase + JPG_EXT;
    } else if (QFile(querybase + PNG_EXT).exists()) {
        target = answerbase + PNG_EXT;
    }
}

if ((test.contains("1.jpg") || test.contains("1.png")) {
    QString leftbase, rightbase;
    if (maxP) {
        gpage = max;
        leftbase = basepath + smax;
        rightbase = basepath + sinc;
    } else {
        page++;
        leftbase = basepath + spage;
        rightbase = basepath + inc;
    }
    add = 1;
    setIfJpgOrPngExists(leftbase, leftbase, p_left);
    setIfJpgOrPngExists(basepath + QString::number(page + 1), rightbase, p_right);
}


Python Pandas Apply with a Lambda Function

Style concerns
There is a bunch of constant that you define in the function that doesn't really need to. No need do (re)define your credentials for the DB at each call, for instance, nor the init_date. You should extract these out as the constant they are.
Same thing for time_map, there is nothing in it that make it mandatory to be defined inside table, so move it out too.
You should also try to reduce too long lines and come up with better names: table doesn't convey much.
Prepared SQL
When dealing with SQL, it is often recommended to not build your query string yourself and let the manager do it for you. pandas let you do it using the params parameter of pd.read_sql. You’ll need to adapt the query a bit to change '{}' into ?.
Pandas efficiency
When dealing with pandas, it is often more efficient to perform operations on a whole column at once; and more often than not, going back and forth between the pure Python world and the pandas one will lead to the kind of performances you’re having.
pandas has its own kind of objects to deal with time. Namely pd.Timestamp and pd.Timedelta. Converting your columns to these objects instead of datetime.datetime or datetime.timedelta should help you speed up your computation. pd.to_timedelta can be quite handy for that.
You should also try to reduce the amount of extra computation. Even the tiniest ones will add up. I’m talking about your offset management, why start at 2012-01-01 and add x - 1 days? You can perform '2011-12-31' + x days instead. Same for the minutes: instead of starting at 6 o'clock and adding x - 1 × 15 minutes, why not start at 5:45?
Unfortunately, you’re dealing with strings to convert to timedeltas. You can take care of the conversion using df['QuarterHourDimID'].map(int), for instance; but it would be so much faster if you could extract integers directly out of your database.
Proposed improvements
import pandas as pd
import pyodbc

DB_CREDENTIALS = "DRIVER={SQL Server};SERVER=OURSERVER;DATABASE=DB"
SQL_QUERY = """
SELECT [ID],[StartDateDimID],[DemographicGroupDimID],[QuarterHourDimID],[Impression]
FROM TABLE_NAME
WHERE (MarketDimID = 1
    AND RecordTypeDimID = 2
    AND EstimateTypeDimID = 1
    AND DailyOrWeeklyDimID = 1
    AND RecordSequenceCodeDimID = 5
    AND ViewingTypeDimID = 4
    AND NetworkDimID = ?
    AND DemographicGroupDimID = ?
    AND QuarterHourDimID IS NOT NULL
)"""

INIT_TIMESTAMP = pd.Timestamp('2011-12-31')
QUARTER_TO_SIX = pd.Timedelta(5.75, unit='h')
DAY = pd.Timedelta(1, unit='D')

def table(network, demo):
    with pyodbc.connect(DB_CREDENTIALS) as cnxn:
        df = pd.read_sql(
            sql=SQL_QUERY,
            con=cnxn,
            params=(network, demo),
            index_col=None
        )

    quarters = pd.to_timedelta(df['QuarterHourDimID'].map(int), unit='m') * 15 + QUARTER_TO_SIX
    # This is what I understood of `time_map`, if this isn't quite right, adapt accordingly
    quarters[quarters>=DAY] -= DAY

    df['datetime'] = pd.to_timedelta(df['StartDateDimID'].map(int), unit='D') + quarter_offsets + INIT_TIMESTAMP

    if network == 1278:
        df = df.loc[df.groupby('datetime')['Impression'].idxmin()]

    df = df.set_index(['datetime'])

    return df


A "map" function that alternates between two mapping functions

So, like @pigworker has said your code looks functionally good to me, but you don't have to break up your solution into two parts like this or create a data structure you don't need.

You might find a better solution using a different approach (pattern matching):

funkyMap :: (a -> b) -> (a -> b) -> [a] -> [b]
funkyMap p q [] = []
funkyMap p q [x] = [p x]
funkyMap p q (x : y : xs) = p x : q y : funkyMap p q xs

The real benefit of this is you don't have to create any intermediate data structures.

The idea behind this solution is to let pattern matching do the hard work for you. Really you have 3 senarios for your function

when there are no elements in the list (where you probably just want to return the empty list)
when you have just one element in the list (where you just want to apply p to the element)
when you have 2 or more elements in the list (where you apply p and q, then use recursion to evaluate the rest of the list)

Using pattern matching lets you wrap that logic up in a really obvious way


If you really want to inflict this JSON library on yourself, your code could be simplified to:

import scala.collection.mutable

val outstandingUserIds = mutable.LinkedHashSet[String]()

val tweetJson = JacksMapper.readValue[Map[String, AnyRef]](body)
for {
  userObj <- tweetJson.get("user")
  idStrObj <- userObj.asInstanceOf[Map[String, AnyRef]].get("id_str")
} outstandingUserIds -= idStrObj.asInstanceOf[String]

But I would recommend you use a better JSON library. For example, using the library that comes with the Play! framework:

(body \ "user" \ "id_str").asOpt[String].foreach {id =>
  outstandingUserIds -= id
}


Each time you call File.AppendAllText, you create a new files stream and close it afterwards. Therfore, you should not write the result into the file by calling File.AppendAllText n times. just use string.Join instead:

var content = string.Join(Environment.NewLine, list);
File.WriteAllText(content);

Your code seems not work correctly, because if z == 1 is reached (which is given as initial state), the inner while loop is not entered anymore. I suppose there is a z = num; missing at the end of the outer while loop.

And in the very unlikely event, that you find a start number where the collatz conjecture does not ends up with 1, your program remains in an endless loop and you will never find out which number it was ;).


How can the below code can be refactored with design pattern?

I would look at replacing the BLL code with the following. With the main focus being replacing the Convert.ToInt16 repetition with a method. The other area of concern is the inability to mock your BAL code. To overcome this I have written a quick and dirty interface which allows better unit testing.

namespace BAL
{
    // interfaces allow the class ot be easily mocked
    internal interface ICustomDataColumnTryParse
    {
        bool TryParseBool(string columnName);
    }

    // interfaces allow the class ot be easily mocked
    internal interface INotificationSettingsDomain
    {
        DataTable GetNotificationList();
    }

    // sample implementation of the interface
    internal class NotificationSettingsBAL : INotificationSettingsDomain
    {
        public DataTable GetNotificationList()
        {
            // do whatever you need to return information from your data repository
            throw new System.NotImplementedException();
        }
    }

    internal class NotificationSettings : ICustomDataColumnTryParse
    {
        // properties are more widely accepted for consumers of this class over
        // traditional fields / variables, but it really depends on 'what' you want
        // to use these values for?
        internal bool SignUPFBPost { get; private set; }
        internal bool SignUPEmails { get; private set; }
        internal bool SignUPPushNotification { get; private set; }
        internal bool SignUPFCActivity { get; private set; }
        internal bool SignUPBubbleNotification { get; private set; }
        internal bool SignUPGlobeNotification { get; private set; }

        private DataTable m_notificationListDataTable;

        internal NotificationSettings()
            : this(new NotificationSettingsBAL())
        { }

        // using an interface allows mocking of the 'NotificationSettingsBAL' object
        public NotificationSettings(INotificationSettingsDomain notificationSettingsDomain)
        {
            // prefer suffixes over prefixes for naming conventions
            m_notificationListDataTable = notificationSettingsDomain.GetNotificationList();

            SignUPFBPost = TryParseBool("FBWallPost");
            SignUPEmails = TryParseBool("Emails");
            SignUPPushNotification = TryParseBool("PushNotification");
            SignUPFCActivity = TryParseBool("FCActivity");
            SignUPBubbleNotification = TryParseBool("BubbleNotification");
            SignUPGlobeNotification = TryParseBool("GlobNotification");
        }

        // this can be a method on this class, extension method (my preference) or a static method
        // i have opted for an internal class method coded against an interface so the data table
        // isn't passed around.
        public bool TryParseBool(string columnName)
        {
            // this can allow for additional logic if we wanted to get a different row index
            var row = m_notificationListDataTable.Rows[0];
            var value = row[columnName];

            if (value == null)
            {
                return false;
            }

            int valueAsInt = 0;
            int.TryParse(value.ToString(), out valueAsInt);
            return valueAsInt == 1;
        }
    }
}

As for replacing the switch statement you might be over-using an architectural pattern. I would only choose something like a strategy pattern if the business logic is likely to change or that additional switch statements will be added / removed with a high frequency in the future.


Assuming that your search input is plain characters (i.e., no <, >, and likes), this is still problematic as can ruin your HTML code. Let's say I search for "border" in a page that contains
, or "class" in virtually any page written in the past 5 years,... IMO, this stuff is better done using a DOM parser and then searching just plain text.

Now, to comment the search itself... Let us assume that the word from your search box is contained in the variable $search. I think this is better done like this:

foreach my $article (@{$search_articles}) {
    $article->{content} =~ s/\Q$word\E/$&<\/span>/g
}


Using a set() is your bottleneck, memory-wise.

>>> numbers = set(range(3, 10**8, 2))
>>> sys.getsizeof(numbers)
2147483872
>>> sys.getsizeof(numbers) + sum(map(sys.getsizeof, numbers))
3547483844

A set of odd numbers up to 100 million is consuming 2GB 3.5GB (thank-you @ShadowRanger) of memory.  When you do an operation like numbers = numbers - num_set, you'll need to have 3 sets in memory at once.  One for the original set, one for the set of numbers you are removing, and one for the resulting set.  This will be greater than 4GB 7GB of memory, since some of the numbers you are removing aren't in the original set.

You don't need to realize the entire set of numbers you are removing in memory.  You could simply remove the numbers from the set one at a time:

for c in range(3, num // 2, 2):
    numbers.remove(i * c)

This is modifying the set in place, so the memory requirement will not exceed the initial 2GB of memory for the set.

Why are you looping c over range(3, num // 2, 2)?  This is doing way too much work.  The maximum value c should obtain should satisfy i*c < num, since no product i*c larger than num will be in the original set.

You should instead loop over range(3, num // i + 1, 2).  This will decrease the size of the set of numbers you are removing as the prime numbers you find increase.

Why start removing primes at 3*i?  When i is 97, you've already removed multiples of 3, 5, 7, 11, 13, 17, ... up to 89. The first multiple you need to remove is 97*97.  You would then continue with 99*97, 101*97, and so on, up to num.  So the range should begin with i, not 3.

for c in range(i, num // i + 1, 2):
    numbers.remove(i * c)

Actually, this is still too complicated.  Let's get rid of the multiplication.  This also greatly simplifies the upper limit of the range.

for multiple in range(i*i, num, 2*i):
    numbers.remove(multiple)

Or equivalently, passing a generator to difference_update to remove items in bulk, but without realizing the set of numbers to be removed in memory simultaneously.

numbers.difference_update(multiple for multiple in range(i*i, num, 2*i))

Even with all of the above changes, you still require 2GB of memory to compute the primes up to 100 million.  And since a set is unordered, you still have to sort the surviving numbers afterwards to get your ordered list of primes.

A better way is to maintain an array of flags, one per candidate number.  With 100 million candidate numbers, if each flag used only a single byte, you'd only require 100 MB of memory, a savings of a factor of 20.  And since the array of flags is ordered, no sorting of the array would be required.

The bytearray is one such structure.  It is an array of bytes.  You can store your candidates in the array as a 1, and any non-primes (multiples of other primes) as 0.

def sieve(num):
    flags = bytearray(num)      # Initially, all bytes are zero

    flags[2] = 1                # Two is prime
    for i in range(3, num, 2):
        flags[i] = 1            # Odd numbers are prime candidates

    # Find primes and eliminate multiples of those primes
    for i in range(3, int(num ** 0.5) + 1, 2):
        if flags[i]:
            for multiple in range(i * i, num, 2 * i):
                flags[multiple] = 0

    return [ i for i, flag in enumerate(flags) if flag ]

Conserving a little bit more memory, you can store your list of primes in an array

import array

def sieve(num):
    flags = bytearray(num)      # Initially, all bytes are zero

    flags[2] = 1                # Two is prime
    for i in range(3, num, 2):
        flags[i] = 1            # Odd numbers are prime candidates

    # Find primes and eliminate multiples of those primes
    for i in range(3, int(num ** 0.5) + 1, 2):
        if flags[i]:
            for multiple in range(i * i, num, 2 * i):
                flags[multiple] = 0

    return array.array('I', (i for i, flag in enumerate(flags) if flag))

For primes up to \$10^8\$, the array.array('I', ...) stores the 5.7 million primes in a mere 23MB of memory.  The list version takes a whopping 212MB.

Note: If you are using a 32-bit version of Python, you may need the type-code 'L' instead of 'I' to get storage for 4-byte integers in the array.

For the truly memory conscious, install the bitarray module.

pip3 install bitarray

In addition to using only a single bit per flag, for 1/8th the memory usage in the sieve, it allows some truly fantastic slice assignments from a single boolean scalar, which makes clearing all multiples of a prime number into a simple single statement.

import array
from bitarray import bitarray

def sieve(num):

    flags = bitarray(num)
    flags.setall(False)
    flags[2] = True                      # Two is prime
    flags[3::2] = True                   # Odd numbers are prime candidates

    for i in range(3, int(num ** 0.5) + 1, 2):
        if flags[i]:
            flags[i*i:num:2*i] = False   # Eliminate multiples of this prime

    primes = array.array('I', (i for i, flag in enumerate(flags) if flag))

    return primes

Timings:

10^3:  0.000
10^4:  0.000
10^5:  0.004
10^6:  0.051
10^7:  0.428
10^8:  4.506

Note: Updated timing info.  I just noticed I had for i in range(3, num + 1, 2) in the last implementation instead of for i in range(3, int(num ** 0.5) + 1, 2), resulting in a lot of wasted time doing nothing.

Python 3.8 Update: Using math.isqrt(num) is better than int(num ** 0.5):

    for i in range(3, math.isqrt(num) + 1, 2):


If it is PHP source code you want to highlight, why not use the tokenizer that's build into PHP itself? I use that and it seems to work fine:

$tokens = token_get_all($sourceCode);

Then all you have to do is walk through all the tokens and give them a color.

See: https://www.php.net/manual/en/function.token-get-all.php

No extra library, no faffing around with endless regular expressions.


Automatically Sending an Email based on a Google Sheet's cell value

Few minor things to improve:

you may unpack row in the for row in rows loop; moreover, you can use extended Python-3.x iterable unpacking
use enumerate() to handle the indexing
remove unused pprint import
remove extra parenthesis around row[4] == "FALSE" and float(row[0])/30 > 0.8 and send_email(is_sent, is_passing)
respect PEP8 guidelines - specifically, watch for spaces around the operators
you can use an f-string to define the cell name

The code of the main() function with the above mentioned improvements applied:

for index, (score, *_, is_email_sent) in enumerate(rows):
    if is_email_sent != "FALSE":
        continue

    is_passing, is_sent = float(score) / 30 > 0.8, False
    if send_email(is_sent, is_passing):
        worksheet.update_acell(f'E{index}', "TRUE")  # Updates the cell to reflect true
    else:
        print("these are not the droids you\'re looking for...")

I am, also, not really sure about that is_sent variable and send_email() having this argument. I think send_email should simply be responsible for sending an email and nothing - do one thing (Single Responsibility Principle). Printing out a message if an email was already sent should be done outside of the function.


Google Code Jam Google String problem: a sequence resulting from bit flips and reversals

Instead of trying to actually calculate Sgoogol, let's try to figure out some properties of the general case Sn and see if that can help.

\$S_n\$ has an odd number of digits

It is obviously of the form \$2k+1\$

\$S_n\$ has length \$2^n - 1\$

This is easily proved with recursion

\$S_n[\frac{2^n}{2} + 1] = S_n[2^{n-1} + 1] = 0\$

From the definition. I'm assuming 0-based indexing.

If \$k > 2^{n-1} + 1\$, then \$S_n[k] = switch(S_n[(2^n - 1) - 1 - k])\$

From the definition. This allows us to convert between halves of the string.

This suggests a simpler algorithm.

def find_value(index, n):
    if n == 2:
        return [True, True, False][index]
    if k == 2 ** (n-1) + 1:
        return True
    if k > 2 ** (n-1) + 1:
        return not find_value((2^n - 1) - 1 - k, n-1)
    return find_value(k, n-1)

And you can convert to 0 or 1 afterwards. (This may blow the stack, so you may have to rewrite iteratively)


Single method for different parameterized mysql command

If I assume that leave_detail_id contains an auto-increment value, starting at 1, you can do:

function queryDatabase($query, $bindings = [])
{
    global $connPDO;
    // prepare statement
    $statement = $connPDO->prepare($query);
    // execute statement
    $statement->execute($bindings);
    // return result in associative array
    return $statement->fetchAll(PDO::FETCH_ASSOC);
}

function getLeaveRequestDetails($leaveCurrentStatus, $leaveDetailId = 0)
{
    // build query
    $query = "SELECT *,
                     emp.Employee_Name,
                     emp.Designation,
                     emp.Emp_Dept_Category,
                     lt.Leave_Name
              FROM tbl_emp_leave_details eld
              INNER JOIN tbl_leave_types lt ON lt.leave_id = eld.leave_type
              INNER JOIN employee emp ON emp.PK = eld.emp_id
              INNER JOIN dept_category dc ON emp.Emp_Dept_Category = dc.PK
              WHERE eld.leave_current_status_id = :leave_current_status_id";
    // define bindings
    $bindings = [":leave_current_status_id" => $leaveCurrentStatus];
    // should details id be use?
    if ($leaveDetailId > 0) {
          $query .= " AND eld.leave_detail_id = :leave_detail_id";
          $bindings[":leave_detail_id"] = $leaveDetailId;
    }
    // query database and return result
    return queryDatabase($query, $bindings);
}

I used a default function argument value to set $leaveDetailId to zero, if it is absent, when the function is called. If it does get a value bigger than zero the condition is added to the query and the value is bound.

I also removed the actual database querying from this function and put it in a separate function. The reason is that this code can probably be used more often in your scripts. Instead of repeating it every time you can now call queryDatabase().

Finally I removed the echo json_encode(); from the function. The function name starts with "get" and that almost always implies something will be returned. Also, echoing JSON would restrict this function to that task alone. Now you can still use it to echo JSON, but you can also use it to do something else with the result. A call, returning JSON, would look something like this:

echo json_encode(getLeaveRequestDetails("fallen",15));

As you can see I've now split your code into more parts than you had before. That's not a bad thing. Now each part performs a single function: Querying the database, getting details of a leave, and echoing JSON. This makes your code easier to read.

In summary:

Do not repeat yourself (DRY). This was your reason for asking this question.
functions should be created with reusability in mind, even the specialized ones. That's the whole reason for their existance.
functions should only perform one task (Single responsibility principle).


Personally, I wouldn't use that approach, because of the amount of boilerplate code and complexity it introduces. Adding N+1 classes, where N is the number of conditions, just seems like overkill to me.

First off, I don't think that the method at hand must necessarily be refactored. I agree that is not a terribly "nice" method from a aesthetic point of view, but the amount of complexity it contains might not justify a refactor. Often, a bit of reformatting is already a good approach:

public int inferCategoryID(Component c) {
    if( c.hasOneChannel() &&
        c.hasValidCertificate() &&
        c.isTested() &&
        c.frequentlyUsed()) {
        return 24;
    }

    [...]
}

If that is not satisfactory, my next approach would've been to simply used separate methods for the conditions. This doesn't imply creating lots of classes, but still clean up the inferCategoryID substantially. While I'm not a fan of giving names like isCat24, I will use it here since the example refactoring technique using classes does so too and I can't infer more useful naming from the conditions:

public int inferCategoryID(Component c) {
    if(isCat24(c)) {
        return 24;
    }

    [...]
}

public boolean isCat24(Component c) {
    return c.hasOneChannel() &&
           c.hasValidCertificate() &&
           c.isTested() &&
           c.frequentlyUsed();
}

Last but not least, in cases where lots of boolean conditions are used, bit-lists are often applicable. The following code isn't tested, but it should give an idea of what I mean:

const int CATEGORY_24 = 15;
const int CATEGORY_95 = 13;

public int inferCategoryID(Component c) {
    int properties = componentProperties(c);

    if(properties == CATEGORY_24) {
        return 24;
    }

    if(properties == CATEGORY_95) {
        return 95;
    }

    [...]
}

public int componentProperties(Component c) {
    return ((int) c.hasOneChannel() << 3) &&
           ((int) c.hasValidCertificate() << 2) &&
           ((int) c.isTested() << 1) &&
           ((int) c.frequentlyUsed());
}

The integer returned from componentProperties is a dense representation of the components properties in form of a bit list, that is each bit represents one boolean property. For example, if all properties are true, the resulting integer (in binary) would be 1111, if only hasOneChannel was true, the integer would be 1000 and so on. This has the advantage that you don't have to create a large amount of methods or classes, but still cleans up the inferCategoryID method quite nicely. The downside of this method is that it is arguably the most cryptic (while I also find the multiple-class approach relatively cryptic :D).

Since I said that adding lots of classes introduces too much complexity to be really worth it (in this case), I can't say that the last solution is any different in terms of adding unnecessary complexity, so I probably wouldn't use either of them for the use case at hand. I just wanted to give you an idea of a different approach that, in my opinion, is preferable to creating a class per condition :)


Calculate all possible combinations of an array of arrays or strings

You don't need the else if/else blocks at the top; it can just be if blocks, since they return and thus exit the function anyway.

if (arr.length === 0) {
  return []; // stop here
}

if (arr.length === 1){
  return arr[0]; // or here
}

// or continue with the rest of the function

As you yourself noted, normalize the input, so you don't need to be type-checking inside the loop

But I'd advice you ignore that you're dealing with strings specifically, and just worry about "combinations of stuff", and try for a generic solution.

Still, the manual array-building makes your code cryptic. I'd suggest a more functional approach, using map and reduce.

Not saying it's easier to follow at first glance, but there's less code, and it's generic:

// this produces a 2d array of combinations
function combinations(array) {
  if(!array.length) {
    return [];
  }

  // wrap non-array values
  // e.g. ['x',['y','z']] becomes [['x'],['y','z']]
  array = array.map(function (item) {
    return item instanceof Array ? item : [item];
  });

  // internal recursive function
  function combine(list) {
    var prefixes, combinations;

    if(list.length === 1) {
      return list[0];
    }

    prefixes = list[0];
    combinations = combine(list.slice(1)); // recurse

    // produce a flat list of each of the current
    // set of values prepended to each combination
    // of the remaining sets.
    return prefixes.reduce(function (memo, prefix) {
      return memo.concat(combinations.map(function (combination) {
        return [prefix].concat(combination);
      }));
    }, []);
  }

  return combine(array);
}

To convert the output of the above function to strings, just add another map call:

function allPossibleCases(arr) {
  return combinations(arr).map(function (combination) {
    return combination.join(" ");
  });
}

You get this:

[ 'x', 'y', 'z' ]                   => [ 'x y z' ]
[ [ 'a', 'b' ], 'c', 'd' ]          => [ 'a c d', 'b c d' ]
[ [ 'a', 'b' ], [ 'c', 'd' ], 'e' ] => [ 'a c e', 'a d e', 'b c e', 'b d e' ]

Note that the order of the last one is different from yours, though it contains the same combinations.


Do you really have a good reason for this permission setup?

RUN chmod 777 /root/startup.sh

It's a bit ironic to use the -v flag of tar, and then redirecting the output to /dev/null here:

RUN tar -xvzf httpd-2.4.18.tar.gz >/dev/null

You can drop that flag and the redirection, the outcome should be the same.

I'm not very familiar with Docker, but I'm wondering what will happen if any of the commands fail. For example, if make fails, you probably don't want to run the make install command that follows it.
I don't have a solution for that, but error handling is something to think about.

The repetitive redirections and duplicated strings don't look pretty, and look hard to maintain. There gotta be a better way. Perhaps you could put the shell commands into a proper .bash script, enjoy all the power of regular bash scripting (variables, functions), and in the docker file just fetch that script using wget, and then execute it.


First, your loops are evil to read.  It took me several minutes to figure out what the condition did.  Essentially, you loop N times, where N is the Nth instance of the character in the string you wish to find the index for.  Each time you loop, you find the index of the next occurrence of the character in the string, update the position to search from, and continue the process until the index is either -1 (no Nth instance), or until n > 0 or n < 0, based on which side you are searching from.

A simpler way to write this algorithm is as follows:

public static int NthIndexOfC(this string input, char charToFind, int n)
{
    int position;

    switch (Math.Sign(n))
    {
        case 1:
            position = -1;
            do
            {
                position = input.IndexOf(charToFind, position + 1);
                --n;
            } while (position != -1 && n > 0);
            break;
        case -1:
            position = input.Length;
            do
            {
                position = input.LastIndexOf(charToFind, position - 1);
                ++n;
            } while (position != -1 && n < 0);
            break;
        default:
            throw new ArgumentOutOfRangeException(message: "param cannot be equal to 0", paramName: nameof(n));
    }

    return position;
}

It takes a little bit more room since I wrote the loop body on its own lines, but it is easy to understand, and there are no assignments in the loop condition.

However, this is still not the way I would write this algorithm.  When you think about how IndexOf works, it iterates the string with a for loop from the specified starting index until it reaches the first instance of the requested character, which it returns.  We can write similar behavior, but just keep track of which index we are at:

public static int NthIndexOf(this string input, char charToFind, int n)
{
    int count = 0;
    switch (Math.Sign(n))
    {
        case 1:
            for (var index = 0; index < input.Length; index++)
            {
                if (input[index] == charToFind)
                {
                    count++;
                }

                if (count == n)
                {
                    return index;
                }
            }
            break;
        case -1:
            for (var index = input.Length - 1; index >= 0; index--)
            {
                if (input[index] == charToFind)
                {
                    count++;
                }

                if (count == Math.Abs(n))
                {
                    return index;
                }
            }
            break;
        default:
            throw new ArgumentOutOfRangeException(message: "param cannot be equal to 0", paramName: nameof(n));
    }

    return -1;
}

This is the longest solution yet, but we aren't finished.  Notice all that duplicated code?

public static int NthIndexOf(this string input, char charToFind, int n)
{
    switch (Math.Sign(n))
    {
        case 1:
            return NthIndexOf(input, charToFind, n, 0, input.Length);
        case -1:
            return NthIndexOf(input, charToFind, n, input.Length - 1, -1);
        default:
            throw new ArgumentOutOfRangeException(message: "param cannot be equal to 0", paramName: nameof(n));
    }
}

public static int NthIndexOf(string input, char charToFind, int n, int searchFrom, int searchTo)
{
    int count = 0;
    var index = searchFrom;

    while (index != searchTo)
    {
        if (input[index] == charToFind)
        {
            count++;
        }

        if (count == Math.Abs(n))
        {
            return index;
        }

        index += Math.Sign(n);
    }

    return -1;
}

There.  Clean, easy to understand and maintain, and lots of options to use.  Notice that the second NthIndexOf needs to have the searchTo one integer larger or smaller than the value you want to stop at to accommodate the multi-directional search.


Merging totals from rows with duplicate IDs

A safer way than this would be to do something like

CREATE TABLE `cart_items_nodupes` (
  `id` int(11) NOT NULL,
  `account_id` int(11) unsigned NOT NULL,
  `itemNumber` int(11) NOT NULL,
  `itemQuantity` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (account_id, itemNumber)
) ENGINE=InnoDB CHARSET=utf8;

This creates a new table that maintains a unique index on account_id and itemNumber, so you'll never have a duplicate.

Then

INSERT INTO `cart_items_nodupes`
SELECT * FROM `cart_items`
ON DUPLICATE KEY UPDATE itemQuantity = itemQuantity + VALUES(itemQuantity)

Or

INSERT INTO `cart_items_nodupes`
SELECT MIN(id) AS id, account_id, itemNumber, SUM(itemQuantity) AS itemQuantity
  FROM `cart_items`
  GROUP BY account_id, itemNumber

Either of these will insert all the values from the old table into the new table, combining those with the same account_id and itemNumber.

And

ALTER TABLE cart_items_nodupes
  DROP PRIMARY KEY,
  MODIFY id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  ADD UNIQUE INDEX uc_account_item (account_id, itemNumber)
ALTER TABLE cart_items_nodupes AUTO_INCREMENT = 56
RENAME cart_items TO cart_items_original, cart_items_nodupes TO cart_items

This creates a new table that ensures there are no duplicates and then copies all the values from the original table.  Then it sets up the primary key so as to continue from the original table.  Finally, it renames the tables so that the new table takes the place of the old table.  The 56 was taken from your table definition -- modify as necessary.  Then you can just continue using the table as normal (after you check the table definition and rows).

Note that you need to stop updating the table while doing this.  You can sort of make the INSERT ON DUPLICATE KEY UPDATE work by saying WHERE id < 56 (again, 56 is based on the table definition -- adjust as necessary).  If new rows are added, you can get them with WHERE id >= 56.  But you should stop updates before altering the table indexes and renaming the tables.

An advantage of doing things this way is that if the data is wrong after the inserts, you can always drop the table and try again.  Your original data is still there, untouched.  And you can test it before doing it for real.


Map groups to parent products using nested subqueries

I think the formatting of your code could use some improvements :

select *
from productstbl
where substring(SegmentCode,1,1) like
(
    select substring(SegmentCode,1,1)
    from categoriestbl
    where CatID in
    (
        select CatID
        from CategoriesGroupsInfo
        where GroupID = 1
    )
)

Selecting every column can be expensive; I would recommend to optimize the query to select only the columns you need.

You could also potentially improve the performance (though not necessarily the execution plan) by creating a stored procedure, so the SQL engine wouldn't have to recalculate the execution plan on each call. This also make it much easier to call it from application code.

As far as improving the execution plan, you could modify the database to add indexing to SegmentCode, or add some columns to contain the individual segment codes (if that is feasible).

Short of changing the database, you could use #TempTables and add indexes on the column(s). That would increase I/O but may improve the execution plan.

On another note, you could most likely change like to in since you are comparing a single character with another. like is mostly used for string comparisons using wildcards, which are very expensive and you are not doing.


Program to calculate Hash and Size of a torrent

Using classes for this seems like a good idea, since you end up with a number of class instances, where each one represents a specific torrent.

In terms of the specific code, you're doing things slightly wrong in 2 ways.

Firstly, you don't need to pass instance parameters into the methods of a class. So you can access info and file as self.info and self.file, so your methods only need the self argument.

Secondly, I can see that you're doing this to try to cache the results of the method calls by overriding the methods in __init__, and while caching is good, this is a bad way of trying to achieve it.

There are 2 alternatives that spring to mind, depending on what you want to do:

If you always want the size and hash calculated when the class is instantiated, then do something similar to what you're doing now, but use different names for the data variables and the methods:

def __init__(self, torrentfile):
    self.metainfo = bencode.bdecode(torrentfile.read())
    self.info = self.metainfo['info']
    self.files = self.metainfo['info']['files']
    self.md5hash = self.calculate_md5hash()
    self.size = self.calculate_size()

def calculate_md5hash(self):
    return hashlib.sha1(bencode.bencode(self.info)).hexdigest()

def calculate_size(self):
    filesize = 0
    for file in self.files:
        filesize += file['length']
    return filesize

Alternatively, if you only want the hash and size calculated when the methods are specifically called, but you also want caching, use lru_cache

lru_cache will cache the result of a function the first time it is run, and then simply return the result for future calls, providing the arguments to the function remain the same.

from functools import lru_cache

class Torrent(object):

    def __init__(self, torrentfile):
        self.metainfo = bencode.bdecode(torrentfile.read())
        self.info = self.metainfo['info']
        self.files = self.metainfo['info']['files']

    @lru_cache()
    def md5hash(self):
        return hashlib.sha1(bencode.bencode(self.info)).hexdigest()

    @lru_cache()
    def size(self):
        filesize = 0
        for file in self.files:
            filesize += file['length']
        return filesize

Then call the methods explicitly:

print(torrentfile.md5hash())
print(torrentfile.size())


Navigation bar built from fetched JSON data

Now, is my code efficient?

We don't care. There's not much going on here, there are not thousands of elements in the DOM, and there are no tight data processing loops. If the UI feels sluggish, something must have been gone horribly wrong.

However, there is one inefficiency in your code that matters: The JSON file is fetched on really every single click. I'm pretty sure this is not desired.

It would be OK if you expect it to change really often (that is, < 15min); or if the response would be dynamically created and you did transmit the currently selected option as a parameter.

  Especially, the click function within getRegion function? Is this the correct way to handle the click events in OO?

Yes, that's fine.

However, a few oddities I found in your code:

You use Object.create and an init function. That's OK if you want to do it that way, and good to understand prototypical inheritance. Yet, I think using a constructor function and new is simpler, as it requires only a single call for instantiation.
Using self in your init function is unnecessary. Just use this, as you do in fetch or display.
Why assign .elem? You never need it.
The url should definitely not be a constant, but go into the options. 'data.json' may be the default value.
navigation is not the most descriptive property name. From the usage, I'd expect an adjective like current or selected in the name.
You use options.navigation to create self.navigation before assigning it a default value.
cycle is not an optimal name either, it sounds as if it cycles through the options periodically. Name it after what the method actually does - or just inline it into the initialisation code.
I'm not entirely sure what getRegion does and whether it needs to go into the fetch callback. If you move it outside, you might avoid the code duplication with the fetch in the click handler below.
You shouldn't use options.navigation for storing the state, but the this.navigation property (which you created in init but never use it) - both in buildFrag and the click handler.
Why does buildFrag assign to that list property? Instead, return your result from the method! display should then take this as a parameter (or you inline it right away).
getRegion is misnamed as well. The only code that is "getting the region" is $ (this).attr ('id').split ('_')[0]. What your whole method does is something like setupRegionSelectHandlers or so.
What is $ ('#flags').find ('div')? This shouldn't be static, make the selector an option. Also, it would get clearer if those selection-sensitive divs were stored in a property similar to .$elem - I assume they're not descendants of each other?
As said above, the clicks shouldn't fetch the file again. Instead, store the promise for the result on your instance, and reuse that for every display.

I'd do something like this:

(function ($, window, document, undefined) {

    function Navigation(options, elem) {
        if (typeof options === 'string')
            options = {navigation: options};
        this.options = $.extend ({}, $.fn.drawNavigation.options, options);

        this.$elem = $(elem);
        this.$selects = $(this.options.select)

        this.navigatedRegion = this.options.navigation;

        this.fetched = $.ajax({
            url: this.options.url
        });

        this.display();
        this.setupSelectionEvents();
    }

    Navigation.prototype.display = function () {
        var self = this;
        return this.fetched.then(function(results) {
            self.$elem.hide().html(self.buildFrag (results)).fadeIn(800);
        });
    };

    Navigation.prototype.buildFrag = function(results) {
        var self = this;
        return $.map(results[this.navigatedRegion].navigation, function(obj, i) {
            return $(self.options.wrapEachWith).append(obj);
        });
    };

    function activate(item) {
        $(item).siblings().removeClass('flag-active');
        $(item).addClass('flag-active');
    }

    function getRegion(item) {
        return $(item).attr('id').split('_')[0];
    }

    Navigation.prototype.setupSelectionEvents = function() {
        var self = this;
        this.$selects.on('click', function () {
            activate(this);
            self.navigatedRegion = getRegion(this);
            self.display();
        });
    };

    $.fn.drawNavigation = function(options) {
        return this.each(function() {
            new Navigation(options, this);
        });
    };

    //default
    $.fn.drawNavigation.options = {
        navigation: 'uk',
        wrapEachWith: '

',
        select: '#flags div',
        url: 'data.json'
    };

}(jQuery, window, document));


There is an arithmetic solution to this problem, without converting to a string. It's probably what the string conversion does internally and will therefore likely be much more efficient.

Quoting Wikipedia:

  The digit sum of a number \$x\$ in base \$b\$ is given by
  $$
\sum_{n=0}^{\lfloor \log_b x\rfloor} \frac{1}{b^n}(x \bmod b^{n + 1} - x \bmod b^n).
$$

This StackOverflow answer has an implementation in C# (which is very similar to Java):

sum = 0;
while (n != 0) {
    sum += n % 10;
    n /= 10;
}

In addition, I'd like to comment on the naming of your method. In my opinion, digit doesn't describe its function very well. Instead I'd recommend something similar to digitSum.

Note that this approach doesn't handle negative numbers. They cause the function to go into an infinite loop. To handle that case, you can something like the following:

public static int digitSum(int n) {
    int sum = 0;
    bool isNegative = false;

    if (n < 0) {
        isNegative = true;
        n *= -1;
    }

    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }

    if (isNegative) {
        return -sum;
    } else {
        return sum;
    }
}


There are several ways we can go to make the code cleaner and faster. Grouping by the scope.

search_item() function

link would probably be a better variable name than a less descriptive item
move the substrings you are looking for into a constant - defined as a list of keywords
then, you can use any() instead of multiple or conditions
fromstring() returns a root node of the tree and not the tree itself - would probably be more logical to name the variable root instead of tree
do not re-do item.lower() - do it once

You may get something like this in the end:

KEYWORDS = ["contact us", "about us", "contact", "about"]

def search_item(driver,url):
    driver.get(url)
    root = fromstring(driver.page_source)

    for link in root.xpath("//a/@href"):
        link = link.lower()
        if any(keyword in link for keyword in KEYWORDS):
            get_item(urljoin(url, link))
            break

get_item() function

you should pre-compile your regular expression
use .search() instead of .findall(), since you are looking for a single match
this would then allow you to avoid handling a bare exception and switch to LBYL approach instead of the EAFP

You may get something like:

EMAIL_PATTERN = re.compile(r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+')

def get_item(link):
    response = requests.get(link).text

    match = EMAIL_PATTERN.search(response)
    if match:
        print(link, match.group(0))

Few other general comments

not sure if "contact us" and "about us" (with a space) would ever appear in an href value..
re-group your imports as per PEP8 recommendations and avoid having multiple different imports on the same line
main_url should probably named be something like start_urls (you need a multiple of "url" since you have a list of them). And, you need to indent the items since you are defining your list on multiple lines:

start_urls = [
    'http://advanta.in/',
    'http://warnaengineeringworks.com/',
    'http://unifrostindia.com/',
    'http://www.superrefrigerations.com/',
    'http://www.westernequipments.com/',
]

consider adding documentation strings to your functions


Python Multi Choice Quiz with a score to count

The biggest gain in readability here is to get rid of the duplication. Currently all your strings are hardcoded and you print each of them separately.

We can make life easier by putting the questions, answers and correct answers into some datastructure we can iterate over. In addition, we can bundle these three things together into one Question class (which is just a collections.namedtuple here, so we can give the fields nice names):

from collections import namedtuple

Question = namedtuple("Question", ["question", "answers", "correct_answer"])

GREETING = """Hello {name}, welcome to Quiz night!
You will be presented with {n} questions.
Enter the appropriate number to answer the question
Good luck!"""

def play_quiz(name, questions):
    score = 0
    for question in questions:
        print(question.question)
        for i, answer in enumerate(question.answers, start=1):
            print(f"{i}. {answer}")

        response = input("Your answer is: ")
        if response != question.correct_answer:
            print("Sorry, that is incorrect!")
        else:
            print(f"Well done! {response} is correct!")
            score += 1
        print(f"Your current score is {score} out of {len(questions)}")

    print(f"Your total score is {score} out of {len(questions)}")
    print(f"Thank you for playing {name}, goodbye!")

if __name__ == "__main__":
    questions = [Question("What is the term for the ‘Maori’ language?",
                          ["Te Rex", "Hangi", "Hongu", "Te Reo"], "4"),
                 Question("What is the Maori term for ‘tribe’ or ‘mob’?",
                          ["Mihi", "Iwi", "Awi", "Hapu"], "2",
                 ...]
    name = input("What is your name? ").title()
    print(GREETING.format(name=name, n=len(questions)))
    play_quiz(name, questions)