Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Annotated "Bird Language" solution in Clear category for Bird Language by BootzenKatzen
# I'll admit... I had to use the hints for this one... I kept trying to do it where I removed the extra letters
# Instead of pulling just the letters I wanted
# The first solution:
def translate(text: str) -> str:
vowels = 'aeiouy' # we need a string of our vowels to check for
new = '' # our new string that we're going to add things into
index = 0 # we have to use index instead of range(len(text)) because we'll need to skip past the duplicates
while index < len(text): # keeps us cycling through the text until the end
if text[index] == ' ': # if the text is a space
new += text[index] # add it to the new text, then it will skip the elifs, and go to index += 1 to continue
elif text[index] not in vowels and text[index + 1] in vowels: # if it's not in vowels but the one beside it is
new += text[index] # add just that letter to the new text
index += 1 #add one to the index which would bring us to the extra letter, but then we have the 2nd index step
elif text[index] in vowels: # if it is a vowel
if text[index:index + 3] == text[index] * 3: #and the 3 letters are the same as the indexed leter 3 times
new += text[index] # add that vowel only once
index += 2 # then add to the index to skip the 2 extra vowels
index += 1 #Keeps the index moving to the next letter after the if/elif statements
return new #once we've gone through all the letters we get our new string
# The second solution:
import re, functools
translates = functools.partial(re.sub,r"(\w)(\1\1|.)",r"\1")
#Ok this one might be a little beyond my level, but I'm going to try
# functools.partial() returns a partial object based on functions, arguments and keywords provided in the parenthesis
# so it can return the part of the text we're interested in
# in this case the function is re.sub which can be used to sub in parts of strings
# I think the r" just tells it that we're using regular expressions
# Regular expressions can be used to recognize patterns or specific data types and pull them into or out of strings
# the(\w) tells it to look at a string of letters (excludes spaces and special characters I think)
# I'm reasonably sure that the \1\1 part is looking for repeats of the same letter
# I think the "|" is an "or" situation
# and then "." is used as a wildcard
# so if the text has the letter 3 times or a letter then a wildcard
# replace that part of the string with just one instance of the letter?
# If anyone can explain this one better than me, please do
print("Example:")
print(translate("hieeelalaooo"))
# These "asserts" are used for self-checking
assert translate("hieeelalaooo") == "hello"
assert translate("hoooowe yyyooouuu duoooiiine") == "how you doin"
assert translate("aaa bo cy da eee fe") == "a b c d e f"
assert translate("sooooso aaaaaaaaa") == "sos aaa"
print("The mission is done! Click 'Check Solution' to earn rewards!")
April 14, 2023
Comments: