This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fitness (password, test_word): | |
| if (len(test_word) != len(password)): | |
| print("taille incompatible") | |
| return | |
| else: | |
| score = 0 | |
| i = 0 | |
| while (i < len(password)): | |
| if (password[i] == test_word[i]): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| def generateAWord (length): | |
| i = 0 | |
| result = "" | |
| while i < length: | |
| letter = chr(97 + int(26 * random.random())) | |
| result += letter | |
| i +=1 | |
| return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| def createChild(individual1, individual2): | |
| child = "" | |
| for i in range(len(individual1)): | |
| if (int(100 * random.random()) < 50): | |
| child += individual1[i] | |
| else: | |
| child += individual2[i] | |
| return child |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import operator | |
| import random | |
| def computePerfPopulation(population, password): | |
| populationPerf = {} | |
| for individual in population: | |
| populationPerf[individual] = fitness(password, individual) | |
| return sorted(populationPerf.items(), key = operator.itemgetter(1), reverse=True) | |
| def selectFromPopulation(populationSorted, best_sample, lucky_few): |
NewerOlder