Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

a. Create a user-defined function that accepts no arguments and returns a list. Create an empty list. Prompt for and read in the number of

a. Create a user-defined function that accepts no arguments and returns a list. Create an empty list. Prompt for and read in the number of integer values in the list. Use a loop and the range() function to iterate through the number of values in the list. Inside the loop: o Prompt for and read in each integral value and then append the item to the end of the list. Return the list. b. Create another user-defined function that accepts one argument, the list of integers, and returns the second highest number in a list of integers without modifying the original list.

There are several ways to do this. The first is the more complicated way of manually stepping through each element in the list, keeping track of the both the highest and second highest integers in the list. However, we want to take advantage of some Python 3 capabilities and let Python do the work! Other ways would be to sort the list or remove the largest integer, but since lists are mutable, this modifies the list, which is not acceptable. Lets explore a couple different ways to copy a list. Make a copy of the list using the assignment operator =, such as new_list = old_list. Use the list sort() method to sort the list. Remember the difference between a method and a function a method is associated with an object and accessed using the dot operator. Print the original list (i.e., the list that was passed to the function to see if it was updated by the sort() method. Return the second largest element in the now sorted copy of the list. Remember that you can access the largest item at the end of the list using -1. Before we go on to the next part of this lab component, was the original list modified when you sorted your new list? What happened is that the assignment operator = made a shallow copy of the original list, meaning that the objects were the same so that if one was modified, so was the other. Instead, we want a deep copy that can be accomplished using the copy() method that creates a separate instance of the list. Now, instead of making a copy using the assignment operator =, use the copy() method to make a deep copy of the original list. c. Create another user-defined function that accepts one argument, the list of integers, and modifies the list so that all the integers in the list are zero or positive (i.e., not negative) without returning any item. Loop through the list, but instead since we want to access and modify individual elements in the list, we should iterate from 0 to the length of the list (using the range() function). Inside the loop: o If the element in the list is less than 0, modify that element in the list to make it positive using the abs() function in the math module. o Note that there is no return statement in this function. d. Once the three functions are defined, you will call your function to create your list, without passing any arguments, assigning the result to a list. e. Print your list. f. In a print() statement, call your function that returns the second highest integer, passing in your list as an argument, with an appropriate message.

g. Call your function to make all elements in the list zero or positive, passing in your list as an argument. h. Finally, print your now positive list. For example, the output might look like this (input shown in bold): Enter number of integers in list: 5 Enter item #1: 4 Enter item #2: -3 Enter item #3: 8 Enter item #4: 7 Enter item #5: -1 My original list: [4, -3, 8, 7, -1] [4, -3, 8, 7, -1] Second largest integer in list is 7 My now positive list is [4, 3, 8, 7, 1]

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

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions