In: Computer Science
Write the following Python script:
Pikachu is a well-known character in the Pokemon anime series. Pikachu can speak, but only 3 syllables: "pi", "ka", and "chu". Therefore Pikachu can only pronounce strings that can be created as a concatenation of one or more syllables he can pronounce. For example, he can pronounce the words "pikapi" and "pikachu".
You are given a String word. Your task is to check whether Pikachu can pronounce the string. If the string can be produced by concatenating copies of the strings "pi", "ka", and "chu", return "YES" (quotes for clarity). Otherwise, return "NO".
Constraints:
Check your work with these examples:
-------------------------------------------------------
word = "kpia"
Returns: "NO"
-------------------------------------------------------
word = "chuchucpihu"
Returns: "NO"
-------------------------------------------------------
word = "pikapi"
Returns: "YES"
"pikapi" = "pi" + "ka" + "pi", so Pikachu can say it.
-------------------------------------------------------
word = "pipikachu"
Returns: "YES"
This time we have "pipikachu" = "pi" + "pi" + "ka" + "chu", so Pikachu can say it as well.
-------------------------------------------------------
word = "pikaqiu"
Returns: "NO"
Pikachu can't say "pikaqiu" since 'q' does not appear in "pi", "ka", or "chu".
-------------------------------------------------------
word = "chupikachupipichu"
Returns: "YES"
-------------------------------------------------------
word = "duke"
Returns: "NO"
SCREENSHOT OF THE PYTHON CODE :
PYTHON CODE :
def search(string):
while True:
if string.endswith("pi"): # if pi found, then remove pi
string = string[:-(len("pi"))]
continue
if string.endswith("ka"): # if ka found remove ka
string = string[:-(len("ka"))]
continue
if string.endswith("chu"): # if chu found remove cha
string = string[:-(len("chu"))]
continue
else:
break
if string == "":
return "YES"
return "NO"
print("Result :", search(input("Enter the string : "))) # READ THE STRING
SAMPLE OUTPUTS :