Commit 16765a7a532e35394fd1788b4a7fefe372b8544f

Authored by Imanol-Mikel Barba Sabariego
1 parent 6be964e1

Added solution for problem 17

Showing 1 changed file with 22 additions and 67 deletions
1   -import sys;
2   -
3   -def getLetterNum(num):
4   - switch(num):
5   - if(num == 1):
6   - return 3;
7   -
8   - elif(num == 2):
9   - return 3;
10   -
11   - elif(num == 3):
12   - return 5;
13   -
14   - elif(num == 4):
15   - return 3;
16   -
17   - elif(num == 5):
18   - return 3;
19   -
20   - elif(num == 6):
21   - return 3;
22   -
23   - elif(num == 7):
24   - return 3;
25   -
26   - elif(num == 8):
27   - return 3;
28   -
29   - elif(num == 9):
30   - return 3;
31   -
32   - elif(num == 10):
33   - return 3;
34   -
35   - elif(num == 11):
36   - return 3;
37   - elif(num == 2):
38   - return 3;
39   - elif(num == 2):
40   - return 3;
41   - elif(num == 2):
42   - return 3;
43   - elif(num == 2):
44   - return 3;
45   - elif(num == 2):
46   - return 3;
47   - elif(num == 2):
48   - return 3;
49   - elif(num == 2):
50   - return 3;
51   - elif(num == 2):
52   - return 3;
53   - elif(num == 2):
54   - return 3;
55   - elif(num == 2):
56   - return 3;
57   -
58   -
59   -def countLetters(start, end):
60   - count = 0;
61   - for i in range(start, end+1):
62   - count += getLetterNum(i);
63   - return count;
64   -
65   -start = int(sys.argv[1]);
66   -end = int(sys.argv[2]);
67   -print "Result is " + str(countLetters(start,end));
  1 +#!/usr/bin/python
  2 +
  3 +S = [0,3,3,5,4,4,3,5,5,4,3,6,6,8,8,7,7,9,8,8]
  4 +D = [0,3,6,6,5,5,5,7,6,6]
  5 +H = 7
  6 +T = 8
  7 +
  8 +total = 0
  9 +for i in range(1,1000):
  10 + c = i % 10 # singles digit
  11 + b = ((i % 100) - c) / 10 # tens digit
  12 + a = ((i % 1000) - (b * 10) - c) / 100 # hundreds digit
  13 +
  14 + if a != 0:
  15 + total += S[a] + H # "S[a] hundred
  16 + if b != 0 or c != 0: total += 3 # "and"
  17 + if b == 0 or b == 1: total += S[b * 10 + c]
  18 + else: total += D[b] + S[c]
  19 +
  20 +total += S[1] + T
  21 +
  22 +print "Result is: " + str(total)
... ...