PHP Add Column to two dimentional array i do have two dimentional array of data in php.

I do want to add another column with constant to that array for passing tabular data to next program.

i am iterating and adding manually. What can be alternate code?

my code:

            if( is_array( $arrSource )){
                foreach( $arrSource as &$dataRow){
                    foreach( $arrColumn as $k=>$v)
                        $dataRow[$k] = $v;
                }
                unset( $dataRow );
            }

I see nothing wrong with what you already have. Assuming of course that you meant to append identical data to each $arrSource element. You are already doing the shorthand by using the reference operator. The only other way would be to write it all out manually, which would be pointless, unless you are looking for more novice readable code. I would hesitate to add, however, that unset should be called tithin the first foreach loop. Though I could not confirm 100% because I don't use the reference operator to know its quirks :)


Timer wrapper review I used this code in several projects. But I am not sure if it is stable. However on production it is working well, but I want to listen your advices:

/// <summary>
/// Standard abstract parent for Timer-powered classes. Has Start|Stop|Pulse events
/// </summary>
public abstract class TimerContainer : IDisposable
{
    protected Timer Timer { get; set; }
    protected TimeSpan Interval { get; set; }

    protected TimerContainer() : this(TimeSpan.FromSeconds(1)) { }

    protected TimerContainer(TimeSpan interval)
    {
        Status = TimerStatus.NotInitialized;
        Timer = new Timer(OnTick, null, Timeout.Infinite, Timeout.Infinite);
        Interval = interval;
    }

    protected abstract void OnTick(object sender);

    #region Standard Events
    public event EventHandler OnStart;
    public event EventHandler OnStop;
    public event EventHandler OnPulse;

    public TimerStatus Status { get; protected set; }

    public void Start()
    {
        Timer.Change(TimeSpan.Zero, Interval);
        Status = TimerStatus.Running;

        if (OnStart != null) {
            OnStart(this, new EventArgs());
        }
    }

    public void Stop()
    {
        Status = TimerStatus.Stopped;
        Timer.Change(Timeout.Infinite, Timeout.Infinite);

        if (OnStop != null) {
            OnStop(this, new EventArgs());
        }
    }

    protected void InvokePulse()
    {
        if (OnPulse != null) {
            OnPulse(this, new EventArgs());
        }
    }

    #region IDisposable Members

    protected bool disposed = false;
    ~TimerContainer()
    {
        Dispose(false);
    }
    public virtual void Dispose()
    {
        if (!disposed) {
            Dispose(true);
            GC.SuppressFinalize(this);
            disposed = true;
        }
    }
    protected virtual void Dispose(bool disposing)
    {
        if (disposing) {
            if (Status != TimerStatus.Stopped) {
                Stop();
            }

            if (Timer != null) {
                Timer.Dispose();
                Timer = null;
            }
        }
    }

    #endregion
    #endregion
}

/// <summary>
/// Timer status
/// </summary>
public enum TimerStatus
{
    NotInitialized = 0,
    Running = 1,
    Stopped = 2
}

One thing is that the way you're calling the events is not thread-safe:

if (OnStop != null) {
    OnStop(this, new EventArgs());
}

If the only subscriber to the event unsubscribes between the null check and the invoke, you will get a NullReferenceException. The correct way is like this:

var onStop = OnStop;
if (onStop != null) {
    onStop(this, new EventArgs());
}

Or, alternatively, you can make sure that the events are never null by adding a subscriber that does nothing:

public event EventHandler OnStart = delegate { };

That way, you don't have to check for null at all. This could hurt performance a bit, but it shouldn't be noticeable (especially since the events most likely won't be invoked more frequently than once every few milliseconds).

Another thing is that your finalizer is completely useless, since you don't have any unmanaged resources. It could make sense if some of the children of this class did have some unmanaged resources, but I think those could have their own fianlizer.

Also, in the usual Dispose() pattern, the parameterless Dispose() method is not virtual. I don't see any reason for it here, having virtual Dispose(bool) should be enough.


Sum of primes up to 2 million using the Sieve of Eratosthenes I've solved question 10 on Project Euler using the Sieve of Eratosthenes, what can I do to optimize my code?

def prime_sum(n):
    l=[0 for i in range(n+1)]

    l[0]=1
    l[1]=1
    for i in range(2,int(n**0.5)+1):
        if l[i]==0:
            for j in range(i * i, n+1, i):
                    l[j]=1
    s=0
    for i in range(n):
        if l[i] == 0:
            s+=i
    print(s)

if __name__ == '__main__':
    prime_sum(2000000)

Code readability and style -

I believe that good code should have good style and should be more readable and concise.

According to PEP 8 -

  Never use the characters l (lowercase letter el), O (uppercase
  letter oh), or I (uppercase letter eye) as single character variable
  names.

  In some fonts, these characters are indistinguishable from the
  numerals one and zero. When tempted to use l, use L instead.

I ran a PEP 8 checker over your code, and here are the results -

This means that your code uses inconsistent whitespaces. Therefore, your code (in terms of readability and style) could be improved like this (along with other recommendations) -

def prime_sum(n):
    my_list = [0 for i in range(n+1)]

    my_list[0] = 1
    my_list[1] = 1

    for i in range(2, int(n**0.5) + 1):
        if my_list[i] == 0:
            for j in range(i*i, n+1, i):
                    my_list[j] = 1
    s = 0
    for i in range(n):
        if my_list[i] == 0:
            s += i
    print(s)

if __name__ == '__main__':
    prime_sum(2000000)

Also, good use of the if __name__ == __'main__': guard. Most people don't even attempt to use it.

Improvements to your code -

Python provides a set type which is quite efficient at performing both of those operations (although it does chew up a bit more RAM than a simple list). Gladly, it's easy to modify your code to use a set instead of a list. Computation with sets is much faster because of the hash tables (What makes sets faster than lists in python?).

Also, we don't need to keep a running total of the sum of the primes. It's better to do that at the end using Python's built-in sum() function, which operates at C speed, so it's much faster than doing the additions one by one at Python speed.

So your code would then look like this -

def eratosthenes(n):
    # Declare a set - an unordered collection of unique elements
    multiples = set()
    for i in range(2, n+1):
        if i not in multiples:
           yield i
           multiples.update(range(i*i, n+1, i))

if __name__ == '__main__':
    # Now sum it up
    prime_sum = sum(eratosthenes(2000000))
    print(prime_sum)

which uses less space too.

If you're wondering what yield is...

  The yield statement suspends function’s execution and sends a value
  back to the caller, but retains enough state to enable function to resume
  where it is left off. When resumed, the function continues execution
  immediately after the last yield run. This allows its code to produce
  a series of values over time, rather them computing them at once and
  sending them back like a list.

Source - https://www.geeksforgeeks.org/use-yield-keyword-instead-return-keyword-python/

And if you are wondering what .update() is...

  The update() adds elements from a set (passed as an argument) to the
  set (calling the update() method).

  The syntax of update() is -

A.update(B)

Source - https://www.geeksforgeeks.org/python-set-update/

Now let's time your code -

# %timeit prime_sum(2000000)
>>> 648 ms ± 19.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

which does look pretty slow.

With the improved code, the time to execute greatly decreases -

# %timeit eratosthenes(2000000)
>>> 264 ns ± 2.73 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Key: ms - milliseconds
     ns - nanoseconds

Hope this helps!


Evaluating an arithmetic expression from a binary tree Let's say I have a Binary Tree that looks like

        +
    2       *
          5   8

I've written a function to traverse inorder to solve it as 2+5*8

Is there a better way? Using eval seems a bit hacky and perhaps I can just have a function calculate the total sum on either side and then do a left + right instead.

class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

class BinarySearchTree {
  constructor() {
    this.root = null;
  }

  solveEquation() {
    let equation = "";
    let traverse = (currentNode = this.root) => {
      if (currentNode !== null) {
        if (currentNode.left !== null) traverse(currentNode.left);
        equation += currentNode.value;
        if (currentNode.right !== null) traverse(currentNode.right);
      }
    }

    traverse();
    return eval(equation);
  }

  init() {
    this.root = new Node('+');
    let currentNode = this.root;
    currentNode.left = new Node(2);
    currentNode.right = new Node('*');
    currentNode = currentNode.right;
    currentNode.left = new Node(5);
    currentNode.right = new Node(8);
  }
}

let bst = new BinarySearchTree();
bst.init();
console.log(bst.solveEquation());

This is not a search tree.  You can call it an abstract syntax tree, or just a binary tree.

You can use a dispatch table to evaluate ops.

 dispatch = {
      '+': (a,b) => a+b,
      '*': (a,b) => a*b
 }
 ...
 if (node.left && node.right && node.value in dispatch) dispatch[node.value]( traverse(node.left), traverse(node.right) );


Converting objects to type Bool The following extension method is being used in our code base:

public static bool ToBool(this object src)
{
    return src != null && ((string) src).ToBool(false);
}

It leverages off another extension method:

public static bool ToBool(this string src, bool defaultValue)
{
    if (src.IsEmpty())
        return defaultValue;

    if (src.IsNumeric())
        return src.ToInt() != 0;

    var ret = false;
    if (bool.TryParse(src, out ret))
        return ret;
    return defaultValue;
}

(where the ToInt and all the Is* methods are wrappers around the appropriate objects IsNullEmpty / Parse method)

This does not 'feel' right but I cannot explain it to the author. Besides for the obvious problem of passing an object that cannot be cast to a string what other reasons are there to justify this as bad code?

Are there any other concrete reasons why this code may be good or bad?

I'd criticize:

Extension methods on "object" should be avoided because of the broad scope, except for special cases (which such a "ToBool" method is not one). It also feels like an overuse of extension methods.
The first one takes an "object", but really wants a string, and will blow up with an InvalidCastException at runtime.
The second one does not check for null (which wouldn't be a problem in itself, but having overloads for default values with differing behavior is bad).


Concurrent quicksort in C++ I have a task to write a concurrent version of quicksort algorithm, to speed it up. This is what I wrote:

template <class T>
void quicksort::sort(T *data, int len) {
    thread_count = 0;
    int p = 0;
    int r = len - 1;
    _sort(data, p, r);
}

template <class T>
void quicksort::_swap(T *data, int first, int second) {
    auto temp = data[first];
    data[first] = data[second];
    data[second] = temp;
}

template <class T>
void quicksort::_sort(T *data, int p, int r) {
    thread_count++;
    if (p < r) {
        auto q = _partition(data, p, r);
        if (thread_count >= thread_limit) {
            _sort(data, p, q - 1);
            _sort(data, q + 1, r);
        } else {
            std::thread lower(&quicksort::_sort<T>, this, data, p, q - 1);
            std::thread upper(&quicksort::_sort<T>, this, data, q + 1, r);
            lower.join();
            upper.join();
        }
    }
    thread_count--;
}

template <class T>
int quicksort::_partition(T *data, int p, int r) {
    auto first = data[p];
    auto last = data[r];
    T pivot = 0;
    // Median of three
    auto middle = data[(p + 2) / 2];
    if (first > middle) {
        if (middle > last) {
            pivot = middle;
        } else {
            pivot = last;
        }
    } else {
        if (middle > last) {
            pivot = std::max(first, last);
        } else {
            pivot = middle;
        }
    }
    auto i = p - 1;
    for (auto j = p; j < r; ++j) {
        if (data[j] <= pivot) {
            _swap(data, ++i, j);
        }
    }
    _swap(data, i + 1, r);
    return i + 1;
}

Those are methods from class quicksort, which also has std::atomic<int> thread_limit and std::atomic<int> thread_count.

So f.e I cap the thread limit to f.e maximum 1000 threads.

The part I am particularly not sure of is the part in _sort() function. Basically, I don't know if my way of starting those threads and then joining them is the right way to do it.

I have a benchmark method, and I benchmarked my quicksort implementation like this: I took the average of 100 benchmarks for every thread_limit from 200 to 2000, stepping by 100 (so 200, 300, 400) and for single-threaded quicksort of mine. And this was for 1.000.000-sized arrays (every time I was creating a new one - this overhead was not measured). The result was shocking for me. It occurred, that of course, even with 2000 threads it was faster than with only 1 thread, but still, the fastest one was with thread_limit = 200. I thought that the optimum would be somewhere nearer the middle.

Sometimes f.e 700 threads were faster than 600 threads, but still, the general rule was: the more threads, the slower.

Do you guys see any flaws in my code or in my understanding of the benchmark I made?

Partition - Pivot selection
There are 2 bugs in the pivot selection code, in combination allowing for \$\mathcal{O}(n^2)\$ worst case runtime.
Bug #1: Wrong average calculation
I guess this is just a typo:
auto middle = data[(p + 2) / 2];

Most likely, (p + r) / 2 was intended instead, and that would fix this problem (disregarding overflow).
Bug #2: Missing a case
if (first > middle) {
    if (middle > last) {
        pivot = middle;
    } else {
        pivot = last;
    }
}

This snippet doesn't handle the case correctly if \$middle < first < last\$. Even though first should be assigned as pivot, last will be.
The fix is simple: Change pivot = last; to pivot = std::min(first, last);.
Consequences
These two bugs allow for runtime complexity to degrade to \$\mathcal{O}(n^2)\$, because they introduce a huge bias towards last as pivot value:
For any call with p >= 4, middle will have the value of an element to the left of data[p], thus being guaranteed to be smaller than first or last due to previous runs of _partition. So `last will be chosen as pivot, due to bug #2.
For any call with p == 2 or p == 3, middle will be assigned the value of data[1], which is equal to first. In that case, pivot will always be assigned the value of middle/first.
For any call with p == 0 or p == 1, the median calculation will work with a slight bias towards last (due to bug #2).
Partition - Moving values
There seems to be an assumption that data[r] == pivot. This might have been a good assumption before due to bug #2, but that isn't necessarily the case once that bug is fixed. There a multiple ways to fix that.
Also, technically it isn't necessary to move values around that are equal to the pivot value: It doesn't actually matter whether they are left or right of the final pivot position.
Multithreading
The current multithreading solution isn't actually that efficient at using threads: Nearly half the threads at a time are waiting for the others to finish. (The first thread is waiting on threads #2 and #3 it spawned, they then are waiting on threads #4-7, ...)
As mentioned in the comments, this could be fixed by letting the original thread keep working on one of the subsections before waiting, i.e. changing this:
        std::thread lower(&quicksort::_sort<T>, this, data, p, q - 1);
        std::thread upper(&quicksort::_sort<T>, this, data, q + 1, r);
        lower.join();
        upper.join();

to:
        std::thread lower(&quicksort::_sort<T>, this, data, p, q - 1);
        _sort(data, q + 1, r);
        lower.join();

Another point to make is that it rarely benefits performance to have a number of threads much larger than the number of available (logical) cores. The final number is likely between #cores and 2 * #cores, but to find the best amount you'll likely need to measure.
Bug #3: No protection for concurrent calls
Since we're already in the multithreaded environment, what happens if a second thread calls quicksort::sort after the first thread, but before that call finished?
It resets thread_count, thus allowing for the creation of more threads than intended. Once they are finished, thread_count will be negative! This might cause problems if that case isn't expected.
Solution: Either don't reset thread_count, or wait until it reaches zero before starting to work on the next dataset.


Printing the start and end dates of the most recent eight weeks I have a table with columns dated by week that changes dynamically each day. Currently, I am using the following php code to get the proper dates for the past eight weeks:

$currentDate = date('Y-m-d');
$weekOneStart = strtotime("-56 day", strtotime($currentDate));
$weekOneStart = date("m/d/Y", $weekOneStart);
$weekOneEnd = strtotime("-49 day", strtotime($currentDate));
$weekOneEnd = date('m/d/Y', $weekOneEnd);
$weekTwoStart = strtotime("-49 day", strtotime($currentDate));
$weekTwoStart = date("m/d/Y", $weekTwoStart);
$weekTwoEnd = strtotime("-41 day", strtotime($currentDate));
$weekTwoEnd = date('m/d/Y', $weekTwoEnd);
$weekThreeStart = strtotime("-41 day", strtotime($currentDate));
$weekThreeStart = date("m/d/Y", $weekThreeStart);
$weekThreeEnd = strtotime("-34 day", strtotime($currentDate));
$weekThreeEnd = date('m/d/Y', $weekThreeEnd);
$weekFourStart = strtotime("-34 day", strtotime($currentDate));
$weekFourStart = date("m/d/Y", $weekFourStart);
$weekFourEnd = strtotime("-27 day", strtotime($currentDate));
$weekFourEnd = date('m/d/Y', $weekFourEnd);
$weekFiveStart = strtotime("-27 day", strtotime($currentDate));
$weekFiveStart = date("m/d/Y", $weekFiveStart);
$weekFiveEnd = strtotime("-20 day", strtotime($currentDate));
$weekFiveEnd = date('m/d/Y', $weekFiveEnd);
$weekSixStart = strtotime("-20 day", strtotime($currentDate));
$weekSixStart = date("m/d/Y", $weekSixStart);
$weekSixEnd = strtotime("-13 day", strtotime($currentDate));
$weekSixEnd = date('m/d/Y', $weekSixEnd);
$weekSevenStart = strtotime("-13 day", strtotime($currentDate));
$weekSevenStart = date("m/d/Y", $weekSevenStart);
$weekSevenEnd = strtotime("-6 day", strtotime($currentDate));
$weekSevenEnd = date('m/d/Y', $weekSevenEnd);
$weekEightStart = strtotime("-6 day", strtotime($currentDate));
$weekEightStart = date("m/d/Y", $weekEightStart);
$weekEightEnd = $currentDate;

and echoing the results in my table like so:

$weekOneStart - $weekOneEnd
$weekTwoStart - $weekTwoEnd
$weekThreeStart - $weekThreeEnd
$weekFourStart - $weekFourEnd
$weekFiveStart - $weekFiveEnd
$weekSixStart - $weekSixEnd
$weekSevenStart - $weekSevenEnd
$weekEightStart - $weekEightEnd

This works find and gives me the info I need, but I feel like there must be a simpler way to do this. I tried (with my variables, just posting the generic code here since I already deleted my version):

$date = '2009-12-06';
// End date
$end_date = '2020-12-31';

while (strtotime($date) <= strtotime($end_date)) {
            echo "$date\n";
            $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
 }

But this didn't work for me. Any other suggestions for cleaning/simplifying my code?

Don't Repeat Yourself

$currentDate = date('Y-m-d');
$weekOneStart = strtotime("-56 day", strtotime($currentDate));

You can simplify this by saying something like

$midnight = strtotime(date('Y-m-d'));
$weekOneStart = strtotime("-56 day", $midnight);

Then you don't have to keep taking strtotime of the same value.

$weekOneEnd = strtotime("-49 day", strtotime($currentDate));
$weekOneEnd = date('m/d/Y', $weekOneEnd);
$weekTwoStart = strtotime("-49 day", strtotime($currentDate));
$weekTwoStart = date("m/d/Y", $weekTwoStart);

Similarly, consider reducing this to

$weekOneEnd = strtotime("-49 day", $midnight);
$weekOneEnd = date('m/d/Y', $weekOneEnd);
$weekTwoStart = $weekOneEnd;

Now you only calculate that value once.

As a for loop

$weekMarkers = [];
$midnight = strtotime(date('Y-m-d'));

for ($current = strtotime('-8 weeks', $midnight); $current <= $midnight; $current = strtotime('+1 week', $current)) {
    $weekMarkers[] = date('m/d/Y', $current);
}

$previous = array_shift($weekMarkers);
foreach ($weekMarkers as $current) {
    echo "$previous - $current	\n";
    $previous = $current;
}

This is actually as two for loops.  You could mix them if you wanted, although many would argue that you are mixing calculation and display in that case.

$midnight = strtotime(date('Y-m-d'));

$current = date('m/d/Y', strtotime('-8 weeks', $midnight));
for ($next = strtotime('-7 weeks', $midnight); $next <= $midnight; $next = strtotime('+1 week', $next)) {
    echo "$current - ";
    $current = date('m/d/Y', $next);
    echo "$current	\n";
}

This would work, although the double use of $current is a bit odd.

I leave it up to you whether either of these is easier than your original code.


Normalizing data over a distribution I have a list X containg the data performed by different users N so the the number of the user is i=0,1,....,N-1. Each entry Xi has a different length.
I want to normalize the value of each user Xi over the global dataset X.

This is what I am doing. First of all I create a 1D list containing all the data, so:

tmp = list()
for i in range(0,len(X)):
    tmp.extend(X[i])

then I convert it to an array and I remove outliers and NaN.

A = np.array(tmp)
A = A[~np.isnan(A)] #remove NaN
tr = np.percentile(A,95)
A = A[A < tr]  #remove outliers

and then I create the histogram of this dataset

p, x = np.histogram(A, bins=10) # bin it into n = N/10 bins

finally I normalize the value of each users over the histogram I created, so:

Xn  = list()
for i in range(0,len(X)):
    tmp = np.array(X[i])
    tmp = tmp[tmp < tr]
    tmp = np.histogram(tmp, x)
    Xn.append(append(tmp[0]/sum(tmp[0]))

My data set is very large and this process could take a while. I am wondering if there is e a better way to do that or a package.

For large datasets, avoid converting between native Python list and NumPy array objects as much as possible.  Look at the np.loadtxt and np.genfromtxt functions.  They may help you go from saved files of your data to NumPy arrays without having to make any Python lists at all.  But suppose you do have a Python list.  You shouldn't have to convert all of the data to a NumPy array, and then later convert each users's data to an array separately.  I would try something like this, assuming that np.loadtxt doesn't work for you:

data_lengths = [len(Xi) for Xi in X]
num_users = len(X)
max_length = max(data_lengths)
all_data = np.zeros(shape = (num_users, max_length), dtype = 'int')

for row, Xi in enumerate(X):
    row_length = len(Xi)
    all_data[row, 0:row_length] = Xi

From then on, every operation on your data should be an operation on a NumPy array instead of on a Python list.  The way I wrote it, it assumes your data are integers, and also that 0 can never occur as a real data point.  You can modify the dtype and offset of the call to np.zeros accordingly to meet the requirements of your particular data.

This approach will only be good if each user has a number of data points that is not too different from the number for the other users.  Other wise representing your data as a full matrix will be memory inefficient.
Use dtypes.  If your data are non-negative integers, for example, then np.bincount() will be much faster than np.histogram, for example.  Actually, if your data are integers, then you could probably just use collections.Counter() to make your histograms in native Python, which could also save time.


Better way to determine date I am looking for best practice on how I may write this function more efficiently.

  var d = new Date(),
      time = d.getHours(),
      day  = d.getDay();

  if (day > 0 || day < 7){
    if (time > 18 || time < 8){
      // execute code

    }
  }

if ( (day > 0 || day < 7) && (t > 18 || t < 8))

Is this what you want?

Second condition will only be evaluated only if the first one is true. If the first one is true, then only the second one is evaluated.


Write to a CV::Mat from a higher scope within a function I've written this function to read matrices from the ROS parameter server.

void read_matrix_from_param(ros::NodeHandle nodehandle, std::string param, cv::Mat &cv_matrix) {
      // 1D vector for temporarily holding matrix elements
      std::vector<double> _data;

      // matrix dimensions
      int _rows, _cols;

      // read from param server
      nodehandle.getParam(param + std::string("/data"), _data);
      nodehandle.getParam(param + std::string("/rows"), _rows);
      nodehandle.getParam(param + std::string("/cols"), _cols);

      // create cv::Mat and copy into cv_matrix
      cv::Mat(_rows, _cols, CV_64F, _data.data()).copyTo(cv_matrix);
  }

called like this:

cv::Mat test_matrix1, test_matrix2;
read_matrix_from_param(private_nh, std::string("camera_matrix"), test_matrix1);
read_matrix_from_param(private_nh, std::string("distortion_coefficients"), test_matrix2);

This works as expected but my editor gives me a style-guide warning for the function signature.

runtime/references: Is this a non-const reference? If so, make const or use a pointer: cv::Mat &cv_matrix

So how should this be done? I have tried to do as the style-guide suggests but been unable to get working code again.

Assuming the matrix is always empty when passed into the function, the more normal way to write this would be to avoid any side effects and make cv_matrix the return type. i.e

cv::Mat read_matrix_from_param(ros::NodeHandle nodehandle, const std::string& param)
{
    cv::Mat cv_matrix

    ...
    return cv_matrix;
}

As a side point you should pass in param and possibly nodehandle, as const & to avoid unnecessary copies.

If this is not the case and you may pass in non-empty matrices ( I think that wouldn't make sense here ), then I would say using a non-const ref is fine. Alternative you could use a pointer i.e.

void read_matrix_from_param(ros::NodeHandle nodehandle, std::string param, cv::Mat *cv_matrix)
{
    ...

    cv::Mat(_rows, _cols, CV_64F, _data.data()).copyTo(*cv_matrix);
}

called as

read_matrix_from_param(private_nh, std::string("camera_matrix"), &test_matrix1);

Personally I wouldn't use a pointer and Core C++ Guidelines recommends a reference, but looks like it is the style recommended by google see https://softwareengineering.stackexchange.com/questions/299021/non-optional-pointers-vs-non-const-references-in-c . If you are using clang-tidy google style guidelines are an option in that I believe so that may be where this warning is coming from.


Replace every letter in the string with the letter following it in the alphabet and capitalize vowels Any suggestions to make this code better(efficient, style, etc.)?

I also want to know if there is a simpler way to check if a character is a vowel or not because I had to copy and paste "element == (vowel)" like 4 times.

int isVowel(char element)
{
    if(element == 'a'||element == 'e'||element == 'i'||element == 'o'||element == 'u')
    {
        return 0;
    }else
    {
        return 1;
    }
}
std::string LetterChanges(const std::string& str)
{
    std::vector<char>cstr(str.c_str(),str.c_str()+str.size());
    for(auto &element : cstr)
    {
        if((element >= 'A' && element < 'Z')||(element >='a' && element <   'z'))
        {
            element = element+1;

            if(isVowel(element)==0)
            {
                element = std::toupper(element);
            }

        }
    }
    std::string ss(cstr.begin(),cstr.end());
    return ss;
}

isVowel()

The first thing to say about isVowel() is that it should return bool, not int. And the logic should really be flipped so that it returns true or 1 if it's a vowel... not false or 0.

You are correct that there is a more elegant way to do a check like isVowel(). What you'd probably want to do is make an array of all the vowels, like this:

static constexpr auto vowels = std::array{ 'a', 'e', 'i', 'o', 'u' };

If you're not using C++17, you need to specify the type and size of the array:

static constexpr auto vowels = std::array<char, 5>{ 'a', 'e', 'i', 'o', 'u' };

Don't forget to #include <array>.

Then to find if a character is a vowel, you'd search for it in the array:

std::find(vowels.begin(), vowels.end(), element);

If the element is not in the array, std::find() will return the end iterator. So all you need to do is check for that. Putting it all together:

bool isVowel(char element) noexcept
{
    static constexpr auto vowels = std::array{ 'a', 'e', 'i', 'o', 'u' };

    return std::find(vowels.begin(), vowels.end(), element) != vowels.end();
}

Note that you can also make the function noexcept. And as of C++20, you can even make it constexpr, because std::find() is constexpr in C++20.

LetterChanges()

If your goal is efficiency, then it must be pointed out that you're doing a lot of unnecessary shuffling around of your data. You get a std::string argument, then you copy it all into a std::vector, then you copy it all again into another std::string.

There's no need for all those acrobatics. Rather than creating a vector, you can simply make cstr a std::string, and return that when you're done. Even better, rather than taking str as a std::string const&, just take it as a std::string and use that. You don't need to copy the data, not even once!

But let's go through the function line-by-line first.

std::vector<char>cstr(str.c_str(),str.c_str()+str.size());

You don't need to go through all that hassle to copy a string's characters into a vector. You can just do:

std::vector<char> cstr{str.begin(), str.end()};

In the loop:

if((element >= 'A' && element < 'Z')||(element >='a' && element <   'z'))

This is a bit of an advanced technical point, but... you are assuming that the letters A-Z and a-z have contiguous code points. (In other words, you are assuming that if you take 'A' and add 25, you'll get 'Z'.) That's true in the most common character sets these days (ASCII and Unicode), but it's not always true.

The correct thing to do would be to have a function that works similarly to isVowel(), with an array. Something like this:

char transformLetter(char c) noexcept
{
    static constexpr auto uppercase = std::array{ 'A', 'B', 'C', /* rest of the letters */ };
    static constexpr auto lowercase = std::array{ 'a', 'b', 'c', /* rest of the letters */ };

    auto const p_uppercase = std::find(uppercase.begin(), uppercase.end(), c);
    if (p_uppercase != uppercase.end())
    {
        // c is an uppercase letter... but is it Z?
        if (++p_uppercase == uppercase.end())
            return 'Z';
        return *p_uppercase;
    }

    // c wasn't uppercase, so check lowercase.
    auto const p_lowercase = std::find(lowercase.begin(), lowercase.end(), c);
    if (p_lowercase != lowercase.end())
    {
        // c is an lowercase letter... but is it z?
        if (++p_lowercase == lowercase.end())
            return 'z';

        // Not z, but is it a vowel?
        if (isVowel(*p_lowercase))
        {
            // Since we already have upper and lowercase arrays,
            // let's just use them to do toupper cheaply.
            return uppercase[std::distance(lowercase.begin(), p_lowercase)];
        }

        return *p_lowercase;
    }

    // Not a letter at all, so just return it unchanged.
    return c;
}

This could be simplified quite a bit, of course.

Anyway, if you don't care about exotic character encodings, and will only be using Unicode or ASCII, what you have is fine.

So if the function is simplified to remove the unnecessary copies, it should look something like:

std::string LetterChanges(std::string str)
{
    for (auto& element : str)
    {
        if ((element >= 'A' && element < 'Z') || (element >='a' && element <   'z'))
        {
            element = element+1;

            if (isVowel(element))
            {
                element = std::toupper(element);
            }
        }
    }

    return str;
}

Since you're not doing any copying or allocation at all, you can even make this function noexcept.

Summary

Use proper and consistent spacing and indentation. It makes code much easier to read and understand, and helps highlight errors.

Use bool for true/false, not int. And don't invert logic. This is not shell scripting; 0 is false, 1 is true.
Don't copy unnecessarily. Whenever someone complains that C++ is slow, at least 70% of the time it's because they're doing a lot of unnecessary copying. If you have a value that you want to modify, why copy it when you can modify it in place?


Calculating pair combinations in Java I have the following code to calculates and retrieves pair combinations:

public static void calculateCombinations(int x){
        int number = combination(x);
        int counter=0,y=0;
        while(counter<number){
            y++;
            for(int i=1; i<=x; i++){
                if(y!=i && !(i<y)){
                    System.out.println(y + " -- " + i);
                }
            }
            counter++;
        }
    }

    public static int combination(int n)
    {
        return permutation(n) / (permutation(2) * permutation(n - 2));
    }

    public static int permutation(int i)
    {
        if (i == 1)
        {
            return 1;
        }
        return i * permutation(i - 1);
    }

The code works well but it doesn't seem an efficient and professional code. One thing I am worried about here is the running cost of this code because it is part of a very huge project, which I care about the running time. What I want is to re-write it in a very professional way. I found lot of codes that do the same job I want but I only want to write my own code. I am asking you here to suggest how I can improve this function.

calculateCombinations

Your method does a lot of not required iterating. You only have to iterate up to x-1 in the outer loop as afterwards the if statement evaluates always to false (-> you can remove the number variable and thus the #combination call). The value of y and counter is the same on each iteration -> you can drop the counter variable.
The if statement can be removed by starting the inner loop at y+1.
The resulting code would look something like:

public static void calculateCombinations(int x) {
    int y = 0;
    while (y < x - 1) {
        y++;
        for (int i = y + 1; i <= x; i++) {
            System.out.println(y + " -- " + i);
        }
    }
}

This can be written with two for loops to keep the code shorter:

public static void calculateCombinations(int x) {
    for (int i = 1; i < x; i++)
        for (int j = i; ++j <= x;)
            System.out.println(i + " -- " + j);
}

Besides that I would rename the method as it prints the combinations, with the current name I would assume that the method returns the combinations.

permutation

This method is effectively a recursively written loop that returns the product of the range [1,i] (=factorial, thus method should be renamed to factorial) and thus can be replaced with:

public static int permutation(int i) {
    // should add check for negative values
    int p = 1;
    for (; i > 1; i--)
        p *= i;
    return p;
}

For O(1) performance you could precompute the factorials for values up to 12 (all factorials in int range).


Lattice traversal functional algorithm I have the following code which returned the solution I was wanting after running for 1 hour. Are there any techniques I could use to improve run-time?

If I were using imperative programming I would just more aggressively cache the partial results, but that's less appropriate for this approach.

How should I transform this function so it will have less run-time?

-module(pathing).
-export([paths/1]).

paths(N) -> paths(N, 0, 0, 0).

paths(0, 0, 0, 0) -> 1;
paths(0, _, _, _) -> 0;
paths(N, X, Y, Z) -> paths(N-1, X, Y+1, Z-1)
                 + paths(N-1, X, Y-1, Z+1)
                 + paths(N-1, X+1, Y, Z-1)
                 + paths(N-1, X-1, Y, Z+1)
                 + paths(N-1, X+1, Y-1, Z)
                 + paths(N-1, X-1, Y+1, Z).

The problem is to count the number of paths from a cell to itself in N steps on a hex grid.

p.s. This is a problem from a problem site. I would link to the problem but I don't wish for this to be a spoiler, and I am happy that this code is correct in terms of the actual problem definition.

Caching is definitely appropriate here as well. You can carry a cache through the calculations using an Erlang map.

I took your original code and made the following modifications:

Grouped the values into a tuple to make it easy to use them together as a cache key.
Changed the paths/4 function to paths/3, which takes as its arguments a list of value tuples, an accumulated sum, and a map for caching.
Made paths/3 recursively walk the list of value tuples passed to it. For each value tuple (the head of the list), it checks the cache and if the value tuple is present, it uses the cached sum, but if not found, it calculates the sum for that value tuple and caches it. When the list of value tuples is empty, the accumulated sum is returned.

Here's the revised code:

-module(pathing).
-export([paths/1]).

paths(N) ->
    {Result, _} = paths([{N, 0, 0, 0}], 0, #{}),
    Result.

paths([], Sum, C) ->
    {Sum, C};
paths([{0, 0, 0, 0}|T], Sum, C) ->
    paths(T, Sum+1, C);
paths([{0, _, _, _}|T], Sum, C) ->
    paths(T, Sum, C);
paths([{N, X, Y, Z}=Key|T], Sum, C) ->
    {NewSum,NC} = case maps:get(Key,C,undefined) of
                      undefined ->
                          {NS,NC0} = paths([{N-1, X, Y+1, Z-1},
                                            {N-1, X, Y-1, Z+1},
                                            {N-1, X+1, Y, Z-1},
                                            {N-1, X-1, Y, Z+1},
                                            {N-1, X+1, Y-1, Z},
                                            {N-1, X-1, Y+1, Z}],0,C),
                          {NS,maps:put(Key,NS,NC0)};
                      S ->
                          {S,C}
                  end,
    paths(T,Sum+NewSum,NC).

The final paths/3 clause is where all the interesting work occurs. It first calls maps:get/3 with a default value of undefined to check if the value tuple is present. If it's not, the default value of undefined is returned, and the case clause for undefined creates a new list of value tuples based on the incoming values of N, X, Y, and Z and passes it to a new paths/3 call, setting the initial sum value to 0 and passing the current cache. This call returns a 2-tuple comprising the sum for that value tuple and a new cache. The final line in this case clause inserts the sum for the value tuple into the cache.

The case clause for when maps:get/3 finds the value tuple just returns the cached sum and the current cache.

The result of the case expression is stored into {NewSum,NC}, and then paths/3 is called recursively with T, which is the tail of the value tuple list, a new sum which is Sum+NewSum, and the new cache NC.

We can run the code in a list comprehension to calculate the running times for N values from 1 to 12 using timer:tc/3 like this:

[begin pathing:paths(V), {V,timer:tc(pathing,paths,[V])} end || V <- lists:seq(1,12)].

We execute the pathing:paths/1 function twice for each value and time only the second call — this is a non-rigorous attempt to prime caches and such to get a more accurate measurement.

Some results for your original code are:

[{1,{3,0}},
 {2,{4,6}},
 {3,{16,12}},
 {4,{86,90}},
 {5,{522,360}},
 {6,{2320,2040}},
 {7,{10364,10080}},
 {8,{55940,54810}},
 {9,{339876,290640}},
 {10,{2099300,1588356}},
 {11,{12857019,8676360}},
 {12,{75383602,47977776}}]

The first value is N, the second value the execution time in microseconds, and the third value the result of the pathing:paths/1 function for that N value.

For N values greater than 2, there is roughly a 6x increase in run time at each step. For N of 12, the execution time is just over 75 seconds.

Here are the results for the revised code:

[{1,{4,0}},
 {2,{8,6}},
 {3,{43,12}},
 {4,{139,90}},
 {5,{229,360}},
 {6,{383,2040}},
 {7,{485,10080}},
 {8,{744,54810}},
 {9,{987,290640}},
 {10,{1351,1588356}},
 {11,{1549,8676360}},
 {12,{2683,47977776}}]

First, we can see that the third numbers, the function results, are identical to those for the original code. This means that either the new code is also correct, or both versions are incorrect.

As you can see the execution times are greatly reduced from the original code for all values of N greater than 4. For the smaller values, the original code is faster because it doesn't have any caching overhead. But for the larger values, each increment in N results in only a small increase in execution time. For N of 12, the run time is only 2.68ms, which is roughly 28000 times faster than the original code. With the revised code, calculating N=100 takes only 2.8 seconds:

{100,{2792880,1791977497884763591097926128926348807126038292969051375074848031878536740320}}


Calculate arithmetic expression represented by a string, in Java I saw that interview question online and gave it a try:
Implement a function that gets a string which represents an arithmethic expression: contains only numbers or the the operators: '+', '-', '*', '/'.
The function returns the calculation result of the arithmethic expression which appears in the string.
This need to be done in O(n) run-time (n is the length of the string) and O(1) extra space.

Example:

"1+2*3" -> 7

I succeed in implementing this (based mainly on some code I found online), but I just have one problem: logic is too complicated. So when reviewing the code, please tell me how can I simplify the logic here.

Here is the code:

class ArithmethicalStatments {

  private static int applyOperator(char operand, int firstNum, int secondNum){
      switch (operand) {
        case '*':
            firstNum *= secondNum;
            break;
        case '/':
            firstNum /= secondNum;
            break;
        case '+':
            firstNum = secondNum;
            break;
        case '-':
            firstNum = -secondNum;
            break;
      }

      return firstNum;
  }

  public static int calc(String str){
    int sum = 0;
    int lastApplyOpRes = 0;
    int lastNum = 0;
    int len = str.length();
    char prevOperator = '+';

    for (int i = 0; i < len; i++) {
        char c = str.charAt(i);

        if (c >= '0' && c <= '9') {
            lastNum = lastNum * 10 + c - '0';
            continue;
        }

        lastApplyOpRes = applyOperator(prevOperator, lastApplyOpRes , lastNum);
        lastNum = 0;

        switch (c) {
            case '+':
            case '-':
                sum += lastApplyOpRes;
                lastApplyOpRes = 0;
                break;
        }

        prevOperator = c;
    }

    lastApplyOpRes = applyOperator(prevOperator, lastApplyOpRes , lastNum);

    return sum + lastApplyOpRes;
  }
}

In case you want to modify the code- some Junit tests I wrote for your convenience:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class ArithmethicalStatmentsTest {

    @Test
    void testEmpty() {
        assertEquals(0, ArithmethicalStatments.calc(""));
    }

    @Test
    void testOneNum() {
        assertEquals(5, ArithmethicalStatments.calc("5"));
    }

    @Test
    void testPlus() {
        assertEquals(8, ArithmethicalStatments.calc("5+3"));
    }

    @Test
    void testMul() {
        assertEquals(15, ArithmethicalStatments.calc("5*3"));
    }

    @Test
    void testMulPlus() {
        assertEquals(8, ArithmethicalStatments.calc("2*3+2"));
    }

    @Test
    void testPlusMul() {
        assertEquals(11, ArithmethicalStatments.calc("2+3*3"));
    }

    @Test
    void testPlusMulPlus() {
        assertEquals(16, ArithmethicalStatments.calc("2+3+3*3+2"));
    }

    @Test
    void testMinus() {
        assertEquals(2, ArithmethicalStatments.calc("5-3"));
    }

    @Test
    void testDivision() {
        assertEquals(2, ArithmethicalStatments.calc("6/3"));
    }

    @Test
    void testMinusPlus() {
        assertEquals(1, ArithmethicalStatments.calc("2-3+2"));
    }

    @Test
    void testPlusDivide() {
        assertEquals(5, ArithmethicalStatments.calc("3+4/2"));
    }

    @Test
    void testAll() {
        assertEquals(6, ArithmethicalStatments.calc("2-3+3/3+2*3"));
    }

}

If your logic is too complicated, why don't you try to separate the concerns. You may create a Parser that will contains more well-named method to parse handle the characters in your stream. This one can either produce an object that is evaluated to produce your result or it can call methods on another class to evaluate each expression as soon as they are found (like the DOM vs SAX parsers).


Sort stack using two stacks My program is supposed to sort a stack such that the smallest item are on the top. I can use a temporary stack, but you may not copy the elements into any other data structure. The program works overall, but I was wondering if there is anything that I can do to make it more efficient/better.

import java.util.Stack;

public class SortStack {

    Stack sorted;

    public Stack sort(Stack unsorted) {

        int temp2 = 0; // to keep track of number of top of the sorted stack

        while(!unsorted.isEmpty()) {
            int temp1 = (int) unsorted.pop();

            if(sorted == null) { // if sorted stack is empty, create it and push the first number onto it
                sorted = new Stack();
                sorted.push(temp1);
                System.out.println(temp1 + " pushed from s1 to s2");
            } else if(temp1 >= temp2) { // push onto sorted stack if what is popped from original stack >= unsorted stack
                sorted.push(temp1);
                temp2 = temp1;
                System.out.println(temp1 + " pushed from s1 to s2");
            } else { // keep on popping from sorted stack to unsorted stack until unsortedTop < sortedTop
                    // this will make sure whatever is popped from original stack will not be less than the peek of the sorted stack
                while(temp1 < temp2) {
                    int sortedPop = (int) sorted.pop();
                    unsorted.push(sortedPop);
                    System.out.println(sortedPop + " pushed from s2 to s1");
                    temp2 = (int) sorted.peek();
                }
                sorted.push(temp1); // after both stacks are balanced, push element to sorted stack
                System.out.println(temp1 + " pushed from s1 to s2");
            }
        }
        return sorted;
    }

    public static void main(String[] args) {

        SortStack ss = new SortStack();

        Stack unsorted = new Stack();
        unsorted.push(7);
        unsorted.push(10);
        unsorted.push(5);
        unsorted.push(12);
        unsorted.push(8);
        unsorted.push(3);
        unsorted.push(1);

        ss.sort(unsorted);

    }

}

Generic type declaration

Your Stacks are missing the type parameter, i.e. in your case Stack<Integer>. This ensures they can only contain Integer objects.

static vs instance fields

sorted is the only instance field used in the only method of your SortStack class. This means it is likely better to exist as a method variable, and in turn your SortStack class becomes an utility class where sort(Stack<Integer> input) is now a static utility method.

Primitive unboxing

On a related note to the first point, you are implicitly relying on primitive unboxing, i.e. converting an Integer to int when you do this:

int temp1 = (int) unsorted.pop();

If a null is lurking in your unsorted object, this will fail.

Debugging and variable naming

If possible, use a logging framework like SLF4J so that you can configure when you want to see the debug statements, instead of simply printing via System.out.println(). Also:

" pushed from s1 to s2"

Isn't that meaningful - is s1 or s2 the sorted one? You may want to specify that. Same goes for temp1 and temp2, it's not possible to identify which contains the previous/current iterative element from the unsorted or sorted stack.

Future considerations

If you don't want to restrict to sorting Stack<Integer> objects, you can generic-fy it as such:

public static <T> sort(Stack<T> input) {
    // ...
}

However, you can't use your arithmetic-based comparison, so you can change that to:

public static <T extends Comparable<T>> sort(Stack<T> input) {
    // ...
    if (fromUnsorted.compareTo(fromSorted) >= 0) {
        // ...
    }
    // ...
}

And if you will like to make it even more 'generic' and cater for non-Comparable types, you can specify a Comparator<T> to do so:

public static <T> sort(Stack<T> input, Comparator<T> comparator) {
    // ...
    if (comparator.compare(fromUnsorted, fromSorted) >= 0) {
        // ...
    }
    // ...
}


Counting pending bookings using a subselect Can this query be improved? Is there a way to eliminate the duplicate function call?

-- a Special Group has many Items which have many Bookings
create function f_BookingsForSpecialGroup(@specialGroupId varchar(99))
returns table as
    return select * from v_Booking where ITEM_CODE in
        (select ITEM_CODE from v_Item i where i.SPECIAL_GROUP_ID = @specialGroupId);
go

create view v_SpecialGroup as
select (NON_BAD_BOOKINGS - PAID_BOOKINGS) as PENDING_BOOKINGS, * from
(
select
    (select count(*) from f_BookingsForSpecialGroup(g.SPECIAL_GROUP_ID) where IS_BAD=0) as NON_BAD_BOOKINGS,
    (select count(*) from f_BookingsForSpecialGroup(g.SPECIAL_GROUP_ID) where IS_PAID=1) as PAID_BOOKINGS,
    *
from SPECIAL_GROUP g
) g
go

The view v_SpecialGroup is never queried directly by the application; it is used to build other views which select individual columns as needed. (You can think of v_SpecialGroup as a "base view" which exists solely to augment the SPECIAL_GROUP table. I have profiled this strategy and it seems that if you don't select the more expensive columns you don't pay for them, but I could be wrong of course...)

Yes, there is a way to eliminate the repeated function call, but as to whether or not it would improve performance you need to benchmark it. This method writes the resulting data into a temporary table, which means it will call the function only once when populating it, but in turn will mean that it will have to write the table to tempdb for that session. Here is how it could be written that way:
if object_id('tempdb..#SpecialBookings') is not null
    drop table tempdb..#SpecialBookings;

select *
into #SpecialBookings
from SPECIAL_GROUP as grp
    cross apply f_BookingsForSpecialGroup(grp.SPECIAL_GROUP_ID) as bookings
where (bookings.IS_BAD = 0 or bookings.IS_PAID = 1);

select (grpCounted.NON_BAD_BOOKINGS - grpCounted.PAID_BOOKINGS) as PENDING_BOOKINGS, *
from
(
    select
        (select count(*) from #SpecialBookings where IS_BAD=0) as NON_BAD_BOOKINGS,
        (select count(*) from #SpecialBookings where IS_PAID=1) as PAID_BOOKINGS,
        *
    from #SpecialBookings
) as grpCounted;

if object_id('tempdb..#SpecialBookings') is not null
    drop table tempdb..#SpecialBookings;

select *
Do you really need all the fields from both the SPECIAL_GROUP table and the f_BookingsForSpecialGroup? If you do really need them, then fair enough, but otherwise, you should not use select *.

Aliases
As you can see I renamed your table aliases a bit to make your code easier to read. Whenever possible, avoid single-letter or otherwise meaningless aliases as they make the code less clear.


Am I wrapping synchronous code in ktor/Kotlin coroutine correctly? I'm playing with ktor right now. If I understand this correctly, code in ktor's handlers need to be as asynchronous as possible. So I was trying to figure out how do you write synchronous (potentially expensive) code in a Kotlin coroutine. Here's what I've written:

    install(DefaultHeaders)
    install(CallLogging) {
        level = Level.INFO
    }
    install(Routing) {
       // More routes here

        get("/hello") {
            val key = async {
                // Emulating an expensive synchronous call by doing lots of calculations
                log.info("Calculating key")
                var sb = StringBuilder(32)
                for (i in 0..15) {
                    var sym: Byte = (Math.random() * 255).toByte()
                    for(j in 1..10000000) {
                        sym = (sym + Math.random() * 40).toByte()
                    }
                    sb.append(String.format("%02X", sym))
                }

                log.info("Ended key calc")

                sb.toString()
            }

            call.respondText("Hello again! Your key is ${key.await()}", ContentType.Text.Plain)
        }
    }
}

I'm wrapping my potentially expensive synchronous code in async to get a Deferred and then await it.

I think it works because I'm able to do simultaneous request to /hello and other routes.

Is this the correct way to do it? Do I even need to bother, or can I write synchronous code in ktor handlers? Thank you.

To start, I don't know Ktor, so what I'm going to tell is when Ktor doesn't do additional stuff, which is highly probably not the case.

Coroutines are managers for threads: they tell threads what to do and when to do it.
When you write a coroutine, everytime you come across a suspend-keyword, the thread that executes the task asks the coroutine what to do next. The coroutine can tell to continue with the task he was working on, but it can also tell the thread to do something else.

This is great if you have to work with triggers:

A database-call
A with triggers the threads
Another thread that returns something.

Instead of waiting, the coroutine can tell the thread to do something else and once it comes across the suspend-keyword to continue with the task if the trigger is fulfilled.

In your code, you introduce a side-task by adding the async-keyword.
The next thing you do is telling to wait on this side-task.
This means that apart from adding a suspend-keyword, it does nothing.

So, a coroutine is not for computation, but for waiting. However, coroutines can manage more than one thread. Giving the side-task to a side-thread and waiting for that side-thread to finish is something that coroutines are great for.

Therefor, your code could be better written as:

/**
 * A dedicated context for sample "compute-intensive" tasks.
 */
val compute = newFixedThreadPoolContext(4, "compute")

get("/"){
    val key = withContext(compute){
       //heavy computation
    }
    call.respond(key.toString())
}

The expanded example can be found here: https://ktor.io/samples/feature/async.html


Remove duplicates from sorted array, in place I solved this problem:

  Given a sorted array nums, remove the duplicates in-place such that each element appears only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

How can I improve its execution time? Is there any better way of doing it?

public int removeDuplicates(int[] numbers) {
    if(numbers.length == 0 || numbers.length == 1) {
        return numbers.length;
    }
    int indexForUniqueElements = 0;
    for (int index = 0; index < numbers.length - 1; index++) {
        if (numbers[index] != numbers[index+1]) {
            numbers[indexForUniqueElements++] = numbers[index];
          }
    }
    numbers[indexForUniqueElements++] = numbers[numbers.length - 1];
    return indexForUniqueElements;
  }

I don't see any way to greatly increase performance since your algorithm is already O(n).

However, there is a minor optimization that can be made.  You should try to avoid having a calculation as the limit in a for loop.  This gets calculated on each iteration of the loop.  Initialize a variable with the result and use that instead:

public int removeDuplicates(int[] numbers) {
    if(numbers.length == 0 || numbers.length == 1) {
        return numbers.length;
    }
    int indexForUniqueElements = 0;
    int limit = numbers.length - 1;
    for (int index = 0; index < limit; index++) {
        if (numbers[index] != numbers[index+1]) {
            numbers[indexForUniqueElements++] = numbers[index];
          }
    }
    numbers[indexForUniqueElements++] = numbers[numbers.length - 1];
    return indexForUniqueElements;
  }


Method that fetches rows from database and maps it to a Model class with SqlDataReader So, I have a project that has a lot of methods that look alike.
In the below method as you can see it just fetches a bunch of rows from database and maps it to a model class.

In this  case ParkingSlipDetails class.
There are several such methods in the project that do the exact same thing only with a different set of data and a different model class. There is no complicated business logic here. Just pure fetching data and mapping it to a model.

Something like this:

  public CustomReturn < List < ParkingSlipDetails >> GetAllParkingSlips(int offset, int fetch)
    {
        SqlConnection sqlConnection = null;
        try
        {
            List < ParkingSlipDetails > ParkingSlipDetailsList = new List < ParkingSlipDetails > ();
            sqlConnection = (SqlConnection) sqlHelper.CreateConnection(); //sqlHelper is a nugget and not in my control
            sqlConnection.Open();
            SqlCommand cmd = new SqlCommand(Constants.GetAllParkingSlips, sqlConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value = gepservice.GetUserContext().UserId;
            cmd.Parameters.Add("@OffsetRows", SqlDbType.NVarChar).Value = offset;
            cmd.Parameters.Add("@FetchRows", SqlDbType.NVarChar).Value = fetch;
            var refCountdr = (RefCountingDataReader) sqlHelper.ExecuteReader(cmd);
            var sqlDr = (SqlDataReader) refCountdr.InnerReader;
            if (sqlDr != null)
            {
                while (sqlDr.Read())
                {
                    ParkingSlipDetails ParkingSlipDetails = new ParkingSlipDetails();
                    ParkingSlipDetails.Id = Convert.ToInt32(sqlDr[FileUploadConstants.Id]);
                    ParkingSlipDetails.FileName = Convert.ToString(sqlDr[FileUploadConstants.FileName]);
                    ParkingSlipDetails.FileUri = Convert.ToString(sqlDr[FileUploadConstants.FileURI]);
                    ParkingSlipDetails.PONumber = Convert.ToString(sqlDr[FileUploadConstants.DocumentNumber]);
                    ParkingSlipDetails.UploadDate = Convert.IsDBNull(sqlDr[FileUploadConstants.UploadDate]) ? DateTime.UtcNow: Convert.ToDateTime(sqlDr[FileUploadConstants.UploadDate]);
                    ParkingSlipDetails.FileStatus = (Status)(Convert.ToInt16(sqlDr[FileUploadConstants.FileStatus]));
                    ParkingSlipDetails.ReceiptNumber = Convert.ToString(sqlDr[FileUploadConstants.RecieptNumber].ToString());
                    ParkingSlipDetails.TotalRows = Convert.ToInt32(sqlDr[FileUploadConstants.TotalRows]);
                    ParkingSlipDetails.CreatedBy = gepservice.GetUserContext().UserId;
                    ParkingSlipDetailsList.Add(ParkingSlipDetails);
                }
            }
            return new CustomReturn < List < ParkingSlipDetails >> (ParkingSlipDetailsList);
        }
        catch(Exception ex)
        {
            LogError("ManageParkingSlipDataAccess", "GetAllParkingSlips", "Error", ex);
            return null
        }
        finally
        {
            if (sqlConnection != null)
            sqlConnection.Close();
        }
    }

I think there is a lot of scope for refactoring here since there is a lot of places where I would repeat :

sqlConnection = (SqlConnection)sqlHelper.CreateConnection();
sqlConnection.Open();

and

var refCountdr = (RefCountingDataReader) sqlHelper.ExecuteReader(cmd);

var sqlDr = (SqlDataReader) refCountdr.InnerReader;
if (sqlDr != null) {
  while (sqlDr.Read())
  {

  }
}

And I also think the looping is repetitive for each method. Each such method has a loop that maps object to cmd.Parameters

How do I separate the looping and mapping? Should I create an extension method on ParkingSlipDetails Class that takes SqlDataReader and returns an instance of ParkingSlipDetails? Even then I will be repeating reader creation and lopping in each model class.

To quote Mark Seemann from the following article Small methods are easy to troubleshoot

  Write small methods. How small? Small enough that any unhandled exception is easy to troubleshoot.

Break the function up into smaller more manageable chunks that focus on a single concern as much as possible.

Yes, the net effect of this is a lot of additional functions. They are however easier to read and maintain.

Another benefit of the many small methods it that it would help identify areas that can be generalized into reusable services that help reduce repeated code, allowing it (the code) to be more DRY (Don't Repeat Yourself)

Lets start with getting the connection

private SqlConnection createConnection() {
    var connection = (SqlConnection) sqlHelper.CreateConnection(); //sqlHelper is a nugget and not in my
    connection.Open();
    return connection;
}

This can now be wrapped in a using block that will dispose of the connection once out of scope

using(SqlConnection connection = createConnection()) {

    //...
}

While there is nothing wrong with creating the command manually, I personal prefer to let the connection do it rather than newing one. Again wrapping in a using block, and populating.

//...

using(SqlCommand command = connection.CreateCommand()) {
    command.CommandText = Constants.GetAllParkingSlips;
    command.CommandType = System.Data.CommandType.StoredProcedure;
    command.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value = gepservice.GetUserContext().UserId;
    command.Parameters.Add("@OffsetRows", SqlDbType.NVarChar).Value = offset;
    command.Parameters.Add("@FetchRows", SqlDbType.NVarChar).Value = fetch;

    //...
}

That could have also been refactored into its own method, but in my opinion it would have ended up with too many arguments.

This could be fixed with an aggregated value object.

public class ParkingSlipQuery {
    public int FetchRows { get; set; }
    public int OffsetRows  { get; set; }
    public string CreatedBy { get; set; }
}

for example

private SqlCommand createCommand(SqlConnection connection, ParkingSlipQuery query) {
    SqlCommand command = connection.CreateCommand();
    command.CommandText = Constants.GetAllParkingSlips;
    command.CommandType = System.Data.CommandType.StoredProcedure;
    command.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value = query.CreatedBy;
    command.Parameters.Add("@OffsetRows", SqlDbType.NVarChar).Value = query.OffsetRows;
    command.Parameters.Add("@FetchRows", SqlDbType.NVarChar).Value = query.FetchRows;
    return command;
}

and implemented

var query = new ParkingSlipQuery {
    CreatedBy = gepservice.GetUserContext().UserId,
    OffsetRows = offset,
    FetchRows = fetch
};
using(SqlCommand command = createCommand(connection, query)) {
    //...
}

The execution of the command and subsequent reader result however can be moved

private SqlDataReader executeReader(SqlCommand command) {
    var refCountdr = (RefCountingDataReader) sqlHelper.ExecuteReader(cmd);
    return (SqlDataReader) refCountdr.InnerReader;
}

As you have realized by now, you should know what is coming next about building the model to populate the collection.

private ParkingSlipDetails getDetails(SqlDataReader reader) {
    ParkingSlipDetails details = new ParkingSlipDetails();
    details.Id = Convert.ToInt32(reader[FileUploadConstants.Id]);
    details.FileName = Convert.ToString(reader[FileUploadConstants.FileName]);
    details.FileUri = Convert.ToString(reader[FileUploadConstants.FileURI]);
    details.PONumber = Convert.ToString(reader[FileUploadConstants.DocumentNumber]);
    details.UploadDate = Convert.IsDBNull(reader[FileUploadConstants.UploadDate]) ? DateTime.UtcNow: Convert.ToDateTime(reader[FileUploadConstants.UploadDate]);
    details.FileStatus = (Status)(Convert.ToInt16(reader[FileUploadConstants.FileStatus]));
    details.ReceiptNumber = Convert.ToString(reader[FileUploadConstants.RecieptNumber].ToString());
    details.TotalRows = Convert.ToInt32(reader[FileUploadConstants.TotalRows]);
    details.CreatedBy = gepservice.GetUserContext().UserId;

    return details;
}

private List<ParkingSlipDetails> getDetailsList(SqlDataReader reader, string userId) {
    List<ParkingSlipDetails> detailsList = new List<ParkingSlipDetails>();
    if (reader != null) {
        while (reader.Read()) {
            ParkingSlipDetails details = getDetails(reader);
            details.CreatedBy = userId;
            detailsList.Add(details);
        }
    }
    return detailsList;
}

Finally, to avoid null reference errors, the method should try to avoid return null. An empty collection is safer to check than a null collection.

This results in refactor looking like

public CustomReturn<List<ParkingSlipDetails>> GetAllParkingSlips(int offset, int fetch) {
    List<ParkingSlipDetails> detailsList = new List<ParkingSlipDetails>();
    try {
        using(SqlConnection connection = createConnection()) {
            string userId = gepservice.GetUserContext().UserId;
            var query = new ParkingSlipQuery {
                CreatedBy = userId,
                OffsetRows = offset,
                FetchRows = fetch
            };
            using(SqlCommand command = createCommand(collection, query)) {
                using(SqlDataReader reader = executeReader(command)) {
                    detailsList = getDetailsList(reader, userId);
                }
            }
        }
    } catch(Exception ex) {
        LogError("ManageParkingSlipDataAccess", "GetAllParkingSlips", "Error", ex);
        detailsList = new List<ParkingSlipDetails>();
    }
    return new CustomReturn<List<ParkingSlipDetails>>(detailsList);
}

The using block will handle the closing and disposal of the connect and the other disposable members.

If following explicit dependency principle, it could be refactored further down by delegating the argument values to the caller.

public CustomReturn<List<ParkingSlipDetails>> GetAllParkingSlips(ParkingSlipQuery query) {
    List<ParkingSlipDetails> detailsList = new List<ParkingSlipDetails>();
    try {
        using(SqlConnection connection = createConnection()) {
            using(SqlCommand command = createCommand(collection, query)) {
                using(SqlDataReader reader = executeReader(command)) {
                    detailsList = getDetailsList(reader, query.CreatedBy);
                }
            }
        }
    } catch(Exception ex) {
        LogError("ManageParkingSlipDataAccess", "GetAllParkingSlips", "Error", ex);
        detailsList = new List<ParkingSlipDetails>();
    }
    return new CustomReturn<List<ParkingSlipDetails>>(detailsList);
}

with the following support methods

private SqlConnection createConnection() {
    var connection = (SqlConnection) sqlHelper.CreateConnection(); //sqlHelper is a nugget and not in my
    connection.Open();
    return connection;
}

private SqlCommand createCommand(SqlConnection connection, ParkingSlipQuery query) {
    SqlCommand command = connection.CreateCommand();
    command.CommandText = Constants.GetAllParkingSlips;
    command.CommandType = System.Data.CommandType.StoredProcedure;
    command.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value = query.CreatedBy;
    command.Parameters.Add("@OffsetRows", SqlDbType.NVarChar).Value = query.OffsetRows;
    command.Parameters.Add("@FetchRows", SqlDbType.NVarChar).Value = query.FetchRows;
    return command;
}

private SqlDataReader executeReader(SqlCommand command) {
    var refCountdr = (RefCountingDataReader) sqlHelper.ExecuteReader(cmd);
    return (SqlDataReader) refCountdr.InnerReader;
}

private List<ParkingSlipDetails> getDetailsList(SqlDataReader reader, string userId) {
    List<ParkingSlipDetails> detailsList = new List<ParkingSlipDetails>();
    if (reader != null) {
        while (reader.Read()) {
            ParkingSlipDetails details = getDetails(reader);
            details.CreatedBy = userId;
            detailsList.Add(details);
        }
    }
    return detailsList;
}

private ParkingSlipDetails getDetails(SqlDataReader reader) {
    ParkingSlipDetails details = new ParkingSlipDetails();

    details.Id = Convert.ToInt32(reader[FileUploadConstants.Id]);
    details.FileName = Convert.ToString(reader[FileUploadConstants.FileName]);
    details.FileUri = Convert.ToString(reader[FileUploadConstants.FileURI]);
    details.PONumber = Convert.ToString(reader[FileUploadConstants.DocumentNumber]);
    details.UploadDate = Convert.IsDBNull(reader[FileUploadConstants.UploadDate]) ? DateTime.UtcNow: Convert.ToDateTime(reader[FileUploadConstants.UploadDate]);
    details.FileStatus = (Status)(Convert.ToInt16(reader[FileUploadConstants.FileStatus]));
    details.ReceiptNumber = Convert.ToString(reader[FileUploadConstants.RecieptNumber].ToString());
    details.TotalRows = Convert.ToInt32(reader[FileUploadConstants.TotalRows]);

    return details;
}


Button controls for a slider I've just finished building my slider and currently trying to refactor some redundant if/else code. Here is a preview of the code that I have built. Inside the event handlers. I have some if/else that are redundant, the purpose of the condition is to detect if slider reaches the last slide it will hide the next button and if the slider goes to the first slide. It will hide the previous button.

The slider I have built has no looping function.

    var programSliderContainer = $('.programs-slider'),
        programSliderWidth     = $('.programs-slider').outerWidth(),
        sliderContainer        = $('.programs-slider .view-programs-description .view-content'),
        totalSlides            = $('.programs-slider .view-programs-description .view-content .views-row').length,
        slides                 = $('.programs-slider .view-programs-description .view-content .views-row'),
        position               = 0,
        move                   = 0;

    //Reset Slider Position when resizing
    $('.programs-slider .view-programs-description .view-content').css({'right' : 0 });

    //Apply the Maximum width based on the total number of slides
    sliderContainer.width(programSliderWidth * totalSlides);

    //Apply the width of the slides based on the width of programSliderWidth
    slides.outerWidth(programSliderWidth);

    //Add Class active on Start
    $('.program-controls .program-list ul li:first-child').addClass('active');

    //Hide Previous Arrow on Start
    if( position == 0 ) {
        $('.arrow-controls .prev').hide();
    }

    $('.program-controls .program-list ul li').on('click', function() {
        position = $(this).index();
        $('.programs-slider .view-programs-description .view-content').css({'right' : position*programSliderWidth });

        $('.program-controls .program-list ul li.active').removeClass('active');
        $(this).addClass('active');

        //Show Hide Directional Navigation based on Slider Position
        if( position == 0 ) {
            $('.arrow-controls .prev').hide();
        }

        else {
            $('.arrow-controls .prev').show();
        }

        if( position == totalSlides - 1  ) {
            $('.arrow-controls .next').hide();
        }

        else {
            $('.arrow-controls .next').show();
        }

    });

    $('.arrow-controls .prev').on('click', function() {
        if( position > 0 ) {
            position--;
            $('.programs-slider .view-programs-description .view-content').css({'right' : position*programSliderWidth });

            $('.program-controls .program-list ul li.active').removeClass('active');
        }

        if( position == 0 ) {
            $('.arrow-controls .prev').hide();
        }

        else {
            $('.arrow-controls .prev').show();
        }

        if( position == totalSlides - 1 ) {
            $('.arrow-controls .next').hide();
        }

        else {
            $('.arrow-controls .next').show();
        }

    });

    $('.arrow-controls .next').on('click', function() {
        if( position < totalSlides - 1 ) {
            position++;
            $('.programs-slider .view-programs-description .view-content').css({'right' : position*programSliderWidth });

            $('.program-controls .program-list ul li.active').removeClass('active');
        }

        if( position == 0 ) {
            $('.arrow-controls .prev').hide();
        }

        else {
            $('.arrow-controls .prev').show();
        }

        if( position == totalSlides - 1 ) {
            $('.arrow-controls .next').hide();
        }

        else {
            $('.arrow-controls .next').show();
        }

    });

}

To reduce if/else you might want to introduce a separate function:

function showIf(selector, condition) {
    if (condition === true)
        $(selector).show();
    else
        $(selector).hide();
}

In this way you can simply write:

showIf('.arrow-controls .prev', position === 0);
showIf('.arrow-controls .next', position === totalSlides - 1);

Which, BTW, is what .toggle() does (see Jack's answer). I'd go a step further. You use those conditions extensively: I'd introduce some separate functions:

showIf('.arrow-controls .prev', isFirstSlideSelected());
showIf('.arrow-controls .next', isLastSlideSelected());

Note that if you want you might change showIf() to accept a function as condition and simply write:

showIf('.arrow-controls .prev', isFirstSlideSelected);

Few minor considerations.

You might want to use let instead of var, see also Is there any reason to use the “var” keyword in ES6?.

You may repeat var/let in each line, multiple declarations are sometimes misleading. Not to mention that when debugging and going through the code step by step that code you can inspect each declaration alone. See also Multiple var statements in JavaScript, not superfluous.

Do not align your declarations, visual appealing isn't important when you want to update one of those lines or you introduce a longer variable name...

If you reuse the same selector multiple times (you do) you should introduce a const for each of them, it will make changes in your HTML code much easier to handle.

Comments: your comments actually repeat what you already have in code, they do not explain anything more. Usually when I see/write this type of comments I feel I'm missing the opportunity to extract a separate function. Will you need to write "Show Hide Directional Navigation based on Slider Position" as comment if you call a function updateNavigationControlsVisibility()? Don't be afraid of reasonably long names, your minifier will do the job for you.


AsyncTask for subscribing to current location I made the below CurrentLocationTask as a way of reducing code duplication, as my application currently has 2 different Activitys which both use GoogleApiClient and OnLocationChanged.

The task basically connects a GoogleApiClient, then uses a LocationRequest to subscribe to RequestLocationUpdates.

In the OnLocationChanged method, CurrentLocation is updated, and the PropertyChanged event is fired.

Because the PropertyChangedEventHandler is public, it can be subscribed to from the required Activity. So the task can be called from an Activity on which we need to get the user's current location.

Usage

OnCreate

Create a new CurrentLocationTask and Execute, passing in the current activity as context. Then subscribe to the PropertyChanged event with OnCurrentLocationChanged:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);

    // Subscribe to current location
    _currentLocationTask = new CurrentLocationTask();
    _currentLocationTask.Execute(this);
    _currentLocationTask.PropertyChanged += OnCurrentLocationChanged;
}

OnCurrentLocationChanged

We can get CurrentLocation from the CurrentLocationTask:

private void OnCurrentLocationChanged(object sender, PropertyChangedEventArgs e)
{
    _currentLocation = _currentLocationTask.CurrentLocation;
}

OnStop

In OnStop, Disconnect the GoogleApiClient:

protected override void OnStop()
{
    base.OnStop();
    _currentLocationTask.Client.Disconnect();
}

The code for the AsyncTask is below. Any recommendations are much appreciated.

I'm not sure if using AsyncTask is the best way of doing this, especially since I don't think very much of the code here is actually being called in RunInBackground.

CurrentLocationTask

using System.Linq;
using System.ComponentModel;
using Android.OS;
using Android.Gms.Common.Apis;
using Android.Gms.Common;
using Android.Gms.Location;
using Android.Locations;
using Android.Content;

namespace WeatherApp.Droid
{
    class CurrentLocationTask :
        AsyncTask<Context, int, Java.Lang.Void>,
        GoogleApiClient.IConnectionCallbacks,
        GoogleApiClient.IOnConnectionFailedListener,
        Android.Gms.Location.ILocationListener,
        INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public Location CurrentLocation { get; set; }
        public GoogleApiClient Client;

        private LocationRequest _locationRequest;
        private Context _context;

        // AsyncTask
        protected override Java.Lang.Void RunInBackground(params
            Context[] context)
        {
            _context = context.First();
            // Build the client and connect
            Client =
                new GoogleApiClient.Builder(_context)
                .AddApi(LocationServices.API)
                .AddConnectionCallbacks(this)
                .Build();

            Client.Connect();

            return null;
        }

        // API connection
        public void OnConnected(Bundle connectionHint)
        {
            _locationRequest = LocationRequest.Create();
            _locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);
            _locationRequest.SetInterval(1000);

            LocationServices.FusedLocationApi.RequestLocationUpdates(
                Client, _locationRequest, this);
        }
        public void OnConnectionFailed(ConnectionResult result)
        {
            MessageBox.Show(_context, "Connection Failed");
        }
        public void OnConnectionSuspended(int cause)
        {
            MessageBox.Show(_context, "Connection Suspended");
        }

        // Location changed
        public void OnLocationChanged(Location location)
        {
            CurrentLocation = location;

            PropertyChanged?.Invoke(this,
                new PropertyChangedEventArgs("CurrentLocation"));
        }
    }
}

Note: the following refers to native Android development, I am not familiar with xamarin.

About the approach
It depends on what your aim is. The downside of using AsyncTask is that it's tied to the Activity you're calling it from. I recently implemented continuous location tracking, where AsyncTasks were not an option, as the process of continuous tracking is not tied to a particular activity. So I opted for an Android Service implementation instead. The Service was in it's own class and while you could bind to a service in a given Activity I instead went for the EventBus pattern (there are other options though) - meaning every time the LocationService obtained a new location it would cache the most recent location and propagate an Event via the Eventbus. This way subscribers (such as Activities) would be informed of location changes and could update the UI accordingly.

However, if you only want to query for locations within the Map Activity, then an AsyncTask may in fact be enough (I would put that in a separate class file though so it's reusable). The downside of this approach is, that since you only start asking for locations once you enter the Map view, it may take a moment to obtain a viable location.


Improving gzip function for huge files I have created a python system that runs Linux core files through the crash debugger with some python extensions. This all works fine but one bit of is problematic.

These files are send to the system in gzip format and consist of a single huge data file. The compressed file can often be as big as 20G. The unzipping works fine but is very slow and often uses huge amounts of memory. As an example, last night this processed a 14G gzip file and it took 9.2 hours to uncompress it (60G uncompressed) and the memory utilisation hovered between 30G, peaking at 60G.

Starting to think perhaps my code is the cause.

def chk_gzip_file(FILE):
    logger.info ("Will write uncompressed file to: "+ COREDIR)
    if os.path.isdir(FILE) == False:
        inF = gzip.open(FILE, 'rb')
        s = inF.read()
        inF.close()
        gzip_fname = os.path.basename(FILE)
        fname = gzip_fname[:-3]
        uncompressed_path = os.path.join(COREDIR, fname)
        open(uncompressed_path, 'w').write(s)
        uncompressedfile=COREDIR+"/"+fname
        return uncompressedfile
    else:
        logger.critical ("No gz file found  : " + FILE)
        sys.exit()

I am not a programmer so I imagine this is fairly poor code. Can this be improved for huge files? I know that speed will be an issue as gzip uncompress is single threaded.

inF = gzip.open(FILE, 'rb')
    s = inF.read()
    inF.close()

That reads the whole uncompressed data into memory. Of course it takes 60GB of memory.

Looking at the documentation for gzip, it has this example of compressing a file:

import gzip
import shutil
with open('file.txt', 'rb') as f_in, gzip.open('file.txt.gz', 'wb') as f_out:
    shutil.copyfileobj(f_in, f_out)

If you switch that round to:

import gzip
import shutil
with open('file.txt', 'wb') as f_out, gzip.open('file.txt.gz', 'rb') as f_in:
    shutil.copyfileobj(f_in, f_out)

then I think you'll find the memory usage is much lower.


Hiding odd rows in a table on click event Problem Statement

  Write JavaScript code hideOddRows.js that attaches to the button click event and hides the odd rows of the table when clicked.

Expected Output

When you click on "Hide Odd Rows":

Solution

/* hideOddRows.js */
document.querySelector('[name=HideRows]').onclick = hideRows;

function hideRows(){
    var elements = document.getElementsByClassName('hide');
    Array.prototype.forEach.call(elements, function(element){
        element.style.display = "none";
    });
    return true;
}
<!DOCTYPE html>

        <title>Hide odd rows</title>
        <meta charset="UTF-8">

                row 1

                row 2

                row 3

                row 4

                row 5

        <button type="button" name="HideRows">Hide Odd Rows</button>

Questions

I could not manage to place script tag in head due to DOM loading issue. How do I improve this solution to place script tag in head?
Can I improve the HTML code?

Note: I've yet to learn jQuery.

Your JavaScript is OK, but beware of writing code where you can get away without it.  In this case, there is a CSS3 :nth-child() selector1 that lets you select odd rows without tagging them manually with class="hide".

You just have to trigger the CSS rule when the button is pressed.  Note that if you want to select one specific element in a document, it's better to mark it using id="…" and select it using document.getElementById(…) — that's exactly what it is designed to do, and browsers will optimize ID lookups to be as fast as possible.

/* hideOddRows.js */
document.getElementById('HideRows').onclick = function hideRows() {
    document.getElementsByTagName('table').item(0).className = 'odd-rows-hidden';
}
table.odd-rows-hidden tr:nth-child(odd) {
    display: none;
}
<!DOCTYPE html>

        <title>Hide odd rows</title>
        <meta charset="UTF-8">

                row 1

                row 2

                row 3

                row 4

                row 5

        <button type="button" id="HideRows">Hide Odd Rows</button>

1 Caveat: requires Internet Explorer ≥ 9.  If you need to support older versions of IE, you'll probably want jQuery.


An algorithm that finds a sequence of numbers to fill a square grid Given numbers 1,2,3,4,5,6,7,8 I need them to replace the x's so that each side adds up to the number in the centre.

*-*---*-*
|x| x |x|
*-*---*-*
|x| 12|x|
*-*---*-*
|x| x |x|
*-*---*-*

As a start I have looped over the numbers to find all of the possible combinations.

var range = [1,2,3,4,5,6,7,8];
var target = 12;
var matches = [];

for (x = 0; x < range.length; x ++){
    for (y = 0; y < range.length; y ++){
        if (y === x){
            continue;
        }
        for (z = 0; z < range.length; z ++){
            if (z === y || z === x){
                continue;
            }

            if (range[x] + range[y] + range[z] === target){
              matches.push([range[x], range[y], range[z]]);
            }
        }
    }
}

Next I have joined the numbers together end to end

for (j=0; j < matches.length; j++){
  for (k=0; k < matches.length; k++){
    if (j==k) continue;
    //if (matches[j][2] != matches[k][0]) continue;
    for (l=0; l < matches.length; l++){
      if (l==j || l==k) continue;
      //if (matches[k][2] != matches[l][0]) continue;
      for (m=0; m < matches.length; m++){
        if (m==j || m==k || m==l) continue;
        if (matches[l][2] != matches[m][0]) continue;
        if (matches[m][2] != matches[j][0]){
          console.log(matches[j], matches[k], matches[l], matches[m]);
        }

      }
    }
  }
}

I have not currently put a check in to make sure each combination of numbers is only used once, which is how I would solve this.

I really would like to know an overall better approach to solving this problem.

A solved cube would look like:

*-*---*-*
|1| 5 |6|
*-*---*-*
|8| 12|4|
*-*---*-*
|3| 7 |2|
*-*---*-*

While not a complete solution for your first example, you can do two things to help speed it up.

If you don't have to use an array of values, don't just use a min and max (though the code doesn't change much if you do use a range).

You also don't need the third for-loop. Your third number will always be target - x - y. If target isn't in the range, you can throw it away.

You can further limit your for loop because you don't need to go all the way through every number in both loops. Since you know that you can't have 2 numbers which are both greater than half of your target, you can go through just the first part in the first loop, and the second loop through the second part. This lets you trim off a good bit from your for loops.

I also ended up using it as a string for this part, since it makes the check for uniqueness a bit easier, though it's not strictly necessary. My reduce also ensures no double digits.

const min = 1;
const max = 8;
const target = 12;

const allCombinations = (target, min, max) => {
  const combos = [];
  for (let i = min; i <= target / 2 && i <= max; i++) {
    for (let j = max; j >= target / 2 && i >= min && target - i - j >= min; j--) {
      combos.push([i, j, target - i - j].sort().join(''));
    }
  }

  return combos
    .reduce((result, a) => result.concat(!result.includes(a) && a[0] !== a[1] && a[1] !== a[2] && a[2] !== a[0] ? a : []), []);
};

console.log(allCombinations(target, min, max));


Use Newton's Method to compute sqrt(x) Given the following task:

  Use Newton's method to compute the square root of a number.  Newton's
  method involves successive approximation.  You start with a guess, and
  then continue averaging successive guesses until you reach a
  satisfactory level of precision.

I wrote the following (rough) solution in Scheme.  Can you help me make it better?

(define (abs x) ((if (< x 0) - +)  x))
(define (almost-equal x y delta)
    (> delta (abs (- x y))))

(define (sqrt-prime x last-x)
  (let ((next-x (/ (+ x last-x) 2)))
        (if (almost-equal next-x x 0.000001) x
            (sqrt-prime next-x x))))

(define (sqrt x) (sqrt-prime x 1))

The sqrt-prime function neither needs nor uses the last-guess argument.  It can be safely eliminated:

(define (sqrt-prime guess x)
  (if (good-enough? guess x) guess
      (sqrt-prime (better-guess guess x) x)))

You may call the function thus:

(define (sqrt x) (sqrt-prime 1.0 x))

I feel that, other than this minor change, your program is succinct enough.


Add JPanel to JFrame I just finished a fairly easy homework assignment. Basically all I had to do was add six JPanels that were just text boxes with color names as the text.  However, the color of the font had to match the text, so for instance, black had to be black, blue had to be blue, etc.  It works find but it just seems like I could have shortened this code up a little.

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.*;

public class SixLabels extends JFrame
{
    public SixLabels()
    {
        // sets layout of square
        setLayout(new GridLayout(2,3));

        //set JLabel properties such as border, text, font, etc.
        Border line =  new LineBorder(Color.yellow, 4);
        Font TNR = new Font("Times New Roman", Font.BOLD, 20);
        JLabel black = new JLabel("Black", JLabel.CENTER);
        JLabel blue = new JLabel("Blue", JLabel.CENTER);
        JLabel cyan = new JLabel("Cyan", JLabel.CENTER);
        JLabel green = new JLabel("Green", JLabel.CENTER);
        JLabel magenta = new JLabel("Magenta", JLabel.CENTER);
        JLabel orange = new JLabel("Orange", JLabel.CENTER);

        // add set properties and add panel to JFrame
        black.setOpaque(true);
        black.setForeground(Color.black);
        black.setBackground(Color.white);
        black.setBorder(line);
        black.setFont(TNR);
        black.setToolTipText("This is Black");
        add(black);

        // add set properties and add panel to JFrame
        blue.setOpaque(true);
        blue.setForeground(Color.blue);
        blue.setBackground(Color.white);
        blue.setBorder(line);
        blue.setFont(TNR);
        blue.setToolTipText("This is Blue");
        add(blue);

        // add set properties and add panel to JFrame
        cyan.setOpaque(true);
        cyan.setForeground(Color.cyan);
        cyan.setBackground(Color.white);
        cyan.setBorder(line);
        cyan.setFont(TNR);
        cyan.setToolTipText("This is Cyan");
        add(cyan);

        // add set properties and add panel to JFrame
        green.setOpaque(true);
        green.setForeground(Color.green);
        green.setBackground(Color.white);
        green.setBorder(line);
        green.setFont(TNR);
        green.setToolTipText("This is Green");
        add(green);

        // add set properties and add panel to JFrame
        magenta.setOpaque(true);
        magenta.setForeground(Color.magenta);
        magenta.setBackground(Color.white);
        magenta.setBorder(line);
        magenta.setFont(TNR);
        magenta.setToolTipText("This is Magenta");
        add(magenta);

        // add set properties and add panel to JFrame
        orange.setOpaque(true);
        orange.setForeground(Color.orange);
        orange.setBackground(Color.white);
        orange.setBorder(line);
        orange.setFont(TNR);
        orange.setToolTipText("This is Orange");
        add(orange);

    }

    public static void main(String[] args)
    {
        SixLabels frame = new SixLabels(); // creates frame
        frame.setTitle("Chapter 12 Exercise 12.8"); // sets name on title bar
        frame.setSize(600, 400); // sets size of GUI frame
        frame.setLocationRelativeTo(null); // Centers GUI frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Stop/Close program on exit
        frame.setVisible(true); // Shows the frame, must be true

    }

}

I would recommend:

You're using a GridLayout(2, 3) -- good.
I'd make it actually a GridLayout(2, 0, GAP, GAP) where GAP is a constant that allows a gap between JLabels. The first and second parameters mean 2 rows with variable number of columns.
Then if you set the background color of the JPanel to yellow, make the JLabels opaque and white, give the background JPanel an empty border of GAP width, you've got your yellow borders
Use an enum to combine a Color with a String. An alternative to this could be to use a Map<String, Color>
The enum method values() would then return an array of all the items of that enum. If using the Map such as a HashMap, you'd get the key set and iterate through this.
You could then use a simple for loop to iterate through this array, creating JLabels that have the desired text and the desired foreground colors.
You risk painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is often called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This can greatly increase the flexibility of your GUI coding.

For example,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class SixLabels2 extends JPanel {
   private static final int GAP = 8;
   private static final int LABEL_WIDTH = 160;
   private static final Dimension LABEL_SIZE = new Dimension(LABEL_WIDTH, LABEL_WIDTH);
   private static final int ROWS = 2;
   private static final Font LABEL_FONT = new Font("Times New Roman", Font.BOLD, 20);

   public SixLabels2() {
      setBackground(Color.yellow);
      setLayout(new GridLayout(ROWS, 0, GAP, GAP));
      setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));

      // iterate through all the items in the MyColor enum
      for (MyColor myColor : MyColor.values()) {
         JLabel label = new JLabel(myColor.getText(), SwingConstants.CENTER);
         label.setForeground(myColor.getColor());
         label.setPreferredSize(LABEL_SIZE);
         label.setFont(LABEL_FONT);
         label.setOpaque(true);
         label.setBackground(Color.white);
         add(label);
      }
   }

   private static void createAndShowGui() {
      SixLabels2 mainPanel = new SixLabels2();

      JFrame frame = new JFrame("SixLabels2");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

enum MyColor {
   BLACK("Black", Color.black), BLUE("Blue", Color.blue),
   CYAN("Cyan", Color.cyan), GREEN("Green", Color.green),
   MAGENTA("Magenta", Color.magenta), ORANGE("Orange", Color.orange);

   private String text;
   private Color color;

   private MyColor(String text, Color color) {
      this.text = text;
      this.color = color;
   }

   public String getText() {
      return text;
   }

   public Color getColor() {
      return color;
   }

   @Override
   public String toString() {
      return text;
   }

}

Note, my code may not be actually shorter in length, but I'll wager that it's easier to debug and enhance. Consider adding a few more colors to your code and mine and see how many variables and statements need to be changed in both. For instance, if you wanted to add two more colors to mine, all you'd need to do would be to change the enum to include two more colors. The program would take care of the rest for you.

For example, if you changed the enum from this:

enum MyColor {
   BLACK("Black", Color.black), BLUE("Blue", Color.blue),
   CYAN("Cyan", Color.cyan), GREEN("Green", Color.green),
   MAGENTA("Magenta", Color.magenta), ORANGE("Orange", Color.orange);

to this:

enum MyColor {
   BLACK("Black", Color.black), BLUE("Blue", Color.blue),
   CYAN("Cyan", Color.cyan), GREEN("Green", Color.green),
   MAGENTA("Magenta", Color.magenta), ORANGE("Orange", Color.orange),
   GRAY("Gray", Color.gray), FOO("Foo", new Color(100, 250, 255));

The GUI would then automatcially accommodate the additional colors.


Implementing `sequenceA` I implemented sequenceA:

sequenceA :: Applicative f => [f a] -> f [a]
sequenceA []     = pure []
sequenceA (x:xs) = (++) <$> (fmap (\y -> [y]) x) <*> sequenceA xs

I don't like the fact that I'm making a new list, and then concatenating the result via ++.

However, I'm not sure how to make use of cons, i.e. :, in this function.

Please critique it.

The trick is to lift (:) into the applicative functor using pure thus getting an expression of type f (a -> [a] -> [a]) which you can then apply using (<*>). This gives you: sequenceA (x:xs) = pure (:) <*> x <*> sequenceA xs.

Now, the pattern pure ... <*> is equivalent to ... <$> so we can rewrite the previous equation to the shorter solution:

sequenceA (x:xs) = (:) <$> x <*> sequenceA xs

This pattern can be generalized to other datatypes: see the traversable typeclass. sequenceA can always be implemented for datatypes* by using pure to lift constructors and (<*>) to apply arguments (or induction hypotheses).

* strictly positive ones, at least