datam
datam

Reputation: 285

Hackerrank Lists Problem ~ Standard test case works but others don't

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

insert i e: Insert integer e at position .
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 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.

Sample Input:

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

My Code:

import sys

if __name__ == '__main__':
    N = int(input())

my_list = []
inputs  = []

for line in sys.stdin:
    inputs.append(line)

for item in inputs:
    if item[0:5] == 'print':
        print(my_list)
    elif item[0:2] == 'in':
        inserts = [s for s in item.split()][1:3]
        inserts = list(map(int, inserts))
        my_list.insert(inserts[0], inserts[1])
    elif item[0:3] == 'rem':
        inserts = list(map(int, [s for s in item.split()][1]))
        my_list.remove(inserts[0])
    elif item[0:2] == 'ap':
        inserts = list(map(int, [s for s in item.split()][1]))
        my_list.append(inserts[0])
    elif item[0:4] == 'sort':
        my_list.sort()
    elif item[0:3] == 'pop':
        my_list.pop()
    elif item[0:7] == 'reverse':
        my_list.reverse()

I'm not sure as to why my code is not getting approved upon submission. In this test case they provided, my code passes. The expected output is the following:

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

Thanks so much for the help!

Upvotes: 2

Views: 60534

Answers (12)

Md. Shahariar Hossen
Md. Shahariar Hossen

Reputation: 1715

### You can try this one:
if __name__ == '__main__':
    n = int(input())
    num_list = []
    for _ in range(n):
        string = input().split()
        command = string[0]
        args = string[1:]
        if command != "print":
            command += "(" + ",".join(args) + ")"
            eval("num_list." + command)
        else:
            print(num_list)

Upvotes: 0

Ravi Rajput
Ravi Rajput

Reputation: 1

x= int(input())
lst = []
for i in range(x):
    n = input()
    lst.append(n)
newlist=[]
for ele in lst:
    splitted = ele.split()
    if splitted[0] == "insert":
        newlist.insert(int(splitted[1]),splitted[2])

    elif splitted[0] == "print":
        print(newlist)

    elif splitted[0] == "remove":
        newlist.remove(splitted[1])

    elif splitted[0] == "append":
        newlist.append(splitted[1])

    elif splitted[0] == "sort":
        newlist.sort()

    elif splitted[0] == "pop":
        newlist.pop()

    elif splitted[0] == "reverse":
        newlist.reverse()

    else:
        print("Command out of Scope")

Upvotes: 0

Sankalpa
Sankalpa

Reputation: 1

if __name__ == '__main__':
    N = int(input())
    l=[]

    for i in range(N):
        s=input().split("\n")
        for j in s:
            m=j.split()
            if m[0]=="insert":
                l.insert(int(m[1]),int(m[2]))
            elif m[0]=="print":
                print(l)
            elif m[0]=="remove":
                l.remove(int(m[1]))
            elif m[0]=="append":
                l.append(int(m[1]))
            elif m[0]=="sort":
                l.sort()
            elif m[0]=="pop":
                l.pop()
            elif m[0]=="reverse":
                l.reverse()       

Upvotes: 0

EscVM
EscVM

Reputation: 11

N = int(input())
lst = []
for i in range(N):
    command = input().split()
    if command[0] == "print":
        print(lst)
    elif len(command) == 1:
        getattr(lst, command[0])()
    elif len(command) == 2:
        getattr(lst, command[0])(int(command[1]))
    else:
        getattr(lst, command[0])(int(command[1]), int(command[2]))

Upvotes: 1

Erik Rosales
Erik Rosales

Reputation: 21

Here is a solution with no "if" or "elif" statements:

if __name__ == '__main__':
    N = int(input())
    commands = {
        "insert": lambda x, y, z: x.insert(y, z),
        "print": lambda x: print(x),
        "remove": lambda x, y: x.remove(y),
        "append": lambda x, y: x.append(y),
        "sort": lambda x: x.sort(),
        "pop": lambda x: x.pop(),
        "reverse": lambda x: x.reverse(),
    }
    out = []
    for i in range(N):        
        a = input()
        split_a = a.split(' ')
        command = split_a[0]
        try:
            commands[command](out, int(split_a[1]), int(split_a[2]))
        except IndexError:
            try:
                commands[command](out, int(split_a[1]))
            except IndexError:
                commands[command](out)

Upvotes: 2

kaushal kukreja
kaushal kukreja

Reputation: 11

*if __name__ == '__main__':
    N = int(input())
    the_list = list()
    
    for cmd in range(N):
        input_cmd = input().split()
        if input_cmd[0] == 'print':
            print(the_list)
        
        elif input_cmd[0] == 'insert':
            the_list.insert(int(input_cmd[1]), int(input_cmd[2]))
        
        elif input_cmd[0] == 'remove':
            the_list.remove(int(input_cmd[1]))
        
        elif input_cmd[0] == 'append':
            the_list.append(int(input_cmd[1]))
        
        elif input_cmd[0] == 'sort':
            the_list = sorted(the_list)
            
        elif input_cmd[0] == 'pop':
            the_list.pop()
        
        elif input_cmd[0] == 'reverse':
            the_list.reverse()*
        

Upvotes: 0

9 Rules
9 Rules

Reputation: 19

def operation(last_value1,operation_name,*number):
    list_number = last_value1
    if operation_name.lower() == 'insert':
        list_number.insert(int(number[0]),int(number[1]))
    elif operation_name.lower() == 'remove':
        list_number.remove(int(number[0]))
    elif operation_name.lower() == 'append':
        list_number.append(int(number[0]))
    elif operation_name.lower() == 'sort':
        list_number = sorted(list_number)
    elif operation_name.lower() == 'print':
        print(list_number)
    elif operation_name.lower() == 'pop':
        list_number.pop()
    elif operation_name.lower() == 'reverse':
        list_number= list_number[::-1]
    return list_number

N = int(input())
last_value = []
for count in range(0,N):
        command_input = input("Please enter the command with number seperated by space")
        command_input = command_input.split()
        if len(command_input) == 3:
            last_value = operation(last_value,command_input[0],command_input[1],command_input[2])
        elif len(command_input) == 2:
            last_value= operation(last_value,command_input[0],command_input[1])
        else:
            last_value = operation(last_value,command_input[0])

Upvotes: 0

Tejeshwar Reddy
Tejeshwar Reddy

Reputation: 19

if __name__ == '__main__':
N = int(input())
reqarr = []
for i in range(0,N):
    inplist = (input().split())
    if(inplist[0] == "insert" ):
        reqarr.insert(int(inplist[1]),int(inplist[2]))
    elif(inplist[0] == "print"):
        print(reqarr)
    elif(inplist[0] == "remove"):
        reqarr.remove(int(inplist[1]))
    elif(inplist[0] == "append"):
        reqarr.append(int(inplist[1])) 
    elif(inplist[0]=="sort"):
        reqarr.sort()   
    elif(inplist[0]=="reverse"):
        reqarr.reverse() 
    elif(inplist[0] == "pop"):
        reqarr.pop()              
                
    

Upvotes: 0

Bickky Sahani
Bickky Sahani

Reputation: 363

def execute(lst, cmd, *args):
    if cmd == 'insert':
        lst.insert(int(args[0]), int(args[1]))
    elif cmd == 'print':
        print(lst)
    elif cmd == 'remove':
        lst.remove(int(args[0]))
    elif cmd == 'append':
        lst.append(int(args[0]))
    elif cmd == 'sort':
        lst.sort()
    elif cmd == 'reverse':
        lst.reverse()
    elif cmd == 'pop':
        lst.pop()
   else: 
        print("Command not recognized!")

lst = []
for _ in range(int(input())):
execute(lst, *input().split())

Upvotes: 1

Wild_Hunter_
Wild_Hunter_

Reputation: 41

if __name__ == '__main__':
    N = int(input())
    m=list()
    for i in range(N):
       method,*l=input().split()
       k=list(map(int,l))
       if len(k)==2:
          q=[k[0]]
          w=[k[1]]
       elif len(k)==1:
          q=[k[0]]
       if method =='insert':
          m.insert(q[0],w[0])
       elif method == 'append':
          m.append(q[0])
       elif  method == 'remove':
          m.remove(q[0])
       elif method =='print':
          print(m)
       elif method == 'reverse':
          m.reverse()
       elif method =='pop':
          m.pop()
       elif method == 'sort':
          m.sort()

Upvotes: 3

hevalhazalkurt
hevalhazalkurt

Reputation: 11

The string slicing is not a good idea. Also, you need to get user input N times. So you can edit your solution like below :

if __name__ == '__main__':
    N = int(input())
    the_list = list()

    for _ in range(N):
        query = input().split()
        if query[0] == "print":
            print(the_list)
        elif query[0] == "insert":
            the_list.insert(int(query[1]), int(query[2]))
        elif query[0] == "remove":
            the_list.remove(int(query[1]))
        elif query[0] == "append":
            the_list.append(int(query[1]))
        elif query[0] == "sort":
            the_list = sorted(the_list)
        elif query[0] == "pop":
            the_list.pop()
        elif query[0] == "reverse":
            the_list.reverse()

Upvotes: 0

Tané Tachyon
Tané Tachyon

Reputation: 1112

Your problem is that your append code has a bug when the number to be appended has more than one digit. In your code here

inserts = list(map(int, [s for s in item.split()][1]))
my_list.append(inserts[0])

for example if the "item" command was "append 12", [s for s in item.split()][1] would be the string "12", so list(map(int, [s for s in item.split()][1])) is mapping each character in that string to an integer, giving you [1, 2] rather than [12], and therefore my_list.append(inserts[0]) would be appending the number 1 instead of 12. So fix that and you will fix your problem.

Upvotes: 2

Related Questions