Решение на Питоните хапят! от Филарета Йорданова

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

Към профила на Филарета Йорданова

Резултати

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

Код

class World:
def __init__(self, width):
self.width = width
self.world = [[Cell() for x in range(width)] for y in range(width)]
def __getitem__(self, index):
if index < 0:
raise IndexError
else:
return self.world[index]
def __len__(self):
return self.width
def __str__(self):
world = ''
for row in self.world:
for cell in row:
world = world + cell.__str__()
world = world + '\n'
return world
class Vec2D():
def __init__(self, x, y):
self.x = x
self.y = y
def __mul__(self, value):
return Vec2D(self.x * value, self.y * value)
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 __neg__(self):
return Vec2D(- self.x, - self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __ne__(self, other):
return not self.__eq__(other)
def __getitem__(self, key):
if(key == 0):
return self.y
elif(key == 1):
return self.x
else:
raise StopIteration
def __repr__(self):
return "v(" + str(self.x) + "," + str(self.y) + ")"
class WorldObject:
pass
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
class Cell(WorldObject):
def __init__(self, contents=None):
if isinstance(contents, WorldObject) or contents is None:
self.contents = contents
else:
raise TypeError
def is_empty(self):
return self.contents is None
def add_content(self, contents):
self.contents = contents
def free_cell(self):
self.contents = None
def __str__(self):
if self.is_empty():
return '..'
else:
return self.contents.__str__()
class Death(Exception):
pass
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)
energy = 0
directions = []
direction = Vec2D(0, 0)
python = []
grown = False
def __init__(self, world, coords, size, direction):
self.size = size
self.head = coords
self.world = world
self._set_on_board(direction)
def _set_on_board(self, direction):
self.world[self.head.y][self.head.x].add_content(PythonHead())
self.python.append(self.head)
self.directions.append(direction)
for i in range(1, self.size + 1):
cell = self.head + (-direction) * i
self.python.append(cell)
self.world[cell.y][cell.x].add_content(PythonPart())
self.directions.append(direction)
def in_range(self, cell):
check_x = cell.x >= 0 and cell.x < self.world.width
check_y = cell.y >= 0 and cell.y < self.world.width
return check_x and check_y
def _move(self, part, direction):
next_part = part + direction
if not self.in_range(next_part):
raise Death
new_cell = self.world[next_part.y][next_part.x]
if isinstance(new_cell.contents, PythonHead):
raise Death
if isinstance(new_cell.contents, PythonPart):
raise Death
if isinstance(new_cell.contents, Food):
self.energy += new_cell.contents.energy
self.grown = True
new_cell.free_cell()
content = self.world[part.y][part.x].contents
if not self.grown and self.python[-1] != part:
self.world[part.y][part.x].free_cell()
if new_cell.is_empty():
new_cell.add_content(content)
return next_part
def _change_direction(self, direction):
for last_dir in self.directions:
if last_dir != direction:
last_dir = direction
break
def move(self, direction):
if self.direction == -direction:
raise ValueError
if any(direct != direction for direct in self.directions):
self._change_direction(direction)
for i in range(self.size+1):
self.python[i] = self._move(self.python[i], self.directions[i])
if self.grown:
new_part = -self.directions[-1] + self.python[-1]
self.python.append(new_part)
self.directions.append(self.directions[-1])
self.size += 1
self.grown = False
self.direction = direction

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

E.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-1pwivkt/test.py", line 88, in test_growth
    py.move(Python.LEFT)
  File "/tmp/d20130606-14014-1pwivkt/solution.py", line 169, in move
    self.python[i] = self._move(self.python[i], self.directions[i])
  File "/tmp/d20130606-14014-1pwivkt/solution.py", line 143, in _move
    raise Death
solution.Death

======================================================================
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-1pwivkt/test.py", line 40, in test_ouroboros_death
    py.move(Python.LEFT)
  File "/tmp/d20130606-14014-1pwivkt/solution.py", line 169, in move
    self.python[i] = self._move(self.python[i], self.directions[i])
  File "/tmp/d20130606-14014-1pwivkt/solution.py", line 143, in _move
    raise Death
solution.Death

======================================================================
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-1pwivkt/test.py", line 29, in test_python_movement_basic
    py.move(direction)
  File "/tmp/d20130606-14014-1pwivkt/solution.py", line 169, in move
    self.python[i] = self._move(self.python[i], self.directions[i])
  File "/tmp/d20130606-14014-1pwivkt/solution.py", line 143, in _move
    raise Death
solution.Death

----------------------------------------------------------------------
Ran 13 tests in 0.048s

FAILED (errors=3)

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

Филарета обнови решението на 13.05.2013 23:33 (преди над 11 години)

+class World:
+ def __init__(self, width):
+ self.width = width
+ self.world = [[Cell() for x in range(width)] for y in range(width)]
+
+ def __getitem__(self, index):
+ if index < 0:
+ raise IndexError
+ else:
+ return self.world[index]
+
+ def __len__(self):
+ return self.width
+
+ def __str__(self):
+ world = ''
+ for row in self.world:
+ for cell in row:
+ world = world + cell.__str__()
+ world = world + '\n'
+ return world
+
+
+class Vec2D():
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __mul__(self, value):
+ return Vec2D(self.x * value, self.y * value)
+
+ 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 __neg__(self):
+ return Vec2D(- self.x, - self.y)
+
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __getitem__(self, key):
+ if(key == 0):
+ return self.y
+ elif(key == 1):
+ return self.x
+ else:
+ raise StopIteration
+
+ def __repr__(self):
+ return "v(" + str(self.x) + "," + str(self.y) + ")"
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+ def __init__(self, energy):
+ self.energy = energy
+
+
+class Cell(WorldObject):
+ def __init__(self, contents=None):
+ if isinstance(contents, WorldObject) or contents is None:
+ self.contents = contents
+ else:
+ raise TypeError
+
+ def is_empty(self):
+ return self.contents is None
+
+ def add_content(self, contents):
+ self.contents = contents
+
+ def free_cell(self):
+ self.contents = None
+
+ def __str__(self):
+ if self.is_empty():
+ return '..'
+ else:
+ return self.contents.__str__()
+
+
+class Death(Exception):
+ pass
+
+
+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)
+ energy = 0
+ directions = []
+ direction = Vec2D(0, 0)
+ python = []
+ grown = False
+
+ def __init__(self, world, coords, size, direction):
+ self.size = size
+ self.head = coords
+ self.world = world
+ self._set_on_board(direction)
+
+ def _set_on_board(self, direction):
+ self.world[self.head.y][self.head.x].add_content(PythonHead())
+ self.python.append(self.head)
+ self.directions.append(direction)
+ for i in range(1, self.size + 1):
+ cell = self.head + (-direction) * i
+ self.python.append(cell)
+ self.world[cell.y][cell.x].add_content(PythonPart())
+ self.directions.append(direction)
+
+ def in_range(self, cell):
+ check_x = cell.x >= 0 and cell.x < self.world.width
+ check_y = cell.y >= 0 and cell.y < self.world.width
+ return check_x and check_y
+
+ def _move(self, part, direction):
+ content = self.world[part.y][part.x].contents
+ self.world[part.y][part.x].free_cell()
+ part = part + direction
+ if not self.in_range(part):
+ raise Death
+ new_cell = self.world[part.y][part.x]
+ if new_cell.is_empty():
+ new_cell.add_content(content)
+ elif isinstance(new_cell.contents, Food):
+ self.energy += new_cell.contents.energy
+ self.grown = True
+ else:
+ raise Death
+ return part
+
+ def _change_direction(self, direction):
+ for last_dir in self.directions:
+ if last_dir != direction:
+ last_dir = direction
+ break
+
+ def move(self, direction):
+ if self.direction == -direction:
+ raise ValueError
+ if any(direct != direction for direct in self.directions):
+ self._change_direction(direction)
+ for i in range(self.size+1):
+ self.python[i] = self._move(self.python[i], self.directions[i])
+ if self.grown:
+ new_part = -self.directions[-1] + self.python[-1]
+ self.python.append(new_part)
+ self.directions.append(self.directions[-1])
+ self.size += 1
+ self.grown = False
+ self.direction = direction

Филарета обнови решението на 15.05.2013 09:44 (преди над 11 години)

class World:
def __init__(self, width):
self.width = width
self.world = [[Cell() for x in range(width)] for y in range(width)]
def __getitem__(self, index):
if index < 0:
raise IndexError
else:
return self.world[index]
def __len__(self):
return self.width
def __str__(self):
world = ''
for row in self.world:
for cell in row:
world = world + cell.__str__()
world = world + '\n'
return world
class Vec2D():
def __init__(self, x, y):
self.x = x
self.y = y
def __mul__(self, value):
return Vec2D(self.x * value, self.y * value)
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 __neg__(self):
return Vec2D(- self.x, - self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __ne__(self, other):
return not self.__eq__(other)
def __getitem__(self, key):
if(key == 0):
return self.y
elif(key == 1):
return self.x
else:
raise StopIteration
def __repr__(self):
return "v(" + str(self.x) + "," + str(self.y) + ")"
class WorldObject:
pass
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
class Cell(WorldObject):
def __init__(self, contents=None):
if isinstance(contents, WorldObject) or contents is None:
self.contents = contents
else:
raise TypeError
def is_empty(self):
return self.contents is None
def add_content(self, contents):
self.contents = contents
def free_cell(self):
self.contents = None
def __str__(self):
if self.is_empty():
return '..'
else:
return self.contents.__str__()
class Death(Exception):
pass
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)
energy = 0
directions = []
direction = Vec2D(0, 0)
python = []
grown = False
def __init__(self, world, coords, size, direction):
self.size = size
self.head = coords
self.world = world
self._set_on_board(direction)
def _set_on_board(self, direction):
self.world[self.head.y][self.head.x].add_content(PythonHead())
self.python.append(self.head)
self.directions.append(direction)
for i in range(1, self.size + 1):
cell = self.head + (-direction) * i
self.python.append(cell)
self.world[cell.y][cell.x].add_content(PythonPart())
self.directions.append(direction)
def in_range(self, cell):
check_x = cell.x >= 0 and cell.x < self.world.width
check_y = cell.y >= 0 and cell.y < self.world.width
return check_x and check_y
def _move(self, part, direction):
- content = self.world[part.y][part.x].contents
- self.world[part.y][part.x].free_cell()
- part = part + direction
- if not self.in_range(part):
+ next_part = part + direction
+ if not self.in_range(next_part):
raise Death
- new_cell = self.world[part.y][part.x]
- if new_cell.is_empty():
- new_cell.add_content(content)
- elif isinstance(new_cell.contents, Food):
+ new_cell = self.world[next_part.y][next_part.x]
+ if isinstance(new_cell.contents, PythonHead):
+ raise Death
+ if isinstance(new_cell.contents, PythonPart):
+ raise Death
+ if isinstance(new_cell.contents, Food):
self.energy += new_cell.contents.energy
self.grown = True
- else:
- raise Death
- return part
+ new_cell.free_cell()
+ content = self.world[part.y][part.x].contents
+ if not self.grown and self.python[-1] != part:
+ self.world[part.y][part.x].free_cell()
+ if new_cell.is_empty():
+ new_cell.add_content(content)
+ return next_part
def _change_direction(self, direction):
for last_dir in self.directions:
if last_dir != direction:
last_dir = direction
break
def move(self, direction):
if self.direction == -direction:
raise ValueError
if any(direct != direction for direct in self.directions):
self._change_direction(direction)
for i in range(self.size+1):
self.python[i] = self._move(self.python[i], self.directions[i])
if self.grown:
new_part = -self.directions[-1] + self.python[-1]
self.python.append(new_part)
self.directions.append(self.directions[-1])
self.size += 1
self.grown = False
- self.direction = direction
+ self.direction = direction