Advertisement
Guest User

#https://usaco.org/index.php?page=viewproblem2&cpid=855 Mixing milk

a guest
Jun 16th, 2024
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. #https://usaco.org/index.php?page=viewproblem2&cpid=855 Mixing milk
  2. class Bucket:
  3.     """Representation of buckets"""
  4.  
  5.     def __init__ (self, volume, milk_vol):
  6.         self.bkt_vol = volume
  7.         self.milk_vol = milk_vol
  8.         self.capacity = volume-milk_vol
  9.         self.pour = 0
  10.         self.new_milk_vol = 0
  11.  
  12.     def calc_pour(self, bucket_A, bucket_B):
  13.         """Calculate pour vol of A-->B; pour may == 0"""
  14.         if bucket_A.milk_vol <= bucket_B.capacity:
  15.             self.pour = bucket_A.milk_vol
  16.         elif bucket_A.milk_vol > bucket_B.capacity:
  17.             self.pour = bucket_B.capacity
  18.         return self.pour
  19.    
  20.     def show_deets(self, bucket_A, bucket_B):
  21.         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}")
  22.         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}")
  23.  
  24.     def update_B(self, bucket_A, bucket_B):
  25.         """Add Bucket A pour amount to Bucket B"""
  26.         bucket_B.new_milk_vol = bucket_A.pour + bucket_B.milk_vol
  27.         return bucket_B.new_milk_vol
  28.    
  29.     def update_A(self,bucket_A):
  30.         """Calculate what is the amount of milk remaining in Bucket A; new_milk_vol may == 0"""
  31.         bucket_A.new_milk_vol = bucket_A.milk_vol - bucket_A.pour
  32.         return bucket_A.new_milk_vol
  33.  
  34.     def show_new_deets (self, bucket_A,bucket_B):
  35.         print(f"\t\nBucket A milk level {bucket_A.new_milk_vol}; Bucket B milk level = {bucket_B.new_milk_vol} ")
  36.  
  37. def update_lst(i,bucket_A, bucket_B,bkt_lst):
  38.     """Takes two instances of buckets (the first two) and uses their attributes to update the bkt_lst"""
  39.     rtn_lst = bkt_lst.copy()
  40.     #Updating bucket A: append A to end so B is in position to be A in next loop
  41.     rtn_lst.pop(i)
  42.     rtn_lst.insert(i,[bucket_A.bkt_vol,bucket_A.new_milk_vol])
  43.     print(f"Updating bkt_lst (step 1). Pop old bucket A and insert new A: {rtn_lst}")
  44.        
  45.     if i == 0 or i == 1:
  46.         #Updating bucket B by removing the old B and replacing with a new B value at same index position
  47.         rtn_lst.pop(i+1)
  48.         rtn_lst.insert(i+1,[bucket_B.bkt_vol, bucket_B.new_milk_vol])  
  49.         print(f"When i = {i}, udate to bkt_lst (step 2). Pop old bucket B and insert new B: {rtn_lst}")
  50.     else:
  51.         rtn_lst.pop(0)    
  52.         rtn_lst.insert(0,[bucket_B.bkt_vol,bucket_B.new_milk_vol])
  53.         print(f"When i = {i}, update bkt_lst (step 2):Bucket B = index 0 {rtn_lst}")
  54.     return rtn_lst
  55.    
  56. #Initial test variable
  57. lst = [[10,3],[11,4],[12,5]]
  58.  
  59. bkt_lst = lst.copy()
  60. #Main program
  61. pour_count = 0
  62. while pour_count < 100:
  63.     print(f"\t\npour count at top = {pour_count}")
  64.     for i in range(len(lst)):
  65.         #Create copy of bkt_lst
  66.         print(f"\ni = {i}. Bkt_lst = {bkt_lst} {id(bkt_lst)}\n")
  67.        
  68.         #Instantiate objects Buckets A and B
  69.         if i == 0 or i == 1:
  70.             bucket_A = Bucket(bkt_lst[i][0],bkt_lst[i][1])
  71.             bucket_B = Bucket(bkt_lst[i+1][0],bkt_lst[i+1][1])
  72.         else:
  73.             bucket_A = Bucket(bkt_lst[2][0],bkt_lst[2][1])
  74.             bucket_B = Bucket(bkt_lst[0][0],bkt_lst[0][1])
  75.  
  76.         #Calculate the amount of milk poured from A-->B
  77.         bucket_A.calc_pour(bucket_A,bucket_B)
  78.         bucket_A.show_deets(bucket_A,bucket_B)
  79.  
  80.         #Update attribute values
  81.         bucket_B.update_B(bucket_A,bucket_B)
  82.         bucket_A.update_A(bucket_A)
  83.        
  84.         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}")
  85.  
  86.         rtnd_lst = update_lst(i,bucket_A, bucket_B, bkt_lst)
  87.         print(f"\t\nReturned list = {rtnd_lst} and bkt_lst {bkt_lst}")
  88.         if rtnd_lst != bkt_lst:
  89.             bkt_lst = rtnd_lst.copy()
  90.             pour_count += 1
  91.             print(f"Pour count= {pour_count}; Rtnd lst not equal to initial list, therefore there has been a pour")
  92.            
  93. print(f"Pour count = {pour_count}; the bucket list output = {bkt_lst}" )
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement