Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
analyze use of the shift key for typing passwords
import random
alpha_lower = 'abcdefghijklmnopqrstuvwxyz'
numeric = '0123456789'
punct = '''`-=[]\;',./'''
shift_punct = '!@#$%^&*()~_+{}|:"<>?'
shift_chars = set(c for c in alpha_upper + shift_punct)
def count_shifts(text):
"""count the number of times the shift key must be pressed to type a given
string of text. assume shift key is held down between consecutive characters
requiring shift"""
shifted = False
count = 0
for c in text:
needs_shift = (c in shift_chars)
if needs_shift and not shifted:
count += 1
shifted = needs_shift
return count
def random_text(alphabet=alpha_lower + numeric + alpha_upper, length=8):
"""generate random text"""
return ''.join(random.choice(alphabet) for i in range(length))
def trial(alphabet, length, n):
"""perform trail runs to determine the average number of required
shift-key pressings per character"""
return sum(count_shifts(random_text(alphabet, length)) for i in range(n)) / float(length * n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment