python coding test course, I don’t want to be a liar

Problem Description

In our neighborhood, there are N people. Each person has their own nickname, and some of them tell lies to each other. A lie is simply the act of leaving ‘their nickname’ to the other person and breaking a promise. You want to find the nicknames of those who actually lied in this situation.

Information is provided about the N people as follows:

  • Their own nickname
  • The number of lies they told to each other

Input Format

The first line contains N (1 ≤ N ≤ 100,000). From the second line onward, N lines contain each person’s nickname and the number of lies that person told.

Output Format

Print the nicknames of liars one per line. If there are no liars, print the message “No liars found.”

Example

    Input:
    3
    Younghee 1
    Cheolsu 0
    Minsu 2

    Output:
    Younghee
    Minsu
    

Solution

To solve this problem, we need to identify each individual’s nickname and the number of lies they told based on the given input. The process of solving the problem using the provided data structure is as follows:

Step 1: Data Structure Design

To handle each person’s information, we will use a list to store the nicknames of the people and the number of lies they told. In this case, we should use a tuple or dictionary to store each person’s information.

Step 2: Collect Input Data

When receiving input from the user, we first read the number of people N, and then for the next N lines, we read each person’s information. In this process, we separate and store each piece of information.

Step 3: Extract Liars

To extract liars, we need to store the nicknames of all individuals whose number of lies is greater than 0. We will use a conditional statement to check the number of lies for each individual.

Step 4: Output Results

Finally, we will print the extracted list of nicknames. If the list is empty, we will print the message “No liars found.”

Code Implementation

Now, let’s implement the code based on the above logic:

def find_liars(n, people):
    liars = []
    
    for name, lies in people:
        if lies > 0:
            liars.append(name)
    
    if liars:
        return liars
    else:
        return ["No liars found."]

if __name__ == "__main__":
    n = int(input("Enter the number of people: "))
    people = []

    for _ in range(n):
        entry = input("Enter name and number of lies: ").split()
        name = entry[0]
        lies = int(entry[1])
        people.append((name, lies))

    result = find_liars(n, people)
    for liar in result:
        print(liar)

Code Explanation

The code above simply returns the entire process. At each step, it stores the information entered by the people and determines the nicknames of those who lied based on this information.

Function Explanation

  • find_liars(n, people): Accepts the given number of people and their information, returning a list of nicknames of those who lied.
  • if __name__ == "__main__":: The main program execution part, which processes the input received from the user.

Conclusion

Through this problem, we solved a common type of problem in coding tests based on understanding simple data structures and lists. I hope the process of solving this problem helps you in preparing for coding tests.