37.3k views
4 votes
Write a program that contains a class to encapsulate a word that is entered by the user. An appropriate accessor method should be written and used. Write appropriate methods to do the following: Convert the word to all lowercase letters. Scramble the word into all possible permutations and store them in a list or array. Sort the permutations in alphabetical order. Display the list of permutations. Report the number of permutations. Test all constructors and methods using a separate driver class.

1 Answer

5 votes

Answer:import random

class Word(object):

def __init__(self):

self.word = ""

def set_word(self, word):

self.word = word

def lowercase(self):

self.word = self.word.lower()

def scramble(self):

length = len(self.word)

perm = 1

for x in range(1, length+1):

perm *= x

scrambled = list()

for i in range(0, perm):

woo = [x for x in self.word]

random.shuffle(woo)

scrambled.append("".join(woo))

scrambled = sorted(scrambled)

print(scrambled)

print(f"Number of permutations: {len(scrambled)}")

d = Word()

d.set_word("under")

print(d.lowercase())

print(d.scramble())

Step-by-step explanation:

The python program defines a class called "Word" that uses an accessor "set_word" to pass in a string value to the object variable "self.word". The lowercase() method converts the string to lowercase if it is capitalized and the scramble() method returns a list of possible rotations of the letters of the word.

User SetSlapShot
by
5.4k points