Module 6 Assignments

Short Project 05

Due date: Oct 8, Tuesday at 7pm

Short Programming projects are submitted during our weekly 45-minute in-person lab sessions. Each lab sessions is guided by two TAs. The instructions for the short project will be available only during the lab sessions. To schedule your lab session go to the weekly lab session spreadsheet.

Programming Problems

Programming Problems should be submitted to gradescope.

Programming Problem 11

Due date: Oct 8, Tuesday at 7pm

Write a Python function that does the following:

  1. Its name is sum_all
  2. It takes a list of numeric values as argument: numbers
  3. It returns the sum of all elements in numbers
  4. Use a while loop (define an index before the loop, use index in the while condition, change the index inside the loop)

Name the program sum.py. Make sure that gradescope gives you the points for passing the test cases.

Test cases:

def main():
  value = sum_all([])
  assert value == 0, f"expected return value was 0, but function returned {value}" 

  value = sum_all([0, 0, 0, 0, 0])
  assert value == 0, f"expected return value was 0, but function returned {value}" 

  value = sum_all([1, -1, 2, -2, 3, -3])
  assert value == 0, f"expected return value was 0, but function returned {value}" 

  value = sum_all([1, 2, 3, 4, 5])
  assert value == 15, f"expected return value was 15, but function returned {value}" 

  print("All tests passed.")

main()

Programming Problem 12

Due date: Oct 8, Tuesday at 7pm

Write a Python function that does the following:

  1. Its name is concatenate
  2. It takes a list of strings as argument: words
  3. It returns a string with all items in words concatenated and separated by spaces
  4. Use a while loop (define an index before the loop, use index in the while condition, change the index inside the loop) – the last word in the resulting string is not followed by space

Name the program concatenate.py. Make sure that gradescope gives you the points for passing the test cases.

Test cases:

def main():
  value = concatenate([])
  assert value == "", \
      f"expected return value was an empty string, but function returned {value}" 

  value = concatenate(["", "", ""])
  assert value == "  ", \
      f"expected return value was an \"  \", but function returned {value}" 
  
  value = concatenate(["Hi", "there"])
  assert value == "Hi there", \
      f"expected return value was an \"Hi There\", but function returned {value}" 

  print("All tests passed.")

main()