Question

In: Computer Science

Create a new Python program (you choose the filename) that contains a main function and another...

Create a new Python program (you choose the filename) that contains a main function and another function named change_list. Refer to the SAMPLE OUTPUT after reading the following requirements.

In the main function:

  • create an empty list.
  • use a for loop to add 12 random integers, all ranging from 50 to 100, to the list.
  • use second for loop to iterate over the list and display all elements on one line separated by a single space.
  • display the 4th element, the element at index 9, and the smallest element.
  • call the change_list function with your list as its sole argument.

In the change_list function:

  • use a slice to make a new list from the list that was passed into this function. The new list should hold the middle 6 elements of the list that was passed in. NOTE: you must accomplish this step with a slice.
  • use a function to determine the size of the new list. Print the size.
  • sort the new list in ascending order and return the sorted list to main.

Back in the main function:

  • assign the list returned by change_list to a new list.
  • use another for loop to iterate over this returned list and display all elements on one line, separated by a single space.

SAMPLE OUTPUT
Here is the list of random integers...
99 66 57 81 75 59 58 74 56 90 75 65
The 4th element in the list is 81
The element at index 9 is 90
The smallest element in the list is 56
The size of the list is now 6
change_list returned this sorted list...
56 58 59 74 75 81

Solutions

Expert Solution

'''

Python version : 2.7

Python program to create a main function and another function named change_list.

'''

import random

'''

function change_list that takes as input a list and returns another list consisting of middle

6 elements and sorts them i ascending order and retruns it

'''

def change_list(in_list):

              

               new_list = []

               # check if length of input is list <=6, then copy the entire list to new_list

               if len(in_list) <= 6:

                              new_list = in_list

               else: # copy the middle 6 elements from in_list to new_list

                              idx = (len(in_list)-6)//2

                              new_list = in_list[idx:idx+6]

               # print the size of the list

               print('The size of the list is now '+str(len(new_list)))

               # sort the new_list

               new_list.sort()

              

               # return the new_list

               return new_list

# main function

def main():

               # create an empty list     

               in_list = []

               # randomly add 12 elements between [50,100]

               for i in range(12):

                              in_list.append(random.randint(50,100))

               # print the list of elements           

               print('Here is the list of random integers...')

               for i in range(len(in_list)):

                              print(str(in_list[i])+ ' '),

               # print the 4th element  

               print('\nThe 4th element in the list is '+str(in_list[3]))

               # print the element at index 9

               print('The element at index 9 is '+str(in_list[9]))

               # print the smallest element of the list using min function

               print('The smallest element in the list is '+str(min(in_list)))

              

               # get the new_list returned by calling change_list function

               new_list = change_list(in_list)

              

               # display the new_list

               print('change_list returned this sorted list...')

               for i in range(len(new_list)):

                              print(str(new_list[i])+ ' '),

#call teh main function                 

main()   

                             

#end of program                            

Code Screenshot:

Output:


Related Solutions

Create and submit a Python program (in a module) that contains a main function that prints...
Create and submit a Python program (in a module) that contains a main function that prints 17 lines of the following text containing your name: Welcome to third week of classes at College, <your name here>! can someone please show me how to do this step by step? I'm just confused and keep getting errors in idle.
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the test average to letter # grades using map and lambda. File has student_name, test1_score, test2_score, # test3_score, test4_score, test5_score. This function must use a lambda # function and map() function. # The input to the map function should be # a list of lines. Ex. ['student1,73,74,75,76,75', ...]. Output is a list of strings in the format # studentname: Letter Grade -- 'student1: C' #...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
Use python. redact_file: This function takes a string filename. It writes a new file that has...
Use python. redact_file: This function takes a string filename. It writes a new file that has the same contents as the argument, except that all of the phone numbers are redacted. Assume that the filename has only one period in it. The new filename is the same as the original with '_redacted' added before the period. For instance, if the input filename were 'myfile.txt', the output filename would be 'myfile_redacted.txt'. Make sure you close your output file.
: Create a Java program that will accept a regular expression and a filename for a...
: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format : The following operators need to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation, no character spans – the...
create a Python script that prompts the user for a title, description, and filename of your...
create a Python script that prompts the user for a title, description, and filename of your Python program and add the following to the bottom of the existing homepage: Add the post title with an emphasis Add the post description beneath the post title Create a hyperlink to the Python file using the filename input Create another hyperlink to the page you will create in the Web Showcase assignment
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT