Buying books

Report a typo

Alice is a book lover, she buys new books regularly. Unfortunately, she doesn't have much time to read them right away, and she always has a stack of unread books. When Alice brings a new book, she puts it on top of the unread ones. Whenever she feels like reading, she takes a book from the very top of her book stack, finishes it in one go, and puts it on the shelf with the books she has already read.

Given a sequence of Alice's actions, print out all books that she reads. Books should be printed on separate lines.

The input is as follows. The number n refers to the total number of actions. BUY book_name denotes buying a book called book_name, while READ refers to reading a book from the top of the stack.

You can assume that initially, Alice doesn't have any unread books.

Use split(" ", 1) to split the input string on the first whitespace only. This way, the book title will not be split.

Sample Input 1:

4
BUY Pride and Prejudice
BUY Anna Karenina
READ
BUY Hamlet

Sample Output 1:

Anna Karenina

Sample Input 2:

8
BUY Pride and Prejudice
BUY Anna Karenina
READ
BUY Hamlet
BUY Pride and Prejudice
BUY Anna Karenina
READ
BUY Hamlet

Sample Output 2:

Anna Karenina
Anna Karenina
Write a program in Python 3





___

Create a free account to access the full topic