Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

INSTRUCTIONS: Examine the starter code in the editor window, and run the code in the interpreter. The function extract _ even _ numbers _ in

INSTRUCTIONS:
Examine the starter code in the editor window, and run the code in the interpreter.
The function extract_even_numbers_in_list extracts the elements of a list that are even. The list my_evens has been created by calling this function on my_list. Inspect the data in my_evens.
Create a new list called my_evens2 by using a list comprehension that does the same thing as extract_even_numbers_in_list, but all in one line.
The function extract_digits_from_string extracts the characters in a string that are digits. In this function, the method string.is_digit is used to test whether or not a string is a digit (or collection of digits). The list str_digits has been created by calling this function on the supplied string s. Inspect the data in str_digits.
Create a new list called str_digits2 by using a list comprehension that d
STARTER CODE:
my_list =[1,3,14,22,29,43,89,102,256]
def extract_even_numbers_in_list(alist):
"""
Returns a list of the numbers in the input alist
that are even numbers
NOTE: n%2==0 if n is even, n%2==1 if n is odd
"""
result =[]
for elem in alist:
if elem%2==0:
result.append(elem)
return result
def extract_digits_from_string(s):
"""
Returns a list of all the digits that appear in a string,
in the order in which they are encountered.
"""
result =[]
for c in s:
if c.isdigit():
result.append(c)
return result
def make_dict_of_squares(n):
"""
Returns a dictionary that maps from integers to their squares,
starting with 0 and ending at one less than the input n
"""
result ={}
for i in range(n):
result[i]= i*i
return result
my_evens = extract_even_numbers_in_list(my_list)
s = 'The answer is 42, but many people guess 12.'
str_digits = extract_digits_from_string(s)
squares = make_dict_of_squares(10)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions