write a python program to shuffle letters of words evenly first time and oddly second time except the first and last character when we run it again and again. it should have the capability to handle files too. own random function using time
input:
If you can’t find homework answers by yourself, turn to our experts to get professional response in any academic field. Even being good at all subjects, you may also be trapped for hours with one of those tricky questions.
import random
def shuffle(word):
random.seed()
word = list(word)
random.shuffle(word) # shuffle letters evenly
if len(word) > 2:
word = [word[0]] + random.sample(word[1:-1], k=(len(word) - 2)) + [word[-1]] # shuffle letters oddly except the first and last character
return ''.join(word)
def shuffle_file(filename):
with open(filename) as file:
lines = file.readlines()
with open(filename, 'w') as file:
for line in lines:
file.write(' '.join([shuffle(word) for word in line.rstrip().split()]) + '\n')
Comments
Leave a comment