texts:
Write a program that loops prompting for positive or zero integers of
  data type long. Then the number of digits the integer consists of (in
  decimal representation) should be printed to stdout. Entering a
  negative number immediately stops the program. Output examples: 0 has
  1 digit. 999 has 3 digits. etc.

(And I think that I am not allowed to use any finished helpful function in any C library. It is just 'plain' C coding or so. I don't know how to describe it. We haven't learned arrays, strings etc. yet)

I heard the tips and have already adjusted it in the code below, so this is v2.

Questions:

I found two solutions yet to get this exercise/program 'working'. You can see it in v2 and v3, I've posted below.

I use VS 2017, i.e. when I 'Start without debugging' (CTRL+F5), then the CMD will be closed immediately, but when I go to Project-Properties/Linker/System and choose "Console (/SUBSYSTEM:CONSOLE) at SubSystem, the CMD-window stays.

Now the comparison between v2 and v3:

I.e. when I use v2 I do not need to set a specific "SubSystem" as I told you before. I think it is because of getchar() in the while() is waiting for an input.

But I can also a while without 'EOF', which some guys on SO suggested before, so I made v3 that uses the while without getchar(). But to see my program, I have to set a "SubSystem" as I described before.

What solution is 'better'? And what would you improve?

Here is my C code v2:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  long number = 0;
  int n = 0;
  int i = 0;

  do
  {
    printf("Enter a number: ");
    fflush(stdout);
    i = scanf_s("%ld", &number);

    if(i == 1)
    {
      if (number < 0)
      {
        exit(EXIT_FAILURE);
      }
      else if (number == 0)
      {
        n = 1;
      }
      else
      {
        while (number != 0)
        {
          number /= 10;
          n++;
        }
      }
    }
    else
    {
      printf("Error - You've probably entered nothing."); //Idk what to write here, because the return-value of scanf_s can be less then 1 in this case so it must be zero,
    }                                                     //if nothing is entered and somehow the program makes it into this else.

    printf("The number you've entered has %d digits.\n\n", n);
    n = 0;

  } while (getchar() != 'EOF');

  return 0;
}

Here is my C-Code v3:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  long number = 0;
  int n = 0;
  int i = 0;
  int buffer = 0;

  do
  {
    printf("Enter a number: ");
    fflush(stdout);
    i = scanf_s("%ld", &number);

    buffer = number;

    if(i == 1)
    {
      if (number < 0)
      {
        exit(EXIT_FAILURE);
      }
      else if (number == 0)
      {
        n = 1;
      }
      else
      {
        while (number != 0)
        {
          number /= 10;
          n++;
        }
      }
    }
    else
    {
      printf("Error - You've probably entered nothing."); //Idk what to write here, because the return-value of scanf_s can be less then 1 in this case so it must be zero,
    }                                                     //if nothing is entered and somehow the program makes it into this else.

    printf("The number you've entered has %d digits.\n\n", n);
    n = 0;

  } while (buffer >= 0);

  return 0;
}


texts: Shorten my asynchronous DB queries, using async.js module

texts:
I am using

Node.js
Express.js
node-mysql.js
async.js

I am making multiple asynchronous queries to my DB, but every query relies on the previous one's results, so I decided to use the waterfall method.

Right now, my code looks like this (I simplified it with only 2 queries, but there are more):

async.waterfall([
    function(cb){
        db.query(insertQuery,values,cb);
    },
    function(results,_,cb){
       var lastInsertId = results.insertId;
       db.query(anotherInsertQuery,otherValues.concat(lastInsertId),cb);
    }
],callback);

But I found my code a bit messy, especially the function(cb) { ... } wraps.

Is there a way to get rid of those annoying function(cb){...} ?


texts: Mapping file data to environment variables

texts:
This is a follow-on to my previous question: Enforcing set environment variables

While learning more about JavaScript, node, and the bluemix environment, I have been using the loading of process environment variables as a starting point for understanding how the systems work.

This part of the system checks to see if a 'default' value exists for an environment variable. If there is a default, and the variable is not set, then the variable is set to the default's value. Default values are stored in a file in a pre-defined directory (the same folder as the script file).

After taking in the review suggestions, and some other suggestions from colleagues, I have changed the code mechanism to use a map/filter/process system on the files. This makes the code quite a bit neater.

Note: this code is a file/module in a node.js application deployed in a Bluemix host.

Again I am looking for any further insights in to how the code can be improved, and if any edge cases exist. Performance is not critical, but as I am just learning JavaScript I would appreciate any insights in to any bad practices that should be avoided so that issues are avoided later on.

In particular, I am concerned that the code is not as asynchronous as it could be, though it is a requirement that all the variables are set before the code terminates (nothing asynchronous can be incomplete).

/*jshint node:true*/

/*
 * Load various pre-determined environment variables
 * (files in this folder with .env extension).
 * Only if they have not previously been set in the environment.
 *
 * This makes the setting of Bluemix style variables quite easy.
 */

var fs = require('fs');
var path = require('path');

var env = /\.env$/;

function isEnv(fileName) {
    return fileName.match(env);
}

function processEnv(fileName) {
    var key = fileName.replace(env, "");
    if (process.env.hasOwnProperty(key)) {
        return;
    }
    var filePath = path.join(__dirname, fileName);
    var data = fs.readFileSync(filePath, 'utf8');
    process.env[key] = data;
}

fs.readdirSync(__dirname)
    .filter(isEnv)
    .forEach(processEnv);


texts: Send daily email alerts of the Bitcoin Price

texts:
My script sends daily alerts of the bitcoin price to my inbox. I am using CoinDesks API, and the following libraries:

requests==2.18.4

schedule==0.5.0

and gmail sender: https://github.com/paulchakravarti/gmail-sender

Here is my code:

"""Send daily Bitcoin price alerts to inbox."""
from time import sleep

from requests import get
import schedule
from gmail import GMail, Message

def get_bitcoin_price():
    """Get current Bitcoin price in USD using Coindesk's real-time API"""
    response = get('https://api.coindesk.com/v1/bpi/currentprice.json').json()

    return response['bpi']['USD']['rate']

def send_email(recipient: str='walid.mujahid.dev@gmail.com'):
    bitcoin_price = get_bitcoin_price()

    # enter actual password, otherwise, nothing happens.
    gmail = GMail('Price Alert <walid.mujahid.open@gmail.com>',
                  password='password')
    message = Message(f'Bitcoin is at {bitcoin_price} right now!',
                      to=recipient,
                      text=f'The current Bitcoin price is {bitcoin_price}.')

    gmail.send(message)

if __name__ == '__main__':
    schedule.every().day.at("06:30").do(send_email)

    while True:
        schedule.run_pending()
        sleep(1)


texts: Variadic templated method to extract values from a table

texts:
Is this the correct, most sane way to do variadic templates in C++?

All that this function does is to see if gate has a key, if it has, it adds it to a vector of values, if not, it default constructs an object in place.

This function belongs to a templated class called core_table where K is the key type and V the value type.

template <typename ...Args>
std::vector<V> values_at(Args const & ...args) const {
    std::initializer_list<K> input {args...};
    std::vector<V> output;
    output.reserve(input.size());
    for (auto const & e : input) {
        auto const it = gate.find(e);
        if (it != gate.end()) {
            output.emplace_back((*it)->second);
            continue;
        }
        output.emplace_back();
    }
    return output;
}

I'm asking it because all the examples of variadic templates that I saw until now made use of recursion. I'm still in a state of desbelief that I can just write std::initializer_list<K> input {k, args...}; and iterate over it, I literally didn't knew until now that this was possible - this does make variadic templates much more approachable in C++, at least for me.

So, is this a good example? How can I make it better, more optimized?

Working Example

#include <iostream>
#include <list>
#include <set>
#include <string>
#include <vector>

using std::cout;
using std::endl;

template <typename K, typename V>
class core_table {
    using value = std::pair<K, V>;
    using chain = std::list<value>;

    using iterator = typename chain::iterator;
    using const_iterator = typename chain::const_iterator;

    struct compare {
        using is_transparent = void;

        bool operator ()(const_iterator x, const_iterator y) const {
            return x->first < y->first;
        }

        bool operator ()(const_iterator x, K const & k) const {
            return x->first < k;
        }

        bool operator ()(K const & k, const_iterator y) const {
            return k < y->first;
        }
    };

    chain data;
    std::set<iterator, compare> gate;

public:
    core_table() = default;

    core_table(core_table && ct) = default;

    core_table(core_table const & ct) {
        for (auto const & e : ct) {
            push(e.first, e.second);
        }
    }

    core_table(std::initializer_list<value> il) {
        for (auto const & e : il) {
            push(e.first, e.second);
        }
    }

    template <typename Key, typename Value>
    core_table & insert(const_iterator cit, Key && k, Value && v) {
        auto const it = gate.find(k);
        if (it != gate.end()) {
            (*it)->second = std::forward<Value>(v);
            return *this;
        }
        gate.emplace(data.emplace(cit, std::forward<Key>(k), std::forward<Value>(v)));
        return *this;
    }

    template <typename Key, typename Value>
    core_table & push(Key && k, Value && v) {
        return insert(data.end(), std::forward<Key>(k), std::forward<Value>(v));
    }

    template <typename ...Args>
    std::vector<V> values_at(Args const & ...args) const {
        std::initializer_list<K> input {args...};
        std::vector<V> output;
        output.reserve(input.size());
        for (auto const & e : input) {
            auto const it = gate.find(e);
            if (it != gate.end()) {
                output.emplace_back((*it)->second);
                continue;
            }
            output.emplace_back();
        }
        return output;
    }
};

int main() {

    core_table<std::string, std::string> ct {{"a", "hello"}, {"b", "world"}};

    for (auto const & e : ct.values_at("b", "a")) {
        cout << e << endl;
    }

    return 0;
}


texts: Query SteamIDs for presence of an avatar

texts:
I have a table vanrust_corev2.tblPlayerLog that has approx 6.7k records,
I also have a table in a different database vanrust_forum.tblPlayerAvatars which has approx 6.7k records.

The purpose of this script is to pull the SteamID of players who don't have an avatar stored for them yet. However the issue here is, the execution time of this script is often 29-31 seconds which seemed a little strange to me as I'm sure there are much more sophisticated and larger databases that would not have this issue.

SELECT
*
FROM
vanrust_corev2.tblPlayerLog log
WHERE
not exists(
SELECT
a.SteamID
FROM vanrust_forum.tblPlayerAvatars a
WHERE a.Steamid = log.SteamID)

This is for the same user, just to a different database.

This script works perfectly the only issue here is execution time so I'm hoping for some pointers on where I could be going wrong.

Here is the execution plan:


texts: Generation of a  in React without  wrapping elements

texts:
In React, the render method must have one and exactly one child (although that child may have many more children). As a result, it becomes difficult to render elements that intrinsically need multiple elements as children without wrapping them in an unsemantic - and in the case of
, forbidden -
 element.

As I result, I wrote the following code to help generate a definition list from a model. I'd like to know what you guys think. Its being used in my company's application at the moment and I was wondering if there was anything I could improve on.

'use strict'
import React from 'react'
import createFragment from 'react-addons-create-fragment'
import isArray from 'lodash.isarray'

/**
 * Creates a
 element using React from the given map.
 * @param  {Map<string, any|Array<any>>} map A map of name-value pairs. The keys of the map are used as labels for definitions, so they should be legible. The values are used as the value of the definition. If there are multiple values to a definition (i.e, an array), then multiple
 elements will be created for the resultant
.
 * @return {ReactElement} A React element encapsulating the
.
 */
export default function createDefinitionList(map) {
  const labels = Object.keys(map)

  return

    {labels.map((label) => createDefinitionFragment(label, map[label]))}

}

/**
 * Creates a
 with associated
s inside of a React fragment. Fragments are keyed.
 * @param  {string} label A human-readable description of the definition
 * @param  {any|Array<any>} value a value, or a set of values. If given a set of values, multiple
 elements will be generated; these elements will be keyed using their array index.
 * @return {ReactElement} A definition list in React element form.
 */
function createDefinitionFragment(label, value) {
  return createFragment({
    definition:
{label}
,
    value: createDefinitionValueFragments(value)
  })
}

/**
 * Creates a React
 fragment for the given value. If value is an array, a fragment will be created which encapsulates all values.
 * @param  {any|Array<any>} value A singular value will return a react fragment containing just the one value encapsulated in a
. Otherwise, a Fragment will be returned that contains many
.
 * @return {ReactElement} A React fragment contain the definition values
 */
function createDefinitionValueFragments(value) {
  if (isArray(value)) {
    return value.map(function (val) {
      return
{val}

    })
  }

  return
{value}

}

Here is an example of the usage. This is basically how we use it now, except this model is retrieved via Ajax, and that's a lot of code for me to paste in a CR question. So, for sake of example, here's a finished call from one of those AJAX calls.

// Example of usage
const keyPerformanceIndicators = {
  'Calls': 0,
  'Tickets': 1,
  'Demos': [0, 10]
}

const definitionList = createDefinitionList(keyPerformanceIndicators)
React.render(definitionList, document.body)

The expected output ends up looking like this (data-reactid omitted for brevity)

Calls

0

Tickets

1

Demos

0

10

I intend to release this as a general-purpose library as this is a niche I don't think has yet been filled in React (surprisingly). I'd like any suggestions on improving it before doing so.


texts: Implementing data sync from multiple source using OOP concepts

texts:
I am writing code to implement data sync from multiple sources. I want code to be maintainable to add more data sources if required.

Here is the pattern I wish to follow:

class Source1Client:

    def get_domain(self):

        pass

    def get_endpoint(self):

        pass

    def get_headers(self):

        pass

    def make_post_request(self, url, data, headers):
        response = requests.post(url, data, headers=headers)
        return response

    def make_get_request(self, url, data):
        response = requests.get(url, data)
        return response

class Source2Client:

    def get_domain(self):

        pass

    def get_endpoint(self):

        pass

    def get_headers(self):

        pass

    def make_post_request(self, url, data, headers):
        response = requests.post(url, data, headers=headers)
        return response

    def make_get_request(self, url, data):
        response = requests.get(url, data)
        return response

    def call_stored_procedure(self, query):

        pass

    def run_query(self, query):

        pass

class BaseSync:

    def convert_data_to_serialized_objects(self, data, serializer, many=True):
        db_object, db_object_list = {}, []
        for i in data:
            for column, value in i.items():
                db_object = {**db_object, **{column: value}}
            db_object_list.append(db_object)

        schema = serializer(many=many)
        m = schema.dump(db_object_list)
        return m.data

    def get_serialized_data(
        self,source=None, query=None, proc=None, serializer=None, many=True, proc_kwargs={},
        endpoint=None, method=None, data=None, headers=None
    ):
        if source == "source1":
            client = Source1Client()
        if source == "source2":
            client = Source2Client()
        if proc:
            formatted_query = proc.format(**proc_kwargs)
            raw_data = cllient.call_stored_procedure(formatted_query)
        if query:
            raw_data = client.run_query(query)
        else:
            if method == "get":
                raw_data == client.make_get_request(endpoint, data)
            if method == "post":
                raw_data == client.make_post_request(endpoint, data, headers)

        serialized_data = self.convert_data_to_serialized_objects(
            raw_data, serializer, many=many
        )
        return serialized_data

class DataSync(BaseSync):

    def sync_source1_data():

        source1_data = self.get_serialized_data(source="source1", endpoint="source1url/path")

        # other operations

    def sync_source2_data():

        source1_data = self.get_serialized_data(source="source2", endpoint="source1url/path")

        # other operations

In my code get_serialized_data is using multiple if-else cases, which will make code dirty and hard to read if more sources are added. Also, it has too many parameters, which is against clean code principles.

What can be a better implementation to make code cleaner, maintainable and readable?


texts: Determine number of times an id was used with a domain IP

texts:
This is a slow query and the table has 50 million rows.

Table named 'log':

| id     | domainIP        |
| foo    | 158.132.34.5    |
| bob    | 128.12.244.3    |
| bob    | 128.12.244.3    |
| bob    | 19.152.134.4    |
| bob    | 168.152.34.9    |
| alice  | 178.132.64.10   |
| alice  | 188.152.214.200 |
| peter  | 208.162.36.153  |
| peter  | 208.162.36.153  |
| peter  | 208.162.36.153  |
| peter  | 198.168.94.201  |

The following query, to get the number of times id was used with each 'domainIP', and the percentage of each:

SELECT
    `log`.`id`,
    `log`.`domainIP`,
    COUNT(`log`.`domainIP`) AS "Times",
    totalsTable.Totals,
    (COUNT(`log`.`domainIP`)/totalsTable.Totals)*100 AS "Percentage"
FROM `log`
JOIN
    (
    SELECT
        `id`,
        COUNT(`domainIP`) AS Totals
    FROM `log` GROUP BY `id`
    ) AS totalsTable

ON (`log`.`id` = totalsTable.`id`)

GROUP BY `log`.`domainIP` ORDER BY `log`.`id` ASC, "Percentage"  DESC

It returns:

| id     | domainIP        | Times | Totals | Percentage
| foo    | 158.132.34.5    | 1     | 1      | 100
| bob    | 128.12.244.3    | 2     | 4      | 50
| bob    | 19.152.134.4    | 1     | 4      | 25
| bob    | 168.152.34.9    | 1     | 4      | 25
| alice  | 178.132.64.10   | 1     | 2      | 50
| alice  | 188.152.214.200 | 1     | 2      | 50
| peter  | 208.162.36.153  | 3     | 4      | 75
| peter  | 198.168.94.201  | 1     | 4      | 25

It returns the result I need, but it's unusable slow.

EDIT: Here's the table structure exported from phpmyadmin.

CREATE TABLE `log` (
  `id` varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
  `eDate` datetime DEFAULT NULL,
  `domainIP` varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
  `event` varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `log`
  ADD UNIQUE KEY `logUnique` (`id`,`eDate`,`event`),
  ADD KEY `eDate` (`eDate`),
  ADD KEY `id` (`id`,`eDate`),
  ADD KEY `event` (`id`,`eDate`,`event`);

Results of EXPLAIN query:

id | select_type | table | type  | possible_keys      | key       | key_len | ref            | rows  | Extra
1 | PRIMARY | <derived2> | ALL   | NULL               | NULL      | NULL    | NULL           | 100   | Using where; Using temporary; Using filesort
1 | PRIMARY | log        | ref   | logUnique,id,event | logUnique | 453     | totalsTable.id | 1     |
2 | DERIVED | log        | index | NULL               | id        | 459     | NULL           | 100   |


texts: Create random list of size n using 2 enums in F#

texts:
type Suit = Club = 1 | Diamond = 2 | Spade = 3 | Heart = 4
type Rank = Two = 2 | Three = 3 | Four = 4 | Five = 5 | Six = 6 | Seven = 7 | Eight = 8 | Nine = 9 | Ten = 10 | Jack = 11 | Queen = 12 | King = 13 | Ace = 14
type Card = { Suit:Suit; Rank:Rank }

let RandomHand (size:int) =
    let rndSuitIndex (rng:System.Random) = rng.Next(1, System.Enum.GetValues(typeof<Suit>).Length + 1)
    let rndRankIndex (rng:System.Random) = rng.Next(1, System.Enum.GetValues(typeof<Rank>).Length + 2)
    let randomCard (rng:System.Random) = {Suit=enum<Suit>((rndSuitIndex rng));Rank=enum<Rank>((rndRankIndex rng))}
    let cards (rng:System.Random) = List.init<Card> size (fun index -> randomCard rng)
    cards (System.Random())

As a C# programmer I'm trying to get into F#. I'm trying to create a function that returns a random list of cards. This currently works, but I'm sure this can be written MUCH shorter and cleaner.

Especially using the (rng:System.Random) 4 times feels like a stupid thing to do. Any tips?


texts: Weighted union-find with path compression in Python 3

texts:
I implemented the Union-Find data structure and would love to receive some feedback about my code. I have just begun programming and welcome any feedback or constructive criticism which would improve my code quality and professionalism.

class UnionFind:

    def __init__(self, N=1001):
        self.N = N
        self.id = [i for i in range(self.N)]
        self._rank = [1] * self.N

    def __root(self, x):
        while x != self.id[self.id[x]]:
            self.id[x] = self.id[self.id[x]]
            x = self.id[x]
        return x

    def find(self, p, q):
        return self.__root(p) == self.__root(q)

    def unite(self, p, q):
        i = self.__root(p)
        j = self.__root(q)
        if self._rank[j] > self._rank[i]:
            i, j = j, i
        self.id[j] = i
        self._rank[i] += self._rank[j]

I was working on the problem of finding cycles in undirected graphs and realised union-find could be used to efficiently solve this problem and hence, implemented it.

In this instance the input would be a list of [u, v] pairs where u and v are integers and u < v. u and v are the node numbers of the graph under consideration.

For example the input [[1, 3], [2, 3], [1, 2]] would represent

Finding cycles

The idea is as follows

  iterate over the [u, v] pairs
  if find(u, v): there exists a cycle since [u, v] belong to the same connected component
  else: unite(u, v)

For the example graph [[1, 3], [2, 3], [1, 2]] above, the code would return on [1, 2] since adding this would result in a cycle.


texts: Optimizing Node.js http get for AWS Lambda

texts:
I have the following Node.JS (runtime v6.10) executing on AWS Lambda:

exports.handler = (event, context) => {
    send(event);
};

var flota = ['CZWF64', 'DRZY65', 'DRZY66', 'FPBS85', 'FPBS86', 'FPBS87'];

function send(json) {

    var ppu = json.vehiculo.patente.replace(/[^a-z0-9]/gi, '');

    if (flota.indexOf(ppu) == -1) {
        return;
    }

    var request = require('request');
    var url = 'http://endpointUrl';

    var gps = json.gps;
    var date = new Date(gps.fecha);

    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var min = date.getMinutes();

    var month = (month < 10 ? "0" : "") + month;
    var day = (day < 10 ? "0" : "") + day;
    var hour = (hour < 10 ? "0" : "") + hour;
    var min = (min < 10 ? "0" : "") + min;

    var propertiesObject = {
        aplicacion: 1802, // ID Waypoint
        imei: ppu,
        lat: gps.lat,
        lon: gps.lon,
        alt: gps.alt,
        vel: gps.speed,
        rumbo: gps.cog,
        fecha: date.getFullYear() + month + day,
        hora: hour + min
    };

    request({
        url: url,
        qs: propertiesObject
    }, function(err, response, body) {
        var success = false;
        var error;
        if (response.statusCode != 200) {
            error = new Error('Get response: ' + response.statusCode);
        } else if (err) {
                        error = new Error('[ERROR]: ' + err);
        } else if (( body.indexOf('IMEI') != -1 &&  body.indexOf('registrada') != -1) ||  body.indexOf('ERROR') != -1) {
                        error = new Error('[ERROR]: ' + body);
        } else if ('OK'.localeCompare(body) == 0) {
            success = true;
        } else {
                       error = new Error('[ERROR]: ' + body);
        }
        if (!success) {
            throw error;
        }
    });

}

And as AWS Lambda charges it's costs per execution time, I'd like to improve the above code the fastest possible execution time.

Any tips would be appreciated.

PS: Is there an actual way to profile Node.JS running in AWS Lambda to inspect the possible bottlenecks?

First approach:

This code is no longer needed, as it has been externalized to the AWS IoT rule condition that invokes the Lambda function, so less invocations are done.

var ppu = json.vehiculo.patente.replace(/[^a-z0-9]/gi, '');

if (flota.indexOf(ppu) == -1) {
    return;
}

Second improvement

Move constants out of the function:

var request = require('request');
var url = 'http://endpointUrl';

Third approach:

Changed if/else if order as HTTP code 200 should cover most cases:

if (response.statusCode == 200) {
   if ('OK'.localeCompare(body) == 0) {
       success = true;
   } else if (err) {
       error = new Error('[Response ERROR]: ' + err);
   } else if ((body.indexOf('IMEI') != -1 && body.indexOf('registrada') != -1) || body.indexOf('ERROR') != -1) {
       error = new Error('[ERROR]: ' + body);
   } else {
       error = new Error('[Unknown ERROR]: ' + body);
   }
} else {
   error = new Error('HTTP response: ' + response.statusCode);
}


texts: Simple chart class for SVG path generation

texts:
Here is a class I have:

class Chart {
    constructor (options) {
        this.cx = options.center[0]
        this.cy = options.center[1]
        this.radius = options.radius
    }

    getTopLeftSegmentPath (perc = 1) {
        const point = (this.radius / 100) * perc
        const start = [this.cx - point, this.cy]
        const end = [this.cx, this.cy - point]

        return getPath(point, start, end)
    }

    getTopRightSegmentPath (perc = 1) {
        const point = (this.radius / 100) * perc
        const start = [this.cx, this.cy - point]
        const end = [this.cx + point, this.cy]

        return getPath(point, start, end)
    }

    getBottomLeftSegmentPath (perc = 1) {
        const point = (this.radius / 100) * perc
        const start = [this.cx, this.cy + point]
        const end = [this.cx - point, this.cy]

        return getPath(point, start, end)
    }

    getBottomRightSegmentPath (perc = 1) {
        const point = (this.radius / 100) * perc
        const start = [this.cx + point, this.cy]
        const end = [this.cx, this.cy + point]

        return getPath(point, start, end)
    }

    getPath (point, start, end) {
        const tc = xy => xy.join(',')

        return `M${tc(start)} A${point}, ${point} 0 0 1 ${tc(end)} L${this.cx}, ${this.cy} Z`
    }
}

Usage:

const chart = new Chart({ center: [200, 200], radius: 100 })

chart.getTopLeftSegmentPath(50)
chart.getTopRightSegmentPath(13)
chart.getBottomLeftSegmentPath(20)
chart.getBottomRightSegmentPath(100)

I am not sure what is the best way to optimize or simplify it.

Maybe there is a chance to have a single getPath method instead of all these getTopRightSegmentPath, getTopLeftSegmentPath, etc.


texts: Choosing the desired icon when fetching results

texts:
I'm creating a Q&A website, Something like SO. Now I need to print a "check" icon (to specify accepted answer) next to every answer. Now there are some conditions:

Is the current user logged?
Is the current user the author of the question?
Is there any accepted answer?

And some other cases.

Actually, I implemented that. But I did it using a lot of nested if-statement and seems to me really unoptimized. Now I want to know is there any better algorithm to write this code?

$results = $stm->fetchAll(PDO::FETCH_ASSOC);       // Fetching all answers

foreach ($results as $result){

    if ($_SESSION['id'] == $author_id){            // User is logged
        if($result['id'] == $AcceptedAnswerId){    // There is accepted answer
            $AcceptAnswerIcon = "

                                 ";
        } else {                                   // There isn't accepted answer
            $AcceptAnswerIcon = "

                                 ";
        }
    }

    elseif ($_SESSION['id'] !== $author_id) {      // User isn't logged
        if($result['id'] == $AcceptedAnswerId){    // There isn accepted answer
            $AcceptAnswerIcon = "";
        } else {                                   // There isn't accepted answer
            $AcceptAnswerIcon = null;
        }
    }

    else {
        $AcceptAnswerIcon = null;
    }

// ... I will use $AcceptAnswerIcon in here and mix it with answer structure

} // End of "foreach" block


texts:
As the title states. I need a different type of icon for the different nodes types. at the moment I have this hacky way of achieving that.

for (var x = 0; x <= nodeList.Count - 1; x++)
{
     if (xNode.Attributes["TYPE"].Value == "server.point.AV")
     {
         var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, AnalogImagesIndex, AnalogImagesIndex);
         inTreeNode.Nodes.Add(tNode2);
         tNode2 = inTreeNode.Nodes[x];
         AddNode(xNode, tNode2);
     }
     else if (xNode.Attributes["TYPE"].Value == "schedule.NSPDigitalSchedule")
     {
         var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, ScheduleImageIndex, ScheduleImageIndex);
         inTreeNode.Nodes.Add(tNode2);
         tNode2 = inTreeNode.Nodes[x];
         AddNode(xNode, tNode2);
     }
     else if (xNode.Attributes["TYPE"].Value == "server.point.SV")
     {
         var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, StringImageIndex, StringImageIndex);
         inTreeNode.Nodes.Add(tNode2);
         tNode2 = inTreeNode.Nodes[x];
         AddNode(xNode, tNode2);
     }
     else if (xNode.Attributes["TYPE"].Value.StartsWith("trend"))
     {
         var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, TrendImageIndex, TrendImageIndex);
         inTreeNode.Nodes.Add(tNode2);
         tNode2 = inTreeNode.Nodes[x];
             AddNode(xNode, tNode2);
         }
    .
    .
    .and so on

I do not want to add the long list of else if's.


texts:
Problem Statement

  The season for Snuke Festival has come again this year. First of all,
  Ringo will perform a ritual to summon Snuke. For the ritual, he needs
  an altar, which consists of three parts, one in each of the three
  categories: upper, middle and lower.

  He has \$N\$ parts for each of the three categories. The size of the
  \$i\$-th upper part is \$A_i\$, the size of the \$i\$-th middle part
  is \$B_i\$, and the size of the \$i\$-th lower part is \$C_i\$.

  To build an altar, the size of the middle part must be strictly
  greater than that of the upper part, and the size of the lower part
  must be strictly greater than that of the middle part. On the other
  hand, any three parts that satisfy these conditions can be combined to
  form an altar.

  How many different altars can Ringo build? Here, two altars are
  considered different when at least one of the three parts used is
  different.

  Constraints

  \$1 ≤ N ≤ 10^5\$
  \$1 ≤ A_i ≤ 10^9 (1 ≤ i ≤ N)\$
  \$1 ≤ B_i ≤ 10^9 (1 ≤ i ≤ N)\$
  \$1 ≤ C_i ≤ 10^9 (1 ≤ i ≤ N)\$

  All input values are integers.

  Time limit: 2 sec / Memory limit: 256 MB

  Input

  Input is given from Standard Input in the following format:

N
A1 … AN
B1 … BN
C1 … CN

  Output

  Print the number of different altars that Ringo can build.

Sample Input1

2
1 5
2 4
3 6

Sample Output1

3

Sample Input2

3
1 1 1
2 2 2
3 3 3

Sample Output2

27

Sample Input3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output3

87

My solution - classified as Time Limit Exceeded:

import qualified Data.Char as Char
import qualified Data.List as List
import qualified Data.Maybe as Maybe
import qualified Text.Printf as Printf
import qualified Control.Monad as Monad
import qualified Data.Array as Array

bisectLeft :: (Ord a) => a -> (Array.Array Int a) -> Int
bisectLeft n xs = bisectLeft' 0 (length xs) n xs

bisectLeft' :: (Ord a) => Int -> Int -> a -> (Array.Array Int a) -> Int
bisectLeft' lo hi n xs = if lo >= hi then lo else
                    if n <= (xs Array.! mid) then bisectLeft' lo mid n xs
                      else bisectLeft' (mid + 1) hi n xs
                    where mid = (lo + hi) `div` 2

bisectRight :: (Ord a) => a -> (Array.Array Int a) -> Int
bisectRight n xs = bisectRight' 0 (length xs) n xs

bisectRight' :: (Ord a) => Int -> Int -> a -> (Array.Array Int a) -> Int
bisectRight' lo hi n xs = if lo >= hi then hi else
                    if n < (xs Array.! mid) then bisectRight' lo mid n xs
                      else bisectRight' (mid + 1) hi n xs
                    where mid = (lo + hi) `div` 2

main :: IO ()
main = do
  n <- (read :: String -> Int) <$> getLine
  as <- (Array.listArray (0,n-1)) . List.sort . map (read :: String -> Int) . words <$> getLine
  bs <- map (read :: String -> Int) . words <$> getLine
  cs <- (Array.listArray (0,n-1)) . List.sort . map (read :: String -> Int) . words <$> getLine
  print $ foldl (+) 0 $ map (\ b -> (bisectLeft b as) * (n - bisectRight b cs)) bs

I think it takes too much time to make the array, but I cannot think of any way to make it more efficient...
How can I this code more efficiently?

Question Link


texts:
I have a file called paths.php which is the first file that loads on every page request. I read that I should make a constant of the root folder path but not for every file, but the main point why I did constants for commonly used paths: when the path changes I don't need to go around different files changing require(ROOT . "/path/to/file"); instead all I need is to change the constant value. The path constant list is building up fast and every time I open that file I feel that something is not right and something needs to be changed so I decided to ask my first question here. I heard about autoloading, but by my (small) knowledge it's only for OOP projects (please fix me if I'm wrong)? This project is done without OOP only for practice purposes, I want to make at least one non-oop page and then move on to OOP.
So here is the code, I hope to get criticism, tips, and what should I change or do differently with arguments so I can learn.
This is not a full file, just a part of it - modules/templates paths
// 04.
defined("MODULES") or define("MODULES", RESOURCES . "/modules");
defined("FUNCTIONS") or define("FUNCTIONS", MODULES . "/functions.php");
defined("AUTH") or define("AUTH", MODULES . "/authentication");
defined("AUTH_CONTROL") or define("AUTH_CONTROL", AUTH . "/auth-control.php");
defined("SIGN_UP") or define("SIGN_UP", AUTH . "/sign-up.php");
defined("SIGN_IN") or define("SIGN_IN", AUTH . "/sign-in.php");
defined("FORMS") or define("FORMS", MODULES . "/forms");
defined("FORM_CONTROL") or define("FORM_CONTROL", FORMS . "/form-control.php");
defined("FORM_ERRORS") or define("FORM_ERRORS", FORMS . "/form-errors.php");
defined("FORM_VALIDATION_FUNCTIONS") or define("FORM_VALIDATION_FUNCTIONS", FORMS . "/form-validation-functions.php");
defined("UPLOAD_IMG") or define("UPLOAD_IMG", MODULES . "/upload-image.php");

// 05.
// Templates Folder
defined("TEMPLATES") or define("TEMPLATES", RESOURCES . "/templates");
// Main Templates
defined("MAIN_TEMPLATES") or define("MAIN_TEMPLATES", TEMPLATES . "/main-templates");
defined("HEAD") or define("HEAD", MAIN_TEMPLATES . "/head.php");
defined("HEADER") or define("HEADER", MAIN_TEMPLATES . "/header.php");
defined("BANNER") or define("BANNER", MAIN_TEMPLATES . "/banner.php");
defined("FOOTER") or define("FOOTER", MAIN_TEMPLATES . "/footer.php");
defined("JS_FILES") or define("JS_FILES", MAIN_TEMPLATES . "/js-files.php");
// Content Templates
defined("CONTENT_TEMPLATES") or define("CONTENT_TEMPLATES", TEMPLATES . "/content-templates/");
// Form Templates
defined("FORM_TEMPLATES") or define("FORM_TEMPLATES", TEMPLATES . "/form-tmp");


texts: Find two unique integers in sorted array

texts:
I have to write the following method:

I have sorted array of integers and in this array every integer repeats twice, except two elements.  I have to find these two elements.

Input: 1, 1, 2, 3, 3, 4, 4, 5
Output: 2, 5

This is my solution:

private static int[] findUniques(int[] arr) {
    int[] res = new int[2];
    int start = 0;    // start can be 0 or 1 only; it indicates the position in array res
    for (int i = 0; i < arr.length-2; ) {
        if(arr[i] == arr[i+1]){
            i+=2;
            if(i == arr.length-1){
                res[1] = arr[arr.length-1];
            }
        }
        else{
            if(arr[i+1] == arr[i+2]){
                res[start] = arr[i];
                start++;
                i+=3;
                if(i == arr.length-1){
                    res[1] = arr[arr.length-1];
                }
            }
            else{
                res[start] = arr[i];
                res[start+1] = arr[i+1];
                break;
            }
        }
    }
    return res;
}

Is there a clearer way to find these two values? I don't like that I have so many if-else statements, but I don't know how to write it better.


texts:
I wrote a shell script to authenticate users connecting to an OpenVPN server. When a user attempts to connect, OpenVPN executes the script with the username and password variables sends.
The script then sends an HTTP request to a web server that either returns a status code of 202 upon success and 401 on failure.

I would like to know if the following script is secure when it comes to remote code execution and url-encoded arguments injection.

#!/bin/bash

# Change the domain to your SUPINBOT installation.
URL=http://localhost:8080/vpn/api/checkAccount
# Change to a secure random string.
API_KEY=PRIVATE_KEY

rawurlencode() {
    local string="${1}"
    local strlen=${#string}
    local encoded=""
    local pos c o

    for (( pos=0 ; pos<strlen ; pos++ )); do
        c=${string:$pos:1}
        case "$c" in
            [-_.~a-zA-Z0-9] ) o="${c}" ;;
            * )               printf -v o '%%%02x' "'$c"
        esac
        encoded+="${o}"
    done
    ENCODED=$encoded
}

rawurlencode $API_KEY;
encodedApiKey=$ENCODED;

rawurlencode $username;
encodedUsername=$ENCODED;

rawurlencode $password;
encodedPassword=$ENCODED;

responseCode=$(curl --write-out %{http_code} --silent --output /dev/null -d "apiKey=$encodedApiKey&username=$encodedUsername&password=$encodedPassword" $URL)

if [ "$responseCode" -eq "202" ]; then
    exit 0;
else
    exit 1;
fi;


texts: The base-ten value of n-base number with Python

texts:
To start off I'd like to note I am aware of int(val, base). My goal was to write a short function capable of taking in a base int, and a string defining the value to be summed.

The function rests upon the equation:

  represented_value = value * base ^ placeholder

>>> base_v = lambda base_v, num_a: sum(int(num_a[len(num_a) - ph - 1]) * base_v ** ph for ph in range(len(num_a)))
>>> base_v(10, "123")
123
>>> base_v(100, "123")
10203

I can even do something silly like:

>>> base_v(-10, "123")
83
>>>
>>> base_v(-10, ["123", "123", "123"])
11193

I am curious how this function can be made more succinct and what pitfalls it may have.


texts: Heaviest Stone algorithm time complexity

texts:
I implemented the heaviest stone algorithm, but I am not sure about its time complexity. I'm doing sort which is O(nlogn), but it' inside a while loop.
Problem:
We have a collection of stones, each stone has a positive integer weight.
Each turn, we choose the two heaviest stones and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is:
If x == y, both stones are totally destroyed;
If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.
At the end, there is at most 1 stone left.  Return the weight of this stone (or 0 if there are no stones left.)
Example:
Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.

My solution:
var lastStoneWeight = function(stones) {
    if(!stones || stones.length === 0)  return 0;
    if(stones.length === 1) return stones[0];

    stones.sort((a,b) => b-a);

    while(stones.length > 1) {
        const x = stones[0];
        const y = stones[1];
        stones.splice(0, 2);
        if(x !== y) {
            stones.push(x-y);
            if(stones.length === 1) return stones[0];
            stones.sort((a,b) => b-a);
        }
    }

    return stones.length > 0 ? stones[0] : 0;
};

By the way, is there a way to have a better performance? Maybe not sorting?
Thanks


texts:
During the test process of a device, there is a procedure that sets a device in a state in which it is ready to be tested (typically by writing default measure parameters, 3G connection parameters and any other stuff that is requested to be configurable by the devices R&D team).

The code below works but I don't like how it looks and I'd like to tidy it up.

public void TestSetupDefault()
{
    nrg.WriteFlagTest();

    NRG96.SetupConfiguration setupConfiguration = new NRG96.SetupConfiguration((int)Configuracion.GetDouble("PRIMARY_VOLTAGE", 1, ParamUnidad.SinUnidad),
        (ushort)Configuracion.GetDouble("SECONDARY_VOLTAGE", 1, ParamUnidad.SinUnidad),
        (ushort)Configuracion.GetDouble("PRIMARY_CURRENT", 1, ParamUnidad.SinUnidad),
        Configuracion.GetString("VOLTAGE_TYPES", "SIMPLES", ParamUnidad.SinUnidad),
        (NRG96.SetupConfiguration.MainDefaultScreens)Configuracion.GetDouble("MAIN_DEFAULT_SCREEN", 1, ParamUnidad.SinUnidad),
        (NRG96.SetupConfiguration.EnergyDefaultScreens)Configuracion.GetDouble("ENERGY_DEFAULT_SCREEN", 1, ParamUnidad.SinUnidad),
        Configuracion.GetString("HARMONIC_CALCULUS", "THD", ParamUnidad.SinUnidad).ToUpper() == "THD",
        (byte)Configuracion.GetDouble("BACKLIGHT_SHUTDOWN_TIME", 1, ParamUnidad.SinUnidad));

    nrg.WriteSetupConfiguration(setupConfiguration);

    NRG96.MaximumDemandConfiguration maximumDemandConfiguration = new NRG96.MaximumDemandConfiguration()
    {
        VariabletoCalculate = Configuracion.GetString("MAXIMUM_DEMAND_VARIABLE", "NO_PD", ParamUnidad.SinUnidad),
        RegisterTime = (ushort)Configuracion.GetDouble("MAXIMUM_DEMAND_REGISTER_TIME", 15)
    };

    nrg.WriteMaximumDemandConfiguration(maximumDemandConfiguration);

    NRG96.TriCalibrationFactors compoundFactors = new NRG96.TriCalibrationFactors((ushort)Configuracion.GetDouble("COMPOUND_VOLTAGE_CALIBRATION_FACTOR", 8790, ParamUnidad.SinUnidad));

    nrg.WriteCompoundvoltageFactors(compoundFactors);

}

By the way I have created the objects in 2 ways, using a constructor first, and setting properties (object initializer). Which one is more correct?

To conclude, the question I am asking is:

  How can I tidy up this code to make it more readable?

I feel like developing separate functions for each structure is like hiding this code with regions and I don't quite like that.

Keep in mind this is not about performance but about readability, I feel like there is no more code needed since what the struct does with the data or how I send it is not what I am asking to be reviewed, just how to properly display the code since that is a field I have much to work on.


texts:
I've written a simple multithreaded queue manager in JavaScript which will be seeing some pretty heavy usage at a later point in the project I'm working on. I think it's pretty solid, but I wouldn't mind hearing some other opinions on it. Any potential problems?

For convenience, here's a JSBin example of the queue.

function Queue(callback, threads, queue) {
    var q = this;

    // variables
    this.queue = queue || [];
    this.callback = callback;
    this.threads = threads || 1;

    // add element to the queue to be checked
    this.add = function (elem) {
        this.queue.push(elem);
    };

    // this should be called when the execute function has finished
    this.continue = function (delay) {
        if (!delay) {
            // if there's no delay, just call the function immediately
            if (!this.queue.length) {
                return;
            }

            this.callback.call(this, this.queue.shift());
        } else {
            // otherwise, wait until later to call the function
            setTimeout(function () {
                // anything left in the queue?
                if (!q.queue.length) {
                    return;
                }

                q.callback.call(q, q.queue.shift());
            }, delay);
        }
    };

    // begin iterating through the queue
    this.begin = function () {
        var i = this.threads;
        while (i--) {
            this.continue();
        }
    };
}

Example usage:

// create object
var q = new Queue();

// specify maximum threads
q.threads = 10;

// and a callback
q.callback = function (item) {
  // do stuff with item
  console.log(item);

  // only call this when this function has finished
  // number is the delay in ms to wait before continuing
  this.continue(1000);
};

// add some items to the queue
for (var i = 0; i < 100; i++) {
    q.add(i);
}

// begin working through the queue
q.begin();


texts: Key / Value store with type classes for formatting / parsing

texts:
I'm currently working on a Scala HTTP library, fetch, mostly because I have yet to find one that suits all my needs.

As part of this library, I need a generic key / value store, which I intend to use both for request / response headers and URL query parameters. This store uses type classes for value parsing and formatting, which makes it both type-safe and convenient to use: ETags, for example, can be manipulated through an ETag class, but I do not need the key / value store to be aware that etags even exist.

This is what I have come up with:

trait ValueReader[S, T] {
  def read(value: S): Try[T]
}

trait ValueWriter[S, T] {
  def write(value: S): Option[T]
}

class KeyValueStore[T](val values: Map[String, T] = Map()) {
  def apply[S](name: String)(implicit reader: ValueReader[T, S]): S =
    reader.read(values(name)).get

  def getOpt[S](name: String)(implicit reader: ValueReader[T, S]): Option[S] = for {
    raw    <- values.get(name)
    parsed <- reader.read(raw).toOption
  } yield parsed

  def get[S](name: String)(implicit reader: ValueReader[T, S]): Option[Try[S]] = values.get(name) map reader.read

  def set[S](name: String, value: S)(implicit writer: ValueWriter[S, T]): KeyValueStore[T] =
    writer.write(value).fold(this)(set(name, _))

  def set(name: String, value: T): KeyValueStore[T] = new KeyValueStore(values + (name -> value))

  def setIfEmpty[S](name: String, value: S)(implicit writer: ValueWriter[S, T]): KeyValueStore[T] =
      if(contains(name)) this
      else               set(name, value)

  def remove(name: String): KeyValueStore[T] =
      if(contains(name)) new KeyValueStore[T](values - name)
      else               this

  def contains(name: String): Boolean = values.contains(name)
}

See this class for sample implementations of readers and writers.

I'd be very happy for this code to be criticised and improvements to be suggested, especially in the following areas:

variance: I will probably end up making this covariant in T. Is there any reason not to do so?
I've included three "accessor" methods: apply (which throws), getOpt (safe but doesn't let the caller know what went wrong) and get (safe, granular, but heavier). Is there a standard pattern for this, or is what I'm doing considered acceptable / idiomatic?
I'm pretty sure I'm not the first person to need this kind of class, are there standard methods that are expected and I'm not providing?
what's the general consensus on "operator-like" methods, such as - for remove or + for add?


texts: Finding previous/next item in an OrderedDict

texts:
I have an OrderedDict and, given a key in it, need to find the previous and next keys accordingly.  The implementation should wrap (i.e. if the given key is the first in the dictionary, the previous key should be the last one and vice versa).

Here's what I've come up with so far (adapted from production code) which works, but seems a bit... clunky.  Any tips for improvement would be appreciated!

from collections import OrderedDict

# Create a sample
a_dict = OrderedDict()
for n in range(25):
    a_dict.update({n: n + 1})

# The currently-selected key (not a plain int in my actual production code)
selected = 0

the_keys = list(a_dict.keys())
# Only find the prev/next item if we have more than one key
if len(the_keys) <= 1:
    return

current = None
for index, key in enumerate(the_keys):
    if key == selected:
        current = index
        break

# For the majority of cases
prev = current - 1
next = current + 1

# For the edge cases (ends of the list)
if current == 0:
    prev = -1
    next = 1
elif current == len(the_keys) - 1:
    prev = current - 1
    next = 0


texts: Synchronous logging of asynchronous retrieved folder info

texts:
I started learning functional programming with JavaScript and I have the basic idea of Functors, container types and etc. I was trying to experiment with arrays but I don't know how to solve the problem of a function call that iterates over a array and returns returns another array. So the first call ends up with an array with 4 elements which are nested nested arrays of promises.
const iterate = p =>
Promise.all(p
.map(f=>({localPath:f.localPath,RelativePathFromDistLibrary:f.RelativePathFromDistLibrary}))
.map(f=>getsubfolders(f.localPath)
  .map(async _f=>({
    RelativeDistLibraryUrlAfterDomain: env.spDistLibraryAbsoluteUrlArray.slice(1).join(''),
    RelativeDistFolder:`${env.spDistFolderAbsoluteUrlArray.slice(3).join('')}${f.RelativePathFromDistLibrary}${_f}`,
    ModifiedTimeInMs: (await readStat(`${f.localPath}${_f}`)).mtimeMs
  }))
))
.then(x=>Promise.all(x.flat(Infinity)))
.then(x=>x.map(z=>console.log(z.ModifiedTimeInMs)))

The first call for iterate we pass an array of four objects. The first map just transforms each object, the second one gets all subfolders for each directory.
Example:
iterate([{RelativePathFromDistLibrary: '/src', localPath: './src'}]))

Then we map over all the results (first nested map). We wrap all in a promise.all since it will return a array of promises. We flatten the resulting array and wrap in a promise all again, then we can access the props.
Any tips that can help me better understand FP or solve the issues from this code would be super helpful.


texts: Sum an array of video lengths in JavaScript into HH:MM:SS

texts:
I am trying to learn functional programming in JavaScript, and saw a little task on Twitter that I wanted to have a go at:

var durations = ["12:38", "6:36", "9:03", "8:34", "5:02", "6:54", "13:22", "4:41", "8:36", "21:58", "3:06", "10:46", "10:13", "12:54",
"14:00", "11:03", "16:03", "10:52", "24:53", "10:03", "11:49",
"15:47", "3:19", "2:06", "5:47", "1:03", "5:29", "5:47", "26:39"];

// Above is a list of video lengths, how long are all the videos together?
// write a function that will take the above array of string durations and convert it hours/mins/seconds
// You can use any JS you want - loops/map/reduce/etc...

It would be great to have some feedback on how this could be refined further or improved.

var hours = 0, minutes = 0, seconds = 0;

function sumArrayIndex(array, i, separator) {
  return array
    .map(x => x.split(separator)
      .map(c => { return parseInt(c) })
    )
    .map(x => { return x[i]; })
    .reduce((x, y) => { return x += y }, 0);
}

function minToSec(m) {
  return m * 60
}

function tallyTime(s) {
  var hrs = 0;
  var min = Math.floor(s / 60);
  var sec = parseInt(s % 60, 16);

  if (min > 59) {
    hrs = Math.floor(min / 60);
    min = min % 60;
  }

  hours += hrs;
  minutes += min;
  seconds += sec;
}

function outputTime() {
  return hours + ':' + minutes + ':' + seconds;
}

var tally = tallyTime(minToSec(sumArrayIndex(durations, 0, ':')) + sumArrayIndex(durations, 1, ':'));

console.log(outputTime(tally));


texts: Overlapping rectangles - multidimensional arrays

texts:
Task: You are given two rectangles.For each rectangle you are given its bottom-left and top-right points. Check if they overlap. If they do, check for bottom-left and top-right points of the overlapping area.

I decided to solve this by using multidimensional arrays. Using object might make it more transaprent tho. Is there anything, that I could change (especially in terms of task approach, code bugs, code style and performance ) ?

I came up with following code:
Method wich checks if the give rectangles overlap

    public static bool Overlapping(int[][] firstRectangle, int[][] secondRectangle)
    {
        bool XOverlapping = secondRectangle[0][0] <= firstRectangle[1][0] &&
        firstRectangle[0][0] <= secondRectangle[1][0];
        bool YOverlapping = secondRectangle[0][1] <= firstRectangle[1][1] &&
        firstRectangle[0][1] <= secondRectangle[1][1];
        if (XOverlapping && YOverlapping)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

Method which checks for bottom-left and top-right points of the overlapping area.

 public static void GetOverlappingArea(int[][] firstRectangle, int[][] secondRectangle)
    {
        if (Overlapping(firstRectangle, secondRectangle))
        {
            int startXOverlapping = Math.Max(firstRectangle[0][0], secondRectangle[0][0]);
            int endXOverlapping = Math.Min(firstRectangle[1][0], secondRectangle[1][0]);
            int startYOverlapping = Math.Max(firstRectangle[0][1], secondRectangle[0][1]);
            int endYOverlapping = Math.Min(firstRectangle[1][1], secondRectangle[1][1]);
            Console.WriteLine("Left down corner: (" + startXOverlapping + " , " + startYOverlapping + "), Upper right corner: (" + endXOverlapping + " , " + endYOverlapping + ")");
        }
        else
        {
            Console.WriteLine("Rectangles do not overlap");
        }
    }

My test method

        static void Main(string[] args)
    {
        int[][] FirstRectangle = new[] { new[] { 2, 4 }, new[] { 8, 7 } };
        int[][] SecondRectangle = new[] { new[] { 5, 7 }, new[] { 10, 15 } };
        GetOverlappingArea(FirstRectangle, SecondRectangle);
    }


texts:
I'm attempting to make a stock trading game in Javascript and I've been trying to find an algorithm to generate fake stock prices. Currently, each Stock object has a minimum value, maximum value, and volatility which I will explain later. The code is as follows:

Stock.prototype.generateValue = function () {
    //mod = probability that the value will decrease next turn
    var mod = 0.5;
    if (this.value > this.max) mod = 0.8;
    if (this.value < this.min) mod = 0.2;
    var dir = (Math.random() >= mod) ? 1 : -1;

    //maxToAdd = max amount that the value can change
    var maxToAdd = (this.max - this.min) * this.volatility / 100;
    this.value += (Math.random() * maxToAdd * dir);

    //value can't be 0 -- random number from 0 to 10
    if (this.value < 0) this.value = Math.random()*10;

    //rounds to 2 decimal places
    this.value = (Math.round(100*this.value))/100;
    this.pastValues[this.pastValues.length] = this.value;
    return this.value;
};

Right now, if the current value is within the allowed range (minimum to maximum), then it has a 50% chance of going up and a 50% chance of going down. If it is out of said range, then it has an 80% chance of heading back towards the range and a 20% chance of heading farther out.

Volatility is how much the stock changes on average. Each turn, it changes by a random number between \$0\$ and \$\frac{volatility}{100} \cdot{range}\$ - that is, the volatility is the percentage of the entire range that it can change each turn (if anyone understands this and can explain it better, I would appreciate it).

The results so far have been pretty believable, but if there's any better algorithm that can be found it would be helpful. I would like something that isn't so, well, random. My algorithm stays between the range but there's not a ton of room for something unexpected to happen.

Some screenshots of results (the second is a new chart, but zoomed in on the lower values):


texts:
I was trying to wrap a Logback logger in order to provide some handy methods and already defined default keys of the logged json output and I came up with something like this.

Do you spot any problem with it, or do you have any suggestion?

public class Log {

    private Logger logger = LoggerFactory.getLogger("jsonLogger");

    private String msg;
    private long start;
    private Map<String, Object> kvs;

    private Log(String msg) {
        this.msg = msg;
        this.start = System.currentTimeMillis();
        this.kvs = new HashMap<>();
    }

    public static Log msg(String msg) {
        return new Log(msg);
    }

    public Log kv(String key, Object value) {
        kvs.put(key, value);
        return this;
    }

    public void log() {
        kvs.put("elapsed", System.currentTimeMillis() - start);
        List<StructuredArgument> args = new ArrayList<>();
        for (String k : kvs.keySet()) {
            args.add(StructuredArguments.kv(k, kvs.get(k)));
        }
        logger.info(msg, args.toArray());
    }

}

In this way you can log just a message, custom key-values and have a way to keep track of the execution between a function:

// just a message
Log.msg("my message").log();

// message with custom kv or complex objects
Log.msg("my message").kv("foo", "bar").kv("arr", new String[]{"a", "b", "c"}).log();

// elapsed time
Log logger = Log.msg("my message").kv("foo", "bar");
Thread.sleep(1240);
logger.log();


texts:
I developed a script for the company I work for in order to extract data from SAP (CJ74) and analyze the data.

One particular issue I am finding is that for any data sets which have over 1000 lines, it struggles to execute the section of code below, therefore I am searching for answers.

This code takes free hand text from cells in Column J into an array, it then splits the text up and tries to find matches to first and last names located in Column F and G.

This works functionally but it runs like a slug with data sets over 1000 lines, and potentially crashes the program if there are data sets which are at 3000 - 4000 lines and above.  Why would this be the case? Have I coded this inefficiently or is this one of VBA's gotchas?  If so would there be a better way to write this code?

Option Compare Text
Option Explicit

Sub Loader()
Dim I As Long, J As Long
Dim T As Variant
Dim match_txt As String

'** Takes text from Column J and wraps it into an array**

 For I = 2 To Range("E50000").End(xlUp).row
    typ = Range("F" & I).Value
    If typ = "" Then
        txt = Range("J" & I).Value

 '** Split array into separate lines **

        txtArray = Split(txt, " ")

 '** Check Column F for matches, if it finds a match, put the matching text into column F under the assumption that it is a first name **

        For Each T In txtArray
            For J = 2 To Range("F50000").End(xlUp).row
                If Range("F" & J).Value = T Then
                    match_txt = T
                    Range("F" & I).Value = match_txt
                End If
            Next J
        Next T

 '** Check Column G for matches, if it finds a match, put the matching text into column G under the assumption that it is a last name **

        For Each T In txtArray
            For J = 2 To Range("F50000").End(xlUp).row
                If Range("G" & J).Value = T Then
                    match_txt = T
                    Range("G" & I).Value = match_txt
                End If
            Next J
        Next T
    End If
Next I
End Sub()


texts:
I have two registration paths in my app. They are very similar and I am curious if I should simplify 4 associated actions down into two actions that handle the same amount of work.

Here are my controller actions:

def employer_registration
  @employer_signup = EmployerSignup.new
end

def employee_registration
  @employee_signup = EmployeeSignup.new
end

def create_employee
  @employee_signup = EmployeeSignup.new(params[:employee_signup])

  if @employee_signup.save
    sign_in @employee_signup.user
    redirect_to root_path, notice: 'You signed up successfully'
  else
    render action: :employee_registration
  end
end

def create_employer
  @employer_signup = EmployerSignup.new(params[:employer_signup])

  if @employer_signup.save
    sign_in @employer_signup.user
    redirect_to root_path, notice: 'You signed up successfully.'
  else
    render action: :employer_registration
  end
end

As you can see, the only difference between the two registration paths are the form object that's being used, EmployeeSignup and EmployerSignup respectively.  As you would expect, views are actually very similar too, they just display a form that's appropriate to each form object.

I have full test suits for each of these registration paths and it seems like an exceptionally large amount of duplication.

Each path handles something different, one handles employees registering, the other handles employers - so I'm tempted to leave them separate.  But, if thought of another way, they both handle one idea: User Registration.  In that case I feel it would be wiser to reconcile the different paths into one path and have a simple parameter control which form object is being used depending on the desired path.

Is my code acceptable as is or would it be more reasonable to refactor it down into one 'new' action and one 'create' action?


texts: Lock-free multi-producer multi-consumer queue

texts:
I'm looking for some feedback on my lock-free queue, based on Disruptor, mainly for any potential concurrency issues, such as where I need additional fences. It looks correct to me, and I can't seem to able to break it, but I'm only testing it on my x86 machine.

#pragma once

#include <atomic>
#include <cstdint>

template <typename T> class LockFreeMPMCQueue
{
    public:
    explicit LockFreeMPMCQueue( size_t size )
        : m_data( new T[size] ), m_size( size ), m_head_1( 0 ), m_head_2( 0 ), m_tail_1( 0 ), m_tail_2( 0 )
    {
    }

    virtual ~LockFreeMPMCQueue() { delete[] m_data; }

    // non-copyable
    LockFreeMPMCQueue( const LockFreeMPMCQueue<T>& ) = delete;
    LockFreeMPMCQueue( const LockFreeMPMCQueue<T>&& ) = delete;
    LockFreeMPMCQueue<T>& operator=( const LockFreeMPMCQueue<T>& ) = delete;
    LockFreeMPMCQueue<T>& operator=( const LockFreeMPMCQueue<T>&& ) = delete;

    bool try_enqueue( const T& value )
    {
        const std::uint64_t head = m_head_2.load( std::memory_order_relaxed );
        std::uint64_t tail = m_tail_1.load( std::memory_order_relaxed );

        const std::uint64_t count = tail - head;

        // count could be greater than size if between the reading of head, and the reading of tail, both head
        // and tail have been advanced
        if( count >= m_size )
        {
            return false;
        }

        if( !std::atomic_compare_exchange_strong_explicit(
            &m_tail_1, &tail, tail + 1, std::memory_order_relaxed, std::memory_order_relaxed ) )
        {
            return false;
        }

        m_data[tail % m_size] = value;

        while( m_tail_2.load( std::memory_order_relaxed ) != tail )
        {
            std::this_thread::yield();
        }

        // Release - read/write before can't be reordered with writes after
        // Make sure the write of the value to m_data is
        // not reordered past the write to m_tail_2
        std::atomic_thread_fence( std::memory_order_release );
        m_tail_2.store( tail + 1, std::memory_order_relaxed );

        return true;
    }

    bool try_dequeue( T& out )
    {
        const std::uint64_t tail = m_tail_2.load( std::memory_order_relaxed );
        std::uint64_t head = m_head_1.load( std::memory_order_relaxed );

        if( head >= tail )
        {
            return false;
        }

        if( !std::atomic_compare_exchange_strong_explicit(
            &m_head_1, &head, head + 1, std::memory_order_relaxed, std::memory_order_relaxed ) )
        {
            return false;
        }

        out = m_data[head % m_size];

        while( m_head_2.load( std::memory_order_relaxed ) != head )
        {
            std::this_thread::yield();
        }

        // Release - read/write before can't be reordered with writes after
        // Make sure the read of value from m_data is
        // not reordered past the write to m_head_2
        std::atomic_thread_fence( std::memory_order_release );
        m_head_2.store( head + 1, std::memory_order_relaxed );

        return true;
    }

    size_t capacity() const { return m_size; }

    private:
    T* m_data;
    size_t m_size;

    // Make sure each index is on its own cache line
    char pad1[56];
    std::atomic<std::uint64_t> m_head_1;
    char pad2[56];
    std::atomic<std::uint64_t> m_head_2;
    char pad3[56];
    std::atomic<std::uint64_t> m_tail_1;
    char pad4[56];
    std::atomic<std::uint64_t> m_tail_2;
};


texts: VBA Text Array - Scan two columns rather than one

texts:
I have some code which is designed to scan columns F and G for occurrences of words found in an array, the array containing text found in column J.

Column J contains free text from a field in SAP. Being free text, it could be "Kerry John Pub Expenses" or "CATS O/H Kerry John", or even "CATS John Kerry O/H". There is no data entry standard for this field... and this is what makes this task difficult.

Example

Here we have 4 rows of data, "John Citizen" is located in row 3, therefore the blank cells in columns F and G, row 2 can be populated with his first and last name.

I would like some suggestions as to how this code could be re-written to achieve this result more efficiently

    Sub arraycolumnmatch()
    Dim txtArray As Variant, T As Variant
    Dim I As Long, J As Long

    For I = 2 To Range("E50000").End(xlUp).row
        typ = Range("F" & I).Value
    If typ = "" Then
        txt = Range("J" & I).Value
        txtArray = Split(txt, " ")

        For Each T In txtArray
            For J = 2 To Range("G50000").End(xlUp).row
                If Range("G" & J).Value = T Then
                    match_txt = T
                    Range("G" & I).Value = match_txt
                    Exit For
                End If
            Next J
        Next T

        For Each T In txtArray
            For J = 2 To Range("F50000").End(xlUp).row
                If Range("F" & J).Value = T Then
                       match_txt = T

                 'Check for surnames which could match first names such as Mary Michael
                       If Not Range("G" & I).Value = T Then

                          Range("F" & I).Value = match_txt
                          Exit For
                       End If
                End If
            Next J
        Next T
   End If
Next I
End Sub


texts: Is this Repository Design Pattern Valid and efficient?

texts:
I am using Dapper ORM in my Data access Layer, but I think analysis of the code below doesn't depend upon any specific ORM.  What should be the single common place throughout the entire Data Access Layer to initialise the Connection String?

Base Abstract Class

abstract class Repository
    {
        protected static IDbConnection _db = new SqlConnection(ConfigurationManager.ConnectionStrings["GuideDB"].ConnectionString);

      //should I use Constructor here
    }

A Generic Repository Interface

public interface IRepository<T>
    {
        IEnumerable<T> GetAll();
        T GetItem(int id);
        T Create(T item);
        bool Update(T item);
        bool Delete(int id);
        void Reset();
    }

Concrete class

public class StateRepository :Repository, IRepository<State>
    {

        private static ICollection<State> _states = PopulateStates();// doing this valid ??
        private static object _lock = new object();

        #region Public Methods

        public IEnumerable<State> GetAll()
        {
            lock (_lock)
            {
                return _states.ToArray();
            }
        }

        public IEnumerable<State> GetAllStatesByCountryId(short id)
        {
            lock (_lock)
            {
                return _states.Where(s => s.CountryId == id).ToArray();
            }
        }

        public State GetItem(int id)
        {
            lock (_lock)
            {
                return _states.FirstOrDefault(s => s.Id == id);
            }
        }

        public State Create(State state)
        {
            lock (_lock)
            {
                if (state == null)
                {
                    throw new ArgumentNullException("state");
                }

                state.Id = (short)(_states.Any() ? _states.Max(s => s.Id) + 1 : 1);
                _states.Add(state);
                return state;
            }
        }

        public bool Update(State item)
        {
            lock (_lock)
            {
                var state = _states.FirstOrDefault(s => s.Id == item.Id);

                if (state != null)
                {
                    state.Id = item.Id;
                    state.Name = item.Name;
                    state.ShortInfo = item.ShortInfo;
                    state.CountryId = item.CountryId;

                    return true;
                }

                return false;
            }
        }

        public bool Delete(int id)
        {
            lock (_lock)
            {
                var state = _states.FirstOrDefault(s => s.Id == id);
                return _states.Remove(state);
            }
        }

        public void Reset()
        {
            lock (_lock)
            {
                _states = PopulateStates(); // doing this valid ??
            }
        }
        #endregion

        private static ICollection<State> PopulateStates()
        {
            return  _db.Query<State>("SELECT * FROM States").ToList(); //Dapper ORM Code
        }

       //How to Add a efficient Dispose method to this repository , is it really needed
    }


texts: Downloading multiple urls with aiohttp Python 3

texts:
I am trying to use aiohttp library in python to download information from url. I have about 300 000 urls. They are saved in file "my_file.txt". When I get web page, I extract pairs of a question and an answer from ask.fm. (it is a website where you can post an anonymous question to some user, and get an answer). And then I look for the next page of answers from the current user, and add it to the list new_urls to use it in the next iteration of a program.

The problem is: the speed of my program is about 1000 pages/minute. Can I somehow speed it up? Do I do something wrong?
If I change number of TCPConnections -- nothing changes.

from bs4 import BeautifulSoup
import asyncio
import aiohttp
import time

new_urls = []

k = [0]

async def fetch_url(session, url):
    async with session.get(url, timeout=1000 * 60) as response:
        f = await response.text()
        quest_answ_user = []
        try:
            soup = BeautifulSoup(f, 'html.parser')
            s = soup.find_all('article', attrs={'class': 'item streamItem streamItem-answer '})
            for el in s:
                quest = el.find('header').find('h2').text
                list_ans = el.find_all('div', attrs={'class': ['streamItem_content']})
                if len(list_ans) == 0:
                    continue
                ans = list_ans[0].text
                quest_answ_user.append([quest, ans])
            next_page = soup.select('.item-page-next')
            if len(next_page) != 0:
                new_urls.append(next_page[0].get('href'))
            print(url, len(quest_answ_user))
            k[0] += 1
            print(k)
        except Exception as e:
            print(url)
            print(e)
        return quest_answ_user

async def fetch_all_urls(session, urls):
    results = await asyncio.gather(*[fetch_url(session, url) for url in urls], return_exceptions=True)
    return results

async def get_htmls(urls, loop):
    connector = aiohttp.TCPConnector(limit=100)
    async with aiohttp.ClientSession(loop=loop, connector=connector) as session:
        list_dialogue = await fetch_all_urls(session, urls)
    return list_dialogue

def main():
    with open('./my_file.txt', 'r') as f:
        urls = f.readlines()

    cur_t = time.time()
    count = 0

    while len(urls) >= 100 and count <= 50000000:
        urls = ['https://ask.fm' + url.strip('\n') for url in urls]
        loop = asyncio.get_event_loop()
        r = loop.run_until_complete(get_htmls(urls, loop))
        mode = 'w' if count == 0 else 'a'
        with open('./dialogues4.txt', mode=mode, encoding='utf-8') as f:
            for item in r:
                try:
                    count += len(item)
                    for i in item:
                        f.write(f'{i}\n')
                except Exception as e:
                    print(e)

        print(len(new_urls))
        urls = new_urls.copy()
        new_urls.clear()
        with open('./for_continuation2.txt', 'w') as f:
            for item in urls:
                f.write(f'{item}\n')

    print(count)
    print(time.time() - cur_t)

main()


texts:
Any suggestions are welcome.

package je3.io;

import java.io.*;
import java.nio.charset.Charset;

/**
 * Created by IDEA on 30/01/15.
 */
public class CountCharsWordsLines {
    public static long[] count(String filename) throws IOException {
        long[] counts = new long[3];
        try(InputStream in = new FileInputStream(filename);
            Reader reader = new InputStreamReader(in, Charset.forName("UTF-8"));
            Reader buffer = new BufferedReader(reader)
        ) {
            int c;
            String newLineChar = System.lineSeparator();
            while((c = reader.read()) != -1) {
                char character = (char) c;
                if(character == '\n') {
                    counts[2]++;
                    counts[1]++;
                    counts[0]++;
                } else if(Character.isWhitespace(character)) {
                    counts[1]++;
                    counts[0]++;
                } else {
                    counts[0]++;
                }
            }
        }
        return counts;
    }

    public static void main(String[] args) throws IOException {
        if(args.length != 1) {
            throw new IllegalArgumentException();
        }
        long[] counts = count(args[0]);
        System.out.printf("Chars: %d; Words: %d; Lines: %d;\n", counts[0], counts[1], counts[2]);
    }
}


texts:
I found myself in a situation where it is necessary to often decorate connection strings with some kind of metadata, like an extra DB Provider attribute, read it back, and remove the decoration without damaging original content.

Here is how it looks like:

        var cs = new MsSqlConnectionStringBuilder
        { User = "dmitry", Password = "Password!" }
        .Build();

        Console.WriteLine(cs); // Password=Password!;User=dmitry;

        cs = cs.With("Provider", "MSSQL");
        Console.WriteLine(cs); // Provider=MSSQL;Password=Password!;User=dmitry;
        Console.WriteLine(cs["Provider"]); // MSSQL

        cs = cs.Excerpt("Provider"); // Password=Password!;User=dmitry;
        Console.WriteLine(cs);

Where custom MsSqlConnectionStringBuilder is:

public class MsSqlConnectionStringBuilder
{
    public string User { get; set; } = "sa";
    public string Password { get; set; }

    public ConnectionString Build() =>
        ConnectionString.Empty
            .With("User", User ?? throw new InvalidOperationException())
            .With("Password", Password ?? throw new InvalidOperationException());
}

And library code is:

public sealed class ConnectionString
{
    public static explicit operator ConnectionString(string text) => Parse(text);
    public static implicit operator string(ConnectionString connectionString) => connectionString.ToString();

    public static readonly ConnectionString Empty = new ConnectionString(ConnectionParameter.None);

    public static ConnectionString Parse(string text, string regex = "(?<name>[^=]+)=(?<value>[^;]);?") =>
        new ConnectionString(
            new Regex(regex ?? throw new ArgumentNullException(nameof(regex)))
                .Matches(text ?? throw new ArgumentNullException(nameof(text)))
                .Select(m => new ConnectionParameter(m.Groups["name"].Value, m.Groups["value"].Value)));

    ConnectionString(IEnumerable<ConnectionParameter> parameters)
    {
        Parameters = parameters.ToArray();
    }

    IEnumerable<ConnectionParameter> Parameters { get; }

    public string this[string name] => Parameters
        .FirstOrDefault(p => p.Name.ToUpper() == name.ToUpper())?.Value;

    public ConnectionString With(string name, object value) =>
        new ConnectionString(Parameters
            .Prepend(new ConnectionParameter(name, $"{value}")));

    public ConnectionString Excerpt(string name) =>
        new ConnectionString(Enumerable.Concat(
            Parameters.TakeWhile(p => p.Name.ToUpper() != name.ToUpper()),
            Parameters.SkipWhile(p => p.Name.ToUpper() != name.ToUpper()).Skip(1)));

    public override string ToString() => ToString("{0}={1};");

    public string ToString(string format) =>
        string.Join("", from p in Parameters
                        select string.Format(format, p.Name, p.Value));
}

And:

sealed class ConnectionParameter
{
    public static readonly ConnectionParameter[] None = new ConnectionParameter[0];

    internal ConnectionParameter(string name, string value)
    {
        Name = name ?? throw new ArgumentNullException(nameof(name));
        Value = value ?? throw new ArgumentNullException(nameof(value));
    }

    public string Name { get; }
    public string Value { get; }
}


texts: String trim functions with destination size limitation

texts:
3 string trimming functions:
  Trim whitespace from the beginning of a string.
  Trim whitespace from the end of a string.
  Trim whitespace from both ends of a string.

Looking to improve code for:

Correctness (tested code - but so far no holes) - especially overlapping src/dest cases.
Design
Portability (3 tried: MS, *nix, embedded)
Efficiency

Note: (unsigned char) used as unless x==EOF, isspace(x) is not defined when x < 0.

/* String trim functions
 *
 * Result is sized limited.
 * Overlapping source and destination is OK.
 * Pointer parameters are assumed to point to valid strings spaces.
 */

#include <ctype.h>
#include <stddef.h>
#include <string.h>

// Return string beginning with first non-white-space in string `src`
// Return NULL when destination is too small, `dest` unchanged.

char *trim_begin(char *dest, size_t dest_size, const char *src) {
  while (isspace((unsigned char) *src)) {
    src++;
  }
  size_t src_size = strlen(src) + 1;
  if (src_size > dest_size) {
    return NULL;
  }
  return memmove(dest, src, src_size);
}

// Return string beginning string `src` but not including any of its trailing
//   white-spaces.
// Return NULL when destination is too small, `dest` unchanged.

char *trim_end(char *dest, size_t dest_size, const char *src) {
  size_t len = strlen(src);
  while (len > 0 && isspace((unsigned char) src[len - 1])) {
    len--;
  }
  size_t src_size = len + 1;
  if (src_size > dest_size) {
    return NULL;
  }
  memmove(dest, src, len);
  dest[len] = 0;
  return dest;
}

// Return string beginning with first non-white-space in string `src` but not
//   including any of its trailing white-spaces.
// Return NULL when destination is too small, `dest` unchanged.

char *trim(char *dest, size_t dest_size, const char *src) {
  while (isspace((unsigned char) *src)) {
    src++;
  }
  return trim_end(dest, dest_size, src);
}


texts:
This is my very first complete, non-completely-trivial Haskell program, and I'm extremely unfamiliar with the language.  I was hoping that I could get some pointers on how to make it more idiomatic.  I'm especially unfamiliar with built-in functions.

I'm specifically concerned with my main function and my scrollHelp guards, as it feels like there should be a better way to do both.  Also the naming of the scroll and scrollHelp functions is arbitrary, and they might be able to be combined.

The program itself is very simple, it takes a numerical width and a string as command line parameters and transforms that string into a fake marquee.

Example:

  ./scroll 4 testing

[    ]
[   t]
[  te]
[ tes]
[test]
[esti]
[stin]
[ting]
[ing ]
[ng  ]
[g   ]
[    ]

scroll.hs

import System.Environment
import System.Exit
import System.IO

slice :: Int -> Int -> [Char] -> [Char]
slice from to xs = take to $ drop from $ xs

slice0 :: Int -> [Char] -> [Char]
slice0 = slice 0

join :: [a] -> [[a]] -> [a]
join a xss = concatMap (++ a) xss

spaces :: Int -> [Char]
spaces a = concat $ take a $ repeat " "

lead = "    ["
end = "]"

format :: [Char] -> [Char]
format xs = lead ++ xs ++ end

scroll :: Int -> [Char] -> [[Char]]
scroll a [] = [format $ spaces a]
scroll a x = scrollHelp a (spaces a ++ x ++ spaces a)

scrollHelp :: Int -> [Char] -> [[Char]]
scrollHelp a xall@(_:xs)
  | length xall >= a = (format $ slice0 a xall) : scrollHelp a xs
  | otherwise      = []
scrollHelp a _ = []

main = do
  args <- getArgs
  case args of
    [widthString, text] | [(width,_)] <- reads widthString ->
      putStrLn $ join "\n" (scroll width text)
    _ -> do
      name <- getProgName
      hPutStrLn stderr $ "usage: " ++ name ++ " <marquee width> <text to marquee>"
      exitFailure


texts: Efficient Multithreaded File Processessing in Java

texts:
This method in my code uses a producer-consumer design to read many files and process them for use in an NLP algorithm. The completion service collects files as they are processed, which means that docId != index. Given that I need to retrieve probabilities elsewhere where docID == index, I've implemented a priority queue to sort. However, I do this by sequentially retrieving documents from a list of futures and then putting them into the queue.

I know this implementation isn't great. I keep staring at the concurrentskiplistset thinking I should be able to implement it for better performance and not have these issues. I'm brand new to concurrency and Java, so I'm sure there's a lot to critique.

How can I make this multithreading more efficient?

Vectorizer:

public class Vectorizer {
public Vectorizer(){
}
public PriorityQueue<Document> readAll(File fileDir)
throws InterruptedException, ExecutionException, IOException{
    //read each file. When each file is vectorized, put it in a minibatch.
    //producer-consumer threading structure.
    int NUM_THREADS = Runtime.getRuntime().availableProcessors();
    BlockingQueue<LinkedList<String>> queue = new ArrayBlockingQueue<>(50);
    ExecutorService service = Executors.newFixedThreadPool(NUM_THREADS);
    CompletionService<Document> completionService =
               new ExecutorCompletionService<Document>(service);
    //TODO: is list the most efficient structure to handle future?
    List<Future<Document>> docs = new ArrayList<Future<Document>>();
    for (int i = 0; i < (NUM_THREADS - 1); i++) {
        docs.add(completionService.submit(new DocumentConsumer(queue)));
    }
    // Wait for ReadFile to complete
    service.submit(new ReadFile(queue, fileDir)).get();
    service.shutdownNow();  // interrupt CPUTasks
    // Wait for DocumentConsumer to complete
    service.awaitTermination(365, TimeUnit.DAYS);

    //do things with processed docs.
    //should I be doing this, though?
    PriorityQueue<Document> Documents = new PriorityQueue<Document>();
    for(Future<Document> d : docs){
        try{
        Document doc = d.get();
        Documents.add(doc);
        System.out.println(Integer.toString(doc.Cj));
        } catch(ExecutionException e) {
            e.getCause();e.printStackTrace();
        }
    }
    return Documents;
}

Document object (without compare and overload):

public class Document implements Comparator<Document>{
int docId, Cj;
HashMap<String,Integer> termTable;
public Document(int id,int Cj, HashMap<String,Integer> termMap){
    this.docId = id;
    this.Cj = Cj;

    this.termTable = termMap;
}

docId is set during file reading. I read in a file and append the count to the first line, which is then set during processing.

The rest of the code is available here.


texts: Compare elements of each list of lists to corresponding ones in different list

texts:
My goal is to reduce processing time significantly (if possible) by making this working code more efficient. Currently 50k row by 105 column data taking about overall 2 hours to process. Share of this piece is 95%.

This piece is a key part of my Python 3.6.3 script that compares two set of list of lists element by element regardless of datatype. I spent long hours but seems I reached my limits here. This is running on Windows 10.

Sorry about lots of variables. Here is description:
Ap, Bu - list of lists. Each sublist

may contain any datatype (usually String, Number, Null, Date).
1st element of list within list is always unique string.
has equal number of elements as other lists
each list in Ap has corresponding list in Bu (corresponding in here means 1st element and ID element (not necessarily other elements) of a sublist  of Ap matches that of Bu, that's considered there is corresponding match)
each sublist will contain unique ID in the same position. Meaning index of ID is same in every sublist (be it Ap or Bu).

prx - is index of a list within Ap

urx - corresponding/matching index of a list within Bu, as evidenced by urx=l_urx.index (prx)

cx - is index of an element in a single list of Au
ux - is a corresponding element index of an element in a matching list of Bu,
     as evidenced by ux = l_ux.index(cx)

rng_lenAp  - is range(len(Ap))
rng_ls - is range(individual list within Ap)

To visualize (just example):

Ap = [['egg', 12/12/2000, 10, ID1, NULL], ['goog', 23, 100, ID2,12/12/2000]]
Bu = [['goog', '3434', 100, ID2, 12/12/2000], ['egg', 12/12/2000, 45, ID1, NULL]]

for prx in rng_lenAp:
    urx = l_urx.index (prx)
    if Ap[prx][0] == Bu[urx][0]:
        for cx in rng_ls:
            ux = l_ux.index(cx)
            #If not header, non-matching cells get recorded with their current value
            if cx!=0 and Ap[prx][cx] != Bu[urx][ux]:
                output[prx].append (str(Ap[prx][cx] + '^' + str(Bu[urx][ux]))
            #Unless it is row header or ID in column, matching cells gets 'ok'
            elif cx!=0 and prx!=0 and urx !=0 and Ap[prx][cx] == Bu[urx][ux]:
                output[prx].append ('ok' +'^' + 'ok')
            # Anything else gets recorded with their current value
            else:
                output[prx].append (str(Ap[prx][cx] + '^' + str(Bu[urx][ux]))

There must a way to reduce processing time drastically. Currently the cell by cell comparison of 50k row by 100 column data to 50k row by 100 column data is takes about 2 hours. Expected under 30 min. 3.1 GHz, 4 CPUs (8196MB RAM).


texts:
What do you think of this bubble sorting function in C, for a string array? I would like to improve the readability, performance, and any good practices.

void         sort_entries(char **entries, int reverse)
{
    char     **next;
    char     **previous;
    char     *t;
    int      value;

    previous = tab;
    while (*(next = previous + 1))
    {
        value = strcasecmp(*previous, *next);
        if ((!reverse && value > 0)
            || (reverse && value < 0))
        {
            t = *previous;
            *previous = *next;
            *next = t;
            previous = entries;
            continue ;
        }
        previous++;
    }
}


texts: Update MediaWiki settings with dynamic IP address

texts:
I'm running a MediaWiki instance from a home server where the public IP address is not guaranteed to remain static. The following script is meant to run at boot time to check if the IP address has been changed an if so to update the relevant line in the LocalSettings.php file.

Here's the script:

#!/bin/bash

echo -n 'Retrieving public IP address... '
current_ip=` dig +short myip.opendns.com @resolver1.opendns.com `

if [ "$current_ip" == "" ]; then
    echo 'no response. No action taken.'; exit 1;
# check for a well formed IP adress: 000.000.000.000
elif [[ $current_ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
    echo $current_ip
else
    echo 'invalid IP address detected. No action taken'; exit 2;
fi

old_ip=`cat /var/lib/mediawiki/LocalSettings.php | grep '\$wgServer' | cut -d '"' -f2 | cut -d\/ -f3`
if [ "$old_ip" == "$current_ip" ]; then
    echo 'LocalSettings.php is already up-to-date. No action taken.'
else
    echo -n "Replacing $old_ip with $current_ip in LocalSettings.php... "
    sed -i.bak "s|\$wgServer =.*|\$wgServer = \"http://${current_ip}\";|" /var/lib/mediawiki/LocalSettings.php
    echo 'done.'
fi


texts: Hints to make Sudoku solver more Pythonic

texts:
As you will see, I am not very familiar with Python and NumPy but want to learn it.

The following code is a very basic Sudoku solver which works fine for simple examples. Although it runs, I still have the feeling it is not very Pythonic.

from numpy import *
import sys

def set(col,row,wert):
    # Set the value in field and adjust the possibility tensor
    pos[col,row,:] = 0 # the field itself
    pos[:,row,wert] = 0 # the col
    pos[col,:,wert] = 0 # the row

    # The 3x3 block
    col_start = floor(col / 3) * 3
    row_start = floor(row / 3) * 3
    pos[col_start:col_start + 3,row_start:row_start + 3,wert] = 0

    # Write down the value
    feld[col,row] = wert + 1

def read(name):
    # Read in the stuff and create field
    fid = open('in','r')
    for col in range(9):
        line = fid.readline()

        for row in range(9):
            if line[row] is not '0':
                set(row,col,int(line[row]) - 1)
    fid.closed

def write(name):
    # Write the output into a file
    fid = open("out","w")

    for col in range(0,9):
        for row in range(0,9):
            fid.write(str(feld[row,col]))
        fid.write('\n')
    fid.closed

def check():
    # Check the solution for errors
    print("%i Missing" % (81 - count_nonzero(feld)))
    neq = lambda x: count_nonzero(unique(x)) != count_nonzero(x)

    for x in range(9):
        if neq(feld[:,x]):
            print("Error in row %i" % (x))

        if neq(feld[x,:]):
            print("Error in col %i" % (x))

        col_start = floor(x / 3) * 3
        row_start = (x % 3) * 3
        if neq(feld[col_start:col_start + 3,row_start:row_start + 3]):
            print("Error in block %i / %i" % (col_start/3,row_start/3))

def only_pos_left():
    # This point is the only one that can be x
    for x in range(9):
            for y in range(9):
                if sum(pos[x,:,y]) == 1:
                    set(x,nonzero(pos[x,:,y])[0][0],y)

                if sum(pos[:,x,y]) == 1:
                    set(nonzero(pos[:,x,y])[0][0],x,y)

                col_start = floor(x / 3) * 3
                row_start = (x % 3) * 3

                if sum(pos[col_start:col_start + 3,row_start:row_start + 3,y]) == 1:
                    index = nonzero(pos[col_start:col_start + 3,row_start:row_start + 3,y]);
                    set(col_start + index[0][0],row_start + index[1][0],y)

def only_pos_for_it():
    # This point can only be x
    for x in range(9):
        for y in range(9):
            if sum(pos[x,y,:]) == 1:
                set(x,y,nonzero(pos[x,y,:])[0][0])

# main method
if __name__ == "__main__":
    # Init some variables
    feld = zeros((9,9),dtype=uint8) # field with the values were sure about
    pos = ones((9,9,9),dtype=bool_) # values which are still possible
    old_pos = 0

    read(sys.argv[0])

    # Let it run
    while any(old_pos != pos):
        old_pos = copy(pos)
        only_pos_left()
        only_pos_for_it()

    write(sys.argv[1])
    check()

Some comments on it:

I know that from NumPy import * is bad practise but its just so much faster to write.
Am I using the lambdas in a correct manner? I want it to be flexible to add more advanced solving algorithms later on.
Please tell me just anything you would change to make better use of the language (speed and readability wise).

I know that in terms of data structures this could be optimized to use something like this

class point:
    def __init__(self)
        self.val = uint8
        self.pos = zeros(9,dtype=bool_)

but then I would lose all the nice indexing that NumPy offers.


texts:
Is there a way to refactor the following Python code so that the number of lines in generation of the dictionary can be reduced?

data = frame['data'].split(';')

cat_data = {
    'xbee_address': '\x00' + frame['source_addr'],
    'state': data[0],
    'flow_rate': data[1],
    'operating_hours': data[2],
    'operating_minutes': data[3],
    'operating_seconds': data[4],
    'hours_until_maintenance': data[5],
    'number_of_cleansing': data[6],
    'serial_number': data[7],
    'item_number': data[8],
    'month_of_manufacturing': data[9],
    'year_of_manufacturing': data[10],
    'software_version': data[11],
}


texts: Finding the string from the list of array objects

texts:
TA.ServiceProxy.RoleManagement.GetSubPrivilagesResponses subPrivilagesResponses = GetSubPrivilagesFromPrivilageCodeStatic("Authoring01");
if (subPrivilagesResponses != null && subPrivilagesResponses.SubprivilagesList != null)
{
    for (int j = 0; j < subPrivilagesResponses.SubprivilagesList.Length; j++)
    {
       if (subPrivilagesResponses.SubprivilagesList[j].PrivilageCode == "CREATOR")
       {
           objTemplate.IsTagged = true;
           break;
       }
    }
}

The above code is written as

TA.ServiceProxy.RoleManagement.GetSubPrivilagesResponses subPrivilagesResponses = GetSubPrivilagesFromPrivilageCodeStatic("Authoring01");
if (subPrivilagesResponses != null && subPrivilagesResponses.SubprivilagesList != null)
{
   var item = subPrivilagesResponses.SubprivilagesList.SingleOrDefault(cd => cd.PrivilageCode == "CREATOR");
    if(item != null && item.PrivilageCode == "CREATOR")
        objTemplate.IsTagged = true;
}

In what are the different way it can be modified in order to boost performance and efficiency by making use of Lambda or LINQ


texts: Efficiency of this constantly running time processor for an HTML5 video control bar

texts:
I built a custom HTML5 video control bar, and for each second, it updates two readings:

Time Elapsed
Time Remaining

in hh:mm:ss notation, like so:

Because of how often this function runs, I want it to be as efficient as possible.

I created this JS Fiddle to test benchmarks. It runs the timer normally, until you un-comment the benchmark and comment the casual running func. The commented function call runs this function 1,000,000 times and logs the speed via the console's timing utility.

I'm looking for best practices, efficiency, and general improvements.

Here's the code. For testing, it's been modified to run in a loop or at a set interval rather than being connected to a video.

/* Video Time Processing Benchmark Snippet */

var timeProgress = $("#time-progress"),
    timeRemaining = $("#time-remaining");

// Set mock video duration for testing
var duration = 5000;

// Convert numbers to double+ digit format (09, 99, 999)
function digitize(n) {
    return n > 9 ? "" + n : "0" + n;
}

// Convert seconds to hh:mm:ss
function secondsToHMS(seconds) {
    var numhours = digitize(Math.floor(((seconds % 31536000) % 86400) / 3600));
    var numminutes = digitize(Math.floor((((seconds % 31536000) % 86400) % 3600) / 60));
    var numseconds = digitize(Math.floor((((seconds % 31536000) % 86400) % 3600) % 60));
    return numhours + ":" + numminutes + ":" + numseconds;
}

// Process the elapsed and remaining times to two hh:mm:ss format
function processTime(currentTime) {
    var value = (100 / duration) * currentTime;
    var progressHMS = secondsToHMS(currentTime);
    var remainingHMS = "-" + secondsToHMS(duration - currentTime);
    timeProgress.html(progressHMS);
    timeRemaining.html(remainingHMS);
}

// Test time via interval or loop
function testTime(type, number) {
    if (type == "interval") {

        // If a number isn't provided, the default is every second (1000)
        if (!number) var number = 1000;
        var i = 0;

        // Set interval to run function
        setInterval(function(){
            currentTime = i;
            processTime(currentTime);
            i++;

            // Clear the interval when duration is reached
            if (i > duration) clearInterval(Int);
        }, number);
    }
    else if (type == "loop"){
        for (var i = 0; i < duration; i++){
            currentTime = i;
            processTime(currentTime);
        }
    }
}

// Use Int to set time for clearInterval() targetng
var Int = testTime("interval");

//Get benchamark on functon, vs optimized versions
/*
console.time("Benchmark");
    testTime("loop", 1000000);
console.timeEnd("Benchmark");
*/