Accessing files in a child directory in Python 2.7.x I have written the below script for use in Python 2.7.x. In essence, I want the script to access a folder contained within the directory the script is and then add all files contained within to a list. I'm then looking to open these files using the csv module and process each line for now.

My script works fine as below but it just seems to be a long-winded way to access the content of files stored in a child directory.

Any suggestions to improve it?

import os
import csv

child_files = []
mypath = "./child_directory/"
onlyfiles = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]
for f in onlyfiles:
    file_path = os.path.relpath(f)
    x = os.path.join(mypath, file_path)
    child_files.append(x)
    print x

for f in child_files:
    with open(f, 'rb') as x:
        reader = csv.reader(x)
        for row in reader:
            print row

I'd also be keen to only add '.csv' files contained within the child directory. I can work on this upon a review of my script unless someone can suggest a pythonistic method of incorporating this.

I suggest a generator:

def children_files(dir):
    onlyfiles = (f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir, f)))
    for f in onlyfiles:
        file_path = os.path.relpath(f)
        yield os.path.join(dir, file_path)

It is simpler to write (no append) and faster as files will be read just as needed.


`set_includes` (string subsequence-containment) in Python 2 I've implemented C++'s std::includes algorithm in Python, so that I can use it to efficiently implement a Scrabble "can I make this word" function:

def word_can_be_made_from_rack(word, rack):
    return set_includes(sorted(rack), sorted(word))

Here's the implementation, with some test cases:

def set_includes(haystack, needle):
    j = 0
    hn = len(haystack)
    for c in needle:
        while j != hn and haystack[j] < c:
            j += 1
        if j == hn:
            return False
        if haystack[j] > c:
            return False
        j += 1
    return True

assert set_includes('abcdef', 'af')
assert set_includes('abcdef', 'bce')
assert set_includes('abcdef', 'abcdef')
assert set_includes('aaaaa', 'a')
assert set_includes('aaaaa', 'aa')
assert set_includes('aaaaax', 'ax')
assert set_includes('abbbcxx', 'abc')

This is similar to Find if one list is a subsequence of another except that it assumes (and requires) that the two input strings are sorted.

The manual management of index j in this code doesn't feel very Pythonic. Am I missing an easier way to write this algorithm?

itertools one-liners will be accepted as answers, especially if they're more performant. :)

AFNP. The loop condition j != hn is more idiomatically expressed as an exception:

try:
    for c in needle:
        while haystack[j] < c:
            ....
except IndexError:
    return False

No naked loops. Factor the while haystack[j] < c: into a function. Among other benefits, it'd allow

    j = search_character(haystack[j:], c)

The binary search for c seems more performant than linear. See bisect module.


Overloading operators in PhoneCall class I am trying to understand the concepts of friend functions, overloaded operators, and inheritance in C++. I am very confused on the specifics used for coding, since I am fairly new in programming, and working in the Visual Studio C++ environment in writing code.

The following is the project details for writing a program in Visual Studio C++:

  Design a PhoneCall class that holds a phone number to which a call
  is placed, the length of the call in minutes, and the rate charged per
  minute. Overload extraction and insertion operators for the class. In
  this program, overload the == operator to compare two PhoneCalls.
  Consider one PhoneCall to be equal to another if both calls are
  placed to the same number. Also, create a main() function that allows
  you to enter ten PhoneCalls into an array. If a PhoneCall has
  already been placed to a number, do not allow a second PhoneCall to
  the same number.

I really need some feedback on the following code:

#include <iostream>
#include <string>

using namespace std;

class PhoneCall {
private:
    string phonenumber;
    double perminuterate;
    double calldurationminutes;
public:
    bool operator==( const PhoneCall &n ) const;
    friend ostream & operator<<( ostream &f, const PhoneCall &n );
    friend istream & operator>>( istream &f, PhoneCall &n );
 };
 bool PhoneCall::operator==( const PhoneCall &n ) const {
    return phonenumber == n.phonenumber;
 };
 ostream & operator<<( ostream &f, const PhoneCall &n ) {
    f << "Phone number: " << n.phonenumber <<
         ", Duration: " << n.calldurationminutes <<
         " minutes, Rate: " << n.perminuterate << endl;
 return f;
 }
 istream & operator>>( istream &f, PhoneCall &n ) {
 f >> n.phonenumber;
 f >> n.calldurationminutes;
 f >> n.perminuterate;
 return f;
 }

int main( ) {
 PhoneCall a[10];
 cout << "Enter 10 phone numbers, duration in minutes, and the per-minute rates." <<
     endl << "Separate each with a space and then hit enter to complete it." << endl;
 for ( int i= 0; i < 10; ) {
     cin >> a[i];
     int j;
     for ( j= 0; j < i; ++j )
         if ( a[i] == a[j] ) {
             cout << "Duplicate number information ignored. Try again." << endl;
             break;
         }
     if ( j == i ) ++i;
 }
 for ( int i= 0; i < 10; ++i )
     cout << a[i];

 system("pause");
 return 0;
 }

using namespace std;

This is generally frowned upon, especially at the "global" level like this. Since you don't know what names might be defined inside namespace std, this has the potential to create conflicts with your code.

class PhoneCall {
private:

Although it's harmless, the private: here is redundant--members of a class are private by default.

    string phonenumber;
    double perminuterate;
    double calldurationminutes;

I think I'd rename these to something like:

string phone_number;
double per_minute_rate;
double call_duration_minutes;

If at all possible, I'd use an std::vector instead of a "raw" array, so this:

 PhoneCall a[10];

...would turn into something like:

std::vector<PhoneCall> a(10);

std::endl is drastically overused. I'd generally just use '\n';. endl also flushes the file's buffer, which you rarely want. When you read/write cin and cout, flushes are done automatically between writing and reading anyway.

 cout << "Enter 10 phone numbers, duration in minutes, and the per-minute rates." <<
     endl << "Separate each with a space and then hit enter to complete it." << endl;
 for ( int i= 0; i < 10; ) {
     cin >> a[i];
     int j;
     for ( j= 0; j < i; ++j )
         if ( a[i] == a[j] ) {
             cout << "Duplicate number information ignored. Try again." << endl;
             break;
         }
     if ( j == i ) ++i;
 }

You might want to use std::find to find whether the array already contains an instance of a particular phone number. This could simplify your logic quite a bit.

When you print out your phone call objects:

 for ( int i= 0; i < 10; ++i )
     cout << a[i];

...you traditionally want to only print out the data itself in your operator<<, and leave any final new-line to separate one object from the next to be printed somewhere else (like right here).

 system("pause");

I'd avoid this, and use something like:

std::cout << "Press return to quit."
char ch;
std::cin.get(ch);


C#; Working code--needs review Techies--
This routine does more once the channels + the subsets of existing batch arrays are re-distributed for processing. However, the success of that will only work as well as the initial splits. The threshold value is dubbed here, but it will come from app.config as configurable setting. Obviously the jagged string array is dubbed for review purposes. The real values will be coming from another routine and will frequently have counts > 100000. The 3 lines of comments where one will do is a product of formatting the code for StackExchange.

Please review what's here.

   #region Channel Assignment Testing

    static void ChannelAssign()
    {
        int THRESHOLD = 1;

        string[] batch = new string[]
            { "item1", "item2", "item3", "item4","item5","item6","item7" };
        int batchcnt = batch.Count();
        int subsetqty; // subset batch item quantity
        string[][] subsets; // Split extension returns these values
        int channelidx; // index for channels back from Split

        if (THRESHOLD != 0) //avoid accidental division by 0.
        {
            float channels = batchcnt/THRESHOLD;
            if (channels < 1) // can handle all items in existing batch based on
                              // threshold setting.
            {
                channels = 1; // dub as a single channel
                subsetqty = batchcnt; // process all batch items in batch
                channelidx = 1; // dub index for a single channel
                subsets = batch.Split(batchcnt); // ship the batchcount
                                                 // to split routine.
            }
            else // we need at least 2 channels; round up from float
                 // and decide how many.
            {
              channels =  (int)Math.Round(channels,
                            MidpointRounding.ToEven); //determines channel# estimate
              subsetqty = batchcnt/(int)channels; // estimates # of
                                                  // existing batch items to
                                                  // place in each subset
              subsets = batch.Split(subsetqty); // actual subsets of batches returned
              channelidx = subsets.GetLength(0); //gets actual channel# assigned
                                                 // by split

            }

            //distribute contents of batch into subsets for channel consumption
            for (int channel = 0; channel < channelidx; channel++)
            {
                for(int i = 0; i < subsets[channel].Length; i++)
                {
                    Console.WriteLine(" Channel:" + channel.ToString() + " ItemName:
                      {0} ", subsets[channel][i]);
                }
            }
        }
        else
        {
            Console.WriteLine("Threshold value set to zero. This is an
                                invalid value. Please set THRESHOLD.");
        }

    }

    #endregion

Split Extension

        public static T[][] Split<T>(this T[] arrayIn, int length)
        {
            bool even = arrayIn.Length % length == 0;
            int totalLength = arrayIn.Length / length;
            if (!even)
                totalLength++;

            T[][] newArray = new T[totalLength][];
            for (int i = 0; i < totalLength; ++i)
            {
                int allocLength = length;
                if (!even && i == totalLength - 1)
                    allocLength = arrayIn.Length % length;

                newArray[i] = new T[allocLength];
                Array.Copy(arrayIn, i * length, newArray[i], 0, allocLength);

            }

            return newArray;
        }

I like the formatting, use of white space and tabs.

I'm not sure what THRESHOLD is supposed to represent or where its set, but in the example, I'd make it a const.

I would do the check for 0 right below the assignment:

if (THRESHOLD == 0)
{
    ...

    return;
}

This will un-nest the main logic of your function and catch the error before any processing is done.

The logic should be moved out into their own functions:

var subsets = (channels < 1) ? ProcessWholeList(...) : ProcessPartialList(...);

This will better portray then intention of the logic and remove most of the unimportant variables out of the function cleaning it up quite a bit.

I like that you have created an extension to do the split, not many people think of that.

I would change the bool even ... into a check for odd numbers.  That what you could change the

if (!even)

to

if (odd)
{
    ....
}

Again, expresses the intent a little better.  I would also either move the assignment to right above the check.

This assignment:

int allocLength = length;
if (!even && i == totalLength - 1)
    allocLength = arrayIn.Length % length;

Should be changed to:

var allocLength = (!even && i == totalLength - 1) ? arrayIn.Length % length : length;

There are just a few simple changes that will clean it up quite a bit.  I hope this is a good start and gives you some ideas.

I have another question, is there a reason why you are using a 2 dimensional array rather than one of the build in IEnumerables (i.e. HashTable)?  I think for performance purposes, the two dimensional array would be the slowest way of doing it.


Obtaining project size metrics with PowerShell I'm often frustrated by the lack of ability to obtain simple size metrics about code projects I'm working on, like total lines of code, or character count. To fix this, I wrote the following PowerShell utility:

Function GetProjectMetrics
{
    [CmdletBinding()]
    Param(
        [Parameter(mandatory=$True)]
        [string]$projectPath,

        [Parameter(mandatory=$True)]
        [string]$fileFilter,

        [switch]$ignorews
    )

    Set-Location $projectPath
    Switch($ignorews)
    {
        $True
        {
            Get-ChildItem -recurse -include $fileFilter                   `
                | Get-Content                                             `
                | Measure-Object -Character -Word -Line -IgnoreWhiteSpace
            Break
        }

        $False
        {
            Get-ChildItem -recurse -include $fileFilter `
                | Get-Content                           `
                | Measure-Object -Character -Word -Line
            Break
        }

        Default
        {
            Write-Host "Error executing 'GetProjectMetrics'."
            Break
        }
    }
}

New-Alias -Name gpm -Value GetProjectMetrics
New-Alias -Name getpm -Value GetProjectMetrics

The above code is placed within my Microsoft.PowerShell_profile.ps1 file (my PowerShell profile), which gives me the ability to use it straight from the PowerShell command line.

Here's some example usage:

PS C:\Users\Ethan> gpm ".\Documents\MyPythonProject" "*.py" -ignorews
PS C:\Users\Ethan> getpm ".\Documents\MyPythonProject" "*.py"
PS C:\Users\Ethan> GetProjectMetrics ".\OneDrive\CS Projects\CS Tests" "*.cs" -ignorews

What can I improve?

My suggestions:

apply PowerShell scripting best practices; for particulars, see Concept: A list of some Best Practices for Windows PowerShell by powershell-guru;
get more knowledge from the utility (see Count and Property output properties; see also -Detailed input parameter);
allow -LiteralPath to take range operator characters in project path literally;
use Set-Alias -Name … -Value … -Force rather than New-Alias;
(not implemented yet) consider involving -Encoding parameter for Get-Content cmdlet.

The script (rewritten almost completely):

<#
 SYNOPSIS: Calculates simple size metrics about code projects.
          Combination of `Get-ChildItem`, `Get-Content` and `Measure-Object`

  Parameter         Meaning (see native cmdlet)
  ---------         ---------------------------
  -Path             obvious (Convert-Path, Get-ChildItem, Get-Item, Push-Location)
  -LiteralPath      obvious (Convert-Path, Get-ChildItem, Get-Item, Push-Location)
  -Filter           obvious (Get-ChildItem)
  -IgnoreWhiteSpace obvious (Measure-Object)
  -Detailed         returns size metrics about individual files severally
  -PushLocation     side effect: `Push-Location`

 RETURNS: Custom combination of GenericMeasureInfo and TextMeasureInfo:

  Name       Type     Meaning
  ----       ----     -------
  Count      [int]    number of files of given pattern in given (literal)path
  Lines      [int]    number of lines
  Words      [int]    number of words
  Characters [int]    number of characters
  Property   [string] tested path
#>

Set-StrictMode -Version latest

Function Measure-Project
{
    [CmdletBinding(DefaultParameterSetName='Items')]
    Param(
        [Alias('projectPath')]
        [Parameter(Mandatory=$false,
                   Position=0,
                   ParameterSetName='Items')]
        [string]$Path='.\',

        [Parameter(Mandatory=$false,
                   Position=0,
                   ParameterSetName='LiteralItems')]
        [string]$LiteralPath='.\',

        [Alias('fileFilter')]
        [Parameter(Mandatory=$false,
                   Position=1)]
        [string]$Filter='*.ps1',

        [Alias('IgnoreWS')]
        [switch]$IgnoreWhiteSpace,

        [Alias('PushD')]
        [switch]$PushLocation,

        [switch]$Detailed
    )
    Function out ($Cnt, $Prm, $Obj) {
        $auxCounter = $Obj | Measure-Object -Character -Word -Line @auxParMO
        $auxCounter | Add-Member -NotePropertyName Count -NotePropertyValue $Cnt
        $auxCounter.Property = $Prm
        return $auxCounter
    }
    if ( $PSCmdlet.ParameterSetName -eq 'LiteralItems') {
        $auxParTP = @{ LiteralPath = $LiteralPath }
    } else {
        $auxParTP = @{ Path = $Path }
    }
    if ( Test-Path @auxParTP ) {
        $auxPath = Convert-Path @auxParTP
        if ( (Get-Item @auxParTP) -is [System.IO.DirectoryInfo]) {
            $auxFiles = Get-ChildItem @auxParTP -Filter $Filter -Recurse -File
            $auxParMO = @{ IgnoreWhiteSpace = $IgnoreWhiteSpace.IsPresent }
                         # $PSBoundParameters.ContainsKey('IgnoreWhiteSpace')
            if ($Detailed.IsPresent -and $auxFiles) {
                $auxFiles | ForEach-Object {
                    out -Cnt 1 -Prm $_.FullName -Obj ($_ | Get-Content)
                }
            } else {
                $auxPar = Join-Path -Path $auxPath -ChildPath $Filter
                if ( $auxFiles ) {
                    if ( $auxFiles -is [array] ) {
                        $auxCnt = $auxFiles.Count
                    } else {
                        $auxCnt = 1
                    }
                    out -Cnt $auxCnt -Prm $auxPar -Obj ($auxFiles | Get-Content)
                } else {
                    Write-Warning "No '$Filter' inside '$Path'"
                    out -Cnt 0 -Prm $auxPar -Obj ''
                }
            }
            if ( $PushLocation.IsPresent ) {Push-Location @auxParTP}
        } else {
            $auxMsg = "'$($auxParTP.Values.GetEnumerator())' is not a directory"
            Write-Error -Message $auxMsg -Category InvalidArgument
        }
    }
    else
    {
        $auxMsg = "Cannot find path '$($auxParTP.Values.GetEnumerator())'"
        Write-Error -Message $auxMsg -Category ObjectNotFound
    }
}

Sample output demonstrates necessity of -Encoding parameter as both files contain nearly the same text so the utility should show the same count of characters. Of course, I'd expect the same encoding of source files in a particular project so sorry for the following simplified example:

  PS D:\PSh> Measure-Project -LiteralPath 'D:\bat\[source]\XX\' -filter * -Detailed | ft

Count Lines Words Characters Property
----- ----- ----- ---------- --------
    1     1     5        118 D:\bat\[source]\XX\[sourcefile].log
    1     1     5         59 D:\bat\[source]\XX\[sourcefile].txt

PS D:\PSh> type -LiteralPath 'D:\bat\[source]\XX\[sourcefile].txt'
Toto je obsah souboru `D:\bat\[source]\XX\[sourcefile].txt`

PS D:\PSh> type -LiteralPath 'D:\bat\[source]\XX\[sourcefile].log' -Encoding Unicode
Toto je obsah souboru `D:\bat\[source]\XX\[sourcefile].log`


Designing a stack such that getMin() is both constant time and constant space I'm attempting the SpecialStack problem on GeeksforGeeks.

  Design a data-structure SpecialStack (using the STL of stack) that
  supports all the stack operations like push(), pop(), isEmpty(),
  isFull() and an additional operation getMin() which should return
  minimum element from the SpecialStack. Your task is to complete all
  the functions, using stack data-Structure.

  Input Format: The first line of input contains an integer T denoting
  the no of test cases. Then T test cases follow. Each test case
  contains two lines. The first line of input contains an integer n
  denoting the number of integers in a sequence. In the second line are
  n space separated integers of the stack.

  Output Format:

  For each testcase, in a new line, print the minimum
  integer from the stack.

  Your Task:

  Since this is a function problem, you don't need to take
  inputs. Just complete the provided functions.

  Constraints:

  1 <= T <= 100

  1 <= N <= 100

  Example:

  Input:

  1

  5 18 19 29 15 16

  Output: 15

This is my code, including the Driver Code which was provided beforehand by the interface (the Driver Code is not modifiable):

// { Driver Code Starts
#include<iostream>
#include<stack>
using namespace std;
void push(int a);
bool isFull(int n);
bool isEmpty();
int pop();
int getMin();
//This is the STL stack (http://quiz.geeksforgeeks.org/stack-container-adaptors-the-c-standard-template-library-stl/).
stack<int> s;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,a;
        cin>>n;
        while(!isEmpty()){
            pop();
        }
        while(!isFull(n)){
            cin>>a;
            push(a);
        }
        cout<<getMin()<<endl;
    }
}// } Driver Code Ends
/*Complete the function(s) below*/

int minEle = 0; // Holds minimum element so far, as elements are pushed into stack

void push(int a)
{
     if (s.empty()) // If stack is empty push a and update minEle to a
     {
         minEle = a;
         s.push(a);
         return;
     }

     if (a < minEle) // If a is less than minEle push (a-minEle) and update minEle to a
     {
         s.push(a - minEle);
         minEle = a;
         return;
     }

     if (a > minEle) // If a is greater than minEle then push a but don't update minEle
     {
         s.push(a);
     }
}

bool isFull(int n)
{
     return (s.size() == n);
}

bool isEmpty()
{
     return s.empty();
}

int pop()
{
    int top = s.top();
    if (top < 0) // If the top element of stack is negative, we reconstruct the previous minEle
    {
        minEle = minEle - top;
        s.pop();
    }
    else // Otherwise, just pop the top element
    {
        s.pop();
    }
    return top;
}

int getMin()
{
    return minEle;
}

I've tried to explain the logic in the comments. This code gives the correct solution for all the test cases (with positive integers) I tried so far. For instance:

For Input:
2
5
18 19 29 15 16
11
34 335 1814 86 82 7 332 82 221 95 40
Your Output is:
15
7

which is the correct solution!

However, when I try to submit the code, it says:

Your program took more time than expected.Time Limit Exceeded
Expected Time Limit < 1.3672sec
Hint : Please optimize your code and submit again.

As far as I understand, my code is \$\mathcal{O}(1)\$ in both time and space complexity and it uses no extra stacks. I'm not quite sure how to optimize the code runtime further. Any ideas?

If the grader is complaining about "time limit exceeded," it must be because some loop is executing too many times. All of your functions are clearly O(1) — they have no loops. So which loop is executing too many times? It must be the only loop in the entire program:

    while(!isFull(n)){
        cin>>a;
        push(a);
    }

Is it possible that your isFull is returning an incorrect answer? It's very straightforward: it tests s.size() == n. Is it possible that push(a) is not updating s.size()?

Reflowed for brevity and clarity, your push looks like this:

void push(int a)
{
     if (s.empty()) {
         minEle = a;
         s.push(a);
     } else if (a < minEle) {
         s.push(a - minEle);
         minEle = a;
     } else if (a > minEle) {
         s.push(a);
     }
}

Do you see the bug yet? The problem is that when a == minEle, you never push anything! So if you get the input 1 1, you'll cause the test driver to loop forever... and that is what causes the timeout.

Also consider that there is no limit on the values of the integers you're given (just on the number of test cases T and the number of integers per test case N). So you'll have to find a way to make your code work with input -1 as well.

Consider maintaining a second stack std::stack<int> minEle alongside your std::stack<int> s. Does that make the problem easier?

EDITED TO ADD: If you kept the current push, then you could rewrite it for even more clarity by factoring the s.push to the bottom of the function, like this:

void push(int a)
{
     if (s.empty()) {
         minEle = a;
     } else if (a < minEle) {
         a -= std::exchange(minEle, a);
     } else if (a > minEle) {
         // do nothing
     } else {
         return;  // uh-oh!
     }
     s.push(a);
}

This shows clearly that each call to push results in one and only one call to s.push (except for the buggy case).


Matching and pushing array This code works, but I feel like it's really hacky and could be done a lot better. Can anyone give some advice on how this could be neater?

$scope.arrays = function() {
        $scope.committees = [];
        $scope.employees = [];

        $http({method: 'GET', url: 'data.php'}).success(function(data) {
            angular.forEach(data, function(value, key) {
                $scope.committees.push(value);
            })

            $http({method: 'GET', url: 'data2.php'}).success(function(data){
                angular.forEach(data, function(value, key) {
                    $scope.employees.push(value);
                })
                $scope.arr = new Array();
                for (var i = 0, committee; committee = $scope.committees[i]; i++) {
                    for (var j = 0, employee; employee = $scope.employees[j]; j++) {
                            if (committee.code === employee.code){
                                $scope.arr.push({
                                    "committee" : committee.name,
                                    "color": committee.color,
                                    "members": committee.members,
                                    "femaleAnnual" : employee.femaleAnnual,
                                    "maleAnnual" : employee.maleAnnual,
                                    "totalAnnual" : employee.totalAnnual,
                                })
                            }
                    }
                }
            })
        })
    }

    $scope.arrays();

Edit: Sorry I should have been clearer! What I'm trying to do here is merge 2 arrays if they have the same value for 'code' - and then push that array to the scope.

You could take advantage of the fact that push accepts more than one argument so you only call push once. Build the arguments array and use apply.

Array.prototype.push.apply($scope.committees, Object.keys(data).map(function(key){
  return data[key];
}));

Or if you're just putting values instead of actually appending to the existing array:

$scope.committees = Object.keys(data).map(function(key){
  return data[key];
});

Now for the AJAX part, you don't need to have a sequential request, that's that last thing you'd want to do in AJAX (besides making them synchronous). You'll need something like Promise.all or jQuery's $.when, fire them at the same time and resolve when all is done.

var comitteePromise = $http({method: 'GET', url: 'data.php'});
var employeePromise = $http({method: 'GET', url: 'data2.php'});

Promise.all([comitteePromise, employeePromise]).then(function(values){
  var committees = values[0];
  var employees = values[1];
  ...
});

The further I look into your code, what you're just doing is merging the data from both arrays. You can build a lookup for one array, and iterate the other. That way, two loops and not to the power of two loops.

var comitteePromise = $http({method: 'GET', url: 'data.php'});
var employeePromise = $http({method: 'GET', url: 'data2.php'});

Promise.all([comitteePromise, employeePromise]).then(function(values){
  var committees = values[0];
  var employees = values[1];

  // Use the committee code (assuming it's unique) as a key for lookup
  var committeesByCode = committees.reduce(function(carry, committee){
    carry[committee.code] = committee;
    return carry;
  }, {});

  // For each employee, lookup the committee and add in the data
  $scope.arr = employees.map(function(employee){
    var committee = committeesByCode[employee.code];
    return {
      committee: committee.name,
      color: committee.color,
      members: committee.members,
      femaleAnnual: employee.femaleAnnual,
      maleAnnual: employee.maleAnnual,
      totalAnnual: employee.totalAnnual
    };
  });

});


Calculate min, max, mean and median out of array Version 2 This question is a follow up of:
Calculate min, max, mean and median out of array

I took all the suggestions an reworked the code. Like the description of the excercise suggests i only use one function for all the computations. Also with the sorting the max and median values can now be calculated easier.

I wonder if its good to return the Macro types INT_MIN INT_MAX and NAN for indicating no valid result?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <math.h>

struct Summary_data {
    long long smallest;
    long long largest;
    double median;
    double mean;
};

static inline int cmp(void const *lhs, void const *rhs) {
    const int *left = (const int *)lhs;
    const int *right = (const int *)rhs;
    return (*left > *right) - (*left < *right);
}

int* intdup(const int * source, const size_t len)
{
    assert(source);

    int * p = malloc(len * sizeof(*source));

    if (p == NULL)
        exit(1);

    memcpy(p, source, len * sizeof(*source));
    return p;
}

struct Summary_data calculate_values(const int* array, const size_t len)
{
    assert(array);

    struct Summary_data summary_data = { 0 };
    int* calc_array = intdup(array, len);
    size_t i;

    if (len <= 0) {             // dont bother to calc if invalid len
        free(calc_array);
        calc_array = NULL;
        summary_data.smallest = INT_MAX;
        summary_data.largest = INT_MIN;
        summary_data.mean = NAN;
        summary_data.median = NAN;
        return summary_data;
    }

    qsort(calc_array, len, sizeof *array, cmp);

    summary_data.smallest = calc_array[0];
    summary_data.largest = calc_array[len - 1];

    for (i = 0; i < len; ++i) {
        summary_data.mean += calc_array[i];
    }
    summary_data.mean /= len;

    if (len % 2 == 0) { // is even == return the arithmetic middle of the two middle values
        summary_data.median = (calc_array[(len - 1) / 2] + calc_array[len / 2]) / 2.0;
    }
    else {  // is odd  == retunr the middle
        summary_data.median = calc_array[len / 2];
    }

    free(calc_array);
    calc_array = NULL;

    return summary_data;
}

void print_result(const struct Summary_data* summary_data)
{
    assert(summary_data);

    printf("smallest: %i\n", summary_data->smallest);
    printf("largest: %i\n", summary_data->largest);
    printf("median: %g\n", summary_data->median);
    printf("mean: %g\n\n", summary_data->mean);
}

int main()
{
    int test_array[] = { 1,7,3,4,5,6,7,8,9 };       // 9 elements
    //  int test_array[] = { 1,7,3,4,5,6,7,8 };         // 8 elements

    int len = sizeof(test_array) / sizeof(test_array[0]);

    //len = 0;                                          // test when len is invalid
    struct Summary_data summary_data = calculate_values(test_array, len);

    print_result(&summary_data);
    getchar();
    return 0;
}

To add to Peter's answer:

I can only think of one case where you couldn't compute a valid result (min, median, max, mean) for an array and that's when the array is empty (or infinite I suppose, but if that's a problem for you please let me know where you buy your RAM). Since this is trivial to check I would generally leave it up to the caller to do so (and document that expectation in the function's interface). It's just simpler and cleaner.

I have some minor style suggestions:

I advise against using assertions for error checking (except for during debugging). Assertion failures cause your program to immediately terminate. In general you would probably want to propagate errors back up the call stack so that the caller can handle them (hopefully) gracefully.
You check if len is less than zero, but this can't occur as len has an unsigned type.
It makes sense to check len before allocating calc_array. In general, if you are checking parameters you generally want to do it at the beginning of a function. It's more readable and may save you work.
I generally prefer to use the types defined in stdint.h when reasonable (uint32_t, uint64_t etc.).


Generate Unique ID in C# Objective

To create a unique id with few characters as possible.

Language: C#
Engine Unity3D
Target : iOS && iPadOS

Scenario

I found quite a few solutions from Stack Exchange but would like to know if someone could just look at my code to review it. It is a very important module IMO

Code

    /// <summary>
    /// Identifiers the generation.
    /// </summary>
    /// <returns>A Unique ID</returns>
    private string generateUniqueID(int _characterLength = 11)
    {
        StringBuilder _builder = new StringBuilder();
        Enumerable
            .Range(65, 26)
            .Select(e => ((char)e).ToString())
            .Concat(Enumerable.Range(97, 26).Select(e => ((char)e).ToString()))
            .Concat(Enumerable.Range(0, 10).Select(e => e.ToString()))
            .OrderBy(e => Guid.NewGuid())
            .Take(_characterLength)
            .ToList().ForEach(e => _builder.Append(e));
        return _builder.ToString();
    }

In the above code, I always pass _characterLength to be always 11.

Request

Would you recommend the above method? If not, could you please specify why?

/// <summary>
    /// Identifiers the generation.
    /// </summary>
    /// <returns>A Unique ID</returns>
    private string generateUniqueID(int _characterLength = 11)

A good summary should describe what the method does. This summary doesn't really tell me anything. Keep it simple: the method generates IDs, so write the summary as such.

generateUniqueID does not follow C# method naming conventions. Call it GenerateUniqueID. Note the PascalCase.

A character always has a length of 1, so _characterLength is a bit of a confusing name for me. What it describes is actually the length of the ID, so call it idLength, or just length since the thing it's describing is obvious from the method name anyway.

You got the camelCasing right for parameter and local identifiers, but it's not conventional to use underscores. Get rid of those.

It's not clear why you'd give such a method a default value, but in any case that 11 should be defined as a constant somewhere. Avoid magic numbers. By giving that number a name, you give it a meaning that's obvious to the next person who reads your code.

private const int DefaultIDLength = 11;

private string GenerateUniqueID(int length = DefaultIDLength)
...

StringBuilder _builder = new StringBuilder();

It's common to use var when the expression type is obvious.

Your ID will always be length characters long, so use the StringBuilder constructor that takes an initial capacity:

var builder = new StringBuilder(length);

Enumerable
            .Range(65, 26)
            .Select(e => ((char)e).ToString())
            .Concat(Enumerable.Range(97, 26).Select(e => ((char)e).ToString()))
            .Concat(Enumerable.Range(0, 10).Select(e => e.ToString()))
            .OrderBy(e => Guid.NewGuid())
            .Take(_characterLength)
            .ToList().ForEach(e => _builder.Append(e));

This is the crux of your implementation, and there's a lot to unpack here, so let's go step by step.

Enumerable
            .Range(65, 26)
            .Select(e => ((char)e).ToString())
            .Concat(Enumerable.Range(97, 26).Select(e => ((char)e).ToString()))
            .Concat(Enumerable.Range(0, 10).Select(e => e.ToString()))

This produces the same sequence every time you run it. It's painful to read. Why not just specify it as a constant? This gets the point across far easier:

private const string AllowableIDCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

But we'll see later that none of this is necessary.

You don't need all those ToStrings everywhere. You gain nothing by using string as an intermediate representation instead of char. Keep them as chars during the shuffle: StringBuilder.Append() has an overload that accepts char.

I try to avoid writing lambdas with side effects. This would be more conventionally written as a foreach loop, which also avoids the need to convert the thing to a list:

foreach (char c in AllowableIDCharacters
            .OrderBy(e => Guid.NewGuid())
            .Take(length))
{
    builder.Append(c);
}

But by far the biggest problem here is your source of entropy, as Turksarama pointed out in the comments. By shuffling the alphabet, you are not allowing any repeated characters! This cuts down the number of unique IDs you can generate by more than half. Drop the shuffling idea entirely, it's not the best tool for this job.

You cannot generate unique IDs without keeping some kind of state. GUIDs work by generating sequences that, while not unique in a pure sense, are so astronomically unlikely to be repeated that they are "unique" for all practical intents and purposes.

If you embrace the non-determinism approach of generating unique IDs that GUID takes, then the question is no longer "how to generate a unique ID with as few characters as possible". It is now "what is an acceptable trade-off between ID length and probability of collision".

Because your method is just a mechanism for generating IDs given a length, then half of your problem reduces to something much simpler:

Given an ID length n, how do I minimize the probability of a collision?
How do I maximize the amount of information contained within a string of length n?

Both of these are straightforward problems. To minimize collisions you need a source of entropy that is both uniformly distributed and unpredictable. There are a lot of ways to do this, with varying degrees of uniformity and unpredictability, but do note that GUIDs are not uniformly distributed, are somewhat predictable, and are therefore not a great choice here. There is plenty of literature on how to generate such entropy so I won't go into detail for now. A common choice in .NET is to use RNGCryptoServiceProvider.

Maximizing the information contained in the ID means trying to squeeze as many bits of data as possible into that string. You chose an encoding that can represent 62 different things per character, or about 5.95 bits of data per character -- but because of the shuffling issue we talked about earlier, the actual number will be much smaller. For IDs of length 11 it's less than half.

I assume you're limited to ID strings of 8-bit ASCII-ish characters. But the alphabet you came up with is really close to Base 64, so why not just use Base 64? That'll give you an even 6 bits of information per character. By selecting a standard encoding as such, you'll avail to yourself a suite of standard tools for converting to and from that encoding.

Here's an example of how you could do it:

private static readonly RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();

private string GenerateUniqueID(int length)
{
    // We chose an encoding that fits 6 bits into every character,
    // so we can fit length*6 bits in total.
    // Each byte is 8 bits, so...
    int sufficientBufferSizeInBytes = (length * 6 + 7) / 8;

    var buffer = new byte[sufficientBufferSizeInBytes];
    random.GetBytes(buffer);
    return Convert.ToBase64String(buffer).Substring(0, length);
}

This gives you 6 bits of entropy per character. It's up to you and the constraints of your business domain to pick an appropriate length. By the "birthday problem" rule of thumb, the probability of two random IDs of n bits to conflict with each other is about one in 2^(n/2); so an ID length of 11 gives you 66 bits, with a one in 2^33 chance of two IDs conflicting.


Queue implementation in Java This is my Queue implementation in Java. Would appreciate a review of the code.

import java.util.Iterator;

public class Queue<T>implements Iterable<T> {

    private Node first, last;
    private int size;

    @Override
    public QueueIterator iterator() {
        return new QueueIterator();
    }

    private class QueueIterator implements Iterator<T>{
        private Node current = first;
        public boolean hasNext(){
                return current != null;
        }
        public T next(){
            T item = current.data;
            current = current.next;
            return item;
        }

    }

    private class Node{
        Node(T data){
            this.data = data;
            this.next = null;
        }
        T data;
        Node next;
    }
    public Queue(){
        first = last = null;
        size = 0;
    }

    public void enqueue(T item){
        Node p = new Node(item);
        if(first == null){
            first = last = p;
            size++;
            return;
        }
        last.next = p;
        last = p;
        size++;

    }

    public T dequeue(){

       Node p = first;
        try{
            first = first.next;
            size--;
        }catch(Exception e){
            System.out.println("Dequeing an empty queue :" + e);
        }

        return p.data;

    }

    public boolean isEmpty(){
        return last == null;
    }

}

Bug

dequeue doesn't set last to null when the queue becomes empty. On the other hand, isEmpty relies on last being null.
enqueue should be streamlined to

    if (first == null) {
        first = p;
    } else {
        last->next = p;
    }
    last = p;
    size++;

to clearly state common vs different actions.


Caterpillar uneaten leaves K caterpillars are eating their way through N leaves, each caterpillar
  falls from leaf to leaf in a unique sequence, all caterpillars start
  at a twig at position 0 and falls onto the leaves at position between
  1 and N. Each caterpillar j has as associated jump number Aj. A
  caterpillar with jump number j eats leaves at positions that are
  multiple of j. It will proceed in the order j, 2j, 3j…. till it
  reaches the end of the leaves and it stops and build its cocoon. Given
  a set A of K elements K<-15, N<=10^9, we need to determine the number
  of uneaten leaves.

  Input:

  N = No of uneaten leaves
  K = No. of caterpillars
  A = Array of integer jump numbers

  Output:

  The integer nu. Of uneaten leaves

  Sample Input:

10
3
2
4
5

  Output:

4

  Explanation:

  [2, 4, 5] is a j member jump numbers, all leaves which
  are multiple of 2, 4, and 5 are eaten, leaves 1,3,7,9 are left, and
  thus the no. 4

Detailed explanation Caterpillars and Leaves

public static int findUneatenLeaves(int[] array, int n) {
    ArrayList<Integer> uneatenLeaves = new ArrayList<Integer>();
    ArrayList<Integer> eatenLeaves = new ArrayList<Integer>();
    for (int i = 1; i <= n; i++) {
        uneatenLeaves.add(i);
    }
    // 1. find the multiple of the eatenLeaves
    for (int i = 0; i < array.length; i++) {
        eatenLeaves.add(array[i]);
        for (int j = 1; j < uneatenLeaves.size(); j++) {
            if (array[i] * uneatenLeaves.get(j) <= n) {
                eatenLeaves.add(array[i] * uneatenLeaves.get(j));
            }
        }
    }
    for (int i = 0; i < eatenLeaves.size(); i++) {
        for (int j = 1; j < uneatenLeaves.size(); j++) {
            if (eatenLeaves.get(i) == uneatenLeaves.get(j)) {
                uneatenLeaves.remove(uneatenLeaves.get(j));
            }
        }
    }
    System.out.println(uneatenLeaves.size());
    return uneatenLeaves.size();
}

How can this be optimized?

Best practices:

So I'll just shortly go through some of the basic beginner mistakes and best-practice "violations" I encounter around here, and tell you how well you did :)

Formatting:

On this front that's an awesome job, well done

Abstraction:

The code's a little lacking in that respect. The general goal in Java is to program against interfaces. This means:

ArrayList<Integer> uneatenLeaves = new ArrayList<Integer>();

becomes (after applying the Diamond Operator)

List<Integer> uneatenLeaves = new ArrayList<>();

Naming:

Naming is one of the hardest things to get right when coding. It's extremely important to have names speaking for themselves, variables containing "exactly what it says on the tin".
This allows to grasp the meaning of code much quicker.

array and n aren't really good names. If I understand the code correctly, jumpNumbers and limit would be better names.

Actual code-review

As already mentioned in a comment, this problem seems to be a variation of the Sieve of Eratosthenes, which is used for finding prime numbers.
Can you see the parallels yet?

To make it simple, you want a way to model two states. The better choice than a number (going from roundabout -2.150.000 to 2.150.000) is a boolean, which is either true or false.

Now we also know how often that state needs to be modeled, each leaf gets a separate boolean. If we pass the leaf with a caterpillar it gets eaten, and is no more there. The simplest way to model that is setting the symbolizing boolean to false.

Another interesting construct here is the do-while loop. A caterpillar eats the next leaf, if (and only if) the next leaf exists. The next leaf is calculated by \$A_j * increment\$ or even \$current + A_j\$. In combination with the number of leaves this makes a nice "terminating condition":

while (nextLeaf < leaves.size)

now we just do that for each caterpillar and suddenly we end up at following pseudocode:

// leaf 0 has already been eaten, since caterpillars always start there
foreach (jump_size in jump_numbers) {
    current = 0;
    while (next_leaf < leaf_count) {
       current = current + jump_size;
       eat current;
    }
}

Implementing this into actual code is left as an exercise to the reader ;)


Pandas DataFrame.sort_values() new method to exclude by row name I was working on new function with Pandas DataFrame. It's meant to sort values by column name but exclude specific rows from sorting and return sorted dataframe with that has rows with preserved positions.
E.g. I have a DataFrame
       Name     marks
rank1   Tom     10
rank2   Jack    30
rank3   nick    50
rank4   juli    5

So I created a function (not finalized, but the idea)
def sort_df_values(df, column_names, ascending=False, fixed_categories=None):
    if fixed_categories is not None and fixed_categories:
        original_positions = {name: position for position, name in enumerate(df.index.values) if name in fixed_categories}
        original_positions = dict(sorted(original_positions.items(), key=operator.itemgetter(1), reverse=True))

        excluded = df.loc[fixed_categories]
        included = [name for name in list(df.index) if name not in fixed_categories]
        new_df = df.loc[included].sort_values(column_names, ascending=ascending)
        result = pd.concat([new_df, excluded])

        new_index_values = list(result.index.values)

        while original_positions:
            val, old_position = original_positions.popitem()
            print(val, old_position)
            for current_position, name in enumerate(new_index_values):
                if name == val:
                    new_index_values.insert(old_position, new_index_values.pop(current_position))
                    break
        result = result.reindex(new_index_values)
    else:
        result = df.sort_values(column_names, ascending=ascending)

    return result

And then I call it like this:
sort_df_values(df, ['marks'], fixed_categories=['rank3'])

The result is correct, I sort all data, but I preserve the excluded rows positions:
       Name     marks
rank2   Jack    30
rank1   Tom     10
rank3   nick    50
rank4   juli    5

Is there any better option on how to do this? Maybe there is already some feature in pandas that could help me out on this one?
DataFrame example:
{'Name': {'rank1': 'Tom', 'rank2': 'Jack', 'rank3': 'nick', 'rank4': 'juli'},
 'marks': {'rank1': 10, 'rank2': 30, 'rank3': 50, 'rank4': 5}}

When using pandas leverage on functions in API almost every function is vectorized.
You can break down your problem into categories.

If fixed is None just sort w.r.t given column_names.
Else sort w.r.t given column_names but filter filter fixed indices.
insert fixed at previous positions.

def sort_values(df, column_names, ascending=False, fixed=None):
    if fixed is None:
        return df.sort_values(column_names, ascending=ascending)
    idx = df.index.get_indexer(fixed).tolist()
    s_df = df.sort_values(column_names, ascending=ascending).T
    keep_cols = s_df.columns.difference(fixed, sort=False)
    out = s_df[keep_cols]
    for (loc, name) in zip(idx, fixed):
        out.insert(loc, name, s_df[name])
    return out.T

Note: fixed should be a list.
Example runs:
fixed = ['rank3']
sort_values(df, ['marks'], ascending=False, fixed=fixed)

#        Name marks
# rank2  Jack    30
# rank1   Tom    10
# rank3  nick    50  # rank3 position fixed.
# rank4  juli     5

fixed = ['rank2', 'rank3']
sort_values(df, ['marks'], ascending=True, fixed=fixed) # ascending order.

# sorted in ascending order
#        Name marks
# rank4  juli     5
# rank2  Jack    30 # rank2 position fixed
# rank3  nick    50 # rank3 position fixed
# rank1   Tom    10

Code Review

PEP-8 Violations

E501 - Lines longer than 79 characters. You have many lines with length greater than 79 characters.

# Examples
original_positions = {name: position for position, name in enumerate(df.index.values) if name in fixed_categories}
# Can be refactored as below
original_positions = {
    name: position
    for position, name in enumerate(df.index.values)
    if name in fixed_categories
}

original_positions = dict(sorted(original_positions.items(), key=operator.itemgetter(1), reverse=True))
# Can be refactored as below
original_positions = dict(
    sorted(
        original_positions.items(), key=operator.itemgetter(1), reverse=True
    )
)

You can use pep8online* to check if your code is in accordance with PEP-8 standard.
You can use black* (code formatting tool)
black --line-length 79 your_file.py

* I'm not affiliated to pep8online and black.


Code review - parse a query to arrays I have a query like this "abc=1, def=2, ghi=3" and I need to parse it into arrays like this keys = [abc, def, ghi] and values = [1,2,3]

currently my code is like this

String[] terms = query.split(",");
int termsCount = terms.length;
String[] keys = new String[termsCount];
String[] values = new Object[termsCount];

for(int i=0; i<termsCount; i++)
{
    if(terms[i].contains("="))
    {
        keys[i] = terms[i].split("=")[0];
        values[i] = terms[i].split("=")[1];
    }
}

sometimes, the query might be empty or may not be well formed - I need to take care of that scenarios too.

Am I doing this right? Is there a better way to do this?

The code as it is, is fine. The biggest point is that you should perform the .split("=") only once:

String[] parts = terms[i].split("=");
keys[i] = parts[0];
values[i] = parts[1];

However there is a more general point: Why are you using arrays?

It's very unusual to use arrays at all in Java, especially in this case where you say yourself the input my be not well formed, possibly leaving you with gaps in the array, which you'll need to work around later. You should at least use Lists. Or even more fitting would be a Map (if the "keys" are unique):

String[] terms = query.split(",");
Map<String, String> map = new HashMap<String, String>(terms.length);

for (String term in terms)
{
    if(term.contains("="))
    {
        String[] parts = term.split("=");
        map.add(parts[0], parts[1]);
    }
}


Creating a wrapper function for jQuery Ajax I am creating a few functions that wrap around jQuery's Ajax function to make it a bit quicker and easier to perform a request. For this I have this JavaScript file:

$( document ).ready( function ()
{
    var ajax = {
        url: "", type: "", success: "", beforeSend: "",
        preform: function ( $url, $type, $success, $beforeSend )
        {
            $.ajax( {
                url: this.url = $url,
                type: this.type = $type,
                success: setSuccess(this.success = $success),
                beforeSend: setBeforeSend(this.beforeSend = $beforeSend)
            } );
        }
    };

    function setSuccess( $selector )
    {
        return function ( $r )
        {
            $( $selector ).html( $r );
        };
    }

    function setBeforeSend( $selector )
    {
        return function ()
        {
            $( $selector ).html( "Loading..." );
        };
    }
} );

And this is used with the following line of code:

ajax.preform( "script/javascript/ajaxfiles/print.php", "GET", "#success", "#before" );

This is working brilliantly, but is there a better way to do this?

I heard you like wrappers
So I wrapped your wrapper (jQuery) in a wrapper (your thing) so you can do ajax.
Why do you do this again??
Instead of wrapping over (slow) jQuery you could just wrap the native ajax functionality. In general I expect that to be faster. Additionally this frees your API to be changed independently of jQuery and it removes that nasty jQuery dependency at all.
Don't wrap wrappers in wrappers, unless you add significant functionality
What significant improvement does your API provide over jQuery's?


Install Python script requirements Sometimes I download a Python script (from a trusted source), and can't run it because of missing dependencies, which I have to install one by one (no requirements.txt is provided). I'm looking for a way to automate this process. It's similar to this question, but I'd like to infer the requirements automatically. I.e. given the script, install  all the packages it's importing.

If myscript.py looks like:

import pandas
import tensorflow as tf
from keras.models import Sequential

Then after running the script (e.g. bash extract_requirements.sh myscript.py) pandas, tensorflow, and keras will be installed.

Of course, a script can call another script, which has its own dependencies, package names don't always match to import names etc. So it's not a perfect solution, but better than nothing.

What I'm using now is a command-line script:

#!/env bash

set -u

usage() {
    echo "$0" '[-d] <MY_SCRIPT>'
    cat <<-EOF
        options:
        -d    Dry-run. Print package names without installing.
EOF
    exit 1;
}

(( $# == 0 )) && usage
dry=0
if [[ "$1" == '-d' ]]; then
    dry=1
    shift
fi
(( $# != 1 )) && usage

script="$1"
imports1=$(egrep '^\s*from\s+[a-zA-Z_.-]+\simport' "$script" | cut -f2 -d' ' | cut -f1 -d'.')
imports2=$(egrep '^\s*import\s+[a-zA-Z_-]+' "$script" | cut -f2 -d' ')
all=$(printf "$imports1\n$imports2" | sort -u)
if (( dry )); then
    echo "$all" | xargs -L 1 echo
else
    echo "$all" | xargs -L 1 sudo pip install
fi

set +u

I was wondering if I'm missing some import cases, and more importantly, whether there's a simpler, more python-ic, solution.

!!! Use this at your own risk. Fiddling with user's packages is scary business !!!

You can monkey-patch the builtin import statement and upon encountering ModuleNotFoundError install whatever dependency is missing. Here is a small example for a script.py. You can of course extend this to feature multiple files or dynamically use the file given as input.

import builtins
import pip
old_import = __import__

def my_import(name, globals=None, locals=None, fromlist=(), level=0):
    if globals["__name__"] == "script":
        try:
            return_args = old_import(name, globals=globals, locals=locals,
                                    fromlist=fromlist, level=level)
        except ModuleNotFoundError:
            builtins.__import__, temp = old_import, builtins.__import__
            pipcode = pip.main(['install', name])
            if pipcode != 0:
                builtins.__import__ = temp
                raise
            else:
                return_args = old_import(name, globals=globals, locals=locals,
                                    fromlist=fromlist, level=level)
                builtins.__import__ = temp
    else:
        return_args = old_import(name, globals=globals, locals=locals,
                                fromlist=fromlist, level=level)

    return return_args

builtins.__import__ = my_import

import script

and the script.py is your custom script:

import os
from time import time

import argh  # this should be installed

print(time())

Note: People sometimes do things like:

try:
    import optional_module
except ImportError:
    pass  # optional all cool

Above code patches import itself. If you remove the if that limits install powers to script you allow recursive installation for everything that is imported. Ever. Even if it is an optional dependency for a 4th level dependency test case of script pip will try to install it.


Euler Problem 7 (finding the 10001st prime) in Powershell I am having issue making this code more efficient. The problem to be solved is as follows:

  By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can
  see that the 6th prime is 13.

  What is the 10,001st prime number?

I'm looking to complete this in powershell. My code runs well up until about 4.8k primes.

$incNum1 = 1
$incNum2 = 2
$divisNum = 2 * $incNum2 - 1
$highestNum = 0
$k = 1
$nextNum = 2 * $incNum1 + 1

while($k -lt 6000){

    $upTo = [int][Math]::Ceiling(($nextNum / 2))

    $break = $false

    while($divisNum -lt $upTo){
        $modRes = $nextNum % $divisNum

        if($modRes -eq 0){

            $break = $true

            break
        }

        $incNum2++
        $divisNum = 2 * $incNum2 - 1
    }

    if(!$break){

        $highestNum = $nextNum

        echo $nextNum

        $k++
    }

    $incNum2 = 2
    $divisNum = 2 * $incNum2 - 1

    $incNum1++

    $nextNum = 2 * $incNum1 + 1
}

echo $highestNum

I'd start with

removing redundancy
measuring the time automatically
output to screen slows down, comment it out

depending on the speed of the computer used I get results of ~74..120 seconds.
Compared with 0.450 secs here with matlalb there IS potential to get better ;-)

## Q:\Test\2018\11\08\CR_207250.ps1

$StartTime = get-date
$PrimeNo = 10001

$incNum1 = 0
$highestNum = 0
$k = 1

while($k -lt $PrimeNo){
    $incNum2 = 2
    $divisNum = 2 * $incNum2 - 1
    $incNum1++
    $nextNum = 2 * $incNum1 + 1

    $upTo = [Math]::Ceiling(($nextNum / 2))
    $break = $false
    while($divisNum -lt $upTo){
        $modRes = $nextNum % $divisNum

        if($modRes -eq 0){
            $break = $true
            break
        }
        $incNum2++
        $divisNum = 2 * $incNum2 - 1
    }
    if(!$break){
        $highestNum = $nextNum
#        "{0}:{1}" -f $k,$nextNum
        $k++
    }
}

"## PrimeNo: {0} is {1} calculated in {2} seconds" -f $PrimeNo,$highestNum,
    ((Get-Date)-$StartTime).TotalSeconds

## PrimeNo: 10001 is 104743 calculated in 73,6911884 seconds
## PrimeNo: 10001 is 104743 calculated in 118,3481173 seconds


Optimize spreadsheet-filling routine I'm stuck with the slow VBA routine below, which I think is the cause of lagging (taking to long). I was wondering if there is a faster way to do this:

With Sheets("Groups")
    .Cells(1, 1).Value = "Groups"
    .Cells(1, 2).Value = "Aantal"
    .Cells(2, 1).Value = "9N4"
    .Cells(2, 2).Value = n9N4
    .Cells(3, 1).Value = "1A2A"
    .Cells(3, 2).Value = n1A2A
    .Cells(4, 1).Value = "1A2B"
    .Cells(4, 2).Value = n1A2B
    .Cells(5, 1).Value = "2A2"
    .Cells(5, 2).Value = n2A2
    .Cells(6, 1).Value = "2A3"
    .Cells(6, 2).Value = n2A3
    .Cells(7, 1).Value = "2A4"
    .Cells(7, 2).Value = n2A4
    .Cells(8, 1).Value = "3A3"
    .Cells(8, 2).Value = n3A3
    .Cells(9, 1).Value = "3A4"
    .Cells(9, 2).Value = n3A4
    .Cells(10, 1).Value = "4A4"
    .Cells(10, 2).Value = n4A4
    .Cells(11, 1).Value = "1B4A"
    .Cells(11, 2).Value = n1B4A
    .Cells(12, 1).Value = "1B4B"
    .Cells(12, 2).Value = n1B4B
    .Cells(13, 1).Value = "1B4C"
    .Cells(13, 2).Value = n1B4C
    .Cells(14, 1).Value = "1B4D"
    .Cells(14, 2).Value = n1B4D
    .Cells(15, 1).Value = "2G4"
    .Cells(15, 2).Value = n2G4
    .Cells(16, 1).Value = "3G4"
    .Cells(16, 2).Value = n3G4
    .Cells(17, 1).Value = "4G4"
    .Cells(17, 2).Value = n4G4
    .Cells(18, 1).Value = "1E3"
    .Cells(18, 2).Value = n1E3
    .Cells(19, 1).Value = "1E4"
    .Cells(19, 2).Value = n1E4
    .Cells(20, 1).Value = "2E4"
    .Cells(20, 2).Value = n2E4
    .Cells(21, 1).Value = "3E3"
    .Cells(21, 2).Value = n3E3
    .Cells(22, 1).Value = "3E4"
    .Cells(22, 2).Value = n3E4
    .Cells(23, 1).Value = "4E4"
    .Cells(23, 2).Value = n4E4
    resultst = n9N4 + n1A2A + n1A2B + n2A2 + n2A3 + n3A3 + n1B4A + n1B4B + n1B4C + n1B4D + n2G4 + n3G4 + n4G4 + n2A4 + n3A4 + n4A4 + n1E3 + n1E4 + n2E4 + n3E3 + n3E4 + n4E4
    .Cells(24, 1).Value = "result"
    .Cells(24, 2).Value = resultst

'check if the sum is correct
    .Cells(24, 3).Value = "=Sum(b2:b23)"
End With

The labels are written and the values come from the code below.
This is a whole list here are a few:

n2E4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "2E4")
n3E3 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "3E3")
n3E4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "3E4")
n4E4 = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), "4E4")

It's hard to say exactly how this will be implemented without seeing the entire sub/function, but what you need is a Select Case statement and a For loop.

Pseudo code to get you started:

    Dim row as Long
    For row = 1 to 24
        Populate row
    next row

End Sub

Private Sub Populate(ByVal row as Long)
     Dim text as string
     text = GetText(row)
     Dim value as Variant
     value =  GetValue(row, text)

     Sheets("Groups").Cells(row,1) = text
     Sheets("Groups").Cells(row,2) = value
End Sub

Private Function GetText(ByVal row as Long) As String
    Select Case Row
        Case 1: GetText = "Groups"
        Case 2: GetText = "9N4"
        Case 3: GetText = "1A2A"

        'etc

     End Select
End Function

Private Function GetValue(ByVal row as Long, ByVal matchText as String) As Variant
    If row = 1 Then
        GetValue = "Aantal"
    Else
        GetValue = Application.WorksheetFunction.CountIf(Sheets("totallist").Range("G2:G" & rn_totallist), matchText)
    End If
End Function


Two cross-platform implementations of getline in C I created my cross-platform implementations of getline function in C.

It takes different arguments and have different return values than 'original' getline function, but aim is the same. The only argument input_file is file from which the line have to be read.  Return value is the line read from file or NULL if nothing read.

Here is one implementation, using fgets():

static inline char*
getline(FILE* input_file){
    const unsigned int chunk_size=256;
    char* line=malloc(chunk_size*sizeof*line+1);
    if(line==NULL){
        fprintf(stderr,"Fatal: failed to allocate %zu bytes.\n",chunk_size*sizeof*line+1);
        exit(1);
    }
    unsigned int i;
    for(i=0;;++i){
        memset(line+chunk_size*i,0,chunk_size);
        if(fgets(line+chunk_size*i,chunk_size+1,input_file)==NULL)
            break;
        if(line[strlen(line)-1]=='\n')
            break;
        char* tmp=realloc(line,chunk_size*(i+2)*sizeof*line+1);
        if(tmp==NULL){
            fprintf(stderr,"Fatal: failed to allocate %zu bytes.\n",chunk_size*(i+2)*sizeof*line+1);
            exit(1);
        }else
            line=tmp;
    }
    if(strlen(line)==0){
        free(line);
        return NULL;
    }else{
        line[strlen(line)-1]=0;
        return line;
    }
}

Here is my second implementation, using fgetc():

static inline char*
getline(FILE* input_file){
    const unsigned int chunk_size=3;
    char* line=calloc(chunk_size,sizeof*line+1);
    if(line==NULL){
        fprintf(stderr,"Fatal: failed to allocate %zu bytes.\n",chunk_size*sizeof*line+1);
        exit(1);
    }
    char c;
    unsigned int i,j;
    for(i=0,j=1;;++i){
        c=(char)fgetc(input_file);
        if(c==EOF||c=='\n')
            break;
        line[i]=c;
        if(i==chunk_size*j){
            ++j;
            char* tmp=realloc(line,chunk_size*(j+1)*sizeof*line+1);
            if(tmp==NULL){
                fprintf(stderr,"Fatal: failed to allocate %zu bytes.\n",chunk_size*(j+1)*sizeof*line+1);
                exit(1);
            }else{
                line=tmp;
                memset(line+chunk_size*j,0,chunk_size);
            }
        }
    }
    if(strlen(line)==0){
        free(line);
        return NULL;
    }else{
        line[strlen(line)]=0;
        return line;
    }
}

exit(1) is probably a bad idea, so is fprintf to stderr.  We don't know how big the string can be, therefore running out of memory could be valid, and if stderr is redirected to somewhere a client can see. realloc should already set errno to ENOMEM, so you could probably just return NULL, and modify comment that upon returning NULL, caller should check errno. Or maybe you should return empty string if there's no data, and use NULL to indicate error.

change calloc to malloc, as you anyway NULL terminate the string in your fgetc implementation.

Instead of having this code duplicated for error message (which I vote against), and actual calculation: chunk_size*(j+1)*sizeof*line+1, you could create a variable and use it in both places, therefore you know you print exactly what you did, and there wasn't a mistake if you had to change the calculation slightly.

Try this on windows (as it claims to be a cross platform implementation), but from memory fgetc will return '\r', which you'll put right into the line, whereas I'm pretty sure fgets won't return '\r', when the line terminator is "\r\n"; I believe fgets returns "\n", not "\r\n" on windows, even if "\r\n" is in the input stream.

You don't need to assign data to tmp like this: char* tmp=realloc(line,chunk_size*(i+2)*sizeof*line+1); as upon success you always do: line=tmp, and upon failure, line will no longer point to valid memory. So you could just assign to line.

Notice how you're calling malloc/calloc/realloc, I would add a freeline function to your code in order to free any memory allocated by this code.  The caller may not be using the same malloc/calloc/realloc you're using, and their free may not be compatible.

Finally I'm not really sure about this: chunk_size*(j+1)*sizeof*line+1
I think you want: chunk_size*(j+1)*sizeof*char+1, as I think line is of size 4 or maybe even 8?, print it out in the debugger, and see, I think you're allocating a lot more memory then you end up putting into the array.


Clojure code adapted from map-invert I wanted to do the following:

Count the frequencies of words in a text (over 5 letters)
Invert the map of words to frequencies, but group together words that have the same frequency in the inversion.
Sort the inverted map by keys descending order and take the top 25.

Here is the code I came up with. Did I re-invent the wheel with map-invert-preserve-dups?  Is there a more concise way to do anything I did? Am I doing anything unnecessarily (i.e. (~k)?

(defn map-invert-preserve-dups
  [m]
  (reduce
    (fn [m [k v]]
      (if (contains? m v)
        (assoc m v (cons k (get m v)))
        (assoc m v `(~k))))
    {}
    m))

(->> "http://www.weeklyscript.com/Pulp%20Fiction.txt"
  (slurp)
  (re-seq #"\w{5,}")
  (frequencies)
  (map-invert-preserve-dups)
  (sort)
  (reverse)
  (take 25))

Well, the most obvious fix is indeed map-invert-preserving-dups - the whole thing could be more easily written as:

(defn map-invert-preserving-dups [m]
  (apply merge-with into
         (for [[k v] m]
           {v [k]})))

The for expression yields a sequence of maps like [{a [1]} {b [2]} {a [5]}]. Apply calls merge-with into on all of those maps. If you look up the definition of merge-with, you can see that this means basically: "Merge all of these maps together, and if the same key exists twice, with values x and y, then make its value (into x y)".


A Summation Function For Various Type Arbitrary Nested Iterable Implementation in C++ This is the follow-up question for A Summation Function For Arbitrary Nested Vector Implementation in C++. The following code is the improved version based on Zeta's answer. I am trying to enhance this the sum function which can deal with iterable things which has begin() and end() (such as std::array) of various types (such as int, char or unsigned int) number. I am not familiar with the usage of std::enable_if and std::is_arithmetic for checking type constraint in template. If there is any possible improvement, please let me know.
template<class Container, typename = typename Container::value_type>
inline long double Sum(const Container& numbers)
{
    long double sumResult = 0.0;
    for (auto& element : numbers)
    {
        sumResult += Sum(element);
    }
    return sumResult;
}

template<class T,
    std::enable_if_t<std::is_arithmetic<T>::value, int> = 0,
    std::enable_if_t<std::is_arithmetic<T>::value, double> = 0>
T Sum(T inputNumber)
{
    return inputNumber;
}

Some tests for this sum function:
int testNumber = 1;

std::vector<decltype(testNumber)> testVector1;
testVector1.push_back(testNumber);
testVector1.push_back(testNumber);
testVector1.push_back(testNumber);
std::cout << std::to_string(Sum(testVector1)) + "\n";

std::vector<decltype(testVector1)> testVector2;
testVector2.push_back(testVector1);
testVector2.push_back(testVector1);
testVector2.push_back(testVector1);
std::cout << std::to_string(Sum(testVector2)) + "\n";

std::vector<decltype(testVector2)> testVector3;
testVector3.push_back(testVector2);
testVector3.push_back(testVector2);
testVector3.push_back(testVector2);
std::cout << std::to_string(Sum(testVector3)) + "\n";

//  std::array test case
std::array<long double, 90> numberArray;
for (size_t i = 0; i < 90; i++)
{
    numberArray[i] = 1;
}
std::cout << std::to_string(Sum(numberArray)) + "\n";

The summary information:

Which question it is a follow-up to?
A Summation Function For Arbitrary Nested Vector Implementation in C++

What changes has been made in the code since last question?

The previous question focus on single type long double and the goal in this question is trying to deal with various type number.

The previous question focus on std::vector and the other iterable things which has begin() and end() are considered here.

The variable name has been modified in order to make better understanding.

Why a new review is being asked for?
Is it a good idea to set the type constraint like std::enable_if_t<std::is_arithmetic<T>::value, int> = 0, std::enable_if_t<std::is_arithmetic<T>::value, double> = 0>?

Oct 18, 2020 Update

The further implementation with c++-concepts:
https://stackoverflow.com/q/64414869/6667035

Proper constraints

Is it a good idea to set the type constraint like std::enable_if_t<std::is_arithmetic<T>::value, int> = 0, std::enable_if_t<std::is_arithmetic<T>::value, double> = 0>?

It is not a good idea to constrain the value type to the arithmetic ones. It will only match primitive integer and floating point types. There are many more types that you can sum together, either ones from the STL (like std::complex) or custom types.
Also, the constraint on the overload that sums over containers is incorrect. You only restrict it to classes that have a value_type, but that doesn't necessarily mean it is a container. Again, std::complex is an example of a class that has a value_type but isn't a container. Instead, you want to restrict it to something that has the properties of a container that you are actually using: that it has a begin() and end() for example. See this question for some ideas of how to check for a container type.
Return type
If you pass a container to Sum(), the result is always long double. But what if I want to sum integers, or std::complex numbers, or something completely different like std::strings? You want the return type to match the value_type of the inner-most container. Have a look at how std::accumulate() handles this.
Summing non-container types
Normally I'm all for handling corner cases properly, and the case of summing over something that has zero nested containers, but just a single value, can be considered such a corner case. But in this case, I think it will most likely be a programming error if you tried to sum a single value. So consider whether it makes sense to have an overload that handles the sum of a single value.
Redundant calls to std::to_string()
Not related to Sum(), but in your example you are converting the results to a string with std::to_string(), but the immediately writing it to std::cout. This is unnecessary, std::cout already knows how to format a long double.


Filtering data from MYSQL database based on parameters passed with POST request I am working on a PHP MVC project for my portfolio. Its an web app where you can store contacts (email and phone number). I am having trouble with filtering contacts based on filter form that has 3 options https://ibb.co/8dJbSWd

I wrote code that works fine but I am sure there is better way of doing this. Here is the code that I wrote for solving this problem.

public function filterContacts($user_id, $group, $email, $phone){

    $query = 'SELECT * FROM contacts WHERE user_id = :user_id ';

    //Nothing is selected
    if($group == '0' && $email == '' && $phone == ''){

        $this->db->query($query . ' ORDER BY name');
        $this->db->bind(':user_id', $user_id);
        return $this->db->resultSet();

        //Everything is selected
    } else if ($group !== '0' && $email !== '' && $phone !== ''){

        $query .= 'AND contact_group = :group AND email != "" AND phone_number != ""';

        $this->db->query($query . 'ORDER BY name');
        $this->db->bind(':user_id', $user_id);
        $this->db->bind(':group', $group);

        return $this->db->resultSet();

        //Just group
    } else if ($group !== '0' && $email == '' && $phone == ''){

        $query .= 'AND contact_group = :group ';

        $this->db->query($query . 'ORDER BY name');
        $this->db->bind(':user_id', $user_id);
        $this->db->bind(':group', $group);

        return $this->db->resultSet();

        //Just email
    } else if ($group == '0' && $email !== '' && $phone == ''){

        $query .= 'AND email != "" ';

        $this->db->query($query . 'ORDER BY name');
        $this->db->bind(':user_id', $user_id);

        return $this->db->resultSet();

        //Just phone
    }  else if ($group == '0' && $email == '' && $phone !== ''){

        $query .= 'AND phone_number != "" ';

        $this->db->query($query . 'ORDER BY name');
        $this->db->bind(':user_id', $user_id);

        return $this->db->resultSet();

        //Group and email
    } else if($group !== '0' && $email !== '' && $phone == ''){

        $query .= 'AND contact_group = :group AND email != ""';

        $this->db->query($query . 'ORDER BY name');
        $this->db->bind(':user_id', $user_id);
        $this->db->bind(':group', $group);

        return $this->db->resultSet();

        //Group and phone number
    } else if($group !== '0' && $email == '' && $phone !== ''){
        $query .= 'AND contact_group = :group AND phone_number != ""';

        $this->db->query($query . 'ORDER BY name');
        $this->db->bind(':user_id', $user_id);
        $this->db->bind(':group', $group);

        return $this->db->resultSet();

        //Email and phone number
    } else if($group == '0' && $email !== '' && $phone !== ''){

        $query .= 'AND phone_number != "" AND email != ""';

        $this->db->query($query . 'ORDER BY name');
        $this->db->bind(':user_id', $user_id);

        return $this->db->resultSet();
    }

}

As you can see I used a lot of if statements to make different queries in Contact model which is possible because i have only 3 filtering options but I cannot imagine doing this with 10 options.

So I was wondering is there a better way of solving this?

Here is link to my github repository if you want to see rest of the code https://github.com/Smiley-dev/Phonebook-demo

The entire long conditional is definitely worth to be decomposed and consolidated.

The main mechanics of optimization is based on 3 aspects:

determining a common logic/behavior that needs to be unified. In your case such a common fragment is:

$this->db->query($query . 'ORDER BY name');
$this->db->bind(':user_id', $user_id);
return $this->db->resultSet();

considering only populated fields
collecting bindings for particular fields

The final optimized function becomes as below:

public function filterContacts($user_id, $group, $email, $phone){

    $query = 'SELECT * FROM contacts WHERE user_id = :user_id';
    $bindings = [':user_id':=> $user_id];

    if ($group !== '0') {
        $query .= ' AND contact_group = :group';
        $bindings[':group'] = $group;
    }
    if ($email !== '') {
        $query .= ' AND email != ""';
    }
    if ($phone !== '') {
        $query .= ' AND phone_number != ""';
    }

    $this->db->query($query . ' ORDER BY name');
    foreach ($bindings as $k => $v) {
        $this->db->bind($k, $v);
    }

    return $this->db->resultSet();
}

In case if your DB clent $this->db allows to pass an array of bindings you can just  replace the loop with $this->db->bind($bindings); (check your DB client interface)


Basic binary search in python I just started learning to code during quarantine and whilst learning python, I created this program for a binary search. This is really basic so I assume that there is a way easier method. If anyone knows how I can make this way simpler, your help would be appreciated.

list = [1,15,37,53,29,22,31,90,14,6,37,40]
finished = False
target = 37
list.sort()
print(list)
while finished == False:
    n = len(list)
    print(n)
    if target < list[int(n/2)]:
        for items in range(int(n / 2), n):
            list.pop()
        print('Item is too large.')
    elif target == list[int(n/2)]:
        finished = True
        print('The item has been found.')
    else:
        list.reverse()
        for items in range(int(n / 2), n):
            list.pop()
        list.reverse()
        print('Item is too small.')
    print(list)

Avoid using list as the variable name, because it is a
function in Python.
You don't need to manipulate the list, such
as list.pop() and    list.reverse(). It is inefficient. You can
determine the updated search range with index.
When target is assigned a value not within list, there will be IndexError: list index out of range. It means you didn't handle the case well.

Modified code:

search_list = [1,15,37,53,29,22,31,90,14,6,37,40]
target = 37
start = 0
end = len(search_list)-1
search_list.sort()
print('The sorted list:',search_list)
while start<=end:
    n = int((end + start)/2)
    if target < search_list[n]: # the target is small
        end = n-1
    elif target > search_list[n]: # the target is large
        start = n+1
    elif target == search_list[n]: # the target is found
        print('The item {} has been found at the index {} in the sorted list.'.format(target,n))
        break
else:
    print('The item is not found.')


In Java: convert an array of floats to bytes and vice versa I'm just returning to Java after a multi-decade hiatus -- there are some nice new packages, such as java.nio.
I need to convert an array of floats into bytes and vice versa.  The floatToBytes() function feels okay, but I'm pretty certain that bytesToFloats() could be implemented better.  Thoughts?
(P.S.: ignore the public, protected etc -- this code is wrapped inside a class...)
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

    protected static final int BYTES_PER_FLOAT = Float.SIZE / 8;

    public static byte[] floatsToBytes(float[] floats){
        ByteBuffer buffer =
                ByteBuffer.allocate(BYTES_PER_FLOAT * floats.length).
                order(ByteOrder.BIG_ENDIAN);
        for (float f : floats) {
            buffer.putFloat(f);
        }
        return buffer.array();
    }

    protected static float[] bytesToFloats(byte[] bytes) {
        if (bytes.length % BYTES_PER_FLOAT != 0) {
            throw new RuntimeException("Illegal length");
        }
        ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN);
        int n_floats = bytes.length / BYTES_PER_FLOAT;
        float[] floats = new float[n_floats];
        for (int i=0; i<n_floats; i++) {
            floats[i] =  buffer.getFloat(i * BYTES_PER_FLOAT);
        }
        return floats;
    }

I also note that there's a FloatBuffer class -- would that be appropriate here?

Welcome again to Code Review, not my field but from a not so clear documentation it seems that you can rewrite your bytesToFloats method using the FloatBuffer class like below, using the asFloatBuffer method from ByteBuffer class:
ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN);
FloatBuffer fb = buffer.asFloatBuffer();

After you have to back the FloatBuffer buffer allocating a new float array like below:
float[] floats = new float[bytes.length / BYTES_PER_FLOAT];
fb.get(floats);

Then your method can be rewritten like below:
protected static float[] bytesToFloats(byte[] bytes) {

    if (bytes.length % BYTES_PER_FLOAT != 0) {
        throw new RuntimeException("Illegal length");
    }

    ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN);
    FloatBuffer fb = buffer.asFloatBuffer();
    float[] floats = new float[bytes.length / BYTES_PER_FLOAT];

    fb.get(floats);

    return floats;

}

In alternative to the bytes.length / BYTES_PER_FLOAT the fb.limit() method seems returning the same result, moreover it is bound directly to the FloatBuffer fb used.


Reading and storing values from JSON I have a class ConfigFile that reads some values from a JSON using Boost and stores them in variables. Everything is done in the constructor:

ConfigFile::ConfigFile(const std::string& configFileNamePathIn)
{
    PTree tmpPT;
    std::string path;

    /// reading aws config file ///
    json::read_json(configFileNamePathIn, tmpPT);

    // read a string value
    path = "path1";
    m_param1 = tmpPT.get<std::string>(path, "");
    checkEmptyness(m_param1, "param1", path);

    // read a float value
    path = "path2";
    m_param2 = tmpPT.get< float >(path, -1);
    checkNegativity(m_param2, "param2", path);

    // ... much more lines like these two also for int and double
}

Because the constructor is long, I thought of creating 4 functions that return string, int, float or double and that receive as input parameters the string path and the PTree:

std::string readStringValue(const std::string& pathIn, const PTree& tmpPT)
{
  path = "path1";
  return tmpPT.get<std::string>(path, "");
}

In the constructor, I just call the function and verify the result:

// ...
m_param1 = readStringValue("path1", tmpPT);
checkEmptyness(m_param1, "param1", path);
// ...

Do you think this is a good improvement, or shall I keep the initial constructor?

I think your proposed change is an improvement for two reasons.  First, it breaks up one long function into multiple smaller ones which is better for human comprehension.  Second, it also allows for reuse of the functions which may also improve efficiency, reduce code size, etc.
There are, however, a couple of things that might be considered for further improvements.
Exceptions
With a long and complex constructor like this, and especially one which involves I/O, things can go wrong.  Carefully considering what might go wrong and using C++ exceptions has the possibility of making your code more durable.
Named variables vs. object collection
Although you're using a constructor, the code looks very procedural. Instead of populating a number of named variables, consider each of those variables itself to be an object.  You could store the name (e.g. "path1"), type, and any required value checks encapsulated within individual objects, making the code easier to read, understand and maintain.  I'd be inclined to use a static data structure to store and associate those things rather than burying all of those details in code.  A std::map might be appropriate.
Example code
To flesh out that latter suggestion a bit, consider that you might have a string, and an unsigned int as configuration items.  To refer to them, you could use just the variable names, or you could create a map:
std::map<std::string, std::unique_ptr<ConfigItem> > config;

Here ConfigItem would be a base class for all configuration items.  You would specialize that class for each different kind of configuration item, such as a plain string, a string that represents a filename, a floating point number, etc.  Using the items would then use the std::string as a key to look up and retrieve the corresponding object.  The std::unique_ptr is just to make sure that the contained objects are deleted when config is deleted.
Here's an example base class:
class ConfigItem
{
public:
    friend std::ostream &operator<<(std::ostream &out, const ConfigItem &ci) {
        return ci.printTo(out);
    }
    virtual operator int() const { return 0; }
    virtual operator std::string () const { return std::string(); }
    virtual ~ConfigItem() {};
protected:
    virtual std::ostream &printTo(std::ostream &out) const {
        return out;
    }
};

Note that the member functions are all virtual.  Here's a specialization for a string:
class ConfigString : public ConfigItem
{
public:
    ConfigString(std::string value) : mValue(value) {}
    std::ostream &printTo(std::ostream &out) const {
        return out << mValue;
    }
    operator std::string () const { return std::string(mValue); }
private:
    std::string mValue;
};

Here's a specialization for an int:
class ConfigInt : public ConfigItem
{
public:
    ConfigInt(int value = 0) : mValue(value) {}
    std::ostream &printTo(std::ostream &out) const {
        return out << mValue;
    }
    operator int() const { return mValue; }
private:
    int mValue;
};

If you look at those two classes carefully, I'm sure it will be pretty easy to spot that they could have been written as a template instead.
Here's a test driver:
#include <iostream>
#include <iterator>
#include <memory>
#include <string>
#include <map>
#include "ConfigItem.h"

int main()
{
    std::map<std::string, std::unique_ptr<ConfigItem> > config;
    config.emplace("inputFilename",
                   make_unique<ConfigString>("/var/log/messages"));
    config.emplace("maxMessages",
                   make_unique<ConfigInt>(200));

    for (const auto &it : config)
        std::cout << it.first << " = " << *it.second << '\n';

    int count = *config["maxMessages"];
    std::cout << "count = " << count << '\n';

    std::string filename = *config["inputFilename"];
    std::cout << "file name = \"" << filename << "\"\n";
}

Also note that if you're only using C++11 and don't have access to a C++14 compiler (one that contains a definition for make_unique() yet), you can use this:
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args )
{
    return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}

Sorry that's so long, but I wanted to show a clear, complete and correct example.


JavaScript API for REST [update: keeping this around for posterity, but we ultimately went to a pub/sub pattern, using Amplify.js. Amplify isn't strictly necessary to implement pub/sub, but it has some nice features and syntax]

I have a REST API, which naturally you can just use HTTP calls (mainly Ajax) in order to send and receive data. I am building a layer of abstraction on top of the raw REST API for JavaScript Developers. I already use jQuery in the project, so I am using $.ajax(). I feel like I have arrived at a good pattern, but I'm not absolutely certain.

var ta = ta || {};

$.ajaxSetup({
  cache: false
});

ta.api = {
  heartbeat: function(successCallback, errorCallback) {
    var callType = "heartbeat";
    // if no custom handlers are provided, wire up the default callbacks or blank callback methods
    successCallback = ta.utils.setDefaultIfNeeded(successCallback, ta.callbacks[callType].success);
    errorCallback = ta.utils.setDefaultIfNeeded(errorCallback, ta.callbacks[callType].error);

    var url = '/rs/heartbeat';

    $.ajax({
      url: url,
      success: function(data) {
        successCallback(data);
      },
      error: function(xhr) {
        errorCallback(xhr);
      }
    });
  },
  version: function(successCallback, errorCallback) {
    var callType = "version";
    // if no custom handlers are provided, wire up the default callbacks or blank callback methods
    successCallback = ta.utils.setDefaultIfNeeded(successCallback, ta.callbacks[callType].success);
    errorCallback = ta.utils.setDefaultIfNeeded(errorCallback, ta.callbacks[callType].error);

    var url = '/rs/version';

    $.ajax({
      url: url,
      success: function(data) {
        successCallback(data);
      },
      error: function(xhr) {
        errorCallback(xhr);
      }
    });
  }
}

ta.utils = {
  setDefaultIfNeeded: function (providedHandler, defaultHandler) {
    var checkedHandler = providedHandler;
    if (typeof (providedHandler !== "undefined")) {
      if (typeof (providedHandler) !== "function") {
        if (typeof (defaultHandler) === "function") {
          checkedHandler = defaultHandler;
        } else {
          checkedHandler = function () { /* intentionally empty */ };
        }
      }
    } else {
      checkedHandler = function () {  /* intentionally empty */ };
    }
    return checkedHandler;
  }
};

So, the API can accept success and error handlers on a per-call basis. The setDefaultIfNeeded will do a few pieces of logic, but one of those pieces is an optional phase of using handlers from a callback object. So the callback object is optional, but it's a handy place to store and organize callbacks:

ta.callbacks = {
  heartbeat: {
    success: function(data) { console.log(data) },
    error: function(xhr) { console.log(xhr.statusText) }
  },
  version: {
    success: function(data) { console.log(data) },
    error: function(xhr) { console.log(xhr.statusText) }
  }
}

Another addition I plan to make but haven't tested yet is to have the functions return the jqXhr. It's already being made, so why not return it? But I haven't tested that yet so I didn't want to provide sample code that might be broken.

So, there are now a few options for using the API, depending on your needs:

Make calls to ta.api methods and supply success and error handlers as needed. I.e. you would define them in the methods that ultimately call the REST API.
Make void calls to ta.api, but the success and error handlers are supplied in the ta.callbacks object.
You can still continue to make calls to the REST service with whatever other arbitrary methods you choose.
*planned - define a variable against ta.api which becomes a jqXhr. You then have full access to the XHR and can do with it as you please.

I have a suspicion that some of you will recommend deferred/promises API, and I am not against it. But if I'm being honest, I can't visualize a strong way to encapsulate the various moving parts and have them extremely readable for other devs.

Looks to me like there's a lot of duplication here. I.e. boilerplate stuff you'd probably end up copy/pasting (and having to deal with the consequences of that later).

I'd just pare it way down for now: All that really changes is the URL. A really simple version might be:

ta.api = (function ($) {
  // private helper function
  function get(url, success, error) {
    return $.ajax({
      url: url,
      success: success || ta.callbacks[url].success;
      error: error || ta.callbacks[url].error;
    });
  }

  return {
    heartbeat: function (success, error) {
      return get('/rs/heartbeat', success, error);
    },

    version: function (success, error) {
      return get('/rs/version', success, error);
    }
  };
}(jQuery));

There are two assumptions/changes here:

You'd use the url as the property name for default callbacks to avoid having to maintain a separate callType variable, and
It assumes that you either pass a function or a false'y value (e.g. null or just nothing) for the callbacks. I.e. don't pass something truthy that's not a function.

Similarly, it assumes that ta.callbacks[url].* is either undefined or a proper function.

However, there's a potential bug if ta.callbacks[url] itself is undefined. So a safer option would be:

  function get(url, success, error) {
    return $.ajax({
      url: url,
      success: success || (ta.callbacks[url] ? ta.callbacks[url].success : null);
      error: error || (ta.callbacks[url] ? ta.callbacks[url].error : null);
    });
  }

If you want to keep your setDefaultIfNeeded, there are few issues with it. For one it's name is not terribly decriptive: Set a default what? A string? A number? It's very specifically a type-checking function that only looks for functions, but given the name, I might think I could use it for anything. And despite its name, it doesn't actually set anything. It just returns, so it should rather be called get....

Secondly, it can be shortened considerably:

ta.utils = {
  getValidCallback: function (providedHandler, defaultHandler) {
    if(jQuery.isFunction(providedHandler)) return providedHandler;
    if(jQuery.isFunction(defaultHandler)) return defaultHandler;
    return function () {};
  }
};

I honestly don't have a good idea for a name (getValidCallback is ok, but not great), because I'd just stick to the first option of using || and simply not have this function.

As for using promises/deferreds instead, the above code allows for that automatically. The result of $.ajax is itself a promise object, and since this code returns it (your function didn't return anything), you can use it both ways:

ta.api.heartbeat(foo, bar);
// or
ta.api.heartbeat().then(foo, bar);


Filtering a list of citizens I have a filter method that receives a list composed of dictionaries. You must discard dictionaries that have their handle or their citizen_number elements off the filtered result as well as concatenate any lists inside that dictionary. In addition to that, in case an element on that main list is a Noneit should be skipped and all the None dictionary elements should be replaced by a zero. This is the solution that I have come up with to do so

def citizens(citizen_list):
    filtered_list = []
    try:
        for citizen in citizen_list:
            if citizen:
                for item in citizen:
                    if not item:
                        citizen[item] = 0
                if (citizen['handle'] == 0 or citizen['citizen_number'] == 0):
                    continue
                if (citizen['organizations'] is not None):
                    citizen['organizations'] = ', '.join(
                        org['sid'] for org in citizen['organizations'])
                else:
                    citizen['organizations'] = 0
                for item in citizen:
                    if(type(citizen[item]) is list):
                        citizen[item] = ', '.join(citizen[item])
                filtered_list.append(citizen)
        return(filtered_list)
    except Exception:
        print(citizen)
        raise

Note that the list concatenation for the organizationsfield must be done before the general one and in that particular way. I feel this code just looks awfully hacked together, how can I make this better?

I can't speak to the specifics of the problem, since I don't understand exactly what you're trying to do, but the general idea seems to be:

Filter citizen_list based on some critiera
Apply some transformation to each citizen in citizen_list

To make that clearer, we can rewrite the code this way:

def _transform_citizen(citizen):
    # ???

def _is_valid_citizen(citizen):
    return citizen['handle'] and citizen['citizen_number']

def citizens(citizen_list):
    return [
        _transform_citizen(citizen)
        for citizen in citizen_list
        if _is_valid_citizen(citizen)
    ]

This has several benefits. It will reduce multiple layers of indentation. It will give you two functions with focused criteria. And it will make it clear that citizens() is doing just a filter and a transformation on the input list.


What is a better way to get unique array Items based on key in PHP? I'm trying to achieve a set of unique array with my following function.

The Function:

/**
* uniqueAssocArray Removes arrys which have same keys
* @param  Array $array     Array to get unique items from
* @param  String $uniqueKey the unique key
* @return Array            new array with unique items
* @author Junaid Qadir Baloch <shekhanzai.baloch@gmail.com>
*/

function uniqueAssocArray($array, $uniqueKey) {
  if (!is_array($array)) {
    return array();
  }
  $uniqueKeys = array();
  foreach ($array as $key => $item) {
    if (!in_array($item[$uniqueKey], $uniqueKeys)) {
      $uniqueKeys[$item[$uniqueKey]] = $item;
    }
  }
  return $uniqueKeys;
}

Example Array:

$actualArray = array(
    user1 => array(
        'name' => 'User1',
        'age' => '25',
        'lastLogin' => '2013-08-16'
    ),
    user1 => array(
        'name' => 'User1',
        'age' => '25',
        'lastLogin' => '2013-08-10'
    ),
    user2 => array(
        'name' => 'User2',
        'age' => '35',
        'lastLogin' => '2013-08-08'
    ),
    user1 => array(
        'name' => 'User1',
        'age' => '25',
        'lastLogin' => '2013-07-10'
    )
);

I then call the function like so:

$resultArray = uniqueAssocArray($actualArray, 'name');

But...

I wonder is there a better way to do the same ?

Assuming your array is already sorted, you can just overwrite the values every time. Skip the inner if.

function uniqueAssocArray($array, $uniqueKey) {
    if (!is_array($array)) {
        return array();
    }
    $uniqueKeys = array();
    foreach ($array as $key => $item) {
        $groupBy=$item[$uniqueKey];
        if (isset( $uniqueKeys[$groupBy]))
        {
            //compare $item with $uniqueKeys[$groupBy] and decide if you
            //want to use the new item
            $replace= ...
        }
        else
        {
            $replace=true;
        }
        if ($replace) $uniqueKeys[$groupBy] = $item;
    }
    return $uniqueKeys;
}