Algorithm that creates in a systematic form, matrix with zero-one elements I'm working on Rcpp and I wrote the following code
#include <RcppArmadilloExtensions/sample.h>
using namespace Rcpp;

// [[Rcpp::export]]

arma::mat SIZA(int K){

  arma::mat P;
  P.ones(K,K);
  P = trimatu(P);

  for(int idx=1; idx<K; ++idx){
  arma::mat W;
  arma::mat Hlp;

  W.ones(K-idx,K-idx);
  W = trimatu(W);
  W = join_horiz(W,Hlp.zeros(K-idx,idx));

  P = join_vert(P,W);
  }

  return P;
}

Essentially it creates matrices of the following form
SIZA(2)
     [,1] [,2]
[1,]    1    1
[2,]    0    1
[3,]    1    0

SIZA(3)
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    0    1    1
[3,]    0    0    1
[4,]    1    1    0
[5,]    0    1    0
[6,]    1    0    0

SIZA(4)
      [,1] [,2] [,3] [,4]
 [1,]    1    1    1    1
 [2,]    0    1    1    1
 [3,]    0    0    1    1
 [4,]    0    0    0    1
 [5,]    1    1    1    0
 [6,]    0    1    1    0
 [7,]    0    0    1    0
 [8,]    1    1    0    0
 [9,]    0    1    0    0
[10,]    1    0    0    0

What I'm curious and interested in is finding a way to make this chunk of code quicker. But from my knowledge, I do not see an obvious way to achieve that. What I'm thinking is to avoid using the FOR LOOP but it seems to be necessary.
Any ideas would be great!

First of all I would try to avoid all the matrix operations like trimming, joining, etc.
And instead just find a function that tells what value is on each position
bool tell(int x, int y, int K);

This would of course require more mathematical skill then programming, but you might eventually end up doing just this:
arma::mat P;
int maxY = K * (K-1) / 2
for (int y = 0; y < maxY; ++y) {
  for (int x = 0; x < K; ++x) {
    P.set(x, y, tell(x, y, K) ? 1 : 0)
  }
}

If tell(x,y) has O(1) complexity, this will probably be faster.
Another option that I can think of is similar, but more iterative.
Fill the topmost KxK part of the matrix with the pattern.
Then another (K-1)x(K-1) part of the metrix, fill with the (K-1) pattern while filling the last column with zeroes.
then another part of size (K-2)x(K-2) fill with pattern of size K-2 while filling the last two columns with zeroes.
And so on and so on until trying to fill pattern 0x0 where you terminate.
But you'll end up on something similar to the previous
int Y = 0;
for (int step = K; step > 0; --step) {
  for (int y = 0; y < step; ++y, ++Y) {
    for (int x = 0; x < step; ++x) {
      P.set(x, Y, tell(x, y) ? 1 : 0)
    }
    for (int r = step; r < K; ++r) {
      P.set(r, Y, 0)
    }
  }
}

here the tell function actually becomes quite trivial and third parameter is not even needed
bool tell(int x, int y)
{
  return x >= y;
}

Whether the tell function for the first implementation is possible in O(1) and how to do it I leave to you and the mathematician inside of you :)


Exporting routes in node.js Express 4 I'm using Express 4 to provide routes to HTML/Jade files, and as well to provide an API.

I want to separate routes from server file, and go even further- separate API from WWW. My code now looks like this:

server.js

//================== ROUTES ======================================

require('./routes/api')(app, express);  //all api routes go in ./routes/api.js file
require('./routes/web')(app, express);  //all website routes go in ./routes/web.js file

apiroutes:

//================================== routes for API ====================================
module.exports = function(app, express) {
    'use strict';
    var api = express.Router();

    api.get('/somget', function(req, res) {
        res.send('some json');
    });

    // init api route. all api routes will begin with /api
    app.use('/api', api);

}

pages routes (both main template and partials):

//================================== routes for index and partials================================

module.exports = function(app, express) {
    'use strict';
    var router = express.Router();

    router.get('/', function(req, res) {
        res.render('index');
    });

    router.get('/partials/:name', function (req, res) {
        var name = req.params.name;
        res.render('partials/' + name);
    });

    //apply router
    app.use('/', router);

};

Is it okay to pass both app and express?

It works, but I'm not sure if such solution may have some bad impact on app and routing -passing both app and express to two functions.

You don't have to inject express into each modules.

requiring express in each file will return the same instance. http://nodejs.org/docs/latest/api/modules.html#modules_caching

Also, ask yourself who should be the one registering the router into the app. The app it self or the router ? For code reuse and clean separation you should let the app handle it. Let's say you have an authentication router used with the path '/auth'. You may want to reuse it in another app with a path like '/authentication'

Here's how I would do it.

server.js

//================== ROUTES ======================================
...

var api = require('./routes/api');
var router = require('./routes/web');

app.use('/api', api);
app.use('/', router);

...

apiroutes:

//================================== routes for API ====================================

var express = require('express');

module.exports = (function() {
    'use strict';
    var api = express.Router();

    api.get('/somget', function(req, res) {
        res.send('some json');
    });

    return api;
})();

pages routes (both main template and partials):

//================================== routes for index and partials================================

var express = require('express');

module.exports = (function() {
    'use strict';
    var router = express.Router();

    router.get('/', function(req, res) {
        res.render('index');
    });

    router.get('/partials/:name', function (req, res) {
        var name = req.params.name;
        res.render('partials/' + name);
    });

    return router;
})();


Draw grid programatically with borders I need to draw a grid with some special requirements. My current solution is working but is far away from ideal I guess. Attached you can find the current working solution:

Some of my thoughts which led to the current solution:

self.view.layer.border... is not working for me because I need different colors for different sides of the border. Also the outer cells of the grid would only have half the border of the other cells because their borders add up when positioned right next to each other.
I could draw a container view which draws the right colors and then position the cells with a gap of 1px but this solution also does not look very nice.

The currently used code snippet:

private func drawBorder() {
    //TODO: better way to do it ?
    var edgesToColorDifferently: UIRectEdge = []
    var edgesToDrawBold: UIRectEdge = []
    var defaultEdges: UIRectEdge = .all
    var colorForBoldEdge = Theme.shared.stoneBorderColor
    let lastLocation = self.tackleBoard.boardSize - 1

    if(location.row == 1 || location.row == lastLocation) {
        edgesToColorDifferently.insert(.top)
    }

    if(location.row == 0 || location.row == lastLocation - 1) {
        edgesToColorDifferently.insert(.bottom)
    }

    if(location.column == 1 || location.column == lastLocation) {
        edgesToColorDifferently.insert(.left)
    }

    if(location.column == 0 || location.column == lastLocation - 1) {
        edgesToColorDifferently.insert(.right)
    }

    if(self.tackleBoard.isCornerStone(atLocation: location)) {
        colorForBoldEdge = Theme.shared.alternateStoneBorderColor
        edgesToColorDifferently.insert(.all)
    }

    if(self.location.row == 0) {
        edgesToDrawBold.insert(.top)
    }

    if(self.location.row == lastLocation) {
        edgesToDrawBold.insert(.bottom)
    }

    if(self.location.column == 0) {
        edgesToDrawBold.insert(.left)
    }

    if(self.location.column == lastLocation) {
        edgesToDrawBold.insert(.right)
    }

    defaultEdges.remove(edgesToColorDifferently)
    defaultEdges.remove(edgesToDrawBold)

    self.addBorder([
        Border(edges: defaultEdges),
        Border(edges: edgesToDrawBold, color: colorForBoldEdge, thickness: 1.0),
        Border(edges: edgesToColorDifferently, color: Theme.shared.alternateStoneBorderColor)
    ])
}

addBorder() is a method in an UIView extension which adds a 1px view with the correct height/width/color to the given edge.

I would love to have your opinions on how to improve the drawing process because I know that the current code is not readable at all and is way too complicated for such a basic thing.

I solved it by drawing the borders 1px outside of the squares bounds. This way I was able to simplify my method enormously:

private func drawBorder() {
        var borders: [Border] = [Border(edges: .all)]
        if self.tackleBoard.isCornerStone(atLocation: location) {
            borders.append(Border(edges: .all, color: Theme.shared.alternateStoneBorderColor))
        }
        if location.row == tackleBoard.boardSize - 1 || location.row == 1 {
            borders.append(Border(edges: .top, color: Theme.shared.alternateStoneBorderColor))
        }
        if location.column == tackleBoard.boardSize - 1 || location.column == 1 {
            borders.append(Border(edges: .left, color: Theme.shared.alternateStoneBorderColor))
        }

        self.addBorder(borders)
    }

This produces the same result as my first code snippet but is way more readable I guess. I am happy with the result now. However if someone has a better solution for me I'd love to improve the code :)


Integer pairs of a product in Python This bit of code is looking for the pair of integers that make up a product.
You can input any integer and it returns the pairs of numbers that, when multiplied together, make it up.
I have done a pretty bad job at explaining this, but the code it pretty simple.
All comments welcome.
num = 498
num_1 = 1

while num_1 < num:
    product = num / num_1
    if product < num_1:
        break
    if product - int(product) == 0:
        print(f"{num_1}, {int(product)}")
    num_1 += 1

Such while-loops that don't offer an advantage are unpythonic. And harder to understand. In your solution, the information about what the loop goes through is spread over six (!) lines. And I actually I need to read all 8 (!) lines, since I need to read the if/print as well in order to see that they don't affect the loop. Better use a for-loop, where it's all cleanly in one line.

If you do use such a while-loop, then there should be an empty line above your num_1 = 1, not under it. Because it belongs to the solution, not to the input.

Floating-point in general has precision issues, better avoid it if you can. Here it's simple to use just integers.

For num = 1, you don't print anything. You should print 1, 1.

Unless you have an actual reason for that formatting, keep it simple and don't format. If you use the output of your program as input for something else, it might also very well be easier to parse without that extra comma.

Resulting code:
from math import isqrt

num = 498

for i in range(1, isqrt(num) + 1):
    if num % i == 0:
        print(i, num // i)


Closest greater lexicographical permutation I wrote this algorithm to find the closest greater lexicographical permutation.
Given a word w, it rearranges the letters to construct another word in such a way that this new word is lexicographically greater than w. From all the possibilities the one that produces the smallest lexicographical word is chosen.

Any ideas to improve the algorithm, it's readability, or the style in general?

#include <iterator>
#include <vector>
#include <algorithm>

using namespace std;

string closest_greater_lexicographical_permutation(string w) {
    for (auto rit = next(w.rbegin()); rit!=w.rend(); ++rit) {
        auto it = prev(rit.base());
        auto lowest_max = it;
        for (auto it_max = next(it); it_max!=w.end(); ++it_max) {
           if (string(1,*it_max) > string(1,*it)){
               if (lowest_max == it){
                   lowest_max = it_max;
               }
               else if (string(1,*it_max) < string(1,*lowest_max)){
                   lowest_max = it_max;
               }
           }
        }
        if (lowest_max != it){
            iter_swap(lowest_max, it);
            sort(next(it),w.end(),
                 [](auto lhs, auto rhs){
                     return string(1,lhs) < string(1,rhs);
                 });
            return w;
        }
    }
    return w;
}

Headers and namespaces

We don't use <vector>, but do need <string>.

Bringing all names in from a namespace is problematic; namespace std particularly so.  It can silently change the meaning of your program when you're not expecting it.  Get used to using the namespace prefix (std is intentionally very short), or importing just the names you need into the smallest reasonable scope.

Bug

This loop fails when w is the empty string:

for (auto rit = next(w.rbegin()); rit!=w.rend(); ++rit) {

This can be fixed with an early return:

if (w.empty())
    return w;

Algorithm

There's a standard algorithm called std::next_permutation() that does most of what we need.  The one difference is that if its input is already the last permutation, it will wrap around to the first one and indicate (by its return value) that it has done so. We can use that to return the original input string in that case.

Tests

You've included no tests.  I think we need to test the trivial case (empty string) and at least one case where we must return a new string and one where we already have the last possible permutation.

Improved code

#include <algorithm>
#include <string>

std::string closest_greater_lexicographical_permutation(const std::string& input) {
    auto word = input;
    return std::next_permutation(word.begin(), word.end())
        ? word   // we changed it
        : input; // no change - already the last permutation
}

// Test program

#include <iostream>
static bool test(const std::string& input, const std::string& expected)
{
    auto const actual = closest_greater_lexicographical_permutation(input);
    if (actual == expected)
        return 0;

    std::cerr << "FAIL (\"" << input << "\"): "
        "got \"" << actual << "\" "
        "instead of \"" << expected << "\"" << std::endl;
    return 1;
}

int main()
{
    return 0
        + test("", "")
        + test("aabb", "abab")
        + test("cba", "cba")
        ;
}


Convert an integer to 4 bytes without bitshift operators This code is applicable to either GLSL or C due to virtually identical syntax. Before GLSL 1.3, bitshift operators were not present and I am aiming for backwards compatibility to GLSL 1.2.

float lut[3] = {256,65536,16777216};

vec4 getBytes(float n)
 {
   vec4 bytes;
   bytes.x = floor(n / lut[2]);
   bytes.y = floor((n- bytes.x*(lut[2]) )/ lut[1]);
   bytes.z = floor((n - bytes.y*(lut[1]) - bytes.x*lut[2] )/ lut[0]) ;
   bytes.w = n - bytes.z*lut[0] - bytes.y*lut[1] - bytes.x*lut[2];
   return bytes;
 }

 float getid(vec4 bytes)
 {
   return bytes.w + bytes.z*lut[0]+ bytes.y*lut[1] + bytes.x*lut[2];
 }

This code will be used to pack a value greater than 8 bits into an 8-bit texture with 4 channels.

I also ported the code to Lua to run a unit test.

 lut = {256,65536,16777216}
 function getBytes(n)

   bytes = {}
   bytes.x = math.floor(n / lut[3]);
   bytes.y = math.floor((n- bytes.x*(lut[3]) )/ lut[2]);
   bytes.z = math.floor((n - bytes.y*(lut[2]) - bytes.x*lut[3] )/ lut[1]) ;
   bytes.w = n - bytes.z*lut[1] - bytes.y*lut[2] - bytes.x*lut[3];
   return bytes
 end

 function getid(bytes)

   return bytes.w + bytes.z*lut[1]+ bytes.y*lut[2] + bytes.x*lut[3]
 end

for i=1,math.pow(2,24),1 do
  if not (getid(getBytes(i)) == i) then

    print("Fail " .. i)
  end

end

There's not a ton that can be improved here, but:

lut should be made const
Due to implicit promotion, lut can be stored as integers instead of floats. Your expressions should evaluate to the same thing.
Consider representing your lut constants in hexadecimal (0x) format - or as 1 << x notation.
Your lut doesn't strictly need to be an array; you're not iterating over it or indexing it dynamically. As such, you may be better off simply making individually-named constants such as XL, YL, ZL.


Console-based Guess the Number I started learning Python and wrote my first program. This is a console game based on guessing the number. Can you take a look and tell me if I'm doing it right or give me any advice? Here is the code: https://github.com/theriseq/GuessTheNumber

Is it a good way of doing this or can I simplify it?

Thank you.

main.py

from can_convert import check
import random

welcome_msg = """Welcome to GuessTheNumber Game!
I\'m thinking of a number between 1 and 100.
Can you guess this number?

[?] Type \'pass\' to exit game.
"""
print(welcome_msg)

number_to_guess = random.randrange(1, 101)
attemps = 0

while True:
    user_inp = str(input("Your choice: "))
    if user_inp != "pass":
        if check(user_inp):
            attemps += 1
            # print('is int')
            if int(user_inp) < number_to_guess:
                print("Your guess is too small.")
            elif int(user_inp) > number_to_guess:
                print("Your guess is too high.")
            else:
                print("Correct! You won the game!")
                print(f"Attemps: {attemps}")
                break
        else:
            print('Enter numbers only!')
    else:
        break

can_convert.py

def check(to_check):
    try:
        int(to_check)
        return True
    except ValueError:
        return False

Here is a list of found issues, in order of occurrence:

importing from your custom can_convert is fine. You should stick to modularization like that for bigger projects. However, here, it actually seems to be in the way of things. It checks if the string can be converted, but does not actually do any conversion. We later have to do the conversion again anyway. This can be combined.
Currently, the welcome message and actual random range can deviate. You already know what f-strings are, so they should be used to keep that in line.
Use randint to include both bounds, as opposed to randrange, which would stop one short of upper_bound, as you already noted.
input will already return a string, no need to convert via str.
By inverting the logic in checking for "pass", a whole block of indentation can be spared. Just check if user_inp == "pass" and break if True. All following code can be one level less indented, since no else statement is required.
Now for the main logic. The try block has moved here, away from the import. Notice that the logic is still the same. However, we assign to number immediately without checking first. If this fails, the except block is triggered.

Note that int("2.2") will also be a ValueError despite "2.2", to humans, clearly being a number. So the error message should specify whole numbers to be more precise.
The added else block is run if no exception was raised, so in the normal game.
number is now available, so no need to call int conversion repeatedly.

Keep try blocks short and concise and never leave an except statement bare. You did this fine! Lastly, try to not nest try/except blocks.
There was a typo for attempts.

import random

upper_bound = 100
lower_bound = 1

welcome_msg = f"""Welcome to GuessTheNumber Game!
I'm thinking of a number between {lower_bound} and {upper_bound}.
Can you guess this number?

[?] Type 'pass' to exit game.
"""
print(welcome_msg)

number_to_guess = random.randint(lower_bound, upper_bound)
attempts = 0

while True:
    user_inp = input("Your choice: ")
    if user_inp == "pass":
        break
    try:
        number = int(user_inp)
    except ValueError:
        print('Enter whole numbers only!')
    else:  # no exception occurred
        attempts += 1
        if number < number_to_guess:
            print("Your guess is too small.")
        elif number > number_to_guess:
            print("Your guess is too high.")
        else:
            print("Correct! You won the game!")
            print(f"Attempts: {attempts}")
            break


Displaying available destinations using Angular.JS I wonder to know if there is any bad smell on my practice for i18n on Angular.
I put the I18n translating function on Angular controller (because I don't know how to put it on HTML template file)

And about the i18n scope, I use this way I18n.t("city." + city_name) to indicate that the city_name is under "city" scope. Could I write it in this way I18n.t(city_name, scope: "city") to make it more understandable.

I appreciate every comment and suggestion to enhance my current solution.

Data structure

departures_lst is a list of countries' English name e.g.,: [US, China, Japan]

Each country has many cities name. e.g. [New York, LA, Boston]

Angular controller

App.controller("departures_ctrl", function($scope, $location, $http) {
    $http.get("/departures.json")
    .success(function (response) {
      $scope.departures_lst = response;
    });
    $scope.get_destinations = function(city_name) {
          return $location.url("/depart_from/" + city_name);
    };
    $scope.i18nCountryName = function(country_name) {
          return I18n.t("country." + country_name) + country_name
    };
    $scope.i18nCityName = function(city_name) {
          return I18n.t("city." + city_name) + city_name
    };
});

HTML teamplate?

{{i18nCountryName(departure.country)}}

        {{i18nCityName(city)}}

Translations are UI logic, a different representation of the same data. They should stay on the template. On the other hand, country and city logic go in the controller. They should only be aware of the data, but not how it is represented.

The answer to this is to use Angular filters, and a separate service/factory/provider to hold the current language. The controller exposes the data to the UI, but the data is language-neutral.

Given that this is so similar, here's another answer where I explained how to use Angular filters with translations: https://codereview.stackexchange.com/a/116063/11919


Is this code likely to stand the test of time? (Add select box to DOM with JS) The reason I asked about standing the test of time is that I previously used PHP echo to create a string and placed it into a div with .innerHTML, but modern browsers were not passing the value of the select box. (Went back and tested wit IE9 and the innerHTML version worked.)

file:js_selecttest.php

<form method="post" action='js_selecttest.php'>

    <input type="text" name="text_input" onClick="create_sel(document.getElementById('select_holder'),'class_date',op_array)">

Filled by JS

<input type="submit">

</form>

<?php
if($_POST)
  {
    var_dump($_POST);
  }
?>

Would you have any suggestions or improvements?

To answer your question in the title: no
For multiple reasons. One being that the things we use today could be out-dated in a few years. who knows
One question/remark:
sel_obj.options[i]=new Option(op_array[i], i);
sel_obj.options[i].value=op_array[i];

is there a reason why you are setting the value twice? (once in the new Option call and once on the next rule)
My suggestions are:
Separation of concerns
You have one file with html, javascript and php all interweaved with each other. So debugging won't make it any easier. Alsog reusing a part of that script is nearly impossible since everything is hard coded.
Overuse of javascript
Is there a reason why you are building the html with javascript? Because this has some drawbacks: Screenreaders will not be able to see the selectlist items since they don't parse javascript. Serach engines will not know about the select-options.
Instead you should use PHP to create the html for the select items.
Hard to read code
The code is very hard to read, the function and variable names don't clearly tell what they are or have typehinting in the names. For instance op_array variable. Words as Array, String, int,.. should  be banned from variable names. Simply call it options. Now by simply reading it I allready know that is has multiple values and that it are the options. Where as op_array tells me that it is an array of 'op'.
Also for future enhancements. If for some reason you are going to change the way options are passed in, you will also have to change the name of the variable...
Also be more consistent in your naming. populateList() needs a select and options. But no list. This is confusing.
Personally I never use the onClick attributes inhtml. Not because they are bad. But because if you do use them, javascript will be hidden in the html and tightly coupled with it whereas if you EvntListners it is very easy to change the html without having to bother about the javascript. this is also better for sepperation of concerns


Vectors assignations and operations in a loop and parallelization with OpenMP I use this piece of code to compute a short-time Fourier transform:

// Output pre-allocation
std::vector<std::vector<std::complex<T> > > y(nFft, std::vector<std::complex<T> >(nFrames, 0.0));

std::vector<T> xt;
std::vector<std::complex<T> > yt;
xt.reserve(nFft);
yt.reserve(nFft);

#pragma omp parallel for private(xt,yt)
for (unsigned int t = 0; t < nFrames; ++t) {
    const int offset = -((int) wSize/2) + t*nHop;

    // Extract frame of input data
    if (offset < 0) {
        xt.assign(x.begin(), x.begin() + offset + wSize);
        xt.resize(wSize, 0.0);
    }
    else if (offset + wSize > n) {
        xt.assign(x.begin() + offset, x.end());
        xt.resize(wSize, 0.0);
    }
    else
        xt.assign(x.begin() + offset, x.begin() + offset + wSize);

    // Apply window to current frame
    std::transform(xt.begin(), xt.end(), w.begin(), xt.begin(), std::multiplies<T>());

    // Zero padding
    std::rotate(xt.begin(), xt.begin() + wSize/2, xt.end());
    xt.insert(xt.begin() + wSize/2, nFft - wSize, 0.0);

    yt = fft(xt);  // Perform the FFT!

    #pragma omp critical
    {
        for (unsigned int f = 0; f < nFft; ++f)
            y[f][t] = yt[f];
    }
}

Is there something that I can improve, either in design style or performances, without sacrificing readability?
Of course the point is to improve my coding skills without using any external library!

Regarding the xt and yt vectors, I put them outside the loop to improve the performances in mono-threaded (when OpenMP is disabled). In multi-threaded, there are copied for each thread anyway (hence the use of the private close).

The only thing worth commenting on is the identifier names could be more meaningful.

Short identifiers are harder to find when maintaining the code (more false positives).
Short identifiers make it hard to put meaning to the identifier.


Slicing and dicing images for Google Play Making an Android app involves making a lot of images of various sizes:

The app's launcher icon, in high/low/medium resolution
Buttons, menu buttons, if any, in high/low/medium resolution
Images for listing on Google Play: main icon, feature graphics

To simplify this, I use high-resolution source images
+ ImageMagick to cut to the various sizes
+ Makefile to only regenerate the images whose source image has changed.

For the feature graphics I cheat:
I just generate a transparent canvas with the required dimensions,
and overlay on top of it the app's icon, as big as it fits.

src_dir:=src
hdpi_dir:=res/drawable-hdpi
ldpi_dir:=res/drawable-ldpi
mdpi_dir:=res/drawable-mdpi

names:=$(patsubst $(src_dir)/%,%,$(wildcard $(src_dir)/*.png $(src_dir)/*.jpg))
hdpi_target:=$(patsubst %,$(hdpi_dir)/%,$(names))
ldpi_target:=$(patsubst %,$(ldpi_dir)/%,$(names))
mdpi_target:=$(patsubst %,$(mdpi_dir)/%,$(names))

appicon:=googleplay/appicon.png
feature:=googleplay/feature.png
canvas:=googleplay/canvas.png
work:=googleplay/work.png

default: all

hdpi: $(hdpi_dir) $(hdpi_target)

ldpi: $(ldpi_dir) $(ldpi_target)

mdpi: $(mdpi_dir) $(mdpi_target)

googleplay: $(appicon) $(feature)

all: hdpi ldpi mdpi googleplay

clean:
    rm $(hdpi_target) $(ldpi_target) $(mdpi_target) $(appicon) $(feature)

$(hdpi_dir):
    @mkdir -p $@

$(ldpi_dir):
    @mkdir -p $@

$(mdpi_dir):
    @mkdir -p $@

$(hdpi_dir)/btn_%.png: $(src_dir)/btn_%.png
    convert -geometry 48x $< $@
    identify $@

$(ldpi_dir)/btn_%.png: $(src_dir)/btn_%.png
    convert -geometry 24x $< $@
    identify $@

$(mdpi_dir)/btn_%.png: $(src_dir)/btn_%.png
    convert -geometry 36x $< $@
    identify $@

$(hdpi_dir)/launcher_%.png: $(src_dir)/launcher_%.png
    convert -geometry 72x $< $@
    identify $@

$(ldpi_dir)/launcher_%.png: $(src_dir)/launcher_%.png
    convert -geometry 36x $< $@
    identify $@

$(mdpi_dir)/launcher_%.png: $(src_dir)/launcher_%.png
    convert -geometry 48x $< $@
    identify $@

$(appicon): $(src_dir)/launcher_main.png
    @mkdir -p $(@D)
    convert -geometry 512x $< $@
    identify $@

$(feature): $(src_dir)/launcher_main.png
    @mkdir -p $(@D)
    convert -size 1024x500 xc:transparent $(canvas)
    convert -geometry 1024x500 $< $(work)
    convert -composite $(canvas) $(work) -gravity west $@
    identify $@

If you want to play with this,
save this script as Makefile,
put an image file in src/launcher_main.png,
and assuming you have ImageMagick installed,
simply run make to have the images of various sizes generated in the res directory.
In Android projects I have res pointing to the real resources directory of the project.

This works well, but it's a bit repetitive at some places.
I'm wondering if this can be more DRY,
or if there are other ways to improve.

Use target- and pattern-specific variables:

$(ldpi_dir)/btn_%.png : GEOMETRY := 24x
$(mdpi_dir)/btn_%.png : GEOMETRY := 36x
$(hdpi_dir)/btn_%.png : GEOMETRY := 72x

and similar definitions for launcher_%.png

Then you can combine all the individual recopies into a single one:

$(ldpi_dir)/%.png $(mdpi_dir)/%.png $(hdpi_dir)/%.png) :
    convert -geometry $(GEOMETRY) $< $@
    identify $@


Fast Prime Sieve (Python Implementation) A modified version of the prime sieve. I actually doubt it could be implemented any faster, but I might be wrong:

from math import sqrt

def sieve(n):
    """
        *   A fast implementation of the prime sieve algorithm. Finds all prime numbers up to a given upper bound.

        *   Params:
            *   n: The upper bound.
        *   Return:
            *   A list of all prime numbers in [0, n].
        *   Implementation:
            *   Implements the sieve of Erasthotenes with a few optimisations for speed.
            *   We only delete multiples of numbers up to `int(sqrt(n))`. This suffices to find all prime numbers.
            *   We manipulate a list of boolean values, indexed by the number itself with the values denoting its primality. This is much faster than deleting elements in the list.
    """
    if n < 2:   return []
    prime = [True]*(n + 1)      #Initialise the list.
    rt = int(sqrt(n))+1
    for x in range(2, rt):      #We only need to seive multiples of numbers <= to `n`.
        if prime[x]:
            for c in range(x*x, n + 1, x):      prime[c] = False
    return [idx for idx, p in enumerate(prime) if p][2:]

Since this is tagged beginner, you should know that speed isn't everything. First of all you should care about making your code readable as ultimately that is more important.

You should always put new statements on new lines. At first I ignored your first guard statement, due to this. I only caught on that you'd done this when I saw the second from last line was huge.
Your comments aren't great. Yes [True]*(n + 1) initializes a list, we can read the code to understand that.
Your variable names don't really help reading the code.

def sieve(limit):
    if limit < 2:
        return []

    limit += 1  # Preincrement `limit` so `sieve` is inclusive, unlike `range`.
    primes = [True]*limit
    for base in range(2, int(limit**0.5 + 1)):
        if primes[base]:
            for composite in range(base*2, limit, base):
                primes[composite] = False
    return [num for num, is_prime in enumerate(primes) if is_prime][2:]

I'd like to point out that your memory usage can be improved. How many \$O(n)\$ lists do you have?

primes to perform the sieve.
Your list comprehension, that gets all the indexes.
Your duplication of the list comprehension to remove two values.

You can remove the second by returning an iterator. As for the third, you can instead perform an islice, or setting 0 and 1 to false. Yes this is now 10% slower.

However if you change the second for loop to run in C land rather than Python land you net a 40% speed-up from the original.

from math import ceil

def sieve(limit):
    if limit < 2:
        return []

    limit += 1  # Preincrement `limit` so sieve is inclusive, unlike `range`.
    primes = [True]*limit
    for base in range(2, int(limit**0.5 + 1)):
        if primes[base]:
            primes[base*2:limit:base] = [False]*(ceil(limit / base) - 2)

    primes[0] = primes[1] = False
    return (num for num, is_prime in enumerate(primes) if is_prime)

From this changing the last line to use itertools.compress rather than an iterator comprehension reduces time an additional 40% (nearly 70% from the original).

from math import ceil
from itertools import compress

def sieve(limit):
    if limit < 2:
        return []

    limit += 1  # Preincrement `limit` so sieve is inclusive, unlike `range`.
    primes = [True]*limit
    for base in range(2, int(limit**0.5 + 1)):
        if primes[base]:
            primes[base*2:limit:base] = [False]*(ceil(limit / base) - 2)

    primes[0] = primes[1] = False
    return compress(range(limit), primes)


Increase the speed of this python code for text processing I need to iterate through a file of words and perform tests on them.

import multiprocessing
import datetime

class line_Worker(object):
    def worker(self, chunks):
        for l in chunks:
            num = 0
            for num in range(100):
                print l, num
        return

if __name__ == '__main__':
    lines = [line.rstrip('\n') for line in open('words.txt')] #opens file and saves to list
    chunkNum = len(lines) / 26
    wordLists = [lines[x:x + chunkNum] for x in range(0, len(lines), chunkNum)]#divides list by 4 and saves to list of lists
    jobs = []
    timeStart = str(datetime.datetime.now())

    for i in range(27):
        p = multiprocessing.Process(target=line_Worker().worker, args=(wordLists[i],))
        jobs.append(p)
        p.start()
    for p in jobs:
        p.join()  # wait for the process to finish

    timeStop = str(datetime.datetime.now())

    print timeStart
    print timeStop

I have a problem set of 35,498,500 individual lines, and I need to try to get it to run in roughly 3 minutes. The current run times are 16 minutes.

Is there any way to speed this up? Thanks all!

Getting rid of the print statement fixed it thanks all!

import multiprocessing
import datetime

class line_Worker(object):
    def worker(self, chunks):
        for l in chunks:
            num = 0
            for num in range(100):
                print l, num#clearing this made the time 4 seconds thanks all!!
        return

if __name__ == '__main__':
    lines = [line.rstrip('\n') for line in open('words.txt')] #opens file and saves to list
    chunkNum = len(lines) / 26
    wordLists = [lines[x:x + chunkNum] for x in range(0, len(lines), chunkNum)]#divides list by 4 and saves to list of lists
    jobs = []
    timeStart = str(datetime.datetime.now())

    for i in range(27):
        p = multiprocessing.Process(target=line_Worker().worker, args=(wordLists[i],))
        jobs.append(p)
        p.start()
    for p in jobs:
        p.join()  # wait for the process to finish

    timeStop = str(datetime.datetime.now())

    print timeStart
    print timeStop


Efficient NumPy sliding window function Here is a function for creating sliding windows from a 1D NumPy array:
from math import ceil, floor
import numpy as np

def slide_window(A, win_size, stride, padding = None):
    '''Collects windows that slides over a one-dimensional array.

    If padding is None, the last (rightmost) window is dropped if it
    is incomplete, otherwise it is padded with the padding value.
    '''
    if win_size <= 0:
        raise ValueError('Window size must be positive.')
    if not (0 < stride <= win_size):
        raise ValueError(f'Stride must satisfy 0 < stride <= {win_size}.')
    if not A.base is None:
        raise ValueError('Views cannot be slided over!')

    n_elems = len(A)
    if padding is not None:
        n_windows = ceil(n_elems / stride)
        A = np.pad(A, (0, n_windows * win_size - n_elems),
                   constant_values = padding)
    else:
        n_windows = floor(n_elems / stride)
    shape = n_windows, win_size

    elem_size = A.strides[-1]
    return np.lib.stride_tricks.as_strided(
        A, shape = shape,
        strides = (elem_size * stride, elem_size),
        writeable = False)

(Code has been updated based on Marc's feedback)
Meant to be used like this:
>>> slide_window(np.arange(5), 3, 2, -1)
array([[ 0,  1,  2],
       [ 2,  3,  4],
       [ 4, -1, -1]])

Is my implementation correct? Can the code be made more readable? In NumPy 1.20 there is a function called sliding_window_view, but my code needs to work with older NumPy versions.

Few suggestions:

Input validation: there is no input validation for win_size and padding. If win_size is -3 the exception says ValueError: Stride must satisfy 0 < stride <= -3.. If padding is a string, numpy throws an exception.

Type hints: consider adding typing to provide more info to the caller.

f-Strings: depending on the version of Python you use, the exception message can be slightly simplified.
From:
if not (0 < stride <= win_size):
    fmt = 'Stride must satisfy 0 < stride <= %d.'
    raise ValueError(fmt % win_size)

To:
if not 0 < stride <= win_size:
    raise ValueError(f'Stride must satisfy 0 < stride <= {win_size}.')

Duplication: the statement shape = n_windows, win_size seems duplicated and can be simplified.
From:
if padding is not None:
    n_windows = ceil(n_elems / stride)
    shape = n_windows, win_size
    A = np.pad(A, (0, n_windows * win_size - n_elems),
             constant_values = padding)
else:
    n_windows = floor(n_elems / stride)
    shape = n_windows, win_size

To:
if padding is not None:
    n_windows = ceil(n_elems / stride)
    A = np.pad(A, (0, n_windows * win_size - n_elems),
                   constant_values=padding)
else:
    n_windows = floor(n_elems / stride)
shape = n_windows, win_size

Warning: FYI on the doc of np.lib.stride_tricks.as_strided there is a warning that says This function has to be used with extreme care, see notes.. Not sure if it applies to your use case but consider checking it out.


Finding the difference between postal addresses I have to compose an email where I have to list a list of difference between old and new value of a list of addresses. Only a subset of properties have to be listed, so the first thing that pops into my mind is doing something like this:

private static List<Difference> RetrieveDifferences(IEnumerable<AddressPoco> newValues, AddressPoco item){
  var result = new List<Difference>();
  var c = newValues.SingleOrDefault(x => x.xedsAddressID == item.xedsAddressID);
  if (c != null)
  {
    if (item.xgeoCountryID != c.xgeoCountryID){
       result.Add(new Difference("Country", item.xgeoCountryID.ToString(), c.xgeoCountryID.ToString()));
    }
    if (item.AddressTypeName != c.AddressTypeName){
       result.Add(new Difference("Address Type", item.AddressTypeName, c.AddressTypeName));
    }
    if (item.City != c.City){
       result.Add(new Difference("City", item.City, c.City));
    }
    if (item.POBox != c.POBox){
       result.Add(new Difference("POBox", item.POBox, c.POBox));
    }
    if (item.MainContact != c.MainContact){
       result.Add(new Difference("Main Contact", item.MainContact, c.MainContact));
    }
    if (item.PostCode != c.PostCode){
       result.Add(new Difference("PostCode", item.PostCode, c.PostCode));
    }
    if (item.Street != c.Street){
       result.Add(new Difference("Street", item.Street, c.Street));
    }
 }
 return result;
}

public class Difference {
  public string Key { get; set; }
  public string OldValue { get; set; }
  public string NewValue { get; set; }
  public Difference(string key, string oldValue, string newValue)
  {
    Key = key; OldValue = oldValue; NewValue = newValue;
  }
}

However, Visual Studio shows me a low maintainability index of 50 and high cyclomatic complexity in 18 rows.  I can understand why.  Any suggestion for how to improve it?

The characteristic numbers are not always indicating bad code...

However, you could do something like that to improve the numbers:

private static List<Difference> RetrieveDifferences(IEnumerable<AddressPoco> newValues, AddressPoco item)
{
    var c = newValues.SingleOrDefault(x => x.xedsAddressID == item.xedsAddressID);
    if (c != null)
        return GetDifferences(c, item).ToList();

    return Enumerable.Empty<Difference>().ToList();
}

private static IEnumerable<Difference> GetDifferences(AddressPoco newAdress, AdressPoco oldAddress)
{
    if (item.xgeoCountryID != c.xgeoCountryID)
        yield return new Difference("Country", item.xgeoCountryID.ToString(), c.xgeoCountryID.ToString()));
    if (item.AddressTypeName != c.AddressTypeName)
        yield return new Difference("Address Type", item.AddressTypeName, c.AddressTypeName));
    if (item.City != c.City)
        yield return result.Add(new Difference("City", item.City, c.City));
    if (item.POBox != c.POBox)
        yield return result.Add(new Difference("POBox", item.POBox, c.POBox));
    if (item.MainContact != c.MainContact)
        yield return result.Add(new Difference("Main Contact", item.MainContact, c.MainContact));
    if (item.PostCode != c.PostCode)
        yield return result.Add(new Difference("PostCode", item.PostCode, c.PostCode));
    if (item.Street != c.Street)
        yield return result.Add(new Difference("Street", item.Street, c.Street));
}


Modified Fibonacci method implementation - dynamic programming I found this question on Hackerrank in dynamic programming:

  Define a modified Fibonacci sequence as:
  $$t_{n+2} = t_n  + t_{n+1}^2$$

  Given three integers, \$t_1\$, \$t_2\$, and \$n\$, compute and print
  term \$t_n\$of a modified Fibonacci sequence.

Could this efficiency be improved?

t1,t2,n = map(int, raw_input().split(" "))
array =[]
array = array+[t1, t2]
for i in range(2,n):
   ele = array[i-2] + array[i-1]*array[i-1]
   array.append(ele)
print array[n-1]

Making the array initialization better:

t1,t2,n = map(int, raw_input().split(" "))
array = [t1, t2]
for i in range(2,n):
   ele = array[i-2] + array[i-1]*array[i-1]
   array.append(ele)
print array[n-1]

Currently your code needs \$\mathcal{O}(n)\$ memory, because you keep all terms. It would be more efficient to just save \$t_{n-1}\$ and \$t_{n-2}\$ and update them every loop, using tuple assignment:

t1, t2, n = map(int, raw_input().split())
for _ in range(2, n):
   t1, t2 = t2 + t1**2, t1
print t2 + t1**2


Parse a formatted string, which may have duplicate keys, into a map I have a String which is coming in particular format so I want to parse that string into map. I have a below method which parses String to a Map and it works fine.

  public static Map<String, String> parseStringToMap(String payload) {
    Map<String, String> map = new HashMap<>();
    try {
      for (String part : payload.split("\\|")) {
        String[] subparts = part.split("=", 2);
        map.put(subparts[0], subparts[1]);
      }
    } catch (Exception ex) {
    }
    return map;
  }

Sample example of a string:

"type=3|Id=23456|user=13456"
"type=3|Id=23456|user=13456|type=3"

It might be possible that same key can appear many times in the same string payload so I need to overwrite the value for that key in my mutable map.

Also if my string payload is not in this format then I would like to return empty map back. My string format will always be like this and if it is not in this format then I would return empty map back.

"a=b|c=d|e=f"

I am still working with Java 7. What is the best and efficient way to do this?

You can not get it much more efficient that this. It could be more readable and there are some code smells.

Remarks:

The input string "zoo=ooz=10" has a map with 1 entry that has "ooz" as value. Is this what you want? Maybe not limit the split and check that it is 2?
"catch (Exception ex)" is considered bad practice. You are basically saying that you do not care about any Exceptions. Also any possible exceptions in all the methods you call. This is never a good idea.
No argument checking. The cases where the argument is null or the empty string you can leave the method early. This improves readability. Now the massive catch also does the argument checking. Having a dedicated piece of code for every thing is easier to read.
You are transforming one iterable (result of payload.split) into another dynamic sized iterable. Usually it is better to then set the initial capacity for the HashMap to prevent rehashes during insertion which is O(n). But in this case the end size does not have to be equal to the input size. It is a border case.

My proposal:

public static Map<String, String> parseStringToMap(String payload) {
  Map<String, String> map = new HashMap<>();

  if (payload == null || payload.isEmpty()) {
    return map;
  }

  for (String part : payload.split("\\|")) {
      String[] subparts = part.split("=");
      if (subparts.length == 2) {
        map.put(subparts[0], subparts[1]);
      }
  }

  return map;
}


Jumble solver algorithm I need to write code for the given problem:

I'm provided with a word and I need to find all the possible combination of it that matches with a given list of words in a file.

Here's my code. Can I make it much better?  I'm sure it can be done. Please offer suggestions.

dict = {}
file = open("/usr/share/dict/words", "r")

for word in file:                                        #Iterate through every word in the dictionary
        word = word.strip().lower()                        #Strip newlines and send to lowercase
        sorted_word = ''.join(sorted(word))                #Alphabetically sort the word
        if sorted_word in dict:                                #Check if sorted_word is already a key
                if word not in dict[sorted_word]:        #Make sure word is not already in the list under the key sorted_word
                        dict[sorted_word].append(word)        #Add to list under the key sorted_word
        else:                                                #If not in dictionary
                dict[sorted_word] = [word]                #Create new list with one entry
while(True):                                                #Loop until quit is typed
        jumble = raw_input("Enter a jumble to decode or 'quit': ")        #Get input
        jumble = jumble.lower()                                #Send jumble to lower
        if(jumble == "quit"):                                #Quit if quit is typed
                break
        jumble = ''.join(sorted(jumble))                #Sort jumble alphabetical
        if jumble in dict:                                #If sorted jumble exists in dictionary
                results = dict[jumble]                        #Get list of words that match jumble
                for result in results:                        #Loop through list printing results
                        print result,                        #Trailing , designates that there should be a space between entries
                print ""                                #Print newlines
        else:                                                #Jumble not found in dictionary print message
                print "Results for jumble not found"

Hum, first of all, there are way too many comments. People simply won't read them, you should only keep the meaningful ones. For example, you could remove:

if sorted_word in dict: #Check if sorted_word is already a key

Anybody who knows how Python dictionaries work will know what this lign is doing, the code is explicit by itself. Instead of always repeating what your dict is doing, you should just explain once what it does when you declare it; the code will tell the rest. You should only explain once at the beginning how your algorithm work and let your code tell the rest. Honestly, you could remove almost all of your comments. Python is designed to be readable and it is.

Second point, avoid to name a variable dict: it is already the name of a built-in type. It's bad pratice and can be confusing for people who will try to read your code.

Also, you open your file once, but never close it. You should close it once you have filled your dict. You could use the with open('...', 'r') as f: construct to have your file closed automatically at the end of the with block, therefore, you won't even have to remember to close it. Also, file, like dict is also the name of a built-in type in Python, find another name :)


Creating a user profile from an omniauth hash I have this code that creates (or updates) a user from an omniauth hash parameter:

  def self.find_or_create_from_auth_hash(auth_hash)
    uid = auth_hash['uid']
    provider = auth_hash['provider']

    user = find_or_create_by(uid: uid, provider: provider)

    user.name = auth_hash['info']['name'] || ''
    user.email = auth_hash['info']['email']
    user.nickname = auth_hash['info']['nickname']
    user.avatar = auth_hash['info']['image'] || ''

    user.access_token = auth_hash['credentials']['token']

    user.location = auth_hash['extra']['raw_info']['location'] || ''
    user.company = auth_hash['extra']['raw_info']['company'] || ''
    user.member_since = auth_hash['extra']['raw_info']['created_at'] || ''

    user if user.save
  end

It is working, but it looks unpleasant. What can be changed in this piece of code, making it easier to read and/or beautiful?

The "default to empty string" bit should probably be done in the model. I.e. in a before_validation callback or similar.

You can also use Hash#slice to pluck out certain keys:

user = find_or_create_by(auth_hash.slice(%w(uid provider)))

user.assign_attributes(auth_hash['info'].slice(%w(name email nickname))
user.avatar = auth_hash['info']['image']

user.access_token = auth_hash['credentials']['token']

extra = auth_hash['extra']['raw_info']

user.assign_attributes(extra['raw_info'].slice(%w(location company)))
user.member_since = extra['raw_info']['created_at']

Alternatively, you could rewrite the keys (e.g. in a loop or using transform_keys) before using assign_attributes:

translations = { # could be a constant somewhere
  'image' => 'avatar',
  'token' => 'access_token',
  'created_at' => 'member_since'
}

attributes = auth_hash['info'].slice(%w(name email nickname image))
attributes.merge!(auth_hash['extra']['raw_info'].slice(%w(location company created_at)))
attributes['token'] = auth_hash['credentials']['token']

attributes.transform_keys! { |key| translations[key] || key }

user.assign_attributes(attributes)


Subtract multiple columns in PANDAS DataFrame by a series (single column) Background

I have tons of very large pandas DataFrames that need to be normalized with the following operation; log2(data) - mean(log2(data))

Example Data

The example DataFrame my_df looks like this;

     iovrrx    nfinsu    mvdfjc    idjges    fubmrg    lvuhfv
0  0.987654  0.206104  0.802920  0.011157  0.860618  0.575871
1  0.706397  0.860083  0.939230  0.436194  0.557081  0.706964
2  0.043139  0.729435  0.597488  0.700998  0.974193  0.917758
3  0.316080  0.461547  0.844540  0.510143  0.908475  0.877330
4  0.828839  0.177670  0.610833  0.328238  0.327697  0.689756

Question

I have tried to perform the normalization operation noted above many different ways however the following code snippet is the only one that I have gotten to work;

log_div_ave = my_df.apply(np.log2).values.T - my_df.apply(np.log2).mean(axis=1).values

log_div_ave = pd.DataFrame(log_div_ave.T,columns=my_df.columns)

print(log_div_ave)

   iovrrx    nfinsu    mvdfjc    idjges    fubmrg    lvuhfv
0  1.667378 -0.593258  1.368628 -4.800610  1.468744  0.889117
1  0.056992  0.340988  0.467991 -0.638518 -0.285601  0.058149
2 -3.467018  0.612699  0.324830  0.555330  1.030127  0.944032
3 -0.941776 -0.395590  0.476099 -0.251165  0.581380  0.531053
4  0.933714 -1.288174  0.493400 -0.402633 -0.405015  0.668708

As you can see I'm converting the DataFrame to a numpy array and transposing it just so I can subtract by the mean of the data. I then have to transpose the resulting array then reconstitute it as a DataFrame. Is there a simpler way to do all of this?

There's need to transpose. You can subtract along any axis you want on a DataFrame using its subtract method.

First, take the log base 2 of your dataframe,  apply is fine but you can pass a DataFrame to numpy functions.

Store the log base 2 dataframe so you can use its subtract method. You can also reuse this dataframe when you take the mean of each row.

Finally subtract along the index axis for each column of the log2 dataframe, subtract the matching mean.

log2df = np.log2(my_df)
log2mean = log2df.mean(axis='columns')
log_div_ave = log2df.subtract(log2mean, axis='index')


Generic Meyers Singleton implementation in C++ Is this a correct implementation of a generic Meyers Singleton in C++17? Any tips appreciated.
Singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H

template<class T>
class Singleton
{
public:

    static T& get()
    {
        static T obj;
        return obj;
    }

protected:

    Singleton() = default;
    virtual ~Singleton() = default;
    Singleton(const Singleton&) = delete;
    Singleton(Singleton&&) = delete;
    Singleton& operator=(const Singleton&) = delete;
    Singleton& operator=(Singleton&&) = delete;

};

#endif

MySingleton.h
#ifndef MYSINGLETON_H
#define MYSINGLETON_H

class MySingleton : public Singleton<MySingleton>
{
    friend class Singleton<MySingleton>;

public:

    // public members

private:

    MySingleton() {};
    ~MySingleton() {};

};

#endif

Overview
I am not sure this byes you anything in savings.
Its one static function and making sure that you can't copy/move the object.
There are some issues aound using templates to implement this as I have tried exactly the same before. If this is a single application it is find but if you start using shared libraries then you may run into issues where you have several instances of T because there is a unique version of get() in every shared library that has not been removed.
Code Review
Not unique.
#ifndef SINGLETON_H
#define SINGLETON_H

Put your code in a namespace and add the namespace the include guards.

Reading ahead it looks like this only works because you assume T inherits from Singleton and thus makes sure that all the copy/move semantics are disabled.
    static T& get()
    {
        static T obj;
        return obj;
    }

But your get() method does not validate this. So I could call this method with anything and get a reference to a thing that is not a signelton.
    std::string myString = Singleton::get<std::string>();

You can make sure that the class works correctly by using static_assert.
    static T& get()
    {
        static_assert(std::derived_from<Singelton, T> == true);

        static T obj;
        return obj;
    }


Schema for Google Docs-like sharing I want to allow users to share documents (and other stuff) in a database-driven application.
I have designed the following schema to allow for this (PostgreSQL). Some of the tables like Party are skeletal for simplicity.
Diagram (minus lookup tables):

A lookup table for party types:
create table partyType(
  id int not null primary key,
  description text not null
);

insert into partyType values (1, 'Individual');
insert into partyType values (2, 'Organization');

A party is an individual or organization, such as a user or customer:
create table party(
  id serial primary key,
  type int not null,
  name text not null,

  foreign key (type) references partyType (id)
);

A party has many email addresses, and an email address can belong to multiple parties (such as "Barb and Jim Jones"):
create table emailAddress(
  id serial primary key,
  address text not null
);

create table partyEmailAddress(
  partyId int not null,
  emailAddressId int not null,

  primary key (partyId, emailAddressId),
  foreign key (partyId) references party (id),
  foreign key (emailAddressId) references emailAddress (id)
);

An item can be private or explicitly shared, or public but unlisted, or public and listed:
create table visibilityType(
  id int not null primary key,
  description text not null
);

insert into visibilityType values (1, 'Private / Explicit');
insert into visibilityType values (2, 'Public Unlisted');
insert into visibilityType values (3, 'Public Listed');

Someone with whom you share an item can be a viewer, commenter (can view too), or editor:
create table sharingRoleType(
  id int not null primary key,
  description text not null
);

insert into sharingRoleType values (1, 'Viewer');
insert into sharingRoleType values (2, 'Commenter');
insert into sharingRoleType values (3, 'Editor');

An item is the thing that you are sharing (this will be an actual type like Document later):
create table item(
  id serial primary key,
  ownerId int not null,
  visibilityType int not null default 1,
  publicRoleType int not null default 1 comment 'the role, if the item is public',
  onlyOwnerCanChangePermissions boolean not null default true,

  foreign key (ownerId) references party (id),
  foreign key (visibilityType) references visibilityType (id)
  foreign key (publicRoleType) references sharingRoleType (id)
);

Do you have a more succinct name than onlyOwnerCanChangePermissions? In gdocs, either only the owner can change permissions, or you can allow other editors to change permissions and add other users.
The below allows an item to be shared with many, and for a party (via their email) to have many items shared with it.
create table itemShare(
  itemId int not null,
  emailAddressId int not null,
  roleType int not null default 1,

  primary key (itemId, emailAddressId),
  foreign key (itemId) references item (id),
  foreign key (emailAddressId) references emailAddress (id),
  foreign key (roleType) references sharingRoleType (id)
);

Should item.ownerId be removed from item and added as a role type, and be added as an "automatic" itemShare?  It would be easier to query for all docs that a user has access to this way.

Is item.publicRoleType correct, seeing as it's only used if the item is public?  It seems somewhat denormalized.  I could move visibilityType to itemShare, get rid of publicRoleType, and make email addy nullable on itemShare.

Alternative schema - improved?

(source: snag.gy)

I would definitely rename onlyOwnerCanChangePermissions, maybe to permissive or restrictPermissions or something like that.

I would also definitely remove item.ownerId, create a new RoleType owner and propagate an itemShare entry. I also think that itemShare is a rather bad name and should be renamed to something like permission or accessList.

Get rid of item.publicRoleType and use itemShare instead. This could also simplify your code e.g. you only have to check itemShare for the access. But i would use an AnonymousUser instead of making the emailAddresse nullable.

I think a better name for sharingRoletype would be just roleType as a role is not necessarily related to sharing.

item.visibilityType doesn't really fit into itemShare as itemShare defines permissions (what and who could do what with the document) for users. But you could create a SystemUser and merge visibilityType and sharingRoleType but this isn't a good fit either.


Simple form validation I would really like to hear how I can improve my form validator. Next, I want to refactor it into OO and learn a testing framework like Jasmine.

Some things I know I could improve:

Don't make it tied to the DOM as much (using parent().parent(), for ex.).
Combined validations can't be used. For example, using isBlank and isInteger will not validate properly because if isInteger fails, it throws an error and isBlank passes and clears all errors.
Make a config object to update with custom error messages.
Possibly make a jQuery plugin so passing the object to validate is easier.

Github repo

  jQuery -> # on Document ready

    errorCount = null

    valEl = $('.validate')  # cache .validate elements
    submitBtn = valEl.parent().parent().find('input[type=submit]')

    valEl.on "blur", ->
      input = $(this)
      validate(input)

    validate = (input) ->
      inputVal = input.val()
      inputLen = inputVal.length
      validations = input.data()         #cache data-xxx tags

      if validations.notblank is on
        if inputLen <= 0
          throwError("This field can't be blank", input)
        else
          removeError(input)

      if validations.minchars > 0
        if inputLen < validations.minchars
          throwError("You need at least 5 chars", input)
        else
          removeError(input)

      if validations.isinteger is on
        isInteger = /^\d+$/
        if isInteger.test(inputVal) is false
          throwError("Must be a whole number", input)
        else
          removeError(input)

    throwError = (message, input) ->
      if !input.hasClass('has-error')
        errorCount += 1
        input.addClass "has-error"
        input.nextAll('.errorContainer').append(message)

    removeError = (input) ->
      if input.hasClass('has-error')
        errorCount -= 1
        input.removeClass("has-error")
        input.nextAll('.errorContainer').empty()

    submitBtn.on "click", (e) ->
      e.preventDefault()
      if errorCount is 0
        valEl.parent().parent().submit()

You could break out your validation functions into a separate object, like so:

validators =
  isinteger: (input) ->
    # check that value is an int

  notblank: (input) ->
    # check that value is present

These functions could return a simple boolean, or they could return an error message if validation fails and null if it passes.

The point is that you keep the functions separate, so they're easier to manage and customize. But it also means that you can give your inputs data attributes like data-validator which simply is the name of the validation method to use. So you can say, for example

method = $(input).data "validator"
result = validators[method]?(input)

With a little string-splitting, you could make the field plural, like data-validators="isinteger, notblank", and check each.

You could also make the error-throwing into an event you can listen for elsewhere. Then you avoid having your error presentation mixed up with your validations, and you don't have to rely on a predefined .errorContainer element. For instance:

throwError = (message, input) ->
  $(input).trigger 'validation:error', message

# ... elsewhere ...

$(document).on 'validation:error', 'input', (event) ->
  # handle error event

Other stuff I noticed:

Don't bind to the submit button's click event! Forms can also be submitted by simply pressing enter/return, without ever clicking the submit button. Instead, bind the form element's submit event which is fired regardless of how the form is submitted, and use preventDefault() to stop the submission if necessary.
Your notblank logic won't complain if the user just enters some space characters. In most cases the field should probably be considered blank, if it only contains whitespace. So use $.trim() on the value first, to trim off leading and trailing whitespace.
HTML5 has a required attribute you can add to inputs - no need to have your own syntax for say an input should be notblank. Check for the required attribute instead (if $(input).attr('required') ...). It makes the HTML more semantically correct.

I don't know if you already have solutions for the specific things you mention, but here are my ideas:

You can use .closest("form") to find the form element instead of parent().parent()
If you store all validation failures in an array, you just need to check the array's length, to see if the were any errors. Multiple validations won't override each other, and you don't need errorCount


Use generic to prevent duplicate code in this case I have this class hierarchy. I want to apply generics to it but don't know how to do.
AddWikiTask, ScanTask and RescanTask are subclasses of AbstractWorkerTask.
DictionaryScanner has 3 functions to check if all tasks finish. How to prevent duplicate code in 3 those functions?

This is class hierarchy

    public abstract class AbstractWorkerTask<Params, Progress, Result>
    extends AsyncTask<Params, Progress, Result>
    implements Workable {
    ...}

    class AddWikiTask extends AbstractWorkerTask<String, Void, Pair<Integer, Dictionary>> {
    ...}

    class ScanTask extends AbstractWorkerTask<DictionaryBean, Void, Pair<Integer, Dictionary>> {
    ...}

    class RescanTask extends AbstractWorkerTask<DictionaryInformation, Void, Pair<Integer, Dictionary>> {
    ...}

This is DictionaryScanner.

public final class DictionaryScanner {
    private final List<RescanTask> rescanTasks = new ArrayList<RescanTask>();
    private final List<ScanTask> scanTasks = new ArrayList<ScanTask>();
    private final List<AddWikiTask> wikiTasks = new ArrayList<AddWikiTask>();

        public boolean didAllRescanTasksFinish() {
            for (final RescanTask task : rescanTasks) {
                if (task.isWorking()) {
                    return false;
                }
            }
            return true;
        }

        public boolean didAllAddWikiTasksFinish() {
            for (final AddWikiTask task : wikiTasks) {
                if (task.isWorking()) {
                    return false;
                }
            }
            return true;
        }

        public boolean didAllScanTasksFinish() {
            for (final ScanTask task : scanTasks) {
                if (task.isWorking()) {
                    return false;
                }
            }
            return true;
        }
}

Edit: And apply generic for this. It's too long. I really don't know how to refactor it.

private void setOnPreExecuteForAddWikiTask(final AddWikiTask task, final ProgressDialog progressDialog) {
    task.setOnPreExecuteListener(new OnPreExecuteListener() {
        @Override
        public void onPreExecute() {
            if (didAllAddWikiTasksFinish()) {
                progressDialog.show();
            }
        }
    });
}

private void setOnPreExecuteForRescanTask(final RescanTask task, final ProgressDialog progressDialog) {
    task.setOnPreExecuteListener(new OnPreExecuteListener() {
        @Override
        public void onPreExecute() {
            if (didAllRescanTasksFinish()) {
                progressDialog.show();
            }
        }
    });
}

private void setOnPreExecuteForScanTask(final ScanTask task) {
    task.setOnPreExecuteListener(new OnPreExecuteListener() {
        @Override
        public void onPreExecute() {
            if (didAllScanTasksFinish() && progressBar != null) {
                progressBar.setVisibility(ProgressBar.VISIBLE);
            }
        }
    });
}

If the snippet is not elaborate enough, please let me know.

You can take the common code in the functions in DictionaryScanner out into a common method:

    public boolean didAllRescanTasksFinish() {
        didAllXTasksFinish(rescanTasks);
    }

    private boolean didAllXTasksFinish(List<AbstractWorkerTask> tasks) {
        for (final AbstractWorkerTask task : tasks) {
            if (task.isWorking()) {
                return false;
            }
        }
        return true;
    }

Otherwise your second block of code is about as simple as you can get, because each listener has different content. If you try to remove the boilerplate you will find it gets much more difficult to understand.


API method to validate Facebook OAuth token I am new to Java / OOP and I'm concerned that I have a method which is doing far too much "stuff" - but I don't easily see how it can be shortened in a way which is not contrived / arbitrary.

This is a server-side API method built using Google App Engine.

Currently, the method (which is really just a template at this stage):

Validates a user's Facebook OAuth token against Facebook's own
servers - this is all done behind the scenes in a FacebookHelper
class I have written.
Catches any exceptions (most likely an
    IOException) thrown during this validation process.
Asks the user to
    log in again if their token is invalid (if (authToken == null)).
Else if authentication has been successful, tries to get the user
    record from my database.
If it doesn't exist, adds the user to the
    database. (registerUserInDatabase).
Responds with a personalised
    welcome to the user - after having registered the user if they were
    not already registered.

Code:

@ApiMethod(name = "getUserData", path = "get_user_data")
public Bean getUserData(@Named("token") String token) {

    Bean response = new Bean();
    FacebookAuthToken authToken;
    User user;
    String userPersonalisedWelcome;

    try {
        authToken = ServerFacebookHelper.getAuthToken(token);
    } catch (Exception e) {
        response.setData("Exception occurred");
        return response;
    }

    if (authToken == null) {
        response.setData("Token invalid, please log in");
        return response;
    } else {
        user = getUserFromDatabase(authToken);
        if (user == null) {
            user = registerUserInDatabase(authToken);
            userPersonalisedWelcome = user.getPersonalisedWelcome;
            response.setData(userPersonalisedWelcome);
        } else {
            response.setData(user.getPersonalisedWelcome);
        }
        return response;
    }
}

Obviously there is already a huge amount of work being done outside this method, such as API calls and database reads/writes, but it still feels far too procedural and frankly "dumb". At the same time, everything it encapsulates is required in order to service this "getUserData" API request - so perhaps I am overthinking here.

You have a lot of null checks (well, two at least), which I think leads to less readable code. Also, it shouldn't really be necessary to have them.

if (authToken == null) this isn't really something this method should concern itself with. In my opinion, getUserFromDatabase should handle that case (via IllegalArgumentException, which is a RuntimeException). Although it's a case that really shouldn't happen in the first place. Why would getAuthToken return null instead of throwing a not found exception?

You should also always try to reduce the level of nesting you have, to increase readability. If you have

if (something) {
    doSomething();
    return;
} else {
    doALotMore();
}

write it as:

// we handle all the exceptional stuff here, and return
if (somethingWentWrong) {
    handleIt();
    return;
}

// the expected code goes here
toDefaultStuff();

With those two points, your code would look something like this:

@ApiMethod(name = "getUserData", path = "get_user_data")
public Bean getUserData(@Named("token") String token) {

    [...]

    try {
        authToken = ServerFacebookHelper.getAuthToken(token);
    } catch (NoConnectionException e) {
        response.setData("Exception occurred");
        return response;
    } catch (InvalidTokenException ite) {
        response.setData("Token invalid, please log in");
        return response;
    } catch (...) {
        ...
    }

    user = getUserFromDatabase(authToken);
    if (user == null) {
        user = registerUserInDatabase(authToken);
        // removed setData duplication
    }
    response.setData(user.getPersonalisedWelcome);
    return response;
}

if (user == null) also doesn't seem like a good way of handling it. Why is user null? Currently, we don't know, it could be a number of reasons (user not found, problems with the database conncetion, incorrect query, etc). This is why returning null for functionality is generally not a great idea, because the caller will have to guess quite a bit.

One solution would be to add a isUserInDatabase(authToken), but that would add additional db queries.

You could also throw a UserNotFound exception, but it doesn't seem like an exceptional situation, so that is also not ideal (at least in this situation; generally, not finding a user would be exceptional for a getUserFromDatabase method).

Another option would be to add an exists method to your user class, but again, that doesn't seem all that clean either.

The question is what the difference between registering and getting really is (and what data is set in user; for registering, this seems to only be the token, which means that you get an incomplete user object returned, which also doesn't seem ideal), and why you don't handle it in completely different methods (ie why getUserData does something - registering a user - if the user doesn't exist already (instead of having another method for that)).


Display students of specific age using struct My task for uni is to create a program using structures. In this program I have to make an entry of students, their name, birth date and group (a random number). And I have to search the students according to the input age.

Please note that I am still learning :)

#include <iostream>
#include <cstring>

using namespace std;

struct student
{
char name[50];
int year;
int group;

};

int main( )
{
int N;
int age;

cout<<"Enter how many entries:";
cin>>N;
struct student student1[N];

for (int i=0;i<N;i++)
{
cout<<"enter the student's name:";
cin>>student1[i].name;
cout<<"enter the birth year:";
cin>>student1[i].year;
cout<<"enter the group:";
cin>>student1[i].group;
}
cout<<"enter the student's age:";
cin>>age;

if (age==2016-student1[N].year)
{
    cout<<"The student with the age "<<age<<" is:";
    cout<<student1[N].name;
    cout<<student1[N].group;
}
else
{
    cout<<"the student wasn't found.";
}
return 0;
}

#include <cstring>

You're not using any of the features in that header. Replace is with <string> and do use those. Also indent your code, it makes it more readable.

struct student
{
  std::string name;
  int year;
  int group;
};

Don't get into the habit of using this:

using namespace std;

See Why is “using namespace std;” considered bad practice?

int N;
int age;

You only need N a few lines down, and age only way below inside the loop. Declare them as close to their first use as possible, in the smallest scope possible.

cin>>N;
struct student student1[N];

A few issues:

You're not checking whether that read of an int actually worked (same for all the other inputs).
You don't need the struct keyword there. You need it in C where structs live in a separate namespace, but that's not the case in C++.
This is not standard C++: this uses a feature called variable-length arrays which are standard C but not part of C++ (but some compilers have support for it anyway). Instead, use a std::vector (don't forget to #include <vector> also).

return 0;

This isn't necessary, if main doesn't explicitly return anything, the standard guarantees that it will act as if it had returned 0. (This is only valid for the main function, all other non-void functions must return something on all their returning branches.)


How can I speed up my RSS feed Android App? I have a problem with parsing RSS from a PHP page because the app is too slow.

This is my parsing code:

public void getdataparse(String url) {

    Log.d("Do in Background","Start Call Void");

    String dbtitle = "";
    String dbDesc = "";
    String dbimgicon = "";
    String dbimg = "";
    String dblink = "";
    String dbpubdate = "";
    String dborderStg = "";
    String dbsourcename = "";
    String dbimagecheck = "";
    int dbfff = 0;

        try {

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new URL(url).openStream());

            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("item");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                dbtitle = "";
                dbDesc = "";
                dbimgicon = "";
                dbimg = "";
                dblink = "";
                dbpubdate = "";
                dborderStg = "";
                dbsourcename = "";
                dbimagecheck = "";
                dbfff = 0;
                Datemx = 0;
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    dbtitle = getTagValue("title", eElement);

                    dbDesc = getTagValue("description", eElement);

                    String[] timeformated = new String[2];

                    timeformated = gettime(getTagValue("pubdate", eElement));

                    dbpubdate = timeformated[0];

                    dborderStg = timeformated[1];

                    Log.d("pubDate = ", "" + dbpubdate + "OrderStg = " + dborderStg);

                    dblink = getTagValue("link", eElement);

                    dbimgicon = getTagValue("sicon", eElement);

                    dbimg = "http://s14.postimg.org/cs10sq11d/whitemega.png";

                    dbsourcename = getTagValue("sname", eElement);

                    dbimagecheck = "";

                    dbfff = Datemx;

                    handler = new DataHandler(getBaseContext());
                    handler.open();
                    long id = handler.insertrssfeedData(dbtitle, dbDesc, dbimg, dbimgicon, dblink, dbpubdate, dbfff, dbsourcename , dbimagecheck);
                    handler.close();

                    Log.d("Add New Recorde","Record = " + temp + "/n Title = " + dbtitle);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

The PHP page URL

The PHP code

none of those Variables need to be set to "" inside the for loop, they have already been set.

This can be set early and outside of the if block and the for loop:

 dbimg = "http://s14.postimg.org/cs10sq11d/whitemega.png";

Otherwise it is a Magic string that you will have to search for later.

The id isn't doing anything here, I don't think that you need it at all, you should be able to call the code like this

handler.insertrssfeedData(dbtitle, dbDesc, dbimg, dbimgicon, dblink, dbpubdate, dbfff, dbsourcename , dbimagecheck);

otherwise you are creating this variable and assigning a lot of data to it and never using it every time you go through the loop.


HTML template for win-loss data in Javascript with Mustache Can anyone suggest a better way to implement my template code?

var $teams = $('#info');

var template = "{{#data}}
{{teamName}} 	{{playedGames}}	{{wins}}	{{draws}}	{{losses}}	{{goalDifference}}	{{points}}
{{/data}}";

$.ajax({
  headers: {
    'X-Auth-Token': '0fc841d392274cb5a26804330ac11e98'
  },
  type: 'GET',
  dataType: 'json',
  url: 'http://api.football-data.org/v1/soccerseasons/401/leagueTable',
  success: function(standings) {
    $teams.append(Mustache.render(template, {data:standings.standing}));
  }
});

This template is pulling in information from an API to display the latest league statistics.

I think it would be much cleaner to put your template in your markup instead of having it as that JavaScript string literal. You can do this by wrapping your template in a script block:

Next, we can fetch our template HTML from the DOM:

var template = $('#RowTemplate').html();

The Mustache documentation says that we can pre-parse our template:

Mustache.parse(template);

Finally, you will have noticed that I did not include the {{#data}} tags in my template. It seemed to me that name-spacing my data in this way was unnecessary. So my call to render looks as follows:

$teams.append(Mustache.render(template, standings.standing));

However, it looks odd to me that the data for each table row is getting individually loaded via AJAX. If it is possible to load all of the data with one AJAX call, then I would do so. My template would then look something like the following:

This assumes the server response contains a standings array of objects, where each object would correspond to a row in our table:

{
    standings: [
        /*...*/
    ]
}


PHP form with bot deterrent I saw somewhere on here that it helps reduce spam by adding a dummy input field that you hide with display: none, and like if it's filled out, then it's obviously a bot sending the message. Well, I did something kind of like that but initially set it to = 0. So if it's not 0 then it doesn't send the form, since a bot would have changed the value to something else. I don't know if that will really work effectively or not. If you have a better way to do something like that, please share.
I don't know if my token validation stuff is anywhere close to how it should be. Please let me know if what I'm doing with that is even adding any security or not.
After the user submits the form, I put some echo's in so that what they entered is still shown in the form fields, in case there was an error so they don't lose what they typed. Is my use of htmlspecialcharacters needed there?

Please share any other comments, recommendations, improvements, etc, etc that you see with my code, because I really want to get better at this and your answers almost always help me understand things better. You know, the manual can be kind of confusing at times when your new so having a master coder review your code and explain things helps so much for me.

<?php

session_start();

function getIp() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else {
         $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

if($_POST['yourlastname'] === '0') {
    if ($_SESSION['token1'] == $_POST['token']) {
    echo "
checker: " . $_SESSION['token1'] . "  and the post token " . $_POST['token'] ."
";
        if(filter_var($_POST['youremail'], FILTER_VALIDATE_EMAIL)) {
            $name = htmlspecialchars($_POST['yourname']);
            $email = htmlspecialchars($_POST['youremail']);
            $from = $name . ' - ' . $email;
            $ip = getIp();
            $message = htmlspecialchars($_POST['yourmessage']) . "\r\n" .
                          'Name : ' . $name . "\r\n" .
                          'Email : ' . $email . "\r\n" .
                          'User IP: ' . htmlspecialchars($ip);
            $message = wordwrap($message, 60, "\r\n");
            $headers = 'From: ' . $from . "\r\n" .
                          'Reply-To: ' . $email;
            mail('email@example.com', 'Static Subject', $message, $headers);
        }
    }
}

$token1 = md5(microtime(true));
$_SESSION['token1'] = $token1;

?>

<!DOCTYPE html>

<meta charset="utf-8">
<title>Tittle</title>

<?php $token2 = md5(microtime(true)); ?>
<form method="post" name="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

<input name="token" type="hidden" value="<?php echo htmlspecialchars($token2); ?>">

Your Name:
<input class="required" id="yourname" name="yourname" type="text" value="<?php if(isset($_POST['yourname'])) { echo htmlspecialchars($_POST['yourname']); } ?>">

Your Last Name:
<input class="required" id="yourlastname" name="yourlastname" type="text" style="display: none;" value="0">

Your Email:
<input class="required" id="youremail" name="youremail" type="text" value="<?php if(isset($_POST['youremail'])) { echo htmlspecialchars($_POST['youremail']); } ?>">

Your Message:
<textarea class="required" id="yourmessage" name="yourmessage" cols="30" rows="8"><?php if(isset($_POST['yourmessage'])) { echo htmlspecialchars($_POST['yourmessage']); } ?></textarea>

<input type="submit" id="send" value="Send">

</form>

Right, I'll be adding to this answer later on, but for a kick-off, here's a few quick remarks/considerations:

On the hidden input fields
As Martijn said in his comment: most bots will probably parse the DOM, and filter out those fields that are marked as required (looking for labels with a * text node in them), and they'll simply ignore hidden fields. You've created regular input fields that are not displayed. That might work on some bots, but like I said, they'll probably parse the DOM and see that they have a style attribute that sets the display to none.
It's not hard to deal with that, and if I were to write a bot, it wouldn't take me 5 minutes to add the code to deal with those input fields, too.

You can keep those hidden input fields as a means of security (they don't do any harm, after all), but don't set their style attributes in the markup, use CSS classes, or better still: use JavaScript to set class/attributes on the load event. That way, the DOM doesn't reflect what the page will actually look like as much as it does now.
This may help a little, but all in all, this will only protect you from script-kiddies and amateurish attacks.

The token stuff
That's fine. Session tokens that should be posted back are basic security steps that everybody should take. How you get those tokens is up to you, but an md5 of the timestamp? A hash doesn't make your token more secure. Don't go thinking that md5 does anything else than add overhead, however minor it may be.
Of course I get that a hash string just makes the token look more important, and in a way less random than it actually is, but why not hash some of the clients data in that case, like the remote address or referrer or whatever... though far from reliable, it can give you something extra to check, and log, and compare to the access logs of your server. It may also give you a clue as to what tools are being used when your page is targeted.

However, you should add checks along the lines of if (isset($_POST['yourlastname'])) and isset($_SESSION['token1']), to ensure that you can actually validate the form submission. But that's as an asside.

htmlspecialchars
Is it needed? NO. In fact, they're capable of doing more harm than good in some cases. Particularly when they're applied to the email address, as you seem to be doing. You're using filter_var($email, FILTER_VALIDATE_EMAIL), which is great, because that filter can handle the complex and wacky email addresses can contain htmlspecial chars, including: <. Here's what I'd do:

$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);//sanitize
if (filter_var($email, FILTER_VALIDATE_EMAIL))
    //continue

As far as the sanitation of the other data that'll make-up your email message is concerned: set the MIME type to text,because it would appear that that's all you need, and take measures against email injection.
A subject I've already discussed in my answer here

Code issues
Just a quick update of my answer. On second glance, I may have spotted an issue with your code that could sometimes result in your script treating a valid form submission as an invalid one:

$token1 = md5(microtime(true));
$_SESSION['token1'] = $token1;//set token, then:
?>
<!DOCTYPE html>
<html>
<!-- setting HTML... -->
<?php $token2 = md5(microtime(true)); ?>
<form method="post" name="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

<input name="token" type="hidden" value="<?php echo htmlspecialchars($token2); ?>">

The token value set in your form is a different variable than the one you've assigned to $_SESSION['token']! Why? The chances that the values of $token1 and $token2 are different are really quite high.
Why aren't you simply using $token1 in the form, too?

Next: htmlspecialchars? What's the point? $token1 and $token2 both are hash strings, which are all [a-z0-9] chars, to put it in regex terms. They don't need escaping, so this function call (which adds overhead) can, and should be dropped.

Also note that you don't have to specify the action attribute for the form, if the target is $_SERVER['PHP_SELF']. Leaving it blank triggers a form submissions default behaviour, which is to submit to self

<form method="post" name="form"> <!-- will do the trick -->

As an aside, also check my addition to the recommendations bit below on how you can make the mix-ins of your php in the markup, IMO at least, a bit more easy to read.

Recommendations
Given the fact you are looking into ways to ensure bots/spammers can't abuse your form, why not use a simple Captcha? It's proven technology, and rather effective anyway...

There are various scripts readily available, phpcaptcha is a free script you can download. It's easy, fairly well documented, actively maintained, there's a lot of plugins readily available for all major CMS/blog type of things and it's already been extensively tested.
I've just looked into the source of phpcaptcha now. I must say, there's a couple of things I don't much care for in there, but on the whole, it's not bad. I've even gone through the trouble of forking the repo, and I've started writing a little extra feature (which I'll call "mangler"). It'll transform words like transform into tr@nsf0rm, in such a way that both transform and tr@nsf0rm are accepted.
Though still work in progress, feel free to contribute ;-P

When echo-ing PHP variables inside HTML, you may find your code easier to read when using the short echo tag. This is not the same as the short open tag (<?) in that it does not require specific ini settings. In other words, it's always going to be available.
If nothing else, it at least requires less typing/code-chars

<input name="token" type="hidden" value="<?php echo htmlspecialchars($token2); ?>">
<!-- can be written as (I'm applying my critiques, too) -->
<input name="token" type="hidden" value="<?= $token1; ?>">

Not even the ; at the end of the line is required, but I prefer to have it there... it's a good habit.


Predicting random numbers based on past output I've been working on a program to predict random numbers based on previous digits. However, I only get access to numbers from 0-53 inclusive, and one only comes every 30 seconds or so, therefore gathering hundreds or thousands of sequential data points is nigh impossible. I also don't know the algorithm being used, although right now I am assuming it is the Mersenne Twister.

With all that being said, here is the code that I am using:

import random
from multiprocessing import Process, Pool
import time
start = time.time()

data = [5, 49, 9, 2, 8]
datalength = len(data)

def actual():
    for i in range(9999999999):
        l1 = []
        random.seed(i)
        append = l1.append

        for i2 in range(datalength):
            x = random.randrange(0, 54)

            try:
                if data[x] == i2:
                    append(x)
            except IndexError:
                break

def next_100(x):
    l1 = []
    random.seed(x)
    for i in range(datalength):
        x = random.randrange(0, 54)
    for i in range(100):
        l1.append(random.randrange(0, 54))
    return l1

with Pool(processes=3) as pool:
    result = pool.apply_async(actual).get()

z = next_100(result)
print('The next 100 should be: {0}'.format(z))
print('It took {0} seconds'.format(round(time.time()-start)))

I have 2 questions:

Is there a better algorithm than I am currently using? (that works with my limited knowledge)
If not, how else can I optimize this? Right now, I can't really guess more than 100 million seeds in a reasonable amount of time. Seed 100 million takes ~23 minutes, and 1 billion should take about 230 minutes, which is way more time than I have.

You need to find or create a Mersenne Twister rainbow table. In other words, pre-compute all (or some subset) possible outputs, store them (time-space tradeoff), and compare the series you receive to the stored series.

The article quoted in https://stackoverflow.com/questions/6618836/how-if-at-all-does-a-predictable-random-number-generator-get-more-secure-after suggests the number of values you need to store may be much smaller.


Transliterating Tatar To Cyrillic Input :

{translate} Dear Atteck {/translate}

{translate}Please complete registration
 {/translate}

Output :

 Дeaр Аттeҗк
Плeaсe җомплeтe</һ5>

Main code:

private static readonly Dictionary<string, IOrderedEnumerable<KeyValuePair<string, string>>> TransliterationMaps = new Dictionary<string, IOrderedEnumerable<KeyValuePair<string, string>>>();

static void Main(string[] args)
{
    Transliterate();

    const string source = "
{translate} Dear Atteck {/translate}

{translate}Please complete
 {/translate} ";

    var splitData = Regex.Split(source, "{/");
    string alldata = string.Empty;

    foreach (var splt in splitData)
    {
        var data = splt.Split(new[] { "{translate}" }, StringSplitOptions.None);

        if (data.Count() > 1)
        {
            data[0] = Regex.Replace(data[0], "[translate}]", string.Empty);
            var result = TransliterationMaps["tatartocyrillic"].Aggregate(data[1], (current, map) => current.Replace(map.Key, map.Value));
            alldata += data[0] + result;
        }
    }
    Console.WriteLine(alldata);
}

Transliterate() method

public static void Transliterate()
{
    var tatarToCyrillicMap = new Dictionary<string, string>();
    tatarToCyrillicMap.Add(('\u0079' + "" + '\u0075').ToString(CultureInfo.InvariantCulture), '\u044e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0059' + "" + '\u0075').ToString(CultureInfo.InvariantCulture), '\u042e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("YU", '\u042e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("yü", '\u044e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("Yü", '\u042e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("YÜ", '\u042e'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0079' + "" + '\u0061').ToString(CultureInfo.InvariantCulture), '\u044f'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("YA", '\u042f'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0059' + "" + '\u0061').ToString(CultureInfo.InvariantCulture), '\u042f'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add("YO", '\u0451'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0079' + "" + '\u006f').ToString(CultureInfo.InvariantCulture), '\u0451'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0059' + "" + '\u006f').ToString(CultureInfo.InvariantCulture), '\u0401'.ToString(CultureInfo.InvariantCulture));
    tatarToCyrillicMap.Add(('\u0079' + "" + '\u00e4').ToString(CultureInfo.InvariantCulture), '\u044f'.ToString(CultureInfo.InvariantCulture));
    .......................
    ...................
    ...............
    .........

    TransliterationMaps.Add("tatartocyrillic", tatarToCyrillicMap.OrderByDescending(m => m.Key));
}

Can this code be optimized?

Consider moving what Transliterate() does to an external dictionary in a file that can be parsed by your program at runtime. You don't want to recompile your program every time you want to add something or change something in that map.


Formatting lists in tabular format # Comment: You can iterate through any number of Enumerables in lockstep

e = {
  numbers:   1..10,
  letters:   %w(a b c d e f g h i j),
  notes:     %w(do re mi fa so la ti do),
  phonetic:  %w(alpha bravo charlie delta echo),
  rainbow:   %w(red orange yellow green blue indigo violet)
}

# Titles
puts e.keys.map(&:capitalize).join(" ")

# Values
e.first[1].zip(*e.values[1..-1]) do |layer|
  layer.zip(e.keys) do |node|
    print node[0].to_s.rjust(node[1].length), " "
  end
  puts
end

This code produces the following output:

Numbers Letters Notes Phonetic Rainbow
      1       a    do    alpha     red
      2       b    re    bravo  orange
      3       c    mi  charlie  yellow
      4       d    fa    delta   green
      5       e    so     echo    blue
      6       f    la           indigo
      7       g    ti           violet
      8       h    do
      9       i
     10       j

I want to tighten up that double loop at the bottom. Can I get rid of the puts that I am using to have each table row on its own line?

You code looks pretty good to me. Some notes:

do |node| and then node[0], node[1]: You should unpack in the block: dp |val1, val2|
Instead of mixing logic with I/O, first build the table string, then print it.

I'd write:

data = {
  numbers:   1..10,
  letters:   %w(a b c d e f g h i j),
  notes:     %w(do re mi fa so la ti do),
  phonetic:  %w(alpha bravo charlie delta echo),
  rainbow:   %w(red orange yellow green blue indigo violet)
}

header = data.keys.map { |k| k.to_s.capitalize }
table = data.values.first.zip(*data.values.drop(1)).map do |row|
  row.zip(header).map do |value, header_name|
    value.to_s.rjust(header_name.size)
  end.join(" ")
end

puts(([header.join(" ")] + table).join("\n"))