Skip to content

Instantly share code, notes, and snippets.

@chadmiller
Created November 2, 2016 01:02

Revisions

  1. chadmiller created this gist Nov 2, 2016.
    44 changes: 44 additions & 0 deletions message_wrapping.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #!/usr/bin/env python3

    import doctest
    import string


    def break_into_chunks(message, allowance, characters_that_count_for_one, characters_that_count_for_two):

    cursor = 0
    allotted = 0

    result = list()

    while allotted < allowance and (cursor+1) < len(message):
    # Step forward in message and STOP when we run out space.
    if message[cursor] in characters_that_count_for_one:
    allotted += 1
    elif message[cursor] in characters_that_count_for_two:
    allotted += 2
    else:
    # nowwhat

    cursor = cursor + 1

    # Cursor is now at the end of the allowed space we COULD send a message in,
    # BUT that is not a nice place to break the message, so we need to find a
    # nice place to break it.
    # OR cursor is at the end of the whole message.

    while message[cursor] != ' ' and message[cursor] != '\n':
    # Now we walb backward with cursor to find a space
    cursor = cursor - 1

    # Now cursor is at a space or newline.

    result.append(message[:cursor])

    return result



    if __name__ == "__main__":
    print(break_into_chunks("one two three", 5, string.ascii_lowercase, string.ascii_uppercase))
    print(break_into_chunks("a b c", 3, string.ascii_lowercase, string.ascii_uppercase))