কম্পিউটার

পাইথন ব্যবহার করে প্রদত্ত স্ট্রিং তৈরি না হওয়া পর্যন্ত র্যান্ডম স্ট্রিং তৈরি করা


একটি স্ট্রিং দেওয়া, আমাদের কাজ হল অক্ষর, বিশেষ অক্ষর, সংখ্যা ইত্যাদির এলোমেলো সমন্বয় ব্যবহার করে কিছু স্ট্রিং তৈরি করা।

উদাহরণ

Input
PP
Output
AK
AK
.
.
.
.
.

অ্যালগরিদম

Step 1: Input a string.
Step2: Here we store all possible combination of lowercase, uppercase and special characters in a variable.
Step3: Use two loops and use random function. From this, we can get all the possible combinations of characters, symbols.
Step4: At the end display same string which is same as an input string and it matches each random string with given input string.
Step5: If both index values are same then store the index and iterate for the remaining.

উদাহরণ কোড

import string
import random
import time
my_possibleCharacters = string.ascii_lowercase + string.digits + string.ascii_uppercase + ' ., !?;:'
t = "ab"
my_attemptThis = ''.join(random.choice(my_possibleCharacters)
for i in range(len(t)))
my_attemptNext = ''
com = False
iteration = 0
# Iterate while completed is false
while com == False:
print(my_attemptThis)
my_attemptNext = ''
com = True
for i in range(len(t)):
if my_attemptThis[i] != t[i]:
   com = False
my_attemptNext += random.choice(my_possibleCharacters)
else:
   my_attemptNext += t[i]
# increment the iteration
iteration += 1
my_attemptThis = my_attemptNext
time.sleep(0.1)
# Driver Code
print("String matched after " + str(iteration) + " iterations")
পরে মিলেছে

আউটপুট

36
G
sM
,L
jt
g1
FN
uR
;W
Ja
3n
4o
Gl
kY
NR
oR
Nw
Lg
Jt
Od
wN
z0
J
3a
9J
sF
v
g6
HO
Ia
AB
Xa
OX
:N
Wo
Dp
f;
tt
kf
Er
In
ou
bD
T
a0
aH
aW
a
a8
ai
ax
az
aN
aJ
ah
a0
a.
aq
ar
ax
ai
am
a;
aO
as
a;
aS
aL
aQ
a8
a3
ae
a5
aS
ao
al
aV
ar
aj
aT
aS
ad
ab
String matched after 83 iterations

  1. একটি প্রদত্ত স্ট্রিং সেট ব্যবহার করে স্বর সংখ্যা গণনা করার জন্য পাইথন প্রোগ্রাম

  2. পাইথন ব্যবহার করে বিটগুলির একটি স্ট্রিং কীভাবে তৈরি করবেন?

  3. পাইথন ব্যবহার করে কিভাবে একটি এলোমেলো 128 বিট স্ট্রিং তৈরি করবেন?

  4. কিভাবে পাইথনে একটি দীর্ঘ মাল্টি-লাইন স্ট্রিং তৈরি করবেন?