Решение на Питоните хапят! от Гергана Маврова

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

Към профила на Гергана Маврова

Резултати

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

Код

from collections import deque
class World():
def __init__(self, width):
self.width = width
self.world = [[Cell() for row in range(width)] for col in range(width)]
def __getitem__(self, item):
return self.world[item]
def __len__(self):
return self.width
def __str__(self):
pass
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, s):
return Vec2D(self.x * s, self.y * s)
def __neg__(self):
return -1 * self
def __eq__(self):
return -1 * self.x == self.x and -1 * self.y == self.y
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
class WorldObject():
pass
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
self.char = ':3'
def __str__(self):
return self.char.__str__()
class PythonPart(WorldObject):
def __init__(self):
self.char = '##'
def __str__(self):
return self.char.__str__()
class PythonHead(PythonPart):
def __init__(self):
self.char = '@@'
def __str__(self):
return self.char.__str__()
class Python:
x = 0
y = 0
LEFT = Vec2D(0, -1)
RIGHT = Vec2D(0, 1)
UP = Vec2D(-1, 0)
DOWN = Vec2D(1, 0)
p = Vec2D(0, 0)
def __init__(self, world, coords, size, direction):
self.direction = direction
self.world = world
self.coords = coords
self.size = size
self.energy = 0
self.head = PythonHead()
self.world[coords.x][coords.y] = Cell(self.head)
self.part = PythonPart()
self.segments = deque([])
self.make_queue()
def make_queue(self):
for tail in range(1, self.size+1):
j = self.coords - self.direction * tail
x = j.x
y = j.y
self.world[x][y] = Cell(self.part)
self.segments.append(self.world[x][y])
def move(self, direction):
old_head = self.world[self.x][self.y]
p = self.coords + direction
self.x = p.x
self.y = p.y
self.coords += direction
if isinstance(self.world[self.x][self.y].contents, PythonPart):
raise Death
elif isinstance(self.world[self.x][self.y].contents, Food):
self.energy += self.world[self.x][self.y].contents.energy
self.size += 1
self.world[self.x][self.y] = Cell(self.head)
old_head.contents = self.part
self.segments.appendleft(old_head)
else:
if 0 <= self.x < self.world.width \
and 0 <= self.y < self.world.width:
self.world[self.x][self.y] = Cell(self.head)
old_head.contents = self.part
self.segments[self.size - 1].contents = None
self.segments.appendleft(old_head)
else:
raise Death
class Death(Exception):
pass

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

.E.EE........
======================================================================
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-r2bwqt/test.py", line 105, in test_move_backwards
    py.move(-direction)
  File "/tmp/d20130606-14014-r2bwqt/solution.py", line 34, in __neg__
    return -1 * self
TypeError: unsupported operand type(s) for *: 'int' and 'Vec2D'

======================================================================
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-r2bwqt/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-r2bwqt/test.py", line 18, in test_python_placement
    x, y = position - direction
TypeError: 'Vec2D' object is not iterable

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

FAILED (errors=3)

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

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

+from collections import deque
+
+class World():
+ def __init__(self, width):
+ self.width = width
+ self.world = [[Cell() for row in range(width)] for col in range(width)]
+
+ def __getitem__(self, item):
+ return self.world[item]
+
+ def __len__(self):
+ return len(self.width)
+
+ def __str__(self): pass
+
+
+
+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, s):
+ return Vec2D(self.x * s, self.y * s)
+
+ def __neg__(self):
+ return -1 * self
+
+ def __eq__(self):
+ return -1 * self.x == self.x and -1 * self.y == self.y
+
+
+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
+
+
+class WorldObject(): pass
+
+
+class Food(WorldObject):
+
+ def __init__(self, energy):
+ self.energy = energy
+ self.char = ':3'
+
+ def __str__(self):
+ return self.char.__str__()
+
+
+class PythonPart(WorldObject):
+ def __init__(self):
+ self.char = '##'
+
+ def __str__(self):
+ return self.char.__str__()
+
+
+class PythonHead(PythonPart):
+ def __init__(self):
+ self.char = '@@'
+
+ def __str__(self):
+ return self.char.__str__()
+
+
+class Python:
+ x = 0
+ y = 0
+ LEFT = Vec2D(0, -1)
+ RIGHT = Vec2D(0, 1)
+ UP = Vec2D(-1, 0)
+ DOWN = Vec2D(1, 0)
+ p = Vec2D(0, 0)
+
+ def __init__(self, world, coords, size, direction):
+ self.direction = direction
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.energy = 0
+ self.head = PythonHead()
+ self.world[coords.x][coords.y] = Cell(self.head)
+ self.part = PythonPart()
+ self.segments = deque([])
+ self.make_queue()
+
+
+ def make_queue(self):
+ for tail in range(1, self.size+1):
+ j = self.coords - self.direction * tail
+ x=j.x
+ y=j.y
+ self.world[x][y] = Cell(self.part)
+ self.segments.append(self.world[x][y])
+
+ def move(self, direction):
+ old_head = self.world[self.x][self.y]
+ p = self.coords + direction
+ self.x = p.x
+ self.y = p.y
+ self.coords += direction
+ if isinstance(self.world[self.x][self.y].contents, PythonPart):
+ raise Death
+ elif isinstance(self.world[self.x][self.y].contents, Food):
+ self.energy += self.world[self.x][self.y].contents.energy
+ self.size += 1
+ self.world[self.x][self.y] = Cell(self.head)
+ old_head.contents = self.part
+ self.segments.appendleft(old_head)
+ else:
+ if 0 <= self.x < self.world.width \
+ and 0 <= self.y < self.world.width:
+ self.world[self.x][self.y] = Cell(self.head)
+ old_head.contents = self.part
+ self.segments[self.size - 1].contents = None
+ self.segments.appendleft(old_head)
+ else:
+ raise Death
+
+
+
+
+
+class Death(Exception):
+ pass
+

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

from collections import deque
class World():
def __init__(self, width):
self.width = width
self.world = [[Cell() for row in range(width)] for col in range(width)]
def __getitem__(self, item):
return self.world[item]
def __len__(self):
- return len(self.width)
+ return self.width
def __str__(self): pass
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, s):
return Vec2D(self.x * s, self.y * s)
def __neg__(self):
return -1 * self
def __eq__(self):
return -1 * self.x == self.x and -1 * self.y == self.y
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
class WorldObject(): pass
class Food(WorldObject):
def __init__(self, energy):
self.energy = energy
self.char = ':3'
def __str__(self):
return self.char.__str__()
class PythonPart(WorldObject):
def __init__(self):
self.char = '##'
def __str__(self):
return self.char.__str__()
class PythonHead(PythonPart):
def __init__(self):
self.char = '@@'
def __str__(self):
return self.char.__str__()
class Python:
x = 0
y = 0
LEFT = Vec2D(0, -1)
RIGHT = Vec2D(0, 1)
UP = Vec2D(-1, 0)
DOWN = Vec2D(1, 0)
p = Vec2D(0, 0)
def __init__(self, world, coords, size, direction):
self.direction = direction
self.world = world
self.coords = coords
self.size = size
self.energy = 0
self.head = PythonHead()
self.world[coords.x][coords.y] = Cell(self.head)
self.part = PythonPart()
self.segments = deque([])
self.make_queue()
def make_queue(self):
for tail in range(1, self.size+1):
j = self.coords - self.direction * tail
x=j.x
y=j.y
self.world[x][y] = Cell(self.part)
self.segments.append(self.world[x][y])
def move(self, direction):
old_head = self.world[self.x][self.y]
p = self.coords + direction
self.x = p.x
self.y = p.y
self.coords += direction
if isinstance(self.world[self.x][self.y].contents, PythonPart):
raise Death
elif isinstance(self.world[self.x][self.y].contents, Food):
self.energy += self.world[self.x][self.y].contents.energy
self.size += 1
self.world[self.x][self.y] = Cell(self.head)
old_head.contents = self.part
self.segments.appendleft(old_head)
else:
if 0 <= self.x < self.world.width \
and 0 <= self.y < self.world.width:
self.world[self.x][self.y] = Cell(self.head)
old_head.contents = self.part
self.segments[self.size - 1].contents = None
self.segments.appendleft(old_head)
else:
raise Death
class Death(Exception):
pass

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

from collections import deque
+
class World():
def __init__(self, width):
self.width = width
self.world = [[Cell() for row in range(width)] for col in range(width)]
def __getitem__(self, item):
return self.world[item]
def __len__(self):
return self.width
- def __str__(self): pass
+ def __str__(self):
+ pass
-
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, s):
return Vec2D(self.x * s, self.y * s)
def __neg__(self):
return -1 * self
def __eq__(self):
return -1 * self.x == self.x and -1 * self.y == self.y
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
-class WorldObject(): pass
+class WorldObject():
+ pass
class Food(WorldObject):
-
def __init__(self, energy):
self.energy = energy
self.char = ':3'
def __str__(self):
return self.char.__str__()
class PythonPart(WorldObject):
def __init__(self):
self.char = '##'
def __str__(self):
return self.char.__str__()
class PythonHead(PythonPart):
def __init__(self):
self.char = '@@'
def __str__(self):
return self.char.__str__()
class Python:
x = 0
y = 0
LEFT = Vec2D(0, -1)
RIGHT = Vec2D(0, 1)
UP = Vec2D(-1, 0)
DOWN = Vec2D(1, 0)
p = Vec2D(0, 0)
-
+
def __init__(self, world, coords, size, direction):
self.direction = direction
self.world = world
self.coords = coords
self.size = size
self.energy = 0
self.head = PythonHead()
self.world[coords.x][coords.y] = Cell(self.head)
self.part = PythonPart()
self.segments = deque([])
self.make_queue()
-
def make_queue(self):
for tail in range(1, self.size+1):
j = self.coords - self.direction * tail
- x=j.x
- y=j.y
+ x = j.x
+ y = j.y
self.world[x][y] = Cell(self.part)
self.segments.append(self.world[x][y])
def move(self, direction):
old_head = self.world[self.x][self.y]
p = self.coords + direction
self.x = p.x
self.y = p.y
self.coords += direction
if isinstance(self.world[self.x][self.y].contents, PythonPart):
raise Death
elif isinstance(self.world[self.x][self.y].contents, Food):
self.energy += self.world[self.x][self.y].contents.energy
self.size += 1
self.world[self.x][self.y] = Cell(self.head)
old_head.contents = self.part
self.segments.appendleft(old_head)
else:
if 0 <= self.x < self.world.width \
and 0 <= self.y < self.world.width:
self.world[self.x][self.y] = Cell(self.head)
old_head.contents = self.part
self.segments[self.size - 1].contents = None
self.segments.appendleft(old_head)
else:
raise Death
-
-
-
class Death(Exception):
pass
-