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. $abc\rightarrow cba$.  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.

Example 

$s=\textbf{'1mnop'}$  

The ordinal values of the charcters are $[108,109,110,111,112]$.  $s_{reverse}=\textbf{'ponmI'}$ and the ordinals are $[112,111,110,109,108]$.  The absolute differences of the adjacent elements for both strings are $[1,1,1,1]$, so the answer is Funny.

Function Description

Complete the funnyString function in the editor below.   

funnyString has the following parameter(s):  

string s: a string to test  

Returns  

string: either Funny or Not Funny

Input Format

The first line contains an integer $\textit{q}$, the number of queries. 

The next $\textit{q}$ lines each contain a string, $\boldsymbol{\mathrm{~S~}}$.   

Constraints

$1\leq q\leq10$    
$2\leq\text{length of}\ s\leq10000$   

Sample Input
STDIN   Function
-----   --------
2       q = 2
acxz    s = 'acxz'
bcxz    s = 'bcxz'

Sample Output
Funny
Not Funny

Explanation

Let $\textbf{r}$ be the reverse of $\boldsymbol{\mathrm{~S~}}$.

Test Case 0: 

$s=\textsf{acxz}$, $r=\textbf{zxca}$ 

Corresponding ASCII values of characters of the strings: 

$s=[97,99,120,122]$ and $r=[122,120,99,97]$ 

For both the strings the adjacent difference list is [2, 21, 2].  

Test Case 1: 

$s=\textbf{bcxz}$, $r=\textbf{zxcb}$ 

Corresponding ASCII values of characters of the strings: 

$s=[98,99,120,122]$ and $r=[122,120,99,98]$ 

The difference list for string $\boldsymbol{\mathrm{~S~}}$ is [1, 21, 2] and for string $\textbf{r}$ is [2, 21, 1].

Source: hackerrank | Difficulty: EASY
Tags: String algorithms


Solutions:
from sys import stdin
import os
FUNNY_STR = 'Funny'
NOT_FUNNY_STR = 'Not Funny'

def reverse(str_to_reverse):
	i = len(str_to_reverse) - 1
	new_reversed_str = ''
	while i >= 0:
		new_reversed_str += str_to_reverse[i]
		i -= 1
	return new_reversed_str

def is_funny(test_str):
	i = 1
	reversed_test_str = reverse(test_str)
	while i < len(test_str):
		a = abs(ord(test_str[i]) - ord(test_str[i - 1]))
		b = abs(ord(reversed_test_str[i]) - ord(reversed_test_str[i - 1]))
		if a != b:
			return False
		i += 1
	return True

def is_funny_str(is_funny_bool):
	if is_funny_bool:
		return FUNNY_STR
	else:
		return NOT_FUNNY_STR

def main():
	line_list = [line.rstrip(os.linesep) for line in stdin.readlines()][1:]
	for line in line_list:
		print(is_funny_str(is_funny(line)))
main()