-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateRandomString.py
More file actions
30 lines (26 loc) · 903 Bytes
/
Copy pathGenerateRandomString.py
File metadata and controls
30 lines (26 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Generating random strings until a given string is generated
import random, time
def generate_random_string(input_string):
size = len(input_string)
start = time.time()
iterator_count = 0
all_characters = []
print('Guessing...')
for item in range(97, 123):
all_characters.append(chr(item))
while True:
guess = random.choices(all_characters, k=size)
iterator_count += 1
guess_word = ''.join(guess)
if guess_word == input_string:
print('Guessed')
break
end = time.time()
return {
'duration': end - start,
'attempts': iterator_count
}
user_string = input('Enter a lowercase string : ')
user_string = user_string.lower()
result = generate_random_string(user_string)
print(f'Your word : {user_string} was guessed in {result["duration"]} seconds and in {result["attempts"]} attempts')