Решение на Питоните хапят! от Ния Дончева

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

Към профила на Ния Дончева

Резултати

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

Код

class Vec2D:
def __init__(self, x, y=None):
if y is None:
self.x = x[0]
self.y = x[1]
else:
self.x = x
self.y = y
def __add__(self, other):
return Vec2D(self.x + other[0], self.y + other[1])
def __sub__(self, other):
return Vec2D(self.x - other[0], self.y - other[1])
def __mul__(self, number):
return Vec2D(self.x * number, self.y * number)
def __eq__(self, other):
return self.x == other[0] and self.y == other[1]
def __neg__(self):
return Vec2D(-self.x, -self.y)
def __len__(self):
return 2
def __getitem__(self, key):
if key == 0:
return self.x
elif key == 1:
return self.y
else:
raise IndexError
def __setitem__(self, key, value):
if key == 0:
self.x = value
elif key == 1:
self.y = value
else:
raise IndexError
def __str__(self):
return '({}, {})'.format(self.x, self.y)
class World:
def __init__(self, width):
self.world = []
self.length = width
for i in range(0, width):
row = []
for j in range(0, width):
row.append(Cell())
self.world.append(row)
def __str__(self):
world = ''
for row in self.world:
line = ''
for cell in row:
line += cell.__str__()
line += '\n'
world += line
return world
def __getitem__(self, key):
return self.world[key]
def __setitem__(self, key, value):
self.world[key] = value
def __len__(self):
return self.length
class Cell:
def __init__(self, contents=None):
if contents:
if isinstance(contents, WorldObject):
self.contents = contents
self.empty = False
else:
raise TypeError
else:
self.contents = '..'
self.empty = True
def is_empty(self):
return self.empty
def __str__(self):
return self.contents.__str__()
class WorldObject:
pass
class Food(WorldObject):
def __init__(self, energy):
self.food = ':3'
self.energy = energy
def __str__(self):
return self.food.__str__()
class PythonPart(WorldObject):
def __init__(self):
self.body = '##'
self.coords = None
def __str__(self):
return self.body
class PythonHead(PythonPart):
def __init__(self, size):
self.head = '@@'
self.parts = []
self.position = None
for i in range(0, size):
self.parts.append(super().__init__())
def __str__(self):
return self.head
class Python:
LEFT = Vec2D(0, -1)
RIGHT = Vec2D(0, 1)
UP = Vec2D(-1, 0)
DOWN = Vec2D(1, 0)
def __init__(self, world, coords, size, direction):
self.python = PythonHead(size)
self.parts = []
self.world = world
self.energy = 0
self.size = size
self.direction = direction
self.head = self.python
for part in self.python.parts:
self.parts.append(PythonPart())
self.python.position = coords
current_coords = coords
world[coords.x][coords.y] = Cell(self.python)
for part in self.parts:
part.coords = current_coords - direction
current_coords -= direction
world[current_coords.x][current_coords.y] = Cell(PythonPart())
def move(self, direction):
if direction is not self.direction:
self.direction = direction
new_position = self.python.position + direction
if new_position.x in range(0, len(self.world))and new_position.y in range(0, len(self.world)):
current = self.world[new_position.x][new_position.y]
check = False
if isinstance(current.contents, Food):
check = True
self.size += 1
self.energy += current.contents.energy
if current.is_empty() or check is True:
last_position = self.python.position
self.world[new_position.x][new_position.y] = Cell(self.python)
for part in self.parts:
self.world[last_position.x][last_position.y] = Cell(part)
temporary = last_position
last_position = part.coords
part.coords = temporary
if check is True:
self.parts.append(PythonPart())
self.world[last_position.x][last_position.y] = self.parts[self.size-1]
self.parts[self.size-1].coords = last_position
else:
self.world[last_position.x][last_position.y] = Cell()
self.python.position = new_position
else:
raise Death
else:
raise Death
class PythonError(Exception):
pass
class Death(PythonError):
pass

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

.E...........
======================================================================
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-at4fox/test.py", line 105, in test_move_backwards
    py.move(-direction)
  File "/tmp/d20130606-14014-at4fox/solution.py", line 191, in move
    raise Death
solution.Death

----------------------------------------------------------------------
Ran 13 tests in 0.040s

FAILED (errors=1)

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

Ния обнови решението на 15.05.2013 13:40 (преди над 11 години)

+class Vec2D:
+ def __init__(self, x, y=None):
+ if y is None:
+ self.x = x[0]
+ self.y = x[1]
+ else:
+ self.x = x
+ self.y = y
+
+ def __add__(self, other):
+ return Vec2D(self.x + other[0], self.y + other[1])
+
+ def __sub__(self, other):
+ return Vec2D(self.x - other[0], self.y - other[1])
+
+ def __mul__(self, number):
+ return Vec2D(self.x * number, self.y * number)
+
+ def __eq__(self, other):
+ return self.x == other[0] and self.y == other[1]
+
+ def __neg__(self):
+ return Vec2D(-self.x, -self.y)
+
+ def __len__(self):
+ return 2
+
+ def __getitem__(self, key):
+ if key == 0:
+ return self.x
+ elif key == 1:
+ return self.y
+ else:
+ raise IndexError
+
+ def __setitem__(self, key, value):
+ if key == 0:
+ self.x = value
+ elif key == 1:
+ self.y = value
+ else:
+ raise IndexError
+
+ def __str__(self):
+ return '({}, {})'.format(self.x, self.y)
+
+
+class World:
+ def __init__(self, width):
+ self.world = []
+ self.length = width
+ for i in range(0, width):
+ row = []
+ for j in range(0, width):
+ row.append(Cell())
+ self.world.append(row)
+
+ def __str__(self):
+ world = ''
+ for row in self.world:
+ line = ''
+ for cell in row:
+ line += cell.__str__()
+ line += '\n'
+ world += line
+ return world
+
+ def __getitem__(self, key):
+ return self.world[key]
+
+ def __setitem__(self, key, value):
+ self.world[key] = value
+
+ def __len__(self):
+ return self.length
+
+
+class Cell:
+ def __init__(self, contents=None):
+ if contents:
+ if isinstance(contents, WorldObject):
+ self.contents = contents
+ self.empty = False
+ else:
+ raise TypeError
+ else:
+ self.contents = '..'
+ self.empty = True
+
+ def is_empty(self):
+ return self.empty
+
+ def __str__(self):
+ return self.contents.__str__()
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+ def __init__(self, energy):
+ self.food = ':3'
+ self.energy = energy
+
+ def __str__(self):
+ return self.food.__str__()
+
+
+class PythonPart(WorldObject):
+ def __init__(self):
+ self.body = '##'
+ self.coords = None
+
+ def __str__(self):
+ return self.body
+
+
+class PythonHead(PythonPart):
+ def __init__(self, size):
+ self.head = '@@'
+ self.parts = []
+ self.position = None
+ for i in range(0, size):
+ self.parts.append(super().__init__())
+
+ def __str__(self):
+ return self.head
+
+
+class Python:
+ LEFT = Vec2D(0, -1)
+ RIGHT = Vec2D(0, 1)
+ UP = Vec2D(-1, 0)
+ DOWN = Vec2D(1, 0)
+
+ def __init__(self, world, coords, size, direction):
+ self.python = PythonHead(size)
+ self.parts = []
+ self.world = world
+ self.energy = 0
+ self.size = size
+ self.direction = direction
+ self.head = self.python
+
+ for part in self.python.parts:
+ self.parts.append(PythonPart())
+
+ self.python.position = coords
+ current_coords = coords
+ world[coords.x][coords.y] = Cell(self.python)
+
+ for part in self.parts:
+ part.coords = current_coords - direction
+ current_coords -= direction
+ world[current_coords.x][current_coords.y] = Cell(PythonPart())
+
+ def move(self, direction):
+ if direction is not self.direction:
+ self.direction = direction
+
+ new_position = self.python.position + direction
+
+ if new_position.x in range(0, len(self.world))and new_position.y in range(0, len(self.world)):
+ current = self.world[new_position.x][new_position.y]
+ check = False
+ if isinstance(current.contents, Food):
+ check = True
+ self.size += 1
+ self.energy += current.contents.energy
+
+ if current.is_empty() or check is True:
+ last_position = self.python.position
+ self.world[new_position.x][new_position.y] = Cell(self.python)
+
+ for part in self.parts:
+ self.world[last_position.x][last_position.y] = Cell(part)
+ temporary = last_position
+ last_position = part.coords
+ part.coords = temporary
+
+ if check is True:
+ self.parts.append(PythonPart())
+ self.world[last_position.x][last_position.y] = self.parts[self.size-1]
+ self.parts[self.size-1].coords = last_position
+ else:
+ self.world[last_position.x][last_position.y] = Cell()
+
+ self.python.position = new_position
+ else:
+ raise Death
+ else:
+ raise Death
+
+
+class PythonError(Exception):
+ pass
+
+
+class Death(PythonError):
+ pass