Решение на Питоните хапят! от Георги Харизанов

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

Към профила на Георги Харизанов

Резултати

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

Код

class Vec2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __neg__(self):
self.x = -self.x
self.y = -self.y
def __add__(self, other):
self.x += other.x
self.y += other.y
return self
def __sub__(self, other):
self.x -= other.x
self.y -= other.y
return self
class Cell:
def __init__(self, content):
self.content = content
def is_empty(self):
return (self.content is None)
def __str__(self):
if(self.is_empty()):
return ".."
return self.content.__str__()
class World:
def __init__(self, width):
self.world_2D = []
self.len = width
for i in range(width):
self.world_2D.append(list(Cell(None) for x in range(width)))
def __str__(self):
result = ""
for i in range(self.len):
for cell_row in self.world_2D:
result += cell_row[i].__str__()
result += "\n"
return result
def __getitem__(self, index):
return self.world_2D[index]
def len(self):
return len
class WorldObject:
def __init__(self, world_char):
self.world_char = world_char
def __str__(self):
return self.world_char
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
self.world_char = ":" + str(energy)
class PythonPart(WorldObject):
def __init__(self, world, coords):
self.coords = coords
self.world_char = "##"
self.world = world
self.world[coords.x][coords.y] = Cell(self)
def clear(self):
self.world[self.coords.x][self.coords.y] = Cell(None)
class Python(PythonPart):
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_char = "@@"
self.coords = coords
self.size = size
self.world = world
self.world[self.coords.x][self.coords.y] = Cell(self)
self.body = []
self.body.append(self)
self.tail_vector = Vec2D(self.coords.x, self.coords.y)
for body_part_index in range(size):
self.tail_vector = self.tail_vector - direction
self.body.append(PythonPart(world, self.tail_vector))
def move(self, direction):
self.body[self.size].clear()
self.body[1] = PythonPart(world, self.coords)
self.coords = self.coords + direction
self.body[0] = self
self.world[self.coords.x][self.coords.y] = Cell(self)
for i in reversed(range(self.size-2)):
self.body[i] = self.body[i-1]
class Death(Exception):
pass

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

EEEEEEEEEEF.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-1jdskud/test.py", line 85, in test_growth
    py.move(Python.LEFT)
  File "/tmp/d20130606-14014-1jdskud/solution.py", line 107, in move
    self.body[1] = PythonPart(world, self.coords)
NameError: global name 'world' is not defined

======================================================================
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-1jdskud/test.py", line 103, in test_move_backwards
    py.move(direction)
  File "/tmp/d20130606-14014-1jdskud/solution.py", line 107, in move
    self.body[1] = PythonPart(world, self.coords)
NameError: global name 'world' is not defined

======================================================================
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-1jdskud/test.py", line 40, in test_ouroboros_death
    py.move(Python.LEFT)
  File "/tmp/d20130606-14014-1jdskud/solution.py", line 107, in move
    self.body[1] = PythonPart(world, self.coords)
NameError: global name 'world' is not defined

======================================================================
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-1jdskud/test.py", line 29, in test_python_movement_basic
    py.move(direction)
  File "/tmp/d20130606-14014-1jdskud/solution.py", line 107, in move
    self.body[1] = PythonPart(world, self.coords)
NameError: global name 'world' is not defined

======================================================================
ERROR: 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-1jdskud/test.py", line 14, in test_python_placement
    self.assertIsInstance(world[10][10].contents, PythonHead)
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-1jdskud/test.py", line 64, in test_snake_death
    {py2.move(Python.LEFT) for repeat in range(0, 5)}
  File "/tmp/d20130606-14014-1jdskud/test.py", line 64, in <setcomp>
    {py2.move(Python.LEFT) for repeat in range(0, 5)}
  File "/tmp/d20130606-14014-1jdskud/solution.py", line 107, in move
    self.body[1] = PythonPart(world, self.coords)
NameError: global name 'world' is not defined

======================================================================
ERROR: 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-1jdskud/test.py", line 54, in test_wallpunch_death
    {py.move(Python.LEFT) for repeat in range(0, 10)}
  File "/tmp/d20130606-14014-1jdskud/test.py", line 54, in <setcomp>
    {py.move(Python.LEFT) for repeat in range(0, 10)}
  File "/tmp/d20130606-14014-1jdskud/solution.py", line 107, in move
    self.body[1] = PythonPart(world, self.coords)
NameError: global name 'world' is not defined

======================================================================
ERROR: test_bigbang (test.WorldTest)
----------------------------------------------------------------------
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-1jdskud/test.py", line 111, in test_bigbang
    self.assertEqual(len(world), 10)
TypeError: object of type 'World' has no len()

======================================================================
ERROR: test_cell (test.WorldTest)
----------------------------------------------------------------------
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-1jdskud/test.py", line 139, in test_cell
    self.assertEqual(cell.contents.energy, 5)
AttributeError: 'Cell' object has no attribute 'contents'

======================================================================
ERROR: test_cell_empty (test.WorldTest)
----------------------------------------------------------------------
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-1jdskud/test.py", line 142, in test_cell_empty
    emptycell = Cell()
TypeError: __init__() missing 1 required positional argument: 'content'

======================================================================
ERROR: test_world_manipulation (test.WorldTest)
----------------------------------------------------------------------
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-1jdskud/test.py", line 134, in test_world_manipulation
    self.assertEqual(world[row][col].contents.energy, 5)
AttributeError: 'Cell' object has no attribute 'contents'

======================================================================
FAIL: test_cell_invalid (test.WorldTest)
----------------------------------------------------------------------
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-1jdskud/test.py", line 147, in test_cell_invalid
    badcell = Cell("snakesandladders")
AssertionError: TypeError not raised

----------------------------------------------------------------------
Ran 13 tests in 0.021s

FAILED (failures=1, errors=11)

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

Георги обнови решението на 15.05.2013 16:56 (преди над 11 години)

+class Vec2D:
+
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __neg__(self):
+ self.x = -self.x
+ self.y = -self.y
+
+ def __add__(self, other):
+ self.x += other.x
+ self.y += other.y
+ return self
+
+ def __sub__(self, other):
+ self.x -= other.x
+ self.y -= other.y
+ return self
+
+
+class Cell:
+ def __init__(self, content):
+ self.content = content
+
+ def is_empty(self):
+ return (self.content is None)
+
+ def __str__(self):
+ if(self.is_empty()):
+ return ".."
+ return self.content.__str__()
+
+
+class World:
+ def __init__(self, width):
+ self.world_2D = []
+ self.len = width
+
+ for i in range(width):
+ self.world_2D.append(list(Cell(None) for x in range(width)))
+
+ def __str__(self):
+ result = ""
+ for i in range(self.len):
+ for cell_row in self.world_2D:
+ result += cell_row[i].__str__()
+ result += "\n"
+ return result
+
+ def __getitem__(self, index):
+ return self.world_2D[index]
+
+ def len(self):
+ return len
+
+
+class WorldObject:
+
+ def __init__(self, world_char):
+ self.world_char = world_char
+
+ def __str__(self):
+ return self.world_char
+
+
+class Food(WorldObject):
+ def __init__(self, energy):
+ self.energy = energy
+ self.world_char = ":" + str(energy)
+
+
+class PythonPart(WorldObject):
+ def __init__(self, world, coords):
+ self.coords = coords
+ self.world_char = "##"
+ self.world = world
+ self.world[coords.x][coords.y] = Cell(self)
+
+ def clear(self):
+ self.world[self.coords.x][self.coords.y] = Cell(None)
+
+
+class Python(PythonPart):
+ 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_char = "@@"
+ self.coords = coords
+ self.size = size
+ self.world = world
+ self.world[self.coords.x][self.coords.y] = Cell(self)
+ self.body = []
+ self.body.append(self)
+
+ self.tail_vector = Vec2D(self.coords.x, self.coords.y)
+
+ for body_part_index in range(size):
+ self.tail_vector = self.tail_vector - direction
+ self.body.append(PythonPart(world, self.tail_vector))
+
+ def move(self, direction):
+ self.body[self.size].clear()
+ self.coords = self.coords + direction
+ self.world[self.coords.x][self.coords.y] = Cell(self)
+
+ for i in reversed(range(self.size-2)):
+ self.body[i] = self.body[i-1]
+
+ self.body[1] = PythonPart(world, self.coords)
+
+
+class Death(Exception):
+ pass

Георги обнови решението на 15.05.2013 16:57 (преди над 11 години)

class Vec2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __neg__(self):
self.x = -self.x
self.y = -self.y
def __add__(self, other):
self.x += other.x
self.y += other.y
return self
def __sub__(self, other):
self.x -= other.x
self.y -= other.y
return self
class Cell:
def __init__(self, content):
self.content = content
def is_empty(self):
return (self.content is None)
def __str__(self):
if(self.is_empty()):
return ".."
return self.content.__str__()
class World:
def __init__(self, width):
self.world_2D = []
self.len = width
for i in range(width):
self.world_2D.append(list(Cell(None) for x in range(width)))
def __str__(self):
result = ""
for i in range(self.len):
for cell_row in self.world_2D:
result += cell_row[i].__str__()
result += "\n"
return result
def __getitem__(self, index):
return self.world_2D[index]
def len(self):
return len
class WorldObject:
def __init__(self, world_char):
self.world_char = world_char
def __str__(self):
return self.world_char
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
self.world_char = ":" + str(energy)
class PythonPart(WorldObject):
def __init__(self, world, coords):
self.coords = coords
self.world_char = "##"
self.world = world
self.world[coords.x][coords.y] = Cell(self)
def clear(self):
self.world[self.coords.x][self.coords.y] = Cell(None)
class Python(PythonPart):
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_char = "@@"
self.coords = coords
self.size = size
self.world = world
self.world[self.coords.x][self.coords.y] = Cell(self)
self.body = []
self.body.append(self)
self.tail_vector = Vec2D(self.coords.x, self.coords.y)
for body_part_index in range(size):
self.tail_vector = self.tail_vector - direction
self.body.append(PythonPart(world, self.tail_vector))
def move(self, direction):
self.body[self.size].clear()
+ self.body[1] = PythonPart(world, self.coords)
self.coords = self.coords + direction
+ self.body[0] = self
self.world[self.coords.x][self.coords.y] = Cell(self)
for i in reversed(range(self.size-2)):
self.body[i] = self.body[i-1]
- self.body[1] = PythonPart(world, self.coords)
class Death(Exception):
pass