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

This Is My Task 5 \'\'\' Generates a list L of random nonnegative integers at mo

ID: 3725666 • Letter: T

Question

This Is My Task 5

'''

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

Python 3.6 code:

'''

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

def left_longest_seq(values):
longest_seq = []
curr_seq = []
for i in range(1, len(values)):
if values[i - 1] == values[i] + 1:

curr_seq = curr_seq + [values[i - 1]]
else :
if len(longest_seq) < len(curr_seq) :
longest_seq = curr_seq
curr_seq = []

return longest_seq

def get_unique(values):
output = []
seen = set()
for value in values:
if value not in seen:
output.append(value)
seen.add(value)
return output


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

sorted_L = sorted(L)

elements_to_keep = [X for X in sorted_L if sorted_L.index(X) % 2 == 0] ## even index values from the sorted list

L_1 = [X for X in L if X in elements_to_keep] ## Filter all element of L which are in elements_to_leep

L_2 = get_unique(L_1) ## Set removes the duplicates

L_3 = left_longest_seq(L)

# 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)

Output shown after running this program :

python3.6 main.py
Enter three nonnegative integers: 10 10 10

The generated list L is:
[9, 0, 6, 7, 9, 0, 3, 7, 7, 4]

The elements to keep in L_1 and L_2 are:
[0, 0, 3, 6, 9, 9]

Here is L_1:
[9, 0, 6, 9, 0, 3]

Here is L_2:
[9, 0, 6, 3]

Here is L_3:
[]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote