Advertisement
SimeonTs

SUPyF Objects and Classes - 9. Messages *

Aug 12th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.38 KB | None | 0 0
  1. """
  2. Objects and Classes
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/950#8
  4.  
  5. SUPyF Objects and Classes - 9. Messages *
  6.  
  7. Problem:
  8. Create a class User, which has a Username (string), and ReceivedMessages (Collection of Messages).
  9. Create a class Message, which has a Content (string) and a Sender (User).
  10. You will have to store a messaging history for every user. The input consists of 2 commands:
  11. “register {username}”
  12. “{senderUsername} send {recipientUsername} {content}”
  13.  
  14. The register command, registers a user with the given username.
  15. The send command, sends a message, from the given sender, to the given recipient, with the given content.
  16. That means that you must add the message to the recipient’s ReceivedMessages.
  17. If even one of the given names does NOT exist, ignore the command.
  18. When you receive the command “exit” you must end the input sequence. After that you will receive 2 usernames,
  19. separated by a space.
  20. You must print all messages, sent, between the two users, corresponding to the given usernames.
  21. The messages should be printed in a specified way. You should print first a message SENT from the first user,
  22. then a message SENT from the second user, then a message from the first user, and so on.
  23. If one of the collections of messages has more elements than the other, just print the remaining elements from it.
  24. The first user’s messages must be printed in the following way:
  25. “{firstUser}: {content}”
  26. The second user’s message must be printed in the following way:
  27. “{content} :{secondUser}”
  28. When you print the whole output, it should look like this:
  29. {firstUser}: {content1}
  30. {content1} :{secondUser}
  31. {firstUser}: {content2}
  32. {content2} :{secondUser}
  33. . . .
  34. In case there are NO messages between the two users, print “No messages”.
  35.  
  36. Examples:
  37.    Input:
  38.        register Ivan
  39.        register Pesho
  40.        Ivan send Pesho pesho
  41.        Ivan send Pesho pesho_tam_li_si?
  42.        Pesho send Ivan kaji_vanka
  43.        Pesho send Ivan tuk_sum
  44.        Pesho send Ivan chakai_che_bachkam
  45.        Ivan send Pesho kvo_stava
  46.        Ivan send Pesho kak_si
  47.        Ivan send Pesho deka_izbega_be?
  48.        Ivan send Pesho pecaaa!!!
  49.        exit
  50.        Ivan Pesho
  51.    Output:
  52.        Ivan: pesho
  53.        kaji_vanka :Pesho
  54.        Ivan: pesho_tam_li_si?
  55.        tuk_sum :Pesho
  56.        Ivan: kvo_stava
  57.        chakai_che_bachkam :Pesho
  58.        Ivan: kak_si
  59.        Ivan: deka_izbega_be?
  60.        Ivan: pecaaa!!!
  61.    Input:
  62.        register John
  63.        John send Harry harry_you_there?
  64.        register Harry
  65.        John send Harry harry?
  66.        register Donald
  67.        Harry send John yeah_sorry_was_out...
  68.        Harry send John wassup?
  69.        Donald send John Yo_John?
  70.        Donald send Jonh You_there?
  71.        John send Harry thank_god!!
  72.        John send Harry I_need_you!
  73.        exit
  74.        John Harry
  75.    Output:
  76.        John: harry?
  77.        yeah_sorry_was_out... :Harry
  78.        John: thank_god!!
  79.        wassup? :Harry
  80.        John: I_need_you!
  81. """
  82. # P.S. I have lost my own solution, of this problem... SO this is copy and paste from someone else's code.
  83.  
  84.  
  85. class User:
  86.  
  87.     def __init__(self, Username, ReceivedMessages):
  88.         self.Username = Username
  89.         self.ReceivedMessages = ReceivedMessages
  90.  
  91.  
  92. class Message:
  93.  
  94.     def __init__(self, Content, Sender):
  95.         self.Content = Content
  96.         self.Sender = Sender
  97.  
  98.  
  99. def Max_num(a, b):
  100.     if a > b:
  101.         return a
  102.     else:
  103.         return b
  104.  
  105.  
  106. if __name__ == '__main__':
  107.  
  108.         input_string = input().split(' ')
  109.         users_list = []
  110.         message_list_1 = []
  111.         message_list_2 = []
  112.  
  113.         while not input_string[0] == 'exit':
  114.             if input_string[0] == 'register':
  115.                 newUser = User(input_string[1], [])
  116.                 users_list.append(newUser)
  117.             else:
  118.  
  119.                 if input_string[2] in [user.Username for user in users_list] and input_string[0] in [user.Username for user in users_list]:
  120.                     for user in users_list:
  121.                         if user.Username == input_string[2]:
  122.                             newMessage = Message(
  123.                                 input_string[3], input_string[0])
  124.                             user.ReceivedMessages.append(newMessage)
  125.  
  126.             input_string = input().split(' ')
  127.  
  128.         final_usernames = input().split(' ')
  129.  
  130.         for user in users_list:
  131.             if user.Username == final_usernames[1]:
  132.                 for messages in user.ReceivedMessages:
  133.                     if messages.Sender == final_usernames[0]:
  134.                         message_list_1.append(
  135.                             f'{messages.Sender}: {messages.Content}')
  136.  
  137.         for user in users_list:
  138.             if user.Username == final_usernames[0]:
  139.                 for messages in user.ReceivedMessages:
  140.                     if messages.Sender == final_usernames[1]:
  141.                         message_list_2.append(
  142.                             f'{messages.Content} :{messages.Sender}')
  143.  
  144.         if len(message_list_1) > 0 or len(message_list_2) > 0:
  145.             for i in range(0, Max_num(len(message_list_1), len(message_list_2))):
  146.                 if i < len(message_list_1):
  147.                     print(message_list_1[i])
  148.  
  149.                 if i < len(message_list_2):
  150.                     print(message_list_2[i])
  151.         else:
  152.             print('No messages')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement