Last active
October 16, 2016 11:35
-
-
Save ulgens/2860d77d0309e2f14db4 to your computer and use it in GitHub Desktop.
Interview solution for sahibinden.com, from 2014
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# Author: Ülgen Sarıkavak | |
# ulgensrkvk@gmail.com | |
# İstanbul - 2014 | |
# Class definitions | |
class Point(object): | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
def get_location(self): | |
return (self.x, self.y) | |
def move_horizontal(self, change): | |
self.x = self.x + change | |
def move_vertical(self, change): | |
self.y = self.y + change | |
def act1(self): | |
self.move_horizontal(1) | |
print 1, self.get_location() | |
def act2(self): | |
self.move_vertical(2) | |
print 2, self.get_location() | |
def act3(self): | |
self.move_horizontal(-1) | |
self.move_vertical(-1) | |
print 3, self.get_location() | |
def act4(self): | |
self.move_horizontal(-5) | |
print 4, self.get_location() | |
def act5(self, n): | |
self.move_horizontal(n) | |
print 5, self.get_location() | |
def act6(self): | |
self.move_vertical(-1) | |
print 6, self.get_location() | |
def __repr__(self): | |
return repr("Point" + str(self.get_location())) | |
# Helper function for rule#5 | |
def lastitem3(numbers): | |
last = 0 | |
line = 1 | |
x = True | |
while x: | |
try: | |
if numbers[-line] % 3 == 0: | |
last = last + 1 | |
line = line + 1 | |
else: | |
x = False | |
except: | |
x = False | |
return last | |
# Golden Ratio | |
gr = (1 + 5 ** 0.5) / 2 | |
# Calculate exact Fibonacci number with Binet's formula | |
def fib(n): | |
return int(round((gr ** n - (1 - gr) ** n) / 5 ** 0.5)) | |
# Main Variables | |
point = Point(0, 0) | |
numbers = [] | |
ifRule6 = "passive" | |
START = 1 | |
FINISH = 256 | |
for number in range(START, FINISH + 1): | |
number = fib(number) | |
numbers.append(number) | |
print "Number is:", number | |
if ifRule6 == "active": | |
if number % 5 == 0: | |
ifRule6 = "destroyed" # İkinci kez 5in tam katı geldiğinde | |
point.act6() | |
else: | |
point.act6() | |
elif ifRule6 != "active": | |
if number % 5 == 0 and ifRule6 != "destroyed": # İlk kez 5in tam katı geldiğinde | |
ifRule6 = "active" | |
point.act6() | |
else: | |
if number % 2 == 1: # Tek sayı gelirse | |
point.act1() | |
if number % 2 == 0: # Çift sayı gelirse | |
point.act2() | |
if number % 7 == 0: # 7nin tam katı ise | |
point.act3() | |
if number % 9 == 0: | |
if len(numbers) >= 6: | |
if sum(numbers[-6:-1]) % 2 == 1: | |
point.act4() | |
else: | |
if sum(numbers[:-1]) % 2 == 1: | |
point.act4() | |
if number % 3 == 0: | |
point.act5(lastitem3(numbers)) | |
print """" | |
----------------------------- | |
- Pawn is on %s - | |
----------------------------- | |
""" % str(point.get_location()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment