Change language

Generate random strings until a given string is generated

| |

Examples:

 Input: GFG Output: n4W mK7 k1x q ;;,! g. ... ... ... ... GF, GFf GFp GFG Target matched after 167 iterations 

Prerequisite: And stored in variable.

Approach: just run two loops and use the random function provided by python. It will show all the possible combinations that can be provided by the random function, and the same will be done in the decryption loop. At the end, the same text that you inserted when prompted to enter will be shown. It will match every random string against the given string. If both indexes match, fix that index and iterate over the rest.

The implementation is as follows:

# Python program for generating and matching
# line from all random lines
# same length

 
# Import line, random
# and temporary modules

import string

import random

import time

 
# all possible characters, including
# lowercase, uppercase and special special characters

possibleCharacters = string.ascii_lowercase + string.digits +  

string.ascii_uppercase + ’.,!?;:’

 
# line to generate

t = "geek"

 
# To accept user input
# t = input (str (& quot; Enter target text: & quot;))

 

attemptThis = ’’ .join (rando m.choice (possibleCharacters)

for i in range ( len (t)))

attemptNext = ’ ’

  

completed = False

iteration = 0

  
# Repeat until complete is false

while completed = = False :

< code class = "undefined spaces">  print (attemptThis)

 

attemptNext = ’’

completed = True

 

# Correct index if matches

  # lines to be generated

  for i in range ( len (t)):

< p> if attemptThis [i]! = t [i]:

  completed = False

  attemptNext + = random.choice (possibleCharacters)

else :

  attemptNext + = t [i]

 

# increase 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 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically