HackerRank ‘Song of Pi’ Solution

H
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.

Link

Song of Pi

Complexity:

time complexity is O(N*T)

space complexity is O(N)

Execution:

 

This problem is straight forward.

Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/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."

About the author

Add comment

Archives

Categories