Parsing a CSV file with Python My goal is to parse this CSV data:

Time,Tank,Product,Volume,TC Vol,Ullage,90 Ul,Height,Water,Temp
2017-10-19T18:52:41.118408,1,UNLEADED,4406,4393,7221,6058.3,37.49,0,64.15
2017-10-19T18:52:41.118408,3,SUPER,8317,8278,3310,2147.3,61.4,0,66.74
2017-10-19T18:52:41.118408,4,ADSL2,6807,6774,4820,3657.3,51.98,0,70.46
2017-10-19T18:53:13.894066,1,UNLEADED,4406,4393,7221,6058.3,37.49,0,64.15
2017-10-19T18:53:13.894066,3,SUPER,8313,8273,3314,2151.3,61.37,0,66.74
2017-10-19T18:53:13.894066,4,ADSL2,6805,6772,4822,3659.3,51.97,0,70.46

Given the a list of Tank numbers, a primary key, and a data column:

>>>tank_numbers = [1, 3]
>>>primary_key = 'Time'
>>>data_column = 'Volume'
>>>
>>>parse_csv('csv_file.csv', tank_numbers, primary_key, data_column)

[
  {'Time': '2017-10-19T18:52:41.118408', 'UNLEADED': '4406', 'SUPER': '8317'}
  {'Time': '2017-10-19T18:53:13.894066', 'UNLEADED': '4406', 'SUPER': '8317'}
]

I have a few questions about the following code;

Using only the standard library, is there a more simple way?
What I have seems like too much just to get the needed information.
Should I be breaking up the parse_csv function into smaller pieces similar
to _parse_csv_to_dicts and _get_tank_names.

import csv

def _parse_csv_to_dicts(file):
    with open(file, 'r') as f:
        return list(csv.DictReader(f))

def _get_tank_names(tanks=None, data=None):
    names = list()
    for n in tanks:
        for tank_dict in data:
            if tank_dict['Tank'] == str(n):
                names.append(tank_dict['Product'])
                break
    return names

def parse_csv(file, tanks, primary_key, data_key):
    """
    :param file: The raw csv data file
    :param tanks: A list of tank numbers, as labeled in the raw csv
    :param key: a list of the keys needed from the raw csv
    :return: a list of dictionaries
    """
    d1 = _parse_csv_to_dicts(file)

    # Remove unneeded tanks from data
    d2 = [row for row in d1 if int(row['Tank']) in tanks]

    # Remove unneeded keys from rows
    keys = [primary_key, data_key, 'Product']
    d3 = [{key:row[key] for key in keys} for row in d2]

    # Create new row template
    tank_names = _get_tank_names(tanks=tanks, data=d1)
    row_template = {key:None for key in (tank_names + [primary_key])}

    # New rows from row template
    d4 = []
    for row in d3:
        # update new row with available keys
        new_row = {key:row.get(key) for key in row_template}

        # update new row with matching values
        for key in new_row:
            if key in row.values():
                new_row[key] = row[data_key]

        # remove keys with None value
        new_row = {k:v for k,v in new_row.items() if v is not None}
        d4.append(new_row)

    # Merge all rows based on Time key
    merged = {}
    for row in d4:
        if row[primary_key] in merged:
            merged[row[primary_key]].update(row)
        else:
            merged[row[primary_key]] = row

    return [value for value in merged.values()]

In general I think this code looks excellent.  You have made good use of comprehensions.  I have two suggestions:

More Comprehensions:

This:

def _get_tank_names(tanks=None, data=None):
    names = list()
    for n in tanks:
        for tank_dict in data:
            if tank_dict['Tank'] == str(n):
                names.append(tank_dict['Product'])
                break
return names

Can be expressed as:

def _get_tank_names(tanks=None, data=None):
    tanks = {str(n) for n in tanks}
    return {t['Product'] for t in data if t['Tank'] in tanks}

This converts tanks to a set while performing the str() conversion, to clean things up a bit.  Then the result can be generated in a single statement.

Gathering like elements

So this entire section:

# Create new row template
tank_names = _get_tank_names(tanks=tanks, data=d1)
row_template = {key: None for key in (tank_names + [primary_key])}

# New rows from row template
d4 = []
for row in d3:
    # update new row with available keys
    new_row = {key: row.get(key) for key in row_template}

    # update new row with matching values
    for key in new_row:
        if key in row.values():
            new_row[key] = row[data_key]

    # remove keys with None value
    new_row = {k: v for k, v in new_row.items() if v is not None}
    d4.append(new_row)

# Merge all rows based on Time key
merged = {}
for row in d4:
    if row[primary_key] in merged:
        merged[row[primary_key]].update(row)
    else:
        merged[row[primary_key]] = row

return [value for value in merged.values()]

Can be reduced down to:

merged = {}
for row in d3:
    merged.setdefault(row['Time'], {'Time': row['Time']}) \
        [row['Product']] = row['Volume']

return [v for k, v in sorted(merged.items())]

The cornerstone here is using dict.setdefault to accumulate the records that match the timestamp.

So how does the setdefault work?

We create an empty dict, the keys to which will be the timestamps we will be accumulating data for.

merged = {}

Loop through the data, a row at a time:

for row in d3:

Look up the element in our dict corresponding to the timestamp for this row:

merged.setdefault(row['Time'], {})

This is most of the magic, it will populate merged[this rows timestamp] with an empty dict if one is not already there, and then return the dict, so that we can put something in it.  An empty dict would often be sufficient, but in this case you also need the timestamp in the final dict, so we can extend the statement to be:

merged.setdefault(row['Time'], {'Time': row['Time']})

This will return a dict with the timestamp already populated, the first time we see that timestamp.  In subsequent loops when we see the timestamp, it will return the dict that was built the previous time we saw the timestamp.

To finish the dict creation code we set the Product and Volume data into the dict with:

merged.setdefault(row['Time'], {'Time': row['Time']}) \
    [row['Product']] = row['Volume']

Lastly, return the list of dicts we built, sorted by the timestamp with:

return [v for k, v in sorted(merged.items())]

Ain't Python fun? :-)


Detecting properly nested parenthesis using functional programming So I have this code that is my attempt at a coding test from Codility. While my code produces correct results according to the requirements, (which unfortunately are copyrighted so I don't think I can reproduce them here), but I feel like it could be better organized, and in some places use a more functional approach to solving the problem.

So the basic idea is that it tests if the string if its properly nested parenthesis, or can be split into 2 halves which are properly nested.

let rec isNested (s : System.String) =
    let s = s.ToCharArray()
    let s = List.ofArray s
    match s with
    | [] -> true //if its empty then its properly nested
    | _ ->
        match s.Length % 2 with
        //if its even split the list in to 2 halves and test them
        | 0 -> //test form VW

            let len = s.Length
            let half = len / 2
            let mutable firsthalf = []
            let mutable secondhalf = []
            for i in 0..half - 1 do
                firsthalf <- s.[i] :: firsthalf
            for i in half..len - 1 do
                secondhalf <- s.[i] :: secondhalf
            firsthalf <- List.rev firsthalf
            secondhalf <- List.rev secondhalf
            let VWtest =
                isNested (new string(Array.ofList (firsthalf))) && isNested (new string(Array.ofList (secondhalf)))
            match VWtest with
            | true -> true
            | false ->
                match (s.[0], s.[s.Length - 1]) with
                | ('(', ')') ->
                  //remove the first and last elements
                    let sublist =
                        s.Tail
                        |> List.rev
                        |> List.tail
                        |> List.rev
                    isNested (new string(Array.ofList (sublist)))
                | _ -> false
        | _ -> false //not a string with an even number of chars therefore can't be properly nested

let test = isNested >> Console.WriteLine
//let expect (b : bool) = Console.WriteLine(" expected ", b)
test "()"     // expect true
test ")("     // expect false
test "(())"   // expect true
test "()()"   // expect true
test "()(()"  // expect false
test "(()())" // expect true

The algorithm is overly complicated, and incorrect: for example, it will fail on the input (())().

  I feel like it could ... use a more functional approach to solving the problem.

There is a more functional approach, maintaining a count of how many parentheses are open.

Let's start with the function signature

let rec isNested' (parens : char list) (openParens : int) : bool =

And fill in the cases. If there are no parentheses left to process, and no open parentheses, they are properly nested.

    match parens, openParens with
    | [], 0 -> true

If we are processing a (, we increase the open parentheses count

    | '(' :: rest, _ -> isNested' rest (openParens + 1)

I'll leave the other two cases up to you.

For convenience, we add a wrapper for this function

let isNested (parens : string) = isNested' (List.ofSeq parens) 0


Unlocking spreadsheet files quickly - follow-up I recently posted a program I made to unlock spreadsheet files( see also ), I've taken all of your advice and made it more Ruby like, it has no shell commands in it, and uses FileUtils

I personally think it looks a whole lot cleaner and a whole lot more Ruby like. I would like a critique on my work so far, and would like some pointers if possible.

Source:

#!/usr/local/bin/ruby

require 'fileutils'

def spreadsheet_unlocker
    puts "Enter Folio # if unlocking multiple seperate with comma followed by space."
    folios = gets.chomp
    folios.split(', ').each do |input_folio|
        lockfile = "/my/dir.~lock.#{input_folio}.ods#"
        if input_folio =~ /^\d{7}/
            if File.exist?( lockfile )
                puts "Unlock file?"
                input = gets.chomp
                    if input =~ /y/i
                     FileUtils.rm( lockfile )
                     puts "File unlocked."
                else
                    puts "You went through that trouble for no reason..."
                end
            else
                puts <<-EDE.gsub(/^\s*>/, ' ')
                >
                >Lockfile not found for Folio # #{input_folio}"
                >
                >If the file name doesn't match the folio number, get the file name
                >and use that instead of the Folio number...
                >
                EDE
                spreadsheet_unlocker
            end
        else
            puts <<-EDF.gsub(/^\s*>/, ' ')
            >
            >What part of 'Folio #' is hard to understand? 7 DIGITS"
            >
            >If the file name doesn't match the folio number, get the file name
            >and use that instead of the Folio number...
            >
            EDF
            spreadsheet_unlocker
        end
    end
end
spreadsheet_unlocker

Example usage:

Enter Folio # if unlocking multiple seperate with comma followed by space.
1111111, 2222222, 3333333
Unlock file?
yes
File unlocked.
Unlock file?
yes
File unlocked.
Unlock file?
yes
File unlocked.

Enter Folio # if unlocking multiple seperate with comma followed by space.
2345678

 Lockfile not found for Folio # 2345678"

 If the file name doesn't match the folio number, get the file name
 and use that instead of the Folio number...

Good job listening to feedback. Keep at it.

Use two spaces to indent.
Name your methods verbs, not nouns (e.g., unlock_spreadsheet).
Split on comma and then use strip to remove leading and trailing whitespace.
Rather than calling the method recursively, you may want to put a loop do around the method and then break when you're done.
If you run Rubocop, you'll see that your method is too long. Instead, you can move each set of actions inside of if and else blocks into their own methods.


Double integral solver in TI-84 Here's a program I made for estimating a double integral over a general region. It's pretty accurate, but it's VERY, VERY slow. (Getting an accurate enough result takes about 30 seconds)

Here's the code; the lines are indented just for readability.

ClrHome
Input "Surface: ",Str0
Str0→Y₈
Input "y-Bound 1: ",Str0
Str0→Y₉
Input "y-Bound 2: ",Str0
Str0→Y₀
Input "X1: ",A
Input "X2: ",B
Input "Num of intv: ",N
(B-A)/N→D
0→V
For(K,0,N-1
    A+KD+.5D→X
    V+fnInt(abs(Y₀-Y₉),X,A+KD,A+(K+1)D)fnInt(Y₈,Y,Y₀(X),Y₉(X))/(Y₀(X)-Y₉(X→V
End
DelVar DDelVar XDelVar YDelVar Str0
Disp "
Disp "Volume in R: ",V

Variables explained:

Str0: Arbitrary (input)
Y₈: Equation of surface f(x,y)
Y₉: Equation of one bound of region R
Y₀: Equation of other bound of region R
A: x-value of left bound of region R (input)
B: x-value of right bound of region R (input)
N: Number of Δx intervals (input)
D: Size of Δx interval
X: x-value of midpoint of Δx interval
V: Volume under the surface f(x,y) bound by the region R (output)

Basically, how I'm tackling it is I'm taking a slice of the region R on the x-y plane (Δx is the width) and finding its area (fnInt(abs(Y₀-Y₉),X,A+KD,A+(K+1)D)). Then I find the x-value that's the middle of that slice, and I find the average z-value along that x (fnInt(Y₈,Y,Y₀(X),Y₉(X))/(Y₀(X)-Y₉(X))), which I assume to be a reasonable estimate for the height of that entire slice of volume. Then I run a For( loop a bunch of times and add up all of the slices together to find the total volume.

But the problem is that I need lots of intervals (about 50-100) to get an accurate enough answer, which takes a ridiculous amount of time.

I already tried to optimize it a little by getting around all the expr('s I used to have, but it's still very very slow.

Any suggestions on optimizing?

Here's the important part:

0→V
For(K,0,N-1
    A+KD+.5D→X
    V+fnInt(abs(Y₀-Y₉),X,A+KD,A+(K+1)D)fnInt(Y₈,Y,Y₀(X),Y₉(X))/(Y₀(X)-Y₉(X→V
End

I suggest you replace the fourth line with:

V+DfnInt(Y₈,Y,Y₀,Y₉)→V

Here's the reason: You should have thin enough slices that the volume in a single slice of width D can be approximated by DfnInt(Y₈,Y,Y₀,Y₉); that's more important than getting the area exactly right. This change will increase your overall speed by about 3x.

Once you've done that, you can drop the For( loop entirely, and use sum(seq( instead. This only helps the performance a little; your bottleneck is still the fnInt(. However it's better style.

Another trick: since your precision is limited more by the Midpoint rule calculation along the x axis than the accurate fnInt( along the Y axis, you may want to increase the tolerance of fnInt( to, say, .005. Then your entire loop becomes:

Dsum(seq(fnInt(Y₈,Y,Y₀,Y₉,.005),X,A+.5D,B,D→V

(This uses the optional 'step' argument to seq(.

Another possibility is to use the Midpoint rule on both axes instead of fnInt(. I'm not sure how much this would help speed or decrease accuracy, but I suspect the accuracy wouldn't be too much worse than using the Midpoint rule on one axis.


Having initialization code certain to run only once I have this static class in an ASP.NET MVC project:

  public static class Setup
  {
    private static bool _intialized = false;

    public static void Initialize(IWindsorContainer Container)
    {
      //Check if it has already run
      if (_intialized)
      { return; }

      var connectionString = config.ConnectionStrings["ccsMongo"].ConnectionString;
      var dbName = config.AppSettings["ccsMongoDb"];

      //Initialization code here.
      Container.Register(
          Component.For<IOrgRepos>().ImplementedBy<OrgRepos>()
            .DependsOn(new { ConnectionString = connectionString, DatabaseName = dbName}),
          Component.For<IIndividualRepos>().ImplementedBy<IndividualRepos>()
            .DependsOn(new { ConnectionString = connectionString, DatabaseName = dbName}),
          Component.For<IScoreboardRepos>().ImplementedBy<ScoreboardRepos>()
            .DependsOn(new { ConnectionString = connectionString, DatabaseName = dbName})
        );

      //Mark as run
      _intialized = true;
    }
  }

I then include a call to it in my Application_Start.  The intent is to be sure that it will never run more than once.

Is there a better way to create a function that can't be run more than once in the the application lifetime?
Am I being paranoid?  I could just put a call to the initialization code into my Application_Start and forget about the "run only once" guard code.

I would be delighted to get any feedback.

This could execute more than once pretty easily if called on multiple threads at the same time. This should make it a bit more foolproof:

  public static class Setup
  {
    private static readonly object _locker = new object();
    private static bool _intialized = false;

    public static void Initialize(IWindsorContainer Container)
    {
      lock (_locker)
      {
        //Check if it has already run
        if (_intialized)
        { return; }

        var connectionString = config.ConnectionStrings["ccsMongo"].ConnectionString;
        var dbName = config.AppSettings["ccsMongoDb"];

        //Initialization code here.
        Container.Register(
            Component.For<IOrgRepos>().ImplementedBy<OrgRepos>()
              .DependsOn(new { ConnectionString = connectionString, DatabaseName = dbName}),
            Component.For<IIndividualRepos>().ImplementedBy<IndividualRepos>()
              .DependsOn(new { ConnectionString = connectionString, DatabaseName = dbName}),
            Component.For<IScoreboardRepos>().ImplementedBy<ScoreboardRepos>()
              .DependsOn(new { ConnectionString = connectionString, DatabaseName = dbName})
          );

        //Mark as run
        _intialized = true;
      }
    }
  }


Reading a file line by line - Two tokens per entry Problem

Read a file and capture two tokens for every multi-line entry.

First token is hexcode with which the entry starts.

Second token is the remaining line(multi-line) of the entry. Trailing backslash indicates the continuation of line.

Typical entries of a file is as shown below:

0x0512ff01 R 5330 R XY.Zion, \
        "parseexp( {f 7},{S \"OWAUTO\"})", "0x0222ff02 -:-", \
        "parseexp( {g 7},{S \"TWFAB\"})", "0x0222ff02 -:-",  \
        "setfault", "0x0222ff03 -:-"
# whatever
0x0436ff02 I 5330  R XY.Yion, " \
                    ! ( strcat( {H 19.19.121.180}, {attr 0x11137f })  \
                            "default", "0x0222ff03 -:-"
0x0236ff03 G 50
0x0588ff03 H 49
0x0777ff03 R 34

There are such ~100 files to read

Solution

Input to this function is a generator that provides path, one at a time

def get_tokens(file_paths):

    for path in file_paths:
        ruleCapture = False
        with open(path) as f:

            for line in f:

                line=line.strip() # remove \n and other spaces
                if line.startswith('#'): # comments
                    continue
                elif (not line.endswith('\\')) and (ruleCapture == False):
                    event_rule_match =  re.search(r'(?<!\\\n)^(0[xX][0-9a-fA-F]+)([^\n]*)',line)
                    if event_rule_match:
                        yield event_rule_match.group(1), event_rule_match.group(2)
                elif line.endswith('\\') and ruleCapture == False:
                    buffer = line[0:-2]
                    ruleCapture = True
                elif line.endswith('\\') and ruleCapture == True:
                    buffer = ' '.join([buffer, line[0:-2]])
                elif (not line.endswith('\\')) and ruleCapture == True:
                    buffer = ' '.join([buffer, line])
                    ruleCapture = False
                    event_rule_match =  re.search(r'(?<!\\\n)^(0[xX][0-9a-fA-F]+)([^\n]*)',buffer)
                    yield event_rule_match.group(1), event_rule_match.group(2)
                    buffer=''

1) Can this code avoid regex?

2) Can elif structure still be optimized?

3) Can this code avoid reading the file line by line? Instead use f.read() and compile regex once....

1) Control flow clarity

if a and b: ...
elif a and not b: ...
elif not a and b: ...
elif not a and not b: ...

can be improved to

if a:
    if b: ...
    else: ...
else:
    if b: ...
    else: ...

2) Remove unnecessary function calls

' '.join([a, b]) can be simplified to a + ' ' + b. In your case, since you're appending b, you can do a += ' ' + b.

3) Implicit slicing notation

line[0:-2] can be line[:-2].

4) Consolidate constants

The regex string is a constant and is used multiple times. It can be moved to the top as a constant (or even outside the function).

5) Remove unnecessary comments

Assume the reader is familiar with built-in functions like str.split(). Thus, # remove \n and other spaces is unnecessary.

6) Readability

Include exactly one space before and after an assignment. e.g line = line.strip().
No need for if a == True or if a == False. Instead, do if a or if not a.
Remove unnecessary line breaks near the top.
Add spaces after commas in a function's parameters. e.g: f(a, b) instead of f(a,b).
Pick either camelCase or under_scores for variable names. Don't use both.

Final result:

def get_tokens(file_paths):
    REGEX = r'(?<!\\\n)^(0[xX][0-9a-fA-F]+)([^\n]*)'

    for path in file_paths:
        rule_capture = False
        with open(path) as f:
            for line in f:
                line = line.strip()
                if line.startswith('#'): # ignore comments
                    continue

                if line.endswith('\\'):
                    if rule_capture:
                        buffer += ' ' + line[:-2]
                    else:
                        buffer = line[:-2]
                        rule_capture = True
                else:
                    if rule_capture:
                        buffer += ' ' + line
                        rule_capture = False
                        event_rule_match = re.search(REGEX, buffer)
                        yield event_rule_match.group(1), event_rule_match.group(2)
                        buffer = ''
                    else:
                        event_rule_match = re.search(REGEX, line)
                        if event_rule_match:
                            yield event_rule_match.group(1), event_rule_match.group(2)

Notice I didn't change any of the logic, but just moved things around or removed things. See if you can simplify the logic (e.g. removing unnecessary assignments, etc).


Checking if three numbers sum to a given number I'm working on the following interview practice problem, and got the following solution, which runs in worst case n! time (best case constant), and I'm trying to optimize it.

Can you offer any suggestions to go about this? I think it should be able to be reduced down to quadratic, if not nlogn, but I'm not entirely sure of an approach to achieve this.

/** Write a function to check if any three numbers in an array sum to a given number, X */
public class SumToNum {

  public static void main(String[] args) {
  int[] test = {1,8,2,3,11,4};
  System.out.println(threeSumTo(test, 6)); //Returns true

  }
  public static boolean threeSumTo(int[] array, int x) {
      List<Integer> items = new LinkedList<>();
      for (int i : array) { items.add(i); }
      return threeSumTo(items, x, 3);
  }
  public static boolean threeSumTo(List<Integer> items,int numRemaining, int count) {
      if (numRemaining == 0) {
          return true;
      }
      if (count == 0){
          return false;
      }
      for (int i = 0; i < items.size(); i++) {
          int curr = items.remove(i);
          if (curr <= numRemaining) {
              boolean result = threeSumTo(items, numRemaining - curr, count - 1);
              if (result) {
                  return result;
              }
          }
          items.add(i, curr);
      }
      return false;
  }

}

EDIT: Ok, here's a solution based on Rafa's advice, using some more clever deduction. Basically, I'm just reducing it down to two integers that sum up to a number, and doing this for every number in the array.  It looks like it works as intended, but in N2 worst case time this time.It also assumes the array is sorted, otherwise, as answered below, the problem is NP-Complete. Thanks for the help.

public static boolean threeSumTo(int[] array, int x) {
    for (int i = 0; i < array.length; i++) {
        boolean result = twoSumTo(array, x - array[i], i);
        if (result) {
            return result;
        }
    }
    return false;
}

private static boolean twoSumTo(int[] array, int x, int low) {
    int high = array.length - 1;
    while (low < high) {
        if (array[low] + array[high] == x) {
            return true;
        }
        if (array[low] + array[high] > x) {
            high--;
        } else {
            low++;
        }
    }
    return false;
}

This is a specialised instance of the Subset sum problem, which is NP-Complete in the general case.

Of course, when working with a small subset size that is known in advance, we can significantly cut down on the running time (at worst checking C{n, k} values, where n is the number of values in the search space and k is the size of the subset we're looking for).  The O(n^3) naive algorithm here is clearly much better than the O(n!) running time of your original algorithm. However, we can get this down to O(n^2 log n) pretty easily. Firstly, sort the original array:

private int[] values;

public Subset(int[] v)
{
    values = Arrays.copyOf(v, v.length);
    Arrays.sort(values);
}

Now, we can loop over each pair of indices in the array, and binary search for the difference between the calculated sum and the desired number:

public boolean sumsTo(int num)
{
    int sum = 0;
    for(int i = 0; i < values.length - 1; ++i) {
        for(int j = i+1;  j < values.length; ++j) {
            sum = values[i] + values[j];
            if(Arrays.binarySearch(values, num - sum) > 0) {
                return true;
            }
        }
    }

    return false;
}

In fact, with a bit more stuffing around, we can get this down to O(n^2). There is an approach to do so outlined here.


Monitoring errors with singleton I need to monitor my functions for "errors" and want to print the warnings at the end of the functions. I thought using singleton class could be a good idea in here (code inspired by this example):

class Warning
{
  bool status;
  std::string message;
  static Warning *global_warning_ptr;
  Warning() {
    defaults();
  }
public:
  void defaults() {
    message = "no warnings";
    status = false;
  }
  void set(std::string v) {
    message = v;
    status = true;
  }
  void print() {
    if (status) {
      std::cout << message std::endl;
      defaults();
    }
  }
  static Warning *msg() {
    if (!global_warning_ptr)
      global_warning_ptr = new Warning;
    return global_warning_ptr;
  }
};

Warning *Warning::global_warning_ptr;

that is used by multiple functions as below

void bar(double x) {
  if (x < 0)
    Warning::msg()->set("Wrong values");
}

void foo(std::vector x) {
  int n = x.size();

  for (int i = 0; i < n; i++) {
    bar(x[i]);
  }

  Warning::msg()->print(); // print single warning if any error occures
}

However using singletons is often considered as a bad coding practice. Is there any better alternative or place for improvment in my code? Is there anything I should worry about when using it?

I don't want to sound harsh, but the code is useless. On top of that, the singleton seems to be slightly off in this situation. You should have a function which is a friend to the class and has static local variable and then simply return reference to it. The class itself should have private constructor, so that only the specified function can instantiate it.

Why?

Because it is not thread safe
Because it is singleton
Because only one message is supported at a time
No real way to retrieve the message as string (companies will need a person who looks at the monitor)
Memory leak

Alternative:

CustomStream& debugStream()
{
    static CustomStream stream;
    return stream;
}

where CustomStream is thread safe stream possibly inhertied from std::ostream. Do note that static variables are initialized in a thread safe way only from C++11 and forward (there might be some compiler extensions that allow it prior to C++11).


Randomly generating a sorted array I'm writing a method that generates a random integer array that are sorted. It takes the arguments of min value, max value and the length of desired array. So for example the arguments with min val = 1 max val = 10 length = 5 could give [1,1,2,3,5,6,9,9,9,10] or [2,4,5,6,6,6,6,6,6,8]. Let me know if the specifications are unclear and I'll elaborate.

Here is my code; it works but is a bit messy:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.ListIterator;
import java.util.Random;

public class test {
    public static void main(String[] args)
    {
        System.out.println(Arrays.toString(generateSorted(10, 0, 100)));
    }

    public static int[] generateSorted(final int length, final int minVal, final int maxVal)
    {
        ArrayList<Integer> data = new ArrayList<>(length);
        data.add(getRandomVal(minVal, maxVal));

        boolean added;
        for(int i = 0; i < length; i++)
        {
            added = false;
            ListIterator<Integer> itr = data.listIterator();
            int rndNum = getRandomVal(minVal, maxVal);
            while(itr.hasNext() && !added)
            {
                Integer currentNum = itr.next();
                if(currentNum >= rndNum)
                {
                    itr.previous();
                    itr.add(rndNum);
                    added = true;
                }
            }

            if(!added)//add to end of arrayList
            {
                data.add(rndNum);
            }
            //printArrList(data);
        }

        return data.stream().mapToInt(i -> {
            return i;
        }).toArray();
    }

    /*prints contents of arraylist all on 1 line*/
    public static void printArrList(ArrayList<Integer> al)
    {
        System.out.print("[");
        /*this could be replaced with a for (each) loop*/
        al.stream().forEach((i) -> {
            System.out.print(i+", ");
        });
        System.out.println("]");
    }

    /*returns random int between [min, max]*/
    private static int getRandomVal(int min, int max)
    {
        int n = max - min + 1;
        int i = rand.nextInt(n);//nextInt(n) returns random int in [0, n)
        return min + i;
    }
    final static private Random rand = new Random();
}

In generateSorted, in every iteration of the outer for loop, you walk over the data array elements until you find the right position to insert. In the worst case, this could be very inefficient. You can improve the performance using binary search:

public int[] generateSorted(final int length, final int minVal, final int maxVal) {
    List<Integer> data = new ArrayList<>(length);

    for (int i = 0; i < length; i++) {
        int rndNum = getRandomVal(minVal, maxVal);
        int insertionPoint = Collections.binarySearch(data, rndNum);
        data.add(insertionPoint > -1 ? insertionPoint : - insertionPoint - 1, rndNum);
    }

    return data.stream().mapToInt(i -> i).toArray();
}

Some other improvements I did here:

Changed i -> { return i; } to the lambda expression i -> i
Generate an array of length, instead of length + 1 as your version
Declare data as List instead of ArrayList

I would also shorten getRandomVal like this:

private int getRandomVal(int min, int max) {
    return min + rand.nextInt(max - min + 1);
}

Finally, I would make the whole thing testable, something like this:

class GenRandomArray {

    private final Random rand;

    public GenRandomArray() {
        rand = new Random();
    }

    public GenRandomArray(int seed) {
        rand = new Random(seed);
    }

    public int[] generateSorted(final int length, final int minVal, final int maxVal) {
        List<Integer> data = new ArrayList<>(length);

        for (int i = 0; i < length; i++) {
            int rndNum = getRandomVal(minVal, maxVal);
            int insertionPoint = Collections.binarySearch(data, rndNum);
            data.add(insertionPoint > -1 ? insertionPoint : -insertionPoint - 1, rndNum);
        }

        return data.stream().mapToInt(i -> i).toArray();
    }

    private int getRandomVal(int min, int max) {
        return min + rand.nextInt(max - min + 1);
    }
}

public class GenRandomArrayTest {
    @Test
    public void testGen10_Between_0_and_100() {
        assertArrayEquals(new int[]{5, 9, 42, 54, 67, 72, 82, 84, 93, 98},
                new GenRandomArray(0).generateSorted(10, 0, 100));
    }
}

About Collections.binarySearch

The javadoc says:

  returns: the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size() if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

If the value is greater than -1, it's the index of the item we searched for. In other words the random number we generated is already in the list. We can insert it this duplicate right there.

If the value is -1 or smaller, the item is not yet in the list, and the returned \$x\$ is such that \$-x - 1\$ is the right place to insert the item to keep the elements ordered.

It becomes clearer to see if you print out the random number, the returned \$x\$ and the content of the list, for example when using \$seed = 0\$ and \$(10, 0, 10)\$ as the params of the generator:

0 : -1 : []
6 : -2 : [0]
8 : -3 : [0, 6]
5 : -2 : [0, 6, 8]
6 : 2 : [0, 5, 6, 8]
2 : -2 : [0, 5, 6, 6, 8]
2 : 1 : [0, 2, 5, 6, 6, 8]
6 : 5 : [0, 2, 2, 5, 6, 6, 8]
2 : 1 : [0, 2, 2, 5, 6, 6, 6, 8]
0 : 0 : [0, 2, 2, 2, 5, 6, 6, 6, 8]

For example:

When inserting 8, it doesn't exist in the list, the returned \$x = -3\$, so we should insert at position \$-x -1 = 3 - 1 = 2\$.
When inserting 2 for the second time, the returned \$x = 1\$, so we should insert at position 2.

Conclusion

It's not worth trying to keep the list sorted. The binary search makes it faster than the original implementation, but keep in mind that the insertion is costly, as every time you insert something, the rest of the array has to be copied. And you cannot compensate for that by using LinkedList instead of ArrayList, because efficient binary search requires a random access list, with a LinkedList you would lose efficiency due to the traversal of elements.

Overall, this will be slower than first adding all the elements and then later sorting with Arrays.sort. So do it that way instead.

public int[] generateSorted(final int length, final int minVal, final int maxVal) {
    List<Integer> data = new ArrayList<>(length);

    for (int i = 0; i < length; i++) {
        data.add(getRandomVal(minVal, maxVal));
    }
    Collections.sort(data);

    return data.stream().mapToInt(i -> i).toArray();
}


Subclass of socket with optional TLS import ssl
from socket import socket

class testsock(socket):
    def __init__(self, tls=True):
        super(testsock, self).__init__()
        self.connect(('192.168.0.1', 443))
        if tls:
            self = ssl.wrap_socket(self) # Ugly "handover"

testsock(tls=True)

This code works, it's the second ugliest bodge I've done as of today.
But it works with the behavior I desire, that .send() and .close() etc are "intact".

However, if I ever try to add any other values to my custom class testsock those will effectively be gone, since I replace self with whatever wrap_socket spits out.

I could use something that looks like this:

class testsock():
    def __init__(self, tls=True):
        self.s = socket()
        ...
        self.s = ssl.wrap_socket(self.s)

    def send(self, data):
        self.s.send(data)

But that goes against the elegance of inheriting the socket object and not having to create all the functions you'd expect out of a ordinary socket. (I know, lazy programming, but it looks cleaner without all the definitions).

I could probably iterate over all the .__dict__ or inspect.getmembers() functions and replace them with the matching available items from wrap_socket, but that would also cause for concerns later on, but it would look something like:

for name, obj in inspect.getmembers(self):
    print(name, obj)

It would yield something like:

close <bound method socket.close of <__main__.testsock fd=...>>

I'm not sure how I'd get socket from this, obj.__class__.__name__ doesn't yield this. If I could some how get socket from the object I would know that it's a socket() bound method, and replace the corresponding method from wrap_socket() return.

I've heard of metaclass programming, and I'm having a go at it.
But my question is, out of all the options - Which would be the best according to the community?

Would it be possible to inline-replace the inherited socket object with whatever ssl.wrap_socket returns?

You don't need a class here, a simple function will do:

import ssl
from socket import socket

def testsock(tls=True):
    sock = socket()
    sock.connect(('192.168.0.1', 443))
    if tls:
        sock = ssl.wrap_socket(sock)
    return sock

testsock(tls=True)

Returned objects are either plain sockets or ssl sockets but they share some common behaviour (like send or recv). If you need to add attributes, you can do that before the return statement.

In case you wish to be able to extend it better, you can still use the socket as an attribute on a custom class and expose the socket API through __getattr__:

class TestSock(object):
    def __init__(self, tls=True):
        sock = socket()
        sock.connect(('192.168.0.1', 443))
        if tls:
            sock = ssl.wrap_socket(sock)
        self.socket = sock
        # other attributes

    def __getattr__(self, attribute_name):
        """Defer unknown behaviour to the socket"""
        return getattr(self.socket, attribute_name)

    # other methods

But you don't need it for now.


Auto-Format GPA while typing : REVISED I need to further simplify this solution to auto-format a text-box field intended only for the user to enter their college Grade Point Average (GPA). Here is my code:

<label for="collegeGPA">GPA:</label>
<input type="text" name="collegeGPA" id="collegeGPA" maxlength="4" style="width:45px;" onpaste="return false;" autocomplete="off" >

<script type="text/javascript">

//<!-- When executed this script [BELOW] auto-formats a Grade Point Average (GPA) field given the element ID [BEGIN] -->

function formatGPA(gPAElementID) {

    var rE = /\D/g; // remove any characters that are not numbers
    var originalGPA = document.getElementById(gPAElementID);
    var originalGPAVal = originalGPA.value;
    var gPAVal = originalGPA.value.replace(rE,"");
    var gPAValFirstCharacter = gPAVal.charAt(0);

if  ( ( ( parseFloat(originalGPAVal) ) > 4 ) && ( ( parseFloat(originalGPAVal) ) < 5 ) )

        {
            originalGPA.value = "4.00";
        }

else
        {

        if  ( ( gPAVal >= 0 ) &&  ( gPAValFirstCharacter < 5 ) )
                {

                        gPALen = gPAVal.length;

                        if  ( gPALen > 1 )
                                {
                                    gPAa=gPAVal.slice(0,1);
                                    gPAb=gPAVal.slice(1);
                                    originalGPA.value = gPAa + "." + gPAb;
                                }
                    else if ( gPALen == 1 )
                                {
                                    originalGPA.value = gPAVal + ".";
                                }
                        else
                                {
                                    originalGPA.value = gPAVal;
                                };

                }

            else
                {
                    originalGPA.value = "";
                };

        };

};

//<!-- When executed this script [ABOVE] auto-formats a Grade Point Average (GPA) field given the element ID [END] -->

document.getElementById('collegeGPA').onblur = function (e) { formatGPA(this.id); };
document.getElementById('collegeGPA').onfocus = function (e) { formatGPA(this.id); };
document.getElementById('collegeGPA').onkeypress = function (e) { formatGPA(this.id); };
document.getElementById('collegeGPA').oninput = function (e) { formatGPA(this.id);  };
document.getElementById('collegeGPA').onchange = function (e) { formatGPA(this.id); };

</script>

...and here is my JSFiddle: http://jsfiddle.net/JamesAndersonJr/kxaBJ/1/ I'm looking for a simpler way to format the GPA input dynamically (i.e. as the user types it in). The format should be:

1st Character: any digit 0-4

2nd Character: Always a Period (should also be auto-inserted after user types a valid first digit)

3rd Character: any digit

4th Character: any digit

I'd maybe try something like the following. What I've done is remove the string manipulation and concentrate on rounding the numbers.

function formatGPA(originalGPA) {

    var originalGPAVal = originalGPA.value;
    var gPAVal = parseFloat(originalGPAVal);

    if (gPAVal < 0) {
        gPAVal = 0;
    } else if (gPAVal > 4) {
        gPAVal = 4;
    }

    if (isNaN(gPAVal)) {
        // Some kind of error handling
    } else {
        originalGPA.value = gPAVal.toFixed(2);
    }

}

//<!-- When executed this script [ABOVE] auto-formats a Grade Point Average (GPA) field given the element ID [END] -->

document.getElementById('collegeGPA').onblur = function () {
    formatGPA(this);
};

The only problem with this is it's difficult to manipulate it as one types. Therefore I've actioned the manipulation only on blur.


C functions for fast IO for competitive programming In some (perhaps not well-designed) competitive programming problems,
the runtime and score is dependent on how fast your program can
process input. Therefore I've written a small C99 library with optimized
functions for reading from stdin and writing to stdout:

fastio.h:

#ifndef FASTIO_H
#define FASTIO_H

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

#ifdef _WIN32
#define putchar_unlocked putchar
#else
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#endif

// Fast IO routines accessing stdin and stdout. The purpose of them is
// to reduce IO overhead in competitive programming. Therefore, no
// error checking anywhere.

// On Linux and OS X, this is a pointer to stdin's next character. On
// Windows, standard getchar() is used and the pointer
// is NULL.
extern char *
FAST_IO_STDIN;

void
fast_io_init();

// Reading
inline char
fast_io_read_char() {
#ifdef _WIN32
    return getchar();
#else
    return *FAST_IO_STDIN++;
#endif
}

inline unsigned int
fast_io_read_unsigned_int() {
    int val = 0;
#ifdef _WIN32
    while (true) {
        char c = getchar();
        if (c < '0') {
            ungetc(c, stdin);
            break;
        }
        val = val * 10 + c - '0';
    }
#else
    do {
        val = val*10 + *FAST_IO_STDIN++ - '0';
    } while(*FAST_IO_STDIN >= '0');
#endif
    return val;
}

inline int
fast_io_read_int() {
    int val = 0;
    int sgn = 1;

#ifdef _WIN32
    char c = getchar();
    if (c == '-') {
        sgn = -1;
    } else {
        ungetc(c, stdin);
    }
    while (true) {
        char c = getchar();
        if (c < '0') {
            ungetc(c, stdin);
            break;
        }
        val = 10 * val + c - '0';
    }
#else
    if (*FAST_IO_STDIN == '-') {
        sgn = -1;
        FAST_IO_STDIN++;
    }
    do {
        val = val*10 + *FAST_IO_STDIN++ - '0';
    } while(*FAST_IO_STDIN >= '0');
#endif
    return val * sgn;
}

// Writing
inline void
fast_io_write_char(char ch) {
    putchar_unlocked(ch);
}

inline void
fast_io_write_long(long n) {
    if (n < 0) {
        putchar_unlocked('-');
        n *= -1;
    } else if (n == 0) {
        putchar_unlocked('0');
        return;
    }
    long N = n;
    long rev = N;
    int count = 0;
    rev = N;

    while ((rev % 10) == 0) {
        count++; rev /= 10;
    }
    rev = 0;
    while (N != 0) {
        rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;
    }
    while (rev != 0) {
        putchar_unlocked(rev % 10 + '0');
        rev /= 10;
    }
    while (count--) {
        putchar_unlocked('0');
    }
}
#endif

fastio.c

#include "fastio.h"

char *
FAST_IO_STDIN = NULL;

void
fast_io_init() {
#ifdef _WIN32
    // Nothing to do on Windows because we use standard getchar().
#else
    int flags = MAP_SHARED;
#ifdef __linux__
    // Prefills the buffer.
    flags |= MAP_POPULATE;
#endif
    struct stat sb;
    (void)fstat(STDIN_FILENO, &sb);
    FAST_IO_STDIN = (char*)mmap(0, sb.st_size,
                                PROT_READ, flags, STDIN_FILENO, 0);
#endif
}

extern inline char fast_io_read_char();
extern inline unsigned int fast_io_read_unsigned_int();
extern inline int fast_io_read_int();
extern inline void fast_io_write_char(char ch);
extern inline void fast_io_write_long(long n);

I want the library to work on Windows, Linux and OS X, but
performance only matters on Linux because bots for competitive
programming only run on Linux. As the goal is to shave off
milliseconds to win contests, there is absolutely no error
checking.

Does anyone have any ideas how to make it even faster?

On Windows, you'd rather use the console API directly instead of stdio.h. This should increase performance somewhat, since these functions are what getchar etc will end up calling anyhow.

Other issues:

All functions should be declared to take void as parameters, rather than to accept any parameter. If they are properly inlined this should hopefully not have any impact on the program, but in some cases it might mess up the calling convention.
Rather than making a mess with compiler switch #ifdef all over the place, you could in the header file do something like:

#ifdef _WIN32
  #define fast_io_read_char fast_io_read_char_windows
#else
  #define fast_io_read_char fast_io_read_char_linux
#endif

And so on. Then separate the implementation of each function, so that you have one file fastio_windows.c and one fastio_linux.c. You can add both files to linking both no matter, the #ifdef in the header will determine which functions that actually get linked to the binary.
Left shifting the signed type long rev is questionable code - you have to ensure that n is never negative or too large.
Missing #include <stdbool.h> so the code won't compile.
getchar calls incorrectly use char instead of int so the code cannot handle EOF. If you don't intend to handle EOF, then you should still cast the result of getchar() to char - to silence compiler warnings and write self-documenting code that shows that you have considered this.


Concurrency mechanism for ensuring repeated threads are executed in a given order Inspired by the following leet-code

Suppose you are given the following code:
class FooBar {
  public void foo() {
    for (int i = 0; i < n; i++) {
      print("foo");
    }
  }

  public void bar() {
    for (int i = 0; i < n; i++) {
      print("bar");
    }
  }
}

The same instance of FooBar will be passed to two different threads.
Thread A will call foo() while thread B will call bar(). Modify the
given program to output "foobar" n times.
Example 1:
Input: n = 1 Output: "foobar" Explanation: There are two threads being
fired asynchronously. One of them calls foo(), while the other calls
bar(). "foobar" is being output 1 time.
Example 2:
Input: n = 2 Output: "foobarfoobar" Explanation: "foobar" is being
output 2 times.

My solution, which is was accepted:
class FooBar {
    private static class Syncer {
        int status;
    }

    private int n;
    private Syncer syncer = new Syncer();

    public FooBar(int n) {
        this.n = n;
    }

    public void foo(Runnable printFoo) throws InterruptedException {
        synchronized(syncer) {
            for (int i = 0; i < n; i++) {
                while (syncer.status != 0){
                    syncer.wait();
                }
                printFoo.run();
                syncer.status = 1;
                syncer.notifyAll();
            }
        }
    }

    public void bar(Runnable printBar) throws InterruptedException {
        synchronized(syncer) {
            for (int i = 0; i < n; i++) {
                while (syncer.status != 1){
                    syncer.wait();
                }
                printBar.run();
                syncer.status = 0;
                syncer.notifyAll();
            }
         }
    }
}

Questions:

Do you see any cases, where this solution would not work? (Assuming the input is correct, i.e. n>=1.)

I'm thinking of cases where one thread would not necessarily see the changes to syncer due to it not being volatile and synchronized. (I know this exact issue does not affect my solution, because syncer is, in fact synchronized, so what I mean if there are other similar "pitfalls" which affect the code above.)

Do you see any issues with performance? (I know the code waits in a while-loop, but it calls wait, which is supposed to be optimal.)

In general, is there a way this solution could be improved?

Does this pattern (threads running multiple times in a given order and waiting for each other) have a name, or a high-level Java concurrency object which implements them? (Slightly related: what better name would you give to syncer?)

Because of the initial constraints (a for loop for two threads different why one prints "foo" and the other one prints "bar") set by leetcode site on the problem I see no alternatives to the way you solved it. Answering your first question:

Do you see any cases, where this solution would not work? (Assuming
the input is correct, i.e. n>=1.)

None, because you are setting the guard condition to one initial value (0) and you are ensuring the alternate order with wait and notify.
Your second question:

Do you see any issues with performance? (I know the code waits in a
while-loop, but it calls wait, which is supposed to be optimal.)

Again, following leetcode constraints there are no alternatives, while it seems that from some solutions posted on the site if you are less strict with constraints performance can be better, but this is probably bound to the fact that
threads just print one string and nothing else, so wait and notify calls represent a significative portion of working time.
Your third question:

In general, is there a way this solution could be improved?

You can use for the lock instead of your static class an Object instance and a boolean variable for the condition, but result should be quite similar.
Fourth question:

Does this pattern (threads running multiple times in a given order and
waiting for each other) have a name, or a high-level Java concurrency
object which implements them? (Slightly related: what better name
would you give to syncer?)

I see this situation like a token ring anomalous case where the token is in this case the value of the status guard, with more than two threads you could use a String token with the name of thread that would be activated.


Parse child value from XML using ezxml I'm parsing an XML file and searching for a child value.

As I'm familiar with this XML file structure, I know that the final value is a child of a child, and so I wrote the following code section:

/*Parse XML file and find device friendlyName*/
pxUpnp = ezxml_parse_file(XML_FILE_PATH);

pxUpnpChild = ezxml_child(pxUpnp, "device");

/*Looking for friendlyName sub-child*/
pxUpnpSubChild = ezxml_child(pxUpnpChild, "friendlyName");

This code works, but I wander if there is a better or more elegant method to parse a 'child of a child' value using ezxml library?

Thank you all in advance!

ezxml has a variadic function which allows you to traverse directly to a specific node, terminated by either -1 as the index, or an empty string for the node name:

pxUpnpSubChild  = ezxml_get(pxUpnp, "device", 0, "friendlyName", 0, "");

This would give you directly the first friendlyName node of the first device node. Internally, it does mostly the same as what you did manually.

The more elegant solution would to use standard XPath expressions, however that isn't supported by ezxml. Libraries for C supporting XPath would e.g. libxml2 or Expat


Basic command line REPL calculator I'm pretty confident in this code but I want to be sure I haven't missed anything to do with optimization or error handling. The program takes input in the form of {number} {operator} {number} and prints the result. If the input is invalid it prints nothing.

Example input: 1.0 + 2.0
Output: 3

#include <iostream>
#include <string>
#include <stdexcept>
#include <regex>

double calculate(const double a, const char op, const double b)
{
    switch (op)
    {
        case '+':
            return a + b;
        case '-':
            return a - b;
        case '*':
            return a * b;
        case '/':
            if (b == 0.0)
                throw std::invalid_argument {"Divide by zero"};
            return a / b;
        default:
            throw std::invalid_argument {{op}};
    }
}

int main()
{
    const std::regex valid_input {
        "([0-9]*\\.?[0-9]*) ([\\+\\-\\*\\/]) ([0-9]*\\.?[0-9]*)"};

    std::string line;
    std::smatch terms;

    double a;
    double b;
    char op;

    while (std::getline(std::cin, line))
    {
        if (!std::regex_match(line, terms, valid_input))
            continue;

        a = std::stod(terms[1]);
        b = std::stod(terms[3]);
        op = terms[2].str().front();

        std::cout << calculate(a, op, b) << '\n';
    }
}

What happens if I enter only + surrounded by spaces?
The program should not abort if I enter 0 / 0, but continue with the next line.
I'd like to omit the spaces around the operator.
In the regular expression, the operator part can be written as [+\\-*/].


Perl script to set IP settings This is my first go at a Perl script to (semi-) automate setting the correct IP settings. I use it to configure a virtual machine after creation. It's also my first Perl script as a whole, so I'm looking for anyone who can give me pointers towards a better and more efficient coding style.

#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;
use Tie::File;

sub printMissingValues;
sub gatherInputValues;
sub setValueForField;

my %resulthash = (
    "IPADDR"  => undef,
    "NETMASK" => undef,
    "GATEWAY" => undef,
     "DNS1"    => undef,
     "DNS2"    => undef,
);

my $filename = undef;

GetOptions ("filename=s"  => \$filename,
    "IP=s"        => \$resulthash{'IPADDR'},
            "NETMASK=s"   => \$resulthash{'NETMASK'},
            "GATEWAY=s"   => \$resulthash{'GATEWAY'},
            "DNS1=s"      => \$resulthash{'DNS1'},
    "DNS2=s"      => \$resulthash{'DNS2'})

or die ("Error in input values");

#gatherInputValues(\%resulthash);

open(my $fd, '>>', "$filename")
    or die("Cannot open file $filename");

tie my @array, 'Tie::File',  $filename
    or die "Cannot tie file '$filename': $!";

for my $line (@array)
{
   my @fields = split /=/, $line;

    if ($fields[0] eq 'BOOTPROTO') {
         $fields[1] = '"static"';
        $line = join '=', @fields;
        }

    elsif ($fields[0] eq 'IPADDR' ) {
            $line = setValueForField('IPADDR', @fields);
    }

    elsif ($fields[0] eq 'NETMASK') {
            $line = setValueForField('NETMASK', @fields);
    }

    elsif ($fields[0] eq 'GATEWAY') {
            $line = setValueForField('GATEWAY', @fields);
    }

    elsif ($fields[0] eq 'DNS1') {
            $line = setValueForField('DNS1', @fields);
    }

    elsif ($fields[0] eq 'DNS2') {
            $line = setValueForField('DNS2', @fields);
    }
}

untie @array;

addMissingValues(\%resulthash);

close($fd);

##functions##

sub addMissingValues {
    my $hash = shift @_;

    while(my($key, $value) = each %{$hash}) {
        if (defined ($value)) {
            createNewField($key, $value);
        }
    }
}

 sub createNewField {
    my $key = shift @_;
    my $value = shift @_;
    print $fd (join '=', $key, $value);
    print $fd ("\n");
}

sub setValueForField {
    my $fieldname = shift;
    my @fields = shift;

    $fields[1] = $resulthash{$fieldname};

    return  (join '=', @fields);
}

sub gatherInputValues {

    my $result_hash = shift @_;

    print "IP4-address: ";
    $result_hash->{'IPADDR'} = <STDIN>;
    print "subnet-mask: ";
    $result_hash->{'NETMASK'} = <STDIN>;
    print "gateway: ";
    $result_hash->{'GATEWAY'} = <STDIN>;
    print "dns1: ";
    $result_hash->{'DNS1'} = <STDIN>;
    print "dns2: ";
    $result_hash->{'DNS2'} = <STDIN>;
}

This is the file the script operates on (standard configuration script for CentOS systems - /etc/sysconfig/network-scripts/ifcfg-etho)

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="eth0"
UUID="7cabbc98-e67d-4a64-a132-7e8bcbeb579b"
DEVICE="eth0"
ONBOOT="yes

There's no need to predeclare the subs (sub whatever;).
There's no need to assign the initial values to %resulthash.
my $filename; does the same as my $filename = undef;. There's no need to specify the undef.
Hash keys are autoquoted if they are simple (i.e. follow the same rules as variable name). Instead of $resulthash{'IPADDR'} you can type just $resulthash{IPADDR}.
shift in a sub operates on @_. So you can just write my $hash = shift;.
Instead of shifting @_ several times, assign all the variables at the same time: my ($key, $value) = @_;.

Plus, you're comparing $fields[0] to various strings, maybe a "dispatch table" would be a bit clearer. See my version of the script:

#!/usr/bin/perl
use strict;
use warnings;

use Getopt::Long;
use Tie::File;

my %resulthash;
my $filename;

GetOptions ('filename=s' => \$filename,
            'IP=s'       => \$resulthash{IPADDR},
            'NETMASK=s'  => \$resulthash{NETMASK},
            'GATEWAY=s'  => \$resulthash{GATEWAY},
            'DNS1=s'     => \$resulthash{DNS1},
            'DNS2=s'     => \$resulthash{DNS2})
or die 'Error in input values';

open my $fd, '>>', $filename
    or die "Cannot open file $filename";

tie my @array, 'Tie::File',  $filename
    or die "Cannot tie file '$filename': $!";

for my $line (@array) {
    my @fields = split /=/, $line;

    { BOOTPROTO => sub { $fields[1] = '"static"';
                         $line = join '=', @fields; },
      IPADDR    => sub { $line = setValueForField('IPADDR', @fields); },
      NETMASK   => sub { $line = setValueForField('NETMASK', @fields); },
      GATEWAY   => sub { $line = setValueForField('GATEWAY', @fields); },
      DNS1      => sub { $line = setValueForField('DNS1', @fields); },
      DNS2      => sub { $line = setValueForField('DNS2', @fields); },
    }->{ $fields[0] }->();
}

untie @array;

addMissingValues(\%resulthash);

close $fd;

sub addMissingValues {
    my $hash = shift;

    while (my ($key, $value) = each %$hash) {
        createNewField($key, $value) if defined $value;
    }
}

sub createNewField {
    my ($key, $value) = @_;
    print {$fd} join '=', $key, $value;
    print {$fd} "\n";
}

sub setValueForField {
    my ($fieldname, @fields) = @_;

    $fields[1] = $resulthash{$fieldname};

    return join '=', @fields;
}

You can also keep the dispatch table outside of the loop, but you need to pass parameters to the callbacks.

my %DISPATCH = (
    BOOTPROTO => sub { $_[1] = '"static"';
                       join '=', @_ },
    IPADDR    => sub { setValueForField('IPADDR', @_) },
    NETMASK   => sub { setValueForField('NETMASK', @_) },
    GATEWAY   => sub { setValueForField('GATEWAY', @_) },
    DNS1      => sub { setValueForField('DNS1', @_) },
    DNS2      => sub { setValueForField('DNS2', @_) },
);

for my $line (@array) {
    my @fields = split /=/, $line;
    my $sub = $DISPATCH{ $fields[0] };
    $line = $sub->( @fields) if $sub;
}


Determine if elements in an ArrayList are consecutively ordered Write a method isConsecutive that accepts an ArrayList of integers as
  a parameter and returns true if the list contains a sequence of
  consecutive integers and false otherwise. Consecutive integers are
  integers that come one after the other in ascending order, as in 5, 6,
  7, 8, 9, etc. For example, if a variable called list stores the values
  [3, 4, 5, 6, 7, 8, 9], then the call of list.isConsecutive() should
  return true. If the list instead stored [3, 4, 5, 6, 7, 12, 13] then
  the call should return false because the numbers 7 and 12 are not
  consecutive. The list [3, 2, 1] might seem to be consecutive, but the
  elements appear in reverse order, so the method would return false in
  that case. Any list with fewer than two values should be considered to
  be consecutive. You may assume that the list passed is not null.

Wondering which better ways there are to do this, and how to improve my future code.

public boolean isConsecutive(ArrayList<Integer> list) {
    int count = 0;
    for (int i = 0; i < list.size() - 1; i++) {
        if (list.get(i) == (list.get(i + 1) - 1)) {
            count++;
        } else {
            count = 0;
        }
    }
    return list.size() - 1 == count || list.isEmpty();
}

You don't need a counter in your code:

public boolean isConsecutive(ArrayList<Integer> list) {
    for (int i = 0; i < list.size() - 1; i++) {
        if (list.get(i) != list.get(i + 1) - 1) {
            return false;
        }
    }
    return true;
}

As a next step, you can change the loop to start at 1 instead of 0, so that the loop condition becomes simpler.

public boolean isConsecutive(ArrayList<Integer> list) {
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i - 1) + 1 != list.get(i)) {
            return false;
        }
    }
    return true;
}

The specification does not require the algorithm to be constant-time (it's not used for cryptography), therefore it should be OK to return as soon as you know the answer.

To be extra correct, you could detect integer overflows:

int prev = list.get(i - 1);
if (prev != list.get(i) /* mismatch */ || prev + 1 < prev /* overflow */) {
    return false;
}

If the requirements were to change that you may only access each list element once, you would need to remember the previous value.

public boolean isConsecutive(ArrayList<Integer> list) {
    Iterator<Integer> it = list.iterator();
    if (!it.hasNext()) {
        return true;
    }

    int prev = it.next();
    while (it.hasNext()) {
        int curr = it.next();
        if (prev + 1 != curr /* mismatch */ || prev + 1 < prev /* overflow */) {
            return false;
        }
        prev = curr;
    }
    return true;
}


Console.log challenge change all of the links on a search results page so that they do not go anywhere? I am reading a book call Front end Web Development "The big nerd ranch guide", in which I came across a silver challenge:

Go to your favorite search engine and search for "anything".
Open the DevTools to the console.
With the functions you wrote in this chapter as a reference, attach event listeners to all of the links and disable their default click functionality.

Here's what I wrote:
function selectLink() {
    'use strict'
    var links = document.querySelectorAll('a');
    var linksArray = [].slice.call(links);
    return linksArray;
}

function disableClick (dis) {
    'user strict';
    dis.addEventListener('click', function (event) {
        event.preventDefault();
    });
}

function click() {
    'use strict';
    var link = selectLink();
    link.forEach(disableClick);
}

click();

I don't know whether this is the correct method to do it, since there is no solution on the book for it.
Can I do better?

Refactoring is commonly seen as "moving out repeating code into reusable functions". But it can also easily mean "moving code that's only ever used once back to where it's being used". Although the book led you to writing individual functions, you don't have to split all this code up.

Also, if you're just turning a NodeList into an array just to use array.forEach(), then this conversion is unnecessary. NodeList already comes with a forEach method (unless you still care about IE).

Your code could be as short as:

document.querySelectorAll('a').forEach(link => {
  link.addEventListener('click', event => event.preventDefault())
})


Some table_handling tools for lua I'm writing some table.tools to make table handling a little easier for me. But I'm a beginner with Lua, so you might help me a bit, to make the code better.

First of all I would like to get the best usability, but performance is a factor as well.

-- is compare equal to AT_LEASE one entry of list_to_search_in?
-- the idea was to make syntax like
--" if value == var1 or value == var2 or value == var(n) "
--- a little shorter

function table.contains(list_to_search_in, compare)

     for k,v in pairs(list_to_search_in) do
        if compare == v then
            return true
        end
      end

      return false
end

    -- is compare equal to EVERY entry of list_to_search_in ?

function table.equalsAll(list_to_search_in, compare)
    local counter = 0
    local truth_counter = 0

    for k,v in pairs(list_to_search_in) do
        if compare == v then
            truth_counter = truth_counter + 1
        end
        counter = counter + 1
    end

    return (truth_counter == counter)
end

    -- compare is equal to HOW_MANY entrys of list_to_search_in?
    -- returns how many entrys are the same

function table.count(list_to_search_in, compare)
    local counter = 0

    for k,v in pairs(list_to_search_in) do
        if compare == v then
            counter = counter + 1
        end
    end
    return counter
end

    -- compare is equal to WHICH entrys of list_to_search_in?
    -- returns a map of equality

function table.truthmap(list_to_search_in, compare)
    local map = { }

    for k,v in pairs(list_to_search_in) do
        table.insert(map, { k , compare == v})
    end
    return map
end

    -- deletes all entys in list, wich equals(or not[,invert]) to compare value
    -- returns a table with all entrys wich have not been equal

function table.removePerValue(list_to_search_in, compare, invert)
    local counter = 0
    local return_table = {}

    for k,v in pairs(list_to_search_in) do

        if invert and (v == compare) then
            table.insert(return_table, v)
        elseif not invert and (v ~= compare) then
            table.insert(return_table, v)
        end
    end

    return return_table
end

(Feel free to use, I'm happy if the code helps someone)

What is truthmap trying to achieve? I can't quite figure it out, sorry.

contains by nature will always be O(N) in your implementation, so yes, it's speed is equal to the number of elements in your table.

count should probably be renamed to countOf to better signify it's intent. Better yet, you could rename it where, have it take a function, and then allow the user to use the standard # operator on the resultant table.

removePerValue seems unnecessary with a where function. Users can simply filter the table rather than remove values from the table.

equalsAll in my opinion again should take a function as an argument and not an object.

You should change your loops from for k, v in pairs(tbl) do to for _, v in pairs(tbl) do if you are not using k.

I'm sure there's things to be said about the implementation of equalsAll but I don't have time to point that out at the moment.

Do you know about the # operator? appending # to the front of a table will return a value of the size of the table (more precisely, it will return the highest indexed number in the table, so it will be incorrect for sparse arrays).

You should ideally not need comments to explain the purpose of code - your functions should describe themselves ;-)


Verifying patterns of various filenames I have a large number of images that were named in a odd way and because of that, when they're displayed in a file browser, they're completely out of order. However, since they weren't named in a completely random manner, I can use regex to properly rename them, once I understand all the subpatterns that went into it. So far I only understood two.

So I'm going to create a program to verify the pattern of each filename, make a set out of it with a sample of each name, and then try to understand the patterns and subpatterns.

def get_pattern(name):
    pattern = []
    for letter in name:
        if letter.isalpha():
            pattern.append('a')
        elif letter.isdigit():
            pattern.append('0')
        else:
            pattern.append(letter)
    return ''.join(pattern)

It's pretty straightforward to loop through the images and then check the pattern for each image.

My questions:

Is there a way to make the code more concise? Maybe using a dictionary?
Is there something in re or other easily available library that does this? Is using isalpha() and isdigit() the way to go here?

This will make your example work in the same way you intended to.

patterns = []
patterns.append(lambda x: 'A' if x.isalpha() else x)
patterns.append(lambda x: '0' if x.isdigit() else x)

def get_pattern(name):
    for pattern in patterns:
        name = map(pattern, name)
    return ''.join(name)

Additionally you can register new patterns. Here is an example:

>>> get_pattern('Mario Cesar 2013')
'AAAAA AAAAA 0000'

About your objective, if you want just to sanitize the file names, will be the best to use a different approach, look at https://github.com/ksze/sanitize


Function returning a vec with prime factors of number See code below and my comment with questions:
pub fn factors(n: u64) -> Vec<u64> {
    let mut m = n; // is it possible to convert n to a mutable type without using another variable?
    let mut a:Vec<u64> = Vec::new();
    while m % 2 == 0 {
        m /= 2;
        a.push(2);
    }
    // in C we can just write for (int i = 3; i <= sqrt(m); i+= 2) ... just saying... and C is not even as fast as python to code... am I really doing it right??
    for i in (3..((m as f64).sqrt() as usize + 1usize)).step_by(2) {
        while m % i as u64 == 0 { // is there a way to avoid all the casts to u64
            a.push(i as u64);
            m /= i as u64;
        }
    }
    if m > 2 {
        a.push(m);
    }
    return a;
}

Is it possible to convert n to a mutable type without using another variable?
The C equivalent of the main loop use twice less ink. Is Rust so verbose by design?
Is there a way to avoid all the casts to u64
any other insight welcome

pub fn factors(n: u64) -> Vec<u64> {
    let mut m = n; // is it possible to convert n to a mutable type without using another variable?

use pub fn factors(mut n: u64) to allow the parameter to be mutable
    let mut a:Vec<u64> = Vec::new();

You don't need : Vec<u64>, rust will infer it.
    while m % 2 == 0 {
        m /= 2;
        a.push(2);
    }

    // in C we can just write for (int i = 3; i <= sqrt(m); i+= 2) ... just saying... and C is not even as fast as python to code... am I really doing it right??
    for i in (3..((m as f64).sqrt() as usize + 1usize)).step_by(2) {

Generally speaking, Rust is less verbose than C, but you've hit one of the major exceptions. Rust is pickier about types, it won't let you mix and match different numeric types. But we can make some improvements here.

You don't need 1usize just use 1.
You can also use 3 ..= (m as f64).sqrt() as usize (..= includes the end)

Alternatively, you could reverse the check:
for index in (3..).step_by(2).take_while(|i| i * i <= m)

I'm not sure if that's better or not.
        while m % i as u64 == 0 { // is there a way to avoid all the casts to u64
            a.push(i as u64);
            m /= i as u64;
        }

You need casts because you are being inconsistent about your types. If you change the usize in the for loop to u64 there will be no need for casts.
    }
    if m > 2 {
        a.push(m);
    }
    return a;
}


Calculate the sum of all primes less than 2,000,000 in Swift This is a Swift program I wrote to calculate the sum of primes below 2 million, but it is tediously slow.

I am curious about what makes it so slow. My theory is that copying the filtered array is the heavy operation.  Any other ideas on how to speed it up?

func initArray (p:Array<UInt32>)  -> Array<UInt32> {

    var arr = Array<UInt32>()
    arr.reserveCapacity(2000000)

    func isDiv(p:Array<UInt32>, val:UInt32) ->Bool {

        for i in p {

            if val % i == 0 {
                return true
            }
        }

        return false
    }

    for i:UInt32 in 2...2000000 {

        if !isDiv(p, val: i) {
            arr.append(i)
        }

    }

    return arr

}

var primes:Array<UInt32> = [2,3,5,7]

primes.reserveCapacity(20000)

var numbers = initArray(primes)

while numbers.count != 0 {
    numbers = numbers.filter( { return ($0 % primes[primes.count - 1] != 0 )} )

    if !numbers.isEmpty {
        primes.append(numbers[0])
    }

}

let result = primes.reduce(0, combine: +)

print (result)

This is a bit of a brute-force solution using trial division, with an optimization that you try dividing by just the previously discovered primes.

When you want to find many primes, though, a much faster algorithm is the sieve-of-eratosthenes.


Project Euler 91 (via HackerRank): Right triangles with integer coordinates This is my code to solve HackerRank's version of Project Euler Problem 91: on an N × N grid, find the number of possible right triangles where one vertex is (0, 0) and the other two vertices are integer grid points.

I know its an open challenge, but it is not for any job related coding, and I have also partially solved the problem and just need more optimized code.

#checks if the input 2 points, along with 3rd point as Origin, forms a right angle triangle
def righttri(a,b):
    temp=[]
    temp.append(((a[0]**2)+(a[1]**2))**(1/2))
    temp.append(((b[0]**2)+(b[1]**2))**(1/2))
    temp.append((((a[0]-b[0])**2)+((a[1]-b[1])**2))**(1/2))
    temp=sorted(temp)
    if temp.count(0)>0:
        return False
    elif round((temp[0]**2 )+ (temp[1]**2)) == round(temp[2]**2):
        done.append(sorted([a,b]))
        return True
    else:
        return False

n=int(input())+1
count=0
#coor stores all the 1-increment possible combination of points.
#ex:- for n=2, coor stores [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
#
#done stores the 2points data for all those val which forms right angle with origin
coor,done=[],[]
a,b=0,0
while a<n:
    while b<n:
        coor.append([a,b])
        b+=1
    b=0
    a+=1
for x in coor:
    for y in coor:
        if sorted([x,y]) not in done:
            if righttri(x,y)==True:
                count+=1

print(count)

Now, this code is tested on 9 different test cases, out of which 3 are showing OK. The rest 6 timeout. It means either my algorithm is faulty or I can just implement this algorithm efficiently. I would like to know how this code can be made fast.

righttri violates the single responsibility principle: it tests the triangle, and (possibly) appends it in the done list. Appending doesn't belong here.
righttri is surely working too hard: it calculates square roots just to compare them being squared back. Besides losing performance with unnecessary math, it also losing precision. With problems like this it is much better to stay within the integer domain.
Algorithm inspects every pair of dots on the grid, that is a time complexity is \$O(N^4)\$. That explains the timeouts. There are just too many pairs which do not form the right triangle.

A better algorithm would enumerate only right triangles via the Euclid's formula.


BST implementation in c++ I wanted to study c++ so i took up algorithms.I have implemented a Binary search tree with insert,delete,inorder and minimum element functions.I would like to have some feedback and some pointers to do things better.Anything is appreciated.

code:

#include <iostream>
using namespace std;

//This represents my tree node
struct node{
    int value;
    node *left,*right;
};

//minimum value of bst
node* minValue(node* tnode){
    if(tnode->left!=NULL){
        return minValue(tnode->left);
    }
    else{
        return tnode;
    }

}

//Inorder traversal
void inorder(node *tnode){
    if(tnode->left!=NULL){
        inorder(tnode->left);
    }
    cout<<tnode->value;
    if(tnode->right!=NULL){
        inorder(tnode->right);
    }

}

//insert function
node* insert(int val,node *tnode){
    node *temp=tnode;
    if(temp==NULL){
        temp= new node;
        temp->value=val;
        temp->left=NULL;
        temp->right=NULL;
    }
    else if(val<temp->value){
        temp->left=insert(val,temp->left);
    }
    else if(val>temp->value){
        temp->right=insert(val,temp->right);
    }

    return temp;
}

//delete function
node* deleteNode(node* tnode,int val){

    if(tnode==NULL)
        return tnode;
    if(tnode->value>val){
        tnode->left=deleteNode(tnode->left,val);
    }
    else if(tnode->value<val){
        tnode->right=deleteNode(tnode->right,val);
    }
    else{
        if(tnode->left==NULL){
            node* temp=tnode->right;
            delete tnode;
            return temp;
        }
        else if(tnode->right==NULL){
            node* temp=tnode->left;
            delete tnode;
            return temp;
        }
        node* temp=minValue(tnode->right);
        tnode->value=temp->value;
        tnode->right=deleteNode(tnode->right,temp->value);
    }
    return tnode;

}
int main(){
cout<<"Binary Tree";
node *ROOT=NULL;
ROOT=insert(3,NULL);
insert(2,ROOT);
insert(5,ROOT);
insert(4,ROOT);
insert(1,ROOT);
insert(7,ROOT);
insert(8,ROOT);
deleteNode(ROOT,3);
inorder(ROOT);
return 0;
}

Design
Lets start by saying this is a C solution to the problem. You need to think more about classes encapsulation and protecting the tree from random coders to make sure your enviranrts remain protected.
Something like:
class BSTTree
{
    struct node { /* As you had */ };

    node*   root;
    public:
        // Constructors/Destructors and assignment here.

        void insert(int value);
        void delete(int value);
        void inorder(std::function<void(int)> action) const;
        int minimum() const;
};

Notice how we hide root to make sure only the methods you write can manipulate the tree. Nobody else can come along and tinker with it. This means if your code is correct then the enviranrts can not be changed.
Also you have not dealt with any of the issues around memory management. So you need to think about this. Who owns each node how are you going to delete them safely. Also  look up the rule of three before you write your class (implement that are the rule of zero).
Also you should probably avoid NULL it modern C++ it has been replaced by nullptr. The advantage of nullptr is that it has a well defined type that is not convertible to an integer type.
Code Review
Sure this works:
node* minValue(node* tnode){
    if(tnode->left!=NULL){
        return minValue(tnode->left);
    }
    else{
        return tnode;
    }
}

Should you return a node? The function says min value. SO why not return the minimum value. Also you don't want to expose the implementation details and returning a node exposes these.
But is recursion the best way to handle this? Since you are simply traversing along a chain a simple loop seems much more effecient:
int minValue()
{
    if (root == nullptr) {
        throw std::runtime_exception("Blaaa");
    }
    node* last = root;
    for(;last->left; last = last->left) {
        /* Do nothing in the body */
    }
    return last->value;
}

The inorder() traversal works.
//Inorder traversal
void inorder(node *tnode){
    if(tnode->left!=NULL){
        inorder(tnode->left);
    }
    cout<<tnode->value;
    if(tnode->right!=NULL){
        inorder(tnode->right);
    }
}

But testing the left and right values before calling makes the code more difficult to read. More traditionally you test to see if the node is null before you start and simply return. Also you simply print the value. Why not pass a function that is called for each value that makes the traversal much ore interesting.
void inorder(std::function<void(int)> action) {
    doInorder(action, root);
}
void doInorder(std::function<void(int)> action, Node* node) {
    if (node == nullptr) {
        return;
    }
    doInorder(action, node->left);
    action(node->vallue);
    doInorder(action, node->right);
}

Then you can call the in-order with a function that prints each value.
tree.inorder([](int v){std::cout << v << " ";});

The insert function is basically correct.
//insert function
node* insert(int val,node *tnode){
    node *temp=tnode;
    if(temp==NULL){
        temp= new node;
        temp->value=val;
        temp->left=NULL;
        temp->right=NULL;
    }
    else if(val<temp->value){
        temp->left=insert(val,temp->left);
    }
    else if(val>temp->value){
        temp->right=insert(val,temp->right);
    }
    return temp;
}

The only difference I would do is make the new a single line.
temp= new node{val, nullptr, nullptr};

The delete node looks great.


Displaying items of a list in a text window In a game that I am making, I have a look in my game which goes through an array, and lists the things in the array

public void listOpen() {
    ArrayList<String> open = main.getOpenable();
    for(int i = 0; i < open.size(); i++) {
        String value = open.get(i);
        textWindow.append(value + "\n");
        error.setScroll();
    }
}

Sometimes when I'm running the program I notice that it lags slightly. Is there anyway I can make it more efficient?

Make only one textWindow.append() operation and consider using StringBuilder, to construct string to append.

public void listOpen() {
    ArrayList<String> open = main.getOpenable();
    StringBuilder appendBuilder = new StringBuilder();
    for(int i = 0; i < open.size(); i++) {
        String value = open.get(i);
        appendBuilder.append(value).append("\n");
        error.setScroll();
    }
    textWindow.append(appendBuilder.toString());
}


optimize binary classification method for speed I have the following code for determining TP, TN, FP and FN values for binary classification given two sparse vectors as input (using the sparse library):
def confused(sys1, ann1):
    # True Positive (TP): we predict a label of 1 (positive), and the true label is 1.
    TP = np.sum(np.logical_and(ann1 == 1, sys1 == 1))

    # True Negative (TN): we predict a label of 0 (negative), and the true label is 0.
    TN = np.sum(np.logical_and(ann1 == 0, sys1 == 0))

    # False Positive (FP): we predict a label of 1 (positive), but the true label is 0.
    FP = np.sum(np.logical_and(ann1 == 0, sys1 == 1))

    # False Negative (FN): we predict a label of 0 (negative), but the true label is 1.
    FN = np.sum(np.logical_and(ann1 == 1, sys1 == 0))

    return TP, TN, FP, FN

I'm trying to find a way to optimize this for speed. This is based on how-to-compute-truefalse-positives-and-truefalse-negatives-in-python-for-binary-classification-problems where my addition was to add the sparse arrays to optimize for memory usage, since the input vectors for the current problem I am trying to solve have over 7.9 M elements, and the positive cases (i.e., 1), are few and far between wrt the negative cases (i.e., 0).
I've done profiling of my code and about half the time is spent in this method.

Well, an obvious improvement is not redoing work. You are currently doing twice as much work as needed because you don't save the results of the comparisons:
def confused(sys1, ann1):
    predicted_true, predicted_false = sys1 == 1, sys1 == 0
    true_true, true_false = ann1 == 1, ann1 == 0

    # True Positive (TP): we predict a label of 1 (positive), and the true label is 1.
    TP = np.sum(np.logical_and(true_true, predicted_true))

    # True Negative (TN): we predict a label of 0 (negative), and the true label is 0.
    TN = np.sum(np.logical_and(true_false, predicted_false))

    # False Positive (FP): we predict a label of 1 (positive), but the true label is 0.
    FP = np.sum(np.logical_and(true_false, predicted_true))

    # False Negative (FN): we predict a label of 0 (negative), but the true label is 1.
    FN = np.sum(np.logical_and(true_true, predicted_false))
return TP, TN, FP, FN

This should speed up the calculation, at the cost of keeping things in memory slightly longer. Make sure you have enough memory available.
I'm not sure I got your true and predicted labels right, which goes to show that ann1 and sys1 are really bad names. Something like true and predicted would be vastly more readable. And while you're at it, write out the other variables as well. Characters don't cost extra.
np.logical_and works perfectly fine with integers (at least on normal numpy vectors, you should check that this is also the case for sparse vectors), so as long as your vectors can only contain 0 or 1, you can directly use the input vectors and save on half the memory:
not_true = np.logical_not(true)
not_predicted = np.logical_not(predicted)
true_positive = np.sum(np.logical_and(true, predicted))
true_negative = np.sum(np.logical_and(not_true, not_predicted))
false_positive = np.sum(np.logical_and(not_true, predicted))
false_negative = np.sum(np.logical_and(true, not_predicted))

Note that you cannot use ~true and ~predicted here, since that does two's complement or something like that...
Finally, note that I could not find proof for or against np.logical_and (or np.logical_not for that matter) being implemented efficiently for sparse vectors, so maybe that is where you actually loose speed. In that case, go and implement it in C and/or Cython and write a pull request for sparse, I guess...


Newsletter SQL query optimization I want to build a small newsletter like tool which sends mails in pre-defined timespans. First after registration, second 14 days later, third 7 days later etc.

I came across for two database designs and those two queries:

Create a separate table (dispatch) and put the next mails into it (can be done by cronjob):

SELECT
    m.campaign_id,
    s.name,
    s.email,
    m.subject,
    m.body
FROM sp_dispatch AS d
    INNER JOIN sp_subscribers AS s
        ON s.id = d.recipient_id
    INNER JOIN sp_mailings AS m
        ON m.id = d.mailing_id
WHERE d.time
    BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 31 DAY)
LIMIT 0, 500

Without a separate table and search through all mails when sending.

SELECT
    r.campaignID,
    DATE_ADD(r.startDate, INTERVAL t.timeOffset DAY) AS sendDate,
    r.name,
    r.email,
    t.subject,
    t.textMail
FROM cw_recipients AS r
    INNER JOIN cw_triggerMails AS t
        ON r.campaignID = t.campaignID
WHERE DATE_ADD(r.startDate, INTERVAL t.timeOffset DAY)
    BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 31 DAY)
LIMIT 0, 500

What do you think is the better/faster/more reliable solution?

DB Design #1
DB Design #2

Use EXPLAIN in the sql console to determine what the query optimizer is doing with each query.  This will give you clues as to how each query performs, and may also help you optimize further, for example by indicating places where adding an index will speed the query up.
Run each of the queries in a loop and benchmark how long they take! If you're using PHP just wrap the query execution in a for loop and execute it 100 times or so.  Record the microtime() before you start and do it again at the end.  Subtract the start time from the end time and you'll have your benchmark.


REST API for random number generation using Falcon I have written REST API for random number generation using Falcon(https://falconframework.org/), just for my learning process.

This is API:

http "localhost:8000/random-number?min=10&max=10"

This is code:

class RandomNumber:
    def on_get(self, req, resp):
        """Handles GET requests"""

        if 'min' not in req.params:
            raise falcon.HTTPMissingParam('min')

        if 'max' not in req.params:
            raise falcon.HTTPMissingParam('max')

        min_n = req.params['min']
        max_n = req.params['max']

        if min_n.isnumeric() == False:
            raise falcon.HTTPInvalidParam('min must be number', min_n)

        if max_n.isnumeric() == False:
            raise falcon.HTTPInvalidParam('max must be number', max_n)

        min_n =int(min_n)
        max_n =int(max_n)

        if min_n > max_n:
            raise falcon.HTTPInvalidParam('min is bigger than max', (min_n, max_n))

        number = random.randint(min_n, max_n)
        result = {'lowerLimit': min_n, 'higherLimit': max_n, 'number': number}
        resp.media = result

api = falcon.API()
api.add_route('/random-number', RandomNumber())

Feedback appreciated.

This is a small code, but there are some improvements to make:

useless copy-paste, what if you had 5 conditions?:

if 'min' not in req.params:
    raise falcon.HTTPMissingParam('min')

if 'max' not in req.params:
    raise falcon.HTTPMissingParam('max')

when you can do:

for c in ['min','max']:
  if c not in req.params:
      raise falcon.HTTPMissingParam(c)

don't test with == False

if min_n.isnumeric() ==  False:

should be

    if not min_n.isnumeric():


Given 2 numbers a and b find the smallest number greater than b by interchanging the digits of a and if not possible print -1 I have made the solution for this question it is based on backtracking. Can anyone suggest me how to make it work faster, I haven't practiced backtracking that much, although my solution seems to work. Please check it out

    class Test {
    static int ans = Integer.MAX_VALUE;

    static void f(char[] a, String s, int r, boolean[] vis, int b) {
        // System.out.println(s);
        if (r == 0) {
            int x = Integer.parseInt(s);
            if (x > b && x < ans)
                ans = Math.min(x, ans);

            return;
        }

        for (int i = 0; i < a.length; i++) {
            if (!vis[i]) {
                s += a[i];
                vis[i] = true;
                f(a, s, r - 1, vis, b);
                s = s.substring(0, s.length() - 1);
                vis[i] = false;
            }
        }

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] a = sc.next().toCharArray();
        // System.out.println(Arrays.toString(a));
        boolean[] vis = new boolean[a.length];
        int b = sc.nextInt();
        f(a, "", a.length, vis, b);

        System.out.println(ans == Integer.MAX_VALUE ? -1 : ans);

    }
}

vis array is used to track which element is already picked same thing
which we do in DFS.
r is the count of remaining elements in a.

main()

When I started your program the first time, I didn't know what to do, because your program didn't tell me. Before scanning a user input, I would tell the user to input something:

Scanner sc = new Scanner(System.in);
System.out.println("Please enter number a:");
char[] a = sc.next().toCharArray();
System.out.println("Please enter number b:");
int b = sc.nextInt();

For number a, I would also use an int instead of a char[]. Then I would convert it to a char[], but not in the main-function:

System.out.println("Please enter number a:");
int a = sc.nextInt();
System.out.println("Please enter number b:");
int b = sc.nextInt();

Another problem now is that your program doesn't validate the user input. The user could enter a letter instead of a number and your program would just break. This problem can be solved like this:

System.out.println("Please enter number a:");
int a;
while(true) {
    try {
        a = sc.nextInt();
        break;
    }
    catch(InputMismatchException e) {
         System.out.println("That's not a number!");
         sc.nextLine();
    }
}
System.out.println("Please enter number b:");
int b;
while(true) {
    try {
        b = sc.nextInt();
        break;
    }
    catch(InputMismatchException e) {
        System.out.println("That's not a number!");
        sc.nextLine();
    }
}

f()

Create a method static void f(int numberA, int b) that creates the char-array and the boolean[] vis and then calls the method static void f(char[] a, String s, int r, boolean[] vis, int b). That way you don't have to do work in the main-method that doesn't belong to the main-method:

static void f(int numberA, int b) {
        char[] a = ("" + numberA).toCharArray();
        boolean[] vis = new boolean[a.length];

        f(a, "", a.length, vis, b);
        System.out.println(ans == Integer.MAX_VALUE ? -1 : ans);

}

I don't like most of your variable names. You should (almost) always use names that tell the person who reads your code, what the variable does.

Your algorithm looks solid. Well done.

All in all it looks like this:

import java.util.Scanner;
import java.util.InputMismatchException;

class ChangeDigits {
    static int ans = Integer.MAX_VALUE;

    static void changeDigits(int numberA, int numberB) {
        char[] change = ("" + numberA).toCharArray();
        boolean[] pickedElements = new boolean[change.length];
        int remaining = change.length;
        changeDigits(change, "", remaining, pickedElements, numberB);
        System.out.println("Solution: " + (ans == Integer.MAX_VALUE ? -1 : ans));

    }

    static void changeDigits(char[] change, String substring, int remaining, boolean[] pickedElements, int numberB) {
        if (remaining == 0) {
            int substrNumber = Integer.parseInt(substring);
            if (substrNumber > numberB && substrNumber < ans)
                ans = Math.min(substrNumber, ans);
            return;
        }

        for (int i = 0; i < change.length; i++) {
            if (!pickedElements[i]) {
                substring += change[i];
                pickedElements[i] = true;
                changeDigits(change, substring, remaining - 1, pickedElements, numberB);
                substring = substring.substring(0, substring.length() - 1);
                pickedElements[i] = false;
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter number a:");
        int numberA;
        while(true) {
            try {
                numberA = sc.nextInt();
                break;
            }
            catch(InputMismatchException e) {
                System.out.println("That's not a number!");
                sc.nextLine();
            }
        }

        System.out.println("Please enter number b:");
        int numberB;
        while(true) {
            try {
                numberB = sc.nextInt();
                break;
            }
            catch(InputMismatchException e) {
                System.out.println("That's not a number!");
                sc.nextLine();
            }
        }

        changeDigits(numberA, numberB);
    }
}


Circular shift string So,basically I am working on an algorithm for circular shifting a string upto a position.There are two parameters required here.

String of text to shift
Number of shifts required.

For example:

Given string: "Hello"
Shifts: 3
Final: "ollHe"

Ignore the limited validation here.

import java.lang.*;
import java.util.*;

public class Shift
{
public String shifting(String s,Integer sh)
{
    Integer shifts=sh;
    String string=s;
    char[] shifted=new char[50];
    Integer pos=0;
    for(int i=string.length();i>shifts;i--)
    {
        System.out.println(""+i);
        pos++;
        shifted[pos]=string.charAt(i-1);
    }
    System.out.println("Shifting complete");
    for(int j=0;j<shifts;j++)
    {
        System.out.println(""+j);
        pos++;
        shifted[pos]=string.charAt(j);
    }
    return new String(shifted);
}
public static void main(String[] args) {
        System.out.println("Enter the string ");
        Scanner sc=new Scanner(System.in);
        String string=sc.nextLine();
        System.out.println("So you have entered:"+string);
        System.out.println("Enter the number of shifts:");
        Integer shifts=sc.nextInt();
        System.out.println("number of shifts is:"+shifts.toString());
        System.out.println("Shifted string:"+new Shift().shifting(string,shifts));
    }
}

`
Give your views on the code here.

Do you have any constraints?

Is your example wrong? I would expected lloHe
If you are concerned of modifying your method parameters make them final. Copy it to local variables and don't change the variables is kind of useless.
Don't use short variables names (exception: the loop counter)
If you use Integer instead of int, you should handle null
Maybe a simple static utility method would be fine here, except you have some inheritance scenario in mind.
Maybe you should also indicate that you are shifting left for positive numbers
You should take the length of you string for the char.

Assuming your example is wrong, what about:

import java.util.Objects;
import javax.annotation.Nonnull;

public final class Shift
{
    @Nonnull
    public static String left( @Nonnull final String string, final int shift )
    {
        final int length = string.length();
        if( length == 0 ) return "";
        final int offset = ((shift % length) + length) % length; // get a positive offset

        return string.substring( offset, length ) + string.substring( 0, offset );
    }

    public static void main( String... args )
    {
        assertEquals( "loHel", Shift.left( "Hello", -2 ) );
        assertEquals( "oHell", Shift.left( "Hello", -1 ) );
        assertEquals( "Hello", Shift.left( "Hello", 0 ) );
        assertEquals( "elloH", Shift.left( "Hello", 1 ) );
        assertEquals( "lloHe", Shift.left( "Hello", 2 ) );
        assertEquals( "loHel", Shift.left( "Hello", 3 ) );
        assertEquals( "oHell", Shift.left( "Hello", 4 ) );
        assertEquals( "Hello", Shift.left( "Hello", 5 ) );
        assertEquals( "elloH", Shift.left( "Hello", 6 ) );
        assertEquals( "", Shift.left( "", 3 ) );
    }

    private static void assertEquals( String expected, String actual )
    {
        if( !Objects.equals( expected, actual ) ) throw new AssertionError( "Expected: >" + expected + "< was: >" + actual + "<" );
    }
}