Skip to content

Instantly share code, notes, and snippets.

@gsathya
Created June 12, 2012 03:13
Show Gist options
  • Save gsathya/2914636 to your computer and use it in GitHub Desktop.
Save gsathya/2914636 to your computer and use it in GitHub Desktop.
# usage - $python trainglewords.py words.txt
import sys
from math import *
file = open(sys.argv[1], 'r')
data = file.read()
file.close()
alpha = {}
count =0
for i in range(ord('a'), ord('z')+1):
alpha[chr(i)] = i-96
for word in data.split(','):
word = word.lower().replace('"','')
sum = 0
for char in word:
sum += alpha[char]
sum = sum*2
"""
solve the quadratic equation
t = (n)(n+1)/2 => 2t = (n)(n+1) => 2t = n*n +n => n*n + n - 2t =0
"""
a=1
b=1
c=-sum
d=b*b-4*a*c #finds the discriminant
if d>0: #when discriminant is positive
x=((-b)+sqrt(d))/(2*a)
y=((-b)-sqrt(d))/(2*a)
elif d==0: #when discriminant is zero
x=(-b)/(2*a)
y=(-b)/(2*a)
else: #when discriminant is negative
x=(-b)/(2*a)
y=(sqrt(d*(-1)))/(2*a)
no_x = str(x)
no_y = str(y)
no_x = no_x.split('.', 1)[1]
no_y = no_y.split('.', 1)[1]
if x>0 and no_x=='0': count +=1
elif y>0 and no_y=='0': count +=1
print count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment