rickri
rickri

Reputation: 43

Create new words from start word python

def make_new_words(start_word):
    """create new words from given start word and returns new words"""
    new_words=[]
    for letter in start_word:
        pass    
        #for letter in alphabet:
        #do something to change letters
        #new_words.append(new_word)

I have a three letter word input for example car which is the start word. I then have to create new word by replacing one letter at a time with every letter from the alphabet. Using my example car I want to create the words, aar, bar, car, dar, ear,..., zar. Then create the words car, cbr, ccr, cdr, cer,..., czr. Finally caa, cab, cac, cad, cae,..., caz.

I don't really know what the for loop should look like. I was thinking about creating some sort of alphabet list and by looping through that creating new words but I don't know how to choose what parts of the original word should remain. The new words can be appended to a list to be returned.

Upvotes: 1

Views: 3119

Answers (5)

Hannele
Hannele

Reputation: 9666

This could work as a brute-force approach, although you might end up with some performance issues when you go to try it with longer words.

It also sounds like you might want to constrain it only to words that exist in a dictionary at some point, which is a whole other can of worms.

But for right now, for three-letter words, you're onto something of the right track, although I worry that the question might be a little too specific for Stack Overflow.

First, you will probably have more success if you loop through the index for the word, rather than the letter:

alphabet = 'abcdefghijklmnopqrstuvwxyz'
for i in range(len(start_word)):

Then, you can use a slice to grab the letters before and after the index.

  for letter in alphabet:
    new_word = start_word[:i] + letter + start_word[i + 1:]

Another approach is given above, which casts the string to a list. That works around the fact that python will disallow simply setting start_word[i] = letter, which you can read about here.

Upvotes: 0

Alexander
Alexander

Reputation: 12785

import string

def make_new_words(start_word):
    """create new words from given start word and returns new words"""
    new_words = []
    for i, letter in enumerate(start_word):
        word_as_list = list(start_word)
        for char in string.ascii_lowercase:
            word_as_list[i] = char
            new_words.append("".join(word_as_list))

    return new_words 

Upvotes: 3

gboffi
gboffi

Reputation: 25023

lowercase is just a string containing the lowercase letters...

We want to change each letter of the original word (here w) so we iterate on the letters of w, but we'll mostly need the index of the letter, so we do our for loop on enumerate(w).

First of all, in python strings are immutable so we build a list x from w... lists are mutable

Now a second, inner loop on the lowercase letters: we change the current element of the x list accordingly (having changed x, we need to reset it before the next inner loop) and finally we print it.

Because we want to print a string rather than the characters in a list, we use the join method of the null string '' that glue together the elements of x using, of course, the null string.

I have not reported the output but it's exactly what you've asked for, just try...

from string import lowercase

w = 'car'
for i, _ in enumerate(w):
    x = list(w)
    for s in lowercase:
        x[i] = s
        print ''.join(x)

Upvotes: 1

seanmhanson
seanmhanson

Reputation: 229

You can do this with two loops, by looping over the word and then looping over a range for all letters. By keeping an index for the first loop, you can use a slice to construct your new strings:

for index in enumerate(start_word):
    for let in range(ord('a'), ord('z')+1):
        new_words.append(start_word[:index] + chr(let) + start_word[index+1:])

Upvotes: 0

dylrei
dylrei

Reputation: 1738

import string
all_letters = string.ascii_lowercase
def make_new_words(start_word):
    for index, letter in enumerate(start_word):
        template = start_word[:index] + '{}' + start_word[index+1:]
        for new_letter in all_letters:
            print template.format(new_letter)

Upvotes: 0

Related Questions