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

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

Към профила на Радослав Платиканов

Резултати

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

Код

from collections import deque
class Vec2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
if not isinstance(other, Vec2D):
return NotImplemented
return Vec2D(self.x + other.x, self.y + other.y)
def __sub__(self, other):
if not isinstance(other, Vec2D):
return NotImplemented
return Vec2D(self.x - other.x, self.y - other.y)
def __neg__(self):
return Vec2D(-self.x, -self.y)
def __mul__(self, other):
return Vec2D(self.x * other, self.y * other)
def __rmul__(self, other):
return Vec2D(self.x * other, self.y * other)
def __getitem__(self, key):
if key == 0:
return self.x
if key == 1:
return self.y
raise IndexError()
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __ne__(self, other):
return self.x != other.x or self.y != other.y
class Cell:
def __init__(self, contents=None):
if contents is not None and not isinstance(contents, WorldObject):
raise TypeError()
self.contents = contents
def is_empty(self):
return self.contents is None
def clear(self):
self.contents = None
class WorldObject:
pass
class Food(WorldObject):
def __init__(self, energy=0):
self.energy = energy
class PythonPart(WorldObject):
def __init__(self, coords):
self.coords = coords
class PythonHead(PythonPart):
def __init__(self, coords):
PythonPart.__init__(self, coords)
class World:
class Column:
def __init__(self, width):
self.data = [Cell() for _ in range(width)]
def __getitem__(self, key):
if not isinstance(key, int):
raise TypeError()
if key < 0 or key >= len(self.data):
raise IndexError()
return self.data[key]
def __setitem__(self, key, value):
if not isinstance(key, int):
raise TypeError()
if key < 0 or key >= len(self.data):
raise IndexError()
self.data[key] = value
def __init__(self, width):
self._field = [World.Column(width) for _ in range(width)]
def __len__(self):
return len(self._field)
def __getitem__(self, key):
if not isinstance(key, int):
raise TypeError()
if key < 0 or key >= len(self):
raise IndexError()
return self._field[key]
class Python:
LEFT = Vec2D(-1, 0)
RIGHT = Vec2D(1, 0)
UP = Vec2D(0, -1)
DOWN = Vec2D(0, 1)
def __init__(self, world, coords, size, direction):
self.world = world
self.size = size
self.direction = direction
self.head = PythonHead(coords)
self._body = deque()
current_pos = coords - direction
for _ in range(size):
self._body.append(PythonPart(current_pos))
current_pos -= direction
world[coords.x][coords.y].contents = self.head
for part in self._body:
world[part.coords.x][part.coords.y].contents = part
self.energy = 0
def move(self, direction):
if self.direction == -direction:
raise ValueError()
self.direction = direction
new_position = self.head.coords + direction
try:
new_cell = self.world[new_position.x][new_position.y]
except IndexError:
raise Death()
grow = False
if isinstance(new_cell.contents, Food):
self.energy += new_cell.contents.energy
grow = True
self.size += 1
elif not new_cell.is_empty():
if new_cell.contents.coords != self._body[-1].coords:
raise Death()
if not grow:
tail = self._body.pop()
self.world[tail.coords.x][tail.coords.y].clear()
old_head_pos = self.head.coords
self.head.coords += direction
self.world[self.head.coords.x][self.head.coords.y].contents = self.head
self._body.appendleft(PythonPart(old_head_pos))
self.world[old_head_pos.x][old_head_pos.y].contents = self._body[0]
class Death(Exception):
pass

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

..F..........
======================================================================
FAIL: 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-1rbjg58/test.py", line 47, in test_ouroboros_death
    py.move(Python.DOWN)
AssertionError: Death not raised

----------------------------------------------------------------------
Ran 13 tests in 0.036s

FAILED (failures=1)

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

Радослав обнови решението на 14.05.2013 22:16 (преди над 11 години)

+from collections import deque
+
+
+class Vec2D:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __add__(self, other):
+ if not isinstance(other, Vec2D):
+ return NotImplemented
+ return Vec2D(self.x + other.x, self.y + other.y)
+
+ def __sub__(self, other):
+ if not isinstance(other, Vec2D):
+ return NotImplemented
+ return Vec2D(self.x - other.x, self.y - other.y)
+
+ def __neg__(self):
+ return Vec2D(-self.x, -self.y)
+
+ def __mul__(self, other):
+ return Vec2D(self.x * other, self.y * other)
+
+ def __rmul__(self, other):
+ return Vec2D(self.x * other, self.y * other)
+
+ def __getitem__(self, key):
+ if key == 0:
+ return self.x
+ if key == 1:
+ return self.y
+
+ raise IndexError()
+
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
+ def __ne__(self, other):
+ return self.x != other.x or self.y != other.y
+
+
+class Cell:
+ def __init__(self, contents=None):
+ if contents is not None and not isinstance(contents, WorldObject):
+ raise TypeError()
+
+ self.contents = contents
+
+ def is_empty(self):
+ return self.contents is None
+
+ def clear(self):
+ self.contents = None
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+ def __init__(self, energy=0):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ def __init__(self, coords):
+ self.coords = coords
+
+
+class PythonHead(PythonPart):
+ def __init__(self, coords):
+ PythonPart.__init__(self, coords)
+
+
+class World:
+ class Column:
+ def __init__(self, width):
+ self.data = [Cell() for _ in range(width)]
+
+ def __getitem__(self, key):
+ if not isinstance(key, int):
+ raise TypeError()
+ if key < 0 or key >= len(self.data):
+ raise IndexError()
+
+ return self.data[key]
+
+ def __setitem__(self, key, value):
+ if not isinstance(key, int):
+ raise TypeError()
+ if key < 0 or key >= len(self.data):
+ raise IndexError()
+
+ self.data[key] = value
+
+ def __init__(self, width):
+ self._field = [World.Column(width) for _ in range(width)]
+
+ def __len__(self):
+ return len(self._field)
+
+ def __getitem__(self, key):
+ if not isinstance(key, int):
+ raise TypeError()
+ if key < 0 or key >= len(self):
+ raise IndexError()
+
+ return self._field[key]
+
+
+class Python:
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+ UP = Vec2D(0, -1)
+ DOWN = Vec2D(0, 1)
+
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.size = size
+ self.direction = direction
+
+ self.head = PythonHead(coords)
+
+ self._body = deque()
+
+ current_pos = coords - direction
+ for _ in range(size):
+ self._body.append(PythonPart(current_pos))
+ current_pos -= direction
+
+ world[coords.x][coords.y].contents = self.head
+
+ for part in self._body:
+ world[part.coords.x][part.coords.y].contents = part
+
+ self.energy = 0
+
+ def move(self, direction):
+ if self.direction == -direction:
+ raise ValueError()
+
+ self.direction = direction
+
+ new_position = self.head.coords + direction
+
+ try:
+ new_cell = self.world[new_position.x][new_position.y]
+ except IndexError:
+ raise Death()
+
+ grow = False
+ if isinstance(new_cell.contents, Food):
+ self.energy += new_cell.contents.energy
+ grow = True
+ self.size += 1
+ elif not new_cell.is_empty():
+ if new_cell.contents.coords != self._body[-1].coords:
+ raise Death()
+
+ if not grow:
+ tail = self._body.pop()
+ self.world[tail.coords.x][tail.coords.y].clear()
+
+ old_head_pos = self.head.coords
+
+ self.head.coords += direction
+ self.world[self.head.coords.x][self.head.coords.y].contents = self.head
+ self._body.appendleft(PythonPart(old_head_pos))
+ self.world[old_head_pos.x][old_head_pos.y].contents = self._body[0]
+
+
+class Death(Exception):
+ pass