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()).
Create a new list called temp_list and fill it with all the elements from the two provided lists (hint: use β+β).
Create another new list called final_list and set it to empty.
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).
Print out the final list to the console to check the output.
Your output should look like this:
Subsection12.10.2Level 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.
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:
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.
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.
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:
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.
Return the final decoded message. Your output, including all the temporary decodings, should look like this:
Subsection12.10.3Level 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.
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.
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).
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.
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.