How does the code prevent duplicates?

I have a question about the suggested code:

letters=“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”
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:
        uniques.append(letter)
return len(uniques)
41 Likes

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!

4 Likes

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

3 Likes
def unique_english_letters(word):
  unique = []
  for i in word:
    if i not in unique:
      unique.append(i)
  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.

5 Likes

what%20am%20i%20doing%20wrong

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
letters.strip(a)
return number
unique_english_letters(‘mississippi’)

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?
thanks

def unique_english_letters(word):
  mek=0
  p=8
  for i in letters:
    for n in word:
      if i==n and p != i:
        mek += 1
        p=i
  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):
  mek=0
  p=8
  for i in letters:
    for n in word:
      if i==n and p != i:
        mek += 1
        p=i
  return mek
# Uncomment these function calls to test your function:
print(unique_english_letters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"))

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.

1 Like

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))
3 Likes

This feels a bit cheaty now that I’m reading a great discussion on the topic, but here’s my solution.

def unique_english_letters(word):
  return len(set(word))

Sets are like lists except they ignore duplicates (and have no sense of order). This way, we can take whatever word and condense it nicely to its unique elements.

5 Likes

Shame i didn’t think of using set! I knew about it & still went about with the ‘letter not in uniques’ method. Thanks for pointing out set also works with alhpabets. :slight_smile:

In addition to letters being unique characters themselves, another reason why it didn’t count duplicate characters is because the condition “if letter in word” checks the word variable & approves the if condition at the first appearance of the letter. It won’t ever reach the second appearance of the same letter. For example, if the letter was ‘i’, the ‘in’ checker would stop at ‘Mi’ in Mississippi, & simply ignore the rest of the i letters.

Also, In this example upper & lower characters were treated as separate characters, but if they were to be treated as the same, then you’d just need to use the lower() function to lowercase all the input text. For example when passing in a word like “India”.

1 Like