Skip to main content
deleted 62 characters in body; edited tags; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

"Funny String" problem from Hackerrank

Suppose you have a String, \$S\$ of length \$N\$ indexed from \$0\$ to \$N-1\$. You also have some String, \$R\$, that is the reverse of \$S\$, where \$S\$ is funny if the condition is \$|S[i] - S[i-1]| = |R[i] - R[i-i]|\$\$\left|S[i] - S[i-1]\right| = \left|R[i] - R[i-1]\right|\$ is true for all \$i \in [1, N-1]\$.

I code mainly in Java. I'm very new to pythonPython, so I would appreciate feedback on how I can make this code more pythonicPythonic, cleaner and more efficient in general. Thanks!

Solution

import math

def isFunny(word):
    i = 0
    length = len(word)
    arr = []
    revarr = []
    for i in range(0,math.ceil(length/2)):
        sdiff = abs(ord(word[i])-ord(word[i+1]))
        rdiff = abs(ord(word[length-i-1])-ord(word[length-i-2]))
        if sdiff == rdiff:
               continue
        else:
               return False
    return True

    if __name__ == "__main__":
    n = int(input())
    for _ in range(n):
        word = input();
        if isFunny(word):
            print("Funny")
        else:
            print("Not Funny")

Any feedback would be appreciated!

"Funny String" problem from Hackerrank

Suppose you have a String, \$S\$ of length \$N\$ indexed from \$0\$ to \$N-1\$. You also have some String, \$R\$, that is the reverse of \$S\$, where \$S\$ is funny if the condition is \$|S[i] - S[i-1]| = |R[i] - R[i-i]|\$ is true for all \$i \in [1, N-1]\$.

I code mainly in Java. I'm very new to python, so I would appreciate feedback on how I can make this code more pythonic, cleaner and more efficient in general. Thanks!

Solution

import math

def isFunny(word):
    i = 0
    length = len(word)
    arr = []
    revarr = []
    for i in range(0,math.ceil(length/2)):
        sdiff = abs(ord(word[i])-ord(word[i+1]))
        rdiff = abs(ord(word[length-i-1])-ord(word[length-i-2]))
        if sdiff == rdiff:
               continue
        else:
               return False
    return True

    if __name__ == "__main__":
    n = int(input())
    for _ in range(n):
        word = input();
        if isFunny(word):
            print("Funny")
        else:
            print("Not Funny")

Any feedback would be appreciated!

"Funny String" problem from Hackerrank

Suppose you have a String, \$S\$ of length \$N\$ indexed from \$0\$ to \$N-1\$. You also have some String, \$R\$, that is the reverse of \$S\$, where \$S\$ is funny if the condition is \$\left|S[i] - S[i-1]\right| = \left|R[i] - R[i-1]\right|\$ is true for all \$i \in [1, N-1]\$.

I code mainly in Java. I'm very new to Python, so I would appreciate feedback on how I can make this code more Pythonic, cleaner and more efficient in general.

import math

def isFunny(word):
    i = 0
    length = len(word)
    arr = []
    revarr = []
    for i in range(0,math.ceil(length/2)):
        sdiff = abs(ord(word[i])-ord(word[i+1]))
        rdiff = abs(ord(word[length-i-1])-ord(word[length-i-2]))
        if sdiff == rdiff:
               continue
        else:
               return False
    return True

if __name__ == "__main__":
    n = int(input())
    for _ in range(n):
        word = input();
        if isFunny(word):
            print("Funny")
        else:
            print("Not Funny")
added 110 characters in body
Source Link
SylvainD
  • 29.8k
  • 1
  • 49
  • 93

"Funny String" problem from Hackerrank

Suppose you have a String, \$S\$ of length \$N\$ indexed from \$0\$ to \$N-1\$. You also have some String, \$R\$, that is the reverse of \$S\$, where \$S\$ is funny if the condition is \$|S[i] - S[i-1]| = |R[i] - R[i-i]|\$ is true for all \$i \in [1, N-1]\$.

I code mainly in Java. I'm very new to python, so I would appreciate feedback on how I can make this code more pythonic, cleaner and more efficient in general. Thanks!

Solution

import math

def isFunny(word):
    i = 0
    length = len(word)
    arr = []
    revarr = []
    for i in range(0,math.ceil(length/2)):
        sdiff = abs(ord(word[i])-ord(word[i+1]))
        rdiff = abs(ord(word[length-i-1])-ord(word[length-i-2]))
        if sdiff == rdiff:
               continue
        else:
               return False
    return True

    if __name__ == "__main__":
    n = int(input())
    for _ in range(n):
        word = input();
        if isFunny(word):
            print("Funny")
        else:
            print("Not Funny")

Any feedback would be appreciated!

Suppose you have a String, \$S\$ of length \$N\$ indexed from \$0\$ to \$N-1\$. You also have some String, \$R\$, that is the reverse of \$S\$, where \$S\$ is funny if the condition is \$|S[i] - S[i-1]| = |R[i] - R[i-i]|\$ is true for all \$i \in [1, N-1]\$.

I code mainly in Java. I'm very new to python, so I would appreciate feedback on how I can make this code more pythonic, cleaner and more efficient in general. Thanks!

Solution

import math

def isFunny(word):
    i = 0
    length = len(word)
    arr = []
    revarr = []
    for i in range(0,math.ceil(length/2)):
        sdiff = abs(ord(word[i])-ord(word[i+1]))
        rdiff = abs(ord(word[length-i-1])-ord(word[length-i-2]))
        if sdiff == rdiff:
               continue
        else:
               return False
    return True

    if __name__ == "__main__":
    n = int(input())
    for _ in range(n):
        word = input();
        if isFunny(word):
            print("Funny")
        else:
            print("Not Funny")

Any feedback would be appreciated!

"Funny String" problem from Hackerrank

Suppose you have a String, \$S\$ of length \$N\$ indexed from \$0\$ to \$N-1\$. You also have some String, \$R\$, that is the reverse of \$S\$, where \$S\$ is funny if the condition is \$|S[i] - S[i-1]| = |R[i] - R[i-i]|\$ is true for all \$i \in [1, N-1]\$.

I code mainly in Java. I'm very new to python, so I would appreciate feedback on how I can make this code more pythonic, cleaner and more efficient in general. Thanks!

Solution

import math

def isFunny(word):
    i = 0
    length = len(word)
    arr = []
    revarr = []
    for i in range(0,math.ceil(length/2)):
        sdiff = abs(ord(word[i])-ord(word[i+1]))
        rdiff = abs(ord(word[length-i-1])-ord(word[length-i-2]))
        if sdiff == rdiff:
               continue
        else:
               return False
    return True

    if __name__ == "__main__":
    n = int(input())
    for _ in range(n):
        word = input();
        if isFunny(word):
            print("Funny")
        else:
            print("Not Funny")

Any feedback would be appreciated!

Source Link
nyim
  • 33
  • 4

Hackerrank Funny String python solution

Suppose you have a String, \$S\$ of length \$N\$ indexed from \$0\$ to \$N-1\$. You also have some String, \$R\$, that is the reverse of \$S\$, where \$S\$ is funny if the condition is \$|S[i] - S[i-1]| = |R[i] - R[i-i]|\$ is true for all \$i \in [1, N-1]\$.

I code mainly in Java. I'm very new to python, so I would appreciate feedback on how I can make this code more pythonic, cleaner and more efficient in general. Thanks!

Solution

import math

def isFunny(word):
    i = 0
    length = len(word)
    arr = []
    revarr = []
    for i in range(0,math.ceil(length/2)):
        sdiff = abs(ord(word[i])-ord(word[i+1]))
        rdiff = abs(ord(word[length-i-1])-ord(word[length-i-2]))
        if sdiff == rdiff:
               continue
        else:
               return False
    return True

    if __name__ == "__main__":
    n = int(input())
    for _ in range(n):
        word = input();
        if isFunny(word):
            print("Funny")
        else:
            print("Not Funny")

Any feedback would be appreciated!