Created
November 2, 2016 01:02
-
-
Save chadmiller/ef8c8c330af06f3d9fbbf62c62295bbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment