-
Notifications
You must be signed in to change notification settings - Fork 1
/
016_All Star Code Challenge #18.py
75 lines (52 loc) · 1.63 KB
/
016_All Star Code Challenge #18.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
Codewars Coding Challenge
All Star Code Challenge #18
Create a function that accepts a string and a single character, and returns an integer of the count of occurrences the 2nd argument is found in the first one.
If no occurrences can be found, a count of 0 should be returned.
("Hello", "o") ==> 1
("Hello", "l") ==> 2
("", "z") ==> 0
str_count("Hello", 'o'); // returns 1
str_count("Hello", 'l'); // returns 2
str_count("", 'z'); // returns 0
Notes
The first argument can be an empty string
In languages with no distinct character data type, the second argument will be a string of length 1
https://www.codewars.com/kata/5865918c6b569962950002a1/train/python
"""
# My Solution
def str_count(strng, letter):
return strng.count(letter)
# Sample test
"""
@test.describe('Should return the correct character count')
def fixed():
@test.it("")
def f():
test.assert_equals(str_count('hello', 'l'), 2)
test.assert_equals(str_count('hello', 'e'), 1)
test.assert_equals(str_count('codewars', 'c'), 1)
test.assert_equals(str_count('ggggg', 'g'), 5)
test.assert_equals(str_count('hello world', 'o'), 2)
test.assert_equals(str_count('', 'z'), 0)
"""
"""
PERFECT SOLUTION FROM CODEWARS
=1=
strCount = str.count
=2=
str_count = lambda strng, letter: strng.count(letter)
=3=
def str_count(strng, letter):
score = 0
for char in strng:
if char == letter:
score += 1
return score
=4=
def strCount(string, letter):
return len([i for i in string if i == letter])
=5=
def str_count(strng, letter):
return sum(1 for i in strng if i in letter)
"""