Saturday, February 23, 2019

Lists - Hacker Rank Solution

Consider a list (list = []). You can perform the following commands:
  1. insert i e: Insert integer  at position .
  2. print: Print the list.
  3. remove e: Delete the first occurrence of integer .
  4. append e: Insert integer  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  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.
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

27 comments:

  1. def main():
    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()


    if __name__ == '__main__':
    L=[]
    N = int(input())
    for i in range(N):
    command=str(input())
    command=command.split(' ')
    main()

    ReplyDelete
    Replies
    1. Bro, what is the meaning of command[0] == append

      Delete
    2. bro, 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

    3. def 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

      Delete
  2. lis = []
    for 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)

    ReplyDelete
  3. 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.

    ReplyDelete
    Replies
    1. eaw_input is used to take input when you are using python2. But if you are using python3 use input() to ask for input.

      Delete
    2. Hey blogger, how much you are earn by this blog???

      Delete
  4. if __name__ == '__main__':
    arr = []
    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 ######

    ReplyDelete
  5. b=int(input())
    list=[]
    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




    ReplyDelete
    Replies
    1. why iam getting empty list outp for this program

      Delete


  6. arr = []
    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)


    ReplyDelete
  7. N = int(input())
    l=[]


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

    ReplyDelete
  8. lis = []

    for 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()

    ReplyDelete
  9. N = int(input())
    arr = []
    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)

    ReplyDelete
  10. if __name__ == '__main__':
    N = 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()

    ReplyDelete
  11. if __name__ == '__main__':
    N = 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)

    ReplyDelete
  12. n = int(input())
    a = []
    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)

    ReplyDelete
  13. N = int(input())

    l=[]


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

    ReplyDelete
  14. if __name__ == '__main__':
    N = 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()

    ReplyDelete
  15. if __name__ == '__main__':
    N = 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])

    ReplyDelete

Powered by Blogger.