Question: Encapsulate this code in a function named countLetters, and generalize
ID: 3619694 • Letter: Q
Question
Question: Encapsulate this code in a function named countLetters, and generalize it so that it accepts the string and the letter as arguments. - Part 2: In another File rewrite this function so that instead of traversing the string, it uses the three-parameter version of find from the previous.--
The program code is below.
---
The following program counts the number of times the letter a appears in a string:
fruit = "banana"
count = 0
for char in fruit:
if char == ’a’:
count = count + 1
print count
This program demonstrates another pattern of computation called a counter.
The variable count is initialized to 0 and then incremented each time an a is
found. (To increment is to increase by one; it is the opposite of decrement,
and unrelated to “excrement,” which is a noun.) When the loop exits, count
contains the result—the total number of a’s. Question: Encapsulate this code in a function named countLetters, and generalize it so that it accepts the string and the letter as arguments. - Part 2: In another File rewrite this function so that instead of traversing the string, it uses the three-parameter version of find from the previous.
--
The program code is below.
---
The following program counts the number of times the letter a appears in a string:
fruit = "banana"
count = 0
for char in fruit:
if char == ’a’:
count = count + 1
print count
This program demonstrates another pattern of computation called a counter.
The variable count is initialized to 0 and then incremented each time an a is
found. (To increment is to increase by one; it is the opposite of decrement,
and unrelated to “excrement,” which is a noun.) When the loop exits, count
contains the result—the total number of a’s.
Explanation / Answer
Part 1: Part 2 :Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.