#! /usr/bin/python3 # ******************************************************** # CS 200 HW #3 DUE Thursday, 10/22/2020 at 11:59 pm # via the submit system on the Zoo # ******************************************************** # Name: # Email address: # ******************************************************** # This file may be opened in python 3 # Lines beginning with hashes are comments. # If you are asked to write a procedure, please make sure it has # the specified name, and the specified number and order of arguments. # The names of the formal arguments need not be the same # as in the problem specification. # For each problem, the intended inputs to your procedures # are specified (for example, "positive integers") # and your procedures need not do anything reasonable # for other possible inputs. # You may write auxiliary procedures in addition to # the requested one(s) -- for each of your auxiliary # procedures, please include a comment explaining # what it does, and giving an example or two. # You may also use procedures you have written # elsewhere in this assignment or previous assignments. # They only need to be defined once somewhere within this file. # Reading: Learning Python, chapters 26-32 # Python Cookbook, chapter 8 # ******************************************************** # ** problem 0 ** (1 easy point) # Replace the number 0 in the definition below to indicate # the number of hours you spent doing this assignment # Decimal numbers (eg, 6.237) are fine. Exclude time # spent reading. def hours(): return 2 ############## class agent: '''Class for agents who have friends and goals.''' count = 0 agents = [] def __init__(self, name): ''' Constructor for agent with name.''' self.name = name self.friends = [] self.goals = [] self.adoptedgoals = [] self.count = agent.count agent.count += 1 agent.agents.append(self) def __repr__(self): ''' Print out agent so that it can evaluate to itself.''' return "agent('{}')".format(self.name) def __str__(self): '''Return agent as a string.''' return "".format(self.name, self.count) def add_friend(self, friend): '''Add a friend without duplicates. Also add reciprocal friend.''' if not friend in self.friends: self.friends.append(friend) friend.add_friend(self) def add_goal(self, goal): '''Add goals (stances) without duplicates.''' if not goal in self.goals: self.goals.append(goal) def pp(self): '''Pretty print agent information.''' result = "Name:\t" + self.name if self.friends: result += "\nFriends:\t" + str(self.friends) if self.goals: result += "\nGoals:\t"+ str(self.goals) if self.adoptedgoals: result += "\nAdopted: \t" + str(self.adoptedgoals) return result def adopt_goals(self): ''' Go through each friend and add that friend's goals to the adoptedgoals list, without duplication.''' for f in self.friends: for g in f.goals: if not g in self.adoptedgoals: self.adoptedgoals.append(g) def __eq__(self, other): ''' Overload == operator. Are two agents equal by name, goals, and friends? ''' return self.name == other.name and self.goals == other.goals and self.friends == other.friends def copy(self): ''' Clone the agent, including name, goals, and friends. ''' newagent = agent(self.name) newagent.goals = self.goals[:] newagent.friends = self.friends[:] return newagent def agreewith(self, other): ''' Return sorted list of stances on which the agents agree, except for importance. No Duplicates. ''' result = [] selfgoals = list(set(self.goals + self.adoptedgoals)) othergoals = list(set(other.goals + other.adoptedgoals)) for g in selfgoals: for og in othergoals: if g == og: result.append(g) return sorted(result) def disagreewith(self, other): ''' Return sorted list of stances on which agents disagree, except for importance. No Duplicates. ''' result = [] selfgoals = list(set(self.goals + self.adoptedgoals)) othergoals = list(set(other.goals + other.adoptedgoals)) for g in selfgoals: for og in othergoals: if g != og: result.append(g) return sorted(result) class stance: ''' Class for importance and side on given issue.''' count = 0 stances = [] def __init__(self, issue, side='pro', importance='A'): ''' Constructor for stance() ''' self.issue = issue.upper() self.side = side.upper() self.importance = importance.upper() self.count = stance.count stance.count += 1 stance.stances.append(self) def __repr__(self): ''' Print out code that evaluates to this stance.''' return 'stance("' + self.issue + '", "' + self.side + '", "' + self.importance + '")' def __str__(self): ''' Return string version of self ''' return "".format(self.count, self.issue, self.side, self.importance) def __eq__(self, other): ''' Overload == operator. Two stances must match issue and side, though not importance. ''' return self.issue == other.issue and self.side == other.side def __ne__(self, other): ''' Overload != operator. Two stances must match issue but not side, and ignore importance. ''' return self.issue == other.issue and self.side != other.side def copy(self): ''' Close a stance. New stance has same issue, side, and importance. ''' return stance(self.issue, self.side, self.importance) def __hash__(self): ''' hash() function for stance. I define this for you. Need this for set() to remove duplicates. We will explore hashes soon. NOte: do not need to include imporantce. Match is on issue and side only. ''' return hash((self.issue, self.side)) def __lt__(self, other): ''' Comparison operator < to allow sorting stances. I define this for you. ''' return self.issue + self.side < other.issue + other.side ''' Examples: james = agent("James Bond") joe = agent("Joe Biden") james.add_friend(joe) james2 = james.copy() james == james2 s1 = stance('gun control') s2 = stance('abortion', 'pro','B') joe.add_goal(s1) joe.add_goal(s2) james.add_goal(s1) james.add_goal(s2) mary = agent("Mary") s3 = stance('abortion', 'con', 'a') s4 = stance('gun control', 'con', 'b') mary.add_goal(s3) mary.add_goal(s4) ''' ########## me = agent('Me') trump = agent('Donald Trump') biden = agent('Joe Biden') # ******************************************************** ### test function from google python course ### ======================================= # Provided simple test() function used in main() to print # what each function returns vs. what it's supposed to return. def test(got, expected): if (hasattr(expected, '__call__')): OK = expected(got) else: OK = (got == expected) if OK: prefix = ' OK ' else: prefix = ' X ' print ('%s got: %s expected: %s' % (prefix, repr(got), repr(expected))) # Provided main() calls the above functions with interesting inputs, # using test() to check if each result is correct or not. def main(): print ('hours') print('# is it greater than 0?') test(hours(), lambda x: x > 0) print('\nagent\n') a1 = agent('Arthur') test(type(a1) == agent, True) test(agent.count, a1.count+1) test(a1 in agent.agents, True) test(a1.name, 'Arthur') test(a1.friends, []) test(a1.goals, []) test(a1.adoptedgoals, []) test(repr(a1), "agent('Arthur')") test(str(a1), "".format(a1.count)) a2 = agent("Judy") test(type(a2), agent) test(a2 in agent.agents, True) test(a2.name, "Judy") test(a2 in a1.friends, False) test(a1 in a2.friends, False) a1.add_friend(a2) test(a2 in a1.friends, True) test(a1 in a2.friends, True) print ('\nstances\n') b1 = stance('gun control') test(type(b1) == stance, True) test(stance.count, b1.count+1) test(b1 in stance.stances, True) test(b1.issue, 'GUN CONTROL') test(b1.side, 'PRO') test(b1.importance, 'A') test(repr(b1), 'stance("GUN CONTROL", "PRO", "A")') b2 = b1.copy() test(b1 == b2, True) b2.importance = 'B' test(b1 == b2, True) b3 = stance('gun control', 'con', 'b') test(b3 == b1, False) test(b3 != b1, True) print ('\nagents with goals\n') a1.add_goal(b1) b3 = stance('bill of rights') b4 = stance('public health') b5 = stance('SCOTUS apointments') a1.add_goal(b3) a1.add_goal(b4) a2.add_goal(b4) a2.add_goal(b5) test(b3 in a1.goals, True) test(b4 in a1.goals, True) test(b4 in a2.goals, True) test(b5 in a2.goals, True) print ('\nagree and disagree.\n') test(a1.agreewith(a1), [stance("BILL OF RIGHTS", "PRO", "A"), stance("GUN CONTROL", "PRO", "A"), stance("PUBLIC HEALTH", "PRO", "A")]) test(a1.agreewith(a2), [stance("PUBLIC HEALTH", "PRO", "A")]) test(a1.disagreewith(a1), []) test(a1.disagreewith(a2), []) print ('\nagree and disagree with adopted goals\n') a3 = agent('Tom') c1 = stance('gun control','con','b') a3.add_goal(c1) a1.add_friend(a3) a1.adopt_goals() test(a1.disagreewith(a1), [stance("GUN CONTROL", "CON", "B"), stance("GUN CONTROL", "PRO", "A")]) test(a3.agreewith(a1), [stance("GUN CONTROL", "CON", "B")]) print ('\nVoting: my views vs Trump and Biden.\n') print ('NOTE: there is no right answer. \n') print ('\nme vs trump\n') test(me.agreewith(trump), []) test(me.disagreewith(trump), []) print ('\nme vs biden\n') test(me.agreewith(biden), []) test(me.disagreewith(biden), []) print ('\ntrump vs biden\n') test(trump.agreewith(biden), []) test(trump.disagreewith(biden), []) # Standard boilerplate to call the main() function. if __name__ == '__main__': main() # ******************************************************** # **** end of hw #3