Commit 123bbede authored by Mathieu Valois's avatar Mathieu Valois
Browse files

random permutations and distance cost

parents
Loading
Loading
Loading
Loading

distances.py

0 → 100644
+24 −0
Original line number Diff line number Diff line
#! /usr/bin/env python3

import sys
import random as rand
import Levenshtein as leven

def rperm(l):
	d = len(l)
	for i in range(d):
		r = rand.randint(i, d-1)
		l[i], l[r] = l[r], l[i]
		yield l[i]

def cost(l):
	c = 0
	perm = rperm(l)
	w0 = next(perm)
	for w in perm:
		c += leven.distance(w0, w)
		w0 = w
	return c

l = ["password", "linkedin", "passwOrd", "football123"]
print(cost(l))
 No newline at end of file