Skip to content

Instantly share code, notes, and snippets.

@shnya
Created May 25, 2011 14:56
Show Gist options
  • Save shnya/991122 to your computer and use it in GitHub Desktop.
Save shnya/991122 to your computer and use it in GitHub Desktop.
time.cgi
#!/usr/bin/env python
import math
def f(MP,t):
return MP * (0.3 + ((0.7 * 75.0 * 75.0) / (10.0 * t * t + 75.0 * 75.0)))
def check(y,SCORE,MP):
if SCORE > f(MP,y):
return True
else:
return False
def solve(SCORE,MP):
lb = 0.0
ub = 75.1
if MP > 1500 or MP < 0:
return -1,-1
if SCORE > MP or SCORE < 0:
return -1,-1
for x in xrange(1,100000):
y = (ub + lb) / 2.0
if abs(ub-lb) < 0.01:
minu = int(math.floor(y))
sec = int((y - minu) * 60)
return minu,sec
elif check(y,SCORE,MP):
ub = y
else:
lb = y
return -1,-1
if __name__ == '__main__':
html = '''Content-Type: text/html
<html>
<head>
<title>TopCoder SRM Score->Time Calculator</title>
</head>
<body>
<h2>TopCoder SRM Score->Time Calculator</h2>
<form method="GET" action="./time.cgi">
<table><tr>
<td>Max Points: </td><td><input type="text" name="mp" value="%s" /></td>
</tr><tr>
<td>Your Score: </td><td><input type="text" name="score" value="%s" /></td>
</tr></table>
<input type="submit" value="calc"/>
</form>
<br>
%s
</body>
</html>'''
import cgi
form = cgi.FieldStorage()
mp_str = ""
score_str = ""
message = ""
if form.has_key('mp') and form.has_key('score'):
try:
mp = float(form['mp'].value)
score = float(form['score'].value)
except:
mp = 0
score = 0
mp_str = "%d" % mp if mp != 0 else ""
score_str = str(score) if score != 0 else ""
minu,sec = solve(score,mp)
if minu > 0:
message = "Your Coding Time: %02dm%02ds<br/>" % (minu, sec)
else:
message = "Wrong Input!<br/>"
print html % (mp_str, score_str, message),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment