We have written a program that checks if a positive integer (or range of integers) satisfies the Collatz conjecture (using "while" and "for" loops). We have also covered the basics of lists and list functions and solved an exercise (demonstrating list comprehension). Finally, we have discussed functions, short circuit evaluation and analyzed the efficiency of the functions we saw.
Code for printing several outputs in one cell (not part of the recitation):
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
Zoom out of a Collatz tree:
Check conjecture for a single number:
m = int(input("please enter an iteger: "))
n = m
while n != 1:
# print(n)
if n % 2 == 0:
n = n//2
else:
n = 3*n + 1
# print(1)
print (m, "is OK")
please enter an iteger: 2738 2738 is OK
Check conjecture for a range of numbers (using a "for" loop):
limit = int(input("please enter an iteger limit: "))
for m in range(1,limit+1):
n = m
while n != 1:
if n % 2 == 0:
n = n//2
else:
n = 3*n + 1
print (m, "is OK")
please enter an iteger limit: 10 1 is OK 2 is OK 3 is OK 4 is OK 5 is OK 6 is OK 7 is OK 8 is OK 9 is OK 10 is OK
Check conjecture for a range of numbers (using a "while" loop):
limit = int(input("please enter an iteger limit: "))
orig_num = 1
while orig_num <= limit:
n = orig_num
while n != 1:
if n % 2 == 0:
n = n//2
else:
n = 3*n + 1
print (orig_num, "is OK")
orig_num += 1
please enter an iteger limit: 10 1 is OK 2 is OK 3 is OK 4 is OK 5 is OK 6 is OK 7 is OK 8 is OK 9 is OK 10 is OK
lst = [1,2,3.14, "python"]
lst[2]
lst[1:3]
lst[-1]
3.14
[2, 3.14]
'python'
lst2 = [-2, 5]
lst+lst2
lst = lst + lst2
lst
[1, 2, 3.14, 'python', -2, 5, -2, 5]
[1, 2, 3.14, 'python', -2, 5, -2, 5]
lst = [1,2, [3, "amir"]]
lst[-1]
lst[2][1]
lst[3] # no such element
[3, 'amir']
'amir'
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-17-fea520cc6cbc> in <module>() 2 lst[-1] 3 lst[2][1] ----> 4 lst[3] IndexError: list index out of range
lst = [1,2,3]
sum(lst)
len(lst)
lst = [859,3,43,3535,64]
sorted(lst)
lst = ["z", "a", "t", "b"]
sorted(lst)
lst.append("c")
lst
lst.extend(["c"])
lst
lst.append(["z"])
lst
lst.extend("amir")
lst
6
3
[3, 43, 64, 859, 3535]
['a', 'b', 't', 'z']
['z', 'a', 't', 'b', 'c']
['z', 'a', 't', 'b', 'c', 'c']
['z', 'a', 't', 'b', 'c', 'c', ['z']]
['z', 'a', 't', 'b', 'c', 'c', ['z'], 'a', 'm', 'i', 'r']
id(lst)
lst = lst + ["d"]
id(lst)
lst.extend(["d"]) # in-place
id(lst)
13035144
88850888
88850888
Using while loop
i = 0
while i < len(lst):
print(lst[i])
i += 1
z a t b c c ['z'] a m i r d d d d
Using for loop with range
for i in range(len(lst)):
print(lst[i])
z a t b c c ['z'] a m i r d d d d
Using for loop over elements
for e in lst:
print(e)
z a t b c c ['z'] a m i r d d d d
count = int(input("How many grades? "))
grades = [] # [-1]*count
above = 0
# s = 0
for i in range(count):
g = float(input("enter a grade: "))
grades = grades + [g]
# grades[i] = g
# s += g
s = sum(grades)
avg = s / count
for g in grades:
if g > avg:
above += 1
print(above, " grades above average!")
How many grades? 3 enter a grade: 59 enter a grade: 95 enter a grade: 88 2 grades above average!
List comprehension example:
collection = [1,2,3,4,5]
lst = []
for var in collection:
if var > 2:
lst.append(2*var)
lst2 = [2 * var for var in collection if var > 2]
lst
lst2
[6, 8, 10]
[6, 8, 10]
Revised solution:
count = int(input("How many grades? "))
grades = [] # [-1]*count
above = 0
# s = 0
for i in range(count):
g = float(input("enter a grade: "))
grades = grades + [g]
# grades[i] = g
# s += g
s = sum(grades)
avg = s / count
above_lst = [g for g in grades if g > avg]
above = len(above_lst)
print(above, " grades above average!")
How many grades? 3 enter a grade: 59 enter a grade: 95 enter a grade: 88 2 grades above average!
def max2(a, b):
if a >= b:
return a
else:
return b
max2(99, 100)
100
def max3_v1(a,b,c):
if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c
max3_v1(99, 98, 100)
100
def max3_v2(a,b,c):
if a >= b:
if a >= c:
return a
else:
return c
else:
if b >= c:
return b
else:
return c
max3_v2(100, 98, 99)
100
def max3_v3(a,b,c):
max_ab = max2(a,b)
total_max = max2(c, max_ab)
return total_max
False and True
# 3//0 and False
False and 3//0
True or 3//0
False
False
True