A community-maintained Python framework for creating mathematical animations.
Added break_up_string_by_terms
Grant Sandersonce57e342b13b
diff --git a/utils/strings.py b/utils/strings.pyindex fdadf99b4..cc4307ace 100644--- a/utils/strings.py+++ b/utils/strings.py@@ -1,5 +1,6 @@import reimport string+import itertools as itdef 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+ ]))