texts: More efficient way to make a Twitter status in a string of just words

texts:
I am making an application where I will be fetching tweets and storing them in a database. I will have a column for the complete text of the tweet and another where only the words of the tweet will remain (I need the words to calculate which words were most used later).

How I currently do it is by using 6 different .replaceAll() functions which some of them might be triggered twice. For example I will have a for loop to remove every "hashtag" using replaceAll().

The problem is that I will be editing as many as thousands of tweets that I fetch every few minutes and I think that the way I am doing it will not be too efficient.

What my requirements are in this order (also written in comments down below):

Delete all usernames mentioned
Delete all RT (retweets flags)
Delete all hashtags mentioned
Replace all break lines with spaces
Replace all double spaces with single spaces
Delete all special characters except spaces

Here is a short and compilable example:

public class StringTest {

    public static void main(String args[]) {

        String text = "RT @AshStewart09: Vote for Lady Gaga for \"Best Fans\""
                + " at iHeart Awards\n"
                + "\n"
                + "RT!!\n"
                + "\n"
                + "My vote for #FanArmy goes to #LittleMonsters #iHeartAwards"
                + " htt…";

        String[] hashtags = {"#FanArmy", "#LittleMonsters", "#iHeartAwards"};
        System.out.println("Before: " + text + "\n");

        // Delete all usernames mentioned (may run multiple times)
        text = text.replaceAll("@AshStewart09", "");
        System.out.println("First Phase: " + text + "\n");

        // Delete all RT (retweets flags)
        text = text.replaceAll("RT", "");
        System.out.println("Second Phase: " + text + "\n");

        // Delete all hashtags mentioned
        for (String hashtag : hashtags) {
            text = text.replaceAll(hashtag, "");
        }
        System.out.println("Third Phase: " + text + "\n");

        // Replace all break lines with spaces
        text = text.replaceAll("\n", " ");
        System.out.println("Fourth Phase: " + text + "\n");

        // Replace all double spaces with single spaces
        text = text.replaceAll(" +", " ");
        System.out.println("Fifth Phase: " + text + "\n");

        // Delete all special characters except spaces
        text = text.replaceAll("[^a-zA-Z0-9 ]+", "").trim();
        System.out.println("Finaly: " + text);
    }
}


texts: Hacking a SecureString based on std::basic_string for C++

texts:
Inspired by reading "How-to write a password-safe class?", I tried some clever (or dumb-fool) hack to create a widely-useable secure string using the std::basic_string-template, which does not need to be explicitly securely erased itself.
At least gcc and clang seem not to choke on it (coliru):
#include <string>
namespace my_secure {
void SecureZeroMemory(void* p, std::size_t n) {
    for(volatile char* x = static_cast<char*>(p); n; --n)
        *x++ = 0;
}

// Minimal allocator zeroing on deallocation
template <typename T> struct secure_allocator {
    using value_type = T;

    secure_allocator() = default;
    template <class U> secure_allocator(const secure_allocator<U>&) {}

    T* allocate(std::size_t n) { return new T[n]; }
    void deallocate(T* p, std::size_t n) {
        SecureZeroMemory(p, n * sizeof *p);
        delete [] p;
    }
};

template <typename T, typename U>
inline bool operator== (const secure_allocator<T>&, const secure_allocator<U>&) {
    return true;
}
template <typename T, typename U>
inline bool operator!= (const secure_allocator<T>&, const secure_allocator<U>&) {
    return false;
}

using secure_string = std::basic_string<char, std::char_traits<char>,
    secure_allocator<char>>;
}

namespace std {
// Zero the strings own memory on destruction
template<> my_secure::secure_string::~basic_string() {
    using X =std::basic_string<char, std::char_traits<char>,
        my_secure::secure_allocator<float>>;
    ((X*)this)->~X();
    my_secure::SecureZeroMemory(this, sizeof *this);
}
}

And a short program using it to do nothing much:
//#include "my_secure.h"
using my_secure::secure_string;
#include <iostream>

int main() {
    secure_string s = "Hello World!";
    std::cout << s << '\n';
}

Some specific concerns:

How badly did I break the standard?

Does the fact that one of the template-arguments is my own type heal the fact that I added my own explicit specialization to ::std?
Are the two types actually guaranteed to be similar enough that my bait-and-switch in the destructor is ok?

Is there any actual implementation where the liberties I took with the standard will come back to haunt me?

Did I miss any place where I should zero memory after use? Or is there any chance that anything will slip by?


texts:
Here is the code I have written to create an inverted index dictionary for a set of documents:

inv_indx = {i:[] for i in corpus_dict}
for word in corpus_dict:
    for i in range(len(docs)):
        if word in docs[i]:
            inv_indx[word].append(i)

docs is a list of sets of the words in various documents:

[{'once','upon','a','time',...},{'lorum','ipsum','time'...},...]

corpus_dict is a set of all the words that appear in any of the documents:

{'once','upon','a','time','lorum','ipsum',...}

inv_index becomes a dictionary with each word in the corpus_dict as a key for a list of the document ids that contain that word:

{'once':[0],'time':[0,1],...}

The problem is this becomes very slow if the number of documents gets too big. How can I make this code more efficient?


texts: Enhancing search using Fuzzy Logic in NodeJS

texts:
Brief Description:

In my NodeJS Application, I have a model called User.

User.js

module.exports = {

  attributes: {
    name: {
      type: 'string'
    }
  }
};

My application consists users searching functionality using name attribute. For that I used find users using exact name as below(I am using MongoDB as my Database). My User Controller as below.

UserController.js

 var name = req.param('name');

 User.find({ name: { 'contains': name } }).exec(function (err, results) {
     if (err) {
         return res.serverError(err);
     }
     return res.send(results);
 });

Now I need to enhance my searching functionality.

As an example if my DB contains record as Michael and if I searched as micel, Michael should be given as a search result.

For that I used Fuzzy Logic Algorithm and I used fuzzy npm package. Now my implementation is as below.

UserController.js

var fuzzy = require('fuzzy');

module.exports = {

    search: function (req, res) {

        var term = req.param('name');
        var thisCtrl = this;

        User.find().exec(function (err, users) {

            if (err) {
                return res.serverError(err);
            }
            thisCtrl.createSearchQuery(term, users, function (orQuery) {
                User.find({or: orQuery}).exec( function (err, finalResulst) {
                    if (err) {
                        return res.serverError(err);
                    }
                    return res.send(finalResulst)
                })
            })
        });
    },

    createSearchQuery: function (term, users, cb) {

        var userNamesArr = [];
        var or = [];

        for (var i = 0; i < users.length; i++) {
            userNamesArr.push(users[i].name)
        }

        var results = fuzzy.filter(term, userNamesArr);
        var matches = results.map(function(el) { return el.string; });

        for (var i = 0; i < matches.length; i++) {

            var tempTerm = { name: { 'contains' : matches[i] }};
            or.push(tempTerm)
        }
        cb(or);
    }
};

This is working as I needed.

But My Question is,

I am getting all the users in my DB and create search term using Fuzzy Logic and finding again in User collection. Now this is ok if I have only about 100 user records. But the time when User collection contains Millions of user records , Will this causes to performance issue to my application or is there a Better Approach than this I should follow.

Sorry for my bad English and Suggestions are highly appreciated.


texts: Getting the top 10 stores within a category

texts:
I have the following query. I know there's an easier way to do this without the sub queries, or at least optimizing how they're used. I've read about cross joins and using HAVING but cannot figure out how to make this better as I know it's pretty bad.

Basically what I'm trying to do is get the top 10 stores within a category. For this category it is category with an id of 9 as you can see in the where clause. I am categorizing each individual user based on the most occurring category of the products they have. And then reordering the results by most orders and getting the first 10 results.

EDIT:

c.id = 9, which should be c.id = '$categoryId' (edited) is representative of where a numeric value would be passed into the query to get the top stores specific to a category id.

SELECT u.id,
       u.avatar,
       u.storeName,
       (SELECT COUNT(o.id) FROM orders o WHERE o.storeId = u.id) AS orders
FROM users u
WHERE (SELECT c.id
       FROM categories c, products p
       WHERE c.id = p.categoryId
       AND p.userId = u.id
       GROUP BY p.categoryId
       ORDER BY COUNT(p.id) DESC
       LIMIT 1) = '$categoryId'
ORDER BY orders DESC
LIMIT 10


texts: Time limit exceeded on finding out the GCD and LCM of a Python list

texts:
I'm doing this HackerRank problem:

  Consider two sets of positive integers, \$A=\{a_0, a_1, \ldots, a_{n-1}\}\$ and \$B=\{b_0, b_1, \ldots, b_{m-1}\}\$. We say that a positive integer, \$x\$, is between sets \$A\$ and \$B\$ if the following conditions are satisfied:

  All elements in \$A\$ are factors of \$x\$.
  \$x\$ is a factor of all elements in \$B\$.

  Given \$A\$ and \$B\$, find and print the number of integers (i.e., possible \$x\$'s) that are between the two sets.

Assuming lists a and b as in the problem, the main idea is finding the LCM of list a and the GCD of list b, and then finding out how many multiples of the LCM divide the GCD perfectly without remainders. I ended up with this -

from fractions import gcd

def lcm(a, b):
    for i in xrange(max(a,b), a*b+1):
        if i%a==0 and i%b==0:
            l = i
            break
    return l

def count(l, g):
    count = 1
    if l==g:
        return count
    elif g%l !=0 and l%g != 0:
        return 0
    elif l<g:
        for i in xrange(l, g, l):
            if g%i==0:
                count += 1
        return count
    else:
        for i in xrange(g, l, g):
            if l%i==0:
                count +=1
        return count

if __name__ == '__main__':
    n,m = raw_input().strip().split(' ')
    n,m = [int(n),int(m)]
    a = map(int,raw_input().strip().split(' '))
    b = map(int,raw_input().strip().split(' '))
    l = reduce(lcm, a)
    g = reduce(gcd, b)
    print count(l, g)

But I pass only 7 of the 8 test cases, the last one getting terminated due to time out. I don't understand which part of my code would result in a long loop that might end up in a timeout.

P.S. I would also be very glad if any of you could point out any other inefficiencies or styling conventions in my code.


texts:
I use these in a windows form with fun buttons, I wanted to see what the world thinks of my coding.

This is something that I coded about 2-3 years ago, I did change some of the coding when I looked at it and thought "this could be better written like this"

Let me know what you think and what I could do differently.

Public class shiftCipher
{
    public string removeSpaces(string plainIn)
    {
        plainIn = plainIn.Replace(" ", string.Empty);
        return plainIn;
    }

    public string reverse(string plainIn)
    {
        char[] charArray = plainIn.ToCharArray();
        Array.Reverse(charArray);

        return new string(charArray);
    }

    public string toUCase(string strIn)
    {
        strIn = strIn.ToUpper();
        return strIn;
    }

    public string shift(string plainIn, int shift)
    {// 65=a 90=z in ASCII
        string readOut=string.Empty;
        char[] charArray;

        charArray = plainIn.ToCharArray();
        for (int i = 0; i < plainIn.Length; i++)
        {
            int Num = Convert.ToInt32(charArray[i]) + shift;
            readOut += Convert.ToChar(Num > 90 ? Num -= 26 : (Num < 65 ? Num += 26: Num));
        }
        return readOut;
    }

    //this method will take all of these methods and put them together to encrypt
    public string Encrypt(string ReadIn, int Shift)
    {
        ReadIn = removeSpaces(ReadIn);
        ReadIn = reverse(ReadIn);
        ReadIn = toUCase(ReadIn);
        ReadIn = shift(ReadIn, Shift);
        return ReadIn;
    }

    public string Decrypt(string ReadIn, int amountShift)
    {
        ReadIn = reverse(ReadIn);// undo the reverse by running the method again
        ReadIn = shift(ReadIn, -amountShift);

        return ReadIn;
    }
}

In the Decrypt I didn't think there was a good way to put the spaces back into the string, so I didn't try to take that on, and I also left the string in uppercase if it was there.


texts:
I have a little chain of JQuery promises.  My problem is at each stage I want to notify my progress to the user, keep them in the loop (Perhaps using .notify somehow?)

What I've eneded up with is this bizarre mess of callbacks and promises.  It works but I'm quite certain there is a neater, more logical way to do this.

both fetchSomeData() and Parser() return a promise

    var parseData = function(json)
    {
        showMessage("Updating your data...");
        return new Parser(json);
    };

    var onParseDone = function()
    {
        hideMessage();
        updateView();
    };

    showMessage("Fetching a bit of data...");
    fetchSomeData().pipe(parseData).done(onParseDone);


texts: Digital root computation with benchmarking

texts:
I've read this question about computing the digital root of some integer, and wondered how it would look like in Python.

As a reminder,

  "If you take the digits of any number and add them together, and then add the digits of the resulting number together, and continue doing that until you get a single digit, that single digit is the digital root of the original number."

This is not much about the algorithm performance itself, since it was shown in this answer that instead of summing the digits, you could do a simple division by 9 to get the same result. The question is more about the readability, elegance, and pythonicity (if this word exists) of my code. I checked its correctness with a tool from Thonky.

More, I'd be interested in knowing your opinion on the way I deal with sys.argv, the user interaction, and the benchmarking.

Finally, is it OK to use the int type for such long numbers? It seems to work fine even with 1000-digit numbers, but intuitively it shouldn't...

Any suggestion would be highly appreciated. Thanks.

digitalroot.py

# -*- encoding: utf-8 -*-

import time
import random
import sys

def request_user_number():
    while True:
        try:
            nb = int(raw_input('Enter a number\n'))
            return nb
        except (ValueError, NameError):
            print 'This is not a number. Try again.'

def request_user_again():
    while True:
        try:
            ans = str(raw_input('New computation? (y/n)\n')).lower()
            if ans[0] == 'n':
                again = False
                print 'Bye !'
                break
            elif ans[0] == 'y':
                again = True
                break
            else:
                print 'Did not understand. Try again.'
        except IndexError:
            print 'Say something.'
    return again

def digits_generator(nb):
    tmp = nb
    while tmp != 0:
        yield tmp % 10
        tmp /= 10

def digital_root(nb):
    if nb < 10:
        return nb
    else:
        s = 0
        for d in digits_generator(nb):
            s += d
        return digital_root(s)

def usage():
    print 'Wrong usage'
    print '\'python digitalroot.py ui\' for a user chosen number'
    print '\'python digitalroot.py benchmark nb_size tries\' for benchmarking'
    exit(1)

if __name__ == '__main__':
    if len(sys.argv) < 2 or sys.argv[1] not in ['ui', 'benchmark']:
        usage()
    if sys.argv[1] == 'ui':
        again = True
        while again:
            nb = request_user_number()
            print 'Computing digital root of', nb
            start_time = time.clock()
            dr = digital_root(nb)
            end_time = time.clock()
            print nb, '-->', dr, '(time :', end_time-start_time, 's)'
            again = request_user_again()
    elif sys.argv[1] == 'benchmark':
        try:
            nb_size = int(sys.argv[2])
            tries = int(sys.argv[3])
        except (ValueError, NameError, IndexError):
            usage()
        global_start = time.clock()
        s = 0
        for i in xrange(tries):
            nb = random.randint(10**(nb_size-1), 10**nb_size-1)
            start_time = time.clock()
            d = digital_root(nb)
            end_time = time.clock()
            s += end_time-start_time
            if random.randint(1, tries/5) == 1:
                print 'Random control : i =', i, ',', nb, '-->', d
        print nb_size, 'digits,', tries, 'tries'
        print s, 'seconds (average =', float(s)/tries, 's/try)'
        print 'Total benchmark time :', time.clock()-global_start


texts: PHP error handling: log everything, and redirect critical errors to an "oops" page

texts:
I'm VERY new to error/exception handling, previously just echoing everything to screen, and am trying to really clean up my code.  My goal was to log everything but only show the user a friendly "oops" page if something was seriously broken.  How does what I have below look?

Any best practice advice is appreciated.

<?php
error_reporting(~0);
ini_set('display_errors', 0);
ini_set('log_errors', 1);

/* Set the error handler. */
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    /* Ignore @-suppressed errors */
    if (!($errno & error_reporting())) return;

    $e = array('type'=>$errno,
               'message'=>$errstr,
               'file'=>$errfile,
               'line'=>$errline);
    redirect($e);
});

/* Set the exception handler. */
set_exception_handler(function ($e) {
    $e = array('type'=>$e->getCode(),
               'message'=>$e->getMessage(),
               'file'=>$e->getFile(),
               'line'=>$e->getLine());
    redirect($e);
});

/* Check if there were any errors on shutdown. */
register_shutdown_function(function () {
    if (!is_null($e = error_get_last())) {
        redirect($e);
    }
});

function redirect($e) {
    $now = date('d-M-Y H:i:s');
    $type = format_error_type($e['type']);
    $message = "[$now] $type: {$e['message']} in {$e['file']} on line {$e['line']}\n";
    $error_log_name = ini_get('error_log');
    error_log($message, 3, $error_log_name);

    switch ($e['type']) {
        /* We'll ignore these errors.  They're only here for reference. */
        case E_WARNING:
        case E_NOTICE:
        case E_CORE_WARNING:
        case E_COMPILE_WARNING:
        case E_USER_WARNING:
        case E_USER_NOTICE:
        case E_STRICT:
        case E_RECOVERABLE_ERROR:
        case E_DEPRECATED:
        case E_USER_DEPRECATED:
        case E_ALL:
            break;
        /* Redirect to "oops" page on the following errors. */
        case 0: /* Exceptions return zero for type */
        case E_ERROR:
        case E_PARSE:
        case E_CORE_ERROR:
        case E_COMPILE_ERROR:
        case E_USER_ERROR:
            return header('Location: /oops_page.htm');
    }
}

function format_error_type($type) {
    switch($type) {
        case 0:
            return 'Uncaught exception';
        case E_ERROR: /* 1 */
            return 'E_ERROR';
        case E_WARNING: /* 2 */
            return 'E_WARNING';
        case E_PARSE: /* 4 */
            return 'E_PARSE';
        case E_NOTICE: /* 8 */
            return 'E_NOTICE';
        case E_CORE_ERROR: /* 16 */
            return 'E_CORE_ERROR';
        case E_CORE_WARNING: /* 32 */
            return 'E_CORE_WARNING';
        case E_CORE_ERROR: /* 64 */
            return 'E_COMPILE_ERROR';
        case E_CORE_WARNING: /* 128 */
            return 'E_COMPILE_WARNING';
        case E_USER_ERROR: /* 256 */
            return 'E_USER_ERROR';
        case E_USER_WARNING: /* 512 */
            return 'E_USER_WARNING';
        case E_USER_NOTICE: /* 1024 */
            return 'E_USER_NOTICE';
        case E_STRICT: /* 2048 */
            return 'E_STRICT';
        case E_RECOVERABLE_ERROR: /* 4096 */
            return 'E_RECOVERABLE_ERROR';
        case E_DEPRECATED: /* 8192 */
            return 'E_DEPRECATED';
        case E_USER_DEPRECATED: /* 16384 */
            return 'E_USER_DEPRECATED';
    }
    return $type;
}
?>


texts:
I just finished a scraper in python using scrapy. The scraper logs in to a certain page and then scrapes a list of other pages using the authenticated session.
It retrieves the title of these pages and puts it in a list of items together with the page name.

Because there are little examples to find on continuing scraping after log-in I would like to receive some feedback on if this is the best way to continue scraping.

(Also I have a weird workaround to get the pagename into the itemlist, since I already have the pagename in the pagelist, but in the action method I retrieve it from the url again. Tips on doing this in an easier way are welcome too.)

p.s. The code has been slightly edited for sharing purposes.

class ShareSpider(scrapy.Spider):
    name = "sharespider"
    start_urls = ['http://www.example.com/public/login.aspx']

    def parse(self, response):
        yield scrapy.FormRequest.from_response(
            response,
            formxpath='//form[@id="login"]',
            formdata={
                'UserName': 'UserNameHere',
                'Password': 'PasswordHere',
                'Action':'1',
            },
            callback=self.after_login
        )

    def after_login(self, response):
        baseurl = 'http://www.example.com/public/'
        #Specify pages to crawl here:
        pagelist = ['page1.aspx', 'page2.aspx', 'page3.aspx', 'page4.aspx']
        for page in pagelist:
            yield Request(url= baseurl + page + "?id=1",
            callback=self.action)

    def action(self, response):
        pageurl = str(response.url)
        page = re.search('public/(.*)id=1', pageurl)
        if page:
            pagename = page.group(1)
        #Get page <title> element and strip whitespace
        title = str(response.selector.xpath('//title/text()').extract_first())
        res = title.strip()

        item = PageItem()
        item['pagename'] = pagename
        item['description'] = res
        yield item


texts: Retrieve high views per day questions with few answers

texts:
I've been messing around with the SE Data Explorer and I've come up with a query that basically returns information about questions that have a high number of views per day, that have few or no answers.

SELECT
      TOP 500
      Question.ViewCount,
      DATEDIFF(dd, CONVERT(date,Question.CreationDate), CONVERT(date, GETDATE()) ) AS Days ,
      Question.Id AS [Post Link],
      (Question.ViewCount / DATEDIFF(dd, CONVERT(date,Question.CreationDate), CONVERT(date, GETDATE()) )) AS ViewsPerDay,
      Question.AnswerCount,
      (SELECT MAX (Answers.Score )
       FROM POSTS "Answers"
       WHERE Answers.ParentId = Question.Id
       AND   Answers.PostTypeId = 2
      ) "Highest Answer Score",
      Question.Tags
 FROM
      POSTS "Question"
WHERE Question.PostTypeId = 1
AND   Question.acceptedanswerid is null
AND   DATEDIFF(dd, CONVERT(date,Question.CreationDate), CONVERT(date, GETDATE()) ) > 10
AND   (Question.ViewCount / DATEDIFF(dd, CONVERT(date,Question.CreationDate), CONVERT(date, GETDATE()) )) > 5
ORDER BY
    AnswerCount ASC,
    "Highest Answer Score" ASC,
    ViewsPerDay DESC

It's been a while since I used SQL directly in anger, so any feedback's welcome.

I particularly don't like the way DATEDIFF(dd, CONVERT(date,Question.CreationDate), CONVERT(date, GETDATE()) ) is repeated in both the SELECT and the WHERE clauses, so I'd like some way of aliasing the check if possible.


texts: Function to convert strings from hmmss, mmss, ss format to milliseconds

texts:
Here's the current logic to convert strings in the format hh:mm:ss, mm:ss or ss to milliseconds.

Any comments on how to improve this?

hhmmssToMillis(hhmmss) {
   let time= hhmmss.split(':').reverse();
   let millis = 0;
   switch(time.length) {
     case 1:
         millis = parseInt(time[0]) * 1000;
         break;
     case 2:
         millis = (parseInt(time[1]) * 60 + parseInt(time[0])) * 1000;
         break;
     case 3:
         millis = (parseInt(time[2]) * 60 * 60 + parseInt(time[1]) * 60 + parseInt(time[0])) * 1000;
         break;
   }
   return millis;
}


texts: Is it bad when my code is throwing exceptions, but doesn't crash during runtime?

texts:
Here's part of code in Java for battleships game I'm working on:

    lblGrid2 = new JLabel(new ImageIcon(myPicture));
    lblGrid2.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            for (int i = 0; i < shipList.size(); i++)
                if (shipList.get(i).isActive())
                    shipList.get(i).place();
        }
    });
    lblGrid2.addMouseMotionListener(new MouseMotionAdapter() {

        @Override
        public void mouseMoved(MouseEvent m) {

            for (int i = 0; i < shipList.size(); i++) {
                if (shipList.get(i).isActive()) {
                    shipList.get(i).moveShip(m.getX(), m.getY());
                }
            }
        }
    });
    lblGrid2.setText("");
    lblGrid2.setBounds(40, 40, 401, 401);
    panel.add(lblGrid2);

When placing ships on board. this listens for mouse movement and should move ship image accordingly, but that happens only after the client and server are connected and there is an active ship that can be placed. If that's not the case, for example I haven't connected to the server yet, every mouse move throws an exception.

Is this something I should be concerned about?


texts:
Is this a proper way to cache side effects (occurring on job.tryLoad)?

case class DFStorage(private var cache: Map[String, DataFrame] = Map()) {
  def tryLoad(job: Job): Kleisli[IO, MakeContext, \/[List[String], Unit]] = {
    if(!cache.contains(job.id)) {
      job.tryLoad.map(_.map(df => add(job, df)))
    } else {
      IO(().right[List[String]]).liftKleisli
    }
  }
  def add(job: Job, df: DataFrame): IO[Unit] = {
    IO(cache += (job.id -> df))
  }
  def toMap = IO(cache)
}


texts: Using QXmlStreamReader to read configuration file of key-value pairs

texts:
Background

The following XML snippet contains configuration parameters for our application in key-value pair manner. (Despite using INI files for such purpose may be better, I have to use XML files.)

<configuration>
    <parameter id="serial_port_name">ttyS0</parameter>
    <parameter id="serial_port_baud_rate">9600</parameter>
    <parameter id="check_period">1000</parameter>
    <parameter id="locale">hu_HU</parameter>
    <!-- ... -->
</configuration>

I would like to read the configuration parameters and store them inside a QHash<QString, QString> object with minimal overhead, therefore I've chosen the QXmlStreamReader for iterating through XML elements.

Current code

The toConfigurationMap() method of the following class iterates through the elements of XML file and when a <configuration> element is found, starts another iteration on the level of <parameter> elements to parse them.

class ConfigurationReader {
public:
    explicit ConfigurationReader(QFile& input) : reader(&input) {
    }

    QHash<QString, QString> toConfigurationMap() {
        QHash<QString, QString> output;

        for(auto token = reader.tokenType(); !reader.atEnd();
                token = reader.readNext()) {
            if(token != QXmlStreamReader::StartElement ||
                    reader.name() != "configuration")
                continue;

            while(reader.readNextStartElement()) {
                if(reader.name() == "parameter") {
                    QString id = reader.attributes().value("id").toString();
                    output.insert(id, reader.readElementText());
                }
                else {
                    reader.skipCurrentElement();
                }
            }
        }

        return output;
    }

private:
    QXmlStreamReader reader;
};

Goals

I would like to reduce the complexity of the code above, by somehow take apart the nested loops. In order to the toConfigurationMap() method be more cleaner, I would like to communicate to the reader that "it parses the parameters for each configuration elements" through the code.


texts:
I have written this function (and others similar to that one) But I am not sure I am using references on their full power.

My currently concerns is if I make a huge use of memory. The subroutine recieve a reference to two files, the subroutines, like these one return a hash (except &log_time, which returns a scalar). The subroutines expect a reference, thats why I use my %current_seq = ($id_name[0] => $seqs[$j]); my %freq_seq = &hexamer_freq(\%current_seq); on the subroutine, I don't think this is a good way to do it, but I can't imagine a better way to do it.

sub comparer{
    # Compare a sequence with the reference frequency of hexamers
    # First argument the file of to analyse, second argument the file of reference

    my %score;

    #Reading arguments
    my $seq = shift;
    my $ref_seq = shift;

    # Calculating the reference log2
    my %ref_seq = &read_fasta($$ref_seq);
    my %freq_ref = &hexamer_freq(\%ref_seq);

    # Counting hexamers and frequencies for each sequence
    my %seqs = &read_fasta($seq);
    while( my ($id,  $sequen) = each %seqs){

        my @id_seq = split(/\s+/, $id);
        my @id_name = split(/\./, $id_seq[0]);
        my $max = 0;
        my $min = 999;
        for (my $i = 0; $i < 3; $i++){
            last if length $sequen <= $i;
            my $sequ = substr($sequen, $i);
            next unless (defined $sequ);
            my $rev_sequ = reverse($sequ);
            my @seqs = ($sequ, $rev_sequ);
            for (my $j = 0; $j < scalar(@seqs); $j++){
                my %current_seq = ($id_name[0] => $seqs[$j]);
                my %freq_seq = &hexamer_freq(\%current_seq);

                # Handle the sequences that are too short to contain an hexamer
                if (scalar keys %freq_seq == 0){
                    print STDERR &log_time(), "Unable to calculate the Hexamer score of $id_name[0]\n";
                    next;
                };
                # Calculate the hexamer score
                my $score = 0;
                my $n_hexamers = scalar keys %freq_seq;
                foreach my $hex (keys %freq_seq){
                    if (defined $freq_ref{$hex}){
                        $score += log2($freq_seq{$hex}/$freq_ref{$hex});
                    }
                };
                # Store the two possible candidates of "best score"
                if ($score/$n_hexamers > $max){
                    $max = $score/$n_hexamers;
                };
                unless ($score/$n_hexamers > $min) {
                    $min = $score/$n_hexamers
                }
            # Store the data for each sequence
            my $key = $id_name[0] . " frame: $i";
            $key .= " FWD" if $j == 0; # The fwd + or - have the same hexamers
            $key .= " REV" if $j == 1;
            $score{$key} = [$max, $min];
            };
        };
    }
    return %score;
};

Besides, I would like to improve the way the $min is calculated. Now the 999 is an arbitrary number, I expect it won't be reached, because the $freq_ are numbers between 0 and 1, and it is unlikely to have so big numbers (but it may happen).


texts:
I made a simple file-encryption utility application for a little school project.
I'm really interested in cryptography, and tried my best while making it.
Can you see it, and give me feedback?

The input is always a FileInputStream (wrapped by a BufferedInputStream), and the output is a ByteBufferOutputStream, which will be written to the output file.

 public class Cryptography {

    private final Cipher c;
    private final MessageDigest md5;
    private final String AES_CBC_PKCS5 = "AES/CBC/PKCS5Padding";
    private final String MD5 = "MD5";
    private final int BLOCK_SIZE = 16;
    private final byte[] bytesIV = new byte[BLOCK_SIZE];

    public Cryptography() throws NoSuchAlgorithmException, NoSuchPaddingException {
        c = Cipher.getInstance(AES_CBC_PKCS5);
        md5 = MessageDigest.getInstance(MD5);
    }

    public void initIV() {
        new SecureRandom().nextBytes(bytesIV);
    }

    public void encryptStream(InputStream in, OutputStream out, byte[] key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException {
        out.write(bytesIV);
        out.flush();
        c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(bytesIV));
        out = new CipherOutputStream(out, c);
        byte[] buf = new byte[1024];
        int numRead = 0;
        while ((numRead = in.read(buf)) >= 0) {
            out.write(buf, 0, numRead);
        }
        out.close();
    }

    public void decryptStream(InputStream in, OutputStream out, byte[] key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException {
        in.read(bytesIV);
        c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(bytesIV));
        in = new CipherInputStream(in, c);
        byte[] buf = new byte[1024];
        int numRead = 0;
        while ((numRead = in.read(buf)) >= 0) {
            out.write(buf, 0, numRead);
        }
        out.close();

    }

    public byte[] hash(byte[] key) {
        return md5.digest(key);
    }

    public byte[] getIV() {
        byte[] temp = new byte[BLOCK_SIZE];
        System.arraycopy(bytesIV, 0, temp, 0, BLOCK_SIZE);
        return temp;
    }
}


texts:
After I wrote this question: Producer/Consumer programs, I realized that I had forgotten to write a version using mutexes.  I have now solved the Producer/Consumer problem using mutexes and implementing the tips I received from answers on that question.  It appears to be working correctly; however, unlike my other programs, which would run for a while or until the buffer was full, this version tends to interlock the producer/consumer threads, so each character is consumed immediately after it is produced.  Is this because the OS tends to preempt the thread when it releases the mutex if there is another waiting for it?

class Program
{
    const int BuffSize = 10;
    const int ValuesToProduce = 95;

    static void Main(string[] args)
    {
        var producerConsumer = new MutexProducerConsumer(BuffSize, ValuesToProduce);
        Thread p = new Thread(new ThreadStart(producerConsumer.Produce));
        Thread c = new Thread(new ThreadStart(producerConsumer.Consume));
        p.Start();
        c.Start();
    }
}

This is MutexProducerConsumer:

public class MutexProducerConsumer : ProducerConsumerBase
{
    public MutexProducerConsumer(int BuffSize, int ValuesToProduce)
        : base(BuffSize, ValuesToProduce) { }

    volatile int Available = 0;
    Mutex BufferLock = new Mutex(false);
    Mutex IsFull = new Mutex(true);
    Mutex IsEmpty = new Mutex(true);

    public override void Produce()
    {
        for (int i = 0; i < _TotalNumberOfValues; i++)
        {
            while (Available == _BuffSize)
            {
                Console.WriteLine("Wait Producer:");
                IsFull.WaitOne(1000);
            }

            BufferLock.WaitOne();
            _Buffer[i % _BuffSize] = (char)(32 + i % 95);
            Available++;
            Console.WriteLine("Produced: {0}", _Buffer[i % _BuffSize]);
            BufferLock.ReleaseMutex();

            try
            {
                IsEmpty.ReleaseMutex();
            }
            catch
            {

            }
        }
    }

    public override void Consume()
    {
        for (int i = 0; i < _TotalNumberOfValues; i++)
        {
            while (Available < 1)
            {
                Console.WriteLine("Wait Consumer:");
                IsEmpty.WaitOne(1000);
            }

            BufferLock.WaitOne();
            char c = _Buffer[i % _BuffSize];
            Available--;
            Console.WriteLine("Consumed: {0}", _Buffer[i % _BuffSize]);
            BufferLock.ReleaseMutex();

            try
            {
                IsFull.ReleaseMutex();
            }
            catch
            {

            }
        }
    }
}

And this is ProducerConsumerBase:

public abstract class ProducerConsumerBase
{
    protected char[] _Buffer;
    protected int _TotalNumberOfValues;
    protected int _BuffSize;

    protected ProducerConsumerBase(int buffSize, int totalNumberOfValues)
    {
        _BuffSize = buffSize;
        _Buffer = new char[buffSize];
        _TotalNumberOfValues = totalNumberOfValues;
    }

    public abstract void Produce();
    public abstract void Consume();
}


texts: How to Avoid Conditional Check Duplication

texts:
In the code, I am checking altPhoneExist and altEmailExist conditions multiple times. How can I avoid this and improve the performance and readability of the code?
Code Description:
A contact is created and added to a list if one of the conditions is true. And a different set operation executed for each one.
boolean altPhoneExist = contact.getAlternativePhoneNumber() != null;
boolean altEmailExist = contact.getAlternativeEmail() != null;

if (altPhoneExist || altEmailExist) {

    if (altPhoneExist) {
        contact.setPhoneNumber(contact.getAlternativePhoneNumber());
    }

    if (altEmailExist) {
        contact.setEmail(contact.getAlternativeEmail());
    }

    rqType.getContactInfoList().add(CraneFactory.createContactPerson(contact));
}


texts: bs4 HTML cleaner function with repetitive nested if clauses,

texts:
[I made a little web-scraper that downloads the source html files as well. Now for the sake of saving storage space i wrote a small function to delete quite a bit of stuff in the html file (specific id's, classes and meta information as well as  footers, headers, and links).
The function takes the html tree as input and outputs the shrunk html to be saved on the machine.
Code:
from bs4 import BeautifulSoup
from bs4 import Comment
tags = ['script', 'noscript', 'footer', 'header']

div_classes = ["head no-print", "col-md-6", "col-md-6 less-padding-left no-print",
               'col-md-4 col-md-pull-8 less-padding-left no-print',
               "col-sm-4 less-padding-left sidebar no-print hidden-xs",]
div_ids = ['navwrapper', 'citations']
links = ['stylesheet', 'text/css', 'canonical', 'alternate']
meta = ['metasummary', 'viewport', 'og_title', 'og_description']

def reduce_filesize(webcontent_string, output_file):
    soup = BeautifulSoup(webcontent_string, features='lxml')

    # remove full tags
    for i in tags:
        for element in soup.find_all(i):
            element.decompose()

    # remove div's based on id
    for i in div_ids:
        for element in soup.find_all('div', {'id': i}):
            element.decompose()

    # remove div's based on class
    for i in div_classes:
        for element in soup.find_all('div', {'class': i}):
            element.decompose()

    # remove stylesheets and css
    for i in links:
        for element in soup.find_all('link', {'rel': i}):
            element.decompose()

    # removing meta text
    for i in meta:
        for element in soup.find_all('meta', {'id': i}):
            element.decompose()

    # remove comments
    comments = soup.find_all(string=lambda text: isinstance(text, Comment))
    for c in comments:
        c.extract()

    with open(output_file, "w", encoding='utf-8') as file:
        file.write(str(soup))
        file.close

Now what's bugging me is the repetition of the if statements with only slight changes each time. I've tried handling this by creating a dict of all the list and use that but couldn't get it to work. To make it faster I tried using list comprehension for the inner loops instead which works just fine but I'm certain it can be improved further.
Thank you for all input and advice!
Here a link to a page to be shrunk


texts:
I wrote a basic command prompt Tic Tac Toe Game. I want to know what can be improved in terms of modeling and what mistakes I've made (if any).

void Main()
{
    TicTacToeBoard board = new TicTacToeBoard();

    while(!board.GameOver())
    {
        var i = int.Parse(Console.ReadLine());
        board.MakeMove(i);
        Console.WriteLine(board.DrawBoard());
    }
}

class TicTacToeBoard
{
    SquareType[] board = new SquareType[9];

    SquareType currentType = SquareType.X;

    public void MakeMove(int i)
    {
        if(board[i] != SquareType.Empty) return;
            board[i] = currentType;
            currentType = OppositeType(currentType);
    }

    SquareType OppositeType(SquareType squareType) =>
        squareType == SquareType.X ? SquareType.O : SquareType.X;

    public bool GameOver()
    {
        return WeHaveWinner() || WeHaveDraw();
    }

    bool WeHaveWinner()
    {
        var winningIndecies = new[]
        {
            new[]{1,2,3},
            new[]{4,5,6},
            new[]{7,8,9},
            new[]{1,5,9},
            new[]{3,5,7}
        };

        foreach(var indecies in winningIndecies)
        {
            var squares = indecies.Select(i => board[i - 1]);
            if(squares.Any(x => x == SquareType.Empty)) continue;
            else if(squares.Distinct().Count() == 1) return true;
        }
        return false;
    }

    bool WeHaveDraw() => !WeHaveWinner() && board.All(x => x != SquareType.Empty);

    public string DrawBoard()
        => $@"|{board[0]},{board[1]},{board[2]}|
|{board[3]},{board[4]},{board[5]}|
|{board[6]},{board[7]},{board[8]}|".Replace("Empty","  ");
}

enum SquareType { Empty, X, O }


texts: C# + Linq - grouping objects by frames of time found in the data

texts:
A bit of background. In the application I work on there are emails sent to notify users about articles they need to review. Each email+recipient pair is stored in database and may be assigned a different creation date ( called DateCreated ) due to emails being generated over some time.

I needed to calculate how many times the emails were sent and how many recipients were there on each turn. For this reason I grouped emails which come in quick succession into groups. I used a TimeSpan of 30 minutes after each "first" email in a group. In order to do it I needed to store the dates defining groups "somewhere". I used a separate method to wrap my grouping function. The TimeSpan I used is safe, mailing ever only takes up to 2 minutes. Also separate mailings usually happen on separate dates.

My questions here are - Is this easy enough to understand? Could I code it simpler? Any other advice is also welcome.

And yes - this should be fixed in the database, there should be a mailing table there but there isn't and I cannot achieve it easily at the moment.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UserCommunication
{
    public class ArticleEmailSummary
    {
        public int ArticleId { get; }
        public int EmailTemplateId { get; }
        public DateTime DateCreated { get; }
        public int RecipientCount { get; }

        private ArticleEmailSummary(IGrouping<DateTime, ArticleEmail> grouping)
        {
            ArticleId = grouping.First().ArticleId;
            EmailTemplateId = grouping.First().EmailTemplateId;
            DateCreated = grouping.Key;
            RecipientCount = grouping.Count();
        }

        public static List<ArticleEmailSummary> List(int ArticleId, int emailTemplateId)
        {
            // get email list from db
            List<ArticleEmail> emails = ArticleEmail.List(ArticleId, emailTemplateId);

            // order it ascending by creation date
            IOrderedEnumerable<ArticleEmail> emailsWithOrder = emails.OrderBy(e => e.DateCreated);

            // group into sublists, each group cachet up to X minutes of emails occuring after the first one
            IEnumerable<IGrouping<DateTime, ArticleEmail>> groups = emailsWithOrder.GroupBy(GetGroupingFunction());

            // convert groupings to summaries and return
            return groups.Select(grouping => new ArticleEmailSummary(grouping)).ToList();
        }

        private static Func<ArticleEmail, DateTime> GetGroupingFunction()
        {
            // I am curious of other dev's opinion on this method :)

            TimeSpan maxMailingTime = TimeSpan.FromMinutes(30);
            DateTime lastDate = DateTime.MinValue;

            return delegate (ArticleEmail email)
            {
                if (email.DateCreated - lastDate > maxMailingTime)
                {
                    // the mailing was not sent within maxMailingTime from the start of first email in series
                    // we will update the lastDate and this will group the emails into a separate group
                    lastDate = email.DateCreated;
                }

                // now we return the value which groups our emails
                return lastDate;
            };
        }
    }
}

Edits:

changed emailTemplateId to be of type int - it was just an enum ( with values such as "articleForApproval", "expiringArticle", etc. ), my bad, this must have been confusing indeed
ArticleEmail.List(...) simply returns List<ArticleEmail> that match the specified type of email and specific article
as requested here, is the ArticleEmail class, a bit simplified for the purpose of this question:

public class ArticleEmail {
    public int ArticleId { get; }
    public int EmailTemplateId { get; }
    public DateTime DateCreated { get; }
    public int RecipientId { get; }

    public static List<ArticleEmail> List( int articleId, int emailTemplateId ) {
        // return instances from db
    }
}


texts: esp8266 code for correctness of reset and overall performance

texts:
Please review my code for the performance and correctness, everything works and we get visible wifi access points  by opening http://esp8266_ip_addres/hotspots  except on refresh the values gets duplicated. and does not reset as I have done in the else block.

#include <ESP8266WebServer.h>

#define HTTP_REST_PORT 80
#define WIFI_RETRY_DELAY 500
#define MAX_WIFI_INIT_RETRY 50

const char* wifi_ssid = "router_ssid";
const char* wifi_passwd = "ssid_password";

int mn;

ESP8266WebServer http_rest_server(HTTP_REST_PORT);

const int numParams = 20;
String params [numParams];
String string = "['Please Select'";

String toString(String my_array[], int size)
{
  for (int i = 0; i < size; i++) {
    string = string + ", '" + my_array[i] + "'";
  }
  return string + "]";
}

int init_wifi() {
  int retries = 0;
  Serial.println("Connecting to WiFi AP..........");
  WiFi.mode(WIFI_STA);
  WiFi.begin(wifi_ssid, wifi_passwd);
  // check the status of WiFi connection to be WL_CONNECTED
  while ((WiFi.status() != WL_CONNECTED) && (retries < MAX_WIFI_INIT_RETRY)) {
    retries++;
    delay(WIFI_RETRY_DELAY);
    Serial.print("#");
  }
  return WiFi.status(); // return the WiFi connection status
}

void readParams(String (& params) [numParams])
{
  for (int i = 0; i < mn; i++)
  {
    params [i] = WiFi.SSID(i).c_str();
  }
}

int store_mn = 0;
String results;

void listHotspots() {
  if (mn != store_mn) {
    results = toString(params, mn);
    store_mn = mn;
  }
  else {
    mn = WiFi.scanNetworks(); // this is not resetting as expected
    params[0] = (String)0; // this is not resetting as expected
    readParams (params); // after reseting I expected to be new values of params
    results = toString(params, mn);
    store_mn = mn;
  }
  http_rest_server.send(200, "application/json", results);
}

void config_rest_server_routing() {
  http_rest_server.on("/", HTTP_GET, []() {
    http_rest_server.send(200, "text/html",
                          "Welcome to the ESP8266 REST Web Server");
  });
  http_rest_server.on("/hotspots", HTTP_GET, listHotspots);
}

void setup(void) {
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  mn = WiFi.scanNetworks(); // scan for hotspots for the first time.
  readParams (params);
  WiFi.scanDelete();
  if (init_wifi() == WL_CONNECTED) {
    Serial.print("Connetted to ");
    Serial.print(wifi_ssid);
    Serial.print("--- IP: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.print("Error connecting to: ");
    Serial.println(wifi_ssid);
  }
  config_rest_server_routing();

  http_rest_server.begin();
  Serial.println("HTTP REST Server Started");
}

void loop(void) {
  http_rest_server.handleClient();
}

Thank You.


texts:
This is exercise 3.1.31. from the book Computer Science An Interdisciplinary Approach by Sedgewick & Wayne:

Write a filter KamasutraCipher that takes two strings as command-line
argument (the key strings), then reads strings (separated by
whitespace) from standard input, substitutes for each letter as
specified by the key strings, and prints the result to standard
output. This operation is the basis for one of the earliest known
cryptographic systems. The condition on the key strings is that they
must be of equal length and that any letter in standard input must
appear in exactly one of them. For example, if the two keys are
THEQUICKBROWN and FXJMPSVLAZYDG, then we make the table
T H E Q U I C K B R O W N
F X J M P S V L A Z Y D G
which tells us that we should substitute F for T, T for F, H for X, X
for H, and so forth when filtering standard input to standard output.
The message is encoded by replacing each letter with its pair. For
example, the message MEET AT ELEVEN is encoded as QJJF BF JKJCJG. The
person receiving the message can use the same keys to get the message
back.

Here is my program:
public class KamasutraCipher
{
    public static void encrypt(String s, String t)
    {
        int m = s.length();
        int n = t.length();
        if (m != n)
        {
            throw new RuntimeException("The key lengths must be equal");
        }
        while (!StdIn.isEmpty())
        {
            String word = StdIn.readString();
            int wordLength = word.length();
            for (int i = 0; i < wordLength; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (String.valueOf(word.charAt(i)).equals(String.valueOf(s.charAt(j))))
                    {
                        String temp = word;
                        word = temp.substring(0,i) + String.valueOf(t.charAt(j)) + temp.substring(i+1);
                    }
                    else if (String.valueOf(word.charAt(i)).equals(String.valueOf(t.charAt(j))))
                    {
                        String temp = word;
                        word = temp.substring(0,i) + String.valueOf(s.charAt(j)) + temp.substring(i+1);
                    }
                }
            }
            System.out.print(word + " ");
        }
    }
    public static void main(String[] args)
    {
        encrypt(args[0], args[1]);
    }
}

StdIn is a simple API written by the authors of the book. I checked my program and it works.
Is there any way that I can improve my program?
Thanks for your attention.


texts:
This is a shopping list program made in Python 3 and it will send the list by email. I am not very experienced but I consider this program as a starting point to what I will do next.

import smtplib

from email.message import EmailMessage

from getpass import getpass

name = str(input("Name: "))
email = str(input("Email: "))
password = str(getpass("Password: "))
recipient = str(input("Recipient's email: "))

while True:
    try:
        productsnm = int(input(f"Hi, {name}!\nHow many products do you want to add to the shopping list? " ))
    except ValueError:
        print ("Please input a number.")
    else:
        break

products = []
quantities = []

for x in range(int(productsnm)):

    product = str(input("Input the product name: "))

    while True:
        try:
            quantity = int(input("Input the product quantity: "))
        except ValueError:
            print ("Please input a number.")
        else:
            break

    products.append(product)
    quantities.append(quantity)

print ("\nThese products have been added to the shopping list:")

for x, y in zip(products, quantities):
    print (f'{x} x {y}')

gmail_user = email
gmail_password = password

msg = EmailMessage()
msg['Subject'] = "Shopping List"
msg['From'] = gmail_user
msg['To'] = [recipient]
message = ""
for i in range(max(len(products), len(quantities))):
    message = message + str(products[i]) + " x " + str(quantities[i]) + "\n"
msg.set_content(message)

try:
    s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    s.ehlo()
    s.login(gmail_user, gmail_password)
    s.send_message(msg)
    s.quit()

    print ("\nThe email has been sent.")

except:
    print ("\nAn error occurred.")

print ("\nHave a nice day!")

exit()


texts:
What are your opinions on my JavaScript function? Can my code below be refactored?

var updatePrice = function () {
    var goPrice;
    var returnPrice;
    if (typeof scope.selectedGo !== 'undefined' && typeof scope.selectedReturn !== 'undefined') {
        for (var go in scope.gooffers) {
            if (scope.gooffers[go].tarifPremiere !== null && typeof scope.gooffers[go].tarifPremiere !== 'undefined' && scope.selectedGo === scope.gooffers[go].tarifPremiere.id) {
                goPrice = scope.gooffers[go].tarifPremiere.tarif;
            } else if (scope.gooffers[go].tarifSecondeFlex !== null && typeof scope.gooffers[go].tarifSecondeFlex !== 'undefined' && scope.selectedGo === scope.gooffers[go].tarifSecondeFlex.id) {
                goPrice = scope.gooffers[go].tarifSecondeFlex.tarif;
            } else if (scope.gooffers[go].tarifSecondeNoFlex !== null && typeof scope.gooffers[go].tarifSecondeNoFlex !== 'undefined' && scope.selectedGo === scope.gooffers[go].tarifSecondeNoFlex.id) {
                goPrice = scope.gooffers[go].tarifSecondeNoFlex.tarif;
            }
            if (scope.returnoffers[go].tarifPremiere !== null && typeof scope.returnoffers[go].tarifPremiere !== 'undefined' && scope.selectedReturn === scope.returnoffers[go].tarifPremiere.id) {
                returnPrice = scope.returnoffers[go].tarifPremiere.tarif;
            } else if (scope.returnoffers[go].tarifSecondeFlex !== null && typeof scope.returnoffers[go].tarifSecondeFlex !== 'undefined' && scope.selectedReturn === scope.returnoffers[go].tarifSecondeFlex.id) {
                returnPrice = scope.returnoffers[go].tarifSecondeFlex.tarif;
            } else if (scope.returnoffers[go].tarifSecondeNoFlex !== null && typeof scope.returnoffers[go].tarifSecondeNoFlex !== 'undefined' && scope.selectedReturn === scope.returnoffers[go].tarifSecondeNoFlex.id) {
                returnPrice = scope.returnoffers[go].tarifSecondeNoFlex.tarif;
            }
        }
       if (goPrice !== null && typeof goPrice !== 'undefined' && returnPrice !== null && typeof returnPrice !== 'undefined') {
                    scope.finalPrice = goPrice + returnPrice;
        }
    }
};


texts:
I want to build a function that, given an area size (target), returns a list of three lengths. The last item of the list (my_list[-1]) corresponds to the square plate.

The first two lengths in the list correspond to an orthogonal plate whose length is approximately 1.5 times larger than the one of the square plate and whose width is 1.5 times smaller. I want the lengths to be whole numbers.

The function I wrote for the task is given below, but I am sure there is a much better/faster solution. Keep in mind that when it comes to rounding floats, the bigger size for the length must be chosen since it is more conservative, but the main consideration is to not overshoot the target more than I have to.

import math

def get_plates(target):
    square = math.ceil(target ** 0.5)
    ortho1 = [math.ceil(target ** 0.5 * 1.5), math.floor(target ** 0.5 * 1.5)]
    ortho2 = [math.ceil(target ** 0.5 / 1.5), math.floor(target ** 0.5 / 1.5)]
    temp = list(zip([ortho1[0] for _ in ortho2], ortho2))
    temp.extend(list(zip([ortho1[1] for _ in ortho2], ortho2)))
    temp = [x for x in temp if x[0]*x[1] >= target]
    temp.sort(key=lambda x: abs(x[0] * x[1] - target))
    return [*temp[0], square]

For example, for a target = 700 the get_plates(700) correctly returns [39, 18, 27] and not [40, 18, 27] since 39x18 = 702 whereas 40x18 = 720 and 40x17 = 680

How can the above be made more efficient?


texts:
I wrote the code for an iterative pre-order traversal using stack for binary search tree. Below is the code for same. Please help me verify the correctness of the algorithm. Here t.t is the value of node t.

static void preorder(TreeNode t)
{
    MyConcurrentStack<TreeNode> s = new MyConcurrentStack<TreeNode>();
    while(s.peek()!=null || t!=null)
    {
        if(t!=null)
        {
            System.out.print(t.t+":");
            s.push(t);
            t = t.left;
        }
        else
        {
            t = s.pop();
            t = t.right;
        }
    }
}


texts: Find the smallest number of perfect squares that sum to a target

texts:
Given a positive integer n, find the smallest number of perfect squares (for example, 1, 4, 9, 16, ...) that sum to n.

sums = {0:0, 1:1}
def numSquares(n: int) -> int:
    if n in sums:
        return sums[n]
    else:
        s = 2**31
        squares = [i**2 for i in range(1, int(n**(1/2)) + 1)]
        while len(squares) and squares[-1] > n:
            squares.pop()
        for i in squares:
            s = min(s, numSquares(n - i) + 1)
        sums[n] = s
        return s

I'm also having a bit of trouble calculating the time complexity (using Big O notation) due to the recursion.

I understand that it's at least \$O(\sqrt n)\$ due to the loop but I'm unsure of how to factor in the recursive call.

Thank you very much!


texts: Using an AsyncTask to populate a ListView in a Fragment From a SQLite table

texts:
This is the first time that I have played around with AsyncTask in Android and I wanted to make sure I'm using it correctly. The idea is I'm grabbing all the rows from a table in the database using dao and putting them into an Arraylist based off my Item class. Once finished, I added it to a custom Adapter and add it to my ListView. I wasn't sure if the placement to the AsyncTask class inside the ListFragment is correct or if I used getActivity() correctly ( would it have been better to pass it as a variable?). Any other constructive criticism is welcome as well.

package com.domain.myapp;

import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.domain.myapp.adapters.ItemsListAdapter;
import com.domain.myapp.database.DAO;
import com.domain.myapp.models.Item;

public class ItemsListFragment extends ListFragment {

    private ItemsListAdapter falAdp;
    private OnFragmentInteractionListener mListener;

    public ItemsListFragment() {
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.item_list_view_menu, menu);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getItemLists gfl = new getItemLists();
        gfl.execute();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mListener.onFragmentInteraction(item.getItemId(), 0);
        return true;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        if (null != mListener) {
            mListener.onFragmentInteraction(R.id.ViewItemDetails, falAdp
                    .getItem(position).getId());
        }
    }

    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(int mnuId, int Id);
    }

    private class getItemLists extends
            AsyncTask<Void, String, ArrayList<Item>> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected ArrayList<Item> doInBackground(Void... params) {
            DAO dao = new DAO();
            ArrayList<Item> fal = dao.ItemsGetList(getActivity());
            return fal;
        }

        @Override
        protected void onPostExecute(ArrayList<Item> result) {
            super.onPostExecute(result);

            falAdp = new ItemsListAdapter(getActivity(),
                    R.layout.fragment_list_item_text_view, result);
            setListAdapter(falAdp);
        }
    }
}


texts: Updating my package version using only a Makefile

texts:
So I have written a Makefile that has a release function that can be run like: make release bump=patch and I want it to be fully executable in the Makefile. The only issue with my current implementation is that there is a lot of redundancy and long lines of commands, which I think can be simplified a lot, although I haven't found any better solutions yet (mainly because of the incrementation). It then tags and pushes.

The required functionality is being able to pass in patch,minor,major and have it overwrite the current file with the new version commit that and push the new tag.

It just has that 'gross' feeling, which I strive to avoid.

My current makefile looks like:

release:
    $(eval v := $(shell git describe --tags --abbrev=0 | sed -E -e 's/^v//' -e 's/(.*)-.*/\1/'))
ifeq ($(bump),patch)
    @echo "$v" | grep -Eo '[0-9]+$$' | tail -n 1 | awk '{print $$1 + 1}' | xargs -I '{}' sed -i '' -E 's/([0-9]+"$$)/{}"/' ._version_.py
else ifeq ($(bump),minor)
    @echo "$v" | grep -Eo '(\.[0-9]+\.)' | awk '{print $$1 + 1}' | xargs -I{} sed -i '' -E 's/(\.[0-9]+\.)/.{}./' ._version_.py
else ifeq ($(bump),major)
 @echo "$v" | grep -Eo '([0-9]+)' | head -n 1 | awk '{print $$1 + 1}' | xargs -I{} sed -i '' -E 's/([0-9]+)/{}/' ._version_.py
endif
    $(eval a := $(shell grep -Eo '([0-9]+\.[0-9]+\.[0-9]+)' ._version_.py))
    @git tag "$a"
    @git commit -am "Bumped to version $a"
    @git push --tags


texts: Using an Array of Objects to Store Quiz Questions/Answers

texts:
For a quiz web app, I decided the best way to store quiz questions, answers, and other user data related to the question was to use an array of objects for the quiz. I'm not sure if this is the best way to go about it since static information such as the questions and answers will be mixed with user data.

Each object in the array contains the quiz question, answer, if the user has enabled the question, and if the question has already been asked during the quiz session.

During a quiz session I wish to ask all questions that are enabled and haven't been asked yet. My script works but I am concerned it is inefficient (with a bank of 1000+ questions)? Is it alright to store user data with my quiz questions? Will this make adding additional questions down the road more difficult? Can I improve what I have or should I look for a different approach?

My script: JS Bin: http://jsbin.com/owehix/1/edit

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>


texts: Selecting from a database into a DataGridView

texts:
I have a piece of code which grabs data from an SQL database and displays it in a DataGridView. This code works perfectly fine for me.

It has been pointed out to me that the below code is bad programming:

Imports System.Data.SqlClient

Public Class Form1

Dim dbConnection As SqlConnection
Dim dbCommand As SqlCommand
Dim dbAdapter As SqlDataAdapter
Dim DataSet As New DataSet
Dim strSQL As String
Dim dbCount As String

Public Sub SQLConnect()
dbConnection = New SqlConnection("Data Source=connectionhere\sqlexpress;Initial Catalog=line_log;Integrated Security=True")
dbConnection.Open()
End Sub

Public Sub SQLCommand()
dbCommand = New SqlCommand(strSQL, dbConnection)
End Sub

Public Sub SQLAdapter()
dbAdapter = New SqlDataAdapter(strSQL, dbConnection)
End Sub

Public Sub SQLDisconnect()
dbConnection.Close()
End Sub

Public Sub DGVLoad()
Try
SQLConnect()
strSQL = "SELECT * FROM [Products]"
SQLAdapter()
DataSet.Clear()
dbAdapter.Fill(DataSet)
SQLDisconnect()
DataGridView1.DataSource = DataSet.Tables(0)

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DGVLoad()
End Sub

Could someone explain to me why what I am doing is bad practice?


texts: ElasticSearch, Tire - refactor autocomplete method for multiple resources

texts:
I am using ElasticSearch and Tire for search in my app across all models.

I would like to refactor autocomplete method because it looks to complex for me.
Am I using a good pattern for searching across model?

Model

class Link < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  tire.mapping do
    indexes :id, :type => 'string', :index => :not_analyzed
    indexes :title, analyzer: 'snowball', boost: 100
  end

  def to_indexed_json
    to_json(
      only: [:id, :title]
    )
  end
  ...
end

Controller

class SearchController < ApplicationController
  def new
    search = SearchesCatalog.new(params[:term])

    render json: search.autocomplete.to_json
  end
end

Lib

class SearchesCatalog
  attr_reader :options

  def initialize(options = {})
    @options = HashWithIndifferentAccess.new(options)
    @resources = [ :questions, :answers, :links, :events, past_events, :reviews ]
  end

  def results
    term = options[:term]

    Tire.search @resources do
      query { string "#{term}" }
    end.results
  end

  def autocomplete
    array = []
    results.each do |result|
      case result.type
    when "past_event"
      array << { title: result.content, label: "#{result.content} PastEvent", value: "/past_events/#{result.id}" }
    when "event"
      array << { title: result.title, label: "#{result.title} Event", value: "/events/#{result.id}" }
    when "topic"
      array << { title: result.name, label: "#{result.name} Topic", value: "/#{result.app_id}/t/#{result.id}" }
    when "link"
      array << { title: result.title, label: "#{result.title} Link", value: "/links/#{result.id}" }
    when "question"
      array << { title: result.content, label: "#{result.content[0..100]} Question" }
    when "answer"
    array << { title: result.content, label: "#{result.content[0..100]} Answer" }
    end
    array
  end
end

Javascript

// set up the search bar
$("#question_search").autocomplete({
  source:$('#question_search').data('source'),
  html: true,
  minlength: 1,
  appendto: "#search_results",
  select: function( event, ui ) {
    window.location=ui.item.value;
    return false;
  },
  focus: function( event, ui ) {
    $("#question_search").val(ui.item.title);
    return false;
  },
  open: function( event, ui ) {
    $('#search_results')
    .find('a:first').addclass('first').end()
    .find('a:last').addclass('last');
  }
});


texts: Implementation of Python's re.split in Clojure (with capturing parentheses)

texts:
If you use capturing parenthesis in the regular expression pattern in Python's re.split() function, it will include the matching groups in the result (Python's documentation).

I need this in my Clojure code and I didn't find an implementation of this, nor a Java method for achieving the same result.

(use '[clojure.string :as string :only [blank?]])

(defn re-tokenize [re text]
  (let [matcher (re-matcher re text)]
    (defn inner [last-index result]
      (if (.find matcher)
        (let [start-index (.start matcher)
              end-index (.end matcher)
              match (.group matcher)
              insert (subs text last-index start-index)]
          (if (string/blank? insert)
            (recur end-index (conj result match))
            (recur end-index (conj result insert match))))
        (conj result (subs text last-index))))
    (inner 0 [])))

Example:

(re-tokenize #"(\W+)" "...words, words...")
  => ["..." "words" ", " "words" "..." ""]

How could I make this simpler and / or more efficient (maybe also more Clojure-ish)?


texts: Using Task and actions for simple threading?

texts:
My question is regarding a rather inelegant solution to a problem that I came up with a while ago.

I was making a Winform Application to access active directory among other things and needed to thread my application to stop my UI from freezing.

Although I have little knowledge of actual threading (I kept hitting cross-thread access issues), I've built a little function that allowed me to pass an object, act on it in a separate thread and then once it returned to the UI CurrentContext I could act on it again to update the view.

TL;DR

The below function sends a Generic Item to a separate thread, then once acted upon, returns the item and acts on it again.

public static void doThreadedQuery<T>(Func<T> processQuery, Action<T> onComplete)
{
    TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext();
    Task<T> doQuery = new Task<T>(processQuery);
    doQuery.ContinueWith((results) => { if (onComplete != null) onComplete(results.Result); }, scheduler);
    doQuery.Start();
}

An example usage for something similar is pass an activeDirectory SearchResultCollection, parse through it and generate a TreeView/Grid. Then return the populated grid and UI updated without freezing.

Now I have done variants with a progress function/cancellation token etc, but I suppose my question is: on a base level, is there some horrible inefficiency or simple equivalent I am unaware of (aside from the Background Worker I am not overly fond of)?

I feel like I'm re-inventing the wheel here.


texts:
When writing Code Review answers, it becomes often necessary to measure how long the modified code takes vs how long the OP's code takes. I needed a nice way to visualize this as a function of the input, so I cobbled together the following script.

The plot_time function takes a single function (of a single variable atm) and an iterable of inputs, performs multiple timings per input and finally plots it as an errorbar plot using matplotlib (although usually the errorbars are too small to be seen).

The plot_times function loops over an iterable of functions and adds some nice labels and a legend (containing the __name__ of each function tested).

from functools import partial
import timeit
import numpy as np
from matplotlib import pyplot

def plot_time(func, inputs, repeats, n_tests):
    """
    Run timer and plot time complexity of `func` using the iterable `inputs`.

    Run the function `n_tests` times per `repeats`.
    """
    x, y, yerr = [], [], []
    for i in inputs:
        timer = timeit.Timer(partial(func, i))
        t = timer.repeat(repeat=repeats, number=n_tests)
        x.append(i)
        y.append(np.mean(t))
        yerr.append(np.std(t) / np.sqrt(len(t)))
    pyplot.errorbar(x, y, yerr=yerr, fmt='-o', label=func.__name__)

def plot_times(functions, inputs, repeats=3, n_tests=1, file_name=""):
    """
    Run timer and plot time complexity of all `functions`,
    using the iterable `inputs`.

    Run the functions `n_tests` times per `repeats`.

    Adds a legend containing the labels added by `plot_time`.
    """
    for func in functions:
        plot_time(func, inputs, repeats, n_tests)
    pyplot.legend()
    pyplot.xlabel("Input")
    pyplot.ylabel("Time [s]")
    if not file_name:
        pyplot.show()
    else:
        pyplot.savefig(file_name)

if __name__ == "__main__":
    import math
    import time

    scale = 100.

    def o_n(n):
        time.sleep(n / scale)

    def o_n2(n):
        time.sleep(n**2 / scale)

    def o_log(n):
        time.sleep(math.log(n + 1) / scale)

    def o_nlog(n):
        time.sleep(n * math.log(n + 1) / scale)

    def o_exp(n):
        time.sleep((math.exp(n) - 1) / scale)

    plot_times([o_n, o_n2, o_log, o_nlog, o_exp],
               np.linspace(0, 1.1, num=10), repeats=3)

Saving the figure can either be done manually, or by passing a file name.

The test code, that defines some functions for the most common complexity classes (\$\mathcal{O}(n), \mathcal{O}(n^2), \mathcal{O}(\log n), \mathcal{O}(n \log n), \mathcal{O}(\exp n)\$), produces this graphical output:

Any thoughts or recommendations are welcome. Especially I don't think there is a nice way around (implicitly) using global variables here (when generating a new graph matplotlib automatically adds it to the current figure).


texts: Condensing multiple conditionals into function

texts:
I feel like I'm repeating a ton of code here and there's got to be a better way to do this but am just completely spacing right now. I basically have an else if clause that, if their conditions are met, repeat the same code, but I'm also repeating the same code in the else statement along with a few other conditions.

What would be the best way to condense this? Could I create one master browser check function?

var searchEngine = document.referrer;

if ($('#userName').length){
      if (/Chrome/.test(searchEngine)){
         browser = "Chrome";
      } else if (/Firefox/.test(searchEngine)){
        browser = "FF";
      } else if (/Safari/.test(searchEngine)){
        browser = "Safari";
      }
} else if (/ip(hone|od|ad)/i.test(searchEngine)){
     if (/Chrome/.test(searchEngine)){
        browser = "Chrome";
     } else if (/Firefox/.test(searchEngine)){
        browser = "FF";
     } else if (/Safari/.test(searchEngine)){
        browser = "Safari";
     }
} else {
     if (/Chrome/.test(searchEngine)){
        browser = "Chrome";
     } else if (/Firefox/.test(searchEngine)){
        browser = "FF";
     } else if (/Safari/.test(searchEngine)){
        browser = "Safari";
     } else if (/ip(hone|od|ad)/i.test(searchEngine)){
        browser = "iOS";
     } else if (/(?:(compatible;.*)?Trident\/7.0)/ig.test(searchEngine)){
        version = 11;
        browser = "IE";
     } else if (/MSIE (\d+\.\d+);/.test(searchEngine)){
        browser = "IE";
     }
   }


texts: Security of API Keygen for a cryptocurrency trading platform

texts:
Here is an API key gen script for a cryptocurrency trading platform I am building.

First it checks to see if a key exists in the db for the user ID. If it does exist, it displays the key. If it doesn't, it creates one.

Next it checks to make sure the key is unique, by polling the db for identical keys. If one is found to be identical, the page is refreshed via meta refresh. If no key collision is found, it inserts the key in the database and refreshes the page via meta refresh. When the page is reloaded, there is now a key in the database for the user ID, and the key is displayed.

Any tips or feedback is greatly appreciated. I am a fairly inexperienced programmer and this is my first serious project.

require_once("models/config.php");
$id = $loggedInUser->user_id;
$api_select = mysql_query("SELECT * FROM userCake_Users WHERE `User_Id`='$id'");
while($row = mysql_fetch_assoc($api_select)) {
if($row["api_key"] !== null) {
    echo '
Your Api Key is:

';
    echo $row["api_key"];
}else{
    $user = $row["Username"];
    $pass = $row["Password"];
    $length = 128;
    $time = date("F j, Y, g:i a");
    $salt1 = $time . hash('sha512', (sha1 .$time));
    $salt2 = substr(md5(uniqid(rand(), true)), 0, 25);
    $salt3 = substr(md5(uniqid(rand(), true)), 0, 25);
    $salt4 = hash('sha256', (md5 .$time));
    $user_hash = hash('sha512', ($salt2 . $user . $salt1));
    $pass_hash = hash('sha512', ($salt1 . $pass . $salt2));
    $keyhash_a = hash('sha512', ($user_hash . $salt3));
    $keyhash_b = hash('sha512', ($pass_hash . $salt4));
    $hash_a = str_split($keyhash_a);
    $hash_b = str_split($keyhash_b);
    foreach($hash_a as $key => $value) {
        $hashed_a[] = $salt2 . hash('sha512', ($salt3 . $value)) . $salt1 . hash('sha256', ($salt4 . $key));
    }
    foreach($hash_a as $key => $value) {
        $hashed_b[] = $salt2 . hash('sha512', ($salt3 . $value)) . $salt1 . hash('sha256', ($salt4 . $key));
    }
    $hash_merge = array_merge($hashed_b, $hashed_a);
    $from_merge = implode($hash_merge);
    $exploded_2 = str_split($from_merge);
    $key_hash_last = implode($exploded_2);
    $key0 = str_shuffle($key_hash_last);
    $key1 = str_split($key0);
    $key2 = array_unique($key1);
    $key3 = implode($key2);
    $key4 = str_shuffle($key3);
    $key5 = str_shuffle($key4);
    $api_key0 = str_shuffle($key3.$key4.$key5.$key2);
    $api_key_prepped = mysql_real_escape_string($api_key0);
    $api_check_no_collision = mysql_query("SELECT * FROM userCake_Users WHERE `api_key` = '$api_key_prepped'"); //check to see if an identical key exists
    if(mysql_num_rows($api_check_no_collision) > 0) {
        echo '<meta http-equiv="refresh" content="0; URL=index.php?page=api">';//an identical key exists in the database, refresh the page and generate a new key.
    }else{
    $api_insert = mysql_query("UPDATE  `testing`.`userCake_Users` SET  `api_key` =  '$api_key_prepped' WHERE  `userCake_Users`.`User_ID` ='$id';"); //the key is unique, submit it to the database.
    echo '<meta http-equiv="refresh" content="0; URL=index.php?page=api">';
    }
}
}


texts: Create plain object from URLSearchParams

texts:
Is there a shorter / cleaner way of creating a plain object from URLSearchParams than this:

const uri = new URL('https://tempuri.org/?token=secret&test=true')
const result = {}
for (let p of uri.searchParams) {
  result[p[0]] = p[1]
}

Expected result:

{ token: 'secret', test: 'true' }


texts: Displaying elements of a list, in columns

texts:
I have a list of strings. I need to list them in rows and columns. Each row should not have more than "cols" number of values. Each of the values in a given row should be "step" away from the previous value. The values should appear only once in the output. Here is what I have. Any better way to write this code?

cols = 4
step = 10
vlist = ["Value" + str(i+1) for i in range(100)]

vlen = len(vlist)
start = 0
while start < vlen and start < step:
    num = 0
    for idx in range(start, vlen, step):
        if num < cols:
            print(vlist[idx], end=", ")
            num += 1
    print("\n")
    start += 1


texts:
Playing around in R with a Edx CS50 homework. The job is meant to be done in C, I am using (the more familiar) R just for warmup.

I mainly wonder whether I am breaking any good practice/style norms overall or in any tiny detail.

initials <- function(full.name) {
  # Returns initials of a full name
  # Input will contain only letters (uppercase and/or lowercase) plus
  # single spaces between words. Folks like Joseph Gordon-Levitt,
  # Conan O’Brien, and David J. Malan won’t be using your program. (If only!)
  if (length(full.name) == 0) {
    stop ("Valid name please")
  } else {
    isspace  <- integer(0)
    fn.split <- unlist(strsplit(full.name, fixed = TRUE, split = ""))
    isspace  <- which(fn.split == " ")
    init     <- toupper(fn.split[c(1, (isspace+1))])
  return(paste(init, collapse = ""))
  }
}

initials("Stack Overflow")
[1] "SO"


texts: Compare two columns and fill in a third column

texts:
I want to find out the records with same group from Column AS in Column AT and add missing invoices in Column AU considering all the values from the group in Column as below table:

Jan|Feb|Mar|ColumnAS|ColumnAT|ColumnAU

$30|blank|blank|AAA|AAA|Feb

blank|blank|blank|AAA|blank|blank

blank|blank|$35|AAA|blank|blank

blank|blank|blank|AAB|AAB|Jan,Mar

blank|$20|blank|AAB|blank|blank

blank|blank|blank|AAB|blank|blank

I have created 2 loops one (j) which runs through all values from Column AS and other (k) through AT. Temp2 is used to add unique values to column AT after comparing values from temp1

Code:

Sub MissingMonth()
Dim wSheet As Worksheet
Dim LastRow As Long
Dim temp1 As String
Dim temp2 As String
Dim j As Long, k As Long

Set wSheet = ThisWorkbook.Sheets("Source Data")
     wSheet.Select

        LastRow = ThisWorkbook.Sheets("Source Data").Cells(Rows.Count, "A").End(xlUp).Row

        temp2 = vbNullString
        For j = 2 To LastRow

            If Not (wSheet.Range("AS" & j) = temp2) Or temp2 = vbNullString Then
                temp1 = wSheet.Range("AS" & j)
                wSheet.Range("AU" & j) = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
                For k = 2 To LastRow
                  temp2 = temp1
                  wSheet.Range("AT" & j) = temp2

                    If wSheet.Range("AS" & k) = temp2 Then
                        Call CheckMissingMonth(k, temp2, "I", j, "Jan ")
                        Call CheckMissingMonth(k, temp2, "J", j, "Feb ")
                        Call CheckMissingMonth(k, temp2, "K", j, "Mar ")
                        Call CheckMissingMonth(k, temp2, "L", j, "Apr ")
                        Call CheckMissingMonth(k, temp2, "M", j, "May ")
                        Call CheckMissingMonth(k, temp2, "N", j, "Jun ")
                        Call CheckMissingMonth(k, temp2, "O", j, "Jul ")
                        Call CheckMissingMonth(k, temp2, "P", j, "Aug ")
                        Call CheckMissingMonth(k, temp2, "Q", j, "Sep ")
                        Call CheckMissingMonth(k, temp2, "R", j, "Oct ")
                        Call CheckMissingMonth(k, temp2, "S", j, "Nov ")
                        Call CheckMissingMonth(k, temp2, "T", j, "Dec")

                    End If
                 Next k
             End If
          Next j
 Set wSheet = Nothing
End Sub

  Sub CheckMissingMonth(k As Long, TempMonth As String, ColumnName As String, j As Long, Replacevalue As String)
     If Not IsEmpty(ThisWorkbook.Sheets("Source Data").Cells(k, ColumnName)) Then
        ThisWorkbook.Sheets("Source Data").Range("AU" & j).Replace What:=Replacevalue, Replacement:="", LookAt:=xlPart, MatchCase:=False
   End If
End Sub


texts: Basic implicit tree implementation in Java

texts:
I tried starting off with something like below. Note that traversal etc. is not included, this is the portion for storage and modifications. It feels like this could be much more elegant at places but not exactly sure how. I'm mainly trying to improve how the code almost repeats for getters and setters for left and right subtrees.

import org.apache.commons.lang3.ArrayUtils;

public class Tree {
  private int[] treeArray;
  private int depth;

  public Tree(int treeDepth) {
    this.depth = treeDepth;
    this.treeArray =  new int[(int) Math.pow(2, treeDepth)];
    treeArray[0] = Integer.MIN_VALUE;
  }

  public int returnRoot() {
    return treeArray[1];
  }

  public int[] getTreeArray() {
    return treeArray;
  }

  public int getDepth() {
    return depth;
  }

  public void setTreeArray(int[] treeArray) {
    this.treeArray = treeArray;
  }

  public Tree returnLeftSubTree() {
    if (depth == 0) return null;

    Tree ret = new Tree(depth - 1);
    for (int i  = 1; i < depth; i ++) {
      for (int j  = 0; j < depth - 1; j ++) {
        ret.getTreeArray()[(int) Math.pow(2, i - 1) + j]
            = treeArray[(int) Math.pow(2, i) + j];
      }
    }

    return ret;
  }

  public Tree returnRightSubTree() {
    if (depth == 0) return null;

    Tree ret = new Tree(depth - 1);
    for (int i  = 1; i < depth; i ++) {
      for (int j  = 0; j < depth - 1; j ++) {
        ret.getTreeArray()[(int) Math.pow(2, i - 1) + j]
            = treeArray[(int) (Math.pow(2, i) + Math.pow(2, i - 1)) + j];
      }
    }

    return ret;
  }

  public void setLeftSubTree(Tree leftSub) {
    if (leftSub.getDepth() >= depth) {
      this.treeArray = ArrayUtils.addAll(
          this.treeArray,
          new int[(int) (Math.pow(2, leftSub.getDepth() + 1) - Math.pow(2, depth))]);
      this.depth = leftSub.getDepth() + 1;
    }

    for (int i  = 1; i < depth; i ++) {
      for (int j  = 0; j < depth - 1; j ++) {
        treeArray[(int) Math.pow(2, i) + j] =
            leftSub.getTreeArray()[(int) Math.pow(2, i - 1) + j];
      }
    }
  }

  public void setRightSubTree(Tree rightSub) {
    if (rightSub.getDepth() >= depth) {
      this.treeArray = ArrayUtils.addAll(
          this.treeArray,
          new int[(int) (Math.pow(2, rightSub.getDepth() + 1) - Math.pow(2, depth))]);
      this.depth = rightSub.getDepth() + 1;
    }

    for (int i  = 1; i < depth; i ++) {
      for (int j  = 0; j < depth - 1; j ++) {
        treeArray[(int) (Math.pow(2, i) + Math.pow(2, i - 1)) + j]
            = rightSub.getTreeArray()[(int) Math.pow(2, i - 1) + j];
      }
    }
  }
}


texts: Function for root matching between two paragraphs

texts:
I created an algorithm that match roots of two texts, a question and a paragraph made of sentences. I aim at predicting in which sentence it exists the answer of a question. Yet It seems that I really complicated it. Do you know if I can improve it ? It is important as far as I apply it to a big file then.

import numpy as np, pandas as pd
# Break the paragraph/context into multiple sentences.
import spacy
en_nlp = spacy.load('en')

# synonyms
from itertools import chain
from nltk.corpus import wordnet

# stemmer/ root maker
from nltk.stem.lancaster import LancasterStemmer
st = LancasterStemmer()

# to evaluate literal expressions (sorry, I know it might not mean anything ...)
import ast

def match_roots(x):
    # we flatten the question
    question = x["question"].lower()

    # We get the sentences of the question in seperated words
    sentences = en_nlp(x["context"].lower()).sents

    # taking anything that look like a root in the question, not just the dominating one
    question_roots = [chunk.root.head.text.lower() for chunk in en_nlp(question).noun_chunks]
    # Attempt of using synonyms
    synonyms = []
    for word in set(question_roots):
        words = []
        words = wordnet.synsets(word)
        for synset in words:
            name = synset.lemmas()[0].name()
            synonyms.append(st.stem(name))
    question_roots.extend(synonym for synonym in set(synonyms))
    # end of attempt
    li = []
    # Here we prepare the ranking list
    ranking_list = []
    # for each sentence of a bunch of sentences
    for i,sent in enumerate(sentences):
        # we store the roots of a sentence
        roots = [st.stem(chunk.root.head.text.lower()) for chunk in sent.noun_chunks]
        common_roots = []
        if sum(1 for root in roots if root in question_roots)>0:
            # Here we score
            common_roots = [root for root in roots if root in question_roots]
            for k,item in enumerate(ast.literal_eval(x["sentences"])):
                if str(sent) in item.lower():
                    li.append(k)
        ranking_list.append((len(common_roots),i))
    return [max(ranking_list,key=lambda item:item[0])[1]]

The input can be :

>>>predicted["question"][0]
What role did Beyoncé have in Destiny's Child?
>>>predicted["context"][0]
'Beyoncé Giselle Knowles-Carter (/biːˈjɒnseɪ/ bee-YON-say) (born September 4, 1981) is an American singer, songwriter, record producer and actress. Born and raised in Houston, Texas, she performed in various singing and dancing competitions as a child, and rose to fame in the late 1990s as lead singer of R&B girl-group Destiny\'s Child. Managed by her father, Mathew Knowles, the group became one of the world\'s best-selling girl groups of all time. Their hiatus saw the release of Beyoncé\'s debut album, Dangerously in Love (2003), which established her as a solo artist worldwide, earned five Grammy Awards and featured the Billboard Hot 100 number-one singles "Crazy in Love" and "Baby Boy".'

The output would be 1. The sentence at rank 1 (the second sentence would be elected as the one containing the answer).


texts: Determine the index where two lists diverge

texts:
Background

A class provides an API to determine the index where two string lists diverge.

import java.util.LinkedList;

import static java.lang.System.out;

/**
 * Provides the ability to determine the index whereat two lists begin
 * to differ in content. Both this list and the list to comapre against
 * must not contain null strings.
 */
public class DivergentList extends LinkedList<String> {
  /**
   * Answers the index at which the strings within this list differ from
   * the strings in the given list.
   *
   * @param list The list to compare against.
   *
   * @return -1 if the lists have no common strings.
   */
  public int diverges( DivergentList list ) {
    int index = -1;

    if( valid( list ) && valid( this ) ) {
      while( equals( list, ++index ) );
    }

    return index;
  }

  /**
   * Answers whether the element at the given index is the same in both
   * lists. This is not null-safe.
   *
   * @param list The list to compare against this list.
   * @return true The lists have the same string at the given index.
   */
  private boolean equals( DivergentList list, int index ) {
    return (index < size()) && (index < list.size()) &&
      get( index ).equals( list.get( index ) );
  }

  /**
   * Answers whether the given element path contains at least one
   * string.
   *
   * @param list The list that must have at least one string.
   * @return true The list has at least one element.
   */
  private boolean valid( DivergentList list ) {
    return list != null && list.size() > 0;
  }

  /**
   * Test the functionality.
   */
  public static void main( String args[] ) {
    DivergentList list1 = new DivergentList();
    list1.addLast( "name" );
    list1.addLast( "first" );
    list1.addLast( "middle" );
    list1.addLast( "last" );
    list1.addLast( "maiden" );

    DivergentList list2 = new DivergentList();
    list2.addLast( "name" );
    list2.addLast( "middle" );
    list2.addLast( "last" );

    // Prints 1
    out.println( list2.diverges( list1 ) );

    list1.clear();
    list1.addLast( "name" );
    list1.addLast( "middle" );
    list1.addLast( "last" );

    list2.clear();
    list2.addLast( "name" );
    list2.addLast( "middle" );
    list2.addLast( "last" );
    list2.addLast( "maiden" );
    list2.addLast( "honorific" );

    // Prints 3
    out.println( list2.diverges( list1 ) );

    list1.clear();
    list2.clear();

    // Prints -1
    out.println( list1.diverges( list2 ) );

    list1.add( "name" );
    list2.add( "address" );

    // Prints 0
    out.println( list1.diverges( list2 ) );

    list2.addFirst( "name" );

    // Prints 1
    out.println( list1.diverges( list2 ) );
  }
}

Questions

A few questions:

How can the code be simplified (e.g., use a different structure)?
How can the code be improved (e.g., use generics; change the name, etc.)?
How would you make the code null-safe (e.g., override all add methods)?
How would you optimize the code?


texts: Represent number N (N>1) as N = x + y, where x * y is the maximum possible value

texts:
My problem is the following: I have to find such x and y (x+y=N) where x * y is the maximum possible value.

I have written an algorithm (which I think works well) and I am looking for more efficient and faster way to solve this problem.

This is my algorithm:

static void Find(int N)
{
    int multiplication = 1;
    int maxMultiplication = 1;
    int desiredNumber = 1;

    for (int i = 1; i <= N / 2; i++)
    {
        multiplication = i * (N - i);
        if(multiplication > maxMultiplication)
        {
            maxMultiplication = multiplication;
            desiredNumber = i;
        }
    }

    Console.WriteLine("Desired presentation of number N is --> N = {0} + {1}",
        desiredNumber, N - desiredNumber);
}


texts: Data Structures BFS Breadth First Search algorithm improvement written in python

texts:
I have written a python program that calculates the traversal of BFS, but it seems inefficient to me for few reasons like

I have to say, how many nodes there will be at the start
I'm not satisfied with the node assigning technique

How to make it more efficient that is my question?

from __future__ import print_function

class graph(object):

    def __init__(self, n):
        #initiating a 2d array for the graph
        self.graph = [[] for y in range(n)]

        #al the alphabet characters ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
        #                            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
        self.alphab = [chr(97+i) for i in range(26)]

    def addnode(self,u,v):
        #populating the 2d array with the graph
        self.graph[self.alphab.index(u)].append(self.alphab.index(v))

    #printing the graph if needed
    def printgraph(self):
        print('\nthe graph structure is {}\n'.format(self.graph))

    def bfs(self, node):

        node = self.alphab.index(node)
        print('\nstarting from node {} the traverse is '.format(self.alphab[node]))
        queue = []
        visited = [None for i in range(len(self.graph))]
        parent = [None for i in range(len(self.graph))]
        distance = [0 for i in range(len(self.graph))]
        queue.append(node)
        visited[node] = True

        while len(queue) != 0:
            node = queue[0]
            del queue[0]
            for i in self.graph[node]:
                if visited[i] == None:
                    queue.append(i)
                    visited[i] = True
                    distance[i] = distance[node] + 1
                    parent[i] = node

        #converting them back to ascii when printing using the list we created
            print('--->', self.alphab[node],)
        print('\nthe distance structure is {}\n\nand the parents structure is {}'.format(distance, [ self.alphab[i] if i != None else i for i in parent ]),)

def main():
    #How many nodes there will be initiating with the constructor
    se = graph(4)

    """
        a----b.
        |  .
        |.
        c----d """

    se.addnode('a', 'b')
    se.addnode('a', 'c')
    se.addnode('b', 'c')
    se.addnode('c', 'a')
    se.addnode('c', 'd')
    se.addnode('d', 'd')

    se.printgraph()
    se.bfs('a')

if __name__ == '__main__':
    main()

the output for starting from node 'a'

the graph structure is [[1, 2], [2], [0, 3], [3]]

starting from node a the traverse is
---> a
---> b
---> c
---> d

the distance structure is [0, 1, 1, 2]

and the parents structure is [None, 'a', 'a', 'c']


texts: Macro for inserting a mini table between heading styles

texts:
I am using the following macro to insert a mini table of contents between two identical heading styles (e.g. Heading 1). It works fine on small documents, but is very slow on large documents.

Sub MiniTOCFields(HeadingNumber As Long, TOCLevel As Long)
' Add mini TOC fields after all occurrences of a Heading style.
' HeadingNumber defines which heading style to add miniTOCs to (e.g. Heading 1).
' TOCLevel defines to what heading level the TOC shows (e.g. down to Heading 3).

Dim bookmarkNumber As Long
Dim bookmarkRange As Range
Dim insertRange As Range
Dim currentParagraph As Paragraph
Dim paragraphNumber As Long
Dim currentTOC As TableOfContents

bookmarkNumber = 0
paragraphNumber = 0

Set bookmarkRange = ActiveDocument.Paragraphs(1).Range

' Cycle through each paragraph
For Each currentParagraph In ActiveDocument.Paragraphs
     paragraphNumber = paragraphNumber + 1

     ' Check for occurrences of Heading X
     If currentParagraph.Style = "Heading " & HeadingNumber Then

         ' If this is not the first occurrence, create a bookmark between this and the last one
         If bookmarkNumber > 0 Then
             ' Move the end of the bookmark to the end of the next paragraph
             bookmarkRange.End = ActiveDocument.Paragraphs(paragraphNumber - 1).Range.End
             ' Create bookmark
             ActiveDocument.Bookmarks.Add _
                 Name:="TOCHeading" & HeadingNumber & "_" & bookmarkNumber, _
                 Range:=bookmarkRange
         End If

     ' Move the start of the bookmark to the beginning of the next paragraph
     bookmarkRange.Start = ActiveDocument.Paragraphs(paragraphNumber + 1).Range.Start

     ' Add the TOC field
     Set insertRange = currentParagraph.Range
     insertRange.Collapse direction:=wdCollapseEnd
     ' Add the new TOC field
     ActiveDocument.Fields.Add _
         Range:=insertRange, _
         Type:=wdFieldEmpty, _
         Text:=" TOC \h \o """ & HeadingNumber & "-" & TOCLevel & """ \b TOCHeading" & HeadingNumber & "_" & (bookmarkNumber + 1)
     bookmarkNumber = bookmarkNumber + 1
 End If

    ' Check for last paragraph and bookmark
    If paragraphNumber = ActiveDocument.Paragraphs.Count Then
        ' Move the end of the bookmark to the end of this paragraph
        bookmarkRange.End = currentParagraph.Range.End
        ' Create bookmark
        ActiveDocument.Bookmarks.Add _
            Name:="TOCHeading" & HeadingNumber & "_" & bookmarkNumber, _
            Range:=bookmarkRange
        bookmarkNumber = bookmarkNumber + 1
     End If

Next currentParagraph

' Update all TOC fields in document
For Each currentTOC In ActiveDocument.TablesOfContents
     currentTOC.Range.Select
     currentTOC.Update
Next currentTOC

End Sub