mirror of
https://github.com/3b1b/manim.git
synced 2025-08-01 17:29:06 +08:00
Added break_up_string_by_terms
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
import string
|
import string
|
||||||
|
import itertools as it
|
||||||
|
|
||||||
|
|
||||||
def to_camel_case(name):
|
def to_camel_case(name):
|
||||||
@ -24,3 +25,27 @@ def camel_case_initials(name):
|
|||||||
|
|
||||||
def complex_string(complex_num):
|
def complex_string(complex_num):
|
||||||
return filter(lambda c: c not in "()", str(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
|
||||||
|
]))
|
||||||
|
Reference in New Issue
Block a user