Question: Need urgent help with following Python Question please: Suppose you record a list of birthdays for your classmates, recorded as month day tuples. An example
Need urgent help with following Python Question please:
Suppose you record a list of birthdays for your classmates, recorded as month day tuples. An example is given below.
dates = [(3,14),(2,8),(10,25),(5,17),(3,2),(7,25),(4,30),(8,7),(int(2),8),(1,22)]
You read about the famous birthday problem and you become interested in the number of pairs of classmates that share the same birthday. Below is an algorithm you write to do this. (Note: the is operator tests that two operands point to the same object)
count = 0
for person_a in dates: for person_b in dates: # Make sure we have different people if person_a is person_b: continue # Check both month and day if person_a[0] == person_b[0] and person_a[1] == person_b[1]: count += 1 # We counted each pair twice (e.g. jane-bob and bob-jane) so divide by 2: print("Total birthday pairs:", count//2)
Q1: What is the (tightest) Big-O running time bound for the above algorithm? You may assume that simple operations like equality check, addition, and print take constant time.
Q2: You notice that your algorithm is inefficient in that it counts each pair twice. For example, it will increment count once when person_a is Jane and person_b is Bob, and again when person_a is Bob and person_b is Jane. Below, revise the algorithm so that it only looks at each pair once.
Q3: What is the (tightest) Big-O running time bound for your new algorithm? What does this tell you about whether your revision was worth making?
Q4: Finally, create a third revision of your algorithm which has a faster Big-O running time bound that both the previous algorithms.
Q5: What is the (tightest) Big-O running time bound for your last algorithm? Explain what trade-off you made to have a faster running time.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
