T
T
TechnoSas2020-06-04 09:20:19
Python
TechnoSas, 2020-06-04 09:20:19

What is the error in the code for the task List of books for the summer?

This summer, the local library suddenly ran into a flood of schoolchildren wanting to borrow some books from the bibliography. In order not to burden their employees, the library management decided to create an electronic book accounting system that would show which books are available. The library was audited and recorded in the system all the books. When another student tries to borrow a book, the librarian first checks to see if it is available. If there is, the book is temporarily removed from the system. The student has one month to return the book. You have been assigned to write this system and test it on a set of simulated records. We will assume that if the student has not returned the book within a month, the collectors hired by the head of the library, on the day of the expiration of the term, independently withdraw the book.

Input format: first, a list of books separated by commas is passed to the program, then strings describing the behavior of visitors: a string like "Take <book> <date> (<name>)" or "Return <book> <date>". Input occurs up to a point. Please note that the library management decided to make things more difficult for you, and the records are not transferred in chronological order.

Output format: after the visitor took the book, you need to print "The book <book> took (a) <name>". If this book does not exist, print "The book <book> is missing. It was taken by <name>".

Note: if a student took a book on the k-th day of the n-th month, then it is guaranteed that the k-th day of the (n + 1)-th month is a valid date.

sample input:

"Эдем", "Солярис", "Война и мир", "Честь имею", "Ночной дозор", "Оно"
Взять "Честь имею" 03.08.2019 (Карл)
Взять "Оно" 23.07.2019 (Джейн)
Взять "Война и мир" 28.09.2019 (Наташа)
Взять "Война и мир" 01.10.2019 (Сергей)
Взять "Ночной дозор" 27.07.2019 (Дмитрий)
Взять "Солярис" 02.08.2019 (Джейн)
Вернуть "Честь имею" 10.08.2019
Взять "Солярис" 01.08.2019 (Саймон)
Взять "Ночной дозор" 28.08.2019 (Сергей)
Взять "Оно" 21.07.2019 (Саймон)
Вернуть "Солярис" 29.07.2019
Взять "Эдем" 29.09.2019 (Наташа)
Вернуть "Война и мир" 30.09.2019
Взять "Эдем" 02.10.2019 (Саймон)
Вернуть "Ночной дозор" 26.08.2019
Взять "Солярис" 19.07.2019 (Карл)
Взять "Оно" 22.08.2019 (Джейн)

.
sample output:

Книгу "Солярис" забрал(а) Карл
Книгу "Оно" забрал(а) Саймон
Книга "Оно" отсутствует. Ее забрал(а) Саймон
Книгу "Ночной дозор" забрал(а) Дмитрий
Книгу "Солярис" забрал(а) Саймон
Книга "Солярис" отсутствует. Ее забрал(а) Саймон
Книгу "Честь имею" забрал(а) Карл
Книгу "Оно" забрал(а) Джейн
Книгу "Ночной дозор" забрал(а) Сергей
Книгу "Война и мир" забрал(а) Наташа
Книгу "Эдем" забрал(а) Наташа
Книгу "Война и мир" забрал(а) Сергей
Книга "Эдем" отсутствует. Ее забрал(а) Наташа


from datetime import datetime as dt

def date_value(date):
    d, m, y = date[:2], date[3:5], date[6:10]
    return 10000 * int(y) + 100 * int(m) + int(d)

m = {}


line = input().split(', ')
line = [i.strip('"') for i in line]

for i in line:
    m[i] = 0

b = input().split(' "')

s = []

while b[0] != '.':
    
    c = [b[0]]
    c.extend(b[1].split('" '))
    b = [c[0], c[1]]
    b.extend(c[2].split(' ('))
    if b[0] == 'Взять':
        b[3] = b[3].strip(')')
    
    s.append(b)
    b = input().split(' "')
s = sorted(s, key = lambda x: date_value(x[2]))

for i in s:
    if i[0] == 'Взять' and (m[i[1]] == 0 or dt.strptime(m[i[1]][1], "%d.%m.%Y") - dt.strptime(i[2], "%d.%m.%Y").days >= 30):
        h = 'Книгу ' + '"' + i[1] + '" ' + 'забрал(а) ' + i[3]
        m[i[1]] = [i[3], i[2]] 
        print(h)
    elif i[0] == 'Вернуть':
        m[i[1]] = 0
    else:
        h = 'Книга ' + '"' + i[1] + '"' + 'отсутствует. Её забрал(а) ' + m[i[1]][0]
        print(h)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question