Решение на Питоните хапят! от Весела Бандова

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

Към профила на Весела Бандова

Резултати

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

Код

class World:
def __init__(self, width):
self.width = width
array = [Cell() for x in range(0, width)]
self.matrix = [array for x in range(0, width)]
def __getitem__(self, index):
if index > self.width or index < 0:
raise IndexError
return self.matrix[index]
def __setitem__(self, index):
if index > self.width or index < 0:
raise IndexError
return self.matrix[index]
def __len__(self):
return self.width
def printe(self):
for i in self.matrix:
for j in i:
print(j.contents)
print("\n")
class Cell():
def __init__(self, contents=None):
if contents and not isinstance(contents, WorldObject):
raise TypeError
self.contents = contents
def is_empty(self):
return not self.contents
class WorldObject:
pass
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
class PythonPart(WorldObject):
def __init__(self, location):
self.location = location
class PythonHead(PythonPart):
def __init__(self, location):
self.location = location
class Vec2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, right_vector):
result_vector = Vec2D(self.x + right_vector.x,
self.y + right_vector.y)
return result_vector
def __sub__(self, right_vector):
result_vector = -right_vector
result_vector = self + result_vector
return result_vector
def __mul__(self, scalar):
result_vector = Vec2D(self.x * scalar, self.y * scalar)
return result_vector
def __neg__(self):
result_vector = Vec2D(self.x * -1, self.y * -1)
return result_vector
class Python:
LEFT = Vec2D(-1, 0)
RIGHT = Vec2D(1, 0)
UP = Vec2D(0, 1)
DOWN = Vec2D(0, -1)
energy = 0
size = 0
def __init__(self, world, coords, size, direction):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
self.head = PythonHead(coords)
self.body = []
positions = [(coords.x - direction.x*i, coords.y - direction.y * i)
for i in range(1, size+1)]
self.world[coords.x][coords.y].contents = self.head
for i in positions:
v = Vec2D(i[0], i[1])
p = PythonPart(v)
self.body.append(p)
self.world[v.x][v.y].contents = p
def move(self, direction):
if direction == -self.direction:
raise ValueError
newHead = self.head.location + direction
if(newHead.x < 0 or newHead.y < 0 or
newHead.x > self.world.width or newHead.y > self.world.width):
raise Death
a = self.world[newHead.x][newHead.y].contents
if a and isinstance(a, PythonPart):
raise Death
if isinstance(a, Food):
self.energy = self.energy + 1
self.size = self.size + 1
p = PythonPart(self.head.location)
self.body.insert(0, p)
else:
last_el = self.body[-1].location
self.world[last_el.x][last_el.x].contents = None
self.body.pop()
p = PythonPart(self.head.location)
self.body.insert(0, p)
self.world[p.location.x][p.location.y].contents = p
self.head.location = newHead
a = self.head
def printe(self):
print(self.head.location.x, self.head.location.y)
for i in self.body:
print(i.location.x, i.location.y)
class Death(Exception):
pass

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

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

======================================================================
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-glt68d/test.py", line 40, in test_ouroboros_death
    py.move(Python.LEFT)
  File "/tmp/d20130606-14014-glt68d/solution.py", line 117, in move
    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-glt68d/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-glt68d/test.py", line 18, in test_python_placement
    x, y = position - direction
TypeError: 'Vec2D' object is not iterable

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

----------------------------------------------------------------------
Ran 13 tests in 0.010s

FAILED (failures=1, errors=4)

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

Весела обнови решението на 15.05.2013 16:43 (преди над 11 години)

+
+
+class World:
+ def __init__(self, width):
+ self.width = width
+ array = [Cell() for x in range(0, width)]
+ self.matrix = [array for x in range(0, width)]
+
+ def __getitem__(self, index):
+ if index > self.width or index < 0:
+ raise IndexError
+ return self.matrix[index]
+
+ def __setitem__(self, index):
+ if index > self.width or index < 0:
+ raise IndexError
+ return self.matrix[index]
+
+ def __len__(self):
+ return self.width
+
+ def printe(self):
+ for i in self.matrix:
+ for j in i:
+ print(j.contents)
+ print("\n")
+
+
+class Cell():
+
+ def __init__(self, contents=None):
+ if contents and not isinstance(contents, WorldObject):
+ raise TypeError
+ self.contents = contents
+
+ def is_empty(self):
+ return not self.contents
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+ def __init__(self, energy):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ def __init__(self, location):
+ self.location = location
+
+
+class PythonHead(PythonPart):
+ def __init__(self, location):
+ self.location = location
+
+
+class Vec2D:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __add__(self, right_vector):
+ result_vector = Vec2D(self.x + right_vector.x,
+ self.y + right_vector.y)
+ return result_vector
+
+ def __sub__(self, right_vector):
+ result_vector = Vec2D()
+
+ def __mul__(self, scalar):
+ result_vector = Vec2D(self.x * scalar, self.y * scalar)
+ return result_vector
+
+ def __neg__(self):
+ result_vector = Vec2D(self.x * -1, self.y * -1)
+ return result_vector
+
+
+class Python:
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+ UP = Vec2D(0, 1)
+ DOWN = Vec2D(0, -1)
+ energy = 0
+ size = 0
+
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.direction = direction
+ self.head = PythonHead(coords)
+ self.body = []
+ positions = [(coords.x - direction.x*i, coords.y - direction.y * i)
+ for i in range(1, size+1)]
+ self.world[coords.x][coords.y].contents = self.head
+ for i in positions:
+ v = Vec2D(i[0], i[1])
+ p = PythonPart(v)
+ self.body.append(p)
+ self.world[v.x][v.y].contents = p
+
+ def move(self, direction):
+ if direction == -self.direction:
+ raise ValueError
+ newHead = self.head.location + direction
+ if (newHead.x < 0 or newHead.y < 0 or
+ newHead.x > self.world.width or newHead.y > self.world.width):
+ raise Death
+ a = self.world[newHead.x][newHead.y].contents
+
+ if a and isinstance(a, PythonPart):
+ raise Death
+
+ if isinstance(a, Food):
+ self.energy = self.energy + 1
+ self.size = self.size + 1
+ p = PythonPart(self.head.location)
+ self.body.insert(0, p)
+
+ else:
+ last_el = self.body[-1].location
+ self.world[last_el.x][last_el.x].contents = None
+ self.body.pop()
+ p = PythonPart(self.head.location)
+ self.body.insert(0, p)
+ self.world[p.location.x][p.location.y].contents = p
+ self.head.location = newHead
+ a = self.head
+
+ def printe(self):
+ print(self.head.location.x, self.head.location.y)
+ for i in self.body:
+ print(i.location.x, i.location.y)
+
+
+class Death(Exception):
+ pass

Весела обнови решението на 15.05.2013 16:47 (преди над 11 години)

class World:
def __init__(self, width):
self.width = width
array = [Cell() for x in range(0, width)]
self.matrix = [array for x in range(0, width)]
def __getitem__(self, index):
if index > self.width or index < 0:
raise IndexError
return self.matrix[index]
def __setitem__(self, index):
if index > self.width or index < 0:
raise IndexError
return self.matrix[index]
def __len__(self):
return self.width
def printe(self):
for i in self.matrix:
for j in i:
print(j.contents)
print("\n")
class Cell():
def __init__(self, contents=None):
if contents and not isinstance(contents, WorldObject):
raise TypeError
self.contents = contents
def is_empty(self):
return not self.contents
class WorldObject:
pass
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
class PythonPart(WorldObject):
def __init__(self, location):
self.location = location
class PythonHead(PythonPart):
def __init__(self, location):
self.location = location
class Vec2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, right_vector):
result_vector = Vec2D(self.x + right_vector.x,
- self.y + right_vector.y)
+ self.y + right_vector.y)
return result_vector
def __sub__(self, right_vector):
- result_vector = Vec2D()
+ result_vector = -right_vector
+ result_vector = self + result_vector
+ return result_vector
def __mul__(self, scalar):
result_vector = Vec2D(self.x * scalar, self.y * scalar)
return result_vector
def __neg__(self):
result_vector = Vec2D(self.x * -1, self.y * -1)
return result_vector
class Python:
LEFT = Vec2D(-1, 0)
RIGHT = Vec2D(1, 0)
UP = Vec2D(0, 1)
DOWN = Vec2D(0, -1)
energy = 0
size = 0
def __init__(self, world, coords, size, direction):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
self.head = PythonHead(coords)
self.body = []
positions = [(coords.x - direction.x*i, coords.y - direction.y * i)
for i in range(1, size+1)]
self.world[coords.x][coords.y].contents = self.head
for i in positions:
v = Vec2D(i[0], i[1])
p = PythonPart(v)
self.body.append(p)
self.world[v.x][v.y].contents = p
def move(self, direction):
if direction == -self.direction:
raise ValueError
newHead = self.head.location + direction
if (newHead.x < 0 or newHead.y < 0 or
newHead.x > self.world.width or newHead.y > self.world.width):
raise Death
a = self.world[newHead.x][newHead.y].contents
if a and isinstance(a, PythonPart):
raise Death
if isinstance(a, Food):
self.energy = self.energy + 1
self.size = self.size + 1
p = PythonPart(self.head.location)
self.body.insert(0, p)
else:
last_el = self.body[-1].location
self.world[last_el.x][last_el.x].contents = None
self.body.pop()
p = PythonPart(self.head.location)
self.body.insert(0, p)
self.world[p.location.x][p.location.y].contents = p
self.head.location = newHead
a = self.head
def printe(self):
print(self.head.location.x, self.head.location.y)
for i in self.body:
print(i.location.x, i.location.y)
class Death(Exception):
pass

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

class World:
def __init__(self, width):
self.width = width
array = [Cell() for x in range(0, width)]
self.matrix = [array for x in range(0, width)]
def __getitem__(self, index):
if index > self.width or index < 0:
raise IndexError
return self.matrix[index]
def __setitem__(self, index):
if index > self.width or index < 0:
raise IndexError
return self.matrix[index]
def __len__(self):
return self.width
def printe(self):
for i in self.matrix:
for j in i:
print(j.contents)
print("\n")
class Cell():
def __init__(self, contents=None):
if contents and not isinstance(contents, WorldObject):
raise TypeError
self.contents = contents
def is_empty(self):
return not self.contents
class WorldObject:
pass
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
class PythonPart(WorldObject):
def __init__(self, location):
self.location = location
class PythonHead(PythonPart):
def __init__(self, location):
self.location = location
class Vec2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, right_vector):
result_vector = Vec2D(self.x + right_vector.x,
self.y + right_vector.y)
return result_vector
def __sub__(self, right_vector):
result_vector = -right_vector
result_vector = self + result_vector
return result_vector
def __mul__(self, scalar):
result_vector = Vec2D(self.x * scalar, self.y * scalar)
return result_vector
def __neg__(self):
result_vector = Vec2D(self.x * -1, self.y * -1)
return result_vector
class Python:
LEFT = Vec2D(-1, 0)
RIGHT = Vec2D(1, 0)
UP = Vec2D(0, 1)
DOWN = Vec2D(0, -1)
energy = 0
size = 0
def __init__(self, world, coords, size, direction):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
self.head = PythonHead(coords)
self.body = []
positions = [(coords.x - direction.x*i, coords.y - direction.y * i)
for i in range(1, size+1)]
self.world[coords.x][coords.y].contents = self.head
for i in positions:
v = Vec2D(i[0], i[1])
p = PythonPart(v)
self.body.append(p)
self.world[v.x][v.y].contents = p
def move(self, direction):
if direction == -self.direction:
raise ValueError
newHead = self.head.location + direction
- if (newHead.x < 0 or newHead.y < 0 or
- newHead.x > self.world.width or newHead.y > self.world.width):
+ if(newHead.x < 0 or newHead.y < 0 or
+ newHead.x > self.world.width or newHead.y > self.world.width):
raise Death
a = self.world[newHead.x][newHead.y].contents
if a and isinstance(a, PythonPart):
raise Death
if isinstance(a, Food):
self.energy = self.energy + 1
self.size = self.size + 1
p = PythonPart(self.head.location)
self.body.insert(0, p)
else:
last_el = self.body[-1].location
self.world[last_el.x][last_el.x].contents = None
self.body.pop()
p = PythonPart(self.head.location)
self.body.insert(0, p)
self.world[p.location.x][p.location.y].contents = p
self.head.location = newHead
a = self.head
def printe(self):
print(self.head.location.x, self.head.location.y)
for i in self.body:
print(i.location.x, i.location.y)
class Death(Exception):
pass