War game/pythonchallenge

pythonchallenge level10

badcob 2009. 10. 20. 02:33
http://www.pythonchallenge.com/pc/return/bull.html

len(a[30]) = ?

가운데 소 그림을 클릭하면 아래와 같은 페이지가 나온다.

a = [1, 11, 21, 1211, 111221,

리스트 a 에서 30번째 값의 길이를 구하면 될 것 같다.

숫자들로 한번 구글링을 해보았다. 아하 요녀석이었구나. 어디선가 많이 본놈.. 

 Look-and-say sequence (http://en.wikipedia.org/wiki/Look-and-say_sequence)

굳은머리 돌려가며 발코딩으로 해결!
def lookAndSay(x):
    number = ''
    count = 0
    tmp = ''
    length = len(x)-1
    while True:
        if length > -1:
            if not number == '' and x[length] == number:
                count += 1
                length -= 1
                continue
            elif not number == '' and not x[length] == number:
                tmp = str(count) + number + tmp
                number = x[length]
                count = 1
                length -= 1
                continue
            else:
                number = x[length]
                count +=1
                length -= 1
                continue
        else:
            if tmp == '':
                tmp = str(count) + number
            else:
                tmp = str(count) + number + tmp
        break
    return tmp

a = [1]
i = 0
while i < 30:
    temp = lookAndSay(str(a[i]))
    a.append(temp)
    i+=1
print 'i',i,'answer',len(str(a[i]))


http://www.pythonchallenge.com/pc/return/5808.html