The problem
The objective of this problem is to write down a operate that takes two inputs: a string and a personality. The operate will depend the variety of occasions that character seems within the string. The depend is case insensitive.
Examples:
count_char("fizzbuzz","z") # 4
count_char("Fancy fifth fly aloof","f") # 5
The character may be any alphanumeric character.
The answer in Python code
Choice 1:
def count_char(haystack, needle):
depend = 0
for c in haystack:
if c.decrease()==needle.decrease():
depend+=1
return depend
Choice 2:
def count_char(s,c):
return s.decrease().depend(c.decrease())
Choice 3:
from collections import Counter
def count_char(s, c):
return Counter(s.decrease())[c.lower()]
Check circumstances to validate our resolution
take a look at.assert_equals(count_char("Hey there", "e"), 3)
take a look at.assert_equals(count_char("Hey there", "t"), 1)
take a look at.assert_equals(count_char("Hey there", "h"), 2)
take a look at.assert_equals(count_char("Hey there", "L"), 2)
take a look at.assert_equals(count_char("Hey there", " "), 1)