Menu

(Solved) : Generates List L Random Nonnegative Integers Equal Given Upper Bound Given Length Controll Q27365734 . . .

Generates a list L of random nonnegative integers at most equalto a given upper bound,
of a given length, all controlled by user input.

Outputs four lists:

1.elements_to_keep, consisting of L’s smallest element, L’sthird 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 ofelements_to_keep, preserving
the original order

L_2, consisting of the leftmost occurrences of the members of Lwhich are part of
elements_to_keep, preserving the original order

L_3, consisting of the LONGEST, and in case there are more thanone candidate, the
LEFTMOST LONGEST sequence of CONSECUTIVE members of L that reducedto a set,
is a set of integers without gaps.

import sys
from random import seed, randint

try:
arg_for_seed, upper_bound, length = input(‘Enter three nonnegativeintegers: ‘).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(‘nThe generated list L is:’)
print(‘ ‘, L)

L_1 = []
L_2 = []
L_3 = []
elements_to_keep = []

# Replace this comment with your code

print(‘nThe elements to keep in L_1 and L_2 are:’)
print(‘ ‘, elements_to_keep)
print(‘nHere is L_1:’)
print(‘ ‘, L_1)
print(‘nHere is L_2:’)
print(‘ ‘, L_2)
print(‘nHere is L_3:’)
print(‘ ‘, L_3)

Output

Enter three nonnegative integers: 0 10 5
The generated list L is:
[6, 6, 0, 4, 8]
The elements to keep in L_1 and L_2 are:
[0, 6]
Here is L_1:
[6, 6, 0]
Here is L_2:
[6, 0]
Here is L_3:
[6, 6]

Enter three nonnegative integers: 1 15 6
The generated list L is:
[4, 2, 8, 3, 15, 14]
The elements to keep in L_1 and L_2 are:
[2, 4, 14]
Here is L_1:
[4, 2, 14]
Here is L_2:
[4, 2, 14]
Here is L_3:
[15, 14]

Enter three nonnegative integers: 0 8 10
The generated list L is:
[6, 6, 0, 4, 8, 7, 6, 4, 7, 5]
The elements to keep in L_1 and L_2 are:
[0, 5, 7]
Here is L_1:
[0, 7, 7, 5]
Here is L_2:
[0, 7, 5]
Here is L_3:
[4, 8, 7, 6, 4, 7, 5]

Expert Answer


Answer to Generates List L Random Nonnegative Integers Equal Given Upper Bound Given Length Controll Q27365734 . . .

OR