Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Python script that inputs a list of integers and passes it to the following functions, which you implement. Do not have any functions

Write a Python script that inputs a list of integers and passes it to the following functions, which you implement. Do not have any functions input or output values. All values used by a function should be input before the function is called and passed to it as arguments. The value computed by a function should be returned, not output by it. Use the test code provided below, which satisfies these constraints and outputs the results of all function applications so we can test your implementations. The test code asks the user at the beginning to input a list of integers, which is used as the running list for testing the functions. When we say below that a function is passed the running list of integers, it could actually be passed any list of integers; it just turns out that the running list is what is passed in our test code.

A function min_max() that is passed any number of integer arguments and returns a tuple with the smallest and largest among them. Use the arbitrary argument lists technique. If only one argument is provided, the two elements in the tuple will be the same. If no argument is provided, return an empty tuple. (To use the list that you input at the beginning, you have to unpack it (use a *) when you provide it as an argument. Note that, if you do this, youll be unpacking the list (with *) when you pass it in and packing the arguments back up (using again a *) in the argument list in the function definition.) DO NOT ANSWER.

A function my_slice() that returns a list containing by default every 2nd element. This default can be overridden by passing a value for the keyword argument step. See the Step Argument slides. DO NOT ANSWER.

A function list_diff() that is passed the running list of integers and a second list of integers. (Prompt for and read the second list of integers before the function call and pass it (and the original list) to the function.) The function should return a list of all the integers in the running list that are not also in the second list. For example, if the two lists are [5, 1, 4, 2] (as the running list) and [6, 1, 4, 3] (as the second list), the function should return[5, 2].Usefilterandconvertthefilterapplicationtoalist.

A function with_factors() that is passed the running list of integers and a second list of integers (which you prompt for and read before the function call) and returns a list containing the elements in the running list that have at least one element in the second list as a factor. Forexample,iftherunninglistis[2, 3, 4, 5, 6, 7, 8, 9]andthelistoffactorsin [2, 3], then the returned list should be [2, 3, 4, 6, 8, 9]. Within the definition of this function, define another function, has_factor(), which is passed two arguments: an integer, x, and the list fcts of factors passed to the top-level function. Then has_factor(x, fcts) returns True if one of the integers in fcts is a factor of x (and otherwise returns False). You can then use filter to apply a lambda expression that uses has_factor() to the running list (that was passed in to the function). You will have to convert the filter expression to a list to force it to evaluate.

A function cum_sum_pairs() that is passed the running list of integers and returns a list of pairs (2-element tuples), where the first element of a pair is the original number and the second is the sum of all previous numbers in the list (0 for the first list element). Consider using list comprehension.

A function pairs_smaller() that is passed the running list of integers and returns a list of pairs (2-element tuples), where the first element in the pair, call it x, is the original number, and the second is a list of all those numbers in the running list that are less than x.

Recall that you can input an entire list (written with [...]) in one line from the terminal using eval(input(prompt)).

The following is some test code that you may include in your program file. The console interaction is listed below the code.

lst = eval(input("Input a list: ")) print('The smallest and largest elements: ', min_max(*lst)) step = eval(input('The step, 0 for the default: ')) print('The stepped sequence is ', my_slice(lst, step=step)

if step else my_slice(lst)) step = eval(input('The step, 0 for the default: '))

print('The stepped sequence is ', my_slice(lst, step=step) if step else my_slice(lst))

lst1 = eval(input("Input another list: "))

print('The difference of the two lists: ', list_diff(lst, lst1))

fcts = eval(input('Enter a list of factors: '))

print('The elements with at least one of these factors are ', with_factors(lst, fcts))

print('The first list of pairs as described: ', cum_sum_pairs(lst))

print('The second list of pairs as described: ', pairs_smaller(lst))Output >>> runfile('C:/SomeFolder/HW1/prob1.py', wdir='C:/SomeFolder/HW1')

Input a list: [7, 2, 12, 9, 15, 4, 11, 5] The smallest and largest elements: (2, 15)

The step, 0 for the default: 0 The stepped sequence is [7, 12, 15, 11]

The step, 0 for the default: 3 The stepped sequence is [7, 9, 11]

Input another list: [11, 3, 15, 2, 8] The difference of the two lists: [7, 12, 9, 4, 5]

Enter a list of factors: [2, 5]

The elements with at least one of these factors are [2, 12, 15, 4, 5]

The first list of pairs as described: [(7, 0), (2, 7), (12, 9), (9, 21), (15, 30), (4, 45), (11, 49), (5, 60)]

The second list of pairs as described: [(7, [2, 4, 5]), (2, []), (12, [7, 2, 9, 4, 11, 5]), (9, [7, 2, 4, 5]), (15, [7, 2, 12, 9, 4, 11, 5]), (4, [2]), (11, [7, 2, 9, 4, 5]), (5, [2, 4])]

Include docstrings with all you function definitions.

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

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions