Monday, July 24, 2017

Sparse Numbers

I just completed my Codility test, and one of my solutions ran slowly. Unfortunately, I am unable to find out where the bottleneck might be (its hard to cover all possible test cases within a limited time).

But I think it might be the way I get the next sparse number. My Java implementation is below.

     * @param i assume i to be sparse.
     * @return
    private int getNextSparse(int i) {

        int mask = 0b11;
        int shift = 0;
        while ((i & (mask << shift)) != 0) {

        return (i & (0b11111111_11111111_11111111_11111111 << shift)) | (1 << shift);

I hope to have a YouTube video out to explain all the above soon. (edit: the YouTube vide is here)

Codility Lessons

Recently I've had to do a Codility ( test while applying for a job. It seems to be a pretty popular website (I just been told to do another test today!).

So before I did the test, I tried some of the Tasks in their Lessons, and they were pretty fun. So I'm going to try some of the more interesting looking tasks. I've posted my solutions to GitHub (here) and YouTube (here).

Obviously this is a work in progress, but I'll try to post as many of my solutions as possible.

Monday, May 22, 2017

Spring MVC's ArgumentResolver

Recently a colleague introduced to me the concept of an ArgumentResolver.

In theory, with an ArgumentResolver, one can inject anything into a Spring MVC Controller. They are usually injected as arguments (aka parameters) in the Controller's methods.

So first in the XML configuration we do the below:

            <bean class="com.mycompany.ArgumentResolver"></bean>

Then we implement the class itself:

import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;

public class ArgumentResolver implements HandlerMethodArgumentResolver {
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().equals(MyClass.class);

    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

        // do what you need to return the Argument

Friday, February 10, 2017

An OpenSSL helper

And by that I mean an 'openssl' command line helper. I normally use openssl to create my CSRs and I always have to look up the command line options. Here is a tool that helps me (especially with my very limited use case).

It creates the command command to use with 'openssl' so I don't have to keep checking. Thanks Digicert! If I had my way, I'd use your services, just for this one tool alone.

And after I create the CSR, I usually just check that its done correctly (I don't know why, just obsessive I guess). And I normally have to check that too. So here is the command line.

openssl req -in xxxxxxxxxxxxxxx.csr -noout - text

Tuesday, February 7, 2017

Change the screenshots folder in Mac

This actually works!!

Tuesday, September 20, 2016

Remove iptables rule

I recently had to fool around with IP tables in one of our web instances, and I found this SO link useful. So here it is for everyone (read: myself).

 This happened because Ubuntu had updated some packages and there was a message telling me to restart the instance when I logged in last week. So I foolishly did. As a result, PostgreSQL was broken (/var/run/something was deleted and it took my colleague a good 30 minutes to find that out). And suddenly our Tomcat application wasn't working.

  1. was timing out.
  2. so was
  3. But the our direct IP was working. 
  4. BUT the direct IP wasn't!
  5. Nginx logs were not logging anything at all.
  6. Checking Amazon AWS, I confirmed that both 80 and 8080 were "allowed".

So there's got to be something between AWS and nginx that was blocking port 80, and allowing port 8080. Which was something no human would do.

So I did something I've never had to do before: check iptables. And sure enough I found the below:

So here's the steps I took to wipe out all those rules.
iptables -L INPUT --line-numbers
iptables -D INPUT 5
iptables-save > /etc/iptables/rules.v4

The 2nd line was done a couple of times to remove all the iptables lines.
The last line was so that each time the instance was restarted, the rules would be the same. The rules might be stored in a different file. Check your local distro for details.

Wednesday, September 23, 2015

Resistance Avalon role "assigner"

First select number of players:

  • Merlin
  • Percival
  • Mordred
  • Oberon
  • Morgana (requires Percival)

Number of players

Next shuffle the deck

Then reveal the secret role to 1 player at a time.

Percival - good
Percival's special power is knowledge of Merlin at the start of the game. Using Percival's knowledge wisely is key to protecting Merlin's identity. Adding Percival into the game will make the Good side more powerful.

Mordred - Evil
Mordred's special power is that identity is not revealed to Merlin at the start of the game. Adding Mordred will make the Evil side more powerful.

Oberon - Evil
Oberon's power is that he does not reveal himself to other evil players, and he doesn't know of the other evil players. Merlin does know who Oberon is. Adding Oberon will make the Good side more powerful.

Morgana - Evil
Morgana's special power is she appears to be Merlin. She reveals herself to Percival as Merlin (Percival must be in play). Adding Morgana will make the Evil side more powerful.