Darlexbg

03. Contacts list

Oct 3rd, 2020 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. contacts = input().split()
  2.  
  3. while True:
  4.     command = input().split()
  5.     action = command[0]
  6.  
  7.     if action == "Add":
  8.         contact = command[1]
  9.         index = int(command[2])
  10.         if contact not in contacts:
  11.             contacts.append(contact)
  12.         else:
  13.             if 0 <= index < len(contacts):
  14.                 contacts.insert(index, contact)
  15.  
  16.     elif action == "Remove":
  17.         index = int(command[1])
  18.         if 0 <= index < len(contacts):
  19.             contacts.pop(index)
  20.  
  21.     elif action == "Export":
  22.         start_index = int(command[1])
  23.         count = int(command[2])
  24.        
  25.         # If the count requested is more than the contacts- just print them to the end.
  26.         if count > len(contacts):
  27.             count = len(contacts)
  28.            
  29.         # Print the next {count} contacts starting from the given {startIndex} (including), separated by a single space.
  30.         if 0 <= start_index < count:
  31.             for contact in range(start_index, count):
  32.                 print(contacts[contact], end=" ")
  33.         print()
  34.  
  35.     elif action == "Print":
  36.         status = command[1]
  37.         print("Contacts: ", end="")
  38.         if status == "Normal":
  39.             print(" ".join(contacts))
  40.         elif status == "Reversed":
  41.             contacts.reverse()
  42.             print(" ".join(contacts))
  43.         break
Advertisement
Add Comment
Please, Sign In to add comment