Skip to content

Instantly share code, notes, and snippets.

@viveksyngh
Created August 15, 2015 07:43
Show Gist options
  • Select an option

  • Save viveksyngh/59ef6e88312d62746f3f to your computer and use it in GitHub Desktop.

Select an option

Save viveksyngh/59ef6e88312d62746f3f to your computer and use it in GitHub Desktop.
Generate nth count-and-say sequence
__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