Getting the count of how many times a route has been hit I have this log file (example below) and I'm trying to find the number of times a particular route is called.

Example log file:

2019-05-29 11:00:00 192.168.1.1 POST /route1 200  100000

2019-05-29 11:00:01 10.1.1.2 POST /route1 200  100000

2019-05-29 11:00:01 192.168.1.2 GET /route2 404 200000

2019-05-29 11:00:02 192.168.1.3 GET /route3 200  100000

2019-05-29 11:00:03 10.1.1.3 GET /route4 200  200000

2019-05-29 11:00:04 192.168.1.1 POST /route1 200 100000

Here is the code I've written:

route_count = Hash.new(0)
File.open('test.log').each do |line|
  temp_array = line.split(" ")
  route = temp_array[4]
  if route_count.has_key?(route)
    route_count[route] = route_count[route] + 1
  else
    route_count[route] = 1
  end
end
puts( route_count.map{ |k,v| "#{k},#{v}" })

and it gives the output:

/route1,3

/route2,1

/route3,1

/route4,1

I wanted to know if there is a better way to do this.

The Enumerable.group_by method is perfect for situations like this. The method takes the items in the enumerable, evaluates a block on them to yield a key, and inserts them into a hash under that key. Using this the code is very short:

puts(File.open('test.log').entries
    .group_by { |line| line.split(" ")[4] }
    .map { |k, v| "#{k},#{v.count}" })


Function that recursively downloads entire directory is slow I created this function to recursively copy an entire directory from an FTP server. It works just fine except that it is about 4 times slower than using FileZilla to do the same operation. It takes approximately 55 seconds to download the directory in FileZilla but it takes 229 seconds with this function. What can I do to make it download/run faster?

Private Sub CopyEntireDirectory(ByVal directory As String)
    Dim localPath = localDirectory & formatPath(directory)
    'creates directory in destination path
    IO.Directory.CreateDirectory(localPath)

    'Gets the directory details so I can separate folders from files
    Dim fileList As ArrayList = Ftp.ListDirectoryDetails(directory, "")

    For Each item In fileList
        'checks if it's a folder or file: d=folder
        If (item.ToString().StartsWith("d")) Then
            'gets the directory from the details
            Dim subDirectory As String = item.ToString().Substring(item.ToString().LastIndexOf(" ") + 1)
            CopyEntireDirectory(directory & "/" & subDirectory)
        Else
            Dim remoteFilePath As String = directory & "/" & item.ToString().Substring(item.ToString().LastIndexOf(" ") + 1)
            Dim destinationPath = localPath & "\" & item.ToString().Substring(item.ToString().LastIndexOf(" ") + 1)
            'downloads file to destination directory
            Ftp.DownLoadFile(remoteFilePath, destinationPath)
        End If
    Next
End Sub

This function below is what is actually taking up 97% of the time.

Public Sub DownLoadFile(ByVal fromFilename As String, ByVal toFilename As String)
    Dim files As ArrayList = Me.ListDirectory(fromFilename, "")
    Dim request As FtpWebRequest = Me.CreateRequestObject(fromFilename)
    request.Method = WebRequestMethods.Ftp.DownloadFile

    Dim response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
    If response.StatusCode <> FtpStatusCode.OpeningData AndAlso response.StatusCode <> FtpStatusCode.DataAlreadyOpen Then
        Throw New ApplicationException(Me.BuildCustomFtpErrorMessage(request, response))
    End If

    Dim fromFilenameStream As Stream = response.GetResponseStream()
    Dim toFilenameStream As FileStream = File.Create(toFilename)

    Dim buffer(BLOCK_SIZE) As Byte
    Dim bytesRead As Integer = fromFilenameStream.Read(buffer, 0, buffer.Length)
    Do While bytesRead > 0
        toFilenameStream.Write(buffer, 0, bytesRead)
        Array.Clear(buffer, 0, buffer.Length)
        bytesRead = fromFilenameStream.Read(buffer, 0, buffer.Length)
    Loop

    response.Close()
    fromFilenameStream.Close()
    toFilenameStream.Close()
End Sub

Check your buffer/BLOCKSIZE. If it's too small, it may be making many more reads and writes, and each has to wait for the network to respond.

I'm not given any information as to what block size you're using, so I can't help anymore.


Formatting seconds as days hours minutes seconds I have the below and it gives me the desired output (ie; 1 day 12:05:33) but it looks overly complicated, is there a more straight forward way of doing this with the least amount of string concatenations?

     std::string eta_string ( int64_t s ) {

     time_t seconds ( s );
     tm p;
     gmtime_s ( &p , &seconds );
     std::string str;

     if ( p.tm_yday > 0 ) {
      if ( p.tm_yday == 1 ) {
        str = std::to_string ( p.tm_yday ) += " day ";
      } else {
        str = std::to_string ( p.tm_yday ) += " days ";
      }
      if ( p.tm_hour < 10 ) {
        str = str.append ( "0" ) += std::to_string ( p.tm_hour ) += ":";
      } else {
        str = str += std::to_string ( p.tm_hour ) += ":";
      }
      if ( p.tm_min < 10 ) {
        str = str.append ( "0" ) += std::to_string ( p.tm_min ) += ":";
      } else {
        str = str += std::to_string ( p.tm_min ) += ":";
      }
      if ( p.tm_sec < 10 ) {
        str = str.append ( "0" ) += std::to_string ( p.tm_sec );
      } else {
        str = str += std::to_string ( p.tm_sec );
      }
     }
     else if ( p.tm_hour > 0 ) {
      if ( p.tm_hour < 10 ) {
        str = str.append ( "0" ) += std::to_string ( p.tm_hour ) += ":";
       } else {
        str = str += std::to_string ( p.tm_hour ) += ":";
       }
       if ( p.tm_min < 10 ) {
        str = str.append ( "0" ) += std::to_string ( p.tm_min ) += ":";
       } else {
        str = str += std::to_string ( p.tm_min ) += ":";
       }
       if ( p.tm_sec < 10 ) {
        str = str.append ( "0" ) += std::to_string ( p.tm_sec );
       } else {
        str = str += std::to_string ( p.tm_sec );
       }
     } else if ( p.tm_min > 0 ) {
      str = "00:";
      if ( p.tm_min < 10 ) {
        str = str.append ( "0" ) += std::to_string ( p.tm_min ) += ":";
      } else {
        str = str += std::to_string ( p.tm_min ) += ":";
      }
      if ( p.tm_sec < 10 ) {
        str = str.append ( "0" ) += std::to_string ( p.tm_sec );
      } else {
        str = str += std::to_string ( p.tm_sec );
      }
     } else {
      str = "00:00:";
      if ( p.tm_sec < 10 ) {
        str = str.append ( "0" ) += std::to_string ( p.tm_sec );
      } else {
        str = str += std::to_string ( p.tm_sec );
      }
    }
    return str;
 }

Try this. It uses a string-based stream to do formatted output. Let the stream put in leading zeroes. When you're done with your output, the str() method returns (a copy of) the embedded string.

#include <time.h>
#include <sstream>
#include <iomanip>

std::string eta_string ( int64_t s )
{
    time_t seconds ( s );
    tm p;
    gmtime_s ( &p, &seconds );

    std::ostringstream os;

    if ( p.tm_yday > 0 )
        os << p.tm_yday << (p.tm_yday == 1 ? " day " : " days ");

    os << std::setfill('0') << std::setw(2) << p.tm_hour << ':' <<
        std::setfill('0') << std::setw(2) << p.tm_min << ':' <<
        std::setfill('0') << std::setw(2) << p.tm_sec;

    return os.str();
}


Alternative methods of solving Sudoku I am learning Java as my first programming language and have written a bit of code. I would love to read expert reviews on this code and suggestions to improve right from naming conventions to logic. Please let me know if there is any kind of mistake.

public class Sudoku {

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        int[][] array = { { 0, 2, 0, 5, 0, 0, 0, 9, 0 },
                          { 5, 0, 0, 0, 7, 9, 0, 0, 4 },
                          { 3, 0, 0, 0, 1, 0, 0, 0, 0 },
                          { 6, 0, 0, 0, 0, 0, 8, 0, 7 },
                          { 0, 7, 5, 0, 2, 0, 0, 1, 0 },
                          { 0, 1, 0, 0, 0, 0, 4, 0, 0 },
                          { 0, 0, 0, 3, 0, 8, 9, 0, 2 },
                          { 7, 0, 0, 0, 6, 0, 0, 4, 0 },
                          { 0, 3, 0, 2, 0, 0, 1, 0, 0 } };
        solve(array);
        System.out.println("Solution is ");
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                System.out.print(" " + array[row][col] + " ");
            }
            System.out.println();
        }
        long endTime = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        System.out.println("Time taken (in MilliSeconds)= " + totalTime);
    }

    public static boolean solve(int[][] array) {
        if (isSum45(array)) {
            return true;
        }
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                if (array[row][col] == 0) {
                    for (int num = 1; num <= 9; num++) {
                        if (noConflict(array, row, col, num)) {
                            array[row][col] = num;
                            if (solve(array)) {
                                return true;
                            }
                        }
                        array[row][col] = 0;
                    }
                    return false;
                }
            }
        }
        return false;
    }

    // This method edited is after taking suggestions from this forum
    public static boolean noConflict(int[][] array, int row, int col, int num) {
        for (int k = 0; k < 9; k++) {
            if (array[row][k] == num) {
                return false;
            }
            if (array[k][col] == num) {
                return false;
            }
        }
        int m = row - (row % 3);
        int n = col - (col % 3);
        for (int p = m; p < m + 3; p++) {
            for (int q = n; q < n + 3; q++) {
                if (array[p][q] == num) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isSum45(int[][] array) {
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                int sum = 0;
                int m = row - (row % 3);
                int n = col - (col % 3);
                for (int p = m; p < m + 3; p++) {
                    for (int q = n; q < n + 3; q++) {
                        sum = sum + array[p][q];
                    }
                }
                if (sum != 45) {
                    return false;
                }
            }
        }
        return true;
    }
}

Overall, this is pretty good code for a beginner's solution to a relatively complex problem.

But your code does have me a little lost.  For one, I'm not quite sure why you're using recursion here.  It seems like that will be a lot more memory intensive than it needs to be for a straightforward problem.  It also confused me for a good ten minutes why you were checking for isSum45 to return that the problem is solved, when the sum for the entire board is actually 405.

I would rewrite this as an iterative solution rather than a recursive solution.  I think the recursion adds a lot of unnecessary complexity to the code and flow.  I don't have the time at the moment to rewrite it as such, so I'll just give some other general comments on your code.

solve(array);
System.out.println("Solution is ");
for (int row = 0; row < 9; row++) {
    for (int col = 0; col < 9; col++) {
        System.out.print(" " + array[row][col] + " ");
    }
    System.out.println();
}

I'm not a big fan of the above implementation because it relies on the solve() method modifying the original array in place.  It also makes your paradigm of returning a boolean from the solve() method irrelevant; you never compare or check for it.  I think it would be a better design if you preserve the original board and create a new int[][] solved variable which you return from the method.

for (int row = 0; row < 9; row++) {
    for (int col = 0; col < 9; col++) {
        if (array[row][col] == 0) {
            for (int num = 1; num <= 9; num++) {
                if (noConflict(array, row, col, num)) {
                    array[row][col] = num;
                    if (solve(array)) {
                        return true;
                    }
                }
                array[row][col] = 0;
            }
            return false;
        }
    }
}

On a positive note, I really like that you named your loop variables row and col versus i and j as so many people do.  It makes your code much more readable.  But one of the principles of good code design is to avoid this kind of intense nesting as much as possible.  You can do this like the following:

for(int row = 0; row < array.length; row++) {
    for(int column = 0; column < array[row].length; column++) {
        if(array[row][column] != 0) continue;
        for(int num = 1; num <= 9; num++) {
            if(!noConflict(array, row, column, num)) continue;
            array[row][column] = num;
            if(solve(array)) return true;
            array[row][column] = 0
        }
        return false;
    }
}

This makes your code a bit less complex and a bit more readable.  I completely avoid your if(solve(array)) block by simply returning the boolean directly from the recursive method call (EDITED: As David Harkness says in the comments below, this actually changes how the method functions, so I've removed that bit.).  Also note that it my loops I compare row and column to array.length and array[row].length, respectively.  This isn't a huge deal if you always know you're going to be handling boards which are 9x9, however when you're designing code you should always try to make your code as flexible as possible.  The more flexible it is and the more scenarios it can handle, the more powerful it is.

public static boolean noConflict(int[][] array, int row, int col, int num) {

This is just general nitpicking, but you should only expose methods as public which actually need to be public.  The solve method makes sense as a public method because it's the part of the API that you will want to use in other parts of the application.  This method, however, is purely internal and thus should be private.  Also, method names should pretty much always be verbs or verb phrases.  I might rename this method to something like noConflictExists or isNoConflict.  Actually, because of the nature of how I rewrote your loops above, I would change it to conflictExists or isConflict that way my usage of it is more intuitive.  These notes also apply to your isSum45 method, which is public and might use a more intuitive name for code readability.

Also, in those two methods, what happened to your beautiful variable names?!?  Why did you suddenly move to using single letters instead of something more descriptive?


Persisting objects in a ListBox ObjectCollection I have a C# WinForms app containing a ListBox control, which I populate with objects of type BukkitServer.  When an object is added or removed from the list, I want to write them to an XML file.  I have a static method in a helper class that takes an ICollection argument and achieves this.

However, since the ListBox.ObjectCollection class is not serializable (fittingly, since it contains elements of type object), I wrote a helper method to convert it to a List<BukkitServer>.

It feels like a hack, though, and I feel (hope) there is a cleaner answer.

Any other comments and criticisms are welcome as well.

static class BukkitServerDataWriter {

    public static void SaveServers(ICollection objects) {

        using (XmlTextWriter writer = new XmlTextWriter("servers.xml", Encoding.UTF8)) {
            XmlSerializer xml = new XmlSerializer(
                typeof(List<BukkitServer>),
                new Type[] { typeof(BukkitServer) });

            List<BukkitServer> servers = MakeBukkitServerList(objects);
            xml.Serialize(writer, servers);
        }

    }

    private static List<BukkitServer> MakeBukkitServerList(ICollection collection) {

        List<BukkitServer> servers = new List<BukkitServer>();
        foreach (object obj in collection) {
            servers.Add((BukkitServer)obj);
        }

        return servers;
    }

}

If you don't mind using a little bit of LINQ, it can be simplified (and optimized) as such:

static class BukkitServerDataWriter
{
    private static readonly XmlSerializer xml = new XmlSerializer(
        typeof(List<BukkitServer>),
        new[] { typeof(BukkitServer) });

    public static void SaveServers(ICollection objects)
    {
        using (var writer = new XmlTextWriter("servers.xml", Encoding.UTF8))
        {
            xml.Serialize(writer, objects.Cast<BukkitServer>().ToList());
        }
    }
}


Check that files don't contain Unicode I use this code to check that all files in a directory are free from Unicode and non-printable characters.

Can I improve the structure of the code?

#!/usr/bin/env python3

import argparse
import os
import string
import sys

def main(input_folder):
    for directory, subdirs, files in os.walk(input_folder):
        for basename in files:
            path = os.path.join(directory, basename)
            try:
                check_file(path)
            except (ValueError, UnicodeDecodeError) as e:
                print(e)
                sys.exit(1)
    print('All files are ok!')

def check_file(path):
    with open(path, encoding='utf-8') as fp:
        try:
            data = fp.read()
        except UnicodeDecodeError:
            raise ValueError('Warning! {} contains non ascii characters'.format(path))
        if not is_printable(data):
            raise ValueError('Warning! {} contains non printable characters'.format(path))

def is_printable(s):
    return all(c in string.printable for c in s)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Verify correctness of files.')
    parser.add_argument('--input', required=True, help='Input folder')
    args = parser.parse_args()
    main(args.input)

Your problem specification is unclear, and the code may be wrong as a result.  What exactly do you mean by "Unicode characters"?  What if a file contains just two bytes, 0x20 and 0x5D — is that two ASCII characters (a Space  and a Right Square Bracket ]), or is that one UTF-16 character (U+205D Tricolon ⁝)?

Unless you have a clear definition of what you mean by "Unicode", and have a special reason for detecting non-ASCII characters specifically, you may be better off opening the file with a latin_1 encoding and just checking is_printable().  In UTF-8, any multibyte sequence will have the leading bit set in all but the last byte, which would place it outside the string.printable range.  You would reject the same files as before, just with a less specific reason given.

For scalability, it would be better to read fixed-size blocks (perhaps around 8 kB) instead of the entire file at once.  It might also be more efficient to check c in set(string.printable) rather than c in string.printable.


Security concern for SQL Injections/XSS This question is everywhere, and I looked at many answers. A lot suggest using mysql_real_escape_string but that isn't an option, since it's depreciated now.

Being a rookie at PHP/SQL and especially when it comes to security, I can only hope some of you may be able to help out here. I know for a fact that some of these procedures I already use contribute to security.

As an example: I didn't use :pname before when defining query_params, which caused SQL to interpret submissions like 'values'; drop table dbname; as actual commands. One thing that did save me from the command not being executed is the conversion of spaces to underscores.

search_submit.php

<?php
$pname=str_replace(' ','_',strtolower($_GET['pname']));
require 'connection.php';
  if(isset($_GET['pname'])){

  if(preg_match("/\w/i", $_GET['pname'])){
   $query="SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE   SCHEMA_NAME = :pname;";
           $query_params = array(
             ':pname' => str_replace(' ','_',strtolower($_GET['pname']))
        );
   $prep=$conn->prepare($query);
   $result=$prep->execute($query_params) or die("failed!");
   $row = $prep->fetch();
        if($row)
        {
            die("Exists");
        }
        else{
            echo("doesn't");
        }
  }
  }
  else{
  echo  "
Error
";
  }
?>

index.html

<form  method="get" action="search_submit.php"  id="searchform">
  <input  type="text" name="pname">
  <input  type="submit" name="input" value="Search">

As of right now everything appears to be working perfectly, and any sneaky submissions are simply interpreted as text. But I am a very paranoid person.

Preventing SQL Injection

  A lot suggest using mysql_real_escape_string

Some people do suggest this, but it's a bad suggestion. The only proper defense against SQL injection are prepared statements.

Escaping is at best a second-best solution in case you cannot use prepared statements for some reason.

  But that isn't an option, since it's depreciated now.

mysql_ is deprecated, and has been for quite some time. mysqli_ however is not, and it does offer mysqli_real_escape_string. Still, prepared statements are the way to go.

The good news is that you use prepared statements, and use them correctly, so you are defending against SQL injection exactly as you should be.

Your additional Filters

That being said, your additional filters are not helping, and are in fact adding a false sense of additional security:

Replacing  with _ doesn't really prevent anything. Pretty much all attacks I can imagine would likely still be possible, depending on the concrete situation. For SQL injection, you could eg use comments (/**/) instead of spaces.
your regex checks if there is any word character given. So ' and 1=1-- would pass through it, as a is a word character.

But as I said, your code is secure against SQL Injection, so you don't need additional protection. Still, input filtering as defense in depth is a good idea (just not the way you were filtering), although I would do it in a centralized place and a bit more structured. See eg here.

As you mention XSS: As you are not echoing anything variable, you do not need to worry about XSS at the moment.

Misc

your indentation is off, making your code hard to read (it's eg hard to see what the last else closes).
your spacing is also off, as is your positioning of brackets.
you never use $pname.
it's not clear to me why you sometimes die and sometimes echo.
you can make your queries more readable by separating keywords and variables by case. So for example, keywords could be all uppercase, variables all lowercase.

Given all of this, your code could look something like this:

require 'connection.php';
if(!isset($_GET['pname'])) {
    return "
Error
";
}

$query="SELECT schema_name FROM information_schema.schemata WHERE schema_name = :pname;";
$query_params = array(
    ':pname' => $_GET['pname']
);

$prep = $conn->prepare($query);
$result = $prep->execute($query_params) or die("failed!");
$row = $prep->fetch();

if($row) {
    echo "Exists" ;
} else {
    echo "doesn't" ;
}


Calculate all possible combinations of given characters I was asked in my textbook Lectures on Discrete Mathematics for Computer Science to construct a program that would take an alphabet ({a,b,c} or any combination of characters {1,4,s,a}) as well as a length value and calculate all possible combinations of this alphabet.

For example:

char[] alphabet = new char[] {'a','b'};
possibleStrings(3, alphabet,"");

This will output:

aaa
aab
aba
abb
baa
bab
bba
bbb

That is all combinations of the alphabet {a,b,c} with the string length set to 3.

The code I have written is functional, however I'd like to read what things I am doing wrong or could be doing better. The book didn't give an example program, so I only hope this is what it was looking for, but maybe there's a much better way to do it or way to improve how I'm doing it. Maybe this is fine, but I just need someone to look at it and tell me in that case.

public class Program {

    public static void main(String[] args) {

        // Create an alphabet to work with
        char[] alphabet = new char[] {'a','b'};
        // Find all possible combinations of this alphabet in the string size of 3
        StringExcersise.possibleStrings(3, alphabet,"");
    }

} class StringExcersise {

    public static void possibleStrings(int maxLength, char[] alphabet, String curr) {

        // If the current string has reached it's maximum length
        if(curr.length() == maxLength) {
            System.out.println(curr);

        // Else add each letter from the alphabet to new strings and process these new strings again
        } else {
            for(int i = 0; i < alphabet.length; i++) {
                String oldCurr = curr;
                curr += alphabet[i];
                possibleStrings(maxLength,alphabet,curr);
                curr = oldCurr;
            }
        }
    }
}

your code looks fine.

a slight performance improvement i'd do is pass a StringBuilder instead of a String - a String in java is immutable so every time you call curr += alphabet[i] youre actually allocating a new String object. instead you could append the character to a StringBuilder (and delete the last character when you leave) to save on the number of Objects created during the run
for(int i = 0; i < alphabet.length; i++) {} could be re-written as a more modern loop: for (char c : alphabet) {}, which would make for a more readable result


Mixin, @extend or (silent) class? I am struggling with the basic concepts of SASS.

I have a page that has multiple section, all of them are sharing properties like width, font-size etc.

I would like to optimize my code and do one of the following:

1. Use an extendable silent class

%main-section{
    width: 100%;
    //... more styles
}

.about {
    // this is a main-section
    @extend %main-section;
}

2. mixin

@mixin main-section($text-color, $bg-color) {

  //some paramaters would provide a flexible module
  width: 100%;
  // etc.
}

3. just a simple class to apply in my HTML

.main-section{
    width: 100%;
    //... more styles
}

I don't know which one i should choose. Maybe there's even a better solution i didn't mention. What is your opinion?

Best solution: Create a single section class and add it to all sections.

Second best solution: Write a specific section class and @extend on other sections (in case you do not have control of HTML)

Worst solution: Writing a mixin (duplicates styles)


Parsing a sequence of bits out of a bit field For homework, I had to parse CPUID instruction output in C, which required a lot of specific bit-manipulation like:

(eax & CACHE_LEVEL) >> 5

I calculated the mask CACHE_LEVEL and the amount that was needed to be right-shifted by hand. It was a pain, so I decided to write a couple Python functions to help with this.

For example:

>>> eax = 0x4004121  # Taken from a register after executing a CPUID instruction with leaf-4 indicated
>>> parse_out_bits(eax, 5, 7)  # Return the value at bits 5 to 7 (inclusive)
1  # This is an L1 cache

Indices are 0-based and inclusive.

Where eax is the following, and I want the value of the bits from S to E:

                        E S
100 0000 0000 0100 0001 0010 0001

I'd like notes on anything here, but specifically how this could be done better in terms of bit manipulation.

from typing import Generator

def bit_place_values(start_index: int, end_index: int) -> Generator[int, None, None]:
    acc = 1 << start_index
    for _ in range(start_index, end_index + 1):
        yield acc
        acc <<= 1

def parse_out_bits(bit_field: int, start_index: int, end_index: int) -> int:
    mask = sum(bit_place_values(start_index, end_index))
    return (mask & bit_field) >> start_index

This is not the kind of thing you should loop for. Just calculate a mask:

def parse_out_bits(bit_field: int, start_index: int, end_index: int) -> int:
    mask = (1 << (end_index - start_index + 1)) - 1
    return (bit_field >> start_index) & mask

In English:

If you're getting bits 5-7, that's 3 bits
2^3 == 8, - 1 = 7 (or 111 in binary - that is your mask)
Shift the field right by 5 to put the bits of interest in the least-significant position
And-mask them to get the result.


Simple AJAX script calling a number I wanted to create a basic interaction which does 1 outcome but with 2 possible actions: get a number from an other document, by either clicking on a button or waiting a fixed amount of time.

I wrote this, which works:

//Initialize the ajax function
(function($) {
    $.fn.getNumber = function() {
        $(this).load('number.html .giver');
    };
})(jQuery);

//Launch ajax function 3s after DOM is loaded
$(function() {
    $('.receiver').delay(3000).queue(function(nxt) {
        $(this).getNumber();
    });
});

//Launch ajax function if a button is clicked
$('button').click(function() {
    $('.receiver').getNumber();
});

Now even if it works I'm pretty sure my code is badly written, for instance my getNumber function seem redundant. I just fear I might get bad habits, any insights of how I could write this in a more elegant way?

The biggest problem with this code is that is not manageable at all. This setup is inefficient and will make it very hard to add features and also bug fix if things start going wrong.

there's no success or error handling on your AJAX request.
There's no way of knowing if getNumber() worked on not, which could end up spewing errors onto the page.
Callbacks are your friend.

Here is a more manageable setup for you.

// jQuery will wait until the DOM is ready. Then in the callback you can define all your listeners.
$('body').on('ready', function(){
    // This is the listener for the button click. $.on() should always be used from jQuery 1.7+.
    $('button').on('click', function(){
        $('.receiver').load('number.html .giver', function(response, status, xhr){
            // This is just a very basic error check. At least is there is an error you'll know.
            if (status == "error") {
                $('.receiver').html("Sorry there was an error: " + xhr.statusText);
            }
        });
    });
});

I would always advise strongly against having timed events in your code. They tend to break easily and are hard to fix. However if you really want them, use the Javascript setTimeout() function after DOM load. setTimeout() is just a bit easier to read and maintain. Instead of timing events, I would advise using callbacks to fire events. This solution allows any number of DOM listeners to be easily added to your page now without hassle.

I would make one more note here: in my experience it never ends well trying to drag AJAX responses through functions unless you have rock solid scopes set up. I would personally always say it is best practice, with adding dynamic content, to always create the AJAX request in the same scope/function that you are going to use it in. So:

// Don't do this.
var ajaxRetrieve = function(){
    $.ajax({
        url: '/gimmeAllOfThat',
        method: 'POST',
        success: function(responseData){
            return responseData;
        },
        error: function(responseData){
            alert('Check yourself');
        }
    });
}

$('button').on('click', function(){
    // This is very bad.
    var data = ajaxRetrieve();
    $('#div').html(data);
});

This config means that you will still have to use whatever you get returned from your function, even if the AJAX request fails. Which could mean you'll have to check for errors twice, instead of just once and using the data as an event in the AJAX request. Also something to note is: that AJAX return will fail so badly, as it won't get returned into the parent function scope, only the success function scope.

TL;DR: use callbacks:

// Do this.
$('body').on('ready', function(){
    var ajaxRetrieve = function(callback){
        $.ajax({
            url: '/gimmeAllOfThat',
            method: 'POST',
            success: function(responseData){
                callback(responseData);
            },
            error: function(responseData){
                alert('Check yourself');
            }
        });
    }

    $('button').on('click', function(){
        ajaxRetrieve(function(data){
            $('#div').html(data);
        });
    });
});

This setup means that that function can be reused for a number of different things, and you can just change the callback function each time you call it. It also means you get out of 'Returning Hell', trying to return data here, there and everywhere.


Unduplicating prox_pass in nginx location blocks I needed to IP block something in nginx and I ended up with duplicated proxy_forward code. How can I refactor and un-duplicate this?

server{
    location /admin{
        allow 123.90.250.0/24;
        allow 123.66.148.0/24;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://foo;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://foo;
    }
}

You may move proxy settings to 'server' block. But proxy_pass should stay at 'location' block

upstream foo {
    server 127.0.0.1:8080;
}

server{
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    location /admin {
        allow 123.90.250.0/24;
        allow 123.66.148.0/24;
        proxy_pass http://foo;
    }

    location / {
        proxy_pass http://foo;
    }
}


HTML editor for Google App Engine Please review this simple code. When I review it I decide to rewrite so that it doesn't write to a template, but instead directly to output since the whole HTML is fetched from the data layer. I used the tool appengine_admin to get editable HTML for simple html capabilities to an appspot project:

    class Page(db.Model):
      html = db.TextProperty(required=True)

    class AdminPage(appengine_admin.ModelAdmin):

        model = Page
        listFields = (
            'html',
            )
        editFields = (
            'html',
            )

  application = webapp.WSGIApplication([  ('/page/([0-9]+)', PageHandler), ])

  class PageHandler(BaseHandler):
      def get(self, file_id):
        page = Page.get_by_id(long(file_id))
        if not page:
          self.error(404)
          return
        self.render_template(file_id+'.html', {
            'body': page.body,
        })

My 2 to-dos:

change variable file_id to page_id
change writing to direct output instead of template

application = webapp.WSGIApplication([  ('/page/([0-9]+)', PageHandler), ])

I'd put the list of urls as a global constant, I think that would make it easier to read.

  class PageHandler(BaseHandler):
      def get(self, file_id):
        page = Page.get_by_id(long(file_id))
        if not page:

If you are checking against None here, I'd  use if page is None:

  self.error(404)
  return

I'd use else instead of returning here

self.render_template(file_id+'.html', {
    'body': page.body,
})


Tic-Tac-Toe for the terminal I recently started learning C, and this is my first fairly large (to me) program. It's a basic Tic Tac Toe game for the console. There's no AI, it's just a 2-player game. Is there anything I can improve?

Some problems I've been told elsewhere:

There's no case for tying/drawing.
I should split the place function into two.

I also think it would be better to use global variables for board and player, since they're used so often.

// Tic Tac Toe in C. V1.0 by James 26/05/2019

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 3

// Draws the board along with rows/cols, numbered.
void draw_board(char board[])
{
    system("clear");
    printf("# 1 2 3\n");

    // Rows
    for (int i = 0, n = 0; i < SIZE; i++)
    {
        // Columns
        printf("%d ", i + 1);
        for (int j = 0; j < SIZE; j++)
        {
            printf("%c ", board[n]);
            n++;
        }
        printf("\n");
    }
}

// Initializes board to '-' characters.
void init_board(char board[])
{
    for (int i = 0; i <  SIZE * SIZE; i++)
    {
        board[i] = '-';
    }
}

// Returns true if the piece was successfully placed,
// false if the position was invalid or already taken.
bool place(char board[], char player)
{
    char posinput[64];

    printf("%c, pick your position (xy, rc): ", player);
    scanf("%s", posinput); // <-- I realise that this is potentially bad, but realistically,
                           // the user would have to be trying to break something, and
                           // this is just a little program I made for practice.

    int row = (posinput[0] - '0') - 1;
    int col = (posinput[1] - '0') - 1;

    int pos = col + row * SIZE;

    if (pos >= 0 && pos < SIZE * SIZE)
    {
        if (board[pos] == 'x' || board[pos] == 'o')
            return false;

        board[pos] = player;
        return true;
    }

    return false;
}

// Returns true if there are three of the same chars in a row.
// b = board, p = player. Shortened for readability.
bool check(char b[], char p)
{
    // Check rows
    if (b[0] == p && b[1] == p && b[2] == p)
        return true;
    if (b[3] == p && b[4] == p && b[5] == p)
        return true;
    if (b[6] == p && b[7] == p && b[8] == p)
        return true;

    // Check columns
    if (b[0] == p && b[3] == p && b[6] == p)
        return true;
    if (b[1] == p && b[4] == p && b[7] == p)
        return true;
    if (b[2] == p && b[5] == p && b[8] == p)
        return true;

    // Check diagonals
    if (b[0] == p && b[4] == p && b[8] == p)
        return true;
    if (b[2] == p && b[4] == p && b[6] == p)
        return true;

    // If no one won, return false
    return false;
}

int main(void)
{
    char board[SIZE * SIZE];
    char player = 'x';
    init_board(board);

    while (true)
    {
        draw_board(board);

        if (place(board, player))
        {
            if (check(board, player))
                break;

            if (player == 'x')
                player = 'o';
            else
                player = 'x';
        }
    }

    draw_board(board);
    printf("-----------------------------\n");
    printf("Player %c wins!!!\n", player);
    printf("-----------------------------\n");
}

First of all: nice work! It's easy to read and understand.

Program organization

It's very good that you split the task to small functions.
Reading the body of main reveals nicely the overall flow.

Ideas for further improvement:

place does two things: it reads input from user and updates the state of the board. It would be good to separate these logically distinct steps to different functions. A new function, say read_next_move could return boolean just like place, and take parameters board and pointers to x and y, whose values will be used when updating the board state.
The name check is too generic. How about is_game_over or has_player_won.
Printing the winner would be good to move to a dedicated function, as you did with other steps.

Use more constants

The special symbols -, x and o would be good to define as constants.
So that instead of if (board[pos] == 'x' || board[pos] == 'o'),
you could write more descriptively if (board[pos] == PLAYER_X || board[pos] == PLAYER_O), and if ever needed change the values easily.
By the way, in this particular example I would replace the condition with if (board[pos] != AVAILABLE).


Implementing pow(x,n) I solved this leetcode problem.

The question is.

  Implement pow(x, n).

This is my solution.

class Solution {
public:
    double doPow(double x, unsigned int n)
    {
        double result = 1;
        while(n > 0)
        {
            if(n & 1) // if bit is set
            {
                result = result * x;
            }
            n = n >> 1;
            x = x * x;
        }
        return result;
    }
    double myPow(double x, int n) {
        unsigned int temp = abs(n);
        if(n < 0)
        {
            return 1/doPow(x, temp);
        }
        return doPow(x,n);
    }

};

Although this solution gets accepted, it converts n to unsigned to make sure that INT_MIN doesn't overflow when it is converted to it's positive values and then computed.

Is there a better way to handle negative exponents?

Extracting the unsigned calculation just to solve the possible overflow problem is unnecessary. Of course, having an unsigned version of the function may be useful for other reasons in "real" code, but that would be a different motivation.

You can handle your problem here with a simple variable, and not a whole function:

double myPow(double x, int n) {
    unsigned int p = abs(n);
    double result = 1;
    while(p > 0)
    {
        if(p & 1) // if bit is set
        {
            result = result * x;
        }
        p = p >> 1;
        x = x * x;
    }

    if(n < 0)
    {
        return 1/result;
    }
    return result;
}

Changing the unsigned part to use the unsigned int p instead of n, and then using n to test for the negative later is just fine.

Now, a more general review...

Your comment (the only one), is not great, the code here is obvious:

if(p & 1) // if bit is set

I would remove the comment, or change it to // for odd powers

I needed to take some time to figure out how you did the algorithm, and how it works out to \$O(\log{p})\$ (where p is the power) complexity. It's an "advanced" solution, and in that respect you can be proud of it. I am trying to figure out how to make the algorithm more obvious though. It's complicated that the result is only set on odd-powers in the shifting. Comments on your algorithm would help, but I am sure there's a way to make the process more obvious in the code... I'm still thinking.

Regardless, the code style is good, your variable names are not great, but are not horrible for 1-letter names either.

Overall, with a better description of the mechanics of the solution as comments in the code, I would call it good.


Tying the enablement of one checkbox to another using AngularJS and jQuery I'm new to AngularJS, coming from a jQuery background and I have a situation I've solved but only by including jQuery in an AngularJS function.  Something just doesn't feel right doing this and I was hoping someone could tell me if there's a better way. (This is all code I have inherited and have amended to enable new functionality)

I have 2 checkboxes, let's call them 'Notary' and 'Login'. If I enable Notary, then Login must get enabled and also disabled so the user can't uncheck it.

The ASPX looks like:

 Enable INKWRX Login:
 <dw-checkbox class="notarylogin" name="UserHasPassword" ng-model="user.Details.HasPassword" ></dw-checkbox>

Notary:
<dw-checkbox name="notary" id="notary" ng-model="user.ExtraData.userPermissions.notary.allowed" ng-click="amendNotary(user.ExtraData.userPermissions.notary.allowed)"></dw-checkbox>

When the dw-checkbox directive is invoked, it renders a checkbox input element for each dw-checkbox tag like this:

 //this directive adds behaviour for showing ticks and cross images in viewMode and an editable checkbox in editmode
    app.directive("dwCheckbox", function ($rootScope) {
        return {
            restict: 'E',
            require: 'ngModel',
            template: '<input name="name" type="checkbox" ng-model="model" />',
            scope: {
                model: '=ngModel',
                name: '=name'
            },
            link: function (scope, element, attrs, ngModel) {

                //render the field according to the mode and the current value of the model
                function render() { ...does the rendering..}

and my amendNotary function which does the checkbox disabling looks like this (I amended another directive to add an inklogin class to the Login checkbox):

$scope.amendNotary = function(value) {
       if (value === true) {
           $scope.user.Details.HasPassword = true;
           $('.inklogin').attr('disabled', true);
       } else {
           $scope.user.SignatureText = "Signature:";
           $('.inklogin').attr('disabled', false);
       }
    }

Is there an obvious cleaner way of doing this, or is mixing jQuery with Angular generally regarded as okay?

Is there an obvious cleaner way of doing this [?]

Without knowing where the element(s) with class name inklogin exist, it can only be presumed that it exists as a child of the element with ng-controller="controller" (or however the controller is named). If that is incorrect, please update your post to include that HTML output (from the ASPX code).

Given the presumption above, jQuery can be eliminated by adding ng-disabled (bound to user.ExtraData.userPermissions.notary.allowed) to the input with class inklogin. Bearing in mind that you stated you attempted this, perhaps there was something wrong with the setup. Below is a working example of this:

angular.module('app', [])
  .controller('ctrl', function($scope) {
    $scope.user = {
      Details: {},
      SignatureText: ''
    };
    $scope.amendNotary = function(value) {
      if (value === true) {
        $scope.user.Details.HasPassword = true;
      } else {
        $scope.user.SignatureText = "Signature:";
      }
    };
  }).directive("dwCheckbox", function($rootScope) {
    return {
      restict: 'E',
      require: 'ngModel',
      template: '<input name="name" type="checkbox" ng-model="model" />',
      scope: {
        model: '=ngModel',
        name: '=name'
      },
      link: function(scope, element, attrs, ngModel) {

        //render the field according to the mode and the current value of the model
        function render() {};
      }
    };
  });

  Enable INKWRX Login:
  <dw-checkbox class="notarylogin" name="UserHasPassword" ng-model="user.Details.HasPassword"></dw-checkbox>

  Notary:
  <dw-checkbox name="notary" id="notary" ng-model="user.ExtraData.userPermissions.notary.allowed" ng-click="amendNotary(user.ExtraData.userPermissions.notary.allowed)"></dw-checkbox>

Signature Text:

  Element with class "inklogin":
  <input type="checkbox" class="inklogin" ng-disabled="user.ExtraData.userPermissions.notary.allowed" />

  or is mixing jQuery with Angular generally regarded as okay?

Generally it is acceptable, though each library requires extra resources (e.g. download size, time, etc.). There are whole websites about removing the need for jQuery, like http://youmightnotneedjquery.com/. if you were working on a team, some teammates might prefer to have as few libraries as possible


Detect loop in linked list (Floyd cycle finding algorithm) I have written this code Floyd Cycle finding algorithm. The methods I have used are createLoop() , doesLoopExist(), lengthOfLoop() and insert().
I want to optimise this code and improve using advanced C++.

#include <iostream>

template <class T>
class LinkedList
{
  struct Node
  {
    T data;
    Node * next;
    Node(T value) : data(value), next(nullptr) {}
  };
  Node *head;

public:
  LinkedList() : head(nullptr) {}
  ~LinkedList();
  void insert(T);
  void createLoop(int);
  bool doesLoopExist();
  int lengthOfLoop();
};

template <class T>
void LinkedList<T>::insert(T data)
{
  Node *node = new Node(data);
  Node *tmp = head;
  if(tmp == nullptr)
  {
    head = node;
  }
  else
  {
    while(tmp->next != nullptr)
    {
      tmp = tmp->next;
    }
    tmp->next = node;
  }
}

template <class T>
void LinkedList<T>::createLoop(int n)
{
  Node *tmp = head;
  Node *tail = head;
  for(int i = 0; i < n-1; i++)
  {
    tmp = tmp->next;
  }

  while(tail->next != nullptr)
  {
    tail = tail->next;
  }
  tail->next = tmp;
}

template <class T>
bool LinkedList<T>::doesLoopExist()
{
  Node *slowPtr = head;
  Node *fastPtr = head;

  while(slowPtr && fastPtr && fastPtr->next)
  {
    slowPtr = slowPtr->next;
    fastPtr = fastPtr->next->next;

    if(slowPtr == fastPtr)
    {
      return true;
    }
  }
  return false;
}

template <class T>
int LinkedList<T>::lengthOfLoop()
{
  int count = 0, loopExist = 0;
  Node *slowPtr = head;
  Node *fastPtr = head;
  while(slowPtr && fastPtr && fastPtr->next)
  {
    slowPtr = slowPtr->next;
    fastPtr = fastPtr->next->next;

    if(slowPtr == fastPtr)
    {
      loopExist = 1;
      break;
    }
  }
  if(loopExist)
  {
    fastPtr = fastPtr->next;
    count++;
    while(slowPtr != fastPtr)
    {
      fastPtr = fastPtr->next;
      count++;
    }
    return count;
  }
  return 0;
}

template <class T>
LinkedList<T>::~LinkedList()
{
  Node *tmp = nullptr;
  while(head)
  {
    tmp = head;
    head = head->next;
    delete tmp;
  }
  head = nullptr;
}

int main()
{
  LinkedList<char> ll1;
  ll1.insert('p');
  ll1.insert('r');
  ll1.insert('o');
  ll1.insert('g');
  ll1.insert('r');
  ll1.insert('a');
  ll1.insert('m');
  ll1.insert('m');
  ll1.insert('e');
  ll1.insert('r');

  int nodeNumber = 5;
  //Node number starts from 1

  ll1.createLoop(nodeNumber);
  bool result = ll1.doesLoopExist();
  if(result == true)
  {
    std::cout <<"Loop exist in the Linked List\n";
  }
  else
  {
    std::cout <<"Loop does not exist in the Linked List\n";
  }

  int len = ll1.lengthOfLoop();
  std::cout << "Length of Loop is " << len <<"\n";

  return 0;
}

DRY. The first loop of lengthOfLoop is literally identical to doesLoopExist. Replace it with

    loopExist = doesLoopExist();

This also hints that doesLoopExist should be Node * doesLoopExist(), and return a node on the loop or nullptr.
The destructor invokes UB when applied to the list with a loop. At the end of the loop the next pointer points to an already deleted node.
insert should be better called append.


Function to convert strings from hmmss, mmss, ss format to milliseconds Here's the current logic to convert strings in the format hh:mm:ss, mm:ss or ss to milliseconds.

Any comments on how to improve this?

hhmmssToMillis(hhmmss) {
   let time= hhmmss.split(':').reverse();
   let millis = 0;
   switch(time.length) {
     case 1:
         millis = parseInt(time[0]) * 1000;
         break;
     case 2:
         millis = (parseInt(time[1]) * 60 + parseInt(time[0])) * 1000;
         break;
     case 3:
         millis = (parseInt(time[2]) * 60 * 60 + parseInt(time[1]) * 60 + parseInt(time[0])) * 1000;
         break;
   }
   return millis;
}

Use const instead of let if the variable is not being reassigned.

You can do away with the millis variable if you return early throughout the switch statement. This also allows you to remove the break statements.

hhmmssToMillis(hhmmss) {
    const time = hhmmss.split(':').reverse();
    switch(time.length) {
       case 1:
         return parseInt(time[0]) * 1000;
       case 2:
         return (parseInt(time[1]) * 60 + parseInt(time[0])) * 1000;
       case 3:
         return (parseInt(time[2]) * 60 * 60 + parseInt(time[1]) * 60 + parseInt(time[0])) * 1000;
    }
    return 0;
}


Smearing estimate with Fortran I wrote this Fortran module and I need its subroutines to run as fast as possible. My hope is to get some advice to optimize this code, and after having the serial version running as fast as it can run I will use MPI to make it parallel.

Module smearingmodule
IMPLICIT NONE
contains
subroutine smearing(i, nMCd, nPeriodT, nPeriods, DF, theta, wm) bind(C, name="smearing_")
    use, intrinsic                         :: iso_c_binding, only : c_double, c_int
    integer(c_int), intent(in)                                 :: i, nMCd, nPeriods
    integer(c_int), intent(in), dimension(nPeriods)            :: nPeriodT
    real(c_double), intent(in), dimension(i,5)        :: DF
    real(c_double), intent(in), dimension(i,nMCd)     :: theta
    real(c_double), dimension(nMCd, nPeriods), intent(out)  :: wm
    integer                                             :: d

    do d=1,nMCd
        CALL subsmearing(d, i, nMCd, nPeriodT, nPeriods, DF, theta, wm(d,:))
    end do

end subroutine smearing

subroutine subsmearing(d, i, nMCd, nPeriodT, nPeriods, DF, theta, wm)
    integer, intent(in)                                 :: d, i, nMCd, nPeriods
    integer, intent(in), dimension(nPeriods)            :: nPeriodT
    double precision, intent(in), dimension(i,5)        :: DF
    double precision, intent(in), dimension(i,nMCd)     :: theta
    double precision, dimension(nPeriods), intent(out)  :: wm
    double precision, allocatable, dimension(:,:)       :: out
    double precision, dimension(i,8)                    :: bigDF
    double precision, dimension(i)                      :: epredC, epredT, diff, A1, A2
    double precision, dimension(i,i)                    :: B1, B2
    integer                                             :: j, Period, jj
    double precision                                    :: sumWeights

    diff = 0.0d0
    wm = 0.0d0
    epredC = exp(DF(:,4) + (theta(:,d) * DF(:,1)))
    epredT = exp(DF(:,4) - (theta(:,d) * (1-DF(:,1))))

    do j=1,i
        B1(:,j)=epredT(j)
        B2(:,j)=epredC(j)
    end do

    A1 = matmul(DF(:,5), B1)
    A2 = matmul(DF(:,5), B2)

    diff = (A1-A2)/DBLE(i)

    bigDF(:,1:5) = DF
    bigDF(:,6) = epredC
    bigDF(:,7) = epredT
    bigDF(:,8) = diff

    do jj =1, nPeriods
        ALLOCATE(out(nPeriodT(jj),7))
        CALL subsetPeriod(bigDF, i, 8, nPeriodT(jj), jj, out) !Filters data
        sumWeights = sum(out(:,2))
        do j=1, nPeriodT(jj)
            wm(jj) = wm(jj) + (out(j,7)*out(j,2)/sumWeights)
        end do
        DEALLOCATE(out)
    end do

end subroutine subsmearing

subroutine subsetPeriod(A, rowA, colA, rowB, Period, B)
    integer, intent(in)                                    :: rowA, colA, rowB, Period
    double precision, dimension(rowA, colA), intent(in)    :: A
    double precision, dimension(rowB,colA-1), intent(out)  :: B
    integer                                                :: i, pos

    pos = 1
    do i = 1, size(A,1)
        if(A(i,2)==Period)then
            B(pos,1) = A(i,1)
            B(pos,2:) = A(i,3:)
            pos = pos+1
        end if
    end do
end subroutine subsetPeriod

end module smearingmodule

DF is going to be millions by 5, theta millions by 4000. That is why I want to optimize the code and make it parallel.

Ultimately, I will be calling this code from R.

Link in case you are curious about smearing

I added some comments to your code. I haven't tested anything, but that's the first things I would do to optimize your code. Under Linux, if you compile with g, you can use perf top to see live where your code takes time.
Good luck!

Module smearingmodule
IMPLICIT NONE
contains
subroutine smearing(i, nMCd, nPeriodT, nPeriods, DF, theta, wm) bind(C, name="smearing_")
    use, intrinsic                         :: iso_c_binding, only : c_double, c_int
    integer(c_int), intent(in)                                 :: i, nMCd, nPeriods
    integer(c_int), intent(in), dimension(nPeriods)            :: nPeriodT
    real(c_double), intent(in), dimension(i,5)        :: DF
    real(c_double), intent(in), dimension(i,nMCd)     :: theta
    real(c_double), dimension(nMCd, nPeriods), intent(out)  :: wm
    integer                                             :: d

!   To use more efficiently vectorization in the subsmearing sburoutine,
!   DF(:,4), theta(:,d) and DF(:,1) need to be 256-bit aligned.
!   This can be done by :
!   1) Setting the size of the 1st dimension of DF and theta to a multiple of 4
!      double-precision elements (padding at the end of each column)
!   2) Forcing the arrays to start at the proper alignment. With the
!      ifort compiler, this can be done after the allocation using
!      !DIR$ ATTRIBUTES ALIGN : 32 :: DF, theta
!
    do d=1,nMCd
    ! * Now you have to pass the physical sizes of the arrays DF and
    !   theta because they don''t correspond to the logical size
    !   because of the padding
    ! * If you can, use wf dimensioned as (nPeriods,nMCd) to avoid
    !   the implicit matrix transposition in (d,:)
        CALL subsmearing(d, i, nMCd, nPeriodT, nPeriods, DF, theta, wm(d,:))
    end do

end subroutine smearing

subroutine subsmearing(d, i, nMCd, nPeriodT, nPeriods, DF, theta, wm)
    integer, intent(in)                                 :: d, i, nMCd, nPeriods
    integer, intent(in), dimension(nPeriods)            :: nPeriodT
    double precision, intent(in), dimension(i,5)        :: DF
    double precision, intent(in), dimension(i,nMCd)     :: theta
    double precision, dimension(nPeriods), intent(out)  :: wm
    double precision, allocatable, dimension(:,:)       :: out
    double precision, dimension(i,8)                    :: bigDF
    double precision, dimension(i)                      :: epredC, epredT, diff, A1, A2
    double precision, dimension(i,i)                    :: B1, B2
    integer                                             :: j, Period, jj
    double precision                                    :: sumWeights

!   In general, put explicitly the boundaries in your array syntax. This
!   makes the code more explicit and in case the physical dimensions
!   don''t correspond to the logical dimensions (because of padding for
!   instance), your code will be more robust.

    diff = 0.0d0
    wm = 0.0d0

!   1) Use 1.d0 insetad of 1 to avoid conversion
!   2) Remove array syntax here so that DF streams only once through the CPU
!     epredC = 0.d0
!     epredT = 0.d0
!     do k=1, i ! and not size(DF,1) because of padding
!      epredC = epredC + exp(DF(k,4) + (theta(k,d) * DF(k,1)))
!      epredT = epredT + exp(DF(k,4) - (theta(k,d) * (1.d0-DF(k,1))))
!     end do
    epredC = exp(DF(:,4) + (theta(:,d) * DF(:,1)))
    epredT = exp(DF(:,4) - (theta(:,d) * (1-DF(:,1))))

    do j=1,i
        B1(:,j)=epredT(j)
        B2(:,j)=epredC(j)
    end do

    ! Here, replace matmul by Lapack matrix multiply (dgemm).
    ! Matmul is good only for small matrices.
    ! Otherwise, put the boundaries of DF as DF(1:i,5)
    call dgemm('N','N',
    A1 = matmul(DF(:,5), B1)
    A2 = matmul(DF(:,5), B2)

    diff = (A1-A2)/DBLE(i)

    ! It would be better to dimension bigDF as (8,i) here for
    ! the subsetPeriod subroutine
    ! In that case, you would need:
    ! do k=1,i
    !   bigDF(1:5,k) = DF(k)
    !   bigDF(  6,k) = epredC
    !   bigDF(  7,k) = epredT
    !   bigDF(  8,k) = diff
    ! end do
    ! If bigDF is properly aligned, you will have a very efficient store
    ! here

    bigDF(:,1:5) = DF
    bigDF(:,6) = epredC
    bigDF(:,7) = epredT
    bigDF(:,8) = diff

    ! 1) Put the allocate and deallocate statements outside of the
    !     loop.
    ! 2) Dimension the array as ( 8, maxval(nPeriodT) ). 8 is better
    !    than 7 for vectorization, and the transposed version is
    !    preferable for the called loop
    ! 3) Compute sumWeights_inv = 1.d0/sumWeights outside of the loop
    !    and multiply by sumWeights_inv in the loop : a division is a
    !    evry expensive operation
    ! 4) sumWeights could be computed on the fly for free in
    !    subsetPeriod. You would need to pass it as an argument
    do jj =1, nPeriods
        ALLOCATE(out(nPeriodT(jj),7))
        CALL subsetPeriod(bigDF, i, 8, nPeriodT(jj), jj, out) !Filters data
        sumWeights = sum(out(:,2))
        do j=1, nPeriodT(jj)
            ! Multiply by inverse
            wm(jj) = wm(jj) + (out(j,7)*out(j,2)/sumWeights)
        end do
        DEALLOCATE(out)
    end do

end subroutine subsmearing

subroutine subsetPeriod(A, rowA, colA, rowB, Period, B)
    integer, intent(in)                                    :: rowA, colA, rowB, Period
    double precision, dimension(rowA, colA), intent(in)    :: A
    double precision, dimension(rowB,colA-1), intent(out)  :: B
    integer                                                :: i, pos

    pos = 1
    do i = 1, size(A,1)
        if(A(i,2)==Period)then
            B(pos,1) = A(i,1)
    ! Transposing bigDF and out is important for better
    ! data access in this line
            B(pos,2:) = A(i,3:)
            pos = pos+1
        end if
    end do
end subroutine subsetPeriod

end module smearingmodule


Dynamically convert property to different data type The code finds marked attributes and parses them according to the attribute type.

Is it possible to simplify this logic? Is there generic way to do it? Please suggest some way to optimise this code. All the data type of Product class will be string. I'm getting product input as XML directly converting serialized data to a class with decimal, int, float will not give proper error message.

If there is list of items it will throw error in XMl and we won't know which row has caused the error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace TestSolution
{
    public interface ICustomParser
    {
        bool Parse(string input);
    }

    public class DecimalParserAttribute : Attribute, ICustomParser
    {
        public bool Parse(string input)
        {
            if (input == null) return false;

            decimal decimalValue;

            return decimal.TryParse(input, out decimalValue);
        }
    }

    public class intParserAttribute : Attribute, ICustomParser
    {
        public bool Parse(string input)
        {
            if (input == null) return false;

            int intValue;

            return int.TryParse(input, out intValue);
        }
    }

    public class Product
    {
        [DecimalParser]
        public string Weight { get; set; }

        [intParser]
        public string NoOfItems { get; set; }

        [intParser]
        public string GRodes { get; set; }

        [intParser]
        public string WRodes { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {

            var sb = Validate(new Product() { NoOfItems = "1", GRodes = "4", Weight = "5", WRodes = "23" });

            Console.WriteLine(sb);
            sb = Validate(new Product() { NoOfItems = "1", GRodes = "4w", Weight = "5", WRodes = "23" });

            Console.WriteLine(sb);
            Console.ReadKey();
        }

        private static string Validate(Product product)
        {
            var sb = new StringBuilder();

            foreach (var property in product.GetType().GetProperties())
            {
                var value = Convert.ToString(property.GetValue(product, null));

                var sel = property.GetAttribute<ICustomParser>();

                if (sel == null) continue;

                var parserinstance = (ICustomParser)sel;

                if (parserinstance.Parse(value)) continue;

                sb.AppendLine(string.Format("{0} Has invalid value", property.Name));
            }
            return sb.ToString();
        }
    }

    public static class Extensions
    {
        public static T GetAttribute<T>(this PropertyInfo property)
        {
            return (T)property.GetCustomAttributes(false).Where(s => s is T).FirstOrDefault();
        }
    }
}

I think you kind of reinventing the wheel a bit here. You could use the ValidationAttribute.

Since C# doesn't allow generics in Attribute classes you either have to create an attribute per type or use an enum to specific what type.  I personally prefer an attribute per type.

An Example of use the ValidationAttrubte for DecimalParser

public class DecimalParserAttribute : ValidationAttribute
{
    public DecimalParserAttribute() : base("Cannot convert to decimal")
    {
    }

    public override bool IsValid(object value)
    {
        if (value == null)
        {
            return false;
        }

        var stringValue = Convert.ToString(value);

        decimal decimalResult;
        return decimal.TryParse(stringValue, out decimalResult);
    }
}

Now to perform the validation you need to use the Validator Class

var p = new Product() {NoOfItems = "1", GRodes = "4w", Weight = "5", WRodes = "2q3"};
var validationResults = new Collection<ValidationResult>();
if (!Validator.TryValidateObject(p, new ValidationContext(p), validationResults, true))
{
    // If we are here there was validation errors
    var x = string.Join(Environment.NewLine,
        validationResults.SelectMany(r => r.MemberNames.Select(n => n + " " + r.ErrorMessage)));
}

This to me fits more in with what you are doing, validation, with the benefit of using the built in .Net Framework classes.


Data attribute text() swap on hover This is a very basic script. If the screen width is larger than say a tablet, and a portfolio link is hovered over, it will fade out the original text and replace it with the text held in the 'data-portfolio' attribute. I'm fairly new to jQuery so any improvements will be welcomed.

You can see it working here.

(function () {

width = $(window).width();

if(width >= 620) {
    $('.portfolio-piece__link').hover(function(){

    var $this = $(this);
        prev = $this.text();
        data = $this.attr('data-portfolio');

        $this.fadeOut(300, function(){
            $this.text(data).fadeIn(300);
        });

    }, function(){

    var $this = $(this);
        $this.fadeOut(300, function(){
            $this.text(prev).fadeIn(300);
        });
    });
}

})();

First off, you should clean up the indention. Currently the wrong indention makes the code hard to read.

The next problem is that your variables width, prev and data are lacking the var keyword making them global und thus making the surrounding IIFE pointless. Unfortunatly you are actually reading the prev variable in the second hover function globally. This will go especially wrong if you happen to have multiple elements with the class portfolio-piece__link one one page. You may want to move the definition of those variables outside of your hover functions and declare them locally inside the IIFE. That way it also has the advantage that you don't need to create them on each hover.

Then what you need to consider is your code will not react to window resizes. What happens if the user originally opens your page with a small window size (< 640) and then resizes the window. BTW, why do you want this effect only on larger window sizes in the first place?

Finally you may want to consider writing this to be generic and not hard code it to only work with that specific class and property name.

EDIT:
Moving the variables outside the hover functions is of course not sensible, if you have multiple elements. I wrote that before I realized that. Instead you should store the old text, for example using jQuery's data method. Example:

function replaceText($element, text) {
   element.fadeOut(300, function(){
        $this.text(text).fadeIn(300);
    });
}

$('.portfolio-piece__link').each(function() {
  // Store original text
  var $this = $(this);
  $this.data('original-text', $this.text());
}).hover(function(){
    var $this = $(this);
    var data = $this.attr('data-portfolio');

    replaceText($this, data);
}, function(){
    var $this = $(this);
    var text = $this.data('original-text');

    replaceText($this, text);
});

BTW, I forgot one thing originally: You have duplicate code for replacing the text. You should replace it with a method as I did above.


Minimum number of moves for a knight The problem is to find the minimum number of moves that a knight will take to go from one square to another on a 'n' cross 'n' chessboard. The code below is based on backtracking. It works well until n equals 5 but from n equals 6 the time limit is exceeded on ideone. How can I make the code more efficient?

#include <stdio.h>

int minimum(int a, int b)
{
    /* Returns the minimum of two numbers */
    if (a <= b) {
        return a;
    } else {
        return b;
    }
}

int minmoves(int r1, int c1, int r2, int c2, int array[10][10], int n)
{
    int moves;

    /*
     * If the place is already occupied dont go to it otherwise it will
     * lead to a infinite loop so we return a large value so that the
     * knight does not take this path
     */
    if (array[r2][c2] == 1) {
        return 32764;
    }

    /* We cannot go outside the board */
    if ((r2 < 0) || (r2 > n - 1) || (c2 < 0) || (c2 > n - 1)) {
        return 32764;
    }

    /* Condition to check if the path is complete */
    if ((r1 == r2) && (c1 == c2)) {
        return 0;
    }

    /* We add the point with coordinates r2, c2 to the path */
    array[r2][c2] = 1;

    /* All the possible moves that a knight can make */
    moves = 1 + minmoves(r1, c1, r2 - 2, c2 - 1, array);
    moves = minimum(moves, 1 + minmoves(r1, c1, r2 - 2, c2 + 1, array));
    moves = minimum(moves, 1 + minmoves(r1, c1, r2 - 1, c2 - 2, array));
    moves = minimum(moves, 1 + minmoves(r1, c1, r2 - 1, c2 + 2, array));
    moves = minimum(moves, 1 + minmoves(r1, c1, r2 + 1, c2 - 2, array));
    moves = minimum(moves, 1 + minmoves(r1, c1, r2 + 1, c2 + 2, array));
    moves = minimum(moves, 1 + minmoves(r1, c1, r2 + 2, c2 - 1, array));
    moves = minimum(moves, 1 + minmoves(r1, c1, r2 + 2, c2 + 1, array));

    array[r2][c2] = 0;

    return moves;
}

int main()
{
    int r1, c1, r2, c2, x, i, j, n;
    int array[10][10];

    scanf("%d\n%d\n%d\n%d\n%d", &r1, &c1, &r2, &c2, &n);

    /* All the elements of array are initialised to zero */
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            array[i][j] = 0;
            printf("0 ");
        }
    }

    x = minmoves(r1, c1, r2, c2, array, n);

    printf("%d",x);
    return 0;
}

I will answer an isomorphous question of finding a minimal value in a binary tree. In your case, the tree is octal, but the same rules apply.

Consider that you have

typedef struct node {
     struct node *pLeft, *pRight;
     int value;
} node_t;

The tree is completely unsorted, and you need to find the minimal value. What you are doing is something like this:

int minval(node_t *pRoot)
{
    if (pRoot == NULL)
        return INT_MAX;

    int min = pRoot->value;

    min = minimum(min, minval(pRoot->pLeft));
    min = minimum(min, minval(pRoot->pRight));

    return min;
}

This is a recursive algorithm. It's elegant, nice, cool and bloody slow.
Recursion is slow because every time you call a function it has to do all sorts of staging (pass arguments, call function, allocate stack space), and then repeat all that on return (clean up stack, return from call, clean up arguments).

There are non recursive algorithms to most problems, but there's also a nice way to get rid of all the costly staging for calling a function. (inlining a function does that too, but it is not possible for recursive functions). Let us take a look at code that does exactly the same thing as above, but without recursion:

int minval(node_t *pRoot)
{
    if (pRoot == NULL)
        return INT_MAX;

    node_t *stack[100]; // At least max tree depth*2, in your case n*8
    int sp = 0;
    int min = INT_MAX;

    stack[sp++] = pRoot;
    while (sp > 0)
    {
        node_t *p = stack[--sp];
        min = minimum(min, p->value);

        if (p->pLeft  != NULL) stack[sp++] = p->pLeft;
        if (p->pRight != NULL) stack[sp++] = p->pRight;
    }

    return min;
}

Here we have a simulated program stack, but we don't do so much with it, we only add one pointer for every tree node, which in your case is 8^5, or 32768. Compared to that many function calls.

If you modify your program to not actually call the function recursively, you should gain a very significant speed boost. Using the template I provided, it shouldn't be a problem, you'll just have 8 things to put on the stack every iteration.

Fun fact: What you are doing is known as a preorder tree traversal, also known as a depth-first search. If you were to do a level order traversal (also known as a breadth-first search), the code would go faster. The difference is that DFS will look at all paths, while you can stop BFS early as soon as it finds one.


Convert data into dictionary I am trying to convert "Random = 1" into dictionary {"Random":1}
I tried

Key = (line.split('=')[0]).split()[0] #to get key and remove spaces
value = (line.split('=')[1]).split()[0]
CQ_Options[Key] = value

How could I do it in more mature way?

First off, you should standardize your names, Key and value don't follow the same naming rules, or if they do, they're not apparent to me.
And so I'd recommend that you name your things in lower_snake_case, to adhere to PEP8.

After this I'd use str.strip rather than str.split.
And use a list comprehension to change both the key and value without having to duplicate the code, which can remove the key and value variables altogether.
This can leave you with:

item = [i.strip() for i in line.split('=')]
CQ_options[item[0]] = item[1]

If you want to stick with using key and value however, you need to make sure that line.split('=') will only return two items. You can make sure it'll return a max of two items with the optional parameter, and since we'd already error if there's not at least a single =, then you can use:

key, value = [i.strip() for i in line.split('=', 1)]
CQ_options[key] = value


See if a specific resource exists in a Kubernetes namespace I want to have a function that checks to see if a certain resource (deployment, job, cronjob etc) exist in a namespace. Using the Kubernetes Python client, I can create a boolean function (using deployments as a concrete example).

from kubernetes import client, config

def deployment_exists(name, namespace="default"):
    config.load_kube_config()
    client = client.AppsV1Api()
    try:
        client.read_namespaced_deployment(name, namespace)
        return True
    except ApiException as e:
        if e.status == 404:
            return False
    return False

Is there a way to achieve the same without relying on the ApiException explicitly?

Without knowing more about the kubernetes API that's hard to say, maybe you'd be more lucky on StackOverflow instead.

That said, if there's no explicit function to check, probably not.

Especially the status code 404 sounds like it would probably mean "not present", so this doesn't seem overly bad actually.

Is it intentional that all other ApiExceptions also return false?  If so, than there's no point to check for the 404.  If not, they should be thrown again.


Stack implementation in C# I started to learn data structures. How can I improve my implementation? For example, I don't now how to push 0 or how to realize my own resize.

public class Stack: IStack
{
    private int[] s;

    public Stack(int N)
    {
        s = new int[N];
    }

    public void push(int x)
    {
        int i = 0;
        while (s[i] != 0)
        {
            ++i;
        }

        if (i+1 >= s.Length)
        {
            Array.Resize(ref s, s.Length*2);
        }

        s[i] = x;
    }

    public int pop()
    {
        int i = 0;

        while (i != s.Length && s[i] != 0)
        {
            i++;
        }

        s[i] = 0;
        return s[i - 1];
    }
}

There are some problems with your code, especially

Naming

Names like s, N or x won't tell much about what they are used for. So maby slot, size and value would be a better fit.
It is not possible to push a 0 to the stack
The Stack is limited to only integers. If you want to push a decimal or a SomeClass you just can't.
Conventions

The default naming conventions for C# is to use PascalCase casing for naming methods, so push and pop should be Push and Pop.

Read: Naming guidelines for NET
You really should take care of the index of the last inserted value. This makes it easier and better to maintain if you need to pop or push.


K-Means clustering in Python2 I've implemented the K-Means clustering algorithm in Python2, and I wanted to know what remarks you guys could make regarding my code. I've included a small test set with 2D-vectors and 2 classes, but it works with higher dimensions and more classes. Here, it should sort all the elements starting with the same letters in the same classes (except ak, with is quite in between).

What I'm interested in:

Did I do something unpythonic?
Are there unclear or unnecessary parts?
What changes could drastically improve the code's performance?

from __future__ import print_function
from operator import add, sub
from _collections import defaultdict
import random as rd
import math

class Element:
    def __init__(self, idf, data, cls='ukn'):
        self.idf = idf
        self.data = data
        self.cls = cls

    def __str__(self):
        return '%s: %s -> %s' % (str(self.idf), str(self.data), str(self.cls))

    def eucl_dist_to(self, other):
        diff = map(sub, self.data, other.data)
        norm2 = math.sqrt(sum([math.pow(coord, 2) for coord in diff]))
        return norm2

def main():
    test_data_tuple = [('aa', 1, 7), ('ab', 1, 8), ('ac', 1, 9), ('ba', 2, 1), ('bb', 2, 3), ('ad', 2, 8), ('ae', 2, 9),
                   ('bc', 3, 2), ('bd', 3, 4), ('af', 3, 7), ('be', 4, 2), ('bf', 4, 3), ('ag', 4, 8),
                   ('bg', 5, 1), ('bh', 5, 3), ('ah', 5, 6), ('bi', 6, 2), ('bj', 6, 4), ('ai', 6, 6), ('aj', 6, 7),
                   ('bk', 7, 0), ('bl', 7, 2), ('bm', 7, 3), ('ak', 7, 5)]
    test_data_elt = list()
    for t in test_data_tuple:
        test_data_elt.append(Element(t[0], tuple(t[1:])))
    # test_data_elt.sort(key=lambda e: e.idf)
    results = k_means(test_data_elt, 2, 5)

def k_means(elts, nb_classes, nb_steps):
    k = nb_classes
    #init
    centroids = list()
    cls_content = defaultdict(list)
    for cls_nb in range(k):
        cls_name = 'class_'+str(cls_nb)
        centroids.append(Element(cls_name, rd.choice(elts).data, cls='centroid'))
        cls_content[cls_name] = list()

    for itr in range(nb_steps):
        # assign
        cls_content.clear()
        for elt in elts:
            min_dist = float('inf')
            for c in centroids:
                dist = c.eucl_dist_to(elt)
                if dist < min_dist:
                    min_dist = dist
                    best_class = c.idf
            cls_content[best_class].append(elt)
            elt.cls = best_class
        # adjust
        for c in centroids:
            elts_in = list(cls_content[c.idf])
            if len(elts_in) == 0:
                sum_elts_data = 0
            else:
                sum_elts_data = list(elts_in[0].data)
                for elt in elts_in[1:]:
                    sum_elts_data = map(add, sum_elts_data, elt.data)
                sum_elts_data[:] = [e / float(len(elts_in)) for e in sum_elts_data]
                c.data = tuple(sum_elts_data)
        print('\nIteration %d' % itr)
        for c in centroids:
            cls_name = c.idf
            print('\n'+str(c))
            for e in cls_content[cls_name]:
                print(e)
    return cls_content

if __name__ == '__main__':
    main()

something unpythonic?

Consider using plural identifiers: test_data_tuples, test_data_elts. Later, for example, you use a perfectly lovely for elt in elts: idiom.

In Element __init__, I wouldn't mind seeing cls='unknown' spelled out, but I am really missing a docstring that explains what the heck idf stands for. Also, as near as I can tell, data really is a coord, e.g. (x, y).

Kudos on k = nb_classes. I feel you did a good job of making the public API very clear, and then within the body k is conventional.

I can't say I'm a fan of import random as rd. It needlessly obscured the fairly obvious meaning of rd.choice(elts).data.

The for itr ... loop exhibits admirable clarity.

Testing if len(elts_in) == 0:, plus the map, seems like it's maybe a bit clunky. I'm just not understanding why empty is a special case, perhaps we could use sum and lambda, or a helper function? It seems like we could always find something sensible to assign to c.data, even in the empty case. Or maybe what I'm really looking for is an if that rapidly moves on to a continue.

The print('\n'+str(c)) is not very pythonic. I'd prefer print('\n%s' % c). Ok, that's the last of the tiny nits I will pick.

Overall, it looks like fairly solid code.

  changes could drastically improve the code's performance?

Sorry, don't see anything obvious that would help.


Non blocking high performance message processor I have some kind of high thoughput message handler receiving ordered messages. Now the task is - adding monitoring stuff in it in order to recognize wrong input messages, more exactly: it must notify about missed messages and just ignore the stale ones (with smaller order).

Due to high thoughput - it mustn't block.

So it must:

not use locks
find missing messages
find wrong ordered messages

Let's have some long counter in message for ordering - here is my initial version:

public class OrderedNonBlockingProcessor implements YetAnotherMessageHandler {
    private static Logger log = Logger.getLogger(OrderedNonBlockingProcessor.class);

    private final AtomicLong messageCounter = new AtomicLong(0);

    @Override
    public boolean onSomeHighThroughputMessage(final YetAnotherMessage message) {
        final long expected = messageCounter.getAndSet(message.getCounter()) + 1;

        if (expected == message.getCounter()) {
            processBusinessStuff(message);
            return true;
        } else if (expected > message.getCounter()) {
            /* wrong message, attempt to restore the sequence to prevent an error on next good message
             * TODO: fix ABA problem here
             */
            messageCounter.compareAndSet(message.getCounter(), expected - 1);

            log.error(String.format("messaging system ordering bug: got stale message %s while expected %s!",
                    message.getCounter(), expected));

            // some other notifying stuff...
        } else if (expected < message.getCounter()) {
            log.error(String.format("got forward message %s while expected %s, missed: %s",
                    message.getCounter(), expected, message.getCounter() - expected));

            // some other notifying stuff...
        }
        return false;
    }

    private void processBusinessStuff(YetAnotherMessage message) {
        log.info(String.format("process message %s", message.getCounter()));
        // some business logic...
    }
}

(Here is a junit for that)

How is it? Are there any races/concurrency issues I missed?

P.S. Next step is fixing ABA problem there.  I'm thinking about AtomicStampedReference[Long] for that.

I think that there is a certain amount of fuzziness to the question but I'll make a couple of suggestions and perhaps they will help.

First of all, I would recommend giving serious consideration to using Akka actors (http://akka.io/). Actors are a very straightforward way to handle multi-threaded processing without locks.

Just to be clear, I think that you are saying that the program receives messages that have an order but does not necessarily receive them IN order and so it needs to restore the correct order before they can be processed.

If this is true, then I would recommend using TWO actors. The first actor would get the messages from wherever they are coming from and dealing with putting them into the correct order. I assume that it would be ok to cache out-of-order messages until it finds the next message to be processed, perhaps on a priority heap.

Once the first actor finds the next message to be processed, it would send it on to the second actor who would then handle the actual processing (by calling processBusinessStuff in your example).


Slot-machine balance counter I'm very new to programming and I decided to start my journey with Python. It's my fifth day of learning and decided to try myself out and make a very simple slot machine. I was stuck for hours because I had issues with the balance counter but finally managed to make it work.
Would you please rate it or give your opinion on where the code could have been simpler? Many thanks!
import random
global a
a = [1000]
global bet
bet = 20
# This is the user's wallet, the list "a" contains the balance

def win1():
    # I did different wins, the win1,2,3 function contains them
    print("WIN!WIN!WIN! \n You won " , bet * 2 , "credits! ")
    a.append(a[-1] + (bet * 2))
    print("Actual balance : ", a[-1])

def win2():
    print("WIN!WIN!WIN! \n You won " , bet * 4 , "credits! ")
    a.append(a[-1] + (bet * 4))
    print("Actual balance : ", a[-1])

def win3():
    print("WIN!WIN!WIN! \n You won " , bet * 6 , "credits! ")
    a.append(a[-1] + (bet * 6))
    print("Actual balance : ", a[-1])

def reels():
    # Here is the random number calculation.As you can see, theres a pretty good chance to win so it
    # would not be a good casino game for the house :)
    reel_1 = random.randint(1,3)
    reel_2 = random.randint(1,3)
    reel_3 = random.randint(1,3)

    if reel_1 + reel_2 + reel_3 == 9 :
        print("Jackpot!!!!!!!!!!")
        a.append(a[-1] + bet * 100)
        print ("Actual balance : " , a[-1])

    elif reel_1 + reel_2 + reel_3 == 8:
        win3()

    elif  reel_1 + reel_2 + reel_3 == 7:
        win2()

    elif  reel_1 + reel_2 + reel_3 == 6:
        win1()

    else :
        print("No win this time")

def spin() :

    # And this is the main function, the spin

    spin1 = input("Balance : "+ str(a[-1])+ " Press enter to spin the wheel ")

    if spin1 == (""):
        print("****************************THE WHEEL IS SPINNING**************************** \n "
              "")
    else :
        print("You can only spin the wheel with enter")
        spin()

    a.append(a[-1] - bet)
    print("Balance : " , (a[-1]))
    reels() , spin()

spin()

All your win1, win2 and win3 functions are essentially the same. When you have multiple functions/pieces of code that are nearly identical, look at what parts of them are the same, and what parts are different. Make the identical parts the body of a function, and the differing parts parameters of the function.
In the case of those three functions, they're all the same; except for the bonus factor. Simply make that a parameter:
def win(bonus_factor):
    amount_won = bet * bonus_factor
    # I did different wins, the win1,2,3 function contains them
    print("WIN!WIN!WIN! \n You won " , amount_won, "credits! ")
    a.append(a[-1] + amount_won)
    print("Actual balance : ", a[-1])

Then change the callsite to use the same function:
elif reel_1 + reel_2 + reel_3 == 8:
    win(6)

elif  reel_1 + reel_2 + reel_3 == 7:
    win(4)

elif  reel_1 + reel_2 + reel_3 == 6:
    win(2)

And better yet, the 6, 4 and 2 should be extracted out into constants at the top of the file so they're named and easily modifiable.
That reduced the duplication of those three functions, but there's multiple things that need to be said about a:

It's a terrible name. The name you associate with an object should be descriptive and let readers know its purpose. Which makes more sense to you?:
a.append(a[-1] + (bet * 4))
balance_history.append(balance_history[-1] + (bet * 4))

You'll find that overwhelmingly, the latter will be easier to understand. Not only is a completely non-descript, but it's also so simple that you can't search for usages of it using simple "find" tools, since a is such a common letter.

It's not necessary. If you do a search for usages of a, you'll see that it's only used for two things: a[-1], and a.append. While there's something to be said about anticipating future needs, you're currently accumulating a history of balances that you never use. This not only uses up more memory than is needed, but it also convolutes the logic. If you're maintaining a history, that suggests that the history is used somewhere. It never is though. You could safely change a to balance = 1000, then just do simple math on it directly. That would make a lot of this code cleaner and more sensical.

Speaking of memory usage, you're using recursion for basic looping, which is not a good thing. Your spin function is essentially:
def spin():
    # Logic
    reels(), spin()

Every single time your player takes another turn, spin is allocating another stack frame, and taking up more memory. If your player were to take ~1000 turns, your program would crash due to the stack being exhausted. Change that function to a basic while loop:
def spin():
    global balance
    while True:
        # And this is the main function, the spin
        spin1 = input("Balance : " + str(balance) + " Press enter to spin the wheel ")

        if spin1 == "":
            print("****************************THE WHEEL IS SPINNING**************************** \n")
            balance -= bet
            print("Balance : ", balance)
            reels()
        else:
            print("You can only spin the wheel with enter")

A few changes I made:

I changes balance to be a simple integer and discussed above (notice how much more sense balance -= bet makes).
It's using a while loop. This does not eat up memory as the program is used. Recursion should be reserved for the very specific cases where it's the proper choice, like when iterating a recursive structure. Especially in a language like Python, recursion should be avoided unless you're very familiar with it and know about its strengths/weaknesses.
I rearranged the if branches. You originally had it so if if spin1 == "": was true, all it would do is print; then the actual functionality happened at the bottom of the function. I think it makes much more sense as I have it here; where everything is grouped.
I did a lot of small touchups (more on that below).

You're making heavy use of global variables. This is quite common when starting out, but it's a habit you should work to fix quickly. General rules of thumb to follow unless you have good reason to do otherwise:

All the changing data that a function requires to operate (balance, for example) should be explicitly passed to the function as arguments. This means you can test the function by simply passing it data, and you don't need to alter global state. This isn't a big deal now, but needing to alter mutable globals to test a function will become a disaster once your function is larger.
All data that the function produces should be returned from the function. Again, this means you can test the function all you want without affecting any other code.

As an alternative, you may also find that creating a class to maintain a state is a cleaner choice as well. That's getting a little more advanced though.

I'm going to show the full final code below, but I needed to make many small fixes to make it clean. Please be aware of formatting. Yes, it is very important. You have many places in your code where care was not taken to make it consistent and readable:
elif reel_1 + reel_2 + reel_3 == 8:
    win3()

elif  reel_1 + reel_2 + reel_3 == 7:  # Too many spaces after the elif
    win2()

"Balance : "+ str(a[-1])+ " Press enter to spin the wheel "  # Inconsistent spacing around +

def spin() :  # There shouldn't be a space before the colon

If you're going to write Python, you should spend an afternoon to read over PEP8 at least once. The look of your code reflects strongly on that care that was taken while writing it. You want you code to look sharp, and clean. You may have your own stylistic preferences, but be consistent when applying them, and when in doubt, stick to PEP8.

The final code:
import random

balance = 1000
bet = 20

def win(bonus_factor):
    global balance
    amount_won = bet * bonus_factor
    # I did different wins, the win1,2,3 function contains them
    print("WIN!WIN!WIN! \n You won ", amount_won, "credits! ")
    balance += amount_won
    print("Actual balance : ", balance)

def reels():
    global balance
    # Here is the random number calculation.As you can see, theres a pretty good chance to win so it
    # would not be a good casino game for the house :)
    reel_1 = random.randint(1, 3)
    reel_2 = random.randint(1, 3)
    reel_3 = random.randint(1, 3)

    if reel_1 + reel_2 + reel_3 == 9:
        print("Jackpot!!!!!!!!!!")
        balance += bet * 100
        print("Actual balance : ", balance)

    elif reel_1 + reel_2 + reel_3 == 8:
        win(6)

    elif reel_1 + reel_2 + reel_3 == 7:
        win(4)

    elif reel_1 + reel_2 + reel_3 == 6:
        win(2)
    else:
        print("No win this time")

def spin():
    global balance
    while True:
        # And this is the main function, the spin
        spin1 = input("Balance : " + str(balance) + " Press enter to spin the wheel ")

        if spin1 == "":
            print("****************************THE WHEEL IS SPINNING**************************** \n")
            balance -= bet
            print("Balance : ", balance)
            reels()
        else:
            print("You can only spin the wheel with enter")

spin()


Construct KD-Tree in Java I want to construct KD-Tree from unsorted List of points in the plane (i.e. 2-KD-Tree). To do so I used the method from Wikipedia: http://en.wikipedia.org/wiki/Kd-tree#Construction  This is my code:

public static Node buildKDTree(List<Node> list, boolean depth){
    if(depth){
        Collections.sort(list, Node.xComparator);
    }
    else{
        Collections.sort(list, Node.yComparator);
    }
    int middle = list.size()/2;
    Node root = list.get(middle);
    List<Node> left = new ArrayList<Node>();
    for (int i = 0; i < middle; i++) {
        left.add(list.get(i));
    }
    List<Node> right = new ArrayList<Node>();
    for (int i = middle+1; i < list.size(); i++) {
        right.add(list.get(i));
    }
    root.leftChild = buildKDTree(left, !depth);
    depth = !depth;
    root.rightChild = buildKDTree(right, !depth);
    return root;
}

The implementation of Node class is not impotrant (I think). What I dont like in my code is that in each iteration I spend O(n) time to copy the first half of the sorted list into new list (left) and then do the same for the other half of the list(right). Is there any way to avoid this? I was thinking to include indexes begin and end as parameters to my method but I dont see how it would speed up, since I dont know method for collectios that will sort the collection partly, i.e. from begin to middle and then from middle to end.

List.subList(low, high)

Is probably the easiest approach to take to the list management without requiring the copying of a lot of nodes.  It does make a lot of Lists though, which are very short lived.  This should be fine.  If you really needed to keep things under control, then you would replace the sublist implementation with one that uses Arrays.sort(a[], low, high, Comparator)

Reviewing the code as is:

root.leftChild = buildKDTree(left, !depth);
depth = !depth;
root.rightChild = buildKDTree(right, !depth);

This looks to me like a bug - I would expect each layer of the tree to be sorted in the same direction, so flipping the bit doesn't make sense.  Instead

root.leftChild = buildKDTree(left, !depth);
root.rightChild = buildKDTree(right, !depth);

I don't like using the boolean to control the direction of the insert -- the variable is answering a question about direction, not logical truth.  An enum would be a more readable fit

private enum SORT_DIRECTION {
    X_SORT, Y_SORT;
}

Alternatively, it might be clearer to track how deep you are in the tree, and choose the direction of the sort based on the depth.  In that case, depth would be an int, and you would use something like a modulus operation to choose which way you want the comparators to go.

Comparator<Node> order = Node.xComparator;

if(0 == (depth % 2){
    order = Node.yComparator;
    Collections.sort(list, Node.xComparator);
}
Arrays.sort(nodes, low, high, order);

Or better, explicitly spell out what you are doing

Comparator order = getOrderForDepth(Node, depth);
   Arrays.sort(nodes, low, high, order);

When dealing with recursion, my usual recommendation is to have a different function set up your pre conditions, and return the root.

Node buildKDTree(List list) {
       int depth = 0;
       return buildKDTree(list, depth);
   }

Finally, when implementing something recursively, make sure that you have a clear and obvious terminating condition (in other words, you need to handle the case where there are no more sub trees).  In this case, it's probably easiest to manage if the buildKDTree method handles an empty List.