Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 3.81 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Finding words that are the reverse of each other in a file
  2. def is_reverse(word1, word2):  
  3.    if len(word1) == len(word2):
  4.      if word1 == word2[::-1]:
  5.        return True  
  6. return False
  7.  
  8. fin = open('List.txt')
  9. for word1 in fin:
  10.     word1 = word1.strip()
  11.     word1 = word1.lower()
  12.     for word2 in fin:
  13.       word2 = word2.strip()
  14.       word2 = word2.lower()
  15.       print word1 + word2
  16.       if is_reverse(word1, word2) is True:
  17.              print word1 + ' is the opposite of ' + word2
  18.        
  19. def is_reverse(word1, word2):
  20.   if len(word1) == len(word2):
  21.       if word1 == word2[::-1]:
  22.         return True
  23.   return False
  24.  
  25. fin = open('List.txt')
  26. fin2 = ['test1','test2','test3','test4','test5']
  27. for word1 in fin:
  28.     word1 = word1.strip()
  29.     word1 = word1.lower()
  30.     for word2 in fin2:
  31.       word2 = word2.strip()
  32.       word2 = word2.lower()
  33.       print word1 + word2
  34.       if is_reverse(word1, word2) is True:
  35.              print word1 + ' is the opposite of ' + word2
  36.        
  37. def is_reverse(word1, word2):  
  38.    if len(word1) == len(word2):
  39.      if word1 == word2[::-1]:
  40.        return True  
  41. return False
  42.  
  43. file1 = open('List.txt')
  44. for word1 in file1:
  45.     word1 = word1.strip()
  46.     word1 = word1.lower()
  47.     file2 = open('List.txt')
  48.     for word2 in file2:
  49.       word2 = word2.strip()
  50.       word2 = word2.lower()
  51.       print word1 + word2
  52.       if is_reverse(word1, word2):
  53.              print word1 + ' is the opposite of ' + word2
  54.        
  55. def is_reverse(word1, word2):
  56.     if len(word1) == len(word2):
  57.         if word1 == word2[::-1]:
  58.             return True
  59.     return False
  60.  
  61. file = open('List.txt')
  62. words = list(file)
  63. for word1 in words:
  64.     word1 = word1.strip()
  65.     word1 = word1.lower()
  66.     for word2 in words:
  67.         word2 = word2.strip()
  68.         word2 = word2.lower()
  69.         print word1 + word2
  70.         if is_reverse(word1, word2):
  71.             print word1 + ' is the opposite of ' + word2
  72.        
  73. >>> file = open('testfile.txt')
  74. >>> it1 = iter(file)
  75. >>> it2 = iter(file)
  76. >>> id(it1)
  77. 3078689064L
  78. >>> id(it2)
  79. 3078689064L
  80. >>> id(file)
  81. 3078689064L
  82.        
  83. >>> list = [1,2,3]
  84. >>> it3 = iter(list)
  85. >>> it4 = iter(list)
  86. >>> id(it3)
  87. 3078746156L
  88. >>> id(it4)
  89. 3078746188L
  90. >>> id(list)
  91. 3078731244L
  92.        
  93. words = []
  94. wordset = set(())
  95.  
  96. file = open('List.txt')
  97. for line in file:
  98.     word = line.strip('n')
  99.     words.append(word)
  100.     wordset.add(word)
  101.  
  102. for word in words:
  103.     reversed = word[::-1]
  104.     if reversed in wordset:
  105.         print word + ' is the opposite of ' + reversed
  106.        
  107. def getWords(fname):
  108.     with open(fname) as inf:
  109.         words = list(w.strip().lower() for w in inf)
  110.     ws = set(words)
  111.     words = list(ws)
  112.     words.sort()
  113.     return words, ws
  114.  
  115. def wordsInReverse(words, wordset):
  116.     for w in words:
  117.         rw = w[::-1]  # reverse the string
  118.         if rw in wordset:
  119.             yield w,rw
  120.  
  121. def main():
  122.     words, wordSet = getWords('List.txt')
  123.  
  124.     for w,rw in wordsInReverse(words, wordSet):
  125.         if rw >= w:  # don't print duplicates
  126.             print('{0} is the opposite of {1}'.format(w, rw))        
  127.  
  128. if __name__=="__main__":
  129.     main()
  130.        
  131. from itertools import chain
  132.  
  133. def main():
  134.     words1, wordSet1 = getWords('List1.txt')
  135.     words2, wordSet2 = getWords('List2.txt')
  136.  
  137.     for w,rw in chain(wordsInReverse(words1, wordSet2), wordsInReverse(words2, wordSet1)):
  138.         print('{0} is the opposite of {1}'.format(w, rw))
  139.        
  140. fin1 = open("list.txt")
  141. for word1 in fin1:
  142.     fin2 = open("list.txt")
  143.     for word2 in fin2:
  144.         ...etc...
  145.        
  146. with open('palindromic.txt') as f:
  147.     ch = f.read()
  148.     li = [ w for w in ch.split() if len(w)>1 ]
  149.  
  150. dic ={}
  151. pals = set([])
  152.  
  153. for line in li:
  154.     word = line.strip().lower()
  155.     if len(word)>1:
  156.         if word not in dic:
  157.             dic[word] = 1
  158.             if word[::-1] in dic and word[::-1]!=word:
  159.                 pals.add(word)
  160.         else:
  161.             dic[word] += 1
  162.  
  163.  
  164. for w in pals:
  165.     print w,dic[w],'  ',w[::-1],dic[w[::-1]]