texts: Querying .NET DataSet and ignore rows that contain NULL

texts:
I've written some code to read a column of data from a dataset called AltIds.  The values are nullable.  My code is written to find all the values in the column that are not null and write them to an array.

Guid[] altIds = myDataSet.Tables[0].AsEnumerable()
  .Select(row => row.Field<Guid?>("AltId"))
  .Where(x => x.HasValue)
  .Select(x=>x.Value)
  .ToArray();

The code works, but it seems a bit complicated.  Is there a better way to do this?


texts: MySQL update with PHP; MySQLi prepared statement in loop

texts:
I'm going to develop an Android application that performs simple MySQL operations by invoking server-sided PHP scripts. PHP-MySQL communication is done by MySQLi extension. Each operation that I'd like to perform is in a separate PHP file, and in a separate class. There's an example of update.php:

<?php
require_once 'functions.php';
if (empty($_POST)) {

    $_POST['UPDATE_DATA'] = array('numer_vin' => 'SIBISIBI', 'model_pojazdu' => 'Vectra', 'marka' => 'Opel');
    $_POST['UPDATE_HEADERS'] = array('table' => 'polisy_oc', 'id' => 'numer_polisy_oc', 'id_value' => '72451');
}

class UpdateRecord
{
    protected $db_connection;

    public function __construct($db_connection) {
        $this->db_connection = $db_connection->connection;

        $this->update($_POST['UPDATE_HEADERS'], $_POST['UPDATE_DATA']);
    }

    private function update($update_headers, $update_data) { //TODO:single query
        if (in_array($update_headers['table'], $_SESSION['tables_white_list']) && in_array($update_headers['id'], $_SESSION['columns_white_list'])) {
            foreach ($update_data as $key => $value) {
                if (checkColumn($key)) {
                    $update = $this->db_connection->prepare("UPDATE {$update_headers['table']} SET $key = ? WHERE {$update_headers['id']} = ?");
                    $update->bind_param('ss', $value, $update_headers['id_value']);
                    $update->execute();
                    $update->close();
                }
            }
        }
    }
}
$db = new Database;
$update = new UpdateRecord($db);
?>

My main concern is whether Depency Injection is properly used regarding database connection (all my other scripts do the same thing).
Here's how Database class is built:

class Database
{
    public $connection;

    function __construct()
    {
        if(!isset($_SESSION)){
            session_start();
        }
        $this->connection = new mysqli('localhost', $_SESSION['login'], $_SESSION['password'], 'Ubezpieczenia_OC');

        if($this->connection->connect_error) {
            doDatabaseConnectionError();
        } else {
            $this->connection->set_charset("utf8");

            if(!isset($_SESSION['tables_white_list']) || !isset($_SESSION['columns_white_list'])){
                $_SESSION['tables_white_list'] = array('klienci', 'polisy_oc', 'parametry_oc', 'modele_samochodow', 'marki_samochodow');
                $_SESSION['columns_white_list'] = getTablesColumns($this->connection);
            }
        }
    }
}

Secondly, it was extremely hard for me to perform MySQL UPDATE query by mysqli extension's prepared statements for variable number of columns. Is above foreach loop solution a serious performance bottleneck and not elastic solution?


texts:
Exercise:
Write a prime number-test isPrime(num: Int), which for integer m >= 2 checks, if the integer is a prime number or not.
My solution:
fun main(args: Array<String>) {
    var isPrime: Boolean = false

    for (i in 2..101) {
        isPrime = isPrime(i)

        if (isPrime) {
            println("$i => ${isPrime(i)}")
        }
    }
}

fun isPrime(num: Int): Boolean {
    val upperLimit = num / 2;
    var i = 2

    while (i <= upperLimit) {
        if (num % i == 0) {
            return false
        }
        i++
    }

    return true
}

Could my solution become improved concerning efficiency?


texts: Probability of event using Central Limit Theorem + plotting results

texts:
I've been doing a Udemy course called: "Statistics for Data Science" and I decided to solve one of the homework with Python to kill two birds with one rocket #elon.

The task was:

  The team of traders under your supervision earns profits which can be
  approximated with Laplace distribution. Profits (of any trade) have a
  mean of $95.70 and a std. dev. of $1,247. Your team makes about 100
  trades every week.

  Questions:

  A. What is the probability of my team making a loss in any
  given week?
  B. What is the probability of my team making over $20,000
  in any given week?

As I just started to learn Python I would be happy for some hints and opinions.

# set up
import math
import scipy.stats as st
import matplotlib.pyplot as plt
import numpy as np

# Data
mu = 95.7                           # mean
sigma = 1247                        # standard deviation
n = 100                             # sampling size (trades here)
xcritical1 = 0                      # making a loss
xcritical2 = 20000 / n              # Earning $20k a weak by 100 trades

mu_1 = mu                           # Based on Central Limit Theorem
sigma_1 = sigma / (math.sqrt(n))    # Based on CLT

# Calc
def Z(xcritical, mu, sigma):
    return (xcritical - mu) / sigma     # Standard Score (z-value)

Z1 = Z(xcritical1, mu_1, sigma_1)
Z2 = Z(xcritical2, mu_1, sigma_1)

P1 = st.norm.cdf(Z1)                    # Cumulative Distribution Function for ND
P2 = 1 - st.norm.cdf(Z2)

print('A. Probability of making loss in any given week is', '{0:.4g}'.format(P1*100) + '%')
print('B. Probability of making over $20k in any given week is', '{0:.4g}'.format(P2*100) + '%')

# Plots
def draw_z_score(x, cond, mu, sigma, title):
    y = st.norm.pdf(x, mu, sigma)            # Probability Density function for ND
    z = x[cond]
    plt.plot(x, y)
    plt.fill_between(z, 0, st.norm.pdf(z, mu, sigma))
    plt.title(title)
    plt.text(-300, 0.0020, r'$\mu=' + str(mu_1) + ',\ \sigma=' + str(sigma_1) + '$')
    plt.show()

x = np.arange(-400, 500, 1)      # Fixed interval by experimenting
title1 = 'Probability of making loss: ' + '{0:.4g}'.format(P1*100) + '%'
title2 = 'Probability of earning more than $20k: ' + '{0:.4g}'.format(P2*100) + '%'

draw_z_score(x, x < xcritical1, mu_1, sigma_1, title1)
draw_z_score(x, x > xcritical2, mu_1, sigma_1, title2)


texts:
I'm still learning PHP OOP, so I'd like to seek for some expert advice or criticism regarding this code I created.

In this script, I created the objects I need to process the user input from a form. While the $homepage object is responsible for displaying the whole webpage.

My questions are:

Is this consider clean code?
Can I completely hide the fact that I actually include the autoload and the configuration files?
Any recommendation or tips to improve my code?

<?php

/**
*
* OOP practice
* @author Joey
*/

require_once 'config/autoload.php';
require_once 'config/config.php';

// Database credentials

$host = $config['host'];
$dbname = $config['dbname'];
$username = $config['username'];
$password = $config['password'];

// Short variable names

$fname = isset($_POST['fname']) ? clean_data($_POST['fname']) : null;
$lname = isset($_POST['lname']) ? clean_data($_POST['lname']) : null;
$your_question = isset($_POST['question']) ? clean_data($_POST['question']) : null;

try {

    // Page object

    $homepage = new Page('Questions and Answers', 'QA', '', 'You ask, we answer');

    // Author object

    $author = new Author($fname, $lname);

    // Question object

    $question = new Question($your_question, $author);

    // Database object

    $pdo = new DbConnect($host, $dbname, $username, $password);

    // Connect to the database

    $pdo->open_connection();
} catch (Exception $e) {
    echo $e->getMessage(), '
';
    exit;
} catch (PDOException $e) {
    echo $e->GetMessage(), '
';
    exit;
}

if (isset($_POST['submit'])) {
    // Insert the user input into the database

    $pdo->execute_query('INSERT INTO author (first_name, last_name) VALUES (?, ?)', array($author->getFirstName(), $author->GetLastName()));
    $pdo->close_connection();
    $homepage->content = '
Your question

Hello ' . $author->getFirstName() . ' ' . $author->getLastName() . '!' . '

';
    $homepage->content .= '
You ask: ' . $question->getQuestion() . '

';
    $homepage->content .= 'Do you want to ';
    $homepage->content .= 'Ask another question?' . ' ' . '
';
} else {
    // Display the form

    $homepage->content = <<<_END

Your question here

    <form action="{$_SERVER['PHP_SELF']}" method="post" role="form">
        Enter your first name

     <input type="text" id="fname" name="fname" size="30">

        Enter your last name

        <input type="text" id="lname" name="lname" size="30">

        What is your question?

        <textarea id="question" name="question" rows="10" cols="70"></textarea>

        <input type="submit" name="submit" value="Submit">
    </form>
_END;
}

// Display the webpage using the Page class

$homepage->display();

function clean_data($data)
{
    $data = trim($data);
    $data = strip_tags($data);
    $data = addslashes($data);
    return $data;
}


texts: Generating Calendar month information using JavaScript

texts:
I am using the below function to set month information on context passed. It is working fine, but please review it and let me know if there are any potential flaws or any improvements in this code.

/* Expects month to be in 1-12 index based. */
var monthInformation = function (year, month) {
    /* Create a date. Usually month in JS is 0-11 index based but here is a hack that can be used to calculate total days in a month */
    var date = new Date(year, month, 0);
    /* Get the total number of days in a month */
    this.totalDays = date.getDate();
    /* End day of month. Like Saturday is end of month etc. 0 means Sunday and 6 means Saturday */
    this.endDay = date.getDay();
    date.setDate(1);
    /* Start day of month. Like Saturday is start of month etc. 0 means Sunday and 6 means Saturday */
    this.startDay = date.getDay();
    /* Here we generate days for 42 cells of a Month */
    this.days = [];
    /* Here we calculate previous month dates for placeholders if starting day is not Sunday */
    var prevMonthDays = 0;
    if (this.startDay !== 0) {
        prevMonthDays = new Date(year, month - 1, 0).getDate() - this.startDay;
    }
    /* This is placeholder for next month. If month does not end on Saturday, placeholders for next days to fill other cells */
    var count = 0;
    // 42 = 7 columns * 6 rows. This is the standard number. Verify it with any standard Calendar
    for (var i = 0; i < 42; i++) {
        var day = {};
        /* So start day is not Sunday, so we can display previous month dates. For that below we identify previous month dates */
        if (i < this.startDay) {
            day.date = (prevMonthDays = prevMonthDays + 1);
            /* belong to next month dates. So, month does not end on Saturday. So here we get next month dates as placeholders */
        } else if (i > this.totalDays + (this.startDay - 1)) {
            day.date = (count = count + 1);
            /* belong to current month dates. */
        } else {
            day.date = (i - this.startDay) + 1;
        }
        this.days[this.days.length] = day.date;
    }
};

/* Usage below */
monthInformation.call(context, 4, 2015);


texts:
I followed React Official Tutorial and before implementing time travel, I wanted to add a "New Game" button, and a LockBoard method that graphically prevents further inputs.
Since I'm new to React, could anybody give me feedback on how I implemented these features?

https://codepen.io/anon/pen/bjVeJG?editors=0110

function Square(props){
    return (
      <button
        className={"square" + ( props.value !== null ? " filled" : "" ) + ( props.value === 'X' ? " red" : ( props.value === 'O' ? " cyan" : "" ) ) }
        onClick={ props.onClick }
      >
        {props.value}
      </button>
    );
}
function ResetBtn(props){
    return(
        <button
            onClick={props.onClick}
        >
        {props.value}
        </button>
    );
}
class Board extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      squares: Array(9).fill(null),
      xIsNext: true
    };
  }

  handleClick(i) {
    const squares = this.state.squares.slice();
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    squares[i] = this.state.xIsNext ? 'X' : 'O';
    this.setState({
        squares: squares,
        xIsNext : !this.state.xIsNext,
        },() => {
            const winner = calculateWinner(this.state.squares);
            if (winner)
              this.lockBoard();
        }
    );

  }

  renderSquare(i) {
    return (
        <Square
        value={ this.state.squares[i] }
        onClick={() => this.handleClick(i) }
        />
    );
  }

  handleResetClick(){
    this.setState({
        squares: Array(9).fill(null),
        xIsNext : true
    });
  }
  lockBoard(){
    const squares = this.state.squares.slice();
    for(let i in squares)
        if( squares[i] === null)
            squares[i] = "";
    this.setState({
        squares : squares
    });
  }
  render() {
    const winner = calculateWinner(this.state.squares);
    let status;
    if (winner) {
      status = 'Winner: ' + winner;
    } else {
      status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
    }

    return (

{status}

        <ResetBtn
            value="New Game"
            onClick={() => this.handleResetClick() }
        />

          {this.renderSquare(0)}
          {this.renderSquare(1)}
          {this.renderSquare(2)}

          {this.renderSquare(3)}
          {this.renderSquare(4)}
          {this.renderSquare(5)}

          {this.renderSquare(6)}
          {this.renderSquare(7)}
          {this.renderSquare(8)}

    );
  }
}

class Game extends React.Component {
  render() {
    return (

          <Board />

{/* status */}

{/* TODO */}

    );
  }
}

// ========================================

ReactDOM.render(
  <Game />,
  document.getElementById('root')
);

function calculateWinner(squares) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
      return squares[a];
    }
  }
  return null;
}


texts:
Just wanted to get a bit of feedback on this simple cache. My use case is to store this data on an Android client to avoid making a high volume of network calls for lookups.

I feel like maybe I should just extend HashMap with the small amount I've added, or change up my thinking and do some type of static implementation?

public class DataCache<K, V> {
    private final long mDefaultTimeout = 15;
    private long mTimeout = 0;
    private HashMap<K, DataValue<V>> dataMap;

    DataCache() {
        dataMap = new HashMap<K, DataValue<V>>();
        mTimeout = mDefaultTimeout;
    }

    DataCache(long timeoutMinutes) {
        dataMap = new HashMap<K, DataValue<V>>();
        mTimeout = timeoutMinutes;
    }

    public void put(K key, V value) {
        dataMap.put(key, new DataValue<V>(value));
    }

    public V get(K key) {
        DataValue<V> data = dataMap.get(key);
        V result = null;
        if (data != null) {
            long diff = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - data.insertTime);
            if (diff >= mTimeout) {
                dataMap.remove(key);
                data.value = null;
            }
            result = data.value;
        }
        return result;
    }

    public void setTimeout(long minutes) {
        mTimeout = minutes;
    }

    public long getTimeout() {
        return mDefaultTimeout;
    }

    private final class DataValue<T> {
        public T value;
        public long insertTime;

        DataValue(T value) {
            this.value = value;
            insertTime = System.currentTimeMillis();
            }
        }
    }
}


texts: How do I optimize memoization in order to find longest palindromic substring?

texts:
I want to find the longest palindromic substring using dynamic programming in Python3. The strings can be as large as this string.

I have seen other questions on this problem that successfully solve it but I am unable to improve my own code following their strategy.

This, approach 3 is the strategy that I want to implement. Even then, my code is not time-efficient.

My code is:

class Solution:

    def __init__(self):
        self.not_pal = {}
        self.pals = {}

    def check_pal(self, s, i, j):
        if len(s) == 0:
            return False
        if i == j:
            return True
        if i == j-1:
            if s[i] == s[j]:
                return True
            return False
        if s[i] == s[j]:
            return self.check_pal(s, i+1, j-1)
        return False

    def longestPalindrome(self, s):
        max_pal_str = ''
        length = len(s)
        for i in range(len(s)):
            if length-i <= len(max_pal_str):
                continue
            for j in range(len(s)-1, -1, -1):
                if self.not_pal.get(s[i: j+1]):
                    continue
                if self.pals.get(s[i: j+1]):
                    continue
                if len(s[i: j+1]) <= len(max_pal_str):
                    continue
                if self.check_pal(s, i, j):
                    self.pals[s[i:j+1]] = 1
                    if len(s[i: j+1]) > len(max_pal_str):
                        max_pal_str = s[i: j+1]
                else:
                    self.not_pal[s[i:j+1]] = 1
        return max_pal_str

I am unable to improve this code further. I expect it to be faster than brute force codes which check all the permutations and combinations without any memoization. It is failing with TLE on Leetcode.


texts: Implementing a cross-browser AJAX utility as an exercise

texts:
I am currently learning JavaScript and created this cross-browser AJAX utility as a learning exercise. It was not meant to be used in production code, and I just tried to cover only the cases that I'm aware of, so there might be a lot of edge cases that are not covered.

I'd be happy if you could check it and let me know how it looks and give me suggestions on how to improve it:

var ajax = {
    request: function(url, requestType, callback, queryString) {
        var ids = ['MSXML2.XMLHTTP.3.0',
                   'MSXML2.XMLHTTP',
                   'Microsoft.XMLHTTP'],
            xhr;

        if (typeof window.XMLHttpRequest === 'function') {
            xhr = new XMLHttpRequest();
        } else { // For IE versions below 7
            for (var i = 0; i < ids.length; i++) {
                try {
                    xhr = new ActiveXObject(ids[i]);
                    break;
                } catch(e) {}
            }
        }
        // Do I really need to put the callback in a
        // closure, as the xhr object gets recreated
        // each time ajax.request() is called?
        xhr.onreadystatechange = (function(myXhr) {
            return function() {
                callback(myXhr);
            };
        })(xhr);
        xhr.open(requestType, url, true);
        if (requestType.toUpperCase() === 'GET') {
            xhr.send('');
        } else if (requestType.toUpperCase() === 'POST') {
            xhr.send(queryString);
        }

    }
}

P.S.: This exercise is from the book "Object-Oriented JavaScript" by Stoyan Stefanov.


texts: Python app-level document/record locking (eg. for MongoDB)

texts:
MongoDB does not provide means to lock a document (like SELECT FOR UPDATE in RDBMS).

There is a recommended approach to Isolate Sequence of Operations, which is sometimes called optimistic locking, but there are scenarios when this approach seems to be overly complicated or even inefficient (that is why pessimistic locks are still in use).

Task

I am trying to implement generic (can be used with various databases) document (or record) locking at application level. Assuming I have only one application working with collection (or table). And my application is multi-threaded.

Usage scenario:

with DocumentLock(doc_id):
  # make sure no other thread interferes
  doc=db.doc_collection.find_one(doc_id)
  ... # analyse and update the document
  db.doc_collection.save(doc)

Solution

Here is my DocumentLock class:

class DocumentLock(object):

  _lock=RLock() # protects access to _locks dictionary
  _locks=WeakValueDictionary()

  def __init__(self, doc_id):
    self.doc_id=doc_id

  def __enter__(self):
    with self._lock:
      self.lock=self._locks.get(self.doc_id)
      if self.lock is None:
        self._locks[self.doc_id]=self.lock=RLock()
    self.lock.acquire()

  def __exit__(self, exc_type, exc_value, traceback):
    self.lock.release()
    self.lock=None # make available for garbage collection

So basically I have a dictionary of RLocks, one RLock per accessed document. I use weak references to get rid of unused locks.

DocumentLock can be subclassed for every collection (or table) that needs record locking.

Question

This all seems to work (although not tested under concurrency) but I am publishing it here for review and advise. Do you see any weaknesses or mistakes in this code? Is there a better approach to do this kind of things in Python?


texts:
I finally got around to redoing my Temperature conversion program. I incorporated many elements of everyone's comments, and am uploading the finished program. Any new, or problems that I missed? Maybe a few very late here where I am working. Main focus of this redo was keeping my functions short and single purposed.

#include <stdio.h>

void inter_face();
void welcome();
void option_prompt();
void input_temp_prompt();
int get_option();
float get_input_temp();
void display_F_to_C(float);
void display_C_to_F(float);
void display_off();
void display_error();
float F_to_C(float);
float C_to_F(float);

enum { F_TO_C = 1, C_TO_F = 2, OFF = 3 } option_type;

int main()
{
    inter_face();
    getchar();
}

void inter_face()
{
    int option = -1;
    float input_temp = 0.0;
    welcome();
    while (option != OFF) {

        option_prompt();
        option = get_option();

        if (option == F_TO_C || option == C_TO_F) {
            input_temp_prompt();
            input_temp = get_input_temp();
        }
        switch (option) {
        case F_TO_C:
            display_F_to_C(F_to_C(input_temp));
            break;
        case C_TO_F:
            display_C_to_F(C_to_F(input_temp));
            break;
        case OFF:
            display_off();
            break;
        default:
            display_error();
        }
     }
 }

 void welcome()
{
    printf("Temperature conversion Calculator!!\nPlease select an option:");
    printf("\n1.) F to C\t2.) C to F\t3.) off\n\n");
}

void option_prompt()
{
    printf("Option: ");
}

void input_temp_prompt()
{
     printf("Temp: ");
}

float get_input_temp()
{
    float input_temp = 0;
    scanf_s("%f", &input_temp);
    return input_temp;
}

int get_option()
{
    int option;
    scanf_s("%d", &option);
    return option;
}

void display_F_to_C(float converted_temp)
{
     printf("Celsius: %f\n", converted_temp);
}

void display_C_to_F(float converted_temp)
{
    printf("Fahrenheit: %f\n", converted_temp);
}

void display_off()
{
    printf("OFF\n");
}

void display_error()
{
    printf("Incorrect input, try again\n");
}

float F_to_C(float input_temp)
{
      return (5.0 / 9.0) * (input_temp - 32);
}

float C_to_F(float input_temp)
{
    return (9.0 / 5.0) * (input_temp)+32;
}


texts: Re-arranging data on a worksheet based on contents of Column A

texts:
The following Macro sorts through a worksheet, copying and removing lines based on what appears in column A. If ("A" & i) is not a number, it triggers and moves the data to the appropriate place. The script at the moment is very slow, however. Is there an easier way to achieve this?

Sub FixAbstracts()
Application.ScreenUpdating = False
Dim Rng As Range
Dim i As Long
i = 2
Dim row As Range
Dim lastRow As Long
lastRow = Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count

While i <= lastRow
    Set Rng = Range("A" & i)
    Set row = ActiveSheet.Rows(i)
    lastRow = Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
    If Application.WorksheetFunction.IsNumber(Rng.Value) = True Then
        i = i + 1

    ElseIf IsNumeric(Rng.Value) = False And Application.WorksheetFunction.CountA(Rng) = 1 Then
        Rng.Copy Destination:=Rng.Offset(-1, 13)
        Rng.Delete Shift:=xlToLeft
    ElseIf IsEmpty(Rng) = True And Application.CountA(row) > 0 Then
        Rng.Delete Shift:=xlToLeft
    ElseIf IsEmpty(Rng) = True And Application.CountA(row) = 0 Then
        row.Delete

    Else: Stop
    End If
Wend

End Sub


texts:
I recently was reading an article labeled "Modern C++ Singleton Template". The proposed solution used C++11 feature of Magic Statics (N2660). I wanted to adapt it for an own project in
"freestanding" (fno-hosted) environment.
My code looks as follows:
Singleton.hpp
namespace std
{
    template <typename T>
    class Singleton
    {
        public:
            // = initialization and termination methods
            Singleton( const Singleton& )   = delete;   // copy constructor
            Singleton(       Singleton&& )  = delete;   // move constructor
            Singleton& operator=( const Singleton& ) = delete;  // assignment operator
            Singleton& operator=(       Singleton&&) = delete;  // copy move operator
            // = accessor methods.
            static T& getInstance();
        protected:
             Singleton() {};                            // default constructor
            ~Singleton() {};                            // default destructor
             struct Token {};
        private:
    }; // end template class T

    template <typename T>
    T& Singleton<T>::getInstance()
    {
        static T    __singleInstance( Token{} );
        return( __singleInstance );
    } // end public method 'getInstance()'
} // end namespace std

Test.hpp
#include "libs/libc++/singleton.hpp"        // declaration of template class 'Singleton'

class Test final : public TestSingelton<Test>
{
    public:
    Test(TestToken);
   ~Test();

    void use();
};

Test.cpp
Test::Test(TestToken)
{
    logTraceEvent_m( loging to file );
}

Test::~Test()
{
    logTraceEvent_m( loging to file );
}

void
Test::use()
{
    logTraceEvent_m( loging to file );
}

Problem
In hosted environment (Linux, GCC 7.5.0) it works as presented in the article refereed to above.
The static variable is initialized only once; the constructor of the derived class is called only once.
Entering main()
Entering a()
constructed
in use
Entering b()
in use
Leaving main()
destructed

However, in my "-ffreestandig" environment it is not working.
"freestanding" = kernel without any standard headers; without any libraries!
Static variable ("__singleInstance") is initialized on every call to "Test::getInstance()".
______ TRACE: constructor = Test::Test(TestSingelton<Test>::TestToken), ...
______ TRACE: use = void Test::use(), file = test/Test.cpp, line = 37.
______ TRACE: constructor = Test::Test(TestSingelton<Test>::TestToken), ...
______ TRACE: use = void Test::use(), file = test/Test.cpp, line = 37.

What I'm doing wrong?
Is there any advice in which direction I have to investigate?

compiler options?
-W -Wall -pedantic -ffreestanding -std=gnu++17 -fno-PIC -fno-exceptions -fno-rtti
-fno-use-cxa-atexit -march=native -m64 -mcmodel=kernel -mno-red-zone -mno-sse3 -mno-ssse3
-mno-sse4.1 -mno-sse4.2 -mno-sse4 -mno-sse4a -mno-3dnow -mno-avx -mno-avx2
runtime environment, that is missing something


texts: JavaScript: Simulating the process of removing multiple videos at once from a YouTube playlist

texts:
I've written the code below with what little knowledge I have of JavaScript at the moment.

The code simulates the process of removing multiple videos at once from a playlist like the one you see on YouTube. As it stands now, the code removes a total of four videos (each positioned at the index 0, 2, 5, and 7), using the combination of the for loop and splice methods. But here are a couple of things I can't figure out on my own:

1. Practically speaking, videos to be removed, and by extension, their corresponding indexes in an array should not be predetermined, but rather be selected by the user. In other words, the playlistIndex array should be empty at first, and index numbers should be added to it one by one as the user ticks checkboxes.

2. Even after I have ironed out the first problem mentioned above, the current code will remove a selected video immediately after one index number is pushed to the playlistIndex array. So I have to make it so that the code does not start to remove videos until the user has selected every video they want to have removed.

3. Lastly, before setting the removal process in motion, the code needs to have the user confirm their decision. Ideally, the user should be asked to press "yes" or "no" in a pop-up that goes something like "You are about to remove 7 videos. Are you sure you want to proceed?"

var playlist = ["video1", "video2", "video3", "video4", "video5", "video6", "video7", "video8", "video9", "video10"];

var playlistIndex = [0, 2, 5, 7];

var bin = [];

for (var i = playlistIndex.length - 1; i >= 0; i--) {
    bin.unshift(playlist.splice(playlistIndex[i], 1));
}

for (var j = bin.length -1; j >= 0; j--) {
    if (bin[j].length === 0) {
        bin.splice(j, 1);
    }
}

if (playlist.length === 0) {
    alert("No videos in the playlist.");
}
else {
    alert(playlist.length+" video(s) remain in the playlist: "+playlist.join(", ")+".");
}

bin = [].concat.apply([], bin);

alert(bin.length+" video(s) have been removed: "+bin.join(", ")+".");


texts:
I was just trying Project Euler problem 50.

  The prime 41, can be written as the sum of six consecutive primes: 41
  = 2 + 3 + 5 + 7 + 11 + 13

  This is the longest sum of consecutive primes that adds to a prime
  below one-hundred.

  The longest sum of consecutive primes below one-thousand that adds to
  a prime, contains 21 terms, and is equal to 953.

  Which prime, below one-million, can be written as the sum of the most
  consecutive primes?

Here's my code for the same in Python:

LIMIT = 1000000

x = [1] * (LIMIT + 1)
x[0] = x[1] = 0

primes = []
length = 0

for i in range(2, LIMIT):
    if x[i]:
        primes.append(i)
        length += 1

        for j in range(i * i, LIMIT + 1, i):
            x[j] = 0

s = 0
prev = -1
cnt = -1

for i in range(length):
    s = 0

    for j in range(i, length):
        s += primes[j]

        if s > LIMIT:
            break

        if x[s] and cnt < j - i + 1:
            cnt = j - i + 1
            prev = s

print(prev)

I just create a list of primes using Sieve of Eratosthenes. For each prime, I'm just finding all the consecutive primes and their sums to get the answer.

This currently takes about one second to run.

Is there a faster, better, and neater way to solve this?

Thank you for your help!

EDIT:

I see there are 2 votes to close my program as it Lacks concrete context. Can you please explain what I should do to add concrete context?


texts: Generalize code that populates MFC list control?

texts:
I'm using the Model View Presenter (MVP) pattern in MFC that's not hugely relevant to my question but I mention it for context.  I have some code that is designed to populate a list control with data from a list.

I have two list boxes which have almost the same code except the type of list is different and the function used to actually put items in the list control is different.  So my questions are:

How can I genericize this member function so that I could pass it an arbitrary list of objects, and a population function (i.e., what AddCustomerToListControl is)?
Besides genericizing it are there other improvements to be made?

(Note the calls to SetTopIndex are so that the scroll position is remembered.)

void MyDialog::SetCustomerList(const std::list<Customer*>& customers)
{
    customersListCtrl_.SetRedraw(FALSE);

    int selectedItemIndex = customersListCtrl_.GetNextItem(-1, LVNI_SELECTED);
    int topIndex = customersListCtrl_.GetTopIndex();

    customersListCtrl_.DeleteAllItems();

    if(customers.size() > 0)
    {
        for(std::list<Customer*>::const_iterator it = customers.begin();
                it != customers.end() ; it ++)
        {
            AddCustomerToListControl(*it);
        }
        customersListCtrl_.SetItemState(selectedItemIndex,LVIS_SELECTED, LVIS_SELECTED);
    }

    customersListCtrl_.SetTopIndex(topIndex);
    customersListCtrl_.SetRedraw(TRUE);
}

UPDATE

Another detail is the the function AddCustomerToListControl above needs a reference to the listControl in order to add items to it (e.g., call InsertItem and SetItemText)


texts: Matching entities in SQL Server to replace iterative Python script

texts:
I have a SQL (MS SQL Server) database of ~22 million companies (called target_db in my code). For example:

+-----------------------+----------------+-----------+
|     CompanyName       |    ReFcode     | ID_number |
+-----------------------+----------------+-----------+
| Mercedes Benz Limited | Germany        |     12345 |
| Apple Corporation     | United States  |     67899 |
| Aunt Mary Butcher     | United Kingdom |     56789 |
+-----------------------+----------------+-----------+

Then, I have another list of companies (called input in my example) and I would like to assign ID_number based on approximate company name match. The size of my sample is 1000 companies but can be much more.

+--------------------+----------------+
|      name          |    ReFcode     |
+--------------------+----------------+
| Mercedes Benz Ltd. | Germany        |
| Apple Corp.        | United States  |
| Butcher Aunt Mary  | United Kingdom |
| Volkswagen Gmbh    | Germany        |
+--------------------+----------------+

I wrote a script in Python that does the following for each of the companies in my sample:

Connect to the database and filter the target_db to only get the names that start with the same 3 letters, have similar length (+- 7 characters) and Soundex DIFFERENCE is 4
Calculate levenshtein with all possible matches (using fuzzywuzzy library)
Choose the match with the highest similarity

This worked mostly fine and accomplished the task in around 6 minutes. I did some profiling and noticed that 5 minutes and 30 seconds was spent on connecting to the DB (in every iteration of the loop), running query and fetching the results. The rest was calculating the similarity. Which I found quite surprising.

I thought that I could save time and improve performance by doing the process entirely in SQL, so I started writing this query:

SELECT distinct input.name,
                target_db.CompanyName,
                input.Country,
                input.ReFcode,
                [dbo].[edit_distance](input.name, target_db.CompanyName) as levenshtein
FROM dbo.Sample as input
JOIN dbo.Company as target_db
on
    (input.ReFcode = target_db.Refcode and
    LEFT(input.name, 3) = LEFT(target_db.CompanyName, 3) and
    SOUNDEX(input.name)=SOUNDEX(target_db.CompanyName))
WHERE ABS( LEN(input.name) - LEN(target_db.CompanyName)) <= 7

ORDER BY levenshtein asc

Then I would take the match with the lowest EditDistance (Levenshtein) below a certain threshold. This works but the process now is taking almost 30 minutes. What am I doing wrong? Any suggestions on how to improve it?


texts: Calculating the percentage on Python 2.7

texts:
An attack with magnitude M will be successful with a chance of (100-M)%. That is, higher magnitude means higher risk of missing it. For instance, if M is 30, the chance of succeeding would be 70%, whereas, if M is 10, the chance of succeeding would be 90%. If an attack with magnitude M is successful, the attacked hero's health points will decrease by M. If the user writes more than
50 or less than 1, the game should warn the player, and re-ask for another attack magnitude as below.

import random

# ---

char1 = raw_input("Player One: ")

while char1 == "":
    char1 = raw_input("Please enter your name: ")

char2 = raw_input("Player two: ")
while char2 == char1:
    char2 = raw_input(char1 + " name is taken, please choose another name: ")
while char2 == "":
    char2 = raw_input("Please enter your name: ")
print char1, "and", char2, "welcome to the game."

# ---

health1 = 50
health2 = 50

print char1, '|' * health1
print char2, '|' * health2

toss = random.randint(0, 1)

if toss == 0:
    print char1, "will start the game"
else:
    print char2, "will start the game"

# ---

while health1 > 0 and health2 > 0:
    if toss == 0:
        n = input(char1 + " select n force: ")
        health2 -= n
        print char2, '|' * health2 + char1,'|' * health1
        toss = 1 # change player
    else:
        n = input(char2 + " select n force: ")
        health1 -= n
        print char1, '|' * health1 + char2,'|' * health2
        toss = 0 # change player

# ---

if health1 > 0:
    print char2, 'wins'
else:
    print char1, 'wins'


texts:
I have made a simple binary search algorithm with the assumptions that the input array is sorted and has only unique values.

def binary_search(input_array, value):
    """
    Iterative binary search in sorted array of unique int values.

    :param input_array: Sorted array of unique int values.
    :type input_array: list<int>
    :param value: The int value to search in input_array
    :type value: int
    :return: The index of value or -1 if it doesn't exist
    :rtype: int
    """
    result = -1
    previous_index_down = 0
    previous_index_up = len(input_array)
    current_index = int(math.floor(len(input_array[previous_index_down+1:previous_index_up]) / 2))
    while True:
        if input_array[current_index] == value:
            result = current_index
            break
        elif current_index == previous_index_down or current_index == previous_index_up:
            break
        elif input_array[current_index] > value:
            previous_index_up = current_index
            current_index = current_index - int(math.ceil(len(input_array[previous_index_down:previous_index_up+1]) / 2))
        elif input_array[current_index] < value:
            previous_index_down = current_index
            current_index = current_index + 1 + int(math.floor(len(input_array[previous_index_down+1:previous_index_up]) / 2))

        if previous_index_down == len(input_array) - 1:
            break
    return result

For test I used various inputs like the following:

test_list = [1,3,9,11,15,19,29]
test_val1 = 25
test_val2 = 15
test_val3 = 29
print binary_search(test_list, test_val1)
print binary_search(test_list, test_val2)
print binary_search(test_list, test_val3)

The results appear to be correct, but I am wondering if the implementation can be improved in terms of style and length. Also I don't know if there are inputs that may make it fail.


texts: Checking whether strings are permutations of each other in Swift

texts:
I'm solving this problem on Hacker Earth, just for practice. The exercise is to determine whether two given equal-length strings are permutations of each other.

Except for this one test case, which has 100 very long strings to compare, all other test cases are fine. But that one is taking too long to execute. What are possible optimizations that can be done?

Also it would be great if you can tell me what expensive operations I have used.

import Foundation;

var response:String = readLine()!

func createDictionary(from arrayOfCharacters: [Character], _ charCount: Int) -> Dictionary<Character, Int>{
    var dict : [Character: Int] = [:];
    for index in 0...charCount-1{
        if(dict.index(forKey: arrayOfCharacters[index]) != nil){
            dict[arrayOfCharacters[index]] = dict[arrayOfCharacters[index]]!+1;
        }
        else{
            dict[arrayOfCharacters[index]] = 1;
        }
    }
    return dict;
}

func checkEqual(_ x:String) -> Bool{
    var flag = false;
    var names = x.components(separatedBy: " ");
    //let charCount = names[0].characters.count;
    let charCount = (x.characters.count-1)/2;
    var dictionaryOne: [Character:Int] = [:];
    var dictionaryTwo: [Character:Int] = [:];

    if(names[1].characters.count == charCount){
        var charsOfStringOne = Array(names[0].characters);
        var charsOfStringTwo = Array(names[1].characters);

        //print(charsOfStringOne);
        //print(charsOfStringTwo);

        dictionaryOne = createDictionary(from: charsOfStringOne, charCount);
        dictionaryTwo = createDictionary(from: charsOfStringTwo, charCount);

        for char in "abcdefghijklmnopqrstuvwxyz".characters {
            if(dictionaryOne[char] != dictionaryTwo[char]){
                return false;
            }

        }

        if(dictionaryOne != dictionaryTwo){
            return false;
        }else{
            flag=true;
        }

        return flag;
    }else{
        return flag;
    }
}

var debug = false;
var count = Int(response)!
 for _ in 1...count {
    var line = readLine()!;
    if(checkEqual(line)){
        if(debug){
            print(line, "YES")
        }
        else {
            print("YES");
        }
    }else{
        if(debug){
            print(line, "NO")
        }
        else {
            print("NO");
        }
    }
}


texts: Wrapping every expression with % 2 ** 32 to get a C-like behavior

texts:
I'm trying to port the following C loop to Python:

for (int32_t i = 32; --i >= 0;) {
    v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
    sum -= delta;
    v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum >> 11 & 3]);
}

The problem is that the these integers are 32-bit unsigned and the code above relies on integer wrapping. My first attempt was to introduce the U32 class (see here), but after porting this code to Python 3, the solution got so ugly (see here) that I started to looking for other ones. My best idea at the moment is to wrap every expression with % 2 ** 32, so that it wraps around. Here's how this looks:

for _ in range(32):
    v0 = (
        v0
        +
        (
            (
                (v1 << 4) % 2**32 ^ (v1 >> 5) % 2**32
            ) % 2**32
            +
            v1 ^ (sum_ + k[sum_ & 3]) % 2**32
        ) % 2**32
    ) % 2**32

    sum_ = (sum_ - delta) % 2 ** 32

    v1 = (
        v1
        +
        (
            (
                (
                    (v0 << 4) % 2**32 ^ (v0 >> 5) % 2**32
                ) % 2**32 + v0
            ) % 2**32
            ^
            (sum_ + k[(sum_ >> 11) % 2**32 & 3]) % 2**32
        ) % 2**32
    ) % 2**32

It looks strange and I'm not sure it's the best idea. What do you think of it?  Is it something that needs improving?  If so, how?


texts:
For my current project I need to validate responses. The requests will be send from multiple different shell scripts and should be controlled from another script. I'd never targeted a .sh with a .sh before, so let's try some FizzBuzz first.

Fizz.sh

#!/bin/bash

if [ $# -eq 0 ]
then
    echo "This FizzBuzz is interactive. Please provide the upper limit."
    echo "Usage : $0 limit"
    exit 1
fi

for i in `seq $1`
do
   echo `source ./buzz.sh $i`
done

Buzz.sh

#!/bin/bash

([ $(($1%15)) -eq 0 ] && echo 'FizzBuzz') ||
([ $(($1%5)) -eq 0 ] && echo 'Buzz') ||
([ $(($1%3)) -eq 0 ] && echo 'Fizz') ||
echo $1;

Is it idiomatic?
Is this the best way to hand all output from Buzz to Fizz?

The goal is to learn decent BASH. The FizzBuzz has no need for optimization, it's mostly about style, how data should be passed from script to script and whether I followed BASH-practices or not.


texts:
In interrupted bubble sort, we need to stop the sorting based on the iteration count. This seems pretty straightforward to me. I have this code, which seems to work fine, but it isn't fast enough for larger input sets. How can I make this faster?

s = '36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 2'
ls = s.split('|')
l = [int(n) for n in ls[0].split(' ') if n.isdigit()]
icount = int(ls[1].strip())
print "list is", l, len(l)
print "count is ", icount

ic = 0
while ic < icount:
    i = 0
    while i < len(l) - 2:
        if l[i + 1] < l[i]:
            tup = l[i], l[i + 1]
            l[i + 1], l[i] = tup
        i += 1
    ic += 1
print ' '.join(str(i) for i in l)

It is not fast enough for larger input sets. How can I make this faster?


texts: find matches between attributes from two different Objects

texts:
I'm new in Java 8, and I'm trying to convert and optimize my method from java 6 to java 8. I want it to be less verbose, with fewer lines of code.
This method gets an object (userRequest) and tries to find from a list of other object type (User) the one with more matches between their attributes(for example, if the user requested has brown eyes, each user from the list who has brown eyes has one more match). If it results that there is more than one User, so the one with less age is chosen.
In this example it's only used a list of 3 users and only 3 or 4 attributes, but in the future it's intended to be more, so I'd like to optimize it for multiple users and attributes.
@Getter
@Setter
@AllArgsConstructor
@ToString
public class UserRequest{
    private String name;
    private String surname;
    private String company;
    private String eyesColor;
}

@Getter
@Setter
@AllArgsConstructor
@ToString
public class User{
    private Long id;
    private String name;
    private String surname;
    private String eyesColor;
    private int age;
}

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserFilters userFilters;

    public int getMostMatchingUserId(UserRequest userRequest){

        List<User> userList = userRepository.getUsers();

        List<User> mostMatchingUserList = userFilters.getMostMatchingUserList(userList, userRequest);

        return mostMatchingUserList.stream().min(Comparator.comparing(User::getAge)).orElse(mostMatchingUserList.get(0)).getId();
    }
}

@UtilityClass
public class UserFilters{

    public List<User> getMostMatchingUserList (List<User> userList, UserRequest userRequest) {

    List<User> newUserList = new ArrayList<>();
    int matchesInitialCount = 0;

    for(User user: userList){
        int userMatchesCount = countMatches(user, userRequest);
        if(userMatchesCount > matchesInitialCount){
            newUserList.clear();
            newUserList.add(user);
            matchesInitialCount = userMatchesCount;
        } else if(userMatchesCount == matchesInitialCount){
            newUserList.add(user);
        }
    }
    return newUserList;
    }

    private int countMatches (User user, UserRequest userRequest) {
        int matchesCounted = 0;

        if (Objects.nonNull(user.getName()) && user.getName().equals(userRequest.getName())) {
            matchesCounted++;
        }
        if (Objects.nonNull(user.getSurname()) && user.getSurname().equals(userRequest.getSurname())) {
            matchesCounted++;
        }
        if (Objects.nonNull(user.getAge()) && user.getAge() == userRequest.getAge() {
            matchesCounted++;
        }
        return matchesCounted;
    }
}

Imagine you get a new userRequest ("John", "Jackson", "aena", "brown") to check which one of the next userList has the most matches:
User1 (111, "John", "Wine", "blue", 37);
User2 (222, "Maria", "Jackson", "brown", 25);
User3 (333, "Michel", "Jackson", "brown", 55);
The result is: User1 has one match and User2 & User3 have two matches each. The next validation checks who is the youngest one from User2 & User3: so User2 will be the chosen one.


texts:
Based on the standard implementation of Array.sort in JavaScript, would the following be a reasonable way of shuffling the contents of an array? Is it moderately efficient and will it produce a fairly even spread of results?

Array.prototype.shuffle = function () {
    return this.sort(function(){ return Math.random() - 0.5;});
};


texts:
Assume that you have the ready sorted array

Is it a good solution?

Plunker OR:

<script>
    var score = [10, 20, 20, 20, 40, 50, 60, 60, 60, 60, 70, 80, 80, 90]
    var j = 0;
    var k = 0;

    for( i in  score ){
        var grade = '';

        if( i== 0 || score[i] > score[i - 1] ){
            grade = j = j+1+k;
            k = 0;
        }else{
            k++;
            grade = j;
        }

        document.write( 'Grade: ' + ( grade ) +'<br>');
    }
    </script>


texts: Any field change in form, disable button, and activate another

texts:
I have JavaScript code that activates a button and deactivates another if a select is changed.

HTML

<select name="list" id="list">
     <option value="">choose</option>
     <option value="1">1</option>
     <option value="1">2</option>
     <option value="3">3</option>
</select>
<input name="refresh" type="button" value="refresh" disabled="" />
<input name="add" type="button" value="add"  />

JavaScript

$('select').change(function () {
    var op = $(this).val();
    if (op != '') {
        $('input[name="refresh"]').prop('disabled', false);
    } else {
        $('input[name="refresh"]').prop('disabled', true);
    }
});
$('select').change(function () {
    var op = $(this).val();
    if (op != '') {
        $('input[name="add"]').prop('disabled', true);
    } else {
        $('input[name="add"]').prop('disabled', false);
    }
});

I need to expand the function to all fields in the form with minimal code. I could use the same code for the rest of inputs, but the code will be very long. I'm looking for a short version.


texts:
I have the following SQL statement that I think could be improved in areas (I believe there may be a way to use where over having, but I'm not sure how and I'm sure there's a way to reference the last updated column in the having clause but I'm unsure how to do it). Any suggestions would be greatly appreciated:

/* Show indexes that haven't had statistics updated in two days or more */
select
        t.name as [Table Name],
        min(convert(CHAR(11),stats_date(t.object_id,s.[stats_id]),106)) as [Last Updated]
from    sys.[stats] as s
        inner join sys.[tables] as t
            on [s].[object_id] = [t].[object_id]
group by t.name
having  min(convert(CHAR(11),stats_date(t.object_id,s.[stats_id]),106)) < dateadd(day, -1, getdate())
order by 2 desc


texts:
The following function gets the lastly clicked cell value and pastes it into the edit box editBox, in which the user can then edit the value. Whenever the user goes out of the editing box the value is copy/pasted back to the cell. Aside from that functionality, the cell that's focused gets overlighted (red color).

I've noticed that sometimes when a value is too large (big number of lines), the copy-paste thingy takes longer than expected : it should be almost live ( <1s ) but actually takes 3-5s.

--- EDIT : JsFiddle added ---

https://jsfiddle.net/k3nj33nu/5/

---------------------------------------

Any suggested performance improvements?

$(document).ready(function() {
    var lastClicked;

    $(document).on('click', '.cell', function() {
        $('.editBox').val($(this).val());

        if (lastClicked != undefined && lastClicked != $(this).attr('name'))
        {
            var lastCell = $(document).find("[name='" + lastClicked + "']");
            var newCell  = $(this);
            ChangeCellBorderColor(lastCell, "");
            ChangeCellBorderColor(newCell, "red");
        }
        lastClicked = $(this).attr('name');

        $('.editBox').keyup(function() {
            $(document).find("[name='" + lastClicked + "']").val($('.editBox').val())
        });
    });
});

function ChangeCellBorderColor(cell, colour)
{
    cell[0].style.borderColor = colour;
}


texts:
I have written my own memcmp() function for learning purposes:

bool memcmp( byte* _bufA, byte* _bufB, uint n )
{
    while ( n > 4 )
    {
        if ( (int)*_bufA != (int)*_bufB )
            return false;
        _bufA += 4, _bufB += 4;
        n -= 4;;
    }
    while( --n )
    {
        if ( *_bufA != *_bufB )
            return false;
        ++_bufA, ++_bufB;
    }
    return true;
}

My thought process was that whenever the byte stream is greater than 4 bytes, I'd compare 4 bytes at a time instead of one. That means fewer iterations which would mean it would be a little more faster. And then just simply take care of the remaining 4 bytes or so in the second while statement.

Benchmarks:

Standard memcmp function comparing 1024 bytes: 585 nanoseconds
My memcmp function comparing 1024 bytes: 586 nanoseconds

It's pretty inconsistent but I figure it's still a pretty good function since it's 50/50 generally.


texts:
I am using .NET Core 2.2, EF Core and C#. I have created a web API project.

I was requested to do an Upsert (insert or update). The entities structure is:

Movie
User
Rating: this is used to give a rating to a movie, so a new rating will have the
MovieId (from Movie) the UserId (from User) and the Rating value.

In the Web API controller I have this request for the result of the upsert:

404 (if movie or user is not found)
400 (if the rating is an invalid value, the values can be an integer from 1 to 5)
200 (OK)

So, according to the request, I have implemented this code, but it does not look correct according to the SOLID principles. Is there any way to refactor this an get a better code? Obviously: making one method for the update and other to create is not an option as it does not respect the request.

This is the code:

[Route("apid/movies")]
[ApiController]
public class MoviesDController : ControllerBase
{
    private readonly IRatingRepository _repository;
    private readonly IUserRepository _userRepository;
    private readonly IMovieRepository _movieRepository;
    public MoviesDController(IRatingRepository repository, IUserRepository userRepository, IMovieRepository movieRepository)
    {
        _repository = repository;
        _userRepository = userRepository;
        _movieRepository = movieRepository;
    }

    // POST: apid/movies
    [HttpPost]
    public IActionResult Upsert([FromBody] Rating rating)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        //1) Get the User
        var user = _userRepository.GetById(rating.UserId);
        if (user == null)
        {
            return NotFound("User not found...");
        }

        //2) Get the Movie
        var movie = _movieRepository.GetById(rating.MovieId);
        if (movie == null)
        {
            return NotFound("Movie not found...");
        }

        //3) Get Rating
        var existingRating = _repository.GetMovieRating(rating.UserId, rating.MovieId);
        if (existingRating == null)
        {
            //Insert
            _repository.InsertRating(rating);

            return Ok("Rating added");
        }

        existingRating.RatingValue = rating.RatingValue;
        //Update
        _repository.UpdateRating(existingRating);

        return Ok("Rating updated");
    }
}

Thanks!


texts: Recursive Insertion in Hash Table (Linear Probing)

texts:
I was practicing implementing a hash function. I tried to use linear probing to insert in hash table. The insertion is working alright but I am not satisfied with the recursion. It doesn't feel like "good recursion". The Linear_Probing function instead of being recursive itself uses two other functions (excluding hash function) for recursive insertion: Index_Finder() and its auxiliary function.

Is there any way I can do the recursion better in this code for inserting into a hash table?

int Hash(int number); //<Hash function returns index by adding the first and last digit of a number;
bool Linear_Probing(int array[], bool flags [], int MAX_SIZE, int number, int &collisions); //<This function if possible inserts the number in the Hash Table
int Index_Finder(int number, bool flags[], int MAX_SIZE, int &collisions); //<This function finds the index where the number should be inserted. It returns -1 if no empty slot is found.
int Index_Finder(bool flags[], int MAX_SIZE, int index, int limit, int &collisions); //<This is an auxilary function

int main()
{
    const int MAX_SIZE = 11;
    int collisions = 0;
    int array[MAX_SIZE];
    bool flags[MAX_SIZE];

    for (int i = 0; i < MAX_SIZE; i++)
    {
        array[i] = 0;
        flags[i] = false;

    }

    //<Inserting numbers in hash table using linear probing
    Linear_Probing(array, flags, MAX_SIZE, 67, collisions);
    Linear_Probing(array, flags, MAX_SIZE, 19, collisions);
    Linear_Probing(array, flags, MAX_SIZE, 3, collisions);
    Linear_Probing(array, flags, MAX_SIZE, 808, collisions);
    Linear_Probing(array, flags, MAX_SIZE, 337, collisions);
    Linear_Probing(array, flags, MAX_SIZE, 1, collisions);
    Linear_Probing(array, flags, MAX_SIZE, 86, collisions);
    Linear_Probing(array, flags, MAX_SIZE, 38, collisions);

    return 0;
}

bool Linear_Probing(int array[], bool flags[], int MAX_SIZE, int number, int &collisions)
{
    int dummy_collisions = 0;
    int index = Index_Finder(number, flags, MAX_SIZE, dummy_collisions);

    if (index == -1)
        return false;

    else
    {
        collisions += dummy_collisions;

        array[index] = number;
        flags[index] = true;
        return true;
    }
}

int Index_Finder(int number, bool flags[], int MAX_SIZE, int &collisions)
{
    int index = Hash(number);

    if (index < MAX_SIZE && !flags[index])
        return index;

    if (index >= MAX_SIZE)
    {
        index = 0;

        if (!flags[index])
            return index;

        else
            collisions++;
    }

    else
    {
        collisions++;
    }

    int limit = index;

    return Index_Finder(flags, MAX_SIZE, ++index, limit, collisions);

}

int Index_Finder(bool flags[], int MAX_SIZE, int index, int limit, int &collisions)
{
    if (limit == index)
        return -1;

    if (index == MAX_SIZE)
        index = 0;

    if (!flags[index])
        return index;

    else
    {
        collisions++;
        return Index_Finder(flags, MAX_SIZE, ++index, limit, collisions);
    }
}

int Hash(int number)
{
    int first_digit = 0, last_digit = 0;
    bool first_time = true;

    while (number > 0)
    {
        if (first_time)
        {
            first_digit = number % 10;
            number -= (number % 10);
            number /= 10;
            first_time = false;
        }

        else
        {
            last_digit = number % 10;
            number -= (number % 10);
            number /= 10;
        }
    }

    return (first_digit + last_digit);
}


texts:
Which is best practice for creating an enum-like lookup for templates? I enjoy being able to bring up my templates with VS IntelliSense so either works for me.

Option 1 (enum/Dictionary combo):

public enum Template
{
    AccountInformation = 1,
    Registration = 2,
    Signed = 3
}
static readonly IDictionary<Template, String> TemplateData = new Dictionary<Template, String>()
{
    {Template.AccountInformation, "accountinfo.txt"},
    {Template.Registration, "registration.txt"},
    {Template.Signed, "signed.txt"}
};

Option 2 (static class):

public static class Template
{
    public static String AccountInformation = "accountinfo.txt";
    public static String Registration = "registration.txt";
    public static String Signed = "signed.txt";
}


texts:
I am trying to write a module that automatically subdivides  div-elements, given some initial parameters.

I've prepared the following jsfiddle.

What I am doing at the moment is:

Setting the width and height of the parent element.
Setting the child elements to float left and assigning percentual width and height to them, based on the specified parameters.

that.createDivisionData = function(paramObj) {

    var
        numOfDivisions = paramObj.divisions,
        widthTop = paramObj.widthTop,
        heightTop = paramObj.heightTop,
        widthBottom = paramObj.widthBottom,
        widthBlock = paramObj.blockWidth,
        heightBlock = paramObj.blockHeight,
        data = [];

    for ( var i = 1; i <= numOfDivisions; i++) {
        var divisionObj = {};
        if (i < 3) {
            divisionObj.widthPerc = i % 2 ? widthTop + '%' : (100 - widthTop) + '%';
            divisionObj.width = i % 2 ? widthTop * widthBlock / 100 : (100 - widthTop) * widthBlock / 100;
            divisionObj.heightPerc = heightTop + '%';
            divisionObj.height = heightBlock * heightTop / 100;
        } else {
            divisionObj.widthPerc =  i % 2 ? widthBottom + '%' : (100 - widthBottom) + '%';
            divisionObj.width =  i % 2 ? widthBottom * widthBlock / 100 : (100 - widthBottom) * widthBlock / 100;
            divisionObj.heightPerc = (100 - heightTop) + '%';
            divisionObj.height = (100 - heightTop) * heightBlock / 100;
        }
        data.push(divisionObj);
    }
    return data;
};

The way I am doing it at the moment actually works. But it feels somehow wrong, dirty, unfinished, you name it. I know this is a subjective question, but maybe someone has an idea. How can I do this in a cleaner way?


texts:
Give a N*N square matrix, return an array of its anti-diagonals. Look at the example for more details.

Example:

Input:

1 2 3
4 5 6
7 8 9

Return the following :

[
  [1],
  [2, 4],
  [3, 5, 7],
  [6, 8],
  [9]
]

This is my approach to this question:

public class Solution {
    public ArrayList<ArrayList<Integer>> diagonal(ArrayList<ArrayList<Integer>> A) {

        //The total number of internal rows in the final list will be 2*noofrows(A) - 1
        int size = 2*A.get(0).size() - 1;
        ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
        int count = 0;

        //Count of the number of rows
        int r = A.size();

        //Time complexity: O(size*r^2)
        //Space complexity: O(size)
        while( count < size)
            {
                ArrayList<Integer> temp = new ArrayList<>();
                for( int i = 0; i < r; i++ )
                {
                    for( int j = 0; j < r; j++ )
                        {
                            if( ((i + j) == count))
                                temp.add(A.get(i).get(j));

                            //Continue when the indices sum up to be > count
                            if((i + j) > count )
                                continue;
                        }
                }
                ans.add(temp);
                count++;
            }
        return ans;
    }

I have the following questions regarding my code:

1) Is there any better approach to solve this problem?

2) How can I improve my time and space complexity?

3) Are there any redundant steps that can be removed?

Reference


texts: Large dataset with pyspark - optimizing join, sort, compare between rows and group by with aggregation

texts:
I have a csv file with more than 700,000,000 records in this structure:

product_id      start_date       end_date
1               19-Jan-2000      20-Mar-2000
1               20-Mar-2000      25-Apr-2000
1               20-May-2000      27-Jul-2000
1               27-Jul-2000
2               20-Mar-2000      25-Apr-2000
3               12-Jan-2010      30-Mar-2010
3               30-Mar-2010

End_date is null means product currently is in used.

End_date can mean 2 things, 1 - disable product, 2 - battery replace

If End_date is the same as the next start_date, then it is battery replacement.

The expect result is, product_id along with the start_date of its current lifecycle (battery replace is counted in current lifecycle).

Which mean, the start_date should be the date after its last disability. For example above, output would be:

product_id      start_date
1               20-May-2000
3               12-Jan-2010

My code is as below. It's kind of ugly, so if you could please review and advise if this code can run well with 700,000,000 records or there are better ways/methods to solve this challenge. I have run this code and seem a little bit slow for 100 records test file.Thank you for your help.

Code:

# csv input
df = spark.read.csv('productlist.csv', header=True, inferSchema=True)

# filter out stopped product id
df2 = df.select("product_id").filter("end_date is null")
df = df.join(df2, ["product_id"])

# sort dataframe by product id & start date desc
df = df.sort(['product_id', 'start_date'],ascending=False)

# create window to add next start date of the product
w = Window.partitionBy("product_id").orderBy(desc("product_id"))
df = df.withColumn("next_time", F.lag(df.start_date).over(w))

# add column to classify if the change of the current record is product disability or battery change.
df = df.withColumn('diff', F.when(F.isnull(df.end_date), 0)
                  .otherwise(F.when((df.end_date != df.next_start_date), 1).otherwise(0)))

# add column to classify if the product has been disabled at least once or not
df3 = df.groupBy('product_id').agg(F.sum("diff").alias("disable"))
df = df.join(df3, ["product_id"])

# get requested start date for those products have not been disabled
df1 = df.filter(df.disable == 0).groupBy("product_id").agg(F.min("start_date").alias("first_start_date"))

# get requested date for those products have been disabled once,
# which is the first next start date at the most recent disable date
df2 = df.filter(df.diff == 1).groupBy("product_id").agg(F.max("next_start_date").alias("first_start_date"))


texts:
This is a trivial problem. The reason I'm here is to get a better idea of how to use Java's numerous APIs for working with files, folders, and paths. I'm hoping this specific example will help.

Problem Statement

I want to rename a file like this:

  /path/to/myFile.txt

to this:

  /path/to/myFile_old.txt

Ugly Solution

I've been trying to find a concise way to do this, and this is the best I can come up with:

Files.move(
        Paths.get(file),
        Paths.get("/" + FilenameUtils.getPath(file) + FilenameUtils.getBaseName(file) + "_old" + FilenameUtils.getExtension(file)));

It works, which is why I'm not on Stack Overflow, but

It's not portable because of the hardcoded "/".
It's really cumbersome -- specifically I don't like how verbose org.apache.commons.io.FilenameUtils is, and how I have to convert my Strings to Paths with Paths.get.

I've looked at using java.io.File or java.nio.file.Paths.resolve... but I haven't found anything tidy.


texts: Granular vs a aggregated tests when dealing with arrays

texts:
I write code in a Test-Driven way and I often build my functions starting with tests for the easy edge cases.
For example, given a flat array of Items that have a category property, return an array of Groups, each built with all the items for one of the categories in the input.
One edge case I find helpful to test first is the behavior when the items are all from the same category. This gives me a chance to see how the Group should look like.
In this case there are (at least) two approaches one can take: two granular tests vs. a single one.
Granular
One could write two granular tests for these conditions:

Given an array with items all from the same category, the output should be an array with a single group
A group should contain all the items for its category and only those

In Swift, these tests might look something like this
func testGroupingArrayOfSameCategoryReturnsOneGroup() {
    let items = [
        Item(name: "a", category: .foo),
        Item(name: "b", category: .foo),
        Item(name: "c", category: .foo),
    ]

    let groups = groupByCategory(items)

    XCTAssertEqual(groups.count, 1)
}

func testGroupingBuildsGroupWithAllItemsForCategory() throws {
    let items = [
        Item(name: "a", category: .foo),
        Item(name: "b", category: .foo),
        Item(name: "c", category: .foo),
    ]

    let groups = groupByCategory(items)

    let group = try XCTUnwrap(groups.first)
    XCTAssertEqual(group.category, .foo)
    XCTAssertEqual(group.items.count, 3)
    XCTAssertEqual(group.items[0].name, "a")
    XCTAssertEqual(group.items[1].name, "b")
    XCTAssertEqual(group.items[2].name, "c")
}

I like how the two behaviors of having a 1:1 match with groups and categories and how the groups are built are separated, but I see a lot of duplication between those tests.
Aggregated
A different approach would be to check both facets of the behavior in the same test.
func testGroupingArrayOfSameCategoryReturnsOneGroupWithAllItemsForCategory() {
    let items = [
        Item(name: "a", category: .foo),
        Item(name: "b", category: .foo),
        Item(name: "c", category: .foo),
    ]

    let groups = groupByCategory(items)

    XCTAssertEqual(groups.count, 1)
    let group = try XCTUnwrap(groups.first)
    XCTAssertEqual(group.category, .foo)
    XCTAssertEqual(group.items.count, 3)
    XCTAssertEqual(group.items[0].name, "a")
    XCTAssertEqual(group.items[1].name, "b")
    XCTAssertEqual(group.items[2].name, "c")
}

Which do you think is clearer and why?


texts:
In the below code snippet, on click of a button, progress bars are added one by one. The bar shows up for 3 seconds. If you continuously click on the button, it will keep adding progress bars. The next bar will start after the previous bar finishes animating.

Is there anyway to make the code better?

;(function(window, document, undefined) {

    'use strict';

    var container = document.getElementsByClassName('container')[0];
    var button = document.getElementsByTagName('button')[0];
    var progressBar = document.getElementsByClassName("bar-container")[0];

    var promise = Promise.resolve();

    button.addEventListener('click', function() {
        var newProgressBar = progressBar.cloneNode(true);
        container.appendChild(newProgressBar);

        promise = promise.then(function() {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                                      newProgressBar.classList.add('active');
                    setTimeout(function() {
                        resolve();
                    }, 3000);
                }, 0);
            });
        })
    });

})(window, document);

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <button id="button">click me</button>

emphasized text


texts:
I'm using the icalendar module to parse (google) calendar entries and the dateutil.rrule module to deal with recurring events.

Right now, I'm using the rrulestr class to parse (part of) the RRULE data and rruleset to add EXDATE entries:

from icalendar import Event
from dateutil.rrule import rrulestr, rruleset
from datetime import datetime, timedelta
from pytz import UTC
from pprint import pprint

# example google calendar entry
event_text = """BEGIN:VEVENT
SUMMARY:Test Event
DTSTART;TZID=Europe/Paris:20160806T100000
DTEND;TZID=Europe/Paris:20160806T120000
RRULE:FREQ=WEEKLY;UNTIL=20160910T000000Z
EXDATE;TZID=Europe/Paris:20160813T100000
EXDATE;TZID=Europe/Paris:20160820T100000
END:VEVENT"""

event = Event.from_ical(event_text)

rules_text = '\n'.join([line for line in event.content_lines()
                        if line.startswith('RRULE')])

rules = rruleset()

first_rule = rrulestr(rules_text,
                      dtstart=event.get('dtstart').dt)

# in some entries, tzinfo is missing
if first_rule._until and first_rule._until.tzinfo is None:
    first_rule._until = first_rule._until.replace(tzinfo=UTC)

rules.rrule(first_rule)

exdates = event.get('exdate')

if not isinstance(exdates, list):  # apparently this isn't a list when
    exdates = [exdates]            # there is only one EXDATE
for exdate in exdates:
    try:
        rules.exdate(exdate.dts[0].dt)
    except AttributeError:  # sometimes there is a None entry here
        pass

now = datetime.now(tz=UTC)
in_2_months = now + timedelta(days=60)
pprint(rules.between(now, in_2_months))

Apparently this works, but I'm wondering if there is a more compact and cleaner way of doing this?

I especially don't like the fact that the RRULE string is parsed twice.

Could the absence of tzinfo be handled differently?

Something else?


texts:
This is my attempt to answer my own equally named question on SO. In this case, I need a method comparing two strings so that the running time is input independent.

// Not private in order to prevent optimizations.
static class Blackhole {
    private static void eat(long n) {
        dummy += n;
    }

    // Not private in order to prevent optimizations.
    static volatile long dummy;
}

public boolean areEqual(String input, String secret) {
    final int length = secret.length();
    // No need to keep the length secret.
    if (input.length() != length) {
        return false;
    }
    long delta = 0;
    for (int i = 0; i < length; ++i) {
        // Any char is interpreted as a non-negative number.
        // Xoring such numbers preserves non-negativity.
        // The result depends on every input bit, so no short-circuit is possible.
        // Overflow is impossible, therefore diff is zero <=> all chars are the same.
        delta += input.charAt(i) ^ secret.charAt(i);
    }
    Blackhole.eat(delta);
    return delta == 0;
}

It's a really short snippet and I'm mainly interested in ensuring that it really works in constant time.  In particular, I'm afraid that the loop could indeed get optimized to something like

int i = 0
for (; ; ++i) {
    if (input.charAt(i) != secret.charAt(i)) break;
}
for (; i < length; ++i) {
    delta += input.charAt(i) ^ secret.charAt(i);
}

where the first loop could be faster on some architectures, as -- after unrolling -- it translates to load, xor with memory, and a conditional jump. Assuming some future architecture capable of executing 6 instructions per cycle when there are no data dependencies and no mispredictions, it could be a win. Or the JIT could believe, it makes sense...

Concerning the Blackhole, I wonder if volatile is necessary. I didn't want to depend on JMH nor copy this monster.

In the meantime I've got an idea avoiding volatile writes nearly perfectly

    private static void eat(long n) {
        if (n == dummy) {
            dummy = new SecureRandom().nextLong();
        }
    }


texts: Updating / validating mass update rails association

texts:
My recipes model is as follows

has_many :quantities
has_many :ingredients, :through => :quantities, :uniq => true
has_many :sizes, :through => :quantities, :uniq => true

And the controller action im trying to refactor

class Admin::QuantitiesController < Admin::AdminController
  layout 'admin'

  authorize_resource :class => :controller

  def create
    recipe        = Recipe.find(params[:recipe_id])
    values      = params[:quantity][:ingredient]
    @quantities = Quantity.find(values.keys)

    @quantities_errors = {}
    @quantities.each do |quantity|
        if quantity.value != values[quantity.id.to_s]
            quantity.value = values[quantity.id.to_s]
            if quantity.valid?
                quantity.save
            else
                @quantities_errors[quantity.id] = quantity.errors.messages
            end
        end
    end

    unless @quantities_errors
        redirect_to edit_admin_recipe_path(recipe), notice: 'Ingredients was successfully added.'
    else
        redirect_to edit_admin_recipe_path(recipe), alert: @quantities_errors
    end

  end

end


texts: Angular 5 Forms - Extending Form Control

texts:
I am taking a first pass at Angular Forms with some colleagues and noticed an interesting design pattern implemented by a coworker where the FormControl is being extended to allow for additional properties including flags like readonly, showcancel and a few other custom properties.
The controls are then iterated over in the form template to then render each input. This is a big concern to me as we are extending the base Angular classes that could change with updates to the framework.
Here is an example of what I am talking about:
export class FormGroupModel extends FormGroup {
    readOnly: boolean;
    showCancel: boolean;
    controls: { [key: string]: FormControlModel | FormGroupModel };

    get(controlName: string): FormControlModel {
        return <FormControlModel> super.get(controlName);
    }
}

export class FormControlModel extends FormControl {
    readOnly: boolean;
    showCancel: boolean;
}

Is there anything else that we should keep in mind when using this strategy? I'm not totally certain that these classes were meant to be extended...


texts:
Is this a right way to use/implement an external library into a project? If it's not how do you do it?

class Mail {
    protected $mail;

    public function __construct(PHPMailer $mail) {
        $this->mail = $mail;
        $this->mail->isHTML(Config::read('mail.isHtml'));
        $this->mail->setFrom(Config::read('mail.fromEmail'), Config::read('mail.fromName'));
    }

    public function sendMail ( $to, $subject, $body, $plainText ) {
        $this->mail->addAddress($to);
        $this->mail->Subject = $subject;
        $this->mail->Body = $body;
        $this->mail->AltBody = $plainText;

        $this->mail->send();
    }
}

and use it in anywhere in your app like this?

$test = new lib\Mail(new PHPMailer);

$test->sendMail('test@domain.com', 'Subject', '<html>Hello username</html>', 'Hello username' );


texts: Improve this numeric solver for properties of geometric objects

texts:
I'm converting some C# code to F#. Basically it is some geometry classes that can have properties tuned by a numerical solver. I start with a discriminated union of type parameter that can be fixed or free ( tunable ) and then build tunable geometry objects out of them.

I create a discriminated union called shape with Line, Circle, Point as sub types. The code is below.

module SketchSolveFS.Solver

type parameter =
    | Fixed of double
    | Free of double ref

let free v = Free (ref v)
let fix v = Fixed v

type point  = {x: parameter; y:parameter}
type line   = {p0: point; p1:point}
type circle = {center: point; radius:parameter}

type shape =
    | Parameter of parameter
    | Point of point
    | Line of line
    | Circle of circle

let rec freeVars v = seq {
    match v with
    | Parameter p ->
        match p with
        | Free v -> yield v
        | _ -> ()
    | Point p ->
        yield! Parameter p.x |> freeVars
        yield! Parameter p.y |> freeVars
    | Line line ->
        yield! Point line.p0 |> freeVars
        yield! Point line.p1 |> freeVars
    | Circle c ->
        yield! Point c.center |> freeVars
        yield! Parameter c.radius |> freeVars
    }

I have a function called freeVars that return me a sequence of
tunable parameters in a list of geometry objects. The code works
and passes my tests but the implementation leaves me uncomfortable.

Here is why. I keep feeling the need to write my discriminated
union this way

type shape =
    Parameter of parameter
    Point of Parameter * Parameter
    Line of Point * Point

instead of having to create concrete types before hand. Then my
function freeVars would look like this.

let rec freeVars v = seq {
    match v with
    | Parameter p ->
        match p with
        | Free v -> yield v
        | _ -> ()
    | Point p ->
        yield! p.x |> freeVars
        yield! p.y |> freeVars
    | Line line ->
        yield! line.p0 |> freeVars
        yield! line.p1 |> freeVars
    | Circle c ->
        yield! c.center |> freeVars
        yield! c.radius |> freeVars
    }

where I don't have to wrap the concrete classes back in the discriminated
union wrappers each time I extract and recursively process. Now obviously
the F# compiler doesn't like this or I wouldn't be asking the question.

Is there a better way to frame the working code. Are discriminated unions
not appropriate for this code perhaps?


texts:
I've tried asking about the performance on the HackerRank discussion forum, it didn't work out.

The task is to write a program with three operations:
 1 x  Push the element x onto the stack.
 2    Delete the element present at the top of the stack.
 3    Print the maximum element in the stack.

The first input line is the number of lines in the program, all subsequent lines are one of the three instructions.

Sample Input:
 10
 1 97
 2
 1 20
 2
 1 26
 1 20
 2
 3
 1 91
 3

Sample Output:
 26
 91

My Solution:
data = []
for _ in range(int(input())):
    ins = input().split()
    if ins[0] == '1':
        data.append(int(ins[1]))
    elif ins[0] == '2':
        data.pop()
    else:
        print(max(data))

It gets slow on working with input size of 1000 elements or so,
how could I speed this up?


texts:
I have a matrix of ~4.5 million vector [4.5mil, 300] and I want to calculate the distance between a vector of length 300 against all the entries in the matrix.

I got some great performance time using the answers from the following post: Efficient numpy cosine distance calculation.

from scipy import spatial

def cos_matrix_multiplication(vector, matrix):

    v = vector.reshape(1, -1)
    scores=spatial.distance.cdist(matrix_1, v, 'cosine').reshape(-1)
    return scores

Here, vector is a NumPy array [300,1] and matrix_1 is a NumPy matrix [4.5mil, 300]

Using this I can calculate scores for the entire matrix (4.5 mil records) in about 19secs. I have been trying to optimize it even further but don't seem to make any progress.

I want to know if it is feasible to convert this code into a cython code or use Process/Thread pool to speed up even more.

I split my matrix into smaller chunks (500K each) and used ThreadPoolExecutor as follows:

matrix_list=[mat_small1,mat_small2,....,mat_small9]

import concurrent.futures
def cos_thread():
    neighbors=[]

    with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:

        future_to_list = {executor.submit(cos_matrix_multiplication,vec,mat_col): mat_col for mat_col in matrix_list}

        for future in concurrent.futures.as_completed(future_to_list):

            data = future.result()

            neighbors.extend(data)

    return neighbors

I can currently compute the entire ~4.5 million cosines in ~5 seconds.


texts: Given an array of N elements, answer Q queries asking if there exist two elements from two specified range with a given XOR value

texts:
We have array of N (<=10^5) elements with Q(<=10^5) queries.

  Each query is of type: p q r s x, where we have to find if
  there exist a element from array in range [p,q] and an element in
  [r,s] whose xor is equal to X. If exists, print "true" else
  "false".

Constraints:

1 <= N <= 10^5
1 <= p,q,r,s <= N
1 <= A[i] <= 10^5
Example:
Array : 1 2 3 4 5 6
Query : 1 3 4 6 5
Output: true.

1 from range [1,3] and 4 from range [4,6] has an XOR value equal to X=5.

I tried to solve it by loading the second sub array into a hashmap then XORing x with each element from first array and if the result exists in hashmap answer is yes.

But this \$O(n)\$ solution for each query isn't sufficient to pass. I am looking for ideas for how I can achieve this in possible \$O(logn)\$ or \$O(1\$) time for each query, or if it is possible to answer queries offline using MO's algorithm in \$O(sqrt(n))\$ time.

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
int a[100001];

bool query(int p,int q,int r,int s,int x)
{
    unordered_map<int,int> mp;
    for(int i =r;i<=s;i++)
    {
        mp[a[i]]=1;
    }
    for(int i =p;i<=q;i++)
    {
        if(mp.count(a[i]^x) > 0)
        {
            return true;
        }
    }
    return false;
}

int main()
{
    int n,q;
    cin>>n>>q;
    for(int i =0;i<n;i++)
    {
        cin>>a[i];
    }
    while(q--)
    {
        int p,q,r,s,x;
        cin>>p>>q>>r>>s>>x;
        // -1 because they are 1-indexed
        if(query(p-1,q-1,r-1,s-1,x))
        {
            cout<<"true\n";
        }
        else
        {
            cout<<"false\n";
        }
    }

    return 0;
}


texts: Creating procfs entry on linux in Kernel Module

texts:
My Linux kernel version is

Linux version 5.0.0-29-generic

I'm currently learning how to write kernel modules for Linux and implemented this simple module which creates a new procfs entry and allows some data to be read from it.

The module consists of the following files:

init.c:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include "pfs_regfile.h"

MODULE_LICENSE("GPL");

static struct proc_dir_entry *pde_regfile;

static struct file_operations fops_regfile = {
    .owner = THIS_MODULE,
    .open = pfs_open,
    .read = pfs_device_read,
    .release = pfs_release
};

static int __init init_md(void){
    pde_regfile = proc_create(PROC_REGFILE_NAME, 0, NULL, &fops_regfile);
    if(!pde_regfile){
        remove_proc_entry(PROC_REGFILE_NAME, NULL);
        printk(KERN_ALERT "Error: could not initialize procfs entry /proc/%s", PROC_REGFILE_NAME);
        return -ENOMEM;
    }
    return 0;
}

static void __exit exit_md(void){
    remove_proc_entry(PROC_REGFILE_NAME, NULL);
}

module_init(init_md);
module_exit(exit_md);

pfs_regfile.h:

#ifndef PFS_REGFILE_H
#define PFS_REGFILE_H

#include <linux/fs.h>

#define PROC_REGFILE_NAME "pfs_regfile"

int pfs_open(struct inode*, struct file *);

int pfs_release(struct inode *, struct file *);

ssize_t pfs_device_read(struct file *, char __user * buffer, size_t, loff_t *);

#endif //PFS_REGFILE_H

pfs_regfile.c:

#include <linux/module.h>
#include <linux/uaccess.h>
#include "pfs_regfile.h"

static char data[5000];
static char *current_ptr;
int pfs_open(struct inode* in, struct file *filp){
    for (int i = 0; i < sizeof(data) - 1; i++){
        data[i] = 'a';
    }
    current_ptr = data;
    try_module_get(THIS_MODULE);
    return 0;
}

int pfs_release(struct inode *in, struct file *filp){
    module_put(THIS_MODULE);
    return 0;
}

ssize_t pfs_device_read(struct file *file, char __user * buffer, size_t len, loff_t *off){
    size_t pos = (size_t)(current_ptr - data);
    size_t to_copy = pos + len < sizeof data ? len : sizeof data - pos;
    unsigned long not_copied = copy_to_user(buffer, current_ptr, to_copy);
    if(not_copied){
        printk(KERN_ALERT "Unknown error occurred when copying data into userspace\n");
    }
    size_t copied = to_copy - (size_t) not_copied;
    current_ptr += copied;
    return (ssize_t) copied;
}

I'm not sure if this module should work in all cases and its implementation is sort of "idiomatic". I'm especially concerned about concurrency. I tried to access it concurrently from different processes (cat /proc/pfs_regfile), and it worked fine.


texts:
I'm a hobby programmer and have started 2 month ago with C++.  I had some knowledge of Python, and I learned it all through Internet tutorials.

I'm looking for your help, since I don't have a tutor I would like to have a comment about my programming style.  I used this small "library" to create the Snake4D game (see bottom of question for further information).

Here is a little program that handles mathematical vectors, 4D and 3D.  Specifically, I ask you to find any improvements in the code's readability and in the algorithms.

Here is the "vec.h" file:

class V4{
private:
    double coords[4];
 public:
    V4();
    V4(double x, double y, double z, double w);
    ~V4();

    void set_cooord(int i, double x); //set_coord (index of coord i, value x)

    double module();
    double operator[](unsigned int i);

    void operator =(V4);
    V4 operator +(const V4);
    V4 operator -(const V4);
    V4 operator *(double k); //moltiplication of vector by a scalar k

    double dot4(V4); //the dot product

    V4 cross(V4 u, V4 v,V4 w); //the cross product
};

Here is the "vec.cpp" file:

/****************
V4 class
*****************/
V4::V4(){
    //set all coords to 0. if it's called without specific coord
    coords[0] = 0.;
    coords[1] = 0.;
    coords[2] = 0.;
    coords[3] = 0.;
}

V4::V4(double x, double y, double z, double w){
    coords[0] = x;
    coords[1] = y;
    coords[2] = z;
    coords[3] = w;
}

V4::~V4(){}//nothing to destroy?

void V4::set_cooord(int i, double x){ //i index, x value
    coords[i] = x;
}

double V4::module(){
    double sum = 0;
    for(int i= 0; i<4; i++){
        sum += coords[i]*coords[i];
    }
    return sqrt(sum);
}

double V4::operator[](unsigned int i){
     //if(i > 4){return;}  //list index out of range ? how to throw give a compile error?
     return coords[i];
 }

void V4::operator =(V4 v1){
    for(int i= 0; i<4; i++){
        coords[i] = v1[i];
    }
}

V4 V4::operator+(V4 v1) {
    double c[4]; // array of 4 double which will be transfered to the returned vector
     for(int i= 0; i<4; i++){
         c[i] = coords[i] + v1[i];
     }
     return V4(c[0],c[1],c[2],c[3]);
 }

 V4 V4::operator-( V4 v1) {
     double c[4];
     for(int i= 0; i<4; i++){
         c[i] = coords[i] - v1[i];
     }
     return V4(c[0],c[1],c[2],c[3]);
 }

V4 V4::operator*(double k){
    double c[4];
    for(int i= 0; i<4; i++){
        c[i] = coords[i] * k;
    }
    return V4(c[0],c[1],c[2],c[3]);
}

double V4::dot4(V4 v1){
    double sum = 0;
    for(int i= 0; i<4; i++){
       sum += coords[i]*v1[i];
    }
    return sum;
}

// requires three external vector and doesn't take any coords of the actual vector
//intended use:
// V4 v1(x,y,z,w), v2(x,y,z,w), v3(x,y,z,w), result, op;
// result = op.cross(v1,v2,v3)
V4 V4::cross(V4 u, V4 v, V4 w){
    //source Steve Hollasch master thesis
    //partial product
    double A = (v[0]*w[1])-(v[1]*w[0]);
    double B = (v[0]*w[2])-(v[2]*w[0]);
    double C = (v[0]*w[3])-(v[3]*w[0]);
    double D = (v[1]*w[1])-(v[2]*w[1]);
    double E = (v[1]*w[3])-(v[3]*w[1]);
    double F = (v[2]*w[3])-(v[3]*w[2]);

    double x =  (u[1]*F)-(u[2]*E)+(u[3]*D);
    double y = -(u[0]*F)+(u[2]*C)-(u[3]*B);
    double z =  (u[0]*E)-(u[1]*C)+(u[3]*A);
    double wc = -(u[0]*D)+(u[1]*B)-(u[2]*A); //defined as wc to not conflic with w vector

    return V4(x,y,z,wc);
}

Here are the source (.h and .cpp) file.

I already see some improvements:

I use the vectors as public members, because if they are private, I cannot get access to all the really comfortable functions they offer.
Instead of giving vertex hand by hand for the generation of the hypercube, there should be an algorithm that does this.
It could be used a general purpose class V containing the basic methods and having a variable int dimension then specialize the V3 and V4 inheriting the class V.

PS: I've made a little clip that shows the Snake4D game written in C++, Qt and OpenGL.


texts: Calculate difference of two ranges given

texts:
Write a method to compute the difference between two ranges. A range is defined by an integer low and an integer high. A - B (A “minus” B) is “everything in A that is not in B”.

This is an interview question and here is my code:

void findDiff(int a1,int a2, int a3, int a4)
{
   cout<<"("<<a1<<","<<a2<<") - ("<<a3<<","<<a4<<") = \n";

   if(a1 < a3)
   {
         int end = (a2<a3) ? a2: a3-1;
         for(int i=a1; i<=end; i++) cout<<i<<"\t";
   }

   if(a4 < a2)
   {
         int start = (a1>a4) ? a1 : (a4+1);
         for(int i=start; i<=a2; i++) cout<<i<<"\t";
   }
   cout<<"\n";
}

Please look at the code and tell me if it correct and if I'm missing any case. Thanks a lot in advance :)


texts:
I've been playing with some genealogical files in XML lately and wanted to create a transform that would list, for each person, the person's name, the count of ancestors, and the list of ancestors.  Here's what I came up with, but I'd like to see if there's a better way to do this.
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
<xsl:output method="text"/>

<xsl:template match="/people">
    <xsl:apply-templates select="person"/>
</xsl:template>

<!-- for each person, print the name and ancestor count -->
<xsl:template match="person">
    <xsl:value-of select="@name"/>
    <xsl:text>(</xsl:text>
    <xsl:apply-templates select="." mode="count-kin"/>
    <xsl:text>): </xsl:text>
    <!-- now name all of the ancestors -->
    <xsl:apply-templates select="." mode="name-kin"/>
    <xsl:text>
</xsl:text>
</xsl:template>

<!-- recursively name the ancestors -->
<xsl:template match="parent" mode="name-kin">
    <xsl:value-of select="concat(@name,' ')"/>
    <xsl:variable name="myname" select="@name"/>
    <xsl:apply-templates select="/people/person[@name=$myname]" mode="name-kin"/>
</xsl:template>

<!-- recursively name the ancestors -->
<xsl:template match="person" mode="name-kin">
    <xsl:apply-templates select="parent" mode="name-kin"/>
</xsl:template>

<!-- recursively count the number of ancestors -->
<xsl:template match="parent" mode="count-kin">
    <xsl:variable name="myname" select="@name"/>
    <xsl:variable name="ancestor-count">
    <xsl:apply-templates select="/people/person[@name=$myname]" mode="count-kin"/>
    </xsl:variable>
    <xsl:value-of select="$ancestor-count"/>
</xsl:template>

<!-- recursively count the number of ancestors -->
<xsl:template match="person" mode="count-kin">
    <xsl:choose>
    <!-- only count ancestors if there are any -->
    <xsl:when test="count(parent)">
        <xsl:variable name="ancestor-count">
        <xsl:apply-templates select="parent" mode="count-kin"/>
        </xsl:variable>
        <xsl:value-of select="number($ancestor-count)+count(parent)"/>
    </xsl:when>
    <!-- no parents -->
    <xsl:otherwise>
        <xsl:value-of select="0"/>
    </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Here's a sample input file:
<?xml version="1.0" encoding="iso-8859-1"?>
<people>
<person name="adam"/>
<person name="eve"/>
<person name="cain">
    <parent name="adam"/>
    <parent name="eve"/>
</person>
<person name="henoch">
    <parent name="cain"/>
</person>
<person name="gladys"/>
<person name="frank">
    <parent name="henoch"/>
</person>
<person name="jose">
    <parent name="gladys"/>
    <parent name="frank"/>
</person>
</people>

Here's sample output from xsltproc on Linux:
adam(0):
eve(0):
cain(2): adam eve
henoch(3): cain adam eve
gladys(0):
frank(4): henoch cain adam eve
jose(6): gladys frank henoch cain adam eve


texts: Converting domain-specific regular-expressions to a list of all matching instances

texts:
There seem to be several questions floating around Stackexchange regarding how to take a python regular expression list the matching instances.  This problem is a bit different because 1) I'm need to start off with a domain-specific regex system, not just the standard python regex, and 2) the possible matches for any given location in string are limited to a specific 'alphabet' of characters.

The current implementation works, but I've got a nagging feeling there's a much more succinct way to do it.

import numpy as np

def shorthand_to_instances(shorthand,alphabet):
    """
    Converts the shorthand-notation for bioinformatics 'motifs'
    into the all the instances of sequences they would match.
    Brackets means "it could be any of these letters" in this location.
    Curly braces means "it's *not* one of these letters" in this location,
    it can be any letter in the given "alphabet" except these.
    Lowercase "x" means it could be any of the letters in the given
    "alphabet".

    >>> binf_motifs_to_sequences('T[AG]','AGCT')
    ['TA', 'TG']

    >>> binf_motifs_to_sequences('T{A}T','AGCT')
    ['TGT', 'TCT', 'TTT']

    >>> binf_motifs_to_sequences('G{AGC}AGTC','AGCT')
    ['GTAGTC']

    >>> binf_motifs_to_sequences('xGTC','AGCT')
    ['AGTC', 'GGTC', 'CGTC', 'TGTC']

    """
    instances=[]
    shc = shorthand
    while len(shc)>0:
        char = shc[0]
        assert char in alphabet+'['+']'+'{'+'}'+'x', "Failed assert: '{0}'".format(char)
        if char in alphabet:
            # it's in the alphabet
            if len(instances)==0:
                instances.append(char)
            else:
                for i in list(np.arange(len(instances))):
                    instances[i] += char
            shc = shc[1:]
        elif char=='[':
            # it's a 'one of these'
            chargroup,c,e = shc[1:].partition(']')
            if len(instances)==0:
                for char in chargroup:
                    instances.append(char)
            else:
                newinstances = []
                for i in list(np.arange(len(instances))):
                    stem = instances[i]
                    for char in chargroup:
                        newinstances.append(stem+char)
                instances = newinstances
            shc = shc[len(chargroup)+2:]
        elif char=='{':
            # it's a 'not this / not these'
            chargroup0,c,e = shc[1:].partition('}')
            chargroup = alphabet.translate(None,chargroup0)
            if len(instances)==0:
                for char in chargroup:
                    instances.append(char)
            else:
                newinstances = []
                for i in list(np.arange(len(instances))):
                    stem = instances[i]
                    for char in chargroup:
                        newinstances.append(stem+char)
                instances = newinstances
            shc = shc[len(chargroup0)+2:]
        elif char=='x':
            # it's an 'any char'
            chargroup = alphabet
            if len(instances)==0:
                for char in chargroup:
                    instances.append(char)
            else:
                newinstances = []
                for i in list(np.arange(len(instances))):
                    stem = instances[i]
                    for char in chargroup:
                        newinstances.append(stem+char)
                instances = newinstances
            shc = shc[1:]
        else:
            break

    return instances

if __name__ == "__main__":
    import doctest
    doctest.testmod()