final exam review (slides)

CSC – Final exam review

Q & A session

What questions should I ask?

  • When you can’t solve the problem without the solution
  • When the solution doesn’t make sense to you
  • When you don’t understand why points were deducted
  • When you solve it in another way but not sure if it is correct

We do not answer questions on projects due today.

Announcements

Final exam

  • Time: this Friday Dec 13, 6:00 - 8:00pm

  • Location: S SCI 100

  • Bring your photo ID

Quiz 12

current time

You have 10 minutes to complete the quiz.

Review

int()

int on “110”:

int("110") # 110
110

int on “CSC110”:

int("CSC110") # Error

Use case: use int() to convert "2" to 2 when reading and summing the number 2 from a .txt file.

string.isnumeric()

.isnumeric() on “110”:

"110".isnumeric() 
True

.isnumeric() on “CSC110”:

"csc110".isnumeric() 
False

Evaluate code

Evaluate last line:

numbers = (1000, 1, 2, 3)
numbers.add(4)
print(numbers[0]) 

Evaluate code - answer

Evaluate last line:

numbers = (1000, 1, 2, 3)
numbers.add(4)
print(numbers[0]) # Error

Correct: Error (code won’t proceed to last line)

Incorrect: 1000

Write to file - compare difference

version 1:

words = ['quick', 'brown', 'fox']
output = open("result.txt", "w")
for w in words:
  output.write(w + ' ')
output.close()

version 2:

words = ['quick', 'brown', 'fox']
output = open("result.txt", "w")
for w in words:
  output.write(w + '\n')
output.close()

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.

More concepts

  • loop table (1D and 2D lists)

  • sorting (number of sweeps and swaps)

  • string methods

    • iterate string
  • data structures and their mutability

    • iterate data structures

    • if conditions

  • reading, aggregating or writing to files