Question
Generates a list L of random nonnegative integers at most equal to a given upper bound, of a given length, all controlled by user input.
Generates a list L of random nonnegative integers at most equal to a given upper bound, of a given length, all controlled by user input.
Outputs four lists:
-elements_to_keep, consisting of L's smallest element, L's third smallest element, L's fifth smallest element, ... Hint: use sorted(), list slices, and set()
-L_1, consisting of all members of L which are part of elements_to_keep, preserving the original order
import sys from random import seed, randint
try: arg_for_seed, upper_bound, length = input('Enter three nonnegative integers: ').split() except ValueError: print('Incorrect input, giving up.') sys.exit() try: arg_for_seed, upper_bound, length = int(arg_for_seed), int(upper_bound), int(length) if arg_for_seed < 0 or upper_bound < 0 or length < 0: raise ValueError except ValueError: print('Incorrect input, giving up.') sys.exit()
seed(arg_for_seed) L = [randint(0, upper_bound) for _ in range(length)] print(' The generated list L is:') print(' ', L)
L_1 = [] elements_to_keep = []
# Replace this comment with your code
print(' ', elements_to_keep) print(' Here is L_1:') print(' ', L_1)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started