Python Forum
count unique letters in a word
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
count unique letters in a word
#1
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 4
But I don't understand the logic. Can anyone explain me the logic in simple terms ?
Reply
#2
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I am not able to write code which delivers expected results but must be explained to me by someone else. But it just me Smile

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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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 4
I 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))
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Word count vanjoe198 1 1,956 Mar-16-2021, 12:27 AM
Last Post: BashBedlam
  sys.stdin to do a word count based on user entry Kaltex 3 3,669 Jul-19-2020, 01:54 PM
Last Post: deanhystad
Exclamation A function that takes a word and a string of forbidden letters pooyan89 5 4,634 Jun-12-2019, 09:44 PM
Last Post: nilamo
  Unique Count Talch 0 2,228 Aug-16-2018, 11:15 AM
Last Post: Talch
  Count Letters in a Sentence (without using the Count Function) bhill 3 5,165 Jun-19-2018, 02:52 AM
Last Post: bhill

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020