Can this recursive binary search in C be made more concise? Can someone please let me know if they see glaring issues with this code? I tested a handful of cases and it seems to work but in other threads.  I often see much more concisely written versions of this.  I just want to know if anything I have done is superfluous or just stylistically poor form.

#include <stdio.h>

int value = 5;
int values[] = {2,4,5,12,23,34};
int n = 6;

int re_search(int value, int values[], int n);

int main(void)
{

    re_search(value, values, n);
    return 0;
}

int re_search(int value, int values[], int n)
{
    int first = 0;
    int last = n-1;
    int middle = (first+last)/2;

    while (first <= last)
    {
        if (value == values[middle])
        {
            printf("Found it!\n");
            return 0;
        }
        else if (value > values[middle])
        {
          first = middle + 1;
          middle = (first + last) / 2;
          return re_search(value, &values[middle], last-first);
        }
        else
        {
            last = middle - 1;
            return re_search(value, &values[middle], last-first);
        }
        middle = (first + last) / 2;

    }
    printf("Did not find it\n");
    return 0;
}

Things you did well

Declaring your parameters as void when you don't take in any arguments.
Keeping your dependencies to a minimum.

Things you could improve

Bugs:

You sorted your array for your program.

int values[] = {2,4,5,12,23,34};

Your program fails to find your value if your array is not sorted initially.

int values[] = {9,4,3,12,69,34};

Let the computer do the hard work by using qsort().

Efficiency:

Recursion is slowing you down in your re_search() method.  Use iteration instead.  The conversion isn't too hard, there are a lot of similarities.

int binarySearch(int a[], int low, int high, int target)
{
    while (low <= high)
    {
        int middle = low + (high - low)/2;
        if (target < a[middle]) high = middle - 1;
        else if (target > a[middle]) low = middle + 1;
        else return middle;
    }
    return -1;
}

Variables/Initialization:

You shouldn't use global variables.

int value = 5;
int values[] = {2,4,5,12,23,34};
int n = 6;

The problem with global variables is that since every function has access to these, it becomes increasingly hard to figure out which functions actually read and write these variables.

To understand how the application works, you pretty much have to take into account every function which modifies the global state. That can be done, but as the application grows it will get harder to the point of being virtually impossible (or at least a complete waste of time).

If you don't rely on global variables, you can pass state around between different functions as needed. That way you stand a much better chance of understanding what each function does, as you don't need to take the global state into account.

So instead of using global variables, initialize the variables in main(), and pass them as arguments to functions if necessary.
Right now your n variable is hard coded.

int n = 6;

Since this is just the number of elements in your array, we can make this a bit more dynamic, so that you don't have to change it if you add a few more elements to your array.

int n = sizeof(values)/sizeof(values[0]);

Right now you are hard coding the number you are looking for.

int value = 5;

I would either let the user enter it, or determine a random number to search for.

srand((unsigned int) time(NULL)); // seeding
int val = values[rand() % n];

Syntax/Styling:

You print out items to the console within your re_search() method.

printf("Found it!\n");
...
printf("Did not find it\n");

I would have it so your function only evaluates if the number exists in the array and returns whether it could find it or not.  Then let main() do all the printing to the console based on that information.

printf("%s\n", (re_search(value, values, n)) ? "Found it!" : "Didn't find it.");

Final Code

I've rewritten your code so that it is bug free, and so that it is a bit more dynamic.  I also rewrote some bits so you could see what was going on, and so the program was more transparent.

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

int binarySearch(int a[], int low, int high, int target)
{
    while (low <= high)
    {
        int middle = low + (high - low)/2;
        if (target < a[middle]) high = middle - 1;
        else if (target > a[middle]) low = middle + 1;
        else return middle;
    }
    return -1;
}

int compare(const void *a, const void *b)
{
    return (*(int*)a - *(int*)b);
}

void printArray(int arr[], int size)
{
    printf("{ ");
    for (int i = 0; i < size; i++) printf("%d, ", arr[i]);
    puts("}");
}

int main(void)
{
    int values[] = {16, 7, 5, 94, 24, 34};
    int n = sizeof(values)/sizeof(values[0]); // find the number of elements in the array
    qsort(values, n, sizeof(values[0]), compare);
    srand((unsigned int) time(NULL)); // seeding
    int val = values[rand() % n]; // fetch a random value in the array
    int found = binarySearch(values, 0, n - 1, val);

    printArray(values, n); // just so we can see the sorted array
    if (found >= 0) printf("Found %d at index %d\n", val, found);
    else printf("Didn't find %d\n", val);
    return 0;
}


Creating a list of new areas for patches of conserved land within a watershed I'm writing code to create a list of new areas for patches of conserved land within a watershed based on the current size distribution of conserved land patches that are in the watershed now.

I'm generating a new random area based on a normal probability density function fitted to the log-transformed distribution of the sizes of currently conserved land. Then I have a list of the areas of tax parcels in the region, and I want to randomly grab a tax parcel that is within ± 10% of the randomly generated area. The loop will continue until I've generated a total 4,735 hectares of new areas for conservation.

The code works, and runs very quickly if I lower the threshold for total new conserved area to ~500 hectares. However, for my current endpoint (4,735 new hectares), it runs so slowly that I haven't actually ever run it all the way through (waited an hour+). Any ideas on how I can streamline this code to get it to run faster are greatly appreciated!

# Set endpoint of total new area
new_cons_area = 4735.44

# Get new random areas until they total the new_cons_area
sum_rand_area = 0
new_rand_area = []
rand_sizes = []
index_list = []
while sum_rand_area < new_cons_area:
    # Get new random size based on current distribution of sizes of conservation patches
    new_log_size = np.random.normal(param_log[0],param_log[1],1)
    # Transform back to actual area
    new_size = np.e**new_log_size[0]
    # Set buffer boundaries
    min_size = new_size * 0.9
    max_size = new_size * 1.1
    # Get length of new_rand_area list
    current_length = len(new_rand_area)
    # Set up loop to grab sizes from the current list of tax parcels suitable for conservation
    if new_size >= 3.33:
        # Run this loop until a new size is added to the list of new random areas
        while len(new_rand_area) == current_length:
            # Grab a random index from the list of parcel areas
            rand_index = random.randrange(len(parcel_areas)-1)
            # See if the parcel area associated with the random index matches the randomly generated size
            if min_size <= parcel_areas[rand_index] <= max_size:
                # If criteria is met, add the randomly generated size to a list
                rand_sizes.append(int(new_size))
                # Add the randomly generated index to list so you can grab the object ids in the next step
                index_list.append(rand_index)
                # Add the actual tax parcel area to a list
                new_rand_area.append(parcel_areas[rand_index])
                # Add the area to the total area of new land to be conserved
                sum_rand_area += parcel_areas[rand_index]

The problem is that random.randrange(len(parcel_areas)-1) most of the times returns an area which fails the min_size <= parcel_areas[rand_index] <= max_size test. Therefore it is logical to restrict the random selection to the set of areas which satisfy the requirements beforehand, and eliminate the inner loop completely.

Sort the list of areas by size. Once you determined min_size and max_size, find corresponding bounds (with binary search), and select randomly within these bounds.


Debounce counter for PIC micro - Suggestions for improvement I developed this code for a vertical debounce counter for the PIC micro (PIC16F57). The code is called every 512 microseconds. It takes 5 bits from PORTB and compares it to a last known debounced state. If an active input has been stable for 64 calls it is accepted as changed. Though it is sufficiently good for the current application, I am interested in possible improvements to speed up overall execution time. I am not interested in prematurely leaving the loop in case there is nothing to update.
                        cblock              0x08
gpcounter:1                                         ; general purpose counter
debstate:1
delta:1
toggle:1
vcounter:6
                        endc

    ; Button debounce
                            ; A button state is considered stable if it has not changed in 64 polls each 512 usec
        debounce            ; Read input state. Only bits 0-4 are inputs
                            movf            PORTB, W
                            andlw           0x1f
                            ; Any input bits that have changed with respect to the last known state
                            ; will be set to 1's in delta and toggle.
                            ; delta remains fixed, toggle will act as a carry while looping
                            ; Because toggle bits will be reset only, toggle will always be a subset of delta.
                            xorwf           debstate, W
                            movwf           delta
                            movwf           toggle
                            ; Load first counter row in index register
                            movlw           vcounter
                            movwf           FSR
                            ; Counter has 6 rows
                            movlw           0x06
                            movwf           gpcounter
        debounceloop        ; Reset row for all bits which  have not changed states
                            movf            delta, W
                            andwf           INDF, F
                            ; Flip row bits according to toggle mask.
                            ; Because the 1's in toggle are a subset of those in delta,
                            ; any reset counter bits can never be toggled back to 1
                            movf            toggle, W
                            xorwf           INDF, F
                            ; If any bits were toggled from 1 to 0, retain carry bit, else discard it
                            comf            INDF, W
                            andwf           toggle, F
                            ; We're done with this row
                            incf            FSR, F
                            decfsz          gpcounter, F
                            goto            debounceloop
                            ; toggle will now contain 1's for any counters
                            ; that have overflown, so it can be used as xor mask
                            ; to update the last known debounced state
                            movf            toggle, W
                            xorwf           debstate, F
                            retlw           0x00

Device selection
I guess the reason you're on the PIC16F57 is that you're a hobbyist, and this device was the one on hand. I can nearly guarantee that, for a production application, you can find a device from Microchip that is newer, cheaper, smaller and more capable.
One change that most new PICs include is selectable Schmitt-trigger input mode, which will help with your debounce or even eliminate the need for software debounce, depending on your specifications. Your current device has no Schmitt trigger selection register (chapter 6), and only a limited number of special-purpose pins that have Schmitt trigger inputs (chapter 11.3).
Another feature you can take advantage of is software-enabled weak pullup, which - given that you're monitoring a button - is likely to simplify your circuit, which you have not shown. This is commonly available in new PICs.
Yet another feature you could use is integrated pin change detection, commonly available in new PICs - the IOCI module. This can potentially reduce your power consumption, since you wouldn't have to run a polling loop - you could set up a timer, set up IOCI, and then if the timer fires before an IOCI the button event is valid. If an IOCI fires before the timer expires, reset the timer. Some devices can actually go to sleep while this happens and wake up for either interrupt.
Typo
overflown = overflowed
Assembly
I don't recognize the flavour of assembly you're using, perhaps because I'm used to the assembler bundled with contemporary releases of MPLABX. But any self-respecting assembler should be able to:

Accept 6 instead of 0x06, the latter having no need for hexadecimal representation. The same with 0x00.
Include a device-specific header file with symbols for PORTB so that the magic number 0x1F can be replaced by what you're actually doing, which is

andlw PORTA0_MASK |
      PORTA1_MASK |
      PORTA2_MASK |
      PORTA3_MASK |
      PORTA4_MASK;

All (official) PIC environments offer such symbols in their header files, and using them will make your project more maintainable.
General approach
It seems deeply strange to me that you're debouncing five buttons all at once. I would expect, for the current device you have,

Poll for one button press - you can check all pins of PORTA at once using the Z flag
When you hear a button press, assign a mask variable that monitors only that button pin
Poll until either the time expires (you can use TIMER0 and its prescaler to get 32ms) or the button is released
Poll on that pin only, using your mask, until 32 ms are up

For most applications you'll only care to process one button at a time.


Styling and activating radio buttons on condition I have some php code to style a button and activate it, when a specific variable is true.
It's actually a radio button, styled with the bootstrap to a button.
As you can see below, this is how it looks when the variable is '1'.
Also, the buttons at the bottom are the same, but then with one extra option.
The HTML code for this is the following:

                    Navigation bar color:

                <?php

                    if ($navcolor == '1') {
                        $nav_color_i_active  = 'active';
                        $nav_color_i_checked = 'checked';
                        $nav_color_n_active  = '';
                        $nav_color_n_checked = '';
                    } else {
                        $nav_color_i_active  = '';
                        $nav_color_i_checked = '';
                        $nav_color_n_active  = 'active';
                        $nav_color_n_checked = 'checked';
                    }
                ?>

                    <label class="btn btn-primary <?= $nav_color_i_active ?>">
                        <input type="radio" name="navcolor" value="1" <?= $nav_color_i_checked ?>> Inverted

                    <label class="btn btn-primary <?= $nav_color_n_active ?>">
                        <input type="radio" name="navcolor" value="0" <?= $nav_color_n_active ?>> Normal

Navigation bar position:

                    <?php
                        if ($navpos == '2') {
                            $nav_pos_2_active  = 'active';
                            $nav_pos_2_checked = 'checked';
                            $nav_pos_1_active  = '';
                            $nav_pos_1_checked = '';
                            $nav_pos_0_active  = '';
                            $nav_pos_0_checked = '';
                        } elseif ($navpos == '1') {
                            $nav_pos_2_active  = '';
                            $nav_pos_2_checked = '';
                            $nav_pos_1_active  = 'active';
                            $nav_pos_1_checked = 'checked';
                            $nav_pos_0_active  = '';
                            $nav_pos_0_checked = '';
                        } else {
                            $nav_pos_2_active  = '';
                            $nav_pos_2_checked = '';
                            $nav_pos_1_active  = '';
                            $nav_pos_1_checked = '';
                            $nav_pos_0_active  = 'active';
                            $nav_pos_0_checked = 'checked';
                        }
                    ?>

                        <label class="btn btn-primary <?= $nav_pos_2_active ?>">
                            <input type="radio" name="navpos" value="2" <?= $nav_pos_2_checked ?>> Floating

                        <label class="btn btn-primary <?= $nav_pos_1_active ?>">
                            <input type="radio" name="navpos" value="1" <?= $nav_pos_1_active ?>> Sticky to top

                        <label class="btn btn-primary <?= $nav_pos_0_active ?>">
                            <input type="radio" name="navpos" value="0" <?= $nav_pos_0_active ?>> Dynamic, stick to top

As you can see, it's a lot of code for not a lot of buttons. I've used empty variables, because otherwise php would return errors, for unknown variables.
Is it possible that this code can look a lot cleaner, and that there can be less code?

Firstly, you could shorten the names of the variables e.g from $nav_color_i_active to $i_active and so on.
Secondly, there is a problem in your logic. You see else part will run if $navcolorhas any value other than 1. You should use elseif instead of else in both parts of code.


Title case a sentence function I am new to programming. I have written a solution in two different ways, but would like to know what is considered a better solution, and why.

Additionally, in terms of performance, why would one be considered better?

Solution 1:

function titleCase(str) {
  str = str.toLowerCase();
  str = str.split("");
  str[0] = str[0].toUpperCase();
  for(i = 1; i<str.length; i++){
    if(str[i+1] == " "){
      str[i+2] = str[i+2].toUpperCase();
    }
  }
  str = str.join("");
  return str;
}

Solution 2:

function titleCase(str) {
  str = str.toLowerCase();
  str = str.split(" ");
  str = str.map(function(val){
    val = val.charAt(0).toUpperCase() + val.slice(1);
    return val;
  });
  str = str.join(" ");
  return str;
}

A regular expression is really all you need:

/(^| +)([a-zA-Z])/g

Then use the String#replace method and pass in a function:

function toTitleCase(s) {
    return s.toLowerCase().replace(toTitleCase.WORD_PATTERN, function(match, $1, $2) {
        return $1 + $2.toUpperCase();
    });
}

toTitleCase.WORD_PATTERN = /(^| +)([a-zA-Z])/g;

If you really feel like it, jsperf.com can give you a good idea what the performance is.


Collecting numbers in ascending order What kind of algorithm wold solve the following problem from https://www.ohjelmointiputka.net/postit/tehtava.php?tunnus=junar ?

  I have been given a positive integer n. Take the n first positive
  integers 1 to n and permute those to some order. At the beginning you
  can swap the position of two numbers. Each round you go through the
  sequence and your aim is to collect numbers from ascending order. If
  the original permutation is given, determine how many ways one can
  select two numbers from the original sequence to be swapped if one
  wants to collect all number with minimum number of rounds and how many
  rounds it would take to collect numbers in ascending order.

For example if the numbers are at the beginning 3, 1, 5, 4, 2, then you collect at the first round numbers 1 and 2, on the second round 3 and 4 and on the third round 5. So totally three rounds. But you can make a swap to the original sequence to make the following sequences:

3, 4, 5, 1, 2
3, 1, 4, 5, 2
3, 1, 2, 4, 5

and all of them can be collected in two rounds.

But what if n is larger than five, say about 100000? The cases are listed in https://www.ohjelmointiputka.net/tiedostot/junar.zip . Each file contains first the number of numbers and the next lines gives the numbers in some order.

Express the solution of each file as form

a b c

where a is the number of numbers to be collected, b is the smallest number of rounds that takes to collect all numbers if you swap at the beginning one pair of numbers, and c denotes how many ways you can swap at the beginning two numbers so that you can collect numbers in ascending order with minimum number of rounds. So the answer to the example 3, 1, 5, 4, 2 should be 5 2 3.

My brute force solution is as follows. First download the file in the link above to your computer and unzip it.

def swap(A,i,j):
    temp = A[i]
    A[i] = A[j]
    A[j] = temp
    return A

def is_same_round(tables,i,j):
    return tables.index(i) < tables.index(j)

def solve(file):
    tables = [line.strip('\n') for line in open(file)]
    for i in range(len(tables)):
        tables[i]=int(tables[i][0:len(tables[i])-1])
    tables = tables[1:len(tables)]
    roundnumbers = []
    for i in range(len(tables)):
        for j in range(i+1,len(tables)):
            rounds = 1
            tables = swap(tables,i,j)
            for luku in range(1,len(tables)):
                if not is_same_round(tables,luku,luku+1):
                    rounds += 1
            roundnumbers.append(rounds)
            tables = swap(tables,i,j)
    result = ""
    result += str(len(tables))
    result += " "
    result += str(min(roundnumbers))
    result += " "
    result += str(roundnumbers.count(min(roundnumbers)))
    print(result)

for i in range(1,10):
    file = 'junar'+str(i)+'.in'
    solve(file)

Here are a few suggestions:

You should get in the habit of wrapping all code that isn't contained in a function, in a main guard. This will protect the code from being run when the file is imported.
Instead of code like, for example, s = "x" + str(i) + "x", you should use f""in front of your strings so you can directly include variable names into the strings, like so: s = f"x{i}x".
You should include module docstrings for all your functions. This will help any documentation identify what your functions are supposed to do.
Running your code through pylint, here are a few warnings:

Parameters: Having one letter parameters isn't a good practice. Your code lit up when I first pasted it into my editor. Having meaningful names for your parameters also helps remind you what your function is supposed to take in.
Spacing: Any spacing like s=1 or a=b+c should be spread out to s = 1 and a = b + c. This improves the readability of your code greatly.

Below is the refactored code:

def swap(array, index_one, index_two):
    """ Swaps two values in an array, then returns the new array"""
    temp = array[index_one]
    array[index_one] = array[index_two]
    array[index_two] = temp
    return array

def is_same_round(tables, element_one, element_two):
    """
    Returns True if the index of `element_one` is greater than the index
    of `element_two`
    """
    return tables.index(element_one) < tables.index(element_two)

def solve(file):
    """ Solves the file """
    tables = [line.strip('\n') for line in open(file)]
    for i in range(len(tables)):
        tables[i] = int(tables[i][0:len(tables[i]) - 1])
    tables = tables[1:len(tables)]
    roundnumbers = []
    for i in range(len(tables)):
        for j in range(i + 1, len(tables)):
            rounds = 1
            tables = swap(tables, i, j)
            for luku in range(1, len(tables)):
                if not is_same_round(tables, luku, luku + 1):
                    rounds += 1
            roundnumbers.append(rounds)
            tables = swap(tables, i, j)

    result = f"{len(tables)} {min(roundnumbers)} {roundnumbers.count(min(roundnumbers))}"
    print(result)

if __name__ == '__main__':
    for i in range(1, 10):
        file_to_solve = f'junar{i}.in'
        solve(file_to_solve)


"switch" with intervals to snap to a 15-minute mark I am working with dates and times which are not necessarily "on point", which means that while I need the times in fixed quarterly sections, I can't make sure that the saved timestamp isn't a little off.

I need xx:00, xx:15, xx:30 and xx:45 obviously, but the times could be yy:03, yy:14 and alike, you get the hang of it...

Normally I would use a switch statement to select what is about to happen, but in PHP switch does not work with intervals.

Currently, I am stuck with this code:

$temp_minute = date('i', $tdd->datum);

if( ( $temp_minute > 10 ) && ( $temp_minute < 20 ) ) { $minute = '15'; }
elseif( ( $temp_minute > 25 ) && ( $temp_minute < 35 ) ) { $minute = '30'; }
elseif( ( $temp_minute > 40 ) && ( $temp_minute < 50 ) ) { $minute = '45'; }
else { $minute = '00'; }

This seems to be far from elegant but gets the job done.

Are there any "nicer" ways around this, given the case that I might need to add more sections or am I bound to elseif statements?

You can do it 'mathematically' like this:

function fixMinutesToQuarter($minutes) {
  return sprintf('%02d',15*(round($minutes/15) % 4));
}

What does it do?

Divide by 15 so the minutes fall in a range from 0.0 to 3.933.
Rounding so it is either 0, 1, 2, 3 or 4.
Take the 'modulo' so 4 becomes 0. See:
http://php.net/manual/en/language.operators.arithmetic.php
Multiply by 15.
Format the output to be two digits with leading zero.

I tested it with this code:

for ($minutes = 0;$minutes < 60;$minutes++) {
  echo $minutes.' gives '.fixMinutesToQuarter($minutes).'<br>';
}

It returns:

0 gives 00       15 gives 15        30 gives 30       45 gives 45
1 gives 00       16 gives 15        31 gives 30       46 gives 45
2 gives 00       17 gives 15        32 gives 30       47 gives 45
3 gives 00       18 gives 15        33 gives 30       48 gives 45
4 gives 00       19 gives 15        34 gives 30       49 gives 45
5 gives 00       20 gives 15        35 gives 30       50 gives 45
6 gives 00       21 gives 15        36 gives 30       51 gives 45
7 gives 00       22 gives 15        37 gives 30       52 gives 45
8 gives 15       23 gives 30        38 gives 45       53 gives 00
9 gives 15       24 gives 30        39 gives 45       54 gives 00
10 gives 15      25 gives 30        40 gives 45       55 gives 00
11 gives 15      26 gives 30        41 gives 45       56 gives 00
12 gives 15      27 gives 30        42 gives 45       57 gives 00
13 gives 15      28 gives 30        43 gives 45       58 gives 00
14 gives 15      29 gives 30        44 gives 45       59 gives 00

In case you can have negative minutes, or minutes > 59 you should limit the range.

If you still prefer your 'if .... else' conditional approach you could now simplify it to:

if     ($temp_minute > 52) $minute = '00';
elseif ($temp_minute > 37) $minute = '45';
elseif ($temp_minute > 22) $minute = '30';
elseif ($temp_minute > 7)  $minute = '15';
else                       $minute = '00';

This can be further shortened by using the ternary operator, but it will then become quite unreadable, and not any faster, so I wouldn't do that.

Personally I prefer the 'mathematical' approach.


Calculating work using dot product in C++ Write a C++ program fulfilling the following task: The user should enter the 3 dimensional force vector F and a 3 dimensional length vector s. The force is assumed to be constant. The program then calculates the work W = F (dot) s and prints the result on the screen.

#include <iostream>

using namespace std;

int main()
{
    //I decided to try and declare my variables locally to make it easier for readers.
    double F[3];
    int i;

    cout << "Enter in components of the force vector F:\n";

    for(i=0; i<3; i++)
    {
        cout << "Component " << i+1 << ": ";
        cin >> F[i];

    }

    double s[3];

    cout << "Enter in the components of the length vector s:\n";

    for(i=0; i<3; i++)
    {
        cout << "Component " << i+1 << ": ";
        cin >> s[i];
    }

    double W = 0.0;

    for(i=0; i<3; i++)
    {
       W = W + (F[i] * s[i]);
    }

    cout << "The work done is: " << W << " J";

    return 0;
}

Does "The force is assumed to be constant" change anything about the code. I don't see how that makes a difference? Is it included for completeness?

using namespace std should be avoided

Not much more to say than that. Just don't do it. The worst case is putting it into a header. Next is using it outside any namespace, so you've done the second worst possible incarnation of this particular problem.

Typing std::cout instead of cout really isn't going to cause you a lot of problem, I promise.

Don't Repeat Yourself

You have essentially identical code for entering the two input vectors. You probably want to use a function to enter the values for a vector, and just call it once for each input vector.

Use the Library

As a general rule, I'd avoid using built-in arrays when at all reasonable. In this case, you can use std::array instead:

std::array<double, 3> F;
std::array<double, 3> s;

The standard library already provides std::inner_product to do exactly the calculation you're doing. You might as well use it:

double W = std::inner_product(F.begin(), F.end(), s.begin(), 0.0);


Copying from sheet to sheet if column headings match Basically, I have three sheets in my workbook where only the second and third worksheets are being used. I want to copy columns from the second sheet to the third sheet only if the column headings match. For example, the code will check the first row of sheet2 to check if it matches any of strings in an array and if it matches, it will copy the entire column to the third sheet under the same heading.

This code takes too long to process and I would like to make it faster.

Sub Standardization()

Dim myHeaders, e, x, wsImport As Worksheet, wsMain As Worksheet
Dim r As Range, c As Range

myHeaders = Array(
Array("Account_ID", "Account_ID"), Array("Claim_ID", "Claim_ID"), Array("Account_Name", "Account_Name"), _
Array("Claim_Type", "Claim_Type"), Array("Coverage", "Coverage"), Array("Claim_Level", "Claim_Level"), Array("Claim_Count", "Claim_Count"), _
Array("File_Date", "File_Date"), Array("File_Year", "File_Year"), Array("Resolution_Date", "Resolution_Date"), _
Array("Resolution_Year", "Resolution_Year"), Array("Claim_Status", "Claim_Status"), Array("Indemnity_Paid", "Indemnity_Paid"), _
Array("Disease_Category", "Disease_Category"), Array("State_Filed", "State_Filed"), Array("First_Exposure_Date", "First_Exposure_Date"), _
Array("Last_Exposure_Date", "Last_Exposure_Date"), Array("Claimant_Employee", "Claimant_Employee"), Array("Claimant_DOB", "Claimant_DOB"), _
Array("Claimant_Deceased", "Claimant_Deceased"), Array("Claimant_DOD", "Claimant_DOD"), Array("Claimant_Diagnosis_Date", "Claimant_Diagnosis_Date"), _
Array("Product_Type", "Product_Type"), Array("Product_Line", "Product_Line"), Array("Company/Entity/PC", "Company/Entity/PC"), _
Array("Plaintiff_Law_Firm", "Plaintiff_Law_Firm"), Array("Asbestos_Type", "Asbestos_Type"), Array("Evaluation_Date", "Evaluation_Date"), _
Array("Tier", "Tier"), Array("Data_Source", "Data_Source"), Array("Data_Source_Category", "Data_Source_Category"), _
Array("Jurisdiction/County", "Jurisdiction/County"), Array("Settlement_Demand", "Settlement_Demand"), Array("Jury_Verdict", "Jury_Verdict"), _
Array("Exposure_Site", "Exposure_Site"), Array("National_Defendant_Firm", "National_Defendant_Firm"), Array("Local_Defendant_Firm", "Local_Defendant_Firm"), _
Array("Expense_Amount", "Expense_Amount"), Array("NCC_Expense_Amount", "NCC_Expense_Amount"), Array("Non_NCC_Expense_Amount", "Non_NCC_Expense_Amount"))

Set wsImport = ThisWorkbook.Sheets(2)
Set wsMain = ThisWorkbook.Sheets(3)

Application.ScreenUpdating = False

For Each e In myHeaders
Set r = wsImport.Cells.Find(e(0), , , xlWhole)

If Not r Is Nothing Then
    Set c = wsMain.Cells.Find(e(1), , , xlWhole)

    If Not c Is Nothing Then
        wsImport.Range(r.Offset(1), wsImport.Cells(Rows.Count, r.Column).End(xlUp)).Copy _
        wsMain.Cells(Rows.Count, c.Column).End(xlUp)(2)
    Else
        msg = msg & vbLf & e(1) & " " & wsMain.Name
    End If
Else
    msg = msg & vbLf & e(0) & " " & wsImport.Name
End If

Next

Application.ScreenUpdating = False
wsMain.Columns("A:AO").Select
Selection.EntireColumn.AutoFit

Application.ScreenUpdating = False
Selection.ClearFormats

Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)
tbl.TableStyle = "TableStyleMedium9"

Application.ScreenUpdating = True

End Sub

Okay. So, let's start with this gigantic array of arrays you've got there. I don't mean to be rude, but what exactly are you doing here?! Each and every one of the inner arrays simply duplicates itself. For the life of me I can't figure out why you have it duplicated. This would work just as well by adding each of these strings to a collection. I would create a single function that intializes and returns this collection.

Private Function GetHeaders() As Collection
    Dim result As New Collection
    With result
        .Add "Account_ID"
        .Add "Claim_ID"
        .Add "Account_Name"
        ' ...
    End With

    Set GetHeaders = result
End Function

And call it at the beginning of Standardization like this.

Dim headers As Collection
Set headers = GetHeaders

Which turns your loop into this.

Dim header As Variant
For Each header In myHeaders
    Set r = wsImport.Cells.Find(header, , , xlWhole)

    If Not r Is Nothing Then
        Set c = wsMain.Cells.Find(header, , , xlWhole)

        If Not c Is Nothing Then
            wsImport.Range(r.Offset(1), wsImport.Cells(Rows.Count, r.Column).End(xlUp)).Copy _
            wsMain.Cells(Rows.Count, c.Column).End(xlUp)(2)
        Else
            msg = msg & vbLf & header & " " & wsMain.Name
        End If
    Else
        msg = msg & vbLf & header & " " & wsImport.Name
    End If

Next

Note that I added a level of indentation inside of the loop, and replaced the cryptic e variable with the more sensible and descriptive header variable.

Now the duplication becomes so obvious that it's painful, so let's extract a couple more functions.

Private Function FindHeaderRange(ByVal ws As Worksheet, ByVal header As String) As Range
    Set FindHeaderRange = ws.Cells.Find(header, , , xlWhole)
End Function

and

Private Function BuildMessage(ByVal currentMessage As String, ByVal ws As Worksheet, ByVal header As String) As String
    BuildMessage = currentMessage & vbLf & header & Space(1) & ws.Name
End Function

Note that I replaced the literal space with a call to Space(1). This is just for readability. It's much easier to parse the function call than a " ".

Also, you never defined msg in your original code. Turn Option Explicit on so this doesn't happen in the future. It helps turn runtime errors into compile time errors. It's kind of magical. You should always be using it.

Anyway, I took the liberty of renaming some more cryptic variables, and now it looks like this.

Dim header As Variant
Dim source As Range
Dim dest As Range
For Each header In headers
    Set source = FindHeaderRange(wsImport, header)

    If Not source Is Nothing Then
        Set dest = FindHeaderRange(wsMain, header)

        If Not dest Is Nothing Then
            wsImport.Range(source.Offset(1), wsImport.Cells(Rows.Count, source.Column).End(xlUp)).Copy _
                wsMain.Cells(Rows.Count, dest.Column).End(xlUp)(2)
        Else
            msg = BuildMessage(msg, wsMain, header)
        End If
    Else
        msg = BuildMessage(msg, wsImport, header)
    End If

Next

Which is better, but I'd prefer to take a happy path whenever possible. It's easier to reason about positive statements than double negatives.

For Each header In headers

    Set source = FindHeaderRange(wsImport, header)
    If source Is Nothing Then
        msg = BuildMessage(msg, wsImport, header)
    Else

        Set dest = FindHeaderRange(wsMain, header)
        If dest Is Nothing Then
            msg = BuildMessage(msg, wsMain, header)
        Else
            wsImport.Range(source.Offset(1), wsImport.Cells(Rows.Count, source.Column).End(xlUp)).Copy _
                wsMain.Cells(Rows.Count, dest.Column).End(xlUp)(2)
        End If
    End If
Next

You don't ever use the variable x. Get rid of it entirely.
You turn screen updating off three times without ever turning it back on. Once is enough.
Speaking of screen updating, anytime you turn it off, you need an error handler to ensure it always gets turned back on no matter what happens while the code is executing.
Don't activate and select. Use object references/variables instead.

wsMain.Columns("A:AO").Select
Selection.EntireColumn.AutoFit
Selection.ClearFormats

Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)

Should be

   Dim cols As Range
   Set cols = wsMain.Columns("A:AO")
   cols.EntireColumn.AutoFit
   cols.ClearFormats

   Dim tbl As ListObject
   Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, cols, , xlYes)
   tbl.TableStyle = "TableStyleMedium9"

Always be explicit about scope. Procedures defined as just plain Sub are public. It's better to write that down instead of relying on a developer's knowledge (or lack there of).

Here's the resulting code. I apologize that I never got around to performance. There were a number of issues to work through first. If I get time, I'll take a look at the performance later.

Public Sub Standardization()
On Error GoTo ExitSub

    Dim headers As Collection
    Set headers = GetHeaders

    Dim msg As String

    Dim wsImport As Worksheet, wsMain As Worksheet

    Set wsImport = ThisWorkbook.Sheets(2)
    Set wsMain = ThisWorkbook.Sheets(3)

    Application.ScreenUpdating = False

    Dim header As Variant
    Dim source As Range
    Dim dest As Range
    For Each header In headers

        Set source = FindHeaderRange(wsImport, header)
        If source Is Nothing Then
            msg = BuildMessage(msg, wsImport, header)
        Else

            Set dest = FindHeaderRange(wsMain, header)
            If dest Is Nothing Then
                msg = BuildMessage(msg, wsMain, header)
            Else
                wsImport.Range(source.Offset(1), wsImport.Cells(Rows.Count, source.Column).End(xlUp)).Copy _
                    wsMain.Cells(Rows.Count, dest.Column).End(xlUp)(2)
            End If
        End If
    Next

    Dim cols As Range
    Set cols = wsMain.Columns("A:AO")
    cols.EntireColumn.AutoFit
    cols.ClearFormats

    Dim tbl As ListObject
    Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, cols, , xlYes)
    tbl.TableStyle = "TableStyleMedium9"

ExitSub:
    Application.ScreenUpdating = True

End Sub


Temperature conversion table in C++14 I'm learning C++ and thought I would try my hand at writing a basic program in modern C++. I'm always looking for ways to reduce my code to STL algorithms and simplifying areas that would require refactoring. I'm also trying to make my code as readable as possible.

Limitation

I can't modify the precision in std::to_string as far as I can see, which affects the rest of the program.

What I want

Looking for ways to simplify stuff to STL algorithms
Ensure everything is modern C++
Readability. What if I want to modify the code 3 weeks from now?
Anything else?

Code:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

auto fah_to_cel(double in)
{
    return (in - 32.) / 1.8;
}

int main()
{
    auto min = 0u;
    auto max = 100u;
    auto step = 5u;

    std::vector<double> temps;

    /* size_type is not necessarily the same
     * type as 0u
     */
    auto max_width = std::string::size_type(0);

    for (auto i = min; i < max; i += step)
    {
        auto width = std::to_string(i).size();
        max_width = std::max(width, max_width);
        temps.emplace_back(i);
    }

    auto header_width = sizeof("Fahrenheit") - 1;
    max_width = std::max(header_width, max_width);

    std::cout << "Fahrenheit"
              << std::string(5, ' ')
              << "Celsius"
              << "\n";

    std::cout << std::string(max_width, '-')
              << std::string(5, ' ')
              << std::string(max_width, '-')
              << "\n";

    for (auto& temp : temps)
    {
                  /* Fahrenheit column */
        std::cout << std::left  << std::setw(max_width)
                  << std::fixed << temp

                  /* Padding */
                  << std::string(5, ' ' )

                  /* Celsius column*/
                  << std::left  << std::setw(max_width)
                  << std::fixed << fah_to_cel(temp)
                  << "\n";
    }
}

Example output:

Fahrenheit     Celsius
----------     ----------
0.000000       -17.777778
5.000000       -15.000000
10.000000      -12.222222
15.000000      -9.444444
20.000000      -6.666667
25.000000      -3.888889
30.000000      -1.111111
35.000000      1.666667
40.000000      4.444444
45.000000      7.222222
50.000000      10.000000
55.000000      12.777778
60.000000      15.555556
65.000000      18.333333
70.000000      21.111111
75.000000      23.888889
80.000000      26.666667
85.000000      29.444444
90.000000      32.222222
95.000000      35.000000

I see some things that may help you improve your program and have provided answers to your questions.

Be clear about your program's purpose

What the program actually does is to print out a table of temperatures.  That much is quite clear, but what is less clear is whether you really wanted the std::vector to be created or not.  If that was just a means to the end goal of creating a table, then I'd suggest you could omit it entirely and simply use a loop to print the values rather than storing them.

Use named variables

The word "Fahrenheit" appears twice within the code.  It would be better to use named variables rather than risk, say, having a typo in one of the versions.  I'd do it like this:

const std::array<std::string, 2>column_labels{"Fahrenheit", "Celsius"};

Remove unecessary data structures

As noted above, the std::vector is not really needed.  If the code simply directly prints the values, the code becomes shorter and easier to read and understand.

Eliminate redundant I/O manipulators

Once the width and precision have been set, there's little reason to assert them again.  I'd write the main loop like this:

for (double temp=min; temp < max; temp += step) {
    std::cout << std::setw(col_width) << std::fixed
              << std::setprecision(4) << temp
              << fah_to_cel(temp) << '\n';
}

Note that the limitation of std::to_string is eliminated by this means because we can use std::setprecision().

Avoid construction/destruction overhead

Each time the code calls std::string(5, ' ') it represents a constructor call and somewhere later, a destructor call.  This can be avoided either by making it a named const variable which can be repeatedly used, or better still, eliminated entirely by simply using the std::setw() manipulator to set the columns to the size you want, automatically creating the required space.

Let the computer do the work

It seems to make more sense to me to let the computer figure out which label is longest.  Here's one simple way to do that:

const auto max_width = std::accumulate(
        column_labels.begin(), column_labels.end(), 0,
        [](std::string::size_type a, const std::string &str){
            return std::max(a, str.size());
        });

This uses a lambda to return the longest string length, but this could be done using a function if you wish.

Use const where practical

Things that don't change should be declared const which includes your min, max and many other variables within the code.

Consider whether to use a function or inline

The fah_to_cel routine is quite simple and only used once, so I'd be inclined to eliminate it in favor of simply putting the code inline instead.

An alternative implementation

Using these suggestions, here's an alternative approach:

#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>

int main()
{
    const auto min = 0u;
    const auto max = 100u;
    const auto step = 5u;
    const std::array<std::string, 2> column_labels{"Fahrenheit", "Celsius"};
    const auto max_width = std::accumulate(
            column_labels.begin(), column_labels.end(), 0,
            [](std::string::size_type a, const std::string &str){
                return std::max(a, str.size());
            });
    const auto col_width = max_width+5;
    std::cout.setf(std::ios_base::left, std::ios_base::adjustfield);
    // print column labels
    for (const auto &label : column_labels) {
        std::cout << std::setw(col_width) << label;
    }
    std::cout << '\n';
    // print column label underlines
    const std::string underline(max_width, '-');
    for (auto i=column_labels.size(); i; --i) {
        std::cout << std::setw(col_width) << underline;
    }
    std::cout << '\n';
    // print temperature table
    for (double temp=min; temp < max; temp += step) {
        std::cout << std::setw(col_width)
                  << std::fixed << std::setprecision(4)
                  << temp << (temp-32)/1.8 << '\n';
    }
}


Scraping names of directors from a website I am scraping the names of the directors from a website using Python / ScraPy.  I am very new to coding (under a year and after work) - any views would be appreciated.

The reason I have a for loop with count from 0 to 100 is that not all the names on the website have a date of birth, and hence where there are blanks I need to return a value ("n/a" in this case) otherwise the lists of names / namerefs / roles / dateofbirths will get out of order.

import scrapy
import re

from CompaniesHouse.items import CompanieshouseItem

class CompaniesHouseSpider(scrapy.Spider):
    name = "companieshouse"
    allowed_domains = ["companieshouse.gov.uk"]
    start_urls = ["https://beta.companieshouse.gov.uk/company/OC361003/officers",
]

    def parse(self, response):
        for count in range(0,100):
            for sel in response.xpath('//*[@id="content-container"]'):
                companys = sel.xpath('//*[@id="company-name"]/text()').extract()
                companys = [company.strip() for company in companys]
                string1 = "officer-name-" + str(count)
                names = sel.xpath('//*[@id="%s"]/a/text()' %string1).extract()
                names = [name.strip() for name in names]
                namerefs = sel.xpath('//*[@id="%s"]/a/@href' %string1).re(r'(?<=/officers/).*?(?=/appointments)')
                namerefs = [nameref.strip() for nameref in namerefs]
                string2 = "officer-role-" + str(count)
                roles = sel.xpath('//*[@id="%s"]/text()' %string2).extract()
                roles = [role.strip() for role in roles]
                string3 = "officer-date-of-birth-" + str(count)
                if sel.xpath('//*[@id="%s"]/text()' %string3):
                    dateofbirths = sel.xpath('//*[@id="%s"]/text()' %string3).extract()
                else:
                    dateofbirths = ["n/a"]
                dateofbirths = [dateofbirth.strip() for dateofbirth in dateofbirths]
                result = zip(companys, names, namerefs, roles, dateofbirths)
                for company, name, nameref, role, dateofbirth in result:
                   item = CompanieshouseItem()
                   item['company'] = company
                   item['name'] = name
                   item['nameref'] = "'" + nameref
                   item['role'] = role
                   item['dateofbirth'] = dateofbirth
                   yield item

        next_page = response.xpath('//*[@class="pager"]/li/a[@class="page"][contains(., "Next")]/@href').extract()
        if next_page:
            next_href = next_page[0]
            next_page_url = "https://beta.companieshouse.gov.uk" + next_href
            request = scrapy.Request(url=next_page_url)
            yield request

DRY

Reduce duplicated logic using helper functions.
Currently you have 2 lines of code for each field you extract,
for example:

companys = sel.xpath('//*[@id="company-name"]/text()').extract()
companys = [company.strip() for company in companys]

names = sel.xpath('//*[@id="%s"]/a/text()' % string1).extract()
names = [name.strip() for name in names]

This is tedious. You could capture the common logic in a helper function, for example:

def to_list(xpath):
    return [v.strip() for v in xpath.extract()]

With that, much of the code can be simplified:

companys = to_list(sel.xpath('//*[@id="company-name"]/text()'))
names = to_list(sel.xpath('//*[@id="%s"]/a/text()' % string1).extract())

Repeated operations

Here, an xpath lookup is performed twice:

if sel.xpath('//*[@id="%s"]/text()' % string3):
    dateofbirths = sel.xpath('//*[@id="%s"]/text()' % string3).extract()
else:
    dateofbirths = ["n/a"]
dateofbirths = [dateofbirth.strip() for dateofbirth in dateofbirths]

It would be better to avoid that:

dateofbirths = to_list(sel.xpath('//*[@id="%s"]/text()' % string3))
if not dateofbirths:
    dateofbirths = ["n/a"]

Use "...".format(...)

The "%s" % ... style formatting is old, it's recommended to use the format function instead, for example:

names = sel.xpath('//*[@id="{}"]/a/text()'.format(string1)).extract()

Formatting

Python has a style guide called PEP8, I suggest to follow it.


Merge Sort - Using Ruby and Recursion I am implementing the algorithms of Course: Coursera Algorithm I.
I implemented the Merge-Sort using recursion and although it sorts correctly I am not sure if it is a correct implementation of this algorithm. Could you guys take a look? How can I keep it more "readable"?

##
# Implemetation of Merge Sort see: {https://pt.wikipedia.org/wiki/Merge_sort}
module Merge
  class Sort
    include Validator, Comparator

    def sort(values)
      return values if invalid_to_sort?(values)
      _sort(values, 0, values.size - 1)
    end

    private

    def _sort(values, ibegin, iend)
      return [values[ibegin]] if ibegin == iend

      mid = ((iend - ibegin) / 2) + ibegin

      left  = _sort(values, ibegin, mid)
      right = _sort(values, mid + 1 , iend)

      merge([], left, right)
    end

    def merge(values, left, right)
      # p "values #{values} left #{left} right #{right}"
      return values if left.empty? and right.empty?

      if left.empty?
        merge(values + [right.shift], left, right)

      elsif right.empty? || (left.first <=> right.first) <= 0
        merge(values + [left.shift], left, right)

      else
        merge(values + [right.shift], left, right)
      end
    end
  end
end

The entire implementation

Yes, both methods seem to work correctly, your code definately implements
mergesort :).

Your code, however, seems like somewhat direct translation of C implementation, as it hardly
uses Ruby idioms, what hurts readability.
Ruby Arrays know they size (#length, #empty? etc.), so you don't need to keep track of it.
So, splitting the array could very well be done like:

# _sort now only accepts one argument, values
mid = values.length / 2
left = _sort(values[0...mid])

Such approach would of course require you to rewrite #merge, but it would
be worth it, as it is more readable to work on actual arrays than criptic numbers. This would also probably eliminate need for Validator module to exist (and Comparator doesn't seem to be used anyways). You should always aim at using idiomatic Ruby, because reader of your code seeing something C-like will waste much time thinking "why didn't <insert idiom> suffice here?" to analyze your code.

If someone passes wierd value to your #sort (like, a Fixnum), it will raise cryptic errors
like NoMethodError: undefined method 'empty?'. You should handle such errors by rescueing them
and raising more descriptive ones (in this case, ArgumentError probably) so they are easier
to analyze. #merge should be handled separately, as NoMethodError there likely means
something else.

def invalid_to_sort?(values)
  # I don't think nil deserves special treatment here though, it should
  #   raise ArgumentError like all non-arrays. This seems to come from
  #   C where arrays are pointers and NULL is pointer too, so they are
  #   hard to distinguish without direct check
  values.nil? || values.empty? || values.one?
rescue NoMethodError
  raise ArgumentError, "some descriptive message"
end

Your current code structure introduces somewhat odd (non-Ruby) interface.

sorter = Merge::Sort.new
sorted = sorter.sort(array)

Following example of standard library, Rubyish thing to do would be something like:

array.merge_sort! # destructive
# and
sorted = array.merge_sort # safe

This is easily achieved by making safe variant work on a duplicate:

class Array
  def merge_sort!
    # ... code code code ...
  end

  def merge_sort
    dup.merge_sort!
  end
end

Of course, patching standard classes is dangerous, but as various examples (most notably Rails)
prove, also extremely usefull. If you are concerned, you could use refinements
, but those are somewhat experimental. However, as you are adding new functionality (and not replacing existing one)
only real danger is conflicting with some other patch, so I'd say go for it.

In any case, there is no need to introduce a class, more so one that you need to instantiate.
If you want external sorting method (to avoid patching Array), just make it part of a module
to keep things simple, i.e:

module Sort
  def self.merge_sort(values)
    # ... code code code ...
  end
end

sorted = Sort.merge_sort(array)

Now I know from your previous question that you come from Java, so you like to instantiate alot ;)
but in Ruby we work somewhat differently, here a module/class is proper object that works like anything else,
not some sort ofmetadata-thingy, so it's easy to use them with dependency injection and what-not,
usually there is no need to create instances just to call some code :)


Translating code numbers to item descriptions in PostgreSQL query For each itemId I need to select a value in my PostgreSQL query. I am doing it like this:

select itemId,
 CASE
-- Revenue
    WHEN itemId in (339,28,29)
    THEN 'Revenues'
    -- Rev CHG calculated on the back-end
--Expenditures
    WHEN itemId = 102
    THEN 'SG'||CHR(38)||'A'
    -- SGA CHG calculated on the back-end
    WHEN itemId = 4375
    THEN 'SG'||CHR(38)||'A MARGIN'
    WHEN itemId = 100
    THEN 'R'||CHR(38)||'D EXPENSE'
    -- RD CHG calculated on the back-end
    WHEN itemId  in (2021, 2114)
    THEN 'CAPEX'
    --CAPEX CHG calculated on the back-end
    WHEN itemId = 34
    THEN 'COGS'
--Income
    WHEN itemId = 379
    THEN 'Net Income'
    WHEN itemId = 7
    THEN 'Earnings from Cont. Ops'
    WHEN itemId = 83
    THEN 'Minority Interest'
    WHEN itemId = 280
    THEN 'Preferred Stock Dividend'
    -- NI CHG calculated on the back-end
    WHEN itemId = 400
    THEN 'EBIT'
    WHEN itemId = 4197
    THEN 'EBIT CHG 1 YR'
    WHEN itemId = 4210
    THEN 'EBIT CHG 2 YR'
    WHEN itemId = 4223
    THEN 'EBIT CHG 3 YR'
    WHEN itemId = 4236
    THEN 'EBIT CHG 5 YR'
    WHEN itemId = 4249
    THEN 'EBIT CHG 7 YR'
    WHEN itemId = 4262
    THEN 'EBIT CHG 10 YR'
    WHEN itemId = 4051
    THEN 'EBITDA'
    WHEN itemId = 4196
    THEN 'EBITDA CHG 1 YR'
    WHEN itemId = 4209
    THEN 'EBITDA CHG 2 YR'
    WHEN itemId = 4222
    THEN 'EBITDA CHG 3 YR'
    WHEN itemId = 4235
    THEN 'EBITDA CHG 5 YR'
    WHEN itemId = 4248
    THEN 'EBITDA CHG 7 YR'
    WHEN itemId = 4261
    THEN 'EBITDA CHG 10 YR'
    WHEN itemId = 4047
    THEN 'EBITDA MARGIN'
    WHEN itemId = 3064
    THEN 'EPS-BASIC'
    --EPS-BASIC CHG calculated on the back-end
    WHEN itemId = 142
    THEN 'EPS-DIL'
    --EPS-DIL CHG calculated on the back-end
    WHEN itemId = 10
    THEN 'GROSS PROFIT'
    WHEN itemId = 4195
    THEN 'GP CHG 1 YR'
    WHEN itemId = 4208
    THEN 'GP CHG 2 YR'
    WHEN itemId = 4221
    THEN 'GP CHG 3 YR'
    WHEN itemId = 4234
    THEN 'GP CHG 5 YR'
    WHEN itemId = 4247
    THEN 'GP CHG 7 YR'
    WHEN itemId = 4260
    THEN 'GP CHG 10 YR'
    WHEN itemId = 4074
    THEN 'GP MARGIN'
    --GP MARGIN CHG calculated on the back-end
    WHEN itemId in (21,5,356,22)
    THEN 'OP PROFIT'
    WHEN itemId = 373
    THEN 'Total Operating Exp'
    --OP PROFIT CHG calculated on the back-end
--Cash Flow
    WHEN itemId = 2207
    THEN 'Cash Flow'
    --CF CHG calculated on the back-end
    WHEN itemId = 4423
    THEN 'UNLEVERED FREE CASH FLOW'
    WHEN itemId = 4430
    THEN 'UCF CHG 1 YR'
    WHEN itemId = 4431
    THEN 'UCF CHG 2 YR'
    WHEN itemId = 4432
    THEN 'UCF CHG 3 YR'
    WHEN itemId = 4433
    THEN 'UCF CHG 5 YR'
    WHEN itemId = 4434
    THEN 'UCF CHG 7 YR'
    WHEN itemId = 4435
    THEN 'UCF CHG 10 YR'
    WHEN itemId = 2006
    THEN 'CASH OPS'
    WHEN itemId = 2081
    THEN 'Net Cash From Discont. Ops'
    --Cash Ops CHG calculated on the back-end
--Return
    WHEN itemId = 4178
    THEN 'ROA'
    WHEN itemId = 4363
    THEN 'ROC'
    WHEN itemId = 4128
    THEN 'ROE'
--Working Capital
    WHEN itemId = 4030
    THEN 'CURRENT RATIO'
    WHEN itemId = 4121
    THEN 'QUICK RATIO'
    WHEN itemId = 4177
    THEN 'ASSET TURNOVER'
    WHEN itemId = 4082
    THEN 'INVENTORY TURNOVER'
  END itemname in
  from item i
  join itemcode ic on i.itemcode = ic.itemcode
  WHERE ic.dataItemId IN (.................);

Can I do it in a better way?

You should put all the mapping between itemId values and whatever constant strings in a separate table (2 columns) and do a join with it.
This allows you to update the mapping (by changing the table content) without even changing the query later on.
(and this is not specific to PostgreSQL, will work with any RDBMS)


CRC function duplication – differ only in pointer vs iterator signature This is a CRC function generated by pyCRC and changed by me:

static inline crc_t crc_update(crc_t          crc,
                               char const *   data,
                               long long      data_len)
{
    while (data_len--)
    {
        crc = (crc_table[(crc ^ *data) & 0xff] ^ (crc >> 8));
        ++data;
    }
    return crc;
}

I made an iterator version of it:

static inline crc_t crc_update(crc_t                          crc,
                               std::istreambuf_iterator<char> data,
                               long long                      data_len)
{
   /* body identical to the above */
}

Is there a way I can avoid duplication? (The main purpose of the code is to be fast.)

The obvious way is to replace the type that changes with a templated type, like this:

template<typename T>
static inline crc_t crc_update(crc_t          crc,
                               T              data,
                               long long      data_len)
{
    while (data_len--)
    {
        crc = (crc_table[(crc ^ *data) & 0xff] ^ (crc >> 8));
        ++data;
    }
    return crc;
}

Or in words: put template<typename T> in front of the function, and replace all char const * with T.

There are some details about inserting typename in some cases to help the compiler, but those do not apply in this case.

To prevent incorrect usage, one might add some checks to ensure that only 8 bit types can be used. However, that is advanced stuff and not required to answer your question.


Append a string to each line in a file I wrote (several months ago) a program that will take an input file, with a provided std::string, and append that to each individual line in the file. Looking at the code again, I am wondering about the performance of this program, possibly if the file is very large or any of the lines/input string is long.

#include <fstream>
#include <iostream>
#include <vector>

// argv[1] is input file name
// argv[2] is output file name
// argv[3] is string to add
int main(int argc, const char * argv[])
{
    if (argc != 4) {
        std::cout << "Needs input file and string to add" << std::endl;
        return EXIT_FAILURE;
    }
    std::ifstream in_file(argv[1]);
    std::string str;
    std::vector<std::string> vec, result_vec;
    while (std::getline(in_file, str)) {
        vec.push_back(str);
    }

    std::for_each(vec.begin(), vec.end(), [&](std::string str) {
        result_vec.push_back(str + argv[3]);
    });

    std::ofstream out_file(argv[2]);
    std::copy(result_vec.begin(), result_vec.end(), std::ostream_iterator<std::string>(out_file, "\n"));
}

There is no need to store all lines from the input file in memory.
For each line read from the input you can append the string and write the
result to the output file. This would save memory for large input files.

You should also check if opening the input and output file was
successful, otherwise your program will silently do nothing if the
files could not be opened/created.

std::ifstream in_file(argv[1]);
if (!in_file) {
    std::cerr << "Could not open input file\n";
    return EXIT_FAILURE;
}
std::ofstream out_file(argv[2]);
if (!out_file) {
    std::cerr << "Could not create output file\n";
    return EXIT_FAILURE;
}
const char * add_string = argv[3];

std::string str;
while (std::getline(in_file, str)) {
    out_file << str << add_string << "\n";
}


HashMap used to store DateTime I have a scenario where I need to save a string as a key and DateTime as value into a hash map. Also, I need to remove strings older than 24 hours.

At the moment the HashMap is implemented as below:

HashMap<String, DateTime> sessions = new HashMap<>();
//Adding to the HashMap
sessions.put(session.toString(), DateTime.now());
//Removing from the HashMap
sessions.forEach((str, time)-> {
    if(time.clicks() < DateTime.now().minus(86400000).clicks())
        sessions.remove(str);
});

Would it be less overhead to save the DateTime clicks as Long datatype rather than saving the whole DateTime object? Like below:

HashMap<String, Long> sessions = new HashMap<>();
//Adding to the HashMap
sessions.put(session.toString(), DateTime.now().clicks);
//Removing from the HashMap
sessions.forEach((str, time)-> {
    if(time < DateTime.now().minus(86400000).clicks())
        sessions.remove(str);
});

I'll go out on a limb and say most senior developers would discourage any form of datetime arithmetic involving raw numbers rather than DateTime objects. There are a whole heap of problems you can avoid (leap years, leap seconds, timezone changes etc.) by always using datetime objects rather than timestamps. See for example Falsehoods programmers believe about time. In short, the potential for bugs completely outweighs the tiny memory savings.


Instant messaging system I'm now developing an instant messaging system but I'm a little confused: I can't decide which approach I have to choose!

I want you to tell me what is good/bad with this programming approach.  One friend of mine said it's difficult to read, but it works.

/*! | (c) 2012, 2013 by Bellashh*/
 /// <reference path="jquery-2.0.0-vsdoc.js" />
$(document).ready(function () {
  var ChatProvider = {
    "Friends": [],
    "People": [],
    "Conferences": [],
    "UI": {},
    "Utils": {
      "functions": {
        "alert": function (str) {
          alert(str);
        } /*ChatProvider.Utils.functions.alert*/ ,

        "addFriend": function (friend) {
          ChatProvider.Friends.push(friend);
          alert(ChatProvider.Friends.pop().Names);
        } /*ChatProvider.Utils.functions.addFriend*/ ,

      } /*ChatProvider.Utils.functions*/ ,

      "events": {} /*ChatProvider.Utils.events*/ ,

      "settings": {

      } /*ChatProvider.Utils.settings*/
    }
  } //var ChatProvider

  ChatProvider.Utils.functions.addFriend({
    "Names": "bellash"
  });

}); //$(document).ready

P.S: for visibility sake, I added some space between line separating objects' properties.

From a once over:

drop functions, it is too long and it's obvious that alert in ChatProvider.utils.alert() is a function. Furthermore, specifically for alert, that should be under UI in my mind
Your functions are all over the place, I would think that adding a friend would be ChatProvider.friends.add() but you put it in ChatProvider.Utils.functions.addFriend()
You treat your object as 1 singleton, what if you need more than 1 instance ?
Maybe you are from a Java background, but namespacing the way you approach it should be avoided


2-player board game app This question is about the best way to declare functions in Javascript. The app is a small 2 player board game I'm working on as part of a bigger web page. Basically I have a socket which receives and send messages and the game acts according to the content of the message.

$(function () {
    var board = document.getElementById('board');
    //When a msg is received
    //structure of msg : msg = ['action','args']
    if (msg[0] === 'action1') {
        func1(args);
    } else if (msg[0] === 'action2') {
        func2(args);
    }
}

function func1(args) {
    var a, b...;
    doAction1(); //uses the DOM element board, the args and local variables
}

function func2(args) {
    var a, b...;
    doAction2();
}

Now, I was thinking of using an object the way I would do in Java but it seems that it is not the best practice. Should I use a single function like so?

$(function () {

    var game = function () {
        var gamevar1, gamevar2, ...;
        action1: function (arg) {
            doAction1();
        }

        action2: function (arg) {
            doAction2();
        }
    }

    //when a msg is received [action,param]
    game.msg[0](msg[1]);
}

Since this is all the code you posted, I'm going to assume you're only using these functions in the place they're defined, so the later code would be best. It's more readable and doesn't include unnecessary identifiers.

Your question is really about anonymous functions, not closures. To answer this question, you should ask yourself, "do I plan on using these functions elsewhere"? Since anonymous functions, by definition, are not bound to an identifier, they will be inaccessible everywhere except where they are defined.

This is the version that I would recommend:

$(function () {

    var game = function () {
        var gamevar1, gamevar2, ...;
        action1: function (arg) {
            doAction1();
        }

        action2: function (arg) {
            doAction2();
        }
    }

    //when a msg is received [action,param]
    game.msg[0](msg[1]);
}


MySQL case statement with two tables How can I improve the following working SQL statement for better performance and consistency?

SELECT if(roll is null,0,roll) as roll,
       if(cpapername is null,0,cpapername) as compulsory,
       if(corepapername is null,0,corepapername) as Core,
       if(e1papername is null,0,e1papername) as Elective1,
       if(e2papername is null,0,e2papername) as Elective2
       FROM(
       Select student.univ_roll as roll, student.name as name, student.rollno as rollno,
       (CASE WHEN student.compulsory = paper.subject
         THEN (paper.paper_name) END) AS cpapername,
       (CASE WHEN student.core= paper.subject
         THEN (paper.paper_name) END) AS corepapername,
       (CASE WHEN student.elective_I= paper.subject
         THEN (paper.paper_name) END) AS e1papername,
       (CASE WHEN student.elective_II= paper.subject
         THEN (paper.paper_name) END) AS e2papername
        FROM student, paper WHERE student.semester='First'
        AND student.batch='2014' AND student.class='BSc'
        AND student.semester=paper.semester AND student.batch=paper.batch
        AND student.class=paper.class  AND student.regd='1')r

Any suggestions and alternative solution are greatly welcome and appreciated as I am looking for better ways to code.

You can simplify if(col is null,0,col) as ifnull(col, 0).

The SQL keywords are inconsistently formatted.
Sometimes you write SELECT, other times as Select.
I suggest to stick to all caps always.
The same goes for if (and ifnull) and as too.
It's easier to read the code when the writing is consistent.

The last line of your script is very long,
and it's difficult to read when I have to scroll to the right and the left part goes out of sight.
I suggest to add a few line breaks in there to make the code fit within one horizontal window. (I see you updated your original post, now it's good.)

It would be better to indent the sub-query more, and to put the closing ) on a new line to make it more visible. Something like this:

SELECT IFNULL(roll, 0) AS roll,
       IFNULL(cpapername, 0) AS compulsory,
       IFNULL(corepapername, 0) AS Core,
       IFNULL(e1papername, 0) AS Elective1,
       IFNULL(e2papername, 0) AS Elective2
       FROM (
           SELECT student.univ_roll AS roll, student.name AS name, student.rollno AS rollno,
           (CASE WHEN student.compulsory = paper.subject
             THEN (paper.paper_name) END) AS cpapername,
           (CASE WHEN student.core= paper.subject
             THEN (paper.paper_name) END) AS corepapername,
           (CASE WHEN student.elective_I= paper.subject
             THEN (paper.paper_name) END) AS e1papername,
           (CASE WHEN student.elective_II= paper.subject
             THEN (paper.paper_name) END) AS e2papername
            FROM student, paper WHERE student.semester='First'
            AND student.batch='2014' AND student.class='BSc'
            AND student.semester=paper.semester AND student.batch=paper.batch
            AND student.class=paper.class  AND student.regd='1'
    ) r


Permutations with repetitions algorithm in R Given a string of length n, print all permutation of the given string.

The code bellow generates all permutations (with repetitions) for the given string and stores them in object res. Could you suggest ways to make the code faster (or a completely different algorithm written in R)? (For the "ABRACADABRA" word it takes more than 9 minutes to complete)

#############################################################
word <- "ABRACADABRA"
#############################################################

vec <- unlist(strsplit(word,""))
charTable <- sort(table(vec),decreasing = TRUE)
totChars  <- sum(charTable)

combTable <-choose(c(totChars,totChars-cumsum(charTable)),charTable)
combTable <-combTable[combTable>0]
names(combTable) <- names(charTable)
totCombs <- prod(combTable)

# initialize res
res <- data.frame(matrix(rep(NA,totCombs*totChars),ncol=totChars))

for(i in 1:length(combTable)){
  totRows <- prod(combTable[1:i])
  copyRows <- prod(combTable[1:(i-1)])

  if (totRows>copyRows){
    tmp <- res[1:copyRows,]
    for(j in 1:(combTable[i]-1)){
      res[(j*copyRows+1):((j+1)*copyRows),] <- tmp
    }
  }

  tmpCombs <- t(combn(ifelse(i>1,totChars-cumsum(charTable)[i-1],totChars), charTable[i]))
  letr <- names(charTable)[i]

  if(i==1){
    for(k in 1:totRows){
      res[k,tmpCombs[k,]] <- letr
    }
  } else if (i>1 & totRows==copyRows){
    for(k in 1:totRows){
      res[k,is.na(res[k,])][tmpCombs[1,]] <- letr
    }
  } else{
    grpRows <- prod(combTable[1:(i-1)])

    for(k in 1 : combTable[i]){
      for(m in ((k-1)*grpRows+1):((k)*grpRows)){
        res[m,is.na(res[m,])][tmpCombs[k,]] <- letr
      }
    }
  }
}

This is one of many instances where people would be tempted to say, "see, loops in R are slow".  And my response is, "loops aren't that slow, but you can put slow code in loops."

There's no reason res needs to be a data.frame.  Leave it as a matrix and your code runs in under a second on my laptop.

# initialize res
res <- matrix(rep(NA,totCombs*totChars),ncol=totChars)

The lesson here is:

  If you don't need the multi-typed columns of a data.frame, use a matrix.

You might look into factoradics as another possible algorithm. That would allow you to generate permutations in lexicographic order.


A program to censor unwanted words Problem:

Censor Unwanted words.

Solution:

Note: I am beginner and self learning programming by reading the book "Programming: Principles and Practises" by Bjarne Stroustrup and this a "try this" exercise from chapter 4.

#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> words;

    std::cout << "Enter a list of words.\n";
    std::string temp;

    while(std::cin>>temp){
        words.push_back(temp);
        }

    for(auto w : words){
        if(w=="idiot"||w=="nonsense"||w=="hyper"){
            std::cout<<"BlEEP"<<" ";
        }
        else std::cout<<w<<" ";
    }
}

Sample Test run.

That's fine. You have exactly the includes you need, you didn't use using namespace, and you've used std::cin correctly.

However, I'd recommend you to use a code formatter next time, since some for your closing braces are misaligned. And if you use braces around a single statement in if, do the same for else.

Other than that, you can take a const auto & word instead of auto w to remove copies. Also word is a nicer name than w. Try to be more verbose, it helps when you read the code a second time.

Since we do not use temp after the while, should keep its scope smaller. We can use a for loop, wrap braces around the while and temp's declaration (which does the same), or write a small (inline) function.

We end up with

#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> words;

    std::cout << "Enter a list of words.\n";

    for(std::string temp; std::cin >> temp;){
        words.push_back(temp);
    }

    for(const auto &word : words){
        if(word == "idiot" || word == "nonsense" || word == "hyper"){
            std::cout << "BlEEP" <<" ";
        } else {
            std::cout << word << " ";
        }
    }
}

I've added some whitespace to make the code slightly easier to read. However, our code works well for three words, but what about four, five or even ten? As soon as we have more words than fit in a single line, we probably want to store them in a container.

Exercise

Read the banned words from a file. Use those words to filter the text just like you did.


Storing configuration data in a global variable on application startup I've never used a global variable in one of my programs(of which there are few) before, is this a fair use of one?

Currently, I have several classes calling a function to grab the data they require from a .yaml file stored in the user home folder. Each class constructs the full path of where the .yaml file should be using two vars; os.path.expanduser("~") and the default filename. This I feel, is messy.

The code below is a simple example of my actual program:

hub.py(Context manager instantiated on startup by main.py):

class Hub:

    def __init__(self):
        self.filename = "config_data.yaml"
        self.filepath = os.path.join(os.path.expanduser("~"), self.filename)

    def __enter__(self):
        global config_data

        with open(self.filepath) as f:
            config_data = yaml.load(f)

        return self

    def __exit__(self, exc_type, value, traceback):
        pass

with Hub() as hub:
    pass

Example use:

from hub import config_data

class Bot:
    def __init__(self):
        self.pricing_params = config_data["pricing"]

Regardless of whether or not this is a good use of global variables, it is a poor use of a class.

Your hub.py could look like this and function exactly the same (you could still import config_data from it):

import os
import yaml

file_path = os.path.join(os.path.expanduser("~"), "config_data.yaml")
with open(file_path) as f:
    config_data = yaml.load(f)

And now that it is obvious that this does not actually do a lot, I would make this a function and put this in your actual script (or maybe a utils script, if you have many more utilities like that):

file utils.py:

import yaml

def get_config(file_path):
    with open(file_path) as f:
        return yaml.load(f)

file bot.py:

import os
from utils import get_config

FILE_PATH = os.path.join(os.path.expanduser("~"), "config_data.yaml")
CONFIG_DATA = get_config(FILE_PATH)

class Bot:
    def __init__(self):
        self.pricing_params = CONFIG_DATA["pricing"]

if __name__ == "__main__":
    bot = Bot()


First simple program; counting button presses This is my first solo Objective-C program... it does the complicated task of displaying a button and counting how many times it's been pressed.  It retains that count (along with the last time it was pressed) between uses of the app.  I have a few specific questions, but eagerly await any and all feedback.

Is the try/catch block the best way to look up count and lastPress for the first time the app is used?  There aren't any values before it's ever used, but afterward there always should be.
I repeat myself in setting up the directories and documents variables.  Should that be eliminated?  If so, what's the best practice there?
I declare the count as an int property in ViewController.h, and I declare lastPress within ViewController.m.  Is one better than the other?

PMNViewController.h

#import <UIKit/UIKit.h>

@interface PMNViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *buttonCount;
@property (strong, nonatomic) IBOutlet UILabel *buttonDate;
@property (assign) int count;
- (IBAction)pushButtonPressed:(id)sender;
- (void)setDateLabel:(NSString*)documents;
@end

PMNViewController.m

#import "PMNViewController.h"

@interface PMNViewController ()

@end

@implementation PMNViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [directories firstObject];
    NSString *filePathCount = [documents stringByAppendingPathComponent:@"count.plist"];
    NSString *datePath = [documents stringByAppendingPathComponent:@"date.plist"];
    NSString *lastPress;

    @try {
        NSString *contents = [NSString stringWithContentsOfFile:filePathCount encoding:NSUTF8StringEncoding error:nil];
        self.count = [contents integerValue];
        lastPress = [NSString stringWithContentsOfFile:datePath encoding:NSUTF8StringEncoding error:nil];

    }
    @catch (NSException *exception) {
        self.count = 0;
        lastPress = @"Never pressed!";
    }
    @finally {
        self.buttonCount.text = [NSString stringWithFormat:@"%i", self.count];
        self.buttonDate.text = lastPress;
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setDateLabel:(NSString*)documents{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm 'on' MM-dd-YYYY"];
    NSDate *now = [[NSDate alloc] init];

    NSString *lastPressText = [dateFormatter stringFromDate:now];
    self.buttonDate.text = lastPressText;
    NSString *datePath = [documents stringByAppendingPathComponent:@"date.plist"];
    [lastPressText writeToFile:datePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

}

- (IBAction)pushButtonPressed:(id)sender {
    NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [directories firstObject];

    self.count += 1;
    NSString *countText = [NSString stringWithFormat:@"%i", self.count];
    self.buttonCount.text = countText;
    NSString *filePathCount = [documents stringByAppendingPathComponent:@"count.plist"];
    [countText writeToFile:filePathCount atomically:YES encoding:NSUTF8StringEncoding error:nil];
    [self setDateLabel:documents];

}

@end

The first thing that needs to be addressed is the try-catch block.

Without (for now) addressing whether or not you're taking the best approach for this sort of persistent data, the fact of the matter here is that there's not a single exception that your try-catch block is going to catch.

In Objective-C, we don't use try-catch blocks all that much, and they are reserved for behavior that is truly, truly exceptional.  Generally speaking, you should almost never be using them, particularly at such a high level of development.

The last argument of the stringWithContentsOfFile:encoding:error: method is reserved for sending a NSError ** variable.  A pointer to a pointer.

If there's some sort of problem reading the file, no exception is thrown.  Instead, the method returns nil and the error variable, if you've sent a pointer to one, is set to an NSError object that describes the problem in incurred.

Internally, the method quite likely uses try-catch blocks.  And if you're developing some framework code, then you might use a try-catch block within a method if you're trying to catch some truly exception problem, but these cases are extraordinarily rare.  And what should your catch block look like in these cases?  Almost always, the catch block in an Objective-C method should look something like this:

@catch (NSException *exception) {
    // build an NSError object
    // assign the pointer to pointer you took as a method argument to point to this object
    return nil;
}

The only time your @catch block should look differently is if you can continue to execute code despite the exception... but as I said, Objective-C exceptions are reserved for truly exceptional behavior and it's extraordinarily unlikely that one will be thrown and you'll be able to continue with the method.

So instead of the try catch block, your code should instead look something more like this:

NSError *error;

NSString *contents = [NSString stringWithContentsOfFile:filePathCount encoding:NSUTF8StringEncoding error:&error];
self.count = [contents integerValue];
lastPress = [NSString stringWithContentsOfFile:datePath encoding:NSUTF8StringEncoding error:&error];

if (!error) {
    self.buttonCount.text = [NSString stringWithFormat:@"%i", self.count];
    self.buttonDate.text = lastPress;
} else {
    self.count = 0;
    lastPress = @"Never pressed!";
}

The NSError objects will tend to contain extra information about exactly what sort of error was encountered, but in many cases, simply checking whether or not it was set it more than enough.

Now, as for this idea of manually writing to file... I mean ultimately, it's just not going to be the best.  The only time writing plain text to a file is recommended would be we you need to generate a plain .txt file for use outside your app.

Otherwise, there are just generally better options available.

For something as simple as this, persisting a single variable across multiple sessions, we can simply use NSUserDefaults.  At the end of the, NSUserDefaults actually is writing data to a file, however it's an extremely efficient way of doing it and it can be done in a very clean and concise way.  Accessing the data stored in NSUserDefaults is so efficient by the way, that it's not really necessary to have a separate local/instance variable for keeping track of the current count.  We can read/write from/to NSUserDefaults instantly.

First, some groundwork.  Let's define some constants for our NSUserDefaults keys, and write a method that will handle updating our labels:

NSString * const kButtonCounter = @"DefaultsCounterKey";
NSString * const kLastCountDate = @"DefaultsDateKey";

- (void)updateLabels {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSInteger count = [defaults integerForKey:kButtonCounter];
    self.buttonCount.text = [NSString stringWithFormat:@"%l", (long)count];

    NSDate *lastPress = [defaults objectForKey:kLastCountDate];
    NSString *dateString;
    if (lastPress) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm 'on' MM-dd-YYYY"];
        dateString = [dateFormatter stringFromDate:lastPress];
    } else {
        dateString = @"Never pressed!";
    }
    self.buttonDate.text = dateString;
}

In viewDidLoad, we can check whether or not an object exists for the kLastCountDate key.  If it doesn't, the buttons never been pressed before, and we should set up our kButtonCounter so that we can properly display 0 for the counter.  Otherwise, we should have good values for both keys.  Once we've done this, we simply call the updateLabels method we've already written:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults objectForKey:kLastCountDate]) {
        [defaults setInteger:0 forKey:kButtonCounter];
    }

    [self updateLabels];
}

In the method handling the button press, it's as simple as updating the information in NSUserDefaults, and then refreshing the UI:

- (IBAction)pushButtonPressed:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:[NSDate date] forKey:kLastCountDate];

    NSInteger newCount = [defaults integerForKey:kButtonCounter] + 1;
    [defaults setInteger:newCount forKey:kButtonCounter];

    [self updateLabels];
}


Processing CSV files with filtering I have 4 CSV files that i need to process (filter out Part segment and then merge them) but the problem is that they do not fit in my memory. So I decided to: [open - filter - write out] each one of theses 4 files and merge them after reopening the filtered version.
I learned that it was good practice to decouple the functionality (filter functionality, merge functionality, write out functionality) but in this case splitting filter and dumping out functionality seem silly, it would be like wrapping an existing function  from pandas library.
But merging the 2 functionality make me also uncomfortable since I have heard that it is not good practice to write functions that return and have side effect (as writing out a CSV) as follow:
def exclude_segm_part(f):
    """ Filter out "Part client" on credit selection and write it on disk

    Args:
        f (string): filepath of the credit base (csv)
    """
    df = pd.read_csv(f,sep=';',encoding="latin")
    df_filter = df[df.LIB_SEGM_SR_ET!="PARTICULIERS"]
    filename = "{base}.txt".format(base=os.path.basename(f).split(".txt")[0])
    df_filter.to_csv(filename,index=False,sep=';')
    return df_filter

What would be your suggestion? (Hope my question is clear enough, I want to get the good practice of coding in data science environment)

First off, what you currently have is perfectly fine. The only thing I would suggest is to use pathlib.Path instead of manually using format and consistently follow the PEP8 naming scheme by using lower_case and having spaces after commas in argument lists:
from pathlib import Path

file_name = Path(f).with_suffix(".txt")

I see two ways you could take this code. One direction is making it more memory efficient by using normal Python to parse the file. This would allow you to make the code fully streaming and process all files in one go:
import csv

def filter_column(files, column, value):
    out_file_name = ...
    with open(out_file_name, "w") as out_file:
        writer = csv.writer(out_file, sep=";")
        for file_name in files:
            with open(file_name) as in_file:
                reader = csv.reader(in_file, sep=";")
                col = next(reader).index(column)
                writer.writerows(row for row in reader if row[col] != value)

This has almost no memory consumption, due to the writerows, which probably consumes the generator expression after it. If you want it fully independent of your memory (as long as a row fits into memory):
                for row in reader:
                    if row[col] != value:
                        csv_writer.writerow(row)

The other possibility is to go parallel and distributed and use something like dask:
import dask.dataframe as dd

files = "file1.csv", "file2.csv"
df = dd.read_csv(files, sep=";")
df_out = df[df.LIB_SEGM_SR_ET != "PARTICULIERS"]
file_name = ...
df_out.compute().to_csv(file_name, index=False, sep=";")

This gives you the full ease of using pandas and splits the task into batches that fit into memory behind the scenes.


RadioButton Selection Extremely Slow I have a DataGrid that I filter when the user inputs text into a search box. The column that is filtered is based upon which radio button the user selects (Name, Town or Post Code). Although initially nothing happens until the user has started typing into the searchbox, for some reason there is a noticeable delay in selecting the different radio buttons.

Intuitively I thought that something was happening when the radio button is clicked, but there is (as far as I can tell) nothing being called. The C# program is relatively long, so I'll include the XAML for the radio buttons and the methods that they are being used but not the whole program, unless nothing is found originally.

SearchGrid

    private void SearchGrid(object sender, TextChangedEventArgs e)
    {
        if (!string.IsNullOrEmpty(searchBox.Text))
        {
            var view = CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);
            var searchText = CharactersOnly(searchBox.Text.ToLower());
            view.Filter = (obj =>
            {
                CompanyModel compDetails = obj as CompanyModel;
                if (compDetails == null)
                    return true;

                if (compNameRad.IsChecked == true)
                {
                    return compDetails.CompanyName != null && CharactersOnly(compDetails.CompanyName.ToLower()).Contains(searchText);
                }
                if (compTownRad.IsChecked == true)
                {
                    return compDetails.CompanyTown != null && CharactersOnly(compDetails.CompanyTown.ToLower()).Contains(searchText);
                }
                if (compPcodeRad.IsChecked == true)
                {
                    return compDetails.CompanyPcode != null && CharactersOnly(compDetails.CompanyPcode.ToLower()).Contains(searchText);
                }
                return false;
            });
        }
        else
        {
            FillDataGrid();
        }
    }

ClearFilter

    private void ClearFilter(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Escape)
        {
            searchBox.Text = string.Empty;
            compNameRad.IsChecked = false;
            compTownRad.IsChecked = false;
            compPcodeRad.IsChecked = false;
            FillDataGrid();
        }
    }

XAML

                <StackPanel Margin="0" Grid.Row="1">
                    <Label Content="Search" HorizontalAlignment="Center" Margin="0,10,0,5" VerticalAlignment="Center" FontSize="21.333" FontWeight="Bold"/>
                    <RadioButton x:Name="compNameRad" Content="Name" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="18.667" GroupName="RadSearch" Margin="7,0,0,5"/>
                    <RadioButton x:Name="compTownRad" Content="Town" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="18.667" GroupName="RadSearch" Margin="7,5,0,5"/>
                    <RadioButton x:Name="compPcodeRad" Content="Pcode" HorizontalAlignment="Left" Margin="7,5,0,5" VerticalAlignment="Top" FontSize="18.667" GroupName="RadSearch"/>
                    <TextBox x:Name="searchBox" TextWrapping="Wrap" Margin="20,5" FontSize="18.667" VerticalAlignment="Center" TextChanged="SearchGrid"/>
                    <Label Content="Press Esc to clear filter" Margin="5,10,5,5" FontSize="16"/>
                </StackPanel>

I really see no reason for anything to slow down purely based on the selection of the radio button, is there a way to improve this code?

I believe  the GetDefaultView method is the bottle neck. If you refer to the source code you'll see that there are serverals loops involved in getting the view:
You start here:
public static ICollectionView GetDefaultView(object source)
{
    return GetOriginalView(GetDefaultCollectionView(source, true));
}

it calls:
static internal CollectionView GetDefaultCollectionView(object source, bool createView, Func<object, object> GetSourceItem=null)
{
    if (!IsValidSourceForView(source))
        return null;

    DataBindEngine engine = DataBindEngine.CurrentDataBindEngine;
    ViewRecord viewRecord = engine.GetViewRecord(source, DefaultSource, null, createView, GetSourceItem);

    return (viewRecord != null) ? (CollectionView)viewRecord.View : null;
}

it then calls the GetViewRecord which seems to be really heavy:
internal ViewRecord GetViewRecord(object collection, CollectionViewSource cvs, Type collectionViewType, bool createView, Func<object, object> GetSourceItem)
{
    // Order of precendence in acquiring the View:
    // 0) If  collection is already a CollectionView, return it.
    // 1) If the CollectionView for this collection has been cached, then
    //    return the cached instance.
    // 2) If a CollectionView derived type has been passed in collectionViewType
    //    create an instance of that Type
    // 3) If the collection is an ICollectionViewFactory use ICVF.CreateView()
    //    from the collection
    // 4) If the collection is an IListSource call GetList() and perform 5),
    //    etc. on the returned list
    // 5) If the collection is an IBindingList return a new BindingListCollectionView
    // 6) If the collection is an IList return a new ListCollectionView
    // 7) If the collection is an IEnumerable, return a new CollectionView
    //    (it uses the ListEnumerable wrapper)
    // 8) return null
    // An IListSource must share the view with its underlying list.

    // if the view already exists, just return it
    // Also, return null if it doesn't exist and we're called in "lazy" mode
    ViewRecord viewRecord = GetExistingView(collection, cvs, collectionViewType, GetSourceItem);
    if (viewRecord != null || !createView)
    {
        return viewRecord;
    }

    // If the collection is an IListSource, it uses the same view as its
    // underlying list.
    IListSource ils = collection as IListSource;
    IList ilsList = null;
    if (ils != null)
    {
        ilsList = ils.GetList();
        viewRecord = GetExistingView(ilsList, cvs, collectionViewType, GetSourceItem);

        if (viewRecord != null)
        {
            return CacheView(collection, cvs, (CollectionView)viewRecord.View, viewRecord);
        }
    }

    // Create a new view
    ICollectionView icv = collection as ICollectionView;

    if (icv != null)
    {
        icv = new CollectionViewProxy(icv);
    }
    else if (collectionViewType == null)
    {
        // Caller didn't specify a type for the view.
        ICollectionViewFactory icvf = collection as ICollectionViewFactory;
        if (icvf != null)
        {
            // collection is a view factory - call its factory method
            icv = icvf.CreateView();
        }
        else
        {
            // collection is not a factory - create an appropriate view
            IList il = (ilsList != null) ? ilsList : collection as IList;
            if (il != null)
            {
                // create a view on an IList or IBindingList
                IBindingList ibl = il as IBindingList;
                if (ibl != null)
                    icv = new BindingListCollectionView(ibl);
                else
                    icv = new ListCollectionView(il);
            }
            else
            {
                // collection is not IList, wrap it
                IEnumerable ie = collection as IEnumerable;
                if (ie != null)
                {
                    icv = new EnumerableCollectionView(ie);
                }
            }
        }
    }
    else
    {
        // caller specified a type for the view.  Try to honor it.
        if (!typeof(ICollectionView).IsAssignableFrom(collectionViewType))
            throw new ArgumentException(SR.Get(SRID.CollectionView_WrongType, collectionViewType.Name));

        // if collection is IListSource, get its list first (bug 1023903)
        object arg = (ilsList != null) ? ilsList : collection;

        try
        {
            icv = Activator.CreateInstance(collectionViewType,
                            System.Reflection.BindingFlags.CreateInstance, null,
                            new object[1]{arg}, null) as ICollectionView;
        }
        catch (MissingMethodException e)
        {
            throw new ArgumentException(SR.Get(SRID.CollectionView_ViewTypeInsufficient,
                            collectionViewType.Name, collection.GetType()), e);
        }
    }

    // if we got a view, add it to the tables
    if (icv != null)
    {
        // if the view doesn't derive from CollectionView, create a proxy that does
        CollectionView cv = icv as CollectionView;
        if (cv == null)
            cv = new CollectionViewProxy(icv);

        if (ilsList != null)    // IListSource's list shares the same view
            viewRecord = CacheView(ilsList, cvs, cv, null);

        viewRecord = CacheView(collection, cvs, cv, viewRecord);

        // raise the event for a new view
        BindingOperations.OnCollectionViewRegistering(cv);
    }

    return viewRecord;
}

who knows what eles it does :-)
then finally there is another loop involved here:
// return the original (un-proxied) view for the given view
static ICollectionView GetOriginalView(ICollectionView view)
{
    for (   CollectionViewProxy proxy = view as CollectionViewProxy;
            proxy != null;
            proxy = view as CollectionViewProxy)
    {
        view = proxy.ProxiedView;
    }

    return view;
}

Refactoring
I suggest you refactor your SearchGrid in a way that you retrieve the view somewhere else and here just update the filter:
private ICollectionView _view;

private void InitializeSearchView()
{
    view = CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);
}

private void SearchGrid(object sender, TextChangedEventArgs e)
{
    if (string.IsNullOrEmpty(searchBox.Text))
    {
        FillDataGrid();
        return;
    }

    var searchText = CharactersOnly(searchBox.Text);
    _view.Filter = (obj =>
    {
        CompanyModel compDetails = obj as CompanyModel;
        if (compDetails == null)
        {
            return true;
        }

        if (compNameRad.IsChecked == true)
        {
            return CompanyContains(compDetails.CompanyName, searchText);
        }
        if (compTownRad.IsChecked == true)
        {
            return CompanyContains(compDetails.CompanyTown, searchText);
        }
        if (compPcodeRad.IsChecked == true)
        {
            return CompanyContains(compDetails.CompanyPcode, searchText);
        }
        return false;
    });
}

at the same time I'd move the condition into a dedicated method and remove nesting by inverting the above if:
private bool CompanyContains(string value, string searchText)
{
    return !string.IsNullOrEmpty(value) && CharactersOnly(value).IndexOf(searchText) > 0;
}