Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.

Similar presentations


Presentation on theme: "Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday."— Presentation transcript:

1 Tutorial 9 Iteration

2 Reminder Assignment 8 is due Wednesday

3 Review Loops! ◦ while ◦ for ◦ nested

4 Review – While loops ***initialize variables*** while condition: ***body of while*** ***update variables *** It will continue to execute the body of the while loop until condition == False Variables MUST be updated, otherwise there might be an infinite loop! This part will continuously be executed until condition is False

5 Review – For loops for item in collection: *** body of for *** It will execute the body of the for loop len(collection) times, once for every element in collection Similar to map ; goes through every element in the collection Be careful of mutating your collection inside the loop!

6 while loop version of a for loop i = 0 while i < len(collection): item = collection[i] ***body of for*** i = i + 1 for item in collection: ***body of for***

7 Review – Nested loops for i in collection1: *** body of outer for *** for j in collection2: *** body of inner for *** For each i in collection1, the inner for loop will be executed Similar to nested lambda in Scheme Body of outer for loop The inner for loop will be executed len(collection2) times

8 Question 1 - all_same_type Write a function all_same_type that consumes a list and produce true if all members of that list are of the same type, else false. Note that Python's built-in type function does not distinguish between types of lists: ◦ type([1,2]) == type(['a','b']) => True

9 Question 3 – valid_input Write a function called valid_input that consumes a string to be used as the prompt, a list of strings of value inputs, and produces the value entered that is equal to one of the elements from the list. The function should continuously prompt the user for input until the user enters a value in the list of valid input values. You may assume that the user enters input that is the correct type.

10 Question 6 Write a Python function divisible_by_3 that consumes a Nat (called n), and produces True if n is divisible by 3, and False if it is not divisible by 3. You must use the following algorithm: ◦ The only numbers less than 10 that are divisible by 3 are: 0,3,6,9 ◦ A number is divisible by 3 if the sum of its digits is also divisible by 3. If the sum of the digits of the number is greater than 10, calculate the sum of the digits of the sum, and repeat until you get a number less than 10.

11 Question 4 – max_even_sum Write a Python function max_even_sum that consumes a nonempty list of lists of positive integers. It computes the sum of the even integers in each sublist, and produces the largest such sum. If a sublist contains no even integers, its sum is zero.

12 Question 2 – selection_sort Write a function selection_sort that consumes a list of numbers. The function will mutate the consumed list to be sorted in increasing order.

13 Question 5: RL_encode Write a Python function RL_encode that consumes a string made up of the four letters {A, C, G, T}, and returns a run- length encoding of the string.

14 Question 5: RL_decode Write a Python function RL_decode that consumes a run-length encoding of a string E made up of only {A,C,G,T} and returns the string D for which E is the run-length encoding.


Download ppt "Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday."

Similar presentations


Ads by Google