Skip to content

Instantly share code, notes, and snippets.

@mrgriscom
Created March 22, 2013 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrgriscom/5218656 to your computer and use it in GitHub Desktop.
Save mrgriscom/5218656 to your computer and use it in GitHub Desktop.
analyze use of the shift key for typing passwords
import random
alpha_lower = 'abcdefghijklmnopqrstuvwxyz'
alpha_upper = '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