CluelessCoder
CluelessCoder

Reputation: 45

Python Scrambler Program

This program takes words in a sentence and scrambles them. The rules are: - first and last letter remain the same - punctuation at the end of a word stays the same - punctuation with a word is scrambled like the middle letters

My problem is that if I have multiple punctuation at the end of a word it does not scramble it.

Ex) testing!!! should be something like t!ste!nig! or t!est!nig! but not tstenig!!!

How can I fix that?

import random
import string

original_text = input("Enter your text: ").split(' ')
seed = int(input("Enter a seed (0 for random): "))

punctuation = []
for c in string.punctuation:
    punctuation.append(c)

if seed is not 0:
    random.seed(seed)

randomized_list = []

def scramble_word(word):
    alpha = word[0]
    end_punctuation = ''

    if word[-1] in punctuation:
        x = -1
        while word[x] in punctuation:
            end_punctuation += word[x]
            x -= 1
        omega = word[x]
        middle = word[1: x]
    else:
        omega = word[-1]
        middle = word[1:-1]
        end_punctuation = ""
    middle_list = list(middle)
    random.shuffle(middle_list)
    shuffled_text = "".join(middle_list)
    new_words = alpha + shuffled_text + omega + end_punctuation
    return new_words
for item in original_text:
    if len(item) <= 3:
        randomized_list.append(item)
    else:
        randomized_list.append(scramble_word(item))
new_words = " ".join(randomized_list)
print(new_words)

Upvotes: 1

Views: 1937

Answers (2)

JackTheCrab
JackTheCrab

Reputation: 132

My take on it, can shorten the code a bit. (In Python 3.5.1)

import random

words = input("Enter your text: ")

def scramble(words):
    for x in words.split():
        middle = x[1:-1]
        middle_list = list(middle)
        random.shuffle(middle_list)
        shuffled = "".join(middle_list)
        print ("".join(x[0]+shuffled+x[-1]),"", end="")


scramble(words)

My output was for example from:

Masterson!!%& makes baking%$ potatoes great!

to Ment!osrs!a%& mkeas bigkna%$ patooets gerat!

I'm sure someone could shorten it even more dramatically.

Upvotes: 1

Ken Y-N
Ken Y-N

Reputation: 15008

The problem is that you don't add in the punctuation to the shuffle; see the two amended lines below:

if word[-1] in punctuation:
    x = -1
    while word[x] in punctuation:
        end_punctuation += word[x]
        x -= 1
    omega = word[x]
    middle = word[1: x] + end_punctuation[1:] # Include all except the final character
    end_punctuation = end_punctuation[0]  # Just use the final character
else:
    omega = word[-1]
    middle = word[1:-1]
    end_punctuation = ""

That does the trick for me:

In [63]: scramble_word('hello!?$')
Out[63]: 'hle?l!o$'

In [64]: scramble_word('hello!?$')
Out[64]: 'h?!ello$'

In [65]: scramble_word('hello!?$')
Out[65]: 'hlel!?o$'

In [66]: scramble_word('hello!')
Out[66]: 'hlleo!'

In [67]: scramble_word('hello!')
Out[67]: 'hello!'

In [68]: scramble_word('hello!')
Out[68]: 'hlleo!'

In [69]: scramble_word('hello')
Out[69]: 'hlelo'

By the way, you don't need the punctuation variable; word[x] in string.punctuation will work the same.

Upvotes: 1

Related Questions