Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #https://usaco.org/index.php?page=viewproblem2&cpid=855 Mixing milk
- class Bucket:
- """Representation of buckets"""
- def __init__ (self, volume, milk_vol):
- self.bkt_vol = volume
- self.milk_vol = milk_vol
- self.capacity = volume-milk_vol
- self.pour = 0
- self.new_milk_vol = 0
- def calc_pour(self, bucket_A, bucket_B):
- """Calculate pour vol of A-->B; pour may == 0"""
- if bucket_A.milk_vol <= bucket_B.capacity:
- self.pour = bucket_A.milk_vol
- elif bucket_A.milk_vol > bucket_B.capacity:
- self.pour = bucket_B.capacity
- return self.pour
- def show_deets(self, bucket_A, bucket_B):
- print(f"Bucket A attributes: \n\t Bucket A Cap {bucket_A.capacity}, Bucket A Vol {bucket_A.milk_vol} and pour volume = {bucket_A.pour}")
- print(f"Bucket B attributes: \n\t Bucket B Cap {bucket_B.capacity}, Bucket B Vol {bucket_B.milk_vol} and pour volume = {bucket_B.pour}")
- def update_B(self, bucket_A, bucket_B):
- """Add Bucket A pour amount to Bucket B"""
- bucket_B.new_milk_vol = bucket_A.pour + bucket_B.milk_vol
- return bucket_B.new_milk_vol
- def update_A(self,bucket_A):
- """Calculate what is the amount of milk remaining in Bucket A; new_milk_vol may == 0"""
- bucket_A.new_milk_vol = bucket_A.milk_vol - bucket_A.pour
- return bucket_A.new_milk_vol
- def show_new_deets (self, bucket_A,bucket_B):
- print(f"\t\nBucket A milk level {bucket_A.new_milk_vol}; Bucket B milk level = {bucket_B.new_milk_vol} ")
- def update_lst(i,bucket_A, bucket_B,bkt_lst):
- """Takes two instances of buckets (the first two) and uses their attributes to update the bkt_lst"""
- rtn_lst = bkt_lst.copy()
- #Updating bucket A: append A to end so B is in position to be A in next loop
- rtn_lst.pop(i)
- rtn_lst.insert(i,[bucket_A.bkt_vol,bucket_A.new_milk_vol])
- print(f"Updating bkt_lst (step 1). Pop old bucket A and insert new A: {rtn_lst}")
- if i == 0 or i == 1:
- #Updating bucket B by removing the old B and replacing with a new B value at same index position
- rtn_lst.pop(i+1)
- rtn_lst.insert(i+1,[bucket_B.bkt_vol, bucket_B.new_milk_vol])
- print(f"When i = {i}, udate to bkt_lst (step 2). Pop old bucket B and insert new B: {rtn_lst}")
- else:
- rtn_lst.pop(0)
- rtn_lst.insert(0,[bucket_B.bkt_vol,bucket_B.new_milk_vol])
- print(f"When i = {i}, update bkt_lst (step 2):Bucket B = index 0 {rtn_lst}")
- return rtn_lst
- #Initial test variable
- lst = [[10,3],[11,4],[12,5]]
- bkt_lst = lst.copy()
- #Main program
- pour_count = 0
- while pour_count < 100:
- print(f"\t\npour count at top = {pour_count}")
- for i in range(len(lst)):
- #Create copy of bkt_lst
- print(f"\ni = {i}. Bkt_lst = {bkt_lst} {id(bkt_lst)}\n")
- #Instantiate objects Buckets A and B
- if i == 0 or i == 1:
- bucket_A = Bucket(bkt_lst[i][0],bkt_lst[i][1])
- bucket_B = Bucket(bkt_lst[i+1][0],bkt_lst[i+1][1])
- else:
- bucket_A = Bucket(bkt_lst[2][0],bkt_lst[2][1])
- bucket_B = Bucket(bkt_lst[0][0],bkt_lst[0][1])
- #Calculate the amount of milk poured from A-->B
- bucket_A.calc_pour(bucket_A,bucket_B)
- bucket_A.show_deets(bucket_A,bucket_B)
- #Update attribute values
- bucket_B.update_B(bucket_A,bucket_B)
- bucket_A.update_A(bucket_A)
- print(f"Update values for A & B: \n\t A milk vol = {bucket_A.new_milk_vol} \n\t B milk vol = {bucket_B.new_milk_vol}")
- rtnd_lst = update_lst(i,bucket_A, bucket_B, bkt_lst)
- print(f"\t\nReturned list = {rtnd_lst} and bkt_lst {bkt_lst}")
- if rtnd_lst != bkt_lst:
- bkt_lst = rtnd_lst.copy()
- pour_count += 1
- print(f"Pour count= {pour_count}; Rtnd lst not equal to initial list, therefore there has been a pour")
- print(f"Pour count = {pour_count}; the bucket list output = {bkt_lst}" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement