-
Notifications
You must be signed in to change notification settings - Fork 68
/
AllStar18.rb
38 lines (29 loc) · 994 Bytes
/
AllStar18.rb
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
=begin
Create a function called strCount() that accepts 2 string arguments and returns
an integer of the count of occurrences the 2nd argument are found in the first.
If no occurrences can be found, a count of 0 should be returned.
strCount('Hello', 'o') // => 1
strCount('Hello', 'l') // => 2
strCount('', 'z') // => 0
Note:
The first argument can be an empty string
The second string argument will always be of length 1
=end
Test.assert_equals(strCount('Hello', 'o'), 1);
Test.assert_equals(strCount('Hello', 'l'), 2);
Test.assert_equals(strCount('', 'z'), 0)
# My Solution
def strCount(word, letter)
word.count(letter)
end
# Codewars random tests
letters = "abcdefghijklmnopqrstuvwxyz"
letters = letters.split("")
(1..200).each do |rtest|
word = []
(2..rand(100)).each {|x| word << letters[rand(0..25)]}
word = word.join
letter = letters[rand(0..25)]
solution = strCount2(word, letter)
Test.assert_equals(strCount(word, letter),solution,"Expected: '#{solution}'")
end