파이썬_실전 프로젝트

프로젝트 오일러 17번문제 - 숫자-알파벳 변환후 글자수카운트

 1부터 1000까지의 숫자를 영어표기법으로 바꾼후에, 글자수를 세는 문제입니다.

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

 

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.

 

 

영어표기법중 and를 넣는 부분이 좀 헷갈립니다. 100이상부터는 무조건 백과 십자리 사이에 and를 넣어준다는군요. 숫자 조합을 할 리스트를 미리 만들어 두고 불러다 사용하겠습니다.

one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
thirty
forty
fifty
sixty
seventy
eighty
ninety

 

1000 까지 루프를 돌면서, 하나씩 글자로 바꾸어 주면 될거 같습니다.

Code

파일 불러오기 / 루프문 만들기
filename = 'q017_data.txt'
with open(filename) as file_object:
    lines = file_object.readlines()
print(lines)

i=1
while i <= 20:
    print("loop:",i)
    i += 1
['one\n', 'two\n', 'three\n', 'four\n', 'five\n', 'six\n', 'seven\n', 'eight\n', 'nine\n', 'ten\n', 'eleven\n', 'twelve\n', 'thirteen\n', 'fourteen\n', 'fifteen\n', 'sixteen\n', 'seventeen\n', 'eighteen\n', 'nineteen\n', 'twenty\n', 'thirty\n', 'forty\n', 'fifty\n', 'sixty\n', 'seventy\n', 'eighty\n', 'ninety\n']
loop: 1
loop: 2
loop: 3
loop: 4
loop: 5
loop: 6
loop: 7
loop: 8
loop: 9
loop: 10
loop: 11
loop: 12
loop: 13
loop: 14
loop: 15
loop: 16
loop: 17
loop: 18
loop: 19
loop: 20

 

숫자 -> 단어 변환(20까지)
filename = 'q017_data.txt'
with open(filename) as file_object:
    lines = file_object.readlines()

i=1
while i <= 20:
    print("loop:",i,end='')
    string_i = lines[i-1].rstrip()
    print(" string_i:",string_i)
    i += 1
loop: 1 string_i: one
loop: 2 string_i: two
loop: 3 string_i: three
loop: 4 string_i: four
loop: 5 string_i: five
loop: 6 string_i: six
loop: 7 string_i: seven
loop: 8 string_i: eight
loop: 9 string_i: nine
loop: 10 string_i: ten
loop: 11 string_i: eleven
loop: 12 string_i: twelve
loop: 13 string_i: thirteen
loop: 14 string_i: fourteen
loop: 15 string_i: fifteen
loop: 16 string_i: sixteen
loop: 17 string_i: seventeen
loop: 18 string_i: eighteen
loop: 19 string_i: nineteen
loop: 20 string_i: twenty

 

숫자 -> 단어 (20 까지, 함수로 분리)
filename = 'q017_data.txt'
with open(filename) as file_object:
    lines = file_object.readlines()

def make_string(number,string=''):
    charNum=str(number)
    lenNum=len(charNum)
    if number != 0 and number <= 20:
        string = string+lines[number-1].rstrip()
    return string

i=1;length_i=0;total_length=0
while i<=20:
    print("loop:",i,end='')
    string_i=make_string(i)
    print(" string_i:",string_i,end='')
    length_i=len(string_i)
    print(" length_i:",length_i,end='')
    total_length=total_length+length_i
    print(" total_length:",total_length)
    i += 1
print("total_length :",total_length)
loop: 1 string_i: one length_i: 3 total_length: 3
loop: 2 string_i: two length_i: 3 total_length: 6
loop: 3 string_i: three length_i: 5 total_length: 11
loop: 4 string_i: four length_i: 4 total_length: 15
loop: 5 string_i: five length_i: 4 total_length: 19
loop: 6 string_i: six length_i: 3 total_length: 22
loop: 7 string_i: seven length_i: 5 total_length: 27
loop: 8 string_i: eight length_i: 5 total_length: 32
loop: 9 string_i: nine length_i: 4 total_length: 36
loop: 10 string_i: ten length_i: 3 total_length: 39
loop: 11 string_i: eleven length_i: 6 total_length: 45
loop: 12 string_i: twelve length_i: 6 total_length: 51
loop: 13 string_i: thirteen length_i: 8 total_length: 59
loop: 14 string_i: fourteen length_i: 8 total_length: 67
loop: 15 string_i: fifteen length_i: 7 total_length: 74
loop: 16 string_i: sixteen length_i: 7 total_length: 81
loop: 17 string_i: seventeen length_i: 9 total_length: 90
loop: 18 string_i: eighteen length_i: 8 total_length: 98
loop: 19 string_i: nineteen length_i: 8 total_length: 106
loop: 20 string_i: twenty length_i: 6 total_length: 112
total_length : 112

 

숫자 -> 단어(99까지)
filename = 'q017_data.txt'
with open(filename) as file_object:
    lines = file_object.readlines()

def make_string(number,string=''):
    charNum=str(number)
    lenNum=len(charNum)
    if number<100 and number > 20:
        string=string+lines[int(charNum[0])+17].rstrip()
        number=number-int(charNum[0])*10
        string=make_string(number,string)
    elif number != 0 and number <= 20:
        string = string+lines[number-1].rstrip()
    return string

i=1;length_i=0;total_length=0
while i<100:
    print("loop:",i,end='')
    string_i=make_string(i)
    print(" string_i:",string_i,end='')
    length_i=len(string_i)
    print(" length_i:",length_i,end='')
    total_length=total_length+length_i
    print(" total_length:",total_length)
    i += 1
print("total_length :",total_length)
loop: 1 string_i: one length_i: 3 total_length: 3
loop: 2 string_i: two length_i: 3 total_length: 6
loop: 3 string_i: three length_i: 5 total_length: 11
loop: 4 string_i: four length_i: 4 total_length: 15
loop: 5 string_i: five length_i: 4 total_length: 19
loop: 6 string_i: six length_i: 3 total_length: 22
...
...
loop: 95 string_i: ninetyfive length_i: 10 total_length: 813
loop: 96 string_i: ninetysix length_i: 9 total_length: 822
loop: 97 string_i: ninetyseven length_i: 11 total_length: 833
loop: 98 string_i: ninetyeight length_i: 11 total_length: 844
loop: 99 string_i: ninetynine length_i: 10 total_length: 854
total_length : 854

 

숫자 -> 단어 (999까지)
filename = 'q017_data.txt'
with open(filename) as file_object:
    lines = file_object.readlines()

def make_string(number,string=''):
    charNum=str(number)
    lenNum=len(charNum)
    if lenNum==3:
        string=lines[int(charNum[0])-1].rstrip()+"hundred"
        number = number - int(charNum[0]) * 100
        if number==0:
            return string
        string=make_string(number,string+"and")
    elif number<100 and number > 20:
        string=string+lines[int(charNum[0])+17].rstrip()
        number=number-int(charNum[0])*10
        string=make_string(number,string)
    elif number != 0 and number <= 20:
        string = string+lines[number-1].rstrip()
    return string

i=1;length_i=0;total_length=0
while i<1000:
    print("loop:",i,end='')
    string_i=make_string(i)
    print(" string_i:",string_i,end='')
    length_i=len(string_i)
    print(" length_i:",length_i,end='')
    total_length=total_length+length_i
    print(" total_length:",total_length)
    i += 1
    
print("total_length :",total_length)
print("add 1000(one thousand,11 letters)",total_length+11)
loop: 1 string_i: one length_i: 3 total_length: 3
loop: 2 string_i: two length_i: 3 total_length: 6
loop: 3 string_i: three length_i: 5 total_length: 11
loop: 4 string_i: four length_i: 4 total_length: 15
loop: 5 string_i: five length_i: 4 total_length: 19
loop: 6 string_i: six length_i: 3 total_length: 22

...
...
loop: 993 string_i: ninehundredandninetythree length_i: 25 total_length: 20968
loop: 994 string_i: ninehundredandninetyfour length_i: 24 total_length: 20992
loop: 995 string_i: ninehundredandninetyfive length_i: 24 total_length: 21016
loop: 996 string_i: ninehundredandninetysix length_i: 23 total_length: 21039
loop: 997 string_i: ninehundredandninetyseven length_i: 25 total_length: 21064
loop: 998 string_i: ninehundredandninetyeight length_i: 25 total_length: 21089
loop: 999 string_i: ninehundredandninetynine length_i: 24 total_length: 21113
total_length : 21113
add 1000(one thousand,11 letters) 21124

 

댓글

댓글 본문
작성자
비밀번호
버전 관리
노마드
현재 버전
선택 버전
graphittie 자세히 보기