The Sky Is The Limit

Reaching it will give you emptiness

Flogvit

Vegard Hanssen

Coding

If it’s not possible to code, it’s not worth doing.

Music

Playing, recording, producing, mixing, mastering.

Presentation

Backgrounds, text, visualizing

$

Running

Halv marathon is fun

Puzzles

Using my brain is vital

Knowledge

You can never know to much

Chicken Hatch Puzzle Game for Android

We have published a new fun puzzle game called Chicken Hatch. Try to get the hens to hatch the chickens. It is not that hard to find a solution for the puzzle, but finding the 3 stars solution is much harder.

Try it out at Google Play. iOS version will come later.

Can you solve them all with 3 stars?

#nodejs module for parsing key/value strings

Since my regexp test on parsing a string was a success, I created a #nodejs module for parsing strings. And the funny part, I scrapped the regexp 🙂 It now supports multiple keys and multiple values.

So you can do something like this:

var res = ps.parse('"foo bar" bar,"foo bar"=1 bar,"foo bar"=bar,"foo bar"');
res = [
   ['foo bar', undefined],
   ['bar', '1'],
   ['foo bar', '1'],
   ['bar', ['bar', 'foo bar']],
   ['foo bar', ['bar', 'foo bar']]
]

You can find the code at GitHub or as a npm module

#javascript and parsing param string

Got a little challenge parsing strings like:

Little brown="and yellow" fox=1 jumps over=lazy dog

I wanted to split it into an array with key/value pairs, so the result would be:

var res =
[ [ 'Little', undefined ],
  [ 'brown', 'and yellow' ],
  [ 'fox', '1' ],
  [ 'jumps', undefined ],
  [ 'over', 'lazy' ],
  [ 'dog', undefined ] ]

And here the regexp guy in me popped out and wanted to go for a short version. I ended up with

var text = 'Little brown="and yellow" fox=1 jumps over=lazy dog';

var res = text.match(/([^=\s]+="[^"]+")|\S+/g).map(function (p) {
    return p.match(/^([^=\s]*)(?:="?([^"]*)"?)?$/).splice(1, 2);
})

console.log(res);

The first one (/([^=\s]+=”[^”]+”)|\S+/g) splits the string into an array of param|param=value|param=”value with space”. The second one (/^([^=\s])(?:=”?([^”])”?)?$/) split that array into separate arrays which contains key,value and remove the possible double quote on the value. Since match return all the splits, I splice out the values from the new array, 1 and 2.

And there you go.

You can find the last version at GitHub

PS: This rely on that array.map() exists, which it does in #nodejs

@nodejs and object sort speed

I was coding a scheduler and needed a sorted array of the schedules, so I started to investigate the speed of different sorting variants I could use.

I created a little program (https://gist.github.com/flogvit/c9df93ab37ca4dbd2d7f) to test 4 different variants.

For the test I created an array with 100000 objects { v: num }, where num is 100000 to 1. v is the value I want to sort on. Then I did the sort 10 times for each variant, and then again running this 5 times.

The first variant was standard compare (compare):

function compare(a, b) {
  if (a.v < b.v)
    return -1;
  if (a.v > b.v)
    return 1;
  return 0;
}

sortarray.sort(compare);

The second was a value only version doing the minus variant (compareValue):

function compareValue(a, b) {
  return a.v - b.v;
}

sortarray.sort(compareValue);

Third variant is using async (async):

   async.sortBy(sortarray, function (entry, callback) {
      callback(null, entry.v);
    }, function (err, results) {
      sortarray = results;
    });

And last variant is using underscore (underscore):

sortarray = _.sortBy(sortarray, 'v');

The result where smaller is better:

compare 36.00
compareValue 36.58
underscore 50.84
async 228.48

And the full output from each run:

$ node --expose-gc sort.js
Running compare for average 30.8 ms
Running compareValue for average 38 ms
Running async for average 227 ms
Running underscore for average 51.8 ms
Running compare for average 38.8 ms
Running compareValue for average 36.6 ms
Running async for average 230.7 ms
Running underscore for average 49.5 ms
Running compare for average 37.2 ms
Running compareValue for average 35.2 ms
Running async for average 234.7 ms
Running underscore for average 52.6 ms
Running compare for average 37.4 ms
Running compareValue for average 37.1 ms
Running async for average 224.4 ms
Running underscore for average 49.4 ms
Running compare for average 35.8 ms
Running compareValue for average 36 ms
Running async for average 225.6 ms
Running underscore for average 50.9 ms

Solution to Vietnamese pesky snake in #perl

I got a link to the Vietnamese Pesky Snake math problem from my wife. (http://m.db.no/2015/05/21/nyheter/utenriks/matematikk/oppgave/39292707/) And as a programmer, I couldn’t really sit there and solve it with my bare hands. Ok, I admit it’s fun to solve math problems, but this was very much looking like a problem with more than one solution, and I needed to know for sure 🙂

So I created a little program in perl to solve this. It’s located at gist: https://gist.github.com/flogvit/ba6e984236f9b4e08c10

And for those wondering, yes, it had several solutions, actually 128 of them.

%d bloggers like this: