Skip to main content

Section 12.10 Week 9 Lab

Note 12.10.1. Material Covered.

Sequences (Chapter 9)

Subsection 12.10.1 Level 1

In this level you will combine two lists and remove all the double entries. The starter code below gives you two lists. Follow the steps (all of your code can go inside main()).
  1. Create a new list called temp_list and fill it with all the elements from the two provided lists (hint: use β€˜+’).
  2. Create another new list called final_list and set it to empty.
  3. Iterate through temp_list, and for each item, check to see if that item is in final_list, if not, add it to final_list (hint: use append to add the item to final_list).
  4. Print out the final list to the console to check the output.
Your output should look like this:

Subsection 12.10.2 Level 2

In this level you are given a jumbled string that you must decode to reveal the hidden message. The starter code below gives you a main function, with a coded message string already provided.
  1. Define a decoder function that accepts a string input and returns a string output. Make sure you add type annotation for this function. The steps below tell you what to put inside this function. For now just print out and then return the string that is passed in. Call this function from main. Your main function and output should look like this:
  2. Inside your decoder function, start by reversing the string so that the last character of the string becomes the first, the second last character becomes the second and so on. Reversing a string can be done using the slice operator with [::-1] as the slicing information. Inside your function, print the resulting string after reversing to ensure the reversal has worked.
  3. Inside the function, split the message using β€˜v’ as the delimiter, saving the result to a temporary list variable. Print out this list to ensure that you have this working.
  4. Create a new variable called space and set it equal to a blank space. Now, join the list back together into a string using the newly created space variable. Save the result of this join to a new variable. Print out this joined message to make sure that you have a single string that looks like this:
  5. Using a for loop, iterate through each character in the joined message. If the character is a β€˜b’ swap it out with an β€˜s’. The image below demonstrates how to swap out a character in a string using the index() operator, slices and concatenation.
  6. Return the final decoded message. Your output, including all the temporary decodings, should look like this:

Subsection 12.10.3 Level 3

In this level you will add some functionality to the poetry analyzer code below.
Run this code right now. The main() function reads in a poem, and then prints out the title. Warning: the spoken word poem that is read in on line 14 has explicit words in it - if you find the F-word offensive, you should use the other poem by commenting out line 14 and uncommenting line 18.
You can look at the two poem text files at the bottom of this page.
Your job is to add functions and uncomment the code in main() that calls those functions. Follow the instructions below the code window.
  1. First follow the example of the extract title function and create a function that will extract and return the author’s name. The author’s name will be on the first line of the file (both poetry files have <title> by <author> as the first line of the file. When you have this working, test it by uncommenting out the two lines in main() that call the extract_author function and print out the result.
  2. Next, add an extract_poem_body function. The poem files have the author and title on the first line of the text file and the URL source of the poem on the second line. We need a list of poetry lines that exclude these two. Inside the function use slicing on the list of poetry lines to get a list that excludes the first two lines. Return this smaller list of poetry lines. Again, test to make sure this works, by uncommenting the lines in main() that call this function and print out the poem (including the for loop).
  3. Now, define a calc_avg_words_per_line function. This function should loop through the lines of the poem and count how many words are in each line. After looping through all the lines, calculate the average and return it. Test this function by uncommenting the lines in main(). For the 10 responses poem, the average words per line should be 44.4. For the Flanders Fields poem, the average words per line should be 7.6111.
  4. The last thing to add is a count_word function. This function should take in the poem and a word, and then return the number of times the word is found in the poem. There are a couple of different ways to do this, keeping in mind that the poem is passed in as a list of lines. In the main() function, uncomment the lines that ask the end user for a search word and then call the count_word() function and prints out the result.
You have attempted of activities on this page.