Showing posts with label find. Show all posts
Showing posts with label find. Show all posts

Friday, October 15, 2010

Python - Anagram generator

# python anagram generator 
# 
# This anagram generator uses a 
# text file as a reference for 
# appropriate words. 
# 
# The word file I used is in the 
# following format (each word has 
# it's own line): 
# 
# act 
# addition 
# adjustment 
# advertisement 
# after 
# again 
# against 
# ... and on and on... 
# 
 
def isAnagramOf(attempt, original):
    # all the same case 
    attempt = attempt.strip()
    if len(attempt) < 1: # only actual words 
        return False
    attempt, original = attempt.lower(), original.lower()
    for character in attempt:
        position = original.find(character)
        if (position == -1):
            return False
        original = original.replace(character, '', 1)
    return True
 
def getAnagramsFor(text):
    anagrams = []
    wordlist = open("wordlist.txt", 'r')
    for line in wordlist:
        line = line.strip("\n") #strip the carriage return 
        if isAnagramOf(line, text):
            anagrams.append(line)
    return anagrams
 
matching_anagrams = getAnagramsFor("pythonic prose")
print(len(matching_anagrams), "total anagrams generated")
for ana in matching_anagrams:
    print(ana)
 
 
# my output: 
# 
# 66 total anagrams generated 
# chest 
# chin 
# copper 
# copy 
# cry 
# he 
# history 
# hope 
# ... skip a few ... 
# theory 
# thin 
# this 
# tin 
# to 
# toe 
# top 
# yes 
 

Python - Anagram detector

# A python Anagram detector 
# manipulate anagrams with python 
# 
# Python could no doubt solve this in 
# many ways.... here is one: 
 
 
 
# determine whether a string is an anagram 
# of another string 
def isAnagramOf(attempt, original):
    # all the same case 
    attempt, original = attempt.lower(), original.lower()
    for character in attempt:
        position = original.find(character)
        if (position == -1):
            return False
        original = original.replace(character, '', 1)
    return True
 
 
original = "Texas Ranger" 
 
 
print("Is Rage an anagram of", original, "=",
      isAnagramOf("Anger", original))
print("Is Extra Angers an anagram of", original, "=",
      isAnagramOf("Extra Angers", original))
print("Is Extra Angered an anagram of", original, "=",
      isAnagramOf("Extra Angered", original))
 
#   my output: 
#   Is Rage an anagram of Texas Ranger = True 
#   Is Extra Angers an anagram of Texas Ranger = True 
#   Is Extra Angered an anagram of Texas Ranger = False 
 
 

Tuesday, October 27, 2009

Python - break a large mysql dump into small dumps

# created a dumpfile from my mysql db
# and found that it was too large to
# upload to my new db host.
 
# this python script breaks up the database
# into smaller pieces that you can more
# easily import through phpmyadmin
# (if I'd only had shell access I wouldn't
# have this problem at all!)
 
# indicates a new table is about to be
# created
dlmtr = "-- Table structure for table"
 
wholeFile = open("myDBDump.sql")
fileN = 0
oFile = open(str(fileN) + ".sql", 'w')
reducing = True
 
for line in wholeFile:
    if line.find(dlmtr) > -1:
        # this is the seam for the next file
        print "starting new file"
        oFile.close()
        fileN += 1
        oFile = open(str(fileN) + ".sql", 'w')
    oFile.write(line)
 
oFile.close()
print "Done"
 
 

Monday, September 28, 2009

Python - pig latin generator

 
 
 
def makePigLatin(word):
    """ convert one word into pig latin """ 
    m  = len(word)
    vowels = "a", "e", "i", "o", "u", "y" 
    # short words are not converted 
    if m<3 or word=="the":
        return word
    else:
        for i in vowels:
            if word.find(i) < m and word.find(i) != -1:
                m = word.find(i)
        if m==0:
            return word+"way" 
        else:
            return word[m:]+word[:m]+"ay" 
 
 
sentence = "Hooray for pig latin" 
pigLatinSentence = "" 
# iterate through words in sentence 
for w in sentence.split(' '):
    pigLatinSentence += makePigLatin(w) + " " 
 
print pigLatinSentence.strip()
 
# output: 
# oorayHay orfay igpay atinlay