Решение на Питоните хапят! от Илия Тобов

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

Към профила на Илия Тобов

Резултати

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

Код

import math
class WorldObject:
pass
class Vec2D:
def __init__(self, x=None, y=None):
if x is not None:
if y is None:
self.x = x[0]
self.y = x[1]
else:
self.x = x
self.y = y
def __add__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x + other.x, self.y + other.y)
else:
raise ValueError
def __sub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x - other.x, self.y - other.y)
else:
raise ValueError
def __mul__(self, number):
return Vec2D(self.x * number, self.y * number)
def __eq__(self, other):
if isinstance(other, Vec2D):
return self.x == other.x and self.y == other.y
else:
raise ValueError
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 '(%s, %s)' % (self.x, self.y)
def __neg__(self):
return Vec2D(-self.x, -self.y)
def length(self):
"""Get the length of the vector."""
return sqrt(self.x**2 + self.y**2)
class World:
def __init__(self, width):
self.world = []
self.len = width
for i in range(0, width):
row = []
for j in range(0, width):
row.append(Cell())
self.world.append(row)
def __str__(self):
field = ''
for row in self.world:
path = ''
for cell in row:
path += cell.__str__()
path += '\n'
field += path
return field
def __setitem__(self, key, value):
self.world[key] = value
def __getitem__(self, key):
return self.world[key]
def __len__(self):
return self.len
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 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.head.parts:
self.parts.append(PythonPart())
self.python.position = coords
current = coords
world[coords.x][coords.y] = Cell(self.python)
for part in self.parts:
part.coords = current - direction
current -= direction
world[current.x][current.y] = Cell(PythonPart())
def move(self, direction):
if direction is not self.direction:
if self.direction is -direction:
raise ValueError
self.direction = direction
new_position = self.python.position + direction
size = range(0, len(self.world))
if new_position.x in size and new_position.y in size:
current = self.world[new_position.x][new_position.y]
flag = False
if isinstance(current.contents, Food):
flag = True
self.size += 1
self.energy += current.contents.energy
if current.is_empty() or flag:
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 flag:
self.parts.append(PythonPart())
self.world[last_position.x][last_position.y] = cell
cell = 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
def __str__(self):
python = '{}'*len(self.parts)
return self.head.__str__() + python.format(*[part.__str__()
for part in self.parts])
class Death(Exception):
pass

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

EE...........
======================================================================
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-75clkj/test.py", line 88, in test_growth
    py.move(Python.LEFT)
  File "/tmp/d20130606-14014-75clkj/solution.py", line 206, in move
    self.world[last_position.x][last_position.y] = cell
UnboundLocalError: local variable 'cell' referenced before assignment

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

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

FAILED (errors=2)

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

Илия обнови решението на 15.05.2013 14:28 (преди почти 11 години)

+import math
+
+
+class WorldObject:
+ pass
+
+
+class Vec2D:
+ def __init__(self, x=None, y=None):
+ if x is not None:
+ if y is None:
+ self.x = x[0]
+ self.y = x[1]
+ else:
+ self.x = x
+ self.y = y
+
+ def __add__(self, other):
+ if isinstance(other, Vec2D):
+ return Vec2D(self.x + other.x, self.y + other.y)
+ else:
+ raise ValueError
+
+ def __sub__(self, other):
+ if isinstance(other, Vec2D):
+ return Vec2D(self.x - other.x, self.y - other.y)
+ else:
+ raise ValueError
+
+ def __mul__(self, number):
+ return Vec2D(self.x * number, self.y * number)
+
+ def __eq__(self, other):
+ if isinstance(other, Vec2D):
+ return self.x == other.x and self.y == other.y
+ else:
+ raise ValueError
+
+ 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 '(%s, %s)' % (self.x, self.y)
+
+ def __neg__(self):
+ return Vec2D(-self.x, -self.y)
+
+ def length(self):
+ """Get the length of the vector."""
+
+ return sqrt(self.x**2 + self.y**2)
+
+
+class World:
+ def __init__(self, width):
+ self.world = []
+ self.len = width
+ for i in range(0, width):
+ row = []
+ for j in range(0, width):
+ row.append(Cell())
+ self.world.append(row)
+
+ def __str__(self):
+ field = ''
+ for row in self.world:
+ path = ''
+ for cell in row:
+ path += cell.__str__()
+ path += '\n'
+ field += path
+ return field
+
+ def __setitem__(self, key, value):
+ self.world[key] = value
+
+ def __getitem__(self, key):
+ return self.world[key]
+
+ def __len__(self):
+ return self.len
+
+
+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 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.head.parts:
+ self.parts.append(PythonPart())
+
+ self.python.position = coords
+ current = coords
+ world[coords.x][coords.y] = Cell(self.python)
+
+ for part in self.parts:
+ part.coords = current - direction
+ current -= direction
+ world[current.x][current.y] = Cell(PythonPart())
+
+ def move(self, direction):
+ if direction is not self.direction:
+ self.direction = direction
+
+ new_position = self.python.position + direction
+ size = range(0, len(self.world))
+
+ if new_position.x in size and new_position.y in size:
+ current = self.world[new_position.x][new_position.y]
+
+ flag = False
+ if isinstance(current.contents, Food):
+ flag = True
+ self.size += 1
+ self.energy += current.contents.energy
+
+ if current.is_empty() or flag:
+ 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 flag:
+ self.parts.append(PythonPart())
+ self.world[last_position.x][last_position.y] = cell
+ cell = 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
+
+ def __str__(self):
+ python = '{}'*len(self.parts)
+ return self.head.__str__() + python.format(*[part.__str__()
+ for part in self.parts])
+
+
+class Death(Exception):
+ pass

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

import math
class WorldObject:
pass
class Vec2D:
def __init__(self, x=None, y=None):
if x is not None:
if y is None:
self.x = x[0]
self.y = x[1]
else:
self.x = x
self.y = y
def __add__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x + other.x, self.y + other.y)
else:
raise ValueError
def __sub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x - other.x, self.y - other.y)
else:
raise ValueError
def __mul__(self, number):
return Vec2D(self.x * number, self.y * number)
def __eq__(self, other):
if isinstance(other, Vec2D):
return self.x == other.x and self.y == other.y
else:
raise ValueError
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 '(%s, %s)' % (self.x, self.y)
def __neg__(self):
return Vec2D(-self.x, -self.y)
def length(self):
"""Get the length of the vector."""
return sqrt(self.x**2 + self.y**2)
class World:
def __init__(self, width):
self.world = []
self.len = width
for i in range(0, width):
row = []
for j in range(0, width):
row.append(Cell())
self.world.append(row)
def __str__(self):
field = ''
for row in self.world:
path = ''
for cell in row:
path += cell.__str__()
path += '\n'
field += path
return field
def __setitem__(self, key, value):
self.world[key] = value
def __getitem__(self, key):
return self.world[key]
def __len__(self):
return self.len
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 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.head.parts:
self.parts.append(PythonPart())
self.python.position = coords
current = coords
world[coords.x][coords.y] = Cell(self.python)
for part in self.parts:
part.coords = current - direction
current -= direction
world[current.x][current.y] = Cell(PythonPart())
def move(self, direction):
if direction is not self.direction:
+ if self.direction is -direction:
+ raise Death
self.direction = direction
new_position = self.python.position + direction
size = range(0, len(self.world))
if new_position.x in size and new_position.y in size:
current = self.world[new_position.x][new_position.y]
flag = False
if isinstance(current.contents, Food):
flag = True
self.size += 1
self.energy += current.contents.energy
if current.is_empty() or flag:
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 flag:
self.parts.append(PythonPart())
self.world[last_position.x][last_position.y] = cell
cell = 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
def __str__(self):
python = '{}'*len(self.parts)
return self.head.__str__() + python.format(*[part.__str__()
for part in self.parts])
class Death(Exception):
pass

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

import math
class WorldObject:
pass
class Vec2D:
def __init__(self, x=None, y=None):
if x is not None:
if y is None:
self.x = x[0]
self.y = x[1]
else:
self.x = x
self.y = y
def __add__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x + other.x, self.y + other.y)
else:
raise ValueError
def __sub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x - other.x, self.y - other.y)
else:
raise ValueError
def __mul__(self, number):
return Vec2D(self.x * number, self.y * number)
def __eq__(self, other):
if isinstance(other, Vec2D):
return self.x == other.x and self.y == other.y
else:
raise ValueError
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 '(%s, %s)' % (self.x, self.y)
def __neg__(self):
return Vec2D(-self.x, -self.y)
def length(self):
"""Get the length of the vector."""
return sqrt(self.x**2 + self.y**2)
class World:
def __init__(self, width):
self.world = []
self.len = width
for i in range(0, width):
row = []
for j in range(0, width):
row.append(Cell())
self.world.append(row)
def __str__(self):
field = ''
for row in self.world:
path = ''
for cell in row:
path += cell.__str__()
path += '\n'
field += path
return field
def __setitem__(self, key, value):
self.world[key] = value
def __getitem__(self, key):
return self.world[key]
def __len__(self):
return self.len
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 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.head.parts:
self.parts.append(PythonPart())
self.python.position = coords
current = coords
world[coords.x][coords.y] = Cell(self.python)
for part in self.parts:
part.coords = current - direction
current -= direction
world[current.x][current.y] = Cell(PythonPart())
def move(self, direction):
if direction is not self.direction:
if self.direction is -direction:
- raise Death
+ raise ValueError
self.direction = direction
new_position = self.python.position + direction
size = range(0, len(self.world))
if new_position.x in size and new_position.y in size:
current = self.world[new_position.x][new_position.y]
flag = False
if isinstance(current.contents, Food):
flag = True
self.size += 1
self.energy += current.contents.energy
if current.is_empty() or flag:
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 flag:
self.parts.append(PythonPart())
self.world[last_position.x][last_position.y] = cell
cell = 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
def __str__(self):
python = '{}'*len(self.parts)
return self.head.__str__() + python.format(*[part.__str__()
for part in self.parts])
class Death(Exception):
pass