1 Replies - 4020 Views - Last Post: 08 June 2012 - 06:48 AM

#1 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 833
  • View blog
  • Posts: 2,257
  • Joined: 23-December 08

Look and Say Sequence Generator

Posted 10 May 2012 - 08:12 AM

Description: As with any generator you can iterate over it. This example prints the first 10 values x = lookSayGenerator() for i in range(10): print(next(x)) you could also use it to create a list comprehension: x = lookSayGenerator() y = [next(x) for i in range(10)] This is a look and say sequence generator. Wikipedia has a nice explanation of the look and say sequence here http://en.wikipedia....nd-say_sequence This generator is a much simpler and more straightforward implementation than theirs.
__author__ = "atraub"
__date__= "5/10/2012"

def lookSayGenerator(firstValue=1):
    """a generator to find look say values"""    
    #handle the first two values to avoid coding these unique cases
    yield firstValue
    currentValue = "1"+str(firstValue)
    yield int(currentValue)

    #generrator for all other values
    #currSub and prevSub are the sub-values within the string
    while True:
        nextValue = ""
        prevSub = currentValue[0] 
        count = 1
        #look at each sub value within the current value
        for currentSub in currentValue[1:]:
            #if the currentSub is different from the previous one
            if currentSub != prevSub:
                #add the count and the previous sub value to next Value
                nextValue += str(count) + prevSub
                #reset count
                count = 1
                #only change previous subvalue if the current subValue changes
                prevSub = currentSub
            else:
                #if the previous subvalue and the current one are the same
                count += 1 #increment count 
            
        nextValue += str(count)+ currentSub #handle adding the last subvalue to next value
        yield int(nextValue) #report the next value
        currentValue = nextValue #set things up for the next run through



Is This A Good Question/Topic? 0
  • +

Replies To: Look and Say Sequence Generator

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 833
  • View blog
  • Posts: 2,257
  • Joined: 23-December 08

Re: Look and Say Sequence Generator

Posted 10 May 2012 - 08:12 AM

Description: As with any generator you can iterate over it. This example prints the first 10 values x = lookSayGenerator() for i in range(10): print(next(x)) you could also use it to create a list comprehension: x = lookSayGenerator() y = [next(x) for i in range(10)] This is a look and say sequence generator. Wikipedia has a nice explanation of the look and say sequence here http://en.wikipedia....nd-say_sequence This generator is a much simpler and more straightforward implementation than theirs.
__author__ = "atraub"
__date__= "5/10/2012"

def lookSayGenerator(firstValue=1):
    """a generator to find look say values"""
    #handle the first two values to avoid coding these unique cases
    yield int(firstValue)
    if len(str(firstValue)) == 1:
        currentValue = "1"+str(firstValue)
        yield int(currentValue)	
    else:
        currentValue = str(firstValue)
    
    #generrator for all other values
    #currSub and prevSub are the sub-values within the string
    while True:
        nextValue = ""
        prevSub = currentValue[0]
        count = 1
        #look at each sub value within the current value
        for currentSub in currentValue[1:]:
            #if the currentSub is different from the previous one
            if currentSub != prevSub:
                #add the count and the previous sub value to next Value
                nextValue += str(count) + prevSub
                #reset count
                count = 1
                #only change previous subvalue if the current subValue changes
                prevSub = currentSub
            else:
                #if the previous subvalue and the current one are the same
                count += 1 #increment count

        nextValue += str(count)+ currentSub #handle adding the last subvalue to next value
        yield int(nextValue) #report the next value
        currentValue = nextValue #set things up for the next run through


Was This Post Helpful? 0
  • +
  • -

#3 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 833
  • View blog
  • Posts: 2,257
  • Joined: 23-December 08

Re: Look and Say Sequence Generator

Posted 08 June 2012 - 06:48 AM

fixed a bug that caused the generator to create wrong results when a 2 digit seed was used.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1