Advertisement
Onesible

School Library

Oct 22nd, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. def add(shelf, book):
  2.     if book not in shelf:
  3.         shelf.insert(0, book)
  4.     return shelf
  5.  
  6. def take(shelf, book):
  7.     if book in shelf:
  8.         shelf.remove(book)
  9.     return shelf
  10.  
  11. def swap(shelf, book1, book2):
  12.     if book1 in shelf and book2 in shelf:
  13.         index1 = shelf.index(book1)
  14.         index2 = shelf.index(book2)
  15.         shelf[index1], shelf[index2] = shelf[index2], shelf[index1]
  16.     return shelf
  17.  
  18. def insert(shelf, book):
  19.     if book not in shelf:
  20.         shelf.append(book)
  21.     return shelf
  22.  
  23. def check(shelf, index):
  24.     if 0 <= index < len(shelf):
  25.         print(shelf[index])
  26.  
  27. def main():
  28.     shelf = input().split('&')
  29.     input_line = input().split(' | ')
  30.     while input_line[0] != 'Done':
  31.         command = input_line[0]
  32.         if command == 'Add Book':
  33.             book = input_line[1]
  34.             shelf = add(shelf, book)
  35.         elif command == 'Take Book':
  36.             book = input_line[1]
  37.             shelf = take(shelf, book)
  38.         elif command == 'Swap Books':
  39.             book1 = input_line[1]
  40.             book2 = input_line[2]
  41.             shelf = swap(shelf, book1, book2)
  42.         elif command == 'Insert Book':
  43.             book = input_line[1]
  44.             shelf = insert(shelf, book)
  45.         elif command == 'Check Book':
  46.             index = int(input_line[1])
  47.             check(shelf, index)
  48.         input_line = input().split(' | ')
  49.     print(*shelf, sep=', ')
  50. if __name__ == '__main__':
  51.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement