#!/usr/bin/python

import string
import sys
import random

snum = {
    1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five', 6 : 'six', 
    7 : 'seven', 8 : 'eight', 9 : 'nine', 10 : 'ten', 11 : 'eleven', 
    12 : 'twelve', 13 : 'thirteen', 14 : 'fourteen', 15 : 'fifteen', 
    16 : 'sixteen', 17 : 'seventeen', 18 : 'eighteen', 19 : 'nineteen', 
    20 : 'twenty', 30 : 'thirty', 40 : 'forty', 50 : 'fifty', 60 : 'sixty', 
    70 : 'seventy', 80 : 'eighty', 90 : 'ninety'
    }

def spelledNumber(x):
    if x >= 100:
        print "must be 99 or less"  
        sys.exit(1);
    elif x <= 20:
        return snum[x]
    else:
        tens = int(x / 10) * 10
        if x - tens == 0:
            return snum[x]
        else:
            return snum[tens] + "-" + snum[x - tens]

def sIfPlural(x):
    if x >= 2:
        return "s, "
    else:
        return ", "

def checkIfTrue(s):
    real = {}
    for i in string.lowercase:
        real[i] = string.count(s, i)      
    if AtoZ == real:
        print "Found it:"
        print s
        sys.exit(0)
    else:
        for l in AtoZ.items():
            x = string.count(s, l[0])
            y = l[1]
            AtoZ[l[0]] = randomizer(x, y)
    return AtoZ

def randomizer(x, y):
    '''randomized Robisonizing'''
    if x == y:
        return x
    if x > y:
        x, y = y, x
    return random.randint(x, y) 
    
seed="darrens python panagram program found this sentence which contains exactly and"
AtoZ = {}
for i in string.lowercase: AtoZ[i] = 1

while 1:
    for x in range(1, 10000):
        s = seed
        for c in string.lowercase:
            s += "%s '%s'%s" % (spelledNumber(AtoZ[c]), c, sIfPlural(AtoZ[c]))
        AtoZ = checkIfTrue(s)
    print "\t10K blip..."

'''
  panagram.py written by Darren Kirby
  Based on rrob.pl written by TANAKA Tomoyuki
  see http://www.cs.indiana.edu/~tanaka/GEB/pangram.txt

  This program found (in ~480000 iterations):

  Darren's python panagram program found this sentence which contains exactly nine 'a's, one 'b', five 'c's, four 'd's, thirty-six 'e's, 
  eleven 'f's, three 'g's, ten 'h's, fifteen 'i's, one 'j', one 'k', three 'l's, three 'm's, twenty-nine 'n's, sixteen 'o's, four 'p's, 
  one 'q', fifteen 'r's, thirty-one 's's, twenty-one 't's, six 'u's, four 'v's, four 'w's, six 'x's, seven 'y's, and one 'z'.
'''