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

Complexity:

time complexity is O(N\*T)

space complexity is O(N)

Execution:

This problem is straight forward.

Solution:

#!/usr/bin/py

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."
        else:
            print "It's not a pi song."

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.