"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Problema HackerRank em Python - Base Data Types Lists

Problema HackerRank em Python - Base Data Types Lists

Published on 2024-11-06
Browse:103

Problema HackerRank em Python - Base Data Types Lists

This Python code is designed to perform a series of operations on a list based on user-supplied commands. Let's analyze the code step by step to understand how it works:

if __name__ == '__main__':
    N = int(input())
    l = []
    while(N>0):
        cmd_l = input().split()
        if(len(cmd_l) == 3 and cmd_l[0] == "insert"):
            #insert statement
            l.insert(int(cmd_l[1]),int(cmd_l[2]))
        elif(len(cmd_l) == 2 and (cmd_l[0] == "remove" or cmd_l[0] == "append")):
            if(cmd_l[0] == "remove"):
                l.remove(int(cmd_l[1]))
            elif(cmd_l[0] == "append"):
                l.append(int(cmd_l[1]))
        elif(len(cmd_l) == 1):
            if(cmd_l[0] == "sort"):
                l.sort()
            elif(cmd_l[0] == "reverse"):
                l.reverse()
            elif(cmd_l[0] == "pop"):
                l.pop()                
            elif(cmd_l[0] == "print"):
                print(l)
        N -= 1

if __name__ == '__main__':

  • This line checks if the script is being executed directly. Practice adopted to ensure that the code within this block will only be executed if the file is the program's entry point.

N = int(input())

  • The program expects the user to enter an integer that is stored in the variable N. This number represents how many operations the user will perform.

l = []

  • Empty list used to store elements as operations are performed.

while(N>0):

  • A while loop is started that will continue running as long as N is greater than 0. This means that the loop will execute N times, once for each operation the user wants to perform.

cmd_l = input().split()

  • Within the loop the program waits for the user to enter a line of text, which is divided into a list of strings (cmd_l) using the split() method. Each element of the cmd_l list represents a part of the operation to be performed.

if(len(cmd_l) == 3 and cmd_l[0] == "insert"):

  • This line checks if the operation is a three-part "insert" command (cmd_l must be length 3 and the first element must be "insert").

l.insert(int(cmd_l[1]),int(cmd_l[2]))

  • If the above condition is true, the insert method of list l is called. The arguments are converted from string to integer: cmd_l[1] is the position where the element will be inserted, and cmd_l[2] is the element to be inserted.

elif(len(cmd_l) == 2 and (cmd_l[0] == "remove" or cmd_l[0] == "append")):

  • This line checks whether the operation is a two-part "remove" or "append" command cmd_l must have length 2 and the first element must be "remove" or "append".

if(cmd_l[0] == "remove"):
l.remove(int(cmd_l[1]))
elif(cmd_l[0] == "append"):
l.append(int(cmd_l[1]))

  • Depending on the command (remove or append), the corresponding method from list l is called. For remove, the cmd_l element (converted to integer) is removed from the list. For append, the element cmd_l[[1](converted to integer) is added to the end of the list.

elif(len(cmd_l) == 1):

  • Checks if the operation is a single-part command (cmd_l must have length 1).

if(cmd_l[0] == "sort"):
l.sort()
elif(cmd_l[0] == "reverse"):
l.reverse()
elif(cmd_l[0] == "pop"):
l.pop()
elif(cmd_l[0] == "print"):
print(l)

  • Depending on the command (sort, reverse, pop, or print), the corresponding method from list l is called. sort sorts the list, reverse reverses the order of the elements, pop removes the last element, and print prints the list.

N -= 1

  • At the end of the loop, N is decremented by 1, indicating that an operation has been performed. This continues until N is 0, when the loop ends.
Release Statement This article is reproduced at: https://dev.to/gusmedeirost/problema-hackerrank-em-python-base-data-types-lists-5h91?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3