Standard deviation in one pass in C++ (See the next iteration.)

I have this template function for computing standard deviation in one pass over the data. The underlying math is here.

coderodde_sd.h:

#ifndef CODERODDE_SD_H
#define CODERODDE_SD_H

#include <cmath>
#include <iterator>
#include <sstream>
#include <stdexcept>

namespace net {

    namespace coderodde {

        namespace stat {

            template<class Iter>
            double sd(Iter begin, Iter end)
            {
                typedef typename std::iterator_traits<Iter>::difference_type
                                                             difference_type;

                difference_type distance = std::distance(begin, end);

                if (distance < 2)
                {
                    std::stringstream ss;
                    ss << "The standard deviation cannot be computed for "
                          "less than two elements. The input sequence has "
                       << distance
                       << " elements.";

                    throw std::runtime_error(ss.str());
                }

                typedef typename std::iterator_traits<Iter>::value_type
                                                             value_type;

                double x = 0.0;
                double x_squared = 0.0;

                for (Iter it = begin; it != end; ++it)
                {
                    x += *it;
                    x_squared += (*it) * (*it);
                }

                return std::sqrt((x_squared - (x * x) / distance) /
                                 (distance - 1)
                                );
            }

        } /* net::coderodde::stat */

    } /* net::coderodde */

} /* net */

#endif  /* CODERODDE_SD_H */

main.cpp:

#include <iostream>
#include <list>
#include "coderodde_sd.h"

using net::coderodde::stat::sd;
using std::cout;
using std::list;

int main(int argc, char** argv) {
    double bad_array[]{1.0};

    try
    {
        sd(bad_array, bad_array);
    }
    catch (std::runtime_error& error)
    {
        cout << "ERROR: " << error.what() << "\n";
    }

    try
    {
        sd(bad_array, bad_array + 1);
    }
    catch (std::runtime_error& error)
    {
        cout << "ERROR: " << error.what() << "\n";
    }

    std::list<int> my_list = { 1, 5, 2, 4, 3 };

    cout << "Standard deviation: "
         << sd(my_list.begin(), my_list.end())
         << "\n";

    return 0;
}

I tried hard to write idiomatic C++; please tell me where I failed to reach that ideal + any other critique.

In your example, you have two passes over the list - std::distance works in linear time unless it operates on random access iterators.

A naive way to fix this is to calculate the number of elements inside your for loop and check it afterwards. But this way you'll pessimize performance for random access iterators.

To avoid this pessimization you can examine the iterator_tag of you Iter at compile time and decide whether you're going to call std::distance or count your elements in a loop.

The simplest way to do this is in C++14 is by tag dispatching. Here is an example:

template<typename RandomAccessIter>
double sd(RandomAccessIter begin, RandomAccessIter end, std::random_access_iterator_tag)
{
    // calling std::distance
}

template<typename ForwardIter>
double sd(ForwardIter begin, ForwardIter end, std::forward_iterator_tag)
{
    // counting elements in a loop
}

template<typename Iter>
double sd(Iter begin, Iter end)
{
    // dispatching
    return sd(begin, end, typename std::iterator_traits<Iter>::iterator_category());
}

This code will corretly handle bidirectional iterators as well since std::bidirectional_iterator_tag inherits from std::forward_iterator_tag. And code duplication can be avoided by incaplusating common logic in separate functions.

Viola! Now you have only one pass.


"Spinning wheels" - Animated spinner with SVG and Sass-CSS I've made this little animation using SVG, CSS-keyframes and a bit of Sass.

html,
body,
.wrap {
  height: 100%;
}
body {
  background-color: #efefef;
}
svg {
  max-width: 500px;
  width: 100%;
  height: 400px;
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
}
circle {
  position: absolute;
  fill: transparent;
  stroke-width: 50px;
  stroke-dasharray: 31.5;
  transform-origin: 50% 50%;
}
circle:nth-of-type(1) {
  stroke: #00aadd;
  z-index: 0;
  animation: turn-circle 5s linear infinite;
}
circle:nth-of-type(2) {
  stroke: #00ab02;
  z-index: 1;
  animation: turn-circle 4s linear infinite;
}
circle:nth-of-type(3) {
  stroke: #0053FF;
  z-index: 2;
  animation: turn-circle 3s linear infinite;
}
@keyframes turn-circle {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
<div class="wrap">
  <?xml version="1.0" encoding="utf-8" ?>
  <svg version="1.1">
    <circle cx="150" cy="150" r="100" />
    <circle cx="330" cy="150" r="100" />
    <circle cx="240" cy="220" r="100" />
  </svg>
</div>

Here's the uncompiled Sass-code:

$backgroundColor:#efefef;
 $blue:#0053FF;
 $green:#00ab02;
 $cyan:#00aadd;
@mixin setCircle ($i:1,
$color:blue,
$zIndex:0,
$animationDuration:3s) {
  circle: nth-of-type(# {
    $i
  }
  ) {
    stroke: $color;
    z-index: $zIndex;
    animation: turn-circle # {
      $animationDuration
    }
    s linear infinite;
  }
}
html,
body,
.wrap {
  height: 100%;
}
body {
  background-color: $backgroundColor;
}
svg {
  max-width: 500px;
  width: 100%;
  height: 400px;
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
}
circle {
  position: absolute;
  fill: transparent;
  stroke-width: 50px;
  stroke-dasharray: 31.5;
  transform-origin: 50% 50%;
}
@include setCircle (1,
$cyan,
0,
5);
@include setCircle (2,
$green,
1,
4);
@include setCircle (3,
$blue,
2,
3);
@keyframes turn-circle {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

It works and looks alright but could surely be improved.

Therefore my questions:

Could one improve the movement of the wheels in a way that it looks better? More natural?
How could the responsiveness be improved? Currently my main issue.
Suggestions concerning the design? I'm an amateur in that field.

I totaly refactored your code (CSS and HTML). I wont address all the modifications and focus on the main ones :

Browser support

The is an issue in firefox when a percentage transform-origin is set on an svg element.If you test your code in FFF, you will see the circles are rotating around the center of the SVG element.
To get around this, I put each circle in a seperate svg element and set the rotation on the svg element itself.

Horizontal and vertical centering

I changed the absolute positionning technique to flexbox as it is more flexible espacialy as you plan on making this responsive.

Responsive

I use the first svg to set the size of the .wrap element. The other ones adapt their size according to the first one with absolute positioning and percentage sizes.

I also tidied up the code

remove positioning on the circle element
removed the z-index as the layers are set with the order of the elements in the markup

html, body { height: 100%; }
body {
  display:flex;
  justify-content:center;
  align-items:center;
  background-color: #efefef;
}
.wrap{
  position:relative;
  width:100%;
  max-width:500px;
  overflow:hidden;
  padding:10% 0;
 }
svg {
  width: 70%;
  height:auto;
  position: absolute;
  animation: turn-circle 5s linear infinite;
  fill: transparent;
  stroke-width: 50;
  stroke-dasharray: 31.5;
}
svg:nth-of-type(1){
  position:static;
  margin:-15% 0 0 -5%;
  stroke: #00aadd;
}
svg:nth-of-type(2) {
  stroke: #00ab02;
  top:0;
  right:-5%;
  animation-duration:4s;
}
svg:nth-of-type(3) {
  stroke: #0053FF;
  top:20%; left:15%;
  animation-duration:3s;
}
@keyframes turn-circle {
  to { -weblit-transform: rotate3d(0,0,1,360deg);
               transform: rotate3d(0,0,1,360deg);}
}
<div class="wrap">
  <svg version="1.1" viewbox="0 0 300 300">
    <circle cx="150" cy="150" r="100" />
  </svg>
  <svg version="1.1" viewbox="0 0 300 300">
    <circle cx="150" cy="150" r="100" />
  </svg>
  <svg version="1.1" viewbox="0 0 300 300">
    <circle cx="150" cy="150" r="100" />
  </svg>
</div>


Implementing a File class I studying for a Python course. I was finished working on task about creating a file class which can read, write lines to file. This class also needs magic methods (__add__, __str__, __iter__, __next__)

Can you review code and give a tip about improvements using the DRY principle (for example, how avoid to copy/paste with open).

from tempfile import gettempdir
from os import path
import uuid

class File():

    def __init__(self, file_path):
       self.file_path = file_path
       self.current_position = 0

    def __iter__(self):
       return self

    def __next__(self):
        try:
            with open(self.file_path, 'r') as f:
                f.seek(self.current_position)
                line = f.readline().strip()
                if not line:
                    self.current_position = 0
                    raise StopIteration('EOF')
                self.current_position = f.tell()
                return line
        except IOError:
            return 'Cant read next line'

    def write(self, line):
        try:
            with open(self.file_path, 'w') as file_obj:
                file_obj.write(line)
        except IOError as err:
            return 'Cant write to file: {0}'.format(err)

    def read(self):
        try:
            with open(self.file_path, 'r') as file_obj:
                return file_obj.read().strip()
        except IOError:
            return ''

    def __add__(self, obj):
        if isinstance(obj, File):
            tmp_file_path = path.join(gettempdir(), str(uuid.uuid4()))
            tmp_file = File(tmp_file_path)
            tmp_file.write(self.read() + '\n' + obj.read())
            return tmp_file

    def __str__(self):
        return self.file_path

f_obj = File('somepath1')
f_obj2 = File('somepath2')
f_obj3 = f_obj + f_obj2
for line in f_obj:
    print(line)
print(f_obj3.read())

__iter__ and __next__
Instead of re-opening the file each time and seeking to the previous position, you could write __iter__ as a generator function by using yield instead of needing to define __next__. Something like:
def __iter__(self):
    with open(file) as f:
        yield from f

Error handling
In case of error, you're returning a string 'Cant read next line'.
What if you have a file that contains text 'Cant read next line'? How do you differentiate between these?
Well... you can't.
Instead of returning a string, throw an exception on error - that's what exceptions are meant for.
Also, this error message of yours isn't really correct. You most likely get IOError when opening a file... before even attempting to read first line from it.
It seems like your error handling code doesn't really do anything very useful with the IOError. So why not instead just let it bubble upwards and drop the try-catch block completely.
Duplication
You can define your own open method, so you don't have to repeat passing the file_path to it:
def open(self, mode='r'):
    open(self.file_path, mode)

You could also investigate writing you own context manager for opening the file with your custom error handling.


Grouping an array by postId and userId I've got this kind of data:

[
  { userId: 1, postId: 1, ... },
  { userId: 1, postId: 2, ... },
  { userId: 2, postId: 3, ... },
  ...
]

And I need to group them by postId and userId like this:

{
  1: { 1: [{ ... }], 2: [{ ... }], ...},
  2: { 3: [{ ... }], ...},
  ...
}

Here is what I've done so far:

var posts = {};
_.forEach(response.posts, function (post) {
    if (!posts[post.userId]) {
        posts[post.userId] = {};
    }
    if (!posts[post.userId][post.postId]) {
        posts[post.userId][post.postId] = [];
    }

    posts[post.userId][post.postId].push(post);
});

Any suggestion is welcome (a functional way of doing it using lodash would be great)!

As you noted, a more functional solution could be better.  Here is a way to write it using lodash.js:

var data = [
             { userId: 1, postId: 1, postData: "first post first user" },
             { userId: 1, postId: 2, postData: "second post first user" },
             { userId: 2, postId: 3, postData: "first post second user" }
           ];

posts = _
  .chain(data)
  .groupBy('userId')
  .mapValues(function(x) {
    return _.extend.apply(this, _.map(x, function (y) {
      return {[y.postId]: y.postData };
    }));
  })
  .value();

The .groupBy('userId') makes the intention clearer, but whether it is a better solution than the original is debatable.  Therefore, you may be better off leaving the code as it is.


User-defined function for estimating coefficient of performance I am using hourly weather data to estimate performance of commercial refrigeration systems under various parameters. The formula for estimating co-efficient of performance (COP) of these systems is repeated for each hour.

I defined a formula to do this calculation:

Public Function estimatedCOP(dischargeTemp As Double, suctionTemp As Double)

Dim a As Double, b As Double, c As Double, d As Double, e As Double, f As Double

a = 9.12808037
b = 0.15059952
c = 0.00043975
d = -0.09029313
e = 0.00024061
f = -0.00099278

estimatedCOP = a + b * suctionTemp + c * suctionTemp * suctionTemp + _
    d * dischargeTemp + e * dischargeTemp * dischargeTemp + _
    f * suctionTemp * dischargeTemp

End Function

The function is repeated twice in a column with 8,760 rows (each hour of the year).

This gives the expected result, but takes a long time to calculate and update (as it's repeated ~17,000 times). How can I improve the performance of this function?

You are using the Double data type for all variables. I think this is a good thing since it should avoid conversions. But it is probably too large for your purpose.

From the doc:

  Double (double-precision floating-point)  8 bytes
    -1.79769313486231E308 to -4.94065645841247E-324 for negative values

Source: Data type summary

Perhaps using the single data type is enough ?

  Single (single-precision floating-point)  4 bytes     -3.402823E38 to
  -1.401298E-45 for negative values
  1.401298E-45 to 3.402823E38 for positive values

You can try and compare results.

For reference a response I made on another topic, that includes comments about data types: Adding qty If duplicates keys found VBA

It is unfortunate that the variable names (a..f) are not descriptive and
meaningful. That makes logic errors harder to spot since the variables look  all the same. Surely all those values have a meaning ?

Something else: if you are doing calculations in a loop and assuming this is in Excel, you can suspend automatic recalculation at the beginning of your procedure:

Application.Calculation = xlManual

Turn it back on when you are done:

Application.Calculation = xlAutomatic


Making a datatable, maybe also with column filters In case the table has class 'trans' - run dataTable() and columnFilter(); otherwise  just run dataTable().

Is it possible to not repeat the dataTable() part?

$('#dt_a').each(function() {
        if ($(this).hasClass('trans')) {
            $(this).dataTable({
                "sDom": "<'row'<'span6'<'dt_actions'>l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
                "sPaginationType": "bootstrap_alt",
                "oLanguage": {
                    "sLengthMenu": "_MENU_ records per page"
                }
            })
            .columnFilter({
                sPlaceHolder: "head:before",
                aoColumns: [
                    null,
                    { type: "text" },
                    { type: "text" },
                    { type: "date-range" },
                    { type: "text" },
                    { type: "text" },
                    null,
                    null
                ]
            });
        } else {
            $(this).dataTable({
                "sDom": "<'row'<'span6'<'dt_actions'>l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
                "sPaginationType": "bootstrap_alt",
                "oLanguage": {
                    "sLengthMenu": "_MENU_ records per page"
                }
            });
        }
    });

NOTE:

columnFilter is an extension to dataTable.

Use some variables. You don't need to chain function calls; it's just a neat thing you can do when appropriate, but it's not required.

$('#dt_a').each(function() {
  var target = $(this), // store this
      table  = target.dataTable({  // and store this too
          "sDom": "<'row'<'span6'<'dt_actions'>l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
          "sPaginationType": "bootstrap_alt",
          "oLanguage": {
              "sLengthMenu": "_MENU_ records per page"
          }
      });

    if (target.hasClass('trans')) {
        table.columnFilter({
            sPlaceHolder: "head:before",
            aoColumns: [
                null,
                { type: "text" },
                { type: "text" },
                { type: "date-range" },
                { type: "text" },
                { type: "text" },
                null,
                null
            ]
        });
    }
});


I made a Python program to calculate price based on Inflation Rate I made a program that calculates how much the value of money has decreased due to inflation over a given set of years.
This takes a principal amount, a start and end date, and with the help of the inflation rate, it calculates how much the value of money is at the current year. (compound)

The data from the CSV file is taken from here

Here is the code-

import pandas

startDate = input("Enter the starting year ")
endDate = input("Enter the ending year ")

principal_amt = float(input("Enter the amount "))
startDate = startDate + "-12-" + "31"
endDate = endDate + "-12-" + "31"
final_amt = 0

print(startDate, endDate)

df = pandas.read_csv("india-cpi.csv")
df = df.set_index("date")

inflation_list = list(df.loc[startDate:endDate, " inflation-rate"])
print(inflation_list)

list_for_p = []
for item in inflation_list:
    principal_amt += principal_amt*(item/100)
    list_for_p.append(principal_amt)

print(list_for_p)
final_amt += principal_amt
print(final_amt)

This is how the output looks like:
Enter the starting year 2016
Enter the ending year 2018
Enter the amount 100
2016-12-31 2018-12-31
[4.941, 2.4909, 4.8607]
[104.941, 107.554975369, 112.782900056761]
112.782900056761

I want to ask if my calculation is correct and if you have any other suggestion then please give me. It will be helpful. Thanks!

The code itself is clear enough, the spacing and variable naming is ok. You mix camelCase and snake_case. Stick to one. PEP-8 advises snake_case for function and variable names and PascalCase for classes.

Only the abbreviation of principal_amt is rather futile. Just use the full name, it costs 3 characters more.

functions

You better separate your program in functions:

read the data
get the user input
calculate the difference
present the results

That way your program is easier to understand, test and reuse

My rule of thumb on where to split the program, is on where data is exchanged from one part to another.

testing

Now you've separated your program in logical blocks, you can start testing the individual parts.

read the data

Make a small function, passing in the filename as argument and getting the index per year back. Here you can use pandas further. For starters, you can make the year a pandas.Period, so you can index the year immediately. Then you can do the division by 100 already. Since pandas has a nice cumprod function, you can already add 1 to each index

def get_inflation_rate(
    filename: typing.Union[Path, str, typing.IO]
) -> pd.Series:
    """Read the inflation data from `filename`"""
    inflation_rate = (
        pd.read_csv(
            data_file,
            skiprows=16,
            parse_dates=["date"],
            usecols=[0, 1],
            index_col=0,
        )
        .div(100)
        .add(1)
        .rename(columns={" Inflation Rate (%)": "inflation_rate"})
    )["inflation_rate"]
    inflation_rate.index = inflation_rate.index.to_period()
    return inflation_rate

This returns a Series with the year as index and the inflation ratio as value

date
1960    1.017799
1961    1.016952
1962    1.036322
1963    1.029462
...
2014    1.063532
2015    1.058724
2016    1.049410
2017    1.024909
2018    1.048607
Freq: A-DEC, Name: inflation_rate, dtype: float64

I included a docstring and typing information so the user of this function and his IDE can know what to expect.

user input

If your user returns some nonsense, your program will do little useful. Better to warn the user as clearly as possible. You can define a function like this:

def get_input(
    *, message, possible_values: typing.Optional[typing.Collection[str]] = None
) -> str:
    while True:
        value = input(message)
        if possible_values is None or value in possible_values:
            return value
        print("Not one of the possibilities")

A more generic one, which can also convert to float if needed can look like this:

T = typing.TypeVar("T")

def get_input(
    *,
    message: str,
    possible_values: typing.Optional[typing.Collection[T]] = None,
    converter: typing.Optional[typing.Callable[[str], T]] = None,
) -> typing.Union[T, str]:
    """Get and convert the user input.

    Tries to call `converter` on the input value.
    If this raises a `ValueError`, asks again.

    If `possible_values` is defined, checks whether the returned value is in
    this collection. If it is not, asks again.

    Args:
        message (str): The message to present to the user.
        possible_values (typing.Collection[T], optional):
            A selection which must contain the user input. Defaults to None.
        converter (typing.Callable[[str], T], optional):
            A function to try to convert the user input. Defaults to None.

    Returns:
        typing.Union[T, str]: The converted user input.

    """
    while True:
        value = input(message)
        if converter is not None:
            try:
                value_converted = converter(value)
            except ValueError:
                print("Invalid value")
                continue
        else:
            value_converted = typing.cast(T, value)
        if possible_values is None or value_converted in possible_values:
            return value_converted
        print("Not one of the possibilities")

calculate the difference

Since you have a series with all the inflation ratios already with periods as index, this becomes easy calculate the cumulative product, or even accept datetime.datetime objects as arguments.

import datetime
def calculate_inflation(
    amount: float,
    start: typing.Union[datetime.datetime, str],
    end: typing.Union[datetime.datetime, str],
    inflation_rates: pd.Series,
) -> typing.Tuple[float, typing.Dict[str, float]]:
    inflation_over_period = inflation_rates[start:end]

    return (
        amount * inflation_over_period.product(),
        {
            str(year): amount * index
            for (year, index) in inflation_over_period.cumprod().iteritems()
        },
    )

bringing it together:

if __name__ == "__main__":
    data_file = Path("<my path>")
    inflation_rates = get_inflation_rate(data_file)
    start_year = get_input(
        message="Enter the starting year:",
        possible_values=set(inflation_rate.index.map(str)),
    )
    end_year = get_input(
        message="Enter the ending year:",
        possible_values={
            year for year in inflation_rate.index.map(str) if year > start_year
        },
    )
    amount = get_input(message="Specify the amount:", converter=float)
    print(
        calculate_inflation(
            amount=amount,
            start=start_year,
            end=end_year,
            inflation_rates=inflation_rates,
        )
    )


Pool readers and streams with Json.NET This code is built on Json.NET and gets called hundreds of times per second. My data starts in an ArraySegment<byte>. I wrap that in a MemoryStream and pass it in to the code. It's generally small data: hundreds of bytes, not thousands. It just doesn't seem right that I have to construct multiple streams and readers for every single deserialization call. How do I optimize this? How do I reuse those two readers?

public object Deserialize(Stream stream, Type type)
{
    using (var utfReader = new StreamReader(stream, Encoding.UTF8, true, 2048, true))
    using (var reader = new JsonTextReader(utfReader) { CloseInput = false })
        return _serializer.Deserialize(reader, type);
}

public T Deserialize<T>(Stream stream)
{
    return (T)Deserialize(stream, typeof(T));
}

You could create your own TextReader that allows you to change the underlying Stream, or even directly reads from the ArraySegment<byte>.

But hundreds of times per second is not that often, are you sure worrying about allocations here is worth it? And if it is, I would first look at the 2 kB of buffer your StreamReader allocates every time it's created, rather than the few tens of bytes for the StreamReader and JsonTextReader objects.


Click handler for setting the active color and shape button in a drawing program public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source.equals(jbColor1))
            color = getBlue();
        else if (source.equals(jbColor2))
            color = getRed();
        else if (source.equals(jbColor3))
            color = getGreen();
        else if (source.equals(jbColor4))
            color = getBlack();
        else if (source.equals(jbColor5))
            color = getYellow();
        else if (source.equals(jbColor6))
            color = getPink();
        else if (source.equals(btnRectangle)) {
            isSelectedReactangle = true;
            isSelectedCircle = false;
            isSelectedLine = false;
            btnRectangle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
            btnLine.setBorder(UIManager.getBorder(defaultbtnLable));
            btnCircle.setBorder(UIManager.getBorder(defaultbtnLable));
            count = 0;
        } else if (source.equals(btnCircle)) {
            isSelectedReactangle = false;
            isSelectedCircle = true;
            isSelectedLine = false;
            btnCircle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
            btnLine.setBorder(UIManager.getBorder(defaultbtnLable));
            btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
            count = 0;
        } else if (source.equals(btnLine)) {
            isSelectedReactangle = false;
            isSelectedCircle = false;
            isSelectedLine = true;
            btnLine.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
            btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
            btnCircle.setBorder(UIManager.getBorder(defaultbtnLable));
            count = 0;
        } else if (source.equals(btnClear)) {
            btnLine.setBorder(UIManager.getBorder(defaultbtnLable));
            btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
            btnCircle.setBorder(UIManager.getBorder(defaultbtnLable));
            panelContent.repaint();
        }
}

I have some code but i would avoid if else statement.
for example this snippet code is repeating few times.

btnLine.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
btnCircle.setBorder(UIManager.getBorder(defaultbtnLable));

Some tips and tricks how to avoid if condition so many times

Why would you have one actionPerformed() method for multiple UI elements instead of creating each button with its individual Action instance? Like:

Action clearAction = new AbstractAction("Clear") {
        public void actionPerformed(ActionEvent e) {
            btnLine.setBorder(UIManager.getBorder(defaultbtnLable));
            btnRectangle.setBorder(UIManager.getBorder(defaultbtnLable));
            btnCircle.setBorder(UIManager.getBorder(defaultbtnLable));
            panelContent.repaint();
        }
}
btnClear = new JButton(clearAction);

[Disclaimer: may contain typos, didn't check it in an IDE]

Then the Action is a first-class object, can be bound to multiple UI elements (e.g. a button and a main menu item) and contains both the name (plus an icon if you like) and the implementation.

And if the Actions have common elements, maybe it's worth having them inherit from a common base Action class.


Is this Java small pubsub memory implementation correct and effective? We want to provide a simple Redis pub/sub alternative in memory for our software. So we implemented this :
public class MemoryDataBus implements DataBus {
    private final Map<Consumer<Message>, MessageListener> subscribers = new ConcurrentHashMap<>();

    @Override
    public void publish(final Channel channel, final Message message) {
        Message nonNullMessage = requireNonNull(message, "cannot publish a null message");
        subscribers.values().stream().filter(l -> l.hasSubscribedTo(channel)).forEach(l -> l.accept(nonNullMessage));
    }

    @Override
    public void subscribe(final Consumer<Message> subscriber, final Channel... channels) throws InterruptedException {
        MessageListener listener = new MessageListener(subscriber, channels);
        subscribers.put(subscriber, listener);
        listener.loopUntilShutdown();
    }

    @Override
    public void unsubscribe(Consumer<Message> subscriber) {
        ofNullable(subscribers.remove(subscriber)).ifPresent(l -> {
            l.accept(new ShutdownMessage());
        });
    }

    private static class MessageListener implements Consumer<Message> {
        private final Consumer<Message> subscriber;
        private final LinkedHashSet<Channel> channels;
        final AtomicReference<Message> message = new AtomicReference<>();

        public MessageListener(Consumer<Message> subscriber, Channel... channels) {
            this.subscriber = subscriber;
            this.channels = asSet(channels);
        }

        boolean hasSubscribedTo(Channel channel) {
            return channels.contains(channel);
        }

        @Override
        public void accept(Message message) {
            subscriber.accept(message);
            synchronized (this.message) {
                this.message.set(message);
                this.message.notify();
            }
        }

        boolean shutdownAsked() {
            Message message = this.message.get();
            return message != null && message.type == Message.Type.SHUTDOWN;
        }

        void loopUntilShutdown() throws InterruptedException {
            synchronized (message) {
                while (!shutdownAsked()) {
                    message.wait();
                }
            }
        }
    }
}

I've removed unnecessary code that brings some noise (logs, couters) the source code is here.
Our unit tests are green, and manual testing shows no regression compared to redis. But as we also know how difficult it is to make a correct thread safe implementation we want to verify with threading experts :

the correctness of it (for example the value returned by shutdownAsked)
if the contention could be improved
if there could be a better implementation of it

PS : must add that the question has been originally posted on SO.

Assuming requireNonNull(...) is java.util.Objects.requireNonNull(...) which throws an exception if null, then there is no need to have a separate local variable for the return value.
Assuming ofNullable(...) is java.util.Optional.ofNullable(...), then I would recommend removing the usage here because the code can be simplified:
// ofNullable(...).ifPresent(l -> ...);
// Simplified:
MyClass result = ...
if (result != null) {
    ...
}

It looks like your locking in MessageListener will cause a deadlock:

MemoryDataBus.subscribe(...) calls loopUntilShutdown which synchronizes on message
MemoryDataBus.publish(...) calls accept which has to wait because lock on message is still held, preventing it from updating message

Edit: No deadlock occurs because Object.wait() is used which releases ownership of the monitor (here field message).


Create 3D monkey head Introduction:

The following script contains code that performs what I would usually do when staring a new scene (add lights, camera, objects). The script will serve as a starting point for most of my future scripts.

In the future, I might use more complex materials, create animations, or use the built-in physics simulator (for example, rigid body, fluid, smoke).

This is what the script below renders:

The command to run the code is blender --background --python-exit-code 1 --python monkey.py. The code has been tested on Blender 2.81a (released December 5, 2019).

Here are some of my concerns:

There are quite a few lines which use bpy.ops. bpy.ops should be avoided.
Clearing all objects in the beginning with select_all and then delete looks like a workaround to me. There might be a way to start a scene without the default cube, light, and camera.

monkey.py

import bpy
import shutil
import os
import time

def add_light(location, light_type='POINT', color=(1.00, 1.00, 1.00), energy=1000.00):
    bpy.ops.object.add(type='LIGHT', location=location)
    obj = bpy.context.object

    obj.data.type = light_type
    obj.data.color = color
    obj.data.energy = energy

def set_smooth(obj, level=None, smooth=True):
    if level:
        modifier = obj.modifiers.new('Subsurf', 'SUBSURF')
        modifier.levels = level
        modifier.render_levels = level

    mesh = obj.data
    for p in mesh.polygons:
        p.use_smooth = smooth

def create_focal_point(location=(0.00, 0.00, 0.00)):
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.10, location=location)
    focal_point = bpy.context.object
    focal_point.hide_render = True
    return focal_point

def set_focal_point(camera, focal_point):
    bpy.context.view_layer.objects.active = camera
    bpy.ops.object.constraint_add(type='TRACK_TO')
    camera.constraints['Track To'].target = focal_point
    camera.constraints['Track To'].track_axis = 'TRACK_NEGATIVE_Z'
    camera.constraints['Track To'].up_axis = 'UP_Y'

def create_monkey(origin=(0.00, 0.00, 0.00)):
    bpy.ops.mesh.primitive_monkey_add(location=origin)
    obj = bpy.context.object
    return obj

if __name__ == '__main__':
    # Delete all objects
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()

    main_object = create_monkey()

    yellow_rgba = (1.00, 1.00, 0.00, 1.00)

    mat = bpy.data.materials.new(name='yellow')
    mat.use_nodes = True
    mat.diffuse_color = yellow_rgba
    nodes = mat.node_tree.nodes

    nodes.clear()
    node_material_output = nodes.new(type='ShaderNodeOutputMaterial')

    node_diffuse = nodes.new(type='ShaderNodeBsdfDiffuse')
    node_diffuse.name = 'Yellow Diffuse'
    node_diffuse.inputs['Color'].default_value = yellow_rgba

    input = node_material_output.inputs['Surface']
    output = node_diffuse.outputs['BSDF']
    mat.node_tree.links.new(input, output)

    main_object = bpy.context.active_object
    main_object.active_material = mat

    set_smooth(main_object, level=5)

    add_light(location=(5.00, -7.50, 5.00))

    bpy.ops.object.camera_add(location=(0.00, -5.00, 0.00))
    main_camera = bpy.context.object
    bpy.context.scene.camera = main_camera

    focal_point = create_focal_point(main_object.location)
    set_focal_point(main_camera, focal_point)

    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.cycles.samples = 50

    bpy.context.scene.render.tile_x = 256
    bpy.context.scene.render.tile_y = 256

    bpy.context.scene.render.resolution_x = 600
    bpy.context.scene.render.resolution_y = 600

    bpy.context.scene.render.resolution_percentage = 100

    bpy.context.scene.render.image_settings.compression = 100

    base_filename = time.strftime('%Y %m %d - %H %M %S - ') + os.path.basename(__file__)
    base_filename_no_extension = os.path.splitext(base_filename)[0]
    shutil.copy(__file__, base_filename)

    bpy.ops.wm.save_as_mainfile(filepath=base_filename_no_extension+'.blend')

    bpy.context.scene.render.filepath = base_filename_no_extension+'.png'
    bpy.ops.render.render(write_still=True)

Use alphabetical order for imports

import bpy
import os
import shutil
import time


Find all file occurences of a search term on drive I have written a small application that lets the user search drives for their given input string. I am only returning the full name of files. What I have now is the following method to retrieve those full names:

private IEnumerable<string> FindOccurences(string searchQuery, DriveInfo drive)
{
    return drive.RootDirectory.EnumerateFiles(searchQuery).Select(file => file.FullName);
}

Where I made the choice of using EnumerateFiles over GetFiles to get faster performance as shown here:

  The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

I was wondering if there is any other solution to increase performance even more, perhaps through a different API? Are there any other tweaks to my code you can suggest?

I really like this code. Let me explain why in a few short sentences:

Typechoice:

You return an IEnumerable and not a List. This is awesome, because a List implies sorted entries and blocks some Types, that you can use with IEnumerable

Naming:

Your names are short, comprehensive and follow the naming conventions. There's not much more to say.

Program flow:

It is instantly obvious to the reader, what your program does and how it does that, simply by speaking english. This is very hard to accomplish, and I want to compliment you on that.

Choice of methods:

You explained very nicely why you chose EnumerateFiles() over GetFiles(). If you'd comment that, IMO your code would be perfect.


Downloading and reading data in go I am trying to experiment with golang.

This code spins off two Go routines: one to download data and another to read from the body. Both go threads are synced by the data channel.

I have two questions:

Should I have to dispose the body inside downloadData method?
Is this type of coding acceptable in Go?

package main

import (
    "fmt"
    "net/http"
    "time"
)

func main() {
    data := make(chan *http.Response)
    go downloadData(data)
    go readData(data)
    var input string
    fmt.Scanln(&input)
}

func downloadData(data chan *http.Response) {
    resp, _ := http.Get("http://www.google.com")
    defer resp.Body.Close()
    time.Sleep(time.Second * 1)
    data <- resp
}

func readData(data chan *http.Response) {
    response := <-data
    fmt.Println(response)
}

After spinning two routines main should wait for them to terminate. See sync package for common synchronisation primitives. fmt.Scanln is for input reading.
http.Get may return error. Proper way would be to handle it.
resp.Body will be closed when downloadData returns and readData won't be able to use it anymore.

Regarding your questions:

As stated by documentation resp.Body must be closed. Close it as soon as you don't need it anymore.
The code lacks error checks and proper routines synchronisation. Despite the fact this code is compiling, this is not acceptable Go code and it must be fixed.

I suggest you to read Effective Go for concurrency and error handling overview.


Solution to Kickstart's Wiggle Walk I'm trying to optimize my solution to Wiggle Walk for Kickstart's round 1C.

  Problem

  Banny has just bought a new programmable robot. Eager to test his
  coding skills, he has placed the robot in a grid of squares with R
  rows (numbered 1 to R from north to south) and C columns (numbered 1
  to C from west to east). The square in row r and column c is denoted
  (r, c).

  Initially the robot starts in the square (SR, SC). Banny will give the
  robot N instructions. Each instruction is one of N, S, E or W,
  instructing the robot to move one square north, south, east or west
  respectively.

  If the robot moves into a square that it has been in before, the robot
  will continue moving in the same direction until it reaches a square
  that it has not been in before. Banny will never give the robot an
  instruction that will cause it to move out of the grid.

  Can you help Banny determine which square the robot will finish in,
  after following the N instructions?

  Input

  The first line of the input gives the number of test cases, T. T test
  cases follow. Each test case starts with a line containing the five
  integers N, R, C, SR and SC, the number of instructions, the number of
  rows, the number of columns, the robot's starting row and starting
  column, respectively.

  Then, another line follows containing a single string of N characters;
  the i-th of these characters is the i-th instruction Banny gives the
  robot (one of N, S, E or W, as described above).

  Output

  For each test case, output one line containing Case #x: r c, where x
  is the test case number (starting from 1), r is the row the robot
  finishes in and c is the column the robot finishes in.

  Limits

  Memory limit: 1GB. 1 ≤ T ≤ 100. 1 ≤ R ≤ 5 × 104. 1 ≤ C ≤ 5 × 104. 1 ≤
  SR ≤ R. 1 ≤ SC ≤ C. The instructions will not cause the robot to move
  out of the grid. Test set 1 (Visible)

  Time limit: 20 seconds. 1 ≤ N ≤ 100. Test set 2 (Hidden)

  Time limit: 60 seconds. 1 ≤ N ≤ 5 × 104.

Here's my current solution:

def main():
    T = int(input())  # the number of test cases

    for case in range(1, T+1):
        N, R, C, r, c = map(int, input().split())
        instructions = input()  # string of N, S, E or W
        seen = {(r, c)}

        for i in instructions:
            if i == 'N':
                r -= 1
                while (r, c) in seen:
                    r -= 1
            elif i == 'S':
                r += 1
                while (r, c) in seen:
                    r += 1
            elif i == 'E':
                c += 1
                while (r, c) in seen:
                    c += 1
            else:  # 'W'
                c -= 1
                while (r, c) in seen:
                    c -= 1
            seen.add((r, c))

        print('Case #{}: {} {}'.format(case, r, c))

main()

How can I represent visited areas in the grid so that getting the destination cell for each instruction would be faster?

A simple dictionary will do once you get the idea ;)

def get_neighbor(r, c, i, neighbors):
    if (r, c, i) in neighbors:
        return neighbors[(r, c, i)]

    if i == 'N':
        return (r - 1, c)
    elif i == 'S':
        return (r + 1, c)
    elif i == 'E':
        return (r, c + 1)
    else:  # 'W'
        return (r, c - 1)

def link_neighbors(r, c, neighbors):
    north = get_neighbor(r, c, 'N', neighbors)
    south = get_neighbor(r, c, 'S', neighbors)
    east = get_neighbor(r, c, 'E', neighbors)
    west = get_neighbor(r, c, 'W', neighbors)

    neighbors[(*north, 'S')] = south
    neighbors[(*south, 'N')] = north
    neighbors[(*east, 'W')] = west
    neighbors[(*west, 'E')] = east

def main():
    T = int(input())  # the number of test cases

    for case in range(1, T+1):
        N, R, C, r, c = map(int, input().split())
        instructions = input()  # string of N, S, E or W
        neighbors = {}

        for i in instructions:
            link_neighbors(r, c, neighbors)
            r, c = get_neighbor(r, c, i, neighbors)  # new position

        print('Case #{}: {} {}'.format(case, r, c))

main()


Creating a Stripe Subscription I have some working Haskell code in a Yesod application that is responsible for creating a new paid Stripe subscription for a user.

Although it works, I'm not pleased with the walking indentation that appears as a result of nested Either case matches.

module Handler.Purchase where

import           Import
import           Web.Stripe
import           Web.Stripe.Customer
import           Web.Stripe.Plan
import           Web.Stripe.Subscription

postPurchaseR :: Handler Html
postPurchaseR = do
  -- I will need to keep a reference to the user later
  _ <- requireAuth

  -- I will need to keep a reference to the Stripe token later
  _ <- runInputPost $ ireq textField "stripeToken"

  -- I hard-code the secret key, which I'm fine with for now
  let config = StripeConfig $ StripeKey "<redacted>"

  -- Here's where things get hairy...
  customer <- liftIO $ stripe config createCustomer
  case customer of
    Left err -> do
      setMessage $ toHtml $ show err
      redirect (AppR :#: ("subscribe" :: Text))
    Right Customer { customerId = cid } -> do
      plan <- liftIO $ stripe config $ getPlan (PlanId "PRO")
      case plan of
        Left err -> do
          setMessage $ toHtml $ show err
          redirect (AppR :#: ("subscribe" :: Text))
        Right Plan { planId = pid } -> do
          subscription <- liftIO $ stripe config $ createSubscription cid pid
          case subscription of
            Left err -> do
              setMessage $ toHtml $ show err
              redirect (AppR :#: ("subscribe" :: Text))
            Right _ -> do
              setMessage "Subscription successful. Thanks!"
              redirect AppR

As I understand it, I should be using ExceptT instead of having all that nesting, but I'm not sure how that works. Correct use of ExceptT will probably obviate the need for me to define how to handle those three failures each time.

Here is how you can use ExceptT:

  -- I hard-code the secret key, which I'm fine with for now
  let config = StripeConfig $ StripeKey "<redacted>"
  let stripe' :: (FromJSON (StripeReturn a))
              => StripeRequest a -> ExceptT StripeError Handler (StripeReturn a)
      stripe' = ExceptT . liftIO . stripe config

  -- Here's where things get hairy...
  r <- runExceptT $ do
    Customer {..} <- stripe' createCustomer
    Plan {..} <- stripe' $ getPlan (PlanId "PRO")
    stripe' $ createSubscription customerId planId
  case r of
    Left err -> do
      setMessage $ toHtml $ show err
      redirect (AppR :#: ("subscribe" :: Text))
    Right _ -> do
      setMessage "Subscription successful. Thanks!"
      redirect AppR

I used RecordWildcards so that the intermediate variables (e.g. cid and pid) aren't necessary.
You'll also need to enable FlexibleContexts because of the way stripe is defined in the library.


Function for accessing and searching dictionaries using /slashed/paths (like xpath) So I have some data being returned to me in the form of a deeply nested Dict and I need to parse multiple values from it which may or may not be there. I saw this library (https://github.com/akesterson/dpath-python) and tried to come up with a simplified version that fits into one function.
I think its pretty good but I'm wondering if I missed something or there's some small way to improve it?
from typing import Dict, Union
import re
import copy

def dict_query(d: Dict, path: str) -> Union[object, None]:
    """
    # Example usage:
    >>> d = {'a': [{}, {'b': 9}]}
    >>> print( dict_query(d, 'a/[1]/b') )
    9
    """
    keys = path.split("/")
    val = copy.deepcopy(d)
    for key in keys:

        idx = re.search("\[(\d+)\]", key)
        if idx:
            # handle list index
            if isinstance(val, (list, tuple)):
                idx = int(idx.group(1))
                val = val[idx]
            else:
                return None
        elif isinstance(val, dict):
            # handle dict key
            val = val.get(key)
        else:
            return None
    return val

# tests of searching nested dicts
d = {'a': {'b': 1}}
print( dict_query(d, 'a/b') ) # 1
print( dict_query(d, 'a/c') ) # None
print( dict_query(d, 'a/') ) # None
print( dict_query(d, 'c/b') ) # None
# tests of searching nested dicts + lists
d = {'a': [{}, {'b': 1}]}
print( dict_query(d, 'a/[0]') ) # {}
print( dict_query(d, 'a/[1]') ) # {'b': 1}
print( dict_query(d, 'a/[1]/b') ) # 1
print( dict_query(d, 'a/[1]/c') ) # None
print( dict_query(d, 'a/[0]/a') ) # None
print( dict_query(d, '[1]/b') ) # None

I would skip the [] in the path.  Just presume val is a dict with strings for keys (the usual case).  It that doesn't work, try converting the key to an int.
def dict_query(d: Dict, path: str) -> Union[object, None]:
    """
    # Example usage:
    >>> d = {'a': [{}, {'b': 9}]}
    >>> print( dict_query(d, 'a/1/b') )
    9
    """
    keys = path.split("/")
    val = d

    try:
        for key in keys:
            try:
                val = val[key]

            except (KeyError, TypeError):
                val = val[int(key)]

        return val

    except (IndexError, KeyError, TypeError, ValueError):
        return None


2D array to draw a letter 'F' This is my code to print letter 'F' with nested loop 2d array. Are there changes that could be made in it to make it look better or more efficient?

 for (int i=0;i<=4;i++){

        for(int j=0;j<=4;j++){

            array[i][j]='*';
            if ( array[1][1]=='*' || array[1][2]=='*' || array[1][3]=='*' || array[1][4]== '*'||
                 array[3][1]=='*' || array[3][2]=='*' || array[3][3]=='*' || array[3][4]== '*'
               ||array[4][1]=='*' || array[4][2]=='*' || array[4][3]=='*' || array[4][4]== '*')
                {
                array[i][j]=' ';
            }
          }
        }
        for (int i=0;i<=4;i++){
          for(int j=0;j<=4;j++){
            System.out.print(" "+array[i][j]);
        }
            System.out.println("\n");
       }

I can't imagine any good reason for drawing an F using that approach.  Drawbacks include:

It is not obvious at all, just from looking at the code, what the expected output is.
Not only is the code hard to read, it is also hard to write and modify.
Using many small System.out.print() calls is very inefficient.

A solution like this, for example, is much easier to understand.  It would also be more efficient, since all of the output is done in one System.out.print() call.

public static final String F =
    "*****\n" +
    "*    \n" +
    "*****\n" +
    "*    \n" +
    "*    \n";

public static void main(String[] args) {
    // Print with 2x magnification
    System.out.print(F.replace("*", " *").replace("\n", "\n\n"));
}


Generate all partitions of a set of items Here are the items:

    var item = ['a', 'b', 'c', 'd'];

Desired output:

    [
        [ ['a'],['b'],['c'],['d'] ],
        [ ['a'],['b','c'],['d'] ],
        [ ['a'],['b'],['c','d'] ],
        [ ['a'],['b','c','d'] ],
        [ ['a'],['c'],['b','d'] ],....
    ]

The code:

function getCombinations(items) {
   var res = [];
   for (var i = 0; i < items.length; i++) {
      for (var j = i + 1; j <= items.length; j++) {
         var temp = items.slice(i, j);
         var remained = items.slice(0, i).concat(items.slice(j, items.length));
         if (remained.length > 0) {
            var combinations = getCombinations(remained);
            combinations.forEach(combination => {
               res.push([temp].concat(combination));
            });
         } else {
            res.push([temp]);
         }
      }
    }
   return res;
}

var item = ['a', 'b', 'c', 'd'];

console.log(getCombinations(item));

I see the performance tag added to this post. Did you intent to have answers address performance in terms of time, space or other aspects?
If you are looking to optimize performance in terms of time then I must restrain myself from suggesting changing the outer for loop to a for of loop. However I do see there is a forEach loop inside the nested for loop. If you really want to optimize for performance, then consider using a for loop instead of that .forEach method. Also consider whether the same result can be achieved by iterating backwards through the loops - this would allow for fewer operations1.
Given that an arrow function is used in the .forEach() callback, other ecmascript-6 features can be used - e.g. const and let instead of var, spread syntax instead of calling .concat(), etc.


Closest distance between points in a list In a course I'm doing, I was given the task of finding the closest pair of points among the given points. The program that passed all test cases was the following:

import math

if __name__ == '__main__':
    n = int(input())
    P = []
    for i in range(n):
        line = input().split()
        x = int(line[0])
        y = int(line[1])
        p = (x, y)
        P.append(p)
    print(closest(P, n))

def square(x):
    return x * x

def square_distance(p0, p1):
    return square(p0[0] - p1[0]) + square(p0[1] - p1[1])

def closest(P, n):
    P.sort() # sort by x coordinates
    return math.sqrt(_closest_square_distance(P, n))

def _closest_square_distance(P, n):
    if n == 2:
        return square_distance(P[0], P[1])
    if n == 3:
        return min(square_distance(P[0], P[1]), square_distance(P[0], P[2]), square_distance(P[1], P[2]))

    mid = n // 2
    dl = _closest_square_distance(P[:mid], mid)
    dr = _closest_square_distance(P[mid:], n - mid)

    closest_square_distance = min(dl, dr)
    closest_distance_so_far = math.sqrt(closest_square_distance)

    mid_x = P[mid][0]
    strip = []
    strip_length = 0
    for i in range(n):
        p = P[i]
        if abs(p[0] - mid_x) < closest_distance_so_far:
            strip.append(p)
            strip_length += 1

    strip.sort(key=lambda x: x[1]) # sort strip by y coordinates

    for i in range(strip_length):
        js = min(strip_length, i + 7) # sufficient to compute next 6 neighbors
        for j in range(i + 1, js):
            ds = square_distance(strip[i], strip[j])
            if ds < closest_square_distance:
                closest_square_distance = ds

    return closest_square_distance

This code was based on the algorithm found at https://en.wikipedia.org/wiki/Closest_pair_of_points_problem#Planar_case . What do you think I can do better? Is there a more efficient way to implement this algorithm? (In this one, I just tried to delay the sqrt computation as long as possible, and avoided to calculate len where I could). Is there an even more efficient algorithm?

1. Review

There are no docstrings. What do these functions do? What arguments do they take and what do they return?
You write:

  I just tried to delay the sqrt computation as long as possible

This suggests that your cost model for Python is not quite right. In CPython, it's more expensive to call square than it to call sqrt:

>>> from timeit import timeit
>>> timeit('square(999)', 'from __main__ import square')
0.11685514450073242
>>> timeit('sqrt(999)', 'from math import sqrt')
0.057652950286865234

That's because most of the time executing a program in CPython is spent in the execution of bytecode. The actual cost of computing the square root is small by comparison.

Because bytecode is expensive, it sometimes pays (in CPython) to do more computation, if you can arrange for it to be done in the fast C implementation rather than in slow Python code. In this case I would consider using math.hypot, like this:

from math import hypot

def distance(p, q):
    "Return the Euclidean distance between points p and q."
    return hypot(p[0] - q[0], p[1] - q[1])

This is faster than calling square_distance:

>>> timeit('square_distance((1, 2), (3, 4))', 'from __main__ import square_distance')
0.4481959342956543
>>> timeit('distance((1, 2), (3, 4))', 'from __main__ import distance')
0.32285499572753906

In closest, the argument n must be the length of the list of points P. So it would be simpler to assign it yourself and avoid the requirement for the caller to pass it in (which is risky, because the caller might get it wrong).
The function closest has a side-effect: as well as computing the shortest distance, it sorts the list P. Unexpected side effects should be avoided — it is easier to understand code if it only has "local" effects on data structures. In this case, there are two alternatives: you could require the caller to sort the points before passing them in, or you could take a copy and sort the copy, like this:

def closest(P):
    "Return the closest Euclidean distance between two points in the list P."
    return _closest_distance(sorted(P), len(P))

When recursing:

dl = _closest_square_distance(P[:mid], mid)
dr = _closest_square_distance(P[mid:], n - mid)

the list slice P[:mid] has to copy out the contents of the slice. It would be cheaper to remember the endpoints of the slice you are working on and avoid the copying:

def _closest(P, start, stop):
    # closest Euclidean distance between two points in the slice P[start:stop]
    # handle base cases here

    mid = (start + stop) // 2
    dl = _closest(P, start, mid)
    dr = _closest(P, mid, stop)

In the base cases, you could save some duplication by using itertools.combinations and writing:

if n <= 3:
    return min(distance(p, q) for p, q in combinations(P, 2))

The base case logic could be extended to larger values of n. I find that for large random point sets, n <= 8 performs well.
The function:

lambda x: x[1]

is the same every time, so consider storing it in a global variable (or using operator.itemgetter(1)).
The code finds the points in the strip by iterating over all the points in P. But since you have the points sorted by their \$x\$ coordinate, you can find the endpoints of the strip using the bisect module.
The code does not take advantage of the fact that the two points being compared can only be closer than closest_distance if they were in different halves (if they were in the same half, then they have already been compared by the recursive calls and closest_distance already takes them into account).

2. Revised code

from bisect import bisect_left, bisect_right
from itertools import combinations, islice
from math import hypot
from operator import itemgetter

def distance(p, q):
    "Return the Euclidean distance between points p and q."
    return hypot(p[0] - q[0], p[1] - q[1])

_Y_COORD = itemgetter(1)

def closest_distance(P):
    "Return the closest Euclidean distance between two points in the list P."
    P = sorted(P)
    PX = [x for x, _ in P]

    def _closest(start, stop):
        if stop - start <= 8:
            return min(distance(p, q)
                       for p, q in combinations(P[start:stop], 2))

        mid = (start + stop) // 2
        dist = min(_closest(start, mid), _closest(mid, stop))

        mid_x = PX[mid]
        left = bisect_left(PX, mid_x - dist, lo=start, hi=mid)
        right = bisect_right(PX, mid_x + dist, lo=mid, hi=stop)
        strip = sorted(P[mid:right], key=_Y_COORD)
        strip_y = list(map(_Y_COORD, strip))

        for p in P[left:mid]:
            y = p[1]
            i = bisect_left(strip_y, y - dist)
            j = bisect_right(strip_y, y + dist)
            assert j - i <= 6
            for q in strip[i:j]:
                dist = min(dist, distance(p, q))

        return dist

    return _closest(0, len(P))


Streamline R code for Shiny usage I have the following code to create a table for overall percentages and the past 30 day percentages.  I would like to add this to a Shiny app I'm making, but I feel there is a simpler or shorter way to create this code/table than what I have, which would work better in Shiny.  I'm not asking for the Shiny code, just a way to reduce the code I have.

a$FAIL<-ifelse(a$OPERATION_STATUS %in% "FAIL", 1, 0)
cc<-as.data.frame((table(a$CRIT_CODE))) #Dataframe with frequency of each crit code
cf<-aggregate(FAIL~CRIT_CODE,a,sum) #Total number of fails based on crit codes
cc<-cbind(cc[,],cf[,2])
names(cc)<-c("CRIT_CODE", "Freq", "FAIL") #Change column names
cc<-transform(cc, Percent=FAIL/Freq*100)

last30<-a[which(a$DATE>=(Sys.Date()-30)),]
last<-as.data.frame((table(last30$CRIT_CODE))) #Dataframe with frequency ofeach crit code
lastfails<-aggregate(FAIL~CRIT_CODE,last30,sum) #Total number of fails based on crit codes
last<-cbind(last[,],lastfails[,2])
names(last)<-c("CRIT_CODE", "Freq", "FAIL") #Change column names
last<-transform(last, Percent=FAIL/Freq*100)

rates<-merge(cc[,c(1,4)], last[,c(1,4)], by="CRIT_CODE")
rates$Percent.x<-round(rates$Percent.x, 2)
rates$Percent.y<-round(rates$Percent.y, 2)
library(gridExtra)
grid.table(rates, rows=NULL, cols=c("Area", "Overall Percent", "30 Day Percent"))

Sample Data

Data <- data.frame(
  DATE=sample(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"), 15),
  OPERATION_STATUS=sample(c("PASS","FAIL"), 15, replace=TRUE),
  CRIT_CODE=sample(c("A", "B", "C"), 15, replace=TRUE)
)

Changed a little bit input data:

n <- 60
set.seed(21)
a <- data.frame(
  DATE =
    rev(seq.Date(as.Date("2018-01-01"), as.Date("2018-06-15"), "days"))[1:n],
  OPERATION_STATUS = sample(c("PASS","FAIL"), n, replace = TRUE),
  CRIT_CODE = sample(c("A", "B", "C"), n, replace = TRUE)
)

Using data.table I would do it like this:

require(data.table)
setDT(a)
a[, group_i := DATE >= (Sys.Date() - 30)]
rates3 <- a[, .(
  Percent1 = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
  Percent2 = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
  ),
  keyby = CRIT_CODE]

rates3
#    CRIT_CODE Percent1 Percent2
# 1:         A    71.43    75.00
# 2:         B    63.16    66.67
# 3:         C    59.26    45.45

Presumably for large data one of the fastest approaches.

P.S. results changes depending on Sys.Date()

Names can be added directly:

rates3 <- a[, .(
  'Overall Percent' = round(sum(OPERATION_STATUS == 'FAIL') / .N * 100, 2),
  '30 Day Percent' = round(sum(OPERATION_STATUS[group_i] == 'FAIL') / sum(group_i) * 100, 2)
  ),
  keyby = .(Area = CRIT_CODE)]


Generates a bare git repo from existing one and copy it to a central server This is a Bash script that takes the name of a local Git repo and a remote server host/path, generates a bare repo, and copies the bare repo to a Git server located in another server.

Example usage:

centralize-git-repo.sh arepo user@gitserver:/srv/git

It generates a folder in server:/srv/git/arepo.git and adds the remote in the local repo.

#!/bin/bash
set -e

GIT="/usr/bin/git"
SCP="/usr/bin/scp"

if [ "$1" == "--help" ] | [ $# -eq 0 ] ; then
    echo "$0 [repo] [user@]hostname:[remote_git_path]"
    exit 0
fi

# Assign parameters
LOCAL_REPO=$(basename $1)
REMOTE_DIR=$2

# Checks if the repo exists in current directory.
if [ ! -d "${LOCAL_REPO}" ]; then
    echo "${LOCAL_REPO} is not a repo in current directory."
    exit 1
fi

# Checks second parameter is present
if [ ! "${REMOTE_DIR}" ]; then
    echo "Remote is missing, pass the target dir on the remote "
    echo " server as the second parameter as: user@server:/dir/../git/"
    echo ""
    echo "e.g.: $0 localrepo user@gitserver:/srv/git"
    exit 1
fi

######

# Creates a container for the bare repo
BARE_TMP_DIR=$(mktemp --quiet --directory)
NEW_REPO_DIRNAME=${LOCAL_REPO}.git
BARE_REPO_PATH=${BARE_TMP_DIR}/${NEW_REPO_DIRNAME}

mkdir ${BARE_REPO_PATH}

$GIT clone --bare ${LOCAL_REPO} ${BARE_REPO_PATH}/

$SCP -r ${BARE_REPO_PATH} ${REMOTE_DIR}

# add remote
cd ${LOCAL_REPO}
$GIT remote add origin ${REMOTE_DIR}/${NEW_REPO_DIRNAME}
$GIT remote -v

# clean
rm -rf ${BARE_TMP_DIR}
cd ..

Bug

First things first, this doesn't work:

if [ "$1" == "--help" ] | [ $# -eq 0 ] ; then

You need || there, not |.

Local bare repo + copy vs remote bare repo + push

I completely agree with @Vogel that it would be better to create the bare repo on the remote, and then push to it:

git push is the simplest and safest way to transport Git repo data
Working with temporary dirs is messy. If your script gets killed in the middle, the cleaning at the end may not be reached (this could be improved by using trap)
The local bare clone unnecessarily doubles the .git storage space locally

Usability

The current script must be run in the parent directory of a Git repo,
as it is enforced by the [ ! -d "${LOCAL_REPO}" ] condition.
This is not convenient, and it doesn't have to be that way.

You could make it work with any valid path by making these two simple changes:

# LOCAL_REPO=$(basename $1)
LOCAL_REPO=$1

# NEW_REPO_DIRNAME=${LOCAL_REPO}.git
NEW_REPO_DIRNAME=$(basename "$LOCAL_REPO").git

Quoting

It's a good practice to double-quote variables used in command arguments.
For example instead of this:

LOCAL_REPO=$(basename $1)

It's good to make it a habit to write like this:

LOCAL_REPO=$(basename "$1")

(I trust you don't intend to ever create a repository with a space in the name, but this is still a good habit to pick up.)

The same goes for running the uses of $GIT and $SCP.

On the other hand, some quoting is unnecessary, for example you could write like this:

GIT=/usr/bin/git
SCP=/usr/bin/scp

if [ "$1" = --help ] || [ $# -eq 0 ] ; then

This is just for your information, it's ok the way you did it.

Only loosely related, but simply echo is the same as echo "".

Changing directories in scripts

Be careful with changing directories in scripts. If an error happens in the middle of the operation, the next operation may find itself in an unexpected directory (in the absence of adequate error handling).

Changing directories can become safer using (...) sub-shells. For example instead of this:

cd ${LOCAL_REPO}
$GIT remote add origin ${REMOTE_DIR}/${NEW_REPO_DIRNAME}
$GIT remote -v
cd ..

You could write like this (without the cd .. at the end):

(
    cd ${LOCAL_REPO}
    $GIT remote add origin ${REMOTE_DIR}/${NEW_REPO_DIRNAME}
    $GIT remote -v
)


Check the hardrive performance of the particular process using a Perl script I have written the following Perl script to monitor the hardrive performance of the particular process (say, for example, Chrome).

use strict;
use warnings;
use Spreadsheet::WriteExcel;

#@usage example perl script.pl 5 5 if($#ARGV!=1);

my $login = (getpwuid $>);
die "must run as root" if $login ne 'root';

my $workbook = Spreadsheet::WriteExcel->new("Performance_report.xls");
my  $disk=$workbook->add_worksheet('disk');
$disk->write(0,0,"Time_interval",0);
$disk->write(0,1,"read/s",0);
$disk->write(0,2,"write/s",0);

#stores the time interval
my $time_interval=$ARGV[0];
#stores the no of iteration
my $test_count=$ARGV[1];
my $iter=0;
while($iter<$test_count)
{
   my @processes;
   @processes=`iotop -b -n 1 |grep chrome`;
   my $read_per_second=0;

   my $write_per_second=0;
   foreach(@processes)
   {
      my $read=0;
      my $write=0;
      while ($_ =~ /([\d\.]+)\s+[B|K]\/s+\s+([\d\.]+)/sg)
      {
          $read_per_second+=$1;
          $write_per_second+=$2;
      }
   }
   $disk->write($iter+1,0,$time_interval*($iter+1));
   $disk->write($iter+1,1,$read_per_second);
   $disk->write($iter+1,2,$write_per_second);
   sleep($time_interval);
   $iter++;
}
$workbook->close() or die "Cannot close the worksheet";

I have used the iotop tool in this script to monitor the disk performance.

sudo iotop -b -n 1 | grep chrome gives the following output:

3687 be/4 venkat      0.00 B/s    0.00 B/s  0.00 %  0.00 % chrome --type=zygote
4284 be/4 venkat      0.00 B/s    0.00 B/s  0.00 %  0.00 % chrome
3693 be/4 venkat      0.00 B/s    0.00 B/s  0.00 %  0.00 % chrome --type=zygote
3695 be/4 venkat      0.00 B/s    0.00 B/s  0.00 %  0.00 % chrome
3728 be/4 venkat      0.00 B/s    0.00 B/s  0.00 %  0.00 % chrome
5468 be/4 venkat      0.00 B/s    0.00 B/s  0.00 %  0.00 % chrome

I store the output to the array, process every value of the array, extract the second and third column value, recursively add the values of every thread of Chrome and store the final sum in the Excel sheet for every iteration.  Is there any better way to do this using a Perl script?

in general this script seems to be straight forward.

But here are some suggestions for improvements:

First check for needed command line arguments and die early with a usage message if they are missing/incomplete. Alternatively you could supply defaults for time interval and iterations like this

#stores the time interval
my $time_interval=$ARGV[0] || 10;
#stores the no of iteration
my $test_count=$ARGV[1] || 50;

You also might want to remove the hard coded process name (chrome) and use a third command line parameter

#stores the process name
my $processname=$ARGV[2] || 'chrome';

and

@processes=`iotop -b -n 1 |grep $processname`;

The result of calls to the write method of Spreadsheet::WriteExcel should be checked for success (=0) and failures should be shown somewhere (either on the console and/or in some log file).

The processing of iotop output could be enhanced.

Variables $read and $write are not needed.
Each line needs to be matched only once (while -> if and no need
for matching modifier g)

The regexs could be made more robust and written more readable.

I defined the regex for the throughput part once in a variable ($throughputpattern) and used it twice afterwards in the match. This is a general principle (don't repeat yourself) for better maintainablility.
the regex now uses the x modifier which allows whitespace in the pattern for better spacing and readability (but then you need to express whitespace to be matched explicitly with \s).
I changed the accepted units of B and K to any capital letter. Who knows what throughput might be possible?

   my $throughputpattern = qr{\b (\d+\.\d+) \s+ [A-Z]/s \s+}x;
   foreach(@processes)
   {
      if ($_ =~ /$throughputpattern $throughputpattern/x)
      {
         $read_per_second+=$1;
         $write_per_second+=$2;
      }
   }


Is this Fibonacci function right? Is this a correct method of Fibonacci with recursion?

function fibb($limit,$first_numer=0,$second_number=1){
        echo $first_numer."\n";
        echo $second_number."\n";
        if($limit > 0){
            fibb($limit-2,$first_numer+$second_number,($first_numer+$second_number+$second_number));
        }
}

In a sense, your function does produce the right numbers.  However, the termination condition is not quite right.  For example, fibb(3) and fibb(4) produce identical output: each prints six numbers.  Why six?  The most natural interpretation of the parameters would be that $limit should specify the number of items in the sequence to be printed.

The root of the problem is that each call to fibb() prints two lines, which means that output will always be printed in pairs.  Normally, you would want to print just one number per call.

In addition, your naming is unconventional.  Why fibb() with two bs?  Also, $first_numer is misspelled.

function fibonacci($limit, $first_number=0, $second_number=1) {
    if ($limit <= 0) {
        return;
    }
    echo "$first_number\n";
    fibonacci($limit - 1, $second_number, $first_number + $second_number);
}


Quickly find percentile with high precision I need to find the percentile where a list of values is higher than a threshold. I am doing this in the context of optimization, so it important that the answer is precise. I am also trying to minimize compute time. I have a O(n) solution which is not very precise, then I use scipy's minimize optimizer to find the exact solution, which is time-intensive. The numbers in my problem are NOT normally distributed.

Is there a more time-efficient way to do this while preserving precision?

from scipy.optimize import minimize
my_vals = []
threshold_val = 0.065
for i in range(60000):
    my_vals.append(np.random.normal(0.05, 0.02))

count_vals = 0.
for i in my_vals:
    count_vals += 1
    if i > threshold_val: break
percKnot = 100 * (count_vals/len(my_vals))
print minimize(lambda x: abs(np.percentile(my_vals, x[0]) - threshold_val), percKnot, bounds=[[0,100]], method='SLSQP', tol=10e-9).x[0]

Use comprehensions

I understand that my_vals is not necessarily the real data and that you might have other means to generate them, but anyway building a list using append is often an antipattern. Use a list comprehension instead:

my_vals = [np.random.normal(0.05, 0.02) for _ in range(60000)]

Same for your actual computation, you basically want to count the amount of values lower than the threshold; use a generator expression and feed it to sum:

sum(1 if x <= threshold_val else 0 for x in my_vals)

This is still \$O(n)\$ and will compute the required value right away (after dividing by len(my_vals)).

Better is to use int(x <= threshold_val) instead of the ternary. Or even the comparison directly (even if more implicit) since True + True is 2.

Use functions

In order to improve reusability and testing.

This also means that you can wrap your demo code into bits that won't necessarily be called every time. For instance:

from scipy.optimize import minimize

def compute_percentile(values, threshold):
    count = sum(x <= threshold for x in values)
    percentage = 100. * count / len(values)
    # Improve precision of the percentile
    return minimize(lambda x: abs(np.percentile(values, x[0]) - threshold), percentage, bounds=[[0,100]], method='SLSQP', tol=10e-9).x[0]

if __name__ == "__main__" :
    demo_values = [np.random.normal(0.05, 0.02) for _ in range(60000)]
    print compute_percentile(demo_values, 0.065)


First touch with python decorator I am getting familiar with decorators, and after a couple of tutorials i came up with a decorator that might is useful for me. Is this the proper way to use decorators? Any suggestion about the code?

def timeit_decorator(original_function):
    import time
    import colorama

    def wrapper(*args, **kwargs):
        tm = time.time()
        res = original_function(*args, **kwargs)
        print("Function " + colorama.Fore.GREEN + original_function.__name__ + colorama.Style.RESET_ALL + " executed in " + colorama.Fore.GREEN + "~{:.2f}".format(
            (time.time() - tm) / 60) + colorama.Style.RESET_ALL + " minutes!")
        return res

    return wrapper

Example of usage:

@timeit_decorator
def display(msg):
    time.sleep(10)
    return msg

print(display(1))

When using decorators, you should familiarize yourself with functools.wraps. Consider the difference in available informations between these two (useless) decorators:

from functools import wraps

def decorate1(func):
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

def decorate2(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

When applied to a function:

@decorate1
def test1(x, y, z):
    "test1 docstring"
    print(x, y, z)

@decorate2
def test2(x, y, z):
    "test2 docstring"
    print(x, y, z)

The help on each function look quite different:

>>> help(test1)
Help on function wrapper in module __main__:

wrapper(*args, **kwargs)

>>> help(test2)
Help on function test2 in module __main__:

test2(x, y, z)
    test2 docstring

Since you’re using Python 3, you should consider using better timing primitives. Namely time.perf_counter():

def wrapper(*args, **kwargs):
    begin = time.perf_counter()
    res = original_function(*args, **kwargs)
    elapsed = time.perf_counter() - begin

Lastly, you should consider using format all along instead of string concatenation:

import time
import colorama
from functools import wraps

def timeit_decorator(original_function):
    @wraps(original_function)
    def wrapper(*args, **kwargs):
        begin = time.perf_counter()
        result = original_function(*args, **kwargs)
        elapsed = time.perf_counter() - begin
        print("Function {color}{}{reset} executed in {color}"
              "~{:.2f}{reset} minutes!"
              .format(
                  original_function.__name__,
                  elapsed / 60,
                  color=colorama.Fore.GREEN,
                  reset=colorama.Style.RESET_ALL))
        return result
    return wrapper


Some suggestions:

number and n could be parameters to this script. That way the whole thing would be reusable.
You can use list comprehensions to partition your string:

>>> def partition(digits: str, length: int) -> List[str]:
...     return [digits[index:index + length] for index in range(len(digits) - length + 1)]
...
>>> partition("12345", 3)
['123', '234', '345']

Multiplying N random digits is going to be slower than checking whether any of the digits are zero. So your first pass could be to exclude any partitions which contain zero. If there are no partitions left afterwards the max is zero, and you've done no multiplications at all.

>>> partitions = partition("7316717653133062491", 13)
>>> nontrivial_partitions = [partition for partition in partitions if "0" not in partition]
>>> nontrivial_partitions
['7316717653133']

An optimization on the above is to immediately discard the next N - 1 digits as soon as you encounter a zero when generating partitions, since all of those partitions are also going to multiply to zero. Make sure you check for any zeros within those numbers as well to keep skipping.
It looks like you have a newline at the end of number.


maxSpan function implementation The original question is as follows:

Consider the leftmost and righmost appearances of some value in an array. We'll say that the "span" is the number of elements between the two inclusive. A single value has a span of 1. Returns the largest span found in the given array. (Efficiency is not a priority.)
maxSpan([1, 2, 1, 1, 3]) → 4
maxSpan([1, 4, 2, 1, 4, 1, 4]) → 6
maxSpan([1, 4, 2, 1, 4, 4, 4]) → 6

Here is my implementation of the maxSpan function.
public int maxSpan(int[] nums) {
  Map<Integer, List<Integer>> spans = new HashMap<>();
  for (int i = 0; i < nums.length; i++) {
    if (!spans.containsKey(nums[i])) {
      List<Integer> position = new ArrayList<>();
      position.add(i);
      spans.put(nums[i], position);
    } else {
      List<Integer> position = spans.get(nums[i]);
      position.add(i);
    }
  }
  int globalMaxSpan = 0; // empty set has maxSpan = 0
  for (List<Integer> value : spans.values()) { //iterating over values only
      int localMaxSpan = 0;
      if (value.size() > 1) {
        localMaxSpan = value.get(value.size() - 1) - value.get(0) + 1;
      } else localMaxSpan = 1;
      if (localMaxSpan > globalMaxSpan)
        globalMaxSpan = localMaxSpan;
  }
  return globalMaxSpan;
}

Here is another implementation of the same algorithm I found on GitHub implemented by others.
public int maxSpan(int[] nums) {
    int max = 0;
    for(int i = 0; i < nums.length; i++) {
        int j = nums.length - 1;
        while(nums[i] != nums[j])
            j--;
        int span = j - i + 1;
        if(span > max)
            max = span;
    }
    return max;
}

Although my implementation is O(n) runtime but it is more complicated than the below implementation. Please give comments to my code in different aspects. Thanks.

Your choice of data structures is unnecessarily heavy. You're interested only in the first and last index of a number, so store only those in a lightweight data structure of two integers (an array):

for (int i = 0; i < nums.length; i++) {
    final int num = nums[i];
    int[] span = spans.get(num);
    if (span == null) {
        span = new int[] { i, i };
        spans.put(num, span);
    } else {
        span[1] = i;
    }
}

Java comes with built in functions for selecting larger or smaller number from two, so use them instead of if-statements or ternary operations. It makes code easier to read.

// Empty set has maxSpan = 0
int maxSpan = 0;
for (int[] span: spans.values()) {
    maxSpan = Math.max(maxSpan, span[1] - span[0] + 1);
}
return maxSpan;

Avoid end-of-line comments. They make the code messy and are a pain in the ass to maintain. Avoid unnecessary comments that describe what code does. The reader can already see that the code iterates over values. Instead document why the code does what it does.

Use variable names that describe what the variable is used for. Value does not give any new information to the reader about what it contains.

Position as a name for a list is deceptive. It's singular form suggests that it contains one value when it instead contains all positions of a certain number.

Edit: You don't actually even need to store the last occurrence of a number. Just store the first one and calculate the maxSpan as you traverse the array the first time.

int maxSpan = 0;
for (int i = 0; i < nums.length; i++) {
    final int num = nums[i];
    Integer firstIndex = firstIndexes.get(num);
    if (firstIndex == null) {
        firstIndex = i;
        firstIndexes.put(num, firstIndex);
    }
    maxSpan = Math.max(maxSpan, i - firstIndex + 1);
}
return maxSpan;


Continuable if error is null I made a method that I can use to avoid having to handle errors from callbacks.
What it does is, call the passed method only if the first argument is null.
So essentially it will bypass to the last method, if an error occurs.

Feedback is welcome.

/**
 * continueIf
 *
 * Returns a continuation method that only continues
 * if error is null'ish
 *
 * @param fn The function to call
 * @param args* Any number of arguments to pass after the arguments from the function
 */

function continueIf (fn) {
  var args = Array.prototype.slice.call(arguments, 1);
  var callback = arguments[arguments.length - 1];
  if (typeof callback !== 'function') {
    throw('Missing callback');
  }
  return function (err) {
    if (err) {
      return callback(err);
    }
    return fn.apply(null, Array.prototype.slice.call(arguments, 1).concat(args));
  };
};

A test case:

it('continues to f2 but not to f3', function (done) {
  var msg = 'hello';
  var msgErr = 'some error';
  function f1 (a, callback) {
    expect(a).toBe(msg);
    callback(null, a);
  }
  function f2 (a, callback) {
    expect(a).toBe(msg);
    callback(msgErr);
  }
  var dummy = jasmine.createSpy('dummy');
  f1(msg, continueIf(f2, continueIf(dummy, function (err) {
    expect(err).toBe(msgErr);
    expect(dummy).not.toHaveBeenCalled();
    done();
  })));
});

That is basically what promises, and monads in the general sense, do. If you take the abstraction one step forward, you can generalize the idea of a continuation into a monad interface (using the FantasyLand spec):

function Cont(run) {
  this.run = run
}

Cont.of = function(a) {
  return new Cont(function(next) {
    next(a)
  })
}

Cont.error = function(msg) {
  return new Cont(function(_, error) {
    error(msg)
  })
}

Cont.prototype.chain = function(f) {
  return new Cont(function(next, error) {
    return this.run(function(a) {
      return f(a).run(next, error)
    }, error)
  }.bind(this))
}

Now your functions would have to return objects in that monad:

var msg = 'hello'
var msgErr = 'some error'

function f1(a) {
  console.log('f1', a === msg)
  return Cont.of(a)
}

function f2(a) {
  console.log('f2', a === msg)
  return Cont.error(msgErr)
}

// won't execute
function dummy() {
  console.log('dummy')
}

Then you can chain the continuations, and run the final computation, providing a callback for the success case, and an optional callback for the error case:

var chain = f1(msg).chain(f2).chain(dummy)

// chain.run(success, error)
chain.run(console.log, console.log)
// f1, true
// f2, true
// some error

Once you have this in place you can abstract regular callback-based functions to use the monad:

var cc = function(f) {
  return function() {
    var args = Array.prototype.slice.call(arguments)
    var cb = f.bind.apply(f, [null].concat(args))
    return new Cont(function(next, error) {
      cb(function(err, value) {
        if (err) return error(err)
        next(value)
      })
    })
  }
}

function f1(a, cb) {
  console.log('f1', a === msg)
  cb(null, a)
}

function f2(a, cb) {
  console.log('f2', a === msg)
  cb(msgErr)
}

var chain = cc(f1)(msg).chain(cc(f2)).chain(cc(dummy))

chain.run(console.log, console.log)
// f1, true
// f2, true
// some error

Of course you could apply cc as a decorator, instead of inline:

var f1 = cc(function(a, cb) {
  console.log('f1', a === msg)
  cb(null, a)
})

var f2 = cc(function(a, cb) {
  console.log('f2', a === msg)
  cb(msgError)
})


ForEachAsyncSemaphore asynchronously processing tasks but enable fast fail I know this has been done many times before, but this is my existing code taken from a sample I found to async process a list of tasks. I've seen Task.Run in use before in many examples, but I'm assuming that in most cases, the workload will be IO bound and therefore the Task scheduler is not clever enough to know whether to grab a thread pool thread rather than use TaskCompletionSource.
I also wanted the below extension method to fast fail, that is if there are 50 items being processed I want the whole thing to fail if just one task encounters an exception. I use a Task.Delay as a "marker" task to signal the whole batch to finish.  Is the below a good implementation and if so how can it be improved upon?
  public static async Task ForEachAsyncSemaphore<T>(this IEnumerable<T> items,
                                                      Func<T, Task> func,
                                                      int dop,
                                                      CancellationToken ct)
    {
        Exception capturedException = null;

        var childCts = new CancellationTokenSource();
        var linkedTokens = CancellationTokenSource.CreateLinkedTokenSource(ct, childCts.Token);

        using (var semaphore = new SemaphoreSlim(dop))
        {
            var cancellationTask = Task.Delay(Timeout.Infinite, linkedTokens.Token);
            var tasks = items.Select(async item =>
            {
                await semaphore.WaitAsync(linkedTokens.Token);

                try
                {
                    await func(item);
                }
                catch (Exception ex)
                {
                    capturedException = ex;
                    childCts.Cancel();
                }
                finally
                {
                    semaphore.Release();
                }

            }).ToArray();

            // Use cancellation task as signal to exit early if unexpected exception
            await Task.WhenAny(Task.WhenAll(tasks), cancellationTask);

            if (capturedException != null)
            {
                throw capturedException;
            }
        }

First let me share with you my revised version then I will detail the changes what I've made:
public static async Task ThrottledConcurrenctForeach<T>(
    this IEnumerable<T> toBeProcessedItems, Func<T, Task> asyncOperation,
    int maxDegreeOfConcurrency, CancellationToken externalCancellationToken)
{
    ExceptionDispatchInfo capturedException = null;

    var internalCancellation = new CancellationTokenSource();
    var internalCancellationSignal = new TaskCompletionSource<object>();

    var combinedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(externalCancellationToken, internalCancellation.Token);
    combinedCancellationTokenSource.Token.Register(() => internalCancellationSignal.TrySetCanceled(), useSynchronizationContext: false);

    using var throttler = new SemaphoreSlim(maxDegreeOfConcurrency);

    var processingOperations = toBeProcessedItems.Select(async item =>
    {
        await throttler.WaitAsync(combinedCancellationTokenSource.Token);

        try { await asyncOperation(item); }
        catch (Exception ex)
        {
            capturedException = ExceptionDispatchInfo.Capture(ex);
            internalCancellation.Cancel();
        }
        finally { throttler.Release(); }

    });

    await Task.WhenAny(Task.WhenAll(processingOperations), internalCancellationSignal.Task);
    capturedException?.Throw();
}

Naming
As you can see I've done a lots of renaming. I do believe that using the meaningful names improves readability and gives clarity. Here are a few examples:

ForEachAsyncSemaphore

Semaphore is an implementation detail

Try to focus on the provided functionality instead: throttling
You are using SemaphoreSlim not Semaphore that's why it's misleading

In case of I/O bound operations we are normally talking about Concurrency, in case of CPU bound operations we talk about Parallelism

If you want to emphasize your intent then make it visible to your
consumers

dop

This is a useless abbreviation

No-one will understand what that means unless (s)he reads the
implementation (if (s)he can access it)

Try to use as expressive parameter names as possible if you do not provide documentation comments

childCts

Yet again try to focus on the provided functionality

ExceptionDispatchInfo

If you don't want to change the StackTrace of the already thrown exception then please prefer ExceptionDispatchInfo
It preserves the original StackTrace with the re-throwability

CancellationToken's Register

Even though Task.Delay(Timeout.Infinite, linkedTokens.Token) works that's not the most elegant way to provide cancellation ability to Task.WhenAll
You can use the TaskCompletionSource<object> for signalling. It works fine with CancellationToken by combining Register and TrySetCanceled

using declaration

In order to reduce indentations (to streamline your code) please prefer using declaration if you are using C# 8 or greater


Linux bash script to ignore '$' Many times I have copy and pasted from a guide that had the following instruction:

$ echo "Execute xyz"

and would get the error

$: command not found

So I created a bash script to ignore the '$' at the beginning; The solution is as follows:

Put the below script in a file called '$'

vim $

Make it executable

chmod +x $

Add it to your bin

sudo cp $ /usr/bin/$

The script:
#!/bin/bash
eval $@

Cute idea. It's not so simple, though.
For example, compare the outputs of
echo 'a   b'

with
$ echo 'a   b'

(because of word splitting).
Also, it fails for compound commands like
for i in a b c ; do echo $i ; done

(because everything after the first ; is another command).
Some commands might fail depending on the contents of the current directory, e.g.
$ [[ a = ? ]]

(if it works, try running touch 1 2 3).


Code review request for fizz buzz Okay code reviewers, I need you to make my FizzBuzz code even better. Any suggestions for improvement, optimizations, etc.

public final class FizzBuzz {

    private FizzBuzz() {};

    /**
     * comparisons for a multiple of 3 = 2
     * comparisons for a multiple of 5 = 2
     * comparisons for a multiple of 15 = 2
     *
     */
    public static void fizzBuzzBad() {
        // most common thought process
        for (int n = 1; n < 20; n++) {
            if (n % 3 == 0) {
                System.out.print("fizz");
            } else if (n % 5 == 0) {
                System.out.println("buzz");
            } else {
                System.out.println("\n" + n);
            }
        }
    }

    /**
     * comparisons for a multiple of 3 = 2
     * comparisons for a multiple of 5 = 2
     * comparisons for a multiple of 15 = 1 <--- here is the savings.
     *
     * This saves the number of comparison for 15's.
     */
    public static void fizzBuzzBetter() {
        // most common thought process
        for (int n = 1; n < 20; n++) {
            if (n % 15 == 0) {
                System.out.print("fizzbuzz");
            } else if (n % 3 == 0) {
                System.out.print("buzz");
            } else if (n % 5 == 0) {
                System.out.println("fizz");
            } else {
                System.out.println("\n" + n);
            }
        }
    }

    public static void main(String[] args) {
        fizzBuzzBad();
        System.out.println("--------------------------------------------");
        fizzBuzzBetter();
    }

}

In both versions, your policy on printing newlines is inconsistent.  With fizzBuzzBetter(), all of your cases are mutually exclusive, so they should all be System.out.println(...) calls, and there is no need to prepend "\n".

Your fizzBuzzBad() is wrong: it won't print "fizzbuzz" for multiples of 15.

Anything that can easily be parameterized ought to be.  Instead of hard-coding 20, your functions should take a max parameter that is passed from main.


Clean up repeated file.writes, if/elses when adding keys to a dict I'm getting familiar with python and I'm still learning it's tricks and idioms.

Is there an better way to implement print_html() without the multiple calls to html_file.write()?  I only separated the HTML for clarity instead of putting the entire string inside write().

In get_car(), is there a way to avoid using if/else to make the dict key ' ' if listing.find() returns nothing?

Is there anything that is flat out wrong and should be avoided/improved?

#!/usr/bin/python

import requests
import bs4
import os

### Declarations

home         = os.environ['HOME']
filename     = home + '/results.html'
results_file = open(filename, 'w')

req = requests.get([url_removed])

soup     = bs4.BeautifulSoup(req.content)
listings = soup.find_all('div', class_='listing listing-findcar listing-dealer     ')
listings.reverse()

### Functions

def print_header(message, html_file):
    html_file.write(message)

def print_html(car, html_file):
    price    = '
' + car['price'].strip() + '
'
    image    = ''
    year     = '
' + car['year'].strip() + ' ' + car['trim'].strip() + '
'
    color    = '
' + car['color'].strip() + '
'
    miles    = '
' + car['miles'].strip() + '
'
    dealer   = '
' + car['dealer'].strip() + '
'
    phone    = '
' + car['phone'].strip() + '
'
    distance = '
' + car['distance'].strip() + '
'
    link     = '
[text_removed]
'

    html_file.write(price)
    html_file.write(image)
    html_file.write('
')
    html_file.write(year)
    html_file.write(color)
    html_file.write(miles)
    html_file.write(dealer)
    html_file.write(phone)
    html_file.write(distance)
    html_file.write(link)
    html_file.write('
')

def get_car(listing):
    car = dict()

    # Year
    if listing.find('span', class_='atcui-truncate ymm'):
        car['year'] = listing.find('span', class_='atcui-truncate ymm').get_text()
    else:
        car['year'] = ''

    # Trim
    if listing.find('span', class_='trim'):
        car['trim'] = listing.find('span', class_='trim').get_text()
    else:
        car['trim'] = ''

    # Price
    if listing.find('h4', class_='primary-price'):
        car['price'] = listing.find('h4', class_='primary-price').get_text()
    else:
        car['price'] = ''

    # Mileage
    if listing.find('span', class_='mileage'):
        car['miles'] = listing.find('span', class_='mileage').get_text()
    else:
        car['miles'] = ''

    # Color
    if listing.find('span', class_='color'):
        car['color'] = listing.find('span', class_='color').get_text()
    else:
        car['color'] = ''

    # Distance
    if listing.find('span', class_='distance-cont'):
        car['distance'] = listing.find('span', class_='distance-cont').get_text()
    else:
        car['distance'] = ''

    # Dealer
    if listing.find('span', class_='owner-title atcui-block atcui-bold'):
        car['dealer'] = listing.find('span', class_='owner-title atcui-block atcui-bold').get_text()
    else:
        car['dealer'] = ''

    # Dealer phone
    if listing.find('span', class_='owner-phone'):
        car['phone'] = listing.find('span', class_='owner-phone').get_text()
    else:
        car['phone'] = ''

    # Image
    if listing.find('img', class_='atcui-vehicle-image   '):
        car['image'] = listing.find('img', class_='atcui-vehicle-image').get('src')
    else:
        car['image'] = ''

    # Link
    if listing.find('a', class_='vehicle-title atcui-trigger '):
        car['link'] = listing.find('a', class_='vehicle-title atcui-trigger ').get('href')
    else:
        car['link'] = ''

    return car

### Main

print_header('
' + str(len(listings)) + ' CARS FOUND
', results_file)

for listing in listings:
    listing_dict = get_car(listing)
    print_html(listing_dict, results_file)

You can make the HTML a multi-line string with formatting templates (see this SO question for discussion on indentation, I've kept it simple):

def print_html(car, html_file):
    html = '''
{0[price]}

{0[year]} {0[trim]}

     ...

'''

Then your writing becomes a single line:

    html_file.write(html.format(car))

Note that this doesn't strip; you should do that when the data go into the dictionary.

There is a lot of duplication in get_car. Also, you do the find twice, which will slow you down. Try something like:

def get_car(listing):
   car = dict()
   data = {'year': ('span', 'atcui-truncate ymm'),
           ...}

   for key in data:
       element, class_ = data[key]
       result = listing.find(element, class_=class_)
       car[key] = '' if result is None else result.get_text().strip()

   return car

Finally, you aren't always following the style guide, e.g. lining up operators:

### Declarations

home = os.environ['HOME']
filename = home + '/results.html'
results_file = open(filename, 'w')


A graphing calculator I wrote a calculator that can solve equations with variables using matplotlib and sympy.
I am looking for tips on performance and style.
How can I improve on this calculator? Is it a good idea to use sympy for this purpose?
Repl.it
import sympy
import matplotlib.pyplot as plt
class Iterator:
  name, equation, val = None, None, None
  def __repr__(self):
    return self.__str__()
  def __init__(self, nameIn, equationIn, valIn):
    self.name, self.equation, self.val = nameIn, equationIn, valIn
  def __str__(self):
    return self.get_name() + "," + str(self.get_value())
  def iterate(self):
    replaced = self.equation.replace(self.name, str(self.val))
    self.val = sympy.sympify(replaced)
  def get_name(self):
    return self.name
  def get_value(self):
    return self.val
def graph():
  numTimes = input("How many points ")
  x = input("What to calculate ")
  points = []
  i = 0
  numInputs = int(input("How many variables "))
  j = 0
  replaced = []
  iterators = []
  while numInputs > j:
    name = input("What's the variabe name ")
    iter = input("What's the iteration equation ")
    val = int(input("What's the starting value "))
    iterators.append(Iterator(name, iter, val))
    j += 1
  while i < int(numTimes):
    replaced = x.replace("n", str(i))
    for j in iterators:
      replaced = replaced.replace(j.get_name(), str(j.get_value()))
      j.iterate()
    final = sympy.sympify(replaced)
    right_type = int(final)
    points.append(right_type)
    i += 1
  plt.plot(points)
  plt.ylabel(input("What's the y label "))
  plt.show()
while(True):
  command = input(">> ")
  if command == "graph":
    graph()
  elif command == "help":
    print("""
    msh.
    Used for graphing.
    Commands:
    graph: an interactive graphing calculator
    help: bring up this message
    quit: exit msh
    """)
  elif command == "quit":
    break

Indentation
You should use four spaces instead of two.
Spacing
Your class methods should be separated by an empty line, so everything doesn't look so crowded.
Getters
Since all class variables are public (except _var and __var variables), you don't need to define getters to access these properties. Simple do Iterator.name, etc.
Iterator.__str__
You should utilize f"" strings to directly include variables in strings.
def __str__(self):
    return f"{self.name},{self.val}"