Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

This is my solution resulting in an error. Returns 0

from collections import Counter

import string

def count_letters(word):

    global count

    wordsList = string.split(word)

    count = Counter()

    for words in wordsList:

        for letters in set(words):

            return count[letters]

word = "The grey old fox is an idiot"

print count_letters(word)

1 Answer

0 votes
by (36.8k points)

Use the below code: 

def count_letters(word):

    return len(word) - word.count(' ')

Alternatively, if you have multiple letters to ignore, you could filter the string:

def count_letters(word):

    BAD_LETTERS = " "

    return len([letter for letter in word if letter not in BAD_LETTERS])

To know about Linux join the Linux training

 

Do check out the video below

 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 13, 2020 in Linux by blackindya (18.4k points)
0 votes
1 answer

31k questions

32.9k answers

507 comments

693 users

...