File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Jug :
2
+
3
+ def __init__ (self , capacity_liters ):
4
+ super ().__init__ ()
5
+ self .capacity_liters = capacity_liters
6
+ self .current_fill = 0
7
+
8
+ def empty (self ):
9
+ self .current_fill = 0
10
+
11
+ def fill (self ):
12
+ self .current_fill = self .capacity_liters
13
+
14
+ def fill_from (self , other_jug ):
15
+ if self .capacity_liters - self .current_fill > other_jug .current_fill :
16
+ # Pour the whole other jug
17
+ self .current_fill += other_jug .current_fill
18
+ other_jug .current_fill = 0
19
+ else :
20
+ # Just top off the current
21
+ other_jug .current_fill -= self .capacity_liters - self .current_fill
22
+ self .current_fill = self .capacity_liters
23
+
24
+
25
+ if __name__ == "__main__" :
26
+ jug_3l = Jug (3 )
27
+ jug_5l = Jug (5 )
28
+
29
+ # Create a fill of 2L in small one
30
+ jug_5l .fill ()
31
+ jug_3l .fill_from (jug_5l )
32
+ jug_3l .empty ()
33
+
34
+ # Fill 5 in the big one, top off the small one with 1L
35
+ jug_3l .fill_from (jug_5l )
36
+ jug_5l .fill ()
37
+ jug_3l .fill_from (jug_5l )
38
+
39
+ print (f"Now in the big jug, we have { jug_5l .current_fill } " )
40
+
41
+ # Progress on water quantities in the jugs
42
+ # 5 - 0
43
+ # 2 - 3
44
+ # 2 - 0
45
+ # 0 - 2
46
+ # 5 - 2
47
+ # 4 - 0
You can’t perform that action at this time.
0 commit comments