word shifts

dave squareswallower at invalid.com
Sun May 4 01:17:07 EDT 2008


Hello,

I made a function that takes a word list (one word per line, text file) 
and searches for all the words in the list that are 'shifts' of 
eachother.  'abc' shifted 1 is 'bcd'

Please take a look and tell me if this is a viable solution.

 def shift(word, amt):
	ans = ''
	for letter in word:
		ans = ans + chr((ord(letter) - ord('a') + amt) % 26 + ord('a'))
	return ans

def fileshift(x):
	fin = open(x)
	d = {}
	for line in fin:
		d[line.strip()] = [1]
		for i in range(1, 26):
			ite = shift(line.strip(), i)
			if ite in d:
				print ite


Any tips/suggestions/critiques greatly appreciated.. I'm trying to 
teach myself Python (and still beginning) and would love any helpful 
info.

thanks!




More information about the Python-list mailing list