Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use Python Pac-Man is a video game character who normally eats dots, power pellets, and the occasional ghost. Recently, hes fallen on hard times and

Use Python

Pac-Man is a video game character who normally eats dots, power pellets, and the occasional ghost. Recently, hes 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-Mans 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 underscores ( ) to represent consumed characters and a less-than sign (<) to represent Pac-Mans final position (either at the very beginning or end of the string, or right before the character that stopped him). Finally, your output should include any uneaten part of the string.

For example, consider the string cat. Pac-Man can eat the c and the a, but stops when he reaches t (because it is one of the letters in GHOST). Thus, the final string will be

Hint #1: Use two while loops to solve this problem: one for the characters before Pac-Man stops, and one for any letters that remain in the input after he stops eating.

Hint #2: If Pac-Man stops eating in the middle of the input, you will need to print him instead of the last character that he successfully consumed. Depending on how you approach this problem, you may find that string slicing is helpful to remove the final character of a string. To slice a string, follow the string variables name with square brackets; inside the brackets, write the starting index, followed by a colon (:), followed by the ending index. The slice will contain all of the characters from the starting index up to but not including the ending index. For example, "helloworld"[3:7] will return the string lowo. Here, your ending index should probably be something like length of string - 1.

TEST Example:

Function Call Return Value
pacman(pacmancanbeawinner) __________________<
pacman(pacmanloses) ______
pacman(Helloworld)

-------------------

def pacman(text): # Add your code here. You will also need to change the line below.  return None 
if __name__ == "__main__": 

############### Tests ############### print('Testing pacman() for text="pacmancanbeawinner": ' + str(pacman("pacmancanbeawinner"))) print('Testing pacman() for text="pacmanloses": ' + str(pacman("pacmanloses"))) print('Testing pacman() for text="Helloworld": ' + str(pacman("Helloworld"))) 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions