sábado, 14 de abril de 2012

Love to Code?

Just saw a facebook advertisemet with the title "Love to Code?" and couldn't resist the temptation to click on it.   It ended up being a door to facebook recruitement process and I decided to spend some minutes to try to get more information on how their process is.

After clicking the ad you are redirected to a very interesting tool from an external company called InterviewStreet who asked me to solve a simple programming problem in less than an hour in any programming language.

The problem was a slightly modified version of the game of life problem where the pieces at the boundaries never change and I decided to solve it in python. I was slightly disappointed because I was really expecting something more difficult (Tuenti contest problems are more difficult for example).

I was able to finish it in the 1h timeframe but didn't pay too much attention to write a great code so they won't probably call me :-)    Find my code below in case somebody wants to improve it.

#Enter your code here. Read input from STDIN. Print output to STDOUT

def solve(board, size):
  result = [list(row) for row in board]
  pos = [(-1, -1),(-1, 0),(-1, 1),(0, -1),(0, 0),(0, 1),(1, -1),(1, 0),(1, 1)]
  for y in range(1, size-1):
    for x in range(1, size-1):
      blacks = whites = 0
      for dx,dy in pos:
        if 0 <= y+dy < size and 0 <= x+dx < size:
          if board[y+dy][x+dx] == 'b':
            blacks += 1
            whites += 1
      result[y][x] = ('b' if blacks > whites else 'w')
  return [''.join(row) for row in result]

tests = raw_input()
for test in range(int(tests)):
  #read input
  size, iters = raw_input().split(' ')
  board = [''] * int(size)
  for i in range(int(size)):
    board[i] = raw_input()

  for i in range(int(iters)):
    board = solve(board, int(size))

  #write result
  print "Board %d" % (test+1)
  for row in board:
    print row

No hay comentarios:

Publicar un comentario