Skip to content

Instantly share code, notes, and snippets.

@Dhrumilcse
Last active September 19, 2018 17:51
Show Gist options
  • Save Dhrumilcse/2ad9ced135f411b5ebd8c51e5692ddbb to your computer and use it in GitHub Desktop.
Save Dhrumilcse/2ad9ced135f411b5ebd8c51e5692ddbb to your computer and use it in GitHub Desktop.
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):
nextGeneration = []
for i in range(best_sample):
nextGeneration.append(populationSorted[i][0])
for i in range(lucky_few):
nextGeneration.append(random.choice(populationSorted)[0])
random.shuffle(nextGeneration)
return nextGeneration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment