Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AnisahTiaraPratiwi/29d64f32a99a41d7da460e2d6299c90b to your computer and use it in GitHub Desktop.

Select an option

Save AnisahTiaraPratiwi/29d64f32a99a41d7da460e2d6299c90b to your computer and use it in GitHub Desktop.
Question 7 Use a dictionary to count the frequency of letters in the input string. Only letters should be counted, not blank spaces, numbers, or punctuation. Upper case should be considered the same as lower case. For example, count_letters("This is a sentence.") should return {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}.
def count_letters(text):
result = {}
text = text.lower()
# Go through each letter in the text
for letter in text:
# Check if the letter needs to be counted or not
if letter .isalpha() and letter not in result:
result[letter] = text.lower().count(letter)
# Add or increment the value in the dictionary
return result
print(count_letters("AaBbCc"))
# Should be {'a': 2, 'b': 2, 'c': 2}
print(count_letters("Math is fun! 2+2=4"))
# Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}
print(count_letters("This is a sentence."))
# Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}
@daviddennis02

daviddennis02 commented Feb 22, 2022

Copy link
Copy Markdown

To ensure we get the letters in lowercase:
I have updated the above solution.

def count_letters(text):
  result = {}
  # Go through each letter in the text
  for letter in text:
    # Check if the letter needs to be counted or not
    if letter .isalpha() and letter not in result:
    # Add or increment the value in the dictionary
      result[letter.lower()]=text.lower().count(letter)
  return result

print(count_letters("AaBbCc"))
# Should be {'a': 2, 'b': 2, 'c': 2}

print(count_letters("Math is fun! 2+2=4"))
# Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}

print(count_letters("This is a sentence."))
# Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}

@RaymondGuo007

Copy link
Copy Markdown

def count_letters(text):
result = {}
text = text.lower()

Go through each letter in the text

for letter in text:
# Check if the letter needs to be counted or not
if letter.isalpha() :
# Add or increment the value in the dictionary
count = text.count(letter)
result[letter] = count
return result

@jgt69

jgt69 commented Jun 15, 2022

Copy link
Copy Markdown

def count_letters(text):
result = {}
text = text.lower()

for letter in text:

checking if letter is within a-z

if letter.isalpha():
    if letter not in result:
        result[letter] = 0
    result[letter] += 1

return result

print(count_letters("AaBbCc"))
{'a': 2, 'b': 2, 'c': 2}

print(count_letters("Math is fun! 2+2=4"))
{'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}

print(count_letters("This is a sentence."))
{'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}

@adichamoli14

Copy link
Copy Markdown

def count_letters(text):
result = {}
text = text.lower()
for letter in text:
if letter not in result and letter.isalpha():
result[letter] = text.count(letter)
return result

@Rcaponi85

Copy link
Copy Markdown

I keep getting the white spaces what did I do wrong?

def count_letters(text):
result = {}
text=text.lower()

Go through each letter in the text

for letter in text:
# Check if the letter needs to be counted or not
if letter.isalpha() and letter not in result:
result[letter]=0
result[letter]=text.count(letter)

# Add or increment the value in the dictionary
___

return result

print(count_letters("AaBbCc"))

Should be {'a': 2, 'b': 2, 'c': 2}

print(count_letters("Math is fun! 2+2=4"))

Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}

print(count_letters("This is a sentence."))

Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}

@abuzar01440

Copy link
Copy Markdown

def count_letters(text):
result = {}
text = text.lower()

Go through each letter in the text

for letter in text:
    if letter in result:
        result[letter] += 1
    else:
        result[letter] = 1
# Check if the letter needs to be counted or not
# Add or increment the value in the dictionary
        
return result

print(count_letters("AaBbCc"))

Should be {'a': 2, 'b': 2, 'c': 2}

print(count_letters("Math is fun! 2+2=4"))

Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}

print(count_letters("This is a sentence."))

Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment