Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement