Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- class Member:
- def __init__(self, name, email): # Constructor
- self.name = name
- self.email = email
- self.friends = []
- def addFriend(self, m):
- self.friends.append(m)
- def listFriends(self, l=0, s=[]): # l => Nivel. s => Lista acumuladora.
- if(len(s) <= l):
- s.append(self.name)
- else:
- s[l] += "\t"+self.name
- for f in self.friends:
- f.listFriends(l+1, s)
- if(l==0):
- return self.toTable(s)
- def toTable(self, s): # Maquilla la lista de amigos en formato tabla
- res = ["--- "+s[0]+" ---\n"]
- del s[0]
- i = 0
- for c in s:
- i += 1
- res.append('Nivel '+str(i)+':\t'+c)
- return "\n".join(res)
- def main():
- # Crear miembros
- # Anidar Miembros
- a.addFriend(b) # Nivel 1
- a.addFriend(c)
- a.addFriend(d)
- b.addFriend(e) # Nivel 2
- b.addFriend(f)
- c.addFriend(g)
- c.addFriend(h)
- d.addFriend(i)
- e.addFriend(j) # Nivel 3
- e.addFriend(k)
- e.addFriend(l)
- i.addFriend(m)
- i.addFriend(n)
- k.addFriend(o) # Nivel 4
- o.addFriend(p) # Nivel 5
- o.addFriend(q)
- print a.listFriends()
- return 0
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment