Hi folks, Im suffering from a Logic Failure in my own brain.
I attempted and successfully completed the first coding challenge with the following code:
def unique_english_letters(word):
count = 0
unique = []
for letter in word:
if letter in letters and letter not in unique:
count +=1
unique.append(letter)
return count
My logical process was this: I need to create a counter to increment for each UNIQUE letter found in a string. I then need to create a list for UNIQUE letters.
I then need to create a loop to iterate through each letter which appears in the string provided by ‘word’. Within that loop, i will check if the current letter appears in the string letters (containing all english letters) AND ensute the letter does not appear in the list of UNIQUE letters defined previously. If both conditions are met, the count is increased and the letter is added to the unique list of letters. This provides the appropriate result (4 and 4 for mississippi and apple)
So, logically, in my mind this made sence and my code followed my brain path.
However, i scroll down a little and find the solution provided is ultimately simpler, yet less logical to my brain:
unique = 0
for letter in letters:
if letter in word:
unique +=1
return unique
So in this i see we set the counter as 0, then iterate through each letter in the predefined letters string. Within this loop we check whether the current letter appears in the string defined by ‘word’ and if so, we add 1 to the count and return the count after the loop is completed.
I cant 100% wrap my head around it, but in writing this post i think i understand.
Am i right in saying that in the second example, we are looping through the letters defined in the english alphabet string, then checking if a single ‘letter’ in the ‘word’ provided matches that condition - therefore, it will only provide unique instances of the letter appearing in word?
Im sorry if this does not make much sense. My brain works in a strange way at times, so im just looking for a sanity check more than anything.
I feel like i am so close to grasping this 90% of the time, but then some logic trips me up and perhaps i overcomplicate the matter.
Anyway, thanks in advance, sorry if i sound stupid and cheers for the awesome posts you guys make in the forums, super helpful to me