How can I solve this python (3.4) problem using While loop ? Part II: A Pac-Man
ID: 3835754 • Letter: H
Question
How can I solve this python (3.4) problem using While loop?
Part II: A Pac-Man Puzzler (3 points0 Pac-Man is a video game character who normally eats dots, power pellets, and the occasional ghost. Recently, he's fallen on hard times and has been reduced to (mainly) eating letters of the alphabet. Pac-Man can eat most letters of the alphabet, but he is unable to digest any of the characters in the word "GHoST" (uppercase or lowercase) When he reaches one of these characters in a string, he loses his appetite and stops eating. Your job is to complete the pacman function, which traces Pac-Man's progress through a string of uppercase and lowercase letters (with no spaces, digits, or symbols) and prints out the final state of the string. Use under- scores to represent consumed characters and a less-than sign (Explanation / Answer
def pacman(diet):
noneatable = "GHOSTghost"
list_str = list(diet)
is_remain = False
for i in range(0, len(list_str)):
if list_str[i] in noneatable:
is_remain = True
if i == 0:
list_str.insert(0, '<')
else:
list_str[i-1] = '<'
break
else:
list_str[i] = '_'
if(not is_remain):
list_str.append('<')
return "".join(list_str)
print(pacman('pacmancanbeawinner'))
print(pacman('pacmanloses'))
print(pacman('HelloWorld'))
# code link: https://paste.ee/p/2kapI
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.