texts:
I calculate a highscore and find the users current rank like shown here. This is the query:

SELECT * FROM (SELECT  q.*, @r := @r + 1 AS rank
FROM ( SELECT  @r := 0 ) vars,
(
    SELECT  p.*, MAX(punkte) AS max_points
    FROM olympia_punkte_alle p
    WHERE markt_id = 1
    GROUP BY fb_id
    ORDER BY max_points DESC
) q) AS q2 WHERE fb_id = '26950' ORDER BY max_points DESC LIMIT 1

Problem is that the table has many rows (~8,5 Mio) and so this query is very slow. I could cache the sub select but than the highscore is always kind of old.

Any thoughts on how to make this faster?

EXPLAIN:

id  select_type         table   type    possible_keys   key key_len ref rows    Extra
1   PRIMARY <derived2>  ALL     NULL    NULL    NULL    NULL    1352    Using where; Using filesort
2   DERIVED <derived3>  system  NULL    NULL    NULL    NULL    1
2   DERIVED <derived4>  ALL     NULL    NULL    NULL    NULL    1352
4   DERIVED             p       ref     markt_id 4              2055    Using where; Using temporary; Using filesort
3   DERIVED NULL        NULL    NULL    NULL    NULL    NULL    NULL    No tables used


texts:
This is one of the first Verilog programs I have written.  I have a Xilinx Artix-7 FPGA card. Right now I just have it transmitting an "X" every second.  It works and I can see the result in my serial terminal.  It uses a UART over USB connection.

I'm just wondering if I can get some feedback on my code and if you see any problems.

module uart_top(input clk,
                input rx,
                output tx);

    reg [31:0] count = 0;
    wire ready;
    uart_send sender("X", count == 100000000, clk, tx, ready);

    always @(posedge clk)
        if(count == 100000000) count <= 0;
        else count <= count + 1;

endmodule

module uart_send #(parameter BAUD_RATE = 9600,
                   parameter CLOCK_SPEED_MHZ = 100)
                  (input [7:0] data_byte,
                  input start_send,
                  input clk,
                  output tx,
                  output ready);

        parameter integer CYCLES_WAIT = CLOCK_SPEED_MHZ * 1e6 / BAUD_RATE;

        parameter IDLE = 0;
        parameter START_BIT = 1;
        parameter END_BIT = 2;
        parameter DATA_BIT = 3;

        reg [2:0] state = IDLE;
        reg [15:0] cycle_count = 0;
        reg [3:0] bit_index = 0;
        reg [7:0] data;

        assign tx = state == IDLE ? 1 :
                    state == START_BIT ? 0 :
                    state == END_BIT ? 1 :
                    data[bit_index];

        assign ready = state == IDLE;

        always @(posedge clk) begin
            if(state != IDLE)
                data <= data_byte;
                if(cycle_count == CYCLES_WAIT) cycle_count <= 0;
                else cycle_count <= cycle_count + 1;

            if(state == IDLE && start_send) begin
                state <= START_BIT;
                cycle_count <= 0;
            end else if(state == START_BIT && cycle_count == CYCLES_WAIT) begin
                state <= DATA_BIT;
                bit_index <= 0;
            end else if(state == DATA_BIT && cycle_count == CYCLES_WAIT) begin
                if(bit_index == 7) state <= END_BIT;
                else bit_index <= bit_index + 1;
            end else if(state == END_BIT && cycle_count == CYCLES_WAIT) begin
                state <= IDLE;
            end
        end

endmodule


texts:
#include<stdio.h>

int main(){

    int numberGiven,num=2,i=0,stack[100] = {0},top=-1;
    // get the number from user
    printf("Enter the number : ");
    scanf("%d",&numberGiven);

    while(numberGiven >= num){
        // check for prime-ness of number
        for(i=num/2;i>1;i--){
            if(num%i == 0){
                break;
            }
        }
        // prime factors
        while(numberGiven%num == 0 && i==1){
            stack[++top] = num;
            numberGiven /= num;
            continue;
        }
        num++;
    }

    while(top>=0){
        printf("%d\n",stack[top--]);
    }
    return 0;
}

Any possible code optimization and improvement of time complexity ?


texts:
I started using SymPy recently, and I implemented convolution using it.

def convolve(f,g,x,lower_limit,upper_limit):
    y=Symbol('y')
    h = g.subs(x,x-y)
    return integrate(f*h,(y,lower_limit,upper_limit))

It seems to work for a few tests I've done.

Would like to know what you think of it, any improvements are appreciated.


texts: Finding hidden field values in a web page before submitting a login form

texts:
In order to submit a login form using wget, I need to find the values for the hidden field that were added to the form recently.  Grepping for the names gives the following:

$ grep csrf signin.php

  <input type="hidden" name="csrf1" value="56a0ecec3f15c" />
  <input type="hidden" name="csrf2" value="4a8cb442fc85a745c9cf92edd44d71cc3ac7225e99acf57d67276ea0481f9ae8" />
  <input type="hidden" name="csrf1" value="56a0ecec3f236" />
  <input type="hidden" name="csrf2" value="292f226a5048c3d75dcfc28c8e05df49089d55160ff37e5572ecb51e208a1e52" />
  <input type="hidden" name="csrf1" value="56a0ecec3f399" />
  <input type="hidden" name="csrf2" value="a99e22c2361505931a8e26f40d26de156b928b9d218fbe4f02a98f15e2364269" />

I need to extract the two middle values for csrf1 and csrf2, and concatenate them into something that looks like the following, all on one line:

  csrf1=56a0ecec3f236&csrf2=292f226a5048c3d75dcfc28c8e05df49089d55160ff37e5572ecb51e208a1e52

What I came up with, using bash, is the following:

CSRF=`grep csrf signin.php | tail -4 | head -2 | awk -F "\"" '{print $4 "=" $6}'`
CSRF=`echo $CSRF | sed -e 's/ /\&/'`

How would you improve these two lines?


texts:
I want a code run just once (say in Dispose). WriteOnceBlock<T> from TPL Data Flow could be used here; but again if we need to check if it is done (in a 'not data flow' friendly manner), we have to call Receive and timeout and things.

I wrote this and it works, but I am not sure about when comparison takes place (==); is it possible for that to be rearranged so something goes wrong and out of time/order?

public class Once
{
    const Int32 JobDone = 11011;
    const Int32 NotDone = 119;

    Int32 _done;

    public Once() { _done = NotDone; }

    public bool IsDone() { return Interlocked.CompareExchange(ref _done, JobDone, NotDone) == JobDone; }
}

And usage:

readonly Once _stopped = new Once();
public void OnStop()
{
    if (_stopped.IsDone()) return;

    //...
}


texts: Windows function to use NamedTemporaryFile

texts:
For my package dealing with pptx-files I need to create a temporary *.pptx file, to open in PowerPoint (e.g. to export as PDF). After some research I found that tempfile.NamedTemporaryFile is broken on Windows (you could have a temporary file, or you could get a filename - but you cannot have both). So I tried to build a function, that returns a filename of a temporary file, and takes care of creating and deleting the file internally. After going in circles many times I came up with the following code monster:

import os
from typing import Generator

import tempfile
import pptx

class TempFileGenerator:
    generator = None
    @classmethod
    def get_new_generator(cls, prs):
        TempFileGenerator.generator = cls.temp_generator(prs)
        return cls.generator

    @staticmethod
    def temp_generator(prs: pptx.presentation.Presentation) -> Generator[str, None, None]:
        temp_pptx = None
        try:
            with tempfile.NamedTemporaryFile(suffix='.pptx', delete=False) as temp_pptx:
                temp_pptx.close()  # needed on windows systems to access file
                prs.save(temp_pptx.name)
                yield temp_pptx.name
        finally:
            if temp_pptx is not None:
                try:
                    os.remove(temp_pptx.name)
                except PermissionError as e:  # file still in use somewhere
                    pass
            else:
                print("How did I get here?")

def get_temporary_pptx_filename(prs: pptx.presentation.Presentation) -> str:
    """
    Generates a temporary pptx file. Returns the filename of the temporary file.
    """
    my_generator = TempFileGenerator.get_new_generator(prs)
    for filename in my_generator:
        return filename

Calling get_temporary_pptx_filename() does what I want. Using a class to save the generator ensures, that the clean up in finally does not happen as soon as I return the filename in get_temporary_pptx_filename. But looking at this code monster, I hope there is a better way to do it? If not I guess it would be easier to not use the tempfile module at all.


texts: A more optimized way of finding prime factors

texts:
I've solved Project Euler #3, but solving the same in Hacker Rank which has time constraint of < 4 seconds doesn't work. I've tried using the square root solution for speeding up but it fails.

Example:

The prime factors of 10000001 are 11 and 909091, we have sqrt(10000001) = 3162, if we check all primes from 2 to 3162, we can only have 11, after loop terminated, the result will be 11, incorrect answer.

So, when I use num/2 the running time is significantly increased.

public class Solution {

static boolean checkPrime(long num){
boolean toret = true;
long sq = (long) Math.sqrt(num);
for(long i=2; i<=sq; i++){
    if(num%i ==0)
        toret = false;
}
return toret;
}

static long gimmeAns(long num){
ArrayList<Long> al  = new ArrayList<Long>();
int q = 0;
long[] list = new long[10];
long sq = (long)Math.sqrt(num);
long sqint;
boolean flag = true;
if(num < 0){
    for(long i =3; i<=num; i+=2){
    if(num%i == 0)
        al.add(i);
        }
}else{
    //for(long i =3; i<=sq; i+=2){
    //if(num%i == 0)
        //al.add(i);
        //}
    boolean isPrimeCheck = checkPrime(num);
    if(isPrimeCheck){
        al.add(num);
    }
    for(long i =3 ; i<= num/2 ; i+=2){
        if(num%i==0){
            al.add(i);

        }
    }
}
Iterator it = al.iterator();
while(it.hasNext()){
    long curr = (long) it.next();
     flag = true;
    long squrt = (long) Math.sqrt(curr);
        for(long i=2; i<=squrt; i++){
            if(curr%i == 0)
                flag = false;
        }
    if(flag == true){

        list[q++] = curr;
    }
}

return list[q-1];

}

public static void main(String[] args) {
    long numberCases;
    Scanner in = new Scanner(System.in);
    numberCases = in.nextLong();
    long result = 0;
    for(long i=0; i< numberCases; i++){
        long num = in.nextLong();
        result = gimmeAns(num);
        System.out.println(result);
    }

}
}


texts:
For the server of which this is part, it makes a request to one of our web servers to validate whether the PHP Session ID is actually valid.  The location this._options.url is a URL that, given a PHPSESSID as a cookie, will return the user ID that is attached.

One of the issues that has crept up is the CPU utilization spiking to 100%.  While I have seen several solutions to this, one was that failure to call a callback function can cause it.  After effectively disabling the following function (I had it simply do callback(null,1); for testing purposes), the CPU utilization issue stopped.

However, I do believe a contributing cause was that the nature of performing an httpd request might not be trapping all of the errors and possibilities and have modified it to look this like:

var http = require('http');
var url = require('url');

function AuthFunc(auth_opts){
    this._options = auth_opts;
    this.checkPHPSession = function(phpsessid, callback){
        var options = {
            host: url.parse(this._options.url).host,
            port: url.parse(this._options.url).port || 80,
            path: url.parse(this._options.url).pathname,
            auth: url.parse(this._options.url).auth,
            headers: {
                'Cookie': this._options.cookie + '=' + phpsessid
            }
        };
        var data = '';
        http.get(options, function(response){
            response.on('data', function(chunk) {
                data += chunk;
            }).on('end', function(){
                try{
                    var arr = JSON.parse(data);
                }catch(e){
                    callback(e, false);
                }
                callback(null, arr.uid);
            }).on('close', function(){
                callback(true, false);
            }).on('error', function(e){
                callback(e, false);
            });
        }).on('error', function(e){
            callback(e, false);
        });
    }
}

module.exports = AuthFunc;

The code works for my purposes, but I'm not sure if it can handle fringe cases of HTTP request failure as that might be difficult to test at best.

Does this code look like it will always properly call callback in all cases, no matter what is returned from the server?  Is this the most proper way to write an HTTP request that guarantees a result callback of some kind?


texts: Creating SQL queries for a MySQL database

texts:
I am writing a PHP application that works with a MySQL database.  As I am fairly new to PHP I am looking for ways to improve my code.  Below are two functions, create_ct_query and create_ct_query2.  create_ct_query2 is the result of my refactoring create_ct_query.  I am fairly happy with it now, but am wondering how it looks to others.

<?php
// experiment with versions of create_ct_query

$lvb_id_ml=30;       // lvb id
$lvb_name_ml=100;    // competition name

$lvb_competitions_table =
  array ("table_name" => "lvb_competitions",
         "table_cols" =>
         array (array ('id', $lvb_id_ml),
                array ('name', $lvb_name_ml),
                array ('age', 'INT')),
         "primary_key" => array ('id', 'age'));

function varchar ($len) {
  return "VARCHAR(" . $len . ")";
}

function primary_key ($item) {
  return $item . " PRIMARY KEY";
}

// version currently in script
function create_ct_query ($tdesc) {
  $ret_val = "CREATE TABLE " . $tdesc['table_name'] . " ( ";

  foreach ($tdesc["table_cols"] as $row) {
    if (is_numeric ($row[1])) {
      $ret_val .= $row[0] . " " . varchar ($row[1]) . ", ";
    } else {
      $ret_val .= $row[0] . " " . $row[1] . ", ";
    }
  }

  // add primary key constraint
  $ret_val .= " CONSTRAINT pk_" . $tdesc["table_name"] .
    " PRIMARY KEY (";
  if (is_array ($tdesc['primary_key'])) {
    $ret_val .= implode (",", $tdesc['primary_key']);
  } elseif (is_string ($tdesc['primary_key'])) {
    $ret_val .= $tdesc["primary_key"];
  } else {
    die ("Primary key not of right type in tdesc for " .
         $tdesc['table_name'] . "\n");
  }
  $ret_val .= "))";

  return $ret_val;
}

function create_ct_query2 ($tdesc) {
  $ret_val = "CREATE TABLE " . $tdesc['table_name'] . " (";

  $ret_val .= cols_desc ($tdesc['table_cols']) . ", ";

  $ret_val .= primary_key_constraint ($tdesc['table_name'],
                                      $tdesc['primary_key']) . "))";

  return $ret_val;
}

// create the item description from the item
// Example:
// item_desc (array ('name', 10));
//    'name VARCHAR(10)'
// item_desc (array ('name', "INT"));
//    'name INT'
function item_desc ($item) {
  return " " . $item[0] . " " .
    (is_numeric($item[1]) ? varchar($item[1]) : $item[1]);
}

// create the column description part of table creation query
// Example:
// cols_desc (array (array ('id', 10), array ('id2', INT)));
//   'id VARCHAR(10), id2 INT'
function cols_desc ($table_cols) {
  return implode (",", array_map ('item_desc', $table_cols));
}

// create the primary key constraint part of the table creation query
// primary_key_constraint ('name', 'id');
//    CONSTRIANT pk_name PRIMARY KEY (id)
// primary_key_constraint ('name', array ('id', 'id2'));
//    CONSTRAINT pk_name PRIMARY_KEY (id, id2)
function primary_key_constraint ($table_name, $primary_key_desc)  {
  return " CONSTRAINT pk_" . $table_name . " PRIMARY KEY (" .
    (is_array($primary_key_desc) ?
     implode (",", $primary_key_desc) :
     $primary_key_desc);
}

echo create_ct_query ($lvb_competitions_table) . "\n\n";
echo create_ct_query2 ($lvb_competitions_table) . "\n\n";


texts: Fetch nearby chat users using Firebase and nested completion handlers

texts:
I have a method that calls three functions that each make a request to Firebase and pass back data in a completion handler. Once the data from 2 of the completion handlers is sent back, I call another method to pass in the data and send back a valid result. Is nesting blocks like this a sign of poor design?

func fetchNearbyUsers(for user: User, completionHandler: usersCompletionHandler?) {

    self.fetchAllUsers(completionHandler: { (users: [User]) in

        ChatProvider.sharedInstance.fetchAllChatrooms(completionHandler: { (chatrooms: [Chatroom]) in

            self.validateNewUsers(currentUser: user, users: users, chatrooms: chatrooms, completionHandler: { (validUsers: [User]) in
                guard validUsers.isEmpty == false else {
                    completionHandler?([])
                    return
                }
                completionHandler?(validUsers)
            })
        })
    })
}


texts:
Here's the issue.  When I build an asp.net application I create a singleton to read the values from the web.config file so that the values are only read once and gives a slight speed increase to the app.  I basically do the following :

/// <summary>
/// Static class that handles the web.config reading.
/// </summary>
public class SiteGlobal
{
    /// <summary>
    /// Property that pulls the connectionstring value.
    /// </summary>
    public static string ConnectionString { get; protected set;}

    /// <summary>
    /// Property that pulls the directory configuration value.
    /// </summary>
    public static string Directory { get; protected set; }

    /// <summary>
    /// Static contructor for the SiteGlobal class.
    /// </summary>
    static SiteGlobal() {
        ConnectionString = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        Directory = WebConfigurationManager.AppSettings["Directory"];

    }
}

Which I don't consider a bad optimization.  (Suggestions for improvement are appreciated through.)

Now though I'm trying to read from a ConfigurationSection that can contain multiple area's for example :

<ApplicationArea>
    <test1>
        <add key="key value" value="value data"/>
    </test1>
    <test2>
        <add key="key value1" value="value data2"/>
    </test2>
</ApplicationArea>

Where I can have multiple area's underneath ApplicationArea, such as test1 and test 2 above.  The best I've been able to come up with for an optimization of this though is this :

public static string ApplicationArea(string skey, string skey2)
    {
        NameValueCollection nvc = WebConfigurationManager.GetSection("ApplicationArea/" + skey) as NameValueCollection;
        return nvc[skey2];
    }

Which is not saving the data and rereads the web.config file every time it's accessed.  Is there an easy way to optimize the reading of the sections?


texts:
I have written this code in C# to connect to an Oracle Database, omitting the tnsnames.ora file, then place results from a query into a CSV File.

Is there more efficient way to do this? (Such as reading in the data into a DataReader rather than a DataTable)

Please review and tell me what you would change.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Data;
using FileHelpers;
using System.IO;

namespace oracle_test
{
class Program
{
    static void Main(string[] args)
    {
        OraTest ot = new OraTest();
        ot.Connect();
    }

    class OraTest
    {
        public void Connect()
        {
            var constr = new OracleConnectionStringBuilder()
            {
                DataSource = @"(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.100.100.10)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = ONE10)))",
                UserID = "100",
                Password = "100",
            }.ConnectionString;

            using (var con = new OracleConnection(constr))
            {
                string prfbalqry = "select * from ati where be = 0";

                OracleCommand cmd = new OracleCommand(prfbalqry, con);
                cmd.CommandType = System.Data.CommandType.Text;
                con.Open();

                DataTable pbResults = new DataTable();
                OracleDataAdapter oda = new OracleDataAdapter(cmd);
                oda.Fill(pbResults);

                StringBuilder sb = new StringBuilder();
                IEnumerable<string> columnNames = pbResults.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
                sb.AppendLine(string.Join(",", columnNames));

                foreach (DataRow row in pbResults.Rows)
                {
                    IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
                    sb.AppendLine(string.Join(",", fields));
                }

                File.WriteAllText(@"C:\Financial Reporting\Output Path\pbresults.csv", sb.ToString());
            }
        }
    }
}
}


texts:
I have the following code:
    export type AvatarSize = "xs" | "sm" | "md";

    const sizes = new Map<AvatarSize, 32 | 44 | 104>([
      ["xs", 32],
      ["sm", 44],
      ["md", 104]
    ]);

To me, it feels dirty. I wonder if there is any other way to write it without hard-coding the sizes?
Thanks!


texts: Am I thinking functionally in these simple Haskell functions?

texts:
I'm learning Haskell using the University of Pennsylvania's online materials. I'm a few lessons in and was looking for some feedback about whether I'm thinking functionally enough or porting over my Python background inappropriately.

Below are my answers to the problems set out in lesson three (these were someone's homework once but not any more, and were never mine!). Is my code below making any rookie mistakes for someone coming from another paradigm to functional?

import Data.List

-- Exercise 1
-- Take a list xs and an integer n, and return a list of every nth element of xs
everyNth :: [a] -> Int -> [a]
everyNth xs n = [snd x | x <- (zip [1..] xs), fst x `mod` n == 0]

skips :: [a] -> [[a]]
skips xs = map (everyNth xs) [1..length xs]

-- Exercise 2
-- Take a list of integers and produce a list of local maxima
triples :: [a] -> [[a]]
triples (x:xs)
  | length (x:xs) < 3 = []
  | otherwise = (x:take 2 xs) : triples xs

isMiddleMax :: (Ord a) => [a] -> Bool
isMiddleMax (x:y:z:[]) = x < y && y > z

localMaxima :: [Integer] -> [Integer]
localMaxima xs = map maximum . filter isMiddleMax $ triples xs

-- Exercise 3
-- Take a list of integers 0-9 and return a histogram
groupSort :: (Eq a, Ord a) => [a] -> [[a]]
groupSort xs = group $ sort xs

countInstance :: (Eq a, Ord a) => [a] -> (a, Int)
countInstance xs = (head xs, length xs)

countInstances :: (Eq a, Ord a) => [a] -> [(a, Int)]
countInstances xs = map (countInstance) $ groupSort xs

maxInstances :: [Int] -> Int
maxInstances xs = maximum $ map (snd) $ countInstances xs

spaceOrStar :: [Int] -> Int-> Char
spaceOrStar xs y
  | y `elem` xs = '*'
  | otherwise = ' '

getData :: (Eq a, Ord a) => [a] -> Int -> [a]
getData xs n = [fst x | x <- countInstances xs, snd x >= n]

histRow :: [Int] -> String
histRow xs = map (spaceOrStar xs) [0..9]

buildHist :: [Int] -> String
buildHist xs = intercalate "\n" . map (histRow) $ map (getData xs) . reverse $ [1..maxInstances xs]

histogram :: [Int] -> String
histogram xs = buildHist xs ++ "\n==========\n0123456789\n"

main :: IO()
main = do
    print $ skips "ABCD"
    print $ skips "hello!"
    print $ skips [1]
    print $ skips [True, False]
    print $ skips ([] :: [Int])
    print $ localMaxima [2,9,5,6,1]
    print $ localMaxima [2,3,4,1,5]
    print $ localMaxima [1,2,3,4,5]
    putStr $ histogram [1,4,5,4,6,6,3,4,2,4,9]


texts: CLP(FD) labeling on possibly infinite domains

texts:
As a follow-up to this SO question, I implemented, for my Prolog-based language Brachylog, my own labeling predicate brachylog_equals/2 which is not guaranteed to terminate but works for infinite domains:

brachylog_equals(Z,Z) :-
    brachylog_equals_('init',10,Z).

brachylog_equals_(I,J,'integer':Z) :-
    fd_inf(Z,'inf'),
    fd_sup(Z,'sup')
    -> (
        (
            I = 'init'
            ;
            I \= 'init',
            abs(Z) #>= I
        ),
        abs(Z) #< J,
        label([Z])
        ;
        I2 is J,
        J2 is J*10,
        brachylog_equals_(I2,J2,'integer':Z)
    )
    ;
    fd_inf(Z,'inf')
    -> (
        (
            I = 'init'
            ;
            I \= 'init',
            Z #=< -I
        ),
        Z #> -J,
        label([Z])
        ;
        I2 is J,
        J2 is J*10,
        brachylog_equals_(I2,J2,'integer':Z)
    )
    ;
    fd_sup(Z,'sup')
    -> (
        (
            I = 'init'
            ;
            I \= 'init',
            Z #>= I
        ),
        Z #< J,
        label([Z])
        ;
        I2 is J,
        J2 is J*10,
        brachylog_equals_(I2,J2,'integer':Z)
    )
    ;
    label([Z]).

A few notes about this code:

This predicate is supposed to be used in automatically generated Prolog code, therefore its name does not describe its function but rather follows a convention.
'integer':Z rather than simply Z is simply here to comply with the rest of the project (it is used to check easily the type of arguments in some other predicates)

Here are my worries:

Is this the most idiomatic solution? (i.e. impose a finite bound on the variable where it has an infinite bound, and increase it recursively if backtracking occurs?)
Is this actually correct in all cases? (From what I tested it is, but I'm wondering if I missed some odd domains where it would break)
Is this computationally efficient?

Of course comments on code style (which I know does not respect usual conventions) or code simplication are also welcome.


texts: Proper design for testing silently passed errors

texts:
I am writing a function to raise an error if a csv/tsv file does not use valid separators. However, due to existing code, currently I am handling all other exceptions to be handled silently. (There are other function in the existing code to handle the exception scenarios, and raising these exceptions here breaks a lot of tests that I don't have enough knowledge of to rewrite myself).

Initially I was using pass to not do anything about these exceptions, but while writing the unit tests for the function, I decided to chnage the function so that it would add all the silently handled exceptions to a list and return the list. I could then manually raise the exceptions in that list during tests to confirm the functionality.

Here's the function:

def _verify_events_file_uses_tab_separators(events_files):
    valid_separators = [',', '\t']
    events_files = [events_files] if not isinstance(events_files, (list, tuple)) else events_files
    errors_raised = []
    for events_file_ in events_files:
        try:
            with open(events_file_, 'r') as events_file_obj:
                events_file_sample = events_file_obj.readline()
        except TypeError as type_err:
            errors_raised.append([events_file_, type_err])  # events is Pandas dataframe.
        except UnicodeDecodeError as unicode_err:
            errors_raised.append([events_file_, unicode_err])  # py3:if binary file
        except IOError as io_err:
            errors_raised.append([events_file_, io_err])  # if invalid filepath.
        else:
            try:
                csv.Sniffer().sniff(sample=events_file_sample,
                                    delimiters=valid_separators,
                                    )
            except csv.Error:
                raise ValueError(
                    'The values in the events file are not separated by tabs; '
                    'please enforce tabs/commas conventions',
                    events_file_)
    return errors_raised

Then I can test the silent functions this way (an example):

def test_for_invalid_filepath():
    filepath = ['junk_file_path1.csv', 'junk_file_path2.csv']
    result = _verify_events_file_uses_tab_separators(events_files=filepath)
    expected_error = result[0][1]
    with assert_raises(IOError):
        raise expected_error

Since the function is not public facing, I figured this won't be a problem. It works as intended, but the whole thing seems rather inelegant to me.

Is there a better way of doing this?


texts:
I have this contract:

public interface Stack<T> {
    void push(T object);
    T pop();
    int size();
}

I am curious what you think about my test of the size() and pop() in order to prevent bugs (underlying data structure in not being cleaned) in implementation like below:

Implementation:

public class StackImpl implements Stack<Object> {
    private int size;
    private Object data[];
    @Override
    public void push(Object object) {
        data[size] = object;
        size++;
    }
    @Override
    public Object pop() {
        --size;
        return data[size];
    }
    @Override
    public int size() {
        return size;
    }
}

Test:

public class StackTest {
    @Test
    public void pushTwoObjectsToEmtyStackCheckThatSizeIsTwo() {
        pushObjectsInOrder(OBJECT_A, OBJECT_B);
        assertEquals(2, stack.size());
    }
    @Test
    public void pushTwoObjectToEmptyStackAndPopTheSameObjectsInReversedOrder() {
        pushObjectsInOrder(OBJECT_A, OBJECT_B);
        assertEquals(OBJECT_B, stack.pop());
        assertEquals(OBJECT_A, stack.pop());
    }
    @Test(expected=IllegalArgumentException.class)
    public void stackDoesntAcceptNullAndThrowExcpetion() {
        stack.push(null);
    }
    @Before
    public void setUp() throws Exception {
        stack = new StackImpl();
    }
    private void pushObjectsInOrder(Object... objects) {
        for (Object object : objects) {
            stack.push(object);
        }
    }
    private static final Object OBJECT_A = new Object();
    private static final Object OBJECT_B = new Object();
    private Stack<Object> stack;
}

I'd like to not focus too much on the implementation itself, like whether it should be List or array etc. Assume that you defined interface and going to give to implement it to other guy. Since you defined the contract you are the one who writes a unit test.


texts: Ensure that column names in output retain original variable names, instead of using arbitrary X1, X2, etc

texts:
Is there a more elegant way to ensure that colnames(output) of a output R dataframe retains the original column variable names in the referenced sample dataset data? So that I can eliminate the 2 gsub() lines of code, which rename my column names back to my original variable names. My R code, which can be run, is as follows

library(mvtnorm)
library(tidyverse)
muvec = apply(data, 2, mean) #data is passed as R dataframe
sigma = cov(data)
df = 10
n = 10
output <- as.data.frame(
        lapply(1:n, function(x) rep(muvec, each=10) + rmvt(10, sigma=sigma, df=df))) %>%
        select (sort(names(.)))
#looped function output assigns arbitrary variable names X1 and X2, instead of original variable names apple and pear
colnames(output) = gsub("X1", "apple", colnames(output))
colnames(output) = gsub("X2", "pear", colnames(output))
print(output)

data as follows

+-------+-------+
| apple | pear  |
+-------+-------+
|  0.18 |  0.05 |
|  0.22 |  0.03 |
|  0.21 |  0.03 |
|  0.11 |     0 |
|  0.15 |  0.03 |
|  0.13 |  0.01 |
|  0.03 |  0.01 |
|     0 |     0 |
| -0.06 | -0.01 |
| -0.09 | -0.02 |
+-------+-------+


texts: Big-O complexity calculation for a merge and sort function

texts:
I have the following code which basically takes a list of sorted integer arrays and merges them together removing the duplicates.

I have tried to do the complexity analysis and came to a rough conclusion that it may be O(n2) order. Could you please see if my understanding is correct here.

Also, if you could suggest ways to make the code better in terms of its running time, I would really be grateful.

If you need the implementation of PrioQueue, please let me know.

public ArrayList MergeMultiple(Object[] input)
{
    ArrayList result = new ArrayList();
    int activeArrayIndex = -1;
    int inputArrayCount = input.Count();

    // Keep track of indices of the arrays
    int[] indices = new int[inputArrayCount];

    // Keep track of whether the end has been reached for each array
    bool[] endOfArray = new bool[inputArrayCount];

    // Heap
    PrioQueue heap = new PrioQueue();

    while (endOfArray.Contains(false)) // O(N)
    {
        for (int i = 0; i < inputArrayCount; i++) // O(N)
        {
            int[] active = (int[])input[i];
            int index = indices[i];
            if (activeArrayIndex == -1 || (i == activeArrayIndex && !endOfArray[i]))
            {
                int value = active[index];

                // Add value to the heap
                heap.Enqueue(i + "|" + value, value); // O(N)??
            }
        }

        // Add to result
        if (!heap.IsEmpty())
        {
            activeArrayIndex = activeArrayIndex == -1 ? 0 : activeArrayIndex;

            string activeArrayIndexWithVal = (string)heap.Dequeue(); // O(1)

            string[] activeArrayObject = activeArrayIndexWithVal.Split('|');

            // Identifying active array
            activeArrayIndex = Convert.ToInt32(activeArrayObject[0]);
            int[] activeArray = (int[])input[activeArrayIndex];

            // Adding value to result object if not already in the array list
            if (!result.Contains(activeArrayObject[1]))  // O(N)
            {
                result.Add(activeArrayObject[1]);
            }

            // Update index list with active array
            indices[activeArrayIndex]++;

            // update end of array count list for current array
            endOfArray[activeArrayIndex] = activeArray.Length == (indices[activeArrayIndex]);
        }
    }
    return result;
}


texts:
I'm multiplying 4x4 matrices for learning purposes. How can I optimize this for performance?

HTML

-1	0	-1	0

8	-9	8	-9

5	6	2	-4

-2	7	-8	1

4	3	4	3

-1	7	-1	7

5	2	9	-3

6	2	4	1

jQuery

var first, fifth,
    second, sixth,
    third, seventh,
    forth, eighth;

    for (var i = 0; i < 4; i++) {
        for (var j = 0; j < 4; j++) {
            first = parseInt($('.matrex-1 td.0-' + i).text());
            second = parseInt($('.matrex-2 td.' + j + '-0').text());
            third = parseInt($('.matrex-1 td.1-' + i).text());
            forth = parseInt($('.matrex-2 td.' + j + '-1').text());
            fifth = parseInt($('.matrex-1 td.2-' + i).text());
            sixth = parseInt($('.matrex-2 td.' + j + '-2').text());
            seventh = parseInt($('.matrex-1 td.3-' + i).text());
            eighth = parseInt($('.matrex-2 td.' + j + '-3').text());

            $('.matrex-3 td.' + j + '-' + i).text(first * second + third * forth + fifth * sixth + seventh * eighth);
        }
    }


texts: Can this method be changed to remove all objects in one call?

texts:
I am converting a VB.NET app over to C#. I won't subject you to the old code but the following is done in a little over 100 lines using nested loops. I am using Entity Framework 6 as my ORM. I have cleaned that up some to this:

public void Remove(int key)
{
    var myObject = _context.MyObjects.FirstOrDefault(x => x.QueryKey == key);
    var myObjectInputs = _context.MyObjectInputs.Where(x => x.QueryKey == key);
    var myObjectInputProperties = _context.MyObjectInputProperties.Where(x => myObjectInputs.Select(y => y.WQI_Key).Contains(x.WQI_Key));
    var myObjectOutputs = _context.MyObjectOutPuts.Where(x => x.QueryKey == key);
    var myObjectOutputProperties = _context.MyObjectOutputProperties.Where(x => myObjectOutputs.Select(y => y.WQO_Key).Contains(x.WQO_Key));
    var myObjectGroups = _context.MyObjectGroups.Where(x => x.QueryKey == key);

    _context.MyObjects.Remove(myObject);
    _context.MyObjectInputs.RemoveRange(myObjectInputs);
    _context.MyObjectInputProperties.RemoveRange(myObjectInputProperties);
    _context.MyObjectOutPuts.RemoveRange(myObjectOutputs);
    _context.MyObjectOutputProperties.RemoveRange(myObjectOutputProperties);
    _context.MyObjectGroups.RemoveRange(myObjectGroups);
    _context.SaveChanges();
}

I still don't like it. The method is huge and doesn't follow the single responsibility principle. I considered refactoring
to this:

public void Remove(int key)
{
    var myObject = _context.MyObjects.FirstOrDefault(x => x.QueryKey == key);
    _context.MyObjects.Remove(myObject);
    RemoveChildren(key);
    _context.SaveChanges();
}

private void RemoveChildren(int key)
{
    RemoveInputs(key);
    RemoveOutputs(key);
    RemoveGroups(key);
}

private void RemoveGroups(int key)
{
    var myObjectGroups = _context.MyObjectGroups.Where(x => x.QueryKey == key);
    _context.MyObjectGroups.RemoveRange(myObjectGroups);
}

private void RemoveOutputs(int key)
{
    var myObjectOutputs = _context.MyObjectOutPuts.Where(x => x.QueryKey == key);
    var myObjectOutputProperties =
        _context.MyObjectOutputProperties.Where(x => myObjectOutputs.Select(y => y.WQO_Key).Contains(x.WQO_Key));
    _context.MyObjectOutPuts.RemoveRange(myObjectOutputs);
    _context.MyObjectOutputProperties.RemoveRange(myObjectOutputProperties);
}

private void RemoveInputs(int key)
{
    var myObjectInputs = _context.MyObjectInputs.Where(x => x.QueryKey == key);
    var myObjectInputProperties =
        _context.MyObjectInputProperties.Where(x => myObjectInputs.Select(y => y.WQI_Key).Contains(x.WQI_Key));
    _context.MyObjectInputs.RemoveRange(myObjectInputs);
    _context.MyObjectInputProperties.RemoveRange(myObjectInputProperties);
}

This seems like an okay option, but I am wondering if there is a way to remove all of these items in one fell swoop.

Obviously in a perfect world I would associate these objects in the database and have them cascade delete; however, I cannot edit our database structure at this time.


texts:
I'm new to programming and I'm trying to teach myself Python through some online books.

One of the exercises has me making a Rock Paper Scissors game. The one I have written works well enough I think, but I was wondering if there were more efficient ways of writing the code, perhaps something shorter.

I'm using Python 3.4.3 IDLE GUI.

    import random

#variables
done = False
comp = random.randrange(1,4)

#start
print("Lets play rock, paper, scissors! Choose numbers 1-3 only please.")
print()

#begin loop

while not(done):
#computer vs input
    guess = int(input("1. Rock 2. Paper 3. Scissors "))
    if comp == 1 and guess == 3:
        print("Rock beats scissors, too bad!")
    elif comp == 2 and guess == 1:
        print("Paper beats rock, tough luck!")
    elif comp == 3 and guess == 2:
        print("Scissors me timbers! Scissors cuts paper! You lose.")
    elif guess == 1 and comp == 3:
        print("Rock beats scissors, you beat me!")
    elif guess == 2 and comp == 1:
        print("You win! Paper beats rock.")
    elif guess == 3 and comp == 2:
        print("Scissor cuts paper, you win!")
    else:
        print("It's a draw!")

#continue
    stop = input("Do you want to quit? ")
    if stop.lower() == "y" or stop.lower() == "yes":
        done = True


texts: Python - Faster random business date generation

texts:
I wrote a program that generates some synthetic data. Everything runs fine, but right now the program is so slow that I am only generating 1000 rows of data (and it still takes 3 or so minutes). Ideally I would like to generate about 100,000 (which at the moment takes upwards of 10 minutes, I killed the program before it finished running).

I've narrowed down the problem to the way I am generating random dates. After those three lines, the rest of the program executes in a few seconds.

import numpy.random as rnd
import datetime
import pandas as pd

random_days = []

for num in range(0,n):
    random_days.append(pd.to_datetime(rnd.choice(pd.bdate_range(start_date, end_date))))

What I need is, given some number n, to generate that many dates at random from a sequence of business days (the business days part is also important). I need to convert the value to datetime because otherwise it returns a numpy timedelta64 object, which causes problems in other parts of the program.

Is there any way to improve my code to have it generate dates faster? Or do I need to settle for a small sample size?

EDIT:

Just to add a little more context:

I use this loop to generate the rest of my data:

for day in random_days:
    new_trans = one_trans(day)
    frame.append(new_trans)

frame = pd.concat(frame)

The function one_trans is the following:

def one_trans(date):

    trans = pd.Series([date.year, date.month, date.date(), fake.company(),
                       fake.company(), fake.ssn(),
                       (rnd.normal(5000000,10000)),
                       random.sample(["USD", "EUR", "JPY", "BTC"], 1)[0]],
                       index=["YEAR", "MONTH","DATE","SENDER","RECEIVER",
                         "TRANSACID","AMOUNT","CURRENCY"])

    return trans.to_frame().transpose()

EDIT 2:
This is how I implemented Vogel612's suggestion:

def rng_dates(n, start_date, end_date):
    for _ in range(n):
        yield pd.to_datetime(rnd.choice(pd.bdate_range(start_date, end_date)))

random_days = rng_dates(n, start_date, end_date)

for day in random_days:
    new_trans = one_trans(day)
    frame.append(new_trans)

frame = pd.concat(frame)


texts: Where will this .offsetTop() function fail?

texts:
The Javascript libraries have pretty complicated functions to calculate an element's offset on the page. I wrote the following function that works in all the places I need it, and I've tested a few edge cases as well. It adds the element's offsetTop to its parent's offsetTop all the way up the DOM to the document. Can someone give me an example of where this wouldn't work?

This demo shows its results compared to jQuery's offset().top function.

Demo: http://jsfiddle.net/ThinkingStiff/5aGqM/

window.Object.defineProperty( Element.prototype, 'documentOffsetTop', {
    get: function () {
        return this.offsetTop + ( this.offsetParent ? this.offsetParent.documentOffsetTop : 0 );
    }
} );

UPDATE:
I was looking for places where traversing up the DOM and adding the offset doesn't work in current browsers. As @Akkuma pointed out, Object.defineProperty doesn't work in earlier IEs.


texts:
My Implementation:

string NumberToEnglish(long n)
{
    StringBuilder builder = new StringBuilder();
    var trillion = 1000000000000;
    var billion = 1000000000;
    var million = 1000000;
    var thousand = 1000;
    var hundred = 100;
    while(n != 0){
        if(n >= trillion){
            builder.AppendFormat("{0} Trillion ",NumberToEnglish(n / trillion));
            n -= (n / trillion) * trillion;
        }
        else if(n >= billion){
            builder.AppendFormat("{0} Billion ",NumberToEnglish(n / billion));
            n -= (n / billion) * billion;
        }
        else if(n >= million){
            builder.AppendFormat("{0} Million ",NumberToEnglish(n / million));
            n -= (n / million) * million;
        }
        else if(n >= thousand){
            builder.AppendFormat("{0} Thousand ",NumberToEnglish(n / thousand));
            n -= (n / thousand) * thousand;
        }
        else if(n >= hundred){
            builder.AppendFormat("{0} Hundred ",NumberToEnglish(n / hundred));
            n -= (n / hundred) * hundred;
        }else if(n >= 20){
            builder.AppendFormat(numerals[(n / 10) * 10] + " ");
            n -= (n / 10) * 10;
        }else{
            builder.Append(numerals[n]);
            n -= n;
        }
    }
    return builder.ToString();
}

Dictionary<long,string> numerals = new Dictionary<long,string>(){
    {   1,          "One"   },
    {   2,          "Two"   },
    {   3,          "Three" },
    {   4,          "Four"},
    {   5,          "Five"},
    {   6,          "Six"}  ,
    {   7,          "Seven"},
    {   8,          "Eight"},
    {   9,          "Nine"},
    {   10,         "Ten"   },
    {   11,         "Eleven"},
    {   12,         "Twelve"},
    {   13,         "Thirteen"},
    {   14,         "Fourteen"},
    {   15,         "Fifteen"   },
    {   16,         "Sixteen"   },
    {   17,         "Seventeen"},
    {   18,         "Eighteen"},
    {   19,         "Nineteen"},
    {   20,         "Twenty"    },
    {   30,         "Thirty"    },
    {   40,         "Forty"}    ,
    {   50,         "Fifty" },
    {   60,         "Sixty" },
    {   70,         "Seventy"},
    {   80,         "Eighty"}   ,
    {   90,         "Ninety"}   ,
    {   100,        "Hundred"}  ,
    {   1000,       "Thousand"},
    {   1000000,    "Million"   },
    {   1000000000, "Billion" },
    {   1000000000000, "Trillion" }
};

Test Method:

void Main()
{
    Console.WriteLine(NumberToEnglish(2120)); // Two Thousand One Hundred Twenty
}

How can I improve this?


texts: Check if user selects correct Spanish word article

texts:
All Spanish nouns have a gender (masculine [el] or feminine [la]; e.g. la casa, el perro). I'm using some javascript and jquery to display a random word from a list of nouns (2D array), as well as buttons corresponding to the two Spanish articles ("el" or "la"). If the user clicks on the correct article for the word, a new one loads; else, the user sees an alert and is prompted to choose the correct article before loading a new word. I made this work on codepen, but I wasn't able to get it to work correctly without reloading the page. All feedback will be well received.

$(document).ready(function() {
  setWord();

  function setWord() {

    //returns either 0 or 1
    var elOrLa = Math.round(Math.random());

    //wordList[0] contains 'la' nouns; wordList[1] contains those 'el' nouns
    var wordList = [["casa", "comida", "chica"],["perro", "amigo", "muchacho"]];

    // Return random int between min (included) and max (excluded)
    function getRandomInt(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min)) + min;
    }

    //maximum length of each subarray
    var laLength = wordList[0].length;
    var elLength = wordList[1].length;

    //random index for subarrays
    var randomLaIndex = getRandomInt(0, laLength);
    var randomElIndex = getRandomInt(0, elLength);

    var article1 = "la ";
    var article2 = "el ";
    var word;
    var articlePlusWord;

    //sets random word and articlePlusWord
    if (elOrLa === 0) {
      word = wordList[0][randomLaIndex];
      articlePlusWord = article1.concat(word);
    } else {
      word = wordList[1][randomElIndex];
      articlePlusWord = article2.concat(word);
    }

    $("#test-word").text(word);

    if (elOrLa === 0) {
      $("#la-btn").on("click", function() {
        location.reload(true);
      });
      $("#el-btn").on("click", function() {
        alert("TOMA NOTA: ".concat(articlePlusWord));
        $("#el-btn").fadeOut();
        $("#la-btn").css({"background":"limegreen"});
        $("#test-word").addClass('bg-danger').removeClass('bg-warning');
      });
    } else {
      $("#el-btn").on("click", function() {
        location.reload(true);
      });
      $("#la-btn").on("click", function() {
        alert("TOMA NOTA: ".concat(articlePlusWord));
        $("#la-btn").fadeOut();
        $("#el-btn").css({"background":"limegreen"});
        $("#test-word").addClass('bg-danger').removeClass('bg-warning');
      });
    }
  };
});

  <button type="button" class="btn btn-primary btn-lg" id="el-btn">EL</button>
  <button type="button" class="btn btn-primary pull-right btn-lg" id="la-btn">LA</button>


texts: Performing an ID check on a person and saving the result

texts:
I'm just trying to understand and use unit tests, which from what I can tell is exposing the issues with my code.  A lot of what I do is MVC/WebAPI.  One of the big issues I come across is that my methods end up too long, doing too much, which makes it very difficult to test them.

Description of what the code is for:

The code is taking a request to ID check a person, firstly I try to do this locally, reusing internal data (cheaper!). But if we don't have the information, we have to use an external web service.  I then return the result of whether the person is verified.

I create a log entry in a database, save that.  Then do a check, which is set against that previous entry.  Then return the result to the calling WebAPIController.

What I want:

Here's is the current block I'm looking at, how could I improve it, what should I do differently?  I am a pragmatic man, but I know this can be a lot cleaner, however I'm unsure where to break it up.  Typically using a repository style.

I want to make it more testable and cleaner.  I hear about keeping to a few lines per method.  Also making sure it only does one thing.  But then I think about my code below and they are a series of conditions which are evaluated.  So I could break them up, but it wouldn't be for re-use at this point, they would only be called once.  Which at the moment doesn't make sense for me to do, but it leaves me with long methods.

public interface IInvestigationManager
{
    Task<idResponseVM> LogAndDoCheck(idRequestVM requestVM);
}

public class InvestigationManager : IInvestigationManager
{
    private readonly IIdentityAuthenticationRepository _identityAuthenticationRepository;

    public async Task<idResponseVM> LogAndDoCheck(idRequestVM requestVM)
    {
        // Initialise the identity check, log the request and get Id
        var newIdCheck = CreateIdentityCheckDM_FromRequestVM(requestVM);

        //Log the Request in the database; Marking it pending result
        _identityAuthenticationRepository.Add(newIdCheck);

        IdentityCheck latestLocalCheck = null;
        idResponseVM responseVM = new idResponseVM();

        try //database interactions.
        {
            await _identityAuthenticationRepository.SaveChangesAsync();

            // Check for existing local authentication
            latestLocalCheck = _identityAuthenticationRepository.LatestPreviousCheckWithinThreshold(newIdCheck);
        }
        catch (SqlException ex)
        {
            latestLocalCheck = null;
            responseVM.Errors.Add("SQL Server error", ex);
            // Divert to try external check
        }
        catch (Exception ex)
        {
            throw ex;
        }

        if (latestLocalCheck != null)
        {
            // Local check
            newIdCheck.IdentityServiceUsed = IdentityServiceUsed.Local;
            newIdCheck.ResultMatchIsValid = latestLocalCheck.ResultMatchIsValid;

            responseVM.ResultIsValidMatch = latestLocalCheck.ResultMatchIsValid;
            responseVM.Results = null;
        }
        else
        {
            try
            {
                // External Id Check
                var idResult = GetExternalCheckResponse(requestVM);
                var isMatch = idResult.matchlevel == enReportType.IndividualReport;

                newIdCheck.IdentityServiceUsed = IdentityServiceUsed.External;
                newIdCheck.ResultMatchIsValid = isMatch;

                newIdCheck.ResultMatchType = idResult.matchlevel.ToString();
                newIdCheck.ExternalSearchId = idResult.searchid;

                responseVM.ResultIsValidMatch = isMatch;
                responseVM.Results = idResult;

            }
            catch (WebException ex)
            {
                // Do other stuff as a backup plan
            }
            catch (Exception ex)
            {
                // Log
                throw ex;
            }
        }

        await _identityAuthenticationRepository.SaveChangesAsync();

        return responseVM;
    }
}


texts: Calculating sum of smallest prime factors for 2 < n < 10**12

texts:
I am trying to calculate the sum of the smallest prime factors of n, where 2 < n < 10**12, and get the remainder of this sum when divided by 10**9:

require 'prime'
puts "Started at #{Time.now}."
num = 10**12
sum = 0
(2..num).each { |n| sum += n.prime_division[0][0] }
sum = sum % 10**9
p sum
puts "Finished at #{Time.now}."

Understanding that the smallest prime factor of any even number is 2, and the smallest prime factor of any prime number is itself, I made the following function:

def findSmallestPrimeFactor(number)
  return 2 if number.even?
  return number if Prime.prime? number
  return number.prime_division[0][0]
end

However I feel this would have no effect on the program's run time as #prime_division works well enough on any number.

Is there any way to shorten the runtime of this program? I have been told that efficient implementation will allow the program to finish in under 1 minute, however I can't see how I can do this without iterating over every number between 2 and one trillion (10**12), which will take several days regardless.


texts: Big switch statement for sorting a table

texts:
I have a webpage with a table.  The table, with 10 columns, can be sorted by each column ascending or descending.

At this moment I control order by big switch:

Func<IQueryable<DeviceUsage>, IOrderedQueryable<DeviceUsage>> orderBy;
        IOrderedEnumerable<DeviceUsage> info;
        switch (sortOrder)
        {
            case "user":
                ViewBag.sortableBy = sortOrder;
                info = unitOfWork.deviceUsageRepository.Get(where, null, null, d => d.DeviceInstance, d => d.Storage, d => d.User).OrderBy(s => s.User.FullName);
                ViewBag.Desc = true;
                return PartialView(info.ToPagedList(pageNumber, 15));
            case "userDesc":
                ViewBag.sortableBy = sortOrder;
                info = unitOfWork.deviceUsageRepository.Get(where, null, null, d => d.DeviceInstance, d => d.Storage, d => d.User).OrderByDescending(s => s.User.FullName);
                ViewBag.Desc = true;
                return PartialView(info.ToPagedList(pageNumber, 15));
            case "manufacturer":
                ViewBag.sortableBy = sortOrder;
                orderBy = q => q.OrderBy(o => o.DeviceInstance.Device.Manufacturer1.Name);
                break;
            case "manufacturerDesc":
                ViewBag.sortableBy = sortOrder;
                orderBy = q => q.OrderByDescending(o => o.DeviceInstance.Device.Manufacturer1.Name);
                break;
            .
            .
            .
            default:
                ViewBag.sortableBy="";
                orderBy=q => q.OrderBy(o => o.DeviceInstance.Device.CatalogNo);
                break;
        }

This code works but looks not nice.  I think it's the longest switch I ever wrote.
Can it be someway upgraded or leave it as it is?

Explanation why there is difference in logic:

As you can see in user I'm sorting by Fullname which isn't in db ( its created locally in DAL from FirstName & LastName).  @Nick's approach is ok BUT adding a new Desc variable is a bit problematic because there is only the possibility to sort by ONE row at a time. So I would need to store it somewhere in the last sorting order.


texts: Project Euler Problem 21 in Python: Summing amicable numbers

texts:
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
  If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.

  For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

  Evaluate the sum of all the amicable numbers under 10000.

My solution to the 21 problem on project Euler is very slow (with pure brute force it took 30 mins to find the solution). Any ways that I can improve my code or should I scrap it and think about something else ?

Here's my code :

import time, math

start = time.time()

amicable_nums = set()

def sum_div(n):
    divisors = []
    for x in range(1, int(math.sqrt(n) + 1)):
        if n % x == 0:
            divisors.append(x)

            if x * x != n and x != 1:
                divisors.append(int(n / x))
    return sum(divisors)

for i in range(1, 10000):
    for j in range(1, 10000):
        if sum_div(i) == j and sum_div(j) == i and i != j:
            amicable_nums.update([i, j])

print(sum(amicable_nums))
print("It took " + str(time.time() - start) + " seconds")


texts: Discover random web servers and print the index page title

texts:
Github repo

I wrote this little toy today that probes random web servers and prints out the title of their index page. I'm curious if this code is any good since I'm a bit rusty.

get_titles.py

#!/usr/bin/env python

""" Probe random web server and print out the title of their index page. """

import os
import sys
import lxml.html

def get_title(url):
    '''Extract the title from a webpage.
    If anything goes wrong like the request
    times out or the page does not have a
    title get_title will return None'''
    try:
        title = lxml.html.parse(url)
        return title.find(".//title").text
    except KeyboardInterrupt:
        sys.exit(0)
    except Exception:
        return None

def shell(command):
    """Run a shell command.
    return it's output as a string"""
    return os.popen(command).read()

def scan(nhosts):
    """Run an nmap scan of n hosts.
    This will return IPv4 addresses of hosts
    that are up and have a service running on
    port 80. Simply supply the number of hosts
    you would like to check."""
    results = set(shell('./discover.sh {}'.format(nhosts)).split('\n'))
    if not results:
        print 'Nothing Found.'
        sys.exit(0)
    return ["http://{}".format(ip) for ip in results if ip]

def main():
    """ Run the scan."""
    if not len(sys.argv) >= 2:
        print 'You forgot to tell me how many hosts to check'
        sys.exit(0)
    for url in scan(sys.argv[1]):
        title = get_title(url)
        if title:
            print title, url

if __name__ == '__main__':
    main()

discover.sh

#!/bin/bash

nhosts=$1

nmap \
--host-timeout 10s \
--max-retries 0 \
–min-parallelism 100 \
-T5 \
-Pn \
-n \
-iR $1 \
-p80 \
-oG tmp.txt \
--open \
tmp.txt > /dev/null

cat tmp.txt | grep 'Host' | awk '{print $2}'
rm tmp.txt


texts: List splitting for RLE compression algorithm

texts:
I try to implement a simple RLE compression algorithm as a learning task. In the first step, i need to split my source list to sequences of repeating and non-repeating elements. I've done it, but code seems to be so ugly. Can I simplify it?

src = [1,1,2,3,3,4,2,1,3,3,4,3,3,3,3,3,4,5]

from itertools import izip

isRepeat = False
sequence = []

def out(sequence):
    if sequence:
        print sequence

def testRepeate(a, b, pr):
    global isRepeat
    global sequence
    if a == b:
        if isRepeat:
            sequence.append(pr)
        else:
            out(sequence)
            sequence = []
            sequence.append(pr)
            isRepeat = True
    else:
        if isRepeat:
            sequence.append(pr)
            out(sequence)
            sequence = []
            isRepeat = False
        else:
            sequence.append(pr)

for a, b in izip(src[:-1], src[1:]):
    testRepeate(a, b, a)
testRepeate(src[-2], src[-1], src[-1])
out(sequence)


texts: A class to store the number of seconds since midnight

texts:
The purpose of this code is to to implement the time as the number of seconds since midnight. The class  should have one data field (an int  with the number of seconds since midnight) instead of three. The Driver class will test the Time2 class . This program should ask the user to input the number of hours, minutes, and seconds past midnight, creating a Time2 object  and using the mutator methods . The program  should then use the toString() method  to print out the time.

I am unsure if I could put throw new IllegalArgumentException into a single statement if I need 3. But I have the code working.

import java.util.Scanner;

public class Driver {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    Time2 time = new Time2();

    System.out.print("Enter hours:");
    int hours = input.nextInt();
    System.out.print("Enter minutes:");
    int minutes = input.nextInt();
    System.out.print("Enter seconds:");
    int seconds = input.nextInt();

    time.setHour(hours);
    time.setMinute(minutes);
    time.setSecond(seconds);
    time.toString();
}

public static class  Time2 {

private  int  hour;
private  int  minute;
private  int  second;

public Time2() {this(0, 0, 0);}
public Time2(int  hour) {this(hour, 0, 0);}
public Time2(int  hour, int  minute) {this(hour, minute, 0);}

public Time2(int  hour, int  minute, int  second) {
    if(hour<0||hour>=24)
        throw new IllegalArgumentException("hour must be 0-23");
    if (minute < 0 || minute >= 60)
        throw new IllegalArgumentException("minute must be 0-59");
    if (second < 0 || second >= 60)
        throw new IllegalArgumentException("second must be 0-59");
    this.hour = hour;
    this.minute = minute;
    this.second = second;
}

public Time2(Time2 time) {this(time.getHour(), time.getMinute(), time.getSecond());}

public void setTime(int  hour, int  minute, int  second) {
    if (hour<0||hour>=24)
        throw new IllegalArgumentException("hour must be 0-23");
    if (minute < 0 || minute >= 60)
        throw new IllegalArgumentException("minute must be 0-59");
    if (second < 0 || second >= 60)
        throw new IllegalArgumentException("second must be 0-59");
    this.hour = hour;
    this.minute = minute;
    this.second = second;
}

public void setHour(int  hour) {
    if (hour < 0 || hour >= 24)
        throw new IllegalArgumentException("hour must be 0-23");
    this.hour = hour;
}

public void setMinute(int  minute) {
    if (minute < 0 && minute >= 60)
        throw new IllegalArgumentException("minute must be 0-59");
    this.minute = minute;
}

public void setSecond(int  second) {
    if (second <= 0 || second > 60)
        throw new IllegalArgumentException("second must be 0-59");
    this.second = second;
}

public int  getHour() {return hour;}
public int  getMinute() {return minute;}
public int  getSecond() {return second;}

public String  toUniversalString() {
    return String .format( "%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}

public String  toString() {
    return String .format("%d:%02d:%02d %s",
                          ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12), getMinute(),
                          getSecond(), (getHour() < 12 ? "AM" : "PM"));
}
}
}


texts: Python text based game room to room movement

texts:
I'm new to coding and working on a text based game moving from room to room. The code works in pycharm but according to an instant feedback program I entered it into, it gave some tips on it and I am not exactly sure how how to improve upon it. Here is what it gave me:

The Great Hall string is a key in main dictionary. Revise code so it is not a key in main dictionary.
It said it can not classify my code (not sure what that means) do I need a def main() command?
Consolidate multiple print commands into one function
Make condition simpler by using non-complex conditions. With simple operators that don't look for a contain within the string
5.Better practice to use while True: and the break reserved word to stop the loop when you require it to stop

data setup
rooms = {'Great Hall': {'name': 'Great Hall', 'South': 'Bedroom', 'North': 'Dungeon', 'West': 'Library', 'East': 'Kitchen'},
     'Bedroom': {'name': 'Bedroom', 'East': 'Cellar', 'North': 'Great Hall'},
     'Cellar': {'name': 'Cellar', 'West': 'Bedroom'},
     'Library': {'name': 'Library', 'East': 'Great Hall'},
     'Kitchen': {'name': 'Kitchen', 'West': 'Great Hall', 'North': 'Dining Room'},
     'Dining Room': {'name': 'Dining Room', 'South': 'Kitchen'},
     'Dungeon': {'name': 'Dungeon', 'South': 'Great Hall', 'East': 'Gallery'},
     'Gallery': {'name': 'Gallery', 'West': 'Dungeon'},
     }
directions = ['North', 'South', 'East', 'West']
current_room = rooms['Great Hall']

# game loop
while True:
   # display current location
   print()
   print('You are in the {}.'.format(current_room['name']))

  # get user input
  command = input('\nWhat direction do you want to go? ').strip()
  # movement
  if command in directions:
     if command in current_room:
         current_room = rooms[current_room[command]]
     else:
        # bad movement
        print("You can't go that way.")
  # Exit game
  elif command.lower() in ('q', 'quit'):
      break
  # bad command
  else:
     print("I don't understand that command.")


texts: Rethrowing Error through several classes

texts:
This is a simplified version of my app. It has a bunch of scraper classes, and a Job class, which controls the collection of data, and the saving to the db. During scraping it could happen many unforeseen problem due to irregular html structure, date format, missing information etc.
I want to have my app to run without stopping, and if there is an unprocessable item, it should log it, and somehow flag it in the database, that there was a problem with the site. After the irregularity is handled in the code, the next time we know that the page has to be scanned again, but this time it will be work fine, and the flag is switched to scanned.

In this example there is some problem with item nr. 4. It is logged (in my app to a file) and an error is thrown. Should I rethrow it in every function call till it reaches the Job class' flush method?

There was a second idea to handle this problem. Instead of throwing errors around the result of Scraper class' scrape method should contain if something went wrong. [1, 2, 3, false, 5, 6 ...] and during the flush handle the falsy item. The problem: this items are obviously not just simple numbers, but complex objects with a lot of links (to media, sub pages etc.) and those links are also scraped with different scrape classes, so I should handle in every for loop and scraper the falsy entries. Maybe there is a better solution.

jsbin: https://jsbin.com/jojiwuyugi/1/edit?js,console

class Scraper {
  check(item) {
    if (item === 4) {
      throw 42
      console.log('There is a problem with ' + item)
      return false
    } else {
      return true
    }
  }

  scrape() {
    const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    const res = []
    for (const item of arr) {
      try {
        if (this.check(item))
          res.push(item)
      } catch(e) {
      }

    }
    return res
  }
}

class SiteScraper {
  scrape() {
    return {
      site: 'Foo'
    }
  }
}

class Job {
  flush(data) {
    try {
      console.log(data.site)
      for (const item of data.items)
        console.log(item)
    } catch(e) {
      console.log('Error while flushing ' + e)
    }
    // here I should switch the flag if everything is fine / wrong
  }

  collectData() {
    const siteScraper = new SiteScraper()
    const scraper = new Scraper()
    const site = siteScraper.scrape()
    site.items = scraper.scrape()
    return site
  }

  run() {
    const data = this.collectData()
    this.flush(data)
  }

}

const j = new Job()
j.run()


texts:
Problem Definition :
When John was a little kid he didn't have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could... he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.
Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it. The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn't care about whether letters are uppercase or lowercase, so that doesn't affect the beauty of a letter. (Uppercase 'F' is exactly as beautiful as lowercase 'f', for example.)
You're a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?
Your program should accept as its first argument a path to a filename. Each line in this file has a sentence.
Print out the maximum beauty for the string.

Background :
I'm still very new to Haskell, trying to wrap my head around things like monads and such, so some of the constructs might seem overly simplistic. I would like any sort of criticism from efficiency to idiomatic usage of Haskell. I harbor suspicions that my countSort function is inefficient as I would solve this using a dictionary in an imperative language but that sort of mutable solution seemed out of place in my understanding of Haskell and I couldn't decide if lazy evaluation would make up for it. Either way the speed was actually a bit faster than what I've implemented in imperative languages.
I thought about generalizing the beauty function by moving the toUpper/IsAlpha out of the method to change it's signature to something like [a] -> Int, but it felt outside of the point of the problem, and I honestly couldn't think of a use for this function outside of this problem.
Code :
import Data.List
import Data.Char
import Data.Ord
import System.Environment

countSort :: Ord a => [a] -> [[a]]
countSort x =  sortBy (flip $ comparing length) (group $ sort x)

maximumStringBeauty :: String -> Int
maximumStringBeauty x = maximumBeautyInner (countSort . map toUpper $ filter isAlpha x) 26
    where maximumBeautyInner [] _ = 0
          maximumBeautyInner (x : xs) maxVal = (length x) * maxVal + maximumBeautyInner xs (maxVal - 1)

main :: IO ()
main = do
    args <- getArgs
    let path = args !! 0
    file <- readFile path
    putStrLn . unlines . map (show . maximumStringBeauty) . lines $ file


texts: Generating combinations from two joined vectors

texts:
I have the following code to generate all combinations from two joined vectors.

I have some restrictions:

Exclude combinations if all members have the same "d" value
Exclude combinations if several members have the same "s" value
If there is only one member in the combination, I don't want all versions with "d" value, just the unique "s" value

#include <algorithm>
#include <cassert>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <vector>

using namespace std;

vector<vector<string>> all_comb(const vector<string> &v, const size_t count)
{
    assert(count <= v.size());
    vector<bool> bitset(v.size() - count, 0);
    bitset.resize(v.size(), 1);

    vector<vector<string>> result;

    do {
        vector<string> tmp;
        for (size_t i = 0; i != v.size(); ++i) {
            if (bitset[i])
                tmp.push_back(v[i]);
        }
        result.push_back(tmp);
    } while (next_permutation(bitset.begin(), bitset.end()));

    return result;
}

void get_all_stat(vector<vector<string>> &stat, const vector<string> &s, const vector<double> &d)
{
    // add all comb without d
    for (size_t i = 1; i <= s.size(); i++)
    {
        vector<vector<string>> ret;
        ret = all_comb(s, i);
        stat.insert(stat.end(), ret.begin(), ret.end());
    }

    // generate list mixed s and d
    vector<string> tmp;
    for (const auto &i : s)
    {
        tmp.push_back(i);
        for (const auto &j : d)
        {
            ostringstream oss;
            oss << noshowpoint << j;
            string s = oss.str();
            tmp.push_back(i + ":" + s);
        }
    }

    // calculate all comb
    for (size_t i = 1; i <= tmp.size(); i++)
    {
        vector<vector<string>> ret;
        ret = all_comb(tmp, i);

        for (const auto &v : ret)
        {
            unordered_map<string, unsigned> seen_s;
            unordered_map<double, bool> seen_d;

            for (const auto &s : v)
            {
                string left { s };
                double right { 0 };

                string::size_type pos { s.find(":") };

                // on a trouvé un :
                if (pos != string::npos)
                {
                    left = s.substr(0, pos);
                    right = stod(s.substr(pos + 1));
                }

                seen_s[left] += 1;
                seen_d[right] = true;
            }

            // we dont want combination with all same "d"
            if (seen_d.size() <= 1)
                continue;

            // we dont want combination with more than one "s"
            bool add { true };
            for (const auto &m : seen_s)
            {
                if (m.second > 1)
                {
                    add = false;
                    break;
                }
            }

            if (add)
                stat.push_back(v);
        }
    }
}

int main()
{
    vector<string> s { "a", "b" };
    vector<double> d { 2.5, 3.7 };

    vector<vector<string>> stat;
    get_all_stat(stat, s, d);

    for (const auto &i : stat)
    {
        for (const auto &j : i)
            cout << " " << j;
        cout << endl;
    }
}

Wanted output (order is not important):

b
a
a b
a:3.7 b:2.5
a:3.7 b
a:2.5 b:3.7
a:2.5 b
a b:3.7
a b:2.5

Live example

Actual performance:

With small s and d vectors it works well but if I had more values to the vectors it eat lots of memory.

With a s vector of 6 elements and d vector of 4 elements my system kill the process after eating more than 10Go of memory.

How could I optimize this?


texts:
I have a simple spring controller which takes a @ModelAtrribute object and registers an employee. If it is a success, I forward it to a success, else an error page, and I am doing this via a boolean return value from @Service and Dao.

@Controller:

@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView registerEmployee(@ModelAttribute LoginModel loginModel){
    LOGGER.info(" REGISTER Controller [{}] ",loginModel.toString());
    ModelAndView model = new ModelAndView();
    if(!registerService.registerEmployeeByAdmin(loginModel)){
      model.setViewName("register");
      model.addObject("errorMessage", "Unable to register user, Please try again");
      return model;
    }
    model.addObject("employee", loginModel);
    return model;
}

@Service:

public boolean registerEmployeeByAdmin(LoginModel loginModel){
    LOGGER.info("Saving Employee login info ",loginModel.toString());
    boolean registered = registerDao.registerEmployeeByAdmin(loginModel);
    if(registered)
        registerDao.registerEmployeeInfo(loginModel);

    return registered;   //the value returned
}

Dao:

public boolean registerEmployeeByAdmin(LoginModel loginModel) {
    boolean registered = false;
    String addEmployeeQuery = "insert into employee_login (loginid,password_hash,user_created_date,"
            + "email) values (?,?,?,?);";
    try (Connection conn = Database.getConnection(env);
            PreparedStatement pstmt = conn.prepareStatement(addEmployeeQuery);) {

        if (conn != null) {
            pstmt.setInt(1, loginModel.getUserid());
            pstmt.setString(2, EmployeeHelper.getHashFromPassword(loginModel.getPassword_hash()));
            pstmt.setTimestamp(3, new java.sql.Timestamp(System.currentTimeMillis()));
            pstmt.setString(4, loginModel.getEmail());
            int rows = pstmt.executeUpdate();
            if (rows > 0)
                registered = true;
        }
    } catch (SQLException | NoSuchAlgorithmException e) {
        LOGGER.error("Error while adding user [{}] with error [{}]", loginModel.toString(), e);

    }
    return registered;
}

Is this the right way or can there be another way which is neat and clean?


texts:
I wrote a script that parses API on schedule (Tuesday-Saturday), downloading everything for the previous day.

import requests
import pandas as pd
from datetime import date, timedelta

# # This is what I'd normally use, but since there would be no data today,
# # I assign specific date myself
# DATE = (date.today() - timedelta(days=1)).strftime("%Y-%m-%d")
DATE = "2020-10-23"
URL = "https://spending.gov.ua/portal-api/v2/api/transactions/page/"

def fetch(session, params):
    next_page, last_page = 0, 0
    while next_page <= last_page:
        params["page"] = next_page
        data = session.get(URL, params=params).json()
        yield pd.json_normalize(data.get("transactions"))\
                .assign(page=params.get("page"))
        next_page, last_page = next_page+1, data["count"] // data["pageSize"]

def fetch_all():
    with requests.Session() as session:
        params = {"page": 0, "pageSize": 100, "startdate": DATE, "enddate": DATE}
        yield from fetch(session, params)

if __name__ == "__main__":
    data = fetch_all()
    pd.concat(data).to_csv(f"data/{DATE}.csv", index=False)

Here I’m wondering about a couple of things.
Firstly, if I’m using requests.Session correctly.
I read in the documentation that:

The Session object allows you to persist certain parameters across requests. ... So if you’re making several requests to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase.

I'm not sure whether that's the case here as I didn't notice any changes in the performance.
Secondly, if splitting code into two functions instead of one was a good idea.
Here I thought that it would be easier to maintain -- the underlying function fetch doesn't change while fetch_all potentially could. For example, I could feed a range of dates instead of a singe date, changing fetch_all to:
def fetch_all(date_range):
    with requests.Session() as session:
        for date in date_range:
            params = {"page": 0, "pageSize": 100, "startdate": date, "enddate": date}
            yield from fetch(session, params)

Also, the yield and yield from -- could've used .append and returned a list instead. Not sure which approach is better.


texts: Project Euler 11: Largest product in a grid


texts: Transforming a parts catalog into a different data structure

texts:
The entire purpose of this code is to arrange the data that was dumped into CommRef_ into the required structure of ResObj.

The issue is, some of the CommRef arrays can have as many as 35,000+ elements in them. This causes the operation to take upwards of 13 seconds, so I'm looking for ways to make this more efficient.

Input Structure

{ "TableName": [
  { "PartNumber": "Widget00001", "PartName": WidgetName },
  { "PartNumber": "Widget00002", "PartName": WidgetName },
  ect.....,
  { "PartNumber": "Widget35001", "PartName": WidgetName }
]

Desired output structure

{
  "DataSources": [
    {
      "Id": "PartNumbers",
      "DeletedRows": [],
      "NewRows": [],
      "Rows": [
        [
          "00001",
          "WidgetName"
        ],
        [
          "00002",
          "WidgetName"
        ],
         [
          "35001",
          "WidgetName"
        ]
      ]
    }
  ]
}

Code

RefDataTables.forEach(function(t,ti){
  try{
    if(CommRef_[t]){
      if (CommRef_[t].hasOwnProperty("error")){
        response.statusCode = 500;
        responseBody = responseBody + "\n" + JSON.stringify(CommRef_[t].error);
      }else{
        response.statusCode = 200;

        //Build Row Array
        rowsObj = {"Rows":[]};
        CommRef_[t].forEach(function(d,di){
          rowsObj.Rows.push(_.values(CommRef_[t][di]));
        });

        //Build NewRows Array

        //Build Deleted Rows Array

        //Append DataSources Array
        ResObj.DataSources.push({"Id": t, "Rows":rowsObj.Rows, "NewRows":[], "DeletedRows":[]});
      }
    }else{
      ResObj.DataSources.push({"Id": t, "Rows":[], "NewRows":[], "DeletedRows":[]});
    }
  }
  catch(err){
    responseBody = responseBody + "\n" + JSON.stringify("Ref Data Object Build Error " + err);
  }
  });


texts: Performing API calls from a mobile client, reauthorizing the session if necessary

texts:
I'm working on mobile app which doing requests to a server's API. I want to develop module that doing following:

get authorization key already exists in previous session
connect to server api and read server StatusLine answer code
if answer code is 403 then try to re authorize
when authorization is passed process server answer body

I implement this with state-machine:

enum ConnectionState {
    checkAnswer, reAuth, processLinks, endProcess
}

And made process in while-loop:

        int authCount = 0;
        HttpResponse httpResponse = null;
        ConnectionState state = ConnectionState.checkAnswer;
        boolean processConnect = true;
        while (processConnect) {
            switch (state) {
                case checkAnswer:
                    httpResponse = client.execute(httpGet);
                    final int serverAnswer = httpResponse.getStatusLine().getStatusCode();
                    if (403 == serverAnswer) {
                        state = ConnectionState.reAuth;
                    } else {
                        state = ConnectionState.processLinks;
                    }
                    break;
                case reAuth:
                    if (++authCount > MAX_AUTH_ATTEMPTS) {
                        state = ConnectionState.endProcess;
                        break;
                    }
                    auth.invalidateAuth();
                    if (!auth.doAuth()) {
                        Utils.log("could not authorize api");
                    } else {
                        state = ConnectionState.checkAnswer;
                    }
                    break;
                case processLinks:
                    if (null == httpResponse)
                        throw new RuntimeException("HttpResponse is null");
                    processLinks(httpResponse, imgLinks);
                    state = ConnectionState.endProcess;
                    break;
                case endProcess:
                    processConnect = false;
                    break;
                default:
                    throw new RuntimeException("Finded impossible state: " + state.toString());
            }
        }

What do you think about such code organization for this task? How to write it more elegantly?


texts: Using serializable to preserve/save objects

texts:
I'm building one of my first android apps. I met this problem that everytime you change screen orientation, Android destroys the app and builds it up again (I know this is more complex, but this isn't the subject of this post.)

I am wondering if it is good idea to have a Game game variable inside my MainActivity.java that would simply implement Serializable. This way I can do this:

public class MainActivity extends AppCompatActivity {

    private MagicSquareGame game;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        game = MagicSquareGame.getEngine();
        initEditTextReferences();
        initButtonReferences();
        updateSumResultsOnScreen();
    }

    (all the other stuff...)

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putSerializable("game", game);
        Log.i("MSG", "saving Game");
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        game = (MagicSquareGame) savedInstanceState.getSerializable("game");
        updateSumResultsOnScreen();
        Log.i("MSG", "loading Game");
    }

Is this an okay approach? Code smell? What problems can I meet?


texts: Looping over an array with requestAnimationFrame

texts:
Over on SO I posted a question: Change transition duration during loop with requestAnimationFrame which hasn't got any responses just yet. I've since come up with this idea and wanted to know if there was any way to improve it.

Essentially I would like to loop over an array of elements that have a smooth transition (a different state to normal) when swapping between them.

Instead of setting a setTimeout during Array.forEach, then using requestAnimationFrame to loop over the whole thing. I'm now using requestAnimationFrame for the main loop and Array.shifting to work on the current element.

Any ideas for improvement? Is this efficient?

function animateArray(fooArray, bar) {

    // Timing
    var start = null;
    var interval = 3000; // how long each element in the array will be displayed for
    var intro = 1000; // easing/tweening in duration
    var outro = 1000; // easing/tweening out duration
    var last = 0 // The last interval,
    var current; //current element of array
    var fooLength = fooArray.length;

    function update(timestamp) {

        // get the start time and progress
        if (!start) start = timestamp;
        var progress = timestamp - start;

        // do something every `interval` seconds (if it can)
        if(!last || timestamp - last >= interval) {
            last = timestamp;

            // pull off the first element in the array to work on
            current = fooArray.shift();
        }

        if (typeof current !== null) {

            // Do something during the intro
            if (timestamp < last + intro) {
                console.log(`${current}-intro`);

            // The main animation
            } else if (timestamp > last + intro && timestamp < last + (interval - outro)) {
                console.log(`${current}`);

            // do something else during the outro
            } else {
                console.log(`${current}-outro`);
            }
        }

        // loop
        if (progress < interval * fooLength) {
            requestAnimationFrame((timestamp) => update(timestamp, fooArray, bar));
        }

    };

    requestAnimationFrame((timestamp) => update(timestamp, fooArray, bar));
}

animateArray([1,2,3], 'bar');


texts: Find the shortest whole repetitive substring

texts:
I'm working on a problem to find wholly repeated shortest substring of a given string, and if no match, return length of the string.

My major idea is using a Trie tree to build substrings from length 1 to half length of the whole string, then traverse the Trie tree to find if there is a wholly repetitive match or not (since when I build Trie tree, I record the depth of leaf node and also how many times the leaf node has been reached).

I think my algorithm is still \$O(n^2)\$ and I'm looking for any code review comments for my current code and better ideas to improve time complexity.

Input and output example

catcatcat => 3
aaaaaa=>1
aaaaaba = > 7

My code

from __future__ import print_function
from collections import defaultdict
import sys
class TrieNode:
    def __init__(self):
        self.isEnd = False
        self.children = defaultdict(TrieNode)
        self.count = 0 # reached how many times
        self.depth = 0 # reached by how long sub-string
    def addNode(self, word):
        if not word:
            return
        node = self
        depth = 0
        for ch in word:
            node = node.children[ch]
        node.isEnd = True
        node.count += 1
        node.depth = len(word)
    # return minimal length of whole repetitive match
    # in a recursive way
    def traverseNode(self, totalLen):
        if self.isEnd == True:
            if (self.count * self.depth == totalLen):
                return self.depth
            else:
                return totalLen
        result = sys.maxint # set to a very big value
        for node in self.children.values():
            result = min(node.traverseNode(totalLen), result)

        return result
if __name__ == "__main__":
    results = []
    word = 'catcatcatcat' # output 3
    #word = 'aaaaaa' # output 1
    #word = 'aaaaaab' # output 7
    for step in range(1, len(word)//2 + 1):
        # whole repetive string must be start from zero
        i = 0
        root = TrieNode()
        while i+step <= len(word):
            root.addNode(word[i:i+step])
            i += step
        results.append(root.traverseNode(len(word)))
        print (step, results[-1])

    print (min(results))


texts:
I am needing to write my own IsNullOrWhiteSpace check for Java but am wondering if this is the best method to do so

public static boolean isStringNullOrWhiteSpace(String value) {
    if (isStringNullOrEmpty(value)){
        return true;
    }

    for (Character chars : value.toCharArray()){
        if (!Character.isWhitespace(chars)) return false;
    }

    return true;
}


texts:
On our home page we need simple multi-slide carousel. It is our highest traffic page, so we decided to remove all libraries (including jQuery) in order to increase page speed.

Functionality we trying to achieve is similar to this

Here is the code we have now: Codepen

function getStyleValue(elem, property) {
  return window.getComputedStyle(elem).getPropertyValue(property);
}

const arrows = document.querySelectorAll(".carousel .arrow"),
  [leftArrow, rightArrow] = arrows,
  slideContainer = document.querySelector(".carousel .slide-container"),
  slides = document.getElementsByClassName("slide"),
  amt = parseInt(getStyleValue(slides[0], "width"));

try {
  let trans = parseInt(
    getStyleValue(slideContainer, "transform")
      .split(",")[4]
      .trim()
  );
} catch (e) {
  console.log("`trans` being set to 0");
  trans = 0;
}

function slide() {
  if (this === rightArrow) {
    trans -= amt;
  } else if (this === leftArrow) {
    trans += amt;
  }
  if (trans <= amt * slides.length / 2 * -1) {
    rightArrow.style.display = "none";
  } else if (trans >= 0) {
    leftArrow.style.display = "none";
  } else {
    arrows.forEach(arrow => (arrow.style.display = "inline-block"));
  }
  slideContainer.style.transform = `translate(${trans}px)`;
}

arrows.forEach(arrow => arrow.addEventListener("click", slide));
body *::selection {
  background: none;
}

.carousel {
  --height: 128px;
  --width: 120px;
  display: block;
  margin: 100px auto 0;
  height: var(--height);
  text-align: center;
}

.carousel * {
  display: inline-block;
  position: relative;
  vertical-align: top;
  height: 100%;
}

.arrow-container {
  --width-arrow: 64px;
  width: var(--width-arrow);
}

.arrow {
  --arrow-width: calc(var(--width-arrow) / 2);
  position: absolute;
  top: 50%;
  left: 50%;
  width: var(--arrow-width);
  height: var(--arrow-width);
  background-color: white;
  font-weight: bold;
  line-height: var(--arrow-width);
  border-radius: 50%;
  transform: translate(-50%) translateY(-50%);
  transition: background-color 111ms, color 222ms;
  cursor: pointer;
}

.arrow:hover {
  background-color: rgba(0, 0, 0, 0.2);
  color: white;
}

.arrow:active {
  transform: scale(0.9) translate(-50%) translateY(-50%);
}

.arrow.left {
  display: none;
}

.display-container {
  /* * 4 to show 4 slides */
  width: calc(var(--width) * 4 - 2px);
  overflow-x: hidden;
}

.slide-container {
  /* * 8 cause that's the number of slides */
  width: calc(var(--width) * 8);
  /* start by going left 2 slides */
  /* transform: translate(calc(var(--width) * -2)); */
  transition: transform 333ms;
}

.slide {
  margin-left: -4px;
  width: var(--width);
  background-color: salmon;
}

.slide:nth-child(2n) {
  background-color: cornflowerblue;
}

    &lt;

1

2

3

4

5

6

7

8

    &gt;


texts: Creating a simple unit of work with Entity Framework and no repository

texts:
I've been trying to implement a .NET MVC Unit of Work API (rather than creating a separate repository), but it doesn't feel right. Am I going about this the correct way?

Following advice from here.

BaseController

public class BaseController : ApiController
    {
        protected DBEntities _dbEntities;

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            _dbEntities = new DBEntities();
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            _dbEntities.SaveChanges();
        }
    }

MyController

public class MyController : BaseController
{
    public HttpResponseMessage PutMyObject(int id, int id2)
    {
        if (id != 0)
        {
            var myObject = _dbEntities.MyObjects.Where(x => x.id == id);
            if (myObject.Count() > 0)
            {
                MyObject temp = myObject.SingleOrDefault();
                temp.Processed = true;
                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound);
            }
        }
        else
        {
            /* do some other stuff */
        }
    }

}

My thinking around this is that the controller action is a single Unit of Work. The database connection is opened when the controller action starts, and SaveChanges is called once the response is returned.

Am I going about this in the correct manner? Do I need to dispose of _dbentities after SaveChanges is called within the BaseController?


texts:
I have an array of objects that have a key attribute with the name of a country and a values attribute that is an array of values that contain data by year:

I needed the opposite: an array of objects where the key is the year and the values an array containing data for each country in the original array.

I wrote this script to remap it, but it feels like a huge hack to me. How can I do this in fewer lines of code and make it more readable?

self.dataByYear = []

$.each(self.dataByGroup, function(i,v) {
    $.each(v.values, function(j,x) {
        var inYears = self.dataByYear.filter(function(d) { return d.key == x.xgroup});
        if (inYears.length > 0 )  {
            if (self.dataByYear[j] !== undefined) {
                self.dataByYear[j].values.push(x);
            }
        } else {
            newObj = {};
            newObj.key = x.xgroup;
            newObj.values = [x];
            self.dataByYear.push(newObj);
        }
    })
});


texts: Random Plinko path using functional programming style

texts:
In order to learn both scala and functional programming, I've been rewriting the projects from my introductory CS class in scala. One of the projects was a plinko game, where the user picks a slot to drop a token into and then the path that the token took and the money won from the end slot is printed to the console. The number of slots is constrained and so as the token falls row by row it can never be less than 0 or greater than 8.

I opted to use a stream to calculate the path, but I feel like the way that I am determining whether or not the token is within the constraints isn't really representative of functional programming because I know that generally vals are preferred to vars. What is a better way to write this in a functional style? In general, is there a way I should think about keeping track of things like this that is more in keeping with a functional programming style?

def getPath(prevSlot: Double): Stream[Double] = {
  var left = Random.nextBoolean
  if (prevSlot <= 0.0) {
    left = false
  }
  else if (prevSlot >= 8.0) {
    left = true
  }

  if (left) {
    prevSlot #:: getPath(prevSlot - 0.5)
  }
  else {
    prevSlot #:: getPath(prevSlot + 0.5)
  }
}


texts: Function that returns words in string not exceeding max number of characters

texts:
I'm trying to create a robust function that receives a string -- typically a paragraph or two -- and the max number of characters in the output. One condition is to make sure that we return full words and never cut one in half.

Here's the code I have and I feel it needs to be improved. This is normal ES2015/ES6 with no additional libraries.

export const shortenTextToSpecifiedNumberOfCharacters = (input, numberOfCharacters) => {

    if (!input) return "";
    else if (input.length <= numberOfCharacters) return input;

    for (var i = numberOfCharacters; i > 0; i--) {
        if (input[i] == ' ') {
            return input.slice(0, i) + ' ...';
        }
    }

}


texts: SPOJ problem - The last digit of a number to a power

texts:
Here is the problem statement:

  Given integers \$a\$ (\$0 \le a \le 20\$) and \$b\$ (\$0 \le b \le 2,147,483,000\$), where \$a\$ and
  \$b\$ are not both 0, find the last digit of \$a^b\$.

  Input

  The first line of input contains an integer \$t\$, the number of test
  cases (\$t \le 30\$). \$t\$ test cases follow. For each test case will appear \$a\$
  and \$b\$ separated by a space.

  Output

  For each test case output an integer per line representing the result.

  Example Input:

2
3 10
6 2

  Example Output:

9
6

Here is the code which is exceeding the time limit:

#include <stdio.h>

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long int base,exponent;
        scanf("%lld%lld",&base,&exponent);
        if(base==0&&exponent==0)
        {
            printf("1\n");
        }
        else
        {
            long long int digit=1;
            long long int i;
            for(i=1;i<=exponent;i++)
            {
                digit=(base*digit)%10;
            }
            printf("%lld\n",digit);
        }
    }
    return 0;
}

How do I make this more efficient?


texts:
I tried to implement a traffic light system using state pattern, please comment on the OOP/design pattern use.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DesignPatternsQuestions
{
    /// <summary>
    /// implement a traffic light system
    /// </summary>
    [TestClass]
    public class StatePatternTest
    {
        [TestMethod]
        public void StateTrafficLightTest()
        {
            TrafficLight light = new TrafficLight(new RedState());
            light.ChangeLight();
            Assert.IsInstanceOfType(light.State,typeof(GreenState));
            light.ChangeLight();
            Assert.IsInstanceOfType(light.State, typeof(YellowState));
            light.ChangeLight();
            Assert.IsInstanceOfType(light.State, typeof(RedState));
            light.ChangeLight();
            Assert.IsInstanceOfType(light.State, typeof(GreenState));
        }
    }

    public class TrafficLight
    {
        private State _state;

        public TrafficLight(State state)
        {
            _state = state;
        }
        // Gets or sets the state

        public State State
        {
            get { return _state; }
            set
            {
                _state = value;
                Console.WriteLine("State: " +
                  _state.GetType().Name);
            }
        }
        //this is the trickS
        public void ChangeLight()
        {
            _state.SetState(this);
        }
    }

    public abstract class State
    {
        public abstract void SetState(TrafficLight light);
    }

    public class RedState: State
    {
        public override void SetState(TrafficLight light)
        {
            light.State = new GreenState();
        }
    }

    public class YellowState : State
    {
        public override void SetState(TrafficLight light)
        {
            light.State = new RedState();
        }
    }

    public class GreenState : State
    {
        public override void SetState(TrafficLight light)
        {
            light.State = new YellowState();
        }
    }
}