Решение на Питоните хапят! от Мария Митева

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

Към профила на Мария Митева

Резултати

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

Код

from collections import deque
class World:
def __init__(self, width):
self.size = width
self.world = [[Cell() for y in range(0, width)]
for x in range(0, width)]
def __getitem__(self, index):
if index in range(0, self.size):
return self.world[index]
raise IndexError
def length(self):
return self.size
def clean(self, x, y):
self.world[x][y].empty()
def __str__(self):
result = ""
for row in range(0, self.size):
result += "row" + str(row)
for cell in self.world[row]:
result += cell.visualize()
result += "\n"
return result
class Cell:
def __init__(self, *args):
if args:
if not isinstance(args[0], WorldObject):
raise TypeError
else:
self.contents = args[0]
else:
self.contents = None
def is_empty(self):
if self.contents:
return False
return True
def fill(self, content):
if not isinstance(content, WorldObject):
raise TypeError
else:
self.contents = content
def empty(self):
self.contents = None
def visualize(self):
TYPES = {Food: ":3",
PythonPart: "##",
PythonTail: "##",
PythonHead: "@@"}
if type(self.contents) in TYPES:
return TYPES[type(self.contents)]
return ".."
def len(world):
return world.length()
class Vec2D:
def __init__(self, x, y):
self.x = x
self.y = y
self.content = [x, y]
def __add__(self, other):
if isinstance(other, Vec2D):
return (self.x + other.x, self.y + other.y)
return (self.x + other[0], self.y + other[1])
__radd__ = __add__
def __iadd__(self, other):
if isinstance(other, Vec2D):
self.x += other.x
self.y += other.y
else:
self.x += other[0]
self.y += other[1]
return self
def __mul__(self, scalar):
return (scalar*self.x, scalar*self.y)
def __sub__(self, other):
if isinstance(other, Vec2D):
return (self.x - other.x, self.y - other.y)
return (self.x - other[0], self.y - other[1])
def __rsub__(self, other):
if isinstance(other, Vec2D):
return (other.x - self.x, other.y - self.y)
return (other[0] - self.x, other[1] - self.y)
def __isub__(self, other):
if isinstance(other, Vec2D):
self.x -= other.x
self.y -= other.y
else:
self.x -= other[0]
self.y -= other[1]
return self
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __neg__(self):
return Vec2D(-self.x, -self.y)
def __iter__(self):
self.index = 0
return self
def __next__(self):
if self.index < 2:
self.index += 1
return self.content[self.index-1]
else:
raise StopIteration
class WorldObject:
pass
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
class PythonPart(WorldObject):
def __init__(self, coords):
self.coords = coords
def move(self, direction, world):
self.coords += direction
self.put(world)
def put(self, world):
x = self.coords.x
y = self.coords.y
world[x][y].fill(self)
class PythonTail(PythonPart):
def move(self, direction, world):
world.clean(self.coords.x, self.coords.y)
self.coords += direction
self.put(world)
def put(self, world):
x = self.coords.x
y = self.coords.y
world[x][y].fill(self)
class PythonHead(PythonPart):
def __init__(self, coords):
self.coords = coords
self.found_food = False
def move(self, direction, world, x, y):
world.clean(x, y)
coords = Vec2D(x, y)
PythonPart(coords).put(world)
coords += direction
return self.put(world, coords)
def put(self, world, coords):
x = coords.x
y = coords.y
if (x, y) in [(a, b) for a in range(0, len(world))
for b in range(0, len(world))]:
if world[x][y].is_empty():
world[x][y].fill(self)
self.found_food = False
return 0
elif isinstance(world[x][y].contents, Food):
energy = world[x][y].energy
world.clean(self.coords.x, self.coords.y)
world[x][y].fill(self)
self.found_food = true
return energy
else:
raise Death
else:
raise Death
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, size, direction):
self.size = size
self.energy = 0
self.direction = direction
self.directions = deque([direction])
self.coords = coords
self.head = PythonHead(self.coords)
self.x, self.y = self.coords
self.world = world
self.head.put(self.world, coords)
position = coords
for body_part in range(0, self.size):
position -= direction
PythonPart(position).put(self.world)
self.tail = PythonPart(position)
def move(self, direction):
if self.direction == -direction:
raise ValueError
self.direction = direction
self.directions.appendleft([direction])
self.energy += self.head.move(direction, self.world, self.x, self.y)
if not self.head.found_food:
self.tail.move(self.directions.pop(), self.world)
else:
self.size += 1

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

E.E..E.......
======================================================================
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-18aq7k1/test.py", line 88, in test_growth
    py.move(Python.LEFT)
  File "/tmp/d20130606-14014-18aq7k1/solution.py", line 230, in move
    self.energy += self.head.move(direction, self.world, self.x, self.y)
  File "/tmp/d20130606-14014-18aq7k1/solution.py", line 176, in move
    return self.put(world, coords)
  File "/tmp/d20130606-14014-18aq7k1/solution.py", line 194, in put
    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-18aq7k1/test.py", line 41, in test_ouroboros_death
    py.move(Python.UP)
  File "/tmp/d20130606-14014-18aq7k1/solution.py", line 232, in move
    self.tail.move(self.directions.pop(), self.world)
  File "/tmp/d20130606-14014-18aq7k1/solution.py", line 145, in move
    self.coords += direction
  File "/tmp/d20130606-14014-18aq7k1/solution.py", line 87, in __iadd__
    self.x += other[0]
  File "/tmp/d20130606-14014-18aq7k1/solution.py", line 78, in __add__
    return (self.x + other[0], self.y + other[1])
TypeError: 'int' object is not subscriptable

======================================================================
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-18aq7k1/test.py", line 68, in test_snake_death
    py2 = Python(world, Vec2D(5, 8), 3, Python.UP)
  File "/tmp/d20130606-14014-18aq7k1/solution.py", line 218, in __init__
    self.head.put(self.world, coords)
  File "/tmp/d20130606-14014-18aq7k1/solution.py", line 194, in put
    raise Death
solution.Death

----------------------------------------------------------------------
Ran 13 tests in 0.043s

FAILED (errors=3)

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

Мария обнови решението на 15.05.2013 16:24 (преди почти 11 години)

+from collections import deque
+
+
+class World:
+ def __init__(self, width):
+ self.size = width
+ self.world = [[Cell() for y in range(0, width)]
+ for x in range(0, width)]
+
+ def __getitem__(self, index):
+ if index in range(0, self.size):
+ return self.world[index]
+ raise IndexError
+
+ def length(self):
+ return self.size
+
+ def clean(self, x, y):
+ self.world[x][y].empty()
+
+ def __str__(self):
+ result = ""
+ for row in range(0, self.size):
+ result += "row" + str(row)
+ for cell in self.world[row]:
+ result += cell.visualize()
+ result += "\n"
+ return result
+
+
+class Cell:
+ def __init__(self, *args):
+ if args:
+ if not isinstance(args[0], WorldObject):
+ raise TypeError
+ else:
+ self.contents = args[0]
+ else:
+ self.contents = None
+
+ def is_empty(self):
+ if self.contents:
+ return False
+ return True
+
+ def fill(self, content):
+ if not isinstance(content, WorldObject):
+ raise TypeError
+ else:
+ self.contents = content
+
+ def empty(self):
+ self.contents = None
+
+ def visualize(self):
+ TYPES = {Food: ":3",
+ PythonPart: "##",
+ PythonTail: "##",
+ PythonHead: "@@"}
+ if type(self.contents) in TYPES:
+ return TYPES[type(self.contents)]
+ return ".."
+
+
+def len(world):
+ return world.length()
+
+
+class Vec2D:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+ self.content = [x, y]
+
+ def __add__(self, other):
+ if isinstance(other, Vec2D):
+ return (self.x + other.x, self.y + other.y)
+ return (self.x + other[0], self.y + other[1])
+
+ __radd__ = __add__
+
+ def __iadd__(self, other):
+ if isinstance(other, Vec2D):
+ self.x += other.x
+ self.y += other.y
+ else:
+ self.x += other[0]
+ self.y += other[1]
+ return self
+
+ def __mul__(self, scalar):
+ return (scalar*self.x, scalar*self.y)
+
+ def __sub__(self, other):
+ if isinstance(other, Vec2D):
+ return (self.x - other.x, self.y - other.y)
+ return (self.x - other[0], self.y - other[1])
+
+ def __rsub__(self, other):
+ if isinstance(other, Vec2D):
+ return (other.x - self.x, other.y - self.y)
+ return (other[0] - self.x, other[1] - self.y)
+
+ def __isub__(self, other):
+ if isinstance(other, Vec2D):
+ self.x -= other.x
+ self.y -= other.y
+ else:
+ self.x -= other[0]
+ self.y -= other[1]
+ return self
+
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
+ def __neg__(self):
+ return Vec2D(-self.x, -self.y)
+
+ def __iter__(self):
+ self.index = 0
+ return self
+
+ def __next__(self):
+ if self.index < 2:
+ self.index += 1
+ return self.content[self.index-1]
+ else:
+ raise StopIteration
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+ def __init__(self, energy):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ def __init__(self, coords):
+ self.coords = coords
+
+ def move(self, direction, world):
+ self.coords += direction
+ self.put(world)
+
+ def put(self, world):
+ x = self.coords.x
+ y = self.coords.y
+ world[x][y].fill(self)
+
+
+class PythonTail(PythonPart):
+ def move(self, direction, world):
+ world.clean(self.coords.x, self.coords.y)
+ self.coords += direction
+ self.put(world)
+
+ def put(self, world):
+ x = self.coords.x
+ y = self.coords.y
+ world[x][y].fill(self)
+
+
+class PythonHead(PythonPart):
+ def __init__(self, coords):
+ self.coords = coords
+ self.found_food = False
+
+ def move(self, direction, world, x, y):
+ world.clean(x, y)
+ coords = Vec2D(x, y)
+ PythonPart(coords).put(world)
+ coords += direction
+ return self.put(world, coords)
+
+ def put(self, world, coords):
+ x = coords.x
+ y = coords.y
+ if (x, y) in [(a, b) for a in range(0, len(world))
+ for b in range(0, len(world))]:
+ if world[x][y].is_empty():
+ world[x][y].fill(self)
+ self.found_food = False
+ return 0
+ elif isinstance(world[x][y].contents, Food):
+ energy = world[x][y].energy
+ world.clean(self.coords.x, self.coords.y)
+ world[x][y].fill(self)
+ self.found_food = true
+ return energy
+ else:
+ raise Death
+ else:
+ raise Death
+
+
+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, size, direction):
+ self.size = size
+ self.energy = 0
+ self.direction = direction
+ self.directions = deque([direction])
+ self.coords = coords
+ self.head = PythonHead(self.coords)
+ self.x, self.y = self.coords
+ self.world = world
+ self.head.put(self.world, coords)
+ position = coords
+ for body_part in range(0, self.size):
+ position -= direction
+ PythonPart(position).put(self.world)
+ self.tail = PythonPart(position)
+
+ def move(self, direction):
+ if self.direction == -direction:
+ raise ValueError
+ self.direction = direction
+ self.directions.appendleft([direction])
+ self.energy += self.head.move(direction, self.world, self.x, self.y)
+ if not self.head.found_food:
+ self.tail.move(self.directions.pop(), self.world)
+ else:
+ self.size += 1