Skip to content

Commit b11fd65

Browse files
committed
Begining exercises from chapter 7
1 parent 87da89c commit b11fd65

16 files changed

+308
-297
lines changed

3.17.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# Program to compute the square roots of a number provided by a user
2-
3-
import math
4-
5-
print("This program estimates the square root of a number")
6-
7-
num = float(input("Enter a number: "))
8-
timesToGuess = int(input("Enter the number of times I can improve my algorithm: "))
9-
guess = num/2
10-
answer = math.sqrt(num)
11-
12-
13-
for i in range(1, timesToGuess):
14-
guess = (guess + (num/guess))/2
15-
16-
finalAnswer = answer - guess
17-
18-
print("My guess is: ", guess)
19-
print("The answer is: % 0.4f" % (answer))
20-
print("I was this close to guessing correctly: %0.4f" % (finalAnswer))
1+
# Program to compute the square roots of a number provided by a user
2+
3+
import math
4+
5+
print("This program estimates the square root of a number")
6+
7+
num = float(input("Enter a number: "))
8+
timesToGuess = int(input("Enter the number of times I can improve my algorithm: "))
9+
guess = num/2
10+
answer = math.sqrt(num)
11+
12+
13+
for i in range(1, timesToGuess):
14+
guess = (guess + (num/guess))/2
15+
16+
finalAnswer = answer - guess
17+
18+
print("My guess is: ", guess)
19+
print("The answer is: % 0.4f" % (answer))
20+
print("I was this close to guessing correctly: %0.4f" % (finalAnswer))

6.10.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Program to create acronyms from user provided strings using functions
2-
3-
def acronym(phrase):
4-
acr = ""
5-
for word in phrase.split():
6-
acr = acr + word[0]
7-
acr = acr.upper()
8-
return acr
9-
10-
def main():
11-
phrase = input("Enter the phrase you want acronym of: ")
12-
print("", acronym(phrase))
13-
14-
main()
15-
16-
1+
# Program to create acronyms from user provided strings using functions
2+
3+
def acronym(phrase):
4+
acr = ""
5+
for word in phrase.split():
6+
acr = acr + word[0]
7+
acr = acr.upper()
8+
return acr
9+
10+
def main():
11+
phrase = input("Enter the phrase you want acronym of: ")
12+
print("", acronym(phrase))
13+
14+
main()
15+
16+

6.11.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Program to modify the list by squaring each entry
2-
3-
def squareEach(nums):
4-
newList = []
5-
for i in nums:
6-
i = i**2
7-
newList.append(i)
8-
return newList
9-
10-
def main():
11-
nums = [1, 2, 3, 4, 5, 6]
12-
print("", squareEach(nums))
13-
14-
main()
15-
16-
1+
# Program to modify the list by squaring each entry
2+
3+
def squareEach(nums):
4+
newList = []
5+
for i in nums:
6+
i = i**2
7+
newList.append(i)
8+
return newList
9+
10+
def main():
11+
nums = [1, 2, 3, 4, 5, 6]
12+
print("", squareEach(nums))
13+
14+
main()
15+
16+

6.12.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Program to return the sum of the numbers in the list
2-
3-
def sumList(nums):
4-
count = 0
5-
for i in nums:
6-
count += i
7-
return count
8-
9-
def main():
10-
nums = [1, 2, 133]
11-
print(sumList(nums))
12-
13-
main()
1+
# Program to return the sum of the numbers in the list
2+
3+
def sumList(nums):
4+
count = 0
5+
for i in nums:
6+
count += i
7+
return count
8+
9+
def main():
10+
nums = [1, 2, 133]
11+
print(sumList(nums))
12+
13+
main()

6.13.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
# Program to convert strings which are numbers to numbers using functions
2-
3-
def toNumbers(strList):
4-
newList = []
5-
for i in strList:
6-
i = int(i)
7-
newList.append(i)
8-
return newList
9-
10-
11-
def main():
12-
strList = ["1", "2", "3"]
13-
print(strList)
14-
print(toNumbers(strList))
15-
16-
main()
17-
1+
# Program to convert strings which are numbers to numbers using functions
2+
3+
def toNumbers(strList):
4+
newList = []
5+
for i in strList:
6+
i = int(i)
7+
newList.append(i)
8+
return newList
9+
10+
11+
def main():
12+
strList = ["1", "2", "3"]
13+
print(strList)
14+
print(toNumbers(strList))
15+
16+
main()
17+

6.14.py

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
# Program to compute the sum of squares of numbers read from a file using functions
2-
# from 3 previous exercises
3-
4-
def toNumbers(strList):
5-
newList = []
6-
for i in strList:
7-
i = float(i)
8-
newList.append(i)
9-
return newList
10-
11-
def squareEach(nums):
12-
newList = []
13-
for i in nums:
14-
i = i**2
15-
newList.append(i)
16-
return newList
17-
18-
def sumList(nums):
19-
count = 0
20-
for i in nums:
21-
count += i
22-
return count
23-
# The function doesn't work but neither does the code answer given by the author
24-
# The cause might be the fact the input file isn't specified
25-
26-
def main():
27-
file = open("listOfSquares", "r").readlines()
28-
print(sumList(squareEach(toNumbers(file))))
29-
30-
main()
1+
# Program to compute the sum of squares of numbers read from a file using functions
2+
# from 3 previous exercises
3+
4+
def toNumbers(strList):
5+
newList = []
6+
for i in strList:
7+
i = float(i)
8+
newList.append(i)
9+
return newList
10+
11+
def squareEach(nums):
12+
newList = []
13+
for i in nums:
14+
i = i**2
15+
newList.append(i)
16+
return newList
17+
18+
def sumList(nums):
19+
count = 0
20+
for i in nums:
21+
count += i
22+
return count
23+
# The function doesn't work but neither does the code answer given by the author
24+
# The cause might be the fact the input file isn't specified
25+
26+
def main():
27+
file = open("listOfSquares", "r").readlines()
28+
print(sumList(squareEach(toNumbers(file))))
29+
30+
main()

6.15.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Program that draws several faces of varying size in a single window using
2-
# specified function
3-
4-
from graphics import *
5-
6-
def drawFace(center, size, win):
7-
return
8-
9-
10-
def main():
11-
center = Point(5, 5)
12-
size = 2.5
13-
win = GraphWin("", 10, 10)
14-
win = setCoords(0, 0, 10, 10)
15-
16-
main()
1+
# Program that draws several faces of varying size in a single window using
2+
# specified function
3+
4+
from graphics import *
5+
6+
def drawFace(center, size, win):
7+
return
8+
9+
10+
def main():
11+
center = Point(5, 5)
12+
size = 2.5
13+
win = GraphWin("", 10, 10)
14+
win = setCoords(0, 0, 10, 10)
15+
16+
main()

6.6.py

+53-53
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
# Program to calculate the area of a triangle given the length of its
2-
# three sides as parameters
3-
4-
import math
5-
from graphics import *
6-
7-
8-
def square(x):
9-
return x ** 2
10-
11-
def distance(p1, p2):
12-
dist = math.sqrt(square(p2.getX() - p1.getX())
13-
+ square(p2.getY() - p2.getY()))
14-
return dist
15-
16-
def triangleArea(a, b, c):
17-
s = (a+b+c) / 2.0
18-
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
19-
return area
20-
21-
def main():
22-
win = GraphWin("Draw a Triangle")
23-
win.setCoords(0.0, 0.0, 10.0, 10.0)
24-
message = Text(Point(5, 2), "Click on three points")
25-
message.draw(win)
26-
27-
# Get and draw three vertices of triangle
28-
p1 = win.getMouse()
29-
p1.draw(win)
30-
p2 = win.getMouse()
31-
p2.draw(win)
32-
p3 = win.getMouse()
33-
p3.draw(win)
34-
35-
# Use Polygon object to draw the triangle
36-
triangle = Polygon(p1, p2, p3)
37-
triangle.setFill("peachpuff")
38-
triangle.setOutline("cyan")
39-
triangle.draw(win)
40-
41-
# Calculate the perimeter and area of the triangle
42-
d1 = distance(p1,p2)
43-
d2 = distance(p2,p3)
44-
d3 = distance(p3,p1)
45-
perim = distance(p1,p2) + distance(p2,p3) + distance(p3,p1)
46-
message.setText("""Perimeter is: %0.2f\nThe area is: %0.2f"""
47-
% (perim, triangleArea(d1, d2, d3)))
48-
49-
# Wait for another click to exit
50-
win.getMouse()
51-
win.close()
52-
53-
main()
1+
# Program to calculate the area of a triangle given the length of its
2+
# three sides as parameters
3+
4+
import math
5+
from graphics import *
6+
7+
8+
def square(x):
9+
return x ** 2
10+
11+
def distance(p1, p2):
12+
dist = math.sqrt(square(p2.getX() - p1.getX())
13+
+ square(p2.getY() - p2.getY()))
14+
return dist
15+
16+
def triangleArea(a, b, c):
17+
s = (a+b+c) / 2.0
18+
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
19+
return area
20+
21+
def main():
22+
win = GraphWin("Draw a Triangle")
23+
win.setCoords(0.0, 0.0, 10.0, 10.0)
24+
message = Text(Point(5, 2), "Click on three points")
25+
message.draw(win)
26+
27+
# Get and draw three vertices of triangle
28+
p1 = win.getMouse()
29+
p1.draw(win)
30+
p2 = win.getMouse()
31+
p2.draw(win)
32+
p3 = win.getMouse()
33+
p3.draw(win)
34+
35+
# Use Polygon object to draw the triangle
36+
triangle = Polygon(p1, p2, p3)
37+
triangle.setFill("peachpuff")
38+
triangle.setOutline("cyan")
39+
triangle.draw(win)
40+
41+
# Calculate the perimeter and area of the triangle
42+
d1 = distance(p1,p2)
43+
d2 = distance(p2,p3)
44+
d3 = distance(p3,p1)
45+
perim = distance(p1,p2) + distance(p2,p3) + distance(p3,p1)
46+
message.setText("""Perimeter is: %0.2f\nThe area is: %0.2f"""
47+
% (perim, triangleArea(d1, d2, d3)))
48+
49+
# Wait for another click to exit
50+
win.getMouse()
51+
win.close()
52+
53+
main()

0 commit comments

Comments
 (0)