Add Chapter 9 exercises
This commit is contained in:
parent
af1eb1c281
commit
13203998df
24 changed files with 1054 additions and 0 deletions
29
thinkcspy/Chapter-9-Exercises/Python-files/Problem10.py
Normal file
29
thinkcspy/Chapter-9-Exercises/Python-files/Problem10.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 9
|
||||
# Problem 10
|
||||
|
||||
def count(substr,theStr):
|
||||
# your code here
|
||||
count = 0
|
||||
charCount = 0
|
||||
pos1 = 0
|
||||
pos2 = len(substr)
|
||||
charPos = len(substr)
|
||||
|
||||
while charCount < len(theStr):
|
||||
if charPos == 1 and substr == theStr[pos1] and (pos2 == len(theStr) or substr != theStr[pos2]):
|
||||
count += 1
|
||||
charCount += 1
|
||||
elif substr == theStr[pos1:pos2]:
|
||||
count += 1
|
||||
charCount += charPos + 1
|
||||
else:
|
||||
charCount += 1
|
||||
|
||||
|
||||
pos1 += 1
|
||||
pos2 += 1
|
||||
|
||||
return count
|
||||
|
||||
print(count("e", "treeleee"))
|
||||
27
thinkcspy/Chapter-9-Exercises/Python-files/Problem11.py
Normal file
27
thinkcspy/Chapter-9-Exercises/Python-files/Problem11.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 9
|
||||
# Problem 11
|
||||
|
||||
def remove(substr,theStr):
|
||||
# your code here
|
||||
pos1 = 0
|
||||
pos2 = len(substr)
|
||||
found = False
|
||||
newStr = theStr
|
||||
|
||||
while pos1 < len(newStr) and not found:
|
||||
currentPos = newStr[pos1:pos2]
|
||||
|
||||
if substr == currentPos:
|
||||
newStr = newStr[:pos1] + newStr[pos2:]
|
||||
found = True
|
||||
else:
|
||||
pos1 += 1
|
||||
pos2 += 1
|
||||
|
||||
return newStr
|
||||
|
||||
print(remove("e", "tree"))
|
||||
print(remove("iss", "Mississippi"))
|
||||
print(remove("an", "banana"))
|
||||
print(remove("e", "treeeleee"))
|
||||
23
thinkcspy/Chapter-9-Exercises/Python-files/Problem12.py
Normal file
23
thinkcspy/Chapter-9-Exercises/Python-files/Problem12.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 9
|
||||
# Problem 12
|
||||
|
||||
def remove_all(substr,theStr):
|
||||
# your code here
|
||||
pos1 = 0
|
||||
pos2 = len(substr)
|
||||
newStr = theStr
|
||||
|
||||
while pos1 < len(newStr):
|
||||
currentPos = newStr[pos1:pos2]
|
||||
|
||||
if substr == currentPos:
|
||||
newStr = newStr[:pos1] + newStr[pos2:]
|
||||
else:
|
||||
pos1 += 1
|
||||
pos2 += 1
|
||||
|
||||
return newStr
|
||||
|
||||
|
||||
print(remove_all("e", "treeeleee"))
|
||||
61
thinkcspy/Chapter-9-Exercises/Python-files/Problem14.py
Normal file
61
thinkcspy/Chapter-9-Exercises/Python-files/Problem14.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 9
|
||||
# Problem 14
|
||||
|
||||
# Your code here
|
||||
import turtle
|
||||
|
||||
def createLSystem(numIters,axiom):
|
||||
startString = axiom
|
||||
endString = ""
|
||||
for i in range(numIters):
|
||||
endString = processString(startString)
|
||||
startString = endString
|
||||
|
||||
return endString
|
||||
|
||||
def processString(oldStr):
|
||||
newstr = ""
|
||||
for ch in oldStr:
|
||||
newstr = newstr + applyRules(ch)
|
||||
|
||||
return newstr
|
||||
|
||||
def applyRules(ch):
|
||||
newstr = ""
|
||||
if ch == 'X':
|
||||
newstr = 'X+YF+' # Rule 1
|
||||
elif ch == 'Y':
|
||||
newstr = '-FX-Y' # Rule 2
|
||||
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(10, "FX") # create the string
|
||||
print(inst)
|
||||
t = turtle.Turtle() # create the turtle
|
||||
wn = turtle.Screen()
|
||||
wn.tracer(10)
|
||||
|
||||
t.up()
|
||||
t.back(100)
|
||||
t.down()
|
||||
t.speed(0)
|
||||
drawLsystem(t, inst, 90, 5) # draw the picture
|
||||
# angle 90, segment length 5
|
||||
wn.exitonclick()
|
||||
|
||||
main()
|
||||
24
thinkcspy/Chapter-9-Exercises/Python-files/Problem18.py
Normal file
24
thinkcspy/Chapter-9-Exercises/Python-files/Problem18.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 9
|
||||
# Problem 18
|
||||
|
||||
import string
|
||||
|
||||
def encrypt(msg, code):
|
||||
alpha = string.ascii_lowercase
|
||||
newStr = ""
|
||||
pos = 0
|
||||
msg = msg.lower()
|
||||
|
||||
for char in msg:
|
||||
if char in alpha:
|
||||
pos = alpha.index(char)
|
||||
newStr += newAlpha[pos]
|
||||
else:
|
||||
newStr += char # no rules apply so keep the character
|
||||
|
||||
return newStr
|
||||
|
||||
newAlpha = "qwertyuiopasdfghjklzxcvbnm"
|
||||
|
||||
print(encrypt("Hello World!", newAlpha))
|
||||
24
thinkcspy/Chapter-9-Exercises/Python-files/Problem19.py
Normal file
24
thinkcspy/Chapter-9-Exercises/Python-files/Problem19.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 9
|
||||
# Problem 19
|
||||
|
||||
import string
|
||||
|
||||
def decrypt(msg, code):
|
||||
alpha = string.ascii_lowercase
|
||||
newStr = ""
|
||||
pos = 0
|
||||
msg = msg.lower()
|
||||
|
||||
for char in msg:
|
||||
if char in alpha:
|
||||
pos = newAlpha.index(char)
|
||||
newStr += alpha[pos]
|
||||
else:
|
||||
newStr += char # no rules apply so keep the character
|
||||
|
||||
return newStr
|
||||
|
||||
newAlpha = "qwertyuiopasdfghjklzxcvbnm"
|
||||
|
||||
print(decrypt("itssg vgksr!", newAlpha))
|
||||
32
thinkcspy/Chapter-9-Exercises/Python-files/Problem21.py
Normal file
32
thinkcspy/Chapter-9-Exercises/Python-files/Problem21.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 9
|
||||
# Problem 21
|
||||
|
||||
import string
|
||||
|
||||
def rot13(mess):
|
||||
# Your code here
|
||||
alpha = string.ascii_lowercase
|
||||
newStr = ""
|
||||
pos = 0
|
||||
mess = mess.lower()
|
||||
|
||||
for char in mess:
|
||||
if char in alpha:
|
||||
pos = alpha.index(char)
|
||||
|
||||
if pos <= 12:
|
||||
pos += 13
|
||||
else:
|
||||
pos = pos % 13
|
||||
|
||||
newStr += alpha[pos]
|
||||
|
||||
else:
|
||||
newStr += char # no rules apply so keep the character
|
||||
|
||||
return newStr
|
||||
|
||||
print(rot13('abcde'))
|
||||
print(rot13('nopqr'))
|
||||
print(rot13(rot13('Since rot13 is symmetric you should see this message')))
|
||||
30
thinkcspy/Chapter-9-Exercises/Python-files/Problem3.py
Normal file
30
thinkcspy/Chapter-9-Exercises/Python-files/Problem3.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 9
|
||||
# Problem 3
|
||||
|
||||
import string
|
||||
|
||||
def count(p):
|
||||
# your code here
|
||||
abc = 0
|
||||
e = 0
|
||||
alphabet = string.ascii_lowercase + string.ascii_uppercase
|
||||
|
||||
for achar in p:
|
||||
for alphaChar in alphabet:
|
||||
if achar == alphaChar:
|
||||
abc += 1
|
||||
if achar == "e" or achar == "E":
|
||||
e += 1
|
||||
|
||||
if abc != 0:
|
||||
ePerAbc = e / abc * 100
|
||||
else:
|
||||
ePerAbc = 0
|
||||
|
||||
finalMsg = "Your text contains {} alphabetic characters, of which {} ({:.1f}%) are 'e'.".format(abc, e, ePerAbc)
|
||||
|
||||
return finalMsg
|
||||
|
||||
text = "Bell of the ball"
|
||||
print(count(text))
|
||||
20
thinkcspy/Chapter-9-Exercises/Python-files/Problem4.py
Normal file
20
thinkcspy/Chapter-9-Exercises/Python-files/Problem4.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 9
|
||||
# Problem 4
|
||||
|
||||
# your code here
|
||||
|
||||
num1 = 1
|
||||
product = 0
|
||||
|
||||
for n in range(12):
|
||||
num2 = 1
|
||||
row = ""
|
||||
|
||||
for x in range(12):
|
||||
product = num1 * num2
|
||||
row += "{} \t".format(product)
|
||||
num2 += 1
|
||||
|
||||
print(row)
|
||||
num1 += 1
|
||||
15
thinkcspy/Chapter-9-Exercises/Python-files/Problem5.py
Normal file
15
thinkcspy/Chapter-9-Exercises/Python-files/Problem5.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# Runestone.Academy thinkcspy course
|
||||
# Chapter 9
|
||||
# Problem
|
||||
|
||||
def numDigits(n):
|
||||
# your code here
|
||||
toText = str(n)
|
||||
count = 0
|
||||
|
||||
for i in range (len(toText)):
|
||||
count += 1
|
||||
|
||||
return count
|
||||
|
||||
print(numDigits(13))
|
||||
Loading…
Reference in a new issue