Answer to Question #241785 in Python for food_calc

Question #241785

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.



1
Expert's answer
2021-09-24T18:56:02-0400
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')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS