Skip to content

Commit bea7dc4

Browse files
committed
Changes so far
1 parent 1b16b7c commit bea7dc4

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

exercises/count_in_list.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ def count_in_list(list, item_to_count)
1919
# 1. A running total of the number of times you've seen the item
2020
# 2. A way to loop/iterate through the list
2121
# 3. A way to add to the running total as you see the item
22+
list = list.to_s
23+
words = list.split
24+
25+
num = words.count(item_to_count)
26+
27+
2228
end
2329

2430
if __FILE__ == $PROGRAM_NAME
2531
# I'd advise putting some sanity checks here.
2632
# How else will you be sure your code does what you think it does?
33+
p count_in_list("A way to add to the running total", "to") == 2
34+
p count_in_list([1,1,1], 1) == 3
35+
p count_in_list([1,2,3], -1) == 0
36+
p count_in_list([1,2,3], 1) == 1
2737
end

exercises/longest_string.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# str.length == 4
1313

1414
def longest_string(list)
15+
1516
my_long_str = list[0] # assigns a varible to the first array index
1617
list.each do |str| # for each str in list
1718
if str.length > my_long_str.length # if the lengthof current str is greater than my_long_str
@@ -32,5 +33,5 @@ def longest_string(list)
3233
p longest_string(["We", "might", "have", "missed", "a", "corner", "case"]) == "missed"
3334
p longest_string(["Remember", "that", "conceptually", "x"]) == "conceptually"
3435

35-
p longest_string([123456789, "might", "have", 23564.25689, "a", "corner", "case"]) == "corner"
36+
p longest_string(["123456789", "might", "have", "23564.25689", "a", "corner", "case"]) == "23564.25689"
3637
end

exercises/shortest_string.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@
44
# Prints: Nothing
55

66
def shortest_string(list)
7-
# This is your job. :)
7+
my_short_str = list[0] # assigns a varible to the first array index
8+
list.each do |str| # for each str in list
9+
if str.length < my_short_str.length # if the lengthof current str is greater than my_short_str
10+
my_short_str = str # set str to my_short_str
11+
end
12+
end
13+
return my_short_str
814
end
915

1016
if __FILE__ == $PROGRAM_NAME
1117
# I'd advise putting some sanity checks here.
1218
# How else will you be sure your code does what you think it does?
19+
20+
p shortest_string(["We", "might", "have", "missed", "a", "corner", "case"]) == "a"
21+
p shortest_string(["Remember", "that", "conceptually", "x"]) == "x"
22+
23+
p shortest_string(["123456789", "might", "have", "23564.25689", "a", "corner", "case"]) == "a"
24+
1325
end

exercises/testrunfile.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def count_in_list(list, item_to_count)
2+
# You'll need three things:
3+
# 1. A running total of the number of times you've seen the item
4+
# 2. A way to loop/iterate through the list
5+
# 3. A way to add to the running total as you see the item
6+
list = list.to_s
7+
puts list
8+
9+
words = list.split(" ")
10+
11+
puts words
12+
13+
num = words.count(item_to_count)
14+
15+
16+
puts num
17+
18+
end
19+
20+
count_in_list([1,1,1], "to")

exercises/word_count.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
def word_count(string)
1212
# Hint: You'll want to use String#split
1313
# See: http://www.ruby-doc.org/core-2.1.2/String.html#method-i-split
14+
15+
mystring = string.split(' ') #spilts the value of string into an array delimited by spaces and assigns to mystring
16+
mystring = mystring.count #counts the number of array elements and assigns to mystring
1417
end
1518

1619
if __FILE__ == $PROGRAM_NAME

0 commit comments

Comments
 (0)