/
algo_force_brute.py
99 lines (77 loc) · 3.22 KB
/
algo_force_brute.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: UTF-8 -*-
import sys
sys.setrecursionlimit(50000) #Augmentation du nb max de mots à traiter
from fonctions_diverses import formate, args_corrects, enregistre, list_to_text, decoupage, measureOfTextEquilibrium, possibleString
#---------------------------------------------------------------------------------
#------------------------------Approche force brute-------------------------------
#---------------------------------------------------------------------------------
#--------------Fonctions utilisés par la fonction equilibre-------------
#-----------------------------------------------------------------------
from fonctions_diverses import longueur, blancLigne
#-----------------------Fonctions d'équilibrage-------------------------
#-----------------------------------------------------------------------
def bruteForceWithTransition(words, text, measure, strl):
if(words == []):
return [],text,measure,strl
else:
text.append([words[0]])
newText = text[:]
newText2 = text[:]
newMeasure = measure + blancLigne([words[0]], strl)
newWords = words[:]
if(type(newWords) is str):
newWords = []
else:
newWords.pop(0)
a = bruteForceWithTransition(newWords, newText, newMeasure, strl)
b = bruteForceWoTransition(newWords, newText2, newMeasure, strl)
if(a[2] < b[2]):
return a
else:
return b
def bruteForceWoTransition(words, text, measure, strl):
if(words == []):
return [],text,measure,strl
else:
wordToJoin = words[0][:]
lastString = text[-1][:]
lastString.append(wordToJoin)
if(possibleString(lastString , strl)):
text.pop(-1)
text.append(lastString)
newText = text[:]
newText2 = text[:]
newMeasure = measureOfTextEquilibrium(newText, strl)
newWords = words[:]
if(type(newWords) is str):
newWords = []
else:
newWords.pop(0)
a = bruteForceWithTransition(newWords, newText, newMeasure, strl)
b = bruteForceWoTransition(newWords, newText2, newMeasure, strl)
if(a[2] < b[2]):
return a
else:
return b
else:
return [],[],sys.maxint,strl
def equilibre(texte, n, m):
firstWord = texte[0][:]
texte.pop(0)
a = bruteForceWithTransition(texte, [[firstWord]], blancLigne([firstWord], n), n)
b = bruteForceWoTransition(texte, [[firstWord]], blancLigne([firstWord], n), n)
if(a[2] < b[2]):
#print a[1]
return a[1],a[2]
else:
#print b[1]
return b[1],b[2]
#------------------------------------------Main--------------------------------------
#------------------------------------------------------------------------------------
#On récupère le texte à découper dans un fichier, et on écrit le texte découpé dans un autre
def main():
f=equilibre
decoupage(f)
if __name__ == "__main__":
#création du mémo
main()