Решение на Питоните хапят! от Цанислава Русева

Обратно към всички решения

Към профила на Цанислава Русева

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 7 успешни тест(а)
  • 6 неуспешни тест(а)

Код

class Cell():
def __init__(self, contents=None):
if contents != None and contents.__class__ not in [WorldObject, Food, PythonPart, PythonHead]:
raise TypeError
self.contents = contents
def is_empty(self):
return self.contents == None
def is_food(self):
return self.contents.__class__ == Food
def is_safe(self):
return self.is_empty or self.is_food
def __str__(self):
return ".."
class World():
def __init__(self, width):
self.width = width
self.field = [[Cell() for column in range(0, width)] for row in range(0, width)]
def __len__(self):
return self.width
def __getitem__(self, coord):
return self.field[coord]
def outside_range(self, x, y):
for i in x, y:
if i < 0 or i > self.__len__() - 1:
return True
return False
class Vec2D():
def __init__(self, x=0,y=0):
self.vector = (x, y)
def __add__(self, other):
return Vec2D(self[0] + other[0], self[1] + other[1])
def __sub__(self, other):
return Vec2D(self[0] - other[0], self[1] - other[1])
def __mul__(self, number):
return Vec2D(number*self[0], number*self[1])
def __neg__(self):
return Vec2D(-self[0], -self[1])
def __getitem__(self, key):
return self.vector[key]
class WorldObject():
def __init__(self, coords=Vec2D()):
self.coords = coords
def __str__(self):
object = self.super()
return object + ' at v' + self.coords
class Food(WorldObject):
def __init__(self, coords=Vec2D(), energy=0):
WorldObject().__init__(coords)
self.coords = coords
self.energy = energy
def __str__(self):
return ":" + self.energy
class PythonPart(WorldObject):
def __init__(self, coords):
self.coords = coords
WorldObject().__init__(coords)
def move(self, coords):
self.coords = coords
def __str__(self):
return "##"
class PythonHead(WorldObject):
def __init__(self, coords):
self.coords = coords
WorldObject().__init__(coords)
def move(self, coords):
self.coords = coords
def __str__(self):
return "@@"
class Death(Exception):
pass
class Python:
LEFT = Vec2D(1, 0)
UP = Vec2D(0, 1)
RIGHT = Vec2D(-1, 0)
DOWN = Vec2D(0, -1)
def __init__(self, world, coords=Vec2D(), size=0, direction=Vec2D()):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
self.head = PythonHead(coords)
self.body = []
self.body.append(self.head)
x, y = coords[0], coords[1]
self.world[x][y].contents = self.head
for part in range(0, size):
body_part = PythonPart(coords - direction*(part+1))
self.body.append(body_part)
x, y = body_part.coords
self.world[x][y].contents = body_part
def move(self, direction):
if direction + self.direction == Vec2D():
raise ValueError
x, y = self.coords
position = Vec2D(self.coords)
direct = Vec2D(direction)
coords_new = (position + direct).vector
x_new, y_new = coords_new
if self.world.outside_range(x_new, y_new) or not self.world[x_new][y_new].is_safe:
raise Death
food = self.world[x_new][y_new].is_food
self.head.move(coords_new)
self.coords = x_new, y_new
self.world[x_new][y_new].contents = self.head
coords = self.coords
for part in range(0, self.size):
coords_old = self.body[part].coords
self.body[part].move(coords)
self.world[x][y].contents = self.body[part]
coords = coords_old
if food:
body_part = PythonPart(coords)
self.body.append(body_part)
self.world[x][y].contents = body_part
self.size += 1

Лог от изпълнението

EEEE.EE......
======================================================================
ERROR: test_growth (test.PythonTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20130606-14014-1wvah1d/test.py", line 85, in test_growth
    py.move(Python.LEFT)
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 135, in move
    if self.world.outside_range(x_new, y_new) or not self.world[x_new][y_new].is_safe:
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 33, in outside_range
    if i < 0 or i > self.__len__() - 1:
TypeError: unorderable types: Vec2D() < int()

======================================================================
ERROR: test_move_backwards (test.PythonTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20130606-14014-1wvah1d/test.py", line 103, in test_move_backwards
    py.move(direction)
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 135, in move
    if self.world.outside_range(x_new, y_new) or not self.world[x_new][y_new].is_safe:
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 33, in outside_range
    if i < 0 or i > self.__len__() - 1:
TypeError: unorderable types: Vec2D() < int()

======================================================================
ERROR: test_ouroboros_death (test.PythonTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20130606-14014-1wvah1d/test.py", line 40, in test_ouroboros_death
    py.move(Python.LEFT)
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 135, in move
    if self.world.outside_range(x_new, y_new) or not self.world[x_new][y_new].is_safe:
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 33, in outside_range
    if i < 0 or i > self.__len__() - 1:
TypeError: unorderable types: Vec2D() < int()

======================================================================
ERROR: test_python_movement_basic (test.PythonTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20130606-14014-1wvah1d/test.py", line 29, in test_python_movement_basic
    py.move(direction)
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 135, in move
    if self.world.outside_range(x_new, y_new) or not self.world[x_new][y_new].is_safe:
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 33, in outside_range
    if i < 0 or i > self.__len__() - 1:
TypeError: unorderable types: Vec2D() < int()

======================================================================
ERROR: test_snake_death (test.PythonTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20130606-14014-1wvah1d/test.py", line 64, in test_snake_death
    {py2.move(Python.LEFT) for repeat in range(0, 5)}
  File "/tmp/d20130606-14014-1wvah1d/test.py", line 64, in <setcomp>
    {py2.move(Python.LEFT) for repeat in range(0, 5)}
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 135, in move
    if self.world.outside_range(x_new, y_new) or not self.world[x_new][y_new].is_safe:
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 33, in outside_range
    if i < 0 or i > self.__len__() - 1:
TypeError: unorderable types: Vec2D() < int()

======================================================================
ERROR: test_wallpunch_death (test.PythonTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20130606-14014-1wvah1d/test.py", line 54, in test_wallpunch_death
    {py.move(Python.LEFT) for repeat in range(0, 10)}
  File "/tmp/d20130606-14014-1wvah1d/test.py", line 54, in <setcomp>
    {py.move(Python.LEFT) for repeat in range(0, 10)}
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 135, in move
    if self.world.outside_range(x_new, y_new) or not self.world[x_new][y_new].is_safe:
  File "/tmp/d20130606-14014-1wvah1d/solution.py", line 33, in outside_range
    if i < 0 or i > self.__len__() - 1:
TypeError: unorderable types: Vec2D() < int()

----------------------------------------------------------------------
Ran 13 tests in 0.054s

FAILED (errors=6)

История (1 версия и 0 коментара)

Цанислава обнови решението на 15.05.2013 03:50 (преди почти 11 години)

+class Cell():
+ def __init__(self, contents=None):
+ if contents != None and contents.__class__ not in [WorldObject, Food, PythonPart, PythonHead]:
+ raise TypeError
+ self.contents = contents
+
+ def is_empty(self):
+ return self.contents == None
+
+ def is_food(self):
+ return self.contents.__class__ == Food
+
+ def is_safe(self):
+ return self.is_empty or self.is_food
+
+ def __str__(self):
+ return ".."
+
+
+class World():
+ def __init__(self, width):
+ self.width = width
+ self.field = [[Cell() for column in range(0, width)] for row in range(0, width)]
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, coord):
+ return self.field[coord]
+
+ def outside_range(self, x, y):
+ for i in x, y:
+ if i < 0 or i > self.__len__() - 1:
+ return True
+ return False
+
+
+class Vec2D():
+ def __init__(self, x=0,y=0):
+ self.vector = (x, y)
+
+ def __add__(self, other):
+ return Vec2D(self[0] + other[0], self[1] + other[1])
+
+ def __sub__(self, other):
+ return Vec2D(self[0] - other[0], self[1] - other[1])
+
+ def __mul__(self, number):
+ return Vec2D(number*self[0], number*self[1])
+
+ def __neg__(self):
+ return Vec2D(-self[0], -self[1])
+
+ def __getitem__(self, key):
+ return self.vector[key]
+
+
+class WorldObject():
+ def __init__(self, coords=Vec2D()):
+ self.coords = coords
+
+ def __str__(self):
+ object = self.super()
+ return object + ' at v' + self.coords
+
+
+class Food(WorldObject):
+ def __init__(self, coords=Vec2D(), energy=0):
+ WorldObject().__init__(coords)
+ self.coords = coords
+ self.energy = energy
+
+ def __str__(self):
+ return ":" + self.energy
+
+
+class PythonPart(WorldObject):
+ def __init__(self, coords):
+ self.coords = coords
+ WorldObject().__init__(coords)
+
+ def move(self, coords):
+ self.coords = coords
+
+ def __str__(self):
+ return "##"
+
+
+class PythonHead(WorldObject):
+ def __init__(self, coords):
+ self.coords = coords
+ WorldObject().__init__(coords)
+
+ def move(self, coords):
+ self.coords = coords
+
+ def __str__(self):
+ return "@@"
+
+
+class Death(Exception):
+ pass
+
+
+class Python:
+ LEFT = Vec2D(1, 0)
+ UP = Vec2D(0, 1)
+ RIGHT = Vec2D(-1, 0)
+ DOWN = Vec2D(0, -1)
+
+ def __init__(self, world, coords=Vec2D(), size=0, direction=Vec2D()):
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.direction = direction
+ self.head = PythonHead(coords)
+ self.body = []
+ self.body.append(self.head)
+ x, y = coords[0], coords[1]
+ self.world[x][y].contents = self.head
+ for part in range(0, size):
+ body_part = PythonPart(coords - direction*(part+1))
+ self.body.append(body_part)
+ x, y = body_part.coords
+ self.world[x][y].contents = body_part
+
+ def move(self, direction):
+ if direction + self.direction == Vec2D():
+ raise ValueError
+ x, y = self.coords
+ position = Vec2D(self.coords)
+ direct = Vec2D(direction)
+ coords_new = (position + direct).vector
+ x_new, y_new = coords_new
+ if self.world.outside_range(x_new, y_new) or not self.world[x_new][y_new].is_safe:
+ raise Death
+ food = self.world[x_new][y_new].is_food
+ self.head.move(coords_new)
+ self.coords = x_new, y_new
+ self.world[x_new][y_new].contents = self.head
+ coords = self.coords
+ for part in range(0, self.size):
+ coords_old = self.body[part].coords
+ self.body[part].move(coords)
+ self.world[x][y].contents = self.body[part]
+ coords = coords_old
+ if food:
+ body_part = PythonPart(coords)
+ self.body.append(body_part)
+ self.world[x][y].contents = body_part
+ self.size += 1