texts:
My website is poorly coded.

The structure is pretty simple:

A RewriteRule redirects /.... to /index.php?page=$1
mypages/page.php contains the page content and actions (model controller and view glued together...)
index.php wrap the demanded page into a theme (basically include header and footer around it)

The site is a complex MMORPG site (~40 differents pages). I need to convert each page to a view in my iPhone/Android app (probably made in JS+HTML with PhoneGap). So I guess I'll have to create an API and separate the model/controller/view on each page.

I don't know how to manage the connection, but I'm sure I'm doing it in a bad way (see code below).

My controller looks like this:

<?php
// Database connection, get $_GET("page") ...
if (file_exists($page)) {
    if(isMemberRestricted($page)) {
    $loadUserInf = TRUE;
        if(!isValid($_SESSION['usern'], $_SESSION['passw'], $db)) {
            if(!isValid($_COOKIE['cookie_password'] , $_COOKIE['cookie_password'], $db)) {
                if(!isValid($_POST['usern'], $_POST['passw'], $db)) {
                       if(isValid($_POST['usern'], $_POST['passw'], $dbt)) {
                          $page = CONTROLLER_PATH . "/desarchivage.php";
                       }
                       else {
                         $page = CONTROLLER_PATH . "/notConnected.php";
                       }
                       $loadUserInf = FALSE;
                 }
            }
        }
        else { // not yet connected
            if($loadUserInf) // but connexion accepted
                connexion ();
        }
    }
    if($loadUserInf) {
        require ('userInf.php'); // Loads informations about the connected user
        pageRoutine(); // Stuff to do on each member restricted page
    }
} else {
   $page = CONTROLLER_PATH . "/404.php";
}

require ($header);
require ($page);
require ($footer);
?>

I really don't know where to start. I've read about MVC/API.  I understood the theory, but I don't know where to begin with an existing project.


texts: Avoiding redundant code in MVC view page

texts:
Based on the true condition, I am making some operations.  But I want to simplify the below HTML code in a better way.

@{
    bool savingHtml = (Request.QueryString["savehtml"] == "1");
    string activeTab = Request.QueryString["from"];
}

@if (savingHtml)
{
    if (activeTab.ToLower() == "index")
    {

Heat Map

Table

        @Html.Partial("~/Views/Shared.MultiQuery/_resultset.cshtml", Model)

    }
    else{

Heat Map

Table

        @Html.Partial("~/Views/Shared.MultiQuery/_resultset.cshtml", Model)

    }
}
else
{

Heat Map

Table

        @Html.Partial("~/Views/Shared.MultiQuery/_resultset.cshtml", Model)

}


texts: Simple object pool template container in C++

texts:
The main advantage of this pattern is when I often create and destroy objects, which in this case are often used.

I made this because I needed to track several short timers at the same time, but it can also  be used for stuff like missiles or else.

#include <vector>
#include <queue>

#define itv(TYPE) std::vector<TYPE>::iterator

using namespace std;
template <class TYPE>
struct object_pool
{
    std::vector<TYPE> pool;
    std::queue<size_t> avail;
    TYPE & operator[](const size_t & i) { return pool[i]; }
    void add(size_t & pos)
    {
        if(avail.empty()) // no reusable object
        { pool.push_back(TYPE()); pos = pool.size()-1; }
        else
        { pos = avail.back(); avail.pop(); }
    }
    void rem(size_t & a) { avail.push(a); }
    size_t size() { return pool.size(); }
};

What do you think ?


texts:
I'm trying to make a program that will create an account on Reddit using an HttpWebRequest. I ONLY want it to do that. (I don't care about it storing a cookie or anything else.) I just want it to be a simple program where you type a username into Username_TextBox.Text and a password into Password_TextBox.Text and then click the CreateAccount_Button and voilà, new account created!

I followed a tutorial on YouTube that poorly explained how everything about a HttpWebRequest worked.

Currently, the program will create a Reddit account as long as the username hasn't been taken already. I just don't want any excess code that is useless. I want any extra code removed that isn't contributing to my goal mentioned above.

using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System;

namespace RedditAccountCreate
{
    public partial class RedditAccountCreate : Form
    {
        public RedditAccountCreate()
        {
            InitializeComponent();
        }

        CookieContainer CreateAccountCookie;

        private void CreateAccount_Button_Click(object sender, EventArgs e)
        {
            string POSTData = "op=reg&dest=https%3A%2F%2Fwww.reddit.com%2F&user=" + Username_TextBox.Text + "&passwd=" + Password_TextBox.Text + "&passwd2=" + Password_TextBox.Text + "&email=&api_type=json";
            CookieContainer TemporaryCookie = new CookieContainer();
            UTF8Encoding Encoding = new UTF8Encoding();
            byte[] ByteData = Encoding.GetBytes(POSTData);

            HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create("https://www.reddit.com/api/register/" + Username_TextBox.Text);
            POSTRequest.Method = "POST";
            POSTRequest.KeepAlive = true;
            POSTRequest.CookieContainer = TemporaryCookie;
            POSTRequest.ContentType = "application/x-www-form-urlencoded";
            POSTRequest.Referer = "http://www.reddit.com/";
            POSTRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
            POSTRequest.ContentLength = ByteData.Length;

            Stream POSTRequestStream = POSTRequest.GetRequestStream();
            POSTRequestStream.Write(ByteData, 0, ByteData.Length);
            POSTRequestStream.Close();
            HttpWebResponse POSTResponse = default(HttpWebResponse);

            POSTResponse = (HttpWebResponse)POSTRequest.GetResponse();
            TemporaryCookie.Add(POSTResponse.Cookies);
            CreateAccountCookie = TemporaryCookie;
            StreamReader POSTRequestReader = new StreamReader(POSTResponse.GetResponseStream());

            string POSTRequestCode = POSTRequestReader.ReadToEnd();
        }

    }
}

Here's the link to the YouTube tutorial that I followed, in case anyone wants it.


texts: Calculating a quality score of something based on attributes

texts:
I'm willing to have a way to calculate a quality score of an object; in this case an addressbook contact.

I believe there are way better approaches to do this and I'd like to hear your references.

How can I calculate a quality scoring of an object based on its properties?

/**
 *
 * get Quality
 *
 * @return float Contact quality percentual score calculated from profile completion
 */
public function getQualityAttribute()
{
    $quality  = 0;
    $quality += isset($this->firstname) ? 1 : 0;
    $quality += isset($this->lastname) ? 1 : 0;
    $quality += isset($this->nin) ? 5 : 0;
    $quality += isset($this->birthdate) ? 2 : 0;
    $quality += isset($this->mobile) ? 4 : 0;
    $quality += isset($this->email) ? 4 : 0;
    $quality += isset($this->postal_address) ? 3 : 0;
    $total    = 20;
    return $quality/$total*100;
}

If you want to know the purpose of this method or further in the app, you can find the code here.


texts: the fox is black - a simple image scraper

texts:
I wrote a simple image scraper for the Desktop Wallpaper project by The Fox is Black. The site is a fantastic (yet, apparently dead) blog full of fabulous images. However, there's no way to download an entire bundle of images, hence this simple scraper.

I'm putting the code up for scrutiny. Any areas for code style and/or implementation improvements? I'd appreciate your feedback.

#!/usr/bin/python

import os
import requests

from bs4 import BeautifulSoup as bs

MAX_PAGES = 1  # Max. number of pages is 41
SAVE_DIRECTORY = 'fox_backgrounds'
BASE_URL = "http://www.thefoxisblack.com/category/the-desktop-wallpaper-project/page"
RESOLUTIONS = [
            "1280x800", "1440x900", "1680x1050", "1920x1200", "2560x1440",
            "iphone", "iphone-5", "iphone6", "iphone-6-plus", "iphone6plus",
            "ipad",
            ]

def fetch_url(url):
    return requests.get(url).text

def clip_url(href):
    return href[href.rfind('/'):]

def save_image(href):
    print(f"Downloading: {clip_url(href)}")
    request = requests.get(href)
    with open(f"fox_backgrounds{clip_url(href)}", 'wb') as output:
        output.write(request.content)

def get_images_from_page(url):
    html = fetch_url(url)
    soup = bs(html, "html.parser")
    for link in soup.find_all("a", class_="btn_download"):
        href = link["href"]
        for resolution in RESOLUTIONS:
            if resolution in href:
                save_image(href)

def make_dir():
    os.makedirs(SAVE_DIRECTORY, exist_ok=True)

def get_backgrounds():
    make_dir()
    for page in range(0, MAX_PAGES):
        get_images_from_page(BASE_URL + str(page + 1))

def main():
    get_backgrounds()

if __name__ == '__main__':
    main()


texts: Unisex Bathroom problem without semaphore

texts:
Could I ask you to evaluate my solution to Unisex Bathroom Problem?

Problem description

  An office has a bathroom that can be used by both men and women, but not both at the same time. If a man is in the bathroom, other men may enter, but any women wishing to use the bathroom should wait for it to be empty. If a woman is in the bathroom, other women may enter, but any men wishing to use the bathroom should wait it to be empty. Each person (man or woman) will spend some time using the bathroom.

Task

  Design and implement a concurrent solution to the problem. The program should show when a person (man or woman) enters or exits the bathroom; as well as how many people (men or women) are in the bathroom at the moment. Because it is a space of relatively small size, the bathroom has a limiting capacity of persons C (provided as input via the command line or prefixed as a constant value) that can use it at the same time and the time that each person passes in the bathroom is random and different to each program execution.

My solution

I tried to also solve a starvation issue and I think I succeeded.

Besides all of that, what is the necessity of semaphores that the problem is related to? Why can't size be simply compared to capacity as I did?

public class Bathroom {

    private String inUseBy;

    private int size;

    private final int capacity;

    private final Lock lock = new ReentrantLock();

    private final Condition man = lock.newCondition();

    private final Condition woman = lock.newCondition();

    public Bathroom(final int capacity, final int size) {
        this.capacity = capacity;
        this.size = size;
        inUseBy = "";
    }

    public void enter(final String sex) throws InterruptedException {
        lock.lock();
        try {
            while (size == capacity || (size > 0 && !sex.equals(inUseBy))) {
                if (sex.equals("M")) {
                    man.await();
                } else {
                    woman.await();
                }
            }
            if (size == 0) {
                inUseBy = sex;
            }
            size++;
        } finally {
            lock.unlock();
        }
        System.out.println(sex + " using bathroom. Current employees in bathroom = " + size);
        Thread.sleep(10000);
        System.out.println(sex + " done using bathroom");
        lock.lock();
        try {
            size--;
            if (size == 0) {
                if (sex.equals("M")) {
                    woman.signalAll();
                } else {
                    man.signalAll();
                }
            } else {
                if (sex.equals("M")) {
                    man.signal();
                } else {
                    woman.signal();
                }
            }
        } finally {
            lock.unlock();
        }
    }

}


texts:
This is my solution to the PermMissingElem problem, I wonder what can be improved? Expected worst case time complexity is O(N), but the performance test shows that it's O(N) or O(N * log(N)), which I suppose there's some solution out there that can truly achieve pure O(N)?

function solution(A) {
    const size = A.length;
    let sum = 0;

    for (i=0;i<size;i++){
        sum += A[i];
    }

    return (((size+ 1)*(size + 2))/2) - sum
}

The original problem is quoted as follows:

  A zero-indexed array A consisting of N different integers is given.
  The array contains integers in the range [1..(N + 1)], which means
  that exactly one element is missing.

  Your goal is to find that missing element.

  Write a function:

  int solution(int A[], int N); that, given a zero-indexed array A,
  returns the value of the missing element.

  For example, given array A such that:

  A[0] = 2   A1 = 3   A[2] = 1   A[3] = 5 the function should return
  4, as it is the missing element.

  Assume that:

  N is an integer within the range [0..100,000]; the elements of A are
  all distinct; each element of array A is an integer within the range
  [1..(N + 1)].

  Complexity:

  expected worst-case time complexity is O(N); expected worst-case space
  complexity is O(1), beyond input storage (not counting the storage
  required for input arguments). Elements of input arrays can be
  modified.


texts:
I am using the following technique for simulating mixins (or Traits) using Java and would like to know whether this is reasonable or just confusing to future developers.  Also, I believe a good naming scheme is the best documentation, so if it would help to change the nomenclature, please provide suggestions.

/** A mixin interface which your class should implement */
public interface IsResizableMixin {

    /**
     * The implementation, which your class should
     * instance and delegate calls to
     */
    public static class IsResizableMixinImpl implements IsResizableMixin {

        // A widget is e.g. a Swing Component, a GWT Widget, etc.
        private Widget w;

        pubic IsResizableMixinImpl(Widget w) {
            this.w = w;
        }

        public void resizeTo(int width, int height);
            /* for example */
            w.setWidth(width);
            w.setHeight(height);
            w.getParent().redraw();
        }
    }

    void resizeTo(int width, int height);
}

Then an example implementation of a Widget, one that is resizable:

public class Foo extends Widget implements IsResizableMixin {
    private IsResizableMixin mixin;
    /* ... */

    public Foo() {
        super();
        this.mixin = new IsResizableMixinImpl(this);
        /* ... */
    }

    /* ... */

    public void resizeTo(int w, int h) {
        mixin.resizeTo(w, h);
    }
}

A richer example would be a Widget that can be set "busy", that is, overlaid with a spinner and disabled.

public interface IsBusyMixin {
    public class IsBusyMixinImpl implements IsBusyMixin {
        private Widget w;
        private boolean isBusy = false;

        pubic IsBusyMixinImpl(Widget w) {
            this.w = w;
        }

        public void toggleBusy();
            isBusy = !isBusy();
            if (isBusy) {
                /* overlay the component with a spinner */
            } else {
                /* remove the spinner overlay */
            }
        }
    }

    void toggleBusy();
}

public class Foo extends Widget implements IsBusyMixin {
    // Other than the fact that this method exists,
    // Foo is unaware that it can be toggled to a busy state.
    public void toggleBusy() {
        this.mixin.toggleBusy();
    }
}

The benefits of this approach are:

Code reuse.  In the past, if we needed Foo functionality, we copy-pasted boilerplate code, but if a bug is discovered, we've lost track of everywhere it's been duplicated.
Delegate implementations can vary; we can have an implementation for a Swing Component and one for a GWT component, but their API is the same.
Field names don't overlap, which is one of the problems with multiple inheritance.

The problems with this approach are:

Possibly confusing.  My solution is to document and hope the developer sees it.
Method name overlapping, which is why multiple inheritance is bad (i.e., Widget may have a getSize() method, and IsResizeableMixin may also declare a getSize()).  The solution is to give mixin methods distinctive names.

This is just a simple example.  Putting the boolean in the implementing class would work just as well, but I am trying to factor that out of Foo.  If we decide to change how that state is stored, all IsBusyMixins would need to be updated.

Any pros/cons I'm missing?

Note that Java 8's default interface implementations don't cover this use-case since mixins must hold some state.


texts:
I have written BST in Java for practice. How can I write better unit test cases. I am sure the way I have written them is not proper.

Code for BST

package com.r.basics.tree;
import org.junit.Before;
import org.junit.Test;

public class BinarySearchTreeTest{
private BinarySearchTree<Integer> binarySearchTreeSUT;

    @Before
    public void setUp() {
        binarySearchTreeSUT =  new BinarySearchTree<>();
    }

    @Test
    public void testShouldInsertItemToBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);
    }

    @Test
    public void testShouldGetMaxItemFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        assert(binarySearchTreeSUT.getMax()  == 8);
    }

    @Test
    public void testShouldGetMinItemFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        assert(binarySearchTreeSUT.getMin()  == 2);
    }
    @Test
    public void testShouldTraverseItemsFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        binarySearchTreeSUT.traversal();
    }

    @Test
    public void testShouldDeleteLeafNOdeItemsFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        binarySearchTreeSUT.traversal();

        binarySearchTreeSUT.delete(3);

        binarySearchTreeSUT.traversal();
    }

    @Test
    public void testShouldDeleteParentNodeWithBothChildrenItemsFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        binarySearchTreeSUT.traversal();

        binarySearchTreeSUT.delete(4);

        binarySearchTreeSUT.traversal();
    }

    @Test
    public void testShouldDeleteParentNodeWithBothChildrenItemsFromBSTWithSuccessor(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(7);

        binarySearchTreeSUT.traversal();

        binarySearchTreeSUT.delete(4);

        binarySearchTreeSUT.traversal();
    }

    @Test
    public void testShouldDeleteParentNodeWithOneChildrenItemsFromBST(){

        binarySearchTreeSUT.insert(4);
        binarySearchTreeSUT.insert(8);
        binarySearchTreeSUT.insert(2);
        binarySearchTreeSUT.insert(3);

        binarySearchTreeSUT.traversal();

        binarySearchTreeSUT.delete(2);

        binarySearchTreeSUT.traversal();
    }

}


texts: 2D Vector: Loading values from .txt file

texts:
I've written a program that runs through a matrix of numbers, ranging from 0 to 2, and stores the results in a 2D vector.

I'm using the values in the 2D vector as the profile for a tile map for a game engine.

I want this to be optimal, and as this is my first attempt at it, I doubt that it is but it does work as is.

Any suggestions for improvement or obvious errors I've made?

The entire project can be found here:
link

Thanks in advance.

    // NOTE (Devan): Opening the file.
    std::ifstream mapFile ( fileToOpen );
    if ( !mapFile.is_open () )
    {
        printf ( "Error: Could not open file: %s\n", fileToOpen );
        return;
    }

    // NOTE (Devan): Setting the dimensions of the map. The width and height are taken from the first two entries of the text file.
    unsigned int width = 0;
    unsigned int height = 0;
    mapFile >> width;
    mapFile >> height;

    int current = 0;
    for ( unsigned int y = 0; y < height; y++ )
    {
        // NOTE (Devan): Forming a new vector to be stored within the outer vector forming the rows of the map.
        std::vector<int> innerVec;

        for ( unsigned int x = 0; x < width; x++ )
        {
            if ( mapFile.eof () )
            {
                printf ( "Reached end of file %s too early. Check width and height parameters.\n", fileToOpen );
                return;
            }

            // NOTE(Devan): Capturing the value stored at the current location within the text file.
            mapFile >> current;

            // NOTE (Devan): Checking if the input is within the tile type boundaries. Current Types: ( 0 : 2 )
            if( current >= 0 && current <= 2 )
            {
                innerVec.push_back ( current );
            }
            // NOTE (Devan): If the input is outside of the boundaries simply push 0 into the inner vector.
            else
            {
                innerVec.push_back (0);
            }
        }
        tileMap.push_back ( innerVec );
    }

    mapFile.close ();


texts: Histogram of Maxwell-Boltzmann distribution

texts:
The following C# source code creates a histogram of Maxwell-Boltzmann's distribution:

using System;
using System.Drawing;
using ZedGraph;

public class CommonDistributions
{
    public static double Uniform(Random random)
    {
        return random.NextDouble();
    }

    static double Gaussian(Random random)
    {
        return Math.Sqrt(-2 * Math.Log(Uniform(random))) * Math.Cos(2 * Math.PI * Uniform(random));
    }
    public static double Gaussian(Random random, double mu, double sigma)
    {
        return sigma * Gaussian(random) + mu;
    }
}

public class MaxwellBolzman
{
    private static double KB = 1.38064852e-23;

    public static double MaxwellVariance(double mass, double temperature)
    {
        return Math.Sqrt(KB * temperature / mass);
    }

    public static double MaxwellComponent(Random random, double mass, double temperature)
    {
        double mu = 0.0;
        double sigma = MaxwellVariance(mass, temperature);

        return CommonDistributions.Gaussian(random, mu, sigma);
    }
    public static double MaxwellSpeed(Random random, double mass, double temperature)
    {
        double one = MaxwellComponent(random, mass, temperature);
        double two = MaxwellComponent(random, mass, temperature);
        double thr = MaxwellComponent(random, mass, temperature);

        return Math.Sqrt(one * one + two * two + thr * thr);
    }
}

public static class Normalization
{
    public static int Normalize(double n_bins, double mu, double sigma, double gaussian)
    {
        var z = (mu - gaussian)/sigma;

        if (z > 3 || z < -3)
        {
            return -1;
        }
        else
        {
            return (int)((z + 3) * n_bins / 6d);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        const double N = 1000000;
        int time = (int)N;
        int binsCount = 51;

        Random rand = new Random();
        int[] bins = new int[binsCount];

        double mass = 14 * 1.67e-27;
        double T = 300;

        for (int i = 0; i < time; i++)
        {
            double gauss = MaxwellBolzman.MaxwellSpeed(rand, mass, T);
            double mu = MaxwellBolzman.MaxwellComponent(rand, mass, T);
            double sigma = MaxwellBolzman.MaxwellVariance(mass, T);

            int index = Normalization.Normalize(binsCount,  mu, sigma, gauss);

            if (index >= 0)
            {
                bins[index]++;
            }
        }

        PointPairList list = new PointPairList();
        for (int i = 0; i < bins.Length; i++)
        {
            list.Add(i, bins[i]);
        }

        PlotForm form = new PlotForm("Maxwell-Bolzman");
        form.AddBar(list, "Actual", Color.Blue);
        form.AxisChange();
        form.ShowDialog();
    }
}

Kindly, review the code.

N.B. The curve seems to be off the shape.


texts:
I'm trying to extract all files (XMLs) from an ftp site with python and the ftplib module, but it's incredibly slow so I suspect I'm doing something wrong.

The files are in subfolders, and all at the same "depth" eg: 'startingdir/folder1/place1/file.xml', 'startingdir/folder1/place1/file2.xml', 'startingdir/folder1/place2/file.xml', 'startingdir/folder2/place1/file.xml'

I want to extract everything regardless of location.

As a side note, if this can be put into a function which iterates the process until it finds .xml files in the directory and then extracts those, that would be incredibly helpful (rather than knowing the "depth" and putting the right number of nested for loops).

Code as it stands:

from ftplib import FTP
import os
destination = r'C:\Destination'

ftp = FTP('ftp.com')
ftp.login('user','pw')

Once I'm in, I get a list of all the folders in my starting directory:

ftp.cwd('startingdir')
templist = []
ftp.retrlines('LIST',templist.append)
subdirlist = [i.split(None,8)[-1].lstrip() for i in templist]

Then, I iterate this process a few times:

for i in subdirlist:
    ftp.cwd('startingdir/'+i)
    templist = []
    ftp.retrlines('LIST',templist.append)
    subdirlist2 = [i.split(None,8)[-1].lstrip() for i in templist]

    for j in subdirlist2:
        ftp.cwd('startingdir/'+ i + '/' + j)
        templist = []
        ftp.retrlines('LIST',templist.append)
        subdirlist3 = [i.split(None,8)[-1].lstrip() for i in templist]

Finally, I extract all files from the right "depth" folder:

        for k in subdirlist3:
            local_filename = os.path.join(destination,k)
            lf = open(local_filename, 'wb')
            ftp.retbinary('RETR '+ k, lf.write, 8*1024)
            lf.close

The code works, but it's incredibly slow (without counting precisely, it looks like 3-4 files/second). Is there anything obvious I'm doing wrong? Having 4-5 nested for loops doesn't look good, but I don't know how else to achieve this.

Thank you in advance for the suggestions!


texts:
I've implemented my own 'power-mod' procedure which will compute a^b 'mod' n for some large b and even larger n. I think the way I've done it is a common procedure, and it does seem well optimised, but I really need to squeeze the maximum performance out of it as it is currently the bottleneck in my program (~75% of the CPU time).

First I will explain in words the procedure (ironically, in an imperative style), then display the code (which is quite short).

In Words

Calculating a^b mod n:

Obtain the binary expansion of b in Big Endian form. Call it bE.

For example, b = 10 becomes b = [1,0,1,0] which is the reverse of its conventional binary representation 0101. This part is well optimised (~5% of the CPU time).

Drop the first element of bE (essentially because this  will always be 1-- I don't care about b=0 as this is trivial).
Start with result = 1. (i.e. a^0)
If bE is empty, then return result.
Take the next element from bE to be k.

If k is 0, then do result = result^2     'mod' n
If k is 1, then do result = result^2 * a 'mod' n

Go to step 4.

The Code

-- This part computes the Big Endian binary representation list of an Integer `k`. It's not a bottleneck, so I'm not so concerned about this.
binarizeE k = binarizeETab k []
  where
    binarizeETab 0 xs  = xs
    binarizeETab k xs = binarizeETab (fst kdivmod) ((snd kdivmod):xs)
      where
        kdivmod = k `divMod` 2

-- The part that I want to optimise to oblivion!
-- Doing `rem` and then ending with a final `mod` may make an incredibly minute improvement for extremely large numbers as compared with using `mod` each and every step.
largepowmod num pow modbase = (operate num opList) `mod` modbase
  where
    opList = drop 1 $ binarizeE pow
    operate k    []   = k
    operate k (0:ops) = operate (k^2         `rem` modbase) ops
    operate k (1:ops) = operate ((k^2 * num) `rem` modbase) ops

My Optimistic Goal

The background here is that I've implemented the Baillie–PSW Primality Test and am trying to improve its performance so that it is comparable with professional software like Mathematica. This bottleneck is happening during the Miller-Rabin Test procedure. Since Haskell is compiled, and Mathematica on my machine utilizes the same CPU % as my program, I don't see why I couldn't achieve something close. At the moment, they are indeed close for some tests (1.1s versus 1.4s), but for fun I'd like to see if maybe I can even surpass Mathematica!

Additional Information / Benchmark

The base a is in my case always 2. Maybe this lends itself to some neat trick. The exponent b and modulo base n vary greatly, but a typical example would be something like n an odd number somewhere in the vicinity of 10^1000 and b ~= n/2.

Example test comparison of my current implementation and Mathematica's:

Set b(n) = (n-1)/2. Then evaluating 2^b mod n for every odd n between 10^1000 and 10^1000 + 10000 takes ~110s for my program, and ~92s for Mathematica.

Testing code below for reference.

Haskell:

import PrimeStuff
import PrimeStuff.PQTrials
import System.Environment
import Control.DeepSeq

main :: IO()
main = do
        let trialNs   = [10^1000 + 1, 10^1000 + 3.. 10^1000 + 9999]
        let modTest n = largepowermod 2 ((n-1) `div` 2) n
        let test      = map modTest trialNs

        let sol   = deepseq test "Done."
        putStrLn(sol)

Mathematica:

nList = 10^1000 + (2*Range[5000] - 1);
Timing[results = PowerMod[2, (# - 1)/2, #] & /@ nList;][[1]]

Ideas

I am always using a=2 as the base, so in big endian form, a^b looks like [1,0,0,0.....0] with b zeros. Is there perhaps a way of taking the modulo of this with respect to n by manipulating the 0s and 1s directly? Would such a thing be faster than what the computer/compiler is already doing with my current code?


texts: Moving average function in C++ for use with R

texts:
I am trying to improve the code and speed up in C++ (Rcpp) a (centered) weighted moving average function I coded.

An example of what the roll_mean function does. Note that the function works no matter what the size of x is and adapts to both tails of my data

w=c(1/2,1,1/2)
x=c(4,2,6,12)
res=c(2,5,7,3)
res=c((x[1:2]*w[2:3])/sum(w[2:3]),x[1:3]*w[1:3]/sum(w[1:3]),x[2:4]*w[1:3]/sum(w[1:3]),x[3:4]*w[1:2]/sum(w[1:2]))

The file PartialMA.cpp

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector roll_mean(const NumericVector& x,
                        const NumericVector& w) {

  int n = x.size();
  int w_size = w.size();
  int size = (w_size - 1) / 2;

  NumericVector res(n);
  int i, ind_x, ind_w;

  double tmp_wsum, tmp_xwsum, tmp_w;

  for (i = 0; i < n; i++) {
    tmp_xwsum = 0;
    tmp_wsum = 0;
    for (ind_x = i - size, ind_w = 0; ind_x < i + size; ind_x++, ind_w++) {
      if((ind_x >= 0) & (ind_x < n)){
      tmp_w = w(ind_w);
      tmp_xwsum += x(ind_x) *  tmp_w;
      tmp_wsum += tmp_w;
      }
    }
    res[i] = tmp_xwsum / tmp_wsum;
  }

  return res;
}

I tried to replace the loop + if statement with this to minimize the number of iteration:

for (ind_x = std::max(0, i - size), ind_w = std::max(0, size-1); ind_x < std::min(n, i + size); ind_x++, ind_w++) {

I feel like I am not rigorous enough and I would be very grateful if someone could help me improving my code and eventually speed up the function as much as possible.


texts:
Does this look right to you? What are its shortcomings? Strengths?

import Control.Monad
import Control.Monad.Trans.Class
import Control.Pipe.Common
import Data.Conduit

c2p :: (Resource m, Monad m) => Conduit a m b -> Pipe a b m ()
c2p = do
  PreparedConduit push close <- lift $ runResourceT $ prepareConduit c
  loop push close
  where loop push close = do
          input <- await
          stepResult <- lift $ runResourceT $ push input
          case stepResult of
            Producing output -> do
              mapM_ yield output
              loop push close
            Finished _ output -> do
              mapM_ yield output
              lift $ runResourceT $ close
              return ()


texts:
Suppose I have an array of sentences:

  [ "How To Format", "put returns between paragraphs", "for linebreak add
  2 spaces at end" ]

I want to print it like this:

How        put            for
To         returns        linebreak
Format     between        add
           paragraphs     2
                          spaces
                          at
                          end

Here's my code:

public static void solve(String[] sentences) {
    int n = sentences.length;

    if(n == 0)
        return;

    List<List<String>> matrix = new ArrayList<List<String>>();
    int maxLength = Integer.MIN_VALUE;

    for(String sentence : sentences) {
        List<String> words = Arrays.asList(sentence.split(" "));
        maxLength = Math.max(maxLength, words.size());

        matrix.add(words);
    }

    for(int col = 0; col < maxLength; col++) {
        for(int row = 0; row < matrix.size(); row++) {
            try {
                System.out.format("%-15s", matrix.get(row).get(col));
            }
            catch(IndexOutOfBoundsException e) {
                System.out.format("%-15s", "");
            }
        }
        System.out.println();
    }
}

Is this easy to understand? Can I make it simpler, such as by using different data structures?


texts: Execute command using `asyncio.create_subprocess_shell` and yield lines of stdout, stderr and finally the return code

texts:
My task is to run a subprocess using asyncio.create_subprocess_shell and yield the lines of output. I am using asyncio to avoid creating threads just to pump the streams.

It is necessary to separate stdout lines from stderr lines.
Both outputs need to be read simultaneously, it is not possible to read all of stdout or stderr first, because then the other output buffer in the subprocess may fill and it would stop running.
This function is incorporated into a gRPC service, which is the reason for wrapping the yielded lines in instances of cli_pb2.ExecuteReply(...)
.readline() returns "" on EOF, so when I see it, that is when to stop reading that stream.

I want to ask if my approach to reading stdout and stderr simultaneously can be improved. I was looking for something akin to UNIX select() and found asyncio.wait({...}, return_when=asyncio.FIRST_COMPLETED). Then I had to figure out which stream (stdout, stderr) the result corresponds to. I hacked at it by looking at _coro.

import asyncio

import cli_pb2
import cli_pb2_grpc

async def run(cmd):
    proc = await asyncio.create_subprocess_shell(
        cmd,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )

    stderr_readline = proc.stderr.readline()
    stdout_readline = proc.stdout.readline()
    pending = {stderr_readline, stdout_readline}

    while pending:
        done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
        for f in done:
            line = (await f).decode()
            if f._coro is stderr_readline:  # bad, but it works and can't think of better approach
                if line:
                    yield cli_pb2.ExecuteReply(stderr=line)
                    stderr_readline = proc.stderr.readline()
                    pending.add(stderr_readline)  # put it back into the `pending` set
            if f._coro is stdout_readline:
                if line:
                    print(line)
                    yield cli_pb2.ExecuteReply(stdout=line)
                    stdout_readline = proc.stdout.readline()
                    pending.add(stdout_readline)
    await proc.wait()
    status = proc.returncode
    yield cli_pb2.ExecuteReply(status=status)

Usage example

async def printlines():
    async for line in run("echo a; sleep 5; echo b; echo c"):
        print(line)

outputs (with a 5 second pause after first line

stdout: "a\n"
stdout: "b\n"
stdout: "c\n"

The protobuf definition is

syntax = "proto3";

option java_multiple_files = true;
option java_package = "djtests.cli";
option java_outer_classname = "CliProto";

package cli;

service Executor {
    rpc Execute (ExecuteRequest) returns (stream ExecuteReply) {
    };
}

message ExecuteRequest {
    string cmd = 1;
}

message ExecuteReply {
    oneof reply_fields {
        string stdout = 1;
        string stderr = 2;
        int32 status = 3;  // sent in the last message
    }
}
```


texts: Query of attributes of mobile phone models, using several UNIONs to find distinct values

texts:
I'd like to know if there's any room to improve the following query which is to find distinct values from multiple tables when someone searches for a model name. Two of the tables are storing phones' series attributes and model attributes. The model attr table is there to deal with special cases, like a series generally use black cases, but it also has released a special edition that uses golden cases.

Here's the query that I have come up. Basically I'm using UNION to unpivot table cpu and find distinct values across cpu,model_attr and series_attr Is there any need for improvement?

Here's a fiddle example.

Query:

SELECT attr_name,attr_val
FROM
(SELECT 'CPU_country' attr_name, c.country attr_val
 FROM cpu c INNER JOIN
 series s ON s.cpu_id = c.cpu_id
 INNER JOIN model m ON m.series_id = s.series_id
 WHERE m.model_name RLIKE 'C79'
 UNION

 SELECT 'CPU_hours' attr_name, c.hours attr_val
 FROM cpu c INNER JOIN
 series s ON s.cpu_id = c.cpu_id
 INNER JOIN model m ON m.series_id = s.series_id
 WHERE m.model_name RLIKE 'C79'
UNION

SELECT a.attr_name attr_name,a.attr_value attr_val
FROM model m INNER JOIN
     series s
     on m.series_id = s.series_id LEFT JOIN
     series_attr sa
     ON sa.series_id = s.series_id LEFT JOIN
     attr saa
     on saa.attr_id = sa.attr_id LEFT JOIN
     model_attr ma
     ON ma.model_id = m.model_id LEFT JOIN
     attr a
     ON a.attr_id = ma.attr_id
     WHERE m.model_name RLIKE 'C79' AND a.attr_value IS NOT NULL
UNION

SELECT saa.attr_name attr_name,saa.attr_value attr_val
FROM model m INNER JOIN
     series s
     on m.series_id = s.series_id LEFT JOIN
     series_attr sa
     ON sa.series_id = s.series_id LEFT JOIN
     attr saa
     on saa.attr_id = sa.attr_id LEFT JOIN
     model_attr ma
     ON ma.model_id = m.model_id LEFT JOIN
     attr a
     ON a.attr_id = ma.attr_id
     WHERE m.model_name RLIKE 'C79' AND saa.attr_value IS NOT NULL
 )k
GROUP BY attr_name,attr_val
ORDER BY attr_name

Table Schema (InnoDB):

CREATE TABLE series
    (`series_id` int, `series_name` varchar(20),`cpu_id` INT ,`ram`int)
;

INSERT INTO series
    (`series_id`,`series_name`,`cpu_id`,`ram`)
VALUES
    (1,'Nokia Series',1,512),
    (2,'Sony Series',2,1024)

;

CREATE TABLE model
    (`model_id` int, `model_name` varchar(20),`series_id` int)
;

INSERT INTO model
    (`model_id`,`model_name`,`series_id`)
VALUES
    (1,'A6578',1),
    (2, 'B2345',1),
    (3, 'C7906',2),
    (4, 'D3544',2)
;

CREATE TABLE attr
    (`attr_id` int, `attr_name` varchar(20),`attr_value` varchar(20))
;

INSERT INTO attr
    (`attr_id`,`attr_name`,`attr_value`)
VALUES
    (1, 'material','Gold'),
    (2, 'material','Plastic'),
    (3, 'color','Grey'),
    (4, 'color','Black'),
    (5, 'color','Green'),
    (6, 'color','White')

;

CREATE TABLE series_attr
    (`series_id` int, `attr_id` int )
;
INSERT INTO series_attr
    (`series_id`,`attr_id`)
VALUES
    (1,2),
    (1,5),
    (2,2),
    (2,3)
;

CREATE TABLE model_attr
    (`model_id` int, `attr_id` int)
;
INSERT INTO model_attr
    (`model_id`,`attr_id`)
VALUES
    (2,1),
    (2,4),
    (4,6)
;

CREATE TABLE cpu
    (`cpu_id` int,`cpu_name` varchar(20), `country` varchar(20),`hours`int,`frequency` int)
;
INSERT INTO cpu
    (`cpu_id`,`cpu_name`,`country`,`hours`,`frequency`)
VALUES
    (1,'CPU A','China',40,24000),
    (2,'CPU B','US',80,30000),
    (3,'CPU C','Japan',100,35000)
;


texts: Can this be written more concise and aligned with the Functional Reactive Programming paradigm?

texts:
Trying to learn FRP and Elm. Is the program below "ok" from the perspective of FRP and Elm? What could be improved? The program can be executed here: http://elm-lang.org/try

import Random
import Mouse
data Fruit = Apple | Orange | Banana | Melon | Guava
rand = Random.range 0 4 (Mouse.clicks)
fruit n = if | n == 0 -> Apple
             | n == 1 -> Orange
             | n == 2 -> Banana
             | n == 3 -> Melon
             | otherwise -> Guava
fruitText = toForm <~ (asText <~ (fruit <~ rand))
time = lift (inSeconds . fst) (timestamp (fps 40))
scene dy form = collage 300 300 [move (0, 100 * cos dy) form]
main = lift2 scene time fruitText


texts:
I'd really appreciate if any of you could do a code review of my Sopping cart demo. I was wondering whether this could be improved? I'm honestly trying to improve myself too!

There's a small problem on the product list! looks like doest work if has more than 1 product. Demo: http://plnkr.co/edit/jLk0VdSgGYPwLhNKkcs7

var module =  {

    init: function () {
        this.shoppingCart();
    },

  shoppingCart: function () {
      numberIncrementer(this);
      updateQuantity(this);
      recalculateCart();

      /* Set rates + misc */
      var taxRate = 0.20;
      var fadeTime = 300;

      /* Assign actions */
      $('.product-quantity input').change( function() {
        updateQuantity(this);
      });

      $('.product-removal button').click( function() {
        removeItem(this);
      });

      /* Recalculate cart */
      function recalculateCart() {
        var subtotal = 0;

        /* Sum up row totals */
        $('.product').each(function () {
          subtotal += parseFloat($(this).children('.product-line-price').text());
        });

        /* Calculate totals */
        var tax = subtotal * taxRate;
        var total = subtotal + tax;

        /* Update totals display */
        $('.totals-value').fadeOut(fadeTime, function() {
        $('#cart-subtotal').html(subtotal.toFixed(2));
        $('#cart-tax').html(tax.toFixed(2));
        $('#cart-total').html(total.toFixed(2));
          if(total == 0){
            $('.checkout').fadeOut(fadeTime);
          }else{
            $('.checkout').fadeIn(fadeTime);
          }
          $('.totals-value').fadeIn(fadeTime);
        });
      }

      /* Update quantity */
      function updateQuantity(quantityInput) {
        /* Calculate line price */
        var quantityInput = $('.product-quantity input');
        var productRow = $(quantityInput).parent().parent();
        var price = parseFloat(productRow.children('.product-price').text());
        var quantity = $(quantityInput).val();
        var linePrice = price * quantity;

        /* Update line price display and recalc cart totals */
        productRow.children('.product-line-price').each(function () {
          $(this).fadeOut(fadeTime, function() {
            $(this).text(linePrice.toFixed(2));
            recalculateCart();
            $(this).fadeIn(fadeTime);
          });
        });
      }

      /* Remove item from cart */
      function removeItem(removeButton) {
        /* Remove row from DOM and recalc cart total */
        var productRow = $(removeButton).parent().parent();
        productRow.slideUp(fadeTime, function() {
        productRow.remove();
        recalculateCart();
        });
      }

    function numberIncrementer(){
          $("div.product-quantity").append('
+

-
');

          $(".button").on("click", function() {
            var $button = $(this);
            var oldValue = $button.parent().find("input").val();

            if ($button.text() == "+") {
              var newVal = parseFloat(oldValue) + 1;
            } else {
               // Don't allow decrementing below zero
              if (oldValue > 0) {
                var newVal = parseFloat(oldValue) - 1;
                } else {
                newVal = 0;
              }
              }
            $button.parent().find("input").val(newVal);
            updateQuantity();
          });

        }
    }
}

$(document).ready(function () {
    module.init();
});


texts: Idiomatic Django conditional filter queryset

texts:
I am sharing a viewset between two types of routes in Django: one is a nested route (/users/:id/comments/:id/), and a regular route (/comments/:id/). The query set returned is either filtered by the user_pk or it's not. What is an idiomatic way of writing the following?

if(user_pk):
    queryset = Comment.objects.filter(pk=pk, user=user_pk)
else:
    queryset = Comment.objects.filter(pk=pk)


texts: Program to compute final grade for weighted assignments

texts:
I wrote a "gradebook" program.  This takes in grades for various assignments. The assignments types are weighted differently and final grade is computed at the end.  The code works but I can't help but think I am being ham handed in my approach. Should I be using a dictionary instead of storing things like hw_final, lab_final, and exam_final as global variables?

hw_tally = 0
hw_weight_count = 0
hw_final = 0
lab_tally = 0
lab_weight_count = 0
lab_final = 0
exam_tally = 0
exam_weight_count = 0
exam_final = 0

def assignment(kind, grade, weight=1):
    global hw_tally, hw_weight_count, hw_final, lab_tally, lab_weight_count, lab_final, exam_tally, exam_weight_count, exam_final
    if kind == 'hw':
        hw_weight_count += weight
        hw_temporary = (grade * weight)
        hw_tally += hw_temporary
        hw_final = hw_tally/hw_weight_count
    elif kind == 'lab':
        lab_weight_count += weight
        lab_temporary = (grade * weight)
        lab_tally += lab_temporary
        lab_final = lab_tally/lab_weight_count
    elif kind == 'exam':
        exam_weight_count += weight
        exam_temporary = (grade * weight)
        exam_tally += exam_temporary
        exam_final = exam_tally/exam_weight_count

assignment('hw', 81)
assignment('exam', 90, 4)
assignment('hw', 100, 2)
assignment('lab', 84, 3)
assignment('lab', 90, 2)
assignment('exam', 78)
assignment('exam', 85)
assignment('lab', 88)
assignment('exam', 75)

hw_weight_percent = 0.4
lab_weight_percent = 0.1
exam_weight_percent = 0.5

final_grade = ((hw_final * hw_weight_percent) + (lab_final *
lab_weight_percent) + (exam_final * exam_weight_percent))
print ("Final grade = ", final_grade)


texts: Three functions to select a random character, each of which can be used at most twice

texts:
I'm attempting to minimise the "duplicate" functions I have defined which do almost the exact same thing, but I'm unsure of the best way to do it, I thought of passing in the type I'm looking for (e.g. 1 for GreyLeader, 2 for HeadMarshal etc.) but that ended up looking too messy for my liking.

Could you guys perhaps suggest a way to combine the three functions into one to return the same result?

Some notes:
 - marshal is a list of a Marshal class which has Name, HeadMarshal, Marshal, GreyLeader boolean flags as properties
 - usedMarshals is a list of Marshals
 - each marshal can only be used twice, so the usedMarshals.count(marshal) = 2 is used for that

Code:

def get_marshal():
    marshal = random.choice(marshals)
    if marshal.Marshal:
        if not marshal in usedMarshals:
            return marshal
        elif marshal in usedMarshals:
            if usedMarshals.count(marshal) < 2:
                return marshal
            elif usedMarshals.count(marshal) >= 2:
                return get_marshal()
    elif not marshal.Marshal:
        return get_marshal()

def get_head_marshal():
    marshal = random.choice(marshals)
    if marshal.HeadMarshal:
        if not marshal in usedMarshals:
            return marshal
        elif marshal in usedMarshals:
            if usedMarshals.count(marshal) < 2:
                return marshal
            elif usedMarshals.count(marshal) >= 2:
                return get_head_marshal()
    elif not marshal.HeadMarshal:
        return get_head_marshal()

def get_grey_leader():
    marshal = random.choice(marshals)
    if marshal.GreyLeader:
        if not marshal in usedMarshals:
            return marshal
        elif marshal in usedMarshals:
            if usedMarshals.count(marshal) < 2:
                return marshal
            elif usedMarshals.count(marshal) >= 2:
                return get_grey_leader()
    elif not marshal.GreyLeader:
        return get_grey_leader()


texts: Exam score query for many types of exams

texts:
I have the following MySQL SELECT statement. It is working fine except that the code is too long. I have been looking throughout the Internet to figure out how I can make it shorter.

 SELECT regd, Subject, Section, date,
 SUM(CASE WHEN (Name_of_exam = 'First Unit Exam'
 OR Name_of_exam = 'Second Unit Exam'
 OR Name_of_exam = 'Third Unit Exam')
 THEN (Mark_score / Full_mark) *25 END) AS t_scored,
 SUM(CASE
 WHEN (Name_of_exam = 'First Unit Exam'
 OR Name_of_exam = 'Second Unit Exam'
 OR Name_of_exam = 'Third Unit Exam')
 THEN (Full_mark) END) AS t_fm,
 SUM(CASE
 WHEN (Name_of_exam = 'First Term Weekly Test'
 OR Name_of_exam = 'Second Term Weekly Test'
 OR Name_of_exam = 'Third Term Weekly Test'
 OR Name_of_exam = 'Final Term Weekly Test')
 THEN (Mark_score / Full_mark) *25
 END ) AS w_scored,
 SUM(CASE
 WHEN (Name_of_exam = 'First Term Weekly Test'
 OR Name_of_exam = 'Second Term Weekly Test'
 OR Name_of_exam = 'Third Term Weekly Test'
 OR Name_of_exam = 'Final Term Weekly Test')
 THEN (Full_mark) END ) AS w_fm,
 SUM(CASE
 WHEN Name_of_exam = 'Final Unit Exam'
 THEN (Mark_score / Full_mark) *25
 END ) AS f_scored,
 SUM(CASE
 WHEN Name_of_exam = 'Final Unit Exam'
 THEN (Mark_score) END ) AS score_m,
 SUM(CASE
 WHEN Name_of_exam = 'CCE'
 THEN (Mark_score / Full_mark) *25
 END ) AS cce_scored,
 SUM(CASE
 WHEN Name_of_exam = 'CCE'
 THEN (Full_mark) END ) AS cce_fm
 FROM exam_mark
 WHERE regd='23' AND Section='A'
 AND date BETWEEN '2013-11-01' AND '2013-11-15'
 GROUP BY Subject

Any suggestion is welcome.


texts: Simple HTTP server that performs a regex test

texts:
I'm sure that my server can work with better performance, but it's just a dream.

Parsing a very long strings is not so obvious task for me.
I have a server that handles a lot of clients requests and finds a substring of a string.

Server

package api

import (
    "net/http"
    "stackexchange/api/controllers"
)

func start() {
    mux := http.NewServeMux()
    mux.HandleFunc("/messages", Messages)
}

func Messages(res http.ResponseWriter, req *http.Request) {
    mc := controllers.NewMessageController()
    switch req.Method {
    case "POST":
        mc.ActionPost(res, req)
    }
}

Controller

package controllers

import (
    "net/http"
    "regexp"
)

type MessageController struct {
}

func NewMessageController() *MessageController {
    return &MessageController{}
}

func (mc *MessageController) ActionPost(res http.ResponseWriter, req *http.Request) {

    msgBody := req.URL.Query().Get("body")
    msgPattern := req.URL.Query().Get("pattern")

    isPatternFind := regexp.MatchString(msgPattern, msgBody)

    if isPatternFind {
        res.Write([]byte("find"))
    } else {
        res.Write([]byte("not find"))
    }
}

Test

package tests

import (
    "fmt"
    "log"
    "net/http"
    "net/http/httptest"
    "stackexchange/api"
    "testing"
)

var longString = `Knowing that millions of people around the world would be
                  watching in person and on television and expecting great
                  things from him — at least one more gold medal for America,
                  if not another world record — during this, his fourth and
                  surely his last appearance in the World Olympics, and realizing
                  that his legs could no longer carry him down the runway with
                  the same blazing speed and confidence in making a huge,
                  eye-popping leap that they were capable of a few years ago
                  when he set world records in the 100-meter dash and in the
                  400-meter relay and won a silver medal in the long jump,
                  the renowned sprinter and track-and-field personality
                  Carl Lewis, who had known pressure from fans and media before
                  but never, even as a professional runner, this kind of pressure,
                  made only a few appearances in races during the few months before
                  the Summer Olympics in Atlanta, Georgia, partly because he was
                  afraid of raising expectations even higher and he did not want
                  to be distracted by interviews and adoring fans who would follow
                  him into stores and restaurants demanding autographs and
                  photo-opportunities, but mostly because he wanted to conserve
                  his energies and concentrate, like a martial arts expert, on
                  the job at hand: winning his favorite competition, the long jump,
                  and bringing home another Gold Medal for the United States,
                  the most fitting conclusion to his brilliant career in track and field.`

var subString = `home`

func TestPostMessage(t *testing.T) {
    url := "http://localhost:8080/messages?"
    url = fmt.Sprintf("%sbody=%s", url, longString)
    url = fmt.Sprintf("%s&pattern=%s", url, subString)

    req, err := http.NewRequest("POST", url, nil)

    if err != nil {
        log.Fatal(err)
    }

    res := httptest.NewRecorder()
    api.Messages(res, req)

    expected := "find"
    actual := res.Body.String()

    if actual != expected {
        log.Fatalf("\nMethod: POST\nActual: %s \nExpected: %s \n", actual, expected)
    }
}


texts:
I have written a k-means function in Python to understand the methodology. I am trying to use this on a more complex dataset with a larger value for k, but it is running super slow. Does anyone have any recommendations for how I can improve this? I have provided code below, along with loading in an example dataset and applying the algorithm.
def Euc(x,y):
    return math.sqrt(sum([(a - b) ** 2 for a,b in zip(x,y)]))

def K_means(TE,k):
    Iteration = 0
    R = []
    O_a = []
    Epoch = 0
    Tol = 1
    Old_Tol = 2
    Tol_r = []
    start_time = time.time()
    mean_cl = [[random.uniform(TE.iloc[i].min(),TE.iloc[i].max()) for i in range(len(TE.columns))] for c in range(0,k)]
    for n in range(len(TE)):
        D = [Euc(TE.iloc[n].tolist(),mean_cl[c]) for c in range(0,k)]
        O_a.append(D.index(min(D)))
    while(abs(Old_Tol - Tol) > 0.005):
        Old_Tol = Tol
        Epoch = Epoch + 1
        mean_cl = [TE.iloc[[j for j, x in enumerate(O_a) if x == i]].mean() for i in range(0,k)]
        N_a = []
        for n in range(len(TE)):
            Iteration = Iteration + 1
            D = [Euc(TE.iloc[n].tolist(),mean_cl[c]) for c in range(0,k)]
            N_a.append(D.index(min(D)))
       Tol = np.mean([x != y for x,y in zip(O_a,N_a)])
        Tol_r.append(Tol)
        O_a = N_a
    R.append(time.time() - start_time)
    R.append(Tol_r)
    R.append(N_a)
    R.append(Iteration)
    return R

def load_Pima():
    url = "http://www.stats.ox.ac.uk/pub/PRNN/pima.tr"
    Pima_training = pd.read_csv(url,sep = '\s+')
    url = "http://www.stats.ox.ac.uk/pub/PRNN/pima.te"
    Pima_testing = pd.read_csv(url,sep = '\s+')
    Pima_training = Pima_training.iloc[1:]
    Pima_testing = Pima_testing.iloc[1:]
    Pima_training.loc[:,"type"] = Pima_training.loc[:,"type"].apply(lambda x : 0 if x == 'Yes' else 1)
    Pima_testing.loc[:,"type"] = Pima_testing.loc[:,"type"].apply(lambda x : 0 if x == 'Yes' else 1)
    Features = Pima_training.loc[:,Pima_training.columns != "type"]
    Means = Features.mean()
    SDs = Features.std()
    for name in Features.columns:
        Pima_training[name] = (Pima_training[name]-Means[name])/SDs[name]
        Pima_testing[name] = (Pima_testing[name]-Means[name])/SDs[name]
    return Pima_training, Pima_testing

Pima_training, Pima_testing = load_Pima()

class_var = "type"
random.seed(2031)
k = 2
TE = Pima_testing
TE = TE.loc[:,TE.columns != class_var]
km = K_means(TE,k)

The function returns the runtime of the algorithm, the tolerance at each epoch (% of changes in cluster assignment), the final cluster assignments, and the total number of iterations. I have already removed four for loops, which has sped it up quite a bit. But I fear my lack of Python programming is holding me back from making this more efficient. Any help is appreciated!


texts: Finding collision between two Images using virtual Rect

texts:
I'm making a game. There is a turtle and lots of platforms it can walk on. It walks on its own and you can change the direction it is walking. I implemented a working logic, but I think it can be improved.
I made 2 classes (one for the turtle and one for the platforms).
class Turtle
{
    public Rect HitBox;
    public bool IsMoving;
    public bool IsDirectionForward;
    public double DistanceWalked;
    public double MetersFalled;
}

class Ground
{
    public Rect HitBox;
}

Then I created a virtual Rect that would simulate the right width and height of the ground and the turtle only after using Inflate function.
public MainWindow()
{
    InitializeComponent();

    ground1 = new Ground();
    ground1.HitBox = new Rect(groundImg1.Margin.Left, groundImg1.Margin.Top, groundImg1.Width, groundImg1.Height);
    ground1.HitBox.Inflate(0, -35);

    zelvicka = new Turtle();
    zelvicka.IsMoving = true;
    zelvicka.IsDirectionForward = true;

    timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromMilliseconds(20);
    timer.Tick += Update;
    timer.Start();
}

I've done the same with the turtle, whose virtual Rect is updating every time it moves in Update function generated by DispatcherTimer so the virtual Rect is always up to date.
    private void Update(object sender, EventArgs e)
    {
        zelvicka.HitBox = new Rect(turtleImg.Margin.Left, turtleImg.Margin.Top, turtleImg.Width, turtleImg.Height);
        zelvicka.HitBox.Inflate(-110, -30);

        // ZELVICKA IS GOING FORWARD AND IS TOUCHING THE GROUND
        // ZELVICKA IS GOING BACKWARDS AND IS TOUCHING THE GROUND
        if (zelvicka.IsMoving && zelvicka.IsDirectionForward && zelvicka.HitBox.IntersectsWith(ground1.HitBox))
        {
            turtleImg.Margin = new Thickness(zelvicka.DistanceWalked += 3, zelvicka.MetersFalled, 0, 0);
        }
        else if (zelvicka.IsMoving && !zelvicka.IsDirectionForward && zelvicka.HitBox.IntersectsWith(ground1.HitBox))
        {
            turtleImg.Margin = new Thickness(zelvicka.DistanceWalked -= 3, zelvicka.MetersFalled, 0, 0);
        }
    }

Rate this code, please, and suggest improvements, if any.


texts: Stack - Implemented as Singly Linked List

texts:
Note: I do know that Python libraries provide a Linked List and Stack. This implementation has been done to practice Python and some of the data structures and Algorithms.

I have implemented Stack as a Singly Linked-List, feel free to make suggestions.
Note: code works

Targeted Big O:

search: O(n), Push and Pop: O(1)

Methods:

push(value)- for insertion
pop()- for removing
is_empty()- to check if empty
peek()- look at what head holds without poping
stack_search(value)-find a value
length()-get size

Classes:

class Node:
    def __init__(self, value):
        self.data = value
        self.front = None

class Stack:
    def __init__(self):
        self.head = None
        self.count = 0

    def push(self, value):
        #make new node
        new_node = Node(value)
        self.count += 1

        if self.head is not None:
            #make new node point to the old node
            new_node.front = self.head
        # make head point to the new element
        self.head = new_node

    def is_empty(self):
        if self.head is None:
            return True
        else:
            return False

    def pop(self):
        if not self.is_empty():
            self.count -= 1
            temp = self.head.data
            # make head point to the node after the node that will be poped
            self.head = self.head.front
            return temp
        else:
            print("can not pop items from an empty list")

    def peek(self):
        return self.head.data

    def __iter__(self):
        # start from the node that head refers to
        node = self.head
        while node:
            yield node
            node = node.front

    def stack_search(self, value):
        # start from the head
        p = self.head
        while p is not None:
            # make p reference to next node
            if p.front is not None:
                if p.data == value:
                    print("Found value")
                    return p.data
                p = p.front
            else:
                print("fail")
                return 0

    def length(self):
        return self.count

Test:

from stack_Queue import Stack

def main():

    print("-------Test Stack----------")
    print("------Test push-------")
    my_stack = Stack()
    test_list = [1, 2, 3, 4, -2000000, 'a', 500000, 50]

    for i in test_list:
        my_stack.push(i)
    print("-------Push Done------")
    print("-------Dump stack-----")
    for i in my_stack:
        print(i.data)
    print("-----Dump stack Done---")
    print("----check count-------")
    print("should be: 8")
    print(my_stack.length())
    print("-----check count Done--")
    print("-------Test pop $ print remaining stack--------")

    my_stack.pop()
    for i in my_stack:
        print(i.data)
    print("-----Test pop Done-----")
    print("-----Test search-------")
    x = my_stack.stack_search('a')
    print(x)
    print("---Test search Done---")

    print("-----Test pop - full stack & print what is being poped-----")
    while my_stack.length() > 0:
        x = my_stack.pop()
        print(x)
    print("-----Test pop Done-----")
    print("-----Test Empty Stack-----")
    for i in my_stack:
        print(i.data)
    print("---------Done---------")

if __name__ == "__main__":
    main()

Result:

-------Test Stack----------
------Test push-------
-------Push Done------
-------Dump stack-----
50
500000
a
-2000000
4
3
2
1
-----Dump stack Done---
----check count-------
should be: 8
8
-----check count Done--
-------Test pop $ print remaining stack--------
500000
a
-2000000
4
3
2
1
-----Test pop Done-----
-----Test search-------
Found value
a
---Test search Done---
-----Test pop - full stack & print what is being poped-----
500000
a
-2000000
4
3
2
1
-----Test pop Done-----
-----Test Empty Stack-----
---------Done---------

Process finished with exit code 0


texts: Find duplicated text for tasks in MS Project

texts:
My code in MS project VBA, in theory, doesn't do anything major.
It compares the tasks with each other, but it runs super slow.

One run through Temp lasts 15 secs. Try that with 1500-2000 tasks and the macro takes hours.

Sub Laczona()

Application.Calculation = xlManual
Application.ScreenUpdating = False

Dim Temp, Temp2 As Integer

For Temp = 1 To ActiveProject.Tasks.Count

For Temp2 = 1 To ActiveProject.Tasks.Count

If ActiveProject.Tasks(Temp).Text30 = ActiveProject.Tasks(Temp2).Text30 Then
If Temp <> Temp2 Then
ActiveProject.Tasks(Temp).Flag5 = 1
End If
End If

Next Temp2
MsgBox "Praca: " & Temp
Next Temp

MsgBox "Prace Laczone Uzupelnione"
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic

End Sub

Addidional explanation:
The code runs in MS Project 2013. Its purpose is to check whether two tasks have the same text in field Text30. If that is the case, the code sets flag5 to 1.


texts:
I've written a small function which displays a div related to an anchor element using data attributes.
The data attribute is used to match the class of the relevant div which is then displayed whilst the others are hidden.
All very basic, however I'm wondering if there is anyway to improve it? Is there a better solution than using the data attribute? Any best practices?
$(".tabs li a").click(function() {
    var cssClass = "." + $(this).data("class");

    // Add active class to current tab, remove from siblings
    $(this).addClass("active").parent().siblings().find("a").removeClass("active");

    // Find the associated content and show it, hide its siblings
    $(".tabs-content").find(cssClass).show().siblings().hide();

    return false;
});

jsfiddle example here

Edit
This ended up being re-written for production using classes instead of data attributes, like so:
$(".tabs li a").click(function() {
    var cssClass = "." + $(this).attr("class");

    $('.tabs a.active').removeClass("active"); // Find the previously active tab and remove it
    $(this).addClass("active");                // Add active class to current tab
    $(".tabs-content").find('> div').hide();   // Hide the all tab divs
    $(".tabs-content").find(cssClass).show()   // Find the associated div and display it

    return false;
});

Updated jsfiddle


texts: Password protected Joomla administrator folder with Python

texts:
I am trying to make a basic auth password protected folder for Joomla. This script will simply ask for username and password to secure administrator folder in Joomla and creates .htaccess and .pass_file.

I want you to review it and offer suggestions so that I can do it more efficiently and learn to manage code.

#!/usr/bin/env python
import os
import pwd
import sys

def ok():
    os.chdir("/home/" + username);
    os.system('htpasswd -c .pass_file '+ user);
    os.chdir("/home/" + username + "/public_html/administrator/");
    os.system('echo AuthUserFile /home/' + username + '/.pass_file > .htaccess');
    os.system('echo AuthName Restricted >> .htaccess');
    os.system('echo AuthType Basic >> .htaccess');
    os.system('echo require valid-user >> .htaccess');
    print "=" *40;
    quit();

username = raw_input("Enter your cpanel username: ");
try:
    pwd.getpwnam(username);
except KeyError:
    print "User not found.";
    quit();
print "=" * 40;
user = raw_input("Enter the username you want to create for authentication: ");
print "=" * 40;
print "Your username will be "+ user +" for authentication";
print "=" * 40;
filename="/home/"+username+"/public_html/administrator/.htaccess";
if os.path.isfile(filename):
    yes = set(['yes','y', 'ye', ''])
    no = set(['no','n'])
    response = raw_input("Filename exists. Overwrite??");
    ans = response.lower();
    if ans in no:
        print "Sorry we didn't secure the administrator folder. :(";
        quit();
    elif ans in yes:
        ok();
    else:
        sys.stdout.write("Please respond with 'yes' or 'no'. Quiting...");
quit();


texts: Handling two buttons that submit a form using AJAX

texts:
I am currently handling forms by binding submit event and then calling the ajax() function:

// Code to handle add post form

$('#add_post').on('submit', function (e) {
    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: "add",
        dataType: 'json',
        data: $(this).serialize(),
        success: function (data) {},
        error: function (data) {}
   })

// Code to handle update post form

$('#edit_post').on('submit', function (e) {
    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: "edit",
        dataType: 'json',
        data: $(this).serialize(),
        success: function (data) {},
        error: function (data) {}
   })

})

As you can see I have a lot repeating code. How can I refactor my code?


texts: Is this IF ok? Or is there a better way?

texts:
So, I wanted to avoid an

if (...) { }
else if (...) { }
else { }

scenario to avoid duplicating code. After a little playing, I got the following working (works from the testing i've done). Am wondering if anyone has any feedback on this approach or suggestions on on possible improvement?

Sometimes I think I know what I'm doing, sometimes I don't.

<?php

  // define("MEMBER_LIMIT",0); // no member limit
defined("MEMBER_LIMIT",100); // limit to 100 ppl in group

  // $memcount is set via sn SQL query
$memcount = 9; // number of people currently in group (let's say).

if (!defined("MEMBER_LIMIT") ||
 ((defined("MEMBER_LIMIT") && MEMBER_LIMIT == 0) ||
  (MEMBER_LIMIT > 0 && $memcount>0 && $memcount < MEMBER_LIMIT))) {
  // will do something
}
else {
  // will do something else.
}
?>


texts: Swift Attempt To Reduce Massive View Controller

texts:
In an effort to reduce the size of a View Controller class I am wondering if this structure is an acceptable, safe way to modify the UI on a Controller Class.

The only thing that I see "wrong" with this approach is not being able to set my outlets or variables to private which is usually very import in order to minimize cohesion and keep the files conforming to Single Responsibility Principle

// In File ViewController.swift

final class ViewController: UIViewController {
    @IBOutlet weak var myButton: UIButton
    @IBOutlet weak var mylabel : UILabel
    @IBOutlet weak var myview  :  UIView

    private lazy var viewModel: VCViewModel = {
        return VCViewModel(main: self)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        initializeUI()
    }

    func initializeUI() {
        viewModel.configureMyButton()
        viewModel.configureMyLabel()
        viewModel.configureMyView()
    }

    ...
}

// Seperate File VCViewModel.swift

final class VCViewModel {
    private main: UIViewController

    init(main: UIViewController) {
        self.main = main
    }

    func configureMyButton() {
        main.myButton.titleLabel.title = "This class configures all UI related code"
    }

    func configureMyLabel() {
        main.myLabel.backgroundColor = UIColor.green
    }

    func configureMyView() {
        main.myLabel.isHidden = true
    }
}


texts: Build a binary-heap from an array, which method is more efficient and why

texts:
I am learning about heaps, and I have found two ways of building them from a given array: I am trying to build up a MAX Heap.

1. Top-down approach

Here I check for every element to see if it is at the correct position or not. By using a function called restoreUp, in which every key is compared to its parent key, and if the parent key is smaller than the parent key is moved down. This procedure continues until the parent key is greater. I check it for every key starting at index position 2.

My code is:

void restoreUp(int arr[],int i)
{
    int k=arr[i];
    int par=i/2;
    while(arr[par]<k)
    {
        arr[i]=arr[par];
        i=par;
        par=i/2;
    }
    arr[i]=k;
}
void buildheap1(int arr[],int size)
{
    int i;
    for(i=2;i<=size;i++)
       restoreUp(arr,i);
}

2. Bottom-up approach

Here I start from the first non-leaf node present at index floor(size/2), and call a function restoreDown until node number 1. I compare a key with both its left and right child and then the greater child is moved up. If both children are greater than the key, then I move the larger of the two children up. This procedure stops when both children are smaller than the key.

My code is:

void restoreDown(int arr[],int i,int size)
{
    int left=2*i;
    int right=2*i+1;
    int num=arr[i];
    while(right<=size)
    {
        if(num>arr[left] && num>arr[right])
        {
            arr[i]=num;
            return;
        }
        else if(arr[left]>arr[right])
        {
            arr[i]=arr[left];
            i=left;
        }
        else
        {
            arr[i]=arr[right];
            i=right;
        }
        left=2*i;
        right=2*i+1;
    }
    if(left==size && arr[left]>num)
    {
        arr[i]=arr[left];
        i=left;
    }
    arr[i]=num;
}
void buildheap2(int arr[],int size)
{
    int i;
    for(i=size/2;i>=1;i--)
       restoreDown(arr,i,size);
}

Both the methods are working for me. Which method is more efficient and why?


texts: Python startswith and then look a word in a conditional block

texts:
I have below working code which works fine, but just looking way around if there is an another elegant way of doing this, because here i'm using else condition to print the last line or dataBlock to be printed.

Note: mydudalt1 and mydudalt2 are the host names

My data File:

$ cat mydata_test.txt
-- mydudalt1 --
            192.168.2.40;       // udalt-sanjose
            192.168.2.56;       // udalt-sj1
            192.168.98.71;        // udalt-japan
            192.168.2.146;      //udalt-sj-fwd1

-- mydudalt2 --
      199.43.4.70;  // udalt-chelms
      192.168.2.56; // udalt-sj1
      192.168.98.71; // udalt-japan

My Working code:

#!/grid/common/pkgs/python/v3.6.1/bin/python3
dataBlock = ''
with open('mydata_test.txt', 'r') as frb:
    for line in frb:
        line = line.strip("\n")

        if line.startswith('--'):
            if "japan" in dataBlock:
                print(dataBlock)
            dataBlock = ''
            dataBlock = dataBlock +  line

        elif "japan" in line:
            dataBlock = dataBlock +  line
    else:
        print(dataBlock +  line)

Resulted Output:

-- mydudalt1 --            192.168.98.71;        // udalt-japan
-- mydudalt2 --      192.168.98.71; // udalt-japan


texts: Custom implementation of the linq Zip operator for different length lists

texts:
Based on my answer I have my implementation of linq Zip operator which operates on different length lists, and loops shortest list.

My implementation:

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

namespace SO
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            List<String> listA = new List<string> {"a", "b", "c", "d", "e", "f", "g"};
            List<String> listB = new List<string> {"1", "2", "3"};

            var mix = listA.ZipNew(listB, (l1, l2) => new[] {l1, l2}).SelectMany(x => x);

            foreach (var m in mix)
            {
                Console.WriteLine(m);
            }
        }
    }

    public static class Impl
    {
        public static IEnumerable<TResult> ZipNew<TFirst, TSecond, TResult>(
            this IEnumerable<TFirst> first,
            IEnumerable<TSecond> second,
            Func<TFirst, TSecond, TResult> resultSelector)
        {
            using (IEnumerator<TFirst> iterator1 = first.GetEnumerator())
            using (IEnumerator<TSecond> iterator2 = second.GetEnumerator())
            {
                var i1 = true;
                var i2 = true;
                var i1Shorter = false;
                var i2Shorter = false;
                var firstRun = true;

                while(true)
                {
                    i1 = iterator1.MoveNext();
                    i2 = iterator2.MoveNext();

                    if (!i1 && (i1Shorter || firstRun))
                    {
                        iterator1.Reset();
                        i1 = iterator1.MoveNext();
                        i1Shorter = true;
                        firstRun = false;
                    }

                    if (!i2 && (i2Shorter || firstRun))
                    {
                        iterator2.Reset();
                        i2 = iterator2.MoveNext();
                        i2Shorter = true;
                        firstRun = false;
                    }

                    if (!(i1 && i2))
                    {
                        break;
                    }

                    yield return resultSelector(iterator1.Current, iterator2.Current);
                }
            }
        }
    }
}

And I wonder if this implementation could be improved somehow, what could be improved, for better readability or speed.


texts: Server side validation for reCAPTCHA V2 or Invisible reCAPTCHA with Java (Servlet)

texts:
The main reason for this post is that I spent quite a lot of time searching the easiest way to validate recaptcha V2. So I'm going to share my knowledge to avoid further time wastage of developers.

I'm using org.json library for this. Get the .jar file from here or read the docs. Add the .jar. file to your project and import the following classes.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONObject;

Use the following method to for validation:

/**
 * Validates Google reCAPTCHA V2 or Invisible reCAPTCHA.
 * @param secretKey Secret key (key given for communication between your site and Google)
 * @param response reCAPTCHA response from client side. (g-recaptcha-response)
 * @return true if validation successful, false otherwise.
 */
public static boolean isCaptchaValid(String secretKey, String response) {
    try {
        String url = "https://www.google.com/recaptcha/api/siteverify?"
                + "secret=" + secretKey
                + "&response=" + response;
        InputStream res = new URL(url).openStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(res, Charset.forName("UTF-8")));

        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        String jsonText = sb.toString();
        res.close();

        JSONObject json = new JSONObject(jsonText);
        return json.getBoolean("success");
    } catch (Exception e) {
        return false;
    }
}

Call the above method as shown:

if(isCaptchaValid("enter_your_key_here", request.getParameter("g-recaptcha-response"))){
    //valid
}


texts:
Here I have a method for reading the contents of a file into a buffer and returning whether or not it is successful.

Here is what I would like reviewed:

Speed:  Am I reading in the file as fast as possible?
Bugs: Are there cases where my code could fail or give unexpected behavior?
Syntax/Styling:  How does my code look?  How could I make it look better?  Anything I'm doing wrong syntax-wise?

int getFileContents(const char *file, void **contents, size_t *size)
{
    // Open file for reading
    int fd = open(file, O_RDONLY);
    if (fd < 0) return -1;

    off_t total = lseek(fd, 0, SEEK_END);
    if (total < 0) return -1;

    lseek(fd, 0, SEEK_SET);

    ssize_t n, rest = total, readb = 0;
    unsigned char buf[BUF_SIZE];
    unsigned char *result = NULL;

    // Read the whole file into memory
    while (rest > 0) {
        n = read(fd, buf, BUF_SIZE);
        if (n < 0) {
            close(fd);
            free(result);
            return -1;
        }

        unsigned char *tmp = (unsigned char*) realloc(result, readb + n);
        if (!tmp) {
            close(fd);
            free(result);
            return -1;
        }

        result = tmp;
        memcpy(result + readb, buf, n);
        readb += n;
        rest -= n;
    }

    close(fd);
    *contents = result;
    *size = total;

    return 0;
}


texts: Can this secure, random generator be improved?

texts:
This random generator uses cryptographically secure numbers/chars instead of Math.random(). The Javascript code with jQuery works well but I affect clean code ;) It would be great if you could help me to optimize the code (e.g. in speed).

(function () {
    var $length, $result, $new, $chars;

    $(document).ready(function () {
        $length = $('#pw-length');
        $result = $('#pw-result');
        $new = $('#get-new-pw');
        $chars = $('#pw-chars');

        // display the result on page load
        getRandomString();
        // generate new password
        $new.click(function () {
            getRandomString();
            return false;
        });

        // allow only numbers in the length-input-field
        $length.on('keyup focusout', function () {
            $length.val($length.val().replace(/[^0-9]/g, ''));
        });
    });

    function randomString(length) {
        var charset = $chars.val(),
            length = parseInt($length.val()),
            i,
            result = "";

        // First we're going to try to use a built-in CSPRNG
        if (window.crypto && window.crypto.getRandomValues) {
            values = new Uint32Array(length);
            window.crypto.getRandomValues(values);

            for (i = 0; i < length; i++) {
                result += charset[values[i] % charset.length];
            }
        }

        // IE calls it msCrypto
        else if (window.msCrypto && window.msCrypto.getRandomValues) {
            values = new Uint32Array(length);
            window.msCrypto.getRandomValues(values);

            for (i = 0; i < length; i++) {
                result += charset[values[i] % charset.length];
            }
        }

        // -> No built-in functionality -> use the function Math.random()
        else {
            for (i = 0; i < length; i++) {
                result += charset[Math.floor(Math.random() * charset.length)];
            }
        }

        return result;
    }

    // Display the result
    function getRandomString() {
        $result.html(randomString(10));
    }
}());

I also uploaded it here for testing.
And of course the corresponding HTML:

<input type="text" id="pw-chars" value="AaBbCcDdEeFfGgHhiJjKkLMmNnoPpQqRrSsTtUuVvWwXxYyZz23456789!?$%#&@^+-~*=_.,:;()" />
<input type="text" id="pw-length" value="10" />
<button id="get-new-pw">New</button>


texts:
I've recently wanted to make a "Loading..." display in C where the dots print one at a time in order and then reset:

Suprisingly, there isn't much on the internet for doing this well, so I figured I would make a simple program for it.

#include <stdio.h>
#include <time.h>

int main(void)
{
    int msec = 0;
    const int trigger = 500; // ms
    const int printWidth = 4;
    int counter = 0;
    clock_t before = clock();

    while (1)
    {
        fputs("Loading", stdout);
        clock_t difference = clock() - before;
        msec = difference * 1000 / CLOCKS_PER_SEC;
        if (msec >= trigger)
        {
            counter++;
            msec = 0;
            before = clock();
        }
        for (int i = 0; i < counter; ++i)
        {
            fputc('.', stdout);
        }
        for (int i = 0; i < printWidth - counter; ++i)
        {
            fputc(' ', stdout);
        }
        fputc('\r', stdout);
        fflush(stdout);

        if (counter == printWidth)
        {
            counter = 0;
        }
    }
}

The output given is seen in the .gif above.

I know this can be done better.  Any suggestions for improvement?


texts: Weighted Probabilities with Integers for Game

texts:
The idea here is to pick a skill at random for the player each round.  Four skills will be chosen, they are all random, and the same skill can repeat multiple times.

The player has a skill level for each ability.  The higher the skill level, the greater the chance that the skill will be chosen.

Rather than use floats and percentages, I have created this solution based on ranges and integers. The readability of the code is what I am concerned with here.  Does it make sense from looking at it?  Would more comments help, or should I use a different approach?

I am using this in a libGDX game, so I am limited to Java 6.

private Ability getRandomAbility() {
    final Map<BZRange, Ability> abilityRanges = new HashMap<BZRange, Ability>();
    int count = 0;
    for (Ability ability : this.skillLevels.keySet()) {
        int abilityLevel = this.skillLevels.get(ability);
        BZRange abilityRange = new BZRange(count, count + abilityLevel);
        abilityRanges.put(abilityRange, ability);
        count += abilityLevel;
        //increase the count by an extra one so that the ranges dont overlap
        count++;
    }

    int randomNumber = this.random.nextInt(count);
    for (BZRange range : abilityRanges.keySet()) {
        if (range.contains(randomNumber)) {
            return abilityRanges.get(range);
        }
    }

    return null;
}

Here is the BZRange class, it is very simple:

public class BZRange {

    private int low;
    private int high;

    public BZRange(int low, int high){
        this.low = low;
        this.high = high;
    }

    public boolean contains(int number){
        return (number >= this.low && number <= this.high);
    }
}


texts:
I have a method which loads data from a remote app (send TCP request and parse response). Now, I have a simple class for sending a TCP request:

public class PremieraTcpClient
    {
        public PremieraTcpClient()
        {
            QueryItems = new NameValueCollection();
            int port;
            int.TryParse(ConfigurationManager.AppSettings["PremieraPort"], out port);
            Port = port;
            ServerIp = ConfigurationManager.AppSettings["PremieraServerIp"];
            ServiceId = ConfigurationManager.AppSettings["PremieraServiceId"];
        }
        public NameValueCollection QueryItems { get; set; }

        private int Port { get; set; }

        private string ServerIp { get; set; }

        private string ServiceId { get; set; }

        private string ReadyQuery { get; set; }

        public string SendQuery()
        {
            StringBuilder parameters = new StringBuilder();

            //...
            // build query for request
            //...
            ReadyQuery = parameters.ToString();
            return Connect();
        }

        private string Connect()
        {
            string responseData;

            try
            {
                TcpClient client = new TcpClient(ServerIp, Port);
                client.ReceiveBufferSize = Int32.MaxValue;

                Byte[] data = Encoding.GetEncoding(1251).GetBytes(ReadyQuery);

                NetworkStream stream = client.GetStream();

                // send data
                stream.Write(data, 0, data.Length);

                var sizeBuffer = new byte[10];

                stream.Read(sizeBuffer, 0, 10);
                var sizeMessage = int.Parse(Encoding.GetEncoding(1251).GetString(sizeBuffer, 0, 10));

                 data = new Byte[sizeMessage];

                var readSoFar = 0;
                //read data
                while (readSoFar < sizeMessage)
                {
                    readSoFar += stream.Read(data, readSoFar, data.Length - readSoFar);
                }

                responseData = Encoding.GetEncoding(1251).GetString(data, 0, data.Length);
                responseData = responseData.TrimStart('&');
                stream.Close();
                client.Close();
                return responseData;
            }
            catch (ArgumentNullException e)
            {
                //return responseData = string.Format("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                //return responseData = string.Format("SocketException: {0}", e);
            }
            return string.Empty;
        }
    }

This is method for load data:

 private static void GetUpdatesFromPremiera()
        {
            Debug.WriteLine(DateTime.Now + ":GetUpdatesFromPremiera");

            PremieraTcpClient client = new PremieraTcpClient();
            client.QueryItems.Add("QueryCode", QueryCode.GetUpdates.ToString());
            client.QueryItems.Add("ListType", "Movie;Hall;Session;Place;Delete");
            client.QueryItems.Add("Updates", _lastUpdateId);
            _lastUpdateId = String.Empty;
            var response = client.SendQuery();

            // here parse response
            //...
        }

This code works fine. But, now I have to load data from two remote app (tomorrow may be three).

The simple solution is to iterate through all remote apps:

private static void GetUpdatesFromPremiera()
{
   foreach(var remoteApp in listRemoteApp)
   {
        PremieraTcpClient client = new PremieraTcpClient();
        // here assigned different properties
        var response = client.SendQuery();
   }
}

Is there is a better way of doing it? Also, each time a connection is established, I think it impacts performance greatly.


texts:
I've got a system and every object in my system has a base class with property: DateTime DateCreated

In the constructor for the base class I set the DateCreated as below:

public EntityBase()
{
    Active = true;
    DateCreated = DateTime.UtcNow;
}

This has been fine but now I'm exposing my data as json via a web service call. As the date are UTC I want the format to have the Z suffix as below:

2016-08-01T10:39:18Z

The DateCreated in each object has Kind set to Utc because I'm setting it using DateTime.UtcNow. However, as I load the data from the database the Kind remains as Unspecified (I assumed that the UTC part would be saved to the database but it appears not). When I return the dates via the web service function I get it without the Z suffix so the calling application doesn't know it is UTC:

2016-08-01T10:39:18

It seems weird to me that the UTC Kind of the DateTime object isn't saved to the database - I thought it would be. The datatype is datetime2(7).

To get the date returned as required I've changed the property in the base class to this:

private DateTime _DateCreated;
[DisplayName("Date Created")]
public DateTime DateCreated
{
    get { return _DateCreated; }
    set
    {
        _DateCreated = value;
        _DateCreated = DateTime.SpecifyKind(_DateCreated, DateTimeKind.Utc);//ensure this is set
    }
}

This does exactly what I need but seems kind of hacky - Is this really the best way?

After Googling someone suggests that using DateTimeOffset would be more appropriate. I'm not convinced? This would have me questioning the use of DateTime in any scenario - and always using DateTimeOffset!?!


texts:
Is this the correct implementation of Heap Sort using Java? How can it be improved further?

import java.util.Arrays;

public class HeapSort {

    public static int heapSize;
    public static int LEFT(int i)
    {
        //returns left index of a zero index based array
        return 2*i+1;
    }

    public static int RIGHT(int i)
    {
        //returns right index of a zero based array
        return 2*i+2;
    }

    public static void BUILD_MAX_HEAP(int A[])
    {
        heapSize = A.length;//heap size initialised
        for(int i=A.length/2; i>=0;i--)//since n/2, n/2+1 ... are leaves we can start from n/2 step downwards
        {
            //call MAX_HEAPIFY on each root node starting from n/2
            MAX_HEAPIFY(A, i);
        }
    }

    public static void MAX_HEAPIFY(int A[],int i)
    {
        int l=LEFT(i);//the left element's index which is 2*i+1 (for zero based indexed array)
        int r=RIGHT(i);//right index = 2*i+2;
        int largestElementIndex = -1;//index can't be negative so initialise largest index , it will be used later
        //heapSize is the current size of the heap being worked upon
        //check if left index lies within the heap.
        //if element at left index is greater than root(A[i]) element, max heap property is violated
        //copy the index of violated element in largestElementIndex
        if(l<heapSize && A[l]>A[i]){
            largestElementIndex = l;
        }
        else //if max heap property is not violated copy the root's index in largestElementIndex
        {
            largestElementIndex=i;
        }
        //check to see the right sub tree for max heap property violation
        //here the largestElementIndex is calculated from previous step
        if(r<heapSize && A[r]>A[largestElementIndex])
        {
            largestElementIndex = r;
        }
        //if root doesn't has the largest index then swap the largest element with root element

        if(largestElementIndex!=i)
        {
            int temp = A[i];
            A[i]=A[largestElementIndex];
            A[largestElementIndex]=temp;
            //after swap, recursively call the MAX_HEAPIFY on the largest index(root element)
            MAX_HEAPIFY(A, largestElementIndex);
        }
    }

    public static void HEAP_SORT(int A[])
    {
        //max heap is built with heapSize initialised
        BUILD_MAX_HEAP(A);
        //starting from end loop through entire array
        for(int i=A.length-1;i>=0;i--)
        {
            //since heap is already heapified and maintains max heap property
            //the first element of the array is root of max heap
            //swap it with the extreme element of the array i.e. setting the largest element to the end of array
            int temp = A[0];
            A[0]=A[i];
            A[i]=temp;
            //reduce the heap window by 1
            heapSize  = heapSize-1;
            //call max heapify on the reduced heap
            MAX_HEAPIFY(A,0);
        }
    }

    public static void main(String[] args) {

        int A[] = new int[]{4,1,3,2,16,9,10,14,8,7};
        HEAP_SORT(A);
        System.out.println(Arrays.toString(A));
    }
}


texts: Augmented training batch generation using `tensorflow.data`

texts:
This is my code for generating augmented training batches in Tensorflow

# x_image is the input image tensor
# y_true is the input onehot tensor

# The input tensors to the batch generator.
# These will be fed with the entire training set
images_tensor = tf.placeholder(tf.float32, shape=self.x_image.shape)
onehots_tensor = tf.placeholder(tf.float32, shape=self.y_true.shape)

# Create a dataset object
data = tf.data.Dataset.from_tensor_slices((images_tensor, onehots_tensor))

# Augment the images
data = data.map(lambda x, y: (self.augment_fn(x), y), num_parallel_calls=32)

# Shuffle them and repeat (images_train is actual data)
data = data.apply(tf.contrib.data.shuffle_and_repeat(len(images_train), None))

# Create a batch
data = data.batch(batch_size)

# Pre-fetch a batch worth of data
data = data.prefetch(batch_size)

# Create batch iterator
iterator = data.make_initializable_iterator()
next_element = iterator.get_next()
init_op = iterator.initializer

# Initialise the generator
# images_train and onehot_train are the actual training data (numpy)
session.run(init_op, feed_dict={images_tensor: images_train, onehots_tensor: onehot_train})

Now inside the training loop I get new batches like:

X, y = session.run(next_element)

Questions:

I put the prefetch size to the batch_size. What would be the point of having more or less? Some examples I see have prefetch size only 5 etc.
Similarly, I put the randomisation size to that of the whole training set, so that the batches are as random as possible. Is there any case for using less than this? Maybe if memory is a problem and the images are being loading from disk?


texts:
Task: Do DFS traversal from a given given graph and print output.

Is this code the correct implementation of DFS traversal in Graph?

from collections import defaultdict

class Graph(object):
    def __init__(self):
        self.graph = defaultdict(list)

    def add_edge(self, u, v):
        self.graph[u].append(v)

    def depth_first_search(self,node):
        visited = []
        stack = [node]

        while stack:

            node = stack.pop()
            if node not in visited:
                print node
                visited.append(node)

                for i in self.graph[node]:
                    stack.append(i)

G = Graph()
G.add_edge(1,2)
G.add_edge(1,4)
G.add_edge(1,5)
G.add_edge(2,3)
G.add_edge(3,5)
G.depth_first_search(1)


texts: Remapping Heading Angle to Coordinates to Move By

texts:
I wrote the following code to take the heading of an object, and determine which direction (x,y) it needs to move to be heading forward in a 2D plane. I have debugged the code and know that it functions as expected, but as far as readability/maintainability is concerned, I feel like it could be better. Thoughts/opinions?

 /*
 *  map($orgLow, $orgHigh, $newLow, $newHigh, $value)
 *  Take existing range of values and remap to new range of values
 *  Requires original high range value, original low range value
 *    new high range value, new low range value, value to be mapped
 *  Returns newly mapped integer value
*/
function map($orgLow, $orgHigh, $newLow, $newHigh, $value) {
    return $newLow + ($value - $orgLow) * ($newHigh - $newLow) / ($orgHigh - $orgLow);
}

/*
 *  coordsToMoveFromDeg($deg)
 *  Take heading and return movement on a (-1 to 1, -1 to 1) plane
 *  Requires heading to be scaled
 *  Optional precision to be given
 *  Returns array consisting of x and y values to change by
*/
function coordsToMoveFromDeg($deg, $precision = false) {

    //Rotate plane 90º CCW by remapping the degree value
    $xdeg = map(0,360,-90,270,$deg);
    if ($xdeg < 0) {
        //When xdeg is negative, add 360 to get proper angle
        $xdeg += 360;
    }
    if ($xdeg > 180) {
        //Remap values larger than 180 to their mirrored
        //value prior to getting final movement values
        $xdeg = map(180,360,180,0,$xdeg);
    }
    $ydeg = $deg;
    if ($ydeg > 180) {
        //Remap values larger than 180 to their mirrored
        //value prior to getting final movement values
        $ydeg = map(180,360,180,0,$deg);
    }
    //Remap x and y to how much to move by, -1 to 1
    $x = map(180,0,-1,1,$xdeg);
    $y = map(180,0,-1,1,$ydeg);

    //Show equal amount of decimal points always for
    //easier readability if precision is set, recommended
    //to only be set when in debug environment
    if ($precision) {
        $x = number_format($x,$precision);
        $y = number_format($y,$precision);
    }

    return array('x'=>$x,'y'=>$y);
}

//Show the mapped values for all degrees value 0 to 359 (360 = 0)
for ($i = 0; $i < 360; $i++) {
    var_dump(coordsToMoveFromDeg($i,4));
}


texts:
I'm reviewing MIT Introduction to Algorithm lectures/exercises and am trying to implement a one dimensional peak finder algorithm.

I've got a working copy but it's a bit messy and I've had to put some array size constraints to get it working properly. How can I improve this implementation?

def peakfinder(arr):
    if len(arr) == 0: # If list is 0, there is no peak.
        return None

    if len(arr) == 1: # If list has been reduced to 1 element, it's a peak.
        return arr

    mid = len(arr) / 2

    left  = mid - 1
    right = mid + 1

    if arr[left] <= arr[mid] >= arr[right]:
        return arr[mid] # Base case. This is a peak.

    if arr[mid] < arr[left]: # Look to left side of array for peak.
        return peakfinder(arr[:mid])

    if arr[mid] < arr[right]: # Look to right side of array for peak.
        return peakfinder(arr[mid+1:])