forked from codeunion/ruby-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_in_list.rb
27 lines (24 loc) · 924 Bytes
/
count_in_list.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
# Method name: count_in_list(list, item_to_count)
# Inputs: 1. a list of anything, 2. an item for us to count in the list
# Returns: The number of times our item is contained in the input list
# Prints: Nothing
#
# For example,
# count_in_list([1,2,3], 1) == 1
# count_in_list([1,2,3], -1) == 0
# count_in_list([1,1,1], 1) == 3
# --- NOTE ---
# Ruby has a built-in method to do this, but the purpose of this kata is
# to write it yourself.
#
# See: http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-count
def count_in_list(list, item_to_count)
# You'll need three things:
# 1. A running total of the number of times you've seen the item
# 2. A way to loop/iterate through the list
# 3. A way to add to the running total as you see the item
end
if __FILE__ == $0
# I'd advise putting some sanity checks here.
# How else will you be sure your code does what you think it does?
end