[Tutor] help on scope

Charlie Clark Charlie Clark <charlie@begeistert.org>
Tue, 24 Jul 2001 12:37:48 +0200


>  What what what, recursion for anagrams?  I have been a little lax on the
>tutor list (go the integer division debate...well, it did go) but I'm pretty
>sure you don't need to do recursion for something like an anagram program.
>  Unless of course you are trying to learn recursion - at which point I'll
>be quiet :)
I don't know how to write an anagram program. It's something I've wanted to 
do for years but because I don't think like a programmer I never made it. At 
the moment I have to functions: one which rotates the letters in a word and 
another one that splits the word up. I get one to call the other. It's some 
kind of recursion and it nearly but not quite works.

def switch(word):
	for letter in word:
		word += word[0]
		word = word [1:]
		#print word
		return word

def chop(word):
	global count
	for letter in range(1, len(word) - 1):
		left = word[:letter]
		right = word[letter:]
		for letter in right:
			count += 1
			right = switch(right)
			print left, right 
# prints our anagrams with a space so we can see them

if __name__ == "__main__":
	count = 0
	
	word = "charlie"
	for letter in word:
		word = switch(word) # rotate letter by letter
		chop(word)	# move from left to right across a single word
	print "%d anagrams" %(count)
-- 
Charlie Clark
Helmholtzstr. 20
Düsseldorf
D- 40215
Tel: +49-211-938-5360
GSM: +49-178-463-6199
http://www.begeistert.org