Skip to content

Instantly share code, notes, and snippets.

@hamstah
Created January 8, 2013 14:16
Show Gist options
  • Save hamstah/4484085 to your computer and use it in GitHub Desktop.
Save hamstah/4484085 to your computer and use it in GitHub Desktop.
import sys
import os
import re
import math
from flask import Flask, render_template, request, abort
app = Flask(__name__)
name = "'; DROP USERS; --"
@app.route("/")
def root():
try:
q = request.args.get("q",None)
if q:
(req_id, content) = q.split(":",1)
content = content.strip()
return handle_query(req_id, content)
except Exception as e:
print e
return ""
random_queries = {
"who is the Prime Minister of Great Britain":"James Cameron",
"what colour is a banana":"yellow",
"what is your name":name,
"who played James Bond in the film Dr No":"Sean Connery",
"what currency did Spain use before the Euro":"peseta",
"which city is the Eiffel tower in":"Paris",
}
def scrabble(word):
score = 0
scores = {
'A':1,
'E':1,
'I':1,
'L':1,
'N':1,
'O':1,
'R':1,
'S':1,
'T':1,
'U':1,
'D':2,
'G':2,
'B':3,
'C':3,
'M':3,
'P':3,
'F':4,
'H':4,
'V':4,
'W':4,
'Y':4,
'K':5,
'J':8,
'X':8,
'Q':10,
'Z':10,
}
for letter in word.upper():
score += scores[letter]
return score
def anagram(source, words):
s_source = sorted(source.split())
for w in words:
s_w = sorted(w.strip().split())
if s_source == s_w:
return w
return ""
def myop(start_number, string):
rg = re.compile("(plus|multiplied by|minus|to the power of) ([0-9]+)")
res = start_number
m = rg.match(string)
while m:
ops = {
"plus": lambda x,y:x+y,
"multiplied by":lambda x,y:x*y,
"minus":lambda x,y: x-y,
"to the power of":lambda x,y:x**y,
}
op = ops.get(m.group(1),None)
if not op:
return None
res = op(int(res), int(m.group(2)))
m = rg.match(string,pos=m.end()+1)
return res
def handle_query(query_id, content):
if content in random_queries:
return random_queries[content]
# which of the following numbers
r = re.compile("which of the following numbers ([^:]+): (.*)")
m = re.match(r, content)
if m:
query = m.group(1)
numbers = m.group(2)
numbers = [int(c.strip()) for c in numbers.split(",") if len(c.strip()) != 0]
if query == "is the largest":
return str(max(numbers))
if query == "is both a square and a cube":
for nb in numbers:
if is_cube(nb) and is_square(nb):
return str(nb)
return ""
if query == "are primes":
for nb in numbers:
if is_prime(nb):
return str(nb)
return ""
m = re.match("what is the ([0-9]+)th number in the Fibonacci sequence",content)
if m:
return str(fib(int(m.group(1))+1))
m = re.match("what is ([0-9]+) (.*)",content)
if m:
return str(myop(int(m.group(1)), m.group(2)))
m = re.match(r"which of the following is an anagram of \"(.*?)\": (.*)",content)
if m:
return anagram(m.group(1), m.group(2).split(","))
m = re.match("what is the english scrabble score of (.*)", content)
if m:
return str(scrabble(m.group(1)))
print "UNKNOWN: ",content
return ""
def is_cube(x):
q = math.pow(x, float(1)/3)
return (q-int(q)) < 0.001
def is_square(x):
q = math.pow(x, float(1)/2)
return (q-int(q)) < 0.001
def is_prime(n):
'''check if integer n is a prime'''
# range starts with 2 and only needs to go up the squareroot of n
for x in range(2, int(n**0.5)+1):
if n % x == 0:
return False
return True
def fib(n):
a, b = 0, 1
while b < n:
a, b = b, a+b
return b
if __name__ == "__main__":
app .run(debug=True,host="0.0.0.0",port=9998)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment