Python Forum
An idiotic problem on Python lists on HackerRank
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An idiotic problem on Python lists on HackerRank
#1
I found the hackerrank.com, where you can solve tasks and use whatever tutorials for this.
There's one problem on Python lists, which seems idiotic and ill-defined:

Quote:Consider a list (list = []). You can perform the following commands:
1. insert i e: Insert integer e at position i.
2. print: Print the list.
3. remove e: Delete the first occurrence of integer e.
4. append e: Insert integer e at the end of the list.
5. sort: Sort the list.
6. pop: Pop the last element from the list.
7. reverse: Reverse the list.

Initialize your list and read in the value of n followed by n lines of commands where each command will be of the 7 types listed above. Iterate through each command in order and perform the corresponding operation on your list.

Input Format

The first line contains an integer, n, denoting the number of commands.
Each line i of the subsequent n lines contains one of the commands described above.

Constraints

The elements added to the list must be integers.

Output Format

For each command of type print, print the list on a new line.

Sample Input 0
12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print

Sample Output 0
Output:
[6, 5, 10] [1, 5, 9, 10] [9, 5, 1]
I wrote the code to match this specific sample input/output0 (with N=12) and it passed.
Upon submitting and running the code on platform's test cases, there was a test case with
N=29, where append and insert were basically repeated a few more times to
get the total 29 number of commands with different integers.
There are no indications on how these integers to populate the list should be chosen.
My non-working solution would be:

if __name__ == '__main__':
    import random, sys
    N = int(input())
    print(N)
    arr = []
    e = random.randint(0, sys.maxsize)
    for i in range(1,N+1):
         random.choice([arr.insert(i, e), print(arr), arr.remove(e), arr.append(e), arr.sort(), arr.pop(), arr.reverse()])
as using or keyword doesn't select any one of the 7 commands (or other objects), but selects only the first one preceding the keyword.

If "everything in Python is an object", why is it not possible to pass the return objects of function calls as arguments for another function (i.e. random.choice or list())?
Is there a solution, using only list operations and some loops, for this idiotic task?
Reply


Messages In This Thread
An idiotic problem on Python lists on HackerRank - by anata2047 - Jun-15-2022, 06:41 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020