In: Computer Science
1. Lets start by creating a traditional random password composed of numbers, letters, and a few special characters.
letters = "abcdefghijklmnopqrstuvwxyz"
caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "1234567890"
# Make an 8 letter password by combining characters from the
three strings
2. Next you follow the XKCD model of selecting four random words and concatenating them together to for our password.
nouns = ['tissue', 'processor', 'headquarters', 'favorite',
'cure', 'ideology', 'funeral', 'engine', 'isolation', 'perception',
'hat', 'mountain', 'session', 'case', 'legislature', 'consent',
'spread', 'shot', 'direction', 'data', 'tragedy', 'illness',
'serving', 'mess', 'resistance', 'basis', 'kitchen', 'mine',
'temple', 'mass', 'dot', 'final', 'chair', 'picture', 'wish',
'transfer', 'profession', 'suggestion', 'purse', 'rabbit',
'disaster', 'evil', 'shorts', 'tip', 'patrol', 'fragment',
'assignment', 'view', 'bottle', 'acquisition', 'origin', 'lesson',
'Bible', 'act', 'constitution', 'standard', 'status', 'burden',
'language', 'voice', 'border', 'statement', 'personnel', 'shape',
'computer', 'quality', 'colony', 'traveler', 'merit', 'puzzle',
'poll', 'wind', 'shelter', 'limit', 'talent']
verbs = ['represent', 'warm', 'whisper', 'consider', 'rub',
'march', 'claim', 'fill', 'present', 'complain', 'offer',
'provoke', 'yield', 'shock', 'purchase', 'seek', 'operate',
'persist', 'inspire', 'conclude', 'transform', 'add', 'boast',
'gather', 'manage', 'escape', 'handle', 'transfer', 'tune', 'born',
'decrease', 'impose', 'adopt', 'suppose', 'sell', 'disappear',
'join', 'rock', 'appreciate', 'express', 'finish', 'modify',
'keep', 'invest', 'weaken', 'speed', 'discuss', 'facilitate',
'question', 'date', 'coordinate', 'repeat', 'relate', 'advise',
'arrest', 'appeal', 'clean', 'disagree', 'guard', 'gaze', 'spend',
'owe', 'wait', 'unfold', 'back', 'waste', 'delay', 'store',
'balance', 'compete', 'bake', 'employ', 'dip', 'frown',
'insert']
adjs = ['busy', 'closer', 'national', 'pale', 'encouraging',
'historical', 'extreme', 'cruel', 'expensive', 'comfortable',
'steady', 'necessary', 'isolated', 'deep', 'bad', 'free',
'voluntary', 'informal', 'loud', 'key', 'extra', 'wise',
'improved', 'mad', 'willing', 'actual', 'OK', 'gray', 'little',
'religious', 'municipal', 'just', 'psychological', 'essential',
'perfect', 'intense', 'blue', 'following', 'Asian', 'shared',
'rare', 'developmental', 'uncomfortable', 'interesting',
'environmental', 'amazing', 'unhappy', 'horrible', 'philosophical',
'American']
# Make a four word password by combining words from the list of nouns, verbs and adjs
3. Of course that does not make the IT department of most colleges
and businesses happy. They still want you to have at least one
capital letter and a number in your password. We’ll learn more
about this in a couple of chapters but it is easy to replace parts
of a string with a different string using the replace method. For
example "pool".replace('o', 'e') gives us peel Once you have your
final password you can replace some letters with number
substitutions. For example its common to replace the letter l with
the number 1 or the letter e with the number 3 or the o with a 0.
You can get creative. You can also easily capitalize a word using
"myword".capitalize() Once you feel confident that you understand
the code below you can use this activecode to make your password
comply with standard procedures to include special characters.
word = "pool"
word = word.replace('o', 'e')
print(word)
word = word.capitalize()
print(word)
4.
Challenge
This last part goes beyond what you have covered in the book so far, but I’ll give you the extra code you need. You will probably be able to figure out what it does and this is kind of a fun preview of things to come.
Lets suppose you DO have a 4 character password composed only of lower case letters. How many guesses would it take you to guess the password? You can actually write a program to create a four character string and compare it to the known password. If we put this process inside a loop we can keep track and see how many guesses it takes us to find the matching password.
import random
import sys
sys.setExecutionLimit(60000) # 60 seconds
my_password = "abcd"
guess_num = 0
done = False
while not done:
guessed_pw = ""
# your code here
if guessed_pw == my_password:
print("found it after ", guess_num, " tries")
done = True
Thanks for the question, I have added comments for all the 4 sub questions. Let me know in case you have any questions or doubts.
============================================================================
letters = "abcdefghijklmnopqrstuvwxyz"
caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "1234567890"
# Make an 8 letter password by combining characters from the
three strings
import random
symbols=[]
for _ in range(3):
symbols.append(random.choice([letter
for letter in letters]))
symbols.append(random.choice([letter
for letter in caps]))
symbols.append(random.choice([letter
for letter in numbers]))
random.shuffle(symbols)
password = ''.join(symbols) # symbols will
contain 9 symbols
password=password[1:] # we slicing and taking the last 8
symbols
print(password)
# 2
nouns = ['tissue', 'processor',
'headquarters', 'favorite',
'cure', 'ideology',
'funeral', 'engine',
'isolation', 'perception',
'hat', 'mountain',
'session', 'case',
'legislature', 'consent',
'spread', 'shot',
'direction', 'data',
'tragedy', 'illness',
'serving', 'mess',
'resistance', 'basis',
'kitchen', 'mine',
'temple', 'mass',
'dot', 'final',
'chair', 'picture',
'wish', 'transfer',
'profession', 'suggestion',
'purse', 'rabbit',
'disaster', 'evil',
'shorts', 'tip',
'patrol', 'fragment',
'assignment', 'view',
'bottle', 'acquisition',
'origin', 'lesson',
'Bible', 'act',
'constitution', 'standard',
'status', 'burden',
'language', 'voice',
'border', 'statement',
'personnel', 'shape',
'computer', 'quality',
'colony', 'traveler',
'merit', 'puzzle',
'poll', 'wind',
'shelter', 'limit',
'talent']
verbs = ['represent', 'warm',
'whisper', 'consider',
'rub', 'march',
'claim', 'fill',
'present', 'complain',
'offer', 'provoke',
'yield', 'shock',
'purchase', 'seek',
'operate', 'persist',
'inspire', 'conclude',
'transform', 'add',
'boast', 'gather',
'manage', 'escape',
'handle', 'transfer',
'tune', 'born',
'decrease', 'impose',
'adopt', 'suppose',
'sell', 'disappear',
'join', 'rock',
'appreciate', 'express',
'finish', 'modify',
'keep', 'invest',
'weaken', 'speed',
'discuss', 'facilitate',
'question', 'date',
'coordinate', 'repeat',
'relate', 'advise',
'arrest', 'appeal',
'clean', 'disagree',
'guard', 'gaze',
'spend', 'owe',
'wait', 'unfold',
'back', 'waste',
'delay', 'store',
'balance', 'compete',
'bake', 'employ',
'dip', 'frown',
'insert']
adjs = ['busy', 'closer',
'national', 'pale',
'encouraging', 'historical',
'extreme', 'cruel',
'expensive', 'comfortable',
'steady', 'necessary',
'isolated', 'deep',
'bad', 'free',
'voluntary', 'informal',
'loud', 'key',
'extra', 'wise',
'improved', 'mad',
'willing', 'actual',
'OK', 'gray',
'little', 'religious',
'municipal', 'just',
'psychological', 'essential',
'perfect', 'intense',
'blue', 'following',
'Asian', 'shared',
'rare', 'developmental',
'uncomfortable', 'interesting',
'environmental', 'amazing',
'unhappy', 'horrible',
'philosophical',
'American']
dictionary_password=''
dictionary_password+=random.choice(nouns)
dictionary_password+=random.choice(adjs)
dictionary_password+=random.choice(nouns)
dictionary_password+=random.choice(verbs)
print(dictionary_password)
#3
dictionary_password=dictionary_password.title() # convert the
first letter to uppercase
print(dictionary_password)
# replace the vowels with digits
dictionary_password=dictionary_password.replace('o','0')
dictionary_password=dictionary_password.replace('i','1')
dictionary_password=dictionary_password.replace('s','$')
dictionary_password=dictionary_password.replace('e','9')
dictionary_password=dictionary_password.replace('a','8')
print(dictionary_password)
# 4
my_password='zzzz'
guess_num=0
# we need 4 for loops for each letter combinations
# for every four letters we will form the 4 letter word
# and compare to the password we need to guess
for first in letters:
for second in
letters:
for
third in letters:
for fourth in letters:
# try all combinations of the letters
attempt=first+second+third+fourth
guess_num+=1
if attempt==my_password:
print('found it after
',guess_num,"tries")
break