Here are some things that may help you improve your code.

Use consistent formatting

The code as posted has inconsistent indentation which makes it hard to read and understand. Pick a style and apply it consistently.

Use more whitespace

Lines like this one:

for(int i=0;i<5;i++){

are easier for most humans to read and understand with more whitespace like this:

for (int i = 0; i < 5; i++) {

Don't use system("cls")

There are two reasons not to use system("cls") or system("pause").  The first is that it is not portable to other operating systems which you may or may not care about now.  The second is that it's a security hole, which you absolutely must care about.  Specifically, if some program is defined and named cls or pause, your program will execute that program instead of what you intend, and that other program could be anything.  First, isolate these into a seperate functions cls() and pause() and then modify your code to call those functions instead of system.  Then rewrite the contents of those functions to do what you want using C++.  For example, if your terminal supports ANSI Escape sequences, you could use this:

void Clear()
{
    std::cout << "\x1b[2J";
}

Minimize the scope of variables

The variable movement is only used within InputHandle.  This means that instead of a parameter, it could and should be a local variable within the member functions rather than a passed parameter.

Only update when needed

The current code updates the screen as fast as possible whether or not there's any reason to do so.  This causes an annoying flicker on my computer and needlessly impedes the quick response of the program to user input.  I'd suggest instead modifying the InputHandle routine to return a bool which is true only if the player's position has changed.

Prefer std::array to raw C-style arrays

Modern C++ provides std::array as an enhancement of a raw C array because it has knowledge of its own size, which helps with bounds checking.

Eliminate "magic numbers"

This code has a number of "magic numbers," that is, unnamed constants such as 4, 5, etc.  Generally it's better to avoid that and give such constants meaningful names.  That way, if anything ever needs to be changed, you won't have to go hunting through the code for all instances of "5" and then trying to determine if this particular 5 is relevant to the desired change or if it is some other constant that happens to have the same value.

Simplify your code

The current code contains this function:

bool CheckWinCondition(int &pX, int &pY)
{
    if (pX == 3 && pY == 4) {
        return true;
    }
    return false;
}

This can be simplified:

bool CheckWinCondition(int &pX, int &pY)
{
    return pX == 3 && pY == 4;
}

Use a portable library

You are right to want to eliminate the use of conio.h because it is neither standard nor portable.  For example, your constants for keys don't work on my machine at all.  One possible alternative would be to use the ncurses library.  See Ncurses Snake game for inspiration on how to use the ncurses library in C++.

Use better names

The function CheckWinCondition and variable maze are good, descriptive names, but InputHandle and pX are not as good.  I'd probably change InputHandle to handleUserInput and pX to simply x.  Also, CheckWinCondition could be named isPlayerAtGoal to make it easy at a glance to understand what true and false might mean.

Don't use macros for constants

Instead of having KEY_UP and friends as old-style #define values, a better, more C++ approach is to use constexpr instead. See ES.32 and Con.5.

Use an object

The Maze would make a rather obvious object.  It could include the maze structure, the player's current position and the goal's location.  Methods (member functions) could include all of the current free-standing functions and the main could be reduced to this:

int main() {
    Maze maze;
    maze();
}

Further hints

Here's a rewritten display() function written using ncurses as a member function:

void Maze::display() const
{
    clear();
    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            addch(tokenAt(row, col));
            addch(' ');
        }
        addch('\n');
    }
}

The clear() and addch() functions are from ncurses and tokenAt() looks like this:

char Maze::tokenAt(int row, int col) const {
    return (col == x && row == y) ? 'P' : maze[row][col];
}

Here's the member function that actually runs the maze:

void Maze::operator()() {
    initscr();
    noecho();
    nodelay(stdscr, TRUE);
    keypad(stdscr, TRUE);
    display();
    while (!isPlayerAtGoal()) {
        if (handleUserInput()) {
            display();
        }
    }
    endwin();
    std::cout << "Congrats, you finished the maze!\n";
}


Implementing a binary operation for the null object pattern

The implementation of a NullRegion, shouldn't be done by a separate class, but by a static property of the same class.
If you overide Equals() and GetHashCode() you can just compare the objects to Region.NullRegion like

public class Region
{
    private static readonly Region nullRegion = new Region();

    private Region()
    {
        Left = 0;
        Top = 0;
        Width = 0;
        Height = 0;
    }
    public Region(Point point, Size size)
    {
        Left = point.X;
        Top = point.Y;
        Width = size.Width;
        Height = size.Height;
    }
    public static Region NullRegion
    {
        get
        {
            return nullRegion;
        }

    }
    public int Left { get; set; }
    public int Top { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }

    public Region Merge(Region other)
    {
        if (this == Region.NullRegion || other == Region.NullRegion)
        {
            return Region.NullRegion;
        }
        // default Merge() implementation here
    }
    public Region Intersects(Region other)
    {
        if (this == Region.NullRegion || other == Region.NullRegion)
        {
            return Region.NullRegion;
        }
        // default Intersects() implementation here

    }
    public override bool Equals(object obj)
    {
        //Implementation of your Equals() here
        return base.Equals(obj);
    }
    public override int GetHashCode()
    {
        //Implementation of your GetHashCode() here
        return base.GetHashCode();
    }
}


Container for fast deletion and addition of elements

struct Laugh {
  Laugh() {
    std::cout << "Ha";
  }
};

int main() {
  ObjectPool<Laugh> pool(3);
}

Do you hear the evil laughter?

"active" vs the lifetime of an object

What happened?  When you resize the vector from its initial size 0 to some larger size in the constructor, then the vector will be filled with default-inserted objects.  So directly after construction of your object pool, it is already completely filled with alive objects.  These objects stay alive all the time (unless you resize).  Your notion of an "active" object basically just revolves around whether one can access (through at) that object.  "deleting" just moves one object out of the "active" (accessible) region of your pool.  "adding" enlarges this "active" region.

This is very confusing and not what one does expect from an "object pool"!

Further, it puts up some requirements to that Element type, namely that it must be default-insertable.  With the standard allocator (which you use) this means that Element has to be constructible through value initialization.  That's a pretty tough requirement, as it basically means that Element needs to have a zero argument constructor.  Now, if Element is a bit more than a simple value / data aggregation type, then its objects will probably depend on some objects of other types.  To follow the SRP one wouldn't let an Element construct the other objects then, but rather pass it the requirements upon its own construction (dependency injection).

Your object pool design does not allow this, though.  In short:  You sign yourself up for lots of future trouble.

To fix this, use reserve for your vector in the constructor, and actually construct new objects when adding (make emplace_back of the vector accessible) and destruct (terminology is important!) them when you want to get rid of them.  To keep that later operation fast, swap the last element and the one you want to get rid of and then erase the last. (Of course this only works when order doesn't matter.  Moreover, it invalidates possibly obtained references to the last element.)

Swapping done right

Calling std::swap directly is almost always wrong.  Bring it into scope with using std::swap;, and then just do an unqualified call: swap(a, b).  This allows ADL to kick in and find potentially useful user-defined swap functions.

Provide a complete interface

std::vector provides at, which - like yours - does bounds checking.  It also provides operator[] which does not.  Thus, I'd hope for (expect) your class to provide this feature, too.

Furthermore, I'd follow the terminology used by the standard std::vector for adding / removing elements here: push_back (or emplace_back) and erase.

Expose the allocator

std::vector allows to use a different allocator than the standard one.  You should expose this with your pool, too.  That allows for object pools whose objects have automatic storage duration ("are on the stack") when used with such an allocator.


A few quick notes:

In case of square_generator() and next_fermat() you have unnecessary...

while True:

Anytime all paths through the loop encounter a return or break, the loop will never loop.

This:

 return bool(num % 4 == 1)

can be:

 return num % 4 == 1

since the == will always produce a bool.

I would code:

def next_fermatf(self):
    true, num = self.next_fermat()
    if true:
        print("%d\t\t%f" % (num, (self.fermat_number_true / (self.fermat_number_false + self.fermat_number_true)) * 100))
    else:
        print("%d\tX\t%f" % (num, (self.fermat_number_true / (self.fermat_number_false + self.fermat_number_true)) * 100))

more like:

def next_fermatf(self):
    is_next_fermat, num = self.next_fermat()

    msg = "" if is_next_fermat else "X"
    print("%d\t%s\t%.1f%%" % (num, msg,
        100.0 * self.fermat_number_true /
        (self.fermat_number_false + self.fermat_number_true))))

Changing true to is_next_fermat allows if is_next_fermat to be read, while if true reads more awkwardly.
Eliminate common code
Keep the code below the pep8 recommended 80 columns
Format the % with fixed decimal and show the units.


You don't use any method which is specific to List<T>. You should always code against interfaces if possible, meaning you should use IList<T> instead of List<T>.
If Retrieved is called very often in a short time it is likely that you get the same object each time. This is because Random when instantiated will use the system clock to provide a seed value. You should have one Random object as a class-level field which should be instantiated in the constructor or directly.
The genericness of the method Add<T> doesn't buy you anything. A simple Add(object) would be enough and does the exact same.
objects should be made readonly because you don't change it.
For Retrieve and Remove I wouldn't introduce the numberObjects variable but if you want to keep it you should rename it to e.g numberOfObjects.

Implementing the mentioned points would look like this:

public class Sack
{
    private readonly IList<object> objects = new List<object>();
    private readonly Random rand = new Random();

    public void Add(object obj)
    {
        objects.Add(obj);
    }

    public object Retrieve()
    {
        int selectedIndex = rand.Next(0, objects.Count);

        return objects[selectedIndex];
    }

    public object Remove()
    {
        int selectedIndex = rand.Next(0, objects.Count);

        object selectedObject = objects[selectedIndex];
        objects.RemoveAt(selectedIndex);

        return selectedObject;
    }

    public void Empty()
    {
        objects.Clear();
    }
}


Finding if there exists an input for a given automata for which no matter what the starting state is, final state would be same everytime

There's no need to define both __str__ and __repr__ for Node when they're both the same functionality. If you just define __repr__ it will be called in place of __str__ when necessary. (though the reverse is not true). See:

>>> class A:
    def __repr__(self):
        return "self"

>>> str(A())
'self'

Instead of assigning a value to x then deleting it with del, use pop.

x = y.pop(i)

This simultaneously passes y[i] to x while removing it from y.

x = y[i]

This is also quite a confusing function, adding a docstring would make it a lot clearer what it actually is/does. As would better names. y could be nodes_copy and x could be... node I guess? I'm not entirely clear on what x is meant to be for, since the name is meaningless. Also what's tNodes? Does the t stand for something? Traversed possibly? Except I don't see you adding anything to the empty list so I don't see what it does at all.

In FindTarget you're calculating node not in target twice. You do need to use this for two separate if statements, but you could just store the boolean to save running the test twice:

def FindTarget(node,p):
    if len(p) == 1:
        return node.dict[p[0]]

    node_in_target = node in target
    if not node_in_target or p not in target[node]:
        x = Gnodes[FindTarget(node,p[:-1])].dict[p[-1]]
        if not node_in_target:
            target[node] = {}
        target[node][p] = x
    return target[node][p]

As @JoeWallis pointed out, a dictionary is fast at testing these look ups, but that doesn't mean this wont still save time.

In allSatisy, isn't x just being set to the result of FindTarget(nodes[0], p)? Because FindTarget never returns None, x will always be set to some result and therefore will only be set on the first iteration. Unless I'm wrong and the node values returned from FindTarget can be None. If that's not the case though, this would make your function clearer:

def allSatisy(nodes,p):
    x = FindTarget(nodes[0], p)
    for node in nodes[1:]:
        if FindTarget(node,p) != x:
            return False
    return True

And if this is the same as your function, then there's a faster way to do this using Python's all function. It can take a generator expression to evaluate every element in a list for a condition and return the result as a single boolean. In your case, you want to know if all the results of FindTarget(node, p) are equal to x, for each node in nodes (after the first node). Thankfully that's practically the syntax:

def allSatisy(nodes,p):
    x = FindTarget(nodes[0], p)
    return all(FindTarget(node, p) == x for node in nodes[1:])


I have plenty of things to say here..

You should apply separation of concerns by isolating the pooling behavior from the logic of the webmethod itself.
you might want to create both instances at startup
blocking inside the web methods will seriously limit throughput, consider using async methods
What happens if there is an exception and you don't release the mutex?
The pooling can be implemented much simpler using higher level concurrency features, for example a BlockingCollection:

public class ObjectPool<T>
{
    private readonly BlockingCollection<Lazy<T>> _pool;

    public ObjectPool(int size, Func<T> constructor)
    {
        var resources = Enumerable.Repeat(new Lazy<T>(constructor), size);
        var stack = new ConcurrentStack<Lazy<T>>(resources);
        _pool = new BlockingCollection<Lazy<T>>(stack);
    }

    public IDisposable Aquire(out T resource)
    {
        Lazy<T> lazy = _pool.Take();
        resource = lazy.Value;
        return new Disposable(() => _pool.Add(lazy));
    }

    public class Disposable : IDisposable
    {
        private readonly Action _dispose;

        public Disposable(Action dispose)
        {
            _dispose = dispose;
        }

        public void Dispose()
        {
            _dispose.Invoke();
        }
    }
}

The call to Aquire will block until an item is available in the pool, it's very important to call Dispose on the returned object, this will release the resouce back into the pool. You can use try/finally or a using statement:

    private static readonly ObjectPool<Random> pool
        = new ObjectPool<Random>(2, () => new Random());

    static int CalculateRandomNumber()
    {
        using (pool.Aquire(out var rnd))
        {
            return rnd.Next();
        }
    }

Notice how a Stack is used in combination with Lazy. This ensures that you will never create more instances than are in use at the same time.


Performance optimization for Linq To SQL

How much did you look into LINQ to SQL? Unless you have a reason not to expose the DataContext, you can just perform queries on it without using the 'search object'.

from c in DataContext.Customers
where c.City == "London" && c.ContactName == "Thomas Hardy"
select c

I haven't used LINQ to SQL myself yet, so I can't really tell much about the best practices and whether or not exposing DataContext is a good idea. Probably it's best to use an existing ORM framework. DataObjects.NET looks really nice.

A nice ORM overview can be found on ORMbattle.


Your code is very hard to read since there is so much empty space. Even if we don't remove all of them, the following variant is a lot easier to read:

#include <stdio.h>

static unsigned long total;

int main(void)
{
  total = 0;

  for(unsigned int i = 1; i < 334; i++){
     total  = total + (3*i);
  }

  for(unsigned int j = 1; j < 1000; j++){

    if(j%5==0 && j%3!=0){
      total  = total + j;
    }else{
      total = total + 0;
    }
  }

  printf("Project Euler problem 1 solution = ");
  printf("%lu \n", total);

  return 0;
}

It fits even into the code example without any scrollbars.

There is no need to make total global. It should be a local variable.

Your first for loop uses an optimization, your second doesn't. That's asymmetrical. Use the same trick for both loops.

total = total + 0 doesn't change total at all. The compiler will most likely throw that line away, but better don't include it.

The following for loop is easier to understand:

for(unsigned int i = 0; i < 1000; i += 3) {
    total += i;
}

Concerning your comments on +=, did you accidentally use total += total + j? This will give wrong results. Otherwise I cannot reproduce your behaviour.

If you printf a unsigned int, you must use %u, not %lu, since the latter is meant for unsigned long int. Your compiler will warn you mismatching specifiers if you enable warnings.

If you apply those comments above, you end up with

#include <stdio.h>

int main(void)
{
  unsigned int total = 0;

  for(unsigned int i = 0; i < 1000; i += 3){
     total += i;
  }

  for(unsigned int i = 0; i < 1000; i += 5){
    if(i % 3 != 0){
      total += i;
    }
  }

  printf("Project Euler problem 1 solution = %u \n", total);

  return 0;
}


Whenever you're concatenating strings inside a loop's body, +-concatenation should be ruled out; transformedText would be a StringBuilder instance, to which each iteration would Append.

However XAML is a subset of XML; as such, there is no reason to treat your output as purely strings: using the classes in the System.Linq.Xml namespace (otherwise known as "Linq-to-Xml"), you can rewrite this in a much more elegant and object-oriented way.

Extending System.String like this is a bit overkill, too. I'm not sure I'd like working in a code base where every single string out there has this ParseLinksToXaml extension method: that functionality is very specialized, and IMO deserves its own class where the client code is actually explicit about what's going on:

var parser = new StringLinksToXamlConverter();
var xaml = parser.ParseLinksToXaml(myString);

The string-building stuff going on under the hood should be an implementation detail - the returned value would be potentially much more useful to the client code if an XDocument was returned, leaving it up to the client code to query or output as a string.


Returns values for a specific array and key

I was able to play around with your requirements, and I think I got a solution.

Here is what I did to profile your code.

I set up the data array with:

$new = array(
    array('a' => 1,
        'b' => 2),
    array('a' => 3,
        'b' => 4),
    array('a' => 5,
        'b' => 6),
);

Calling it like so:

valuelist($new, 'b');

to produce:

Array
(
    [0] => 2
    [1] => 4
    [2] => 6
)

And the average function time was about 2.1894 E-6 seconds.

However, PHP 5.5 introduced us to the array_column function! And it happens to do exactly what you want! Yipee!

We can do a big swap-out, and come up with this:

function valuelist($array, $array_column) {
    return array_column($array, $array_column);
}

Now when we do the 50k calls, we learn that now the average function time is about 2.6226 E-10 seconds.

However, there is a little overhead due to the user function applied over top of the built-in function. By just calling array_column without the function does reduce the processing time, but by an amount so small it's hard to even say it's an amount! Just for the sake of cleanliness, I'd remove the user function anyways.

Don't tell me that's not an improvement!

However, without this function, I was unable to get a faster execution. I tried array_filter, array_map, and array_walk. Using use for the closure, as that proved faster than array_map's third parameter.


Python script for sorting a huge log file based on timestamps

You're ignoring the Date-part of the timestamps; it doesn't sound like that's on purpose.
(Also, the year is missing altogether, which should make us quite nervous.)
Also, let's use explicit datetime utilities and regexes.

import datetime
import re

timestamp_regex = re.compile("[^:]+:\d\d:\d\d")

def convert_time(logline):
    stamp = timestamp_regex.match(logline).group() #this will error if there's no match.
    d = datetime.strptime(stamp, "%b %e %H:%M:%S")
    return int(d.timestamp())

As for the rest, the comments are right that we can't do much unless we know exactly what it would mean for the solution to be improved.

If the concern is just to handle the biggest file with the least ram, something like this might work:

def save_where_we_can_find_it(line, temp_file):
    retval = temp_file.tell()
    temp_file.write(line)
    return retval

def fetch_line(location, temp_file):
    temp_file.seek(location)
    return temp_file.readline()

items = []

with open("log.txt", 'r') as original, open(".temp.log.txt", 'w') as temp:
    for line in original:
        items.append((convert_time(line), save_where_we_can_find_it(line, temp)))

items.sort(key = lambda pair: pair[0]) #sort-in-place isn't necessarily a good idea; whatever.

with open(".temp.log.txt", 'r') as temp:
    for (stamp, location) in items:
        print(fetch_line(location, temp))

import os
os.remove(".temp.log.txt")

But this is just a really inefficient way using a scratch-file. Better to register scratch-space in the OS, and then do your file manipulation "in memory".


Data flow anomaly: how to do it correct?

It would be bad practice to silently swallow an exception.  However, I think the following code would be acceptable:

String userAgent = "volley/0";  // Default value
try {
    String packageName = getPackageName();
    PackageInfo info = getPackageManager().getPackageInfo(packageName, 0);
    userAgent = packageName + "/" + info.versionCode;
} catch (PackageManager.NameNotFoundException justUseDefaultUserAgent) {
}

For the second snippet, I would use a ternary conditional to emphasize the goal of assigning stack a value no matter what.

HttpStack stack = (Build.VERSION.SDK_INT >= 9) ? new HurlStack() :
    new HttpClientStack(AndroidHttpClient.newInstance(userAgent));


Ruby-ize for loop - counting all the n-digit numbers that contain the digit 5 anywhere

I would use recurence in place of the loop - it is easier to understand the logic behind it then:

def number_containing_5(pwr)
  return 0 if pwr == 0
  number_containing_5(pwr-1) * 9 + 10**(pwr-1)
end


O(n) Solution

You don't need to keep track of all the places you can reach.  You only need to know what is the highest index you can reach, because you can reach any lower index by choosing a shorter jump.  Scan the list from 0 to the end keeping track of the maximum reachable index.  If you ever reach an index greater than the maximum reachable index, you've hit a block and can't reach the end.

For example, for a jump list = [3,2,1,0,4].  Starting at index 0, we can reach any index up to 0 + 3 = 3.  From index 1 we can reach 1 + 2 = 3.  From 2, we can reach 2+1 = 3.  From index 3, we can reach 3 + 0 = 3.  So far the maximum reachable index is 3.  So when we reach index 4, 4 > 3 so index 4 is unreachable and we can't get to the end.

def canjump(nums):
    maximum_reachable_index = 0

    for index, max_jump in enumerate(nums):
        #print(f"{index}", end=' ')
        if index > maximum_reachable_index:
            #print(f"> {maximum_reachable_index} and is unreachable.")
            return False

        #print(f"+ {max_jump} => {index + max_jump} : {maximum_reachable_index} ")
        maximum_reachable_index = max(index + max_jump, maximum_reachable_index)

    return True

Uncomment the print statements to see how it works.


Readability

It's a bit difficult to comment on the security aspects of code that's hard to read.
There are several readability issues here:

The indentation is very messy and inconsistent throughout.
It would be good to clean that up and use consistent indenting,
for example use 4 spaces inside every code block (inside { ... })
$_FILES["file".$x] is duplicated in many places,
which makes the code look very noisy.
You could save it in a local variable and the whole thing will look a lot simpler.
It's good to put a space around operators, for example:

instead of: $newfilename=$date." ".$originalname;
write like this: $newfilename = $date . " " . $originalname;

Simplify

Other simplifications are possible.

Instead of listing up all the lowercase and uppercase variants of extensions like this:

$allowedExts = array("docx", "DOCX", "DOC", "doc","pdf","PDF");

You could use list just in lowercase, and when you need to check the extension,
just wrap that in strtolower, like this:

in_array(strtolower($extension), $allowedExts)

You could reduce one nesting level in the conditions. Instead of this:

  if ($_FILES["file".$x]["error"] > 0) {
    // ...
  }
  else {
    if (file_exists($destinationdirectory . $_FILES["file".$x]["name"])) {
      // ...
    } else {
      // ...
    }
  }

You could use an elseif:

  if ($_FILES["file".$x]["error"] > 0) {
      // ...
  } elseif (file_exists($destinationdirectory . $_FILES["file".$x]["name"])) {
      // ...
  } else {
      // ...
  }


Extract Common Data From List of Objects

Basically, this boils down to the question of how to find out whether a given number of items is always the same. The mathematical tool for this is a set and checking its cardinality.

Putting this into code, you can use a Set implementation in Java:

Set<LocalDate> allStartDates = new HashSet<>();

for (...) {
    ...
    allStartDates.add(order.getStartDate());
    ...
}

if (allStartDates.size() == 1)
    commonData.setCommonStartDate(allStartDates.iterator().next());

BUT: in a real-world-application, I'd move this logic to the CommonData class.

Basically, redesign the CommonData in a way, that you can simply add an order and then boil the loop down to:

CommonData commonData = new CommonData();
orders.stream().forEach(commonData::add);

and inside the common data add method, add the codes and ids, maintain two sets of dates, and do the if-logic in the appropriate getters.


This is a most elegant solution. I have two small improvements:

let neighboursCount (board: int[,]) x y =
    board.[y..y+2, x..x+2]
    |> flatten
    |> Array.indexed
    |> Array.sumBy (fun (i, n) -> if i <> 4 then n else 0)

let expandBoard board =
    let N = (Array2D.length1 board) - 1
    array2D [|
        yield                    [| yield board.[N, N]; yield! board.[N, *]; yield board.[N, 0] |]
        for i in [0..N] do yield [| yield board.[i, N]; yield! board.[i, *]; yield board.[i, 0] |]
        yield                    [| yield board.[0, N]; yield! board.[0, *]; yield board.[N, 0] |]
    |]

Here is N set to (Array2D.length1 board) - 1 because that's the value you're using through out the function.


At the very least, you should place the second if statement in a finally block.  As things stand, the code is not generally correct from a purely mechanical perspective.  I would also phrase it as:

bool dbDispose = db == null;
db = db ?? new DataContext();

From a design point of view: yes, I'd say this violates the single responsibility principle.  You have a method that not only does certain calculations, but also creates a DataContext if necessary.  If creation has to be modified at any point, then you'll have many places to change that.  (Unlikely if it's default-constructed like this, but I doubt it's that simple in practice.)

It looks to me like SignIn should actually be a member function of DataContext.


Better code for error handling in NodeJs and Express

What you first need to do is convert all non-promise/callback-based operations into promises, or wrap them in one. This way, you can write seamless promise-based code. For instance, convert bcrypt.compare() into:

const bcryptCompare = (pw1, pw2) => {
  return new Promise((resolve, reject) => {
    bcrypt.compare(pw1, pw2, (error, result) => {
      if(error) reject(error)
      else resolve(result)
    })
  })
}

There are libraries that do this for you, and newer versions of libraries usually support promise-based versions of their APIs. But if you ever need to craft this yourself, this is how it's generally done.

Next, I would separate express from this logic. This way, you're not constrained with Express. Instead of accessing request, pass only the needed information to your function. Instead of immediately sending a response, consider throwing error objects instead. This way, you can defer your response logic to your routing logic.

Next, you could turn to async/await for cleaner code. It's just syntax sugar on top of Promises, allowing asynchronous code to look synchronous.

// Custom error class to house additional info. Also used for determining the
// correct response later.
class PasswordMismatch extends Error {
  constructor(message, user){
    this.message = 'Neue Passwörter stimmen nicht überein!'
    this.user = user
  }
}

class UserDoesNotExist extends Error {...}
class PasswordIncorrect extends Error {...}

// More custom error classes here

// Our logic, which knows nothing about express and only requires a few
// pieces of information for our operation.
const changePassword = async (username, oldPw, newPw, newPw2) => {

  // Throwing an error inside an async function rejects the promise it returns
  if (newPw !== newPw2) {
    throw new PasswordMismatch('Neue Passwörter stimmen nicht überein!', user)
  }

  const results = await SQL.getUserFromDB(username)

  if (!results) {
    throw new UserDoesNotExist('...', user)
  }

  const result = bcryptCompare(oldPw, results[0].password)

  if(result !== true) {
    throw new PasswordIncorrect('...', user)
  }

  try {
    SQL.changeUserPassword(username, newPw)
  } catch(e) {
    throw new PasswordChangeFailed('...', user)
  }
}

// In your express router
app.post('route', async (request, response) => {
  // Gather the necessary data from the request
  const username = request.session.username
  const oldPw = request.body.oldPw
  const newPw = request.body.newPw
  const newPw2 = request.body.newPw2

  try {
    await changePassword(username, oldPw, newPw, newPw2)

    // If the operation didn't throw, then we're good. Happy path.
    response.render('pages/changePassword', {
      user: username,
      text: 'Passwort erfolgreich geändert.'
    });
  } catch (e) {
    // If any of the operations failed, then we determine the right response

    // In this case, we used a custom error. We can get the data from it.
    // You can have more of these for the other custom classes.
    if(error instanceof PasswordConfirmError){
      response.render('pages/changePassword', {
        user: error.user,
        text: error.message
      });

    } else {
      // Generic 500 error, if all else fails.
    }
  }
})


Inserting data into SQL-Server table dynamically

The main problem with this code is that it's not screening column values, unfortuantely buit in mechanisms in Invoke-SQLCMD aren't viable. It allows to use -Variable but it also accepst stings, like -Variable "p0='var_value'" which don't help at all with escaping, even making it worse.

but here are opesource alternative to Invoke-SQLCMD: https://github.com/RamblingCookieMonster/PowerShell/blob/master/Invoke-Sqlcmd2.ps1

It can be installed by executing

Install-Module WFTools -Force -AllowClobber

Example of unescaped query with Invoke-SQLCMD

> Invoke-Sqlcmd -Database tb-sql-db -Server XXXXX -Username YYYY -Password ZZZZ -Query "SELECT id, um.text FROM archive_srv_db.tbUniMessage um WHERE id = '3161e665-a30e-48c4-87f2-0008d62da8a6'"

id                                   text
--                                   ----
3161e665-a30e-48c4-87f2-0008d62da8a6 Hello!

Same query using parameters and Invoke-Sqlcmd2:

$secpasswd = ConvertTo-SecureString "ZZZZ" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("YYYYY", $secpasswd)
Invoke-Sqlcmd2 -Database tb-sql-db -Server XXXXX -Credential $mycreds -Query "SELECT id, um.text FROM archive_srv_db.tbUniMessage um WHERE id = @p0" -SqlParameters @{ p0="3161e665-a30e-48c4-87f2-0008d62da8a6"}

id                                   text
--                                   ----
3161e665-a30e-48c4-87f2-0008d62da8a6 Hello!

If you are concerned with escaping issues, if any had occur, you should migrate to Invoke-Sqlcmd2.
But please take a note, that parametrized query in ADO.NET can hold up to roughly 2000 parameters.
So your loop condition should be  ($i * $columns.Length) -gt 1900  (just assumption that you won't have more than 100 columns. It's to late here to write exact code :-).

And in your loop you should append to query @p$counter, where counter is incrementing variable defined outside of a loop and incremetring inside loop on each value. and build dictionary, populating it with named parameters.

Example:

#define dictionary before outer loop
$dict = @{}
....

#add value inside inner loop
$Insert += "?,"
$dict.Add("@p$counter",$data.rows[$x].$item)


Hide hashmap implementation
The code can be improved by removing the requirement for the user to operate on a hashmap or to even know that a hashmap is used.
How? This should be hidden from the user by an "interface".
Why?
This is a good coding practice as it would allow the underlying structure/code to change (for example, the hashmap could be swapped for a db) but there would be no API changes to the interface.
I have implemented this change and the changes suggested by @Shepmaster here: github


Stopping all Runnable tasks when one is terminated

Your general approach is sound

When your Callable throws, the throw will be propagated out of the Future, you catch it and cancel all remaining futures. I believe it is safe to cancel a completed job so you might not need the !isdone() check. But that's nitpicking.
Then you shutdown your thread pool to clean up.

Your implementation is a bit messy

The loop where you iterate over all the futures seems a bit messy to me.

For example:

for (Future ft : futureList) {
    if (!futureList.isEmpty()) {

this condition will always be true. And you are never removing from the list (if you were, you would get a ConcurrentModificationException. So the check is wholly redundant.

So you loop over all of the futures in futureList and call take() on your completionService. This doesn't make sense to me, if you're going to call take() use a loop that describes what you want to do better. This is further strengthened by the fact that you never use the ft loop variable.

Maybe something like this:

while(futureList.size() > 0) {
    Future ft = completionService.take();
    futureList.remove(ft);

    try{
        String task = ft.get();
    }catch(InterruptedException e){
        // Interrupted
    }catch(ExecutionException e){
        // Snap, something went wrong in the task! Abort! Abort! Abort!
        for(Future f : futureList){
            f.cancel(true);
        }
        futureList.clear();
    }
}
pool.shutdownNow();


Worksheet functions like CountIf are pretty well optimized, so that's a good way to find out if TXTBUSCAART.Text is in column A of your dataSheet. but so is Find.

By using Find, you could eliminate the initial step (CountIf) to determine if your search string is in the destination and you eliminate the InStr call to recheck if it's in each individual row of your data set. Unfortunately, you really need to know how many times your .Text appears to make the most efficient use of ReDim dataOut, so we'll leave that in there.

This is what I came up with for your loop using Find:

Private Sub LStart_Change()

  Dim rowCount As Long, itemCount As Long, counter As Long
  Dim dataSheet As Worksheet
  Dim dataOut() As Variant

  LStart.Clear
  LStart.ColumnCount = 9

  Set dataSheet = Sheets("sheet1")
  With dataSheet
    rowCount = .Cells(.Rows.Count, "A").End(xlUp).Row
    Dim searchString As String
    searchString = "abcde"
    itemCount = Application.WorksheetFunction.CountIf(.Range("A2:A" & rowCount), "*" & searchString & "*")
    If itemCount > 0 Then
      Dim searchRange As Range
      Set searchRange = .Range(.Cells(2, 1), .Cells(rowCount, 1))
      ReDim dataOut(1 To itemCount, 1 To 9)
      Dim foundCell As Range
      Set foundCell = searchRange.Find(What:=searchString, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
      counter = 1
      Dim firstCell As Range
      Do Until foundCell Is Nothing
        If firstCell Is Nothing Then
          Set firstCell = foundCell
        End If
        With foundCell
          dataOut(counter, 1) = .Cells(1, 1)
          dataOut(counter, 2) = .Cells(1, 3)
          dataOut(counter, 3) = .Cells(1, 2)
          dataOut(counter, 4) = .Cells(1, 4)
          dataOut(counter, 5) = .Cells(1, 6)
          dataOut(counter, 6) = .Cells(1, 5)
          dataOut(counter, 7) = .Cells(1, 8)
          dataOut(counter, 8) = .Cells(1, 9)
          dataOut(counter, 9) = .Cells(1, 7)
        End With
        Set foundCell = searchRange.FindNext(foundCell)
        If foundCell.Address = firstCell.Address Then
          Set foundCell = Nothing
        Else
          counter = counter + 1
        End If
      Loop
      LStart.List = dataOut
    End If
  End With

End Sub

It compiles cleanly, but you may have to fiddle with the .Find() parameters a little bit since I don't have your exact data set to work from.

Using .Find() takes you directly to each row that has your TXTBUSCAART.Text in it so you may save 100s of If InStr() comparisons in a row looking at rows that don't contain your required text. I've set MatchCase:=False to eliminate the UCase() call on each comparison since it didn't seem to matter (you don't UCase()the text in your initial CountIf(), so I didn't think you really cared - adjust as necessary).

I set the variable searchString to TXTBUSCAART.Text one time because there's no need to reference the form every time through the loop. This may well save you milliseconds in total execution time! :)

When using .Find() it will continue to loop through the search range forever, so you have to store off the first cell it finds then compare each newly found cell to that one to stop the loop when you've returned to the beginning.

One note on .Find(): it will use any parameters set in the Find dialog box that you haven't explicitly overwritten, so make sure you're including all the parameters you need. Also, any parameters you set in code will appear the next time you open the Find dialog box, so if you have a favorite group of settings, you may want to store them off before fiddling with them in your code, then reset them when you're done.


Fetching all nodes of a directed graph by an id of any node

I am not well qualified to answer this, as I know nothing of PostgreSQL or the RECURSIVE extension you are using, so unfortunately I cannot very well comment on your solution.

However perhaps it can be tackled with (relatively!) standard SQL. Let's define a temporary table

CREATE TABLE #Temp ( id int, status int )

A status of 1 will indicate the node is reachable, but it has not yet been processed, a status of 2 indicates the node is reachable, and all immediate descendants have been added. We initialise by inserting the starting node ( I think in your example it is 2858 ).

INSERT INTO #temp ( id, status ) VALUES ( 2858, 1 )

Now we can set up a loop to insert all the reachable nodes:

DECLARE @x int
WHILE 1 = 1
BEGIN
  SELECT @x = -1 -- Some value that is not a valid node id
  SELECT @x = id FROM #Temp WHERE Status = 1
  IF @x = -1 BREAK -- All reachable nodes processed.

  INSERT INTO #temp( id, status )
  SELECT descendant_id, 1
  FROM DocumentDependency WHERE predecessor_id = @x
  AND descendant_id NOT in ( SELECT id FROM #Temp )

  UPDATE #temp SET status = 2 WHERE id = @x
END

When the loop terminates, #Temp should contain all the reachable node ids. Provided suitable indexes are defined, I think this should be reasonably efficient. As a variation, you could perhaps process multiple nodes with status 1 on each iteration of the loop, I will leave that as an exercise ( you may need an extra status value to differentiate between nodes that have just been added and nodes that are being processed ).


To answer your first question, I would create the method as a static method, because similar to sorting, detecting loops is basic functionality, that is applicable to Lists in general, not only a specific List object.

I would not recommend inheriting from the original List implementation in order to create the merge sort functionality. If you do so you will end up with List objects that can be sorted using merge sort and List objects, that cannot. But there is not really a difference between the two in structure, the only difference is that one is you inherited version and one is the LinkedList implementation. This complicates things unnecessarily. Again I would create a static method to merge sort any List, which would add the ability to merge sort any LinkedList, any ArrayList and any other custom List implementation, that implements the Java List interface.


How to improve WCF client proxy based on CreateChannel (not generated from WSDL) in C#

Instead of wrapping the proxy creation in a concrete service proxy implementation, I'd write a concrete service factory instead which in turn uses the ChannelFactory. The service factory class could also use an interface for creation, so you can have multiple concrete factories.

http://en.wikipedia.org/wiki/Factory_pattern

That way your code can continue to depend on the interface only, and you can change the way the proxy is created without writing concrete proxies. You could also have a "local service factory" which doesn't go over the wire.

Usage would be something like this instead:

var service = ServiceFactory.Create("http://127.0.0.1/svc");
var result = service.DoSomething(request);
// if you need cleanup..
ServiceFactory.Release(service);


RAII object for releasing pointer with arbitrary function

unique_ptr has capability for custom deleters.

Besides that assuming you are doing this to deal with COM objects then you can use ComPtr and its GetAddressOf

If you define a destructor you should also define (or disallow in this case) the copy construct and copy assign. Failure to do so will result in dangling pointers and double frees:

Releaser<IMFAttributes> attributes;
auto status = MFCreateAttributes(attributes.fill(), 1);
{
    Releaser<IMFAttributes> att = attributes;
} // att gets cleaned up

//attributes contains a dangling pointer


I don't like the idea of having to solve Diophantine equations just to be able to understand the code.  I'll suggest the same solution here as I did for @cody.codes:

public static boolean isSequential(int a, int b, int c) {
    int min = Math.min(a, Math.min(b, c));
    int max = Math.max(a, Math.max(b, c));
    return max - min == 2 && a != b && a != c && b != c;
}

"The minimum and maximum differ by 2, and all three numbers are unique" is easier to understand.


Haskell: Efficient and clear list concatenation

GHCi is frequently significantly slower than compiled code even if you compile without any optimizations turned on, consider the timing information that GHCi gives you with great suspicion. Compile your code with -O2 and then time and the difference should largely disappear.

One of the nice things about the Haskell Prelude is that there is very little magic going on behind the scenes. Haskell lists use some privileged constructor names ((:) and []) but besides that they are a data type like any other. So let's peek behind the curtain and take a look at how (++) is defined.

(++) :: [a] -> [a] -> [a]
(++) []     ys = ys
(++) (x:xs) ys = x : xs ++ ys

You should recognize the similarity to your function f. There is no way around the fact that growing a list by n elements takes n cons operations. When you want to add one element to the front of a list, you use (:) and that's it, you're done. When you want to add one element to the back of a list there's no constructor for that, you have to consider that element to be a new list of length one, and prepend your original list in front of it, causing you to have to walk down every single element of the original list. That is “slower” than the other case, but if the ordering of elements in your list is important you gotta do what you've gotta do, right?

Take some time to familiarize yourself with the functions available in Prelude and Data.List. There are many easier ways to write explodeBy.

As always, consider the types and use Hoogle. Looking at take n . repeat $ x think about the type you would give that function on its own. n is an Int, and x is any type a. So you'd have a type like func :: Int -> a -> [a]. The first hit is for replicate, which does exactly what you want.

Next consider what easy intermediate values you can construct that get you closer to the result you want. The first step would be to replicate each element in the original list n times. That sounds like a job for map.

map (replicate n) xs :: [[a]]

Now we need :: [[a]] -> [a], which we could also search for. But this is of course list concatenation. There's one more trick you might not find by searching though, so here is when it comes in handy to have read through the documentation for Prelude. concatMap handles mapping and list concatenation in one go.

explodeBy :: Int -> [a] -> [a]
explodeBy n = concatMap (replicate n)


I find it confusing that you first assign an empty string and then append to it. I suggest returning directly, like

// var words:String = "" Delete this

if number >= 0 && number <= 19 {

    return unitNames[number]

And a return for each branch. When I see a return I can rest my mind that the function has ended, if I see a string appending, maybe it will grow more before being returned.

The line:

 let end = NSDate();

Inside Euler17 should be deleted as it is useless.

Euler17 can be simplified a lot.

return (1...1000).map{count(convertIntegerToWords($0)}.sum

Use functional programming where it is worth it.

 var digits = number.array

Should be at the start of the function as you repeat it in almost every branch.

About @jimbo 's comment:

  For speed, I'd put the >= 100 && number <= 999 as the first > if, because 899 times out of 999, that'll be the one that's hit, > > and you don't want that being the last one.

Do not do it. Readibility will decrease and the performance impact will likely be minimal (benchmark needed).


Maybe You could do better if you:

Define an interface with complete JavaDoc.
Make s1 and s2 final
Write a JUnit-Test with constant testdata instead of reading input from keyboard. Do not use System.out.println in JUnit.


As I read it, you want:

one <div class="names"> per todo (in this case inside .contents) surrounding the todo text in a span and the buttons "Edit" / "Remove"
on clicking "Edit", the span is exchanged for an input field and an "Update" button, "Edit" and "Remove" are removed.
on clicking "Update", the prior state with the new text is recreated (span, Edit, Remove)

After you follow Inkbug`s advice from his comment, you should probably do this:

cache the todo root element (var todoRoot = $(...);)
rename addUpdateField/.update to edit/.edit and updateBtn to update - consistent with the button texts and easier to read
change the api of your todo.js functions. For update / edit / remove, just use the dom node of the div as the argument:

function(target, action) {
    todoRoot.on('click', target, function(e) {
        e.preventDefault();
        e.stopPropagation();
        action.call(todo, $(this).parents('.names').first());
    });
}

bindClick('.remove', todo.remove);
bindClick('.edit',   todo.edit);
bindClick('div',     todo.update);

don't use that many appends. Put it all in a string (that can contain more than one DOM node!)
don't create / delete all the nodes. Create all three buttons, use .toggleClass(...) and CSS to make all buttons/fields disappear that you don't need. That moves all your HTML into the add function and goes easy on the DOM
move the bindings into the Todo function. You don't want to have to remember to bind these events for every Todo list you create


I can't say whether yours is right or wrong but there were a couple of things that seemed odd to me.

I wouldn't add functions to variables unless you intend to use them more than once. (or it adds to the readability)

var startAjaxing = function() {
    myajaxer = getAjaxer($("#myid"));
    myajaxer();
}

$(startAjaxing);

then becomes:

$(function() {
    getAjaxer($("#myid"))();
}); // Start ajaxing

Also I would get rid of arguments.callee and get the element every time:

var getAjaxer = null;
getAjaxer = function(elementid) {
    return function() {
        $.ajax({
            url: "/path/to/resource",
            success: function(response) {
                $("#" + elementid).html(response);
            },
            dataType: "html",
            timeout: 10000,
            type: "POST",
            complete: getAjaxer(elementid);
        });
    };
}

To ME this seems easier to read butt it may be only a matter of taste. (PS. I didn't check this for syntax errors etc.)

If you don't want to create functions every time?

getAjaxer = function(elementid) {
    var elementAjaxer = $("#" + elementid).data("ajaxer");
    if(elementAjaxer == null){
        elementAjaxer = function() {
            $.ajax({
            // etc etc
            });
        };
        $("#" + elementid).data("ajaxer", elementAjaxer);
    }
    return elementAjaxer;
}


Assertions about data in infrastructure code as unit tests

This is an interesting code.

Criticism:

def testConfigStackYaml(self):

Can we rename these functions to snake_case such as test_config_stack_yaml maybe.

assert not bad_list, "Expected config/<stack_name>.yaml files to exist: %s" % bad_list

You can access unit test asserts from `TestCas
How:

self.assertTrue(expr, msg=None)
self.assertFalse(expr, msg=None)

More information at: https://docs.python.org/3.6/library/unittest.html#unittest.TestCase.assertTrue

expected_file = "%s/config/%s.yaml" % (stack_name, stack_name)

I personally like using the new formatter. % is more unreadable compared to
"{stack_name}/config/{stack_name}".format(stack_name=stack_name)
We can also use os.path.join to join sections of a path. This makes our intention clear.

if __name__ == "__main__":
    unittest.main()

If you add this to the end of the file you can run these test files individually as a simple script.

Creating a custom assert

I recommend creating a memeber function named assert_config_exists.
Parameters: path_format, message_format

def assert_config_exists(self, path_format: str, message_format: str):
    bad_list = []
    for stack in glob('*/config'):

        stack_name = os.path.dirname(stack)
        expected_file = path_format.format(stack_name=stack_name)

        if not os.path.isfile(expected_file):
            bad_list.append(expected_file)

    self.assertFalse(bad_list, message_format.format(bad_list=bad_list))
```


Determining average of integers using quicksort

Just a few comments:

You can't really hope that your quicksort will be faster than what Java provides. I'm not saying, it can't be done, but it'd take an excellent programmer a few days at best.

So let's forget this part... and now to your algorithm. After some reformatting, I might understand it:

int result = 0;

for (int k = 0; k < n; k++) {
    int desired = 2 * a[k];
    int l = 0;
    int r = n-1;
    while (l < r) {
        int diff = a[l] + a[r] - desired;
        // made symmetric
        if (diff < 0) {
            l++;
        } else if (diff > 0) {
            r--;
        } else {
            result++;
            break;
        }
    }
}

You really seem to be doing what you stated. The total time is quadratic, so let's forget the sorting once again.

Some better variable naming would surely do no harm.

I'll come back when I get an idea concerning the speed.


Java - Object declaration out of while loop

At the bytecode level, there is no such thing as inner block. All local variables are declared at the scope of the method. As a result, both your variants should produce equivalent bytecode. To make sure, decompile them with javap and look at the bytecode. From the programmer's point of view, version 1 is more clear and concise.


I believe a simple split on newline and a hard-coded parse would be better than the regex solution.... especially if you can trust your input data will be valid. In essence, the grouping and iterative looping in the regex is probably the slow part.

Of course, the console.info for each loop is also very slow.... and probably is something you added afterwards? If it is in your actual code, that's almost certainly the problem.

Let me explain the hard-parse with a code example:

var objParsed = {};
var lastKey;
var lines = str.split('\n');
for(var i = 0; i < lines.length; i++) {
    var clause = lines[i].trim();
    if (clause.length == 0) {
        continue;
    }
    if (clause[0] == '[') {
        lastKey = {};
        objParsed[clause.substring(1, clause.length - 1)] = lastKey;
    } else {
        var pos = clause.indexOf("=");
        lastKey[clause.substring(0, pos)] = clause.substring(pos + 1);
    }
}

See the comparative results in this jsfiddle


Review

For what it does that is very dense code. Consider reducing variable name length.

Major points

Direct element reference reduces code size, improves performance, and forces you to ensure element IDs and names are unique.
Adding content via innerHTML, clobbers events, forces reflows, and chews power.
Incorrect currency handling. Money comes in cents, not fractions of a cent. Always use integer math for calculations that involve cash exchanges.

Minor points

Avoid adding event listeners directly to the element's on property. Use addEventListener
There are some cases where innerHTML can be useful, most of the time it is the worst way to add to the DON. Don't use it!
Add text content via element.textContent.
Don't change element style properties directly, create CSS rules and set the appropriate class name.
Use const for constants.
Don't add comments that point out the obvious.
Use arrow functions for anon functions;

Direct element reference

All elements that have an Id property set can be accessed by that id directly in the global namespace.

Foo


Change filename of Excel-files based on the content of a cell

The good practice is to finish your code with

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

which has the advantage of returning an error code to the system (for example for batch processing of related programs).

It means 3 things for changing your code:

import sys
moving your command rename_excel_files(r"C:\Users\Stewie\Documents\excel_folder") to the new created main() function.
adding an return statement to the main() function with value 0 in the case of the success (and optionally something else for the failure):

So the result will be something as

import os
import sys
import xlrd
    ...
    ...
    ...

def main():
    rename_excel_files(r"C:\Users\Stewie\Documents\excel_folder")
    return 0

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

(See "3.3.1 Use sys.exit in your script to return proper error codes" in the book Writing Idiomatic Python by Jeff Knupp, 2013 - or directly from our BDFL - in All Things Pythonic - Python main() functions by Guido van van Rossum
May 15, 2003)


1. Review

There are no docstrings. What does process_file do? What does it return?
The name process_file is vague. It's better to use specific names, for example kmz_coordinates.
The saved_file logic should be unnecessary, because the caller already knows which filename they passed to process_file. So you could write something like:

results = executor.map(kmz_coordinates, kmz_files, chunksize=10)
for filename, coords in zip(kmz_files, results):
    print("{} / {}".format(count,len(kmz_files)))
    count = count + 1
    f.write(f"File {filename}:\n")
    for line in coords:
        f.write(line);

using zip to match up the filenames with the results, and remove the saved_file logic from kmz_coordinates.
Similarly, the count logic is unnecessary, because you could use enumerate to generate the counts:

results = executor.map(kmz_coordinates, kmz_files, chunksize=10)
for count, (filename, coords) in enumerate(zip(kmz_files, results)):
    print("{} / {}".format(count, len(kmz_files)))
    f.write(f"File {filename}:\n")
    f.writelines(coords)

The use of try: ... except: pass to suppress exceptions is a bad idea. There are two reasons for this. First, a bare except: catches all exceptions, including KeyboardInterrupt, which might make it difficult to stop the program by typing control-C. Second, the exceptions might be due to bugs in your program, but by suppressing them you make the bugs difficult to spot.

If you really do have a good reason for suppressing exceptions, then you should localize the try: ... except as tightly as you can around the line of code that might raise the exception, and you should pick the appropriate exception classs. For example, if the problem you are worrying about is that lxml.html.fromstring will raise lxml.etree.ParserError, write something like:

try:
    doc = html.fromstring(kml)
except lxml.etree.ParserError:
    continue # skip this file

When opening a file, it is a good idea to use with. For example, instead of:

f=open('kmz_search_output.txt', 'wt')
# ... code using f ...
f.close()

write:

with open('kmz_search_output.txt', 'w') as f:
    # ... code using f ...

This saves a line of code (no need to close the file explicitly), and ensures that the file gets closed in a timely manner even if an exception occurs inside the ... code using f ....
The content of a KML file XML, not HTML, so shouldn't you be using lxml.etree instead of lxml.html?
The code tries to parse every file in the KMZ as if it is a KML file:

for kml_name in kmz.namelist():
    if 'doc.kml' in kml_name:
        continue
    kml = kmz.open(kml_name, 'r').read()
    doc = html.fromstring(kml)

But a KMZ can include other kinds of data, for example images and audio, so I would expect some check on the filename, for example to see if has the .kml extension:

for kml_name in kmz.namelist():
    if not kml_name.endswith('.kml') or 'doc.kml' in kml_name:
        continue

If you did this then maybe you could omit the try: ... except: completely.
The name tmp is vague. Better to be as specific as possible, for example track.
This section of code loops over the first track in each placemark:

for pm in doc.cssselect('Document Placemark'):
    tmp = pm.cssselect('track')
    if len(tmp):
        # Track Placemark
        tmp = tmp[0]  # always one element by definition

But if there is only one track in each placemark, then it looks to me as though this can be as a single loop:

for track in doc.cssselect('Document Placemark track'):

Similarly, the code then loops through the descendents of the track looking for coord elements. So you could combine that into the same loop:

for coord in doc.cssselect('Document Placemark track coord'):

Instead of splitting the content of the coord element twice:

lon = float(content.split()[0])
lat = float(content.split()[1])

Split it just once and use map and tuple assignment:

lon, lat = map(float, content.split())

The search coordinates are always the same, so they should be assigned outside the loop. It would make sense for them to be keyword arguments to the kml_track_coordinates, then you could use the function to search for coordinates of your choice.
Similarly, the tolerance of 1 degree is arbitrary and should also be a keyword argument to the function.

2. Revised code

This is untested, so probably contains some mistakes.

def kmz_coordinates(filename, search_lon=-47, search_lat=47, tolerance=1):
    """Return list of track coordinates found in a KMZ file that are
    within tolerance degrees of (search_lon, search_lat).

    """
    coords = []
    with ZipFile(filename, 'r') as kmz:
        for kml_name in kmz.namelist():
            if not kml_name.endswith('.kml') or 'doc.kml' in kml_name:
                continue
            with kmz.open(kml_name, 'r') as kml:
                doc = html.fromstring(kml.read())
            for coord in doc.cssselect('Document Placemark track coord'):
                content = coord.text_content()
                lon, lat = map(float, content.split())
                if (abs(lat - search_lat) <= tolerance
                    and abs(lon - search_lon)) <= tolerance):
                    coords.append(content + '\n')
    return coords

def kmz_search_directory(directory, output_filename='kmz_search_output.txt'):
    """Search directory for KMZ files, find matching coordinates within
    the files, and write the results to output_filename.

    """
    kmz_files = []
    for root, _, files in os.walk(directory):
        for filename in files:
            if filename.endswith('.kmz'):
                kmz_files.append(os.path.join(root, filename))

    with open(output_filename, 'w') as f:
        with concurrent.futures.ProcessPoolExecutor(max_workers=8) as executor:
            results = executor.map(kmz_coordinates, kmz_files, chunksize=10)
            for count, (filename, coords) in enumerate(zip(kmz_files, results)):
                print("{} / {}".format(count, len(kmz_files)))
                f.writelines(coords)

    print('Done')


Python Reverse the binary equivalent of input and output the integer equivalent of the reverse binary

x  = 0

There is no point in doing this. You just replace it anyways

while True:

    try:
        x = int(raw_input('input a decimal number \t'))

        if x in xrange(1,1000000001):

Probably better to use if 1 <= x <= 100000000001: although I'm really not sure why you are doing this check. Also, you should probably explain to the user that you've reject the number.

            y = bin(x)

            rev = y[2:]

I'd use reversed = bin(x)[:1::-1] rather then splitting it out across the tree lines.

        print("the reverse binary soln for the above is %d") %(int('0b'+rev[::-1],2))

I'd convert the number before the print to seperate output from the actual math.

            break

    except ValueError:
        print("Please input an integer, that's not an integer")
        continue

This continue does nothing.


Find duplicates in List where all the numbers in between is <= than the duplicates

Firstly, note that you should use .equals not == to compare two Integer values. Also, setting j to equal arr.size() is an odd way of doing a break.

However the main problem with your algorithm is that it has worst case performance O(n^2). If you run it on a descending List like [1000000, 999999, 999998, ..., 0], it is very slow because j is never set to be arr.size().

However, for random Lists (i.e. Lists where every item is chosen using random.nextInt(1000000) for example), it is in general very quick. This is because j generally doesn't have to get very far before we break out of the j loop, so it essentially has O(n) performance for random Lists.

The fact that the performance can vary so drastically depending on the nature of the List makes it very difficult to compare two algorithms. However, for many cases the following algorithm is quicker. It only traverses the List once, keeping track of the occurrences of the numbers found. Because it uses a TreeMap I think the worst case performance is O(n log n).

private static int countPairs(List<Integer> list) {
    int total = 0;
    NavigableMap<Integer, Integer> map = new TreeMap<>();
    for (int a : list) {
        map.headMap(a).clear();
        Integer m = map.get(a);
        if (m == null)
            map.put(a, 1);
        else {
            map.put(a, m + 1);
            total += m;
        }
    }
    return total;
}


You should be able to await the repo.SaveChangesAsync() method.

public async Task AddTemplateAsync()
{
    var template = new Template {...};

    using (var repo = new Repository())
    {
        try
        {
            IsBusy = true;
            repo.Templates.Add(template);
            await repo.SaveChangesAsync();
        }
        catch {...}
        finally
        {
            IsBusy = false;
            TryClose();
        }
    }
}

Since AddTemplate() is an async method, it should be called AddTemplateAsync() and return a Task. Take a look at these best practices for more details. You might want to consider passing in a CancellationToken depending on your requirements.

I would also advise against swallowing the base Exception type for reasons explained here.


Testing a probabilistic prime-testing algorithm that may fail

Your test is missing the central point of unit testing which is to prove the code under test works in the specific circumstances in question, these should be deterministic.  So for each parameter you can know in advance what the result should be.  Create multiple unit tests for the known factors that should work and a number for the parameters expected to fail.

Secondly, I suggest you create your own Checked Exception for the failing circumstance rather than reusing the runtime UnsupportedOperationException, this will ensure a client using your function/library is forced to handled the exception.  When the error can be handled use a checked exception, only use the unchecked runtime exceptions when the error cannot be handled by the code.  If the program can, it should still fail gracefully with either.  I can see that is what you are trying to achieve for your live code but visibility of the issue is more important in unit tests.

The client code handling exception could skip the value and proceed to the next value.  Simply retrying with the same value should produce the same result, so the retry with the same value is actually pointless.

You can verify that the test produces the result you expect by catching the expected failure with the expected Exception clause of the @Test annotation.

@Test(expected = BirthdayParadoxException.class)
public void testBirthdayParadox() {
    op.primeFactorsPollardRho(BIRTHDAY_PARADOX_VALUE);
}

@Test
public void testPassingValue() {
    assertEquals(EXPECTED_RESULT, op.primeFactorsPollardRho(FACTORABLE_VALUE);
}

Create multiple tests for each specific situation you can predict and verify the expected behaviour, catching failures should be considered expected behaviour for unit testing purposes.

Also see : https://github.com/junit-team/junit4/wiki/Exception-testing


Script to load the ISS location and data

Not bad for a first Python script!

In general, you can greatly benefit from giving parts of your code names, by encapsulating them in functions. This makes it also re-usable and let's you add a docstring giving more detail on what this function does.

If you add a main function which you call under a if __name__ == "__main__": guard you can also import these functions from another script without all of your code running.

Instead of urllib.request, you can use the requests module, it even has a method to directly return a JSON object.

While it is nice that the astronauts response contains the number of astronauts in space, you could get the same information from calling len(people), which is \$\mathcal{O}(1)\$ for Python lists.

Familiarize yourself with f-strings (Python 3.6+). They are a nice and compact way to write formatted strings, since they can contain arbitrary Python expressions (I used it for the address here).

import requests
import reverse_geocoder

def get_json(url):
    """Retrieve JSON object from url.

    Raises an exception if connection fails.
    """
    response = requests.get(url)
    response.raise_for_status()  # make sure an exception is raised if something goes wrong
    return response.json()

def get_astronauts():
    """Returns a list of all people currently in space"""
    url = "http://api.open-notify.org/astros.json"
    return get_json(url)['people']

def get_iss_location():
    """Returns the current latitude and longitude of the ISS"""
    url = "http://api.open-notify.org/iss-now.json"
    position = get_json(url)['iss_position']
    return position['latitude'], position['longitude']

def get_address(position):
    """Do a reverse lookup getting the closest address to a given position"""
    return reverse_geocoder.search(position)[0]

def main():
    astronauts_in_space = get_astronauts()
    print("People in space : ", len(astronauts_in_space))
    print("----NAMES----")
    for astronaut in astronauts_in_space:
        print(astronaut['name'])

    iss_position = get_iss_location()
    print("Latitude: {}, Longitude: {}".format(*position))

    address = get_address(iss_position)
    print("----Address----")
    print(f"{address['name']}, {address['admin1']} {address['admin2']} {address['cc']}")

if __name__ == "__main__":
    main()


So the logic I used was to check if a cell is part of an existing island either to above (n) or to the left (w). I came up with three possibilities:

Neither n nor w is an island so start a new island, record the islands number and increment the count.
Only one of the adjoining cells is an island or both are the same island so just add it to the same island.
The adjoining cells are from different islands, merge the islands by decrementing the count.

Something like this:

let numIslands = function(grid) {
  logGrid('input', grid);

  let count = 0;
  let work = [];
  let islandNo = 0;

  grid.forEach((row, rowi) => {
    work.push( (new Array(row.length)).fill(undefined) );
    row.forEach((value, coli) => {
      if (value !== '1')
       return;

      let n       = rowi > 0 ? work[rowi-1][coli] : undefined;
      let w       = work[rowi][coli-1];
      let type;

      if (n===undefined && w===undefined) {
        type  = 'new  ';
        islandNo++;
        work[rowi][coli] = islandNo;
        count++;
      } else if (n===w || n===undefined || w===undefined) {
        type = 'exist';
        work[rowi][coli] = n || w;
      } else /* n !== w */ {
        type  = 'merge';
        work[rowi][coli] = n || w;
        count--;
      }

      // console.log(type, rowi, coli, work[rowi][coli] );

    })
  })
  logGrid('work', work);
  return count
}

   let logGrid = function(title, grid) {
 console.log('------------');
 console.log(title);
 console.log('------------');
 console.log( grid.map(r=> r.map(r=>r||'.').join('')).join("\n"));
 console.log('------------');
   }

   let test = function(grid) {
 grid = grid.map (r => r.replace(/0/g,'.').split("") );
 console.log( numIslands(grid) );
 console.log();
   }

test(['111',
      '11.',
      '111',
      '.1.',
      '111'  ]);

test(['111',
      '...',
      '111',
      '...',
      '111' ]);

test(['0000000000',
      '0111001111',
      '0001001001',
      '0101001001',
      '0101111011',
      '0000000010',
      '0110011000' ] );

Note that I don't consider diagonally adjacent cells to be the same island and this algorithm probably doesn't handle all situations well, especially hollow islands.
Just a styistic note. Use either var or let, but don't mix the two.


Use LINQ expression compilation to generate mapping code at runtime. The concept is to generate a method that does obj.Property1 = dataReader["Property1"]; ... dynamically.

public class Converter<T> where T : new()
{
    private static ConcurrentDictionary<Type, object> _convertActionMap = new ConcurrentDictionary<Type, object>();
    private Action<IDataReader, T> _convertAction;

    private static Action<IDataReader, T> GetMapFunc()
    {
        var exps = new List<Expression>();
        var paramExp = Expression.Parameter(typeof(IDataReader), "dataReader");
        var targetExp = Expression.Parameter(typeof(T), "target");
        var getPropInfo = typeof(IDataRecord).GetProperty("Item", new[] { typeof(string) });
        foreach (var property in typeof(T).GetProperties())
        {
            var getPropExp = Expression.MakeIndex(paramExp, getPropInfo, new[] { Expression.Constant(property.Name, typeof(string)) });
            var castExp = Expression.TypeAs(getPropExp, property.PropertyType);
            //var bindExp = Expression.Bind(property, castExp);
            var bindExp = Expression.Assign(Expression.Property(targetExp, property), castExp);
            exps.Add(bindExp);
        }
        return Expression.Lambda<Action<IDataReader, T>>(Expression.Block(exps), new[] { paramExp, targetExp }).Compile();
    }

    public Converter()
    {
        _convertAction = (Action<IDataReader, T>)_convertActionMap.GetOrAdd(typeof(T), (t) => GetMapFunc());
    }

    public T CreateItemFromRow(IDataReader dataReader)
    {
        T result = new T();
        _convertAction(dataReader, result);
        return result;
    }
}

Test method with 80,000 x 100 iteration

    static void Main(string[] args)
{
    var dummyReader = new DummyDataReader();
    var properties = typeof(DummyObject).GetProperties();
    var startDate = DateTime.Now;
    var converter = new Converter<DummyObject>();
    for (int i = 0; i < 80000 * 100; i++)
    {
        //var obj = CreateItemFromRow2<DummyObject>(new DummyDataReader());
        var obj = CreateItemFromRow<DummyObject>(dummyReader, properties);
        //var obj = converter.CreateItemFromRow(dummyReader);
        dummyReader.DummyTail = i;
    }

    //var obj = CreateItemFromRow2<DummyObject>(new DummyDataReader());
    Console.WriteLine("Time used : " + (DateTime.Now - startDate).ToString());
    Console.ReadLine();
}

Result:

CreateItemFromRow : 18.5 seconds
Converter<T> : 7.3 seconds

Map function:

    private static IList<T> Map<T>(DbDataReader dr) where T : new()
    {
            // initialize our returnable list
            List<T> list = new List<T>();
            // fire up the lamda mapping
            var converter = new Converter<T>();

            while (dr.Read()) {
                // read in each row, and properly map it to our T object
                var obj = converter.CreateItemFromRow(dr);
                list.Add(obj);
            }

            // reutrn it
            return list;
     }


Instead of using a foreach here, you need to use a for-loop, since you have to know the index of the image to assign it to the correct texture.

// this line replaces the first for loop you have there
var textureLinks = tzPlayInfo.Instance.bcGameTables.Select(t => t.dlrimage).ToArray();
// verify we have enough uitex elements:
if (uitex.Length < textureLinks.Length)
{
    throw new RuntimeException("too many textures linked");
}
for (int i = 0; i < textureLinks.Length; i++)
{
    new BestHTTP.HTTPRequest(new System.Uri($"**.amazonaws.com/resources/dealer/pic/{textureLinks[i]}"),
        (req, res) => {
            var tex = new Texture2D(20, 20);
            tex.LoadImage(res.Data);
            uitex[i].GetComponent<UITexture>().mainTexture = tex;
        }).Send();
}

There's also some words to be lost on the naming convention you use.

dealer_img should be dealerImg, uitex should be uiTextures, bc_gametablelist should be bcGameTables, dlrimage should be dealerImage...

In general you seem to try to shorten your names and adhere to C++ conventions. That would be fine if you were programming the graphics in C++. But since this is C#, you should be following C# convention (namely camelCase)

In the code above I made use of interpolated strings for the Uri. I also left the variable types of the lambda up for inference by the compiler. It should be smart enough to do so, if BestHTTP properly specified the generics on HTTPRequest.


Finding maximum length of continuous string which has same characters

Yes, you can tackle this problem is a much faster way, with an O(n) algorithm, using a left and a right pointer delimiting the substrings to consider.

For each character in the input String, the idea is to keep an array counting the number of 'a' and 'b' between a given left pointer, that will represent the start of the substring, and the current character.

When the count of each 'a' or of each 'b' is less than the allowed number of characters to change, we can consider making the change and incrementing the answer. Then, when both of those 2 counts becomes greater than the allowed number of characters to change, it means we cannot change more characters so we hit a maximum length: the left pointer is increased, and the count of the character at that pointer is decreased.

The count of 'a' and 'b' characters can be modeled as an integer array (index 0 corresponds to the count of 'a' while index 1 corresponds to the count of 'b').

Scanner in = new Scanner(System.in);
int n = in.nextInt(), max = in.nextInt();
String s = in.next();

int left = 0, answer = 0;
int[] count = { 0, 0 };
for (char c : s.toCharArray()) {
    count[c - 'a']++;
    if (Math.min(count[0], count[1]) > max) {
        count[s.charAt(left) - 'a']--;
        left++;
    } else {
        answer++;
    }
}
System.out.println(answer);

This approach passes all the tests of the challenge.


I'm just commenting on your HTML/CSS.

I commented out some br tags, because they were not necessary
I splitted the HTML in three parts with div's
Slightly adjusted the CSS rules

<!DOCTYPE html>

        <meta charset="utf-8">
        <title>Aligner</title>

Aligner

        Editor:
        <textarea id="editor">
  DATA : wa_ekpo TYPE ekpo.
  DATA : wa_eket TYPE eket.
  DATA : wa_decision TYPE zrms_performance_decision.
  DATA : wa_receipt_item TYPE zrmt_rcpt_items.
  DATA : wa_container TYPE zrmt_rcpt_hdrs_c.
        </textarea>

        Align at
        <input type="text" id="token" class="emo" value="TYPE">
        <button type="button" id= "button">Align</button>

        Output:
        <textarea id="output"></textarea>


I'd pull out that huge if/else block into a switch block.

This will make your code more readable.  (Also slightly better performance.)  Right now it's difficult to see the logic because it is hidden behind code duplication.  See how a switch block makes it very easy to see the possible keycodes and their corresponding behaviors.

switch(e.getCode()) {
    case(KeyCode.ESCAPE) :  stage.close(); break;
    case(KeyCode.DELETE) :  view.setEffect(null); break;
    case(KeyCode.SPACE)  :  view.setEffect(new Reflection()); break;
    ...


Take a number and return English language representation

At first glance, you're trying to be a bit cute here and it's making your code more complex than necessary.  For example, why not handle 11 and 12 as part of the teens and why not just type out the teen and decade names in full rather than having extra logic to append a not-quite common suffix ("teen" and "ty")?

For n < 1000 you have the following rules (let <n> mean "n in words", % mean modulo, and // mean integer division):

if 100 <= n then <n // 100> " hundred" ++
                 (if n % 100 == 0 then "" else " and " ++ <n % 100>)
if 20 <= n then  {"", "twenty", ..., "ninety"}[n // 10] ++
                 (if n % 10 == 0 then "" else " " ++ <n % 10>)
otherwise        {"zero", "one", "two", ..., "ten", "eleven", ..., "nineteen"}[n]

Now you only need to deal with the orders of magnitude ("thousand", "million", etc.).


Encoding

I hope I understood the problem correctly :)

When observing the problem, I found you can replace the H and V with 0 and 1. The lexicographical permutations then become binary numbers. There are only 10 V's and 10 H's, so the final number will be below 2^20 so fits even in an int.

   HHHVV -> 00011
   VVHHH -> 11000
   etc.

Start out by setting the starting int to  (1 << Vs) -1 and use the next-bit permutation logic from here

Then, if you found the nth permutation, transform it back to a String

Time complexity

As calculation the next bit permutation is simply a constant time calculation, finding the nth permutation takes O(n) time.

Proposed solution

public class Permutations
{
    //treat H as 0, V as 1.
    private static String getLongAsHVString( long size, long l )
    {
        String s = Long.toBinaryString( l ).chars().mapToObj( Permutations::getStr ).collect( Collectors.joining() );

        //Prepend the 'missing' zeroes as 'H' s
        while ( s.length() < size )
        {
            s = "H" + s;
        }
        return s;
    }

    private static String getStr( int c )
    {
        return ( c == '0' ) ? "H" : "V";
    }

    private static long getNthLexoPerm( int hCount, int vCount, int n )
    {
        long current = ( 1 << vCount ) - 1;

        for ( int i = 0; i < n; i++ )
        {
            current = nextPerm( current );
        }
        return current;
    }

    private static String getNthLexoPermAsString( int hCount, int vCount, int n )
    {
        return getLongAsHVString( hCount + vCount, getNthLexoPerm( hCount, vCount, n ) );

    }

    public static long nextPerm( long v )
    {
        long t = ( v | ( v - 1 ) ) + 1;
        return t | ( ( ( ( t & -t ) / ( v & -v ) ) >> 1 ) - 1 );

    }

    public static void main( String[] args )
    {

        System.out.println( getNthLexoPermAsString( 2, 2, 3 ) );
        System.out.println( getNthLexoPermAsString( 4, 5, 4 ) );
    }

}


Google Foobar Level 1 - Minion Task Schedule

if len(data) > 99:
    exit('List contains more than 100 elements')

I'd change that error message to something like 'List contains a hundred or more elements'. I might also raise a ValueError instead of calling exit. I think that's more appropriate for an error that the user may want to catch.

You can get rid of your call to setdefault by using a defaultdict instead:
from collections import defaultdict

. . .

count = defaultdict(int)  # int is a function that returns 0 when no args are given to it
for i in data:
    count[i] += 1

The gain here isn't huge, but they're handy in many cases.
collections.Counter would probably be appropriate here as well, but I've only used it once, so I can't show a good example of it.


Some improvements:

random_weighted_classifier2 <- function(n = 1, weightA, weightB, weightC){
  x <- sample(1:100, n, replace = T) / 100
  i1 <- x <= weightA
  i2 <- x > weightA & x <= (weightA + weightB)
  rez <- rep('no_change', n)
  rez[i2] <- "worse"
  rez[i1] <- "better"
  rez
}

test <- function(){

  betters <- rep("better", 100)
  worses <- rep("worse", 50)
  no_changes <- rep("no_change", 10)
  reference <- sample(c(betters, worses, no_changes))

  better <- sum(reference == "better")
  worse <- sum(reference == "worse")
  no_change <- sum(reference == "no_change")
  total <- length(reference)

  iterations <- 1000
  f1_rwc <- pres_rwc <- rec_rwc <- matrix(0., iterations, 3)
  kappa_rwc <- vector("double", iterations)

  referenceF <- factor(reference, levels = c("better","worse", "no_change"))
  for (i in seq(1:iterations)) {

    prediction_rwc <- random_weighted_classifier2(total,
                                                  better/total,
                                                  worse/total,
                                                  no_change/total)
    prediction_rwc <-
      factor(prediction_rwc, levels = c("better","worse", "no_change"))
    conTable <- table(prediction_rwc, referenceF)
    cm <- confusionMatrix(conTable, positive = c("better", "worse"),
                          mode = "everything")

    f1_rwc[i, 1:3] <-  cm$byClass[,"F1"]
    pres_rwc[i, 1:3] <- cm$byClass[,"Precision"]
    rec_rwc[i, 1:3] <- cm$byClass[,"Recall"]
    kappa_rwc[[i]] <- round(cm$overall["Kappa"], 2)
  }

  print(list("f1" = colMeans(f1_rwc, na.rm = T),
             "precision" = colMeans(pres_rwc, na.rm = T),
             "recall" = colMeans(rec_rwc, na.rm = T),
             "kappa" = mean(kappa_rwc, na.rm = T)))
}

It should be around 50 % faster (16.80 vs 11.02 sec for 1k iterations).

You, possibly, could speed it up even more by removing the confusionMatrix function and calculation of all of the necessary parameters by yourself.

Update

Based on confusionMatrix I managed to extract relevant parts of code and wrap into a function:

statistics <- function(data, beta = 1) {
  stat <- sapply(rownames(data), function(relevant) {

    if (nrow(data) > 2) {
      m <- matrix(NA, 2, 2)
      colnames(m) <- rownames(m) <- c("rel", "irrel")
      irrelCol <- which(!(colnames(data) %in% relevant))
      relCol <- which(colnames(data) %in% relevant)
      m[1, 1] <- sum(data[relCol, relCol])
      m[1, 2] <- sum(data[relCol, irrelCol])
      m[2, 1] <- sum(data[irrelCol, relCol])
      m[2, 2] <- sum(data[irrelCol, irrelCol])
      m <- as.table(m)
      relevant <- "rel"
    }
    numer <- m[relevant, relevant]
    denom <- sum(m[relevant, ])
    prec <- ifelse(denom > 0, numer/denom, NA) # Precision

    denom <- sum(m[, relevant])
    rec <- ifelse(denom > 0, numer / denom, NA) # Recall

    F1 <- (1 + beta^2)*prec*rec/((beta^2 * prec) + rec) # F1
    c('Precision' = prec, 'Recall' = rec, 'F1' = F1)
  })

  k <- unlist(e1071::classAgreement(data))["kappa"]
  list(stat, kappa = k)
}

and then test3 looks like:

test3 <- function(iterations = 100){

  vals <- c("better","worse", "no_change")
  betters <- rep("better", 100)
  worses <- rep("worse", 50)
  no_changes <- rep("no_change", 10)
  reference <- sample(c(betters, worses, no_changes))

  better <- sum(reference == "better")
  worse <- sum(reference == "worse")
  no_change <- sum(reference == "no_change")
  n <- length(reference)

  f1_rwc <- pres_rwc <- rec_rwc <- matrix(0., iterations, 3)
  kappa_rwc <- vector("double", iterations)
  referenceF <- factor(reference, levels = vals)

  for (i in seq(1:iterations)) {

    prediction_rwc <-
      random_weighted_classifier2(n, better/n, worse/n, no_change/n)
    prediction_rwc <-  factor(prediction_rwc, levels = vals)
    conTable <- table(prediction_rwc, referenceF)
    cm2 <- statistics(conTable)
    f1_rwc[i, 1:3] <-  cm2[[1]][3, ]
    pres_rwc[i, 1:3] <- cm2[[1]][1, ]
    rec_rwc[i, 1:3] <- cm2[[1]][2, ]
    kappa_rwc[[i]] <- round(cm2[[2]], 2)
  }

  list("f1" = colMeans(f1_rwc, na.rm = T),
       "precision" = colMeans(pres_rwc, na.rm = T),
       "recall" = colMeans(rec_rwc, na.rm = T),
       "kappa" = mean(kappa_rwc, na.rm = T))
}

This should run under 1sec for 1k iterations.

p.s. kappa can be calculated with this:

  n <- sum(data)
  ni <- rowSums(data)
  nj <- colSums(data)
  p0 <- sum(diag(data, names = F))/n
  pc <- sum((ni/n) * (nj/n))
  k <- (p0 - pc)/(1 - pc)

p.s.s. when reducing to those formulas, I stripped the code which was used for testing, so if you change your data format, bugs may appear. I assumed that the setting doesn't change.


Are there easier ways to print something like a linebreak after each string rather than hardcoding the \n into the string itself?

Embedded newlines are certainly the easiest way to do this, but definitively not the most versatile way. Embedding the newlines kind of pushes you in the direction that  the strings will be used for outputting only. You might want to do lots of other stuff with them too.
I advocate keeping the strings pure and adding the linebreaks, or any other prefix or suffix for that matter, later on. Since you already have to count the characters in the string, you can at the same time copy the string to a buffer (every non-trivial program has some general purpose buffer at the ready). Once you read the zero terminator you store the newline in the buffer. Then you're ready to print the buffer contents for which you now know the length.
In below code PrepStringForOutput is a leaf function which means you can do pretty much anything you like in it. You don't have to follow any register conventions per se.
# Program, print out a list of strings, one per line
.data

SYS_EXIT   = 60
SYS_WRITE  = 1
SYS_STDOUT = 1

# Empty string means end of strings
Strings:    .asciz  "Once", "upon", "a", "time", "...", ""
Buffer:     .ascii  "12345"

.text
.globl _start

; IN (%rbx is asciz) OUT (%rsi is buffer, %rdx is length) MOD (%al)

PrepStringForOutput:
    mov     $Buffer, %rsi        # Destination
    xor     %edx, %edx           # Length
  .L1_loop:
    mov     (%rbx, %rdx), %al    # Character from asciz string
    test    %al, %al
    jz      .L1_exit
    mov     %al, (%rsi, %rdx)    # Store in buffer
    inc     %edx
    jmp     .L1_loop
  .L1_exit:
    mov     $10, (%rsi, %rdx)    # Adding newline
    inc     %edx
    ret

_start:

    mov     $Strings, %rbx
  PrintLoop:
    cmpb    $0, (%rbx)           # End of list ?
    je      Exit
    call    PrepStringForOutput  # -> %rsi is address, %rdx is length
    add     %rdx, %rbx           # Advancing in the list
    mov     $SYS_STDOUT, %edi
    mov     $SYS_WRITE, %eax
    syscall
    jmp     PrintLoop
  Exit:
    xor     %edi, %edi
    mov     $SYS_EXIT, %eax
    syscall

What places can I improve?

You'll easily spot these in above code...
See the nice tabular layout?


Is it overkill to suggest such a solution?

Yes, I think so :-)

  Under what conditions would you split it into a Model and Views?

If you need more than one view. For example, if the specification says that the program should be able to write the results to the screen, a CSV/PDF file, a network socket, a web service etc.

  Are there any issues with the code?

It's fine, just three issues:

Naming: I'd rename $data to $result.
Input checks:

What should happen when $start > $end? (Check and throw an exception.)
What should happen when $period is not a number? (Throw an exception in the constructor.)

Unit tests are missing.

  Whether any changes were likely to be made in the future?

I would not try predicting it. If there are some change requests refactor it. The tests will show whether there is a regression or not. On Programmers.SE there are same questions about it:

Design for future changes or solve the problem at hand
Future proofing code


I would remove the out parameter from the constructor. It is really confusing to read. Maybe create a function that throws an exception instead. If the database implements IDisposable you can use a using block instead.

Edit - You have some duplicate code as well:

Log.WriteERROR("Can not open database");
Application.Exit();
return;


function pack(bytes) {
    var str = "";
// You could make it faster by reading bytes.length once.
    for(var i = 0; i < bytes.length; i += 2) {
// If you're using signed bytes, you probably need to mask here.
        var char = bytes[i] << 8;
// (undefined | 0) === 0 so you can save a test here by doing
//     var char = (bytes[i] << 8) | (bytes[i + 1] & 0xff);
        if (bytes[i + 1])
            char |= bytes[i + 1];
// Instead of using string += you could push char onto an array
// and take advantage of the fact that String.fromCharCode can
// take any number of arguments to do
//     String.fromCharCode.apply(null, chars);
        str += String.fromCharCode(char);
    }
    return str;
}

function unpack(str) {
    var bytes = [];
    for(var i = 0; i < str.length; i++) {
        var char = str.charCodeAt(i);
// You can combine both these calls into one,
//    bytes.push(char >>> 8, char & 0xff);
        bytes.push(char >>> 8);
        bytes.push(char & 0xFF);
    }
    return bytes;
}

so to put it all together

function pack(bytes) {
    var chars = [];
    for(var i = 0, n = bytes.length; i < n;) {
        chars.push(((bytes[i++] & 0xff) << 8) | (bytes[i++] & 0xff));
    }
    return String.fromCharCode.apply(null, chars);
}

function unpack(str) {
    var bytes = [];
    for(var i = 0, n = str.length; i < n; i++) {
        var char = str.charCodeAt(i);
        bytes.push(char >>> 8, char & 0xFF);
    }
    return bytes;
}


My Python 3.6 script to detect stamp card redeem fraud and prepare a csv report

The description of the problem in the post says that the case to be detected is redeeming "over 3 time" but what the code actually detects is 3 times or more. Which is right?
The description of the problem in the post says that the case to be detected is redeeming:

  on same stamp card within 10 mins

But what the code actually detects is redeeming:

  on same stamp card by the same user within 10 mins

Which of these is right? It seems to me that the description in the post would be better, because otherwise someone could avoid detection by switching to a new user account after redeeming twice. (Maybe the software ensures that each stamp card is redeemable only by one user, but in that case why check the user at all?)
The code assumes that the database will return records for each same stamp card id in increasing stamp time order, otherwise this won't work:

g.tail(1).stamp_time.values - g.head(1).stamp_time.values

But the SQL query only orders them by stamp card id, not by stamp time. Perhaps it happens to be the case at the moment that records are added to the table in stamp time order. But it is a bad idea to depend on this, because a change to the way the database is updated could break this assumption. It would be more robust to be explicit and write:

ORDER BY stamp_card_id, stamp_time

Here's what I suspect is the main performance problem:

for i in range(0, len(unique_users)):
    user = unique_users[i]
    new_df = df[df.user_id == user]

The expression df[df.user_id == user] looks innocent, but actually it has to loop over all the records in the dataframe twice: once to test user_id == user, and a second time to select the matching records. And this has to be done for every user. So if there are 36,000 records and (say) 10,000 unique users, then the user_id == user test gets run 360,000,000 times!

Instead of iterating over the unique users and then finding the records for each user, use Panda's groupby method to efficiently group the records by user.

But looking at my point §2 above, is this step really necessary? Perhaps what we really need to do is to ignore the users altogether and go straight to the stamp card ids.
The same anti-pattern occurs in the stamp card processing code:

sid = new_df.stamp_card_id.unique()
for i in sid:
    fdf = new_df[new_df.stamp_card_id == i]

This needs to become:

for fdf in df.groupby(['stamp_card_id']):

If you really do care about the users as well as the stamp cards, then you should order the query results by both:

ORDER BY user_id, stamp_card_id, stamp_time

and group by both:

for fdf in df.groupby(['user_id', 'stamp_card_id']):

The code creates a multiprocessing pool, but does not use it! You have to call the pool's methods, for example multiprocessing.Pool.apply, to take advantage of the pool—it doesn't work by magic.

But actually I suspect that there's no need to use multiprocessing here. Once you have eliminated the quadratic runtime behaviour as discussed above, then it should run in an acceptable amount of time in a single process.