texts: Minimize the number of workers/test rigs, with a capacity constraint, using Excel VBA Solver loop

texts:
I created a code that runs 60 optimizations through solver through 4 series of loops (each with 15 'iterations'). The code works great but it is taking FOREVER to run through (over an hour). Each optimization is a simple linear model (global solution is found), just changing which month I am looking at.

Problem setup: I am trying to minimize the number of workers/test rigs that will satisfy the intake with the constraint of being under capacity.

I am not sure how I can make it go any faster, but I cannot send this to other people and expect them to use it. Does anybody have any suggestions? I have time sequenced it and each iteration takes equally as long. There is no bottleneck.

Below is my code:

Sub Optimization()

Application.ScreenUpdating = False

'Unlocks workbook to allow updating
Call Unlock_Workbook

'Makes visible and selects the tab where the optimization problem is set up
Sheets("Optimization").Visible = True
Sheets("Optimization").Select

'Clear variable ranges that solver will change
Range("Worker_All[[1]:[15]]").Clear
Range("TestRig_All[[1]:[15]]").Clear
Range("Worker_787[[1]:[15]]").Clear
Range("TestRig_787[[1]:[15]]").Clear

'Install the Add-in for users who have no done this already
AddIns("Solver Add-in").Installed = True

'Optimized All Workers

For i = 1 To 15

'Cell address for objective
Min = Cells(3, 2 + i).Address
'Cell adress for variable
Variable = Range("Worker_All[" & i & "]").Address
'Cell address for constraint range
ConstraintRange = Range("IntakeHours_NonKeyWO[" & i & "]").Address
'Cell address for constrants
Constraint = Range("IntakeHours_NonKeyWOC[" & i & "]").Address

SolverReset

SolverOk SetCell:=Min, MaxMinVal:=2, ValueOf:=0, ByChange:=Variable, _
    Engine:=2, EngineDesc:="Simplex LP"
SolverAdd CellRef:=ConstraintRange, Relation:=1, FormulaText:=Constraint
SolverSolve True

Next i

'Optimized All Test Rigs

For i = 1 To 15

Min = Cells(4, 2 + i).Address
Variable = Range("TestRig_All[" & i & "]").Address
ConstraintRange = Range("IntakeHours_NonKeyMO[" & i & "]").Address
Constraint = Range("IntakeHours_NonKeyMOC[" & i & "]").Address

SolverReset

SolverOk SetCell:=Min, MaxMinVal:=2, ValueOf:=0, ByChange:=Variable, _
    Engine:=2, EngineDesc:="Simplex LP"
SolverAdd CellRef:=ConstraintRange, Relation:=1, FormulaText:=Constraint
SolverSolve True

Next i

'Optimized 787 Workers

For i = 1 To 15

Min = Cells(5, 2 + i).Address
Variable = Range("Worker_787[" & i & "]").Address
ConstraintRange = Range("IntakeHours_Key787WO[" & i & "]").Address
Constraint = Range("IntakeHours_Key787WOC[" & i & "]").Address

SolverReset

SolverOk SetCell:=Min, MaxMinVal:=2, ValueOf:=0, ByChange:=Variable, _
Engine:=2, EngineDesc:="Simplex LP"
SolverAdd CellRef:=ConstraintRange, Relation:=1, FormulaText:=Constraint
SolverSolve True

Next i

'Optimized 787 Test Rigs

For i = 1 To 15

Min = Cells(6, 2 + i).Address
Variable = Range("TestRig_787[" & i & "]").Address
ConstraintRange = Range("IntakeHours_Key787MO[" & i & "]").Address
Constraint = Range("IntakeHours_Key787MOC[" & i & "]").Address

SolverReset

SolverOk SetCell:=Min, MaxMinVal:=2, ValueOf:=0, ByChange:=Variable, _
    Engine:=2, EngineDesc:="Simplex LP"
SolverAdd CellRef:=ConstraintRange, Relation:=1, FormulaText:=Constraint
SolverSolve True

Next i

Sheets("Cell Summary").Select
Sheets("Optimization").Visible = False

Call Lock_Workbook

Application.ScreenUpdating = True

End Sub


texts: Separation of database and network calls

texts:
I have a API and the corresponding service method is like this:

@Transactional
public List<Object> getData(args) {
    databaseStatements;
    databaseStatementsOnOtherThread();
    NetworkCallStatements;
    CPUBoundStatements(calculation, sorting, etc)
}

With this setup, each request holds a database connection as I have @Transactional annotation on the method. So If I have 5 connections to database, I was able to serve only 5 requests concurrently. Other requests are waiting for database connection and gets timed-out.
I have refactored this method to avoid blocking of database connection during network and CPU tasks:

public List<Object> getData(args) {
    getDataFromDatabase1();
    databaseStatementsOnOtherThread();
    getDataFromDatabase2();
    NetworkCallStatements;
    CPUBoundStatements(calculation, sorting, etc)
}

/* The below methods are from different classes as @Transactional will not effect in same class */
@Transactional
public Data1 getDataFromDatabase1() {
    //some statements
}

@Transactional
public Data2 getDataFromDatabase2() {
    //some statements
}

As I have removed @Transactional annotation from main method and added to sub methods having only database calls, connection will not be blocked during network calls and CPU tasks. This gives a very good result as I am able to serve more no of requests with less no of connections and the failure rate also reduced.
Now the problem is when there is one or two requests at a time (or very few requests), the response time has been increased from the previous version. What I feel is, earlier I was obtaining database connection one time, now obtaining 2 or more time(one for each sub methods) which may cause this slowness.

Whether my understanding is correct?
How can I organize the sub methods into a single method to obtain the connection only once? All these sub methods are returning different data types and also I cannot have them in the same class due to @Transactional limitations. Any idea to overcome the above problems?
Or can I refactor the initial version of getData differently?


texts: Securing access to PHP services from Javascript Ajax calls

texts:
I am creating a generic PHP handler, or "dispatcher", for Ajax calls made from Javascript running on the page served by PHP. The handler will take posted data and, depending on a value, decide how to route the call further. WordPress is being used, but my question is not specific to WordPress (if it was I would have asked on the WordPress sub-site).

The PHP Ajax handler should be secured, not serving calls from anywhere other than the page that the visitor is viewing in the browser (assuming that Apache is not configured to restrict access to the handler). I have designed a security structure with this aim in mind, and would like to know if my approach is good or could be improved. Are there any weaknesses that could be exploited?

The design is quite simple. When the page is constructed for serving, a session cookie is created, and its value is an encrypted token, derived from the client's IP, and a random salt. The cookie's value, the encrypted token, is also used as the name of a temporary db record for 60 minutes, and the record's value is the salt that was used to generate the token.

$ajaxSalt = bin2hex(openssl_random_pseudo_bytes(30)); // Create random salt
$cookieAndTrans = md5(crypt($_SERVER['REMOTE_ADDR'], $ajaxSalt)); // We use cookie value as token
setCookie('qnrwp_ajax_cookie', $cookieAndTrans); // Set session cookie, for JS Ajax caller to echo back to us
set_transient('qnrwp_ajax_temp_salt_'.$cookieAndTrans, $ajaxSalt, 60 * MINUTE_IN_SECONDS); // Save salt for 60 mins

When the page is served and the client makes an Ajax request, the Javascript code will, as part of the data payload, send the value of the cookie to the handler.

Receiving the call, the handler will check that the transmitted cookie value matches the cookie value that was set initially. It then uses this value as the name of the temporary db record to call up (if 60 minutes haven't passed), and uses the value of the record, the salt, to confirm that the IP matches.

// ----------------------- Security check
if ($_POST['qnrwp_ajax_cookie'] !== $_COOKIE['qnrwp_ajax_cookie']) wp_die();
$ajaxTrans = get_transient('qnrwp_ajax_temp_salt_'.$_POST['qnrwp_ajax_cookie']);
if (!$ajaxTrans) wp_die();
if ($_POST['qnrwp_ajax_cookie'] !== md5(crypt($_SERVER['REMOTE_ADDR'], $ajaxTrans))) wp_die();

A couple of things to clarify to avoid confusion: md5() is primarily used as replacement for bin2hex(), its weak crypto security just an added bonus, but not being relied upon - crypt() is used for good encryption. crypt() is able to generate a salt, but I prefer creating my own as I'll store it in the temporary record (set/get_transient() in the code). The salt is quite long, helping avoid clashes between different clients accessing the site at the same time. A clash is still possible, but I think unlikely, and even if it happened, it would not be catastrophic - if the clashing clients share their IP, one of them may find up to 120 minutes available after page load rather than 60, no big deal.

I believe my question is at quite an advanced level and would prefer if those with good knowledge of the subject write answers rather than try to engage in discussion in the comments. That said, if anything is unclear, feel free to ask in the comments. If you see weaknesses in the code, please try to provide concrete suggestions for improvement, rather than merely pointing out the loopholes. The most constructive answer will be accepted. I will wait a few days before accepting.

Please note I'm not open to suggestions of third-party tools or WordPress plugins - this is a case of "rolling my own".


texts: Schema for allowing adding tags to entities in unrelated tables

texts:
I'm implementing a "tags" features to an already working solution.

Final users need to be able to add tags to three separate sections of the solution:

Posts
Accounts
Groups

Each section has its corresponding table in the database, and each table contains a unique ID:

PostID
AccountID
GroupID

I'm thinking about implementing a tag schema similar to the one that Wordpress uses, where I'll have:

A tagmap table that will contain a unique ID for each "tagmap".
A foreign key to each section's table ID.
Another foreign key to the ID of the tag.

CREATE TABLE Posts(
  PostID int(2) NOT NULL AUTO_INCREMENT,
  Content varchar(255),
  PRIMARY KEY(PostID)
 );

CREATE TABLE Groups(
  GroupID int(2) NOT NULL AUTO_INCREMENT,
  GroupName varchar(255),
  PRIMARY KEY(GroupID)
 );

CREATE TABLE Links(
  LinkID int(2) NOT NULL AUTO_INCREMENT,
  href varchar(255),
  PRIMARY KEY(LinkID)
 );

CREATE TABLE tagmap(
  TagmapID int(2) NOT NULL AUTO_INCREMENT,
  PostID int(2),
  GroupID int(2),
  LinkID int(2),
  TagID int(2) NOT NULL,
  PRIMARY KEY(TagmapID)
 );

CREATE TABLE tags(
  TagID int(2) NOT NULL AUTO_INCREMENT,
  TagName varchar(255) NOT NULL,
  PRIMARY KEY(TagID)
);

I like this approach because it is decently normalized, but queries might get a bit complex. Also, it will have a lot of NULL columns every time a tag is assigned to a post but not to an account or to a group, so I'm unsure how it will behave performance-wise (the table will hold around 100,000 records almost immediately).

Is there a better alternative to this?


texts: Nearly Illegible Prime Number Calculator

texts:
This is my first reasonably large C# program. Considering my code looks like it was written in an esolang to me, I'm guessing I did something wrong at some point. My main priority is readability, although speed is also a plus.

using System;

public class PrimeSearcher {
    public static string Stringilate (int[] iterable) {
        string returnable="";
        foreach(int i in iterable){
            returnable+=i.ToString()+" ";
        };
        return returnable;
    }
    public static bool isPrime (int[] iterable,float target) {
        foreach(int i in iterable){
            if(target%i==0){
                return false;
            }
            if(i==0){ return true; }
            //reached the "end" (last discovered prime) of the array, hurray!
        }
        return true; //will only do anything for the last element
    }
    static public void Main () {
        int[] primes=new int[1000];
        float j=2; //float because I think it's the smallest datatype that can return a non-zero number when divided, correct me if I'm wrong
        while(primes[primes.Length-1]==0){
            if(isPrime(primes,j)){
                primes[Array.IndexOf(primes,0)]=(int)j;
            }
            j++;
        }
        Console.WriteLine(Stringilate(primes));
        //I don't *think* ToString worked when I tried it.
    }
}


texts: Use of dictionary for file-saving function

texts:
I have the following function:

def save_to_file(data, columns_order, save_filename):
    """ Saves data to file
    :param data: data to be saved
    :param columns_order: the order of columns
                          if needed to rearrange
                          else put ""
    :param save_filename: the filename (csv, xls, xlsx)
    :return: True if done or None if error
    """
    try:
        df = pd.DataFrame(data)
        if columns_order:  # Rearrange columns if needed
            df = df[columns_order]
        if save_filename.endswith(".csv"):  # Save as csv
            df.to_csv(save_filename, index=False)
            return True
        elif any(save_filename.endswith(ext) for ext in [".xls", ".xlsx"]):  # Save as xls or xlsx
            df.to_excel(save_filename)
            return True
        else:
            print "Choose between csv, xls, xlsx."
            return None
    except Exception, e:
        print "Save_to_file error:", e
        return None

I don't know if I should leave it like that or put the functions in a dictionary:

func_dict = {".csv": df.to_csv(save_filename, index=False), ".xlsx:": df.to_excel(save_filename)}


texts:
I want to find a more elegant way to build a recursive sorting function.

The function randomly selects one of the numbers from a given list, k, and splits the list into three: list of smaller numbers, equal to and greater than k.

Now, I recursively sort each list and then combine the 3 to a single sorted list.

For example:

The list [5,2,7,4,9,3] for k = 5 is split into three: [7,9], [5], [2,4,3].

Now sort recursively to get the lists [7,9] and [2,4,3] in order to receive [7,9] and [2,3,4].

The three lists sorted for the receipt of one sorted list: [2,3,4,5,7,9]

Any suggestions?

def partition(lst,k):
    bigger = []
    smaller = []
    equal = []
    i =  0
    while i < len(lst) :
        if lst[i] < k:
            smaller.append(lst[i])
            i += 1
        elif lst[i] == k:
            equal.append(lst[i])
            i += 1
        else:
            bigger.append(lst[i])
            i += 1
    # merge 3 list:
    merged = [smaller,equal ,bigger]
    return merged

def my_sort(lst):
    import random
    k = (random.choice(lst))
    lst3 = partition(lst,k)

    def merge(left, right):
        merged = []

        left_i, right_i = 0, 0
        while left_i < len(left) and right_i < len(right):
            if left[left_i] <= right[right_i]:
                merged.append(left[left_i])
                left_i += 1
            else:
                merged.append(right[right_i])
                right_i += 1
        # copy remaining:
        merged += left[left_i:] + right[right_i:]
        return merged

    def mergesort(lst):
        if len(lst) <= 1:
            return lst

        middle = len(lst) / 2
        left = mergesort(lst[: middle])
        right = mergesort(lst[middle:])
        return merge(left, right)

    left = mergesort(lst3[0])
    right = mergesort(lst3[2])

    print k
    return merge(merge(left,lst3[1]),right)


texts: CSS attribute Selector: responsive grid performance

texts:
I am looking to create a simple column based responsive framework, and would like to call my classes something like:

where the div would display as 4 columns on a desktop browser, 2 columns on a tablet and one column on a phone (these breakpoints are hypothetical).

or

where the div would display as 8 columns on a desktop browser, 4 columns on a tablet and 2 columns on a phone.

The easiest way would be to use attribute selectors, but I am not sure if this would be a huge performance hit. Is this a good idea, or should I write out all the permutations of the column structure (720 classes)? Or just do what Bootstrap does, with col-sm-12 col-md-6 col-lg-3

// columns
.col[class|="c-6"] { width: 16.666%; }
.col[class|="c-5"] { width: 20%; }
.col[class|="c-4"] { width: 25%; }
.col[class|="c-3"] { width: 33.333%; }
.col[class|="c-2"] { width: 50%; }
.col[class|="c-1"] { width: 100%; }

@media only screen and (max-width:800px) {
  .col[class*="-6-"]  { width: 16.666%; }
  .col[class*="-5-"]  { width: 20%; }
  .col[class*="-4-"]  { width: 25%; }
  .col[class*="-3-"]  { width: 33.333%; }
  .col[class*="-2-"]  { width: 50%; }
  .col[class*="-1-"]  { width: 100%; }
}

@media only screen and (max-width:600px) {
  .col[class$="-6"] { width: 16.666%; }
  .col[class$="-5"] { width: 20%; }
  .col[class$="-4"] { width: 25%; }
  .col[class$="-3"] { width: 33.333%; }
  .col[class$="-2"] { width: 50%; }
  .col[class$="-1"] { width: 100%; }
}


texts: More elegant way to get timestamp from day/hour? (Perl)

texts:
http://coolwx.com/buoydata/data/curr/all.html provides weather data
from ships (or maybe buoys) at sea, but the date is given as "25/18" (meaning the 18th
hour of the 25th day of the month, all times GMT).

I need to convert this to a full timestamp ("2013-09-25
18:00:00"). All reports are in the near past. One glitch: if today is the
1st, and the report is "31/18" for example, it's referring to the 31st
of last month.

Here's my solution, which I feel is kludgey. Can it be improved?

sub ship2time {
  my(@now) = gmtime(time());
  # assumed given as "foo/bar";
  my($day,$hour) = split(/\//, $_[0]);
  # today's date (GMT)
  my($today) = strftime("%d", @now);
  # if the given day is in the past (or very near future) it's this month
  if ($day <= $today+1) {
    return strftime("%Y-%m-$day $hour:00:00", @now);
  }

  # otherwise, we're referring to last month (eg, today is the 1st,
  # report says 31st)
  my($year, $month) = split(/\-/, strftime("%Y-%m", @now));
  $month--;
  if ($month < 0) {$year--; $month=12;}

  return "$year-$month-$day $hour:00:00";
}


texts:
I have inside a Backbone model validate function several if statements:

if(!attrs.firstAttr) errorMsg+="first attr mandatory";
if(!attrs.secondAttr) errorMsg+="second attr mandatory";

as you can see there is a lot of repetition (I have actually 10 fields to check with this.

I was thinking of introducing an interface Validator with a method validate. Then implement a MandatoryFieldValidator with:

validate: function(attr, errors) {
   if(!attr) errors.push(attr + " mandatory");
}

So the validate method would become like this:

mandatoryFieldValidator.validate(attr.firstAttr, errors); // errors now is an array
mandatoryFieldValidator.validate(attr.secondAttr, errors);

Would you think is a good solution? I still don't like it because I need to rewrite it a lot of times. Next step would be to have some kind of foreach but I don't have a list of parameters. The attributes passed by the Backbone validate is an object with the properties of the model.

Would it be ok to declare an array with every element I want to validate like [attr.firstAttr, attr.secondAttr]?

If yes, how would it be possible to do it in Backbone? I'm referring to the fact that _.each function from underscore.js just passes an array with a callback function and I still need to pass the error array and apply the validate method of my Validator.


texts:
I'm trying to implement some simple logic using rails. Code is working fine but its look uglier to me, could any one help me on refractor or implement in a efficient way.

Context:

Data hash assetType key which can be image/clip and Sizes key has array of hashes.

if asset type is image then I'm filtering Sizes array if each hash has TeeShirtSize or not.

Same way all Sizes array of hashes should have Description and be unique.

each statement runs only if Sizes array not empty.

def get_parsed_asset_data(data)
     data['Sizes'].select!{|size| size.key?('TeeShirtSize')} if data['AssetType'] == 'Image' && data['Sizes'].any?
     data['Sizes'].select!{|size| size.key?('Description')} if data['Sizes'].any?
     data['Sizes'].uniq!{|size| size['Description']} if data['Sizes'].any?
     data
  end


texts: Go implementation of dining philosophers

texts:
Follow-up of this question.

Questions:

is this code a correct implementation of the Dining Philosophers problem?
what is there to improve regarding Go Best Practices?

package main

import (
    "fmt"
    "sync"
)

func philos(id int, left, right chan bool, wg *sync.WaitGroup) {
    fmt.Printf("Philosopher # %d wants to eat\n", id)
    <-left
    <-right
    left <- true
    right <- true
    fmt.Printf("Philosopher # %d finished eating\n", id)
    wg.Done()
}

func main() {
    const numPhilos = 5
    var forks [numPhilos]chan bool
    for i := 0; i < numPhilos; i++ {
        forks[i] = make(chan bool, 1)
        forks[i] <- true
    }
    var wg sync.WaitGroup
    for i := 0; i < numPhilos; i++ {
        wg.Add(1)
        go philos(i, forks[(i-1+numPhilos)%numPhilos], forks[(i+numPhilos)%numPhilos], &wg)
    }
    wg.Wait()
    fmt.Println("Everybody finished eating")
}


texts: Is there a better way to optimize these conditionals?

texts:
Public Function MyProjectFuncion(firstObject As Object,
                                 myEnumeration As Enumeration,
                                 theString As String,
                                 someCollection As List(of Object),
                                 anotherObject As Object) _
                            As String

    If IsNothing(firstObject) Then
        Throw New Exception("No data was received in 'firstObject'")
    Else
        If IsNothing(anotherObject) Then
            Throw New Exception("No data was received in 'anotherObject'")
        Else
            If IsNothing(myEnumeration) OrElse myEnumeration = 0 Then
                Throw New Exception("No data was received in 'myEnumeration'")
            Else
                If IsNothing(theString) OrElse theString = String.Empty Then
                    Throw New Exception("No data was received in 'theString'")
                Else
                    If IsNothing(someCollection) OrElse someCollection.Count = 0 Then
                        Throw New Exception("No data was received in 'someCollection'")
                    End If
                End If
            End If
        End If
    End If

    Return "All the variables have value"
End Function


texts: Speeding up naive backtracking Sudoku Solver in Haskell

texts:
After watching a recent computerphile video on building a very simple sudoku solver I tried to implement the same in Haskell. From this CR question I learned that it is probably a better idea to use Vectors instead of just lists to represent a grid of numbers that is gonna be mutated. (But it is supposedly still worse than using a sparse representation.) And from this one (and another question of mine) I learned about Control.Lens, but I decided against using it to avoid using many different packages that I'm not familiar with.

Now the program I wrote is close to the original in python, but very slow. So I would like to get some feedback on how to speed it up without deviating form this very simple aproach too much.

The Program

The code defines a Board that represents a (solved or unsolved) state, with zeros for the entries that area yet to be determined. It can be indexed using Coordinates. Then there are a few setters and getters that probably could be replaced by using Control.Lens - but I'd like to avoid that for now as I just want to focus on the performance. Then there is a possible function which takes a Board, Coordinates and a candidate number an just reports whether it is possible to put the candidate at some given coordinates. Finally there is solve that does the backtracking.

So far I tried to add a take 1 $ or take 1 $! to speed it up (but only returning at most a single solution), but without success.

--https://www.youtube.com/watch?v=G_UYXzGuqvM
import qualified Data.Vector as V

data Board = Board (V.Vector (V.Vector Integer))
type Coordinates = (Int, Int)

instance Show Board where
 show (Board b)=unlines . V.toList $ V.map show b

fromList :: [[Integer]] -> Board
fromList l = Board $ V.fromList $ V.fromList <$> l

-- a few setters and getters
(!):: Board -> Coordinates -> Integer
(Board b) ! (i,j) = (b  V.! j)V.! i

getColumn :: Board -> Coordinates -> [Integer]
getColumn b (i, _) = [b ! (i, j) | j<-[0..8]]

getRow :: Board -> (Int, Int) -> [Integer]
getRow b (_, j) = [b ! (i, j) | i<-[0..8]]

getSquare :: Board -> Coordinates -> [Integer]
getSquare b (i, j) = [b ! (i'*3 + u, j'*3 + v) | u<-[0..2],v<-[0..2]]
 where i' = i `div` 3
       j' = j `div` 3

insert :: Board -> Coordinates -> Integer -> Board
insert (Board b) (i, j) k = Board b'
 where v = b V.! j
       v' = v V.// [(i, k)]
       b' = b V.// [(j, v')]

-- check whether it is possible to insert candidate at given position
possible :: Board -> Coordinates -> Integer -> Bool
possible b coords@(i, j) k
 |i < 0 || i >= 9 || j < 0 || j >= 9 || k < 0 || k > 9 = undefined
 |b ! coords > 0 = False
 |k `elem` getRow b coords = False
 |k `elem` getColumn b coords = False
 |k `elem` getSquare b coords = False
 |otherwise = True

-- check whether board is already full
full :: Board -> Bool
full b = 0 `notElem` [b ! (i,j) | i<-[0..8], j<-[0..8]]

-- recursion to find all solutions to a given board
solve :: Board -> [Board]
solve b
 |full b = [b]
 |otherwise =  concat[ solve $! (insert b (x, y) n)|
                       x<-[0..8],
                       y<-[0..8],
                       n<-[1..9],
                       possible b (x,y) n]

main = print $ solve b

-- normal sudoku
b :: Board
b = fromList [
 [5,3,0,0,7,0,0,0,0],
 [6,0,0,1,9,5,0,0,0],
 [0,9,8,0,0,0,0,6,0],
 [8,0,0,0,6,0,0,0,3],
 [4,0,0,8,0,3,0,0,1],
 [7,0,0,0,2,0,0,0,6],
 [0,6,0,0,0,0,2,8,0],
 [0,0,0,4,1,9,0,0,5],
 [0,0,0,0,8,0,0,7,9]
 ]

-- only one a few entries are missing
c :: Board
c = fromList [
 [0,0,0,0,9,4,5,6,1],
 [9,2,1,5,3,6,8,7,4],
 [4,5,6,7,8,1,9,2,3],
 [1,4,7,3,5,9,2,8,6],
 [2,8,3,6,1,7,4,5,9],
 [5,6,9,8,4,2,3,1,7],
 [6,7,4,9,2,5,1,3,8],
 [8,9,2,1,7,3,6,4,5],
 [3,1,5,4,6,8,7,9,2]]

Try it online!


texts: Increase Perfomance of Nested Object Instantiation C#

texts:
I'm filling a list of items from the database, which is taking extremly long time to run (Approx 60 seconds)

Here is the code with the relevant breakdown

97% of the performance issues come in the foreach loop at the bottom.   Is there a better way to match list items on the nested object.

private List<ROPSetting> TestMethod()
{

    using DataSet ds = DB.GetDs($"EXEC GetSupplyChainROP_C @DateFrom={DateTime.Now.AddYears(-1).ToDbQuote()}");
    // load DS takes 3 seconds.

    using var itemReader = ds.Tables[0].CreateDataReader();

    var items = itemReader.ParseList<ROPSetting>().ToList();

    using var usageReader = ds.Tables[1].CreateDataReader();
    var usageItems = usageReader.ParseList<ItemUsage>().ToList();

    // 97% of the performance impact is below.  Is there a better way to match
    foreach (var itm in items)
    {
        itm.Usage = usageItems
                     .Where(a => a.ItemCode == itm.ItemCode && a.WarehouseCode == itm.WarehouseCode).ToList();

    }

    return items;

}

public class ItemUsage
{
    public DateTime Date { get; set; }
    public decimal Quantity { get; set; }
    internal string ItemCode { get; set; } // for matching
    internal string WarehouseCode { get; set; } // for matching
}
public class ROPSetting
{
    public string ItemCode { get; set; }
    public string PartNumber { get; set; }
    public decimal AverageCost { get; set; }
    public string SupplierCode { get; set; }
    public string SupplierName { get; set; }
    public int TotalLeadTime { get; set; }
    public int IntervalTime { get; set; }
    public int ReorderPoint { get; set; }
    public string WarehouseCode { get; set; }
    public IList<ItemUsage> Usage { get; set; }
}


texts: Building HTML with JS (function expression vs comment for code clarity)

texts:
I'm currently building some HTML with JS (specifically using the jQuery library). My code is as follows:

goToError: function (error, returnView, returnController, callback) {
    console.log(error);

    this._transitionOutPanel(function () {
        loadError();
    });

    var loadError = function () {
        $("#panel").html("");
        navigation.setHeader("Error");

        (function buildErrorHtml() {
            var ctrlDiv = $('

', {
                style: "text-align: center;"
            });

            var ctrlP1 = $("

", {
                html: ("An error occured while loading this view.
Error code: {0} ({1})").format(error.status, error.statusText)
            }).appendTo(ctrlDiv);

            var ctrlP2 = $("

").appendTo(ctrlDiv);

            var ctrlBackBtn = $("<button></button>", {
                html: "Back",
                class: "btn btn-danger"
            }).appendTo(ctrlP2);
            ctrlBackBtn.click(function () {
                if (returnView != null && returnController != null) {
                    navigation.go(returnView, returnController);
                } else {
                    navigation.go("Login", "Accounts", null, "Login");
                }
            });

            ctrlDiv.appendTo("#panel");
        })();

        navigation._transitionInPanel(function () {
            if (typeof callback === "function")
                callback.call(this);
        });
    };

    loadError();
}

More specifically, my question is in regards to when it is best to use a comment vs a function expression for code clarity. I currently have a self-invoking function expression named buildErrorHtml() that constructs the HTML and appends it to the relevant element on my page.

Would this be better outside of a function expression (amongst the rest of the code in var loadError = function() { ... }) or should it be as it is, inside an anonymous function? I've been told to only use comments to tell readers 'why' instead of 'what', and I don't feel using a comment is justified with this criteria.

The alternative is using a comment like so:

// Build HTML
var ctrlDiv = $('

', {
    style: "text-align: center;"
});
...

Which method tells anybody reading my code what it is the code is doing best?

Feedback on anything else is also greatly welcome.


texts:
Design a class TIME which stores hour, minute and second. The class
  should have

  the methods to support the following:

  User may give the time value in 24-hour format.
  User may give the time value in AM/PM format
  Display the time in 24-hour format.
  Display the time in AM/PM format.
  User may like to add minute with a time value.

#include <iostream>
#include <string>

class Time {
    public:
        Time(int h = 0, int m = 0, int s = 0, const std::string &am_or_pm = "");
        void print_time24();
        void print_time12();
        void add_min();
    private:
        int hr;
        int min;
        int sec;
};

Time::Time(int h, int m, int s, const std::string &am_or_pm):
    hr(h), min(m), sec(s)
{
    if (am_or_pm == "AM" && hr == 12)
        hr = 0;
    if (am_or_pm == "PM" && hr != 12)
        hr += 12;
}

void Time::print_time24()
{
    std::cout << hr << ':' << min << ':' << sec;
}

void Time::print_time12()
{
    if (hr == 0 || hr == 12)
        std::cout << "12";
    else
        std::cout << hr % 12;
    std::cout << ':' << min << ':' << sec;

    if (hr / 12 == 0)
        std::cout << " AM";
    else
        std::cout << " PM";
}

void Time::add_min()
{
    if (min == 59)
        hr = (hr + 1) % 24;

    min = (min + 1) % 60;
}

// To test the class
int main()
{
    using namespace std;
    cout << "Choose option(1. 12 hr format 2. 24 hr format): ";
    int op;
    cin >> op;
    cout << "Enter the time: ";
    int hr, min, sec;
    cin >> hr >> min >> sec;
    string am_or_pm;
    if (op == 1)
        cin >> am_or_pm;
    Time t(hr, min, sec, am_or_pm);
    t.print_time12();
    cout << '\n';
    t.print_time24();
    cout << '\n';

    cout << "Adding a minute,\n";
    t.add_min();
    t.print_time12();
    cout << '\n';
    t.print_time24();
    cout << '\n';

    return 0;
}

The code works but can it be made better? Also what will be the correct way to handle invalid arguments in the constructor?


texts:
I'm aware I am probably reinventing the wheel somewhat here, but I am trying to teach myself simple bash coding by completing simple tasks such as parsing files.

To that end I am looking to learn what elephants in the room I could be missing or if there are better ways to use the core functionality of bash without installing any additional tools.

This simple code returns a list of unique IP addresses that have hit the index of my site along with a count of the hits.

a="access.log"; for b in $(cat $a | awk '{print $1}' | sort | uniq);do echo $b;grep $a -e "GET / HTTP" | grep -c $b;done;

Assumptions:
access.log is in the current directory and is in the regular format

Any advice or suggestions for improvement are greatly appreciated


texts:
I`m writing a function for small pygame program. The ideas is that I have a 500 pixels square screen and I want to place 5 objects per row with the size of width and height in random order and I need a function to generate and store coordinates.

from random import randint

def gen_coords(width=50, height=50, side_of_scr=500):
    """Generates random coordinates for objects in rows
    that are positioned on 5x5 grid."""

    coords = []
    for row in range(5):
        # creating row container
        coords.append([])
        for pos in range(5):
            # range of values in randint reflects that i want the objects
            # to be placed with offset of the sides of the screen
            x = randint(0.5 * width, side_of_scr - 0.5 * width)

            # the distance between the rows is equal to the height of an object
            y = 0.5 * height + row * 2 * height
            coords[row].append((x, y))
    return coords

So this

for coord in gen_coords():
    print(coord)

returns

[(177, 25.0), (367, 25.0), (214, 25.0), (284, 25.0), (125, 25.0)]
[(425, 125.0), (446, 125.0), (126, 125.0), (409, 125.0), (187, 125.0)]
[(210, 225.0), (188, 225.0), (215, 225.0), (132, 225.0), (431, 225.0)]
[(303, 325.0), (471, 325.0), (252, 325.0), (26, 325.0), (111, 325.0)]
[(468, 425.0), (127, 425.0), (452, 425.0), (30, 425.0), (345, 425.0)]

I've done this using list of lists and it's working so far, but im looking for the better way to do it. Thanks for help.


texts: CarTalk's Homophones Puzzler: A Programmatic Solution

texts:
The puzzler:

  This was sent in by a fellow named Dan O'Leary. He came upon a common
  one-syllable, five-letter word recently that has the following unique
  property. When you remove the first letter, the remaining letters form
  a homophone of the original word, that is a word that sounds exactly
  the same. Replace the first letter, that is, put it back and remove
  the second letter and the result is yet another homophone of the
  original word. And the question is, what's the word?

  Now I'm going to give you an example that doesn't work. Let's look at
  the five-letter word, 'wrack.' W-R-A-C-K, you know like to 'wrack with
  pain.' If I remove the first letter, I am left with a four-letter
  word, 'R-A-C-K.' As in, 'Holy cow, did you see the rack on that buck!
  It must have been a nine-pointer!' It's a perfect homophone. If you
  put the 'w' back, and remove the 'r,' instead, you're left with the
  word, 'wack,' which is a real word, it's just not a homophone of the
  other two words.

I've used CMU Pronouncing Dictionary and modified it (manually) to keep alphabetical strings only.

My code:

def read_dict(file_path):
    """Reads 'CMU Pronouncing Dictionary' text file.

       returns: dict mapping from a word to a string describing
       its pronounciation

       file_path: str
       """
    fin = open(file_path)
    pron_dict = {}

    for line in fin:
        index = line.find(' ')  # e.g. "AA  EY2 EY1"
        word = line[:index]
        pron = line[index+2:]
        pron_dict[word] = pron

    return pron_dict

def are_homophones(pron_dict, word1, word2, word3):
    """Returns whether 3 words are homophones.

       pron_dict: dict
       """

    for word in [word1, word2, word3]:
        if not word in pron_dict:
            return False

    return pron_dict[word1] == pron_dict[word2] == pron_dict[word3]

def find_homophones_words(pron_dict):
    """Returns a list of words, where:

    * word
    * word with the first item omitted
    * word with the second item omitted

    are all homophones and existing in pron_dict.

    pron_dict: dict
    """

    homophones = []

    for word in pron_dict:
        first_word = word[1:]  # word with the first letter omitted
        second_word = word[0] + word[2:] # word with the second letter omitted
        if are_homophones(pron_dict, word, first_word, second_word):
                homophones.append(word)

    return homophones

pron_dict = read_dict('CMUDict.txt')
homophones = find_homophones_words(pron_dict)

for word in homophones:
    print([word, word[1:], word[0] + word[2:]])

Notes:

I haven't, strictly, followed the puzzler. The code finds words satisfying
the main requirements, except that the word is "one-syllable and five-letters".
I'm a hobbyist and a beginner.
I don't only want to optimize the code, but also my skills and style. Therefore any notes, advice and suggestions aren't only welcomed, but also encouraged!


texts:
Let's say I have a struct…

struct Person {
   let isMale: Bool
   let name: String
}

and an array of Person structs. I want to trim all the men (isMale == true) from the start and end of the array (similarly to how you'd trim whitespace from the start & end of a string)…

func trimMen(people: [Person]) -> [Person]

    var trimmedPeople: [Person] = people

    while trimmedPeople.first?.isMale {
        trimmedPeople.removeFirst()
    }
    while trimmedPeople.last?.isMale {
        trimmedPeople.removeLast()
    }
    return trimmedPeople
}

Is there a more efficient way to do it?


texts: Image upload handler with random filename generator and postprocessing

texts:
I have a function to handle image uploads.  Originally, it contained a goto:

restartA:
$name = $this->random_name() . $ext;
if(file_exists($this->img_path . $name)){
    unset($name);
    goto restartA;
}

It seems pretty clear to me what the code does, and I think anyone who reads it would immediately understand what's happening.  On the other hand, goto is considered evil, so I rewrote it like this:

while(true){
    $name = $this->random_name() . $ext;
    if(file_exists($this->img_path . $name)){
        unset($name);
    }else{
        break;  //weve got a unique name
    }
}

Here's the context in which the code excerpt above appears. In place of some code which I am not allowed to post, I have substituted an array of 3 static sizes.

public function original_img_upload()
{
    $img_name = 'original_image';
    $image_name = $_FILES[$img_name]['name'];
    $image_temp = $_FILES[$img_name]['tmp_name'];
    $image_type = $_FILES[$img_name]['type'];
    $image_info = getimagesize($image_temp);
    $valid = ['.jpg', '.jpeg', '.gif', '.png'];
    $exif = [IMAGETYPE_GIF,IMAGETYPE_JPEG,IMAGETYPE_PNG];
    $ext = strtolower(strrchr($image_name, "."));
    $names_to_return = []; //image names to return to main scope
    $sizes = [
        [1000,608],
        [657,400],
        [329,200]
    ]; //image target sizes

    if( !is_uploaded_file($_FILES[$img_name]['tmp_name']) ){
        $this->e('bad file input');
    }
    if( !in_array(exif_imagetype($image_temp),$exif) && in_array($ext,$valid) ){
        $this->e('file type not allowed');
    }
    if( !$image_info ){
        $this->e('invalid image size.');
    }

    switch($image_type){
        case 'image/png':
            $image_res = imagecreatefrompng($image_temp);
        break;
        case 'image/gif':
            $image_res = imagecreatefromgif($image_temp);
        break;
        case 'image/jpeg':
        case 'image/pjpeg':
            $image_res = imagecreatefromjpeg($image_temp);
        break;
        default:$this->e('bad file input');
    }

    for($i=0;$i <3; $i++){
        $canvas = imagecreatetruecolor($sizes[$i][0],$sizes[$i][1]);//width + height
        //( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
        imagecopyresampled(
            $canvas,
            $image_res,
            0,0,0,0,
            $sizes[$i][0],
            $sizes[$i][1],
            $image_info[0],
            $image_info[1]
        );

        //create random destination file name
        while(true){
            $name = $this->random_name() . $ext;
            if(file_exists($this->img_path . $name)){
                unset($name);
            }else{
                break;  //weve got a unique name
            }
        }

        $destination = $this->img_path . $name;

        //write image
        switch(strtolower($image_type))
        {
            case 'image/png':
                imagepng($canvas,$destination);
                break;
            case 'image/gif':
                imagegif($canvas,$destination);
                break;
            case 'image/jpeg':
            case 'image/pjpeg':
                imagejpeg($canvas,$destination,100);
                break;
        }

        //push image name to return images
        array_push($names_to_return,$this->args['base_url'].'img/'. $name);
        //unset resources and free memory
        unset($canvas,$name,$destination);
    }
    return $names_to_return;
}


texts: Finding all possible combination of alphabets of length 5

texts:
The code finds all the combination of alphabet with length 5 and saves it in a list(its ASCII value, minus 97). I want to optimize the code to reduce execution time.

from itertools import product
def permuteKey():

    def pp(chunks):
        return map(''.join, chunks)

    k=pp(product('abcdefghijklmnopqrstuvwxyz', repeat=5))
    key=[]
    for i in range(len(k)):
        temp=k[i]
        temp2=[]
        for j in range(len(temp)):
            temp1=temp[j]
            temp2.append(ord(temp1)-97)
        key.append(temp2)
        print key

permuteKey()


texts: Efficient algorithm for counting frequency of a numbers in intervals

texts:
I need to build a bar graph that illustrate a distribution of pseudorandom numbers that determined by linear congruential method

$$X_n+1 = (a  X_n + c) \mod m$$
$$U = X/m$$

on the interval [0,1]

For example:

Interval           Frequency
[0;0,1]            0,05
[0,1;0,2]          0,15
[0,2;0,3]          0,1
[0,3;0,4]          0,12
[0,4;0,5]          0,1
[0,5;0,6]          0,15
[0,6;0,7]          0,05
[0,7;0,8]          0,08
[0,8;0,9]          0,16
[0,9;1,0]          0,4

I used such a method:

float mas[10] = {0,0,0,0,0,0,0,0,0,0};
void metod1()
{
    int x=-2, m=437, a=33, c=61;
    float u;
    for(int i=0;i<m;i++){
        x=(a*x + c) % m;
        u=(float)x/(float)m;
        int r;
        r = ceil(u*10);
        mas[r] = mas[r] + 1;
    }
    for(i=0;i<10;i++) cout<<"["<<(float)i/10<<";"<<(float)(i+1)/10<<"]"<<" | "<<mas[i]<<"\n-----------------"<<endl;
    return;
}

If you know another efficient algorithm for this problem, that isn't straightforward, I would appreciate it.


texts:
I have this little server at home, which is hosting some websites, school stuff and a minecraft server. I made this backup script and I would like some feedback since I'm not very good at scripting.

#!/bin/bash
# declare folders to backup
dirs=(
    "/home/ferre/Minecraft"
    "/var/lib/automysqlbackup/daily"
);

# backup names
names=(
  "Minecraft"
  "Mysql"
);

# backup folder
backupdir="/mnt/backup";

# backup algorithm
for (( i=0; i<=$(( ${#dirs[*]} -1 )); i++))
do
  # info
  echo "starting backup of ${names[i]}";

  # create folder
  path="${backupdir}/${names[i]}";
  $(mkdir -p "$path");

  # create backup
  file="${path}/$(date +%F).tar";
  $(tar -czf "$file" --absolute-names "${dirs[i]}");

  # reporting
  result=$?;
  if [ $result -eq 0 ]; then
    echo "SUCCESS";
  elif [ $result -eq 1 ]; then
    echo "WARNING some files where changed while copying!";
  else
    echo "FATAL ERROR";
  fi
done


texts: Calculating GetHashCode efficiently with unordered list

texts:
I'm wondering what would be the best way to calculate the hashcode when the order of a sequence doesn't matter. Here's the custom IEqualityComparer<T> i've implemented for an answer on Stackoverflow.

public class AccDocumentItemComparer : IEqualityComparer<AccDocumentItem>
{
    public bool Equals(AccDocumentItem x, AccDocumentItem y)
    {
        if (x == null || y == null)
            return false;
        if (object.ReferenceEquals(x, y))
            return true;
        if (x.AccountId != y.AccountId)
            return false;
        return x.DocumentItemDetails.Select(d => d.DetailAccountId).OrderBy(i => i)
            .SequenceEqual(y.DocumentItemDetails.Select(d => d.DetailAccountId).OrderBy(i => i));
    }

    public int GetHashCode(AccDocumentItem obj)
    {
        if (obj == null) return int.MinValue;
        int hash = obj.AccountId.GetHashCode();
        if (obj.DocumentItemDetails == null)
            return hash;
        int detailHash = 0;
        unchecked
        {
            var orderedDetailIds =  obj.DocumentItemDetails
                .Select(d => d.DetailAccountId).OrderBy(i => i);
            foreach (int detID in orderedDetailIds)
                detailHash = 17 * detailHash + detID;
        }
        return hash + detailHash;
    }
}

As you can see the foreach in GetHashCode needs to order the (nested) sequence before it starts calculating the hashcode. If the sequence is large this seems to be inefficient. Is there a better way to calculate the hashcode if the  order of a sequence can be ignored?

Here are the simple classes involved:

public class AccDocumentItem
{
    public string AccountId { get; set; }
    public List<AccDocumentItemDetail> DocumentItemDetails { get; set; }
}

public class AccDocumentItemDetail
{
    public int LevelId { get; set; }
    public int DetailAccountId { get; set; }
}


texts: Refactoring my problem-solution to reduce complexity

texts:
I need some help refactoring my solution to the problem below:
Problem Statement

An Association of Computer Scientists meets on a weekly basis. There is a certain number of seats in the meeting room and all members know their seats. For example, if there are 10 seats and only 5 members attending, each member will sit on his own seat, and there may be empty seats left between the members, because all have to sit on their 'reserved' seats.
If a member, sitting on seat Xi, starts or joins the discussion, exactly Ti people (after him) will take part of the discussion. For every member, we know his seat and his voice. Only those members who are within the reach of the previous member's voice can hear him and participate in the discussion. For example, if we are given the seat and the voice of a member, and they are 1, 7 (respectively), only people seated on seats within range of 1 to 8 (inclusive) can hear him and participate in the discussion.

Example Data

Input:
N - number of members attending
Xi - seat of member i (for all i=1,2,...N), Li - his voice
Output:
The number of people participating in the discussion after member
i.
Example Input:
5

1 7

2 3

8 7

10 5

100 2

Output:
4

1

2

1

1

And now, here's my solution:
#include <iostream>
#include <vector>
using namespace std;

int main()
{
        int n;
        cin >> n;
        vector<pair<int, int> > c;
        for (int i=0; i<n; i++) {
                int a, b;
                cin >> a >> b;
                c.push_back(make_pair(a,b));
        }
        for (int i=0; i<n; i++) {
                int a, b, r=1;
                a = c[i].first;
                b = c[i].second;
                for (int j=i+1; j<n; j++) {
                        if (a+b >= c[j].first) {
                                if (c[j].first + c[j].second > a+b) {
                                        a = c[j].first;
                                        b = c[j].second;
                                }
                                r++;
                        }
                }
                cout << r << endl;
        }

        return 0;
}

The problem with this solution is in its quadratic O-complexity. Is it possible to reduce it somehow? I've been thinking of the divide and conquer approach, but I'm fairly new to the concept, and I have no idea if that's the right approach for this problem, and how I may implement that.


texts:
I wanted to create a type or class, which can be used to handle angles. I started with defining a class, to make it possible to store degrees in Integral, radians in Floating, etc... But I realized, that I will meet complex problems if I continue it so. For example handling the addition of two angles, like a Radian Double and a Degree Int.

To keep it simple, easy to use and develop, I decided to create one type only, and write the functions around it.

module Data.Angle where

import Data.Fixed -- mod'

data Angle a = Radians a deriving (Eq, Show)

-- Creating Angle from a value

-- | Create an Angle with the given degrees
angleFromDegrees :: (Integral d, Floating r) => d -> Angle r
angleFromDegrees x = Radians $ (realToFrac x) * pi/180

-- | Create an Angle with the given turns
angleFromTurns :: (Real t, Floating r) => t -> Angle r
angleFromTurns x = Radians $ (realToFrac x) * pi*2

-- | Create an Angle with the given turns
angleFromRadians :: (Floating r) => r -> Angle r
angleFromRadians x = Radians x

-- Get the value from Angle

-- | Get degrees from an Angle
angleValueDegrees :: (Floating r, RealFrac r, Integral d) => Angle r -> d
angleValueDegrees (Radians x) = round $ x / pi * 180.0

-- | Get radians from an Angle
angleValueRadians :: (Floating r) => Angle r -> r
angleValueRadians (Radians x) = x

-- | Get turns from Angle
angleValueTurns :: (Floating r) => Angle r -> r
angleValueTurns (Radians x) = x / (pi*2)

-- Basic functions

-- | Adding two angles
addAngle :: (Floating a) => Angle a -> Angle a -> Angle a
addAngle (Radians r1) (Radians r2) = Radians $ r1 + r2

-- | Normalize Angle: transforming back to (0-2pi)
normAngle :: (Floating a, Real a) => Angle a -> Angle a
normAngle (Radians r) = Radians $ mod' r (pi*2)

-- | Add two angles and normalize the result
addAngleNorm :: (Floating a, Real a) => Angle a -> Angle a -> Angle a
addAngleNorm a b = normAngle $ addAngle a b

-- | Distance between two angles
distAngle :: (Floating a, Real a) => Angle a -> Angle a -> Angle a
distAngle (Radians r1) (Radians r2) = Radians $ if (a' < b') then a' else b'
    where
        a' = mod' (r1-r2) (pi*2)
        b' = mod' (r2-r1) (pi*2)

-- | Flip Angle
flipAngle :: (Floating a) => Angle a -> Angle a
flipAngle (Radians r) = Radians (-r)

-- | Flip Angle and normalize the result
flipAngleNorm :: (Floating a, Real a) => Angle a -> Angle a
flipAngleNorm = normAngle . flipAngle

-- | Add degrees to Angle
addAngleDegrees :: (Floating r, Integral d) => Angle r -> d -> Angle r
addAngleDegrees ang deg = addAngle ang $ angleFromDegrees deg

-- | Add radians to Angle
addAngleRadians :: (Floating r) => Angle r -> r -> Angle r
addAngleRadians (Radians r1) r2 = Radians $ r1 + r2

-- | Add turns to Angle
addAngleTurns :: (Floating r, Real t) => Angle r -> t -> Angle r
addAngleTurns ang turn = addAngle ang $ angleFromTurns turn

-- Trigonometric functions

-- | Sine of the angle
sinAngle :: (Floating a) => Angle a -> a
sinAngle (Radians r) = sin r

-- | Cosine of the angle
cosAngle :: (Floating a) => Angle a -> a
cosAngle (Radians r) = cos r

-- | Tangent of the angle
tanAngle :: (Floating a) => Angle a -> a
tanAngle (Radians r) = tan r

-- | Cotangent of the angle
cotAngle :: (Floating a) => Angle a -> a
cotAngle (Radians r) = 1 / (tan r)

-- Inverse trigonometric functions

-- | Create angle from inverse sine
asinAngle :: (Floating a) => a -> Angle a
asinAngle x = Radians $ asin x

-- | Create angle from inverse cosine
acosAngle :: (Floating a) => a -> Angle a
acosAngle x = Radians $ acos x

-- | Create angle from inverse tangent
atanAngle :: (Floating a) => a -> Angle a
atanAngle x = Radians $ atan x

-- | Create angle from inverse cotangent
acotAngle :: (Floating a) => a -> Angle a
acotAngle x = Radians $ (pi/2) - (atan x)

The finished library is available here:

Haskell Angle Library


texts: Is it "legit" to use a Linq query in a razor view?

texts:
I have a Linq query in my razor view like below. Its just getting contacts that belong in the group.

@using (var tabs = Html.Bootstrap().Begin(new Tabs("TabStripId")))
{
    foreach (var g in Model.Groups)
    {
        @tabs.Tab(g.GroupName)
    }

    foreach (var g in Model.Groups)
    {

        using (tabs.BeginPanel())
        {

            using (Html.Bootstrap().Begin(new Table().Striped()))
            {
               ///Table header here

               ///The line in question
               var m = Model.Contacts.Where(x => x.GroupId == g.Id);

               foreach (var item in m)
               {
                 /// Row output here
               }
            }
        }
    }
}

The code I have works just fine, but the question is: Is it good practice to be using Linq in a view like that? If not, how would that be handled in the model and/or controller?


texts: Extract last delivery for each name on each date

texts:
I've been using Java 8 Stream features quite a lot these days and am very happy with it. I've written a code that uses groupingBy and max but was wondering if it can still be optimized or even just be tweaked for better readability.

Some sample data:

Name, OrderDate, DeliveryTime
abc, 2010-01-15, 2010-01-15T11:00:00
abc, 2010-01-15, 2010-01-31T07:00:00 //should be marked as latest
zzz, 2010-01-15, 2010-01-13T11:00:00
zzz, 2010-01-15, 2010-01-23T07:00:00 //should be marked as latest

Here's the code I've written:

public class DeliveryTest {
   class Delivery {
      String name;
      LocalDate orderDate;
      LocalDateTime deliveryTime;
      boolean isLatest = false;
      //getters and setters here
   }

   @Test
   public void testLatestDelivery() {
      List<Delivery> deliveries = new ArrayList<>();
      //populate the test data here

      //group the deliveries by name and orderDate
      Map<String, Map<LocalDate, List<Delivery>>> groupByNameAndOrderDate = deliveries.stream()
            .collect(groupingBy(Delivery::getName, groupingBy(Delivery::getOrderDate)));

      //find and mark as "Latest" for each of the the max deliveryTime per name and orderDate grouping,
      groupByNameAndOrderDate.forEach((name, byNameMap) -> {
          byNameMap.forEach((orderDate, byOrderDateList) -> {
              byOrderDateList.stream()
                  .max((n1, n2) -> n1.getDeliveryTime().compareTo(n2.getDeliveryTime())).get()
                  .setIsLatest(true);
          });
      });
      //do assertions here
   }
}

Can this Java 8 code still be improved (for example the part where I have nested forEach)?

Or written shorter but still readable (for example, can the group and max be combined)?


texts: Coin Flip Streak from Automate the Boring Stuff with Python

texts:
I apologize beforehand if the question has been repeated so many times. This is a practice task from Automate the Boring Stuff with Python. In brief, the task entails writing a code that carries out an experiment of checking if there is a streak of 6 'heads' or 'tails' in 100 coin tosses, then replicates it 10,000 times and gives a percentage of the success rate.
When I wrote the code, I tried to be different by making the code applicable to any streaks in a number of predetermined experiments (in my case, the sample was 1 million coin tosses). I also tried to find the longest streak possible in that said experiment.
I also want to apologize beforehand that the comments were awfully verbose.
import random, copy, time

def torai(seq,pop): # seq is for #=streak, pop is for total sample/population/experiment
    # Creating a random chance of heads and tails
    tosses = []
    for i in range(pop):
        tosses.append(random.randint(1,2)) # 1 and 2 for head and tail, and vice versa

    # Defining initial values for the main loop
    streak = 0 # Iterated streak
    curlongstr = 0 # Current longest streak
    longeststr = 0 # Longest streak evaluated
    peak = [] # Record local streaks from 'tosses' list

    # The main loop
    for i in range(len(tosses)): # Looping based on list indexes
        if i == 0:  # Conditional for preventing tosses[0] == tosses[-1]
            continue

        elif tosses[i] == tosses[i-1]: # Conditional for checking if an i element has the same value as the previous element value, i-1
            streak += 1 # Adding tally mark if the line above is fulfilled
            if i == len(tosses)-1: # A nested conditional for adding the last tally mark from 'tosses' into the overall list of steaks 'peak', see lines 27-33
                peak.append(streak)

        elif tosses[i] != tosses[i-1]: # Conditional for checking if an i element value is different than the previous element value, i-1
            curlongstr = copy.copy(streak) # Creating a variable by returning a copy of streak before it resets to 0, see line 31
            if curlongstr > longeststr: # A nested conditional for comparing the current longest streak and the longest streak that has happened when looping the 'tosses' list
                longeststr = curlongstr
            streak = 0 # This is where streaks ended and then resets to 0, so before that, the value of the streak is copied first, see line 28
            if curlongstr > streak: # After streak is reset to 0, the value of current long streak is compared to 0, so that we create a list of streaks from 'tosses' list
                peak.append(curlongstr)

    truepeak = []
    for i in peak: # Example: a 2-streak is equal to either [1,1,1] or [2,2,2], a 4-streak is either [1,1,1,1,1] or [2,2,2,2,2]
        truepeak.append(i+1)

    apr = []
    # Loop for finding how many #-streaks happened
    for i in truepeak:
        if i == seq:
            apr.append(i)

    print('%s-streak count: ' %seq, len(apr)) # Total of #-streaks happened in 'tosses' list
    print('%s-streak prob (percent): ' %seq, (len(apr)/pop)*100) # Calculating probability if how many #-streak happened in given n times tosses
    print('longest streak: ',longeststr + 1) # Similar reason as line 36
    print('process time: ',time.process_time(), 'second\n')

    return (len(apr)/pop)*100

x = torai(2,1000000)
y = torai(6,1000000)
z = torai(10,1000000)
print(x, y, z)

I tried to increase the sample to 10 million coin tosses. However, the program will run 9-10 slower each time the function was called.
My request are can anyone check whether if the result (probability of n-streak) is right or not and are there any ways to make the code and process time shorter?


texts: Wait until all files are loaded asynchronously, then run code

texts:
I'm an experienced programmer but not too great at JavaScript so I'm looking to see if I'm doing this 'right'. I want to have several files loaded in (Ajax or really AJAJ) and, once loaded, run some final code. My basic idea is:

var jobsToDo = 2; // global

function getAsync(url, func) {
    var client = new XMLHttpRequest();
    var handler = function() {
        func();
        jobsToDo--;
        if(jobsToDo == 0)
            allLoaded();
    };
    client.open('GET', url);
    client.onreadystatechange = handler;
    client.send();
}

getAsync('file1.json', function() {
    // process file
});
getAsync('file2.json', function() {
    // process file
});

function allLoaded() {
    // ...
}

Is this correct? Is it a reasonable solution, or are there better ways? Actually onreadystatechange already feels like the wrong thing; is there a better handler that will wait until I get a 200, or should I expand my function above to exit unless the state is appropriate?

Also, since I don't do much JavaScript these days (or all the cool kids calling it ECMAScript now?): will I run into trouble with the local function being passed around?

My current code has only one file being loaded, so I'm trying to extract the basic functionality here to keep the code DRY.

Meta: I don't know if this belongs here or on SO; feel free to move if I made the wrong choice.


texts:
This is my second iteration of a function to remove all nodes containing value 'x'.  The previous iteration is here.

This function takes as input the head of a list and has to delete all the nodes that contain a given value taken.  Is this an improvement on the previous iteration?  Is there anything else that can be improved?

typedef struct node {
    int val;
    struct node *next;
} node_t;

node_t *rimuovi(node_t *head, int x){

    node_t *temp = head;
    node_t *curr = head;

    if (head == NULL){
        printf("Lista vuota, finito!\n");
        return head;
    }

    while(head->val == x){
        if (head->next == NULL){
            free(head);
            printf("La lista adesso e' vuota. Finito\n");
            return head;
        }
        else {
            curr = head->next;
            free(head);
            head = curr;
        }
    }

    temp = head;
    curr = temp->next;

    while (curr != NULL){
        if (curr->val == x){
            if(curr->next != NULL){
                temp->next = curr->next;
                free (curr);
                curr = temp->next;
            }
            else {
                temp->next = NULL;
            }
        }
        else {
            temp = curr;
            curr = curr->next;
        }
    }

    return head;
}


texts: Checking whether any item in an array has a desired value for a key

texts:
I want to check whether or not an object exists in an array given its key (key is not unique).

The object takes the form:

class item {
    key: string,
    value: (string|int|boolean),
    id: Guid
}

And this._items is an item array.

First approach is simply filtering the array and checking its length:

has(key) {
    return this._items.filter(item => item.key === key).length > 0;
}

Then a for loop that terminates when exists is true.

has(key) {
    let exists = false;
    for (let i = 0; i < this._items.length && !exists; i++) {
        exists = this._items[i].key === key;
    }

    return exists;
}

One more take on it, this.keys() returns an array of unique item keys:

has(key) {
    return this.keys().toString().match(new RegExp(key, 'ig')).length > 0;
}

The first one is cleaner / more readable but iterates over every item in the array. The second one doesn't read quite as well but only iterates over as many items as it needs to.

Until it needs to be optimized, is this just a preference/coding-style thing?


texts:
Assume you have the following data in the form of a csv-file. The content looks something like this:

,Action,Comedy,Horror
1,650,819,
,76,63,
2,,462,19
,,18,96
3,652,457,18
,75,36,89

which can be interpreted as a table of the form:

           Action       Comedy       Horror
1           650          819
             76           63
2                        462           19
                          18           96
3           652          457           18
             75           36           89

The goal was to write a function that takes a lst with genre names as elements in form of a str and returns a scatter plot of the data, where the data that should appear on the scatter plot is in the second row of every index (76, 63 , and , 18, 96 and 75, 36, 89). The function should be able to distinguish between two-dimensional and three-dimensional scatter plots depending on the input.

from pandas import DataFrame
from csv import reader
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def genre_scatter(lst):
    """
    Creates an scatter plot using the data from genre_scores.csv.
    :param lst: a list with names of the genres considered
    :return: saves a pdf-file to the folder Fig with the name gen_1_ge_2.pdf
    """
    # First we need to determine the right columns of genre_scores.
    first_row = [row for row in reader(open('genre_scores.csv', 'r'))][0]
    index = [first_row.index(x) for x in lst]

    # Get the relevant data in the form of a DataFrame.
    # Please note that the first row of data for every index is not necessary for this task.
    data = DataFrame.from_csv('genre_scores.csv')
    gen_scores = [data.dropna().iloc[1::2, ind - 1].transpose() for ind in index]

    # rewrite the values in an flattened array for plotting
    coordinates = [gen.as_matrix().flatten() for gen in gen_scores]

    # Plot the results
    fig = plt.figure()
    if len(coordinates) == 2:
        plt.scatter(*coordinates)
        plt.text(70, 110, "pearson={}".format(round(pearson_coeff(coordinates[0], coordinates[1]), 3)))
        plt.xlabel(lst[0])
        plt.ylabel(lst[1])
        plt.savefig("Fig/{}_{}.pdf".format(*lst))
    else:
        ax = fig.add_subplot(111, projection='3d')
        ax.scatter(*coordinates)
        ax.update({'xlabel': lst[0], 'ylabel': lst[1], 'zlabel': lst[2]})
        plt.savefig("Fig/{}_{}_{}.pdf".format(*lst))
    plt.show()
    plt.close("all")

if __name__ == "__main__":
    genre_scatter(['Action', 'Horror', 'Comedy'])

The code works and I'm happy with the output but there are a few things that bug me and I'm not sure if I used them right.

I'm not incredibly familiar with list comprehension (I think that is what you call expressions of the form [x for x in list], please correct me if I'm wrong) and haven't used them very often, so I'm not quite sure if this here was the right approach for the problem. My biggest concern is the first use of this kind of expression, where I basically need the first row of the CSV file but create a list with all the rows only to use the first. Is there a smarter way to do this?
Is there a better way to label the axes? Ideally some function where I just could pass the *lst argument?

Please forget the pearson_coeff() part in the code, it's not really relevant for this.


texts:
I searched the net for some bfs and dfs code but rarely found some as reference. I wrote this for an assignment, I am getting the results fine. I want to make my code more compact and remove unwanted stuff or need suggestions to make the code better. I used stack and queue data structures for the implementation
#include <stdio.h>
#include<stdlib.h>
#include <stdbool.h>
#include <string.h>
int F=-1,R=-1;char q[10];
char v[10],check[10];int a[100][100];char stack[10];int top=-1;
char vertex;int pos[10];int s;char Dfsv[10]; int u=9;
int n=0;
int dequeue(){
  if(F==-1)
  {
    printf("underflow\n");
  }
  else{
        vertex=q[F];
        printf("%c",vertex);
         F++;
         if(F<10)
        { return pos[F];}
        else{ return -1;}
}
}

void insert(int j){
  if(R==9)
  {
    printf("overflow\n");
  }
  else if(F==-1&&R==-1)
  {  F++;R++;
    check[j]='v';
      vertex=v[j];
      q[R]=vertex;
  }
  else{ if(check[j]!='v')
          { check[j]='v';R++;
            vertex=v[j];pos[R]=j;

            q[R]=vertex;}
  }
}
void BFS(int s){

 int k=s;

  while(k!=-1)
    {
           for(int j=0;j<n;j++)
            {
               if(a[k][j]==1)
               {
                insert(j);

                }
             }
    k=dequeue();
  }
}
void PUSH(int j){
  if(top==9)
  {
    printf("overflow\n");
  }
  else{ if(check[j]!='v')
          { check[j]='v';top++;
            vertex=v[j];pos[top]=j;

            stack[top]=vertex;

  }
}}
int POP(){
  if (top==-1)
  {
    printf("underflow\n");
  }
  else{  Dfsv[u]=stack[top];

          top--;u--;
        if(top!=-1)
       { return pos[top+1];}
       else{ return -1;}

  }
}

void DFS(int s){
 int k=s;
  while(k!=-1)
    {
           for(int j=0;j<n;j++)
            {
               if(a[k][j]==1)
               {
                PUSH(j);

                }
             }
    k=POP();
  }
}
void BDFST(){
printf("enter source number\n");
scanf("%d",&s );
vertex=v[s];insert(s);

printf("BFS: ");BFS(s);printf("\nDFS: ");
memset(check, 0, 10);
PUSH(s);
DFS(s);
for(int u=0;u<10;u++)
{
  printf("%c",Dfsv[u] );
}
}

void print(int a[100][100],int n,char v[])
{ int j=0;
  for(int i=0;i<n;i++)
  {
     for(j=0;j<n;j++)
     { printf("%c%c[%d][%d]: ",v[i],v[j],i,j);
       printf("%d\t",a[i][j]);
     }
  printf("\n");
  }
}

void main(){
int i=0,j=0;
  printf("enter number of vertex\n");
  scanf("%d",&n);
    printf("enter vertex's\n");
     scanf("%s",&v);

  printf("enter the edges\n");
  for( i=0;i<n;i++)
  {  printf("enter 1 for edges of %c\n",v[i]);
     for(j=0;j<n;j++)
     {
       printf("%c%c[%d][%d]: ",v[i],v[j],i,j);
       scanf("%d",&a[i][j] );
      printf("\n");
     }
     printf("\n");
  }
    print(a,n,v);

    BDFST();

}

```


texts:
I have two kendoGrid(s) named:

var sourcegrid = $('#usersGrid').data("kendoGrid");        //SOURCE GRID
var destinationgrid = $('#teamGrid').data('kendoGrid');    //DESTINATION GRID

In sourcegrid, I have 1000 records and in destinationgrid has no records.

I have a button that transfers data from sourcegrid to destinationgrid.

 sourcegrid.select().each(function () {
        var dataItem = sourcegrid.dataItem($(this));
        destinationgrid.dataSource.add(dataItem);
        sourcegrid.removeRow($(this));
    });

When I'm transferring 500 records to destinationgrid, the process of transferring is so slow. How can this be optimized?

You can use this jsfiddle.net link here.


texts: Programming challenge “Greatest Odd Divisor”

texts:
I am trying to solve a programming problem on a coding platform. When I submit the code on the coding platform, it throws a "Time Limit Exceeded" error. Can someone check my solution and help optimize it?

A food delivery company X gets a lot of orders every day. It charges some
commission from the restaurants on these orders. More formally, if an order
value is K, X charges a commission which is the greatest odd divisor of K. You
can assume that an order value will always be an integer. Given an order value
N, and let C(N) be the greatest odd-divisor of N, output the value of C(1) +
C(2) + C(3) + … + C(N).
INPUT : Input will be an integer, N, the order value. 1 <= N <= 10^9
OUTPUT : Single integer which is the answer

import java.util.*;
public class Solution {
    static double GOD(double num)
    {
        if(num%2!=0)
        {
            return num;
        }
        else
        {
            for (double i = num / 2; i > 0 ;i--)
            {
                if (num % i == 0 && i % 2 != 0)
                {
                    return i;
                }
            }
            return 0;
        }
     }
public static void main(String args[] ) throws Exception {

    Scanner sc = new Scanner(System.in);
    double num = sc.nextDouble();
    double sum = 0;
    for(double i = 1; i<=num; i++)
        {
        sum = sum + GOD(i);
    }
    System.out.println((int)sum);
}
}


texts:
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.


texts:
I had a need to isolate some long-running blocking calls in a background thread of my app. I also needed to keep this thread running indefinitely, because COM would complain if some object I created in that thread were accessed from another thread. I then needed to control what these objects did from my main UI thread, in a guaranteed-FIFO manner.

The solution I came up with was kind of ActiveObject-reminiscent; a polling loop running as the DoWork handler of a BackgroundWorker, which accepted encapsulated Command objects representing the work to perform from a thread-safe ConcurrentQueue. Here's the vitals:

//basic implementation of the Command pattern; no serialization/persistence needed,
//just need to be able to encapsulate some work and defer it.
internal class Command
{
    private Action action;

    public Command(Action action)
    {
        this.action = action;
    }

    protected Command()
    {
    }

    public virtual void Execute()
    {
        action();
    }
}

//Generic variation accepts a single parameter
internal class Command<T>:Command
{
    private T param;

    private Action<T> action;

    public Command(Action<T> action, T param)
    {
        this.action = action;
        this.param = param;
    }

    public override void Execute()
    {
        action(param);
    }
}

...

//event handlers run in the UI thread will Enqueue() Command objects
//containing the delegates that should be run in the background thread
ConcurrentQueue<Command> queuedCommands = new ConcurrentQueue<Command>();

//The BGW DoWork handler; runs indefinitely until the UI that needs it is Dispose()d
private void MainBackgroundLoop(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        Command command;
        bool commandAvailable;

        do
        {
            commandAvailable = queuedCommands.TryDequeue(out command);
            if (commandAvailable)
                command.Execute();
            else
                Thread.Sleep(100); //<-- Here's your code smell
        } while (!e.Cancel);
    }

Now, this functions beautifully. But, the use of Thread.Sleep() in any SO/CR post usually gets called out as a code smell. I get why; this loop requires CPU to try to pull commands out of the queue at least 10 times a second, which will eat CPU for an "idle" thread. But, given there are other reasons why the thread has to keep running, this seems acceptable.

Thoughts?


texts: Closing a ResultSet and rethrowing an Exception

texts:
Is this an acceptable way to make sure a java.sql.ResultSet is always closed, and also make sure that an Exception caught is propagated to the caller?

Please don't hesitate to review other aspects of this sample as well.

public void updateReplicationStatus(BeanReplicationTask taskBean, String strStatus) throws Exception {
    String strSelect = this.createReplicationSelectStatement(taskBean);
    ResultSet resultSet = null;
    try {
        resultSet = DerbyDog.getResultSet(strSelect);
        if (resultSet.next()) {
            resultSet.updateString(COLUMN_STATUS, strStatus);
            resultSet.updateRow();
        }
    } catch (Exception e) {
        throw e;
    } finally {
        if (resultSet != null) {
            resultSet.close();
        }
    }
}


texts:
This is an iterative review. The previous iteration can be found here.

I've now been programming in python for all of 6 hours.
This is probably my last iteration of FizzBuzz.

Thoughts?

FizzBuzz.py

from utilities import ask_something
from utilities import ask_yes_no

def print_fizz_buzz_output(start_num, end_num, pair_list):
    for num in range(start_num, end_num + 1):
        divisor_text = [text for divisor, text in pair_list if num % divisor == 0]
        if divisor_text:
            print(''.join(divisor_text))
        else:
            print(num)

def ask_divisor_text_pairs():
    loop = True
    while loop:
        divisor = ask_something('Divisor? ', int,
                                'Invalid input for divisor. Divisor must be a whole number. Please try again.')
        text = ask_something('Text? ', str)
        yield (divisor, text)

        loop = (True if 'y' == ask_yes_no('Input another Divisor (y/n)? ') else False)

def ask_iteration_range() -> tuple:
    start_num = ask_something('Start Number? ', int)
    while True:
        end_num = ask_something('End Number? ', int)
        if end_num >= start_num:
            break
        else:
            print('Invalid input for end number. Must be a whole number greater than or equal to the start number.'
                  ' Please try again.')
    return start_num, end_num

if __name__ == '__main__':
    print_fizz_buzz_output(*ask_iteration_range(), list(ask_divisor_text_pairs()))

utilities.py

def ask_something(prompt: str, convert, error_message='Invalid input.'):
    while True:
        value = input(prompt)
        try:
            return convert(value)
        except ValueError:
            print(error_message)

def ask_yes_no(prompt: str, allow_upper_case=True) -> str:
    # returns 'y' or 'n'
    while True:
        value = ask_something(prompt, str)
        if value == 'y' or (value == 'Y' and allow_upper_case):
            return 'y'
        elif value == 'n' or (value == 'N' and allow_upper_case):
            return 'n'
        else:
            print('Invalid Input. Please input "y" or "n"')

Example Input/Output

Start Number? 1
End Number? 15
Divisor? 3
Text? Monty
Input another Divisor (y/n)? y
Divisor? 5
Text? Python
Input another Divisor (y/n)? n
1
2
Monty
4
Python
Monty
7
8
Monty
Python
11
Monty
13
14
MontyPython


texts: Copying into two different folders on a machine simultaneously

texts:
I am trying to copy files from machineB and machineC into machineA as I am running my shell script on machineA.

If the files are not in machineB, then it should be there in machineC for sure. I will try copying the files from machineB first, and if it is not in machineB, then I will try copying the same files from machineC.

I am copying the files in parallel using GNU parallel library and it is working fine. I am currently copying two files in parallel.

Earlier, I was copying the PRIMARY_PARTITION files in PRIMARY folder using GNU parallel and once that was done. I was then copying the SECONDARY_PARTITION files in the SECONDARY folder using same GNU parallel so it is sequential as of now with regards to the PRIMARY and SECONDARY folders.

Now I decided to copy files in PRIMARY and SECONDARY folder simultaneously.

#!/bin/bash

export PRIMARY=/test01/primary
export SECONDARY=/test02/secondary
readonly FILERS_LOCATION=(machineB machineC)
export FILERS_LOCATION_1=${FILERS_LOCATION[0]}
export FILERS_LOCATION_2=${FILERS_LOCATION[1]}
PRIMARY_PARTITION=(550 274 2 546 278) # this will have more file numbers
SECONDARY_PARTITION=(1643 1103 1372 1096 1369 1568) # this will have more file numbers

export dir3=/testing/snapshot/20140103

do_Copy() {
  el=$1
  PRIMSEC=$2
  scp david@$FILERS_LOCATION_1:$dir3/new_weekly_2014_"$el"_200003_5.data $PRIMSEC/. || scp david@$FILERS_LOCATION_2:$dir3/new_weekly_2014_"$el"_200003_5.data $PRIMSEC/.
}
export -f do_Copy

parallel --retries 10 -j 5 do_Copy {} $PRIMARY ::: "${PRIMARY_PARTITION[@]}" &
parallel --retries 10 -j 5 do_Copy {} $SECONDARY ::: "${SECONDARY_PARTITION[@]}" &
wait

echo "All files copied."

The script is working fine for me, but I am trying to see is there any better way of doing the same thing using GNU Parallel instead of using &.


texts: Determine whether a list contains a particular sequence

texts:
Allowing wraparound, the sequence [2, 3, 5, 7, 11] contains the sequence [7, 11, 2].

I'm looking for compact/pythonic way to code this, rather than an optimally-efficient way. (Unless I need the efficiency, I usually gravitate to the path of least ASCII).

An obvious method would be as follows (I provide three different ways to code it):

def Contains(L, s):
    L_extended = L+L[0:len(s)]

    for i in xrange( len(L) ):
        if L_extended[i:i+len(s)] == s:
            return True

    return False

def Contains2(L, s):
    L_extended = L+L[0:len(s)]

    return next( \
        ( True for i in xrange(len(L))  if  L_extended[i:i+len(s)] == s ), \
        False \
        )

def Contains3(L, s):
    L_extended = L+L[0:len(s)]

    try:
        next( i  for  i in xrange(len(L))  if  L_extended[i:i+len(s)] == s )
    except StopIteration:
        return False

    return True

print Contains3( [1,2,3,4,5], [1,3] )   # False
print Contains3( [1,2,3,4,5], [2,3] )   # True
print Contains3( [1,2,3,4,5], [4,5,1] ) # True

My preference is for the first, but Python is full of pleasant surprises -- can anyone see a cleaner way to do it?


texts:
I am writing a piece of code that returns all the unique variants that a product is available in for an ecommerce app.  For example, a shirt product can be available in different colors, sizes, and linen. If the available attributes are red, green, L, XL, Cotton, and Polyester, then a list of the unique variants should be eventually returned as:

[{red; L; Cotton} ; {red; L; Polyester} ; {red; XL; Cotton} ; {red; XL; Polyester} ; {green; L; Cotton} ; {green; XL; Cotton} ; {green; L; Polyester} ;

{green; XL; Polyester}]

This would be the unique variants available for the product.

The code below works and eventually returns a string list of IDs representing each variant available for the product. The only problem that I am having with this is that it generates a duplicate of each variant. I can easily take care of that with a Set.ofList function after this code runs, but would like to solve that problem here internally.  I'm new to F#, so what can I do to optimize this code?

type NewProductAttributeInfo = {
    AttributeId : string;
    AttributeCategoryId : string
}

let rec private returnVariant (curIdx: int) (listLength: int)
    (attList: (int * NewProductAttributeInfo * NewProductAttributeInfo) list)
    (curList: NewProductAttributeInfo list) =

    match curList with
    | x when x.Length = listLength -> curList
    | x ->
        let attTup =
            attList
            |> List.filter (fun x' ->
                                let idx1,att1,att2' = x'
                                idx1 >= curIdx && not(curList
                                                        |> List.exists (fun x'' ->
                                                                            x'' = att2'))
                            )
        let idx1,att1,att2 = attTup |> List.head
        let newList = curList @ [att2]
        returnVariant idx1 newList.Length attList newList

let rec calculateVariants (attList: NewProductAttributeInfo list)
    (currentList: (int * NewProductAttributeInfo * NewProductAttributeInfo) list) =

    // group attribute list by category id
    let attGrouped = attList |> List.groupBy (fun x -> x.AttributeCategoryId)
    let (firstGroupCatId,firstGroupDetails) = attGrouped.[0]

    match currentList with
    | [] ->
        let rawVariants = [for nxt in 0 .. (attGrouped.Length - 1) do
                            if nxt > 0
                            then
                                // begin iteration
                                for d in firstGroupDetails do
                                    let _,det = attGrouped.[nxt]
                                    for det' in det do
                                        yield (nxt, d, det')
                        ]
        calculateVariants attList rawVariants
    | x ->
        let groupLength = x |> List.groupBy (fun (idx,d0,nxtD) -> idx)
                        |> List.length |> ((+)1)
        let sortedGroup = x |> List.sortBy (fun (x,y,z) -> x)
        if groupLength > 2
        then // below is the block that generates the duplicates
            [for att in sortedGroup do
                for attCompare in sortedGroup do
                    let idx1,att1,att2 = att
                    let idx2,attC1,attC2 = attCompare
                    if idx2 > idx1 && att2 <> attC2
                    then
                        let idString =
                            returnVariant idx2 groupLength x [att1; att2; attC2]
                            |> List.map (fun nl -> nl.AttributeId)
                        yield String.concat "," idString
            ]
        else
            [
                for att in sortedGroup do
                    let idx1,att1,att2 = att
                    let idString =
                            returnVariant idx1 groupLength x [att1; att2]
                            |> List.map (fun nl -> nl.AttributeId)
                    yield String.concat "," idString
            ]


texts: Refactoring code finding if Binary Tree is BST

texts:
I have written an code to find if a given tree is a BST. Need help in refactoring it. Some of the things I am looking are.

Getting Rid of temp variables (As suggested by Martin Fowler)
Combining leftTreeMax and RighTreeMin methods
Better naming of methods.

Also it will be great if someone can suggest other refactorings which i can do.

package com.cc.bst;

import com.cc.queue.Queue;
import com.cc.trees.BinaryTreeNode;

public class IsBinaryTreeBST {

public static boolean binaryTreeBST ( BinaryTreeNode root) {

    if (root == null) {
        return false;
    }
    int maxVal = leftTreeMax(root.getLeft());
    int minVal = rightTreeMin(root.getRight());
    int rootVal = root.getData();

    if (maxVal == 0 || minVal == 0 ) {
        return false;
    }

    if (rootVal > maxVal && rootVal < minVal) {
        return true;
    }

    return false;

}

private static int leftTreeMax (BinaryTreeNode node) {

    if (node == null) {
        return 0;
    }
    Queue nodeQueue = new Queue();
    nodeQueue.enqueue(node);
    int maxValue = node.getData();

    while (!nodeQueue.isEmpty()) {
        BinaryTreeNode tempNode = (BinaryTreeNode) nodeQueue.dequeue();
        BinaryTreeNode left = tempNode.getLeft();
        BinaryTreeNode right = tempNode.getRight();

        if (left != null ) {
            if (left.getData() > tempNode.getData()) {
                return 0;
            }
            nodeQueue.enqueue(left);
        }
        if (right != null) {
            if ( tempNode.getData() > right.getData()) {
                return 0;
            }
            nodeQueue.enqueue(right);
        }
        if (tempNode.getData() > maxValue) {
            maxValue = tempNode.getData();
        }
    }
    System.out.println("---------- maxVal -------->" +  maxValue);
    return maxValue;
}

private static int rightTreeMin(BinaryTreeNode node) {

    if (node == null) {
        return 0;
    }
    Queue nodeQueue = new Queue();
    nodeQueue.enqueue(node);
    int minValue = node.getData();

    while (!nodeQueue.isEmpty()) {
        BinaryTreeNode tempNode = (BinaryTreeNode) nodeQueue.dequeue();
        BinaryTreeNode left = tempNode.getLeft();
        BinaryTreeNode right = tempNode.getRight();
        System.out.println("---------- tempNode -------->" + tempNode.getData());

        if (left != null ) {
            System.out.println("---------- left -------->" + left.getData());

            if (left.getData() > tempNode.getData()) {
                return 0;
            }
            nodeQueue.enqueue(left);
        }
        if (right != null) {
            if ( tempNode.getData() > right.getData()) {
                return 0;
            }
            System.out.println("---------- right -------->" + right.getData());

            nodeQueue.enqueue(right);
        }
        if (tempNode.getData() < minValue) {
            minValue = tempNode.getData();
        }
    }
    System.out.println("---------- minVal -------->" +  minValue);

    return minValue;
}

}


texts:
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

The following code works on SpiderMonkey 1.8.5. But didn't work on my nodejs.
It takes about two seconds, and gives the correct answer.
My two major concerns are my style. And my choice in functions.
Personally I think str_sorted is horrible, int -> str -> array -> str.
// Like Python's range.
function range(start, stop, step){
    if (typeof(stop)==='undefined'){
        stop = start;
        start = 0;
    }
    if (typeof(step)==='undefined') step = 1;
    if (stop === null){
        while (true){
            yield start;
            start += step;
        }
    }else{
        for (number = start; number < stop; number += step){
            yield number;
        }
    }
}

function str_sorted(num){
    num = num.toString();
    arr = Array();
    for (index in num){
        arr.push(num[index]);
    }
    return arr.sort().join('');
}

function permuted(num){
    tmp = str_sorted(num);
    return tmp == str_sorted(num * 2) &&
           tmp == str_sorted(num * 3) &&
           tmp == str_sorted(num * 4) &&
           tmp == str_sorted(num * 5) &&
           tmp == str_sorted(num * 6);
}

function find(iterable, fn){
    for (num in iterable){
        if (fn(num)){
            return num;
        }
    }
}

print(find(range(1, null), permuted));

The way that I run this code is via the command line.
$ js p52.js
142857
$

This runs the the SpiderMonkey 1.8.5 interpreter. I got it from the Arch repository at some point.


texts:
I've written my first web scraper, which (surprisingly) does the job. I'm scraping a popular comic website for the images (there are over 950 of them) but the problem is the scraper is way too slow.

For example, if I download a sample of 10 comics it takes an average of 4 to 5 secs per image (a total of > 40 secs for the sample), which is a bit too slow if you ask me because each image is approx. the size of 80KB to 800KB.

I've read I could switch to LXML to do the scraping asynchronously but the package is not compatible with Python 3.6.

I'm submitting the scraper for both the code and performance review. Any insight on both topic will be highly appreciated.

import time
import os
import sys
import re
import requests
import itertools
from requests import get
from bs4 import BeautifulSoup as bs

HOME_DIR = os.getcwd()
DEFAULT_DIR_NAME = 'poorly_created_folder'

def show_logo():
  print("""
a Python comic(al) scraper for poorlydwarnlines.com
                         __
.-----.-----.-----.----.|  |.--.--.
|  _  |  _  |  _  |   _||  ||  |  |
|   __|_____|_____|__|  |__||___  |
|__|                        |_____|
                __ __   __
.--.--.--.----.|__|  |_|  |_.-----.-----.
|  |  |  |   _||  |   _|   _|  -__|     |
|________|__|  |__|____|____|_____|__|__|

.-----.----.----.---.-.-----.-----.----.
|__ --|  __|   _|  _  |  _  |  -__|   _|
|_____|____|__| |___._|   __|_____|__|
                      |__|
version: 0.2 | author: baduker | https://github.com/baduker
  """)

def handle_menu():
  print("\nThe scraper has found {} comics.".format(len(found_comics)))
  print("How many comics do you want to download?")
  print("Type 0 to exit.")

  while True:
    try:
      global n_of_comics
      n_of_comics = int(input(">> ").strip())
    except ValueError:
      print("Error: incorrect value. Try again.")
      continue
    if n_of_comics > len(found_comics) or n_of_comics < 0:
      print("Error: incorrect number of comics to download. Try again.")
      continue
    elif n_of_comics == 0:
      sys.exit()
    else:
      break
  return n_of_comics

def move_to_dir(title):
  if os.getcwd() != HOME_DIR:
    os.chdir(HOME_DIR)
  try:
    os.mkdir(title)
    os.chdir(title)
  except FileExistsError:
    os.chdir(title)
  except:
    print("Couldn't create directory!")

def generate_comic_link(array, num):
  for link in itertools.islice(array, 0, num):
    yield link

def grab_image_src_url(link):
  req = requests.get(link)
  comic = req.text
  soup = bs(comic, 'html.parser')
  for i in soup.find_all('p'):
    for img in i.find_all('img', src=True):
      return img['src']

def download_image(link):
  file_name = url.split('/')[-1]
  with open(file_name, "wb") as file:
    response = get(url)
    file.write(response.content)

def fetch_comic_archive():
  url = 'http://www.poorlydrawnlines.com/archive/'
  req = requests.get(url)
  page = req.text
  soup = bs(page, 'html.parser')
  all_links = []
  for link in soup.find_all('a'):
    all_links.append(link.get('href'))
  return all_links

def filter_comic_archive(archive):
  pattern = re.compile(r'http://www.poorlydrawnlines.com/comic/.+')
  filtered_links = [i for i in archive if pattern.match(i)]
  return filtered_links

show_logo()

all_comics = fetch_comic_archive()
found_comics = filter_comic_archive(all_comics)

handle_menu()

start = time.time()
for link in generate_comic_link(found_comics, n_of_comics):
  print("Downloading: {}".format(link))
  move_to_dir(DEFAULT_DIR_NAME)
  url = grab_image_src_url(link)
  download_image(url)
end = time.time()

print("Successfully downloaded {} comics in {:.2f} seconds.".format(n_of_comics, end - start))


texts: Separate construction phase from constructed phase

texts:
I am sure you can find many weak spots, please let me know about them.
My main question is if this is a sensible approach to separate the construction phase from the constructed phase.

Basically I have a set of objects that needs to be transformed into similar (but different) objects. Once transformed I want to be able to query these later.

So I have a PartitionedContainersBuilder which deals with the transformation part and I have a PartitionedContainer which represents the transformed query-able.

What I do like with this is that there is no invalid state anywhere and little risk of doing mistakes with member variables. Had Java been able to return two values then I would probably have solved it differently. However with that said, I know for example that calling it builder and build may not be the best thing (it is not the builder pattern).

I would prefer having the constructor in PartitionedContainer do the work rather than a static build - would that be better? My gut feeling says yes, but is there a tangible advantage?

public class PartitionedContainers {

static class PartitionedContainersBuilder
{
    private LogicalExcelAreaInterface buildLogicalExcelArea(ContainerNodeInterface pContainer, PapyrusInterface pPapyrus)
    {
        // TODO: introduce LeafItemContainer to remove if cases
        if (pContainer.getItem() != null)
        {
            return pContainer.getItem().getLogicalExcelArea(pPapyrus);
        } else {
            return new MutableLogicalExcelArea(1, 1)
                        .withCellWidth(0, 0, pPapyrus.getWidth())
                        .withCellHeight(0, 0, pPapyrus.getHeight());
        }
    }

    public PartitionedContainers build(final ContainerNodeInterface pRootContainer, final Map<ContainerInterface, PapyrusInterface> pPapyrusFromContainer)
    {
        final Map<PartitionedContainerInterface, PapyrusInterface> lPapyrusFromContainer = new HashMap<PartitionedContainerInterface, PapyrusInterface>();
        final Map<ContainerNodeInterface, MutablePartitionedContainer> lPartitionedContainerFromContainer = new HashMap<ContainerNodeInterface, MutablePartitionedContainer>();

        for (final ContainerNodeInterface lNode : new NodeCollections<ContainerNodeInterface>().getAllSortedTopDown(pRootContainer))
        {
            lPartitionedContainerFromContainer.put(
                    lNode,
                    new MutablePartitionedContainer(lNode.getElement(), buildLogicalExcelArea(lNode, pPapyrusFromContainer.get(lNode)))
            );
            if (!lNode.isRoot()) {
                lPartitionedContainerFromContainer.get(lNode).withParent(lPartitionedContainerFromContainer.get(lNode.getParent()));
                lPartitionedContainerFromContainer.get(lNode.getParent()).withAdditionalChild(lPartitionedContainerFromContainer.get(lNode));
            }
            lPapyrusFromContainer.put(lPartitionedContainerFromContainer.get(lNode), pPapyrusFromContainer.get(lNode));
        }
        return new PartitionedContainers(lPartitionedContainerFromContainer.get(pRootContainer), lPapyrusFromContainer);
    }

}

PartitionedContainerInterface mRootContainer;
Map<PartitionedContainerInterface, PapyrusInterface> mPapyrusFromContainer;

private PartitionedContainers(PartitionedContainerInterface pRootContainer, Map<PartitionedContainerInterface, PapyrusInterface> pPapyrusFromContainer)
{
    mRootContainer = pRootContainer;
    mPapyrusFromContainer = pPapyrusFromContainer;
}

public PartitionedContainerInterface getRoot()
{
    return mRootContainer;
}

public static PartitionedContainers build(ContainerNodeInterface pRootContainer, final Map<ContainerInterface, PapyrusInterface> pPapyrusFromContainer)
{
    return new PartitionedContainersBuilder().build(pRootContainer, pPapyrusFromContainer);
}

public PapyrusInterface getPapyrusFromContainer(PartitionedContainerInterface pContainer)
{
    return mPapyrusFromContainer.get(pContainer);
}
}


texts: SQL Look up in a Stored Procedure across three tables

texts:
The idea of the below code is that I feed back to a Gridview if the user has permission to view the property otherwise that property is not show.

The data is passed in via SessionParameters

USE [database]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

alter PROCEDURE [dbo].[spSafeguardingActionPropertyByPermission]

@RegionID bigint
,@EmployeeID varchar(10)
,@PropertyID varchar(10)

AS

BEGIN

SELECT  TblA.PropertyID as A_PropertyID,
        TblA.Propertyname as A_Propertyname ,
        TblB.FireSafety as FireSafety1,
        TblB.DisplayScreenEquipment as DSE
FROM    TbPropertyDetails as TblA
        INNER JOIN TbPropertyDetailsSafeguarding as TblB
            ON TblA.PropertyID = TblB.PropertyID
WHERE   TblA.RegionID > 0
AND     TblA.PropertyID LIKE '%' + @PropertyID + '%'
AND     EXISTS
        (   SELECT  1
            FROM    tblPropertyViewPermissions AS pvp
            WHERE   pvp.EmployeeID = @EmployeeID
            AND     pvp.PropertyID = TblA.PropertyID
        )

        END