Attachment 'lec3.py'
Download 1 ####################
2 ## EXAMPLE: for loops over strings
3 ####################
4 #s = "demo loops"
5 #for index in range(len(s)):
6 # if s[index] == 'i' or s[index] == 'u':
7 # print("There is an i or u")
8 #
9 #for char in s:
10 # if char == 'i' or char == 'u':
11 # print("There is an i or u")
12
13
14 ####################
15 ## EXAMPLE: while loops and strings
16 ## CHALLENGE: rewrite while loop with a for loop
17 ####################
18 #an_letters = "aefhilmnorsxAEFHILMNORSX"
19 #word = input("I will cheer for you! Enter a word: ")
20 #times = int(input("Enthusiasm level (1-10): "))
21 #
22 #i = 0
23 #while i < len(word):
24 # char = word[i]
25 # if char in an_letters:
26 # print("Give me an " + char + "! " + char)
27 # else:
28 # print("Give me a " + char + "! " + char)
29 # i += 1
30 #print("What does that spell?")
31 #for i in range(times):
32 # print(word, "!!!")
33
34
35
36 ####################
37 ## EXAMPLE: perfect cube
38 ####################
39 #cube = 27
40 ##cube = 8120601
41 #for guess in range(cube+1):
42 # if guess**3 == cube:
43 # print("Cube root of", cube, "is", guess)
44 # # loops keeps going even after found the cube root
45
46
47 ####################
48 ## EXAMPLE: guess and check cube root
49 ####################
50 #cube = 27
51 ##cube = 8120601
52 #for guess in range(abs(cube)+1):
53 # # passed all potential cube roots
54 # if guess**3 >= abs(cube):
55 # # no need to keep searching
56 # break
57 #if guess**3 != abs(cube):
58 # print(cube, 'is not a perfect cube')
59 #else:
60 # if cube < 0:
61 # guess = -guess
62 # print('Cube root of ' + str(cube) + ' is ' + str(guess))
63
64
65 ####################
66 ## EXAMPLE: approximate cube root
67 ####################
68 #cube = 27
69 ##cube = 8120601
70 ##cube = 10000
71 #epsilon = 0.1
72 #guess = 0.0
73 #increment = 0.01
74 #num_guesses = 0
75 ## look for close enough answer and make sure
76 ## didn't accidentally skip the close enough bound
77 #while abs(guess**3 - cube) >= epsilon and guess <= cube:
78 # guess += increment
79 # num_guesses += 1
80 #print('num_guesses =', num_guesses)
81 #if abs(guess**3 - cube) >= epsilon:
82 # print('Failed on cube root of', cube, "with these parameters.")
83 #else:
84 # print(guess, 'is close to the cube root of', cube)
85
86
87 ####################
88 ## EXAMPLE: bisection cube root (only positive cubes!)
89 ####################
90 #cube = 27
91 ##cube = 8120601
92 ## won't work with x < 1 because initial upper bound is less than ans
93 ##cube = 0.25
94 #epsilon = 0.01
95 #num_guesses = 0
96 #low = 0
97 #high = cube
98 #guess = (high + low)/2.0
99 #while abs(guess**3 - cube) >= epsilon:
100 # if guess**3 < cube:
101 # # look only in upper half search space
102 # low = guess
103 # else:
104 # # look only in lower half search space
105 # high = guess
106 # # next guess is halfway in search space
107 # guess = (high + low)/2.0
108 # num_guesses += 1
109 #print('num_guesses =', num_guesses)
110 #print(guess, 'is close to the cube root of', cube)
111
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.