Уведомления

Группа в Telegram: @pythonsu

#1 Фев. 23, 2012 23:11:58

Armany
От:
Зарегистрирован: 2011-12-18
Сообщения: 11
Репутация: +  0  -
Профиль   Отправить e-mail  

Метод __repr__

Помогите пожалуйста исправить последнюю функцию __repr__ которая должна показывать координаты. Вот например

>>> a=Polygone()
>>> a.add(5,3)
>>> a.add(5,9)
>>> a
[ <__main__.Point instance at 0x011F0558>,<__main__.Point instance at 0x011E8F30>]
код python

# -*- coding: latin-1 -*-

class Point:
def __init__(self,x,y):
self.coords = (x, y)
self.next = None

def getData(self):
return self.coords

def getNext(self):
return self.next

def setData(self,newdata):
self.coords = newdata

def setNext(self,newnext):
self.next = newnext


class Polygone:
def __init__(self):
self.head = Point(-1,-1)
self.head.setNext(self.head)

def isEmpty(self):
return self.head.getNext() == self.head

def add(self,x,y):
temp = Point(x,y)
temp.setNext(self.head.getNext())
self.head.setNext(temp)

def addAfter(self,x,y,q,z):
current = self.head.getNext()
temp = Point(q,z)
temp.setNext(current.getNext())
current.setNext(temp)

def length(self):
current = self.head.getNext()
count = 0
res=""
while current != self.head:
count = count + 1
current = current.getNext()
res += 'Votre polygone a '+str(count)+' points'
return res

def search(self,x,y):
current = self.head.getNext()
found = False
while current != self.head and not found:
if current.getData() == (x,y):
found = True
else:
current = current.getNext()
return found

def remove(self,x,y):
previous = self.head
current = self.head.getNext()
found = False
while current != self.head and not found:
if current.getData() == (x,y):
found = True
else:
previous = current
current = current.getNext()
if found:
previous.setNext(current.getNext())

def removeLast(self):
previous = self.head
current = self.head.getNext()
previous.setNext(current.getNext())


def __repr__(self):
current = self.head.getNext()
res = "[ "
while( current != self.head ):
res += str(current)
current = current.getNext()
res+="]"
return res



Отредактировано (Фев. 23, 2012 23:20:38)

Офлайн

#2 Фев. 24, 2012 00:11:03

s0rg
От:
Зарегистрирован: 2011-06-05
Сообщения: 777
Репутация: +  25  -
Профиль   Отправить e-mail  

Метод __repr__

res += '(x: %d, y: %d), ' % current.getData()
А зачем городить односвязный список? Чем встроенный list не устраивает?

Офлайн

#3 Фев. 24, 2012 00:44:02

Armany
От:
Зарегистрирован: 2011-12-18
Сообщения: 11
Репутация: +  0  -
Профиль   Отправить e-mail  

Метод __repr__

s0rg
res += '(x: %d, y: %d), ' % current.getData()
А зачем городить односвязный список? Чем встроенный list не устраивает?
спасибо большое!!!!

Я не знаю как с помощью list сделать :S



Отредактировано (Фев. 24, 2012 00:55:52)

Офлайн

#4 Фев. 24, 2012 01:31:45

Armany
От:
Зарегистрирован: 2011-12-18
Сообщения: 11
Репутация: +  0  -
Профиль   Отправить e-mail  

Метод __repr__

И почему то list наоборот выходит..

>>> a=Polygone()
>>> a.add(1,1)
>>> a.add(2,2)
>>> a.add(2,5)
>>> a
[(x: 2, y: 5),(x: 2, y: 2),(x: 1, y: 1),]



Офлайн

#5 Фев. 24, 2012 01:35:01

s0rg
От:
Зарегистрирован: 2011-06-05
Сообщения: 777
Репутация: +  25  -
Профиль   Отправить e-mail  

Метод __repr__

Armany
Я не знаю как с помощью list сделать :S
Ну как-то так:
class Point(object):
__slots__ = ['coords']

def __init__(self, x, y):
self.coords = (x, y)


class Polygone(object):
def __init__(self):
self._points = []

def isEmpty(self):
return (len(self._points) == 0)

def add(self, x, y):
self._points.append(Point(x, y))

def addAfter(self, x, y, q, z):
# Не знаю зачем тут x, y - оставил для совместимости ))
self._points.insert(0, Point(q, z))

def length(self):
return 'Votre polygone a %d points' % len(self._points)

def search(self, x, y):
found = filter(lambda pt, coords=(x, y): pt.coords == coords, self._points)
return bool(found)

def remove(self, x, y):
self._points = filter(lambda pt, coords=(x, y): pt.coords != coords, self._points)

def removeLast(self):
self._points.pop()

def __repr__(self):
return '[%s]' % ', '.join(map(lambda pt: '(x: %d, y: %d)' % pt.coords, self._points))

Офлайн

#6 Фев. 24, 2012 01:37:02

s0rg
От:
Зарегистрирован: 2011-06-05
Сообщения: 777
Репутация: +  25  -
Профиль   Отправить e-mail  

Метод __repr__

Armany
И почему то list наоборот выходит..
Ну так вы же его с ‘конца’ читаете

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version