Question about Python Code Challenge: Strings Count letter review

Hi, first of all, sorry if I post in the wrong place. I would be grateful if any admin that sees it could move the post to the appropriate place if that is the case, and I would love any DM or reply with a link to a working “New User Guide” for posting in the correct place etc :slight_smile:

To the question!
I’ve been doing the Learn Python 3 course, and I am now at the last couple of coding challenges. After the first challenge, I see that I had a solution in mind that is VERY different from the one proposed, and built for by the challenge. Therefore i would love it if someone with experience could rate and/or share their thoughts on the two solutions. What is the benefit and drawbacks, and why should I strive for the one over the other?

The assignment in the challenge:

Blockquote
Write a function called unique_english_letters that takes the string word as a parameter. The function should return the total number of unique letters in the string. Uppercase and lowercase letters should be counted as different letters.
We’ve given you a list of every uppercase and lower case letter in the English alphabet. It will be helpful to include that list in your function.

I am providing the code including both solutions (in the same code) here:

letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" # Write your unique_english_letters function here: # Codecademy solution ref Solution 1 def unique_english_letters(word): uniques = 0 for letter in letters: if letter in word: uniques += 1 return uniques # My solution ref Solution 2 def unique_english_letter(word): unique = [] for letter in word: if letter not in unique: unique.append(letter) else: continue return len(unique) print("Solution 1:") # Uncomment these function calls to test your function: print(unique_english_letters("mississippi")) # should print 4 print(unique_english_letters("Apple")) # should print 4 print("Solution 2:") print(unique_english_letter("mississippi")) # should print 4 print(unique_english_letter("Apple")) # should print 4

Right out the gate, I do see the proposed solution as more compact, and I guess that is a benefit. While I guess the benefit if mine is that you do not need to refer to an outside reference to the alphabet, though feel free to tell me what issues this might create.

Looking forward to learning from you all!
Thank you in advance!

Consider the following test cases:

print(unique_english_letters("missis sip pi!?")) # 4
print(unique_english_letters("A pp le!?")) # 4

print(unique_english_letter("missis sip pi!?")) # 7
print(unique_english_letter("A pp le!?")) # 7

You can fix this by amending your condition to:

if (letter not in unique) and (letter in letters): 
# OR if you don't want to rely on the global variable
if (letter.isalpha()) and (letter not in unique):

After the fix, I guess there are tradeoffs in both approaches.

For short strings such as "Apple", the Codecademy solution will execute 52 iterations of the for loop, whereas your solution will only need 5 iterations.

For very long strings (suppose a string with 1000 characters), the Codecademy solution will arrive at an answer in 52 iterations, whereas your solution will need 1000 iterations of the loop.

Wait. There is an error in my code? I didn’t even realize since the lesson check gave me “correct” :sweat_smile:
:S I’ll take a look at it asap.