Created
October 20, 2011 17:42
-
-
Save oc/1301769 to your computer and use it in GitHub Desktop.
es.py
This file contains hidden or 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
| #!/usr/bin/env python | |
| from flask import Flask, request | |
| import re | |
| import os | |
| import math | |
| app = Flask(__name__) | |
| def is_prime(n): | |
| import math | |
| n = abs(n) | |
| i = 2 | |
| while i <= math.sqrt(n): | |
| if n % i == 0: | |
| return False | |
| i += 1 | |
| return True | |
| def fib(n): | |
| a, b = 0, 1 | |
| for i in range(n): | |
| a, b = b, a + b | |
| return a | |
| def matches_for(regex, string, type): | |
| matches = re.search(regex, string) | |
| return [] if matches is None else [type(match) for match in matches.groups()] | |
| NAME="" | |
| def is_cube(n): | |
| a = int(round(n**(1.0/3.0))) | |
| return int(n) == int(a*a*a) | |
| def is_square(n): | |
| return math.sqrt(n) % 1 == 0 | |
| def get_name(): | |
| name = "" | |
| if not os.path.exists("name.txt"): | |
| return "" | |
| with open("name.txt") as f: | |
| name = f.read() | |
| return name | |
| def set_name(name): | |
| with open("name.txt", "w") as f: | |
| f.write(name) | |
| def answer(q): | |
| if "who played James Bond in the film Dr No" in q: | |
| return "Sean Connery" | |
| if "what is the twitter id of the organizer of this dojo" in q: | |
| return "olemartin" | |
| if "who is the Prime Minister of Great Britain" in q: | |
| return "David Cameron" | |
| if "what currency did Spain use before the Euro" in q: | |
| return "Peseta" | |
| if "what is your name" in q: | |
| return "Programming, Motherfucker" | |
| if "what colour is a banana" in q: | |
| return "yellow" | |
| if "which city is the Eiffel tower in" in q: | |
| return "Paris" | |
| num = re.search(r'what is the (\d+).. number in the Fibonacci sequence', q) | |
| if num: | |
| n = int(num.group(1)) | |
| return str(fib(n)) | |
| numbers = matches_for(r'what is (\d+) plus (\d+) multiplied by (\d+)', q, int) | |
| if len(numbers) == 3: | |
| return numbers[0] + numbers[1] * numbers[2] | |
| numbers = matches_for(r'what is (\d+) multiplied by (\d+) plus (\d+)', q, int) | |
| if len(numbers) == 3: | |
| return (numbers[0] * numbers[1]) + numbers[2] | |
| numbers = matches_for(r'what is (\d+) to the power of (\d+)', q, int) | |
| if len(numbers) == 2: | |
| return numbers[0] ** numbers[1] | |
| numbers = matches_for(r'what is (\d+) plus (\d+) plus (\d+)', q, int) | |
| if len(numbers) == 3: | |
| return numbers[0] + numbers[1] + numbers[2] | |
| numbers = matches_for(r'what is (\d+) plus (\d+)', q, int) | |
| if len(numbers) == 2: | |
| return numbers[0] + numbers[1] | |
| numbers = matches_for(r'which of the following numbers is the largest: ([\d,\s]+)', q, str) | |
| if len(numbers) == 1: | |
| return max(map(int, numbers[0].split(", "))) | |
| numbers = matches_for(r'which of the following numbers is both a square and a cube: ([\d,\s]+)', q, str) | |
| if len(numbers) == 1: | |
| for number in map(int, numbers[0].split(", ")): | |
| if is_square(number) and is_cube(number): | |
| return number | |
| return "" | |
| numbers = matches_for(r'which of the following numbers are primes: ([\d,\s]+)', q, str) | |
| if len(numbers) == 1: | |
| res = "" | |
| for number in map(int, numbers[0].split(", ")): | |
| if is_prime(number): | |
| res = str(number) + ", " + res | |
| return res.strip(', ') | |
| numbers = matches_for(r'what is (\d+) minus (\d+)', q, int) | |
| if len(numbers) == 2: | |
| return numbers[0] - numbers[1] | |
| numbers = matches_for(r'what is (\d+) multiplied by (\d+)', q, int) | |
| if len(numbers) == 2: | |
| return numbers[0] * numbers[1] | |
| name = matches_for(r"my name is (\w+). what is my name", q, str) | |
| if len(name) == 1: | |
| set_name(name[0]) | |
| return name[0] | |
| if "what is my name" in q: | |
| return get_name() | |
| numbers = matches_for(r'the product of (\d+) and (\d+)', q, int) | |
| if numbers: | |
| return numbers[0] * numbers[1] | |
| foo = re.search(r'the product of (\w+) and (\w+)', q) | |
| if foo: | |
| return foo.group(1) + foo.group(2) | |
| return "" | |
| @app.route("/") | |
| def root(): | |
| if request.remote_addr != "10.0.21.59": | |
| print "RED ALERT!!!!!!!!" | |
| q = request.args.get("q", "") | |
| print "Question: ", q | |
| a = str(answer(q)) | |
| print "Answer: ", a | |
| if a == "": | |
| print "#############################" | |
| print "DON'T KNOW?: " + q | |
| print "#############################" | |
| return a | |
| if __name__ == "__main__": | |
| app.run(debug=True, host="0.0.0.0", port=31337) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment