diff python/anagram.py @ 896:83618049c2ff

py3
author Jeff Hammel <k0scist@gmail.com>
date Fri, 11 Feb 2022 09:33:38 -0800
parents 4096771c8b9f
children
line wrap: on
line diff
--- a/python/anagram.py	Fri Aug 13 15:16:21 2021 -0700
+++ b/python/anagram.py	Fri Feb 11 09:33:38 2022 -0800
@@ -25,7 +25,7 @@
 def anagramize(theword, wordlist, level=0):
 
     if 0:
-        print '%s%s : %s' % ('-' * level, theword, wordlist)
+        print('%s%s : %s' % ('-' * level, theword, wordlist))
 
     anagrams = []
 
@@ -49,12 +49,12 @@
             anagrams += [ ' '.join((word, i)) for i in subgram ]
 
     if 0:
-        print '%s%s returning %s' % ('-' * level, theword, anagrams)
+        print('%s%s returning %s' % ('-' * level, theword, anagrams))
 
     if anagrams:
         return anagrams
     return None
-    
+
 if __name__ == '__main__':
     import sys
     from optparse import OptionParser
@@ -75,16 +75,16 @@
                 options.filename = i
                 break
         else:
-            print 'Dictionary not found'
+            print('Dictionary not found')
             parser.print_help()
             sys.exit(1)
 
     if not args:
-        print 'please provide an anagram'
+        print('please provide an anagram')
         sys.exit(0)
 
-    f = file(options.filename, 'r')
-    read_dictionary(f)
+    with open(options.filename, 'r') as f:
+        read_dictionary(f)
 
     # XXX could cleanup
     anagram = ' '.join(args)
@@ -95,8 +95,8 @@
 
     wordlist = [ i for i in dictionary
                  if i and is_in(i, anagram) is not None ]
-    
+
     anagrams = anagramize(anagram, wordlist)
 
     if anagrams:
-        print '\n'.join(anagrams)
+        print('\n'.join(anagrams))