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 and/or program:

    The archetype of all humans, their ideal image, is the computer, once it has liberated itself from its creator, man. The computer is the essence of the human being. In the computer, man reaches his completion.
    Friedrich Dürrenmatt (1921–1990)

    In 1862 the congregation of the church forwarded the church bell to General Beauregard to be melted into cannon, “hoping that its gentle tones, that have so often called us to the House of God, may be transmuted into war’s resounding rhyme to repel the ruthless invader from the beautiful land God, in his goodness, has given us.”
    —Federal Writers’ Project Of The Wor, U.S. public relief program (1935-1943)