вторник, 1 января 2013 г.

Facebook interviews again

N friends are playing a game. Each of them has a list of numbers in front of himself. Each of N friends chooses a number from his list and reports it to the game administrator. Then the game administrator sorts the reported numbers and shouts the K-th largest number.
You want to know the count all possible numbers that the game administrator can shout.
Input Format:
First line of the input contain an integer T, the number of testcases. Then follow T testcases. For each test case the input is of the following format. In the first line there are two numbers N and K. In each of next N lines there is an integer a_i followed by a_i integers, ith line describes the list for ith person. All the numbers in all the lists are unique.

 My code solves it problem:
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

open(my $f, "test") or die "$!";

my $nums = [];
my (undef, $k) = split(/\s/, <$f>);
while (my $line = <$f>) {
    my ($s, @arr) = split(/\s/, $line);
    @arr = sort {$a <=> $b} @arr;
    push(@$nums, \@arr);
}


my $all = 0;

for (my $i=0; $i<@$nums; $i++) {
    for (my $j=0; $j<@{$nums->[$i]}; $j++) {
        my $number = $nums->[$i]->[$j];

        my $s = 0;
        my $addit = 0;
        my @arr = ();
        L:
        for (my $l=0; $l<@$nums; $l++) {
            next if $l == $i;

            if ($nums->[$l]->[-1] < $number) {
                $s++;
            } elsif($nums->[$l]->[0] > $number) {
                # we cannot encrease
            } else {
                $addit++;
            }
        }

        if ( $s == $k - 1) {
            $all++;
        } elsif (($s < $k - 1) && ($s + $addit) >= $k - 1) {
            $all++;
        }
    }
}

print "Answer: " . ( $all ) . "\n";

close($f);





воскресенье, 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 .

суббота, 1 октября 2011 г.

Shad

Hey, yo! It's been 4 lectures and 4 seminars i have visited this year. Course is named "Algorithm's and data search". Even though I understand not all material while listening this course, it is very interesting.

Yestarday I have listened Binary Search. I didn't thought that there will be so many problems to code that (yep, mr Peter Mitrichev :)).

During the course little by little I starting understand that to code hard tasks you need to
1) have a good mood :)
2) theoretically prove your solve
3) test your solve very hard ;)
4) don't forget about compiler differences
5) shit, again test

Theoretical prove is sociated with some invariant that must be compiled in your mind.

пятница, 11 февраля 2011 г.

воскресенье, 6 февраля 2011 г.

Perl magic

Since i am perl-intern, perl is my second language :)
Some of it is very strange!
For example, pattern /^\w+(\d+)\w$/ match one string blabla01d and doesn't match blabla10d. I know, that \w can match digits also, so I made /^\w+?(\d+)\w$/, so it worked on MY machine. But on other computers first pattern aslo works fine.
So I DON'T KNOW WHAT IS HAPPANING!

Kill me by wall...(c)