Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# list comprehension exercise def list_comprehension(): # given the list numbers nums = [1, 2, 3, 4, 5, 6, 7, 8, 9] # task 1:

# list comprehension exercise def list_comprehension(): # given the list numbers nums = [1, 2, 3, 4, 5, 6, 7, 8, 9] # task 1: Convert the list of numbers (nums here) to their squares. squared_numbers = [] #TODO complete this line print(squared_numbers) # should output: [1, 4, 9, 16, 25, 36, 49, 64, 81]

# task 2: Extract even numbers from the list nums even_numbers = [] #TODO complete this line print(even_numbers) # should output: [2, 4, 6, 8]

# task 3: Create a list of tuples with the first letter and the length of each word in a list words: words = ['apple', 'banana', 'cherry'] first_letter_len_tuple_list = [] #TODO complete this line print(first_letter_len_tuple_list) # should output: [('a', 5), ('b', 6), ('c', 6)]

# task 4: Flatten a list of list. nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flattened_list = [] #TODO complete this line print(flattened_list) # Should output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

def flip_dictionary(d): """ funtion that properly reverses the keys and values of a dictionary - each key (originally a value) should map to a value (originally key) that mapped to it. For example, flip_dictionary({"a": "1", "b": "2"}) => {"1": "a", "2": "b"}

d: a dictionary :return: a dictionary that switch d's (key, value) to (value, key) """ pass

if __name__ == '__main__': list_comprehension() # morse code morse_code = {'a': '.-', 'b': '-...', 'c': '-.-.', 'd': '-..', 'e': '.', 'f': '..-.', 'g': '--.', 'h': '....', 'i': '..', 'j': '.---', 'k': '-.-', 'l': '.-..', 'm': '--', 'n': '-.', 'o': '---', 'p': '.--.', 'q': '--.-', 'r': '.-.', 's': '...', 't': '-', 'u': '..-', 'v': '...-', 'w': '.--', 'x': '-..-', 'y': '-.--', 'z': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', ' ': '/'} # We can use the morse code to encode a message message = "This is a secret message" encoded_message = ' '.join([morse_code[char.lower()] for char in message]) print(encoded_message) # To decode the message, we first flip the morse code flip_morse_code = flip_dictionary(morse_code) # Then decode the message decoded_message = ''.join([flip_morse_code[code] for code in encoded_message.split()]) print(decoded_message)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

love of humour, often as a device to lighten the occasion;

Answered: 1 week ago

Question

orderliness, patience and seeing a task through;

Answered: 1 week ago

Question

well defined status and roles (class distinctions);

Answered: 1 week ago