Consider a list (
list = []
). You can perform the following commands:insert i e
: Insert integer at position .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.
Input Format
The first line contains an integer, , denoting the number of commands.
Each line of the subsequent lines contains one of the commands described above.
Each line of the 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]
Lists - Hacker Rank Solution
We can solve this using list methods and conditionals.
Problem Tester's code:
arr = []
for i in range(int(raw_input())):
s = raw_input().split()
for i in range(1,len(s)):
s[i] = int(s[i])
if s[0] == "append":
arr.append(s[1])
elif s[0] == "extend":
arr.extend(s[1:])
elif s[0] == "insert":
arr.insert(s[1],s[2])
elif s[0] == "remove":
arr.remove(s[1])
elif s[0] == "pop":
arr.pop()
elif s[0] == "index":
print arr.index(s[1])
elif s[0] == "count":
print arr.count(s[1])
elif s[0] == "sort":
arr.sort()
elif s[0] == "reverse":
arr.reverse()
elif s[0] == "print":
print arr
def main():
ReplyDeleteif command[0]=='insert':
L.insert(int(command[1]),int(command[2]))
elif command[0]=='print':
print(L)
elif command[0]=='append':
L.append(int(command[1]))
elif command[0]=='remove':
L.remove(int(command[1]))
elif command[0]=='sort':
L.sort()
elif command[0]=='reverse':
L.reverse()
elif command[0]=='pop':
L.pop()
if __name__ == '__main__':
L=[]
N = int(input())
for i in range(N):
command=str(input())
command=command.split(' ')
main()
Bro, what is the meaning of command[0] == append
Deletebro, its a string that we get as an input. Command will be a list of any of the above commands. So we have to check the 0th string position and then do the corresponding operation.
Delete
Deletedef lst_op():
if command[0] == 'insert':
L.insert(int(command[1]), int(command[2]))
elif command[0] == 'print':
print(L)
elif command[0] == 'append':
L.append(int(command[1]))
elif command[0] == 'remove':
L.remove(int(command[1]))
elif command[0] == 'sort':
L.sort()
elif command[0] == 'reverse':
L.reverse()
elif command[0] == 'pop':
L.pop()
L=[]
N = int(input())
while N:
command=str(input())
command=command.split(' ')
lst_op()
N=N-1
lis = []
ReplyDeletefor i in range(int(input())):
cmd = input().split()
if(len(cmd) == 1):
if cmd[0] == "print":
print(lis)
else:
cmdE = "lis." + cmd[0] + "()"
eval(cmdE)
elif(len(cmd) == 2):
cmdE = "lis." + cmd[0] + "("+ cmd[1] +")"
eval(cmdE)
elif(len(cmd) == 3):
cmdE = "lis." + cmd[0] + "("+ cmd[1] +"," + str(cmd[2]) + ")"
eval(cmdE)
1st of all program returns with an error near arr.index. Because of there is no parenthesis near print function. After fixing all the parenthesis, it shows "name raw_input is not defined. Kindly fix it.
ReplyDeleteeaw_input is used to take input when you are using python2. But if you are using python3 use input() to ask for input.
Delete* raw_input
DeleteHey blogger, how much you are earn by this blog???
Deleteif __name__ == '__main__':
ReplyDeletearr = []
for i in range(int(input())):
s = input().split()
for i in range(1,len(s)):
s[i] = int(s[i])
if s[0] == "append":
arr.append(s[1])
elif s[0] == "extend":
arr.extend(s[1:])
elif s[0] == "insert":
arr.insert(s[1],s[2])
elif s[0] == "remove":
arr.remove(s[1])
elif s[0] == "pop":
arr.pop()
elif s[0] == "index":
print(arr.index(s[1]))
elif s[0] == "count":
print(arr.count(s[1]))
elif s[0] == "sort":
arr.sort()
elif s[0] == "reverse":
arr.reverse()
elif s[0] == "print":
print(arr)
##### Code for pyhon3 ######
What is the meaning of "if s[0]=append
Deletes[0] means store the location in 0 positioin
Deletewhat is the function of line5,6
Deleteplease solve program in arrar
ReplyDeleteb=int(input())
ReplyDeletelist=[]
for i in range(12):
a = [str(k) for k in input().split()]
a[i].lower()
if a[0]=="print":
print(list)
elif a[0]=='remove':
list.remove(int(a[1]))
elif a[0]=='append':
list.append(int(a[1]))
elif a[0]=='sort':
list.sort()
elif a[0]=='pop':
list.pop()
elif a[0]=='reverse':
list.reverse()
elif a[0]=="insert":
list.insert(int(a[1]),int(a[2]))
else:
pass
why iam getting empty list outp for this program
Delete
ReplyDeletearr = []
for i in range(int(input())):
s = input().split()
for i in range(1,len(s)):
s[i] = int(s[i])
if s[0] == "insert":
arr.insert(s[1],s[2])
elif s[0] == "append":
arr.append(s[1])
elif s[0] == "extend":
arr.extend(s[1:])
elif s[0] == "remove":
arr.remove(s[1])
elif s[0] == "sort":
arr.sort()
elif s[0] == "pop":
arr.pop()
elif s[0] == "reverse":
arr.reverse()
elif s[0] == "print":
print (arr)
iam getting empty list output
DeleteN = int(input())
ReplyDeletel=[]
for i in range(0,N):
j=input().split()
if len(j)>2:
if j[0]=='insert':
l.insert(int(j[1]),int(j[2]))
elif len(j)<2:
if j[0]=='sort':
l.sort()
elif j[0]=='print':
print(l)
elif j[0]=='pop':
l.pop()
elif j[0]=='reverse':
l.reverse()
elif j[0]=='remove':
l.remove(int(j[1]))
elif j[0]=='append':
l.append(int(j[1]))
lis = []
ReplyDeletefor i in range(N):
co = input().split()
if co[0] == 'insert':
lis.insert(int(co[1]), int(co[2]))
elif co[0] == 'print':
print(lis)
elif co[0] == 'remove':
lis.remove(int(co[1]))
elif co[0] == 'append':
lis.append(int(co[1]))
elif co[0] == 'sort':
lis.sort()
elif co[0] == 'pop':
lis.pop()
elif co[0] == 'reverse':
lis.reverse()
N = int(input())
ReplyDeletearr = []
for i in range(N):
s = input().split()
for i in range(1,len(s)):
s[i] = int(s[i])
if s[0] == "append":
arr.append(s[1])
elif s[0] == "extend":
arr.extend(s[1:])
elif s[0] == "insert":
arr.insert(s[1],s[2])
elif s[0] == "remove":
arr.remove(s[1])
elif s[0] == "pop":
arr.pop()
elif s[0] == "index":
print (arr.index(s[1]))
elif s[0] == "count":
print (arr.count(s[1]))
elif s[0] == "sort":
arr.sort()
elif s[0] == "reverse":
arr.reverse()
elif s[0] == "print":
print (arr)
if __name__ == '__main__':
ReplyDeleteN = int(input())
dict_obj = []
list_opr = []
for i in range(N):
command=str(input())
command=command.split(' ')
dict_obj.append([command[0],command[1:]])
for i in range(N):
if dict_obj[i][0] == 'insert':
list_opr.insert(int(dict_obj[i][1][0]),int(dict_obj[i][1][1]))
elif dict_obj[i][0] == 'print':
print(list_opr)
elif dict_obj[i][0] == 'remove':
list_opr.remove(int(dict_obj[i][1][0]))
elif dict_obj[i][0] == 'append':
list_opr.append(int(dict_obj[i][1][0]))
elif dict_obj[i][0] == 'sort':
list_opr = sorted(list_opr)
elif dict_obj[i][0] == 'pop':
list_opr.pop()
elif dict_obj[i][0] == 'reverse':
list_opr.reverse()
if __name__ == '__main__':
ReplyDeleteN = int(raw_input())
arr = []
arr.insert(0, 5)
arr.insert(1, 10)
arr.insert(0, 6)
print(arr)
arr.remove(6)
arr.append(9)
arr.append(1)
arr.sort()
print(arr)
arr.pop()
arr.reverse()
print(arr)
n = int(input())
ReplyDeletea = []
a.insert (0,5)
a.insert (1,10)
a.insert (0,6)
print(a)
a.remove(6)
a.append(9)
a.append(1)
a.sort()
print(a)
a.pop()
a.reverse()
print(a)
N = int(input())
ReplyDeletel=[]
for i in range(0,N):
x=input()
if(x=='insert'):
l.insert(int(input()),int(input()))
elif(x=='print'):
print(l)
elif(x=='remove'):
l.remove(int(input()))
elif(x=='append'):
l.append(int(input()))
elif(x=='sort'):
l.sort()
elif(x=='pop'):
l.pop()
elif(x=='reverse'):
l.reverse()
if __name__ == '__main__':
ReplyDeleteN = int(raw_input())
command=[]
array=[]
for i in range(N):
s = raw_input().split()
command.append(s)
for i in range(len(command)):
if(command[i][0]=="insert"):
array.insert(int(command[i][1]),int(command[i][2]))
elif(command[i][0]=="print"):
print(array)
elif(command[i][0]=="remove"):
array.remove(int(command[i][1]))
elif(command[i][0]=="append"):
array.append(int(command[i][1]))
elif(command[i][0]=="pop"):
array.pop()
elif(command[i][0]=="sort"):
array.sort()
elif(command[i][0]=="reverse"):
array.reverse()
if __name__ == '__main__':
ReplyDeleteN = int(raw_input())
my_list = []
inputs = []
insert = []
import sys
for line in sys.stdin:
inputs.append(line)
for cmd in inputs:
if "pop" in cmd:
my_list.pop()
elif "reverse" in cmd:
my_list.reverse()
elif "print" in cmd:
print(my_list)
elif "sort" in cmd:
my_list.sort()
elif "append" in cmd:
insert = [s for s in cmd.split()][1:]
insert = list(map(int,insert))
my_list.append(insert[0])
elif "remove" in cmd:
insert = [s for s in cmd.split()][1:]
insert = list(map(int,insert))
my_list.remove(insert[0])
elif "insert" in cmd:
insert = [s for s in cmd.split()][1:]
insert = list(map(int,insert))
my_list.insert(insert[0],insert[1])