Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Solved with regex solution in Clear category for Combining Celebrity Names by Kolia951
import re
def brangelina(word1: str, word2: str) -> str:
VOWELS = "aeiou"
CONSONANTS = "bcdfghjklmnpqrstvwxyz"
# first word with more than one groups
if result := re.search(rf"[{VOWELS}]+[{CONSONANTS}]+[{VOWELS}]+.?$", word1):
first = word1[:result.span()[0]]
# first word with only one group
elif result := re.search(rf"^[{CONSONANTS}]+", word1):
first = word1[:result.span()[1]]
# first word with no mathes
else:
first = ""
# substitution of consonants in first word with first word
if word2[0] in CONSONANTS:
return re.sub(rf"^[{CONSONANTS}]+", first, word2)
else:
return first + word2
print("Example:")
print(brangelina("brad", "angelina"))
# These "asserts" are used for self-checking
assert brangelina("brad", "angelina") == "brangelina"
assert brangelina("angelina", "brad") == "angelad"
assert brangelina("sheldon", "amy") == "shamy"
print("The mission is done! Click 'Check Solution' to earn rewards!")
Nov. 28, 2023
Comments: