ProblemSet11_23f - Jupyter Notebook

.pdf

School

Washtenaw Community College *

*We aren’t endorsed by this school

Course

141

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

6

Uploaded by DeanPelican3624

Report
12/3/23, 1:45 PM ProblemSet11_23f - Jupyter Notebook https://zeus-clone.wccnet.edu/user/e28785829b1e407584d9d2477cd58899/notebooks/ProblemSet11_23f.ipynb# 1/6 Problem Set 11 Overiew This learning unit's focus is on the use of the built-in map() (https://www.w3schools.com/python/ref_func_map.asp) , filter() (https://www.w3schools.com/python/ref_func_filter.asp) , zip() (https://www.w3schools.com/python/ref_func_zip.asp) functions, as well as list comprehension (https://www.w3schools.com/python/python_lists_comprehension.asp) . Solve the problems below using these tools as indicated in the instructions. Run the code cell below The objects that are created in the cell below will be used throughout the problem set. Do not overwrite these objects, and do not duplicate this code in your solutions. In [ ]: Problem 1 Retrieve the computeScrabbleScore() function developed in previous problem sets (PS 5, 6, 7), or re-write it here. This function accepts a string as a parameter and returns an integer that represents the Scrabble score for the string. The letter_values dictionary is provided below. Outside of the function, use the map() function to make a list of Scrabble scores for each respective item in the words list. Print the list of scores. In [ ]: In [ ]: Expected Output: [14, 12, 4, 7, 11, 8, 16, 5] <enter your name here> myInts = [ i for i in range ( 1 , 301 ) ] words = [ 'python' , 'program' , 'list' , 'string' , 'unix' , 'hours' , 'syntax' , myQuote = "The quality of mercy is not strained it dropeth as the gentle r letter_values = { 'a' : 1 , 'b' : 3 , 'c' : 3 , 'd' : 2 , 'e' : 1 , 'f' : 4 , 'g' : 2 , 'h' : 4 , 'i' : 1 , 'j' : 8 , 'k' : 5 , 'l' : 1 , 'm' : 3 , 'n' : 1 , 'o' : 1 , 'p' : 3 , 'q' : 10 , 'r' : 1 , 's' : 1 , 't' : 1 , 'u' : 1 , 'v' : 8 , 'w' : 4 , 'x' : 8 , 'y' : 4 , 'z' : 10 } # write your solution below 1 1 2 3 1 2 3 1 2
12/3/23, 1:45 PM ProblemSet11_23f - Jupyter Notebook https://zeus-clone.wccnet.edu/user/e28785829b1e407584d9d2477cd58899/notebooks/ProblemSet11_23f.ipynb# 2/6 Problem 2 Using the words list and the selectWord() function (described below), produce a list named evenWords that contains only those items from words that have an even number of characters. Write selectWord() such that it accepts the parameter word , which is a string. The function will return True if the length of the string is an even number. Otherwise it will return False . Outside of the function use filter() along with selectWord() to create and print evenWords . In [ ]: Expected Output: ['python', 'list', 'string', 'unix', 'syntax'] Problem 3 Alter your solution for Problem 2 to use only a list comprehension (instead of the filter() function) to create a list of words with an odd number of letters. You are free to re- use the selectWord() function from Problem 2, but do not re-write it here. In [ ]: Expected Output: ['program', 'hours', 'error'] Problem 4 Use a list comprehension to create a reverse-ordered list of tuples based on the myQuote string. The tuples will hold each word from myQuote , along with it's Scrabble score. The built-in reversed() function will be useful here. In [ ]: Expected Output: # write your code below # write your code here # write your code below 1 2 1 2 1 2
12/3/23, 1:45 PM ProblemSet11_23f - Jupyter Notebook https://zeus-clone.wccnet.edu/user/e28785829b1e407584d9d2477cd58899/notebooks/ProblemSet11_23f.ipynb# 3/6 [('heaven', 16), ('from', 9), ('rain', 4), ('gentle', 7), ('the', 6), ('as', 2), ('dropeth', 13), ('it', 2), ('strained', 9), ('not', 3), Problem 5 The variable names is a list where each item is a string in "Firstname Lastname" format. Make a list named newNames from names where each item is in "Lastname, Firstname" format. Use list comprehension to convert the list. In [ ]: In [ ]: Expected Output: ['Newton, Isaac', 'Einstein, Albert', 'Bohr, Niels', 'Curie, Marie', 'Darwin, Charles', 'Galilei, Galileo', 'Mead, Margaret'] Problem 6 Use list comprehension to create a list of those numbers from myInts that are evenly divisible by 17 but are not evenly divisible by 51. Use only three lines of code: the first will create the list; the second will print the list; the third will print the length of the list. In [ ]: Expected Output: [17, 34, 68, 85, 119, 136, 170, 187, 221, 238, 272, 289] list length: 12 Problem 7 Use list comprehension to create a list of numbers from myInts that contain 99 or 44. Use only three lines of code: the first will create the list; the second will print the list; the third will print the length of the list. names = [ "Isaac Newton" , "Albert Einstein" , "Niels Bohr" , "Marie Curie" , "Charles Darwin" , "Galileo Galilei" , "Margaret Mead" ] # write your code below # write your code below 1 2 1 2 1 2
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help