patternpythonMinor
Funny string python solution
Viewed 0 times
solutionstringpythonfunny
Problem
Problem Statement
Suppose you have a string \$S\$ which has length \$N\$ and is indexed from \$0\$ to \$N−1\$. String \$R\$ is the reverse of the string \$S\$. The string \$S\$ is funny if the condition \$|S_i−S_{i−1}|=|R_i−R_{i−1}|\$ is true for every \$i\$ from \$1\$ to \$N−1\$.
(Note: Given a string \$str\$, \$str_i\$ denotes the ascii value of the \$i\$th character (0-indexed) of \$str\$. \$|x|\$ denotes the absolute value of an integer \$x\$)
Example
Solution
Notes
Suppose you have a string \$S\$ which has length \$N\$ and is indexed from \$0\$ to \$N−1\$. String \$R\$ is the reverse of the string \$S\$. The string \$S\$ is funny if the condition \$|S_i−S_{i−1}|=|R_i−R_{i−1}|\$ is true for every \$i\$ from \$1\$ to \$N−1\$.
(Note: Given a string \$str\$, \$str_i\$ denotes the ascii value of the \$i\$th character (0-indexed) of \$str\$. \$|x|\$ denotes the absolute value of an integer \$x\$)
Example
is_funny("acxz") returns Trueis_funny("bcxz") returns FalseSolution
def is_palindrome(ary):
return ary == ary[::-1]
def create_difference_list(s):
res = []
for i in range(1, len(s)):
res.append(abs(ord(s[i]) - ord(s[i-1])))
return res
def is_happystr(s):
return is_palindrome(create_difference_list(s))Notes
- Have used slow version of palindrome check assuming string is not going to be too long. Hope it doesn't affect performance that much.
- Approached problem via functional style, could have done all the computation in the same loop but is it a good practice?
Solution
Answering your specific questions:
Have used slow version of palindrome check assuming string is not going to be too long. Hope it doesn't affect performance that much.
You made a very good choice. This solution is simpler and more readable, you may switch to a new faster implementation later iff performance becomes a problem and you are sure
Approached problem via functional style, could have done all the computation in the same loop but is it a good practice?
To be precise on terminology, you used an imperative style with subroutines. I think that the use of subroutines ("functions") like you do greatly improves readability, so well done.
You may re-factor into functions further by writing a function to return pairs from a list and calling it from
Have used slow version of palindrome check assuming string is not going to be too long. Hope it doesn't affect performance that much.
You made a very good choice. This solution is simpler and more readable, you may switch to a new faster implementation later iff performance becomes a problem and you are sure
is_palindrome is the problem.Approached problem via functional style, could have done all the computation in the same loop but is it a good practice?
To be precise on terminology, you used an imperative style with subroutines. I think that the use of subroutines ("functions") like you do greatly improves readability, so well done.
You may re-factor into functions further by writing a function to return pairs from a list and calling it from
create_difference_listContext
StackExchange Code Review Q#104892, answer score: 5
Revisions (0)
No revisions yet.