codeblock

2016年3月30日 星期三

Python Challenge level 10

http://www.pythonchallenge.com/pc/return/bull.html (login:huge/file)

- Click the bull in the picture, you will see "a = [1, 11, 21, 1211, 111221, "
- This is so called "look and say sequence", please refer to https://en.wikipedia.org/wiki/Look-and-say_sequence

- Apparently I should calculate the 31st item of look-and-say sequence.

a = range(1, 32)
for i in range(len(a)-1):
 string = str(a[i])
 chars=[]
 occurs=[] 
 next_str = ""
 for j in range(len(string)):
  if len(chars)==0 and len(occurs) == 0:
   chars.append(string[j])
   occurs.append(1)
  elif string[j] == chars[-1]:
   occurs[-1] += 1 
  else:
   next_str = next_str + str(occurs[-1])
   next_str = next_str + chars[-1]
   chars.append(string[j])
   occurs.append(1)
 next_str =  next_str + str(occurs[-1])
 next_str = next_str + chars[-1]
 a[i+1] = int(next_str)
print len(str(a[30]))
- The answer is "5808"

沒有留言:

張貼留言