Home > Software engineering >  Python program to generate all possible words by change letter 'c' in the word "ace&q
Python program to generate all possible words by change letter 'c' in the word "ace&q

Time:09-17

this is my first post here so apologies for formatting in advance, I am trying to write a simple program in Python that takes a word, in this case, the word "ace" and checks all the possible words that can be generated by switching out the letter 'c' with all the letters in the alphabet. What I have tried is turning both my word and the alphabet into lists so I can and create some kind of loop that runs through all the possibilites and eventually cross references them with a dictionary of possible english words (haven't got there yet). I don't have strong a programming background so this has proven to be harder than I thought, my limited work is below, have been at this for a few hours, thanks!

My code...(it doesnt work at the moment)

#take letter ace and input new middle letter

word = list("ace")
alphabet = list("abcdefghijklmnopqrstuvwxyz")
wordnew = []
counter = 0
for word[1] in word:
    wordnew = word.replace("c", alphabet[0] if counter < 25)

print(wordnew)

CodePudding user response:

Note that you have to put a variable name to be created between the for and the inword[1] is not a valid variable name, so your code should fail with a SyntaxError exception.

You can iterate over each letter of the alphabet and create a list of words generated from ace:

alphabet = "abcedfghijklmnopqrstuvwxyz"
words = []
for letter in alphabet:
    words.append("ace".replace("c", letter))

You can even do this in one line, using a list comprehension:

words = [ "ace".replace("c", letter) for letter in "abcdefghijklmnopqrstuvwxyz" ]

Note how I didn't have to turn alphabet into a list—in Python, strings are iterable, meaning that you can loop through them just like you can with lists.

Of course, you can print them all, add this at the end:

print(words)

PS: You could also turn "abcdefghijklmnopqrstuvwxyz" into string.ascii_lowercase, though you'll have to import the string module (built into python).

CodePudding user response:

You're close, here is a simple way to do it

>>> word = "ace" #no need to make it a list, you want it to be a string so you can use .replace on it
>>> alphabet = "abcdefghijklmnopqrstuvwxyz" #you can use string in a for loop, in which case they are treated like a list of characters
>>> for letter in alphabet:
        print(word.replace("c",letter)) #here you do what you need, here I print it but you can save in a list by doing an .append into one or with a list comprehension which is the prefer mode to make list

    
aae
abe
ace
ade
aee
afe
age
ahe
aie
aje
ake
ale
ame
ane
aoe
ape
aqe
are
ase
ate
aue
ave
awe
axe
aye
aze
>>> 
>>> wordnew = [word.replace("c",letter) for letter in alphabet] #the list comprehension version of the above
>>> wordnew
['aae', 'abe', 'ace', 'ade', 'aee', 'afe', 'age', 'ahe', 'aie', 'aje', 'ake', 'ale', 'ame', 'ane', 'aoe', 'ape', 'aqe', 'are', 'ase', 'ate', 'aue', 'ave', 'awe', 'axe', 'aye', 'aze']
>>>     

CodePudding user response:

word = 'ace'
alphabet = list("abcdefghijklmnopqrstuvwxyz")
for letter in alphabet:
    print(word.replace('c', letter))

CodePudding user response:

If you want the list of all possible "words" after replacement of letter "c" by any other letter from the alphabet you can simply do the following

word = "ace"
alphabet = "abcdefghijklmnopqrstuvwxyz"
new_words = [word.replace('c', ch) for ch in alphabet]

print(new_words)

CodePudding user response:

word = "ace" 
alphabet = list("abcdefghijklmnopqrstuvwxyz") 
for letter in alphabet:
    wordnew = word.replace("c", letter)
    print(wordnew)

You should iterate through the alphabet, not the word.

CodePudding user response:

Assuming you somehow have a list of all the words that exist, this works:

word = "ace"
alphabet = "abcdefghijklmnopqrstuvwxyz" 
wordnew = []
counter = 0


# list full of all real words
legitWords = ['ace', 'man', 'math', 'marry' 'age',
          'ape', 'are', 'ate', 'awe' 'axe']

for letter in alphabet:  # looping through every letter in the alphabet
    newWord = word.replace('c', letter)  # replaces c with current letter
    
    if newWord in legitWords:
        # adds the counter and appends the new word, if it really exists
        counter  = 1
        wordnew.append(newWord)

note that you don't have to convert the strings to lists as you have done, because they are iterable .

  • Related