Add currently solved exerises from Chapter 16
This commit is contained in:
parent
0dde0c524d
commit
1a90502946
6 changed files with 269 additions and 0 deletions
37
thinkcspy/Chapter-16-Exercises/gallons.py
Normal file
37
thinkcspy/Chapter-16-Exercises/gallons.py
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
def setState():
|
||||||
|
jugA = {"name": "Jug A", "state": 0, "cap": 4}
|
||||||
|
jugB = {"name": "Jug B", "state": 0, "cap": 3}
|
||||||
|
target = 2
|
||||||
|
|
||||||
|
return fillJug(1, jugA, jugB, target)
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
setState()
|
||||||
28
thinkcspy/Chapter-16-Exercises/hanoi.py
Normal file
28
thinkcspy/Chapter-16-Exercises/hanoi.py
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
def stackTowers(num):
|
||||||
|
A = []
|
||||||
|
B = []
|
||||||
|
C = []
|
||||||
|
rounds = 0
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(num):
|
||||||
|
A.insert(0, i+1)
|
||||||
|
|
||||||
|
|
||||||
|
def hanoi(num, start, aux, end):
|
||||||
|
nonlocal rounds
|
||||||
|
|
||||||
|
if num == 1:
|
||||||
|
end += [start.pop()]
|
||||||
|
rounds += 1
|
||||||
|
print(rounds, A, B, C)
|
||||||
|
else:
|
||||||
|
hanoi(num - 1, start, end, aux)
|
||||||
|
end += [start.pop()]
|
||||||
|
rounds += 1
|
||||||
|
print(rounds, A, B, C)
|
||||||
|
hanoi(num -1, aux, start, end)
|
||||||
|
|
||||||
|
hanoi(num, A, B, C)
|
||||||
|
|
||||||
|
stackTowers(5)
|
||||||
55
thinkcspy/Chapter-16-Exercises/hilbert.py
Normal file
55
thinkcspy/Chapter-16-Exercises/hilbert.py
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
|
||||||
|
import turtle
|
||||||
|
|
||||||
|
def createLSystem(numIters, axiom):
|
||||||
|
if numIters == 0:
|
||||||
|
return axiom
|
||||||
|
else:
|
||||||
|
return createLSystem(numIters - 1, processString(axiom))
|
||||||
|
|
||||||
|
|
||||||
|
def processString(oldStr):
|
||||||
|
newstr = ""
|
||||||
|
for ch in oldStr:
|
||||||
|
newstr = newstr + applyRules(ch)
|
||||||
|
|
||||||
|
return newstr
|
||||||
|
|
||||||
|
|
||||||
|
def applyRules(ch):
|
||||||
|
newstr = ""
|
||||||
|
if ch == 'A':
|
||||||
|
newstr = '-BF+AFA+FB-' # Rule 1
|
||||||
|
elif ch == 'B':
|
||||||
|
newstr = '+AF-BFB-FA+'
|
||||||
|
else:
|
||||||
|
newstr = ch # no rules apply so keep the character
|
||||||
|
|
||||||
|
return newstr
|
||||||
|
|
||||||
|
|
||||||
|
def drawLsystem(aTurtle, instructions, angle, distance):
|
||||||
|
for cmd in instructions:
|
||||||
|
if cmd == 'F':
|
||||||
|
aTurtle.forward(distance)
|
||||||
|
elif cmd == '+':
|
||||||
|
aTurtle.left(angle)
|
||||||
|
elif cmd == '-':
|
||||||
|
aTurtle.right(angle)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
inst = createLSystem(4, "A")
|
||||||
|
t = turtle.Turtle()
|
||||||
|
wn = turtle.Screen()
|
||||||
|
|
||||||
|
t.up()
|
||||||
|
t.back(200)
|
||||||
|
t.down()
|
||||||
|
t.speed(9)
|
||||||
|
drawLsystem(t, inst, 90, 5)
|
||||||
|
|
||||||
|
wn.exitonclick()
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
55
thinkcspy/Chapter-16-Exercises/kochsnowflake.py
Normal file
55
thinkcspy/Chapter-16-Exercises/kochsnowflake.py
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
|
||||||
|
import turtle
|
||||||
|
|
||||||
|
def createLSystem(numIters, axiom):
|
||||||
|
if numIters == 0:
|
||||||
|
return axiom
|
||||||
|
else:
|
||||||
|
return createLSystem(numIters - 1, processString(axiom))
|
||||||
|
|
||||||
|
|
||||||
|
def processString(oldStr):
|
||||||
|
newstr = ""
|
||||||
|
for ch in oldStr:
|
||||||
|
newstr = newstr + applyRules(ch)
|
||||||
|
|
||||||
|
return newstr
|
||||||
|
|
||||||
|
|
||||||
|
def applyRules(ch):
|
||||||
|
newstr = ""
|
||||||
|
if ch == 'F':
|
||||||
|
newstr = 'F-F++F-F' # Rule 1
|
||||||
|
else:
|
||||||
|
newstr = ch # no rules apply so keep the character
|
||||||
|
|
||||||
|
return newstr
|
||||||
|
|
||||||
|
|
||||||
|
def drawLsystem(aTurtle, instructions, angle, distance):
|
||||||
|
for cmd in instructions:
|
||||||
|
if cmd == 'F':
|
||||||
|
aTurtle.forward(distance)
|
||||||
|
elif cmd == 'B':
|
||||||
|
aTurtle.backward(distance)
|
||||||
|
elif cmd == '+':
|
||||||
|
aTurtle.right(angle)
|
||||||
|
elif cmd == '-':
|
||||||
|
aTurtle.left(angle)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
inst = createLSystem(4, "F--F--F")
|
||||||
|
t = turtle.Turtle()
|
||||||
|
wn = turtle.Screen()
|
||||||
|
|
||||||
|
t.up()
|
||||||
|
t.back(200)
|
||||||
|
t.down()
|
||||||
|
t.speed(9)
|
||||||
|
drawLsystem(t, inst, 60, 5)
|
||||||
|
|
||||||
|
wn.exitonclick()
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
45
thinkcspy/Chapter-16-Exercises/mountain.py
Normal file
45
thinkcspy/Chapter-16-Exercises/mountain.py
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
import turtle
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
def drawTriangle(points,color,myTurtle):
|
||||||
|
myTurtle.fillcolor(color)
|
||||||
|
myTurtle.up()
|
||||||
|
myTurtle.goto(points[0][0],points[0][1])
|
||||||
|
myTurtle.down()
|
||||||
|
myTurtle.begin_fill()
|
||||||
|
myTurtle.goto(points[1][0],points[1][1])
|
||||||
|
myTurtle.goto(points[2][0],points[2][1])
|
||||||
|
myTurtle.goto(points[0][0],points[0][1])
|
||||||
|
myTurtle.end_fill()
|
||||||
|
|
||||||
|
|
||||||
|
def getMid(p1, p2, delta):
|
||||||
|
return ( (((p1[0]+p2[0]) / 2) + random.uniform(-delta, delta)),
|
||||||
|
(((p1[1] + p2[1]) / 2) + random.uniform(-delta, delta)))
|
||||||
|
|
||||||
|
|
||||||
|
def fractalMnt(points,degree,myTurtle):
|
||||||
|
colormap = ['floralwhite','brown','darkgoldenrod','olive','maroon']
|
||||||
|
|
||||||
|
drawTriangle(points,colormap[degree],myTurtle)
|
||||||
|
|
||||||
|
if degree > 0:
|
||||||
|
mid1 = getMid(points[0], points[1], 10)
|
||||||
|
mid2 = getMid(points[0], points[2], 10)
|
||||||
|
mid3 = getMid(points[1], points[2], 10)
|
||||||
|
|
||||||
|
fractalMnt([points[0], mid1, mid2], degree-1, myTurtle)
|
||||||
|
fractalMnt([points[1], mid1, mid3], degree-1, myTurtle)
|
||||||
|
fractalMnt([points[2], mid3, mid2], degree-1, myTurtle)
|
||||||
|
fractalMnt([mid1, mid3, mid2], degree-1, myTurtle)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
myTurtle = turtle.Turtle()
|
||||||
|
myWin = turtle.Screen()
|
||||||
|
myPoints = [[-100,-50],[0,100],[100,-50]]
|
||||||
|
fractalMnt(myPoints,4,myTurtle)
|
||||||
|
myWin.exitonclick()
|
||||||
|
|
||||||
|
main()
|
||||||
49
thinkcspy/Chapter-16-Exercises/tree.py
Normal file
49
thinkcspy/Chapter-16-Exercises/tree.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import turtle
|
||||||
|
import random
|
||||||
|
|
||||||
|
def color(branchlen, t):
|
||||||
|
t.pensize(branchlen/5)
|
||||||
|
if branchlen < 15:
|
||||||
|
t.color("green")
|
||||||
|
else:
|
||||||
|
t.color("brown")
|
||||||
|
|
||||||
|
|
||||||
|
def getAngle():
|
||||||
|
return random.randrange(15, 46)
|
||||||
|
|
||||||
|
|
||||||
|
def getLen(t):
|
||||||
|
return random.randrange(5, 15)
|
||||||
|
|
||||||
|
|
||||||
|
def tree(branchLen,t):
|
||||||
|
if branchLen > 5:
|
||||||
|
color(branchLen, t)
|
||||||
|
t.forward(branchLen)
|
||||||
|
angle = getAngle()
|
||||||
|
t.right(angle)
|
||||||
|
tree(branchLen-getLen(t),t)
|
||||||
|
t.left(angle * 2)
|
||||||
|
tree(branchLen-getLen(t),t)
|
||||||
|
t.right(angle)
|
||||||
|
t.backward(branchLen)
|
||||||
|
t.color("brown")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
t = turtle.Turtle()
|
||||||
|
myWin = turtle.Screen()
|
||||||
|
t.left(90)
|
||||||
|
t.up()
|
||||||
|
t.backward(100)
|
||||||
|
t.down()
|
||||||
|
t.color("brown")
|
||||||
|
t.speed(10)
|
||||||
|
t.pensize(5)
|
||||||
|
tree(75,t)
|
||||||
|
myWin.exitonclick()
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in a new issue