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

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

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

Резултати

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

Код

class Death(Exception):
pass
class Vec2D:
def __init__(self, x_or_pair, y=None):
if y is None:
self.x = x_or_pair[0]
self.y = x_or_pair[1]
else:
self.x = x_or_pair
self.y = y
def __add__(self, other):
return Vec2D(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vec2D(self.x - other.x, self.y - other.y)
def __mul__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x * other.x, self.y * other.y)
else:
return Vec2D(self.x * other, self.y * other)
def __iter__(self):
yield self.x
yield self.y
class WorldObject:
pass
class Food(WorldObject):
symbol = ':3'
def __init__(self, energy=0):
self.energy = energy
class PythonPart(WorldObject):
symbol = '##'
class PythonHead(PythonPart):
symbol = '@@'
class Cell:
def __init__(self, contents=None):
if contents is None or isinstance(contents, WorldObject):
self.contents = contents
else:
raise TypeError
def __str__(self):
if self.contents is None:
return '..'
else:
return self.contents.symbol
def is_empty(self):
return self.contents is None
class World:
def __init__(self, width):
self.width = width
self.grid = []
for row in range(width):
self.grid.append([])
self.grid[row] = [Cell() for times in range(width)]
def __len__(self):
return self.width
def __getitem__(self, x):
return self.grid[x]
def __str__(self):
print_grid = ''
for row in self.grid:
for item in row:
print_grid += str(item)
print_grid += '\n'
return print_grid
def is_valid_move_position(self, move_position):
x, y = move_position
if (isinstance(self.grid[x][y].contents, PythonPart) or
(x < 0 or x > self.width or y < 0 or y > self.width)):
return False
return True
def get_cell_value(self, coords):
return self.grid[coords.x][coords.y]
class Python:
UP = Vec2D(0, 1)
DOWN = Vec2D(0, -1)
LEFT = Vec2D(-1, 0)
RIGHT = Vec2D(1, 0)
def __init__(self, world, coords, size, direction):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
self.energy = 0
self.body = []
def move(self, direction):
if self.direction + direction == Vec2D(0, 0):
raise ValueError
new_coords = self.coords + self.direction
if self.world.is_valid_move_position(new_coords) is not True:
raise Death
if isinstance(self.world.get_cell_value(new_coords).contents, Food):
self.energy += self.world.get_cell_value(
new_coords).contents.energy
self.size += 1

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

FEFFFFF......
======================================================================
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-10jtj9n/test.py", line 105, in test_move_backwards
    py.move(-direction)
TypeError: bad operand type for unary -: 'Vec2D'

======================================================================
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-10jtj9n/test.py", line 92, in test_growth
    self.assertTrue(cartesian_coord or screen_coord)
AssertionError: False is not true

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

======================================================================
FAIL: 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-10jtj9n/test.py", line 32, in test_python_movement_basic
    self.assertIsInstance(world[x][y].contents, PythonHead)
AssertionError: None is not an instance of <class 'solution.PythonHead'>

======================================================================
FAIL: test_python_placement (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-10jtj9n/test.py", line 14, in test_python_placement
    self.assertIsInstance(world[10][10].contents, PythonHead)
AssertionError: None is not an instance of <class 'solution.PythonHead'>

======================================================================
FAIL: 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-10jtj9n/test.py", line 76, in test_snake_death
    self.assertTrue(cartesian_coord or screen_coord)
AssertionError: False is not true

======================================================================
FAIL: 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-10jtj9n/test.py", line 54, in test_wallpunch_death
    {py.move(Python.LEFT) for repeat in range(0, 10)}
AssertionError: Death not raised

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

FAILED (failures=6, errors=1)

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

Христо обнови решението на 15.05.2013 14:30 (преди почти 11 години)

+class Death(Exception):
+ pass
+
+
+class Vec2D:
+
+ def __init__(self, x_or_pair, y=None):
+ if y is None:
+ self.x = x_or_pair[0]
+ self.y = x_or_pair[1]
+ else:
+ self.x = x_or_pair
+ self.y = y
+
+ def __add__(self, other):
+ return Vec2D(self.x + other.x, self.y + other.y)
+
+ def __sub__(self, other):
+ return Vec2D(self.x - other.x, self.y - other.y)
+
+ def __mul__(self, other):
+ if isinstance(other, Vec2D):
+ return Vec2D(self.x * other.x, self.y * other.y)
+ else:
+ return Vec2D(self.x * other, self.y * other)
+
+ def __iter__(self):
+ yield self.x
+ yield self.y
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+ symbol = ':3'
+
+ def __init__(self, energy=0):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ symbol = '##'
+
+
+class PythonHead(PythonPart):
+ symbol = '@@'
+
+
+class Cell:
+
+ def __init__(self, contents=None):
+ if contents is None or isinstance(contents, WorldObject):
+ self.contents = contents
+ else:
+ raise TypeError
+
+ def __str__(self):
+ if self.contents is None:
+ return '..'
+ else:
+ return self.contents.symbol
+
+ def is_empty(self):
+ return self.contents is None
+
+
+class World:
+
+ def __init__(self, width):
+ self.width = width
+ self.grid = []
+ for row in range(width):
+ self.grid.append([])
+ self.grid[row] = [Cell() for times in range(width)]
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, x):
+ return self.grid[x]
+
+ def __str__(self):
+ print_grid = ''
+ for row in self.grid:
+ for item in row:
+ print_grid += str(item)
+ print_grid += '\n'
+ return print_grid
+
+ def is_valid_move_position(self, move_position):
+ x, y = move_position
+ if (isinstance(self.grid[x][y].contents, PythonPart) or
+ (x < 0 or x > self.width or y < 0 or y > self.width)):
+ return False
+ return True
+
+ def get_cell_value(self, coords):
+ return self.grid[coords.x][coords.y]
+
+
+class Python:
+ UP = Vec2D(0, 1)
+ DOWN = Vec2D(0, -1)
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.direction = direction
+ self.energy = 0
+ self.body = []
+
+ def move(self, direction):
+ if self.direction + direction == Vec2D(0, 0):
+ raise ValueError
+
+ new_coords = self.coords + self.direction
+
+ if self.world.is_valid_move_position(new_coords) is not True:
+ raise Death
+
+ if isinstance(self.world.get_cell_value(new_coords).contents, Food):
+ self.energy += self.world.get_cell_value(
+ new_coords).contents.energy
+ self.size += 1