Uber | Phone | Break Messages
Anonymous User
5605

Problem

Write a function that takes a big message and splits it into minimum number of smaller messages adhering to given limit. While splitting the function should not split a word into two messages. In the output messages please add tag ( x/y ). The total length (including the tag) should not exceed 160.

Constraints

Each chunk:

  • up to 160 characters long
  • no word should be split in the middle
  • each chunk has to have its order suffixed in the form of ' (k/n)', e.g. "this is the first chunk (1/2)", "this is the second chunk (2/2)"
  • if the text provided to the function is less than 160 characters, no ordering should be suffixed

Input:
The South Lake Union Streetcar is a streetcar route in Seattle, Washington, United States. Traveling 1.3 miles (2.1 km), it connects downtown to the South Lake Union neighborhood on Westlake Avenue, Terry Avenue, and Valley Street. It was the first modern Seattle Streetcar line, beginning service on December 12, 2007, two years after a separate heritage streetcar ceased operations. It was conceived as part of the redevelopment of South Lake Union into a technology hub, with lobbying and financial support from Paul Allen.

Output Not fully correct just to make it easy for you to imagine it :)
Array of messages
The South Lake Union Streetcar is a streetcar route in Seattle, Washington, United States. Traveling 1.3 miles (2.1 km), it connects downtown to the South (1/4)
Lake Union neighbourhood on Westlake Avenue, Terry Avenue, and Valley Street. It was the first modern Seattle Streetcar line, beginning service on December (2/4)
12, 2007, two years after a separate heritage streetcar ceased operations. It was conceived as part of the redevelopment of South Lake Union into (3/4)
a technology hub, with lobbying and financial support from Paul Allen. (4/4)

Solution Implmented

def messageList(message):
    if not message:
        return "Message is Empty!"
    if len(message) <= 160:
        return message

    msgs_map = {}
    message_words = message.split()
    current_message = ''
    current_message_number = 1
    for word in message_words:
        if len(current_message + ' ' + word) > 154:
            msgs_map[current_message_number] = current_message
            current_message = word
            current_message_number += 1
        else:
            current_message += word + ' '

    n = len(msgs_map)
    messages = []
    for k in msgs_map:
        messages.append(msgs_map[k] + ' (' + str(k) + '/' + str(n) + ')')

    return messages


if __name__ == '__main__':
    T = int(input())
    for _ in range(T):
        message = input()
        print(messageList(message))

Anyslis I did, am not sure if it is correct?

n ==> # of words in provided message
m ==> # of the broken messages
O (n + m)

Suggested solution without having time to implment it

by first find the n which is the number of messages we can have and do it in this way:

    len(bigMessage)/154 => number of broken messages
	
Then go over the words build the list in one path now we will be improved the space
to only O(w + n) :  w => # of words, n # of messages
Time complexity will be improved to only we do one path to build the messages list,
don't forget the split function that it takes O(n)
so total O(n + n) ==> O(2n) ==> O(n)

If anyone have any idea about what could be the best solution I will be happy to know it :).

Comments (9)