HackerRank 'Song of Pi' Solution

Martin Kysel · June 24, 2015

Short Problem Definition:

That’s the value of pi! (Ignoring the floating point) A song is a pi song if the length of its words represent the value of pi.

Song of Pi


time complexity is O(N\*T)

space complexity is O(N)


This problem is straight forward.



PI = map(int, list("31415926535897932384626433833"))

def isPiSong(s):
    for idx, word in enumerate(s):
        if len(word) != PI[idx]:
            return False    
    return True
if __name__ == '__main__':
    t = input()
    for _ in range(t):
    	s = raw_input().split()
        if isPiSong(s):
            print "It's a pi song."
            print "It's not a pi song."

Twitter, Facebook