Created
January 22, 2020 06:50
-
-
Save 0ex-d/e33f67c830c0c548d60196500a04dc5b to your computer and use it in GitHub Desktop.
Generate Random String until target is matched
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python program to generate and match | |
# the string from all random strings | |
# of same length | |
# Importing string, random | |
# and time modules | |
import string | |
import random | |
import time | |
# all possible characters including | |
# lowercase, uppercase and special symbols | |
possibleCharacters = string.ascii_lowercase + string.digits + | |
string.ascii_uppercase + ' ., !?;:' | |
# string to be generated | |
t = "Jason Borne" | |
# To take input from user | |
# t = input(str("Enter your target text: ")) | |
attemptThis = ''.join(random.choice(possibleCharacters) | |
for i in range(len(t))) | |
attemptNext = '' | |
completed = False | |
iteration = 0 | |
# Iterate while completed is false | |
while completed == False: | |
print(attemptThis) | |
attemptNext = '' | |
completed = True | |
# Fix the index if matches with | |
# the strings to be generated | |
for i in range(len(t)): | |
if attemptThis[i] != t[i]: | |
completed = False | |
attemptNext += random.choice(possibleCharacters) | |
else: | |
attemptNext += t[i] | |
# increment the iteration | |
iteration += 1 | |
attemptThis = attemptNext | |
time.sleep(0.1) | |
print("Target matched after {} iterations".format(str(iteration))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment