Look-and-say Sequence - Computer Program

Computer Program

The following Python code generates a look-and-say sequence using lazy evaluation:

def look_and_say(member): while True: yield member breakpoints = ( + [i for i in range(1, len(member)) if member != member] + ) groups = :breakpoints] for i in range(1, len(breakpoints))] member = ''.join(str(len(group)) + group for group in groups) # Print the 10-element sequence beginning with "1" sequence = look_and_say("1") for i in range(10): print sequence.next

The following Javascript code implementation using regular expression to generate first 10 elements of look-and-say sequence:

var number = "1"; var regexp = /(\d)\1*/g; for (var i = 0; i < 10; i++) { console.log(number); number = number.replace(regexp, function(fullmatch, symbol) { return fullmatch.length.toString + symbol; }); }

Read more about this topic:  Look-and-say Sequence

Famous quotes containing the words computer program, computer and/or program:

    Family life is not a computer program that runs on its own; it needs continual input from everyone.
    Neil Kurshan (20th century)

    The analogy between the mind and a computer fails for many reasons. The brain is constructed by principles that assure diversity and degeneracy. Unlike a computer, it has no replicative memory. It is historical and value driven. It forms categories by internal criteria and by constraints acting at many scales, not by means of a syntactically constructed program. The world with which the brain interacts is not unequivocally made up of classical categories.
    Gerald M. Edelman (b. 1928)

    “Why visit the playhouse to see the famous Parisian models, ... when one can see the French damsels, Norma and Diana? Their names have been known on both continents, because everything goes as it will, and those that cannot be satisfied with these must surely be of a queer nature.”
    —For the City of New Orleans, U.S. public relief program (1935-1943)