Calvin (Deutschbein)
W3Fri: 13 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:int) -> int: # we only use ints!
# we'll print the divisors here
return 0 # we'll return the number of divisors here
def divisors(n:int) -> int: # we only use ints!
# we'll print the divisors here
return 0 # we'll return the number of divisors here
def divisors(n:int) -> int: # we only use ints!
# we'll print the divisors here
return 0 # we'll return the number of divisors here
def x_divides_n(x:int, n:int) -> bool:
return False # return any bool for now.
def divisors(n:int) -> int: # we only use ints!
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:int, n:int) -> bool:
print("Finding if x =", x, "divides n =", n)
return False # return any bool for now.
>>> def divisors(n:int) -> int: # we only use ints!
... 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
0
>>>
print('a','b') # prints 'a' then prints 'b' in the same line
print('a' 'b') # error/crash
def divisors(n:int) -> int: # we only use ints!
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:int) -> int:
if debugging:
print("Finding divisors of n =", n)
return 0 # placeholder value
>>> False and print('hi')
False
>>> True and print('hi')
hi
>>> def maybe(b:bool):
... b and print('yes')
...
>>> maybe(1 == 1)
yes
>>> maybe(1 == 2)
>>>
>>> def divisors(n:int) -> int:
... debugging and print('Finding divisors of n =', n)
... return 0
...
>>> debugging = True
>>> divisors(7)
Finding divisors of n = 7
0
>>> debugging = False
>>> divisors(7)
0
def divisors(n:int) -> int: # print divisors, return number of
debugging and print('Finding divisors of n =', n)
return 0 # placeholder
def x_divides_n(x:int, n:int) -> bool:
debugging and print("Finding if x =", x, "divides n =", n)
return False # return any bool for now.
def divisors(n:int) -> int: # print divisors, return number of
debugging and print('Finding divisors of n =', n)
return 0 # placeholder
def x_divides_n(x:int, n:int) -> bool:
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 False # return any bool for now.
def x_divides_n(x:int, n:int) -> bool:
debugging and print("Finding if x =", x, "divides n =", n)
remainder = x % n
debugging and print("x % n is", remainder)
return False # return any bool for now.
>>> x_divides_n(3, 10)
Finding if x = 3 divides n = 10
x % n is 3
False
>>> x_divides_n(3, 9)
Finding if x = 3 divides n = 9
x % n is 3
False
>>> x_divides_n(3, 9)
Finding if x = 3 divides n = 9
x % n is 3
False
def x_divides_n(x:int, n:int) -> bool:
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 False # return any bool for now.
>>> x_divides_n(3,9)
Finding if x = 3 divides n = 9
rem of n div by x is 0
False
def x_divides_n(x:int, n:int) -> bool:
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:int, n:int) -> bool:
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:int) -> int: # we only use ints!
# 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:int) -> int: # we only use ints!
# 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:int) -> int: # we only use ints!
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:int) -> int: # we only use ints!
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:int) -> int: # we only use ints!
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:int) -> int: # we only use ints!
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:int) -> int: # we only use ints!
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:int) -> int: # we only use ints!
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:int) -> int: # we only use ints!
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
>>>
|
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
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 0
>>> divisors(6)
2
2 is a div, count is 1
3
3 is a div, count is 2
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:int) -> int: # 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:int,s:str) -> NoneType: # print -> None
print(n * ' ', s) # test - that is 'n times space plus s'