more on lists (slides)

CSc 110 – More lists

Announcements

  • Fill out SCS at scsonline.ucatt.arizona.edu
  • If 80% of students complete the survey, one of your lowest quizzes grade will be replaced with full points (2.5/2.5 pts)
  • Review session next Tuesday at 5pm.
  • Midterm 3 next Wednesday, Apr 23.

Combining lists

What will happen? Run this code on your computer and on Python Tutor

vegetables = ["kale", "carrot", "peas", "celery"]
fruits = ["banana", "tomato", "pear"]
print(vegetables + fruits)

Will vegetables be mutated?

def combine_lists(list_1, list_2):
  list_1 = list_1 + list_2
  return list_1
  
if __name__ == "__main__":
  vegetables = ["kale", "carrot", "peas", "celery"]
  fruits = ["banana", "tomato", "pear"]
  combine_lists(vegetables, fruits)
  print(vegetables)
['kale', 'carrot', 'peas', 'celery']

Combining lists

Will vegetables be mutated? Run this code on your computer and on Python Tutor

def combine_lists(list_1, list_2):
  for item in list_2:
    list_1.append(item)
  return list_1
  
if __name__ == "__main__":
  vegetables = ["kale", "carrot", "peas", "celery"]
  fruits = ["banana", "tomato", "pear"]
  combine_lists(vegetables, fruits)
  print(vegetables)
['kale', 'carrot', 'peas', 'celery', 'banana', 'tomato', 'pear']

Combining lists

Will vegetables be mutated? Run this code on your computer and on Python Tutor

def combine_lists(list_1, list_2):
  list_1.extend(list_2)
  return list_1
  
if __name__ == "__main__":
  vegetables = ["kale", "carrot", "peas", "celery"]
  fruits = ["banana", "tomato", "pear"]
  combine_lists(vegetables, fruits)
  print(vegetables)
['kale', 'carrot', 'peas', 'celery', 'banana', 'tomato', 'pear']

list methods

  • .append(value)
  • .insert(index, value)
  • .pop(index)
  • .remove(value)
  • .extend(list)

Evaluate the code

Indicate when an error occurs.

numbers = [1, 2, 31, 0, 2]
numbers.append(40)
numbers # evaluate this at this point

numbers.insert(3, 10)
numbers # evaluate this at this point

numbers.remove(2)
numbers # evaluate this at this point

numbers.pop(2)
numbers # evaluate this at this point

new_list = [40, 50, 60]
numbers.extend(new_list)
numbers # evaluate this at this point

Evaluate the code – solution

numbers = [1, 2, 31, 0, 2]
numbers.append(40)
numbers # evaluate this at this point
[1, 2, 31, 0, 2, 40]
numbers.insert(3, 10)
numbers # evaluate this at this point
[1, 2, 31, 10, 0, 2, 40]
numbers.remove(2)
numbers # evaluate this at this point
[1, 31, 10, 0, 2, 40]
numbers.pop(2)
numbers # evaluate this at this point
[1, 31, 0, 2, 40]
new_list = [40, 50, 60]
numbers.extend(new_list)
numbers # evaluate this at this point
[1, 31, 0, 2, 40, 40, 50, 60]

Write a function

  1. Its name is remove_and_append
  2. It has three parameters, all lists: original_list, to_remove_list, to_append_list
  3. It mutates the original_list removing values found in to_remove_list and then appending the values in to_append_list

Test case:

test_list = [1, 2, 3]
remove_and_append(test_list, [2, 3, 4], [10, 11])
assert test_list == [1, 10, 11]

Write a function – solution

def remove_and_append(original_list, to_remove_list, to_append_list):
  for item in to_remove_list:
    if item in original_list:
      original_list.remove(item)
  original_list.extend(to_append_list)
  return original_list

if __name__ == "__main__":
  test_list = [1, 2, 3]
  remove_and_append(test_list, [2, 3, 4], [10, 11])
  assert test_list == [1, 10, 11]
  print(test_list) # [1, 10, 11]
[1, 10, 11]

Write python code

Write python code that determines if a number is prime or not.

  • A number n is NOT prime if it is divisible by any integer between 2 and n / 2.

  • The integer 1 is not a prime number.

assert is_prime(4) == False # divisible by 2
assert is_prime(5) == True  # not divisible by 2
assert is_prime(6) == False # divisible by 2 or 3
assert is_prime(11) == True # not divisible by any number between 2 and 5

Determining prime number - solution

def is_prime(n):
    if n == 1:
         return False
    for denominator in range(2, int(n/2 + 1)):
          if n % denominator == 0:
               return False
    return True
  
if __name__ == "__main__":
  assert is_prime(4) == False 
  assert is_prime(5) == True  
  assert is_prime(6) == False 
  assert is_prime(11) == True
  print("pass all test cases")
pass all test cases

Write python code

Name your file prime_numbers.py and submit for attendance. Test cases:

if __name__ == "__main__":
    numbers = [102, 250, 509, 345, 2, 4, 547]
    result = create_dictionary(numbers)
    assert result == {102: False, 250: False, 509: True, 345: False, 
                      2: True, 4: False, 547: True}

    numbers = [102, 250, 509, 345, 2, 4, 547]
    mutate_list(numbers)
    assert numbers == [False, False, True, False, True, False, True]

    numbers = [102, 250, 509, 345, 2, 4, 547]
    remove_primes(numbers)
    assert numbers == [102, 250, 345, 4]

Write python code – solution

def create_dictionary(numbers):
    result = {}
    for n in numbers:
        result[n] = is_prime(n)
    return result

def mutate_list(numbers):
    for i in range(len(numbers)):
        numbers[i] = is_prime(numbers[i])
    return numbers
          
def remove_primes(numbers):
    for i in range(len(numbers)-1, -1, -1):
        if is_prime(numbers[i]):
            numbers.pop(i)
    return numbers