Computer Science 15-110, Spring 2011
Class Notes: Reasoning-About-Code Practice


Reasoning-About-Code Practice

Describe what each of the following functions do at a high-level in a few words of plain English.  Do not describe what the code does at a low level!
  1. # 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."
    -------------------------------------------------------------------------

  2. 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.
    -------------------------------------------------------------------------

  3. 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
    -------------------------------------------------------------------------

  4. 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
    -------------------------------------------------------------------------

  5. 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.
    -------------------------------------------------------------------------

  6. 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.
    -------------------------------------------------------------------------

  7. # 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)
    -------------------------------------------------------------------------

  8. 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.
    -------------------------------------------------------------------------