Advertisement
Benkex

Keresztrejtvény nagyon gyors

Nov 15th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. from itertools import permutations
  2. from time import time
  3. pms=permutations
  4.  
  5. osszbetu=[]
  6.  
  7. def betuszerintbont(arr):
  8.     global osszbetu
  9.     new=[]
  10.     ind=0
  11.     for i in range(len(arr)):
  12.         if i==0:
  13.             new.append([arr[i]])
  14.             osszbetu.append(arr[i][0])
  15.             continue
  16.         if arr[i][0]==arr[i-1][0]:
  17.             new[ind].append([arr[i]])
  18.         else:
  19.             new.append([arr[i]])
  20.             osszbetu.append(arr[i][0])
  21.             ind+=1
  22.     return new
  23.    
  24. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25.  
  26. def getwords(s, w, n, a):
  27.     for i in range(len(w[n])):
  28.         if n==len(w)-1:
  29.             szo=''.join([w[k][a[k]] for k in range(len(w))])
  30.             s.append(szo)
  31.         else:
  32.             getwords(s, w, n+1, a)
  33.         if a[n]<len(w[n])-1:
  34.             a[n]+=1
  35.         else:
  36.             a[n]=0
  37.            
  38. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39.  
  40. def graph(arr, layer=0, letters=1, word=0, graphized=[]):
  41.    
  42.     if layer==0:
  43.         graph(arr, layer=1, graphized=graphized)
  44.         return graphized
  45.        
  46.     elif letters<=len(arr[word]) and word<len(arr):
  47.         cur_word=word
  48.        
  49.         while cur_word<len(arr) and arr[word][:letters-1] == arr[cur_word][:letters-1]:
  50.             new_index=cur_word
  51.            
  52.             cur_let=arr[cur_word][letters-1]
  53.             if cur_let not in graphized:
  54.                 graphized.append(cur_let)
  55.            
  56.             if letters < len(arr[cur_word]):
  57.                 if arr[cur_word-1][:letters]==arr[cur_word][:letters] and cur_word>0:
  58.                     graphized.append([''])
  59.                 else:
  60.                     graphized.append([])
  61.                 part=graphized[-1]
  62.                 new_index=graph(arr, layer+1, letters+1, cur_word, part)
  63.                
  64.             if new_index==cur_word:
  65.                 cur_word+=1
  66.             else: cur_word=new_index
  67.            
  68.         return cur_word
  69.        
  70.     return word
  71.    
  72. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73.    
  74. def search(graph, word, letter=0):
  75.     wlen=len(word)
  76.     if letter<wlen and word[letter] in graph:
  77.         list_ind=graph.index(word[letter])+1
  78.         if len(graph)>list_ind:
  79.             part=graph[list_ind]
  80.             if isinstance(part, list):
  81.                 return search(part,word,letter+1)
  82.             elif letter==wlen-1:
  83.                 return True
  84.             else:
  85.                 return False
  86.         elif letter==wlen-1:
  87.             return True
  88.         else:
  89.             return False
  90.     elif '' in graph and letter==wlen:
  91.         return True
  92.     else:
  93.         return False
  94.        
  95. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  96.  
  97. f=open('Sort_lemma.txt', 'r', encoding='latin-1')
  98. print('Filet megnyitottam')
  99.  
  100. kivalogat=[szo.split()[0].lower() for szo in f if len(szo.split())!=0]
  101. print('Kivalogattam')
  102. kivalogat.sort()
  103.  
  104. t=time()
  105. mygraph=graph(kivalogat)
  106. print('Grafot letrehoztam', str(time()-t),'ido alatt\n')
  107. f.close()
  108.  
  109. word="megszentségteleníthetetlenségeitekért"
  110. t=time()
  111. for i in range(100000):
  112.     what=search(mygraph, word)
  113. print(what, str(time()-t), "mp volt", word, "megkeresése")
  114.  
  115. word="cuccombuccos"
  116. t=time()
  117. for i in range(100000):
  118.     what=search(mygraph, word)
  119. print(what, str(time()-t), "mp volt", word, "megkeresése")
  120.  
  121. hany=int(input('Hany szo lesz: '))
  122. words=[]
  123. for i in range(hany):
  124.     word=input(str(i+1)+'. szó: ')
  125.     words.append(word)
  126.  
  127. perm=pms([i for i in range(len(words))])
  128. count=1
  129. osszes=[]
  130. for p in perm:
  131.     wds=[words[k] for k in p]
  132.     index=[0 for w in words]
  133.     s=[]
  134.     getwords(s, wds, 0, index)
  135.     for check in s:
  136.         if search(mygraph, check) and not check in osszes:
  137.             print("|", end='', flush=True)
  138.             f=open('kigyujtott.txt', 'a')
  139.             f.write(str(check)+'\n')
  140.             f.close()
  141.             osszes.append(check)
  142.     print (str(count))
  143.     count+=1
  144.    
  145. print ('Kigyujtve')
  146. print ('Elmentve')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement