Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import doctest
  4. import string
  5.  
  6.  
  7. def break_into_chunks(message, allowance, characters_that_count_for_one, characters_that_count_for_two):
  8.  
  9. cursor = 0
  10. allotted = 0
  11.  
  12. result = list()
  13.  
  14. while allotted < allowance and (cursor+1) < len(message):
  15. # Step forward in message and STOP when we run out space.
  16. if message[cursor] in characters_that_count_for_one:
  17. allotted += 1
  18. elif message[cursor] in characters_that_count_for_two:
  19. allotted += 2
  20. else:
  21. # nowwhat
  22.  
  23. cursor = cursor + 1
  24.  
  25. # Cursor is now at the end of the allowed space we COULD send a message in,
  26. # BUT that is not a nice place to break the message, so we need to find a
  27. # nice place to break it.
  28. # OR cursor is at the end of the whole message.
  29.  
  30. while message[cursor] != ' ' and message[cursor] != '\n':
  31. # Now we walb backward with cursor to find a space
  32. cursor = cursor - 1
  33.  
  34. # Now cursor is at a space or newline.
  35.  
  36. result.append(message[:cursor])
  37.  
  38. return result
  39.  
  40.  
  41.  
  42. if __name__ == "__main__":
  43. print(break_into_chunks("one two three", 5, string.ascii_lowercase, string.ascii_uppercase))
  44. print(break_into_chunks("a b c", 3, string.ascii_lowercase, string.ascii_uppercase))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement