I need help with this question, I need to do it in Python. Please help me how to
ID: 3854906 • Letter: I
Question
I need help with this question, I need to do it in Python. Please help me how to do this in python. Thanks!
7. r randomly permuti string into a Python program: Read a word. Repeat len(word) times Pick a random position i in the word, but not the last position Pick a ord. Swap the letters at positions j and i. random position j > i in the w Print the word To swap the letters, construct substrings as follows: first midde last Then replace the string with: first +wordlil middle wordlil last t + word[jl +Explanation / Answer
The answer is as follows:
The code is as follows: (Python 3.x)
#!/usr/bin/python
from random import randint
data = input('Enter the word:')
len = len(data)
i = randint(0,len-2)
j = -1
while j < i:
j = randint(0,len-1)
k = i-1
l = i+1
m = j-1
n = j+1
if i == 0:
first = ""
else:
first = data[0:k]
middle = data[l:m]
if j == len-1:
last = ""
else:
last = data[n:]
data1 = first + data[j] + middle + data[i] + last
print(data1)