How does the code prevent duplicates?

I have a question about the suggested code:

def unique_english_letters(word):
> uniques = 0
>>for letter in letters:
>>if letter in word:
>>>uniques += 1

return uniques

In this case, calling the function with the argument ‘mississippi’ returns a value of 4. I see that there are only 4 distinct letters, but what part of the code makes it so letters aren’t added to “uniques” more than once? Thanks!

Because your code is iterating over letters which is comprised of all unique characters.

In technical terms, we wouldn’t need to iterate the long string, just the word and compare it to the letters string.

uniques = []
for letter in word:
    if letter in letters and not letter in uniques:
return len(uniques)

Ohh, I see. That makes a lot more sense now. I was thinking of some other ways to do it, too, but your explanation helps a lot. Thank you!


Thanks for contributing this method too, it does illustrate and explain the function in another way!

def unique_english_letters(word):
  unique = []
  for i in word:
    if i not in unique:
  return len(unique)

I misunderstood at first thinking it wanted only the unique letters returned, so originally I had return unique. I read it more carefully and saw it wanted a number return so I found return len(unique) to be the best option.


I’ve seen the solution but still having no idea why my code doesn’t work.
Could someone points out what’s wrong with my code?

Your solution creates a list of completely unique letters but that does not tell us if it is unique in the source in the source string. For that we need to verify its count.

if word.count(i) == 1:

letters = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”
def unique_english_letters(word):
number = 0
for a in word:
if a in letters:
number +=1
return number

Hi, this is my code.
I wonder why I have strip the a but it still count it nonetheless and resulting in multiple counting the word?

def unique_english_letters(word):
  for i in letters:
    for n in word:
      if i==n and p != i:
        mek += 1
  return mek

This is how I do it with 2 loops.

Can you show us some edge case outputs? Does this always work as expected?

1 Like

What does “edge case” mean? How can I test the code with edge case?

Sorry. I am not a native English speaker so some term is difficult for me to understand.

Translate edge case to your language, then Google it. Bring what you find back in your reply so we may continue this discussion.

I got it!! Edge case means problem that occur at an extreme operating parameter. So how should I test my code with edge case? I changes input to contain many letters and it still returns correct count.

def unique_english_letters(word):
  for i in letters:
    for n in word:
      if i==n and p != i:
        mek += 1
  return mek
# Uncomment these function calls to test your function:

That means thinking outside the box and supplying values that don’t fit a Norman Rockwell scenario. We use these cases to determine if we have all the necessary moving parts, or perhaps a surplus of them. Either will be exposed when we stress test our code.

1 Like

If you were to translate that code to english instructions and tell a friend those instructions I think that friend would be unconvinced that it would have the desired effect

Indeed, maybe you would want to write down your strategy in english first and consider whether it is a convincing strategy, and only then implement the matching code.

Your code adds 1 without considering what letters have been previously seen, so it’s probably not going to get the right result, yeah?

How you would carry out the task manually is a good indicator of what your program should be doing.
If you have some kind of definition then you could implement that as well. unique english letters means you could eliminate all non-english letters and then keep only unique values, so the definition is also a suitable implementation. You might come up with something other than that if you consider how you’d go about it manually, and that’d probably be fine too.

1 Like

Hm, @ionathan, I would go even further. You suggest explaining to a friend (equivalent of Rubber Duck Debugging) i would go in another direction: think how would you solde the problem not with code but with pen and paper. Normally all of these problems have a equivalent in real life where code is not used but a technique of some sort helps organize the task.

Here, how could i tell if a written text in front of me contains only english letters. I would need to know what English Letters contain, knowing by heart is one way, writing them on a piece of paper in front of me would be another way (equivalent of placing all letters in a string variable). Then i would go letter by letter in the my text (thus a loop for letters in text), see if current letter is english (search to see if letter is within the English Letters noted on paper) and keep track on a side paper of all letters that i have encounted so far (thus a list variable that has all unique values so far). I i see that i have already the letter (the letter exists in my unique list) i go on, if not, i mark it down on my side paper (thus append new letter to unique list). Once that i finished all my test, i will count the letters that i have in my side note (return lenght of list).

Imagine yourself with one finger folowing letters in original text. This is your inde in for index in all_letters_in_text. Then with another finger folowing letters in English Letters while searching for your current index in text. We humans are lazy enouth so that we will try to find the task that gets us out of work the fasters. Nobody will keep searching in text in real life once that he found a strange letter in text! Our codes should be just the same.

Based on CA’s solution and the way it was built, I used the list comprehension in order to iterate through the list ‘letters’ instead of using a ‘for loop’ . Obviously the result is the same, just a few lines shorter…

def unique_english_letters(word):
  uniques = [letter for letter in letters if letter in word]
  return (len(uniques))