texts: Getting new vs. returning customers in Postgres

texts:
I have this work task, to write a SQL query that would show how many of the active customers are new (= do not have prior transactions) as opposed to returning per a given period.
There is nothing particular in the DB to get this, so my solution takes a transaction and compares customer_id to the pool of customer_ids that belong to all transactions that took place prior. Here's how it looks:
SELECT allthem.period, allthem.c "all", newonly.c "new", (allthem.c - newonly.c) "returning"
FROM (
    SELECT date_trunc('week', t.paid::timestamptz) AS period, COUNT(DISTINCT(t.customer_id)) AS c
    FROM transactions t
    WHERE t.status = 'paid' AND (t.price->>'payment_total')::real > 35
    GROUP BY date_trunc('week', t.paid::timestamptz)
    ORDER BY date_trunc('week', t.paid::timestamptz) DESC) AS allthem
JOIN (
    SELECT date_trunc('week', b.paid::timestamptz) AS period, COUNT(DISTINCT(t.customer_id)) AS c
    FROM transactions t
    WHERE t.status = 'paid' AND (t.price->>'payment_total')::real > 35
    AND t.customer_id NOT IN (SELECT customer_id FROM transactions WHERE status='paid' AND (price->>'payment_total')::real > 35 AND paid::timestamptz < t.paid::timestamptz)
    GROUP BY date_trunc('week', t.paid::timestamptz)
    ORDER BY date_trunc('week', t.paid::timestamptz) DESC) AS newonly ON allthem.period=newonly.period
WHERE allthem.period > date_trunc('week', now()::timestamptz at time zone 'pst') - interval '12 months'

It works, but the problem is that it is quite slow.
Is there any way to compute the required data with less server load?


texts: Check if an inputted string is in the right format for a valid rgb color

texts:
It is a practice from the site edabit.com and you have to check if the input is valid. That is the original task:

Given an RGB(A) CSS color, determine whether its format is valid or not. Create a function that takes a string (e.g. "rgb(0, 0, 0)") and return True if it's format is correct, otherwise return False.

The rules are shown in the testing code:
# True Tests
Test.assert_equals(valid_color('rgb(0,0,0)'), True, 'rgb lowest valid numbers')
Test.assert_equals(valid_color('rgb(255,255,255)'), True, 'rgb highest valid numbers')
Test.assert_equals(valid_color('rgba(0,0,0,0)'), True, 'rgba lowest valid numbers')
Test.assert_equals(valid_color('rgba(255,255,255,1)'), True, 'rgba highest valid numbers')
Test.assert_equals(valid_color('rgba(0,0,0,0.123456789)'), True, 'alpha can have many decimals')
Test.assert_equals(valid_color('rgba(0,0,0,.8)'), True, 'in alpha the number before the dot is optional')
Test.assert_equals(valid_color('rgba(   0 , 127 , 255 , 0.1 )'), True, 'whitespace is allowed around numbers (even tabs)')
Test.assert_equals(valid_color('rgb(0%,50%,100%)'), True, 'numbers can be percentages')

# False Tests
Test.assert_equals(valid_color('rgb(0,,0)'), False, 'INVALID: missing number')
Test.assert_equals(valid_color('rgb (0,0,0)'), False, 'INVALID: whitespace before parenthesis')
Test.assert_equals(valid_color('rgb(0,0,0,0)'), False, 'INVALID: rgb with 4 numbers')
Test.assert_equals(valid_color('rgba(0,0,0)'), False, 'INVALID: rgba with 3 numbers')
Test.assert_equals(valid_color('rgb(-1,0,0)'), False, 'INVALID: numbers below 0')
Test.assert_equals(valid_color('rgb(255,256,255)'), False, 'INVALID: numbers above 255')
Test.assert_equals(valid_color('rgb(100%,100%,101%)'), False, 'INVALID: numbers above 100%')
Test.assert_equals(valid_color('rgba(0,0,0,-1)'), False, 'INVALID: alpha below 0')
Test.assert_equals(valid_color('rgba(0,0,0,1.1)'), False, 'INVALID: alpha above 1')

I fear that my solution is not very elegant, efficent or short:
def create_rgbA_tuple(color):
    indexBracket1 = color.rfind("(")
    save = color[0] + color[1] + color[2] + color[3]
    if save != 'rgb(' and save != 'rgba':
        return False
    indexBracket2 = color.rfind(")")
    bracketArea = ""
    for i in range(indexBracket1 + 1, indexBracket2):
        bracketArea = bracketArea + color[i]

    bracketArea = bracketArea.split(',')  # bracketArea is now an Array (r,g,b/A)
    if len(bracketArea) == 4 and save != 'rgba' or len(bracketArea) == 3 and save != 'rgb(':
        return False
    try:
        for i in range(len(bracketArea)):
            bracketArea[i] = float(bracketArea[i])
    except:
        try:
            for i in range(len(bracketArea)):
                if bracketArea[i][-1] == "%":
                    bracketArea[i] = int(bracketArea[i].strip('%')) / 100 * 255
                else:
                    return False
        except:
            return False
    return bracketArea

def valid_color(color):
    rgb = create_rgbA_tuple(color)
    if not rgb:
        return False
    for i in range(3):
        if int(rgb[i]) < 0 or int(rgb[i]) > 255:
            return False

    if color[3] == "a":  # if rgbA
        if rgb[3] < 0 or rgb[3] > 1:
            return False
    return True
```


texts:
I have created a simple jQuery based finance calculator.

Just to make it easy to read I will list below all the functionality and validation it has.

Functionalities:

Ability to enter the amount you would like to borrow
Ability to select the repayment period (in months) by dragging the slider

Validation:

Check if the amount is between £400 and £2000 (including empty input field)
Re-calculate repayments through the slider or "UPDATE" button.

Since I am new to jQuery I would like to hear from you if I have followed best practices in building it and if there is something I can improve/optimize on it from your point of view.

Please see link to MY JSFIDDLE

I have also listed my jQuery code below:

(function($) {
  "use strict";
  $(document).ready(function() {
    //Finance Amount Field
    $("#update_financeAmount").click(function() {
      //Validation | Empty input field or amount bigger than 399 or smaller than 2000
      if (parseInt($("#input-financeAmount").val()) > 399 && parseInt($("#input-financeAmount").val()) < 2001) {

        $("input#update_financeAmount").css("background", "#323470");
        var amountBorrowed = $('#input-financeAmount').val();
        var monthlyBreakdown = $('#monthPick_slider').slider('value');
        var totalRepayment = (amountBorrowed / monthlyBreakdown).toFixed(2);
        $('#financeAmount').text('£' + amountBorrowed);
        $('#monthly_repayments').text('£' + totalRepayment);
      }
    });
    //if "ENTER" hit force above click event
    $("#input-financeAmount").keydown(function(event) {
      if (event.keyCode == 13) {
        $("#update_financeAmount").click();
        //Prevent from submitting the form by pressing "ENTER"
        $("form").submit(function() {
          return false;
        });
      }
    });
    //Monthly Range Slider
    $("#monthPick_slider").slider({
      range: 'min',
      min: 3,
      max: 18,
      value: 12,
      slide: function(event, ui) {
        var curValue = ui.value;
        var tooltip_Date = '

' + curValue + ' months

';
        $(this).find('.ui-slider-handle').html(tooltip_Date);
        $("#number_repayments").html(curValue);
      },
      create: function(event, ui) {
        var curValue = ui.value || 12;
        var tooltip_Date = '

' + curValue + ' months

';
        $(this).find('.ui-slider-handle').html(tooltip_Date);
        $("#number_repayments").html(curValue);
      }
    });
    //Re-calculate on change
    $('#monthPick_slider').slider().on({
      slidechange: function() {
        //Validation | Empty input field or amount bigger than 399 or smaller than 2000
        if (parseInt($("#input-financeAmount").val()) > 399 && parseInt($("#input-financeAmount").val()) < 2001) {
          var amountBorrowed = $('#input-financeAmount').val();
          var monthlyBreakdown = $('#monthPick_slider').slider('value');
          var totalRepayment = (amountBorrowed / monthlyBreakdown).toFixed(2);
          $('#monthly_repayments').text('£' + totalRepayment);
          $("#update_financeAmount").click();
        }
      }
    });

  });
})(jQuery);


texts:
In our console app, the parsing of application arguments is done like so:

using System.Linq;

namespace Generator
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var param1 = args.SingleOrDefault(arg => arg.StartsWith("p1:"));
            if (!string.IsNullOrEmpty(param1))
            {
                param1  = param1.Replace("p1:", "");
            }
            //...
        }
    }
}

It's supposed to be called like this:

`Generator.exe p1:somevalue`

Is there a better/simpler way to parse arguments?


texts:
This code returns an array that consists of two copies of the input array.

Can you review this algorithm?

//input: [1, 2, 3]  output: [1, 2, 3, 1, 2, 3]
function duplicate(arr) {
 let copy = [];
 for(let i = 0; i < arr.length; i++) {
  copy.push(arr[i])
  }
 return copy.concat(arr)
 }


texts:
I wrote the following code. A user from Stack Overflow recommended me to write my code here in order to be reviewed for optimization.

How could I optimize it?
Could I have this code in a professional, better form with increased performance?

Explanation: This code calculate multiplication of three matrix in c() function. Then, using Omo as old and Omn as new into c() calculate L(). Finally, defining two if statement accept Ls which are bigger than 1 and R3 which is random number. This code is MCMC fitting data. Here I fit omn and Mn.

import numpy as np
import emcee
import matplotlib.pyplot as plt
from math import *
from scipy.integrate import quad
from scipy.integrate import odeint
O_m=chi=None
xx=np.array([0.01,0.012])
yy=np.array([32.95388698,33.87900347])
Cov=[[137,168],[28155,-2217]]     # this is a covariance matrix for simplification I chose the 2*2 one

temp=1e5
z0=0
M=2
Omo = 0.3
Odo=0.7
H0=70

def ant(z, Om, Od):
    return 1/sqrt(((1+z)**2)*(1+Om*z)-z*(2+z)*Od)
def dl(n, Om, Od, M):
    Od=1-Om
    q=quad(ant,0,xx[n],args=(Om,Od))[0]
    h=5*log10((1+xx[n])*q)
    fn=(yy[n]-M-h)
    return fn

def c(Om, Od, M):
    f_list = []
    for i in range(2):  # the value '2' reflects matrix size
        f_list.append(dl(i,Om,Od,M))
    rdag=[f_list]
    rmat=[[f] for f in f_list]
    a=np.dot(rdag,Cov)
    b=np.dot(a,rmat)
    Matrix=np.linalg.det(b)*0.000001
    return Matrix
N=2000
with open('txtfile.txt', 'w') as f:
    for i in range (1,N):
        R1=np.random.uniform(0,1)
        R2=np.random.uniform(0,1)
        R3=np.random.uniform(0,1)
        R4=np.random.uniform(0,1)
        def delta(r1, r2):
            sig=0.04
            d=sig*(np.sqrt(-2*np.log(r1))*np.cos(np.radians(r2)))
            return d
        Omn=Omo+delta(R1, R2)
        Odn=1-Omn
        Mn=M+delta(R3,R4)
        R3=np.random.uniform(0,1)

        def L():
            l=np.exp(-0.5*(c(Omn,Odn,Mn)-c(Omo,Odo,M)))
            return l
        if L()>1:
            O_m=Omn
            chi=c(Omn,Odn,Mn)

        elif L()>R3:
            O_m=Omn
            chi=c(Omn, Odn, Mn)

        f.write("{0}\t{1}\n".format(chi, O_m))
    print("Minimum of chi squre is")
    if chi<temp:
        temp=chi
        chimin=temp
    print(chimin)
    print(input("Press any key to exit... "))

I appreciate your help and your attention


texts:
I am doing C coding practice by reinventing a getline function that works with any streams. Any comments on the overall code quality, including correctness, style and performance, are greatly appreciated.
fgetln.h:
#ifndef FGETLN_H
#define FGETLN_H
#include <stdio.h>

char *fgetln(FILE *fp);
/*
 * Read a line from fp, dynamically allocate memory and store it as a string.
 * The '\n' at the end of the line and any '\0' before are discarded. Return
 * a pointer to the string when successful; return NULL if the line is empty
 * or an error occurs.
 *
 * This function implements the "callee allocates, caller frees" model. You
 * should pass the returned pointer to free() when it is no longer needed.
 */

#endif  // FGETLN_H

fgetln.c:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>

#define EXPAND_RATIO 2
// Aggressiveness of memory expansion, must be an integer ≥ 2.

char *fgetln(FILE *fp)
{
    signed char c;

    // Look for hints of an empty line and discard any '\0' at the start.
    while (true) {
        c = getc(fp);
        if (c == '\0') continue;
        if (c == '\n' || c == EOF) return NULL;
        break;
    }

    // If we have reached here, the line has content in it.
    ungetc(c, fp);

    char *p_tmp, *line = NULL;
    size_t arr_size = sizeof(char);
    bool end_of_line = false;
    unsigned long long i = 0;

    p_tmp = realloc(line, arr_size);
    if (p_tmp == NULL) goto err_cleanup;
    line = p_tmp;

    do {
        c = getc(fp);
        if (c == '\0') continue;
        if (c == '\n' || c == EOF) {
            end_of_line = true;
            c = '\0';
        }

        if ((i * sizeof(char)) == arr_size) {
            if (arr_size == SIZE_MAX) {
                if (c != '\0') goto err_cleanup;  // Too long
            } else {
                // Assuming SIZE_MAX % sizeof(char) == 0.
                arr_size = arr_size > (SIZE_MAX / EXPAND_RATIO)
                         ? SIZE_MAX
                         : arr_size * EXPAND_RATIO;
                p_tmp = realloc(line, arr_size);
                if (p_tmp == NULL) goto err_cleanup;
                line = p_tmp;
            }
        }

        line[i++] = c;
    } while (!end_of_line);

    // Shorten the string to its logical size if available.
    size_t logic_size = i * sizeof(char);

    if (logic_size < arr_size) {
        p_tmp = realloc(line, logic_size);
        if (p_tmp == NULL) goto err_cleanup;
        line = p_tmp;
    }
    return line;

err_cleanup:
    free(line);
    return NULL;
}

#undef EXPAND_RATIO


texts: Remove Empty String array from ArrayList

texts:
I am trying to remove empty a String array from ArrayList:

  List<String[]> someList = new ArrayList<String[]>();

which contains data like:

[
  { "Loic" , "Remy" , "Fail","Medical"} ,
  { "Faser" , "Foster" , "Pass","Southampton","GK","Young"} ,
  { "" , "" ,} ,
  { "" , "" , "","","",""} ,
  { "Emre" , "Can" , "Pass","Liverpool","CDM"}
]

I want to remove the empty String[] from the ArrayList:

List<String[]> someList = (List<String[]>) csvMappedData.get(Constants.CSV_DATA);
    //Clone to new Array List
    List<String[]> cloneCSV = new ArrayList<String[]>(someList);
    for (String[] csvSingleLine : cloneCSV) {
        //Create New List of String only
        List<String> strings = new ArrayList<String>(Arrays.asList(csvSingleLine));
        //Remove all string that are null or blank
        strings.removeAll(Arrays.asList(null, ""));
        //Check if the List is empty or null after removing in above step
        if (strings.isEmpty() || Validator.isNull(strings)) {
            //If yes it is all blank String array
            //Remove from orginal LIST
            someList.remove(csvSingleLine);
        }
    }

The above code is working fine for me.

Questions:

Is there any other alternative solution that is quick and elegant?
What are the change that I could make to it, to make it more
efficient?

The size of each row varies, so I couldn't use remove all by creating a dummy array.


texts: VBA userform loop to gather numbers from cells on an Excel sheet

texts:
I am working on a userform and have everything working the way it should but it's taking a little longer than I would like. It's looping through 42 labels for each For statement and there are six of them.

In an effort to learn how to code a little more efficient, could someone please review my code and show me some faster ways to get the job done?

Private Sub Find()
Application.ScreenUpdating = False
Dim i As Integer, k As Integer
k = 1

i = 1

For i = i To 42
C1 = "C1_" & i

Me.Controls(C1) = Sheet14.Range("I" & k).Value

If Me.Controls(C1) = "0" Then
Me.Controls(C1).ForeColor = &H8000000F

End If
k = k + 1

Next

k = 1

i = 1

For i = i To 42
C2 = "C2_" & i
Me.Controls(C2) = Sheet14.Range("J" & k).Value
If Me.Controls(C2) = "0" Then
Me.Controls(C2).ForeColor = &H8000000F

End If
k = k + 1

Next
'
k = 1

i = 1

For i = i To 42
C3 = "C3_" & i
Me.Controls(C3) = Sheet14.Range("K" & k).Value
If Me.Controls(C3) = "0" Then
Me.Controls(C3).ForeColor = &H8000000F

End If
k = k + 1

Next
k = 1

i = 1

For i = i To 42
C4 = "CL4_" & i
Me.Controls(C4) = Sheet14.Range("L" & k).Value
If Me.Controls(C4) = "0" Then
Me.Controls(C4).ForeColor = &H8000000F

End If
k = k + 1

Next

k = 1

i = 1

For i = i To 42
C5 = "C5_" & i
Me.Controls(C5) = Sheet14.Range("M" & k).Value
If Me.Controls(C5) = "0" Then
Me.Controls(C5).ForeColor = &H8000000F

End If
k = k + 1

Next

k = 1

i = 1

For i = i To 42
C6 = "C6_" & i
Me.Controls(C6) = Sheet14.Range("N" & k).Value
If Me.Controls(C6) = "0" Then
Me.Controls(C6).ForeColor = &H8000000F

End If
k = k + 1

Next
Application.ScreenUpdating = True
End Sub


texts:
I already have some experience with Java and I'm used to modeling with a DDD approach. Therefore, now I'm beginning with JS and NodeJS and I'd like to know which is the best practice to design with a similar approach in Javascript (without ES6).

Here I have an example of what I'm trying to do, but I don't know if these is the better way. I appreciate some opinions.

function Purchase(pCustomer, pOrders, pPayment, pTotal) {
    let idPurchase = PurchaseId.purchaseId();
    let customer = pCustomer;
    let payment = pPayment;
    let orders = pOrders;
    let deliveryAddress;
    let billingAddress;
    let total = pTotal;

    // Return an object exposed to the public
    return {
        setOrders: function (o) {
            orders = o
        },
        setDeliveryAddress: function (address1, address2, zipCode, city) {
            deliveryAddress = new Address(address1, address2, zipCode, city);
        },
        setBillingAddress: function (address1, address2, zipCode, city) {
            billingAddress = new Address(address1, address2, zipCode, city);
        },
        getId: function () {
            return idPurchase;
        },
        getOrders: function () {
            return orders;
        },
        getCustomer: function () {
            return customer;
        },
        toJSON: function () {
            return {
                idPurchase: idPurchase,
                customerOnPurchase: customer.toJSON(),
                deliveryAddress: deliveryAddress.toJSON(),
                billingAddress: billingAddress.toJSON(),
                total: total,
            }
        }
    }
}

module.exports = Purchase;


texts: Dynamically combine ActiveRecord Relation results

texts:
This isn't the actual use-case I have but for simplicity, let's say I have an ActiveRecord Animal model in a Rails app. The Animals have a name (string) and rating (int). There could be animals with the same name but different ratings. In my Animal model I have a hardcoded constant like this:

SPECIAL_ANIMAL_TYPES = [
  { name: 'Cat', rating: 3 },
  { name: 'Dog', rating: 5 },
  { name: 'Fish', rating: 1 }
]

I want to grab a single ActiveRecord:Relation with all Animals that match these attributes exactly. I can't pass an array to the where clause like where(name: ['Cat', 'Dog', etc], rating: [3, 5, 1]) because I only want cats that are rating 3, and dogs that are rating 5 and so on. Like I said, there could be multiple records with the same name but different ratings.

In the Animal model I have a class method (basically a scope) to grab the records that match the types specified in SPECIAL_ANIMAL_TYPES. The following code does what I want, but clearly is pretty ugly and if I added more types to SPECIAL_ANIMAL_TYPES I'd have to modify this method:

def self.special_animals_only
 where(SPECIAL_ANIMAL_TYPES[0]).or(where(SPECIAL_ANIMAL_TYPES[1])).or(where(SPECIAL_ANIMAL_TYPES[2]))
end

I also tried something like:

def self.special_animals_only
  SPECIAL_ANIMAL_TYPES.collect { |types| where(types) }.sum
end

But that returns an array and I want an ActiveRecord Relation. So is there a more elegant way to write this class method so I can continue modifying/adding types to the constant and have this method work dynamically?


texts:
Just another step in my educational Trek. I didn't know when/why I would use the transparent feature and in experimenting with it, I made this.

This one was fun to behold once done. It's an animated flying gif and you can move it around the screen and press keys to alter its speed or add effects to the image itself.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.effect.Bloom;
import javafx.scene.effect.Glow;
import javafx.scene.effect.MotionBlur;
import javafx.scene.effect.Reflection;
import javafx.scene.effect.Shadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class AnimatedFun extends Application {
    int animationSpeed = 5;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        ImageView view = new ImageView(new Image("https://d2aao99y1mip6n.cloudfront.net/images/runes/idols/eJ8HJ8Ge9Ae9Be9Ae9Bj9Fj9Geigtzillnuhymn.gif"));
        view.setEffect(new Glow());
        view.setOnMouseClicked(e -> stage.close());
        view.setFocusTraversable(true);
        view.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.ESCAPE) {
                stage.close();
            } else if (e.getCode() == KeyCode.SPACE) {
                view.setEffect(new Reflection());
            } else if (e.getCode() == KeyCode.DELETE) {
                view.setEffect(null);
            } else if (e.getCode() == KeyCode.ENTER) {
                view.setEffect(new Bloom());
            } else if (e.getCode() == KeyCode.SHIFT) {
                view.setEffect(new Shadow());
            } else if (e.getCode() == KeyCode.RIGHT) {
                stage.setX(stage.getX() + animationSpeed);
            } else if (e.getCode() == KeyCode.LEFT) {
                stage.setX(stage.getX() - animationSpeed);
            } else if (e.getCode() == KeyCode.UP) {
                stage.setY(stage.getY() - animationSpeed);
            } else if (e.getCode() == KeyCode.DOWN) {
                stage.setY(stage.getY() + animationSpeed);
            } else if (e.getCode() == KeyCode.CONTROL) {
                animationSpeed += 3;
                if (animationSpeed >= 15) {
                    view.setEffect(new MotionBlur());
                }
            } else if (e.getCode() == KeyCode.ALT) {
                if (animationSpeed >= 15 && animationSpeed < 18){
                    view.setEffect(new Glow());
                }
                animationSpeed = (Math.max(5, animationSpeed - 2));
            }
        });

        Group root = new Group();
        root.getChildren().add(view);

        stage.setScene(new Scene(root, Color.TRANSPARENT));
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.getIcons().add(new Image("https://d2aao99y1mip6n.cloudfront.net/images/runes/lg/eJ8HJ8Ge9Ae9Be9Ae9Bj9Fj9Geigtzillnuhymn.jpg"));
        stage.show();
    }
}

Feel free download the runnable jar.

I'd like a general review of its current state as well as any befitting improvements, be it concerning JavaFX convention, or just the general structure.

Pardon the horizontally unkempt-ness, side-effect of porting over a usable version using online links for the images.


texts: Re-inventing the wheel: Binary search in Scala

texts:
I am trying to implement the code described in this post about how Reddit uses binary search to identify duplicate user names, in Scala. Just to clarify, this is mainly for me figuring out how these things work, i.e. reinventing the wheel. The way I understand it, the steps for binary search are:

You have a new user name you are comparing to an alphabetically sorted list of existing names
You compare that new name to the middle item in the list.
If there is no match, you check if the new name is higher or lower in the alphabet than the middle item, and focus on this side of the list, again checking the middle item.
You repeat until there is a match ("User name taken") or none ("User name available").

Below is what I have. It seems to work when I test it on the names in the list and random other names. It'd cool if someone had some feedback re:

Will this work correctly or is there some kind of bug?
Is there anything really un-Scalaesque in here? I'm just starting out...
Is the while loop the right way to go? Are there better options?
I think the comparer pattern matching function should be expanded to do more, i.e. cover different cases of non-matching, but I couldn't quite get there..
Any other comments are much appreciated!

-

//this is my test data
val lines = List("ally", "ben", "albert", "zong", "hermit", "??ennis", "123pat", "alfred", "zil", "zol", "zal")

def comparer(name_one:String, name_two:String): Boolean = name_one match
    //comparer takes two strings, returns true if identical
    {
    case `name_two` => true
    case _ => false
    }

def checker(new_name: String, old_names: List[String]) : Boolean =
    //checker takes a new user name, compares to List of names already in use
    //returns true if already present or false if not
    {
    val sortednames:List[String] = old_names.sorted
    println(new_name + "input list is length " + sortednames.length)

    //set vars, initialize
    var found: Boolean = false
    var startpoint: Int = 0
    var endpoint: Int = sortednames.length-1
    //while loop, breaks when something is found
    while (found == false && startpoint < endpoint)
    {
        println("start: "+startpoint+" end: "+endpoint)
        //set the midpoint were checking against
        var mid: Int = (startpoint + endpoint) / 2
        println("mid is " + mid)
        //run comparer to set found var initialized abpve
        found = comparer (new_name, sortednames(mid))
        println("new name " + new_name)
        println("ist window " + sortednames(mid))
        //check if new_name is higher or lower in alphabet than mid
        if (new_name > sortednames(mid)) {
            startpoint = mid +1
            println("set start to " + startpoint)
            }
        if (new_name < sortednames(mid)) {
            endpoint = mid
            println("set end to " + endpoint)
    }
    }
    found
    }

val x:Boolean =checker("ally", lines)

println("checker returns ")
println(x)


texts: Three implementations of observable objects

texts:
I wonder, what might be the best (most pythonic) way of implementing multiple observerables in Python.  There are multiple objects, which should be able to register themselves at one or multiple observables.

Shall I simply submit the update methods, like this:

class MethodObservable():
    def __init__(self, data_source):
        self.observers = []
        self.data_source = data_source

    def add(self, callback):
        self.observers.add(callback)

    def remove(self, callback):
        try:
            self.observers.remove(callback)
        except ValueError:
            pass

    def notify():
        for callback in self.observers:
            callback(self.data_source)

class MethodObserver():
    def __init__(self, observer1, observer2):
        observer1.add(self.update_for_1)
        observer2.add(self.update_for_2)

    def update_for_1(self, data_source):
        # code

    def update_for_2(self, data_source):
        # code

data_source1 = SomeObject()
data_source2 = SomeOtherObject()
observable1 = MethodObservable(data_source1)
observable2 = MethodObservable(data_source2)
observer = MethodObserver(observable1, observable2)

Or is it better, to come up with a dictionary of classes and methods:

class DictObservable():
    def __init__(self, data_source):
        self.observers = {}
        self.data_source = data_source

    def add(self, observer, callback):
        self.observers[observer] = callback

    def remove(self, observer):
        try:
            del self.observers[observer]
        except ValueError:
            pass

    def notify():
        for callback in self.observers.values():
            callback(self.data_source)

class DictObserver():
    def __init__(self, observer1, observer2):
        observer1.add(self, self.update_for_1)
        observer2.add(self, self.update_for_2)

    def update_for_1(self, data_source):
        # code

    def update_for_2(self, data_source):
        # code

data_source1 = SomeObject()
data_source2 = SomeOtherObject()
observable1 = DictObservable(data_source1)
observable2 = DictObservable(data_source2)
observer = DictObserver(observable1, observable2)

Or is probably the best way, that a class, which registers to a specific observer should have a specified update/callback-method, which kind of enforces the implementation of an interface:

class SpecifiedObservable():
    def __init__(self, data_source, callback_method='update'):
        self.observers = []
        self.data_source = data_source
        self.callback_method = callback_method

    def add(self, observer):
        self.observers.add(observer)

    def remove(self, observer):
        try:
            self.observers.remove(observer)
        except ValueError:
            pass

    def notify():
        for observer in self.observers:
            getattr(observer, self.callback_method)(self.data_source)

class SpecifiedObserver():
    def __init__(self, observer1, observer2):
        observer1.add(self)
        observer2.add(self)

    def update_for_1_specified(self, data_source):
        # code

    def update(self, data_source):
        # code

data_source1 = SomeObject()
data_source2 = SomeOtherObject()
observable1 = SpecifiedObservable(data_source1, 'update_for_1_specified')
observable2 = SpecifiedObservable(data_source2)
observer = SpecifiedObserver(observable1, observable2)

Maybe there are even better options, I wonder, which one should be preferred?


texts:
Worth noting:
I cannot use any languages other than HTML5 and JavaScript (no JQuery!).
Grade calculator needs to update live.
Grade calculator needs to show a percentage grade based off score obtained/score possible AND a letter grade.
I've been at this for three days.  Please help me, and if possible, please explain why my code doesn't work, or why the solution does vs. mine, so that I can learn.

<!DOCTYPE html>

    <meta charset="utf-8">
    <title>Grade Calculator</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet"  href="styles.css">
    <link rel="javascript" href="script.js">

SAIT Grade Calculator

 Grade Calculator

        <input type="text" id= "enumerator" onkeyup="setGrades();" />
        <input type= "text" id = "denominator" onkeyup="setGrades();"/>

Your Percentage Grade

Your percentage grade is:

        <input type="text" id="percentgrade">

Your Letter Grade

Your letter grade is:

        <input type="text" id="letter">

© Nicole  2017


texts:
I'm new to Java and have just started making JFrames. I wrote a program for a game project I'm working on and would like help on where I can improve. I'm not new to programming, I also know HTML and Python well.

I believe one of the goals for all programmers is to make a useful/fun program as simple and short as possible.

I would like pointers/ideas/improvements for my Java program on how I can make it shorter, where I should aim for and how I can make it simpler.

package main;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Game {

    //Set Variables to make it easier for myself
    public static double VER = 1.0;
    public static String STATE = "ALPHA";
    public static int WIDTH = 400;
    public static int HEIGHT = 400;
    public static int SCALE = 1;

    public static void main(String [ ] args) {
        //Create JFrame
        JFrame launcher = new JFrame("Infinite Doom " + STATE + " " + VER);
        launcher.pack();

        //Settings for JFrame
        launcher.setVisible(true);
        launcher.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        launcher.setResizable(false);
        launcher.setIconImage(new ImageIcon("Logo.png").getImage());
        launcher.setSize(WIDTH, HEIGHT);

        //Check Screen Res and centre window
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        launcher.setLocation(dim.width/2-launcher.getSize().width/2, dim.height/2-launcher.getSize().height/2);
         }
    }


texts: 01 Matrix is too slow when solved using DFS

texts:
I tried solving Leetcode 01 Matrix problem.
It is running too slow when solved using DFS approach.

Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.

Example 1
Input:
[[0,0,0],
 [0,1,0],
 [0,0,0]]

Output:
[[0,0,0],
 [0,1,0],
 [0,0,0]]

Note:

The number of elements of the given matrix will not exceed 10,000.
There are at least one 0 in the given matrix.
The cells are adjacent in only four directions: up, down, left and right.

class Solution(object):
    def updateMatrix(self, matrix):
        if not matrix or not matrix[0]:
            return []
        m, n = len(matrix), len(matrix[0])
        op = [[-1 for _ in range(n)] for _ in range(m)]
        directions = [(1,0), (-1,0), (0, 1), (0, -1)]
        def dfs(i,j):
            if matrix[i][j] == 0:
                return 0

            if op[i][j] != -1:
                return op[i][j]

            matrix[i][j] = -1
            closest_zero = float('inf')
            for direction in directions:
                x,y = direction[0] + i , direction[1] + j
                if 0 <= x < m and 0 <= y < n and matrix[x][y] != -1:
                    closest_zero = min(dfs(x,y), closest_zero)
            closest_zero += 1
            matrix[i][j] = 1
            return closest_zero

        for i in range(m):
            for j in range(n):
                if matrix[i][j] == 1 and op[i][j] == -1:
                    op[i][j] = dfs(i,j)
                elif matrix[i][j] == 0:
                    op[i][j] = 0
        return op

It is running too slowly and I don't understand what is the reason for that. Most optimised solution have solved this using BFS.


texts:
don't blame me I am trying to learn, the code works but I am not
sure it is well optimized. Please tell me if you see something wrong.
My goal is to achieve most optimized code (and learn something in
between).

User register -> check if username or email exists , if not = insert into database
User login -> check database if username exists, then verify password, let them in :)

Login.php
<?php
session_start();
....
<form....>// https://stackoverflow.com/questions/11974613/pdo-php-check-if-row-exist
    $stmt = $link->prepare('SELECT * FROM users WHERE username=? LIMIT 1');
    $stmt->bindParam(1, $username, PDO::PARAM_STR);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC); //$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if( $row ) {
            if (password_verify($_POST['password'], $row['password'])) {
                // ==============================
                session_regenerate_id(); // https://www.php.net/manual/en/function.session-regenerate-id.php
                $_SESSION["loggedin"] = true; // Store data in session variables
                $_SESSION["id"] = $row['id'];
                $_SESSION["username"] = $_POST["username"];
                    //$form->printr($_SESSION); // for debug
                header("location: profile.php");
                // ==============================
            } else { $form->error_message("Паролата не е вярна."); } // User ok , pass not valid
        }  else { $form->error_message("Няма такъв акаунт в системата."); } // No such user
    $link = null; // close pdo connection

Register.php
<?php
session_start();
...
<form...>//
/* https://stackoverflow.com/questions/11974613/pdo-php-check-if-row-exist
        $sql = 'SELECT 1 from table WHERE id = ? LIMIT 1';
        //$sql = 'SELECT COUNT(*) from table WHERE param = ?'; // for checking >1 records
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(1, $_GET['id'], PDO::PARAM_INT);
        $stmt->execute();
        if($stmt->fetchColumn()) echo 'found';
*/
        $stmt = $link->prepare("SELECT 1 from users WHERE username=? or email=? LIMIT 1");
        $stmt->execute(array($username,$email));
        if ($stmt->fetchColumn() > 0) {
           $form->add_to_errors('Името или Имейла вече е зает, моля опитайте с друг.'); // Username taken
        } else {
           $data = ['name' => $username,'email'  => $email, 'pass' => $password]; // INSERT INTO DATABASE
        $sql = "INSERT INTO users (username, email, password) VALUES (:name, :email, :pass)";
        // ===========================================================
        $stmt= $link->prepare($sql); //$stmt->execute($data);
        if ($stmt->execute($data)) {  //success
            $form->success_message("Добре дошъл в системата: <b>".$username."</b>");
            //session_regenerate_id();
        } else { $form->add_to_errors('Проблем със заявката за регистрация, моля опитайте по-късно или се свържете с администратор.'); } // failed
        }
    $link = null; // close pdo connection


texts:
My task is:

  Write a program that requests the hours worked in a week and then
  prints the gross pay, the taxes, and the net pay. Assume the
  following:

  Basic pay rate = $10.00/hr
  Overtime (in excess of 40 hours) = time and a half
  Tax rate: 15% of the first $300

  20% of the next $150
  25% of the rest

Is my code way too long and complicated for that?

#include <stdio.h>
#define PAYRATE 10 //basic pay rate per hour
#define OVERTIME 15 //in excess of 40 hours a week
int NetPay(int hours);

int main()

{
    int userWeeklyHours;

    printf("Please enter your total weekly working hours: \n");
    scanf("%d", &userWeeklyHours);// get the user weekly hours
    NetPay(userWeeklyHours);

    return 0;
}

int NetPay(int hours)// implementing the function to calculate total pay, total taxes, and net pay

{
    int firstRate, secondRate, restOfRate, secondAmount, rest, payAfterTax, payedBeforeTax, overHours;

    if (hours > 40)
    {
        overHours = hours - 40;
        payedBeforeTax = (40 * PAYRATE) + (overHours * OVERTIME);
    }

    else
        payedBeforeTax = hours * PAYRATE;//defining the user first paycheck before taxes

    if (payedBeforeTax <= 300)//paying only first rate case
    {
        firstRate = payedBeforeTax*0.15;
        payAfterTax =payedBeforeTax - firstRate;
        printf("your total gross is %d, your taxes to pay are %d, your net pay is %d", payedBeforeTax, firstRate, payAfterTax);
    }

    else if (payedBeforeTax > 300 && payedBeforeTax <= 450)//paying first and second rate
    {
        secondAmount = payedBeforeTax - 300;
        firstRate = (payedBeforeTax - secondAmount) * 0.15;
        secondRate = secondAmount * 0.20;
        payAfterTax = payedBeforeTax - (firstRate + secondRate);
        printf("your total gross is %d, your taxes to pay are %d, your net pay is %d", payedBeforeTax, firstRate + secondRate, payAfterTax);

    }

    else if (payedBeforeTax > 450)// paying all rates
    {
        rest = payedBeforeTax - 450;
        secondAmount = (payedBeforeTax - 300) - rest;
        firstRate = (payedBeforeTax - (rest + secondAmount)) * 0.15;
        secondRate = secondAmount * 0.20;
        restOfRate = rest * 0.25;
        payAfterTax = payedBeforeTax - (firstRate + secondRate + restOfRate);
        printf("your total gross is %d, your taxes to pay are %d, your net pay is %d", payedBeforeTax, firstRate + secondRate + restOfRate, payAfterTax);

    }

    return payAfterTax;
}


texts: Handling all error conditions on success or failure response

texts:
I'm working on a screen where I sent a request and I get response in two types.
One is success and contains the below format:

{
    "searchNames": [
        {
            "CustomerName": "John",
            "CustomerType": "Business",
            "Profession": "Businessman",
            "address": {
                "address1": null,
                "address2": null,
                "city": null,
                "state": null,
                "country": null
            },
            "ContactNumber": null
        }
    ],
    "Count": 0
}

The other is failure response and the format differently every time, there can be no error code or message in the format.

{
status: "FAILURE"
error: {
errorMsg: [1]
0:  {
fieldName: "CustomerName"
errorCode: null
message: ""
}
errorNo: 12
}
}

I have tried validating the response and displaying the response, but I have no idea whether it is the right way to do the same.

This function is called in a promise:

function(response)
{
if(response.searchNames)
{
console.log("result", response.searchNames );
}
else
                          {
                            var errorString = ["Error : Invalid Data"];
                              if (response.status && response.status.toLowerCase() ===
                                  'failure' && response.error && response.error.errorMsg &&
                                  response.error.errorMsg.length > 0) {
                                  var errorString = [];
                                  for (var errMsg in response.error.errorMsg) {
                                      errorString.push(response.error.errorMsg[
                                          errMsg].message);
                                  }
}

My questions are:

Is this the right way to do the error validations, or are there any best practices for this? The error response may or may not contain error message. If there is error message, I should show the error message, otherwise I should show the default error message stored in another variable.
In the success response as well is it the right way to check the success response.
What is the right way in handling all the conditions and how can I do it?


texts: Searching for repeated characters in a sequence of characters

texts:
The following code solves this problem:

  The 3072 characters below contain a sequence of 5 characters which is
  repeated. However, there is a twist: one of the sequences has a typo.
  To be specific, you're looking for two groups of 5 characters which
  share 4 of those characters. Enter the 4 shared characters below to
  proceed.

Any suggestions on how it can be improved using idiomatic Lisp/Clojure?

(ns slurp.slurp)

(defn get-five [in]
  (if (> (.length in) 4)
    (subs in 0 5)
    nil))

(defn how-good [a b position goodness bad-position]
  (if (= position 5)
    (list goodness bad-position)
    (if (= (subs a position (+ 1 position)) (subs b position (+ 1 position)))
          (recur a b (+ 1 position) (+ 1 goodness) bad-position)
          (recur a b (+ 1 position) goodness position))))

(defn matches [a b]
  (let [x (how-good a b 0 0 -1)]
    (list (> (first x) 3) (last x))))

(defn remove-bad-char [in-string bad-index]
  (str (subs in-string 0 bad-index) (subs in-string (+ 1 bad-index))))

(defn sub-parse [left in]
  (let [right (get-five in)]
    (if (not (= right nil))
      (let [match-result (matches left right)]
        (if (first match-result)
          (let [bad-index (last match-result)]
            (println left "is a match. bad-position:" (last match-result) "ans: "(remove-bad-char right bad-index)))
          (recur left (subs in 1)))))))

(defn parse [in]
    (let [left (get-five in)] ;; as soon as we hit the ass end of the input string, get-five returns null and we stop trying to match.
      (if (not (= left nil))
        (do
          (sub-parse left (subs in 1))
          (recur (subs in 1))))))

(parse (slurp "/Users/clojure/challange.txt"))

;; https://www.readyforzero.com/challenge/491f97bc25574346aed237d43e7d0404
;; (answer is i2+r)


texts:
Array of size \$(n-m)\$ with numbers from \$1..n\$ with \$m\$ of them missing. Find all of the missing numbers. The array is sorted.

  Example: If arr = [1,2,4,5,6,8], result should be 3 & 7.

Here is my code which I would like to get reviewed:

import java.util.*;

class FindMissingNumsArray {

    private List<Integer> missing = new ArrayList();

    private void findMissingNums(int[] array) {
        if(array == null)
            return;
        int length = array.length - 1;
        if(length <= 1)
            return;

        getMissings(array, 0, length);
    }

    private void getMissings(int[] array, int lIndex, int hIndex) {
        if(hIndex - lIndex == 2) {
            fillMissings(array, lIndex, lIndex, hIndex);
            return;
        }
        if(lIndex == hIndex)
            return;

        int i = lIndex;
        int j = hIndex;
        int mid = lIndex + (hIndex - lIndex) / 2;
        getMissings(array, i, mid);
        getMissings(array, mid + 1, j);
        fillMissings(array, i, mid, j);
    }

    private void fillMissings(int[] array, int lIndex, int mid, int hIndex){
        int i = lIndex;
        int j = mid;
        int k = hIndex;
        int diff;
        while(i < mid) {
            diff = array[i + 1] - array[i];
            for(int c = 1; diff > 1; diff--) {
                if(!missing.contains(array[i] + c))
                    missing.add(array[i] + c);
                c++;
            }
            i++;
        }
        while(j < k) {
            diff = array[j + 1] - array[j];
            for(int a = 1; diff > 1; diff--) {
                if(!missing.contains(array[j] + a))
                    missing.add(array[j] + a);
                a++;
            }
            j++;
        }
    }

    private void printMissingList() {
        for(int i: missing)
            System.out.print(i + " ");
    }

    public static void main(String[] args) {
        int[] array = {1, 4, 5, 6, 9, 10, 12, 15, 19};
        int[] array1 = {0, 5, 6, 9, 10};
        FindMissingNumsArray findMissings = new FindMissingNumsArray();
        findMissings.findMissingNums(array);

        System.out.print("Missing numbers in array are: ");
        findMissings.printMissingList();

        findMissings.missing = new ArrayList();
        findMissings.findMissingNums(array1);

        System.out.print("Missing numbers in array1 are: ");
        findMissings.printMissingList();
    }
}

I know there are ways to implement this better in terms of logic, run time, data structures, etc. It will be nice to know what I can do to improve the code.


texts: Parsing a list of single numbers and number ranges

texts:
I have input of a string containing a single number (like: \$3\$) or a range (like: \$1-5\$). Sample input, all together, looks like: "1-5,3,15-16", and sample output for that input looks like "1,2,3,4,5,15". Output doesn't need to be sorted.

I built something to parse this, but it's ugly. How can I improve this?

from itertools import chain
def giveRange(numString:str):
    z=numString.split("-")
    if(len(z)==1):
        return [int(z[0])]
    elif(len(z)==2):
        return list(range(int(z[0]),int(z[1])+1))
    else:
        raise IndexError("TOO MANY VALS!")

def unpackNums(numString:str):
    rList=[]
    rList.extend(set(chain(*map(giveRange,numString.split(",")))))
    return rList
unpackNums("1-2,30-50,1-10")


texts: Keeping track of byte count in a binary protocol handler

texts:
I am putting together a fairly simple server that listens for a connection then creates this thread - textbook Java code - then accepts data on that connection.

I am following a protocol that the manufacturer has laid out for start of message (0xfd)  and EOM (0xfe)  as below. I then simply populate a byte array with using a bytecounter.

I think this is the simplest doing so, and it seems to work fine. Is there any possible problem with taking this approach?  I want to be sure using a bytecounter with an array is acceptable. I can't see anyway that bytecounter could get out of sync or anything like that. I like to keeps things simple. Is there anything wrong with this code with regard to stuffing the inbyte[] array?

public void run() {

try {

    boolean socketalive = true;

    MSG_ID = 0;

    disIn = new DataInputStream( in  );
    disOut = new DataOutputStream ( out );

    String msg = null;

    StringBuilder hexforlog;

    byte[] c ;

    int i = 0;
    int bytecounter = 0;

    byte[] inbyte ;

    while( true ){

        i = 0;
        bytecounter = 0;

        inbyte = new byte[1024];

        byte b ;

        try{

            /* read from network until end of message byte is received */
            while( ( b = disIn.readByte() ) !=  (byte)0xfe ){

                /* checking for start of message */
                if( b == (byte)0xfd  ){

                    inbyte = new byte[1024];
                    inbyte[0] = b;
                    bytecounter = 1;

                }
                else {

                     inbyte[bytecounter] = b;
                     bytecounter++;

                }

            }

        }catch ( java.io.EOFException ioef ){
            System.out.println("EOF recieved bytes"  );
            break;
        }

        /*test for link verification */

        if( bytecounter == 15 ){

             byte SOURCEbyte = inbyte[2];
             byte DESTbyte = inbyte[3];

             /* swap bytes per protocol */
             inbyte[2] = (byte)DESTbyte;
             inbyte[3] = (byte)SOURCEbyte;

            disOut.write( java.util.Arrays.copyOfRange(inbyte,0,16) );
            disOut.flush();

        }

        /* process the data as per manufacturer spec */
        else if( bytecounter> 16  ){

            arrayindex = 12;

            int SOM = inbyte[0];
            int CLASS = inbyte[1];
            int SOURCE = inbyte[2];
            int DEST = inbyte[3];
            int MSG_PRG_NUMBER = inbyte[4];
            int SPARE1 = inbyte[5];
            int SPARE2 = inbyte[6];
            int SPARE3 = inbyte[7];

            LENGTH = twoBytesToInt( new byte[] { inbyte[8], inbyte[9] } );

            NUM_MSG = twoBytesToInt( new byte[] {  inbyte[10], inbyte[11]  } );

           System.out.println("LENGTH: " + LENGTH  + " NUM_MSG: " + NUM_MSG );
           System.out.println("Setting NUM_MSG to 1" );

            NUM_MSG = 1;

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

                MSG_ID = twoBytesToInt( new byte[] { inbyte[arrayindex],
                     inbyte[arrayindex+1] } );

                System.out.println("MSG_ID: " + MSG_ID );

                MSG_LENGTH =twoBytesToInt( new byte[] { inbyte[arrayindex+2],
                    inbyte[arrayindex+3] } );

                System.out.println("MSG_LENGTH: " + MSG_LENGTH  );

                arrayindex = arrayindex+4; //first go around 15

                currentarray = java.util.Arrays.copyOfRange(
                   inbyte, arrayindex, arrayindex+MSG_LENGTH+1);

                arrayindex = arrayindex+MSG_LENGTH ;

                //process messages

                if(MSG_ID == 50){
                    processMessage50( currentarray );
                }
                else if(MSG_ID == 70){
                    processMessage70( currentarray);
                }

            }

        }

    }

    disIn.close();
    disOut.close();

} catch (IOException e) {
    e.printStackTrace();
}

}


texts: Rendering drop-down boxes for country and region

texts:
In PHP how can I follow the DRY (don't repeat yourself) principle in this piece of code? The 2 classes in question share an awful lot of code.

<?php

interface iSelect
{
    public static function display();
}

class CountrySelect implements iSelect {

    static $template = 'select_template.php';

    public static function display() {

        if ( class_exists( 'View' ) ) {

            // Get the full path to the template file.

            $templatePath = dirname( __FILE__ ) . '/' . static::$template;

            $viewData = array(
                "options" => '_countries',
                "optionsText" => 'name',
                "optionsValue" => 'geonameId',
                "value" => 'selectedCountry',
                "caption" => 'Country'
            );

            // Return the rendered HTML
            return View::render( $templatePath, $viewData );

        }
        else {
            return "You are trying to render a template, but we can't find the View Class";
        }
    }
}

class RegionSelect implements iSelect {

    static $template = 'select_template.php';

    public static function display() {

        if ( class_exists( 'View' ) ) {

            // Get the full path to the template file.

            $templatePath = dirname( __FILE__ ) . '/' . static::$template;

            $viewData = array(
                "options" => '_regions',
                "optionsText" => 'name',
                "optionsValue" => 'name',
                "value" => 'selectedCity',
                "caption" => 'Region'
            );

            // Return the rendered HTML
            return View::render( $templatePath, $viewData );

        }
        else {
            return "You are trying to render a template, but we can't find the View Class";
        }
    }
}

?>

The code in question is for php templating. Here is the template:

        <select data-bind="options: <? echo $options ?>,
            optionsText: '<? echo $optionsText ?>',
            optionsValue: '<? echo $optionsValue ?>',
            value: <? echo $value ?>,
            optionsCaption: '<? echo $caption ?>'">
        </select>

Here is the View.php which takes the $ViewData and the $template file and then renders it:

<?php

/** View.php **/

class View {

    /**
     * -------------------------------------
     * Render a Template.
     * -------------------------------------
     *
     * @param $filePath - include path to the template.
     * @param null $viewData - any data to be used within the template.
     * @return string -
     *
     */
    public static function render( $filePath, $viewData = null ) {

        // Was any data sent through?
        ( $viewData ) ? extract( $viewData ) : null;

        ob_start();
        include ( $filePath );
        $template = ob_get_contents();
        ob_end_clean();

        return $template;
    }
}
?>


texts:
In an exam question I need to output some numbers self.random_nums with a certain probability self.probabilities:

I have written the following program that successfully returns the correct answer and also a test at the bottom which confirms that everything is working well.

Here my questions:

Are there any better ways that this problem could be solved? Does my
program go against certain conventions that I should regard?
Why is the unittest ignoring the rounding?
Are there any style issues in my program?

Any other comments are appreciated

from __future__ import division  # in case of python 2
import numpy as np
from matplotlib import pyplot as plot

class RandomGen(object):
    def __init__(self):
        self.random_nums = np.array([-1, 0, 1, 2, 3])
        self.probabilities = np.array([0.01, 0.3, 0.58, 0.1, 0.01])

        # generate cumulative probabilities and add a 0 to the beginning and remove the 1 at the end
        self.cum_probabiliies = np.cumsum(self.probabilities)
        self.cum_probabiliies = np.delete(self.cum_probabiliies, self.cum_probabiliies.size - 1)
        self.cum_probabiliies = np.append([0], self.cum_probabiliies)

    def next_num(self, i):
        num = 0
        while num < i:
            rnd = np.random.uniform(low=0.00, high=1)  # between 0 and 0.99 (less than the high)
            x = (self.cum_probabiliies[self.cum_probabiliies <= rnd]).size - 1
            number = self.random_nums[x]
            yield number
            num += 1

    def get_expected_probabilities(self):
        print("Expected probabilities:")
        target = dict(zip(self.random_nums, self.probabilities))
        target = {k: round(v, 2) for k, v in target.items()}
        print(target)
        return target

    def get_actual_probabilities(self, n):
        # create histogram with corresponding buckets
        hist = np.histogram(n, bins=np.append(self.random_nums, max(self.random_nums + 1)))

        s = sum(hist[0])
        print("Actual Probabilities:")
        effective_probabilities = hist[0] / s
        actual = dict(zip(hist[1], effective_probabilities))
        actual = {k: round(v, 2) for k, v in actual.items()}
        print(actual)
        return actual

if __name__ == "__main__":
    r = RandomGen()
    n = list(r.next_num(10000))
    print("Output:")
    print(n)
    print()
    r.get_expected_probabilities()
    print()
    r.get_actual_probabilities(n)

    plot.hist(n)
    plot.show()

As test I suggest the following:

import unittest
import man_exercise

class TestRandomGen(unittest.TestCase):
    def test_next_num(self):
        iterations = 10000
        delta = iterations / 50000

        r = man_exercise.RandomGen()
        n = list(r.next_num(iterations))

        target = r.get_expected_probabilities() # dictionary with numbers and their expected probabilities
        effective = r.get_actual_probabilities(n) # dictionary with numbers and their actual probabilities

        for k,_ in target.items():
            self.assertAlmostEqual(target[k], effective[k], delta=delta)


texts: Write input bytes to a file directly in Python3

texts:
I am using Python 3.6.1. This program simply allows a user to enter some bytes as a "string" and it writes those bytes to a file as actual bytes in the order provided.

import sys

WELCOME_MSG = ("Welcome to Byte Writer. Enter some bytes separated by spaces "
            "like 49 A7 9F 4B when prompted. 'exit' or 'quit' to leave")
FILE_WRITE_ERROR = "Error writing to file... exiting."
INVALID_INPUT_ERROR = ("Improper byte format. Remember to enter "
    "2 chars at a time separated by spaces. Hex digits only")
INPUT_PROMPT = "Enter bytes: "
VALID_CHARS = ['0', '1', '2', '3', '4', '5', '6', '7', '8',
            '9', 'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f']

def main(argv):
    if not validate_args(argv):
        return -1
    with open(argv[1], "ab") as f:
        print(WELCOME_MSG)
        while run_loop(argv, f) is True:
            pass

    print("Have a nice day.")

def run_loop(argv, f):
    output_list = []
    ui = input(INPUT_PROMPT)
    if ui == "exit" or ui == "quit":
        return False

    list_of_str_bytes = ui.split(' ')
    if validate_input_list(list_of_str_bytes) is False:
        print(INVALID_INPUT_ERROR)
        return False

    for b in list_of_str_bytes:
        output_list.append( int(b, 16) )

    try:
        f.write(bytes(output_list))
    except:
        print(FILE_WRITE_ERROR)
        return False
    return True

def validate_args(argv):
    if len(argv) != 2:
        print("USAGE: {} [filename to save to]".format(argv[0]))
        return False
    return True

def validate_input_list(input_list):
    for b in input_list:
        if len(b) != 2 or not valid_chars(b):
            return False

    return True

def valid_chars(chars):
    for c in chars:
        if c not in VALID_CHARS:
            return False

        return True
if __name__ == '__main__':
    main(sys.argv)


texts: Checking if all the characters from an A-Z is present in the string

texts:
A string is said to be complete if it contains all the characters from
  a to z. Given a string, check if it complete or not.

  Input First line of the input contains the number of strings N. It is
  followed by N lines each contains a single string.

  Output For each test case print "YES" if the string is complete, else
  print "NO"

  Constraints 1 <= N <= 10 The length of the string is at max 100 and
  the string contains only the characters a to z.

Here are the two solutions that I wrote:

class TestClass {
    private static byte N;
    public static void main(String args[] ) throws Exception {
        BufferedReader keyboard = new BufferedReader(new InputStreamReader(
                System.in));
       N = Byte.parseByte(keyboard.readLine());
        for (byte i = 0; i < N; i++) {
            String testingString = keyboard.readLine();
            isCompleteString(testingString);
        }
    }

    private static void isCompleteString(String testingString) {
        String resultString= "";
        char[] toChar = testingString.toCharArray();
        int length = toChar.length;
        for (int i = 0; i < length; i++) {
            if(resultString.indexOf(toChar[i])==-1){
                resultString=resultString.concat(toChar[i]+"");

            }

        }

        if(resultString.length()==26)System.out.println("YES");
        else System.out.println("NO");
    }

}

This took 1.6469 secs against various inputs, while this code took:

class TestClass {
    private static byte N;
    public static void main(String args[] ) throws Exception {
        BufferedReader keyboard = new BufferedReader(new InputStreamReader(
                System.in));
       N = Byte.parseByte(keyboard.readLine());
        for (byte i = 0; i < N; i++) {
            String testingString = keyboard.readLine();
            isCompleteString(testingString);
        }
    }

    private static void isCompleteString(String testingString) {

        char[] toChar = testingString.toCharArray();
        int length = toChar.length;
        boolean[] findingAlphabet = new boolean[26];
        for (int i = 0; i < length; i++) {
            byte alphabets = (byte) toChar[i];
            findingAlphabet[122-alphabets]=true;

        }
        int count=0;
        for(boolean b:findingAlphabet){
            if(b)
            count++;
        }
        if(count==26)System.out.println("YES");
        else System.out.println("NO");
    }

}

1.5818 secs against various inputs.

How can I optimize these programs further (I have used BitSet)? What are other possible optimized methods of doing the same?


texts: Determine file provider based on column headers, replace columns and process data

texts:
Each day I receive files from different customers relaying the same information, but the customers use proprietary formats. For example,

Customer1Data = {'User':['BobJones','BobJones','BobJones'],
'UserSong':['Song1','Song2','Song3'],
'UserSplit':[0.50,0.25,0.50],
'UserEarnings':[100,200,300]}

Customer2Data = {'Name':['BobJones','BobJones','BobJones'],
'SongName':['Song1','Song2','Song3'],
'SongSplit':[0.50,0.25,0.50],
'SongEarnings':[100,200,300]}

I would like to be able to read in whatever CSV file is present in the download directory, replace the column names and then do summations based on replaced column names. Right now, I do this.

from collections import namedtuple
_customers = namedtuple('CustomerName',['statement_headers','first_column'])
Customer1 = _customers(statement_headers = {'User':'Name',
'UserSong':'Song',
'UserSplit':'Split',
'UserEarnings' :'Earnings'},
first_column = 'User')

Customer2 = _customers(statement_headers = {'Name':'Name',
'SongName':'Song',
'SongSplit':'Split',
'SongEarnings':'Earnings'},
first_column = 'Name')

I then create a dictionary with the first column from each customers csv file:

_customertypes = {'Customer1':Customer1,'Customer2':Customer2}
def _create_first_col_dictionary():
    first_col = {}
    for k,v in _customertypes.items():
        first_col[v.first_column] = k
    return first_col

Create a dictionary to hold the csv based on the file type, read in the header row and assign to csv_dict

import pandas as pd
csv_dict = {k:[] for k in _customertypes.keys()}
files = [r'C:\location\csv1.csv',r'C:\location\csv2.csv',r'C:\location\csv3.csv']
for file in files:
    x = pd.read_csv(file,nrows = 2,encoding = 'LATIN-1')
    col = x.columns.tolist()[0]
        file_type = _first_col[col]
        csv_dict[file_type].append(file)

Now I am left with this:

csv_dict = {'Customer1':[r'C:\location\csv1.csv',r'C:\location\csv3.csv'],
'Customer2':[r'C:\location\csv2.csv']}

Finally, I merge the like csv files together for Customer1 and Customer2, then replace column names using the dictionary above and do analysis.

I am curious if anyone has better suggestions on how to do something like this. I think it could require a level of maintenance (adding new columns to the dictionaries, adding the first_column name, etc).


texts:
Exercise description:

Find the most common number in an array of numbers.

Create a software that takes an array of numbers as parameter.
The return of the function shall be the most common number.

Example:
Assume that the list is ... 1, 5, 1, 3, 5, 5, 2.

The program will output 5, because this is the most common number.

Note that if there is more than one most common number, the program
will just print one of the most common numbers.

My solution (written in TypeScript):

let numbers: number[] = [1, 5, 1, 3, 5, 5, 2];

namespace Main {
  export function searchMostCommonNumber(arr: number[] = []): number {
    let current: number = 0;
    let max: number = 0;
    let mostCommonNumber: number = 0;
    let i: number;

    for (i = 0; i < arr.length - 1; i++) {
      let current: number = 1;
      let j: number;

      for (j = i + 1; j < arr.length; j++) {
        if (arr[i] === arr[j]) {
          current++;
        }
      }

      if (current > max) {
        max = current;
        mostCommonNumber = arr[i];
      }
    }

    return mostCommonNumber;
  }
}

console.log(Main.searchMostCommonNumber(numbers));

Live demo (with compiled code):

var numbers = [1, 5, 1, 3, 5, 5, 2];
var Main;
(function(Main) {
  function searchMostCommonNumber(arr) {
    if (arr === void 0) {
      arr = [];
    }
    var current = 0;
    var max = 0;
    var mostCommonNumber = 0;
    var i;
    for (i = 0; i < arr.length - 1; i++) {
      var current_1 = 1;
      var j = void 0;
      for (j = i + 1; j < arr.length; j++) {
        if (arr[i] === arr[j]) {
          current_1++;
        }
      }
      if (current_1 > max) {
        max = current_1;
        mostCommonNumber = arr[i];
      }
    }
    return mostCommonNumber;
  }
  Main.searchMostCommonNumber = searchMostCommonNumber;
})(Main || (Main = {}));
console.log('The most common number is :', Main.searchMostCommonNumber(numbers));

What do you think about my solution?

Is there a better way to accomplish the task? What would you have done differently?

Looking forward to reading your comments and answers.


texts:
I want to check in my if and else if statement if the inputs form a square or a rectangle, now my if and else if statements seems to be unreadable, now I'm thinking of ways to improve it.

  /* input are angles in degrees, 1st side is parallel to 3rd side, 2nd side is parallel to 4th side*/
  static void Main(string[] args)
  {
     Console.Write("Input first: ");
     int first = int.Parse(Console.ReadLine());

     Console.Write("Input second: ");
     int second = int.Parse(Console.ReadLine());

     Console.Write("Input third: ");
     int third = int.Parse(Console.ReadLine());

     Console.Write("Input fourth: ");
     int fourth = int.Parse(Console.ReadLine());

     // all sides are equal
     // logic for if and else-if, needs codereview for improvement
     if (first == second && first == third && first == fourth && second == third && second == fourth && third == fourth)
     {
        Console.WriteLine("Square");
     }
     else if (first != second && first == third && first != fourth && second != third && second == fourth )
     {
        Console.WriteLine("Rectangle");
     }
     else
     {
        Console.WriteLine("Not a square nor a rectangle");
     }

     Console.ReadKey();
  }


texts: Model Pipeline to run multiple Classifiers for ML Classification

texts:
As a general rule of thumb, it is required to run baseline models on the dataset. I know H2O- AutoML and other AutoML packages do this. But I want to try using Scikit-learn Pipeline,
Here is what I have done so far,
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import BernoulliNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.pipeline import Pipeline
from xgboost import XGBClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

import os
rs = {'random_state': 42}

# Train-test Split
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size = 0.33,
                                                    random_state = 42)

# Classification - Model Pipeline
def modelPipeline(X_train, X_test, y_train, y_test):

    log_reg = LogisticRegression(**rs)
    nb = BernoulliNB()
    knn = KNeighborsClassifier()
    svm = SVC(**rs)
    mlp = MLPClassifier(max_iter=500, **rs)
    dt = DecisionTreeClassifier(**rs)
    et = ExtraTreesClassifier(**rs)
    rf = RandomForestClassifier(**rs)
    xgb = XGBClassifier(**rs, verbosity=0)

    clfs = [
            ('Logistic Regression', log_reg),
            ('Naive Bayes', nb),
            ('K-Nearest Neighbors', knn),
            ('SVM', svm),
            ('MLP', mlp),
            ('Decision Tree', dt),
            ('Extra Trees', et),
            ('Random Forest', rf),
            ('XGBoost', xgb)
            ]

    pipelines = []

    scores_df = pd.DataFrame(columns=['Model', 'F1_Score', 'Precision', 'Recall', 'Accuracy', 'ROC_AUC'])

    for clf_name, clf in clfs:

        pipeline = Pipeline(steps=[
                                   ('scaler', StandardScaler()),
                                   ('classifier', clf)
                                   ]
                            )
        pipeline.fit(X_train, y_train)

        y_pred = pipeline.predict(X_test)
        # F1-Score
        fscore = skm.f1_score(y_test, y_pred)
        # Precision
        pres = skm.precision_score(y_test, y_pred)
        # Recall
        rcall = skm.recall_score(y_test, y_pred)
        # Accuracy
        accu = skm.accuracy_score(y_test, y_pred)
        # ROC_AUC
        roc_auc = skm.roc_auc_score(y_test, y_pred)

        pipelines.append(pipeline)

        scores_df = scores_df.append({
                                      'Model' : clf_name,
                                      'F1_Score' : fscore,
                                      'Precision' : pres,
                                      'Recall' : rcall,
                                      'Accuracy' : accu,
                                      'ROC_AUC' : roc_auc

                                      },
                                     ignore_index=True)

    return pipelines, scores_df

I have a nagging feeling that there's something I might be overlooking, or missing, or that there might be a more efficient way to do this.
Is there a better / more elegant / more accurate way to run multiple classifiers for ML classification?
Efficiency is also good, though we never call this on large datasets, it is not a huge concern.


texts:
I am creating a class object which must inherit from numpy ndarrays.  I perform an isinstance check and an array.shape check with the __new__ method.  Is it alright to have it here, or should it be elsewhere?  I saw suggestions to create an exceptions class to accompany it, but it doesn't seem necessary... maybe preferable.

'''
pnts_instantiation.py
'''
import numpy as np
import sys
from types import *

class Points(np.ndarray):
  '''ndarray required,info='array info',name='pnts'')
  '''
  def __new__(cls,arr=None,info=None,name='pnts'):
    '''create Points from existing data'''
    err = 'Failed...Requires an ndarray...\nProvided: {}'.format(type(arr))
    if isinstance(arr,(NoneType,StringType,UnicodeType,ListType,TupleType)):
      return err
    if arr.shape < (4,2):
      return ('Failed...Requires array shape > (4,2)\nProvided: {}'.format(arr.shape))
    self = np.asarray(arr).view(cls)   # view as Points class
    self.info = info          # set info
    self.name = name          # set name
    self.applied = None
    # other properties not pertinent to discussion removed for simplicity
    return self

  def __array_finalize__(new_arr, src_arr):
    '''new_arr: new Points object...housecleaning takes place
         for explicit, view casting or new from template...
       src_arr: None, any subclass of ndarray including our own OR another
         instance of our own array (see docs)'''
    if src_arr is None: return
    new_arr.applied = getattr(src_arr,'applied',None) # provide a default
    new_arr.name = getattr(src_arr,'name',None)

  def __array_wrap__(self,out_arr,context=None):
    '''wrap it up'''
    return np.ndarray.__array_wrap__(self, out_arr, context)

  def __repr__(self):
    '''return point info, shape and dtype'''
    s = self
    st = '==> {}'.format(s)
    if (hasattr(s,'name')) and (hasattr(s,'info')):
      st = 'name:  {}\nshape: {}\ninfo:  {}\nvalues:\n{}'.format(s.name,s.shape,s.info,s)
    elif (hasattr(s,'name')) and (hasattr(s, 'applied')):
      st = '{}.{}:  {}'.format(s.name,s.applied, s)
    else:
      st = '{}: {}'.format(s.applied,s)
    return st

def test_cases():
  '''conditional case check'''
  cases = [None,
           'string',
          [[1,2],[3,4]],
          np.asarray([[1,2],[3,4]],dtype='float64',),
          np.ma.asarray([[1,2],[3,4],[5,6]],dtype='float64'),
          np.asarray([[1,2],[3,4],[5,6]],dtype='float64'),
          np.asarray(zip(np.arange(5),np.arange(5)),dtype='float64')
          ]
  counter = 0
  for a_case in cases:
    print('\nCase: {}\nIn: {}\nOut: {}'.format(counter,a_case,Points(a_case)))
    counter += 1

#-------------------
if __name__ == '__main__':
  test_cases()


texts: Finding the postion of a substring in a string

texts:
I am given two strings and return the position if in the larger string the smaller string is found.  If not I am returning -1 for position.  Any suggestions for improving my code.

using System;
using System.Collections.Generic;
using System.Linq;

namespace Reverse
{
    class MainClass
    {
        static int IsFound (string stringOne, string stringTwo){
            int j = 0;
            int position = -1;
            int length = stringTwo.Length - stringOne.Length;
            for (int i = 0; i < length; i++) {
                if (stringOne [j] == stringTwo [i]) {
                    position = i;
                    while (stringOne [j] == stringTwo [i]) {
                        j++;
                        i++;
                        if (stringOne.Length == j) {
                            return position;
                        }
                    }
                    j = 0;
                    position = -1;
                }
            }
            return position;
        }
        public static void Main (string[] args)
        {
            string stringOne = "charter";
            string stringTwo = "cstarchabsccharldmscharterfadgafga";
            int test = IsFound (stringOne, stringTwo);
            Console.WriteLine(test);
            string stringThree = "atfc";
            test = IsFound (stringThree, stringTwo);
            Console.WriteLine(test);
        }
    }
}


texts: Suggestions on how to improve this .htaccess file for page load speed

texts:
Here is my standard .htaccess file, any suggestions on how I can improve it with regards to page load speed? Would gZip instead of DEFLATE make any sort of difference?

ExpiresActive On
RewriteEngine on
RewriteBase /

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Expires 1 month after file is first accessed
ExpiresByType image/jpeg A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType text/plain A2592000

# Expires 1 month after file is modified
ExpiresByType application/x-javascript M2592000
ExpiresByType text/css M2592000
ExpiresByType text/javascript M2592000
ExpiresByType text/html M2592000


texts: Word frequencies from large body of scraped text

texts:
I have a file with word frequency logs from a very messy corpus of scraped Polish text that I am trying to clean to get accurate word frequencies. Since this is a big text file, I divided it into batches.

Here is a snippet from the original file:

 1 środka(byłe
 1 środka.było
 1 środkacccxli.
 1 (środkach)
 1 „środkach”
 1 środkach
 1 środkach...
 1 środkach.",
 1 środkach"
 1 środkach".
 1 środkachwzorem
 1 środkach.życie
 1 środkajak
 1 "środkami"
 1 (środkami)
 1 „środkami”)
 1 środkami!"
 1 środkami”
 1 środkami)?
 1 środkami˝.

My goal is to clean true word labels and remove noisy word labels (e.g. collocations of words concatenated through punctuation). This is what is achieved by the first part of the script. As you can see in the data sample above, several noisy entries belong to the same true label. Once cleaned, their frequencies should be added. This is what I try to achieve in the second part of my script.

Here is the code in one piece with fixed indentation, in case you are able to reproduce my issues on your end:

# -*- coding: utf-8 -*-

import io
import pandas as pd
import numpy as np

num_batches = 54

for i in range(1 ,num_batches +1):

    infile_path = r'input_batch_' + str(i) + r'.txt'
    outfile_path = r'output_batch_' + str(i) + r'.txt'

    with io.open(infile_path, 'r', encoding = 'utf8') as infile, \
         io.open(outfile_path, 'w', encoding='utf8') as outfile:

        entries_raw = infile.readlines()
        entries_single = [x.strip() for x in entries_raw]
        entries = [x.split('\t') for x in entries_single]

        data = pd.DataFrame({"word": [], "freq": []})

        for j in range(len(entries)):
            data.loc[j] = entries[j][1], entries[j][0]

            freq_dict = dict()
            keys = np.unique(data['word'])

            for key in keys:
                for x in range(len(data)):
                    if data['word'][x] == key:
                        if key in freq_dict:
                            prior_freq = freq_dict.get(key)
                            freq_dict[key] = prior_freq + data['freq'][x]
                        else:
                            freq_dict[key] = data['freq'][x]

        for key in freq_dict.keys():
        outfile.write("%s,%s\n" % (key, freq_dict[key]))

The problem with this code is that it is either buggy, running into an infinite loop or sth, or is very slow, even for processing a single batch, to the point of being impractical. Are there ways to streamline this code to make it computationally tractable? In particular, can I achieve the same goal without using for loops? Or by using a different data structure for word-frequency lookup than a dictionary?


texts: Finding the highest values a and b from an array

texts:
I struggled a short while to make this work, so I'm wondering if there are any smarter ways for finding the highest of two values per array element? Uh, hard to explain by words, but by code its easy:

Data (random):

    (a) (b) // a and b are both positive values.
    10  0.5
    13  10.8
    8   0.123
    9   0.123
    17  0.3
    17  0.4  <- must find this
    17  0.1
    0   0.13
    0   0.5

Code:

int max_a = -1;
float max_b = -1;

for(int i = 0; i < total_values; i++){
    if(values[i].a > max_a){
        max_a = values[i].a;
        max_b = -1; // reset
    }
    if(values[i].a == max_a){
        if(values[i].b > max_b){
            max_b = values[i].b;
            found_id = i;
        }
    }
}

So far it seems to work. But is this the most efficient way of doing this? (without changing the for loop; must loop linearly like that)

Edit: Some notes: I use std::vector for my arrays. The code should work with any struct given to the vector, only 2 of the elements from that struct will be used for value checking. Edit2: Values cannot have negative values, thats why there is initial -1 values set to mean "not set".

Edit: My test code:

struct valuestruct {
    int a;
    float b;
    // this could have more values than just two. still, only two values are tested.
    // using shortest struct as possible to save memory for testing.

    valuestruct(int a, float b) : a(a), b(b) {}
};

bool operator<(const valuestruct &v1, const valuestruct &v2){
    return (v1.a < v2.a) || ((v1.a == v2.a) && (v1.b < v2.b));
}

void test_speeds(){
    // initialize array values:
    int total_values = 10000000; // = 80megs ram
    vector<valuestruct> values;
    for(int i = 0; i < total_values; i++){
        values.push_back(valuestruct(random(0,1000), random(0.0f,100.0f)));
    }

    int found_id = -1;
    // dummy stuff for preventing loops optimized off:
    int ids = 0;
    int out_id1 = -1;
    int out_id2 = -1;
    int out_id = -1;

    // Loki's solution:
    float t1 = microtime();
    if(total_values > 0){ // prevent crash accessing empty array.
        for(int dummy_repeat = 0; dummy_repeat < 100; dummy_repeat++){
            valuestruct max = values[0];
            for(int i = 1; i < total_values; i++){
                if(max < values[i]){
                    max = values[i];
                    found_id = i;
                }
            }
            out_id = found_id;
            ids += found_id; // dummy
        }
        out_id2 = out_id;
    }
    float speed1 = microtime()-t1;

    // Keith's solution:
    float t2 = microtime();
    for(int dummy_repeat = 0; dummy_repeat < 100; dummy_repeat++){
        int max_a = -1;
        float max_b = -1;
        for(int i = 0; i < total_values; i++){
            if(values[i].a > max_a || (values[i].a == max_a && values[i].b > max_b)){
                max_a = values[i].a;
                max_b = values[i].b;
                found_id = i;
            }
        }
        out_id = found_id;
        ids += found_id; // dummy
    }
    out_id1 = out_id;
    float speed2 = microtime()-t2;

    clock_t t3 = clock();
    for(int dummy_repeat = 0; dummy_repeat < 100; dummy_repeat++){
        std::vector<valuestruct>::iterator find = std::max_element(values.begin(), values.end());
        found_id = std::distance(values.begin(), find);
        out_id = found_id;
        ids += found_id; // dummy
    }
    clock_t speed3 = clock() - t3;

    // Output:
    // Lokis: 6166.7ms
    // Keith: 3932.1ms

   // Timing on Loki's machine (using 1000 dummy iterations)
   // Compiled uisng g++ -O3 (replaced microtime() with clock())
   // Times are in seconds (result is the value of ids)
   Loki:  12.814812000 Result: 53500000
   Keith: 12.885715000 Result: 53500000
   Max:   12.832419000 Result: 53500000
}


texts: AutoreleasePool NSString Automatic Reference Counting

texts:
I have some doubt about how NSString is released in ARC Mode.

I would like to know if have something i can do to release a nsstring when i want in arc mode.

Take a look at this code:

__block NSString *strImage = nil;
    dispatch_sync(backGround, ^{
        // Convert NSData To NSString.
        strImage = [UIImagePNGRepresentation([UIImage imageWithData:reg.photoData]) base64Encoding];
    });

    // Prepare Request Operation.
    NSString * strPost = @"myUrl";

    strPost = [strPost stringByAppendingFormat:@"}&image="];
    strPost = [strPost stringByAppendingFormat:@"%@",strImage];
    //NSLog(@"POST: %@",strPost);

    // setting up the URL to post to
    NSString *urlString = @"myURLAPI";

    // setting up the request object now
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    /*
     add some header info now
     we always need a boundary when we post a file
     also we need to set the content type

     You might want to generate a random boundary.. this is just the same
     as my output from wireshark on a valid html post
     */
    NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the post
     */
    NSMutableData *body = [NSMutableData data];

    [body appendData:[NSData dataWithData:[strPost dataUsingEncoding:NSUTF8StringEncoding]]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

    //MY DOUBT ABOUT RELEASE THIS STR
    @autoreleasepool {
        strImage = nil;
    }

is it correct or have a best way to do it? Thank you


texts: Filtering a large (50gb+) JSON lines file matching CIDR's

texts:
I'm trying to speed up a Python script that reads a large log file (JSON lines, 50gb+) and filter out results that match 1 of 2000 CIDR ranges.

Logfile

20 million lines

{"ip":"xxx.xxx.xxx.xxx","timestamp":"2017-05-27T04:00:35-04:00","data":{},"error":"EOF","error_component":"banner"}
{"ip":"xxx.xxx.xxx.xxx","timestamp":"2017-05-27T04:00:35-04:00","data":{"banner":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ehlo":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","starttls":"500 Unknown command\r\n"},"error":"Bad return code for STARTTLS","error_component":"starttls"}
{"ip":"xxx.xxx.xxx.xxx","timestamp":"2017-05-27T04:00:35-04:00","data":{},"error":"EOF","error_component":"banner"}
{"ip":"xxx.xxx.xxx.xxx","timestamp":"2017-05-27T04:00:35-04:00","data":{"banner":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ehlo":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","starttls":"502 No such command\r\n"},"error":"Bad return code for STARTTLS","error_component":"starttls"}

CIDR file

2,000 lines

86.80.0.0/12
77.160.0.0/12
188.200.0.0/13
178.224.0.0/13
84.24.0.0/13

Script

import sys
import json
from netaddr import *

reload(sys)
sys.setdefaultencoding('utf-8')

filename = 'results.json'
filename = unicode(filename, 'utf-8')
cidr_filename = 'cidr.txt'

rowcount = 0
count = 0

# Load CIDR ranges
with open(cidr_filename, 'r') as f:
    cidr = [line.strip() for line in f]

# Load JSON line by line
with open(filename) as f:
    for line in f:
        output = json.loads(line)
        rowcount += 1

        # Match if IP is in CIDR ranges
        if all_matching_cidrs(output['ip'], cidr):
            if 'banner' in output['data']:
                print(output['ip'] + '\t' + output['data']['banner'])
                count += 1

print('---------------------------------------')
print('LINES:   {rowcount}')
print('RESULTS: {count}')
print('---------------------------------------')

Current results

Parsing an example set of 100,000 rows takes now 8 minutes using:

Pypy
MacBook Pro with 2.8 GHz Intel Core i7, 16Gb RAM, SSD

Parsing the complete set of 20,000,000 rows would take a staggering 26 hours.

---------------------------------------
LINES:   100000
RESULTS: 1243
---------------------------------------

real    7m57.739s
user    7m52.127s
sys     0m4.177s

The bottleneck is the number of CIDR ranges to search within, when I run an example set of 100,000 row against 1 CIDR range it takes only 1.2 seconds.

---------------------------------------
LINES:   100000
RESULTS: 4
---------------------------------------

real    0m1.201s
user    0m1.095s
sys     0m0.090s

Is there a faster way of accomplishing this? Would Multithreading/Multiprocessing speed things up? Any help or other feedback would be much appreciated!

Things I've done:

Using Pypy, this is 9x(!) faster than Python 2.7 for this job.
Tried using Tim Bray's Widefinder but couldn't make it work as it focuses on regex searches IMHO.

UPDATE

rolfl's solution brought my times to parse 20,344,457 rows from ±26 hours to 4.5 minutes!

---------------------------------------
LINES:   20344457
RESULTS: 130863
---------------------------------------

real    4m27.661s
user    3m55.171s
sys     0m26.793s

TemporalWolf's advice to cProfile my code showed that indeed json.loads() was a bottleneck:

ncalls    tottime  percall  cumtime  percall filename:lineno(function)
16607394  131.960    0.000  131.960    0.000 {_pypyjson.loads}

Following his advice to slice the IP address natively instead of loading each line as JSON it was 2.5x times faster!

---------------------------------------
LINES:   20344457
RESULTS: 130863
---------------------------------------

real    1m40.548s
user    1m13.885s
sys     0m22.664s


texts:
I am practicing for interviews and I tried solving the problem on my own and got a solution. I  was wondering how can I improve the performance and memory on this program?

The problem performs addition on linked list nodes. The nodes are 1s->10s->100s->.... The goal of this program is to perform addition on two of these linked lists. The output should be a linked list of the same format.

Example:

  1 -> 4 -> 3
+ 1 -> 5 -> 9 -> 2

Linked list output:

2 -> 9 -> 2 -> 3

Here is my code:

public class prob2_5 {
    /*
     * You have two numbers represented by a linked list, where each node contains a single digit.
     * The digits are stored in reverse order, such that the 1's digit is at the head of the list.
     * Write a function that adds the two numbers and returns the sum as a linked list
     *
     */

    public static LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2) {
        int l1sum = 0;
        int l2sum = 0;
        int multiplier = 1;

        while(l1 != null) {
            l1sum = l1sum + l1.value*multiplier;
            multiplier = multiplier*10;
            l1 = l1.next;
        }
        multiplier = 1;
        while(l2 != null) {
            l2sum = l2sum + l2.value*multiplier;
            multiplier = multiplier*10;
            l2 = l2.next;
        }

        int total = l2sum + l1sum;

        char[] totalnum = (total + "").toCharArray();
        int len = totalnum.length;
        LinkedListNode tail = new LinkedListNode(totalnum[len - 1] - '0');
        LinkedListNode newNode;
        LinkedListNode prevNode = tail;
        System.out.print(totalnum);

        for (int i = len - 2; i >= 0; i--) {
            newNode = new LinkedListNode(totalnum[i] - '0');
            prevNode.next = newNode;
            prevNode = newNode;
        }
        return tail;
    }
}

Here is my linked list class:

public class LinkedListNode {
    LinkedListNode next;
    int value;
    public LinkedListNode(int value) {
        this.next = null;
        this.value = value;
    }
}


texts:
I have successfully implemented a simple login page using ReactJS by coding it all in JavaScript (a flexibility that I like) but I am still a beginner in React and would like to know whether this is efficient and modular and if anything can be improved.

"use strict";

function Login(email, password) {
  var LOGIN_FLAG = 1002
  $.post("/server/account/login.php", { email: email, password: password })
   .done(function(data) {
     var JSONData = tryParseJSON(data);
     if (!JSONData || JSONData["statusCode"] == LOGIN_FLAG) {
       ShowFailureAtDOM("container");
       return;
     }

     var JWTData = KJUR.jws.JWS.parse(JSONData["jwt"]);
     if (JWTData) {
       ShowSuccessAtDOM("container", JWTData.payloadObj.name);
     } else {
       ShowFailureAtDOM("container");
     }
   });
};

function ShowSuccessAtDOM(id, name) {
  ReactDOM.unmountComponentAtNode(document.getElementById(id));
  ReactDOM.render(
    <LoginSuccess name={name} />,
    document.getElementById(id)
  );
};

function ShowFailureAtDOM(id) {
  ReactDOM.unmountComponentAtNode(document.getElementById(id));
  ReactDOM.render(
    <LoginFail />,
    document.getElementById(id)
  );
};

var Header = React.createClass({
  render() {
    return (

Web App - Login

    )
  }
});

var LoginForm = React.createClass({
  ValidateLogin() {
    var email = this.refs.loginEmail.state.value;
    var password = this.refs.LoginPassword.state.value;
    Login(email, password);
  },
  render() {
    return (

        <LoginEmail ref="loginEmail"/>
        <LoginPassword ref="LoginPassword"/>

        <LoginSubmit ValidateLogin={this.ValidateLogin}/>

    )
  }
});

var LoginEmail = React.createClass({
  getInitialState() {
    return {value: null}
  },
  onChange(e) {
    this.setState({value: e.target.value});
  },
  render() {
    return (

Email:

        <input type="text" onChange={this.onChange}/>

    )
  }
});

var LoginPassword = React.createClass({
  getInitialState() {
    return {value: null}
  },
  onChange(e) {
    this.setState({value: e.target.value});
  },
  render() {
    return (

Password:

        <input type="password" onChange={this.onChange}/>

    )
  }
});

var LoginSubmit = React.createClass({
  onClick() {
    this.props.ValidateLogin();
  },
  render() {
    return (
      <button onClick={this.onClick}>Login</button>
    )
  }
});

var LoginSuccess = React.createClass({
  render() {
    return (

Login Success! Welcome Back, {this.props.name}

    )
  }
});

var LoginFail = React.createClass({
  render() {
    return (

Login FAIL...

    )
  }
});

ReactDOM.render(
  <LoginForm />,
  document.getElementById("container")
);


texts: Find all letter Combinations of a Phone Number

texts:
The task

is taken from LeetCode

  Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A
  mapping of digit to letters (just like on the telephone buttons) is
  given below. Note that 1 does not map to any letters.

  Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

  Note:

  Although the above answer is in lexicographical order, your answer could be in any order you want.

My solution

(I've been told I should provide additional information to my solution otherwise I get downvoted.)

is based on backtracking. Why I choosed that approach? Well, whenever you encounter a task where you have to combine or permutate things, then backtracking is a possible approach.

But if you know a better one, then please go ahead. Otherwise I only got generic questions to my code: Can you make it faster, cleaner, more readable, etc.?

Also I'm interested in functional programming. So, if you got a good functional approach, feel free to post it here, too. Other than that, I'm always interested in a different perspective. So, if you got a fancy or conservative approach, please feel free to post them here.

Imperative approach

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function(digits) {
  if (digits === '') { return []; }
  const strDigits = {
    '2': 'abc',
    '3': 'def',
    '4': 'ghi',
    '5': 'jkl',
    '6': 'mno',
    '7': 'pqrs',
    '8': 'tuv',
    '9': 'wxyz',
  };

  if (digits.length === 1) { return [...strDigits[digits]]; }
  const res = [];
  const combine = (cur, n) => {
    if (cur.length === digits.length) {
      res.push(cur);
      return;
    }

    [...strDigits[digits[n]]].forEach(x => {
      combine(cur + x, n + 1);
    });

  };

  combine('', 0);
  return res;
};

Functional approach

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function(digits) {
  if (digits === '') { return []; }

  const strDigits = {
    '2': 'abc',
    '3': 'def',
    '4': 'ghi',
    '5': 'jkl',
    '6': 'mno',
    '7': 'pqrs',
    '8': 'tuv',
    '9': 'wxyz',
  };

  const combine = (cur, n) => cur.length === digits.length
    ? cur
    : [...strDigits[digits[n]]].flatMap(x => combine(cur + x, n + 1));

  return combine('', 0);
};


texts: Shorten JavaScript Code due to duplicate variables without polluting global namespace

texts:
I am at a loss as to how to simplify the following two functions that share variables without polluting the global namespace. (Partial code for a carousel). I would like to streamline it.

Note:
snapshotCheck(); is called once on window load.
snapshotScroll(); is called on click events.

//FIRST FUNCTION
function snapshotScroll(amount, refresh) {
  var imageSum = snapshots.total_entries; //Inner Box
  var dl = $j('#snapshots')[0]; //Outer Box
  var c = $j('#photo-carousel')[0];
  var l = ((dl.style.left) ? parseInt(dl.style.left) : 0);
  var pos = l + parseInt(amount);
  var pos0 = pos;
  var viewport = $j('#photo-carousel')[0];
  var inView = Math.floor(viewport.clientWidth / snapshotWidth); //How many snapshots in view
  var maxPos = (imageSum * -snapshotWidth);
  if(pos > 0) { // prevent left side from going too far right
      pos = 0;
  } else if(pos < c.clientWidth - dl.clientWidth) {
      pos = c.clientWidth - dl.clientWidth;
      if(pos == 8) { // To account for 8px variation, set position to initial pos0 for animation.
          pos = pos0;
      }
  }

  $j(dl).animate({"left" : pos}, 1200); // Animate scroll
  // Toggle "filter" class (reduces opacity) depending on position.
  // if position is max, toggleClass of #next.
  // if position is 0, toggleClass of #prev.
  $j('#next').toggleClass('filter', (pos - maxPos) == (c.clientWidth))
  $j('#prev').toggleClass('filter', (pos >= 0))

  if(imageSum > inView) {
     dl.style.width = maxPos * -1 + "px";
  }
} // end snapshotScroll();

//SECOND FUNCTION
function snapshotCheck(){ // Check if snapshot is in view
  var c = $j('#photo-carousel')[0]; // #snapshots parent container
  var dl = $j('#snapshots')[0]; // ol container holding snapshots
  var imageSum = snapshots.total_entries;
  var l = ((dl.style.left) ? parseInt(dl.style.left) : 0);
  var maxPos = (imageSum * -snapshotWidth);
  var setWidth;
  var viewport = $j('#photo-carousel')[0]
  var inView = Math.floor(viewport.clientWidth / snapshotWidth); // # of snapshots in view
  (setWidth = function(){ // set initial width in order to retrieve position of currentSnapshot
    if(imageSum > inView) {
      dl.style.width = maxPos * -1 + "px";
    }
  })();
  if(typeof currentSnapshot != 'undefined') {
    var aSnapshot = $j('#' + currentSnapshot.snapshot.id + '_slide');  // jQuery object, array
    var theSnapshot = $j('#' + currentSnapshot.snapshot.id + '_slide')[0];  // first DOM element
    var pos = -aSnapshot.position().left; // a negative value, set to first position carousel (in view)
    $j(dl).animate({"left" : pos}, 1200); // Animate scroll
  }
  $j('#next').toggleClass('filter', (pos - maxPos) == (c.clientWidth))
  $j('#prev').toggleClass('filter', (pos >= 0))
  } // end snapshotCheck();