Repository navigation
Expand file tree
/
Copy pathChapter 5
More file actions
214 lines (167 loc) · 5.47 KB
/
Copy pathChapter 5
File metadata and controls
214 lines (167 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Thinkpython
My answers for the Think Python excersizes (Book by Allen B. Downey)
"""
Created on Tue Mar 5 19:08:56 2019
@author: Usman Kotwal
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
def do_n(a, n):
for i in range(n):
countdown(a)
do_n(5, 3)
#Chapter 5 Exercise 5.1
#The time module provides a function, also named time, that returns
#the current Greenwich Mean Time in "the epoch", which is an arbitrary
#time used as a reference point. On UNIX systems, the epoc is 1 January 1970
#Write a script that reads the current time and converts it to a time of day
#in hours, minutes and seconds, plus the number of days since the epoch
import time
def timeofday():
now = time.time()
days = now//(3600*24)
hours = (now//3600) % (days*24)
minutes = (now//60) % (days*24*60) - hours*60
seconds = int(now) % (days*24*60) - hours*3600 - minutes*60
print(days, hours, minutes, seconds)
timeofday()
#Chapter 5 Exercise 5.2
#Fermat's Last Theorem says that there are no positive integers a, b and c such
#that
#a^n + b^n = c^n
#for any values of n greater than 2
#------------------------------------------------------------
#1. Write a function named check_format that takes four parameters
#-a, b, c and n- and checks to see if Fermat's theorem holds. If n is greater
#than 2 and
#a^n + b^n = c^n
#the program shoud print "Holy smokes, Fermat was wrong!" Otherwise
#the program should pirnt, "No, that doesn't work."
def check_fermat(a,b,c,n):
a = int(a)
b = int(b)
c = int(c)
n = int(n)
if a^n + b^n == c^n and n > 2:
print("Holy smokes, Fermat was wrong!")
else:
print("No, that doesn't work")
check_fermat(3,4,5,2)
#----------------------------------------------------------------
#2. Write a function that promts the user to input values for
#a, b, c and n, convertest them to integers, and uses check_fermat to check whether they
#violate Fermat's theorem
def check_fermat():
promta = input("an integer for a \n")
promtb = input("an integer for b \n")
promtc = input("an integer for c \n")
promtn = input("an integer for n \n")
a = int(promta)
b = int(promtb)
c = int(promtc)
n = int(promtn)
if n <= 2:
print("wow, this has nothing to do with Fermat")
elif a^n + b^n == c^n:
print("Holy smokes, Fermat was wrong!")
else:
print("No, that doesn't work")
check_fermat()
#5.3 If you are give three sticks, you may or may not be able
#to arrange them in a triangle. For example, if one of the sticks
#is 12 inches long and the other two are one inch long, you will
#not be able to get the short stick to meet in the middle.
#For any three lenghts, there is a simple test to see
#if it is possible to form a triangle:
# If any of the three lengths is greater than the sum of
# the other two, then you cannot form a triangle. Otherwise
# you can.
#1. Write a function named is_triangle that takes threee integers
#as an argument, and that prints either "Yes" or "No" depending
# on whether you can or cannot form a triangle from the sticks
# with the given lenghts.
def is_triangle(a,b,c):
if c > a+b or a > b+c or b > c+a:
print("No")
else:
print("Yes")
is_triangle(2,8,5)
#--------------------------------------------
#2. Write a function that prompts the user to input
#three stick lengths, converts them to integers, and uses
#is_triangle to check whether sticks with the given
#lenght can form a triangle
"""
def is_triangle(a,b,c):
if c > a+b or a > b+c or b > c+a:
print("No")
else:
print("Yes")
def check_triangle():
a = int(input("an integer for a \n"))
b = int(input("an integer for b \n"))
c = int(input("an integer for c \n"))
is_triangle(a,b,c)
check_triangle()
#-----------------------------------------------
#Ex. 5.5 Read the following function and see if
#you can figure out what it does. Then run it and see
#if you got it right
import turtle
t = turtle.Turtle()
def draw(t, length, n):
if n == 0:
return
angle = 50
t.fd(length*n)
t.lt(angle)
draw(t,length,n-1)
t.rt(2*angle)
draw(t, length, n-1)
t.lt(angle)
t.bk(length*n)
draw(t, 20, 8)
#I did not get it right at all :(
#----------------------------------------------
#Exercise 5.6 The Koch curve is a fractal that looks
#something like Figure 5.2. To draw a Koch curve with
#length x, all you have do do is
#Step1. Draw a Koch curve with length x/3
#Step2. Turn left 60 degrees
#Step3. Draw a Koch curve with length x/3
#Step4. Turn right 120 degrees
#Step5. Draw a Koch curve with length x/3
#Step6. Turn left 60 degrees
#Step7. Draw a Koch curve with length x/3
#The exception is if x is less than 3 --> straight line length x
#1. Write a function called koch that takes a turtle
#and a length as parameters, and that uses the turtle
#to draw a Koch curve with the given length
import turtle
t = turtle.Turtle()
def koch(t = "turtle", x = "length"):
if x < 3:
t.fd(x)
else:
koch(t, x/3)
t.lt(60)
koch(t, x/3)
t.rt(120)
koch(t, x/3)
t.lt(60)
koch(t, x/3)
#2. Write a function called snowflake that draws three
#Koch curves to make the outline of a snowflake
def snowflake(t,x):
for i in range(3):
koch(t,x)
t.rt(120)
t.speed(0)
t.pu()
t.goto(-400, 300)
t.pd()
snowflake(t, 300)
turtle.mainloop()