final exam review (slides)

CSC – Final exam review

Announcements

Final exam

  • 1pm: Monday May 12, 1:00 - 3:00pm, GITT 129B

  • 2pm: Friday May 09, 1:00 - 3:00pm, GITT 129B

  • Bring your photo ID

Review of strings

string methods

  • string is not mutable, means every string method will return a new string

  • indexing a string

("Jimmy42"[0], "Jimmy42"[-1])
('J', '2')
  • slicing a string
("Jimmy42"[:2], "Jimmy42"[1:3])
('Ji', 'im')

string methods

  • .isnumeric() returns True if all the chars are numeric (0-9)
("Jimmy42".isnumeric(), "42".isnumeric())
(False, True)
  • .lower() returns a string with all lowercase chars

  • .strip() removing chars in the beginning or end

"Jimmy42".strip('42')
'Jimmy'
  • .split() return a list by splitting strings by char
"Jimmy42Jimmy42Jimmy".split('42')
['Jimmy', 'Jimmy', 'Jimmy']

string: concatenation

string: iteration

Review of lists

list methods

  • list[index] = value mutate a list value using index

  • .append(value) append an item to the end of a list

  • .insert(index, value) insert a value at index

  • .pop(index) remove an item at index

  • .remove(value) remove a value

  • .extend(list_b) concatenate with another list

  • list[index_a:index_b] list slicing

  • 2D lists

list: iterate with mutation

list: iterate without mutation

Review of dictionaries

dictionary methods

  • dict[key] = value mutate a value using key

  • dict[key] = value add a new key-value pair

  • dict.pop(key) remove a key-value pair using key

  • for k in dict: iterate only the keys

  • for v in dict.values(): iterate only the values

  • for k, v in dict.items(): iterate both

mutating a dictionary

adding to a dictionary

iterating a dictionary

Final exam also includes reading, aggregating or writing to files, and sortings

More practice problems

write a function

Write a python function that does the following:

  1. Its name is create_list

  2. It takes two arguments, a set of strings and an integer n

  3. It returns a list that contains each string from the set repeated n times

items = {"banana", "apple", "pear"}
print(create_list(items, 2)) 
# order does not matter
# ['banana', 'banana', 'apple', 'apple', 'pear', 'pear']

write a function - solution

def create_list(items, n):
  new_list = [ ]
  for value in items:
    new_list.extend([value] * n)
  return new_list

def main():
  items = {"banana", "apple", "pear"}
  print(create_list(items, 2))  
  
main()
['apple', 'apple', 'pear', 'pear', 'banana', 'banana']

write a function

  1. Its name is maximum
  2. It takes a variable number of arguments: *values
  3. It returns the highest value in values
assert maximum(1) == 1
assert maximum(2,4,6) == 6
assert maximum() == None

Write a function – solution

def maximum(*values):
  max = None
  for v in values:
    if max == None or v > max:
      max = v
  return max

def main():
  assert maximum(1) == 1
  assert maximum(2,4,6) == 6
  assert maximum() == None

main()

Can we change the order of max == None and v > max?

No we CANNOT. Try it on your own laptop.