Home  >  Article  >  Backend Development  >  Problema HackerRank em Python - Base Data Types Lists

Problema HackerRank em Python - Base Data Types Lists

WBOY
WBOYOriginal
2024-09-04 14:31:101019browse

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 if 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.

The above is the detailed content of Problema HackerRank em Python - Base Data Types Lists. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn