Решение на Питоните хапят! от Александър Тахчиев

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

Към профила на Александър Тахчиев

Резултати

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

Код

import math
class Column:
def __init__(self, height):
self.height = height
self.cells = height * [Cell()]
def __getitem__(self, y):
if y < 0 or y > self.height:
raise IndexError()
else:
return self.cells[y]
def __setitem__(self, y, cell):
if y < 0 or y > self.height:
raise IndexError()
else:
self.cells[y] = cell
class World:
def __init__(self, width):
self.width = width
self.matrix = width * [Column(width)]
def __len__(self):
return self.width
def __getitem__(self, x):
if x < 0 or x >= self.width:
raise IndexError()
else:
return self.matrix[x]
class Cell:
def __init__(self, _contents=None):
if isinstance(_contents, WorldObject) or _contents is None:
self.contents = _contents
else:
raise TypeError('expected world object')
def is_empty(self):
return self.contents is None
class WorldObject:
pass
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
class PythonPart(WorldObject):
pass
class PythonHead(PythonPart):
pass
class Vec2D:
def __init__(self, x, y):
self.x, self.y = x, 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):
return Vec2D(other * self.x, other * self.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 __iter__(self):
return IterVec(self)
class IterVec:
def __init__(self, vector):
self.i = 0
self.vector = vector
def __iter__(self):
return self
def __next__(self):
if self.i >= 2:
raise StopIteration
else:
self.i += 1
return [self.vector.x, self.vector.y][self.i - 1]
class Python:
LEFT = Vec2D(-1, 0)
RIGHT = Vec2D(1, 0)
UP = Vec2D(0, 1)
DOWN = Vec2D(0, -1)
energy = 0
def move(self, direction):
self.coords += direction
self.direction = direction
if self.coords.x < 0 or self.coords.y < 0 or\
self.coords.x > len(self.world) or self.coords.y > len(self.world):
raise Death
def __init__(self, world, coords, size, direction):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
self.world[coords.x][coords.y] = Cell(PythonHead())
print(self.world[coords.x][coords.y].contents)
for s in range(1, size + 1):
self.world[coords.x - direction.x * s]\
[coords.y - direction.y * s] = Cell(PythonPart())
class Death(Exception):
pass

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

F
Stdout:
<solution.PythonHead object at 0xb77c6a6c>
F
Stdout:
<solution.PythonHead object at 0xb77c6f6c>
F
Stdout:
<solution.PythonHead object at 0xb77d546c>
F
Stdout:
<solution.PythonHead object at 0xb77d594c>
.F
Stdout:
<solution.PythonHead object at 0xb77dc0ec>
<solution.PythonHead object at 0xb77dc36c>
<solution.PythonHead object at 0xb77dc3ec>
.......
======================================================================
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-1yit33d/test.py", line 92, in test_growth
    self.assertTrue(cartesian_coord or screen_coord)
AssertionError: False is not true

Stdout:
<solution.PythonHead object at 0xb77c6a6c>

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

Stdout:
<solution.PythonHead object at 0xb77c6f6c>

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

Stdout:
<solution.PythonHead object at 0xb77d546c>

======================================================================
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-1yit33d/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'>

Stdout:
<solution.PythonHead object at 0xb77d594c>

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

Stdout:
<solution.PythonHead object at 0xb77dc0ec>
<solution.PythonHead object at 0xb77dc36c>
<solution.PythonHead object at 0xb77dc3ec>

----------------------------------------------------------------------
Ran 13 tests in 0.009s

FAILED (failures=5)

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

Александър обнови решението на 15.05.2013 16:48 (преди над 11 години)

+import math
+
+
+class Column:
+ def __init__(self, height):
+ self.height = height
+ self.cells = height * [Cell()]
+
+ def __getitem__(self, y):
+ if y < 0 or y > self.height:
+ raise IndexError()
+ else:
+ return self.cells[y]
+
+ def __setitem__(self, y, cell):
+ if y < 0 or y > self.height:
+ raise IndexError()
+ else:
+ self.cells[y] = cell
+
+
+class World:
+
+ def __init__(self, width):
+ self.width = width
+ self.matrix = width * [Column(width)]
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, x):
+ if x < 0 or x >= self.width:
+ raise IndexError()
+ else:
+ return self.matrix[x]
+
+
+class Cell:
+
+ def __init__(self, _contents=None):
+ if isinstance(_contents, WorldObject) or _contents is None:
+ self.contents = _contents
+ else:
+ raise TypeError('expected world object')
+
+ def is_empty(self):
+ return self.contents is None
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+
+ def __init__(self, energy):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ pass
+
+
+class PythonHead(PythonPart):
+ pass
+
+
+class Vec2D:
+
+ def __init__(self, x, y):
+ self.x, self.y = x, 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):
+ return Vec2D(other * self.x, other * self.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 __iter__(self):
+ return IterVec(self)
+
+class IterVec:
+ def __init__(self, vector):
+ self.i = 0
+ self.vector = vector
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ if self.i >= 2:
+ raise StopIteration
+ else:
+ self.i += 1
+ return [self.vector.x, self.vector.y][self.i - 1]
+
+class Python:
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+ UP = Vec2D(0, 1)
+ DOWN = Vec2D(0, -1)
+ energy = 0
+
+ def move(self, direction):
+ self.coords += direction
+ self.direction = direction
+ if self.coords.x < 0 or self.coords.y < 0 or\
+ self.coords.x > len(self.world) or self.coords.y > len(self.world):
+ raise Death
+
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.direction = direction
+
+ self.world[coords.x][coords.y] = Cell(PythonHead())
+ print(self.world[coords.x][coords.y].contents)
+
+
+ for s in range(1, size + 1):
+ self.world[coords.x - direction.x * s]\
+ [coords.y - direction.y * s] = Cell(PythonPart())
+
+
+class Death(Exception):
+ pass

Александър обнови решението на 15.05.2013 16:51 (преди над 11 години)

import math
-
-
+
+
class Column:
+
def __init__(self, height):
self.height = height
self.cells = height * [Cell()]
-
+
def __getitem__(self, y):
if y < 0 or y > self.height:
raise IndexError()
else:
return self.cells[y]
-
+
def __setitem__(self, y, cell):
if y < 0 or y > self.height:
raise IndexError()
else:
self.cells[y] = cell
-
-
+
+
class World:
-
+
def __init__(self, width):
self.width = width
self.matrix = width * [Column(width)]
-
+
def __len__(self):
return self.width
-
+
def __getitem__(self, x):
if x < 0 or x >= self.width:
raise IndexError()
else:
return self.matrix[x]
-
-
+
+
class Cell:
-
+
def __init__(self, _contents=None):
if isinstance(_contents, WorldObject) or _contents is None:
self.contents = _contents
else:
raise TypeError('expected world object')
-
+
def is_empty(self):
return self.contents is None
-
-
+
+
class WorldObject:
pass
-
-
+
+
class Food(WorldObject):
-
+
def __init__(self, energy):
self.energy = energy
-
-
+
+
class PythonPart(WorldObject):
pass
-
-
+
+
class PythonHead(PythonPart):
pass
-
-
+
+
class Vec2D:
-
+
def __init__(self, x, y):
self.x, self.y = x, 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):
return Vec2D(other * self.x, other * self.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 __iter__(self):
return IterVec(self)
-
+
+
class IterVec:
+
def __init__(self, vector):
self.i = 0
self.vector = vector
-
+
def __iter__(self):
return self
-
+
def __next__(self):
if self.i >= 2:
raise StopIteration
else:
self.i += 1
return [self.vector.x, self.vector.y][self.i - 1]
-
+
+
class Python:
LEFT = Vec2D(-1, 0)
RIGHT = Vec2D(1, 0)
UP = Vec2D(0, 1)
DOWN = Vec2D(0, -1)
energy = 0
-
+
def move(self, direction):
self.coords += direction
self.direction = direction
if self.coords.x < 0 or self.coords.y < 0 or\
self.coords.x > len(self.world) or self.coords.y > len(self.world):
- raise Death
-
+ raise Death
+
def __init__(self, world, coords, size, direction):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
-
+
self.world[coords.x][coords.y] = Cell(PythonHead())
print(self.world[coords.x][coords.y].contents)
-
-
+
for s in range(1, size + 1):
self.world[coords.x - direction.x * s]\
[coords.y - direction.y * s] = Cell(PythonPart())
-
-
+
+
class Death(Exception):
- pass
+ pass