Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- import unittest
- ############ Split words
- def SplitInnerLoop1(text):
- i = 0
- res = []
- while i < len(text):
- while i < len(text) and text[i] == " ":
- i += 1
- if i >= len(text):
- break
- word = ""
- while i < len(text) and text[i] != " ":
- word += text[i]
- i += 1
- res.append(word)
- return res
- def SplitHelpperFunc(text):
- def collect(text, start_i, is_good_char):
- chars = ""
- while start_i < len(text) and is_good_char(text[start_i]):
- chars += text[start_i]
- start_i += 1
- return start_i, chars
- i = 0
- res = []
- while i < len(text):
- i, spaces = collect(text, i, lambda ch: ch == " ")
- if i >= len(text):
- break
- i, word = collect(text, i, lambda ch: ch != " ")
- res.append(word)
- return res
- def SplitInnerLoop2(text):
- i = -1
- res = []
- while i < len(text) - 1:
- i += 1
- if text[i] == " ":
- continue
- word = ""
- while i < len(text) and text[i] != " ":
- word += text[i]
- i += 1
- res.append(word)
- return res
- def SplitInnerLoop3(text):
- i = -1
- res = []
- while i < len(text) - 1:
- i += 1
- if text[i] != " ":
- word = ""
- while i < len(text) and text[i] != " ":
- word += text[i]
- i += 1
- res.append(word)
- return res
- def SplitStateMachine(text):
- in_word = False
- word = ""
- res = []
- for ch in text:
- if ch == " ":
- if in_word:
- res.append(word)
- word = ""
- in_word = False
- else:
- word += ch
- in_word = True
- if in_word:
- res.append(word)
- return res
- def SplitStateMachineSentinal(text):
- in_word = False
- word = ""
- res = []
- for ch in text + " ":
- if ch == " ":
- if in_word:
- res.append(word)
- word = ""
- in_word = False
- else:
- word += ch
- in_word = True
- return res
- def SplitListEmpty(text):
- res = [""]
- for ch in text:
- if res[-1] and ch != " ":
- res[-1] = res[-1] + ch
- else:
- if ch == " ":
- ch = ""
- res.append(ch)
- return filter(lambda word: word, res)
- def SplitInnerIter(text):
- res = []
- it = iter(text)
- for ch in it:
- if ch != " ":
- word = ch
- for ch in it:
- if ch == " ":
- break
- word += ch
- res.append(word)
- return res
- def SplitRecursive(text, prevWord=" "):
- if prevWord == " ":
- if not text:
- return []
- if text[0] == " ":
- return SplitRecursive(text[1:])
- return SplitRecursive(text[1:], text[0])
- if not text:
- return [prevWord]
- if text[0] == " ":
- return [prevWord] + SplitRecursive(text[1:])
- return SplitRecursive(text[1:], prevWord + text[0])
- def SplitRecursive2Helper(text, prev_word=""):
- if not text:
- return [prev_word]
- if text[0] == " ":
- return [prev_word] + SplitRecursive2Helper(text[1:])
- return SplitRecursive2Helper(text[1:], prev_word + text[0])
- SplitRecursive2 = lambda text: filter(lambda word: word, SplitRecursive2Helper(text))
- class TestSplitWords(unittest.TestCase):
- def test_All(self):
- funcs = [SplitInnerLoop1, SplitHelpperFunc, SplitInnerLoop2,
- SplitInnerLoop3, SplitStateMachine, SplitStateMachineSentinal,
- SplitListEmpty,
- SplitInnerIter, SplitRecursive, SplitRecursive2]
- for f in funcs:
- self.do_func(f)
- def do_func(self, func):
- s = ""
- a = func(s)
- self.assertEqual([], a)
- s = "x"
- a = func(s)
- self.assertEqual(["x"], a)
- s = " x"
- a = func(s)
- self.assertEqual(["x"], a)
- s = "x "
- a = func(s)
- self.assertEqual(["x"], a)
- s = " x "
- a = func(s)
- self.assertEqual(["x"], a)
- s = "a b c"
- a = func(s)
- self.assertEqual(["a", "b", "c"], a)
- s = "a b c"
- a = func(s)
- self.assertEqual(["a", "b", "c"], a)
- s = "azzzz bcccc ctttttt"
- a = func(s)
- self.assertEqual(["azzzz", "bcccc", "ctttttt"], a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement