воскресенье, 23 декабря 2012 г.

Facebook interview

While I was searching the web, I have met wonderful site. It's http://prepare.hackerearth.com or just MyCareerStack. It is database of questions for job interviewers for most popular IT-companies in Silicon Valley. I decided to solve some problems.

So, Facebook question is:
The beauty of a number X is the number of 1s in the binary representation of X.
Two players are plaing a game. There is number n written on a black board. The game is played as following:
Each time a player chooses an integer number (0 <= k) so that 2^k is less than n and (n-2^k) has as beautiful as n. Next he removes n from blackboard and writes n-2^k instead. The player that can not continue the game (there is no such k that satisfies the constrains) looses the game.
The First player starts the game and they play in turns alternatively. Knowing that both two players play optimally you have to specify the winner.
Input:
First line of the Input contains an integer T, the number of testcase. 0 <= T <= 5.
Then follow T lines, each containing an integer n.
n more than 0 and less than 10^9 +1.
Output
For each testcase print "First Player" if first player can win the game and "Second Player" otherwise.
Sample Input
7 1 2 8 16 42 1000 123
Sample Output
Second Player First Player First Player Second Player Second Player First Player Second Player
Explanation
In the first example n is 1 and first player can't change it so the winner is the second player.
In the second example n is 2, so the first player subtracts 1 (2^0) from n and the second player looses the game.
even :: B odd :: A
I have solved it with this perl-code:

perl -e 'my $s = 8; my $n = 0; my $sum = 0;  while($s) { if ($s&1) {$sum+=$n} else {++$n; }   $s>>=1;  };  print $sum&1 ? "First\n" : "Second\n";  '

Beautiful, isn't it?

воскресенье, 21 октября 2012 г.

NTP server every day ^_^

Since i have tired to see my ubuntu clocks asynced, i have decided to put following code snippet to /etc/rc.local file:

for SERVER in ntp1 ntp2 ntp3 ntp4 ntp21; do
        echo "Trying ${SERVER}.vniiftri.ru to connect"
        if ntpdate ${SERVER}.vniiftri.ru; then
                break
        fi
done


Vniiftri is Russian center of time control.
I assume that there is nothing bad in connecting to ntp*.vniiftri.ru every moment my laptop starts.

Python is nearby!

Recently I've decided, that knowledge of Python will be a good addition to Perl-programmer.
Althoug, it is beautiful (maybe just beacuase it's new to me), it has some big disadvantages. 

One of them is that it is very slow! Yestarday night and tomorrow I had been fighting with problem on codechef:
http://www.codechef.com/problems/INTEST
Nothing helped  me to pass the test. The failure is in the way how Python reads from stdin. It's very slow. Neither raw_input(2.7.2), nor input(3.2.1) passed input test limit. Moreover, I tried os.read() function and again not good news :( . I also found direct method reading from stdin: sys.stdin.readlines(), but it was not helpful.

What can I say? Maybe Python < 2.7.2 really can handle it:

But now in codechef there is Python  interpreters greater, then 2.7.1

One failure is unable spot my mood. But it is not very cozy to know, that upgrading PYTH on your large data-set systems can cause increasing execution of functionality, written on python .