![]() |
count unique letters in a word - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: count unique letters in a word (/thread-18889.html) |
count unique letters in a word - sunny_awesome - Jun-05-2019 Problem description : 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 have written the code : letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" # Write your unique_english_letters function here: def unique_english_letters (word) : counter = 0 for i in letters : if i in word : counter += 1 return counter # Uncomment these function calls to test your function: print(unique_english_letters("mississippiiiii")) # should print 4 print(unique_english_letters("Apple")) # should print 4But I don't understand the logic. Can anyone explain me the logic in simple terms ? RE: count unique letters in a word - buran - Jun-05-2019 (Jun-05-2019, 12:22 PM)sunny_awesome Wrote: But I don't understand the logic. Can anyone explain me the logic in simple terms ?how did you write it, without understanding the logic? i.e. each code snippet implements some logic (algorithm) - with or without success RE: count unique letters in a word - perfringo - Jun-05-2019 I am not able to write code which delivers expected results but must be explained to me by someone else. But it just me ![]() For example, I can easily explain following code which accomplishes task at hand. One of the reasons - I wrote it myself. def unique_english_letters(text): return len(set(range(65, 91)).union(range(97, 123)) & {ord(chr) for chr in text})There is nothing wrong with asking for help. Just ability to write code which you don't understand is little bit suspicious. RE: count unique letters in a word - jefsummers - Jun-06-2019 letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" #Makes a string of the letters in alphabet to count # Write your unique_english_letters function here: def unique_english_letters (word) : counter = 0 #initializes counter for i in letters : #creates loop to loop through the letters in the alphabet if i in word : #If the letter in the alphabet is in the word.... counter += 1 #then increment the counter return counter # Uncomment these function calls to test your function: print(unique_english_letters("mississippiiiii")) # should print 4 print(unique_english_letters("Apple")) # should print 4I think the confusion is that I would logically have gone through the letters of "word" and tried to count unique letters. This works it the other way around - go through the alphabet and increment if that letter exists in "word". Also, thumbs up to Perfringo - this is a great place to use sets. If you know your input will not contain letters, spaces, or punctuation it could be even simpler - return len(set(word)) RE: count unique letters in a word - kotter - Jun-06-2019 Maybe you understand this code better: # write function that counts number of unique english letters def unique_english_letters(word): var1 = [y for y in word if y.isalpha()] #list containing only letters of given string var2 = list(set(var1)) #no duplicates in list, all elements are now unique return len(var2) #count number of elements = number of unique letters |