Lists HackerRank python Solution

Consider a list (list = []). You can perform the following commands: insert i e: Insert integer e at position i. print: Print the list. remove e:
Admin
Lists HackerRank python Solution

Consider a list (list = []). You can perform the following commands:

insert i e: Insert integer e at position i.

print: Print the list.

remove e: Delete the first occurrence of integer e.

append e: Insert integer e at the end of the list.

sort: Sort the list.

pop: Pop the last element from the list.

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.

Example

N = 4

append 1

append 2

insert 3 1

print 

append 1:appent 1 to the list ,arr = [1].

append 2:append 2to the list arr=[1,2].

insert 3 1:insert 3 at index 1, arr=[1,3,2].

print:print the array.

Output
[1, 3, 2]

Input Format

The first line contains an integer,n, denoting the number of commands.

Each line i of the n subsequent 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

[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]

Solution pypy3

if __name__ == '__main__':
    N = int(input())
    # Lists in Python - Hacker Rank Solution START
    Output = [];
    for i in range(0,N):
        ip = input().split();
        if ip[0] == "print":
            print(Output)
        elif ip[0] == "insert":
            Output.insert(int(ip[1]),int(ip[2]))
        elif ip[0] == "remove":
            Output.remove(int(ip[1]))
        elif ip[0] == "pop":
            Output.pop();
        elif ip[0] == "append":
            Output.append(int(ip[1]))
        elif ip[0] == "sort":
            Output.sort();
        else:
            Output.reverse();

Explanation

This code is a solution to the "Lists" problem on HackerRank. The problem requires you to perform a set of operations on a list based on the given input.

The if __name__ == '__main__': line is a common way to ensure that the code only runs if the script is run directly (as opposed to being imported as a module).

The code takes the first input N which specifies the number of commands to follow. The Output list is initialized as an empty list [].

Then, for each of the N commands, the input string is split and the first element of the resulting list is used to determine which operation to perform on the list Output.

If the first element is "print", the current state of the Output list is printed.

If the first element is "insert", the second and third elements of the input list are integers which represent the index and value to be inserted into the Output list.

If the first element is "remove", the second element of the input list is the value to be removed from the Output list.

If the first element is "pop", the last element of the Output list is removed.

If the first element is "append", the second element of the input list is an integer to be appended to the end of the Output list.

If the first element is "sort", the Output list is sorted in ascending order.

If the first element is "reverse", the Output list is reversed.

Note that the int() function is used to convert the second and third elements of the input list to integers before performing the corresponding operations on the Output list.

Overall, this code provides an efficient solution to the "Lists" problem on HackerRank by utilizing a conditional statement to determine which operation to perform on the list based on the given input.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.