Решение на Питоните хапят! от Иван Латунов

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

Към профила на Иван Латунов

Резултати

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

Код

import math
WORLD_WIDTH = 2
class Vec2D:
def __init__(self, x, y):
self._x = x
self._y = y
def __add__(self, other):
to_return = Vec2D(0, 0)
to_return._x = self._x + other._x
to_return._y = self._y + other._y
return to_return
def __iadd__(self, other):
self = self.__add__(other)
return self
def __sub__(self, other):
to_return = Vec2D(0, 0)
to_return._x = self._x - other._x
to_return._y = self._y - other._y
return to_return
def __isub__(self, other):
self = self.__sub__(other)
return self
def __mul__(self, value):
self._x *= value
self._y *= value
return self
def __neg__(self):
return Vec2D(-self._x, -self._y)
def __eq__(self, other):
return self._x == other._x and self._y == other._y
def __str__(self):
return "v(%d, %d)" % (self._x, self._y)
def __repr__(self):
return "<%s %s>" % (self.__class__.__name__, self.__str__())
def __getitem__(self, value):
if value % 2 == 0:
return self._x
if value % 2 == 1:
return self._y
def len(self):
return math.sqrt(self._x ** 2 + self._y ** 2)
class WorldObject:
def __init__(self, coords=Vec2D(0, 0)):
self.coords = coords
def __repr__(self):
return "<class %s> at %s" % (self.__class__.__name__, str(self.coords))
class Death(Exception):
pass
class Cell:
__COMPONENT__ = '.'
def __init__(self, contents=None):
self._contents = None
self._is_empty = True
self.contents = contents
def is_empty(self):
return self._is_empty
def clear(self):
self._contents = None
self._is_empty = True
@property
def contents(self):
return self._contents
@contents.setter
def contents(self, contents):
if (isinstance(contents, WorldObject) and
not isinstance(self._contents, PythonPart)):
self._contents = contents
if self.contents is None:
self._is_empty = True
else:
self._is_empty = False
def __str__(self):
if self._contents is not None:
return self._contents.__str__()
else:
return self.__COMPONENT__ * WORLD_WIDTH
class Food(WorldObject):
def __init__(self, energy=0, coords=Vec2D(0, 0)):
super(Food, self).__init__(coords)
self.energy = energy
def __str__(self):
return ':%s' % (self.energy)
class World:
def __init__(self, width):
self.width = (int)(abs(width))
self.cells = [[Cell() for col in range(width)] for row in range(width)]
#self.cells = [list(i) for i in zip(*self.cells)]
def __len__(self):
return self.width
def __getitem__(self, k):
if k < 0 or k >= self.width:
raise IndexError
return self.cells[k]
def __str__(self):
to_return = ''
for x in range(self.width):
for y in range(self.width):
to_return += self.cells[x][y].__str__()
to_return += '\n'
return to_return
class PythonPart(WorldObject):
__COMPONENT__ = '#'
def __init__(self):
super(PythonPart, self).__init__()
def __str__(self):
return self.__COMPONENT__ * WORLD_WIDTH
class PythonHead(PythonPart):
__COMPONENT__ = '*'
def __str__(self):
return self.__COMPONENT__ * WORLD_WIDTH
class Python:
LEFT = Vec2D(-1, 0)
RIGHT = Vec2D(1, 0)
UP = Vec2D(0, 1)
DOWN = Vec2D(0, -1)
def __plot__(self):
x = self.coords[0]
y = self.coords[1]
self.world[y][x] = PythonHead()
for a in range(self.size):
x -= self.direction._x
y -= self.direction._y
self.world[y][x] = PythonPart()
def __init__(self, world, coords, size, direction):
'''MORE TO BE DONE HERE'''
self.world = world
self.coords = coords
self.size = size
self.direction = direction
self.last_pos = coords - direction * size
self.__plot__()
def move(self, direction):
pass

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

FEEEEEE...F..
======================================================================
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-c6rc3c/test.py", line 98, in test_move_backwards
    py = Python(world, Vec2D(10, 10), 3, Python.LEFT)
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 183, in __init__
    self.__plot__()
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 173, in __plot__
    self.world[y][x] = PythonPart()
IndexError: list assignment index out of range

======================================================================
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-c6rc3c/test.py", line 39, in test_ouroboros_death
    py = Python(world, Vec2D(10, 10), 5, Python.LEFT)
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 183, in __init__
    self.__plot__()
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 173, in __plot__
    self.world[y][x] = PythonPart()
IndexError: list assignment index out of range

======================================================================
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-c6rc3c/test.py", line 27, in test_python_movement_basic
    py = Python(world, Vec2D(10, 10), py_size, direction)
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 183, in __init__
    self.__plot__()
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 173, in __plot__
    self.world[y][x] = PythonPart()
IndexError: list assignment index out of range

======================================================================
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-c6rc3c/test.py", line 14, in test_python_placement
    self.assertIsInstance(world[10][10].contents, PythonHead)
AttributeError: 'PythonHead' 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-c6rc3c/test.py", line 58, in test_snake_death
    py1 = Python(world, Vec2D(5, 5), 3, Python.UP)
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 183, in __init__
    self.__plot__()
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 173, in __plot__
    self.world[y][x] = PythonPart()
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 131, in __getitem__
    raise IndexError
IndexError

======================================================================
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-c6rc3c/test.py", line 51, in test_wallpunch_death
    py = Python(world, Vec2D(5, 5), 3, Python.LEFT)
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 183, in __init__
    self.__plot__()
  File "/tmp/d20130606-14014-c6rc3c/solution.py", line 173, in __plot__
    self.world[y][x] = PythonPart()
IndexError: list assignment index out of range

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

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

----------------------------------------------------------------------
Ran 13 tests in 0.068s

FAILED (failures=2, errors=6)

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

Иван обнови решението на 15.05.2013 13:30 (преди над 11 години)

+import math
+
+class Vec2D:
+ def __init__(self, x, y):
+ self._x = x
+ self._y = y
+
+ def __add__(self, other):
+ to_return = Vec2D(0, 0)
+
+ to_return._x = self._x + other._x
+ to_return._y = self._y + other._y
+
+ return to_return
+
+ def __iadd__(self, other):
+ self = self.__add__(other)
+ return self
+
+ def __sub__(self, other):
+ to_return = Vec2D(0, 0)
+
+ to_return._x = self._x - other._x
+ to_return._y = self._y - other._y
+
+ return to_return
+
+ def __isub__(self, other):
+ self = self.__sub__(other)
+ return self
+
+ def __mul__(self, value):
+ self._x *= value
+ self._y *= value
+
+ def __str__(self):
+ return "v(%d, %d)" % (self._x, self._y)
+
+ def __repr__(self):
+ return "<%s %s>" % (self.__class__.__name__, self.__str__())
+
+ def len(self):
+ return math.sqrt(self._x ** 2 + self._y ** 2)
+
+
+class WorldObject:
+ def __init__(self, coords=Vec2D(0, 0)):
+ self.coords = coords
+
+ def __repr__(self):
+ return "<class %s> at %s" % (self.__class__.__name__, str(self.coords))
+
+
+class Death(Exception):
+ pass
+
+
+class Cell:
+ def __init__(self, contents=None):
+ if isinstance(contents, WorldObject):
+ self.contents = contents
+ else:
+ self.contents = None
+
+ if self.contents == None:
+ self._is_empty = True
+ else:
+ self._is_empty = False
+
+ def is_empty(self):
+ return self._is_empty
+
+
+class Food(WorldObject):
+ def __init__(self, energy=0, coords=Vec2D(0, 0)):
+ super(Food, self).__init__(coords)
+ self.energy = energy
+
+
+class World:
+ def __init__(self, width):
+ self.__width__ = (int)(abs(width))
+ self.__cells__ = [[Cell() for x in range(width)] for x in range(width)]
+
+ def __len__(self):
+ return self.__width__
+
+ def __getitem__(self, k):
+ if k < 1 or k >= self.__width__:
+ raise IndexError
+ return self._cells[k]
+
+
+class PythonPart(WorldObject):
+ pass
+
+
+class PythonHead(PythonPart):
+ pass

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

import math
+
+WORLD_WIDTH = 2
+
+
class Vec2D:
def __init__(self, x, y):
self._x = x
self._y = y
def __add__(self, other):
to_return = Vec2D(0, 0)
-
to_return._x = self._x + other._x
to_return._y = self._y + other._y
-
+
return to_return
def __iadd__(self, other):
self = self.__add__(other)
return self
def __sub__(self, other):
to_return = Vec2D(0, 0)
-
+
to_return._x = self._x - other._x
to_return._y = self._y - other._y
-
+
return to_return
def __isub__(self, other):
self = self.__sub__(other)
return self
def __mul__(self, value):
self._x *= value
self._y *= value
-
- def __str__(self):
+ return self
+
+ def __neg__(self):
+ return Vec2D(-self._x, -self._y)
+
+ def __eq__(self, other):
+ return self._x == other._x and self._y == other._y
+
+ def __str__(self):
return "v(%d, %d)" % (self._x, self._y)
- def __repr__(self):
+ def __repr__(self):
return "<%s %s>" % (self.__class__.__name__, self.__str__())
+ def __getitem__(self, value):
+ if value % 2 == 0:
+ return self._x
+ if value % 2 == 1:
+ return self._y
+
def len(self):
return math.sqrt(self._x ** 2 + self._y ** 2)
class WorldObject:
def __init__(self, coords=Vec2D(0, 0)):
self.coords = coords
-
+
def __repr__(self):
return "<class %s> at %s" % (self.__class__.__name__, str(self.coords))
class Death(Exception):
pass
class Cell:
+ __COMPONENT__ = '.'
+
def __init__(self, contents=None):
- if isinstance(contents, WorldObject):
- self.contents = contents
- else:
- self.contents = None
+ self._contents = None
+ self._is_empty = True
+ self.contents = contents
- if self.contents == None:
+ def is_empty(self):
+ return self._is_empty
+
+ def clear(self):
+ self._contents = None
+ self._is_empty = True
+
+ @property
+ def contents(self):
+ return self._contents
+
+ @contents.setter
+ def contents(self, contents):
+ if (isinstance(contents, WorldObject) and
+ not isinstance(self._contents, PythonPart)):
+ self._contents = contents
+
+ if self.contents is None:
self._is_empty = True
else:
self._is_empty = False
- def is_empty(self):
- return self._is_empty
+ def __str__(self):
+ if self._contents is not None:
+ return self._contents.__str__()
+ else:
+ return self.__COMPONENT__ * WORLD_WIDTH
class Food(WorldObject):
def __init__(self, energy=0, coords=Vec2D(0, 0)):
super(Food, self).__init__(coords)
self.energy = energy
+ def __str__(self):
+ return ':%s' % (self.energy)
+
class World:
def __init__(self, width):
- self.__width__ = (int)(abs(width))
- self.__cells__ = [[Cell() for x in range(width)] for x in range(width)]
+ self.width = (int)(abs(width))
+ self.cells = [[Cell() for col in range(width)] for row in range(width)]
+ #self.cells = [list(i) for i in zip(*self.cells)]
def __len__(self):
- return self.__width__
+ return self.width
def __getitem__(self, k):
- if k < 1 or k >= self.__width__:
+ if k < 0 or k >= self.width:
raise IndexError
- return self._cells[k]
+ return self.cells[k]
+ def __str__(self):
+ to_return = ''
+ for x in range(self.width):
+ for y in range(self.width):
+ to_return += self.cells[x][y].__str__()
+ to_return += '\n'
+ return to_return
+
class PythonPart(WorldObject):
- pass
+ __COMPONENT__ = '#'
+ def __init__(self):
+ super(PythonPart, self).__init__()
+ def __str__(self):
+ return self.__COMPONENT__ * WORLD_WIDTH
+
+
class PythonHead(PythonPart):
- pass
+ __COMPONENT__ = '*'
+
+ def __str__(self):
+ return self.__COMPONENT__ * WORLD_WIDTH
+
+
+class Python:
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+ UP = Vec2D(0, 1)
+ DOWN = Vec2D(0, -1)
+
+ def __plot__(self):
+ x = self.coords[0]
+ y = self.coords[1]
+ self.world[x][y] = PythonHead()
+ for a in range(self.size):
+ x -= self.direction._x
+ y -= self.direction._y
+ self.world[x][y] = PythonPart()
+
+ def __init__(self, world, coords, size, direction):
+ '''MORE TO BE DONE HERE'''
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.direction = direction
+ self.last_pos = coords - direction * size
+
+ self.__plot__()
+
+ def move(self, direction):
+ pass

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

import math
WORLD_WIDTH = 2
class Vec2D:
def __init__(self, x, y):
self._x = x
self._y = y
def __add__(self, other):
to_return = Vec2D(0, 0)
to_return._x = self._x + other._x
to_return._y = self._y + other._y
return to_return
def __iadd__(self, other):
self = self.__add__(other)
return self
def __sub__(self, other):
to_return = Vec2D(0, 0)
to_return._x = self._x - other._x
to_return._y = self._y - other._y
return to_return
def __isub__(self, other):
self = self.__sub__(other)
return self
def __mul__(self, value):
self._x *= value
self._y *= value
return self
def __neg__(self):
return Vec2D(-self._x, -self._y)
def __eq__(self, other):
return self._x == other._x and self._y == other._y
def __str__(self):
return "v(%d, %d)" % (self._x, self._y)
def __repr__(self):
return "<%s %s>" % (self.__class__.__name__, self.__str__())
def __getitem__(self, value):
if value % 2 == 0:
return self._x
if value % 2 == 1:
return self._y
def len(self):
return math.sqrt(self._x ** 2 + self._y ** 2)
class WorldObject:
def __init__(self, coords=Vec2D(0, 0)):
self.coords = coords
def __repr__(self):
return "<class %s> at %s" % (self.__class__.__name__, str(self.coords))
class Death(Exception):
pass
class Cell:
__COMPONENT__ = '.'
def __init__(self, contents=None):
self._contents = None
self._is_empty = True
self.contents = contents
def is_empty(self):
return self._is_empty
def clear(self):
self._contents = None
self._is_empty = True
@property
def contents(self):
return self._contents
@contents.setter
def contents(self, contents):
if (isinstance(contents, WorldObject) and
not isinstance(self._contents, PythonPart)):
self._contents = contents
if self.contents is None:
self._is_empty = True
else:
self._is_empty = False
def __str__(self):
if self._contents is not None:
return self._contents.__str__()
else:
return self.__COMPONENT__ * WORLD_WIDTH
class Food(WorldObject):
def __init__(self, energy=0, coords=Vec2D(0, 0)):
super(Food, self).__init__(coords)
self.energy = energy
def __str__(self):
return ':%s' % (self.energy)
class World:
def __init__(self, width):
self.width = (int)(abs(width))
self.cells = [[Cell() for col in range(width)] for row in range(width)]
#self.cells = [list(i) for i in zip(*self.cells)]
def __len__(self):
return self.width
def __getitem__(self, k):
if k < 0 or k >= self.width:
raise IndexError
return self.cells[k]
def __str__(self):
to_return = ''
for x in range(self.width):
for y in range(self.width):
to_return += self.cells[x][y].__str__()
to_return += '\n'
return to_return
class PythonPart(WorldObject):
__COMPONENT__ = '#'
def __init__(self):
super(PythonPart, self).__init__()
def __str__(self):
return self.__COMPONENT__ * WORLD_WIDTH
class PythonHead(PythonPart):
__COMPONENT__ = '*'
def __str__(self):
return self.__COMPONENT__ * WORLD_WIDTH
class Python:
LEFT = Vec2D(-1, 0)
RIGHT = Vec2D(1, 0)
UP = Vec2D(0, 1)
DOWN = Vec2D(0, -1)
def __plot__(self):
x = self.coords[0]
y = self.coords[1]
- self.world[x][y] = PythonHead()
+ self.world[y][x] = PythonHead()
for a in range(self.size):
x -= self.direction._x
y -= self.direction._y
- self.world[x][y] = PythonPart()
+ self.world[y][x] = PythonPart()
def __init__(self, world, coords, size, direction):
'''MORE TO BE DONE HERE'''
self.world = world
self.coords = coords
self.size = size
self.direction = direction
self.last_pos = coords - direction * size
self.__plot__()
def move(self, direction):
pass