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

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

Към профила на Десислава Петрова

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 8 успешни тест(а)
  • 5 неуспешни тест(а)

Код

class Vec2D():
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, vect):
x = vect.x + self.x
y = vect.y + self.y
return Vec2D(x, y)
def __sub__(self, vect):
x = self.x - vect.x
y = self.y - vect.y
return Vec2D(x, y)
def __mul__(self, scalar):
x = scalar * self.x
y = scalar * self.y
return Vec2D(x, y)
def __neg__(self):
x = -1 * self.x
y = -1 * self.y
return Vec2D(x, y)
def opposite(self, vect):
x = self.x * vect.x
y = self.y * vect.y
return x == 0 and y == 0
def __iter__(self):
yield self.x
yield self.y
class WorldObject():
def __str__(self):
return '..'
class Food(WorldObject):
def __init__(self, energy=0):
self.energy = energy
def __str__(self):
return ':3'
class PythonPart(WorldObject):
def __str__(self):
return '##'
class PythonHead(PythonPart):
def __str__(self):
return '@@'
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.energy = 0
self.setPythonCoords(coords, size)
def setPythonCoords(self, coords, size):
self.world[coords.x][coords.y] = Cell(PythonHead())
self.coords = [coords]
for part in range(1, size + 1):
x, y = -self.direction*part + coords
self.coords.append(Vec2D(x, y))
self.world[x][y] = Cell(PythonPart())
def move(self, direction):
if direction.opposite(self.direction):
raise ValueError()
self.direction = direction
head_pos = self.coords[0] + direction
head_x, head_y = head_pos
if -1 in [head_x, head_y]:
raise Death("Hit wall!")
if isinstance(self.world[head_x][head_y], PythonPart):
raise Death("Hit python part!")
if self.world[head_x][head_y].is_empty():
self.world[head_x][head_y] = Cell(PythonHead())
self.coords.insert(0, head_pos)
lastCoord_x = self.coords[self.size].x
lastCoord_y = self.coords[self.size].y
self.world[lastCoord_x][lastCoord_y] = Cell()
del self.coords[self.size]
elif isinstance(self.world[head_x][head_y].contents, Food):
self.energy += self.world[head_x][head_y].contents.energy
self.world[head_x][head_y] = Cell(PythonHead())
self.coords.insert(0, head_pos)
class Death(Exception):
pass
class Cell():
def __init__(self, contents=None):
if isinstance(contents, WorldObject):
self.contents = contents
elif contents:
raise TypeError()
def is_empty(self):
return 'contents' not in self.__dict__
def __str__(self):
if not self.is_empty():
self.contents.__str__()
else:
return '..'
class World():
def __init__(self, width):
self.width = width
self.cells = [[Cell()]*width for x in range(width)]
def __getitem__(self, index):
if 0 > index < self.width:
raise IndexError
return self.cells[index]
def __setitem__(self, index, row):
self.rows[index] = row
def __len__(self):
return self.width

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

FFEE.E.......
======================================================================
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-48pdjw/test.py", line 41, in test_ouroboros_death
    py.move(Python.UP)
  File "/tmp/d20130606-14014-48pdjw/solution.py", line 83, in move
    raise ValueError()
ValueError

======================================================================
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-48pdjw/test.py", line 35, in test_python_movement_basic
    self.assertIsInstance(world[x][y].contents, PythonPart)
AttributeError: 'Cell' object has no attribute 'contents'

======================================================================
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-48pdjw/test.py", line 64, in test_snake_death
    {py2.move(Python.LEFT) for repeat in range(0, 5)}
  File "/tmp/d20130606-14014-48pdjw/test.py", line 64, in <setcomp>
    {py2.move(Python.LEFT) for repeat in range(0, 5)}
  File "/tmp/d20130606-14014-48pdjw/solution.py", line 83, in move
    raise ValueError()
ValueError

======================================================================
FAIL: 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-48pdjw/test.py", line 94, in test_growth
    self.assertEqual(py.size, 4)
AssertionError: 3 != 4

======================================================================
FAIL: 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-48pdjw/test.py", line 105, in test_move_backwards
    py.move(-direction)
AssertionError: ValueError not raised

----------------------------------------------------------------------
Ran 13 tests in 0.011s

FAILED (failures=2, errors=3)

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

Десислава обнови решението на 14.05.2013 21:07 (преди почти 12 години)

+
+class Vec2D():
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __add__(self, vect):
+ x = vect.x + self.x
+ y = vect.y + self.y
+ return Vec2D(x, y)
+
+ def __sub__(self, vect):
+ x = self.x - vect.x
+ y = self.y - vect.y
+ return Vec2D(x, y)
+
+ def __mul__(self, scalar):
+ x = scalar * self.x
+ y = scalar * self.y
+ return Vec2D(x, y)
+
+ def __neg__(self):
+ x = -1 * self.x
+ y = -1 * self.y
+ return Vec2D(x, y)
+
+ def opposite(self, vect):
+ x = self.x * vect.x
+ y = self.y * vect.y
+ return x == 0 and y == 0
+
+ def __iter__(self):
+ yield self.x
+ yield self.y
+
+
+class WorldObject():
+ def __str__(self):
+ return '..'
+
+
+class Food(WorldObject):
+ def __init__(self, energy=0):
+ self.energy = energy
+
+ def __str__(self):
+ return ':3'
+
+
+class PythonPart(WorldObject):
+ def __str__(self):
+ return '##'
+
+
+class PythonHead(PythonPart):
+ def __str__(self):
+ return '@@'
+
+
+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.energy = 0
+ self.setPythonCoords(coords, size)
+
+ def setPythonCoords(self, coords, size):
+ self.world[coords.x][coords.y] = Cell(PythonHead())
+ self.coords = [coords]
+ for part in range(1, size + 1):
+ x, y = -self.direction*part + coords
+ self.coords.append(Vec2D(x, y))
+ self.world[x][y] = Cell(PythonPart())
+
+ def move(self, direction):
+ if direction.opposite(self.direction):
+ raise ValueError()
+
+ self.direction = direction
+ head_pos = self.coords[0] + direction
+ head_x, head_y = head_pos
+
+ if -1 in [head_x, head_y]:
+ raise Death("Hit wall!")
+ if isinstance(self.world[head_x][head_y], PythonPart):
+ raise Death("Hit python part!")
+
+ if self.world[head_x][head_y].is_empty():
+ self.world[head_x][head_y] = Cell(PythonHead())
+ self.coords.insert(0, head_pos)
+ lastCoord_x = self.coords[self.size].x
+ lastCoord_y = self.coords[self.size].y
+ self.world[lastCoord_x][lastCoord_y] = Cell()
+ del self.coords[self.size]
+ elif isinstance(self.world[head_x][head_y].contents, Food):
+ self.energy += self.world[head_x][head_y].contents.energy
+ self.world[head_x][head_y] = Cell(PythonHead())
+ self.coords.insert(0, head_pos)
+
+
+class Death(Exception):
+ pass
+
+
+class Cell():
+ def __init__(self, contents=None):
+ if isinstance(contents, WorldObject):
+ self.contents = contents
+ elif contents:
+ raise TypeError()
+
+ def is_empty(self):
+ return 'contents' not in self.__dict__
+
+ def __str__(self):
+ if not self.is_empty():
+ self.contents.__str__()
+ else:
+ return '..'
+
+
+class World():
+ def __init__(self, width):
+ self.width = width
+ self.cells = [[Cell()]*width for x in range(width)]
+
+ def __getitem__(self, index):
+ if 0 > index < self.width:
+ raise IndexError
+ return self.cells[index]
+
+ def __setitem__(self, index, row):
+ self.rows[index] = row
+
+ def __len__(self):
+ return self.width