texts:
I am wondering if there is any way to meaningfully shorten the chain of conditionals used here to compare two versions. struct VERSIONCODE represents a version number of the form major.minor.revision and CompareVersions returns EQUAL if they're equal; LHS_NEWER if vLHS is newer, and RHS_NEWER if vRHS is newer.

typedef enum
{
    EQUAL = 0,
    LHS_NEWER,
    RHS_NEWER
} ECOMPARISON;

// Specifically not named "VERSION" to avoid conflicting with common names from third-party libraries etc.
typedef struct
{
    int nMajor;
    int nMinor;
    int nRev;
} VERSIONCODE;

ECOMPARISON CompareVersions(VERSIONCODE vLHS, VERSIONCODE vRHS)
{
    if (vLHS.nMajor > vRHS.nMajor)
    {
        return LHS_NEWER;
    }
    else if (vLHS.nMajor < vRHS.nMajor)
    {
        return RHS_NEWER;
    }
    else// if (vLHS.nMajor == vRHS.nMajor)
    {
        if (vLHS.nMinor > vRHS.nMinor)
        {
            return LHS_NEWER;
        }
        else if (vLHS.nMinor < vRHS.nMinor)
        {
            return RHS_NEWER;
        }
        else// if (vLHS.nMinor == vRHS.nMinor)
        {
            if (vLHS.nRev > vRHS.nRev)
            {
                return LHS_NEWER;
            }
            else if (vLHS.nRev < vRHS.nRev)
            {
                return RHS_NEWER;
            }
            else// if(vLHS.nRev == vRHS.nRev)
            {
                return EQUAL;
            }
        }
    }
}


texts: Keeping the average user from opening two copies of my program

texts:
I know that this is an impossible task against someone determined, but I'd like to keep mid-average users from being able to open more than one copy of my program at a time.

Here's the quick code I've made:

const tmp = require('tmp'),
    fs = require('fs'),
    path = require('path');

// Randomly generated, constant in each copy of the app
const app_code = '19c2cefbfa76b4887a1d4664704a3fe0';

// Write protection here, although it doesn't help much
const lock = tmp.fileSync({ mode: 0444, prefix: app_code});

const filedir = path.dirname(lock.name);
const filename = path.basename(lock.name);

// Search for already running copies
fs.readdirSync(filedir).forEach(f => {
    // If it's made by the app and it's not mine
    if (f.includes(app_code) && f !== filename) {
        // Delete my lock and close
        fs.unlink(lock.name);
        process.exit(1);
    }
});

// Watch for my own lock being deleted
fs.watch(filedir, function(event, who) {
    if (event === 'rename') {
        // If my lock doesn't exist or a new lock has been made
        if (!fs.existsSync(lock.name) || who.includes(app_code)) {
            // Delete my lock and close
            fs.unlink(lock.name);
            process.exit(1);
        }
    }
});

// Main program content, use placeholder here:
setInterval(() => console.log("Running alone"), 1000);

There are a few issues that I see right off the bat:

Different tmp folders being used
Locks being left in place if the program is terminated, meaning no copies could run

Anything obvious I'm missing?


texts:
Simple static class that will write out a message that is passed in.  The SpriteSheet is 256x256 and the A-Z starts at line 240 and the 0-9 starts at 248.  Each character is 8x8.  I hate the if (ix >= 32) and wondered if there is a tidier way of doing this?  I'm pleased it's only one Draw call, but it looks ugly.

public static class Font
{
    private static String chars = "" + //
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ      " + //
        "0123456789.,!?'\"-+=/\\%()<>:;     " + //
        "";

    public static void Draw(string msg, SpriteSheet sprites, SpriteBatch spriteBatch, int x, int y, Color color)
    {
        msg = msg.ToUpper();
        for (int i = 0; i < msg.Length; i++)
        {
            int ix = chars.IndexOf(msg[i]);
            int w = 0, h = 240;
            if (ix >= 0)
            {
                if (ix >= 32)
                {
                    w = 32;
                    h = 248;
                }
                spriteBatch.Draw(sprites.Sheet, new Rectangle(x + i * 8, y, 8, 8), new Rectangle((ix - w) * 8, h, 8, 8), color);
            }
        }
    }
}


texts: Parser Composition - a parser directive for Boost Spirit X3

texts:
While exploring Boost Spirit X3 (documentation), I noticed that I couldn't find a parser directive that composed parsers. I dug through the source code to see how the existing directives were made, and built one.

The idea is to have a parser A with an attribute that can be coerced to std::string, and a parser B; compose(B)[A] would run B on the output of A.

I'm a bit worried that I re-invented the wheel, or that what I did goes against the spirit of Spirit X3. I'm also worried about the efficiency or usability of it - I use x3::parse internally, which might also make error handling hard.

compose.hpp

#pragma once

#include <boost/spirit/home/x3.hpp>

namespace x3_extra {
    namespace x3 = boost::spirit::x3;

    template <typename Primary, typename Secondary>
    class compose_directive
        : public x3::unary_parser<Primary, compose_directive<Primary, Secondary>>
    {
        Secondary secondary_;

    public:
        using base_type = x3::unary_parser<Primary, compose_directive<Primary, Secondary>>;
        static constexpr bool has_attribute = true;
        using attribute_type = typename x3::traits::attribute_of<Secondary, void>::type;

        template <typename TPrimary, typename TSecondary>
        compose_directive(TPrimary&& primary, TSecondary&& secondary)
            : base_type{std::forward<TPrimary>(primary)}
            , secondary_{std::forward<TSecondary>(secondary)}
        {}

        template <typename Iterator, typename Context, typename RContext, typename Attribute>
        bool parse(Iterator& first, Iterator const& last, Context const& context,
                   RContext& rcontext, Attribute& attr) const
        {
            std::string primaryResult;
            bool isPrimarySuccess
                = this->subject.parse(first, last, context, rcontext, primaryResult);
            if (!isPrimarySuccess) return false;

            auto secondaryFirst = primaryResult.begin();
            auto secondaryLast = primaryResult.end();

            attribute_type result;
            auto const isSecondarySuccess
                = x3::parse(secondaryFirst, secondaryLast, secondary_, result);
            if (!isSecondarySuccess) return false;
            if (secondaryFirst != secondaryLast) return false;

            x3::traits::move_to(result, attr);
            return true;
        }
    };

    template <typename Secondary>
    struct compose_gen
    {
        Secondary secondary;

        template <typename TPrimary>
        auto operator[](TPrimary&& primary) const&
        {
            return compose_directive<std::decay_t<TPrimary>, Secondary>{
                std::forward<TPrimary>(primary), secondary};
        }

        template <typename TPrimary>
        auto operator[](TPrimary&& primary) &&
        {
            return compose_directive<std::decay_t<TPrimary>, Secondary>{
                std::forward<TPrimary>(primary), std::move(secondary)};
        }
    };

    template <typename Secondary>
    auto compose(Secondary secondary)
    {
        return compose_gen<Secondary>{std::move(secondary)};
    }
}

Example usage:

// parse ints with underscores ('_') in the middle
auto recognizeInt = x3::char_("0-9") >> *(*x3::lit('_') >> +x3::char_("0-9"));
// convert said strings into ints
auto parseInt = x3_extra::compose(x3::int_)[recognizeInt];


texts: Repetitive jQuery click functions for changing multiple elements

texts:
I'm using the following code to change elements text and classes when a click event is fired. My question is: Is there a better way to go about doing this that limits how repetitive the functions are? It seems extremely messy and like there would be a better way to go about structuring this.

function updateAveragesDropdown(element, text) {
    // Update text
    $("#timeframe_dropdown_container .dropdown-toggle p").text(text);

    // Remove active class
    $("#timeframe_dropdown_container #dropdown_today").parent("li").removeClass("active");
    $("#timeframe_dropdown_container #dropdown_week").parent("li").removeClass("active");
    $("#timeframe_dropdown_container #dropdown_month").parent("li").removeClass("active");

    // Update nav-tabs
    $("#timeframe_pills_container #tab_today").parent("li").removeClass("active");
    $("#timeframe_pills_container #tab_week").parent("li").removeClass("active");
    $("#timeframe_pills_container #tab_month").parent("li").removeClass("active");

    // Add active class
    $(element).parent("li").addClass("active");
}

function removeDropdownClasses() {
    $("#timeframe_dropdown_container #dropdown_today").parent("li").removeClass("active");
    $("#timeframe_dropdown_container #dropdown_week").parent("li").removeClass("active");
    $("#timeframe_dropdown_container #dropdown_month").parent("li").removeClass("active");
}

function updateDropdownElement(element, text) {
     $(element).parent("li").addClass("active");
     $("#timeframe_dropdown_container .dropdown-toggle p").text(text);
}

// Clear active labels on dropdown menu
$("#timeframe_dropdown_container #dropdown_today").click(function(e) {
    updateAveragesDropdown(this, "Today");
    $("#timeframe_pills_container #tab_today").parent("li").addClass("active");
});

$("#timeframe_dropdown_container #dropdown_week").click(function(e) {
    updateAveragesDropdown(this, "7-Day");
    $("#timeframe_pills_container #tab_week").parent("li").addClass("active");
});

$("#timeframe_dropdown_container #dropdown_month").click(function(e) {
    updateAveragesDropdown(this, "30-Day");
    $("#timeframe_pills_container #tab_month").parent("li").addClass("active");
});

$("#timeframe_pills_container #tab_today").click(function(e) {
    removeDropdownClasses();
    updateDropdownElement("#timeframe_dropdown_container #dropdown_today", "Today");
});

$("#timeframe_pills_container #tab_week").click(function(e) {
    removeDropdownClasses();
    updateDropdownElement("#timeframe_dropdown_container #dropdown_week", "7-Day");
});

$("#timeframe_pills_container #tab_month").click(function(e) {
    removeDropdownClasses();
    updateDropdownElement("#timeframe_dropdown_container #dropdown_month", "30-Day");
});

HTML

Today

7-Day

30-Day

        <button class="btn btn-default dropdown-toggle" type="button" id="timeframe_dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">

Today

        </button>

Today

7-Day

30-Day


texts:
I'm trying to compute de Bruijn sequences for alphabets which have a number of characters which is a power of two. So the sequence must contain every possible subsequence of length \$N\$ from \$0\$ to \$2^n\$ as a sequence of consecutive characters exactly once.

I need to use only 2 digits (0 and 1) with a length \$N \le 20\$ so \$B(2, N)\$.

The idea is quite simple, I have implemented it but I have time restriction 0.5 sec and from \$N=16\$ to \$N=20\$ I will get Time Limit Exceeded (I didn't submit the code yet because that programming contest web evaluator is not working at the moment). Using my computer I get for \$N=15\$ somewhere between \$0.2\$ to
\$0.3\$ seconds but for \$N=16\$ I get \$1.180\$ seconds which is not good at all.

  Prefer Ones rule: Write \$n\$ zeros. Then, always write a one unless it would cause the repetition of an \$n\$-length string; otherwise, write a zero.

This is my code without libraries declaration:

int construct(int n, string ans){
    int number=0, poz=0;
    for(int i=ans.length()-n; i<ans.length(); ++i, ++poz)
        if(ans[i]=='1')
            number ^= (1<<poz);
    return number;
}

int main ()
{
    int n, aux, usedN=1; // usedN counts the combinations
                         // I created in my sequence (till 2^n)
    fin>>n;
    aux=n;

    bool used[(1<<n)+1];
    string ans;
    memset(used, 0, sizeof(used));
    used[0]=1; // that's why I have usedN = 1

    while(aux--)
        ans.push_back('0');

    while(usedN!=(1<<n)) {
        ans.push_back('1'); // first I try with an '1'

        int number=construct(n, ans); // I construct as a number
                                      // the new combination created
                                      // of length N
                                      // with the last added digit, the
                                      // '1' in this case to check if I already
                                      // have it in my de Bruijn sequence (in ans)
        if(used[number]==1) {
            ans.pop_back();
            ans.push_back('0');
            used[construct(n, ans)]=1;
        } else
            used[number]=1;

        ++usedN;
    }

    cout<<ans.length()<<'\n'<<ans;
    return 0;
}

I'm mostly looking for optimizations so that my running time gets considerably slower, but feedback on all other aspects of my code would be great too!


texts:
I have written Command Class & CommandCollection class.

Every command will have instance of Command object and it get assigned in the CommandCollection object.

Usage:

$commandCollection = new CommandCollection();

$data = ['domain' => 'www.domain.com'];

// version 4 and 5 is belong to script_one.sh file under add-site name
$commandCollection->addCommand('add-site', 'path/script_one.sh', $data)
                   ->forVersion([4, 5]);

$commandCollection->addCommand('add-site', 'path/script_two.sh', $data)
                   ->forVersion([6]);

View the shell script of version 5 (path/script_one.sh)

echo $commandCollection->viewCommand('add-site', 5);

I will be using CommandCollection object in some of Manager classes such as SitesManager class and CronJobManager class

What do you think of my code and is there anything could be improved or refactored?

CommandCollection Class

class CommandCollection
{
    private $commandFiles;

    public function __construct()
    {
        $this->commandFiles = collect([]);
    }

    public function addCommand($name, $filename, $data)
    {
        $nameFilter = $this->commandFiles->where('name', $name)->where('filename', $filename);

        if (!$nameFilter->isEmpty()) {
            throw new \Exception('Already exists!');
        }

        $command = new Command($name, $filename, $data);
        $this->commandFiles->push($command);

        return $command;
    }

    public function viewCommand($name, $version)
    {
        foreach ($this->commandFiles as $commandFile) {
            if ($commandFile->name == $name && $commandFile->hasVersion($version)) {
                return $commandFile->render();
            }
        }
    }
}

Command Class

class Command
{
    protected $name;
    protected $filename;
    protected $version;

    protected $data;

    public function __construct($name, $filename, $data)
    {
        $this->name = $name;
        $this->filename = $filename;
        $this->data = $data;

        $this->version = collect();
    }

    public function forVersion($versions)
    {
        foreach ($versions as $version) {
            $this->version->push($version);
        }
    }

    public function render()
    {
        return view($this->filename, $this->data)->render();
    }

    public function hasVersion($number)
    {
        return $this->version->contains($number);
    }

    public function __get($property)
    {
        if (property_exists($this, $property)) {
            return $this->$property;
        }
    }
}


texts: jQuery datatables data rendering from mongodb

texts:
I am using jQuery data Tables server processing  to show some data to user. The documentation instructs that the response that the server sends must have this data:

recordsTotal
recordsFiltered
data

when I am trying to provide this data, I have to query the database 3 times! One for total document count, once for filtered data count and once for the data itself. The whole point of using serverSide processing is to increase the whole performance, but this is too much pain for the server. Any suggestions?

PS1: I have around 6000 documents in Tests collection, so client side rendering is not an option.

PS2: Because I am using $lookup I had to use aggregation for search

PS3: I know the code is a mess right now! (But it's working)

app.post('/getTests', (req, res) => {
    var searchStr = {};
    if (req.body.search['value']) {
        var regex = new RegExp(req.body.search.value, "i");
        searchStr =  { $or: [{'name': regex }, {'user.lastName': regex}, {'category.title': regex }] };
    }

     const userLookup = {
        $lookup: {
            from: 'users',
            localField: 'melliCode',
            foreignField: 'melliCode',
            as: 'user'
        }
    };

    const categoryLookup = {
        $lookup: {
            from: 'categories',
            localField: 'category',
            foreignField: '_id',
            as: 'category'
        }
    };

    // QUERY NO 1
    Test.find().estimatedDocumentCount({}, function(err, c) {

        if(err) console.log(err);
        let recordsTotal = c;

        // QUERY NO 2
        Test.aggregate([
            userLookup,
            categoryLookup,
            {
                $match: searchStr
            },
            {
                $count: "filtered"
            }
            ])
            .then(count => {

            const recordsFiltered = count[0].filtered;

            // QUERY NO 3
            Test.aggregate([
                userLookup,
                categoryLookup,
                {
                    $unwind: '$user'
                },
                {
                    $unwind: '$category'
                },
                {
                    $match: searchStr
                },
                {
                    $skip: Number(req.body.start) || 0
                },
                {
                    $limit: Number(req.body.length),
                }

            ])
                .then(results => {

                    if(!results) return res.send('error');

                    res.json({
                        "draw": req.body.draw,
                        "recordsFiltered": recordsFiltered,
                        "recordsTotal": recordsTotal,
                        "data": results
                    });
                })
                .catch((error) => {
                    if(error) console.log(err);
                    res.send(error)
                })
        })
            .catch((error) => {
                console.log(error);
            })
    })
})


texts: The game of life with a truly infinite board

texts:
The game of life is often implemented by representing the board as a 2D boolean array.  This doesn't scale very well to larger boards -- it starts to consume lots of memory, and without some separate mechanism to keep track of a list of live cells, you have to visit each board cell on each iteration.  This implementation just keeps a list of live cells to represent the board state; the board "size" is limited only by the maximum of an integer.

import Data.List as L
import Data.Map  as M

type Coo = (Int,Int)
type Board = Map Coo Int

moveBoard::Coo->Board->Board
moveBoard (dx,dy) = M.mapKeysMonotonic (\(x,y)->(x + dx, y + dy))

countNeighbors::Board->Board
countNeighbors b =
  unionsWith (+) [ moved (-1, -1),  moved (0, -1),  moved (1, -1),
                   moved (-1,  0),                  moved (1,  0),
                   moved (-1,  1),  moved (0,  1),  moved (1,  1) ]
    where moved (dx, dy) = moveBoard (dx, dy) b

lifeIteration::Board->Board
lifeIteration b = M.union birth survive
  where neighbors = countNeighbors b
        birth     = M.map (const 1)  (M.filter (==3) neighbors)
        survive   = M.intersection b (M.filter (==2) neighbors)

glider = M.fromList $ L.map (\(x,y)->((x,y),1::Int)) ([(1,1),(1,2),(1,3),(2,3),(3,2)]::[(Int,Int)])


texts: Number of Google search results over a period of time, saved to database

texts:
I am writing a Python script that scrapes data from Google search results and stores it in a database. I couldn't find any Google API for this, so I am just sending a HTTP GET request on Google's main site (and also Google News site). Then, using Beautiful Soup, I extract the number of search results found. I store this number in a mongo database using pymongo.

I want to collect the number of search results for specific keywords for next few months and then visualize the data. I would want something like this in the end:

Here is my code and I will run this following script four times a day using cron:

#! /bin/python

import re
import datetime

import pymongo
from pymongo import MongoClient
import requests
from bs4 import BeautifulSoup

REGEX = r'About (.*) results'
keywords = ['Barack Obama', 'Gandhi', 'Putin']

def number_of_search_results(key):
    def extract_results_stat(url):
        headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0'
        }
        search_results = requests.get(url, headers=headers, allow_redirects=True)
        soup = BeautifulSoup(search_results.text)
        result_stats = soup.find(id='resultStats')
        m = re.match(REGEX, result_stats.text)
        # print m.group(1)
        return int(m.group(1).replace(',',''))

    google_main_url = 'https://www.google.co.in/search?q=' + key
    google_news_url = 'https://www.google.co.in/search?hl=en&gl=in&tbm=nws&authuser=0&q=' + key
    return (extract_results_stat(google_main_url), extract_results_stat(google_news_url))

if __name__ == '__main__':
    conn = MongoClient()
    db = conn['search_results']
    current_time = datetime.datetime.utcnow()
    for key in keywords:
        google_main, google_news = number_of_search_results(key)
        # print key, google_main, google_news
        db.search_results.insert({'time': current_time, 'name': key, 'google_main': google_main, 'google_news': google_news})

I am new to databases, so is my mongo schema good for my project?
Any general improvements?


texts: Using Node.js, Express, and Express Router to implement a very simple REST API

texts:
To learn the ropes of Node.js and Express, I'm trying to implement a very simple REST API. The API says what it should do instead of doing it, so I can focus on the framework's structure. The requirement for the API is the following:

Mock API for a question and answer game.
/qanda endpoint: Repository of all question-answer pairs.
/qanda/questionId endpoint: A specific question-answer pair.
Should handle HTTP verbs GET, POST, PUT, DELETE.
Assume HTTP request body is JSON.

All this is arbitrary. I'm just trying to get the hang of dealing with and parsing of HTTP requests and responses. To keep things organized I'm also using Express Router and Node.js modules.
Here is index.js
'use strict';
const express = require('express');

const qanda = require('./routes/qanda.js');

const port = 9999;
const app = express();

app.use('/qanda', qanda);

app
  .all('/', (req, res, nxt) => {
  res
    .status(200)
    .send('Welcome!');
  })
  .listen(port, () => {
    console.log(`Listening to localhost:${port}`);
  });

And here is ./routes/qanda.js
'use strict';
const express = require('express');

const qandaRouter = express.Router();
qandaRouter.use(express.json());

qandaRouter
  .route('/')
  .get((req, res, nxt) => {
    res
      .status(200)
      .send('Sending all the qandas!');
  })
  .post((req, res, nxt) => {
    res
      .status(200)
      .send(`Adding ${req.body.q} = ${req.body.a}`);
  })
  .put((req, res, nxt) => {
    res
      .status(405)
      .send('PUT not supported.');
  })
  .delete((req, res, nxt) => {
    res
      .status(200)
      .send('Deleting all qands!');
  });

qandaRouter
  .route('/:qId')
  .get((req, res, nxt) => {
    res
      .status(200)
      .send(`Sending qanda ${req.params.qId}`);
  })
  .post((req, res, nxt) => {
    res
      .status(405)
      .send('POST not supported.');
  })
  .put((req, res, nxt) => {
    res
      .status(200)
      .send(`Updating ${req.params.qId} with ${req.body.q} = ${req.body.a}`);
  })
  .delete((req, res, nxt) => {
    res
      .status(200)
      .send(`Deleting qanda ${req.params.qId}`);
  });

module.exports = qandaRouter;

I'm looking forward to all kind of feedback! From style, to best practices, anti-patterns, whatever you can think of is very much appreciated! I do have some specific questions:

Is method chaining the usual way to handle something like this in Express?
Is the method chaining style readable and maintainable?
Is there a more concise way to achieve this?
What would you do differently?


texts: Find the max number in list after m operations between indices a and b

texts:
Given the problem:

  You are provided a list of N elements all initialized to 0, and a list of M operations. Where each m in M consists of 3 elements a, b, and k. For each m in M add k to all indices in the range [a, b]. At the end of all M operations print out the max.

I have two implementations, one of which is faster because of constant time lookups, whereas the other can be more memory usage efficient. I am looking for a third implementation which might run faster than the previous two.

#include <cassert>
#include <climits>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <vector>

void run_tests1(int N, int M, const std::vector<std::vector<int> > &a_b_k)
{
    std::vector<int> LIST(N);
    int max = INT_MIN;

    for (int m = 0; m < M; ++m) {
        int a = a_b_k[m][0];
        int b = a_b_k[m][1];
        int k = a_b_k[m][2];

        clock_t now = clock();
        for (int i = a; i <= b; ++i) {
            LIST[i] += k;
            if (LIST[i] > max) {
                max = LIST[i];
            }
        }
        clock_t end = clock() - now;
        std::cout << "Took " << ((float)end / CLOCKS_PER_SEC) << " on "
          << __FUNCTION__ << " to insert " << (b - a + 1) << std::endl;
    }
}

void run_tests2(int /*N*/, int M, const std::vector<std::vector<int> > &a_b_k)
{
    std::map<int, int> LIST;
    int max = INT_MIN;

    for (int m = 0; m < M; ++m) {
        int a = a_b_k[m][0];
        int b = a_b_k[m][1];
        int k = a_b_k[m][2];

        clock_t now = clock();
        for (int i = a; i <= b; ++i) {
            std::pair<std::map<int, int>::iterator, bool> status =
              LIST.insert(std::make_pair(i, 0));

            status.first->second += k;
            if (status.first->second > max) {
                max = status.first->second;
            }
        }
        clock_t end = clock() - now;
        std::cout << "Took " << ((float)end / CLOCKS_PER_SEC) << " on "
          << __FUNCTION__ << " to insert " << (b - a + 1) << std::endl;
    }
}

void run_tests3(int N, int M, const std::vector<std::vector<int> > &a_b_k)
{
    // Choose Data Structure
    int max = INT_MIN;

    for (int m = 0; m < M; ++m) {
        int a = a_b_k[m][0];
        int b = a_b_k[m][1];
        int k = a_b_k[m][2];

        clock_t now = clock();
        // Run Algorithm
        clock_t end = clock() - now;
        std::cout << "Took " << ((float)end / CLOCKS_PER_SEC) << " on "
          << __FUNCTION__ << " to insert " << (b - a + 1) << std::endl;
    }
}

int main()
{
    const int K = 1000;
    const int N = 10000000;
    const int M = 10;
    std::vector<std::vector<int> > data(M);

    for (int m = 0; m < M; ++m) {
      data[m].resize(3);
      data[m][0] = rand() % N;
      data[m][1] = rand() % N;
      data[m][2] = rand() % K;

      if (data[m][0] > data[m][1]) {
          std::swap(data[m][0], data[m][1]);
      }
    }

    run_tests1(N, M, data);
    run_tests2(N, M, data);
    run_tests3(N, M, data);
}


texts: Animation of adding / removing nodes from VBox

texts:
Method hide_old_show_new (I can't think of a suitable name) gradually, using FadeTransition, removes all nodes (from last to first) and then adds new nodes (from first to last)

But, it is unnecessarily complex.

Do you know how to simplify it? Or a better algorithm? I'll be grateful for the improvement.

However, I do not want to use an algorithm using *.fxml.

import java.util.ArrayList;
import javafx.animation.FadeTransition;
import javafx.scene.text.Text;
import javafx.util.Duration;

public final class Cart extends javafx.scene.layout.VBox {

    public Cart(double x, double y, double width, double height) {
        super();
        setLayoutX(x);
        setLayoutY(y);
        setWidth(width);
        setHeight(height);
    }

    public FadeTransition hide_old_show_new(ArrayList<Text> nodes) {
        var transition = new FadeTransition();
        var size = getChildren().size();
        if (size > 0 && !getChildren().get(0).equals(nodes.get(0))) {
            transition = new FadeTransition(Duration.millis(200), getChildren().get(size - 1));
            transition.setFromValue(1);
            transition.setToValue(0);
            transition.setOnFinished((event) -> {
                getChildren().remove(size - 1);
                hide_old_show_new(nodes);
            });
            transition.play();
        } else if (size < nodes.size()) {
            getChildren().add(nodes.get(size));
            getChildren().get(size).setOpacity(0);
            transition = new FadeTransition(Duration.millis(200), getChildren().get(size));
            transition.setFromValue(0);
            transition.setToValue(1);
            transition.setOnFinished((event) -> {
                hide_old_show_new(nodes);
            });
            transition.play();
        }
        return transition;
    }
}


texts: How to do these extraction/computations efficiently?

texts:
I have a set of products(e.g. P0006, P0009, etc.) and a set of production lines (eg. L1H175, L2H175). A product can be produced on a subset of the lines set (and not necessarily all of them).

If we decide to produce a product on a production line(assuming that the line can actually produce it), we need to buy the appropriate raw materials (e.g. If we decide to produce P0009 on L1h175 we need to buy the set of the selected raw materials in the image). Each raw material has a price and a property(S or NS).

What I want the code to do:

For each product, return where it can be produced. For example, to see where P0009 can be produced, I need to look at all the rows of the fist columns and whenever I find it, I add the appropriate line id to the set of lines that produce that product.
Reversely, for each line, what are the products that it can produce.
For each (p,l) couple store the set of all need raw materials, to produce product p on l, the property of each raw material(coded as 1 if it's S and 0 otherwise) and the total price (I don't need individual prices).

Here is how I am doing it now:

def process(x, y, recipes_df):
    raw_materials = {}
    total_cost = 0
    for product, line, rm, rm_type, rm_price in \
    recipes_df[['product','line', 'rm','rm_type','rm_price']].values:
        if (x,y) == (product,line):
            total_cost += float(rm_price)
            raw_materials[rm] = 1 if rm_type == 'S' else 0
    return (raw_materials, total_cost)

lines_set = set(recipes_df['line'])
lines = []

rm_data = {}
products_set = set()
for line in lines_set:
    for row in recipes_df.itertuples(index=False):
        # if the line_id in this row correspond to line in the outer loop
        if row[1] == line:
            # extract the raw material information by using the process function.
            rm_data[row[0]] = process(row[0],row[1], recipes_df)

            # add the product to the set of products that can be manufactured on the line
            products_set.add(row[0])

            # add the informations to lines list
            lines.append((rm_data, products_set))

The file has more than 3000 lines, my approach takes a lot of time.

Observations:
0. There is no assumption about how the file is sorted, meaning that, I can find P0009 in line 1 and in line 3000, for example, with a lot of other products in between. Same remark for the lines.
1. The process function search in all the file every time it's cold for a couple (p,l), which may be inefficient
2. For every line in lines_set we browse all the file.

Question: How to do all of this more efficiently?

Edit : I did only 2. and 3., the 1. is very similar to 2.

Edit2: recipes_df is a pandas data frame

Edit3:  The link to the data


texts: Accessing the contents of a project's root directory in Python

texts:
This morning I was trying to find a good way of using os.path to load the content of a text file into memory, which exists in the root directory of my current project.

This approach strikes me as a bit hackneyed, but after some thought it was exactly what I was about to do, except with os.path.normpath(os.path.join(__file__, '..', '..'))

These relative operations on the path to navigate to a static root directory are brittle.

Now, if my project layout changes, these associations to the (former) root directory change with it. I wish I could assign the path to find a target, or a destination, instead of following a sequence of navigation operations.

I was thinking of making a special __root__.py file. Does this look familiar to anyone, or know of a better implementation?

my_project
|
| __init__.py
| README.md
| license.txt
| __root__.py
| special_resource.txt
| ./module
  |-- load.py

Here is how it is implemented:

"""__root__.py"""
import os
def path():
    return os.path.dirname(__file__)

And here is how it could be used:

"""load.py"""
import __root__
def resource():
    return open(os.path.join(__root__.path(), 'special_resource.txt'))


texts: Modifying an image URL based on hash options

texts:
The code I'm trying to improve modifies a URL according to the options passed in a hash:

{
  :image_aspect_ratio => "square",
  :image_size => 50
}

or

{
  :image_size => {
    :width => 50,
    :height => 60
  }
}

The code looks like this:

module X
  module Y
    class Z

def image_url
  if raw_info['picture'] && image_size_opts_passed?
    image_url_with_size_opts
  else
    raw_info['picture']
  end
end

def image_size_opts_passed?
  !!(options[:image_size] || options[:image_aspect_ratio])
end

def image_url_with_size_opts
  params_index = raw_info['picture'].index('/photo.jpg')
  if params_index
    raw_info['picture'].insert(params_index, image_params)
  else
    raw_info['picture']
  end
end

def image_params
  image_params = []
  if options[:image_size].is_a?(Integer)
    image_params << "s#{options[:image_size]}"
  elsif options[:image_size].is_a?(Hash)
    image_params << "w#{options[:image_size][:width]}" if options[:image_size][:width]
    image_params << "h#{options[:image_size][:height]}" if options[:image_size][:height]
  end
  image_params << 'c' if options[:image_aspect_ratio] == 'square'

  '/' + image_params.join('-')
end

Here's the stuff I hate about this code:

Lots of raw_info['picture'] being called, but I'm not sure if using a local variable is better than accessing a hash twice.
I'm seeing some duplication in the else branch in the image_url and image_url_with_size_opts methods but I don't know how to improve that.
This code is all inside a class which is inside a module which is inside another module, so I'm not sure if I could also memoize the image_params result.


texts:
Please check if my tables specs agree with business rules. Also, please explain the differences between CONSTRAINT, FOREIGN KEY and CONSTRAINT FOREIGN KEY.

I have four MySQL tables (USER, NAME, PATIENT and DOCTOR) with the following rules:

A patient must have a name
A patient can be a user
A doctor must have a name
A doctor must be a user
A patient can be a doctor and vice versa...
By 3 & 5, if a patient is a doctor that patient must be a user

CREATE TABLE USER (
userName VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL UNIQUE,
userId MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
userEmail VARCHAR(100) NOT NULL,
userPassword VARCHAR(64) NOT NULL,
PRIMARY KEY(userId))

CREATE TABLE PATIENT (
patientId MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
userId MEDIUMINT UNSIGNED,
patientBirthday CHAR(8), # yyyymmdd
patientBirthplace VARCHAR(50),
patientGender TINYINT(1) UNSIGNED, # 0:male 1:female
patientBloodType TINYINT(1) UNSIGNED,
PRIMARY KEY(patientId),
CONSTRAINT FOREIGN KEY(userId) REFERENCES USER(userId)
ON DELETE CASCADE ON UPDATE CASCADE)

CREATE TABLE DOCTOR (
doctorId MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
userId MEDIUMINT NOT NULL UNSIGNED,
nameId MEDIUMINT NOT NULL UNSIGNED,
PRIMARY KEY(patientId),
CONSTRAINT FOREIGN KEY(userId) REFERENCES USER(userId)
CONSTRAINT FOREIGN KEY(nameId) REFERENCES USER(nameId)
ON DELETE CASCADE ON UPDATE CASCADE)

CREATE TABLE NAME (
nameId MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
userId MEDIUMINT UNSIGNED,
lastName VARCHAR(100) NOT NULL,
firstName VARCHAR(100) NOT NULL,
PRIMARY KEY(nameId),
CONSTRAINT FOREIGN KEY(userId) REFERENCES USER(userId)
ON DELETE CASCADE ON UPDATE CASCADE)


texts:
This is a programming problem:

  Write a program that finds and displays all pairs of 5-digit numbers
  that between them use the digits 0 through 9 once each, such that the
  first number divided by the second is equal to an integer N so that:

abcde / fghij = N

  where each letter represents a different digit. The first digit of one
  of the numerals is allowed to be zero.

  Input

  Each line of the input file consists of a valid integer N. An input of
  zero is to terminate the program.

  Output

  Your program have to display ALL qualifying pairs of numerals, sorted
  by increasing numerator (and, of course, denominator). Your output
  should be in the following general form:

xxxxx / xxxxx = N

xxxxx / xxxxx = N

  In case there are no pairs of numerals satisfying the condition, you
  must write "There are no solutions for N.". Separate the output for
  two different values of N by a blank line.

This keeps giving me TLE. How can I speed it up?

#include <iostream>
#include <algorithm>

using namespace std;

int i[10] = {0,1,2,3,4,5,6,7,8,9};

int check(int helper, int n)
{
    int a[5] = {i[5],i[6],i[7],i[8],i[9]};
    sort(a,a+5);
    int p = 0;
    int comp = helper*n;
    while (p < 120)
    {
        next_permutation(a,a+5);
        if(comp == a[0]*10000 + a[1]*1000 + a[2]*100 + a[3]*10 + a[4])
            return 1;
        p++;
    }
    return 0;
}

int main()
{
    int n;
    int helper = 0;
    int boo = 0;
    string str;
    int j = 0;
    int a1,a2,a3,a4,a5;
    while (cin >> n)
    {
        while(j < 10)
        {
            i[j] = j;
            j++;
        }
        do
        {
            a1 = i[0];
            a2 = i[1];
            a3 = i[2];
            a4 = i[3];
            a5 = i[4];
            while (a1 == i[0] && a2 == i[1] && a3 == i[2] && a4 == i[3] && a5 == i[4])
                next_permutation(i,i+10);
            helper = i[0]*10000 + i[1]*1000 + i[2]*100 + i[3]*10 + i[4];
            if(check(helper,n))
            {
                cout << helper * n << "/" << i[0] << i[1] << i[2] << i[3] << i[4] << endl;
                boo++;
            }
        }while (helper * n <= 100000);
        if (boo == 0)
            cout << "There are no solutions for " << n << "." << endl;
        boo = 0;
    }
    return 0;
}


texts: Fahrenheit / Celsius temperature converter - follow-up

texts:
This is an updated version of the script Fahrenheit / Celsius temperature converter. I have changed the script to be non-interactive, using positional parameters instead of asking for user input once the script is running. In my testing of the script, everything seems to work as it should. But I am quite new to using positional parameters and I'm wondering if this is the best way of using them or if there might be a more streamlined approach.

help shift says:

shift: shift [n]
    Shift positional parameters.

    Rename the positional parameters $N+1,$N+2 ... to $1,$2 ...  If N is
    not given, it is assumed to be 1.

    Exit Status:
    Returns success unless N is negative or greater than $#.

Which leaves me still uncertain as to what that means exactly or how to implement it correctly. The script seems to work but I'd like to know if I could / should do anything differently.

#!/bin/bash

usage() {
    echo -e "usage: temp [-f][-c]|[number]|[-h]\nExample:\n\ttemp -f 54\n\ttemp -c 24.5\n"
}
re='^-?[0-9]+([.][0-9]+)?$'
if [ "$1" = "" ]; then
    usage
    exit 1
elif [[ ! "$2" =~ $re ]]; then
    usage
    exit 1
elif [[ "$3" != "" ]]; then
    usage
    exit 1
fi
while [ "$1" != "" ]; do
    case $1 in
        -f )    shift
                RES="$(echo "scale=2; (${1}-32)*5/9" | bc -q -l; printf "\b˚C")"
                echo "${1}˚F = "${RES}
                exit
                ;;

        -c )    shift
                RES="$(echo "scale=2; ${1}*9/5+32" | bc -q -l; printf "\b˚F")"
                echo "${1}˚C = "${RES}
                exit
                ;;

        -h )    usage
                exit
                ;;

        * )     usage
                exit 1
    esac
done


texts:
I have implemented the following methods in 1 and 2 and need feedback to improve my implementation. Any different approach and ideas are welcome.

  A static method, minRectangle, accepts an array of rectangles (objects of type Rectangle) and returns the rectangle with the smallest area. Create this method.
  A static method, selectRectangles, accepts an array of rectangles (objects of type Rectangle) and a color (an object of type String), and returns those rectangles (as an array) that are of the given color. Create this method.
  Create an array of rectangles (objects of type Rectangle). Then use the methods selectRectangles and minRectangle to determine the smallest of the rectangles in the array that are yellow.

public class Rectangle {

// the lengths of the rectangle’s sides
private double length;
private double width;

// the rectangle’s color
private String color;

public Rectangle(double length, double width, String color) {
    this.length = length;
    this.width = width;
    this.color = color;
}

public String getColor() {
    return color;
}

public double area() {
    return length * width;

}

public String toString() {
    return "<" + this.length + ", " + this.width + "," + this.color + ">";

}

public static Rectangle minRectangle(Rectangle[] rect) {

    Rectangle min = rect[0];
    for (int i = 0; i < rect.length; i++) {
        if (rect[i].area() < min.area()) {
            min = rect[i];
        }
    }
    return min;

}

public static Rectangle[] selectRectangles(Rectangle[] rect, String color) {
    int countRect = 0;
    for (int i = 0; i < rect.length; i++) {
        if (rect[i].getColor().equals(color))
            countRect++;

    }

    Rectangle[] Rect = new Rectangle[countRect];
    int companionVar = 0;
    for (int j = 0; j < rect.length; j++) {
        if(rect[j].getColor().equals(color))
        Rect[companionVar++] = rect[j];

    }
    return Rect;
}

public static void main(String[] args) {
    Rectangle[] rect = {new Rectangle(2, 3, "yellow"),
            new Rectangle(3, 5, "red"),
            new Rectangle(1, 1, "yellow"),
            new Rectangle(2, 2, "red"),
            new Rectangle(3, 4, "red"),
            new Rectangle(2, 8, "red"),
            new Rectangle(5, 2, "yellow")};

    System.out.println("Smallest yellow Rectangle:" + minRectangle(rect));
    Rectangle[] rectangles = selectRectangles(rect, "yellow");
    System.out.println(Arrays.toString(rectangles));
    }

}


texts: Searching for pseudo-palindromes ("semordnilaps")

texts:
I was interested to know which words of the English language, when spelled backwards, are still valid words (e.g. "drawer", which when spelled backwards is "reward"), so I wrote a small program to go through a dictionary text file to search for them.

I don't know if there's a word to describe them, but because of the similarity to palindromes, I decided to call them pseudo-palindromes.

So, I wanted to know:

Is there a more efficient way to search for pseudo-palindromes? I believe my approach has \$O(n*log(n))\$ complexity.  Is that right?
Are there ways to make my code more readable and/or maintainable?
Are there any other tips to enhance the performance or style?

#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>

using namespace std;

// Load a dictionary file (line-separated strings) into a vector<string> and return the vector.
vector<string> loadFromDic(string filename)
{
  ifstream file;
  vector<string> strings;
  string aux;

  file.open(filename.c_str());
  while(!file.eof())
  {
    getline(file,aux);
    strings.push_back(aux);
  }

  return strings;
}

int main()
{
  // Load the dictionary from memory.
  vector<string> strings = loadFromDic("english_dic.txt");

  // Sort it alphabetically.
  sort(strings.begin(),strings.end());

  vector<string> ok_strings; // Will hold the strings which passed the test.
  string aux;
  vector<string>::iterator it;

  // For each string:
  for(it = strings.begin() ; it != strings.end() ; it++)
  {
    // Reverse the string.
    aux = *it;
    reverse(aux.begin(),aux.end());

    // If the reversed string is a word contained in the dictionary, add it to the list.
    if(binary_search(strings.begin() , strings.end() , aux))
    {
      ok_strings.push_back(*it);
    }
  }

  // Sort ok_strings based on string length first and then alphabetically.
  sort
  (ok_strings.begin() , ok_strings.end() ,
    [](string a, string b)->bool
    {
      if(a.size() == b.size())
    return a < b;
      return a.size() < b.size();
    }
  );

  // Display the strings which passed the test.
  for(it = ok_strings.begin() ; it != ok_strings.end() ; it++)
  {
    cout << *it << endl;
  }

  return 0;
}

The dictionary file I'm using is inside this .zip (renamed the US.dic -> english_dic.txt)


texts: EscapeString Class for MySQL and JSON Using the Builder Pattern

texts:
This class is being used to build some complex strings JSON and MySql.
Note: I'm not using the JSon converter because is difficult to create the structures the at that I want them and MySQL require extra escape characters.
At a later date, I may consider upgrading the class using Regular Expressions or CreateObject("System.Collections.StringBuilder").  ANy advice on either of these methods would be appreciated.
EscapeString:Class
Attribute VB_Name = "EscapeString"
Attribute VB_PredeclaredId = True
Attribute VB_Ext_KEY = "Rubberduck" ,"Predeclared Class Module"
Option Explicit
'@PredeclaredId

Private Type TMembers
    Content As String
End Type
Private this As TMembers

Public Property Get Create(ByVal Content As String) As EscapeString
    With New EscapeString
        .Content = Content
        Set Create = .Self
    End With
End Property

Public Property Get Self() As EscapeString
    Set Self = Me
End Property

Public Property Get EscapeMySQL() As EscapeString
    Rem https://dev.mysql.com/doc/refman/8.0/en/string-literals.html
    Rem \\  A backslash (\) character
    Rem \"  A double quote (") character
    Rem \b  A backspace character
    Rem \n  A newline (linefeed) character
    Rem \r  A carriage return character
    Rem \t  A tab character
    Rem EscapeJSON escapes all the referenced characters to this point
    IIf False, Me.EscapeJSON, ""

    Rem \0  An ASCII NUL (X'00') character
    If InStr(Content, Chr(0)) > 0 Then Content = Replace(Content, Chr(0), "\0 ")
    Rem \'  A single quote (') character
    If InStr(Content, "'") > 0 Then Content = Replace(Content, "'", "\'")
    Rem \Z  ASCII 26 (Control+Z); see note following the table
    If InStr(Content, Chr(26)) > 0 Then Content = Replace(Content, Chr(26), "\Z ")
    Rem \%  A % character; see note following the table
    If InStr(Content, "%") > 0 Then Content = Replace(Content, "%", "\%")
    Rem \_  A _ character; see note following the table
    If InStr(Content, "_") > 0 Then Content = Replace(Content, "_", "\_")

    Set EscapeMySQL = Self
End Property

Public Property Get EscapeJSON() As EscapeString
    Rem https://bettersolutions.com/vba/strings-characters/ascii-characters.htm
    Rem Backslash to be replaced with \\
    If InStr(Content, "\") > 0 Then Content = Replace(Content, "\", "\\")

    Rem Backspace to be replaced with \b
    If InStr(Content, Chr(8)) > 0 Then Content = Replace(Content, Chr(8), "\b")

     Rem Newline to be replaced with \n
    If InStr(Content, vbNewLine) > 0 Then Content = Replace(Content, vbNewLine, "\n")

    Rem Form feed to be replaced with \f
    If InStr(Content, vbLf) > 0 Then Content = Replace(Content, vbLf, "\f")

     Rem Tab to be replaced with \t
    If InStr(Content, vbTab) > 0 Then Content = Replace(Content, vbTab, "\t")

    Rem Double quote to be replaced with \"
    If InStr(Content, Chr(34)) > 0 Then Content = Replace(Content, Chr(34), "\" & Chr(34))
    Set EscapeJSON = Self
End Property

Public Function ToString(Optional ContentWrapper As String = "") As String
        ToString = ContentWrapper & this.Content & ContentWrapper
End Function

Public Property Get Content() As String
    Content = this.Content
End Property

Public Property Let Content(ByVal Value As String)
    this.Content = Value
End Property

Examples


texts:
This is supposed to search for a node in a huge Treeview. The node could be found in many places. In other words there may be many nodes and the resulting tree should be expanded depending on where nodes are found.

I was asked to avoid recursivity in this method. The only solution I was able to get is using LINQ queries but I cannot since we are under 2.0 framework.

Can I avoid recursivity in this method?

  private void CheckFoundTreeNode(TreeNode startingTreeNode, List<Item> parentItems, ItemNode foundItemNode)
    {
        foreach (TreeNode childTreeNode in startingTreeNode.Nodes)
        {
            ItemNode itemNode = TreeBuilder.GetItemNode(childTreeNode);
            Item item = itemNode.Item;

            bool isParent;
            if (item.Ancestor == null)
                isParent = parentItems.Contains(item);
            else
            {
                Item ancestor = ((Grammar)item).DirectAncestor;
                isParent = parentItems.Contains(ancestor);
            }

            if (isParent)
            {
                childTreeNode.Expand();
                CheckFoundTreeNode(childTreeNode, parentItems, foundItemNode);
            }

            else if (itemNode == foundItemNode)
            {
                startingTreeNode.Expand();
                m_ListOfFoundNodes.Add(childTreeNode);
            }
        }
    }


texts:
I've written a python module that randomly generates a list of ideas that
can be used as the premises for furthur research. For instance, it would be useful in situations where a new thesis topic is required, either for a masters or a doctorate program. Or if someone is just simply bored with their lives and wants some brainstorming ideas about the kind of project that they would like to be involved in, this python module can be used.

The output is a list of random ideas or technologies. It is up to the user as to how they interpret the output. Each idea in the list can be assessed individually, or the ideas can be combined in ways never thought off before to create something new.

I would like some input on how else I can improve the project or what other features I may add. The project is hosted on github. The name of the project is ranto for lack of a better name.

For the impatient, here is the code from the sole python file in the project...

import random
import sys

def topics(f):
    f = open(f, 'r')
    wordlist = []

    for i in f:
        wordlist.append(i.strip())
    return wordlist

def mainp():
    wordlist = topics('../data/' + sys.argv[1])

    while True:
        print random.sample(wordlist, int(sys.argv[2]))
        if raw_input() == '':
            continue
        else:
            break

mainp()

If you want to test it out, you need to download one of the data files from the github repo linked earlier.

Any feedback would be appreciated.


texts:
I was bored the other day and got to wondering how I would implement a circular FIFO buffer in Rust. My goal was to create an implementation I could use on a bare metal microcontroller. This means no dynamic allocation and a fixed size known at compile time, so I used a primitive fixed size array instead of a vector as a backing store.

My next pass at this will be to make the type stored in the FIFO buffer generic and to find a way to specify the capacity, but I wanted to get some feedback before doing so. Am I missing any test cases? What can I improve here?

src/lib.rs

const FIFO_CAPACITY: usize = 32;

pub struct Fifo {
    size: usize,
    read_idx: usize,
    write_idx: usize,
    buffer: [u8; FIFO_CAPACITY]
}

impl Fifo {
    pub fn new() -> Fifo {
        Fifo {
            size: 0,
            read_idx: 0,
            write_idx: 0,
            buffer: [0; FIFO_CAPACITY]
        }
    }

    pub fn push(&mut self, item: u8) -> Result<(), &'static str> {
        if self.buffer_full() {
           Err("Buffer full.")
        } else {
            self.buffer[self.write_idx] = item;

            self.write_idx = Fifo::increment_index(self.write_idx);
            self.size = self.size + 1;

            Ok(())
        }
    }

    pub fn pop(&mut self) -> Option<u8> {
        if self.size == 0 {
            None
        } else {
            let result = self.buffer[self.read_idx];

            self.read_idx = Fifo::increment_index(self.read_idx);
            self.size = self.size - 1;

            Some(result)
        }
    }

    pub fn buffer_full(&self) -> bool {
        self.size == FIFO_CAPACITY
    }

    fn increment_index(idx: usize) -> usize {
        (idx + 1) % FIFO_CAPACITY
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pop_item_that_was_pushed_to_buffer() {
        let mut buffer = Fifo::new();
        assert!(buffer.push(5).is_ok());
        let pop_result = buffer.pop();
        assert_eq!(Some(5), pop_result);
    }

    #[test]
    fn popping_empty_buffer_returns_none() {
        let mut buffer = Fifo::new();
        assert_eq!(None, buffer.pop());
    }

    #[test]
    fn popping_returns_first_pushed_first() {
        let mut buffer = Fifo::new();
        assert!(buffer.push(1).is_ok());
        assert!(buffer.push(2).is_ok());

        assert_eq!(Some(1), buffer.pop());
        assert_eq!(Some(2), buffer.pop());
    }

    #[test]
    fn pop_beyond_write_index_returns_none() {
        let mut buffer = Fifo::new();
        assert!(buffer.push(1).is_ok());
        assert_eq!(Some(1), buffer.pop());
        assert_eq!(None, buffer.pop());
    }

    #[test]
    fn pop_beyond_write_index_continuing_on_works() {
        let mut buffer = Fifo::new();
        assert!(buffer.push(1).is_ok());
        assert_eq!(Some(1), buffer.pop());
        assert_eq!(None, buffer.pop());

        assert!(buffer.push(2).is_ok());
        assert_eq!(Some(2), buffer.pop());
    }

    #[test]
    fn buffer_wraps_around() {
        let mut buffer = Fifo::new();
        let capacity = FIFO_CAPACITY as u8;
        for x in 0..=(capacity * 3) {
            assert!(buffer.push(x).is_ok());
            assert_eq!(Some(x), buffer.pop());
        }
    }

    #[test]
    fn push_fails_if_buffer_is_full() {
        let mut buffer = Fifo::new();
        let capacity = FIFO_CAPACITY as u8;

        for x in 0..(capacity) {
            assert!(buffer.push(x).is_ok());
        }

        assert!(buffer.push(capacity + 1).is_err())
    }
}


texts: Optimization in overcoming TLE in interviewStreet problem

texts:
I have been trying to get the problem Quadrant Queries correct for a while now. Although my approach is an optimal one, I am getting a TLE on 3 test cases. Please help me optimize my approach so that I can pass all test cases for the problem.

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

typedef pair <int,int> pii;

void performCquery(int i,int j, const vector< pii > &pointList)
{
    i = i-1;
    j = j-1;
    int q1 = 0;
    int q2 = 0;
    int q3 = 0;
    int q4 = 0;

    for (int k = i; k <=j ; k++) {
        if ( (pointList[k].first > 0 ) && ( pointList[k].second > 0) ) {
            q1++;
        }
        else if ( (pointList[k].first < 0) && ( pointList[k].second > 0) ) {
            q2++;
        }
        else if  ( ( pointList[k].first < 0) && (pointList[k].second < 0 ) ) {
            q3++;
        }
        else if ( ( pointList[k].first > 0) && (pointList[k].second < 0 ) ) {
            q4++;
        }
    }

    cout << q1 << " " << q2 << " " << q3 << " " << q4 << endl;
}

void performXYquery(char qType, int i, int j, vector< pii > &pointList)
{
        i = i-1;
        j = j-1;
    for ( int k = i; k <= j ;k++) {
        pii pnt = pointList[k];
        if ( qType == 'X') {
            pointList[k].second = -pointList[k].second;
        }
        else if (qType == 'Y') {
            pointList[k].first = -pointList[k].first;
        }
    }
}

int main(int argc, char* argv[])
{
    int numOfPoints;
    cin >> numOfPoints;

    vector < pii > pointList;

    for (int i = 0; i < numOfPoints ; i++) {
        int x,y;
        cin >> x >> y;
        pii temp(x,y);
        pointList.push_back(temp);
    }

    int numOfQueries;
    cin >> numOfQueries;

    for (int i = 0; i < numOfQueries; i++) {
        char qName;
        int startIndex;
        int endIndex;
        cin >> qName >> startIndex >> endIndex;
        if (qName == 'C') {
            performCquery(startIndex,endIndex, pointList);
        }
        else if (qName == 'X' || qName == 'Y') {
            performXYquery(qName, startIndex,endIndex, pointList);
        }
        else {
            cerr << "Error: This line should not be printed" <<endl;
        }
    }

    return 0;
}


texts: Solving the Fibonacci series using a for loop

texts:
I have developed what I believe to be a novel way of coding the Fibonacci series in Java using a for loop. This code appears to work, but I was wondering if this is a good solution and how it could be improved.
 static int loopFibonacci(int num) {
        if(num <1) {
            throw new IllegalArgumentException("this number is not valid for fibonacci series");
        }
        int sum = 1;
        int prev = 0;
        for (int i=1;i<num;i++) {
            sum+=prev;
            prev = sum-prev;
//            System.out.println("sum="+sum+" , prev = "+prev);
        }
        return sum;
    }
}


texts: Complete graph traversal algorithm in Prolog

texts:
Given a table, I want to explore all possible transition between the elements in the table.
ex: for a table with size 3 [0,1,2], the output of the algorithm should be 0->1, 1->0, 0->2, 2->1, 1->2, 2->0.
I guess this can be regarded as traversing a complete graph.

I have an implementation of an algorithm doing this in Python:

def test1(n):
   theList=[]
   theList.append(0)

   for x in range(0,n+1):
      for y in range(n,x+1,-1):
         theList.append(y)
         theList.append(x)

      if x!=n:
         theList.append(x+1)

   for x in range(n,0,-1):
      theList.append(x-1)
   return theList

This code always start at element 0, and return a list of all the transitions.

But I need my algorithm i Prolog. So I have done an attempt to port the Python-code to Prolog. My main focus has been on writing readable and maintainable code. But I guess there is great room for improvement wrt. performance of the Prolog code:

:- use_module(library(clpfd)).
:- use_module(library(between)).
:- use_module(library(lists)).

dynappend( List, X ) :-
   (
   foreach( Item, List ),
   param(X,T)
   do
      var( Item ), var(T) ->
      Item = X ,
      T = 1
      ;
      true
   ).

s3(List,N):-
   LSize is N*(N+1)+1,
   length(List,LSize),

   dynappend( List, 0 ),
   (
      for(X1, 0, N ),
      param( N, List )
      do
         X1T is X1+2,
         ( between:numlist( X1T, N, YList ) -> true ; YList=[] ) ,
         lists:reverse(YList,RYList),

         (
            foreach(Y, RYList ),
            param( X1, List )
            do
               dynappend( List, Y ),
               dynappend( List, X1 )
         ),

         (   X1 #\= N -> X1T2 is X1+1, dynappend( List, X1T2 ) ;  true )
   ),
   N1 is N-1,
   numlist( 0, N1, XList ),
   lists:reverse(XList,RXList),
   (
   foreach(X2, RXList ),
   param(List)
   do
      dynappend( List, X2 )
   ).

Running the code:

| ?- s3(L, 2).
L = [0,2,0,1,2,1,0] ?
yes

Any suggestions for improvements of the Prolog code?


texts: Applying 'Uncle Bob's Clean Code' on this small exercice

texts:
I'm trying to apply Uncle Bob's Clean Code practices onto this exercise I'm helping someone with.
(The exercise assignment is in a "docstring" comment above the Main.)
I'm unsure what the best naming convention would be for ReturnAllCombinationsOfInputtedList, although I suspect it doesn't involve the return word in it's name for starters.
Same for CreateArray1to30(). Any suggestions for name choices/proper conventions here?
Do you guys see anything else that I should've done differently in your opinion?
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleAppHelpingAlexWithExercices
{
    class Program
    {
        //**************************************************************************
        // How many different exam tickets with two different questions can be made,
        // from a syllabus with 30 questions
        //**************************************************************************
        static void Main(string[] args)
        {
            int[] questionsList = CreateArray1to30();
            List<string> combinations = ReturnAllCombinationsOfInputtedList(questionsList);
            int amountOfUniqueCombinations = combinations.Distinct<string>().Count();
            Console.WriteLine("Total unique combinations: {0}", amountOfUniqueCombinations);
        }

        static List<string> ReturnAllCombinationsOfInputtedList(int[] questionsList)
        {
            List<string> combinations = new List<string>();
            int[] aTest = new int[2];
            foreach (int firstQuestion in questionsList)
            {
                foreach (int secondQuestion in questionsList)
                {
                    if (firstQuestion != secondQuestion)
                    {
                        aTest[0] = firstQuestion;
                        aTest[1] = secondQuestion;
                        //Makes AB and BA tests both AB, so we can de-duplicate later.
                        Array.Sort(aTest);
                        combinations.Add(String.Format("{0}&{1}", aTest[0], aTest[1]));
                        Console.WriteLine("{0} and {1}", aTest[0], aTest[1]);
                    }
                }
            }
            return combinations;
        }

        static int[] CreateArray1to30()
        {
            return Enumerable.Range(1, 30).ToArray();
        }
    }
}


texts: Python bot to answer mathematical questions for a remote server

texts:
The MSP Tech Club puts on an annual CTF, available here. I've worked on a solution but was wondering if there's a better way to write it or if I have any particular bad habits showing themselves?

The challenge is to write a script to connect to a server and answer math problems presented to it, it looks like this:

Answering 500 questions in a small enough timeframe reveals this:

I've left the netcat server in the code to allow for testing of changes. Since the CTF is public facing I don't think this poses an issue but feel free to remove if I'm breaking any S/O rules.

#!/usr/bin/python3

import socket
import re

if __name__ == '__main__':
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(('195.154.53.62', 1337))

    while True:
        data = b''
        while True:
            chunk = client.recv(4096)
            data += chunk
            if len(chunk) < 4096:
                break

        # our flag contains ALEXCTF, once it's revealed print recevied data and exit
        if 'ALEXCTF' in data.decode('utf-8'):
            print(data.decode('utf-8'))
            break

        # \d+ matches a digit (equal to [0-9])
        # .{3} matches any  character, except line terminators exactly three times
        m = re.search('\d+.{3}\d+', data.decode('utf-8'))
        expression = m.group(0)

        #properly handle division
        if '/' in expression:
            expression = expression.replace('/', '//')

        result = eval(expression)

        #print results to screen to see script progress
        print(expression + ' = ' + str(result))

        #encode and transfer
        data = str(result).encode('utf-8') + b'\n'
        client.send(data)


texts:
The task:

  A number is considered perfect if its digits sum up to exactly 10.

  Given a positive integer n, return the n-th perfect number.

  For example, given 1, you should return 19. Given 2, you should return
  28.

My solution:

const sum = (acc, m) => acc + Number(m);
const getNthPerfectNumber = n => {
  let i = 0, x = 0;
  while (i !== n) {
    x++;
    if (x
      .toString()
      .split('')
      .reduce(sum, 0) === 10) { i++; }
  }
  return x;
};

console.log(getNthPerfectNumber(2));


texts: KMeans in the shortest and most readable format

texts:
I'm learning Python (coming from Java) so I decided to write KMeans as a practice for the language. However I want to see how could one improve the code and making it shorter and yet readable.  I still find the code rather long. Also if you have comments regarding conventions or proper practices, I would really appreciate it.

import numpy as np
from numpy import linalg as LA

def main():
    #data = np.arange(20).reshape( (4,5) )
    file_name = "/Users/x/Desktop/tst.csv"
    with open(file_name) as f:
        ncols = len(f.readline().split(","))
    data = np.loadtxt(file_name, delimiter=",", usecols=range(0,ncols-1))
    kmeans = KMeans()
    kmeans.cluster(data, 3, 200)
    print kmeans.means

class KMeans:

    def __init__(self):
        self.means = None
        self.data_assignments = None

    #data is a 2D Numpy array
    def cluster(self, data, numClusters, iterations):
        if numClusters < 1:
            raise Exception("The number of clusters should be larger than 0.")
        if numClusters > data.shape[0]:
            raise Exception("The number of clusters is beyond the number of rows.")

        #Pick random means
        randomMeansIndices = np.random.choice( range( data.shape[0] ),
                                               size=numClusters, replace=False )

        for i in randomMeansIndices:
            if self.means is not None:
                self.means = np.vstack( ( self.means, data[ i ] ) )
            else:
                self.means = data[ i ]

        for iteration in xrange(iterations):
            #Data assignment
            self.data_assignments = {}
            for row in data:
                distances = {}
                meanID = 0
                for mean in self.means:
                    distances[meanID] = LA.norm(mean - row)
                    meanID += 1
                nearestMean = min( distances, key=lambda x: distances[x] )
                if nearestMean in self.data_assignments:
                    self.data_assignments[nearestMean] = np.vstack( (self.data_assignments[nearestMean], row) )
                else:
                    self.data_assignments[nearestMean] = row

            #Update the means
            self.means = None
            for mean_data_matrix in self.data_assignments.values():
                if mean_data_matrix.ndim == 1:
                    mean_data_matrix = mean_data_matrix.reshape((1,-1)) #reshape to make it 2D
                if self.means is None:
                    self.means = np.mean( mean_data_matrix, axis=0 ) #the first updated mean vector
                else:
                    newMean = np.mean( mean_data_matrix, axis=0 ) #updated mean vector
                    self.means = np.vstack( (self.means, newMean) )#add an updated mean

main()


texts: CSV Creation from two tables with URL for user to click on

texts:
The code works but with an increase in data it's now slower so needs a tweak so looking for pointers.

'To import into Module
    Dim sConnString As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("SQL").ConnectionString
        Dim vDateNow As Date = Now()
    vDateNow = vDateNow.ToString("dd-MMMM-yyyy")
    Dim vDateNowStr As String = Replace(vDateNow, "/", "-")
    Dim vURL As String = "C:\Sites\Website\www\Reports\"

Private Sub PrcBespokeReports(vDateNowStr As String, vURL As String)
        '"#######################################################
        Dim vReportName As String = "Overall Risk Category"
        '"#######################################################
        '"CSV create code
        '"######## Define the string
        Dim str As New StringBuilder
        '"######## Define the string
        '"########Connection code
         Dim dsNames As New SqlDataSource
        dsNames.ConnectionString = sConnString
        Dim sSQL As String = ""
        Dim sSQLAttach As String = ""

'Main code

        sSQL = "SELECT a.ID AS ID,  a.UPRN,p.BuildingNo,p.Street,p.Postcode ,a.ItemRef,a.SurveyDate ,a.OverallRiskCategory ,a.SurveyorsComments FROM TblAsbestos AS a JOIN TblProperty As p on a.UPRN = p.UPRN ORDER BY a.OverallRiskCategory"

        dsNames.SelectCommand = sSQL
        '"########Connection code
        '"########Loop code to get data
        str.Append(Chr(34) & "Report By Site to show ACM: - " & vDateNowStr & Chr(34) & ",")
        str.Replace(",", vbNewLine, str.Length - 1, 1) ' go to next line

        Dim arrNames As New ArrayList
        With arrNames
            .Add("UPRN")
            .Add("BuildingNo")
            .Add("Street")
            .Add("Postcode")
            .Add("ItemRef")
            .Add("SurveyDate")
            .Add("OverallRiskCategory")
            .Add("SurveyorsComments")
        End With

        For i As Integer = 0 To arrNames.Count - 1
            str.Append("=" & Chr(34) & arrNames(i).ToString() & Chr(34) & ",")
        Next

        str.Replace(",", vbNewLine, str.Length - 1, 1) ' go to next line

    'The above sets the headers and the array

        For Each datarow As Data.DataRowView In dsNames.Select(DataSourceSelectArguments.Empty)

            'Create the string / string builder and bring in the datarows
            For i As Integer = 0 To arrNames.Count - 1
                str.Append("=" & Chr(34) & Trim(Replace(If(TryCast(datarow(arrNames(i).ToString).ToString, String), ""), vbTab, " ") & Chr(34) & ","))
            Next
            '"########
            str.Replace(",", vbNewLine, str.Length - 1, 1) ' go to next line
        Next
    'Completed
        str.Append("Searched:" & sSQLAttach)

        '"Create CSV file or totally overwrites all data in CSV file
        My.Computer.FileSystem.WriteAllText((vURL & vDateNowStr & "_" & vReportName & ".csv"), str.ToString & "Created " & vDateNowStr, False)
        'Show CSV Link
        Dim vHyperLinkPath As String
        vHyperLinkPath = "http://Website.Net/Reports/" & vDateNowStr & "_" & vReportName & ".csv"
        CSVLinkName.Visible = vHyperLinkPath
        CSVLinkName.Visible = True

    End Sub


texts: LRU Cache with a static variable for garbage value

texts:
I have tried to implement a Least Recently Used cache with only C++ STL containers. The main thing that I keep asking my self the static variable called garbage_val. Is it a good practice to have such a static variable just for garbage values?

template<typename Key, typename Value>
class LRU_Cache
{
struct Node
{
    Key k;
    Value v;
};

public:
static Value garbage_val;

LRU_Cache(unsigned int capacity)
{
    capacity_ = capacity;
}

Value get(Key key)
{
    auto node = cache_.find(key);

    if(node == cache_.end())
    {
        return garbage_val;
    }

    Value val = (*(node->second)).v;
    recentlyList_.erase(node->second);

    Node n; n.v = val; n.k = key;

    recentlyList_.push_back(n);
    cache_[key] = --recentlyList_.end();

    return val;
}

void set(Key key, Value val)
{
    auto node = cache_.find(key);

    if(node != cache_.end())
    {
        recentlyList_.erase(node->second);
    }
    else
    {
        evict_if_needed();
    }

    Node n; n.v = val; n.k = key;
    recentlyList_.push_back(n);
    cache_[key] = --recentlyList_.end();
}

void evict_if_needed()
{
    if(cache_.size() >= capacity_)
    {
        auto node = cache_.find(recentlyList_.begin()->k);
        recentlyList_.pop_front();
        cache_.erase(node);
    }
}

virtual ~LRU_Cache(void)
{
}

void print()
{
    std::cout << "Objects in Memory:" << std::endl;
    for(auto& c : cache_)
    {
        std::cout << "(" << c.first << "," << (*(c.second)).v << ")" << std::endl;
    }

    std::cout << "Recently used:" << std::endl;
    for(auto& r : recentlyList_)
    {
        std::cout << "(" << r.k << "," << r.v << ")" << std::endl;
    }
}
private:
LRU_Cache(void){}

std::unordered_map<Key, typename std::list<Node>::iterator> cache_;
std::list<Node> recentlyList_;
unsigned int capacity_;
};
template<typename Key,typename Value> Value LRU_Cache<Key,Value> ::garbage_val;


texts: How do I improve this logging mechanism?

texts:
There are quite a few things I am considering:

I will have to check for null values
I will have serious trouble persisting it in the database.

How do I improve this or re-factor this for better results?

public class ErrorLog
    {
        public static void LogError(Exception e)
        {
            var exceptionLog = new Dictionary<String, String>
                                   {
                                       {"Inner Exception", e.InnerException.Message},
                                       {"Message", e.Message},
                                       {"Source", e.Source},
                                       {"StackTrace", e.StackTrace},
                                       {"MethodName",e.TargetSite.Name}
                                   };
            foreach (KeyValuePair<String, String> kvp in e.Data)
                exceptionLog.Add(kvp.Key, kvp.Value);
        }
    }

Update with the new and simpler class

  public class ErrorLog
    {
        public static void LogError(Exception e)
        {
            var innerExcpetionMessage = e.InnerException == null ? "Null" : e.InnerException.Message;
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                    var cmd = new SqlCommand("ApplicationErrorLog", connection) { CommandType = System.Data.CommandType.StoredProcedure };
                    cmd.Parameters.AddWithValue("@Type", e.GetType().Name);
                    cmd.Parameters.AddWithValue("@InnerException", innerExcpetionMessage);
                    cmd.Parameters.AddWithValue("@Message", e.Message);
                    cmd.Parameters.AddWithValue("@Source", e.Source);
                    cmd.Parameters.AddWithValue("@StackTrace", e.StackTrace);
                    cmd.Parameters.AddWithValue("@MethodName", e.TargetSite.Name);
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    connection.Close();
                }

            }
        }
    }

Now what if there is an error in updating this log, how will I log that?


texts: Function to find two prime numbers that sum up to a given even number

texts:
The Goldbach Conjecture asserts that any even number over 2 will be the sum of exactly two prime numbers.  I've created a function to find two prime numbers that add up to the number entered, but would like some help streamlining it and making it more robust.  One thing I would like to add is the ability to find all prime pairs that add up to the given number and append them as tuples to a list and return it.  I have a separate function that will find all prime numbers lower than or equal to a given number.

def goldbach_conj(number):
    x, y = 0, 0
    result = 0
    if not number % 2:
        prime_list = list_of_primes(number)
        while result != number:
            for i in range(len(prime_list)):
                x = prime_list[i]
                if result == number: break
                for j in range(len(prime_list)):
                    y = prime_list[j]
                    result = x + y
                    print("Adding {} and {}.".format(x, y))
                    print("Result is {}".format(result))
                    if result == number: break
    return x, y

def is_prime(number):
    if number % 2:
        # equivalent to if number % 2 != 0 because if number is
        # divisible by 2 it will return 0, evaluating as 'False'.
        for num in range(3, int(math.sqrt(number)) + 1, 2):
            if number % num == 0:
               return False
        return True
    else:
        return False

def list_of_primes(number):
    prime_list = []
    for x in range(2, number + 1):
            if is_prime(x):
                prime_list.append(x)
    return prime_list

def main():
    while True:
        usr_in = eval(input("Please enter a positive number"
                            " greater than 1: "))
        if usr_in > 1: break
        else:
            print("Number not valid.")

    prime_list = goldbach_conj(usr_in)
    print(prime_list)
    # prime_list = list_of_primes(usr_in)
    # for x in prime_list:
    #     print(x)

if __name__ == '__main__':
    main()


texts:
Inspired by a shell script I saw on Github, I put together a Python version to update the dynamic IP address for a subdomain I have that uses DigitalOcean's nameservers. I added a check to see if the IP address actually needs to be updated, and not do the update if the IP address hasn't changed. All of the user-configurable variables - API token, domain, subdomain - are stored in a separate .env file alongside the script.

#!/usr/bin/env python3

# Import required modules
import dotenv
import json
import os
import requests

# Load user-configured variables from .env file
dotenv.load_dotenv()
token = os.environ.get('DO_API_TOKEN')
domain = os.environ.get('DO_DOMAIN')
subdomain = os.environ.get('DO_SUBDOMAIN')

# Other variables
check_ip_url = 'https://api.ipify.org'
do_api_url = 'https://api.digitalocean.com/v2/domains/'

# Get the current external IP
def get_current_ip():
    curr_ip = requests.get(check_ip_url).text.rstrip()
    return curr_ip

# Get the current subdomain IP
def get_sub_info():
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token
    }
    response = requests.get(do_api_url + domain + '/records', headers=headers).text
    records = json.loads(response)
    for record in records['domain_records']:
        if record['name'] == subdomain:
            subdomain_info = {
                'ip': record['data'],
                'record_id': record['id']
            }
            return subdomain_info

# Update DNS records if required
def update_dns():
    current_ip_address = get_current_ip()
    subdomain_ip_address = get_sub_info()['ip']
    subdomain_record_id = get_sub_info()['record_id']
    if current_ip_address == subdomain_ip_address:
        print('Subdomain DNS record does not need updating.')
        return
    else:
        headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer $token',
        }
        data = '{"data":"' + current_ip_address + '"}'
        response = requests.put(do_api_url + domain + '/records/' + subdomain_record_id, headers=headers, data=data)
        if '200' in response:
            print('Subdomain IP address updated to ' + current_ip_address)
        else:
            print('IP address update failed with message: ' + response.text)
        return

if __name__ == '__main__':
    update_dns()

It works, but isn't the prettiest Python code out there, and I'm sure if could be made a bit more efficient. Are there any stylistic changes I should make to better adhere to best practices? I have run it through a PEP8 checker, and the only thing it brought up was about the line length in a few places, which I'm not super concerned about.


texts:
I want to load 2 files and show this data in 2 strings. I trying to do it like this and it works fine. But I think it is not the best solution... How to do more elegant solution?
var saleNumber1 = ""
var saleNumber2 = ""

override func viewDidLoad() {
        super.viewDidLoad()

        sale1()
        sale2()
}

func sale1() {

        let url = URL(string: "http://example.com/folder/sale1.txt")!

        let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
            guard let data = data else { return }
            saleNumber1 = "\(String(data: data, encoding: .utf8)!)"
        }

        task.resume()

    }

    func sale2() {

        let url = URL(string: "http://example.com/folder/sale2.txt")!

        let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
            guard let data = data else { return }
            saleNumber2 = "\(String(data: data, encoding: .utf8)!)"
        }

        task.resume()

    }


texts:
So, this is my first web scraper (or part of it at least) and looking for things that I may have done wrong, or things that could be improved so I can learn from my mistakes.

I made a few short function that can take a user and search tpb to get the maximum number of pages of content that that user has by requesting the url, parsing the html for the links div, and then crawling through them to see if each page is valid or not (since some linked pages actually have no content).

I know that I haven't covered all cases such as invalid urls, non-existent users etc, just trying to find what I've done wrong so far in actually parsing existent content before I go further.

There's two main things I'm concerned about:

First, I have filter/map/lambda combos all over the place here. These are generally slow from what I remember about their efficiency, and they seemed to be a fairly easy and concise way to get the filtering I needed (although not the prettiest). So, is this acceptable and/or is there a better way with bs4 or another alternative?

Second, since beautifulsoup is recursive in finding nested tags anyways does calling my get_max_pages function recursively really matter here?

import requests, urllib, re
from bs4 import BeautifulSoup

BASE_USER = "https://thepiratebay.se/user/"

def request_html(url):
    hdr = {'User-Agent': 'Mozilla/5.0'}
    request = urllib.request.Request(url, headers=hdr)
    html = urllib.request.urlopen(request)
    return html

def get_url(*args):
    for arg in args:
        url = BASE_USER + "%s/" % arg
    return url

def check_page_content(url):
    bs = BeautifulSoup(request_html(url), "html.parser")
    rows = bs.findAll("tr")
    rows = list(filter(None, map(lambda row: row if row.findChildren('div', {'class': 'detName'}) else None, rows)))
    return True if rows else False

def get_max_pages(user, url=None, placeholder=0, links = list(), valid=True):
    if url is None:
        url = get_url(user, str(placeholder), 3)
    if valid:
        td = BeautifulSoup(request_html(url), "html.parser").find('td', {'colspan': 9, 'style':'text-align:center;'})
        pg_nums = td.findAll('a', {'href': re.compile("/user/%s/\d{,3}/\d{,2}" % user)})
        pages = list(filter(None, map(lambda a: int(a.text) - 1 if a.text else None, pg_nums)))
        if links: #Unnecessary to filter this at start as placeholder = 0
            pages = list(filter(None, map(lambda x: x if int(x) > placeholder else None, pages)))
        if pages:
            i = 0
            while valid and i < len(pages):
                element = pages[i]
                valid_page = check_page_content(get_url(user, element, 3))
                if valid_page:
                    links.append(element)
                else:
                    valid = False
                i += 1
            return get_max_pages(user, get_url(user, len(links), 3), len(links), links, valid)
        else:
            return links
    else:
        return links

Also, if anyone wouldn't mind would it be viable to split this into threads for say the content validation? I don't see how that would really improve anything all that much with python's GIL, but curious nonetheless.

To find ~35 pages of valid content it takes about 1.5 mins.


texts: Matlab Code for Convolutional Neural Networks

texts:
I am using Matlab to train a convolutional neural network to do a two class image classification problem. I have an imbalanced data set (~1800 images minority class, ~5000 images majority class). As I understand it, the splitEachLabel function will split the data into a train set and a test set. In my case, it will put 1024 images (selected from both classes) into the train set and the rest into the test set.

I am using new random seeds on each iteration to select different sets of images, just to check if the neural network architecture works well when given different subsets of the same data. Is that a good way to do it?

My only issue is my training accuracy is about 20% Lower than my testing accuracy, which I find extremely perplexing.

% delete accuracy between runs if changing number of iterations
for i = 1:6
    rng(i); % for reproducible results
    datastore = imageDatastore(fullfile('.'), 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
    [trainingSet, testingSet] = splitEachLabel(datastore, 1024, 'randomize');
    % convolutional output area = 1+(inputWidth - filterSize + 2*Padding) /
    % Stride
    layers = [imageInputLayer([128 128 3]);
          convolution2dLayer(7,48,'Stride',1,'Padding',3);
          reluLayer();
          maxPooling2dLayer(2,'Stride',2);
          convolution2dLayer(5,128,'Stride',1,'Padding',2);
          reluLayer();
          maxPooling2dLayer(2,'Stride',2);
          convolution2dLayer(3,192,'Stride',1,'Padding',1);
          reluLayer();
          convolution2dLayer(3,192,'Stride',1,'Padding',1);
          reluLayer();
          convolution2dLayer(3,128,'Stride',1,'Padding',1);
          reluLayer();
          maxPooling2dLayer(2,'Stride',2);
          fullyConnectedLayer(256);
          fullyConnectedLayer(2);
          softmaxLayer();
          classificationLayer()];
    options = trainingOptions('sgdm','MaxEpochs',30,'InitialLearnRate',0.0001,'MiniBatchSize',128, ...
    'LearnRateDropPeriod', 6, 'LearnRateDropFactor', 0.2);
    convnet = trainNetwork(trainingSet, layers, options);
    trainPredictions = classify(convnet, trainingSet);
    trainLabels = trainingSet.Labels;
    train_accuracy{i} = sum(trainPredictions == trainLabels)/numel(trainLabels);

    testPredictions = classify(convnet, testingSet);
    testLabels = testingSet.Labels;
    test_accuracy{i} = sum(testPredictions == testLabels)/numel(testLabels);

    disp(train_accuracy{i})
    disp(test_accuracy{i})
end
disp("Mean:")
disp(mean(cell2mat(train_accuracy)))
disp(mean(cell2mat(test_accuracy)))

Edit:
I discovered I needed to add a parameter to the training options in order for the learn rate to drop every so many epochs. Specifically, I needed to add 'LearnRateSchedule' with the option 'piecewise'.

Also, increasing the number of training images a little bit and removing one of the 192 filter convolutional layers seems to have made the training and testing accuracy more closely align with each other. So I believe that issue was just a fluke regarding my architecture and limited number of training images versus the size of the testing set.


texts:
My code consists of Split, Mid, Left, Right and Replace functions which are used to trim my data into a more neat manner. However, it's too long and inefficient, it takes too much time to go through all this because I have up to thousands of files that will go through this.

My main code goes through a folder and goes through all the files inside the folder to find and search for strings and then copy it out into a new worksheet.

'Test RowA(1)
wks.Cells(BlankRow, 1).Replace What:="testtest         : ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

'Start RowB(2)
wks.Cells(BlankRow, 2).Replace What:="  Started at: ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

'Temp RowC(3)
Dim strSearchC As String
Dim cCell As Range
Dim s3 As String
strSearchC = "testflyy"

Set cCell = wks.Cells(BlankRow, 3).Find(What:=strSearchC, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not cCell Is Nothing Then
s3 = Split(cCell.Value, ":")(1)
s3 = Mid(s3, 1, 3)
cCell.Value = s3
End If

'Type RowD(4)
Dim strSearchD As String
Dim dCell As Range
Dim s4 As String
strSearchD = "testflyy"

Set dCell = wks.Cells(BlankRow, 4).Find(What:=strSearchD, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not dCell Is Nothing Then
s4 = Split(dCell.Value, "_", 3)(2)
s4 = Mid(s4, 1, 3)
dCell.Value = s4
End If

'No RowF(6)
Dim strSearchF As String
Dim fCell As Range
Dim s6 As String
strSearchF = "homebeestrash_archivetestts"

Set fCell = wks.Cells(BlankRow, 6).Find(What:=strSearchF, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not fCell Is Nothing Then
s6 = Split(fCell.Value, "testts")(1)
s6 = Mid(s6, 1, 2)
fCell.Value = s6
End If

'End RowG(7)
Dim strSearchG As String
Dim gCell As Range
Dim s7 As String
strSearchG = "homebeestrash_archivetestts"

Set gCell = wks.Cells(BlankRow, 7).Find(What:=strSearchG, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not gCell Is Nothing Then
s7 = Split(gCell.Value, "reports")(1)
s7 = Split(s7, "Report")(0)
s7 = Left(s7, 8) & " " & Right(s7, 6)

'Month RowH(8)
wks.Cells(BlankRow, 8).Value = WorksheetFunction.Transpose(Left(s7, 4) & "-" & Mid(s7, 5, 2) & "-" & Mid(s7, 7, 2)) '<~~ get 2019-03-02
gCell.Value = s7
End If

Basically these are to trim the already extracted data in each respective rows worksheet, Row A and B is just simply replacing the data with nothing, for Row C onwards to Row H,, i will search if the string exists on the particular row and then use Split or Right, Left, Mid functions.

Question: Is there any other way to write it more efficiently because there are too many codings with formats like this and I feel like its slowing my program down a lot.


texts:
I often have static classes which uses the DataContext somehow, and they all make a new DataContext, but often I already have one instantiated elsewhere, so why not use that?

public static bool SignIn(string email, string password, DataContext db = null)
{

    bool disposeDb = false;
    if (db == null)
    {
        disposeDb = true;
        db = new DataContext();
    }

    //Sign In stuff, or any other stuff...

    if (disposeDb) db.Dispose();
}

But is there a cool trick to do this, or am I just coding it wrong?  Or is it ok?

Update

The DataContext is not expensive to instantiate.  I guess I just leave it be.

But here is another example:

public static User GetUser(DataContext db = null)
{
    if (HttpContext.Current.Request.Cookies["Id"] == null)
    {
        return null;
    }
    else
    {
        bool disposeDb = false;
        if (db == null)
        {
            disposeDb = true;
            db = new DataContext();
        }

        int id = Convert.ToInt32(HttpContext.Current.Request.Cookies["Id"].Value);
        string cookieKey = HttpContext.Current.Request.Cookies["CookieKey"].Value;

        User user = db.Users.FirstOrDefault(x => x.Id == id && x.CookieKey == cookieKey);

        if (disposeDb) db.Dispose();
        return user;
    }
}

This is again nice because if I already have a context, I can provide it, and then I won't have to attach the entity (User) to another context if I want to modify it:

User user = MyClass.GetUser(db);
user.Email = "asdsasd";
db.SaveChanges();

How can I improve it?  Or is it okay?

Update 2

The DataContext should only be instantiated once per page request. And here is a neat way to do it, that actually allows me to access it even from static methods.


texts: Defining a new Delphi Class with built-in Connection

texts:
Using Delphi XE2, I'm wondering if it would be usable to define a base object something like below.
Class is prone to be used in multiple projects.

I've chosen TSQLConnection as my starting point - since it's using the dbxpress framework and should be able to connect to a wide range of databases and other. But if a better solution exists - please feel free to comment on this.

TMyObject = class
private
  fConnection : TSQLConnection;
public
  procedure SetupConnection(aConn : TSQLConnection);
  property Connection : TSQLConnection read fConnection write fConnection;
  ... // a lot of other properties and methods ...
end;

procedure TMyObject.SetupConnection(aConn : TSQLConnection);
begin
  fConnection := aConn; // Should we clone here? ... or is a reuse ok?
end;

This way it would be possible to use a connection from any of my inherited classes.

Questions:

Is this a good design? (from a general point of view)
Is the use of TSQLConnection OK, if we have like 200 inherited instances?
Making the assignment of the Connection - is it ok to re-use from the datamodule?
Perhaps the SQLConnection was better located in an Interface?

Intentions

My intention is to create a "base class structure" for all my classes (in all my projects), and make it easier for co-workers to develop new applications, based on this hierarchy.


texts:
I am trying to implement a algorithm for the GCD of two numbers in a functional approach. Can this be improved further, also with regards to performance?

def gcd(a, b)
   return b if a ==0
   return a if b == 0
   return b if (a%b == 0)
   return a if (b%a == 0)
  (a > b)? gcd(a, a%b) : gcd(b%a, b)
end


texts: Reading in different datatypes from a file

texts:
I'm making a database program that reads from a file info on tennis players. Everything works, but is there a simpler way of doing this?  Is there anything I can improve here?

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

const int MAX_PLAYERS = 5;

struct player {
    char *fullname[2];
    int age;
    double rating;
    char *racketBrand;
}; typedef struct player tPlayer;

void readData(tPlayer *player){
    FILE *file = fopen("database", "r");
    if(!file){
        perror("File not found");
        return;
    }

    int idx = 0;
    char buffer[50];
    while(fgets(buffer, sizeof(buffer), file)){
        player[idx].fullname[0] = malloc(20);
        player[idx].fullname[1] = malloc(20);
        player[idx].racketBrand = malloc(20);
        sscanf(buffer, "%s %s %d %lf %s", player[idx].fullname[0], player[idx].fullname[1],
            &player[idx].age, &player[idx].rating, player[idx].racketBrand);
        idx++;
    }
    fclose(file);
}

void printInfo(tPlayer *player){
    int idx;
    for(idx = 0; idx < MAX_PLAYERS; idx++){
        printf("***Name: %s %s ***Age: %d ***Rating: %.1lf ***Racket Brand: %s\n", player[idx].fullname[0],
            player[idx].fullname[1], player[idx].age, player[idx].rating, player[idx].racketBrand);
    }
}

int main(){
    tPlayer player[MAX_PLAYERS];
    readData(player);
    printInfo(player);
}


texts: Generate random 10% of file to be used in testing with Ruby

texts:
I'm new to Ruby, but not to programming.  I just need a simple script that, given a text file, will pull out around 10% of the lines at random.  Below is what I came up with based upon a Python script I wrote.  What do I need to do differently to make it more Ruby-like?

prng = Random.new

File.open('english-words-partial.txt', 'w') {  |f|
  File.readlines('english-words-full.txt').each do |line|
    if prng.rand >= 0.9
      f.write(line)
    end
  end
}