Generating random strings until a given string is generated
Given the string, the task is to generate the same string using the random combination of special character, numbers, and alphabets.
Examples :
Input : GFG
Output :n4W
mK7
k1x
q;;, !g
.
.
.
.
.
GF,
GFf
GFp
GFG
Target matched after 167 iterations
Prerequisite : Generating random Id’s in Python
string.ascii_lowercase, string.digits, string.ascii_uppercase are some of the common string constants present in the string module in Python, which are used here as dictionary. All these string constants are combined with other special characters like ‘ ., !?;:’ and are stored in a variable.
Approach : Simply run two loops and use random function provided by python. It will show all the possible combinations that can be provided by the random function and same thing will be done by the decrypt loop. At the end, it will show the same text as you have inserted when prompted to enter. It will match each random string with the given string. If both index matches then fix that index and iterate for the remaining.
Below is the implementation :
# 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 = "geek" # 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 = Falseiteration = 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) # Driver Code print("Target matched after " + str(iteration) + " iterations") |
Output :
FyFJ .:YZ aubo . . . g56G gk6R g7Se gT o gD d gXek g0ek g ek . . gUek giek geek Target matched after 168 iterations
Recommended Posts:
- Generating random Id's in Python
- Generating Random id's using UUID in Python
- Generating random number list in Python
- Python | Generate random string of given length
- Random sampling in numpy | random() function
- Python | Visualize graphs generated in NetworkX using Matplotlib
- Formatted string literals (f-strings) in Python
- Python | Split CamelCase string to individual strings
- Python | Sort given list of strings by part of string
- Python | Convert list of strings to space separated string
- Find the longest common prefix between two strings after performing swaps on second string
- Generate two output strings depending upon occurrence of character in input string in Python
- Generating subarrays using recursion
- Generating all possible Subsequences using Recursion
- Generating Strong Password using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



