forked from iambillmccann-zz/ProjectEulerPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (59 loc) · 2.36 KB
/
main.py
File metadata and controls
74 lines (59 loc) · 2.36 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import time
from projecteuler.mathlibrary import utilities
from projecteuler.problems import ProblemFactory
LASTPROBLEM = 13
QUITSTRING = 'Q'
def get_user_input():
""" Get user input from the console
getUserInput is a simple method for reading from the console.
Returns:
A string that represents the user's input.</returns>
"""
return check_user_input(input('What problem shall I run? Or type \'Q\' to quit. '))
def check_user_input(user_input):
""" Validate the user's input
checkUserInput will validate the user's input. This is a very redimentary
validation method. It checks three things ...
1. Checks for the letter 'Q'
2. Checks for an integer
3. Checks that the integer is positive
4. Checks that the integer is no greater than the last problem solved.
These checks assume that problems are solved in order.
Note: If the user enters 'Q', a minus one is returned to the caller.
Args:
userInput: A string entered by the user.
Returns:
A valid integer result.
"""
result = -1
if user_input.upper() == QUITSTRING: result = -1
else:
try:
result = int(user_input)
if result < 1:
print('\nBTW, problem numbers are positive integers.')
result = get_user_input()
elif result > LASTPROBLEM:
print('\nI have only completed problems 1 through {}'.format(LASTPROBLEM))
result = get_user_input()
except ValueError:
print('\nSorry but I did not understand that. Please type the problem number or Q to quit.')
result = get_user_input()
return result
#
# Main Program
#
problem_number = get_user_input()
while (problem_number > 0):
print('problem number is {}'.format(problem_number))
solution = ProblemFactory.get_solution(problem_number)
start_time = time.time()
result = solution.compute()
stop_time = time.time()
total_time = stop_time - start_time
print('\n-----------------------------------------------------------------------')
print('Solution to problem {} = {}'.format(problem_number, result))
print('Execution time was ' + utilities.format_milliseconds(total_time))
print('-----------------------------------------------------------------------')
print()
problem_number = get_user_input()