Reputation: 23
I am learning Python using the How To Think Like a Computer Scientist interactive edition, and one of the exercises has the following requirement:
"Assign to a variable in your program a triple-quoted string that contains your favorite paragraph of text - perhaps a poem, a speech, instructions to bake a cake, some inspirational verses, etc.
Write a function that counts the number of alphabetic characters (a thru z, or A thru Z) in your text and then keeps track of how many are the letter ‘e’. Your function should print an analysis of the text like this:
Your text contains 243 alphabetic characters, of which 109 (44.8%) are 'e'."
I wrote the code that (to me) seems to be doing exactly what i was asked to but when i check their solution to test my code, i get different results.
My code:
text = ''' "If the automobile had followed the same development cycle as the computer, a
Rolls-Royce would today cost $100, get a million miles per gallon, and explode
once a year, killing everyone inside."
-Robert Cringely'''
lowercase_text = text.lower()
def charCounter(some_text):
e_counter = 0
char_counter = 0
for char in lowercase_text:
if char == 'e':
e_counter = e_counter + 1
else:
char_counter = char_counter + 1
return ("Your text contains " + str(char_counter) + " alphabetic characters, of which " + str(e_counter) + " (" + str((e_counter / char_counter) * 100) + "%)" + "are 'e'.")
My code output:
Your text contains 188 alphabetic characters, of which 25 (13.297872340425531%)are 'e'.
Solution code provided by authors:
def count(p):
lows="abcdefghijklmnopqrstuvwxyz"
ups="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numberOfe = 0
totalChars = 0
for achar in p:
if achar in lows or achar in ups:
totalChars = totalChars + 1
if achar == 'e':
numberOfe = numberOfe + 1
percent_with_e = (numberOfe/totalChars) * 100
print("Your text contains", totalChars, "alphabetic characters of which", numberOfe, "(", percent_with_e, "%)", "are 'e'.")
p = '''"If the automobile had followed the same development cycle as the computer, a
Rolls-Royce would today cost $100, get a million miles per gallon, and explode
once a year, killing everyone inside."
-Robert Cringely'''
count(p)
Code output by author solution:
Your text contains 166 alphabetic characters of which 25 ( 15.060240963855422 %) are 'e'.
Can someone please explain what i am doing wrong ? I don't get why there is this difference in results.
Upvotes: 0
Views: 1955
Reputation: 13222
I've got no solution to the problem for it's solved already (no downvotes please), I just want to present a more pythonic way to tackle this problem (no upvotes needed either):
import string
text = ''' "If the automobile had followed the same development cycle as the computer, a
Rolls-Royce would today cost $100, get a million miles per gallon, and explode
once a year, killing everyone inside."
-Robert Cringely'''
def check_character(text, character):
text = text.lower()
count_sum = len(list(c for c in text if c in string.ascii_lowercase))
count_char = len(list(c for c in text if c == character))
return count_sum, count_char, 100 * count_char / float(count_sum)
char = 'e'
result = check_character(text, char) + (char,)
print("Your text contains {} alphabetic characters of which {} ({:.2f}%) are '{}'.".format(*result))
Upvotes: 0
Reputation: 7099
Your solution does not check if a character is indeed alphanumerical and counts whitespace as well. In addition, 'e' is not added towards the total character count.
The problem is in your for-loop:
for char in lowercase_text:
if char == 'e':
e_counter = e_counter + 1
else:
char_counter = char_counter + 1
It should look like this:
for char in lowercase_text:
# Check if we have an alphanumeric string and continue the loop if not
if not char.isalpha():
continue
# Increment the total character counter
char_counter += 1
# Additionaly, increment the 'e' counter if we have an 'e'
if char == 'e':
e_counter += 1
Upvotes: 4
Reputation: 599570
You're including punctuation, numbers and spaces in the count, which you're not supposed to be doing.
Upvotes: 0