Created
August 15, 2015 07:43
-
-
Save viveksyngh/59ef6e88312d62746f3f to your computer and use it in GitHub Desktop.
Generate nth count-and-say sequence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| __author__ = 'Vivek' | |
| #This will Generate count-and-say sequence | |
| def countAndSay(A): | |
| seq = '' | |
| prevSeq = '1' | |
| if A==1 : | |
| return '1' | |
| for i in range(0, A-1) : | |
| seq = '' | |
| count = 1 | |
| for j in range(0, len(prevSeq)) : | |
| if j < len(prevSeq)-1 and prevSeq[j] == prevSeq[j+1] : | |
| count = count + 1 | |
| elif j < len(prevSeq)-1 and prevSeq[j] != prevSeq[j+1] : | |
| seq += str(count) | |
| seq += str(prevSeq[j]) | |
| count = 1 | |
| elif j == len(prevSeq)-1 : | |
| seq += str(count) + str(prevSeq[j]) | |
| prevSeq = seq | |
| return seq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment