# Notes from Monday, November 6

# Creating an interconnected network of objects

class Student:

    def __init__(self, name):
        self.name = name
        self.friends = []
        self.lastRumor = None

    def __str__(self):
        return self.name

    def who(self):
        if self.friends == []:
            print self, "says: I don't have any friends"
        else:
            print "Hi, I'm", self, "and my friends are:"
            for friend in self.friends:
                print friend

    def befriend(self, otherStudent):
        if otherStudent in self.friends:
            print self, "says: I'm already friends with", otherStudent
        else:
            print "Hi %s, I'm %s!  Let's be best friends!" % (self, otherStudent)
            self.friends.append(otherStudent)
            otherStudent.friends.append(self)

    def tell(self, gossip):
        if gossip != self.lastRumor:
            print self, "says: Hmm, I didn't know that!"
            self.lastRumor = gossip
            for friend in self.friends:
                friend.tell(gossip)

    def dish(self):
        if self.lastRumor == None:
            print self, "says: I haven't heard anything"
        else:
            print self, "says: I heard that", self.lastRumor

#------------------------------------------------------------------------
# Example showing the operation of a nested for-loop:

# nums = [1, 2, 3, 4]
# for n1 in nums:
#     for n2 in nums:
#         print (n1, n2)
# (1, 1)
# (1, 2)
# (1, 3)
# (1, 4)
# (2, 1)
# (2, 2)
# (2, 3)
# (2, 4)
# (3, 1)
# (3, 2)
# (3, 3)
# (3, 4)
# (4, 1)
# (4, 2)
# (4, 3)
# (4, 4)

# We can use the above idea to create mutual friendships between all
# of the members of a group, represented as a list of Student objects:

def createClique(people):
    for person1 in people:
        for person2 in people:
            if person1 != person2 and person1 not in person2.friends:
                person1.befriend(person2)

#------------------------------------------------------------------------
"""
Example:

create some students

>>> diana = Student("Diana")
>>> elizabeth = Student("Elizabeth")
>>> emily = Student("Emily")
>>> jesse = Student("Jesse")
>>> kim = Student("Kim")
>>> patrick = Student("Patrick")
>>> sam = Student("Sam")
>>> xaq = Student("Xaq")
>>> zeilend = Student("Zeilend")
 
create a clique of three students
 
>>> createClique([diana, kim, emily])
Hi Diana, I'm Kim!  Let's be best friends!
Hi Diana, I'm Emily!  Let's be best friends!
Hi Kim, I'm Emily!  Let's be best friends!
 
tell Diana some juicy gossip
 
>>> diana.tell("Bob is going out with Sue")
Diana says: Hmm, I didn't know that!
Kim says: Hmm, I didn't know that!
Emily says: Hmm, I didn't know that!
 
>>> emily.dish()
Emily says: I heard that Bob is going out with Sue
 
create a second clique with a connection to the first one
 
>>> createClique([xaq, sam, jesse, patrick])
Hi Xaq, I'm Sam!  Let's be best friends!
Hi Xaq, I'm Jesse!  Let's be best friends!
Hi Xaq, I'm Patrick!  Let's be best friends!
Hi Sam, I'm Jesse!  Let's be best friends!
Hi Sam, I'm Patrick!  Let's be best friends!
Hi Jesse, I'm Patrick!  Let's be best friends!

>>> jesse.befriend(kim)
Hi Jesse, I'm Kim!  Let's be best friends!

tell Jesse the latest news

>>> jesse.tell("Bob and Sue broke up")
Jesse says: Hmm, I didn't know that!
Xaq says: Hmm, I didn't know that!
Sam says: Hmm, I didn't know that!
Patrick says: Hmm, I didn't know that!
Kim says: Hmm, I didn't know that!
Diana says: Hmm, I didn't know that!
Emily says: Hmm, I didn't know that!
 
>>> diana.dish()
Diana says: I heard that Bob and Sue broke up
 
poor Zeilend is out of the loop
 
>>> zeilend.dish()
Zeilend says: I haven't heard anything

"""
