From ce57e342b13b707ced425c13164be3691b42e501 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Sat, 5 May 2018 19:41:30 -0700 Subject: [PATCH] Added break_up_string_by_terms --- utils/strings.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/utils/strings.py b/utils/strings.py index fdadf99b..cc4307ac 100644 --- a/utils/strings.py +++ b/utils/strings.py @@ -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 + ]))