Calvin (Deutschbein)
W3Fri: 12 Sep
In engineering, debugging is the process of finding the root cause of and workarounds and possible fixes for bugs.
For software, debugging tactics can involve interactive debugging, control flow analysis...
In engineering, a bug is a design defect in an engineered system that causes an undesired result.
|
>>> for i in range(10,20):
... if i % 3 == 0:
... print(i)
... if not i % 3:
... print(i)
...
12
12
15
15
18
18
>>> |
Does it happen more than once? | |||
No | Yes (it's a loop) | ||
Is it conditional (do we ask Karel a question)? |
No |
|
|
Yes |
|
|
def divisors(n):
# we'll print the divisors here
return 0 # we'll return the number of divisors here
def divisors(n):
# we'll print the divisors here
return 0 # we'll return the number of divisors here
def divisors(n):
# we'll print the divisors here
return 0 # we'll return the number of divisors here
def x_divides_n(x, n):
return None # We return what we know, which is nothing!
def divisors(n):
print("Finding divisors of n =", n) # what does this print?
# we'll print the divisors here
return 0 # we'll return the number of divisors here
def x_divides_n(x, n):
print("Finding if x =", x, "divides n =", n)
return None
def divisors(n):
print("Finding divisors of n =", n)
# we'll print the divisors here
return 0 # we'll return the number of divisors here
divisors(7)
Finding divisors of n = 7
def divisors(n):
print("Finding divisors of n =", n) # what does this print?
# we'll print the divisors here
return 0 # we'll return the number of divisors here
debugging = True
def divisors(n):
if debugging:
print("Finding divisors of n =", n)
return 0 # placeholder value
False and print('ABCDEF')
True and print('123456')
123456
def divisors(n):
debugging and print('Finding divisors of n =', n)
return 0
debugging = True
divisors(14)
debugging = False
divisors(28)
Finding divisors of n = 14
def divisors(n): # print divisors, return number of
debugging and print('Finding divisors of n =', n)
return 0 # placeholder
def x_divides_n(x, n):
debugging and print("Finding if x =", x, "divides n =", n)
return None
def divisors(n): # print divisors, return number of
debugging and print('Finding divisors of n =', n)
return 0 # placeholder
def x_divides_n(x, n):
debugging and print("Finding if x =", x, "divides n =", n)
# x divides n if x divided by n has no remainder.
# The modulo operator % computes remainders
remainder = x % n
debugging and print("x % n is", remainder)
return None
def x_divides_n(x, n):
debugging and print("Finding if x =", x, "divides n =", n)
remainder = x % n
debugging and print("x % n is", remainder)
return None
x_divides_n(3, 10)
x_divides_n(3, 9)
Finding if x = 3 divides n = 10
x % n is 3
Finding if x = 3 divides n = 9
x % n is 3
Finding if x = 3 divides n = 10
x % n is 3
Finding if x = 3 divides n = 9
x % n is 3
def x_divides_n(x, n):
debugging and print("Finding if x =", x, "divides n =", n)
# x divides n if x divided by n has no remainder.
# The modulo operator % computes remainders
remainder = n % x
debugging and print("rem of n div by x is", remainder)
return None
x_divides_n(3,9)
Finding if x = 3 divides n = 9
rem of n div by x is 0
def x_divides_n(x, n):
debugging and print("Finding if x =", x, "divides n =", n)
remainder = n % x
debugging and print("rem of n div by x is", remainder)
if remainder == 0 : # Then x divides n
return True
else:
return False
if remainder == 0 : # Then x divides n
return True
else:
return False
return remainder == 0
return remainder == 0
def x_divides_n(x, n):
debugging and print("Finding if x =", x, "divides n =", n)
remainder = n % x
debugging and print("rem of n div by x is", remainder)
return remainder == 0 # Then x divides n
def divisors(n):
# we'll print the divisors here that aren't 1 or n
return 0 # we'll return the number of divisors here
def divisors(n):
# we'll print the divisors here that aren't 1 or n
return 0 # we'll return the number of divisors here
>>> n = 9
>>> range(1,n)
range(1, 9)
def divisors(n):
for x in range(1,n):
x_divides_n(x,n) # can test with debug
return 0 # we'll return the number of divisors here
>>> divisors(4)
Finding if x = 1 divides n = 4
rem of n div by x is 0
Finding if x = 2 divides n = 4
rem of n div by x is 0
Finding if x = 3 divides n = 4
rem of n div by x is 1
0
def divisors(n):
for x in range(2,n):
x_divides_n(x,n) and print(x) # short circuit
return 0 # we'll return the number of divisors here
def divisors(n):
for x in range(2,n):
x_divides_n(x,n) and print(x) # short circuit
return 0 # we'll return the number of divisors here
debugging = True
divisors(4)
Does x = 2 divide n = 4
rem of n div by x is 0
2
Does x = 3 divide n = 4
rem of n div by x is 1
0 |
debugging = False
divisors(4)
2
0
|
Which is easier?
def divisors(n):
for x in range(2,n):
x_divides_n(x,n) and print(x) # short circuit
return 0 # we'll return the number of divisors here
def divisors(n):
for x in range(2,n):
x_divides_n(x,n) and print(x) # short circuit
return 0 # we'll return the number of divisors here
def divisors(n):
count = 0
for x in range(2,n):
if x_divides_n(x,n):
print(x) # print if divisor
return 0 # we'll return the number of divisors here
def divisors(n):
count = 0
for x in range(2,n):
if x_divides_n(x,n):
print(x) # print if divisor
count = count + 1
debugging and print(x,'is a div, count is', count)
return 0 # we'll return the number of divisors here
divisors(4)
Does x = 2 divide n = 4
rem of n div by x is 0
2
2 is a div, count is 1
Does x = 3 divide n = 4
rem of n div by x is 1
0
debugging = True
def divisors(n):
count = 0
for x in range(2,n):
if x_divides_n(x,n):
print(x)
count = count + 1
debugging and print('DIVISOR:', x,'is a div, count is', count)
return 0
def x_divides_n(x,n):
debugging and print("X_DIV_N:", "Does x =", x, "divide n =", n)
remainder = n % x
debugging and print("X_DIV_N:", "rem of n div by x is", remainder)
return remainder == 0
divisors(6) |
X_DIV_N: Does x = 2 divide n = 6
X_DIV_N: rem of n div by x is 0
2
DIVISOR: 2 is a div, count is 1
X_DIV_N: Does x = 3 divide n = 6
X_DIV_N: rem of n div by x is 0
3
DIVISOR: 3 is a div, count is 2
X_DIV_N: Does x = 4 divide n = 6
X_DIV_N: rem of n div by x is 2
X_DIV_N: Does x = 5 divide n = 6
X_DIV_N: rem of n div by x is 1
0
>>>
|
def divisors(n):
count = 0
for x in range(2,n):
if n % x == 0:
print(x)
count = count + 1
debugging and print(x,'is a div, count is', count)
return count
There is a better (fewer total loops) way to do this with a while loop. Find for up to +15% on PS2.
Above + solve in one line + verbal explanation for +100% on PS2
def divisors(n): # this is useful
count = 0
for x in range(2,n):
if n % x == 0:
print(x)
count = count + 1
return count
def n_spaces_before(n,s):
print(n * ' ', s) # test - that is 'n times space plus s'