paiza Skill check past problem collection "C rank equivalent search history"

(problem) https://paiza.jp/works/mondai/skillcheck_archive/search_history?language_uid=python3

(Video solved in Java) https://paiza.jp/works/skillcheck/primer/skillcheck4

I didn't get an answer to this question, so I'll post the answer code for my own review. The problem statement is as follows.

You cannot see the history of search words in the browser you are using. You found it inconvenient not to be able to see the history of search words, so I decided to create a function to see the history of search words myself.

The history of search words is created as follows.

If the search word W has been previously entered: Delete W in the history. Add W to the beginning of the history. If the search word W has never been entered before: Add W to the beginning of the history.

Since N search words W are given, write a program that displays the history after N search words are given.

If you find it a little difficult to understand from the problem statement alone, you can understand it by looking at the link, as there are concrete examples.

Answer guidelines

The first line of standard input is given an integer N that represents the number of search words. In the second and subsequent lines that follow, the searched words are given one word per line. Create an empty list in advance, look at the words line by line, store the word in the list if it is not in the list, and delete the word in the list if it is in the list. If you add it to the list above, it seems to match the given conditions.


#Get the integer N
n = int(input())
#Empty list
words=[]

#Get words line by line
for i in range(n):
    W = str(input())
#Check if the word obtained by the if statement is included in the list before storing the list.
    if W in words :
        words.remove(W) #Delete words in the list if they are in the list
    words.append(W) #Append because it is added regardless of whether it is in the list()The method is in this position

When displaying the result, there is a condition that the order is newly added to the list line by line, so the answer that matches it is output.


#View results
for i in range(int(len(words))):
    print(words[-i-1])

That's all there is to it.

Recommended Posts

paiza Skill check past problem collection "C rank equivalent search history"
Solve word counts (equivalent to paiza rank C) in Python
Solve Fizz Buzz (equivalent to paiza rank C) in Python
Paiza Skill Check List of Frequently Used D and C Ranks ~ Python ~
[With commentary] Solve Fizz Buzz (equivalent to paiza rank C) in Python