Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / study / engineering / computer science / computer science questions and a

ID: 3728261 • Letter: H

Question

home / study / engineering / computer science / computer science questions and answers / this is my task 5 ''' generates a list l of random nonnegative integers at most equal to a ... 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.

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

- L_2, consisting of the leftmost occurrences of the members of L which are part of elements_to_keep, preserving the original order

- L_3, consisting of the LONGEST, and in case there are more than one candidate, the LEFTMOST LONGEST sequence of CONSECUTIVE members of L that reduced to 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 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 = []

L_2 = []

L_3 = []

elements_to_keep = []

# Replace this comment with your code

print(' The elements to keep in L_1 and L_2 are:')

print(' ', elements_to_keep) print(' Here is L_1:')

print(' ', L_1)

print(' Here is L_2:')

print(' ', L_2)

print(' Here is L_3:')

print(' ', L_3)

Explanation / Answer

Hi,

Below code covering the most test cases. Since you didn't provide the sample test case. I considered my own.

So test yourself with necessary test cases before submitting this work.

Comments help you in guiding the approach for solution.

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_2 = []

L_1 = []
L_3 = []
SortedL = list(set(sorted(L)))

#Uses list slicing to store odd smallest numbers till the end of sorted list
elements_to_keep = [ SortedL[i] for i in range(0,len(SortedL),2)]

# Iterate each element L[i] of L, and store L[i] in L_1 if it is present in elements_to_keep
for element in L:
if element in elements_to_keep:
L_1.append(element)

# Iterate each element L[i] of L, and store L[i] in L_2 if it is present in elements_to_keep and not in L_2. Preserving only left most occurance of element
for element in L:
if element in elements_to_keep:
if element not in L_2:
L_2.append(element)

# Iterate each element L[i] of L and store the consecutive sequence members. if two sequences have same count only the leftmost sequence is preserved
count = -1
consecutiveSet = set()
tempConsecutiveSet = set()
consecutiveSetList = [consecutiveSet]
tempcount = 0
tempConsecutiveSet.add(L[0])
for index in range(1,len(L)):
if L[index] == L[index-1]:
continue
elif L[index] == L[index-1] + 1:
tempcount = tempcount + 1
tempConsecutiveSet.add(L[index])
else:
if tempcount > count:
count = tempcount
consecutiveSet = tempConsecutiveSet
tempConsecutiveSet = set()
L_3 = consecutiveSet

# Replace this comment with your code

print(' The elements to keep in L_1 and L_2 are:')

print(' ', elements_to_keep)
print(' Here is L_1:')

print(' ', L_1)

print(' Here is L_2:')

print(' ', L_2)

print(' Here is L_3:')

print(' ', L_3)

All the best.