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

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

Към профила на Александър Петков

Резултати

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

Код

class Vec2D:
def __init__(self, x, y):
self.x = x
self.y = 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, number):
return Vec2D(number*self.x, number*self.y)
def __neg__(self):
return Vec2D(-self.x, -self.y)
def __str__(self):
return "[{},{}]".format(self.x, self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __iter__(self):
yield self.x
yield self.y
class World():
def __init__(self, width):
self.width = width
self.matrix = [[Cell() for i in range(width)] for j in range(width)]
def __getitem__(self, index):
if index >= self.width:
raise IndexError
else:
return self.matrix[index]
def __setitem__(self, index):
if index >= self.width:
raise IndexError
else:
return self.matrix[index]
def __str__(self):
result = ""
for i in range(self.width):
for j in range(self.width):
result += str(self.matrix[i][j])
result += "\n"
return result
def __len__(self):
return self.width
class WorldObject():
def __init__(self):
self.contents = self
class Cell():
def __init__(self, content=WorldObject()):
if not isinstance(content, WorldObject):
raise TypeError
self.contents = content
def is_empty(self):
return type(self.contents) not in [PythonPart, Food]
def __str__(self):
return ".."
class Food(WorldObject):
def __init__(self, energy=0):
self.energy = energy
def __str__(self):
return ":3"
class PythonPart(WorldObject):
def __str__(self):
return "##"
class PythonHead(PythonPart):
def __str__(self):
return "@@"
class Python:
RIGHT = Vec2D(0, 1)
LEFT = Vec2D(0, -1)
UP = Vec2D(-1, 0)
DOWN = Vec2D(1, 0)
def __init__(self, world, coords, size, direction):
world.matrix[coords.x][coords.y] = PythonHead()
self.tail = []
self.tail.append(coords)
current = coords
for i in range(size):
current -= direction
if current.x < 0 or current.y < 0:
raise IndexError
else:
world.matrix[current.x][current.y] = PythonPart()
self.tail.append(current)
self.world = world
self.head_pos = coords
self.size = size
self.direction = direction
self.energy = 0
def _add_to_tail(self, new_coords):
self.tail.insert(0, new_coords)
def _remove_from_tail(self):
return self.tail.pop(len(self.tail) - 1)
def move(self, direction):
if self.direction == -direction:
raise ValueError
new_pos = self.head_pos + direction
new_x, new_y, size = new_pos.x, new_pos.y, self.world.width
exist = any(map(lambda x: x < 0 or x >= size, [new_x, new_y]))
if exist or isinstance(self.world.matrix[new_x][new_y], PythonPart):
raise Death
else:
is_there_food = isinstance(self.world.matrix[new_x][new_y], Food)
if is_there_food:
self.energy += self.world.matrix[new_x][new_y].energy
self.world.matrix[new_x][new_y] = PythonHead()
self._add_to_tail(new_pos)
self.direction = direction
self.world.matrix[self.head_pos.x][self.head_pos.y] = PythonPart()
if not is_there_food:
to_be_removed = self._remove_from_tail()
self.world.matrix[to_be_removed.x][to_be_removed.y] = Cell()
self.head_pos = new_pos
class Death(Exception):
pass

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

F............
======================================================================
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-12cbojn/test.py", line 94, in test_growth
    self.assertEqual(py.size, 4)
AssertionError: 3 != 4

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

FAILED (failures=1)

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

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

+class Vec2D:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = 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, number):
+ return Vec2D(number*self.x, number*self.y)
+
+ def __neg__(self):
+ return Vec2D(-self.x, -self.y)
+
+ def __str__(self):
+ return "[{},{}]".format(self.x, self.y)
+
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
+ def __iter__(self):
+ yield self.x
+ yield self.y
+
+
+class World():
+ def __init__(self, width):
+ self.width = width
+ self.matrix = [[Cell() for i in range(width)] for j in range(width)]
+
+ def __getitem__(self, index):
+ if index >= self.width:
+ raise IndexError
+ else:
+ return self.matrix[index]
+
+ def __setitem__(self, index):
+ if index >= self.width:
+ raise IndexError
+ else:
+ return self.matrix[index]
+
+ def __str__(self):
+ result = ""
+ for i in range(self.width):
+ for j in range(self.width):
+ result += str(self.matrix[i][j])
+ #result += " red nomer {}\n".format(i)
+ result += "\n"
+ return result
+
+ def __len__(self):
+ return self.width
+
+
+class WorldObject():
+ def __init__(self):
+ self.contents = self
+
+
+class Cell():
+ def __init__(self, content=WorldObject()):
+ if not isinstance(content, WorldObject):
+ raise TypeError
+ self.contents = content
+
+ def is_empty(self):
+ return type(self.contents) not in [PythonPart, Food]
+
+ def __str__(self):
+ return ".."
+
+
+class Food(WorldObject):
+ def __init__(self, energy=0):
+ self.energy = energy
+
+ def __str__(self):
+ return ":3"
+
+
+class PythonPart(WorldObject):
+ def __str__(self):
+ return "##"
+
+
+class PythonHead(PythonPart):
+ def __str__(self):
+ return "@@"
+
+
+class Python:
+ RIGHT = Vec2D(0, 1)
+ LEFT = Vec2D(0, -1)
+ UP = Vec2D(-1, 0)
+ DOWN = Vec2D(1, 0)
+
+ def __init__(self, world, coords, size, direction):
+ world.matrix[coords.x][coords.y] = PythonHead()
+ self.tail = []
+ self.tail.append(coords)
+ current = coords
+ for i in range(size):
+ current -= direction
+ if current.x < 0 or current.y < 0:
+ raise IndexError
+ else:
+ world.matrix[current.x][current.y] = PythonPart()
+ self.tail.append(current)
+
+ self.world = world
+ self.head_pos = coords
+ self.size = size
+ self.direction = direction
+ self.energy = 0
+
+ def _add_to_tail(self, new_coords):
+ self.tail.insert(0, new_coords)
+
+ def _remove_from_tail(self):
+ return self.tail.pop(len(self.tail) - 1)
+
+ def move(self, direction):
+ if self.direction == -direction:
+ raise ValueError
+ new_pos = self.head_pos + direction
+ new_x, new_y, size = new_pos.x, new_pos.y, self.world.width
+ exist = any(map(lambda x: x < 0 or x >= size, [new_x, new_y]))
+ if exist or isinstance(self.world.matrix[new_x][new_y], PythonPart):
+ raise Death
+ else:
+ self.world.matrix[new_x][new_y] = PythonHead()
+ self._add_to_tail(new_pos)
+ self.direction = direction
+ self.world.matrix[self.head_pos.x][self.head_pos.y] = PythonPart()
+ if not isinstance(self.world.matrix[new_x][new_y], Food):
+ to_be_removed = self._remove_from_tail()
+ self.world.matrix[to_be_removed.x][to_be_removed.y] = Cell()
+ else:
+ self.energy += self.world.matrix[new_x][new_y].energy
+ self.head_pos = new_pos
+
+
+class Death(Exception):
+ pass

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

class Vec2D:
def __init__(self, x, y):
self.x = x
self.y = 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, number):
return Vec2D(number*self.x, number*self.y)
def __neg__(self):
return Vec2D(-self.x, -self.y)
def __str__(self):
return "[{},{}]".format(self.x, self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __iter__(self):
yield self.x
yield self.y
class World():
def __init__(self, width):
self.width = width
self.matrix = [[Cell() for i in range(width)] for j in range(width)]
def __getitem__(self, index):
if index >= self.width:
raise IndexError
else:
return self.matrix[index]
def __setitem__(self, index):
if index >= self.width:
raise IndexError
else:
return self.matrix[index]
def __str__(self):
result = ""
for i in range(self.width):
for j in range(self.width):
result += str(self.matrix[i][j])
#result += " red nomer {}\n".format(i)
result += "\n"
return result
def __len__(self):
return self.width
class WorldObject():
def __init__(self):
self.contents = self
class Cell():
def __init__(self, content=WorldObject()):
if not isinstance(content, WorldObject):
raise TypeError
self.contents = content
def is_empty(self):
return type(self.contents) not in [PythonPart, Food]
def __str__(self):
return ".."
class Food(WorldObject):
def __init__(self, energy=0):
self.energy = energy
def __str__(self):
return ":3"
class PythonPart(WorldObject):
def __str__(self):
return "##"
class PythonHead(PythonPart):
def __str__(self):
return "@@"
class Python:
RIGHT = Vec2D(0, 1)
LEFT = Vec2D(0, -1)
UP = Vec2D(-1, 0)
DOWN = Vec2D(1, 0)
def __init__(self, world, coords, size, direction):
world.matrix[coords.x][coords.y] = PythonHead()
self.tail = []
self.tail.append(coords)
current = coords
for i in range(size):
current -= direction
if current.x < 0 or current.y < 0:
raise IndexError
else:
world.matrix[current.x][current.y] = PythonPart()
self.tail.append(current)
self.world = world
self.head_pos = coords
self.size = size
self.direction = direction
self.energy = 0
def _add_to_tail(self, new_coords):
self.tail.insert(0, new_coords)
def _remove_from_tail(self):
return self.tail.pop(len(self.tail) - 1)
def move(self, direction):
if self.direction == -direction:
raise ValueError
new_pos = self.head_pos + direction
new_x, new_y, size = new_pos.x, new_pos.y, self.world.width
exist = any(map(lambda x: x < 0 or x >= size, [new_x, new_y]))
if exist or isinstance(self.world.matrix[new_x][new_y], PythonPart):
raise Death
else:
+ is_there_food = isinstance(self.world.matrix[new_x][new_y], Food)
+ if is_there_food:
+ self.energy += self.world.matrix[new_x][new_y].energy
+
self.world.matrix[new_x][new_y] = PythonHead()
self._add_to_tail(new_pos)
self.direction = direction
self.world.matrix[self.head_pos.x][self.head_pos.y] = PythonPart()
- if not isinstance(self.world.matrix[new_x][new_y], Food):
+ if not is_there_food:
to_be_removed = self._remove_from_tail()
self.world.matrix[to_be_removed.x][to_be_removed.y] = Cell()
- else:
- self.energy += self.world.matrix[new_x][new_y].energy
+
self.head_pos = new_pos
class Death(Exception):
pass

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

class Vec2D:
def __init__(self, x, y):
self.x = x
self.y = 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, number):
return Vec2D(number*self.x, number*self.y)
def __neg__(self):
return Vec2D(-self.x, -self.y)
def __str__(self):
return "[{},{}]".format(self.x, self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __iter__(self):
yield self.x
yield self.y
class World():
def __init__(self, width):
self.width = width
self.matrix = [[Cell() for i in range(width)] for j in range(width)]
def __getitem__(self, index):
if index >= self.width:
raise IndexError
else:
return self.matrix[index]
def __setitem__(self, index):
if index >= self.width:
raise IndexError
else:
return self.matrix[index]
def __str__(self):
result = ""
for i in range(self.width):
for j in range(self.width):
result += str(self.matrix[i][j])
- #result += " red nomer {}\n".format(i)
result += "\n"
return result
def __len__(self):
return self.width
class WorldObject():
def __init__(self):
self.contents = self
class Cell():
def __init__(self, content=WorldObject()):
if not isinstance(content, WorldObject):
raise TypeError
self.contents = content
def is_empty(self):
return type(self.contents) not in [PythonPart, Food]
def __str__(self):
return ".."
class Food(WorldObject):
def __init__(self, energy=0):
self.energy = energy
def __str__(self):
return ":3"
class PythonPart(WorldObject):
def __str__(self):
return "##"
class PythonHead(PythonPart):
def __str__(self):
return "@@"
class Python:
RIGHT = Vec2D(0, 1)
LEFT = Vec2D(0, -1)
UP = Vec2D(-1, 0)
DOWN = Vec2D(1, 0)
def __init__(self, world, coords, size, direction):
world.matrix[coords.x][coords.y] = PythonHead()
self.tail = []
self.tail.append(coords)
current = coords
for i in range(size):
current -= direction
if current.x < 0 or current.y < 0:
raise IndexError
else:
world.matrix[current.x][current.y] = PythonPart()
self.tail.append(current)
self.world = world
self.head_pos = coords
self.size = size
self.direction = direction
self.energy = 0
def _add_to_tail(self, new_coords):
self.tail.insert(0, new_coords)
def _remove_from_tail(self):
return self.tail.pop(len(self.tail) - 1)
def move(self, direction):
if self.direction == -direction:
raise ValueError
new_pos = self.head_pos + direction
new_x, new_y, size = new_pos.x, new_pos.y, self.world.width
exist = any(map(lambda x: x < 0 or x >= size, [new_x, new_y]))
if exist or isinstance(self.world.matrix[new_x][new_y], PythonPart):
raise Death
else:
is_there_food = isinstance(self.world.matrix[new_x][new_y], Food)
if is_there_food:
self.energy += self.world.matrix[new_x][new_y].energy
self.world.matrix[new_x][new_y] = PythonHead()
self._add_to_tail(new_pos)
self.direction = direction
self.world.matrix[self.head_pos.x][self.head_pos.y] = PythonPart()
if not is_there_food:
to_be_removed = self._remove_from_tail()
self.world.matrix[to_be_removed.x][to_be_removed.y] = Cell()
self.head_pos = new_pos
class Death(Exception):
pass