Advertisement
simeonshopov

Contact List

Jan 28th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. #!/usr/local/bin/python3.7
  2. # -*- coding: utf-8 -*import
  3.  
  4.  
  5. def valid_index(i: str, lst: list):
  6.     if len(i) == 1 and i.isdigit():
  7.         return 0 <= int(i) < len(lst)
  8.  
  9.  
  10. def check_len(lst: list, num: int):
  11.     return len(lst) >= num
  12.  
  13.  
  14. contacts = input().split(' ')
  15.  
  16. while True:
  17.     command = input().split(' ')
  18.     if 'Print' in command:
  19.         break
  20.     else:
  21.         if 'Add' in command and check_len(command, 3):
  22.             name = command[1]
  23.             if name in contacts:
  24.                 if valid_index(command[2], contacts):
  25.                     idx = int(command[2])
  26.                     contacts.insert(idx, name)
  27.             else:
  28.                 contacts.append(name)
  29.         elif 'Remove' in command and check_len(command, 2):
  30.             if valid_index(command[1], contacts):
  31.                 idx = int(command[1])
  32.                 contacts.remove(contacts[idx])
  33.         elif 'Export' in command and check_len(command, 3):
  34.             if valid_index(command[1], contacts):
  35.                 idx = int(command[1])
  36.                 count = int(command[2]) + idx
  37.                 start_slice = contacts[idx:]
  38.                 if count > len(start_slice):
  39.                     print(' '.join(start_slice))
  40.                 else:
  41.                     start_slice = contacts[idx:count]
  42.                     print(' '.join(start_slice))
  43.  
  44. if check_len(command, 2):
  45.     if 'Normal' in command:
  46.         print(f"Contacts: {' '.join(contacts)}")
  47.     elif 'Reversed' in command:
  48.         contacts = contacts[::-1]
  49.         print(f"Contacts: {' '.join (contacts)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement