Rename files, add headers, fix typos in 13
This commit is contained in:
parent
a6b4a427e0
commit
954069159a
8 changed files with 31 additions and 2 deletions
41
thinkcspy/Chapter-16-Exercises/Problem9and10_gallons.py
Normal file
41
thinkcspy/Chapter-16-Exercises/Problem9and10_gallons.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 16
|
||||
# Problems 9 and 10
|
||||
|
||||
def fillJug(rounds, jugA, jugB, target):
|
||||
pour = 0
|
||||
|
||||
if jugA["state"] == target:
|
||||
print("Target reached")
|
||||
return
|
||||
else:
|
||||
if jugB["state"] == 0:
|
||||
jugB["state"] += jugB["cap"] #fill jugB
|
||||
print(rounds, jugA, jugB)
|
||||
|
||||
if jugA["state"] == 0:
|
||||
pour = jugB["state"] - jugA["state"]
|
||||
else:
|
||||
pour = jugA["cap"] - jugA["state"]
|
||||
|
||||
#fill jugA with the contents of jugB
|
||||
jugA["state"] += pour
|
||||
jugB["state"] -= pour
|
||||
print(rounds, jugA, jugB)
|
||||
|
||||
if jugA["state"] == jugA["cap"]:
|
||||
jugA["state"] = 0 #empty jugA if full
|
||||
print(rounds, jugA, jugB)
|
||||
|
||||
fillJug(rounds + 1, jugA, jugB, target)
|
||||
|
||||
|
||||
def main():
|
||||
jugA = {"name": "Jug A", "state": 0, "cap": 4}
|
||||
jugB = {"name": "Jug B", "state": 0, "cap": 3}
|
||||
target = 2
|
||||
|
||||
return fillJug(1, jugA, jugB, target)
|
||||
|
||||
|
||||
main()
|
||||
Loading…
Reference in a new issue