Hackerrank - Funny String Solution
1 min read

Hackerrank - Funny String Solution

Hackerrank - Funny String Solution

In this challenge, you will determine whether a string is funny or not. To determine whether a string is funny, create a copy of the string in reverse e.g. . Iterating through each string, compare the absolute difference in the ascii values of the characters at positions 0 and 1, 1 and 2 and so on to the end. If the list of absolute differences is the same for both strings, they are funny.

Determine whether a give string is funny. If it is, return Funny, otherwise return Not Funny.

For example, given the string , the ordinal values of the charcters are .  and the ordinals are . The absolute differences of the adjacent elements for both strings are , so the answer is Funny.

Function Description

Complete the funnyString function in the editor below. For each test case, it should return a string, either Funny or Not Funny.

funnyString has the following parameter(s):

  • s: a string to test

Input Format

The first line contains an integer , the number of queries.
The next  lines each contain a string, .


Output Format

For each string  print whether it is Funny or Not Funny on a new line.

Sample Input


Sample Output

Not Funny


You can use  to store the reverse of .

Test Case 0:

Corresponding ASCII values of characters of the strings:
For both the strings the adjacent difference list is [2, 21, 2] so we print Funny.

Test Case 1:

Corresponding ASCII values of characters of the strings:
The adjacent difference list for string  is [1, 21, 2] and for string  it is [2, 21, 1]. Since they are not the same we print Not Funny.

Solution in Python

def funnyString(s):
    s = [abs(ord(s[i])-ord(s[i+1])) for i in range(len(s)-1)]
    return "Funny" if list(reversed(s))== s else "Not Funny"

for _ in range(int(input())):

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.