일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 디스크 컨트롤러
- heap
- Finite Difference Method
- 프로젝트오일러
- Turbulent
- 회귀
- Laminar
- Crank-Nicolson
- 우선순위큐
- 파이썬
- 유체역학
- Compressible Flow
- Python
- Boundary Layers
- 통계학
- 예제
- Statistics
- projecteuler
- FTCS
- regression
- Heat Equation
- Fluids
- 힙
- 프로그래머스
- Fluid Dynamics
- python3
- Navier-Stokes
- programmers
- 이중우선순위큐
- Blasius
- Today
- Total
Sudal's Garage
Project Euler 22 본문
Question:
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?
문제:
여기 5천개 이상의 영문 이름들이 들어있는 46KB짜리 텍스트 파일 names.txt 이 있습니다 (우클릭해서 다운로드 받으세요).
이제 각 이름에 대해서 아래와 같은 방법으로 점수를 매기고자 합니다.
- 먼저 모든 이름을 알파벳 순으로 정렬합니다.
- 각 이름에 대해서, 그 이름을 이루는 알파벳에 해당하는 숫자(A=1, B=2, ..., Z=26)를 모두 더합니다.
- 여기에 이 이름의 순번을 곱합니다.
예를 들어 "COLIN"의 경우, 알파벳에 해당하는 숫자는 3, 15, 12, 9, 14이므로 합이 53, 그리고 정렬했을 때 938번째에 오므로 최종 점수는 938 × 53 = 49714가 됩니다.
names.txt에 들어있는 모든 이름의 점수를 계산해서 더하면 얼마입니까?
Solution:
import time f = open('p022_names.txt', 'r') data = f.read() f.close() data = data.replace('"',"").replace("'","").split(',') data.sort() def alpha2num(n): num = 0 for i in range(len(n)): num += ord(n[i]) - 64 return num result = 0 start = time.time() for i in range(len(data)): result += (i+1) * alpha2num(data[i]) print("{}\nFound in ...{:.2f}s".format(result,time.time() - start))
text 파일을 열어주고 data 에 저장해준다.
alpha2num 함수는 알파벳을 숫자로 변환 후 한 값을 반환한다.
'Programming > Project Euler - python' 카테고리의 다른 글
Project Euler 24 (0) | 2019.03.12 |
---|---|
Project Euler 23 (0) | 2019.03.11 |
Project Euler 21 (0) | 2019.03.09 |
Project Euler 20 (0) | 2019.03.08 |
Project Euler 19 (0) | 2019.03.07 |