Решение на Питоните хапят! от Георги Урумов

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

Към профила на Георги Урумов

Резултати

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

Код

class IndexError(Exception):
def __init__(self):
self.message = "Invalid index!"
class Death(Exception):
def __init__(self):
self.message = "You died! Couse NOOB"
class ValueError(Exception):
def __init__(self):
self.message = "Invalid direction!"
class WorldObject(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
class Food(WorldObject):
def __init__(self, energy=0, x=0, y=0):
self.energy = energy
WorldObject.__init__(self, x, y)
def __str__(self):
return ":3"
class PythonPart(WorldObject):
def __init__(self, x=0, y=0):
WorldObject.__init__(self, x, y)
def __str__(self):
return "##"
class PythonHead(PythonPart):
def __init__(self, x=0, y=0):
PythonPart.__init__(self, x, y)
def __str__(self):
return "@@"
class Vec2D(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __add__(self, other):
if (isinstance(other, Vec2D)):
return Vec2D(self.x + other.x, self.y + other.y)
def __sub__(self, other):
if (isinstance(other, Vec2D)):
return Vec2D(self.x - other.x, self.y - other.y)
def __mul__(self, other):
if (isinstance(other, int)):
return Vec2D(self.x * other, self.y * other)
def __neg__(self):
return self * -1
class Cell(object):
def __init__(self, content=".."):
if (content == ".."):
self.contents = content
elif (isinstance(content, WorldObject)):
self.contents = content
else:
raise TypeError
def __str__(self):
return self.contents
def is_empty(self):
return (self.contents == "..")
class PositiveArray(object):
def __init__(self, array):
self.array = array
def __getitem__(self, coordinate):
if (coordinate < 0 or coordinate >= len(self.array)):
raise IndexError
else:
return self.array[coordinate]
def __setitem__(self, key, value):
if (key < 0 or key >= len(self.array)):
raise IndexError
#elif (self.array[key] != ".." or not isinstance(value, Cell)):
# raise ValueError
else:
self.array[key] = value
class World(object):
def __init__(self, width):
self.width = width
self.world = PositiveArray([self.makeRows() for i in range(width)])
def makeRows(self):
return PositiveArray([Cell() for i in range(self.width)])
def __len__(self):
return self.width
def __getitem__(self, coords):
return self.world[coords]
def __setitem__(self, key, value):
self.world[key] = value
def __str__(self):
for i in range(self.width):
for j in range(self.width):
print (self.world[j][i].__str__(), end="")
print ("\n")
return ""
class Python(object):
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 = world
self.energy = 0
self.initialized = 0
self.direction = direction
self.head = coords
self.body = [PythonHead(coords.x, coords.y)]
for i in range(size):
bodypart = coords - self.direction * (i + 1)
self.body.append(PythonPart(bodypart.x, bodypart.y))
self.updateWorld()
def move(self, direction):
if (direction == -self.direction):
raise ValueError
else:
self.direction = direction
oldHead = PythonPart(self.head.x, self.head.y)
self.head = self.head + direction
self.body.pop(0)
self.body.insert(0, oldHead)
self.body.insert(0, PythonHead(self.head.x, self.head.y))
self.updateWorld()
def updateWorld(self):
try:
cell = self.world[self.head.x][self.head.y]
except IndexError:
raise Death
if (not cell.is_empty() and not isinstance(cell.contents, Food)):
raise Death
elif (not self.initialized):
for i in range(len(self.body)):
self.world[self.body[i].x][self.body[i].y] = Cell(self.body[i])
self.initialized = 1
else:
for i in range(2):
self.world[self.body[i].x][self.body[i].y] = Cell(self.body[i])
if (isinstance(cell.contents, Food)):
self.energy += cell.contents.energy
else:
lastPart = self.body.pop()
self.world[lastPart.x][lastPart.y] = Cell()

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

EE.EEE.......
======================================================================
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-t8ifd3/test.py", line 86, in test_growth
    self.assertEqual(py.size, 3)
AttributeError: 'Python' object has no attribute 'size'

======================================================================
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-t8ifd3/test.py", line 105, in test_move_backwards
    py.move(-direction)
  File "/tmp/d20130606-14014-t8ifd3/solution.py", line 168, in move
    self.updateWorld()
  File "/tmp/d20130606-14014-t8ifd3/solution.py", line 176, in updateWorld
    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-t8ifd3/test.py", line 30, in test_python_movement_basic
    x, y = Vec2D(10, 10) + direction
TypeError: 'Vec2D' object is not iterable

======================================================================
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-t8ifd3/test.py", line 18, in test_python_placement
    x, y = position - direction
TypeError: 'Vec2D' object is not iterable

======================================================================
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-t8ifd3/test.py", line 68, in test_snake_death
    py2 = Python(world, Vec2D(5, 8), 3, Python.UP)
  File "/tmp/d20130606-14014-t8ifd3/solution.py", line 156, in __init__
    self.updateWorld()
  File "/tmp/d20130606-14014-t8ifd3/solution.py", line 176, in updateWorld
    raise Death
solution.Death

----------------------------------------------------------------------
Ran 13 tests in 0.039s

FAILED (errors=5)

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

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

+class IndexError(Exception):
+
+ def __init__(self):
+ self.message = "Invalid index!"
+
+
+class Death(Exception):
+
+ def __init__(self):
+ self.message = "You died! Couse NOOB"
+
+
+class ValueError(Exception):
+
+ def __init__(self):
+ self.message = "Invalid direction!"
+
+
+class WorldObject(object):
+
+ def __init__(self, x=0, y=0):
+ self.x = x
+ self.y = y
+
+
+class Food(WorldObject):
+
+ def __init__(self, energy=0, x=0, y=0):
+ self.energy = energy
+ WorldObject.__init__(self, x, y)
+
+ def __str__(self):
+ return ":3"
+
+
+class PythonPart(WorldObject):
+
+ def __init__(self, x=0, y=0):
+ WorldObject.__init__(self, x, y)
+
+ def __str__(self):
+ return "##"
+
+
+class PythonHead(PythonPart):
+
+ def __init__(self, x=0, y=0):
+ PythonPart.__init__(self, x, y)
+
+ def __str__(self):
+ return "@@"
+
+
+class Vec2D(object):
+
+ def __init__(self, x=0, y=0):
+ self.x = x
+ self.y = y
+
+ def __add__(self, other):
+ if (isinstance(other, Vec2D)):
+ return Vec2D(self.x + other.x, self.y + other.y)
+
+ def __sub__(self, other):
+ if (isinstance(other, Vec2D)):
+ return Vec2D(self.x - other.x, self.y - other.y)
+
+ def __mul__(self, other):
+ if (isinstance(other, int)):
+ return Vec2D(self.x * other, self.y * other)
+
+ def __neg__(self):
+ return self * -1
+
+
+class Cell(object):
+
+ def __init__(self, content=".."):
+ if (content == ".."):
+ self.contents = content
+ elif (isinstance(content, WorldObject)):
+ self.contents = content
+ else:
+ raise TypeError
+
+ def __str__(self):
+ return self.contents
+
+ def is_empty(self):
+ return (self.contents == "..")
+
+
+class PositiveArray(object):
+
+ def __init__(self, array):
+ self.array = array
+
+ def __getitem__(self, coordinate):
+ if (coordinate < 0 or coordinate >= len(self.array)):
+ raise IndexError
+ else:
+ return self.array[coordinate]
+
+ def __setitem__(self, key, value):
+ if (key < 0 or key >= len(self.array)):
+ raise IndexError
+ #elif (self.array[key] != ".." or not isinstance(value, Cell)):
+ # raise ValueError
+ else:
+ self.array[key] = value
+
+
+class World(object):
+
+ def __init__(self, width):
+ self.width = width
+ self.world = PositiveArray([self.makeRows() for i in range(width)])
+
+ def makeRows(self):
+ return PositiveArray([Cell() for i in range(self.width)])
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, coords):
+ return self.world[coords]
+
+ def __setitem__(self, key, value):
+ self.world[key] = value
+
+ def __str__(self):
+ for i in range(self.width):
+ for j in range(self.width):
+ print (self.world[j][i].__str__(), end="")
+ print ("\n")
+ return ""
+
+
+class Python(object):
+
+ 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 = world
+ self.energy = 0
+ self.initialized = 0
+ self.direction = direction
+ self.head = coords
+ self.body = [PythonHead(coords.x, coords.y)]
+ for i in range(size):
+ bodypart = coords - self.direction * (i + 1)
+ self.body.append(PythonPart(bodypart.x, bodypart.y))
+ self.updateWorld()
+
+ def move(self, direction):
+ if (direction == -self.direction):
+ raise ValueError
+ else:
+ self.direction = direction
+ oldHead = PythonPart(self.head.x, self.head.y)
+ self.head = self.head + direction
+ self.body.pop(0)
+ self.body.insert(0, oldHead)
+ self.body.insert(0, PythonHead(self.head.x, self.head.y))
+ self.updateWorld()
+
+ def updateWorld(self):
+ try:
+ cell = self.world[self.head.x][self.head.y]
+ except IndexError:
+ raise Death
+ if (not cell.is_empty() and not isinstance(cell.contents, Food)):
+ raise Death
+ elif (not self.initialized):
+ for i in range(len(self.body)):
+ self.world[self.body[i].x][self.body[i].y] = Cell(self.body[i])
+ self.initialized = 1
+ else:
+ for i in range(2):
+ self.world[self.body[i].x][self.body[i].y] = Cell(self.body[i])
+ if (isinstance(cell.contents, Food)):
+ self.energy += cell.contents.energy
+ else:
+ lastPart = self.body.pop()
+ self.world[lastPart.x][lastPart.y] = Cell()