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.
PYTHON. Create a function that accepts a filename where in each line there is a name...
PYTHON. Create a function that accepts a filename where in each line there is a name of a type of bird. use a python dictionary in order to count the # of time each bird appears. (1 line = 1 appearance) Loop over your dict. to print each species and the # of times it was seen in the file once all lines have been counted. return dictionary containing count the filename opens this: blackbird canary hummingbird canary hummingbird canary...
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.
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.
Write a program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
Create a java program that has a code file with main() in it and another code...
Create a java program that has a code file with main() in it and another code file with a separate class. You will be creating objects of the class in the running program, just as the chapter example creates objects of the Account class. Your system handles employee records and processes payroll for them. Create a class called Employee that holds the following information: first name, last name, monthly salary, and sales bonus. The class should have all the gets...
: 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
(previous program) Prompt the user for the filename. Create a new file object sensehat_data_file which opens...
(previous program) Prompt the user for the filename. Create a new file object sensehat_data_file which opens the file in write mode. Write a loop which will read in 20 values for temperature and humidity from the SenseHat. Sleep 0.5 seconds between each reading taken from the SenseHat Write out the temperature and humidity to the file sensehat_data_file, separated by a comma. Close the file.) ONLY ANWSER problem below Then Open the file in read only mode. Use a loop to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT