# Lists

Received a message from HackerRank to solve the following challenge:  https://www.hackerrank.com/challenges/python-lists?utm_campaign=challenge-recommendation&utm_medium=email&utm_source=24-hour-campaign ranked easy.

After listing some basic operations on lists the requirements for the challenge are listed.

Following is a console screen capture of sample input and output generated by the Spyder IDE:

12 <== number of commands

insert 0 5

insert 1 10

insert 0 6

print

[6, 5, 10] <== output

remove 6

append 9

append 1

sort

print

[1, 5, 9, 10] <== output

pop

reverse

print

[9, 5, 1] <== output

The sample input generated the sample output. Following is my Python code for the challenge:

# **** ****

if __name__ == ‘__main__’:

# **** ****

N = int(input())

#   print(“N:”, N)

# **** ****

list = []

# **** ****

while (N > 0):

# **** read the next line ****

line = input()

#        print(“line:”, line)

# **** split the line ****

args = line.split()

#       print(“args:”, args)

# **** extract and process the command ****

cmd = args[0]

#       print(“cmd:”, cmd)

if (cmd == “insert”):

list.insert(int(args[1]), int(args[2]))

elif (cmd == ‘print’):

print(list)

elif (cmd == ‘remove’):

list.remove(int(args[1]))

elif (cmd == ‘append’):

list.append(int(args[1]))

elif (cmd == ‘sort’):

list.sort()

elif (cmd == ‘pop’):

list.pop()

elif (cmd == ‘reverse’):

list.reverse()

else:

print(‘<<< unknow cmd:’, cmd)

# **** decrement N ****

N -= 1

The code passed the two tests.

If you have comments / questions regarding any entry in this blog please let me know via email. I will not use your name unless you explicitly allow me to do so.

John

john.canessa@gmail.com