Skip to content

Instantly share code, notes, and snippets.

View Dhrumilcse's full-sized avatar

Dhrumil Patel Dhrumilcse

View GitHub Profile
import random
def generateAWord (length):
i = 0
result = ""
while i < length:
letter = chr(97 + int(26 * random.random()))
result += letter
i +=1
return result
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]):
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):
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
import random
def mutateWord(word):
index_modification = int(random.random() * len(word))
if (index_modification == 0):
word = chr(97 + int(26 * random.random())) + word[1:]
else:
word = word[:index_modification] + chr(97 + int(26 * random.random())) + word[index_modification+1:]
return word
@Dhrumilcse
Dhrumilcse / dictionary.py
Created November 17, 2018 13:13
An Interactive Dictionary
#Import library
import json
from difflib import get_close_matches
#Loading the json data as python dictionary
#Try typing "type(data)" in terminal after executing first two line of this snippet
data = json.load(open("data.json"))
#Function for retriving definition
def retrive_definition(word):
@Dhrumilcse
Dhrumilcse / dictionary_1.py
Created November 17, 2018 13:14
Dictionary Part 1
#Import library
import json
#Loading the json data as python dictionary
#Try typing "type(data)" in terminal after executing first two line of this snippet
data = json.load(open("data.json"))
#Function for retriving definition
def retrive_definition(word):
return data[word]
@Dhrumilcse
Dhrumilcse / dictionary_2.py
Created November 18, 2018 07:40
Dictionary Part 2
#Import library
import json
#Loading the json data as python dictionary
#Try typing "type(data)" in terminal after executing first two line of this snippet
data = json.load(open("dictionary.json"))
#Function for retriving definition
def retrive_definition(word):
#Check for non existing words
@Dhrumilcse
Dhrumilcse / dictionary_3.py
Created November 18, 2018 07:47
Dictionary Part 3
#Import library
import json
#Loading the json data as python dictionary
#Try typing "type(data)" in terminal after executing first two line of this snippet
data = json.load(open("dictionary.json"))
#Function for retriving definition
def retrive_definition(word):
#Removing the case-sensitivity from the program
@Dhrumilcse
Dhrumilcse / dictionary_4-1.py
Created November 18, 2018 08:08
Dictionary Part 4.1
#Import library
import json
# This is a python library for 'Text Processing Serveices', as the offcial site suggests.
import difflib
from difflib import SequenceMatcher
#Let's load the same data again
data = json.load(open("dictionary.json"))
#Run a Sequence Matcher