A string game is played by the children in a primary school.Given a string S , the children must check if it is a palindrome.A palindrome is a word, phrase, or sequence that reads the same backward as forward.If S is a palindrome , then the children must say the number of characters in that string.If S is not a palindrome , then the children must say the reverse of the string.Write a function game and implement the above scenario.Sample Input1wordSample Output1drowSample Input2popSample Output23
Question
A string game is played by the children in a primary school.Given a string S , the children must check if it is a palindrome.A palindrome is a word, phrase, or sequence that reads the same backward as forward.If S is a palindrome , then the children must say the number of characters in that string.If S is not a palindrome , then the children must say the reverse of the string.Write a function game and implement the above scenario.Sample Input1wordSample Output1drowSample Input2popSample Output23
Solution
Here is a Python function that implements the game described:
def game(S):
# Check if the string is a palindrome
if S == S[::-1]:
# If it is, return the number of characters in the string
return len(S)
else:
# If it's not, return the reverse of the string
return S[::-1]
You can test this function with the provided sample inputs:
print(game("word")) # Expected output: "drow"
print(game("pop")) # Expected output: 3
Similar Questions
Geetha loves playing word games with her friends. One day, she writes a word on the board and challenges her friends to determine whether the given word is a palindrome or not. A palindrome is a word that reads the same backward as forward.She wants to write a program to get a string and find whether the string is a palindrome or not. help her to accomplish the task.Input format :The input consists of a string S1, a word written by Geetha.Output format :The output consists of a single line that states whether the given word is a palindrome or not in the following format:If the word is a palindrome, the program prints: "{word} is a palindrome".If the word is not a palindrome, the program prints: "{word} is not a palindrome".
Your friend is in university and there is a coding challenge going on. A string S of lowercase letters is displayed over the screen. The length of the string is N. An integer M is also displayed over the screen.Your task is write a program to determine if we can make the string Palindrome by performing at most M operations on the provided string
With the Module on Java Characters, Strings, and the StringBuilder that I have provided in our Messenger GC and in Teams. Do this Laboratory Activity:Write a Java program that takes a user input string, reverses it, and then checks if it is a palindrome. Sample Words to check: civic, radar, level, rotor, kayak, madam, and refer
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.Given a string s, return true if it is a palindrome, or false otherwise. Example 1:Input: s = "A man, a plan, a canal: Panama"Output: trueExplanation: "amanaplanacanalpanama" is a palindrome.Example 2:Input: s = "race a car"Output: falseExplanation: "raceacar" is not a palindrome.Example 3:Input: s = " "Output: trueExplanation: s is an empty string "" after removing non-alphanumeric characters.Since an empty string reads the same forward and backward, it is a palindrome. Constraints:1 <= s.length <= 2 * 105s consists only of printable ASCII characters.
You are provided with a code to find if a given string is a palindrome or not.The code is shuffled. Rearrange the code in the correct order.System.out.println(str+" is not a Palindrome");import java.util.Scanner;StringBuffer sb = new StringBuffer(str);public static void main(String[] args) {Scanner sc=new Scanner(System.in);public class Main {elseString str=sc.next();sb.reverse();if(str.equalsIgnoreCase(reverse)) }}System.out.println(str+" is a Palindrome");String reverse=sb.toString();
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.