Python Challenge [10]

Ah word puzzles…

For this challenge, the most significant part is picking up on the sequence. After that, the solution can be reached through a straightforward loop with a check related to the sequence. The puzzle is more about the thought behind a process than learning to use a specific library.

From the source, we’re given the first elements of a sequence.
1, 11, 21, 1211, 111221,

Having seen this type of sequence before, it happened to seem easy – not the case for a couple friends of mine and it definitely sparked an interesting discussion about how different people who are good at recognizing patterns go about that process of discovery. If you’re stuck, I recommend reading the sequence aloud.

As for the code to solve the sequence:


import StringIO

a = ['1']
for i in range(30):
	n = a[-1]+'?'
	next = []
	start = 0
	for end in range(len(n)):
		if n[end] != n[start]:
			next.append(str(end-start)+ n[start])
			start = end

	a.append(''.join(next))


print len(a[-1])