Exploring Lists of Lists and File I/O in Python

cmpt 120 n.w
1 / 7
Embed
Share

Dive into exploring lists of lists for interior decorators and practice file input/output operations in Python. Enhance your understanding of selection and insertion sort algorithms while working on practical exercises and solutions.

  • Python
  • Lists of Lists
  • File I/O
  • Selection Sort
  • Insertion Sort

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CMPT 120 Lecture 31 Practice Exercises 7 Partial Solution

  2. 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

  3. Question 1 Exploring Lists of Lists 1. Have a look at this code https://repl.it/repls/FuzzyGlitteringTexts 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 aFloor 4. Printing: how differently would these three Python code fragments print the content of the list of lists aFloor 1) print(aFloor) 2) for row in range(theHeight): print(aFloor[row]) 3) the function def printFloor(theFloor, theHeight): Solution: https://repl.it/repls/MoralJitteryMatter 3

  4. 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 no solution yet 4

  5. Question 2 Exploring File I/O a) Have a look at the 5 demos in this code https://repl.it/repls/IllSomePasswords What do they do? no solution yet b) Problem Statement: Write a Python program that reads the file words.txt found here: http://greenteapress.com/thinkpython2/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

  6. 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,26,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,__ 47,15,36,26,27,38,5,19__ 6

  7. 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,26,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)? No Does the sorted section of data look the same? No 7

More Related Content