Created
November 11, 2015 22:28
-
-
Save mdonkers/5786e0c6b82e64db3173 to your computer and use it in GitHub Desktop.
Extreme Startup Game implementation in Python, up and until round 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, session, redirect, url_for, escape, request | |
import math | |
import re | |
app = Flask(__name__) | |
def get_name(question): return "Miel" | |
def get_largest_num(question): | |
numbers = question.split(':')[1].split(',') | |
parsed_numbers = [int(x) for x in numbers] | |
return max(parsed_numbers) | |
def get_sum(question): | |
m = re.search(r'\w+ (\d+) plus (\d+).*', question) | |
first_num = int(m.group(1)) | |
second_num = int(m.group(2)) | |
return first_num + second_num | |
def get_multiplied(question): | |
m = re.search(r'\w+ (\d+) .*\s(\d+).*', question) | |
first_num = int(m.group(1)) | |
second_num = int(m.group(2)) | |
return first_num * second_num | |
def get_square_cube(question): | |
numbers = question.split(':')[1].split(',') | |
parsed_numbers = [int(x) for x in numbers] | |
for num in parsed_numbers: | |
if math.pow(num, 1/2).is_integer() and math.pow(num, 1/3).is_integer(): | |
return num | |
return "" | |
def is_prime(num): | |
for i in range(2,num): | |
if (num % i) == 0: | |
print(num,"is not a prime number") | |
return False | |
else: | |
print(num,"is a prime number") | |
return True | |
def get_primes(question): | |
numbers = question.split(':')[1].split(',') | |
parsed_numbers = [int(x) for x in numbers] | |
primes = [str(num) for num in parsed_numbers if is_prime(num)] | |
return ", ".join(primes) | |
def get_prime_minister(question): | |
return "David Cameron" | |
def get_james_bond(question): | |
return "Sean Connery" | |
def get_eiffel_tower(question): | |
return "Paris" | |
def get_currency_spain(question): | |
return "Peseta" | |
def get_banana(question): | |
return "yellow" | |
def fallback_handler(question): | |
return "No answer yet..." | |
def parse_request_string(question): | |
return question.split(':', maxsplit=1)[1].strip() | |
def get_handler_method_for_question(question): | |
if question.startswith('what is your name'): | |
return get_name | |
if question.startswith('which of the following numbers is the largest'): | |
return get_largest_num | |
if re.match(r'^what is \d* plus \d*$', question): | |
return get_sum | |
if re.match(r'^what is \d* multiplied by \d*$', question): | |
return get_multiplied | |
if question.startswith('which of the following numbers is both a square and a cube:'): | |
return get_square_cube | |
if question.startswith('which of the following numbers are primes'): | |
return get_primes | |
if 'Prime Minister' in question: | |
return get_prime_minister | |
if 'Spain' in question: | |
return get_currency_spain | |
if 'Bond' in question: | |
return get_james_bond | |
if 'Eiffel' in question: | |
return get_eiffel_tower | |
if 'banana' in question: | |
return get_banana | |
else: | |
return fallback_handler | |
def request_handler(parsed_question): | |
handler_method = get_handler_method_for_question(parsed_question) | |
print_unknown = handler_method == fallback_handler | |
if print_unknown: | |
print("Received Question: {}".format(parsed_question)) | |
print("Using handler method: {}".format(str(handler_method))) | |
response = str(handler_method(parsed_question)) | |
print("Response: {}".format(response)) | |
return response | |
@app.route('/') | |
def answer(): | |
q = request.args.get("q", "") | |
parsed_question = parse_request_string(q) | |
return request_handler(parsed_question) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment