texts:
I've been porting python Keyczar to work under 2to3 conversion (Github). I wanted to consolidate its streaming AES encrypt/decrypt backend interface with its string decrypt/encrypt. So I wrote a new implementation using io.BufferReader and io.BufferedWriter. In my construction, I decrypt up until the last block and then check the MAC before decrypting that block and unpadding.

I feel a bit unsure of whether this is the right thing to do, as it's not my typical construction, and whether instead I should just buffer the whole thing and not decrypt at all until the MAC is checked.

I'm not a Python developer so any API or idiomatic suggestions are very welcome too.

These snippets are covered under the Apache license, follow the links to see whole code.

Encrypt

 def EncryptIO(self, reader , writer):
    """
    Write ciphertext byte stream containing Header|IV|Ciph|Sig.
    """
    mac = self.hmac_key.CreateStreamable()

    #Write and update mac for header
    header = self.Header()
    writer.write(header)
    mac.Update(header)

    #Generate, write, and update mac for IV
    iv_bytes = util.RandBytes(self.block_size)
    writer.write(iv_bytes)
    mac.Update(iv_bytes)

    buffer_size = self.block_size * 4
    cipher = self.__CreateCipher(self.key_bytes, iv_bytes)
    more = True
    while more:
      data = util.ReadLength(reader, buffer_size)
      more = len(data) == buffer_size

      if not more:
        data = self._Pad(data)

      #encrypt, update mac and write out ciphertext
      ciph_bytes = cipher.encrypt(data)
      mac.Update(ciph_bytes)
      writer.write(ciph_bytes)

    #call final mac and write whatever is returned
    ciph_bytes = cipher.final()
    mac.Update(ciph_bytes)
    writer.write(ciph_bytes)

    #write mac and flush
    writer.write(mac.Sign())
    writer.flush()

Decrypt

  def DecryptIO(self, header, reader, writer):
    mac = self.hmac_key.CreateStreamable()

    #update mac of header
    mac.Update(header)
    #read out iv and update mac
    iv_bytes = util.ReadLength(reader, self.block_size)
    mac.Update(iv_bytes)

    cipher = self.__CreateCipher(self.key_bytes, iv_bytes)

    more = True
    # buffer_size is a block multiple -- req'd
    buffer_size = self.block_size * 4
    # read out one more byte into starting tag buff
    # so on a valid ciphertext we will know we are on
    # the last block to unpad
    initial_len = util.HLEN + 1
    tagbuff = util.ReadLength(reader, initial_len)

    while more:
      buff = util.ReadLength(reader, buffer_size)
      more = len(buff) == buffer_size
      tempbuff = tagbuff + buff
      if more:
        buff = tempbuff[:buffer_size]
        tagbuff = tempbuff[buffer_size:]
      else:
        buff = tempbuff[:-util.HLEN]
        tagbuff = tempbuff[-util.HLEN:]
      mac.Update(buff)
      if not more:
        #check the mac before we try and finish decryption
        calctag = mac.Sign()
        if not util.ConstantTimeCompare(tagbuff, calctag):
          raise errors.InvalidSignatureError()
      ptext = cipher.decrypt(buff)
      if not more:
        #remove padding now that we know the ciphertext is valid
        ptext += cipher.final()
        ptext = self._UnPad(ptext)
      writer.write(ptext)
      writer.flush()


texts: Using bluepy to send and read serial data

texts:
I am trying to use bluepy to write and read serial data to/from an HC-08 Bluetooth module. The HC-08 is connected to a device that returns a fixed number of bytes whenever it receives a message over the HC-08.
Below is my code so far. This seems to work—however, I kind of winged this code from examples and an a lot of trial and error. I am not entirely sure what I am doing. Therefore, any comments/feedback for potential improvements would be very welcome. The code is implemented as a class myhc08 that has a write method. This method writes a string to the hc08 and then reads out a set number of bytes.
import struct
import bluepy.btle as btle
from matplotlib import pyplot

class ReadDelegate(btle.DefaultDelegate):
    def __init__(self):
        self.data = b''

    def reset(self):
        self.data = b''

    def handleNotification(self, cHandle, data):
        self.data = self.data + data

    @property
    def data_length(self):
        return len(self.data)

class myHC08:
    def __init__(self):
        self.mac = '34:14:B5:50:34:77'
        self.write_service_id = 4
        self.write_service = None
        self.peripheral = btle.Peripheral(self.mac)
        self.delegate = ReadDelegate()
        self.peripheral.withDelegate(self.delegate)

    def connect(self):
        s = self.write_service_id
        services = self.peripheral.getServices()
        self.write_service = self.peripheral.getServiceByUUID(list(services)[s].uuid)

    def write(self, message, min_bytes, unpack_string=False):
        print('Writing:', message)
        self.delegate.reset()
        c = self.write_service.getCharacteristics()[0]
        c.write(bytes(message, "utf-8"))
        print('Receiving %i bytes' % min_bytes)
        while self.peripheral.waitForNotifications(1): pass
        while self.delegate.data_length < min_bytes: pass
        received = self.delegate.data
        if unpack_string: received = struct.unpack(unpack_string,received)
        return received

    def disconnect(self):
        self.peripheral.disconnect()

bt = myHC08()
bt.connect()
r = bt.write('1*', 1250*2, '1250H')
bt.disconnect()

In case this helps, my computer lists the following UUIDs for the HC-08 device:
00001800-0000-1000-8000-00805f9b34fb Generic Access
00001801-0000-1000-8000-00805f9b34fb Generic Attribute
0000180a-0000-1000-8000-00805f9b34fb Device Information
0000ffe0-0000-1000-8000-00805f9b34fb Unknown
0000fff0-0000-1000-8000-00805f9b34fb Unknown


texts:
A part of my day job is load-testing of our infrastructure. I wanted a tool to quickly write a load scenario for some application (DB, HTTP, ...).  I have tried to write something in Haskell (a fairly new language for me). Would someone help me improve by reviewing the code?
This is the main module:
module Load.Core
  ( runLoadN,
    Config (..),
  )
where

import Control.Concurrent (ThreadId, forkIO)
import Control.Concurrent.STM (STM, atomically, newEmptyTMVar, putTMVar, readTMVar)
import Control.Concurrent.STM.TBMQueue (TBMQueue, closeTBMQueue, newTBMQueue, readTBMQueue, writeTBMQueue)
import Control.Concurrent.STM.TSem (TSem, newTSem, signalTSem, waitTSem)
import Control.Concurrent.TokenBucket (newTokenBucket, tokenBucketWait)
import Control.Exception.Lifted (finally)
import Control.Monad (replicateM_, void)
import Control.Monad.Base (MonadBase, liftBase)
import Control.Monad.Trans.Control (MonadBaseControl, liftBaseWith)
import Data.Default (Default, def)
import Data.Word
import Numeric.Natural

data Config m a b c = Config
  { supplier :: m a,
    performer :: a -> IO b,
    collector :: b -> c -> c,
    workers :: Natural,
    queue :: Natural,
    rate :: Double,
    burst :: Integer
  }

runLoadN :: (MonadBase IO m, MonadBaseControl IO m, Default c) => Natural -> Config m a b c -> m c
runLoadN nat cfg = do
  let n = toInt nat
  let w = toInt $ workers cfg
  let q = toInt $ queue cfg
  let b = fromIntegral $ burst cfg
  let r = toInvRate $ rate cfg
  let perf = performer cfg
  let coll = collector cfg
  let supp = supplier cfg

  jobq <- atomically' $ newTBMQueue q
  resq <- atomically' $ newTBMQueue q
  mvar <- atomically' newEmptyTMVar
  sema <- atomically' $ newTSem 0
  tobu <- liftBase newTokenBucket

  startWorkers w perf jobq resq sema tobu r b
  _ <- forkIO' $ closing resq $ \_ -> waitForWorkers w sema
  _ <- forkIO' $ closing jobq $ supplyRequests n supp
  _ <- forkIO' $ collectResults coll def resq mvar
  atomically' . readTMVar $ mvar
  where
    collectResults f z resq mv = do
      mbNxt <- atomically' $ readTBMQueue resq
      case mbNxt of
        Nothing -> atomically' $ putTMVar mv z
        Just nxt -> let z' = f nxt z in seq z' $ collectResults f z' resq mv

    supplyRequests n supp q = replicateM_ n (supp >>= \req -> atomically' $ writeTBMQueue q req)

    waitForWorkers w sema = atomically' (replicateM_ w (waitTSem sema))

    startWorkers w perf jobq resq sema tobu r b = do
      replicateM_ w $ forkIO' $ signaling sema $ performRequests perf jobq resq tobu r b

    -- todo: what about exceptions?
    performRequests perf jobq resq tobu r b = run
      where
        run = do
          mbreq <- atomically' (readTBMQueue jobq)
          case mbreq of
            Nothing -> return ()
            Just req -> do
              res <- liftBase $ tokenBucketWait tobu b r >> perf req
              atomically' $ writeTBMQueue resq res
              run

-- performs f on the queue and closes it afterwards
--
-- use for sending requests, closing when done
closing :: (MonadBaseControl IO m) => TBMQueue a -> (TBMQueue a -> m ()) -> m ()
closing q f = f q `finally` atomically' (closeTBMQueue q)

-- performs f and signals the the semaphore afterwards
--
-- use for worker threads, signaling that they are done
signaling :: (MonadBaseControl IO m) => TSem -> m () -> m ()
signaling sem f = f `finally` atomically' (signalTSem sem)

-- some convenience methods
--
--

forkIO' :: MonadBaseControl IO m => m () -> m ThreadId
forkIO' m = liftBaseWith $ \runInBase -> forkIO (void $ runInBase m)

atomically' :: (MonadBaseControl IO m) => STM a -> m a
atomically' stm = liftBase $ atomically stm

toInt :: Natural -> Int
toInt = fromIntegral . toInteger

toInvRate :: Double -> Word64
toInvRate r = round (1e6 / r)

Repository can be found here: https://gitlab.com/michoffmann/load


texts:
I'm working on an MVC project and I'm writing the code for get access to the database connection.

I have two files:

The first (PdodbLib.class.php) extends PDO:

class PdodbLib extends PDO {

    public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS) {

        parent::__construct($DB_TYPE . ":host=" . $DB_HOST . ";dbname=" . $DB_NAME, $DB_USER, $DB_PASS);

    }

}

The second (ModelLib.class.php) created the object:

abstract class ModelLib {

    public function __construct() {
        $this->db = new PdodbLib("mysql", "localhost", "test", "test", "my_pass_123");
    }

}

If I want to perform a select for example in other file I do something like this:

$sql = $this->db->prepare("select * from users");
$sql->execute();

return $sql->fetchAll(PDO::FETCH_ASSOC);

I would like to know if that's a good practise or if it's better to do it in a different way.


texts:
I've made a fictional hotel website that shows you hotel details when you click on a button.

jsFiddle

var hotel = {Name: "Park Hotel", Price: "£120.00", Rooms: 50}
var hotel2 = {name: "West End", price: "£240.00", rooms: 150}

el_p = document.getElementById("para1");
el_picture = document.getElementById("picture");
el_picture2 = document.getElementById("picture2");

function showPrice() {
    el_picture2.style.display = "none";
    el_picture.style.display = "block";
    el_p.innerHTML = " ";
    for(details in hotel) {
        el_p.innerHTML += (details += ": " + hotel[details] + "
");
    }
}

function showPrice2() {
    el_picture.style.display = "none";
    el_picture2.style.display = "block";
    el_p.innerHTML = " ";
    for(details in hotel2) {
        el_p.innerHTML += ( details += ": " + hotel2[details] + "
");
    }
}

Is there an easier way to code this? I'm just learning so I just did what I could.


texts: K&R 1-20 Converting tabs to spaces using idiomatic C

texts:
I'm learning C with K&R 2nd Ed.  I just completed exercise 1-20 (which is the detab program).  I was hoping to get some feedback on my work.  I want to make sure that I am taking a good C approach and that my knowledge in other languages isn't bleeding in.

/*
Exercise 1-20 in K&R 2nd Edition
detab: clarified here: http://stackoverflow.com/questions/7178201/kr-exercise-1-20-need-some-clarification
Written by Z. Bornheimer (provided as is without warranty).
*/

#include <stdio.h>

#define MAXLEN 10000
#define TABSTOP 4

int detab(char c, char str[], int i);

/* calls detab with appropriate data */
main()
{
    int i = 0;
    char c, str[MAXLEN];
    while ((c = getchar()) != EOF)
       i = detab(c, str, i);
    printf("%s\n", str);
    return 0;
}

/* replaces tabs w/ spaces in accordance to TABSTOP */
int detab(char c, char str[], int i)
{
    if (c == '\t')
        do
            str[i++] = ' ';
        while ((i % TABSTOP) != 0);
    else
        str[i++] = c;
    return i;
}


texts:
The challenge is to reconstruct an n-digit number using the following information:

  On each step, you choose an index x from 1 to n.  For all indices y (y
  < x), you calculate the difference by = ax - ay.

  You then calculate B1 — sum of all by which are greater
  than 0 and B2 — sum of all by which are less than 0.

  The answer for this step is B1 - B2.

  Input

  The first line contains two integers n and m, denoting the number
  of digits and number of steps. The second line contains n digits
  (without spaces) a1, a2, ..., an.
  Each of the next m lines contains a single integer x denoting the index for
  the current step.

  Output

  For each of m steps print single number in a line - answer of the
  step.

Can someone please help me optimize this code or provide a better, less time-consuming solution?

import java.io.*;
import java.util.*;
public class Chefs
{
    //BufferedReader in;
    DataInputStream in;
    PrintWriter out;
    HashMap<Integer,Integer> mymap=new HashMap<Integer,Integer>();
    int a[],n,m,b1,b2;
    public Chefs()
    {
        //in=new BufferedReader(new InputStreamReader(System.in));
        in=new DataInputStream(System.in);
        out=new PrintWriter(System.out,true);
        readInput();
        calchef();
    }
    void readInput()
    {
        try
        {
            String digit=in.readLine();
            String split[]=digit.split("\\s+");
            n=Integer.parseInt(split[0]);
            m=Integer.parseInt(split[1]);
            a=new int[n];

            digit=in.readLine();

            for(int i=0;i<digit.length();i++)
                a[i]=digit.charAt(i);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    void calchef()
    {
        for(int i=0;i<m;i++)
        {
            try
            {
                b1=0;
                b2=0;
                int index=Integer.parseInt(in.readLine());
                if(mymap.containsKey(index))
                {
                    int ans=mymap.get(index);
                    out.println(ans);
                }
                else
                {
                    for(int j=0;j<index-1;j++)
                    {
                        if(a[index-1]-a[j]>0)
                        {
                            b1+=(a[index-1]-a[j]);
                        }
                        else
                        {
                            b2+=(a[index-1]-a[j]);
                        }
                    }
                    int ans=b1-b2;
                    out.println(ans);
                    mymap.put(index,ans);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args)
    {
        //long startTime = System.currentTimeMillis();
        Chefs c=new Chefs();
        //long endTime   = System.currentTimeMillis();
        //long totalTime = endTime - startTime;
        //System.out.println(totalTime);
    }
}


texts: Determine fragment identifier from HTML page in Swift

texts:
For an iOS app which helps me rolling back vandalism on Stack Exchange, I have a piece of Swift code which downloads a revision page (example) and tries to find the 'spacer' fragment just above a certain revision. It might not be clear what I'm talking about, so here's a picture from Firefox + developer tools:

I have the HTML content of this page, and the GUID of the revision (8f9ab85f-1401-41e9-8f75-8a07b10bad32) from the Stack Exchange API. I'm looking for that element just above the revision header, since those are the only HTML elements with IDs on the page. I need that spacer-9617187a-fe48-4212-9a1a-f3a366e62736 so I can link directly to https://codereview.stackexchange.com/posts/189958/revisions#spacer-9617187a-fe48-4212-9a1a-f3a366e62736

For that, I've written a few lines of Swift code. The problem is that string handling in Swift confuses the **** out of me. Most of the language feels rather good, but I'd rather do string manipulation in SQL than in Swift...

Here is what I have so far. It works, but I was wondering if it could break in cases I haven't foreseen, or if it can be made more understandable/manageable by a future me. You see, even Stack Exchange's syntax highlighter has problems understanding it...

The input parameters for this piece of code are html (a String containing the content of the revisions page, e.g. https://codereview.stackexchange.com/posts/189958/revisions) and revisionGUID (8F9AB85F-1401-41E9-8F75-8A07B10BAD32 in the example above - the API returns them in upper case). fragment is eventually used as output parameter. The 43 is the length of spacer- plus a GUID.

// Find fragment just above selected revision
let range = html.range(of: #"onclick="StackExchange.revisions.toggle('"# + revisionGUID.lowercased() + #"')""#)!
let index = html.range(of: #"<tr id=""#, options: .backwards, range: html.startIndex..<range.lowerBound)!.upperBound
let fragment = String(html[index..<html.index(index, offsetBy: 43)])


texts:
I am starting to explore automated testing of code, so I decided to write some trivial code about the cartesian plane and test it. I am particularly interested in automated testing conventions and tips.

cartesian.py

def pytagoras(a,b):
    return (a**2 + b**2)**0.5

def distance(point_1,point_2):
    return pytagoras((point_1[0]-point_2[0]),(point_1[1]-point_2[1]))

def point_in_line(point,line):
    def function(x):
        return line[0] * x + line[1]
    return function(point[0]) == point[1]

def average(lst):
    return sum(lst) / len(lst)

def middle_point(point_1,point_2):
    return [average([point_1[0],point_2[0]]),average([point_1[1],point_2[1]])]

def is_valid_triangle(sides):
    return sum(sides) - max(sides) > max(sides)

def heron(AB,BC,AC):
    if not is_valid_triangle((AB,BC,AC)):
        raise ValueError("The sides length given can't be a triangle.")
    sp = sum([AB,BC,AC]) / 2
    return (sp * (sp-AB) * (sp-BC) * (sp-AC) )**0.5

def area_of_tringle(points):
    AB = distance(points[0],points[1])
    BC = distance(points[1],points[2])
    AC = distance(points[0],points[2])
    return heron(AB,BC,AC)

test_cartesian.py

import unittest
import cartesian

class test_cartesian(unittest.TestCase):
    def test_pytagoras(self):
        self.assertEqual(cartesian.pytagoras(3,4),5)
        self.assertEqual(cartesian.pytagoras(6,8),10)

    def test_distance(self):
        self.assertEqual(cartesian.distance( (0,0) , (3,4) ),5)
        self.assertEqual(cartesian.distance( (1,2) , (4,6) ), 5)
        self.assertEqual(cartesian.distance( (1,1) , (1,1) ),0)

    def test_point_in_line(self):
        self.assertTrue(cartesian.point_in_line([4,6],[1,2]))
        self.assertTrue(cartesian.point_in_line([2,8],[3,2]))

    def test_average(self):
        self.assertEqual(cartesian.average([8,4,8,0]), 5)

    def test_middle_point(self):
        self.assertEqual(cartesian.middle_point([8,2],[0,4]),[4,3])
        self.assertEqual(cartesian.middle_point([5,5],[7,13]),[6,9])

    def is_valid_triangle(self):
        self.assertFalse(cartesian.is_valid_triangle(2,4,100))
        self.assertTrue(cartesian.is_valid_triangle(6,5,20))

    def test_heron(self):
        self.assertEqual(cartesian.heron(3,4,5),6)
        self.assertAlmostEqual(cartesian.heron(3,6,8),7.64,places=2)

    def test_area_of_triangle(self):
        self.assertAlmostEqual(cartesian.area_of_tringle(([4,2],[6,2],[7,0])),2)
        self.assertAlmostEqual(cartesian.area_of_tringle(([4,1],[3,4],[9,1])),7.5)

if __name__ == '__main__':
    unittest.main()


texts:
Parameters:

first - the first string.
second - the second string.
similarCharacters - outputs the number of similar characters.
tolerance - the number of mistakes allowed to consider the strings "similar."

Function:

bool Str_Similar(std::string first, std::string second, unsigned int* similarCharacters = nullptr, int tolerance = INT_MIN)
{
    // Don't even check if either strings are empty.
    if(first.empty() || second.empty()) return false;

    // Determine if the first is greater than or equal to the second.
    const bool firstGreaterOrEqualToSecond = first.length() >= second.length();

    // By default, set the tolerance to half the length of the smaller string.
    if(tolerance == INT_MIN) tolerance = (firstGreaterOrEqualToSecond ? (second.length() / 2) : (first.length() / 2));
    if(tolerance < 0) tolerance = 0;

    // Start off with any length difference, which are considered mistakes.
    unsigned int mistakes = (unsigned int)abs(first.length() - second.length());
    // Search only the length of the smaller string.
    const size_t searchLength = (firstGreaterOrEqualToSecond ? second.length() : first.length());

    // Do the search.
    for(size_t i = 0, max = searchLength; i < max; i++)
    {
        if(first.at(i) != second.at(i)) mistakes++;
    }

    // Output the similar characters.
    if(similarCharacters != nullptr) *similarCharacters = (unsigned int)abs(searchLength - mistakes);

    // Compare the mistakes to the tolerance.
    return (mistakes <= tolerance);
}


texts: Simple React component to show sorted articles

texts:
I have a basic articles sorting application, the App component gets some articles and pass them to the Articles component which renders a list of the articles sorted by date or by upvotes.
Each article is an object with id (string), title (string), upvotes (number), and date (string format YYYY-MM-DD) properties.
By default the articles are shown by upvotes in descending order.
This is the Articles component:
import React from "react";

function Articles({ articles }) {
  return (

            Title
            Upvotes
            Date

          {articles.map(({id, title, upvotes, date}) => (

              {title}
              {upvotes}
              {date}

          ))}

  );
}

export default Articles;

And this is the App component:
import React, { useState } from 'react';
import './App.css';

import Articles from './components/Articles';

function App({ articles }) {

  const sortByUpvotesDesc = (a,b) => b.upvotes - a.upvotes
  const sortByDateDesc = (a,b) => b.date > a.date ? 1 : b.date === a.date ? 0 : -1

  const [articlesOrder, setArticlesOrder] = useState('byUpvotesDesc')

  const mostUpvotedClickHandler = () => {
    setArticlesOrder('byUpvotesDesc')
  }

  const mostRecentClickHandler = () => {
    setArticlesOrder('byDatesDesc')
  }

  return (

        Sort By
        <button onClick={mostUpvotedClickHandler} >Most Upvoted</button>
        <button onClick={mostRecentClickHandler} >Most Recent</button>

      <Articles articles={articles.sort(articlesOrder === 'byUpvotesDesc' ? sortByUpvotesDesc : sortByDateDesc)} />

  );

}

export default App;

I would appreciate any insights and feedback on how to improve this, especially regarding the App component.


texts:
I am a beginner in Python and I have written a program to convert binary numbers to decimal. It works, but are there any ways to improve the code and make it better?
def binary_to_decimal_converter(binary_value):
    print(f"Binary: {binary_value}")
    decimal_value = 0
    # Treating binary input as a string
    # power is power of number 2
    # power must be less than string length because computer starts counting from 0th index
    power = len(str(binary_value)) - 1
    loop_var = 0
    while loop_var < len(str(binary_value)):
        if str(binary_value)[loop_var] == str(1):
            decimal_value += 2 ** power
        power -= 1
        loop_var += 1
    return print(f"Decimal: {decimal_value}")

# Calling the function
binary_to_decimal_converter(1111101100)

Output:
Binary: 1111101100
Decimal: 1004

```


texts: Reversing a string in place without using iteration in C

texts:
This is function for reversing a string in C using recursive calls:

#include <stdio.h>
#include <string.h>

void revert(char* left, char* right) { // Should I pass char* like I'm doing now or are char** a better idea?
    if(left == right) return;

    char *ll = left; char* rr = right;

    char tmp = right[0];
    right[0] = left[0];
    left[0] = tmp;
    revert(++ll,--rr);
}

int main() {
    char rev_string[] = "Some teststring";
    printf("%s\n", rev_string);
    revert(&rev_string[0],&rev_string[strlen(rev_string)-1]);//rev_string[strlen(rev_string)-1]);
    printf("%s\n", rev_string);
    return 0;
}

Can it be simplified? Can anything be written better?


texts:
For some time, JavaScript string concatenation could be optimised by join()ing an array of strings, instead of +ing them together.

Recently, browsers have been optimised to handle strings better, and +ing is back in favour.

The various posts I've read on the subject have cited many jsperf.com tests, which are invariably regular, and nothing much like the dynamic irregularity typically encountered in most web apps that need to concatenate on the fly.

So, I built a new test, the idea of which is to subject each method of concatenation to a relatively equal degree of randomness.

The results show a similar performance difference as other tests of arr.join("") vs. str + str.

Question: Is this jsperf.com test code good jsperf.com test code; is there method in my madness, or am I just mad?

Familiararity with jsperf.com testing is assumed.

"Where a script is concatenating arbitrary numbers of strings dynamically and often, is it 'better' to define a function to handle it, arr.join() inline, or perform simple inline concatenations?"

Preparation code:

Here I aim simply to create a large string from which random substrings can be extracted and stored to global vars for concatenation during the test runs. Also create a couple of functions, one for programmatic concatenation of an arbitrary number of strings using the arr.join("") method, and another for the random selection of the global substrings.

var base = "so many ways to skin a cat",
    c = 0, bl, hbl,
    rand = function( s, e ) {
        return Math.floor( Math.random() * ( e - s ) ) + s;
    },
    concatenateThese = function() {
        return [].slice.call( arguments ).join( "" );
    };
while ( ++c < 10 ) {
    base += base;
}
bl = base.length;
hbl = Math.floor( bl / 2 );
c = 0;
while ( ++c < 100 ) {
    window[ "randStr" + c ] = base.substr( rand( 0, hbl ), rand( hbl, bl ) );
}

My intention is that all the following test snippets are subject to a realistically equal amount of computational randomness where e.g. gluing an identical string to itself for 10000 iterations, can't be itself optimised. Each value is unknowable until runtime.

Test Snippet 1:

This should concatenate 7 randomly selected global strings from those created by the preparation code, by use of the + operator.

var rslt = window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ] +
    window[ "randStr" + rand( 0, 100 ) ];

Test Snippet 2:

This should concatenate 7 randomly selected global strings from those created by the preparation code, by use of the join() method.

var rslt = [ window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ] ].join( "" );

Test Snippet 3:

This should concatenate 7 randomly selected global strings from those created by the preparation code, by use of the join() method via a handler function created by the preparation code.

var rslt = concatenateThese( window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ],
    window[ "randStr" + rand( 0, 100 ) ] );


texts:
Yesterday I have found that I can't use the wireless network at some spots in my house. I used another modem as a WiFi booster and I managed to cover these spots.

The problem is that when I go to these dead spots I need to use static IP and change my primary DNS servers, or I get limited connection. Also, I still want to use DHCP when I'm not in these spots.

I have written two Batch files and a Python script to define the wireless adapter settings. I would like someone to take a look and suggest how to improve it.

Batch Files (I'm using shortcuts because of the option to run them as administrator)

DeadSpots.bat.ink

netsh interface ip set address "Wi-Fi" static 192.168.x.x 255.255.255.0 192.168.x.x
netsh interface ip set dns "Wi-Fi" static 192.168.x.x primary # This is the second modem
netsh interface ip add dns "Wi-Fi" ISP.dns.IP index=2

Regular.bat.ink

netsh interface ip set address "Wi-Fi" dhcp
netsh interface ip set dnsservers "Wi-Fi" source=dhcp

Python code

import subprocess as sub

def WiFi():
    filepath1 = Path_To_DeadSpots.bat.ink
    filepath2 = Path_To_Regular.bat.ink
    loc = input("Please choose your location: 1-Rooms, 2-Rest \n")
    while(loc != "1" and loc != "2"):
        print("Wrong input, please choose again")
        loc = input("Please choose your location: 1-Rooms, 2-Rest \n")
    if loc == "1":
        p = sub.Popen(filepath1,shell=True,stdout=sub.PIPE)
    else:
        p = sub.Popen(filepath2,shell=True,stdout=sub.PIPE)
WiFi()


texts:
I'm doing a problem from Project Euler to find the sum of all primes under two million.

My code manages to get a pretty good result for small inputs, but when trying inputs like million or two it just won't finish.

import time
import math

def primes_sum(limit):
    num_list = range(2, limit)
    i, counter = 2, 1
    while i <= math.sqrt(limit):
        num_list = num_list[:counter] + filter(lambda x: x%i!=0, num_list[counter:])
        i = num_list[counter]
        counter +=1
    print sum(num_list)

def main():
    start_time = time.time()
    primes_sum(2000000)
    print 'Rum time: '+str(time.time()-start_time)

if __name__ == "__main__":
    main()

The error for 2 million:

Internal error: TypeError: FUNCTION_TABLE[HEAP[(HEAP[(c + 4)] + 28)]] is not a function


texts: In matrix of sub-matrices, select diagonal indices of off-diagonal submatrices

texts:
Let's say I have a square matrix of size (n*m) x (n*m) that is composed of n x n sub-matrices, with each submatrix being a square of size m x m.

I want to select the diagonal indices of the off-diagonal submatrices. This is the code that I have so far, but I would like to do this without the calls to itertools.permutations if possible.

n_submatrix = 3
submatrix_size = 2
my_matrix = np.zeros([n_submatrix * submatrix_size,
                      n_submatrix * submatrix_size])

for submatrix_index in itertools.permutations(range(n_submatrix),2):
    my_slice0 = slice(submatrix_index[0]*submatrix_size, (submatrix_index[0]+1)*submatrix_size)
    my_slice1 = slice(submatrix_index[1]*submatrix_size, (submatrix_index[1]+1)*submatrix_size)
    np.fill_diagonal(my_matrix[my_slice0,my_slice1],1)

my_matrix

#array([[ 0.,  0.,  1.,  0.,  1.,  0.],
#       [ 0.,  0.,  0.,  1.,  0.,  1.],
#       [ 1.,  0.,  0.,  0.,  1.,  0.],
#       [ 0.,  1.,  0.,  0.,  0.,  1.],
#       [ 1.,  0.,  1.,  0.,  0.,  0.],
#       [ 0.,  1.,  0.,  1.,  0.,  0.]])


texts: Drawing random smooth lines contained within a square

texts:
I'm trying to write a matlab function that creates random, smooth trajectories in a square of finite side length. Here is my current attempt at such a procedure:

function [] = drawroutes( SideLength, v, t)
%DRAWROUTES Summary of this function goes here
%   Detailed explanation goes here

%Some parameters intended to help help keep the particles in the box
RandAccel=.01;
ConservAccel=0;
speedlimit=.1;
G=10^(-8);
%
%Initialize Matrices
Ax=zeros(v,10*t);
Ay=Ax;
vx=Ax;
vy=Ax;
x=Ax;
y=Ax;
sx=zeros(v,1);
sy=zeros(v,1);
%
%Define initial position in square
x(:,1)=SideLength*.15*ones(v,1)+(SideLength*.7)*rand(v,1);
y(:,1)=SideLength*.15*ones(v,1)+(SideLength*.7)*rand(v,1);
%
for i=2:10*t
    %Measure minimum particle distance component wise from boundary
    %for each vehicle
    BorderGravX=[abs(SideLength*ones(v,1)-x(:,i-1)),abs(x(:,i-1))]';
    BorderGravY=[abs(SideLength*ones(v,1)-y(:,i-1)),abs(y(:,i-1))]';
    rx=min(BorderGravX)';
    ry=min(BorderGravY)';
    %
    %Set the sign of the repulsive force
    for k=1:v
    if x(k,i)<.5*SideLength
        sx(k)=1;
    else
        sx(k)=-1;
    end
    if y(k,i)<.5*SideLength
        sy(k)=1;
    else
        sy(k)=-1;
    end
end
%
%Calculate Acceleration w/ random "nudge" and repulive force
Ax(:,i)=ConservAccel*Ax(:,i-1)+RandAccel*(rand(v,1)-.5*ones(v,1))+sx*G./rx.^2;
Ay(:,i)=ConservAccel*Ay(:,i-1)+RandAccel*(rand(v,1)-.5*ones(v,1))+sy*G./ry.^2;
%
%Ad hoc method of trying to slow down particles from jumping outside of
%feasible region
for h=1:v
    if abs(vx(h,i-1)+Ax(h,i))<speedlimit
       vx(h,i)=vx(h,i-1)+Ax(h,i);
    elseif (vx(h,i-1)+Ax(h,i))<-speedlimit
       vx(h,i)=-speedlimit;
    else
        vx(h,i)=speedlimit;
    end
end
for h=1:v
    if abs(vy(h,i-1)+Ay(h,i))<speedlimit
       vy(h,i)=vy(h,i-1)+Ay(h,i);
    elseif (vy(h,i-1)+Ay(h,i))<-speedlimit
       vy(h,i)=-speedlimit;
    else
        vy(h,i)=speedlimit;
    end
end
%
%Update position
x(:,i)=x(:,i-1)+(vx(:,i-1)+vx(:,i))/2;
y(:,i)=y(:,i-1)+(vy(:,i-1)+vy(:,1))/2;
%
end
%Plot position
clf;
hold on;
axis([-100,SideLength+100,-100,SideLength+100]);
cc=hsv(v);
for j=1:v
plot(x(j,1),y(j,1),'ko')
plot(x(j,:),y(j,:),'color',cc(j,:))
end
hold off;
%
end

My original plan was to place particles within a square, and move them around by allowing their acceleration in the x and y direction to be governed by a uniformly distributed random variable. To keep the particles within the square, I tried to create a repulsive force that would push the particles away from the boundaries of the square.
In practice, the particles tend to leave the desired "feasible" region after a relatively small number of time steps (say, 1000)."

I'd love to hear your suggestions on either modifying my existing code or considering the problem from another perspective.

When reading the code, please don't feel the need to get hung up on any of the ad hoc parameters at the very beginning of the script. They seem to help, but I don't believe any beside the "G" constant should truly be necessary to make this system work.

Here is an example of the current output:

Many of the vehicles have found their way outside of the desired square region, [0,400] X [0,400].


texts: Finding the lowest possible number of changes to turn one string into another

texts:
I've been working on a task: I have two giant strings, both consist of the same characters just scrambled (both consist only capital english letters). The task is to find the lowest possible number of changes you can make to turn the first string in the other one, while 1 change = switching neighbour chars in the string. I found a solution that works just fine but there is a problem.

It works under 5 seconds only for input of about 100 000 char Strings. I need to make it work for up to 1000 000 char. I tried ArrayList, LinkedList, regular arrays, substrings and different variations of the algorithm. This one is the best so far I came up with but I'm out of ideas. Any help? Is there a faster collection I can use? Maybe the algorithm is wrong here?

"steps" int is the output:

String nazwiskoJas = fileInput.nextLine();
String nazwiskoMal = fileInput.nextLine();
ArrayList<Character> jas = new ArrayList();
ArrayList<Character> mal = new ArrayList();

for(int i=0;i<charNumber;i++) {
    jas.add(nazwiskoJas.charAt(i));
    mal.add(nazwiskoMal.charAt(i));
}

fileInput.close();

int steps=0;
int index=0;
while(jas.size()>1) {

    if(jas.get(0)!=mal.get(index)) {

        int distance = jas.indexOf(mal.get(index));
            jas.remove(distance);
            steps+=distance;

        }else {
            jas.remove(0);
        }
    index++;
}

System.out.println(steps);


texts: Getting the frequency of letters in each position

texts:
I have a text file like this example:

>chr12:86512-86521
CGGCCAAAG
>chr16:96990-96999
CTTTCATTT
>chr16:97016-97025
TTTTGATTA
>chr16:97068-97077
ATTTAGGGA

This file is divided into different parts, and every part has 2 lines. The line which starts with > is ID and the 2nd line is a sequence of letters and the letters are A, T, C or G and also the length of each sequence is 9 so, for every sequence of letters there are 9 positions. I want to get the frequency of the 4 mentioned letters in every position (we have 9 positions).

Here is the expected output for the small example:

one = {'T': 1, 'A': 1, 'C': 2, 'G': 0}
two = {'T': 3, 'A': 0, 'C': 0, 'G': 1}
three = {'T': 3, 'A': 0, 'C': 0, 'G': 1}
four = {'T': 3, 'A': 0, 'C': 1, 'G': 0}
five = {'T': 0, 'A': 1, 'C': 2, 'G': 1}
six  = {'T': 0, 'A': 3, 'C': 0, 'G': 1}
seven = {'T': 2, 'A': 1, 'C': 0, 'G': 1}
eight = {'T': 2, 'A': 1, 'C': 0, 'G': 1}
nine = {'T': 1, 'A': 2, 'C': 0, 'G': 1}

I am doing that in Python using the following command. This command has 3 steps. Steps 1 and 2 work fine, but would you help me to improve step 3, which made this pipeline so slow for big files?

Step 1: to parse the file into a comma-separated file

def fasta_to_textfile(filename, outfile):
    with open(filename) as f, open(outfile, 'w') as outfile:
        header = sequence = None
        out = csv.writer(outfile, delimiter=',')
        for line in f:
            if line.startswith('>'):
                if header:
                    entry = header + [''.join(sequence)]
                    out.writerow(entry)
                header = line.strip('>\n').split('|')
                sequence = []
            else:
                sequence.append(line.strip())
        if header:
            entry = header + [''.join(sequence)]
            out.writerow(entry)

Step 2: comma-separated file to a Python dictionary

def file_to_dict(filename):
    f = open(filename, 'r')
    answer = {}
    for line in f:
        k, v = line.strip().split(',')
        answer[k.strip()] = v.strip()
    return answer

To print functions from steps 1 and 2:

a = fasta_to_textfile('infile.txt', 'out.txt')
d = file_to_dict('out.txt')

Step 3: to get the frequency

one=[]
two=[]
three=[]
four=[]
five=[]
six=[]
seven=[]
eight=[]
nine=[]
mylist = d.values()
for seq in mylist:
    one.append(seq[0])
    two.append(seq[1])
    se.append(seq[2])
    four.append(seq[3])
    five.append(seq[4])
    six.append(seq[5])
    seven.append(seq[6])
    eight.append(seq[7])
    nine.append(seq[8])

from collections import Counter
one=Counter(one)
two=Counter(two)
three=Counter(three)
four=Counter(four)
five=Counter(five)


texts: Concisely, robustly, temporarily stashing c++ iostream formats

texts:
I got tired of the boilerplate and tedium restoring formatting context, so I made an RAII stasher that relies on the destroy-at-end-of-full-statement temporary semantics. With C++17 I can get it down to a single type, no helpers:

#include <iostream>
template< template<typename, class> class streamtmp, typename charT, class traits >
struct fmtstash {
    typedef streamtmp<charT,traits> streamtype;
    streamtype &from;
    std::ios_base::fmtflags flags;
    std::streamsize width;
    std::streamsize precision;
    charT fill;
    fmtstash(streamtype &str)
        : from(str), flags(str.flags()), width(str.width())
        , precision(str.precision()), fill(str.fill())  {}
    ~fmtstash() { from.flags(flags), from.width(width), from.precision(precision), from.fill(fill); }
    template<typename T> streamtype  &operator<<(const T &rhs) { return from << rhs; }
    template<typename T> streamtype  &operator>>(      T &rhs) { return from >> rhs; }
};

int main(int c, char **v)
{
    using namespace std;
    fmtstash(cout) << hex << 51901 <<'\n';
    cout << 51901 <<'\n';

    return 0;
}

which produces about the easiest-to-write-and-read formatting I can manage.

Am I missing any bets here? Shorter or more robust is what I'm after.

The biggest improvement I found since I first wrote it is to use from << rhs instead of the from.operator<<(rhs) I had, since user-defined stream insert/extractors have to be free functions and of course an explicit member callout breaks those.

I could make the template args simpler, just template<class streamtmp>, the full breakout is a holdover from when I had the internal reference as ios_base<charT,traits> &from; instead of the full original type, but I ran across a broken inserter that wanted to be first or only and took an ostream reference as its left operand.

And of course the boost savers don't supply the inserter and extractor templates at all.  Is this an oversight or is there some argument against using the destroy-at-end-of-full-expression semantics this way?


texts: Prompt user for input then create an acronym from it

texts:
My program is complete and working, but I would like second opinions on it. The program prompts the user to enter something like a phrase or some words. Then it will create an acronym of what was entered. What are some adjustments I can make to my program to make it more efficient?

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string acronym(string str);

int main()
{
    string str;

    while (true)
    {
        cout << "\nPlease enter a string: ";
        getline(cin, str);

        if (str == "")
        {
            break;
        }

        cout << "\n\nThe acronym is \"" << acronym(str) << "\"" << "\n";
    }

    system("PAUSE");

    return 0;
}

string acronym(string str)
{
    string phrase = "";

    phrase = str[0];

    for (int i = 0; i < str.length(); i++)
    {
        if (str[i] == ' ')
        {
            phrase += str[i+1];
        }
    }

    return phrase;
}


texts: Copying and comparing two sheets before deleting duplicates

texts:
I am running this code as a macro in Excel, which copies two columns from sheet 1 and pastes them into sheet two. It then compares the first column to a column in sheet two before deleting any duplicates. The two columns I am copying are product code and price. I need to compare the product code to the existing ones.

Private Sub CommandButton1_Click()

Dim MasterList As New Dictionary
Dim iListCount As Integer
Dim x As Variant
Dim iCtr As Integer
Dim v As Variant
Dim counter As Integer, i As Integer
counter = 0

With Sheets("Sheet2")
.Range("M:M").ClearContents
.Range("S:S").ClearContents

Sheets("Sheet1").Range("C:C").Copy
Sheets("Sheet2").Range("M1").Select
ActiveSheet.Paste

Sheets("Sheet1").Range("X:X").Copy
Sheets("Sheet2").Range("S1").Select
ActiveSheet.Paste

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

' Get count of records in master list
iListCount = .Cells(Rows.Count, "A").End(xlUp).Row
'Load Dictionary:
For iCtr = 1 To iListCount
    v = .Cells(iCtr, "A").Value
    If Not MasterList.Exists(v) Then MasterList.Add v, ""
Next iCtr

'Get count of records in list to be deleted
iListCount = .Cells(Rows.Count, "M").End(xlUp).Row

' Loop through the "delete" list.
    For iCtr = iListCount To 1 Step -1
        If MasterList.Exists(.Cells(iCtr, "M").Value) Then
            .Cells(iCtr, "M").Delete shift:=xlUp
            .Cells(iCtr, "S").Delete shift:=xlUp
        End If
    Next iCtr
  End With

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

MsgBox "Done!"
End Sub

There is just under 30,000 rows that it has to compare so I know that it is always going to take some time, but I was wondering if there was any way to speed it up or even just make my code more streamline and efficient.


texts:
Here's my code.

javascript:x=document.createElement("script");x.src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"; document.getElementsByTagName("head")[0].appendChild(x);copy(1)

Is this the correct way? Are there any flaws in it?

Running:

copy(1)

doesn't work by itself so that's not a possibility, and

javascript:x=document.createElement("script");x.src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"; document.getElementsByTagName("head")[0].appendChild(x);copy(1)

appears to be the best so far.


texts: Showing all possible permutations from a set

texts:
I wrote a small program which shows all possible permutations of choosing m integers from the set \${1, 2, ..., n}\$, as follows:

def perm(nlist, m):
    """
    Find all possible permutations of choosing m integers from a len(n)-list.

    INPUT:
    @para nlist: a list of length n.
    @para m: a integer.

    OUTPUT:
    All permutations arranged in ascending order lexicographically.
    """
    if m > len(nlist):
        return
    if m == 0 or m == len(nlist):
        return [[]]
    results = []
    for list_i in nlist:
        temp = list(nlist)
        temp.remove(list_i)
        results.extend(map(lambda x: [list_i] + x, perm(temp, m-1)))
    return results

However, this code is inefficient. Perhaps the recursive structure is not well-designed. How can I improve this code?


texts:
This is the first time I've written a class in Java to do encryption using AES. Since security is involved I would love it if someone could take a look at it and let me know if anything is wrong with my implementation. Additionally, any feedback about shortcuts I could have taken or sloppy processes I've used would be greatly appreciated!

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

public class Encrypt {

    private Cipher encryptCipher;
    private SecretKey key;
    private IvParameterSpec iv;

    private byte[] clearText;

    /**
     * Encrypts data using AES-128
     * @param clearText The data to be encrypted
     */
    public Encrypt(String clearText) {

        //Generate the IV and key
        this.iv = new IvParameterSpec(this.generateIv());
        this.key = this.generateKey();

        //Create an AES cipher in CBC mode using PKCS5 padding
        try {
            this.encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            try {
                this.encryptCipher.init(Cipher.ENCRYPT_MODE, this.key, this.iv);
            }
            catch(InvalidKeyException ex) {
                Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
            }
            catch(InvalidAlgorithmParameterException ex) {
                Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        catch(NoSuchAlgorithmException ex) {
            Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch(NoSuchPaddingException ex) {
            Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
        }

        //Convert the clear text passed by the user into bytes
        try {
            this.clearText = clearText.getBytes("UTF-16");
        }
        catch(UnsupportedEncodingException ex) {
            Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Generates a random IV to be used in the encryption process
     * @return The IV's byte representation
     */
    private byte[] generateIv() {
        SecureRandom random = new SecureRandom();
        byte[] ivBytes = new byte[16];
        random.nextBytes(ivBytes);
        return ivBytes;
    }

    /**
     * Generates a secret key to be used in the encryption process
     * @return The secret key
     */
    private SecretKey generateKey() {
        KeyGenerator keygen;
        try {

            //Java normally doesn't support 256-bit key sizes without an extra installation so stick with a 128-bit key
            keygen = KeyGenerator.getInstance("AES");
            keygen.init(128);
            SecretKey aesKey = keygen.generateKey();
            return aesKey;
        }
        catch(NoSuchAlgorithmException ex) {
            Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    /**
     * Encrypts the data passed during instantiation of this object
     * @return The byte representation of the encrypted data
     */
    public byte[] encrypt() {
        try {
            return this.encryptCipher.doFinal(this.clearText);
        }
        catch(IllegalBlockSizeException ex) {
            Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        catch(BadPaddingException ex) {
            Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

}


texts:
I have comma-delimited files like these, where the first field is sorted in increasing order:

Case 1 ( 1st file ) :

abcd,1
abcd,21
abcd,122
abce,12
abcf,13
abcf,21

Case 2 ( and another file like this ) :

abcd,1
abcd,21
abcd,122

What I want to do is convert the first file to like this :

abcd 1,21,122
abce 12
abcf 13,21

And similarly, for the second file like this :

abcd 1,21,122

Now, I wrote a very ugly code with a lot of if's to check whether the next line's string before the comma is same as current line's string so, if it is then do ....

It's so badly written that, I wrote it myself around 6 months back and it took me around 3-4 minutes to understand why I did what I did in this code.
Well in short it's ugly, in case you would like to see, here it is ( also there's a bug currently in here and since I needed a better way than this whole code so I didn't sort it out, for the curious folks out there the bug is that it doesn't print anything for the second case mentioned above and I know why ).

def clean_file(filePath, destination):
    f = open(filePath, 'r')
    data = f.read()
    f.close()
    curr_string = current_number = next_string = next_number = ""
    current_numbers = ""
    final_payload = ""
    lines = data.split('\n')[:-1]
    for i in range(len(lines)-1):
        print(lines[i])
        curr_line = lines[i]
        next_line = lines[i+1]
        curr_string, current_number = curr_line.split(',')
        next_string, next_number = next_line.split(',')
        if curr_string == next_string:
            current_numbers += current_number + ","
        else:
            current_numbers += current_number    # check to avoid ',' in the end
            final_payload += curr_string + " " + current_numbers + "\n"
            current_numbers = ""
    print(final_payload)
    # For last line
    if curr_string != next_string:
        # Directly add it to the final_payload
        final_payload += next_line + "\n"
    else:
        # Remove the newline, add a comma and then finally add a newline
        final_payload = final_payload[:-1] + ","+next_number+"\n"
    with open(destination, 'a') as f:
        f.write(final_payload)

Any better solutions?


texts: Top k closest pairs in a set of million 128-dimensional points

texts:
I have a set of 1 million points in 128-dimensional space. Among all trillion pairs of the points in the set, I need to get a subset of 100 million pairs whose cosine distances are less than that of every pair outside the subset.

I tried measuring cosine distances of all trillion pairs, sort them and get first 100 million of sorted pairs. But this program is estimated to take few years in single thread.

from scipy.spatial.distance import cosine

points = load_points_list()         # List of points, where each point is tuple of 128 floats
M = len(points)                     # 1 million points

cosine_distances, pairs = [], []
for i in range(M):
    for j in range(M):
        cosine_distances.append(cosine(points[i], points[j]))
        pairs.append((i, j))

# Sort pairs based on cosine distances
cosine_distances, pairs = (list(s) for s in zip(*sorted(zip(cosine_similarities, pairs_indices))))

top_100_million_closest_pairs = pairs[:100000000]

Is there a more efficient algorithm or GPU-enabled acceleration for a problem at this scale?


texts:
I'm a beginner in C++ and I would appreciate advices to optimize the following function I wrote with Eigen (in fact, to be used with RcppEigen).
So far, I observe a 3.5x speed-up compared to the corresponding function written in R, and I was wondering whether I could gain more.

Note that I'm working with very large matrices, so I rely on maps to avoid copies from the corresponding objects in R.

Thanks in advance for your help!

#include <RcppEigen.h>

using namespace Rcpp;
using namespace Eigen;

typedef Map<ArrayXd> MapArr1D;
typedef Map<ArrayXXd> MapArr2D;
typedef Map<MatrixXd> MapMat;
typedef Map<VectorXd> MapVec;

// [[Rcpp::depends(RcppEigen)]]

// [[Rcpp::export]]
void myFct(const MapMat M1, const MapMat M2, MapMat M3, MapMat M4, MapArr2D A1,
           MapArr2D A2, const MapArr1D a1, const MapArr1D a2, const MapArr1D a3,
           const MapArr1D a4, const MapArr1D a5, const double d1) {

 for (int j = 0; j < M1.cols(); ++j) {

    M4.noalias() -= M1.col(j) * M3.row(j);

    A1.row(j) = a1 * a2 * ((M2 - M4).transpose() * M1.col(j)).array();

    A2.row(j) = exp(-Fct(a3(j) - a4(j) - a5 / 2 - d1 / 2 -
      pow(A1.row(j).transpose(), 2) / (2 * a1) - log(a1) / 2));

    M3.row(j) = A1.row(j) * A2.row(j);

    M4.noalias() += M1.col(j) * M3.row(j);

  }
}

where Fct is some other function.


texts: Displaying grades and calculating average grade

texts:
I'd love to improve this code. I know I should put a lot of stuff in another class but I have no idea how.

import java.util.Scanner;

public class UserInteraction {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int choice = 0;
        String[] subjects = new String[10];
        int grades[] = new int[10];
        double sum = 0.0;

    do
    {
        System.out.println("1. Enter a course name and a grade");
        System.out.println("2. Display all grades");
        System.out.println("3. Calculate the average grade");
        System.out.println("4. Exit program");

        choice = scan.nextInt();

        if ( choice == 1 )
        {
            Scanner scansubjects = new Scanner(System.in);
            Scanner scangrades = new Scanner(System.in);

            System.out.println("Enter 10 subjects and their corresponding grades:");
            System.out.println();

            int i = 0;

            for( i = 0; i < 10; i++ )
            {
                System.out.println("Subject:");

                String temp = scansubjects.nextLine();
                subjects[i] = temp.toLowerCase();

                System.out.println("Grade:");

                grades[i] = scangrades.nextInt();

                if( i == ( subjects.length - 1 ) )
                {
                    System.out.println("Thank you!");
                    System.out.println();
                }
            }
        }

        if ( choice == 2 )
        {
            System.out.println("Subjects" + "\tGrades");
            System.out.println("---------------------");

            for(int p = 0; p < subjects.length; p++)
            {

                System.out.println(subjects[p] + "\t" + "\t" + grades[p]);
            }
        }

        if ( choice == 3 )
        {
              System.out.println("Total of grades: " + getSum(grades));
              System.out.println("Count of grades: " + grades.length);
              System.out.println("Average of grades: " + getAverage(grades));
              System.out.println();
        }

    } while ( choice != 4);

}

    public static double getAverage(int[] array)
    {
        int sum = 0;
        for(int i : array) sum += i;
        return ((double) sum)/array.length;
    }

    public static double getSum(int[] array)
    {
        int sum = 0;
        for (int i : array)
        {
         sum += i;
        }
        return sum;
    }

}


texts: Is it ok to use PHP's strpos and substr altogether in one statement?

texts:
I am using PHP to build a REST API. I want to receive requests from the URL in the following format:

http://mydomain.com/api/format/method?app_id=value&call=encrypted_request

So, I have modified my .htaccess as:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Now, I receive the request URI and extract the name of the method and the parameters from it:

$params = explode('/',$_SERVER['REQUEST_URI']);
$format = $params[count($params)-2];
$method = substr($params[count($params)-1],0,strpos($params[count($params)-1],'?'));
$app_id = $_REQUEST['app_id'];
$enc_request = $_REQUEST['call'];

My question here is, whether it is ok to use substr and strpos on the array value, simultaneously ? Also, whether this approach of forming the URL and extracting the paramters from it is correct ?

Reference :
substr strpos


texts: Remove list of files from another list of files by their name

texts:
In groovy I have a list of files A and also another list of files B. I want to remove all the files from A that have a filename that also occurs in the list of files B. I wrote the following code:

List<File> removeByName(final List<File> files, final List<File> removalFiles) {
    final Set<String> removalNames = removalFiles.collect { it.name }.toSet()
    files.findAll { ! removalNames.contains(it.name) }
}

Unfortunately I cannot use the remove method in the Collection class because two files are not equal even if they have the same file name. Are there any methods that I am missing which could be used to make that code cleaner?


texts: Using setInterval() to make data reactive

texts:
I have a video component passing data from an async GET request to a child component:
// Inside a v-for loop
<video
    :title="video.title"
    :id="video._uid"
>
// ...
<Controls :videoData="video" />

When receiving the prop in Controls I then use that data to select the video by it's id in the DOM and set data properties according to that element:
created() {
    setTimeout(() => {
        this.video = document.getElementById(this.videoData._uid);
        this.videoDuration = this.video.duration;

        setInterval(() => {
            this.videoCurrentTime = this.video.currentTime;
            this.videoPlaying = !this.video.paused;
            this.videoVolume = this.video.volume;
        }, 1);
    }, 1000);
}

The setTimeout is to get around the data being asynchronous and the setInterval is to continuously update the data, such as the current time of the video, whether it's paused or not, etc.
It works exactly as it should, but it seems really inefficient. What if someone has slow internet and it takes longer than 1000ms to fetch the data, for example.
I would think the fact it's in a v-for loop would prevent the need for setTimeout as the component wouldn't exist before the video has been fetched. I must be missing something.
Request:
created() {
    this.fetchVersion().then(async () => {
        await Axios.get(
            `https://api.storyblok.com/v1/cdn/stories/videos?version=published&token=<STORYBLOK_TOKEN>&cv=${this.getVersion}`
        ).then(res => {
            res.data.story.content.body.forEach(i => {
                this.storyblokVideos.push(i);
            });
        });
    });
}

Does anyone have a better way of making the data reactive?
UPDATE
Thanks to @potato's answer:
I set a data property called isLoading: true then used the video's event oncanplay to conditionally load the controls: @canplay="isLoading = false". No more async errors. Then I used the different events to pass data:
@play="playing = true"
@pause="playing = false"
@timeupdate="updateCurrentTime()"
@volumechange="updateVolume()"

Much more efficient.


texts:
I've made component that based on values submitted by user allows to update the list of records in the list. Code is working really nice, but I've been thinking if there is any room for improvement.
Especially I'm concerned about nextId value which is hardcoded, but I think it'll be better to have it increment based on existing last id (assuming ids are sorted in ascending order).
import { useState } from "react";
import GuestsList from "./GuestsList";
import GuestsForm from "./GuestsForm";

const initialValues = [
  { id: 1, firstName: "John", lastName: "Snow", email: "john@snow.com" },
  { id: 2, firstName: "Jack", lastName: "Frost", email: "jack@frost.com" },
];
var nextId = 3;
function GuestsManagementForm() {
  const [data, setData] = useState(initialValues);
  function parentSubmit(value) {
    console.log("Form submitted from parent");
    const newRecord = {
      id: nextId++,
      firstName: value.firstName,
      lastName: value.lastName,
      email: value.email,
    };
    setData([...data, newRecord]);
  }
  function resetForm() {
    console.log("Form restarted from parent");
  }
  return (

      <GuestsForm onSubmit={parentSubmit} onReset={resetForm} />

      <GuestsList listings={data} />

  );
}

export default GuestsManagementForm;

and the form itself can be found here:
import { Formik, Field, Form, ErrorMessage } from "formik";
import * as Yup from "yup";

function GuestsForm(props) {
  const handleReset = () => {
    props.onReset();
  };
  return (
    <Formik
      initialValues={{ firstName: "", lastName: "", email: "" }}
      validationSchema={Yup.object({
        firstName: Yup.string()
          .max(15, "Must be max 15 characters")
          .min(2, "Must be at least 2 characters")
          .required("Required"),
        lastName: Yup.string()
          .max(20, "Must be max 20 characters")
          .min(2, "Must be at least 2 characters")
          .required("Required"),
        email: Yup.string().email("Invalid email address").required("Required"),
      })}
      onSubmit={(values, actions) => {
        props.onSubmit(values);
        actions.setSubmitting(false);
        actions.resetForm();
      }}
    >
      <Form>
        First Name
        <Field name="firstName" type="text" />
        <ErrorMessage name="firstName" />

        Last Name
        <Field name="lastName" type="text" />
        <ErrorMessage name="lastName" />

        Email Address
        <Field name="email" type="email" />
        <ErrorMessage name="email" />

        <button type="submit">Submit</button>
        <button type="reset" onClick={handleReset}>Reset</button>
      </Form>
    </Formik>
  );
}

export default GuestsForm;


texts: Optimizing the Cython code for solving system of ODEs

texts:
I am trying to optimize this code for solving a system of ODE. It seems Cython does not speed up the code compared to code using numpy.

This the python code using numpy:

"""Solve system of ODEs."""

def solver(ode_sys, I, t, integration_method):

    N = len(t)-1
    u = np.zeros((N+1, len(I)))
    u[0, :] = I
    dt = t[1] - t[0]

    for n in range(N):
        u[n+1, :] = integration_method(u[n, :], t[n], dt, n, ode_sys)
    return u, t

def RK2(u, t, dt, n, ode_sys):

    K1 = dt * ode_sys(u, t)
    K2 = dt * ode_sys(u + 0.5 * K1, t + 0.5 * dt)
    unew = u + K2
    return unew

def problem1(u, t):
    return -u + 1.0

from numpy import exp

def problem2(u, t):
    return - u + exp(-2.0 * t)

to run the code:

def run(ode_sys, N, nperiods=40):
    I = np.ones(N)
    time_points = np.linspace(0, nperiods * 2 * np.pi, nperiods * 30 + 1)
    u, t = solver(ode_sys, I, time_points, RK2)

timing

%timeit run(problem1, 1000, 1000)
%timeit run(problem2, 100, 500)

418 ms ± 22.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
233 ms ± 79 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

My Cython version:
I directly call the functions inside the module to get more speedup

%%cython --annotate
import numpy as np
cimport numpy as np
cimport cython
ctypedef np.float_t DT

cdef extern from "math.h":
    double exp(double)

@cython.boundscheck(False) # turn off bounds checking for this func.
@cython.wraparound(False)  # Deactivate negative indexing.
cpdef solver(ode_sys, np.ndarray[DT, ndim=1, negative_indices=False, mode='c'] I,
             np.ndarray[DT, ndim=1, negative_indices=False, mode='c'] t,
             integration_method):

    cdef int N = len(t)-1
    cdef np.ndarray[DT, ndim=2, negative_indices=False,
                    mode='c'] u = np.zeros((N+1, len(I)))
    u[0, :] = I
    cdef double dt = t[1] - t[0]
    cdef int n
    for n in range(N):
        u[n+1, :] = RK2(u[n, :], t[n], dt, n, ode_sys)
    return u, t

def RK2(np.ndarray[DT, ndim=1, negative_indices=False, mode='c'] u,
        double t, double dt, int n, ode_sys):

    cdef np.ndarray[DT, ndim=1, negative_indices=False, mode='c'] K1, K2, unew
    K1 = dt * problem1(u, t)
    K2 = dt * problem1(u + 0.5 * K1, t + 0.5 * dt)
    unew = u + K2
    return unew

cdef problem1(np.ndarray[DT, ndim=1, negative_indices=False, mode='c'] u, double t):
    return -u + 1.0

cdef problem2(np.ndarray[DT, ndim=1, negative_indices=False, mode='c'] u, double t):
    return - u + exp(-2.0 * t)

timing:

%timeit run(problem1, 1000, 1000) # note that to check promlem2 I need to change it inside the modules

424 ms ± 8.23 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Thanks in advance for any guide.


texts: Using Dictionary of int and list of tuple in C#

texts:
I have implemented mathematical functions using a C# library. I basically need to output data by the RiskMatrixByFunds in the following format.

Key will contain Id and value will contain collection of string, double, double

For example:

Id 1

value

{'ArithmeticMean', 12.34, 3.44},
{'AverageGain', 12.35, 3.45},
{'AverageLoss', 12.36, 3.46},

I have used a Dictionary structure that will contain the int and list of tuple collection.

Let me know if it can be enhanced, any ideas on magic strings used, a better way to implement it.

public class RiskMatrix : IRiskMatrix
{

    public (double Monthly, double Annual) ArithmeticMean(IEnumerable<double> ReturnsList)
    {
        double returnList = ReturnsList.Mean();
        return (Monthly: returnList, Annual: returnList * Math.Pow(12, 0.5));
    }

    public (double Monthly, double Annual) AverageGain(IEnumerable<double> ReturnsList)
    {

        double returnList = ReturnsList.GainMean();
        return (Monthly: returnList, Annual: returnList * Math.Pow(12, 0.5));
    }

    public (double Monthly, double Annual) AverageLoss(IEnumerable<double> ReturnsList)
    {
        double returnList = ReturnsList.LossMean();
        return (Monthly: returnList, Annual: returnList * Math.Pow(12, 0.5));
    }

    public Dictionary<int, List<Tuple<string,double, double>>> RiskMatrixByFunds(Dictionary<int, IEnumerable<double>> ReturnsList)
    {
        Dictionary<int, List<Tuple<string ,double, double>>> returnsList = new Dictionary<int, List<Tuple<string,double, double>>>();
        List <Tuple<string, double, double>> list = null;

        foreach (KeyValuePair<int, IEnumerable<double>> entry in ReturnsList)
        {
            list = new List<Tuple<string, double, double>>();
            (double Monthly, double Annual) arithmeticMeanResult = ArithmeticMean(entry.Value);
            list.Add(new Tuple<string, double, double>("ArithmeticMean", arithmeticMeanResult.Monthly, arithmeticMeanResult.Annual));
            (double Monthly, double Annual) averageGainResult = AverageGain(entry.Value);
            list.Add(new Tuple<string, double, double>("AverageGain", averageGainResult.Monthly, averageGainResult.Annual));
            (double Monthly, double Annual) averageLossResult = AverageLoss(entry.Value);
            list.Add(new Tuple<string, double, double>("AverageLoss", averageLossResult.Monthly, averageLossResult.Annual));
            (double Monthly, double Annual) betaCorrelationresult = BetaCorrelation(entry.Value);

           returnsList.Add(entry.Key, list);
        }
        return returnsList;
    }
}


texts:
I decided to make my own version of C++17's std::optional

It's basically a class that can optionally hold a variable (as the name suggests), it avoids having to resort to std::pair<bool, T> to check whether a variable is set.

I'd like some feedback regarding the overall implementation

template<typename T>
class Optional {
 public:
  /**
   * The constructor
   */
  Optional() :
    is_set_(false) {}
  /**
   * The constructor
   * @param val The value to set it to
   */
  Optional(const T& val) :
    val_(val), is_set_(true) {}

  /**
   * Sets the value
   * @param val The value to set it to
   */
  void set(const T& val) { val_ = val; is_set_ = true; }

  /**
   * @return The value
   */
  T get() const { return val_; }

  /**
   * Clears the value
   */
  void clear() { val_ = T{}; is_set_ = false; }

  /**
   * @return Whether the value is set
   */
  bool empty() { return !is_set_; }

  /* Operators */

  Optional operator=(const T& val) {
    val_ = val;
    is_set_ = true;
    return *this;
  }

  operator T() const {
    return val_;
  }

  bool operator==(const bool& val) const {
    return (is_set_ == val);
  }

  explicit operator bool() const { return is_set_; }

 private:
  T val_{};
  bool is_set_ = false;
};


texts: jQuery plugin to detect Konami cheat code sequence

texts:
How can this be written a bit shorter?

jQuery.konami = function(fn, code) {
    // ↑ ↑ ↓ ↓ ← → ← → B A
    code = code || [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];

    var kkeys = '',
        i = 0;

    $(document).keydown(function(e) {
        var char = String.fromCharCode(e.which).toLowerCase();
        if (char === code[i++]) {
            kkeys += char;

            if (kkeys === code) {
                fn();
                kkeys = '';
                i = 0;
            }
        } else if (e.which === code[kkeys++]) {
            if (kkeys === code.length) {
                fn();
                kkeys = '';
                i = 0;
            }
        } else {
            kkeys = '';
            i = 0;
        }
    });
};


texts: CancellationToken and Reactive Extensions

texts:
I have a couple of calls that I need to cancel a subscription based on a CancellationToken.  I saw How to cancel an observable sequence but I needed a more generic way.  I created an extension method that seems to work.

public static IObservable<TSource> TakeUntil<TSource>(this IObservable<TSource> source, CancellationToken token)
{

    var cancelationObservable =
        Observable.Create<object>(observer => token.Register(() => observer.OnNext(null)));
    return source.TakeUntil(cancelationObservable);
}

Is there a built in way of Rx to cover this?  Is Observable.Create the best way to trigger the pulse?

This seems so basic that I feel I may be missing something.


texts: Union-set intersection-set for a binary-tree implementation of sets

texts:
From SICP:

  Exercise 2.65

  Use the results of
  exercises 2.63 and  2.64 to give (n)
  implementations of union-set and
  intersection-set for sets implemented
  as (balanced) binary trees.41

I wrote the following answer:

(define (union-set a b)
  (define (rec set1 set2)
    (cond ((null? set1) set2)
          ((null? set2) set1)
          ((= (car set1) (car set2)) (cons (car set1) (union-set (cdr set1) (cdr set2))))
          ((> (car set1) (car set2)) (cons (car set1) (union-set (cdr set1) set2)))
          (else (cons (car set2) (rec set1 (cdr set2))))))
  (let ((ls-result (rec (tree->list a) (tree->list b))))
    (partial-tree ls-result (length ls-result))))

(define (intersection-set a b)
  (define (rec set1 set2)
    (cond ((or (null? set1) (null? set2)) '())
          ((= (car set1) (car set2)) (cons (car set1) (rec (cdr set1) (cdr set2))))
          ((> (car set1) (car set2)) (rec set1 (cdr set2)))
          ((> (car set2) (car set1)) (rec (cdr set1) set2))))
  (let ((ls-result (rec (tree->list a) (tree->list b))))
    (partial-tree ls-result (length ls-result))))

Is this answer ok?  Could it be better?


texts: Counting numbers that are divisible by 3 and can be produced from the digits of a given number

texts:
I'm trying to solve this problem from codewars.

  Problem

  Given a certain number, how many multiples of three could you obtain with its digits? Suppose that you have the number 362. The numbers that can be generated from it are: 3, 6, 2, 36, 63, 62, 26, 32, 23, 236, 263, 326, 362, 623, 632 and among those 3, 6, 36, 63 are  divisible by 3.

  We need a function that can receive a number ann may output in the following order:

  the amount of multiples(without 0)
  the maximum multiple

My solution

I have found all possible permutations of the digits of the given number and checked if the number is divisible by 3. Here is my code -

from itertools import permutations as perm
from functools import reduce

def to_int(digs):    # takes a list of digits of an integer and returns the number
    n = reduce(lambda total, d: 10 * total + d, digs, 0)
    return n

def find_mult_3(num):
    digs = list(map(int, str(num)))
    perms = []        # stores all possible permutations with the digits of num
    for i in range(1, len(digs)+1):
        perms.extend(list(p) for p in set(perm(digs, i)))

    count, mx = 0, 0
    seen = set()      # to keep track of divisors already seen
    if 0 in perms:
        perms.remove(0)
    for p in perms:
        n = to_int(p)
        if n % 3 == 0 and n not in seen:
            if mx < n:
                mx = n
            count += 1
            seen.add(n)

    return [count, mx]

But getting TLE, is there any way to make this program faster or any other clever way to solve this problem? Please ask if there is any need for clarification in any part of the code.


texts:
Which of these 3 python snippets is more pythonic?

This 1 liner list comprehensions which is a little overcomplex

users_to_sent = [(my_database.get_user_by_id(x), group['num_sent'][x]) for x in user_ids]

or this multi liner which is 'too many lines of code'

users_to_sent = []
for id in user_ids:
    t1 = my_database.get_user_by_id(id)
    t2 = group['num_sent'][id]
    users_to_sent.append( (t1,t2) )

or should the 1 liner be spun out into a separate function and called from a list comprehension?

def build_tuple(x, my_database, group):
    return (my_database.get_user_by_id(x), group['num_sent'][x])

users_to_sent = [build_tuple(x, my_database, group) for x in user_ids]


texts: Analysis of call center employee performance

texts:
This code basically calculates a few metrics based on numerators and denominators and then it bins the data into quintiles. This is a common operation that I perform for various data sets. My issue is that I always have to rename the metrics and columns.

I have a feeling that I could create functions to make this more general perhaps. The common operations I am doing are:

% of total numerator
delta from a target
attainment (actual divided by the target)
impact (the weighted delta from the target, how much a person is increasing or decreasing the total score)
The quintiles (I have them labeled so the end-user understands what they are. These files are used in Excel for pivoting how the top and bottom employees perform)

I would like to have it more general: input a numerator and denominator. Instead of global variables, perhaps they could be input into a function?

csat_target =  0.75
fcr_target = 0.75
nps_target = 0.25
aht_target = 5.25

csat_weight = 0.30
fcr_weight = 0.30
nps_weight = 0.40
aht_weight = 0.00

df['fcr'] = df['final_hits'] / df['total_hits']
df['csat'] = df['csat_score_weight'] / df['survey_count']
df['nps'] = (df['promoters'] - df['detractors']) / df['survey_count']
df['aht'] =  (df['total_time'] / df['total_calls'])
df['goal_attainment'] = ((df['nps'] / nps_target) * nps_weight) + ((df['csat'] / csat_target) * csat_weight) + ((df['fcr'] / fcr_target) * fcr_weight)

df['fcr_volume'] = df['total_hits'] / df['total_hits'].sum()
df['fcr_delta'] = df['fcr'] - fcr_target
df['fcr_impact'] = (df['fcr_delta'] * df['fcr_volume'])

df['survey_volume'] = df['survey_count'] / df['survey_count'].sum()
df['nps_delta'] = nps_target - df['nps']
df['nps_impact'] = df['nps_delta'] * df['survey_volume']
df['overall_nps_impact'] = np.where(df['survey_count'] > 0, df['nps_impact'], 0)

df['csat_delta'] = df['csat'] - csat_target
df['csat_impact'] = -(df['csat_delta'] * df['survey_volume'])
df['overall_csat_impact'] = np.where(df['survey_count'] > 0, df['csat_impact'], 0)

df.replace([np.inf, -np.inf], np.nan, inplace = True)

df['call_volume'] = df['total_calls'] / df['total_calls'].sum()
df['aht_delta'] = df['aht'] - aht_target
df['aht_impact'] = (df['aht_delta'] * df['call_volume'] )

df['fcr_weight'] = np.where(df['total_hits'] > 0, fcr_weight, 0)
df['nps_weight'] = np.where(df['survey_count'] > 0, nps_weight, 0)
df['csat_weight'] = np.where(df['survey_count'] > 0, csat_weight, 0)

df['fcr_dfile'] = pd.qcut(df['fcr_impact'], 5, labels = ["D1", "D2", "D3", "D4", "D5"])
df['nps_dfile'] =  pd.qcut(df['overall_nps_impact'] , 5, labels = ["D1", "D2", "D3", "D4", "D5"])
df['csat_dfile'] = pd.qcut(df['overall_csat_impact'] , 5, labels = ["D1", "D2", "D3", "D4", "D5"])
df['aht_impact_dfile'] = pd.qcut(df['aht_impact'], 5, labels = ["D1", "D2", "D3", "D4", "D5"])
df['aht_dfile'] = pd.qcut(df['aht'], 5, labels = ["D1", "D2", "D3", "D4", "D5"])


texts:
I have this prime factor generator for some number n. It returns list of all prime factors. In other words, product of the list equals n.

def prime_factors_generate(n):
    a = []
    prime_list = sorted(primes())
    pindex = 0
    p = prime_list[pindex]
    num = n
    while p != num:
        if num % p == 0:
            a.append(p)
            num //= p
        else:
            pindex += 1
            p = prime_list[pindex]
    return a

The primes() function uses the Sundaram Sieve from here and returns a set of all primes below some limit, by default 106.

How can I make this code functional? I want to get rid of the while-loop, list appending and mutated state.


texts: How can I make this code (that is usually in a loop) faster and efficiently?

texts:
When I have a place with limited space, where I entry a text that can be few letters or huge amount of text because it's dynamic, I'm using this code to make the text fit the specific space.

#region TextSizing
string textSizing = databaseTable.Description;
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(IntPtr.Zero))
{
    size = g.MeasureString(textSizing, new System.Drawing.Font("Arial", 14)).Width;
}

if (size < 1500)
    lblText.Text = textSizing;
else
    lblText.Text = textSizing.Length > 135 ? textSizing.Remove(135) + "..." : textSizing;
#endregion

A friend of mine suggested that I should use cache to store the Font and the Graphics, I didn't understand it very well, but keep thinking about it... How can I make this code (that is usually in a loop) faster and efficiently ?


texts: Saving the Universe - Code Jam 2008 Qualification problem A

texts:
I'm trying to brush up on my Python, so I'm going through some simple problems.  Big concerns are style, though any algorithmic problems I'm open to suggestions on.

Basically you have an input file where the first line is the number of test cases.

The following lines are composed of the cases. Each case starts with a number S followed by S unique lines representing search engines.  Then there is a number Q followed by Q lines representing queries. Each query is the name of a search engine. You have to run the queries in sequence, but you can batch them up; however you cannot run a query on the corresponding search engine. The program should return the minimum number of switches between engines required.  I know that the result is correct (passes the tests on the site), so again focus is mainly style (not just PEP, but also pythonic-ness so to speak).

Source: Problem A. Saving the Universe

import fileinput

def solve(inFile):
    #discard list of possible values
    S = int(next(inFile))
    for i in range(0,S): #Feels wrong doing this one at a time
        next(inFile)

    count = 0
    Q = int(next(inFile))
    queue = set()
    #iterate over queries, adding each one to a set until the set contains all
    #S possible queries. Then increment count and reset set to only include
    #the newest query since we had to have cleared the queue before it arrived
    while Q > 0 :
        Q -= 1
        temp = next(inFile)
        queue.add(temp)
        if len(queue) == S:
            queue -= queue   #Feels wrong but queue=set(temp) doesn't work
            queue.add(temp)
            count += 1
    return count

if __name__ == "__main__":
    inFile = fileinput.input()
    T = int(next(inFile))
    for i in range(1,T+1):
        print("Case #", end="") #Feels wrong having four prints
        print(i, end="")
        print(": ", end="")
        print(solve(inFile))


texts:
Still learning Assembly. This program is a simple guessing game to test most of the new things I've learned! Any advice and all topical comments on code optimization and conventions is appreciated!

Compiled as follows using VS2017 x64 Native Tools Command Prompt:

> nasm -g -fwin64 guess.asm

> cl /Zi guess.obj msvcrt.lib legacy_stdio_definitions.lib

guess.asm

;; bits 64
default rel

extern time, srand, rand
extern printf, scanf

%macro str 2
    %2: db %1, 0
%endmacro

; use DQ to define a quad-word, DD to define a double-word, DW to define a word, and DB to define a byte.
section .rdata          ; immutable predefined variables
    str "Enter your guess (1-100): ", prompt
    str "%u", scan_fmt
    str "ERROR! Input was not a number!", scan_fail
    str {"Too high!", 10}, too_high
    str {"Too low!", 10}, too_low
    str {"You guessed it!", 13, 10}, congrats

; use RESQ to reserve a number of quad-words, RESD to reserve a number of double-words, RESW to reserve a number of words, and RESB to reserve a number of bytes.
section .bss            ; mutable undefined variables
    target: resb 4      ; 4 bytes of storage (enough for a 32bit number)

section .text
    global main
main:
    stack_reserve: equ 40      ; 32 + 8
    sub     rsp, stack_reserve ; shadow space for callees + 8 bytes for stack alignment

    ;; calculate the random number
    xor     rax, rax
    mov     rcx, rax        ; clear rax and rcx
    call    time
    mov     rcx, rax
    call    srand
    call    rand

    ;; rand = (rand % 100) + 1
    xor     rdx, rdx        ; clear rdx
    mov     rcx, 100
    div     rcx
    inc     rdx
    mov     [target], rdx
    ;mov     rcx, scan_fmt
    ;call    printf
.check:
    lea     rcx, [prompt]
    call    printf

    ; memory from rsp+0..31 has potentially been stepped on by printf
    ; leave RSP where it is, ready for another call

    ;;; scanf into that 8-byte block of stack space above the shadow space, or into our *own* shadow space
    lea     rdx, [rsp+32]        ; stack addresses are normally 64-bit, can't get away with edx
    lea     rcx, [scan_fmt]
    mov     dword [rdx], 0       ; instead of error check, set n = 0 in case of I/O error
    call    scanf
    cmp     eax, 1               ; success = exactly 1 conversion
    jnz     .scanf_fail          ; error check

    mov     r8d, [rsp+32]        ; r8d: 32-bit unsigned number to be checked

    cmp     r8d, [target]
    jg      .too_high
    jl      .too_low

    lea     rcx, [congrats]
    call    printf
.end:
    ;xor     eax,eax              ; return 0
    movzx   eax, byte [rsp+32]   ; return EXIT_SUCCESS(0) or EXIT_FAILURE(1)
    add     rsp, stack_reserve
    ret
.too_high:
    lea     rcx, [too_high]
    call    printf
    jmp     .check
.too_low:
    lea     rcx, [too_low]
    call    printf
    jmp     .check
.scanf_fail:
    lea     rcx, [scan_fail]
    call    printf
    jmp     .end


texts:
Given the following definitions of Functor and Coproduct from fp-course:

class Functor f where
  (<$>) ::
    (a -> b)
    -> f a
    -> f b

class Functor f => Applicative f where
  pure ::
    a -> f a
  (<*>) ::
    f (a -> b)
    -> f a
    -> f b

class Functor t => Traversable t where
  traverse ::
    Applicative f =>
    (a -> f b)
    -> t a
    -> f (t b)

data Coproduct f g a =
  InL (f a)
  | InR (g a)

Is there a cleaner or more succinct way to implement the following?

instance (Traversable f, Traversable g) =>
  Traversable (Coproduct f g) where
  traverse ::
    Applicative h =>
    (a -> h b)
    -> Coproduct f g a
    -> h (Coproduct f g b)
  traverse f (InL fa) = InL <$> traverse f fa
  traverse f (InR ga) = InR <$> traverse f ga


texts: Finding the date from "2nd Friday of X month"-style input

texts:
I've been working through some puzzles from exercism.io. Current puzzle asks me to return a date() from input that essentially says "The 3rd Friday of December" or "The 1st Monday of October". I'd love to hear some feedback on how to clean this code up:

def meetup_day(year, month, weekday, spec_weekday):
    import calendar
    from datetime import date

    last_day = calendar.monthrange(year, month)[1]
    wkday = {'Monday': 0, 'Tuesday': 1, 'Wednesday': 2, 'Thursday': 3, 'Friday': 4, 'Saturday': 5, 'Sunday': 6}
    schedule_day = wkday[weekday]

    if spec_weekday == "teenth":
        check_range = range(13, 20)
    elif spec_weekday == "last":
        check_range = range(last_day - 6, last_day + 1)
    else:
        spec_weekday = int(spec_weekday[0:1])
        check_range = range(7 * spec_weekday - 6, 7 * spec_weekday + 1)

    for index in check_range:
        if index > last_day:
            break
        if schedule_day == calendar.weekday(year, month, index):
            schedule_day = index

    return date(year, month, schedule_day)

meetup_day(2014, 3, "Friday", "2nd")
meetup_day(2013, 6, "Wednesday", "4th")
meetup_day(2013, 12, "Monday", "1st")
meetup_day(2015, 5, "Tuesday", "teenth")
meetup_day(2015, 4, "Thursday", "last")

I'm still fairly new to Python, and datetime stuff throws me through a loop. I chose to use calendar as well as date because I thought the syntax was easier to understand. However, I'm definitely open to ideas on more efficient ways to do this.

Quick note: The "teenth" thing:

  Note that "Monteenth", "Tuesteenth", etc are all made up words. There
  was a meetup whose members realised that there are exactly 7 days that
  end in '-teenth'. Therefore, one is guaranteed that each day of the
  week (Monday, Tuesday, ...) will have exactly one date that is named
  with '-teenth' in every month.


texts: Scala 3 Typeclass derivation using low-level mechanisms: The `Eq`-example

texts:
I've been playing around with this example from the Scala 3 documentation, and I noticed that certain things duplicate functionality already present in the standard API; In particular, the summonAll (which was working for Eq[_] only) seemed to be a combination of

Tuple.Map[Tup, F] type constructor, which recursively deconstructs a tuple Tup and modifies each entry by wrapping it in F
compiletime.summonAll, which already knows how to summon elements for each entry of a tuple-type.

I think it was either done this way for didactic purposes, or maybe the API changed after the example was written.
Anyway, I've attempted to modify it, and to use the above mentioned definitions from the API, that's what I obtained:
import deriving.Mirror
import compiletime.{ erasedValue, summonAll }

trait Eq[T]:
  def eqv(x: T, y: T): Boolean

object Eq:
  inline given derived[T](using m: Mirror.Of[T]): Eq[T] =
    val elemInstances = summonAll[Tuple.Map[m.MirroredElemTypes, Eq]]
    inline m match
      case s: Mirror.SumOf[T] => eqSum(s, elemInstances)
      case p: Mirror.ProductOf[T] => eqProduct(p, elemInstances)

  def eqSum[T](s: Mirror.SumOf[T], elems: Tuple): Eq[T] =
    new Eq[T]:
      def eqv(x: T, y: T): Boolean =
        val ordx = s.ordinal(x)
        (s.ordinal(y) == ordx) && check(elems.productElement(ordx))(x, y)

  def eqProduct[T](p: Mirror.ProductOf[T], elems: Tuple): Eq[T] =
    new Eq[T]:
      def eqv(x: T, y: T): Boolean =
        iterator(x).zip(iterator(y)).zip(elems.productIterator).forall{
          case ((x, y), elem) => check(elem)(x, y)
        }

  def iterator[T](p: T) = p.asInstanceOf[Product].productIterator

  def check(elem: Any)(x: Any, y: Any): Boolean = elem.asInstanceOf[Eq[Any]].eqv(x, y)

  given Eq[Int] with
    def eqv(x: Int, y: Int) = x == y
end Eq

case class Foo(a: Int, b: Int) derives Eq

Questions:

I had to introduce more Anys, in check in particular. Is there some way to specify that it's coming from a product type where each entry is a subtype of Eq[_]? Should one attempt to tighten some other types?
Is there anything else that could be shortened or simplified using the API methods?
Is p.asInstanceOf[Product] actually safe? Or should there be some additional conversion step that transforms the type T into Product?


texts: Proper practice using enums in header files

texts:
Long time coder, but very very new to C++. I'm trying to put together my first real-sized project and figuring out what I need to change about my script-kiddie style of C++ to get there. Below is my working code.

#include <math.h>
#include <vector>
#include <iostream>
#include <stdexcept>

enum class Metric { Cosine, Euclidean};

double cosine(std::vector<double> A, std::vector<double> B, bool similarity){
    double mul = 0.0;
    double d_a = 0.0;
    double d_b = 0.0;

    std::vector<double>::iterator A_iter = A.begin();
    std::vector<double>::iterator B_iter = B.begin();

    for ( ; A_iter != A.end(); A_iter++ , B_iter++ ) {
        mul += *A_iter * *B_iter;
        d_a += *A_iter * *A_iter;
        d_b += *B_iter * *B_iter;
    }

    if (d_a == 0.0f || d_b == 0.0f) {
        throw std::logic_error("Vectors must have a length greater than zero");
    }

    double cos_similarity = mul / sqrt(d_a * d_b);
    if (similarity == false) {
        return acos(cos_similarity) / M_PI;
    } else {
        return cos_similarity;
    }
}

double euclidean(std::vector<double> A, std::vector<double>B, bool similarity) {
    double sq_dist = 0.0;
    std::vector<double>::iterator A_iter = A.begin();
    std::vector<double>::iterator B_iter = B.begin();

    for ( ; A_iter != A.end(); A_iter++, B_iter++) {
        sq_dist += pow(*A_iter - *B_iter, 2);
    }
    if (similarity == false) {
        return sqrt(sq_dist);
    } else {
        return 1.0/sqrt(sq_dist);
    }
}

double distance(std::vector<double> A, std::vector<double> B, Metric metric, bool similarity = false) {

    if (A.size() != B.size()) {
        throw std::logic_error("A and B must be the same size!");
    }

    if (A.size() < 1) {
        throw std::logic_error("Empty vectors belong to no metric space");
    }
    switch (metric) {
        case Metric::Cosine:
            return cosine(A, B, similarity);
        case Metric::Euclidean:
            return euclidean(A, B, similarity);
    }
}

double similarity(std::vector<double> A, std::vector<double> B, Metric metric) {
    return distance(A, B, metric, true);
}

My first question is around exactly how I should construct the header file. Specifically curious about the right way to handle the enum. Secondly, any general feedback, things that I could clean up, cut out, etc... would be greatly appreciated.


texts: Calculate daily average for selected period in PHP


texts: Flattening a HashMap of (String → ArrayList) to an ArrayList

texts:
I have one HashMap<String, ArrayList<String>> and I want to concatenate a key with every item in its ArrayList.

Consider the following demo program:

HashMap<String, ArrayList<String>> map1 = new HashMap<String, ArrayList<String>>();

        ArrayList<String> map11 = new ArrayList<String>();
        map11.add("a");
        map11.add("b");
        map1.put("1", map11);

        ArrayList<String> map12 = new ArrayList<String>();
        map12.add("c");
        map12.add("d");
        map1.put("2", map12);

ArrayList<String> finalList = new ArrayList<String>();
        for (Entry<String, ArrayList<String>> ent : map1.entrySet()) {
            ArrayList<String> ls = ent.getValue();
            for (String string : ls) {
                finalList.add(ent.getKey()+" = "+string);
            }
        }

This is working fine and giving expected output. The value of finalList after executing program is:

  [1 = a, 1 = b, 2 = c, 2 = d]

If there is any better and faster way to do this, please let me know. In the actual project ArrayList, every HashMap entry contains thousands of items.


texts: Parsing a string of the form "key1=value1;key2=value2;" into a Hash

texts:
I need to parse the following input string into a hash table with the following format:

Input:

'1001=23;1001B=5;1002=0;1003=5;'

Output:

=> {'1001'=>23,'1001B'=>5,'1002'=>0,'1003'=>5}

My solution:

def parser input
  output = {}
  input.split(';').map {|ar| ar.split('=')}.each {|id, c| output[id]=c.to_i}
  output
end
parser '1001=23;1001B=5;1002=0;1003=5;'

Any insight is appreciated.


texts: Ensuring if an object is Assignable from a class type

texts:
I am trying to ensure type in the below method. How to reduce its cognitive complexity to below 15 from the current 27? It looks more readable as it is? Is there a way to break this down to make it more readable?
Shifting some of the checks to separate method will be one way but is there a better way?
private Object ensureType(Class<?> expectedClass, Object value) {
    if (value == null)
        return null;
    Class<?> valueClass = value.getClass();
    if (expectedClass.isAssignableFrom(valueClass))
        return value;

    if (expectedClass.isAssignableFrom(String.class)) {
        return value.toString();
    }
    if (expectedClass.isAssignableFrom(Date.class)) {
        if (Number.class.isAssignableFrom(valueClass)) {
            return new Date(((Number) value).intValue());
        }
        try {
            return formatter.parse(value.toString());
        } catch (ParseException e) {
            throw new ClassCastException("argument " + value + " of type " + valueClass + " cannot be converted to "
                    + expectedClass + ":" + e);
        }
    }
    if (expectedClass.isAssignableFrom(Integer.class)) {
        if (Number.class.isAssignableFrom(valueClass))
            return ((Number) value).intValue();
        return Integer.parseInt(value.toString());
    }
    if (expectedClass.isAssignableFrom(Double.class)) {
        if (Number.class.isAssignableFrom(valueClass))
            return ((Number) value).doubleValue();
        return Double.parseDouble(value.toString());
    }
    if (expectedClass.isAssignableFrom(Float.class)) {
        if (Number.class.isAssignableFrom(valueClass))
            return ((Number) value).floatValue();
        return Float.parseFloat(value.toString());
    }
    if (expectedClass.isAssignableFrom(Short.class)) {
        if (Number.class.isAssignableFrom(valueClass))
            return ((Number) value).shortValue();
        return Short.parseShort(value.toString());
    }
    if (expectedClass.isAssignableFrom(Byte.class)) {
        if (Number.class.isAssignableFrom(valueClass))
            return ((Number) value).byteValue();
        return Byte.parseByte(value.toString());
    }
    if (expectedClass.isAssignableFrom(Long.class)) {
        if (Number.class.isAssignableFrom(valueClass))
            return ((Number) value).longValue();
        return Long.parseLong(value.toString());
    }
    if (expectedClass.isAssignableFrom(Boolean.class)) {
        return Boolean.parseBoolean(value.toString());
    }

    throw new ClassCastException(
            "argument " + value + " of type " + valueClass + " cannot be converted to " + expectedClass);
}

PS: I am moving this question from Stackoverflow to CR as it was suggested in the comments.


texts: JSON-ification and processing of data with Python

texts:
My script looks like this:

with open('toy.json', 'rb') as inpt:

    lines = [json.loads(line) for line in inpt]

    for line in lines:
        records = [item['hash'] for item in lines]
    for item in records:
        print item

What it does is read in data where each line is valid JSON, but the file as a whole is not valid JSON. The reason for that is because it's an aggregated dump from a web service.

The data looks, more or less, like this:

{"record":"value0","block":"0x79"}
{"record":"value1","block":"0x80"}

The code works, it allows me to interact with the data as JSON, but it's so slow that it's essentially useless. Is there a good way to speed up this process?


texts: Spotify API GET curl with auth using php - is this code good?

texts:
$spotify_track = '1HbcclMpw0q2WDWpdGCKdS';
    $client_id = '[my_id]';
    $client_secret = '[my_secret]';

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL,            'https://accounts.spotify.com/api/token' );
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($curl, CURLOPT_POST,           1 );
    curl_setopt($curl, CURLOPT_POSTFIELDS,     'grant_type=client_credentials' );
    curl_setopt($curl, CURLOPT_HTTPHEADER,     array('Authorization: Basic '.base64_encode($client_id.':'.$client_secret)));

    $response = curl_exec($curl);
    $token = json_decode($response)->access_token;
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
      curl_close($curl);
    } else {
          $curl = curl_init();
          curl_setopt_array($curl, array(
          CURLOPT_URL => "https://api.spotify.com/v1/audio-features/".$spotify_track,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json",
            "Authorization: Bearer ".$token,
          ),
        ));
          $track_info = curl_exec($curl);
          var_dump($track_info);
          curl_close($curl);
    }

I've modified the code suggested on stackoverflow and came up with my own solution. I successfully fetch the song information. However, I wonder if this is the proper way in handling API using curl in php. I also noticed the page reloads a little bit slower compared to without the code aforementioned.