List problem solution |PYTHON| HackerRank
OVERVIEW:-
Consider a list (list = []). You can perform the following commands:---->>
print: Print the list.
remove e: Delete the first occurrence of integer .
append e: Insert integer 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 followed by lines of commands
where each command will be of the types listed above. Iterate through
each command in order and perform the corresponding operation on your list.
The first line contains integer ,N , denoting number of commands .
Each line i of the N subsequent lines contains one of the commands decribed above.
Output Format:-
For each command of type print , print the list on a new line.
Constraints:-
The elements added to the list will be an integer.
Sample Input :-
12insert 0 5insert 1 10insert 0 6remove 6append 9append 1sortpopreverse
Sample Output:-
[6, 5, 10][1, 5, 9, 10][9, 5, 1]
Solution Code:-
Note:-- Code is for Python 3
n = int(input())data = list()for x in range(n):command = input().split()name = command[0]if(name == 'insert'):data.insert(int(command[1]),int(command[2]))if(name == 'reverse'):data.reverse()if(name == 'print'):print(data)if(name == 'remove'):data.remove(int(command[1]))if(name == 'append'):data.append(int(command[1]))if(name == 'pop'):data.pop()if(name == 'sort'):data.sort()
Note:-- Code is for Python 2
n = int(raw_input())data = list()for x in range(n):command = raw_input().split()name = command[0]if(name == 'insert'):data.insert(int(command[1]),int(command[2]))if(name == 'reverse'):data.reverse()if(name == 'print'):print(data)if(name == 'remove'):data.remove(int(command[1]))if(name == 'append'):data.append(int(command[1]))if(name == 'pop'):data.pop()if(name == 'sort'):
data.sort()
ABOUT
Contact By:-- --- charlievishwakarma.2503@gmail.com
Created By:------ shubham vishwakarma(Naime.com)
Comments
Post a Comment
Let me Know if having doubts regarding it