Answer to Question #260115 in Python for sindhu

Question #260115

Write a  PYTHON function to get the input from the user to go with words given on the fly (input through keyboard) or read it from a file.  At least it should contain a single word otherwise throw an error to the user rather than showing a PYTHON error.


Shuffle letters of words evenly and oddly except the first and last character when we run it again and again. You need to store the modified one so everytime you run it changes.  Keep the odd one in one color and even in another color and keep black for the first and last character

Adcricnog to reacersh at an Engslih Urvtisniey, it deosn't mttear in waht oedrr the ltreets in a wrod are, the olny itopmanrt tih

1
Expert's answer
2021-11-02T17:36:23-0400
import random


def shuffle(wd):
    random.seed()
    wd = list(wd)
    
    random.shuffle(wd) 
    if len(wd) > 2:
        wd = [wd[0]] + random.sample(wd[1:-1], k=(len(wd) - 2)) + [wd[-1]] 
    return ''.join(wd)


str1=input("Enter a string to shuffle: ")
print(shuffle(str1))

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