Added break_up_string_by_terms

This commit is contained in:
Grant Sanderson
2018-05-05 19:41:30 -07:00
parent d6fba3576c
commit ce57e342b1

View File

@ -1,5 +1,6 @@
import re
import string
import itertools as it
def to_camel_case(name):
@ -24,3 +25,27 @@ def camel_case_initials(name):
def complex_string(complex_num):
return filter(lambda c: c not in "()", str(complex_num))
def break_up_string_by_terms(full_string, *terms):
"""
Given a string, and an arbitrary number of possible substrings, returns a list
of strings which would concatenate to make the full string, and in which
these special substrings appear as their own elements.
For example, break_up_string_by_terms("to be or not to be", "to", "be") would
return ["to", " ", "be", " or not ", "to", " ", "be"]
"""
if len(terms) == 0:
return [full_string]
term = terms[0]
substrings = list(it.chain(*zip(
full_string.split(term),
it.repeat(term)
)))
substrings.pop(-1)
substrings = filter(lambda s: s != "", substrings)
return list(it.chain(*[
break_up_string_by_terms(substring, *terms[1:])
for substring in substrings
]))