Computer Science 15-110, Spring 2011
Class Notes: Reasoning-About-Code Practice
# Basic example to get started
def f(x,y):
if (x > y):
return y
return x
-------------------------------------------------------------------------
Solution (in white text):
f(x,y)
returns the smaller of x or y. Note that you would receive zero points
if you answered something like this: "f compares x and y, and if x is
larger than y, it returns y, otherwise it returns x."
-------------------------------------------------------------------------
def f(s):
result = 0
for c in s:
d = ord(c) - ord("0")
if ((d >= 0) and (d <= 9)):
result += 1
return result
-------------------------------------------------------------------------
Solution (in white text):
f(s) returns the number of digits in the string s.
-------------------------------------------------------------------------
def f(s):
for i in range(len(s)):
if (s[i] != s[len(s)-1-i]):
return False
return True
-------------------------------------------------------------------------
Solution (in white text):
f(s) returns True if s is a palindrome and False otherwise
-------------------------------------------------------------------------
def f(s):
t = ""
for c in s:
t = c + t
return (s == t)
-------------------------------------------------------------------------
Solution (in white text):
f(s) returns True if s is a palindrome and False otherwise
-------------------------------------------------------------------------
def f(list):
copy = [] + list
copy.sort()
return (copy == range(len(list)))
-------------------------------------------------------------------------
Solution (in white text):
f(list)
returns True if the list contains the numbers from 0 to (n-1) exactly
once, and in any order (that is, a permutation), for any integer n, and
False otherwise.
-------------------------------------------------------------------------
def f(list):
seen = [False]*len(list)
for value in list:
if ((value < 0) or (value >= len(list)) or (seen[value] == True)):
return False
seen[value] = True
return True
-------------------------------------------------------------------------
Solution (in white text):
f(list)
returns True if the list contains the numbers from 0 to (n-1) exactly
once, and in any order (that is, a permutation), for any integer n, and
False otherwise.
-------------------------------------------------------------------------
# helper function for fn)
def make2dList(rows, cols):
a=[]
for row in range(rows): a += [[0]*cols]
return a
def f(n):
result = make2dList(n, n)
for rc in range(n):
result[rc][rc] = 1
return result
-------------------------------------------------------------------------
Solution (in white text):
f(n) returns an n-by-n "identity" matrix (all 1's down the top-left-to-bottom-right diagonal, 0's elsewhere)
-------------------------------------------------------------------------
def g(matrix):
# assume matrix is a 2d list of integers
rows = len(matrix)
cols = len(matrix[0])
if (rows != cols):
return False
for row in range(rows):
for col in range(cols):
if (((row != col) and (matrix[row][col] != 0)) or
((row == col) and (matrix[row][col] != 1))):
return False
return True
-------------------------------------------------------------------------
Solution (in white text):
g(matrix) returns True if matrix is an "identity" matrix and False otherwise.
-------------------------------------------------------------------------