May-08-2019, 12:07 AM
(This post was last modified: May-08-2019, 12:54 AM by michalmonday.)
Maybe it doesn't sound like a difficult task but the rule about not breaking words in combination with the pagination and 1440 char limit makes it a bit complicated I think.
''' Max length of the message is 1440, this would mean that the number of SMS will not exceed 9 (because 1440/160 is 9) but that is not true. After adding characters needed for pagination the total length of all messages may exceed 1440 and occupy 10 SMS all together. This in turn will change how many characters the pagination requires. E.g. instead of (1/9) it could be (1/10) which takes 1 character more. The task does not specify that the messages must be sent in the most efficient way, therefore it will be assumed that each page symbol always occupies 7 characters instead of 6 or variable number. ''' example_text = "As a reminder, you have an appointment with Dr. Smith tomorrow at 3:30 pm. If you are unable to make this appointment, please call our customer service line at least 1 hour before your scheduled appointment time." example_text_2 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." example_text_3 = "Very short message without pagination." example_text_4 = "A " * 719 + "BB" # text that has 1440 characters (with BB at the end to make sure it's properly sending the characters at the end) def get_index_of_last_space_before_nth_char(text, n): '''Function that checks where to cut the message (it can't be just cut at 160th char because it could cut mid-word). Notice that if the text message does not contain a space character then it just won't work. So it would be good idea to modify this function so it will return index of last of any 'whitespace' (e.g. tab, carriage return/new line) or punctuation (or both combined).''' return n - text[:n][::-1].index(' ') def get_string_messages(msg): '''Function that takes in a string message and returns an array of string messages with pagination if needed.''' if len(msg) <= 160: # No pagination is needed return [msg] else: # Pagination is needed single_str_max_len = 153 # 153 because 7 chars are reserved for page number string_messages = [] while msg: if len(msg) <= single_str_max_len: # if it's the last message then don't bother about not breaking words msg_len = len(msg) else: # check where is the last space before end of the sms to avoid breaking words msg_len = get_index_of_last_space_before_nth_char(msg, single_str_max_len) # generate new SMS msg_content = msg[: msg_len] # append it to returned list string_messages.append(msg_content) # cut it out of the whole text msg = msg[msg_len:] # append pagination to each string message for i in range(0, len(string_messages)): string_messages[i] += f'({i+1}/{len(string_messages)})' return string_messages def present_program_functionality(): print("Example 1:") for i, sms in enumerate(get_string_messages(example_text)): print(f'SMS number {i+1}: {sms}') print("\n\nExample 2:") for i, sms in enumerate(get_string_messages(example_text_2)): print(f'SMS number {i+1}: {sms}') print("\n\nExample 3:") for i, sms in enumerate(get_string_messages(example_text_3)): print(f'SMS number {i+1}: {sms}') print("\n\nExample 4:") for i, sms in enumerate(get_string_messages(example_text_4)): print(f'SMS number {i+1}: {sms}') present_program_functionality()
Output:Example 1:
SMS number 1: As a reminder, you have an appointment with Dr. Smith tomorrow at 3:30 pm. If you are unable to make this appointment, please call our customer service (1/2)
SMS number 2: line at least 1 hour before your scheduled appointment time.(2/2)
Example 2:
SMS number 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, (1/3)
SMS number 2: quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum (2/3)
SMS number 3: dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.(3/3)
Example 3:
SMS number 1: Very short message without pagination.
Example 4:
SMS number 1: A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A (1/10)
SMS number 2: A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A (2/10)
SMS number 3: A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A (3/10)
SMS number 4: A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A (4/10)
SMS number 5: A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A (5/10)
SMS number 6: A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A (6/10)
SMS number 7: A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A (7/10)
SMS number 8: A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A (8/10)
SMS number 9: A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A (9/10)
SMS number 10: A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A BB(10/10)