I’m having an issue running the browser test environment for the Python pig latin exercise. The tests time out. Here’s my code:
def arrayConstructor(string):
“”“Pushes each word as seperate elements in an array”“”
indiv_words =
i = 0
while i < len(string):
if string[i] == " ":
indiv_words.append(string[0:i])
string = string[i:]
i = 0
i += 1
indiv_words.append(string[:i])
return indiv_words
def translate(text):
“”“Iterates through each letter of each element of the array, translating into pig latin”“”
vowels = [“a”, “e”, “i”, “o”, “u”]
text = text.strip()
wordArray = arrayConstructor(text)
k = 0
final = “”
while k < len(wordArray):
word = wordArray[k].strip()
i = 0
while i < len(word):
if word[i] in vowels:
newstring_end = word[i:]
new_string_beginning = word[:i] #Rule 3
if (word.startswith(“xr”)) or (word.startswith(“yt”) or word[0] in vowels):
final += " " + word + “ay”
i = len(word) - i
elif “qu” in word[0:i+1]:
final += " " + newstring_end[1:] + new_string_beginning[0:i-1] + “qu” + “ay”
i = len(word) - i
else:
final += " " +newstring_end + new_string_beginning + “ay”
i = len(word) - i
i += 1
k += 1
return final.strip()
I know it’s not efficient but would something here cause the timeout? Maybe a bad loop somewhere? It runs fine in VSCode.
PS if someone could tell me how to format code in these posts that’d be great :p sorry for the bad formatting.
def arrayConstructor(string):
"""Pushes each word as seperate elements in an array"""
indiv_words = []
i = 0
while i < len(string):
if string[i] == " ":
indiv_words.append(string[0:i])
string = string[i:]
i = 0
i += 1
indiv_words.append(string[:i])
return indiv_words
def translate(text):
"""Iterates through each letter of each element of the array, translating into pig latin"""
vowels = ["a", "e", "i", "o", "u"]
text = text.strip()
wordArray = arrayConstructor(text)
k = 0
final = ""
while k < len(wordArray):
word = wordArray[k].strip()
i = 0
while i < len(word):
if word[i] in vowels:
newstring_end = word[i:]
new_string_beginning = word[:i]
#Rule 3
if (word.startswith("xr")) or (word.startswith("yt") or word[0] in vowels):
final += " " + word + "ay"
i = len(word) - i
elif "qu" in word[0:i+1]:
final += " " + newstring_end[1:] + new_string_beginning[0:i-1] + "qu" + "ay"
i = len(word) - i
else:
final += " " +newstring_end + new_string_beginning + "ay"
i = len(word) - i
i += 1
k += 1
return final.strip()
What exactly are you running in VSCode? Are you running all the tests? Your code does have an infinite loop with square where i keeps changing in a loop.