We tested if a positive integer (or a range of integers) satisfies the Collatz conjecture (using "while" and "for" loops). We discussed the basics of lists, the efficiency of list operations and how to efficiently concatenate a list to another. We also demonstrated list comprehension. Finally, we 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"
orig_num = int(input("Enter a positive integer to apply Collatz algorithm: "))
num = orig_num
while num > 1:
print(num, end=" ")
if num%2 == 0:
num = num//2
else:
num = 3*num+1
print(num)
print( orig_num, "is OK!")
Enter a positive integer to apply Collatz algorithm: 7 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 7 is OK!
limit = int(input("Check Collatz algorithm for 1 ... "))
orig_num =1
while orig_num <= limit:
num = orig_num
while num > 1:
print(num, end=" ")
if num%2 == 0:
num = num//2
else:
num = 3*num+1
print(num)
print( orig_num, "is OK!")
orig_num += 1
Check Collatz algorithm for 1 ... 10 1 1 is OK! 2 1 2 is OK! 3 10 5 16 8 4 2 1 3 is OK! 4 2 1 4 is OK! 5 16 8 4 2 1 5 is OK! 6 3 10 5 16 8 4 2 1 6 is OK! 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 7 is OK! 8 4 2 1 8 is OK! 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 9 is OK! 10 5 16 8 4 2 1 10 is OK!
for i in range(10):
print(i)
0 1 2 3 4 5 6 7 8 9
for i in range(1, 7, 2):
print(i)
1 3 5
for i in range(7, 1, -2):
print(i)
7 5 3
limit = int(input("Check Collatz algorithm for 1 ... "))
for orig_num in range(1, limit+1):
num = orig_num
while num > 1:
print(num, end=" ")
if num%2 == 0:
num = num//2
else:
num = 3*num+1
print(num)
print( orig_num, "is OK!")
Check Collatz algorithm for 1 ... 4 1 1 is OK! 2 1 2 is OK! 3 10 5 16 8 4 2 1 3 is OK! 4 2 1 4 is OK!
X = 10
lst = [x,1,2,3,"name", print, True, [3.14, 2.5]]
len(lst)
lst[4]
lst[5]
lst[5]("hello")
#lst[100]
lst[-1]
lst[-1][0]
lst[1] = 1000
lst
lst[8] = 40
8
'name'
<function print>
hello
[3.14, 2.5]
3.14
[True, 1000, 2, 3, 'name', <function print>, True, [3.14, 2.5]]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-78-5dd83b43223a> in <module>() 10 lst[1] = 1000 11 lst ---> 12 lst[8] = 40 13 IndexError: list assignment index out of range
empty_lst = []
len(empty_lst)
empty_lst[0]
0
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-79-12a587872310> in <module>() 1 empty_lst = [] 2 len(empty_lst) ----> 3 empty_lst[0] IndexError: list index out of range
lst = [x,1,2,3,"name", print, True, [3.14, 2.5]]
lst[3: 8: 2]
[3, <function print>, [3.14, 2.5]]
lst1 = [1,2,3]
lst2 = [10,20,30]
lst_new = lst1 + lst2
lst_new
lst1
lst2
print("Bad coding:")
lst1 = lst1 + lst2
lst1
print("Good coding:")
lst1 = [1,2,3]
lst2 = [10,20,30]
lst1.extend(lst2)
lst1
lst1.append(10)
lst1
lst1.append(lst2)
lst1
lst1 = [1,2,3]
lst2 = [10,20,30]
lst1 += lst2 #invokes extend!!
[1, 2, 3, 10, 20, 30]
[1, 2, 3]
[10, 20, 30]
Bad coding:
[1, 2, 3, 10, 20, 30]
Good coding:
[1, 2, 3, 10, 20, 30]
[1, 2, 3, 10, 20, 30, 10]
[1, 2, 3, 10, 20, 30, 10, [10, 20, 30]]
lst = [10, -40.5, 6]
sum(lst)
-24.5
lst = [10, -40.5, 6]
slst = sorted(lst)
slst
lst
lst.sort()
lst
[-40.5, 6, 10]
[10, -40.5, 6]
[-40.5, 6, 10]
Using while loop
lst = [1,2,3,4,"hi", "bye", 100]
i = 0
while i < len(lst):
print(lst[i])
i += 1
1 2 3 4 hi bye 100
Using for loop with range
lst = [1,2,3,4,"hi", "bye", 100]
for i in range(len(lst)):
print(lst[i])
1 2 3 4 hi bye 100
Using for loop over elements
lst = [1,2,3,4,"hi", "bye", 100]
for item in lst:
print(item)
1 2 3 4 hi bye 100
count = int(input("How many grades? "))
above = 0
grades = []
for i in range(count):
grade = float(input("enter a grade: "))
#grades[i] = grade #wrong
#grades.append(grade)
#grades.extend([grade])
grades += [grade] #invokes extend
#grades = grades + [grade] #bad coding!
s = sum(grades)
avg = s/count
for grade in grades:
if grade > avg:
above += 1
print(above, "grades are above the average", avg)
How many grades? 4 enter a grade: 10 enter a grade: 78 enter a grade: 60.5 enter a grade: 92 3 grades are above the average 60.125
lst = [0,1,2,3,4,5,6,7,8,9]
lst1 = [x for x in lst if x%3==0]
lst1
lst2 = [x**2 for x in lst if x%3==0]
lst2
lst3 = ["!" for x in lst if 2*x > 10]
lst3
lst4 = [1 for x in lst]
lst4
[0, 3, 6, 9]
[0, 9, 36, 81]
['!', '!', '!', '!']
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
count = int(input("How many grades? "))
above = 0
grades = []
#try to replace this loop by list comprehension exp
for i in range(count):
grade = float(input("enter a grade: "))
#grades[i] = grade #wrong
#grades.append(grade)
#grades.extend([grade])
grades += [grade] #invokes extend
#grades = grades + [grade] #bad coding!
s = sum(grades)
avg = s/count
above = len([grade for grade in grades if grade > avg])
print(above, "grades are above the average", avg)
How many grades? 3 enter a grade: 100 enter a grade: 50 enter a grade: 20 1 grades are above the average 56.666666666666664
def max2(a,b):
'''
max2(float,float) ---> float
return the maximum od a and b
'''
if a>=b:
return a
#else:
# return b
return b
x = max2(10,30)
x
y = max2(10)
30
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-52-54b0c2131972> in <module>() 11 x = max2(10,30) 12 x ---> 13 y = max2(10) TypeError: max2() missing 1 required positional argument: 'b'
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
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
def max3_v3(a,b,c):
## max_ab = max2(a,b)
## total_max = max2(max_ab,c)
## return total_max
return max2(max2(a,b), c)
x = True or 3/0
x
x = 3/0 or True
x
True
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-91-d85cd7f7f52a> in <module>() 1 x = True or 3/0 2 x ----> 3 x = 3/0 or True 4 x ZeroDivisionError: division by zero