# 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
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Showing posts with label replace. Show all posts
Showing posts with label replace. Show all posts
Friday, October 15, 2010
Python - Anagram generator
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
Monday, October 19, 2009
Python - quickly update urls in a web page
# update a webpages url references. # Your web page has a collection of images and you # want to update all the "folder1" references # to the new folder you've populated called "newfolder" # normally you would open the actual html file # and iterate through the file one line at a time # like in this post. # but for simplicity sake lets just use a multi line string # for the example theString = """ <img src="folder1/pic2324.jpg" /> <img src="folder1/pic2255.png" /> <img src="folder2/pic552.jpg" /> <img src="folder1/pica2f.jpg" /> """ # all you need is to iterate through the # file and replace 'folder1' with 'newfolder' for line in theString.split('\n'): line = line.replace("folder1/", "newfolder/") print line #output: # <img src="newfolder/pic2324.jpg" /> # <img src="newfolder/pic2255.png" /> # <img src="folder2/pic552.jpg" /> # <img src="newfolder/pica2f.jpg" />
Tuesday, September 29, 2009
Python - generate double dutch
# this example is similar # to the double dutch generator def createDoubleDutch(word): ''' create and return a double dutch version of word ''' for v in ("a", "e", "i", "o", "u", "y"): # double dutch-ize each vowel word = word.replace(v, v+"b"+v) return word if __name__ == '__main__': ddSentence = "" for w in "My sample sentence for double dutch".split(' '): ddSentence += createDoubleDutch(w) + " " print ddSentence.strip() #output: # Myby sabamplebe sebentebencebe fobor doboubublebe dubutch
Thursday, September 17, 2009
Python - string stripping
# string manipulations
# striping characters out of strings
aUrl = "http://www.example.com"
# strip, lstrip, rstrip only remove characters
# on the either edge (strip), the left edge (lstrip),
# and the right edge(rstrip)
print aUrl.strip("htp:/w.com")
#outputs:
# example
print aUrl.lstrip("htp:/")
#outputs:
# www.example.com
print aUrl.rstrip(".com")
#outputs:
# http://www.example
# to strip out all occurances
# of a character use replace()
print aUrl.replace('m', "")
#outputs:
# http://www.exaple.co
Friday, July 10, 2009
Python remove vowels from a sentence
# create a tuple with vowels....yes I include y as a vowel vowels = 'a', 'e', 'i', 'o', 'u', 'y' # movie quote for our sentence sentence = "My name is Werner Brandes, my voice is my password, verify me." # iterate through the tuple of vowels for vowel in vowels: # replace the vowel with nothing sentence = sentence.replace(vowel, '') # display the vowel-less sentence! print sentence 'M nm s Wrnr Brnds, m vc s m psswrd, vrf m.' # additional string functions: http://docs.python.org/library/stdtypes.html # manipulate vowels to create pig latin or double dutch
Labels:
python,
remove,
replace,
string.replace,
vowel
Subscribe to:
Posts (Atom)