- dictionaries
Burkay Genç, Ahmet Selman Bozkır, and Selma Dilek
26/04/2023
Program testing can be used to show the presence of bugs, but never to show their absence!
Edsger Dijkstra
No amount of experimentation can ever prove me right; a single experiment can prove me wrong.
Albert Einstein
^^^ One sentence summary of the “scientific method”
def is_bigger(x, y): """ Assumes x and y are ints Returns True if y is less than x, else False """
def sqrt(x, eps): """ Assumes x, epsilon floats, x >= 0, epsilon > 0 Returns res such that x-epsilon <= res*res <= x+epsilon """
def sqrt(x, epsilon): """ Assumes x, epsilon floats, x >= 0, epsilon > 0 Returns res such that x-epsilon <= res*res <= x+epsilon """
def isPrime(x): """ Assumes x is a nonnegative int Returns True if x is prime; False otherwise """ if x <= 2: return False for i in range(2, x): if x % i == 0: return False return True
def isPrime(x): """ Assumes x is a nonnegative int Returns True if x is prime; False otherwise """ if x <= 2: return False for i in range(2, x): if x % i == 0: return False return True isPrime(0) # expected value: False
## False
isPrime(2) # expected value: True
## False
def abs(x): """ Assumes x is an int Returns x if x>=0 and –x otherwise """ if x < -1: return -x else: return x
abs(-1)
incorrectly returns -1def average(li): """ Computes the average of numbers in a list """ sum = li[0] for i in li: sum += i return (sum / len(li)) print(average([0,-1,1])) ## Expect 0.0
## 0.0
print(average([0,1,2,3,4])) ## Expect 2.0
## 2.0
print(average([1,2,3])) ## Expect 2
## 2.3333333333333335
def average(li): print("List input:", li) ## Initial value of the argument """ Computes the average of numbers in a list """ sum = li[0] for i in li: print("Adding:", i) ## The number added in this iteration sum += i print("Current sum:", sum) ## Value of sum after each loop iteration print("List length:", len(li)) ## The length of the list return (sum / len(li)) print(average([1,2,3])) ## Expect 2
## List input: [1, 2, 3] ## Adding: 1 ## Current sum: 2 ## Adding: 2 ## Current sum: 4 ## Adding: 3 ## Current sum: 7 ## List length: 3 ## 2.3333333333333335
1
the current sum becomes 2
??sum=li[0]
def average(li): print("List input:", li) ## Initial value of the argument """ Computes the average of numbers in a list """ sum = li[0] for i in li: print("Adding:", i) ## The number added in this iteration sum += i print("Current sum:", sum) ## Value of sum after each loop iteration print("List length:", len(li)) ## The length of the list return (sum / len(li)) print(average([1,2,3])) ## Expect 2
sum = 0
def average(li): print("List input:", li) ## Initial value of the argument """ Computes the average of numbers in a list """ sum = 0 ## FIXED for i in li: print("Adding:", i) ## The number added in this iteration sum += i print("Current sum:", sum) ## Value of sum after each loop iteration print("List length:", len(li)) ## The length of the list return (sum / len(li)) print(average([1,2,3])) ## Expect 2
## List input: [1, 2, 3] ## Adding: 1 ## Current sum: 1 ## Adding: 2 ## Current sum: 3 ## Adding: 3 ## Current sum: 6 ## List length: 3 ## 2.0
def recursiveReverse(s): if len(s) <= 1: return "" else: return recursiveReverse(s[2:len(s)]) + s[0] print("-", recursiveReverse("burkay"), "-") # Expecting "yakrub"
## - arb -
def recursiveReverse(s): print("Input string:", s) if len(s) <= 1: return "" else: print("Recursive call:", s[2:len(s)], "+", s[0]) return recursiveReverse(s[2:len(s)]) + s[0] print("-", recursiveReverse("burkay"), "-") # Expecting "yakrub"
## Input string: burkay ## Recursive call: rkay + b ## Input string: rkay ## Recursive call: ay + r ## Input string: ay ## Recursive call: + a ## Input string: ## - arb -
s[2:len(s)]
is trimming 2 characters at onces[2:len(s)]
with s[1:len(s)]
def recursiveReverse(s): print("Input string:", s) if len(s) <= 1: return "" else: print("Recursive call:", s[1:len(s)], "+", s[0]) return recursiveReverse(s[1:len(s)]) + s[0] print("-", recursiveReverse("burkay"), "-") # Expecting "yakrub"
## Input string: burkay ## Recursive call: urkay + b ## Input string: urkay ## Recursive call: rkay + u ## Input string: rkay ## Recursive call: kay + r ## Input string: kay ## Recursive call: ay + k ## Input string: ay ## Recursive call: y + a ## Input string: y ## - akrub -
return ""
with return s
def recursiveReverse(s): print("Input string:", s) if len(s) <= 1: return s else: print("Recursive call:", s[1:len(s)], "+", s[0]) return recursiveReverse(s[1:len(s)]) + s[0] print("-", recursiveReverse("burkay"), "-") # Expecting "yakrub"
## Input string: burkay ## Recursive call: urkay + b ## Input string: urkay ## Recursive call: rkay + u ## Input string: rkay ## Recursive call: kay + r ## Input string: kay ## Recursive call: ay + k ## Input string: ay ## Recursive call: y + a ## Input string: y ## - yakrub -
def foo(x): return (3*x**2+2)/(2*x**2-18)
def foo(x): return (3*x**2+2)/(2*x**2-18)
foo(3)
## ZeroDivisionError: division by zero
def foo(x): a = 3*x**2+2 b = 2*x**2-18 if b == 0: return None else: return a/b
def listMerge(l1, l2): l3 = [] for i in range(len(l1)): l3.append( (l1[i], l2[i]) ) return l3 listMerge([1,2,3], ['a', 'b', 'c'])
## [(1, 'a'), (2, 'b'), (3, 'c')]
def circleIntersect(c1, c2): # each circle is a triple (x,y,r) dist = ((c1[0]-c2[0])**2 + (c1[1]-c2[1])**2)**0.5 if dist >= c1[2] and dist < c1[2] + c2[2]: return True else: return False circleIntersect((3,3,3),(-1,4,5))
## True
def circleIntersect(c1, c2): # each circle is a triple (x,y,r) dist = ((c1[0]-c2[0])**2 + (c1[1]-c2[1])**2)**0.5 if dist >= c1[2] and dist < c1[2] + c2[2]: return True else: return False
These slides are a direct adaptation of the slides used for MIT 6.0001 course present (as of February 2020) on MIT OCW web site.
Original work by:
Ana Bell, Eric Grimson, and John Guttag. 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Massachusetts Institute of Technology: MIT OpenCourseWare. License: Creative Commons BY-NC-SA.
Adapted by and for:
Assoc. Prof. Dr. Burkay Genç. MUH101 Introduction to Programming, Spring 2020. Hacettepe University, Computer Engineering Department.