-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecture 3
111 lines (99 loc) · 2.79 KB
/
lecture 3
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
####################
## EXAMPLE: for loops over strings
####################
#s = "demo loops"
#for index in range(len(s)):
# if s[index] == 'i' or s[index] == 'u':
# print("There is an i or u")
#
#for char in s:
# if char == 'i' or char == 'u':
# print("There is an i or u")
####################
## EXAMPLE: while loops and strings
## CHALLENGE: rewrite while loop with a for loop
####################
#an_letters = "aefhilmnorsxAEFHILMNORSX"
#word = input("I will cheer for you! Enter a word: ")
#times = int(input("Enthusiasm level (1-10): "))
#
#i = 0
#while i < len(word):
# char = word[i]
# if char in an_letters:
# print("Give me an " + char + "! " + char)
# else:
# print("Give me a " + char + "! " + char)
# i += 1
#print("What does that spell?")
#for i in range(times):
# print(word, "!!!")
####################
## EXAMPLE: perfect cube
####################
#cube = 27
##cube = 8120601
#for guess in range(cube+1):
# if guess**3 == cube:
# print("Cube root of", cube, "is", guess)
# # loops keeps going even after found the cube root
####################
## EXAMPLE: guess and check cube root
####################
#cube = 27
##cube = 8120601
#for guess in range(abs(cube)+1):
# # passed all potential cube roots
# if guess**3 >= abs(cube):
# # no need to keep searching
# break
#if guess**3 != abs(cube):
# print(cube, 'is not a perfect cube')
#else:
# if cube < 0:
# guess = -guess
# print('Cube root of ' + str(cube) + ' is ' + str(guess))
####################
## EXAMPLE: approximate cube root
####################
#cube = 27
##cube = 8120601
##cube = 10000
#epsilon = 0.1
#guess = 0.0
#increment = 0.01
#num_guesses = 0
## look for close enough answer and make sure
## didn't accidentally skip the close enough bound
#while abs(guess**3 - cube) >= epsilon and guess <= cube:
# guess += increment
# num_guesses += 1
#print('num_guesses =', num_guesses)
#if abs(guess**3 - cube) >= epsilon:
# print('Failed on cube root of', cube, "with these parameters.")
#else:
# print(guess, 'is close to the cube root of', cube)
####################
## EXAMPLE: bisection cube root (only positive cubes!)
####################
#cube = 27
##cube = 8120601
## won't work with x < 1 because initial upper bound is less than ans
##cube = 0.25
#epsilon = 0.01
#num_guesses = 0
#low = 0
#high = cube
#guess = (high + low)/2.0
#while abs(guess**3 - cube) >= epsilon:
# if guess**3 < cube:
# # look only in upper half search space
# low = guess
# else:
# # look only in lower half search space
# high = guess
# # next guess is halfway in search space
# guess = (high + low)/2.0
# num_guesses += 1
#print('num_guesses =', num_guesses)
#print(guess, 'is close to the cube root of', cube)