Harrington Web

Reverse a number without converting to a string.

Saturday, April 29, 2017

Sometimes I like to work on coding problems just for fun. Things like Project Euler, Code Eval Hackerrank etc. Depending on what language you decide to do things in it might be easier and more efficient to think a little different.

Some languages like Ruby, for example, converting a number to a string with to_str() and then reversing is rather trivial. But what if I want to solve it by keeping the number a number?

Currently, I am playing around with Rust so my example will be in that language however you can use this same formula in whatever language you like.

pub fn num_reverse(number: i32) -> i32 {
    let mut num = number;
    let mut rev = 0;
    
    while num > 0 {
        rev = rev * 10 + (num % 10);
        num = num / 10;
    }

    return rev;
}
Now that we have the code lets map out a little bit of how it works. Let us take the number 1234.
num = 1234; rev = 0;

Grab the first digit.

rev = rev * 10 + (num % 10)

Which can be written as.

rev = 0 * 10 + (1234 % 10)
rev = 0 + 4

Then reduce num.

num = num / 10
num = 1234 / 10
num = 123

Notice it was not 123.4, we are keeping these as integers and the divide will treat them as numbers without decimal places.

num = 123; rev = 4;

Grab the next digit.

rev = rev * 10 + (num % 10)

Which can be written as.

rev = 4 * 10 + (123 % 10)
rev = 40 + 3

Then reduce num.

num = num / 10
num = 123 / 10
num = 12
num = 12; rev = 43;

Grab the next digit.

rev = rev * 10 + (num % 10)

Which can be written as.

rev = 43 * 10 + (12 % 10)
rev = 430 + 2

Then reduce num.

num = num / 10
num = 12 / 10
num = 1
num = 1; rev = 432;

Grab the next digit.

rev = rev * 10 + (num % 10)

Which can be written as.

rev = 432 * 10 + (1 % 10)
rev = 4320 + 1

Then reduce num.

num = num / 10
num = 1 / 10
num = 0
num = 0; rev = 4321;

Now since num is 0 we can end the while loop and return rev.

Remote API useful tricks for Docker on Mac OS X

Tuesday, April 18, 2017

Install Socat

Socat allow you to forward ports to a variety of things. For Docker forwarding a port to the docker.sock file makes a quick way to get access to the Docker remote api.

Docker also allow a way to open up that TCP port however on Mac it is temperamental and hard to use

brew install socat
socat -d TCP-LISTEN:2375,reuseaddr,fork UNIX:/var/run/docker.sock &

Create an IP alias

Running Docker containers locally is a fairly common thing. Interconnectivity between containers works naturally. However, sometimes you may also want a container to connect back to the Docker Remote API. If you are constantly on the go changing networks you do not want to have to change that IP address every time.

A great use case for this is running Jenkins and using docker slaves

sudo ifconfig lo0 alias 192.168.99.50

Remote API

There are also some interesting endpoints you can use for information

curl http://localhost:2375
curl http://localhost:2375/containers/json
curl http://localhost:2375/images/json