Could I use replace to work through the letters?

Hi everyone,

Could anyone tell me where is my mistake in this code:

def unique_english_letters(word):
  count = 0
  for letter in word:
    match = letters.find(letter)
    if match != -1:
      letters.replace(letter," ")
      count += 1
    return count

Thanks!

str.replace() is greedy , or in other words applies globally to the string. All matching characters are replaced, not just the first one.

If I’m to understand the name of the function in literal terms it would mean there is only one of a certain letter in the word (a string).

The cheap and dirty way would be to parse out a set from the word.

return len(set(list(word)))

Of course that will never do if we are just learning to build algorithms to return the same findings.

One such approach would be to build temporary string and then add only letters that are not already in that string, to the string. We must assume there is a global, letters for this example (as with the above posted sample).

temp = ""
for letter in word:
    if letters.find(letter) > -1:
        if letter not in temp:
            temp += letter
return len(temp)

Another approach, although less of a naive algorithm, would be to use the built in count() function.

temp = ""
for letter in word:
    if letters.find(letter) > -1:
        if word.count(letter) == 1:
            temp += letter
return len(temp)

And we could go on to find other approaches, as well. The bottom line, as I perceive this problem is to find only unique occurrences of each letter and ignore any that are duplicated.

The string is immutable, .replace() method returns a copy of the string after replacement. Also, you are dealing with the word, not the letters string provided at the beginning.
Try this intead:

def unique_english_letters(word):
  count = 0
  for letter in word:
    match = word.find(letter)
    if match != -1:
      count += 1
      new_word = word.replace(letter," ")  # both "" or " "(with space) work
      word = new_word
  return count

or a simpler version:

def unique_english_letters(word):
  count = 0
  for letter in word:
    if letter in word:
      count += 1
      word = word.replace(letter,"") 
  return count

I think this approach you mentioned is better, which we don’t even need a long helper string (“letters”) at all.

It would really help if we had a link to the this exercise so the finer points can be ironed out. What are we after? Only unique letters that are not duplicated? Or only a list of letters that appear in the string.

mississippi

Only m is unique, but there are four letters in all, misp. So what’s it to be?

Good point which I didn’t go into since I scrapped that approach…

Quote from the exercise, exercise link

The function should return the total number of unique letters in the string.

:grinning:

1 Like

Okay, so we wish to return 4 from mississippi. The word unique is ambiguous in this context since only the m is unique, the rest are similar, but make up the other three distinct letters.

As for the letters string, that’s as much as saying, letter.isalpha() to prevent non-alpha characters from being included in the count. Not sure, but I think the only other place we see isalpha() is in the Pyg Latin exercise.