Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations
- from time import time
- pms=permutations
- osszbetu=[]
- def betuszerintbont(arr):
- global osszbetu
- new=[]
- ind=0
- for i in range(len(arr)):
- if i==0:
- new.append([arr[i]])
- osszbetu.append(arr[i][0])
- continue
- if arr[i][0]==arr[i-1][0]:
- new[ind].append([arr[i]])
- else:
- new.append([arr[i]])
- osszbetu.append(arr[i][0])
- ind+=1
- return new
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def getwords(s, w, n, a):
- for i in range(len(w[n])):
- if n==len(w)-1:
- szo=''.join([w[k][a[k]] for k in range(len(w))])
- s.append(szo)
- else:
- getwords(s, w, n+1, a)
- if a[n]<len(w[n])-1:
- a[n]+=1
- else:
- a[n]=0
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def graph(arr, layer=0, letters=1, word=0, graphized=[]):
- if layer==0:
- graph(arr, layer=1, graphized=graphized)
- return graphized
- elif letters<=len(arr[word]) and word<len(arr):
- cur_word=word
- while cur_word<len(arr) and arr[word][:letters-1] == arr[cur_word][:letters-1]:
- new_index=cur_word
- cur_let=arr[cur_word][letters-1]
- if cur_let not in graphized:
- graphized.append(cur_let)
- if letters < len(arr[cur_word]):
- if arr[cur_word-1][:letters]==arr[cur_word][:letters] and cur_word>0:
- graphized.append([''])
- else:
- graphized.append([])
- part=graphized[-1]
- new_index=graph(arr, layer+1, letters+1, cur_word, part)
- if new_index==cur_word:
- cur_word+=1
- else: cur_word=new_index
- return cur_word
- return word
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def search(graph, word, letter=0):
- wlen=len(word)
- if letter<wlen and word[letter] in graph:
- list_ind=graph.index(word[letter])+1
- if len(graph)>list_ind:
- part=graph[list_ind]
- if isinstance(part, list):
- return search(part,word,letter+1)
- elif letter==wlen-1:
- return True
- else:
- return False
- elif letter==wlen-1:
- return True
- else:
- return False
- elif '' in graph and letter==wlen:
- return True
- else:
- return False
- #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- f=open('Sort_lemma.txt', 'r', encoding='latin-1')
- print('Filet megnyitottam')
- kivalogat=[szo.split()[0].lower() for szo in f if len(szo.split())!=0]
- print('Kivalogattam')
- kivalogat.sort()
- t=time()
- mygraph=graph(kivalogat)
- print('Grafot letrehoztam', str(time()-t),'ido alatt\n')
- f.close()
- word="megszentségteleníthetetlenségeitekért"
- t=time()
- for i in range(100000):
- what=search(mygraph, word)
- print(what, str(time()-t), "mp volt", word, "megkeresése")
- word="cuccombuccos"
- t=time()
- for i in range(100000):
- what=search(mygraph, word)
- print(what, str(time()-t), "mp volt", word, "megkeresése")
- hany=int(input('Hany szo lesz: '))
- words=[]
- for i in range(hany):
- word=input(str(i+1)+'. szó: ')
- words.append(word)
- perm=pms([i for i in range(len(words))])
- count=1
- osszes=[]
- for p in perm:
- wds=[words[k] for k in p]
- index=[0 for w in words]
- s=[]
- getwords(s, wds, 0, index)
- for check in s:
- if search(mygraph, check) and not check in osszes:
- print("|", end='', flush=True)
- f=open('kigyujtott.txt', 'a')
- f.write(str(check)+'\n')
- f.close()
- osszes.append(check)
- print (str(count))
- count+=1
- print ('Kigyujtve')
- print ('Elmentve')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement