Web-scraping investment fund data into Pandas

I created function get_soup because this code is offen used many times in code. In this code I could put all in get_data.

get_data gets url and uses get_soup with this url. Later it scrapes data from html, creates DataFrame and returns it

Main part uses get_data with two urls and gets two dataframes.

I put some other comments in code.

# <-- remove not used modules
# <-- use more popular names
from urllib.request import urlopen
from bs4 import BeautifulSoup as BS
import pandas as pd
from IPython.display import display

# --- functions ---
# <-- all functions before main part

def get_soup(url):
    # <-- more readable names (and more popular)
    response = urlopen(url)
    html = response.read()
    response.close()
    soup = BS(html, "html.parser")
    return soup

def get_data(url):

    soup = get_soup(url)

    #Isolate header to get name and symbol
    h1 = soup.h1

    #Isolate stock symbol
    # <-- find() to get only first item
    title = h1.find("span",{"class":"label-primary"}).text

    #print(title)

    #strip space and line break
    # <-- use the same variable instead strip,strip2, strip3
    # <-- maybe too much comments
    header = h1.text.strip()

    #strip stock symbol
    header = header.strip(title)

    #strip remaining line break
    header = header.strip()

    #print(strip)

    # <-- use better name 'table'
    table = soup.find_all("table",{"class":"chart base-table"})[1]

    #Create lists to fill
    #sector_data = [row.text for row in table.find_all("td")]
    sector_data = []
    # <-- remove lists which will be created later

    for row in table.find_all("td"):
        sector_data.append(row.text)

    #Assign every other value to proper list to get 2 columns
    sector_name = sector_data[::2]
    sector_weight = sector_data[1::2]

    #Insert name/symbol for clarification/validation
    sector_weight.insert(0, title)
    sector_name.insert(0, header)

    # create dataframe in pandas
    # <-- create DF directly with data
    df = pd.DataFrame({
       'Sector': sector_name,
       'Weight': sector_weight,
    })

    #display(df)

    return df

# --- main ---

# <-- the same variable `url` because it keep the same type of data
#     and I will no need this value later - so I can resuse this name.
url = 'https://etfdb.com/etf/IWD/'
df1 = get_data(url)

url = 'https://etfdb.com/etf/IWF/'
df2 = get_data(url)

results = df1.merge(df2, on="Sector")
results.columns = ['Sector', 'IWD', 'IWF']
display(results)


I'm personally not a fan of goto's and to be honest I don't even know if I've ever used on in c#.  I tend to find they are a bit hard to follow as you have to jump around a bit too much and the natural flow of the code is potentially broken.

Here's an idea for using a different approach (Updated as suggested by svick)

string errorMessage;
while(!TryTableFill(myDataTable, out errorMessage))
{
    DialogResult result = MessageBox.Show("Can't load data:\n" + errorMessage, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);

    if(result != DialogResult.Retry)
        break;
}

private boolean TryTableFill(myDataTable, out string errorMessage)
{
    errorMessage = string.Empty;

    try
    {
       oleDbDataAdapter1.Fill(myDataTable);
       return true;
    }
    catch (Exception ex) {
       errorMessage = ex.Message;
       return false;
    }
}

or alternatively (I like this way but I'm not sure if it's better.  Added just to provide an alternative):

private boolean TryTableFill(myDataTable, out string errorMessage)
{
    errorMessage = string.Empty;

    try
    {
       oleDbDataAdapter1.Fill(myDataTable);
    }
    catch (Exception ex) {
       errorMessage = string.Format("Error occured: {0}", ex.Message);
    }

    return !string.isNullOrEmpty(errorMessage);
}


Variable names

firstSum and secondSum variable names aren't really descriptive of their purpose. Let's give them names according to the algorithm description:

int sumExcludingMostRecentNonAdjacentElement = 0;
int sumIncludindMostRecentNonAdjacentElement = values[0];
int sumUpToPreviousElement = values[1];

That's too verbose and it makes sense to remove common parts:

int excluding = 0;
int including = values[0];
int upToPrevious = values[1];

for (int i = 2; i < values.length; i++) {
    int value = values[i];
    int upToCurrent = Math.max(excluding, including) + values[i];
    excluding = Math.max(excluding, including);
    including = upToPrevious;
    upToPrevious = upToCurrent;
}

Negative numbers

The algorithm is based on the following assumption:

  Taking the max of these two sums and adding them to the current value will produce the maximum sum for non-adjacent elements up to that index.

which doesn't take negative numbers into account:

if both of these sums are negative they should not be added to the result;
if the current value is negative it should not be added to the result.

To give an example, for the input 0, -1, -1 the method returns -1.

You can try to fix that by carefully keeping sums as positive and handling negative values:

int value = Math.max(values[i], 0);

You will also need to fix the initialization:

int including = Math.max(values[0],0);
int upToPrevious = Math.max(values[1], 0);

, and the trivial case:

if (values.length == 1) {
    return Math.max(values[0], 0);
}

As an alternative, you can replace negative elements with zeroes before processing.

Refactoring/understanding.

As far as I can see, the code should work correctly at this point. But both the algorithm description and the code are hard to follow with all these transitions:

int excluding = 0;
int including = Math.max(values[0], 0);
int upToPrevious = Math.max(values[1], 0);

for (int i = 2; i < values.length; i++) {
    int value = Math.max(values[i], 0);
    int upToCurrent = Math.max(excluding, including) + values[i];
    excluding = Math.max(excluding, including);
    including = upToPrevious;
    upToPrevious = upToCurrent;
}
return Math.max(including, upToPrevious);

Let's try to make code easier to understand step-by-step.

Currently, upToPrevious refers to the upToCurrent from the previous step. upToCurrent values for each iteration can be stored in an array, it saves us an assignment and hopefully helps us to derive a recurrence relationship between values:

int n = values.length;
//upToSums[i] refers to the upToCurrent value at the i-th step
int[] upToSums = new int[n];
upToSums[1] = Math.max(values[1], 0);
for (int i = 2; i < n; i++) {
    int value = Math.max(values[i], 0);
    upToSums[i] = Math.max(excluding, including) + value;
    excluding = Math.max(excluding, including);
    including = upToSums[i-1];
}
return Math.max(including, upToSums[n-1]);

Turns out, including referes upToSums[i-1] from the previous step, we can replace it with upToSums[i-2]:

upToSums[0] = Math.max(values[0], 0);
upToSums[1] = Math.max(values[1], 0);
for (int i = 2; i < n; i++) {
    int value = Math.max(values[i], 0);
    upToSums[i] = Math.max(excluding, upToSums[i-2]) + value;
    excluding = Math.max(excluding, upToSums[i-2]);
}
return Math.max(upToSums[n-2], upToSums[n-1]);

Now it's easier to see what's going on with excluding:

At step 2 excluding is 0.
At every other step excluding is the maximal element of upToSums from 0 to i-3.

Let's define an array for the maximum value of upToSums and calculate excluding based on that:

int[] maxSums = new int[n];
for (int i = 2; i < n; i++) {
    int value = Math.max(values[i], 0);
    int excluding = i==2 ? 0 : maxSums[i-3];
    upToSums[i] = Math.max(excluding, upToSums[i-2]) + value;
    maxSums[i-2] = Math.max(excluding, upToSums[i-2]);
}

This code still vaguely follows your algorithm description as "maximum sum from the preceding non-adjacent elements that excludes the most recent non-adjacent element" is somewhat equivalent to "maximum sum of non-adjacent elements for the sequence with the last three elements removed".

Notice, that maxSums[i-2] is calculated twice. We can reuse the result by changing the order of calculations:

    int value = Math.max(values[i], 0);
    maxSums[i-2] = Math.max(i==2 ? 0 : maxSums[i-3], upToSums[i-2]);
    upToSums[i] = maxSums[i-2]+ value;

It's weird that maxSums[i-2] is calculated at the i-th step. We can define starting values and calculate maxSums[i] right after upToSums[i]:

int[] maxSums = new int[n];
maxSums[0] = upToSums[0];
maxSums[1] = Math.max(upToSums[0], upToSums[1]);
for (int i = 2; i < n; i++) {
    int value = Math.max(values[i], 0);
    upToSums[i] = maxSums[i-2]+ value;
    maxSums[i] = Math.max(maxSums[i-1], upToSums[i]);
}

Also, we can use maxSums to return the result as the maximum is already calculated:

//maximal sum is already calculated
return maxSums[n-1];

Code so far (without trivial cases):

int n = values.length;
int[] upToSums = new int[n];
upToSums[0] = Math.max(values[0], 0);
upToSums[1] = Math.max(values[1], 0);
int[] maxSums = new int[n];
maxSums[0] = upToSums[0];
maxSums[1] = Math.max(upToSums[0], upToSums[1]);
for (int i = 2; i < n; i++) {
    int value = Math.max(values[i], 0);
    upToSums[i] = maxSums[i-2]+ value;
    maxSums[i] = Math.max(maxSums[i-1], upToSums[i]);
}
return maxSums[n-1];

At this point upToSums is never used and can be eliminated:

int n = values.length;
int[] maxSums = new int[n];
maxSums[0] = Math.max(values[0], 0);
maxSums[1] = Math.max(Math.max(values[1], 0), maxSums[0]);
for (int i = 2; i < n; i++) {
    int value = Math.max(values[i], 0);
    maxSums[i] = Math.max(maxSums[i-1], maxSums[i-2]+ value);
}
return maxSums[n-1];

At last we also separate the handling of negative numbers from the main algorithm that will work with an array of non-negative numbers:

//replacing negative values with 0
int n = values.length;
for(int i=0; i<n; i++) {
    if(values[i]<0) {
        values[i] = 0;
    }
}

//trivial case
if(n==0) {
    return values[0];
}

int[] maxSums = new int[n];
maxSums[0] = values[0];
maxSums[1] = Math.max(values[1], values[0]);
for (int i = 2; i < n; i++) {
    maxSums[i] = Math.max(maxSums[i-1], maxSums[i-2]+ values[i]);
}
return maxSums[n-1];

This code is logically equivalent to the previous version, but it's easier to follow. Actually, the main part of the algorithm (recursive formula) is written in the code itself:

  The largest sum of non-adjacent elements (maxSums(i)) for the first i elements of the list of non-negative numbers a is defined using the recurrence
  maxSums(i) = max(maxSums(i-1), maxSums(i-2)+a[i])
  with initial conditions:
  maxSums(0) = a[0]
  maxSums(1) = max(a[0], a[1])

Sum of zero elements

In its current form, the algorithm will return 0 for the list of negative numbers. This may or may not be acceptable, as the question doesn't state that the sum should contain at least one element. You'll need to clarify the particular requirements with your interviewer and, if necessary, write the code to detect this special case.


High performance exponential of a skew Hermitian matrix in Fortran 95

Your code looks good. When s is small, there should be no need to use BLAS instead of matmul(). I like you exploit the shorthand array notation and the forall construct.

Debatable is your use of explicit shaped arrays. They could be very slightly faster, because they are contiguous, but in many cases the compiler will have to copy the arrays when calling your routines and the overall code will be slower. The preferred way in modern code are assumed shape arrays, e.g.

 complex(complex_kind), intent(in) :: H(:,:)
 complex(complex_kind)             :: B(1:ubound(H,1),1:ubound(H,1))

but there may be reasons to use your way and it may be shorter.

Generally avoid using star notation real*4 *8 *16 for sizes of your variables. It is non-standard and obsolete. Use real(some_kind_constant).

You can use selected_real_kind() and selected_int_kind() (now preferred), to get the constants.

Or if you don't mind Fortran 2008 features and need to know the size in bits you can use kind constants from the iso_fortran_env module, like integer(int32), real(real64) and so on.

Another possibility from Fortran 2003 is to use kind constants from the iso_c_binding module, like integer(c_int) if you need interoperability with C code.


Well you got a lot going on so we will take it one step at a time.  The improve the performance you will need to use the TPL.  This should allow things to start to process in parallel. Word of warning I didn't test all this code as I don't have the Excel sheet or website so there might be minor bugs in it

first you have a lot going on for one method we should break them out to each one does a specific thing.  Also to make things easier we going to create a class to instantiate so we can keep some state.

Lets create a couple small classes, I'd nest them in the main class, to hold some state for passing around.

First one is to hold the ExcelData we care about

private class ExcelDataSink
{
    public int ExcelPosition { get; set; }
    public string UPC { get; set; }
    public double CurrentPrice { get; set; }
}

This will be better for anyone coming after you.  Like them I had to assume cPrice is the current price.  Here it's self explanatory what it is.

Next is a class to hold the Pricing Data we get back from the WebSite plus the info we need to update the Excel Document.

private class PricingDataSink
{
    public int ExcelPosition { get; set; }
    public IList<double> Prices { get; set; }
    public double CurrentPrice { get; set; }
}

Since we are going to try processing in parallel we will want to put a rate limiter on it.  We don't want to flood a website with over 80 request a second.  I'm going to make a constant that you can test with and tinker to see what works best for your situation. Also going to switch to the HttpClient instead of the WebClient and hard code the URL in a constant as well.

public class Pricing
{
    private readonly HttpClient _httpClient = new HttpClient();
    private const string URL = @"http://awebsiteservice/prices?SearchString=";
    private const int InFlight= 5; // How many request do we want to process at once

We need to read from the Excel file and get use data.  That should be it's own method. Excel is single threaded so no point trying to parallel this code

/// <summary>
/// check if the price hasn't been updated in over 5 days and if not reads the current price and upc
/// </summary>
/// <param name="worksheet">Wrk Sheet to use</param>
/// <param name="maxValue">row to end with in Excel Worksheet</param>
/// <returns></returns>
private IEnumerable<ExcelDataSink> GetExcelData(ExcelWorksheet worksheet, int maxValue)
{
    for (var i = 2; i < maxValue; i++)
    {
        if (worksheet.Cells[1, 8].Value == null ||
            DateTime.ParseExact(worksheet.Cells[i, 8].Value.ToString(), "M/dd/yyyy", null) <
            DateTime.Now.AddDays(-5).Date) //If analysis ran in last 5 days skip
        {
            yield return new ExcelDataSink()
            {
                ExcelPosition = i,
                CurrentPrice = double.Parse(worksheet.Cells[i, 2].Value),
                UPC = worksheet.Cells[i, 1].Value
            };
        }
    }
}

Going to drop out the console stuff as the TPL standard way to cancel is with a CancellationToken  Plus this class should only care about what it needs to do and not about displaying progress or reading keyboard input.  If you need to show progress I would recommending accepting the IProgress Interface and using that for reporting.

Now the TPL DataFlow Blocks are good at making a mesh for this kind of thing so I'm going to use that.  There are other options I just find this easier to understand.

Going to use the BufferBlock to put the data we read from Excel in.  You can kind of think of this as the queue that holds the data. To load the data into the Block we will create another method and mark the buffer complete when we are done reading from Excel

/// <summary>
/// Loads data from Excel into the target Block
/// </summary>
/// <param name="sourceBlock"></param>
/// <param name="workSheet"></param>
/// <param name="maxValue"></param>
/// <param name="token"></param>
/// <returns></returns>
private async Task LoadData(ITargetBlock<ExcelDataSink> sourceBlock, ExcelWorksheet workSheet, int maxValue,
    CancellationToken token)
{
    foreach (var sink in GetExcelData(workSheet, maxValue))
    {
        await sourceBlock.SendAsync(sink, token);
    }

    sourceBlock.Complete();
}

Now we need to change the ExcelDataSink to the PricingDataSink and make the web call

/// <summary>
/// Retreives new pricing from website
/// </summary>
/// <param name="excelDataSink"></param>
/// <returns></returns>
private async Task<PricingDataSink> GetWebPricing(ExcelDataSink excelDataSink)
{
    var content = await _httpClient.GetStringAsync(URL + excelDataSink.UPC);

    var xml = new XmlDocument();
    xml.LoadXml(content);

    return new PricingDataSink()
    {
        CurrentPrice = excelDataSink.CurrentPrice,
        ExcelPosition = excelDataSink.ExcelPosition,
        Prices = GetTrustedPrices(xml).ToList()
    };
}

private IEnumerable<double> GetTrustedPrices(XmlDocument xmlDocument)
{
    return xmlDocument.GetElementsByTagName("Store").Cast<XmlNode>()
        .Where(node => node.ChildNodes[2].InnerText == "Trusted")
        .Select(node => XmlConvert.ToDouble(node.ChildNodes[4].ChildNodes[2].InnerText));
}

Now we need to update the prices in the Excel File.

/// <summary>
/// Update the Excel file with the new lower price
/// </summary>
/// <param name="pricingSink"></param>
/// <param name="worksheet"></param>
private static void UpdatePrices(PricingDataSink pricingSink, ExcelWorksheet worksheet)
{
    var lowest = pricingSink.Prices.Where(p => p >= 1).DefaultIfEmpty().Min();
    if (lowest >= 1 && lowest < pricingSink.CurrentPrice)
    {
        worksheet.Cells[pricingSink.ExcelPosition, 6].Value = lowest;
        worksheet.Cells[pricingSink.ExcelPosition, 7].Value = lowest - pricingSink.CurrentPrice;
        worksheet.Cells[pricingSink.ExcelPosition, 8].Value = DateTime.Now.Date.ToShortDateString();
    }
}

Now we just need to wire everything up.  Going to make a method called UpdatePrice for the main entry point.

public async Task UpdatePrice(ExcelWorksheet workSheet, int maxValue, CancellationToken token)
{
    // This acts like our queue
    var buffer = new BufferBlock<ExcelDataSink>(new DataflowBlockOptions()
    {
        BoundedCapacity = InFlight, // put a constraint to not dump all data in at once
        CancellationToken = token
    });

    // marking our consumer of the queue to process one items at a time
    var executionOptions = new ExecutionDataflowBlockOptions()
    {
        BoundedCapacity = 1,
        CancellationToken = token
    };

    // List of task running
    var inFlight = new List<Task>();
    var linkOption = new DataflowLinkOptions()
    {
        PropagateCompletion = true
    };

    // create a consumer for the number we want inflight
    for (var i = 0; i < InFlight; i++)
    {
        // Call our code to transform the Excel into the pricing data
        var transform = new TransformBlock<ExcelDataSink, PricingDataSink>(
            async e => await GetWebPricing(e), executionOptions);

        // Call the code to update the sheet
        var updateExcel = new ActionBlock<PricingDataSink>(p => UpdatePrices(p, workSheet), executionOptions);

        // wire the mesh together
        transform.LinkTo(updateExcel, linkOption);
        buffer.LinkTo(transform, linkOption);
        inFlight.Add(updateExcel.Completion);
    }

    // Start the load data task
    inFlight.Add(LoadData(buffer, workSheet, maxValue, token));

    // Wait for everything to be completed
    await Task.WhenAll(inFlight);
}

I would highly recommend reading up on TPL and the TPL DataFlow blocks to understand what is all happening.   To run the code you will need to create a CancellationTokenSource and if you want to cancel it you cancel that the token source.


Optimizing string replacement program that uses recursion

Many strange numbers. Why is Tmp2 only 250 long?
Why is buffer static?
Why is Tmp2 not freed in the else branch? Most likely the malloc() should also only happen in the then branch.
The replace_str function should take a length argument for FinalStr, now this might cause a buffer overflow.
Your naming is very strange: Both TmpStr and Tmp2 are temporary strings, but they are named quite different. Better give them proper names.
if (TmpStr) uses implicit boolean conversion of pointers. That's bad practice.
Your variable naming convention is inconsequent, some are UpperCamelCase, some are lower case.
Why is there a call to puts? The function should work on a string, not print on stdout.
Now that I think about it: Using fixed size malloc is generally stupid.
Either make that dependent on the actual needs or just use char[256].
Also you might want to use calloc instead of malloc.
Potential buffer overflow in memcpy(Tmp2,TempStr,strlen(TempStr));
Potential buffer overflow in sprintf(buffer+(TempStr-SrcStr), "%s%s", RepStr, Tmp2+strlen(SubStr));
Also why are you using strncpy and sprintf for the same purpose (string concatenation)? You might want to look at strncat.
Unnecessary memory eating: The temporary strings are not needed for the recursion, better free before.
Similar for buffer.


The Box class has all public members and doesn't have any behavior (no methods). As such, it's a missed opportunity and could have been a simple struct.

Even worse, what's the point of a Box if any of length or width or height is undefined? These should all be required to construct a meaningful representation of a box.

The calculation of the surface and volume are inside the main method. This is not convenient, because you would have to re-type the formula every time you want to calculate this for every single box.

To remedy the above:

make the length, width, height variables private: code outside of the class should not modify them
make the length, width, height required at construction time: a box without these parameters doesn't make sense, and is not good modelization
I would go as far as making these variables constant: in real life, the width / height / length of a physical box is usually not editable
add surface and volume calculation logic as methods on the box, so that they can be reused for any Box object

A better interface for a Box would be:

class Box
{
public:
    Box(double length, double width, double height);
    inline double volume();
    inline double surface();
private:
    const double m_Length;
    const double m_Width;
    const double m_Height;
};

With implementation:

Box::Box(double length, double width, double height) :
    m_Length(length), m_Width(width), m_Height(height) {}

double Box::volume()
{
    return m_Length * m_Width * m_Height;
}

double Box::surface()
{
    return 2 * (m_Length * m_Width + m_Width * m_Height + m_Height * m_Length);
}

With the above, the main method can be rewritten as:

int main()
{
    Box box1(78, 24, 18);

    cout << "Volume of box1 = " << box1.volume() << endl;
    cout << "Surface Area of box1 = " << box1.surface() << endl;
}

Note that since the implementation of the constructor and the surface and volume methods are so simple,
it might be tempting to write them inside the declaration of the class, like this:

class Box
{
public:
    Box(double length, double width, double height) : m_Length(length), m_Width(width), m_Height(height) {}
    double volume() { return m_Length * m_Width * m_Height; }
    double surface() { return 2 * (m_Length * m_Width + m_Width * m_Height + m_Height * m_Length); }
private:
    double m_Length;
    double m_Width;
    double m_Height;
};

But this is a not a good practice.
The good practice is to cleanly separate the interface declaration from the implementation.
However,
while you're learning,
if this last version is easier for you,
then it's ok, you can do it this way, for now.
Just remember later as your understanding evolves,
to try to separate interface from implementation.

Finally, some other minor issues:

No need to write 18.0 when assigning to a double variable,
you could write simply 18.

No need to put parentheses around multiplication in (a * b) + (c * d),
a * b + c * d is equivalent.


Missing the specs

You're missing a very important rule of the game:

  The pattern gets longer each time the player completes the pattern. If the player presses a wrong button, the game ends.

You implemented only a one-shot round, which is not so interesting.

Mutually exclusive conditions

These conditions are mutually exclusive:

if n == 1:
    # ...
if n == 2:
    # ...
if n == 3:
    # ...

So you should use elif for the 2nd and later conditions.

Use consistent names

It's odd to name some constants with all caps, and others with all lowercase like this:

WHITE = (255, 255, 255)
RED = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)

Be consistent, name all of them all caps!

Avoid hard-coding

This function relies on having 4 buttons:

def generate_correct_sequence(length):
    return [random.randint(1, 4) for _ in range(length)]

It would be better to avoid hardcoding the number 4.
You could for example use len(COLORS) instead.
Even better would be to use a dedicated constant,
and derive both the elements of colors from it,
and use it everywhere else where it's relevant, as in this function.

Use local variables to explain logic

In this code:

if mouse_pos[0] < WIDTH / 2 and mouse_pos[1] < HEIGHT / 2:
    return 1

if mouse_pos[0] > WIDTH / 2 and mouse_pos[1] < HEIGHT / 2:
    return 2
# ...

mouse_pos[0] and mouse_pos[1] are repeated many times,
which is tedious, and not as clear as it can be.
I don't actually have tkinter now,
but I'm guessing these stand for x and y coordinates.

I would either change the method to take parameters named x and y,
or introduce the local variables x and y early in the method:

x = mouse_pos[0]
y = mouse_pos[1]

The rest of the code would become shorter, and clearer.

Why not use 0-base indexing everywhere?

This method is slightly more complex than it needs to be due to the 1-based indexing:

def draw_buttons():
    for i in range(1, 5):
        draw_button(i, COLORS[i - 1])

I bet you could change to this simpler implementation,
and adjust the rest of the code to work with it without sacrificing anything:

def draw_buttons():
    for i in range(4):
        draw_button(i, COLORS[i])

Notice the hardcoded number 4 again. This highlights again the importance to avoid hardcoding. The range(1, 5) came from the fact of the hard limit 4,
and if you decided to change 4 at some point, searching in the code for "4" you would never find range(1, 5).

So, really, replace the number 4 with a constant. len(COLORS) may look tempting, but as I explained above, I suggest to use something else as the single authoritative point of control.


An imitation of reading a C Struct from binary file in python 2.7

A major drawback of this design is that you cannot reuse a single instance of structure.structure to read multiple records.  Instead of s.fill(file_ptr), consider making a method s.read(f) that returns an OrderedDict of the deserialized data.  (By the way, mentioning "pointer" in a parameter name isn't very Pythonic.)  Note that that would also replace your .get_value() method with a dictionary lookup, which can be dealt with more idiomatically. (Or, at least implement dict-compatible iteration and subscripting in your structure class.)

Switching to a reusable .read() method would also eliminate a point of awkwardness in your code, which is that ._table is a dict of dicts, each having a "format" and a "value".

In your implementation of .fill(), why not call .unpack_from(file_ptr) instead of .unpack( file_ptr.read( self.get_size() ) )?

A more natural default endianness would be either big-endian (because that is the standard Network Byte Order) or host byte order (because that is a non-opinionated choice that also happens to be the path of least resistance).

Whatever you design, it should accommodate the opposite operation — serializing data to a file.

Consider allowing a structure to contain nested structures as fields.

The .show() method should probably be .__str__() instead.

I'm not convinced of the benefit of requiring multiple calls to .add_field() to set up the structure.  I'd prefer to have the constructor accept the field specification as a list of 2-tuples.  Not only would the calling code be cleaner, but the implementation of the structure class would be simpler as well if it is immutable.

I'm not a fan of your printed warnings.  Utility code, such as this, has absolutely no business contaminating sys.stdout, and ideally shouldn't print to sys.stderr either.  An end user would be completely clueless about what to do about these "warnings".  A programmer would also be puzzled about where these warnings came from (if it's a large program) and what actually happened as a consequence.  Rather, you should decide to either tolerate these situations gracefully or raise exceptions.  In my opinion…

"Field with name … already exists" should raise an ArgumentError.
"Format cannot be an empty string" should be tolerated (or perhaps an assertion failure).
"There is no such field" would be eliminated altogether based on my suggestion to return an OrderedDict.  If you had to offer a .get_value() method, then you should just let the KeyError propagate.


Is it acceptable to use a function property as its own private storage?

It works, but it's weird, and the variable intended for use only inside the function isn't private, which is potentially confusing and potentially unsafe.

Functions, being objects, can have arbitrary key-value pairs assigned to them, so you can put properties on it like you're doing. It's not unheard of. Usually, this sort of thing is reserved for static properties and methods which are associated with the class/function, but don't make sense as instance methods, and are meant for use by outside consumers. For a trivial example, the below function exposes a canEat property:

function Person() {
}
Person.canEat = ['Apple', 'Banana', 'Carrot'];
Person.prototype.eat = function(food) {
  if (!Person.includes(food)) {
    throw new Error('Invalid food');
  }
  // do stuff depending on what argument is
}

Maybe it'll change over time, so users of Person can check Person.canEat before calling eat. But this is all intended for users of Person.

Another example of a useful static property on a function intended for outside use is Number.MAX_SAFE_INTEGER.

But if a property isn't meant to be used outside of the function, putting it on the function itself both sabotages the variable's privacy and is confusing (because a reader may well expect that a property accessible anywhere may be intended for use anywhere, which isn't what you mean).

If you want the function to be able to store data persistently and for itself only, you can use a closure instead. Here's one option:

const storeFunctions = (() => {
  const funcArray = [];
  return (func) => {
    if (funcArray.includes(func))
      return 'function already added';
    else {
      funcArray.push(func);
      return 'a new function is added';
    }
  }
})();

function c() { return 1 };
function d() { return 2 };
console.log(storeFunctions(c));
console.log(storeFunctions(d));
console.log(storeFunctions(c));

Professional Javascript often uses modules to organize code, in which case the IIFE is not needed - instead, have the module that defines the storeFunctions declare an array private to that module, and export the function:

const funcArray = [];
export const storeFunctions = (func) => {
  if (funcArray.includes(func))
    return 'function already added';
  else {
    funcArray.push(func);
    return 'a new function is added';
  }
};

import { storeFunctions } from './storeFunctions';
function c() {return 1};
function d() {return 2};
console.log(StoreFunctions(c));
console.log(StoreFunctions(d));
console.log(StoreFunctions(c));

The above is what I'd prefer. For any non-trivial script, modules make organizing code much easier.

Another note on your code - unless you need a collection with rearrangeable indicies, rather than using an array and iterating over it with .includes, it would probably be more appropriate to use a Set, which is a generic collection of values and whose lookup time with .has is an order of complexity faster than an array's .includes.

const storeFunctions = (() => {
  const funcSet = new Set();
  return (func) => {
    if (funcSet.has(func))
      return 'function already added';
    else {
      funcSet.add(func);
      return 'a new function is added';
    }
  }
})();

function c() { return 1 };
function d() { return 2 };
console.log(storeFunctions(c));
console.log(storeFunctions(d));
console.log(storeFunctions(c));


Validating conditions for enabling sign-up button

You bind password but then in your return logic you use self.passwordTextField.text.isEmpty which seems to be wrong - but would get caught by countElements(password) >= 4 later.

This would be arguably more readable, and perhaps modular, if you introduced intermediate predicates.  Like such:

class Whatever {

  func hasName () -> Bool {
    return !self.firstNameTextField.text.isEmpty  &&
           !self.lastNameTextField.text.isEmpty
  }

  func hasEmail () -> Bool {
    return !self.emailTextField.text.isEmpty
  }

  func hasValidPassword () -> Bool {
    let password = self.passwordTextField.text.stringByReplacingOccurrencesOfString(" ", withString: "")
    return countElements(password) >= 4
  }

  func canSignUpButtonBeEnabled() -> Bool {
    return hasEmail() && hasName() && hasValidPassword()
  }
}


Using async / await with dynamic import() for ES6 modules

Approach 2 does not work because accoring to MDN:

import '/modules/my-module.js';

Is how you import a module for Import a module for its side effects only

  Import an entire module for side effects only, without importing anything. This runs the module's global code, but doesn't actually import any values.

So, await import('/path/to/my-module.js'); won't actually import your myfunction1 and myFunction2 for you to use them, however, if you put an IIFE in there, it will be called.

If you don't want to use long names to call your function, you can destructure them :

(async () => {

  const { myFunction1, myFunction2 } = await import('/path/to/my-module.js');

  myFunction1();
  myFunction2();

})();

Or, call them in my-module.js and just import it and they will get called.

See the answers here for es6 import for side effects meaning.


Ruby splits text into matrix creating a hash of counts of column 2

Sorry, but I didn't get your question on the shadowing part... Anyway, if you want to shorten it (and try to use something similar to each_with_object), you can do it this way, as a oneliner :)

pp data.each_line.with_object(Hash.new(0)) {|row,solution| solution[row.split(" ")[1]]+=1}

Except for the indirect handling with the double square brackets, I think that it is quite readable :)

Explanation (slightly changed by introducing an "index" temp variable for the sake of clarity)

pp data
 .each_line                                  #cycling line by line
 .with_object(Hash.new(0)) { |row,solution|  #using a Hash as accumulator, later called solution
     index=row.split(" ")[1]                 #setting the temp variable as the second column
     solution[index]+=1                      #increase the relevant counter in accumulator
 }


Couple of basic things with C#

Don't use Egyptian style Bracing, that's for Java.
Watch your indentation, I fixed it in the question.
Casing should follow basic C# standards

PascalCase for all Methods, Constants, and Property names
camelCase for everything else
Protected and public instance variables are UpperCamelCase (to avoid confusion with 3.2) as Jeroen commented

You have more than enough newlines in the code, this can be distracting in your code.
From what I see of your Comments they are all unneeded

old code should be deleted
other comments are not needed to explain the flow of code or the purpose of the code because it is apparent what is going on with your code.

With these Variables I am not sure what was meant by the Underscore on them, they don't look private or look like properties, they should have a more meaningful name as well

   int _l;
   int _r;

   // We are only choosing 2 values out of any list of numbers
   int[] two_list = new int[2];

This seams like a waste of time to me, you want two numbers, not an array of numbers and not a list of numbers, so you should have two numbers.

int number1;
int number2;

and these numbers should not be written outside of the double loop, this brings me to the conclusion that they are not needed at all, and that you should write this:

        for (int i =0; i < stack.Count; i++) {

            two_list[0] = stack[stackLast];

            two_list[1] = stack[i];

            result.Add(new int[] {two_list[0], two_list[1]});

        }

instead like this

for (int i =0; i < stack.Count; i++)
{
    result.Add(new int[] {stack[stackLast], stack[i]});
}


Update SQL database with using statement and SqlParameters

Stacking the using statements

I am particularly difficult to please with code that has nesting, and I highly prefer to stack the usings when it is possible.

using (SqlConnection connection = ...)
{
    using (SqlCommand command = ...)
    {
        ...
    }
}

versus

using (SqlConnection connection = ...)
using (SqlCommand command = ...)
{
   ...
}

Exception Handling

You should include exception handling into your code, as it will likely throw an exception sooner or later (such as getting disconnected, network issues, etc.)

using (SqlConnection connection = ...)
using (SqlCommand command = ...)
{
    try
    {
        // open the connection, execute, etc
    }
    catch
    {
        // log and handle exception(s)
    }
}

Using Lists not Arrays

It would likely do you more harm than good to be using an array, especially in the context of maintainable code. I would recommend you switch to using Lists, like this:

List<SqlParameter> p = new List<SqlParameter>();
p.Add(new SqlParameter("@type", newType.Text));
p.Add(new SqlParameter("@name", newName.Text));
p.Add(new SqlParameter("@image", newImage.Text));
p.Add(new SqlParameter("@id", id));

This will let you introduce more items into your parameters without having to do deal with the headaches of explicitly indexing.

Note: If you want to keep the same method signature for your GetExample() method, then don't forget to pass the parameters as an array --- you could use ToArray() to accomplish that.

Here it all is together...

string query = "UPDATE [guitarBrands] SET type = @type, name = @name, image = @image WHERE id = @id";

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["brandsConnection"].ToString()))
using (SqlCommand command = new SqlCommand(query, connection)
{
    try
    {
        // open the connection, execute, etc
        List<SqlParameter> p = new List<SqlParameter>();
        p.Add(new SqlParameter("@type", newType.Text));
        p.Add(new SqlParameter("@name", newName.Text));
        p.Add(new SqlParameter("@image", newImage.Text));
        p.Add(new SqlParameter("@id", id));

        connection.Open();
        GetExample(command, p.ToArray());
        command.ExecuteNonQuery();
        command.Parameters.Clear();
    }
    catch
    {
        // log and handle exception(s)
    }
}


There is a reason why you can write LINQ queries using the query syntax or the method syntax: sometimes one is better and sometimes it's the other one.

So, when you're writing a new query, use the syntax that suits your query better. This usually means method syntax for simpler queries (especially if they're as simple as your example) and query syntax for more complicated queries (especially ones with multiple sources).

When you need to use an operator that's only available in method syntax (like SingleOrDefault()), I think that points towards using method syntax for the whole query. But using both syntaxes in the same query can sometimes make sense too.

When it comes to rewriting old code: if it works and it's reasonably readable, I would leave it be, I think the effort spent rewriting it isn't worth it.


Because of the 'I must not use classes / methods etc. Only Control Flow Statements' I will try to not give comments to that direction.
Here are some advices

Input checking

Integer.parseInt(scanner.nextLine())

This will crash when someone enters anything but a digit (eg. 'a'). You should add exception handling for this

way to keep things going

while (true)

I would personally go for while(isRunning), as it will allow you to end the loop without the need for the "return" statement in the switch.

usage of var

var number = ThreadLocalRandom.current().nextInt(origin, bound);
int newOrigin = Integer.parseInt(scanner.nextLine());

You are mixing the usage of var and int when reading from the command line (which is technically ok) but I think consistency is also important in code.

default case

There is no default case in the switch. That would for example allow you to handle people entering number 6

changing variables in printing output

System.out.println(currentAttempts-- + " attempts left:")

This gets really tricky when you would use a logging framework and suddenly the user decides to switch of the level of logging.
I would go for System.out.println is to print current values, and to change values before/after (whatever is appropriate)

coding style

if (input == number)
    break;
else
   System.out.println ...

I would advice to always put {}, even if the next line is a simple instruction. This will avoid any human  reading mistakes.

eg.

if (<somecondition>)
    dosomething();
    break;
else
   System.out.println ...


First off, don't print every single loop. It's wasteful and surprisingly intensive. Also, you don't need to return all primes, your brief only requires one. You should make it more clear what the function is for. Return the last found prime, probably also rename your function to prime.

Don't omit 2, it's only a strange and confusing work around that you haven't even explained in the code. Just include it in the initialisation of primes.

primes = [2]

Also you don't need to do

for i in range(len(primes)):
    if attempt % primes[i] == 0:

Python has a simpler for var in iterable syntax that lets you get the values directly, which is more efficient.

for i in primes:
    if attempt % i == 0:

But rather than using a for loop at all you can use all. It employs short circuiting and will immediately stop iterating over the loop when it finds any value that has an invalid factor. This is faster than breaking when a prime is found, as primes are much rarer than non primes especially when you get into the high values.

def primes(n):
    primes = [2]
    attempt = 3
    while len(primes) < n:
        if all(attempt % prime != 0 for prime in primes):
            primes.append(attempt)
        attempt += 2
    return primes[-1]

Note, you also forgot to increment attempt when a prime was found. That means that every time you found a prime, it then had to be checked yet again in a very costly way since only the last element in the list would invalidate it.


Prime number sieve using difference of two squares

Good comments make code more maintainable. I had to reverse engineer what this code was doing, when a few comments could have made it clear.

For my taste the code could use a lot more whitespace, but I'm going to try to stick to your style in the proposed changes.

Separation of responsibilities: it would be cleaner to return a vector rather than to "return" with print.

There's a lot in common between the two main loops:

for(x=3,floor(sqrt(g)),
   if(Mod(x,2)==1,i=0,i=1);
   forstep(y=i,x-3,[2],
      A[(x^2-y^2-1)/2]=0));
for(x=ceil(sqrt(g)),ceil((g+9)/6),
   i=ceil(sqrt(x^2-g));
   if(Mod(x+i,2)==0,i=i+1);
   forstep(y=i,x-3,[2],
      A[(x^2-y^2-1)/2]=0));

It might clarify things to refactor them into one:

for(x=3,floor((g+9)/6),
  forstep(y=x-3,0,-2,
    if(x^2-y^2>g,break);
    A[(x^2-y^2-1)/2]=0
  ));

Note that this fixes an out-by-one in the upper bound of x.

Now I'm ready to look at performance. The low-hanging fruit here is x^2-y^2: it's usually faster to calculate a constant product or square of a loop index by addition. I'm also going to use bitwise operations for multiplication and division by 2 because that means not having to worry about how clever the compiler is.

for(x=3,floor((g+9)/6),
  d=(x<<1)-1;
  forstep(y=x-3,0,-2,
    d+=(y+1)<<2;
    if(d>g,break);
    A[d>>1]=0
  ));

You should easily be able to prove the invariant that d == x^2 - y^2, and since it's odd (d-1)/2 == d>>1. In fact, we can instead work with (x^2-y^2)>>1:

cap=(g-1)>>1;
for(x=3,floor((g+9)/6),
  d=x-1;
  forstep(yp=(x-2)<<1,2,-4,
    d+=yp;
    if(d>cap,break);
    A[d]=0
  ));

With Try It Online! and pushing towards the size limit for allocation of A I see a speedup from 4.4s (user) to 3.5s. (Output removed for timing purposes. Timings available by expanding the "Debug" section).

Other optimisations:

If the vector is just initialised as vector((g-1)>>1) then it will initially contain 0 at all indices. Then change A[d]=0 to A[d]=1 and change the logic in the output loop.
Ditch the concat and output 2 as a special case.

Beyond that, you need to look at different algorithms. The binary quadratic form x^2-y^2 doesn't seem to be a common one in prime sieving. You could see how it compares with Sundaram's sieve (BQF x+y+2xy) or failing that, if you're working with large enough ranges, Atkin-Bernstein (e.g. a combination of 3x^2+y^2, 4x^2+y^2, 5x^2+3y^2, 15x^2+y^2, and 3x^2-y^2 for different totients of 60).


How can I make this CSV Importer code better of any code smell?

I like that you already extracted a WebCsvImporter Plain Old Ruby Object which makes the code already a lot easier to understand and refactor.
Here is a refactored solution which I will explain in more detail:
class CsvImporter
  Result < Struct.new(:merchants) do
    def valid_count
      @valid_count ||= merchants.count(&:persisted?)
    end

    def invalid_count
      @invalid_count ||= total - valid_count
    end

    def total
      @total ||= merchants.count
    end

    def errors
      merchants.map do |merchant|
        merchant.errors.full_messages.to_sentence
      end.to_sentence
    end
  end

  def initalize(url)
    @url = url
  end

  def call
    merchants = valid_rows.map do |row|
      Merchant.create(
        roles: [Role.find_or_create_by(name: 'admin')],
        name: row[:name],
      )
    end

    Result.new(merchants)
  end

  private

  attr_accessor :url

  def valid_rows
    CSV.parse(data, headers: true, header_converters: :symbol).select do |row|
      row[:name].present? && row[:email].present? && row[:status].present?
    end
  end

  def data
    @data||= URI.open(url).read.force_encoding('UTF-8')
  end
end

Note that I removed the p prints and return a result object instead. You can use this result object to print the output in different formats or silence. Another solution could be to write to a logger object but you should avoid using puts as it will make testing and reusing of this class harder.
Here are a couple of suggestions:
Replace Temp with Query
Replace Temp with Query is a standard refactoring method which often improves the code quality a lot. Here we use the temp variable to a method which will also remove the need for a method parameter of parse_csv.
def data
  @data||= URI.open(url).read.force_encoding('UTF-8')
end

Extract helper methods
Extrating helper methods is also a standard refactoring method. Your parse_csv method is quite long and complicated so we should think about splitting it apart. One thing I did was extracting skipping invalid rows like this.
def valid_rows
  CSV.parse(data, headers: true, header_converters: :symbol).select do |row|
    row[:name].present? && row[:email].present? && row[:status].present?
  end
end

Don't use to_h to create object
Rather than using to_h and pass the all params to the object creation I would rather only pass in allowed parameters. I just used name as example here. It is problematic to pass all parameters in as it could easily break your application (e.g. CSV and database always need to have the same format) or worst case this could be a security issue (e.g. Merchant has an is_admin or password attribute which could get overwritten) without noticing. Specifying which params are allowed here seems like a good idea.
Result object
I return a result object and do the counting logic in the result object to make it simpler. This also has the advantage to move the printing / output to a different layer making testing a lot easier. For instance this test
def test_valid_count
  result = CsvImporter.new('example.com').call

  assert_equal(1, result.valid_count)
end

seems a lot easier and robust than
def test_valid_count
  io = capture_io do
    CsvImporter.new('example.com').call
  end

  assert_equal("Imported 1 merchant, 1 duplicate rows ain't added in total", io)
end

Make attr_accessor private
If the attr_accessor are not accessed externally you should move them to be private.
Hope that helps, let me know what you think.
Update

Reading a little further, I am concerned about performance issues when the data I want to pull is large and save into database. An example is CSV.parse which surely will result into issues when the csv dataset is large. Though I do not know the .select you append to it will solve that issue.

Reading files generally is quite fast. If you have of course 100s of millions of rows this can become a performance bottleneck. Generally I would advise to do benchmarks with your input files to see if this is really a bottleneck rather than premature optimisations.
However, here are a few ideas what you could do:

Divide & Conquer: Rather than having one single CSV file you could split the file into many files and start an import worker for each of them. Ideally this would happen already when generating the CSV files because then you also distribute the download time (rather than e.g. download 1GB you download 10 * 100MB).

Read the CSV file line by line

Also, I think I should move row[:name].present? && row[:email].present? && row[:status].present? to a separate predicate method.

Personally I don't think this will improve much readability because you would end up with either having a method with parameter or need to extract another object which both are not ideal.
# need a method parameter
def valid_row?(row)
  row[:name].present? &&
    row[:email].present? &&
    row[:status].present?
end

# extract object
class Row
  def initialize(params)
    @params = params
  end

  def valid?
    params[:name].present? &&
      params[:email].present? &&
      params[:status].present?
  end
end

Row.new(row_params).valid?

Also what happens if those rows are not present.

This shouldn't be a problem as nil.present? will return false.


Code style: Should prints be high or low level?

This is a basic but important question.
Most functions can be arranged to operate purely in the realm of computation:
they take data as input, return data as output, and have no side effects in the
"real world". Examples of side effects would include interacting with the file
system or a database, getting user input, and printing.
Basically anything that exists outside of the tidy confines of the computer's
"brain".
Functions with side effects tend to have trickier failure scenarios and they
are harder to test in an automated way (for example, with typical unit testing
frameworks) without jumping through various hoops.
That's why most people advise you to aim for a design where side effects like
printing end up in a thin outer layer of your program (for example, in main()
or a close sibling) rather than in lower-level, computation-heavy functions.
Keep as much of your code base as you can in the realm of pure computation.
Gary Burnhardt's talk on this
subject is excellent.
Of course, every best practice has its exceptions. Don't become a slave to
abstract rules. For example, programs that are designed to run persistently in
response to requests (such as a web service) might have very strong reasons to
want printing in many locations -- for debugability, auditing, and
other reasons. In those situations, you definitely want to use a logging
framework. Those frameworks have configuration options that allow them to play
nicely with automated testing and other development needs. But even in that use
case where we have decided to allow side effects deeper than the top level, the
same general principles apply: put the logging as high in the call stack as you
can, consistent with other needs. For example, in a classic web service, my
default would be to put logging in the methods directly handling incoming
requests, not in various utility/helper functions than can be focused purely on
data-oriented computation. Drawing those lines is a judgment call, of course.
And, as shown in Burnhardt's demo, sometimes initial judgements are incorrect,
and a design can be refactored without too much difficulty to narrow the
proportion of a code base that deviates from pure data-oriented computation.
The main point is to avoid casually scattering side effects across your code
base. Allow side effects only when you don't have an alternative. And if you
can wrap the side effect in a tool designed to lessen the costs (such as a
logging framework) do that as well.


I spent clearly too much time on this... Main ideas:

your understanding of how the payment on a fixed mortgage is computed was wrong. The payment is computed once at the beginning and remains the same regardless of prepayments, which is why people who choose to prepay end up repaying their whole mortgage before the scheduled term, as the example below should show.
I added pmt as an argument to the function as it is best practice for a function to not use any variable defined outside its scope (the dreaded global variables)
A big no-no in your code was the use of constructs like capital <- append(capital, currentCapital). At each iteration, a new and longer vector is created. This is slow and uses a lot of memory. Granted, you would need a very long mortgage to see the negative effects but it's good to learn to implement this the right way. You could a) pre-allocate a vector of length term.in.months and replace its value like this: capital[month.idx] <- currentCapital. Or b) use a list: lists use pointers in memory so they don't suffer the issue I was describing as they grow in size. My implementation uses this b) approach where I grow a list named db. At the end, I am turning this list of monthly data into a data.frame.
You'll see I use things like 12L instead of 12. That's the difference between integers and numerics. It is recommended to use integers when possible as it can prevent (rare) issues regarding floating point errors. It uses less memory and can sometimes make your code faster. Again, you won't notice it here, but it is a good practice.
Regarding @janos' comment about trying to avoid for loops. I agree, R works best and faster when vectorization can replace loops. However, your particular algorithm is a case of an iterative (state i+1 depends on state i) process so it cannot be vectorized. Whereas vectorization can replace loops only in processes where each iteration can be computed independently. So you will find that my answer still has a loop, in the form of a while().
I modified the variables' names to be closer to the official language. (I work in finance.)

mortgage <- function(property.value = 1e6,
                     downpayment = 0.10,
                     interest.rate = 0.04,
                     term = 25L,
                     prepayments = NULL,
                     pmt.fun = pmt) {

   original.balance <- property.value * (1 - downpayment)
   monthly.rate     <- interest.rate / 12L
   term.in.months   <- term * 12L
   monthly.payment  <- -pmt.fun(monthly.rate, nper = term.in.months,
                                              pv = original.balance)
   db <- list()
   month.idx <- 1L
   start.balance <- original.balance

   while(start.balance > 0) {

      balance.due       <- start.balance * (1 + monthly.rate)
      payment.due       <- min(monthly.payment, balance.due)
      prepayment        <- if (is.null(prepayments)) 0 else
                           min(sum(subset(prepayments, month == month.idx)$payment),
                               balance.due - payment.due)
      total.payment     <- payment.due + prepayment
      interest.payment  <- start.balance * monthly.rate
      end.balance       <- balance.due - total.payment
      principal.payment <- start.balance - end.balance

      db[[month.idx]] <- c(month             = month.idx,
                           start.balance     = start.balance,
                           balance.due       = balance.due,
                           payment.due       = payment.due,
                           total.payment     = total.payment,
                           prepayment        = prepayment,
                           interest.payment  = interest.payment,
                           principal.payment = principal.payment,
                           end.balance       = end.balance)
      month.idx     <- month.idx + 1L
      start.balance <- end.balance
   }
   as.data.frame(do.call(rbind, db))
}

source("http://pastebin.com/raw.php?i=q7tyiEmM")  # for pmt()

overPayments <- data.frame(
   month   = c(20,    25,    30,    35,    40),
   payment = c(20000, 20000, 20000, 20000, 20000))

cap1 <- mortgage()
cap2 <- mortgage(prepayments = overPayments)

par(mfcol=c(1, 2))
plot(c(cap1$start.balance, 0), type="l",
     main = "Without Prepayments", xlab = "Month", ylab = "Balance")
plot(c(cap2$start.balance, 0), type="l",
     main = "With Prepayments",    xlab = "Month", ylab = "Balance")


Do you have only a limited number of metric types?

In which case, you might want to make a union case of the possible types.
The compound cases can be recursive, referring to the type.

type Metric =
    | Revenue of string * string
    | Volume  of string * string
    | Compound  of string * Metric list

Then your formatMetric function needs to handle each case separately

let formatMetric metric =
    match metric with
    | Revenue (label,formula) ->
        sprintf "%s: {%s}" label formula
    | Volume  (label,formula) ->
        sprintf "%s: {%s}" label formula
    | Compound (label,metrics) ->
        sprintf "%s: {%s}" label "other metrics"

Note that that if you use this approach you don't really need the label any more, as the cases are the label.

type Metric =
    | Revenue of string
    | Volume  of string
    | Compound  of Metric list

And then you could also make formatMetric recursive, printing all the child formulas for a compound formula

let rec formatMetric metric =
    match metric with
    | Revenue formula ->
        sprintf "Revenue={%s}" formula
    | Volume  formula ->
        sprintf "Volume={%s}" formula
    | Compound metrics ->
        let childFormats =
            metrics
            |> List.map formatMetric
            |> List.reduce (+)
        sprintf "Compound: {%s}" childFormats

The formatting is not exactly what you want, but I'm sure you get the general idea.

EDIT

General Comment

Personally, I wouldn't add those member functions to the type. I would focus on designing the types in their own module independent of any particular representation, and then have a separate module that mapped them into the appropriate representation for MongoDB.  That way you are not mixing up different requirements.

String join.

A simple string join is trivial to write:

let stringJoin sep list :string =
    let append s t = s + sep + t
    list |> List.reduce append

stringJoin ", " ["a"; "b"; "c"; ]

Separate the types from the representation

But if you separate the types from the representation, you may find that you naturally create MongoDB specific functions that generate the appropriate representation. The idea is to build up a mini-language that you can compose together to make bigger blocks.
I don't know the MongoDB syntax, but maybe something like this?

let mSum p = sprintf "{ $sum: ""%s"" }" p
let mDivide p q = sprintf "{ $divide: [""%s"", ""%s""] }" p q
let mLabel p q = sprintf "%s : %s" s
let mGroup id ps = sprintf "$group: { %s, %s }" id (stringJoin ", " ps)
let mProject p q = sprintf "$project: { %s }" (stringJoin ", " ps)

If you are doing a lot of this, you might be better off using a JSON library. In which case the workflow would be:

domain type ==>  convert to intermediate type for Mongo API ==> convert to JSON


AES-256-CBC encrypt and decrypt files in Python

def pad(s):
    return s + b"\0" * (AES.block_size - len(s) % AES.block_size)

Generally a good library should have you covered when it comes to padding and unpadding. Prefer the Python 3 bytearray types rather than the stringified b"\0" trick.

Worse, you're implementing Zero padding, which would strip off zero valued bytes from your text files. That may not harm text files much (well, unless UTF-16LE I suppose), but for binary files that's very very dangerous.

Everybody tends to use PKCS#7 compatible padding for CBC mode.

def encrypt(message, key, key_size=256):

The key size is usually required for C-style languages but not here. You're already giving it a key, and the key has a certain size. The only thing you can do is to test the key against the key size if you're (overly) afraid that you might pass a 128 bit / 16 byte key by accident. But in general you should not pass parameters that can be derived from other parameters anyway.

    message = pad(message)

This creates a copy of the message. Now that's all right for small messages, but for files over 4 GiB it becomes "a bit of a nuisance". Especially if you decide to use your logical cores to encrypt many files. In theory AES/CBC pads the messages, but in practice we only pad the last 16 byte block that we need to encrypt.

    iv = Random.new().read(AES.block_size)

It's not all gloom and doom, you used a cryptographic random number generator to generate an unpredictable IV and you used AES.block_size instead of 16. And then prefixed it to the ciphertext. Well done!

    return plaintext.rstrip(b"\0")

And there's the Zero padding problem being executed. If you have a file consisting of all zero's, it would empty entirely. Oops!

    plaintext = base64.b64encode(plaintext)

Ah yes, that saves us from the padding problem, as base 64 doesn't contain zero valued bytes. The disadvantage is of course that your ciphertext will now expand 33%, and that you create another copy in memory for that size. Sure, the raw plaintext can now be freed up, but your system will still not be happy with you.

    with open("file.txt" + ".enc", 'wb') as fo:

The "file.txt" name is a bit out of wag with the 'wb' flag, don't you thing? And besides, although you base 64 encoded your plaintext, you didn't do it with the ciphertext. And you should not need to, just keeping everything in binary is fine for files.

Besides that, you probably should do something with a filename parameter. At least create a note to fix things later when you're experimenting.

    with open(file_name[:-4]+ ".dec", 'wb') as fo:

Ah, we just replace the last 4 characters now, without checking if the filename even has an extension?

    return dec.decode()

What, why? And what are we decoding?

key = b'1234sfsfdsafasdf'

Strings are not keys. If you want to create a test key, create one by decoding hexadecimal encoded bytes. And those look like 16 characters which means AES-128 rather than 256. If you want to use a password, you need a password based key derivation function (to perform so called Password Based Encryption, see the PKCS#5 standard).

CBC mode is fine for keeping files confidential, but generally cryptographers prefer authenticated modes such as GCM or EAX nowadays. Your files are likely kept confidential, but there is no message integrity or authenticity implemented for the files. Note that CBC mode is entirely unsuitable to send unauthenticated messages over a network.

Files can be large, so generally we try and use an API that provides update and final methods or a streaming API (not to be confused with the update method specified for pycryptodome's AEAD ciphers). That way you can encrypt files piecemeal.

Quite often for files it makes sense to use a specific container format such as CMS or PGP. In that case most of your errors are avoided (although, truth to be said, many container formats are starting to show their age).


In Python, there is a predefined function sum.  This completely negates your need for grades_sum(scores).

def grades_average(grades):
    return sum(grades) / float(len(grades))

Also, you do not need to cast len(grades) to float if you are using Python 3.


First of all, you're using a wrong algorithm. A depth-first finds some path, but not necessarily the shortest one. What you actually need is a breadth-first search. You can just use a queue instead of a stack to fix it.

Storing everything as a string is bad idea. You code'll be much better if you use appropriate data types. The position on the board or a shift vector for one move is not string. I'd suggest to create a separate Position class that holds a pair of integers to represent a position on the board and has a shiftBy and an isValid method (to get the next position after one move and to check if a position is valid).

I'd also recommend to separate the code that reads the data and prints the result from the part that actually runs the computations.

Fixing the indentation and spacing of code according to the Java coding convention would make it more readable.

The names of the variables are also not very descriptive. For instance, what does ele mean? What is str (which is, in fact, not really a string)? I have no clue. It should be something like currentPosition and shiftVector because that's what it actually is.


Find items that appear the same number of times in two collections

Nicely done, an efficient solution to the problem, easy to read and unit tested. For the sake of completeness, it would be good to include time and space complexity analysis. My main objections are concerning the unit testing technique and naming.

Unit testing

Each test method should have a single, clear purpose. The test methods here essentially pick 3 example inputs, but it's not clear how you picked those examples.

In general, tests should aim to cover cases like:

Happy paths: simple execution paths that run with success
Interesting corner cases
Invalid paths: invalid, tricky, malicious inputs

All methods should have names that describe their purpose. In this particular example, it would be great to add test methods for these cases:

Should get 0 when the collections have nothing in common
Should get size if the collections have identical content
Should get 0 if either ecollection is empty
Should get size of collection if the other is superset

Btw I would use these statements as test method names, with spaces replaced with underscores.

Simplify

In the tests, instead of a List<String>, it would be slightly simpler to use a List<Character>.

Naming

Some of the names are a bit overcomplicated. I suggest some simplifications:

countItemsInCollection: as collection is already implied by the parameter type, I would drop that word from the name
compareMapForMatchingKeyAndValue: a method that starts with the name compare gives the impression that it might follow the contract of comparators, returning either of -1, 0, or 1. So it's a bit misleading. Since this method effectively calculates the intersection of map entries, I would call it intersectEntries, or even just simply intersect
mapItemToNumberOfOccurance: how about simply counts, or itemCounts


The last time I checked (which admittedly has been a while) all major Linux distributions except Ubuntu have a default setup in which sudo is installed, but not configured to be able to start arbitrary applications. So on those your script will fail.

Apart from that I think it's a bad idea to use split like this. This will break if the python file (or the path to it if it was invoked with a path) contains spaces. I'd do it like this instead:

if sudo:
    root = ["/usr/bin/sudo"]
else:
    root = ["/bin/su", "--command"]
command = root + [ sys.argv[0] ]

A further problem is that you're requiring the script to be marked as executable. I think it'd be a better idea to use sys.executable to get the python interpreter and invoke that.


White Space

This is hard on my aging eyes:

static String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
    ".--","-..-","-.--","--.."};

This is much easier to read:

static String[] morse = {
    ".-",   "-...",  "-.-.",  "-..",   ".",     "..-.",  "--.",   "....",  "..",   ".---",
    "-.-",  ".-..",  "--",    "-.",    "---",   ".--.",  "--.-",  ".-.",   "...",  "-",
    "..-",  "...-",  ".--",   "-..-",  "-.--",  "--.."
};

alpha[i].charAt(0)

In the following, you use create 26 strings, and call .charAt(0) on each:

static String[] alpha = {"a","b","c","d","e","f","g","h","i","j","k",
    "l","m","n","o","p","q","r","s","t","u","v",
    "w","x","y","z"};
static String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
    ".--","-..-","-.--","--.."};

private static HashMap<Character, String> letters = new HashMap<>();

public static void main(String[] args) {
    for (int i = 0; i < alpha.length; i++) {
        letters.put(alpha[i].charAt(0), morse[i]);
    }

You could simply use one 26-character string:

final static String alpha = "abcdefghijklmnopqrstuvwxyz";

...

for (int i = 0; i < alpha.length(); i++) {
    letters.put(alpha.charAt(i), morse[i]);
}

Or you could statically initialize the structure directly:

Map<Character, String> letters = Map.of(
    'a', ".-",    'b', "-...",  'c', "-.-.",  'd', "-..",  'e', ".",
    'f', "..-.",  'g', "--.",   'h', "....",  'i', "..",   'j', ".---",
    ... etc ...);

Stream API

The following code:

private static String encodeWord(String word){
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < word.length(); i++) {
        stringBuilder.append(letters.get(word.charAt(i)));
    }
    return stringBuilder.toString();
}

is pretty straight forward.  It could be reduced to 1 line of code with the Stream API, if you are familiar with it:

private static String encodeWord(String word){
    return word.chars().mapToObj(i -> letters.get((char) i).collect(Collectors.joining());
}

Words & Encoded Words

Right now, you are allocating a parallel array for encodedWords, and matching words and encoded words by index.  You could merge these two structures into one object, a Map<String, String> where encoded words map to their decoded values.  Passing this object to decodePossibility would be more cohesive than two arrays, which could be of different length.

Efficiency

Continuing with the Map<String, String> of decodings...

In decodePossibility, you are looping over all encodedWords trying each one to see if it is the next possibility in the message.  With 7 words, this isn't too inefficient, but if the list of words was larger, it could be.

Instead, you could find the length of the shortest encoded word, and develop a Map of lists of words starting with a prefixes of that length:

Map<String, Map<String, String>> partitioned_decoding;

For instance, the encoding of "the" produces the shortest sequence -..... of 6 characters, so in partition_decoding each key would be the first 6 characters of encodedWords, and each value would contain a mapping of encodedWords to words for only the encodedWords starting with that key.

"-....."  ->  Map.of("-.....", "the")
".---.-"  ->  Map.of(".---.--.-.-.-", "jack")
".---.."  ->  Map.of(".---...-...-..", "jill")
".--.-."  ->  Map.of(".--.-.-", "went", ".--.-..", "and")
"..-.--"  ->  Map.of("..-.--.", "up")
"......"  ->  Map.of(".......-...-..", "hill")

During decodePossibility, if code was at least 6 characters long, you would fetch the first 6 characters of code, look up that entry in partitioned_decoding, and if present, loop over that (ideally only 1) entry in that submap.  In the case of ".--.-.", you'd have to try both "went" and "and", but that is only 2 words, which is still much smaller than examining all 7.

Neither "e" or "t" are valid single letter words, so the worst case would be if words contained the word "a", which would cause the key length to reduce to 2 characters, and words would be partitioned into 4 different bins: "--", ".-", "-.", and "..", which should still provide a 4 times speed-up, since only one quarter of entries would be checked at each stage.

Alternative to Recursion

Instead of recursion, you could use a loop, and allocate your own stacks for current position and word iteration for each level.  When a solution is found, the return statement would directly return the solution - no need to check for a sub-step returning non-null and returning that value, eventually unwinding the call-stack to the top.  When a dead-end is found, you pop the previous state from your own stacks, and continue looping until a solution is found or no solution is possible.


"What is the fastest solution for finding the maximum sum of a subarray?"

Can't get much better
First of all, I would have to agree with the commenters that your solution is fine.  Any further gains upon your solution would probably depend on random factors in the Leetcode judging system.  Your main loop does the minimal amount of work necessary to solve the problem.
Reduce code?
That being said, I went ahead and tried a few things to see how random the Leetcode judging system was.  I ended up with this code which is essentially the same as the original code except it doesn't special case the first array entry and is therefore a little smaller:
class Solution {
    public int maxSubArray(int[] nums)
    {
        int size = nums.length;
        int sum = 0;
        int max = Integer.MIN_VALUE;

        for (int i = 0; i < size; i++)
        {
            sum += nums[i];
            if (sum > max)
                max = sum;
            if (sum < 0)
                sum = 0;
        }
        return max;
    }
}

I submitted this code several times and randomly got a submission in the 98.67% percentile (13 ms).  I also had submissions that scored 85% (14 ms), 56% (15 ms), 32% (16 ms), and 18% (17 ms).  Given the fact that the same code varied in time between 13 ms and 17 ms, I would say that random factors have more to do with your score than the actual code.
You can see my 13 ms submission here.  I'm not sure if that link is private to my account or not, but you could always just copy and paste my code and submit many times yourself.


Use -Wall

When you are evaluating your code, you should use Haskell -Wall verbosity.

ghc -Wall mycode.hs

or

runghc -Wall mycode.hs

For example, it will tell you:

Unneeded imports (the import of ... is redundant)
Inappropriate use of variable names (this binding for ... shadows the existing binding)
Missing type signatures (top-level binding with no type signature ...)
etc.

This is generally good advice.

Specifying imported functions and types in import statements helps future reading of your code to determine why a particular import is needed.

For example:

import Data.List.Split (splitOn)

Use hlint

hlint is another useful tool to help you improve your code. For example, it would tell you that parentheses are unnecessary in the following case:

longestLengthBeforeChar - (length (partBeforechar line))

That could be replaced by:

longestLengthBeforeChar - length (partBeforechar line)

Or that, you shouldn’t use mapM if you don’t intend to use the result. You should use mapM_ (note the underscore) to make it clear.

Again, like -Wall, hlint suggestions are generally good advice you should follow.

Factorize

When you often see a pattern in your code, it may be an alert that it needs
factorization.

For example:

mapM putStrLn $ alignOn "," ["Programming, Puzzles", "And, Code Golf"]
mapM putStrLn $ alignOn ", " ["Code, Review", "And Other, improvements"]
mapM putStrLn $ alignOn "abc" ["Example abc bar", "foo abc Example"]

could be replaced by:

let tests = [ (","  , ["Programming, Puzzles", "And, Code Golf"])
            , (", " , ["Code, Review", "And Other, improvements"])
            , ("abc", ["Example abc bar", "foo abc Example"])
            ]

mapM_ (putStr . unlines . uncurry alignOn) tests

(better: use QuickCheck and put the test code outside your module)

Naming convention

You should use appropriate names for your parameters. Naming char a parameter which is in fact a String is misleading. It also does not give any indication on the role of this parameter. It could be renamed separator, for example.

You should also avoid to use a name that will shadow another one. You use lines in your function as a parameter, but lines is a well defined function (the counterpart of unlines).

Warning

Some Haskell functions like head can generate exceptions:

head [] -- Exception: Prelude.head: empty list

When you use it in your code, you should ensure an empty list will never occur (since it comes right after the splitOn function, it is safe to use it).

Coding convention

Avoid a where clause inside another where clause. It generally indicates the code should be split because your function is complex. Complex functions are harder to debug.

In fact, in your code, the alignOn can be splitted into independent, reusable and testable functions.

For example:

alignOn :: String -> [String] -> [String]
alignOn sep rows = fmap padline rows
    where padding row = longestLengthBefore sep rows - lengthBefore sep row
          padline row = replicate (padding row) ' ' ++ row

lengthBefore :: String -> String -> Int
lengthBefore sep = length . head . splitOn sep

longestLengthBefore :: String -> [String] -> Int
longestLengthBefore sep = maximum . fmap (lengthBefore sep)

Note:

This example is NOT optimized.
fmap should be preferred to map as it is more general.

Generalizing

The padding character could be defined as a parameter.

Optimization

You could rewrite your code to use Text instead of String.


Bring phone numbers in consistent format

There are two aspects to this, the general design, and the implementation.
Implementation
The entire things should be extracted as a function. The first line of code:

String number = allContactNumbers.get(i).get(j);

indicates that this code is being run inside a loop (i,j). You need to extract it to be:
String number = normalizePhoneNumber(allContactNumbers.get(i).get(j));

and the function would look something like:
public String normalizePhoneNumber(String number) {
    ......
    return normalized;
}

Right, about that function, putting your code in it ends up with:
public String normalizePhoneNumber(String number) {

    number = number.replaceAll("[^+0-9]", ""); // All weird characters such as /, -, ...

    String country_code = getResources().getString(R.string.countrycode_de);

    if (number.substring(0, 1).compareTo("0") == 0 && number.substring(1, 2).compareTo("0") != 0) {
        number = "+" + country_code + number.substring(1); // e.g. 0172 12 34 567 -> + (country_code) 172 12 34 567
    }

    number = number.replaceAll("^[0]{1,4}", "+"); // e.g. 004912345678 -> +4912345678

    return number;
}

Design
OK, about the design.... I believe this is a problem. Handling phone numbers is much more complicated than what you have.... it is really a challenging problem.
It is easy enough to strip off the junk, but it gets hard really fast. For example, this is the the Queen of England's land-line number (not kidding):

Public Information Officer
Buckingham Palace
London SW1A 1AA
Tel (during 9am - 5pm (GMT) Monday to Friday): (+44) (0)20 7930 4832. Please note, calls to this number may be recorded.

As a Canadian, the international dialing code for this would be:
01144207934832

How will your script translate that in to:
+442079305832

I think you need to revise your plan for this problem, it is more complicated than you realize.


Summing data from two arrays, grouped and sorted by date

It looks as if you have chosen the wrong data structure. The multidimensional Array looks very much like a Hash. Even your code mentions the terms key (k) and value (v).

If you were to use a Hash, you could use Hash#merge:

one = Hash[[["23 Jan",3],["29 Jan",10],["19 Jan",23],["25 Jan",23]]]
two = Hash[[["31 Jan",7],["29 Jan",12],["01 Feb",4]]]

one.merge(two) { |_value, old, new| old + new }
# => {"23 Jan"=>3, "29 Jan"=>22, "19 Jan"=>23, "25 Jan"=>23, "31 Jan"=>7, "01 Feb"=>4}

Note: If you're using a recent version of Ruby, you can replace Hash::[] with Array#to_hash

If you really do need multidimensional Arrays, you could still use Hashes internally and convert to the required format (Hash#to_a) when needed.


Firstly, code that looks like:

foo = 0
while foo < bar:
    ...  # foo doesn't change here
    foo += 1

is a bad sign. In Python, this can immediately be simplified, using range, to:

for foo in range(bar):
    ...

Secondly, hard-coding the shape of your "arrays" everywhere (having the literals 51, 150 and 207 in the code itself), referred to as "magic numbers", is poor practice in every programming language. These are the lengths of each sub-list, so should be calculated as needed using len(...). This makes the code more flexible; it can now be easily applied to add arrays of other sizes. it's also slightly awkward to hard-code the depth of the addition (i.e. that it only handles four-dimensional arrays).

Thirdly, however, code like:

for index in range(len(sequence)):
    element = sequence[index]
    ...

is generally better replaced with:

for element in sequence:
    ...

where you need the index too, you can use enumerate, and where you need to iterate over multiple iterables you can use zip; Python has lots of handy functions for dealing with iterables (see also itertools).

Fourthly, when you have a loop appending to a list, it's usually more efficient to replace it with a "list comprehension". For example:

foo = []
for bar in baz:
    foo.append(bar)

can be replaced by:

foo = [bar for bar in baz]

Finally, however, this is totally trivial if you switch to numpy, which is extremely helpful for manipulating arrays:

>>> import numpy as np
>>> arr1 = np.random.randint(1, 10, (1, 51, 150, 207))
>>> arr2 = np.random.randint(1, 10, (1, 51, 150, 207))
>>> arr1 + arr2  # wasn't that easy!
array([[[[11, 13, 14, ...,  9, 14,  8],
         [15,  4, 10, ..., 14, 10, 16],
         [11, 11,  9, ...,  4,  9,  9],
         ...,
         [17, 10, 14, ..., 10, 12, 12],
         [12, 11, 14, ...,  7,  8,  8],
         [ 9, 11,  6, ...,  3, 11,  8]],

        [[10, 15, 16, ..., 11, 16, 10],
         [17, 17,  8, ...,  8,  7,  8],
         [11,  7,  2, ..., 16, 11,  5],
         ...,
         [15, 10, 16, ..., 16, 13,  9],
         [15,  8,  7, ..., 13,  5,  6],
         [ 4,  5, 10, ...,  7, 10, 10]],

        [[11, 10,  4, ..., 10,  7, 11],
         [12,  3, 14, ..., 11, 12, 12],
         [ 6, 11, 16, ..., 14,  9, 12],
         ...,
         [18, 10, 11, ..., 13, 14,  9],
         [11, 11, 10, ...,  9, 13, 12],
         [10, 10, 10, ..., 12, 14,  8]],

        ...,
        [[16, 11, 12, ..., 13, 13,  9],
         [ 7, 15, 10, ...,  9, 11,  5],
         [ 5, 11, 14, ..., 14,  4, 11],
         ...,
         [15, 13,  6, ..., 17,  6, 10],
         [ 9, 12,  7, ...,  7, 17, 11],
         [12,  9, 10, ...,  4,  9,  5]],

        [[15, 12, 10, ...,  5, 12, 15],
         [17, 14, 15, ..., 13, 11,  8],
         [13, 10, 10, ...,  8,  4,  8],
         ...,
         [12,  9, 10, ...,  9,  7, 12],
         [13, 12, 17, ...,  7, 13, 10],
         [13,  6,  8, ..., 15, 13,  7]],

        [[ 9,  8,  8, ...,  6, 10,  2],
         [ 9, 15, 14, ..., 14,  4,  5],
         [ 3, 12, 12, ...,  5, 10, 14],
         ...,
         [ 6, 11, 15, ..., 11,  4, 15],
         [ 4, 12, 14, ..., 12, 11,  9],
         [10,  3,  5, ...,  5,  7, 10]]]])


Adding another answer since I'm actually looking at the code now...

You are swallowing every exception that gets thrown.  The calling code has no idea whether it tried to hit a page that didn't exist, its request was malformed, etc.  If you don't want to expose the exceptions (probably a good idea so your client code doesn't have to worry), then you might want to set some sort of error flag as part of your class that can be probed for the cause of not getting back the XML, or throw your own custom exceptions.

I'm assuming in your other methods you do similar sorts of set up for the connection (the accepts, etc)...I'd recommend moving that out into its own setup method.  If you need to specialize based on the type of request, then pass in the type and you can at least consolidate all of that into one place.


For long strings in CoffeeScript consider using the heredocs notation:

logs[index].extra = {
  message: """
           Camera came online

             after #{getTime2(online, offline)}

           """
}

or simply concatenate multiple lines:

logs[index].extra = {
  message: "Camera came online" +
           "" +
           "  after #{getTime2(online, offline)}" +
           ""
}

The extra newlines and multiple spaces shouldn't matter in HTML since they're all trimmed to one space by the renderer (except defined otherwise, e.g. in pre or code).

It wasn't your question, but please also consider this syntax for defining an object and adding a field to it. I think it's better readable due to less indentation, it's more explicit and doesn't make anyone thing you'd add on top of an existing object:

logs[index].extra = {}
logs[index].extra.message = "Camera came online" +
                            "" +
                            "  after #{getTime2(online, offline)}" +
                            ""

The code from this example is equivalent to the code above it.


I am getting the output but I am not sure if this is correct. I saw one or two solution using joins So I am confused that whether the query I have written is correct or not

It is a good practice to use JOIN : Explicit vs implicit SQL joins.

Second you could use aliases so you do not have to type entire table name.

SELECT  SUM(po.quantity)         AS TotalQuantity
       ,SUM(po.quantity*p.price) AS TotalSaleAmount
FROM productorder po
JOIN product p
  ON po.ProductId = p.ProductId
JOIN `order` o
  ON o.OrderId = po.OrderId
WHERE o.OrderDate = CURDATE();

SqlFiddleDemo


You defined the class Game. However, this is an extremely vague name. Even if you have already named your module something like guessthenum, you should still give the class a specific name. I would select GuessTheNum as the class name.

Next up, we have your initialization function:

def __init__(self):
    self.winningNumber = random.randint(0,100)
    self.humanGuess()

Nothing much to talk about here, other than my minor nitpick that you should add a space after your commas. I do have something to talk about the humanGuess and comGuess functions:

def humanGuess(self):
    self.humanGuessNumber = int(input("Guess from 0-100"))
    self.comGuess()
    return self.humanGuessNumber

def comGuess(self):
    self.comGuessNumber = random.randint(0,100)
    self.conclude()
    return self.comGuessNumber

The problem with the humanGuess function is that it forces input to be passed using the input function. In my opinion, classes should not engage in any form of IO unless strictly necessary (for example, a class for reading files).

Following this change, there is no need to return anything from humanGuess and comGuess. After all, the data is stored in the class.

To fix these problems, I would change both all three functions to this:

def __init__(self):
    self.winningNumber = random.randint(0, 100)

def humanGuess(self, n):
    self.humanGuessNumber = n

def comGuess(self):
    self.comGuessNumber = random.randint(0, 100)

Next, the conclude function:

def conclude(self):
    if abs(self.winningNumber - self.humanGuessNumber)< abs(self.winningNumber - self.comGuessNumber):
        print("You win")
    else:
        print("You suck")

Once again, following the no IO rule, I restructured the function such that it would return a boolean value indicating if the player wins:

def conclude(self):
    return (
        abs(self.winningNumber - self.humanGuessNumber)
        < abs(self.winningNumber - self.comGuessNumber)
    )

Now that we've cleared up the functions, let's take a look at the whole class:

class GuessTheNum:
    def __init__(self):
        self.winningNumber = random.randint(0, 100)

    def humanGuess(self, n):
        self.humanGuessNumber = n

    def comGuess(self):
        self.comGuessNumber = random.randint(0, 100)

    def conclude(self):
        return (
            abs(self.winningNumber - self.humanGuessNumber)
            < abs(self.winningNumber - self.comGuessNumber)
        )

I find that the structure of this class is unwieldy. If I were to design a GuessTheNum class, I would have just a single isWin function. It would take in my guess, generate a computer guess, then tell me if I suck or not.

Here's the way I would have written the full program:

class GuessTheNum:
    def __init__(self):
        self.winningNumber = random.randint(0, 100)

    def isWin(self, n):
        self.humanGuessNumber = n
        self.comGuessNumber = random.randint(0, 100)
        return (
            abs(self.winningNumber - self.humanGuessNumber)
            < abs(self.winningNumber - self.comGuessNumber)
        )

gtn = GuessTheNum()
n = int(input("Guess a number from 0 to 100: "))
if gtn.isWin(n):
    print("You win")
else:
    print("You suck")

EDIT 1:

@jonrsharpe reminded me that I had a DRY violation: 0 and 100 was repeated in a few places. Here's the updated version:

class GuessTheNum:
    def __init__(self, lower, upper):
        self.lower, self.upper = lower, upper
        self.winningNumber = random.randint(self.lower, self.upper)

    def isWin(self, n):
        self.humanGuessNumber = n
        self.comGuessNumber = random.randint(self.lower, self.upper)
        return (
            abs(self.winningNumber - self.humanGuessNumber)
            < abs(self.winningNumber - self.comGuessNumber)
        )

lower, upper = 0, 100
gtn = GuessTheNum(lower, upper)
n = int(input("Guess a number from %d to %d: " % (lower, upper)))
if gtn.isWin(n):
    print("You win")
else:
    print("You suck")

EDIT 2:

@TheBlackCat gave a few suggestions and reminded me of the PEP style. This fixes another DRY with random.randint.

class GuessTheNum:
    def _gen_number(self):
        return random.randint(self.lower, self.upper)

    def __init__(self, lower=0, upper=100):
        self.lower, self.upper = lower, upper
        self.win_num = self._gen_number()

    def isWin(self, human_guess):
        com_guess = self._gen_number()
        return (
            abs(self.win_num - human_guess)
            < abs(self.win_num - com_guess)
        )

lower, upper = 0, 100
gtn = GuessTheNum(lower, upper)
n = int(input("Guess a number from %d to %d: " % (lower, upper)))
if gtn.isWin(n):
    print("You win")
else:
    print("You suck")


Care about indentation. Pascal is usually written with 2 space indentation.
Repeat is a posttest loop, and your code needs to run at least once, so why not a repeat..until loop. You can save a begin..end block, as well.
you should explain to your self the inputs, unite measures and all the info you need to properly insert the requested variables.
in pascal, prefixing your own variables is good practice for complex code (not your case). Take a look at this answer. I choose to prefix our global variables with 'G'; Also, functions and procedures goes with CapitalLetters.
Avoid repetition, I choose to use GAccMoneyOut:=GMoneyControl; instead of repeating the code to raise the money.
Care about the user, you should use readkey and not readln at the end. ReadLn increases the chance of getting lost.

Putting all together:

program whilel;

uses Crt;

var
  GInterestRate,
  GMoneyEveryYear,
  GTotalAmount,
  GMoneyControl,
  GAccMoneyOut:real;

begin
  WriteLn('Enter the porcentage interest rate');
  ReadLn(GInterestRate);

  WriteLn('Enter the money you will put every year into the account');
  ReadLn(GMoneyEveryYear);

  WriteLn('Enter the money you want to get');
  ReadLn(GTotalAmount);

  GAccMoneyOut:=0;
  GMoneyControl:=0;
  repeat
    GMoneyControl:=(GAccMoneyOut+GMoneyEveryYear)*((GInterestRate/100)+1);
    if GMoneyControl>GTotalAmount then
      Break
    else
      GAccMoneyOut:=GMoneyControl;
  until GAccMoneyOut=GTotalAmount;

  WriteLn('Result is:');
  WriteLn(GAccMoneyOut:6:2);

  WriteLn('Press any key to exit...');
  Readkey;
end.


Looks pretty good to me.  There are a few shortcuts you could add if you don't mind using the Tupelo Clojure library.  One of them is vals->map
and with-map-vals.

Called with a list of symbols like (vals->map a b c) returns a map like {:a a :b b :c c}:

(let [a 1
      b 2
      c 3]
  (vals->map a b c))  ;=>  {:a 1 :b 2 :c 3} }

Given a map like {:a 1 :b 2 :c 3} (such as generated by (vals->map a b c)), performs safe let destructuring using grab like:

   (let [some-map  {:a 1 :b 2 :c 3} } ]
     (with-map-vals some-map [a b c]
        (+ a b c)))  ;=>  6

with-map-vals is safe for typos since grab will throw if the requested key is not present. See vals->map for simple creation of labeled data maps.

For default values, I think the simplest way is like this:

  (let [defaults {:a 1 :dir :up :name "joe"}
        v1       {:a 2}
        v2       {:a 9 :dir :left :name "sally"}]
    (spyx (into defaults v1))
    (spyx (glue defaults v2))

There is also the pure clojure way:

; Pure clojure way
(let [{:keys [a dir name ] } v1]
  (spyx :pure-1 (vals->map a dir name)) )

; can't use `defaults` here, need map with keyword => symbol
(let [{:keys [a dir name] :or {a 1 dir :up name "joe"}} v1]
  (spyx :pure-2 (vals->map a dir name)))

(let [{:keys [a dir name ] :or {a 1 dir :up name "joe"}} v2]
  (spyx :pure-1 (vals->map a dir name)) )

See also Clojure Destructuring.

Lastly, instead of concat I like to use glue.

; Glue together like collections:
(is (= (glue [ 1 2] '(3 4) [ 5 6] )       [ 1 2 3 4 5 6 ]  ))   ; all sequential (vectors & lists)
(is (= (glue {:a 1} {:b 2} {:c 3} )       {:a 1 :c 3 :b 2} ))   ; all maps
(is (= (glue #{1 2} #{3 4} #{6 5} )      #{ 1 2 6 5 3 4 }  ))   ; all sets
(is (= (glue "I" " like " \a " nap!" )   "I like a nap!"   ))   ; all text (strings & chars)

; If you want to convert to a sorted set or map, just put an empty one first:
(is (= (glue (sorted-map) {:a 1} {:b 2} {:c 3})   {:a 1 :b 2 :c 3} ))
(is (= (glue (sorted-set) #{1 2} #{3 4} #{6 5})  #{ 1 2 3 4 5 6  } ))

Lastly, I wouldn't bother with defn-  It just makes testing harder and provides little value.  If you have non-public functions that you don't want in the documentation (via the lein-codox plugin), you can add metadata to the function like:

(defn ^:no-doc my-helper-fn
  ...)


Changing height of an UI panel using Array content

It could be written as :

var graphBars = new[] { GraphBar1, GraphBar2, GraphBar3, GraphBar4, GraphBar5, GraphBar6, GraphBar7, GraphBar8, GraphBar9, GraphBar10 };
for (int i = 0; i < graphBars.Length; i++)
    graphBars[i].GetComponent<RectTransform>().sizeDelta =
        new Vector2(50, oScript.reactionTimeArray[i] * 1000);


This is a subjective opinion, but I find it useful to extract methods for the three types of matches you are checking in your code.

I refactored your code and extracted the methods:

bool OrderPartyMatches(Item item, Item current)
bool OrderTypeMatches(Item item, Item current)
bool SecurityMatches(Item item, Item current)

While all I did was move stuff around, I think this version gains in readability, because I find

OrderPartyMatches(item, current)

to reveal its intent more clearly than

( String.Equals(item.Security, all) ||String.Equals(current.Security, item.Security)

which is now encapsulated in this named method.

The final code I ended up with is:

const string all = "All";

private static bool CompareOrderItem(Item item, Item current)
{
    return
        SecurityMatches(item, current) &&
        OrderTypeMatches(item, current) &&
        OrderPartyMatches(item, current);
}

private static bool SecurityMatches(Item item, Item current)
{
    return
        String.Equals(item.Security, all) ||
        String.Equals(current.Security, item.Security);
}

private static bool OrderTypeMatches(Item item, Item current)
{
    return
        item.OrderType == OrderTypeEnum.None ||
        Equals(current.OrderType, item.OrderType);
}

private static bool OrderPartyMatches(Item item, Item current)
{
    return
        String.Equals(item.Counterparty, all) ||
        String.Equals(current.Counterparty, item.Counterparty);
}

Please note how the need for comments disappears, as the code now explains itself progressively.

Some people may only be interested to see that three fields are matched (and thus only look at the first method). Others may also be interested to see how the matching is done, and the explanation is just a few lines away.


The code in the post does not work, because the files are opened in text mode, and in text mode Python 3 translates newlines by default. To quote the Python documentation:

newline controls how line endings are handled. It can be None, '', '\n', '\r', and '\r\n'. It works as follows:

When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

This means that the code in the post never gets to see the original line endings and so it does not behave as intended when run on Windows. (This makes me suspect that it has not been tested in all four configurations: Unix → DOS on Unix; DOS → Unix on Unix; Unix → DOS on Windows; DOS → Unix on Windows.)
In order to operate on the original line endings, you could open the file in binary mode (both for reading and writing), or open it in text mode but set newline='' so that newlines are not translated.