SimeonTs

SUPyF2 P.-Mid-Exam/30 June 2019/1 - Contact List

Oct 28th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. """
  2. Programming Fundamentals Mid Exam - 30 June 2019 Group 1
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1682#2
  4.  
  5. SUPyF2 P.-Mid-Exam/30 June 2019/1 - Contact List
  6.  
  7. Problem:
  8. Create a program that helps you keep track of the contacts that you have.
  9. You will receive the list of contacts you already have on a single line,
  10. separated by a single space in the following format:
  11. "{contact1} {contact2} {contact3}… {contactn}"
  12. Then you will receive commands that you need to execute over your list. There are four possible commands:
  13. • "Add {contact} {index}":
  14. o   If {contact} isn’t already contained – add it in the end of the collection.
  15. o   If {contact} is already contained – add it on the given index, if the index exists.
  16. • "Remove {index}"
  17. o   Remove the contact on the given index, if the index exists.
  18. • "Export {startIndex} {count}":
  19. o   Print the next {count} contacts starting from the given {startIndex} (including), separated by a single space.
  20. If the count requested is more than the contacts- just print them to the end.
  21. "{contact} {contact} {contact}"
  22. • "Print Normal/Reversed"
  23. o   Print the contact list in normal (in the order they have been added) or reversed order and then stop the program:
  24. "Contacts: {contact1} {contact2}… {contactn}"
  25. Input
  26. • On the 1st line, you will receive the starting list with the names of the contacts separated by a single space.
  27. • On the next lines, you will receive commands in the format described above.
  28. Output
  29. • Print the needed output upon the "Export" command.
  30. • Print the list after the manipulations upon the "Print" command in the format described above.
  31.  
  32. Examples:
  33. Input:
  34. Alisson Bellamy Candace Tristan
  35. Remove 3
  36. Add Bellamy 2
  37. Print Normal
  38.  
  39. Output:
  40. Contacts: Alisson Bellamy Bellamy Candace
  41.  
  42. Comments:
  43. First, we receive the “Remove 3” command, so we remove the contact at index 3 (“Tristan”).
  44. Then, we receive the command “Add Bellamy 2” but we already have “Bellamy” in our collection, so we add it on index 2.
  45. Lastly, we have to print the collection in normal order, so our output is: “Contacts: Alisson Bellamy Bellamy Candace”
  46.  
  47. Input:
  48. Zayn Katy Ariana Avril Nick Mikolas
  49. Remove 3
  50. Add Jacob 0
  51. Export 0 3
  52. Export 3 8
  53. Print Reversed
  54.  
  55. Output:
  56. Zayn Katy Ariana
  57. Nick Mikolas Jacob
  58. Contacts: Jacob Mikolas Nick Ariana Katy Zayn
  59. """
  60. contacts = input().split()
  61.  
  62. while True:
  63.     command = input().split()
  64.     if command[0] == "Add":
  65.         contact, index = command[1], int(command[2])
  66.         if contact not in contacts and index < len(contacts):
  67.             contacts.append(contact)
  68.         elif 0 <= index < len(contacts):
  69.             contacts.insert(index, contact)
  70.  
  71.     elif command[0] == "Remove":
  72.         index = int(command[1])
  73.         if 0 <= index < len(contacts):
  74.             contacts.pop(index)
  75.  
  76.     elif command[0] == "Export":
  77.         start_index = int(command[1])
  78.         count = int(command[2])
  79.         print(*contacts[start_index:start_index + count])
  80.  
  81.     elif command[0] == "Print":
  82.         if command[1] == "Normal":
  83.             print(f"Contacts: {' '.join(contacts)}")
  84.         elif command[1] == "Reversed":
  85.             print(f"Contacts: {' '.join(contacts[::-1])}")
  86.         exit(0)
Add Comment
Please, Sign In to add comment