name = 'billy'
name = name + name
name
'billybilly'
Let's try something cool!
f = open('data/words.txt', 'r')
dictionary = f.read().split('\n')
user_word = input('Enter a word (or hit enter to stop): ')
in_count = 0
out_count = 0
while user_word:
if user_word in dictionary:
print(user_word + ' is in the dictionary!\n')
in_count += 1
else:
print(user_word + ' is not in the dictionary.\n')
out_count += 1
user_word = input('Enter a word (or hit enter to stop): ')
print(f'\nWord(s) in dictionary: {in_count}\nWord(s) not in dictionary: {out_count}')
Enter a word (or hit enter to stop): zebra zebra is in the dictionary! Enter a word (or hit enter to stop): data science data science is not in the dictionary. Enter a word (or hit enter to stop): berkeley berkeley is not in the dictionary. Enter a word (or hit enter to stop): microphone microphone is in the dictionary! Enter a word (or hit enter to stop): Word(s) in dictionary: 2 Word(s) not in dictionary: 2
i = 0
while i < 10:
print(i)
i = i + 1 # also can be i += 1
print('blastoff!')
0 1 2 3 4 5 6 7 8 9 blastoff!
sum_first_n
¶def sum_first_n(n):
j = 1
total = 0
while j <= n:
total += j
j += 1
return total
# 1 + 2 + 3 + 4
sum_first_n(4)
10
sum_first_n_no_threes
¶def sum_first_n_no_threes(n):
j = 1
total = 0
while j <= n:
# If j is not a multiple of 3
if j % 3 != 0:
total += j
j += 1
return total
# 1 + 2 + 4 + 5
sum_first_n_no_threes(6)
12
def doubling_time(pop, r):
start = pop
t = 0
while start < 2 * pop:
start = start * r # same as start *= r
t += 1
return t
doubling_time(1000, 1.618)
2
def fib(n):
#prev, curr = 0, 1
prev = 0
curr = 1
i = 1
while i < n:
prev, curr = curr, prev + curr
i += 1
return curr
fib(10)
55
fib(243)
271964099255182923543922814194423915162591622175362
It turns out there's a formula that we can use to compute the $n$-th Fibonacci number, without iteration:
$$F_n = \frac{\phi^{n} - (1 - \phi)^{n}}{\sqrt{5}}$$where $\phi = \frac{1 + \sqrt{5}}{2}$ is the "Golden Ratio".
golden = (1 + 5**0.5) / 2
golden
1.618033988749895
def fib_no_iteration(n):
return int((golden**n - (1 - golden)**n) / (5**0.5))
fib_no_iteration(243)
271964099255185114887189134762467813705450055008256
Let's first define smallest_factor
, a function that takes in a positive integer n
and returns the smallest factor of n
. (This number is prime!)
# One implementation
def smallest_factor(n):
k = 2
while k <= n:
if n % k == 0:
return k
k += 1
# An equivalent implementation
def smallest_factor(n):
k = 2
while n % k != 0:
k += 1
return k
smallest_factor(5)
5
smallest_factor(24)
2
Now, let's define prime_factors
, a function that takes in a positive integer n
and prints all of the prime factors of n
.
def prime_factors(n):
while n > 1:
k = smallest_factor(n)
print(k)
n = n // k
prime_factors(22)
2 11
prime_factors(144)
2 2 2 2 3 3
def sum_squares_for(values):
total = 0
for v in values:
total += v**2
return total
# 3^2 + 4^2 + 5^2
sum_squares_for([3, 4, 5])
50
def sum_squares_while(values):
total = 0
j = 0
while j < len(values):
total += values[j]**2
j += 1
return total
# 3^2 + 4^2 + 5^2
sum_squares_while([3, 4, 5])
50
name = input('Enter your name: ')
print('Hi, ' + name + '!')
Enter your name: Oski Hi, Oski!
user_word = input('Enter a word (or hit enter to stop): ')
in_count = 0
out_count = 0
while user_word:
if user_word in dictionary:
print(user_word + ' is in the dictionary!\n')
in_count += 1
else:
print(user_word + ' is not in the dictionary.\n')
out_count += 1
user_word = input('Enter a word (or hit enter to stop): ')
print(f'\nWord(s) in dictionary: {in_count}\nWord(s) not in dictionary: {out_count}')