Advertisement
franji

Split function

May 16th, 2018
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.61 KB | None | 0 0
  1. from __future__ import division
  2. import unittest
  3.  
  4. ############ Split words
  5. def SplitInnerLoop1(text):
  6.     i = 0
  7.     res = []
  8.     while i < len(text):
  9.         while i < len(text) and text[i] == " ":
  10.             i += 1
  11.         if i >= len(text):
  12.             break
  13.         word = ""
  14.         while i < len(text) and text[i] != " ":
  15.             word += text[i]
  16.             i += 1
  17.         res.append(word)
  18.     return res
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. def SplitHelpperFunc(text):
  26.     def collect(text, start_i, is_good_char):
  27.         chars = ""
  28.         while start_i < len(text) and is_good_char(text[start_i]):
  29.             chars += text[start_i]
  30.             start_i += 1
  31.         return start_i, chars
  32.  
  33.     i = 0
  34.     res = []
  35.     while i < len(text):
  36.         i, spaces = collect(text, i, lambda ch: ch == " ")
  37.         if i >= len(text):
  38.             break
  39.         i, word = collect(text, i, lambda ch: ch != " ")
  40.         res.append(word)
  41.     return res
  42.  
  43.  
  44.  
  45. def SplitInnerLoop2(text):
  46.     i = -1
  47.     res = []
  48.     while i < len(text) - 1:
  49.         i += 1
  50.         if text[i] == " ":
  51.             continue
  52.         word = ""
  53.         while i < len(text) and text[i] != " ":
  54.             word += text[i]
  55.             i += 1
  56.         res.append(word)
  57.     return res
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. def SplitInnerLoop3(text):
  66.     i = -1
  67.     res = []
  68.     while i < len(text) - 1:
  69.         i += 1
  70.         if text[i] != " ":
  71.             word = ""
  72.             while i < len(text) and text[i] != " ":
  73.                 word += text[i]
  74.                 i += 1
  75.             res.append(word)
  76.     return res
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. def SplitStateMachine(text):
  86.     in_word = False
  87.     word = ""
  88.     res = []
  89.     for ch in text:
  90.         if ch == " ":
  91.             if in_word:
  92.                 res.append(word)
  93.                 word = ""
  94.             in_word = False
  95.         else:
  96.             word += ch
  97.             in_word = True
  98.     if in_word:
  99.         res.append(word)
  100.     return res
  101.  
  102.  
  103.  
  104.  
  105. def SplitStateMachineSentinal(text):
  106.     in_word = False
  107.     word = ""
  108.     res = []
  109.     for ch in text + " ":
  110.         if ch == " ":
  111.             if in_word:
  112.                 res.append(word)
  113.                 word = ""
  114.             in_word = False
  115.         else:
  116.             word += ch
  117.             in_word = True
  118.     return res
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. def SplitListEmpty(text):
  126.     res = [""]
  127.     for ch in text:
  128.         if res[-1] and ch != " ":
  129.                 res[-1] = res[-1] + ch
  130.         else:
  131.             if ch == " ":
  132.                 ch = ""
  133.             res.append(ch)
  134.     return filter(lambda word: word, res)
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. def SplitInnerIter(text):
  146.     res = []
  147.     it = iter(text)
  148.     for ch in it:
  149.         if ch != " ":
  150.             word = ch
  151.             for ch in it:
  152.                 if ch == " ":
  153.                     break
  154.                 word += ch
  155.             res.append(word)
  156.     return res
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. def SplitRecursive(text, prevWord=" "):
  166.     if prevWord == " ":
  167.         if not text:
  168.             return []
  169.         if text[0] == " ":
  170.             return SplitRecursive(text[1:])
  171.         return SplitRecursive(text[1:], text[0])
  172.     if not text:
  173.         return [prevWord]
  174.     if text[0] == " ":
  175.         return [prevWord] + SplitRecursive(text[1:])
  176.     return SplitRecursive(text[1:], prevWord + text[0])
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. def SplitRecursive2Helper(text, prev_word=""):
  186.     if not text:
  187.         return [prev_word]
  188.     if text[0] == " ":
  189.         return [prev_word] + SplitRecursive2Helper(text[1:])
  190.     return SplitRecursive2Helper(text[1:], prev_word + text[0])
  191.  
  192. SplitRecursive2 = lambda text: filter(lambda word: word, SplitRecursive2Helper(text))
  193.  
  194.  
  195.  
  196.  
  197. class TestSplitWords(unittest.TestCase):
  198.   def test_All(self):
  199.       funcs = [SplitInnerLoop1, SplitHelpperFunc, SplitInnerLoop2,
  200.                SplitInnerLoop3, SplitStateMachine, SplitStateMachineSentinal,
  201.                SplitListEmpty,
  202.                SplitInnerIter, SplitRecursive, SplitRecursive2]
  203.  
  204.       for f in funcs:
  205.           self.do_func(f)
  206.  
  207.   def do_func(self, func):
  208.       s = ""
  209.       a = func(s)
  210.       self.assertEqual([], a)
  211.       s = "x"
  212.       a = func(s)
  213.       self.assertEqual(["x"], a)
  214.       s = " x"
  215.       a = func(s)
  216.       self.assertEqual(["x"], a)
  217.       s = "x "
  218.       a = func(s)
  219.       self.assertEqual(["x"], a)
  220.       s = " x "
  221.       a = func(s)
  222.       self.assertEqual(["x"], a)
  223.       s = "a b c"
  224.       a = func(s)
  225.       self.assertEqual(["a", "b", "c"], a)
  226.       s = "a b  c"
  227.       a = func(s)
  228.       self.assertEqual(["a", "b", "c"], a)
  229.       s = "azzzz bcccc  ctttttt"
  230.       a = func(s)
  231.       self.assertEqual(["azzzz", "bcccc", "ctttttt"], a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement