CMPT 120 Lecture 31 Practice Exercises 7 Goals

  • Slides: 7
Download presentation
CMPT 120 Lecture 31 – Practice Exercises 7

CMPT 120 Lecture 31 – Practice Exercises 7

Goals for Today! 1. Exploring Lists of Lists -> good prep for our Assignment

Goals for Today! 1. Exploring Lists of Lists -> good prep for our Assignment 2 2. Exploring File I/O -> also good prep for our Assignment 2 3. Practising Selection and Insertion Sort 2

Question 1 – Exploring Lists of Lists 1. Have a look at this code

Question 1 – Exploring Lists of Lists 1. Have a look at this code https: //repl. it/repls/Fuzzy. Glittering. Texts What does it do? Add code to it to … 2. Create a few more floor patterns 3. Put the string 'MM' at the centre of the first floor pattern Hint: Put the string in the ‘list of lists’ variable a. Floor 4. Printing: how differently would these three Python code fragments print the content of the ‘list of lists’ a. Floor 1) print(a. Floor) 2) for row in range(the. Height): print(a. Floor[row]) 3) the function def print. Floor(the. Floor, the. Height): 3

Question 1 – Exploring Lists of Lists (cont’d) Imagine you are an interior decorator

Question 1 – Exploring Lists of Lists (cont’d) Imagine you are an interior decorator and would like to see what the floor plan would look like if you were to place a sofa on the east edge of the room 5. Create a function that places a sofa (2 cells deep by 6 cells long), i. e. , the string 'SS' along the right-hand side edge of the floor Remember: The cells are 2 -character wide and 1 -character high 4

Question 2 – Exploring File I/O a) Have a look at the 5 demos

Question 2 – Exploring File I/O a) Have a look at the 5 demos in this code https: //repl. it/repls/Ill. Some. Passwords What do they do? b) Problem Statement: Write a Python program that reads the file words. txt found here: http: //greenteapress. com/thinkpython 2/code/words. txt and prints only the words it contains that have more than 20 characters (not counting whitespace and newline character) Our Python program must print one word per line 5

Question 3 – a) Selection Sort • Visualize the execution of the Selection Sort

Question 3 – a) Selection Sort • Visualize the execution of the Selection Sort using https: //visualgo. net/en/sorting • Once you have a good idea how Selection Sort works, sort the following data: 3, 38, 5, 47, 15, 36, 27, 2, 4, 19 using Selection Sort and write down what the unsorted section of the data looks like once 3 iterations of Selection Sort have executed, in order words, once data has 2, 3, 4 in its sorted section: 2, 3, 4, ______________ 6

Question 3 – b) Insertion Sort • Visualize the execution of the Insertion Sort

Question 3 – b) Insertion Sort • Visualize the execution of the Insertion Sort using https: //visualgo. net/en/sorting • Once you have a good idea how Insertion Sort works, sort the following data: 3, 38, 5, 47, 15, 36, 27, 2, 4, 19 using Insertion Sort After 3 iterations of Insertion Sort, does the unsorted section of data look the same as in your answer to Question 3 a)? Does the sorted section of data look the same? 7