From 88d121ca63f77ceb7e823af06933f2ed780a0b85 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Thu, 7 Jun 2018 16:11:34 -0400 Subject: [PATCH 1/4] Solve all Enumerables challenges --- challenge.rb | 79 +++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/challenge.rb b/challenge.rb index 0585882..1035283 100644 --- a/challenge.rb +++ b/challenge.rb @@ -1,40 +1,43 @@ def capitalize_each_string(input) - #implement your solution here -end - -def fetch_the_dog(input) - #implement your solution here -end - -def no_dogs_allowed(input) - #implement your solution here -end - -def count_the_animals(input) - #implement your solution here -end - -def fetch_the_first_two(input) - #implement your solution here -end - -def fetch_CD_animals(input) - #implement your solution here -end - -## DO NOT CHANGE CODE BELOW THIS LINE ## - -animals = ["cat", "moose", "dog", "bird"] - -p capitalize_each_string(animals) == ["Cat", "Moose", "Dog", "Bird"] - -p fetch_the_dog(animals) == ["dog"] - -p no_dogs_allowed(animals) == ["cat", "moose", "bird"] - -p count_the_animals(animals) == 4 - -p fetch_the_first_two(animals) == ["cat", "moose"] - -p fetch_CD_animals(animals) == ["cat", "dog"] + input.map { |item| item.capitalize } + end + + def fetch_the_dog(input) + input.find_all { |animal| animal == "dog" } + end + + def no_dogs_allowed(input) + input = input.dup + input.delete("dog") + return input + end + + def count_the_animals(input) + input.length + end + + def fetch_the_first_two(input) + input[0, 2] + end + + def fetch_CD_animals(input) + input.find_all { |animal| animal.start_with?("c", "d")} + end + + ## DO NOT CHANGE CODE BELOW THIS LINE ## + + animals = ["cat", "moose", "dog", "bird"] + + p capitalize_each_string(animals) == ["Cat", "Moose", "Dog", "Bird"] + + p fetch_the_dog(animals) == ["dog"] + + p no_dogs_allowed(animals) == ["cat", "moose", "bird"] + + p count_the_animals(animals) == 4 + + p fetch_the_first_two(animals) == ["cat", "moose"] + + p fetch_CD_animals(animals) == ["cat", "dog"] + \ No newline at end of file From 313631b15e1363c0002408422fcf9c0a5dcc350f Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Fri, 8 Jun 2018 11:53:20 -0400 Subject: [PATCH 2/4] Use Enumerable method reject instead of delete --- challenge.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/challenge.rb b/challenge.rb index 1035283..6b81ee8 100644 --- a/challenge.rb +++ b/challenge.rb @@ -8,9 +8,7 @@ def fetch_the_dog(input) end def no_dogs_allowed(input) - input = input.dup - input.delete("dog") - return input + input.reject { |animal| animal == "dog" } end def count_the_animals(input) From 4db11cc356d8077a32f19db78636cbfcd3a47148 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Fri, 8 Jun 2018 11:55:32 -0400 Subject: [PATCH 3/4] Use grep istead of find_all --- challenge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge.rb b/challenge.rb index 6b81ee8..8e9efe4 100644 --- a/challenge.rb +++ b/challenge.rb @@ -20,7 +20,7 @@ def fetch_the_first_two(input) end def fetch_CD_animals(input) - input.find_all { |animal| animal.start_with?("c", "d")} + input.grep(/^[c,d]/) end ## DO NOT CHANGE CODE BELOW THIS LINE ## From 1b89d555db280adc424e66043467a6626cd0a621 Mon Sep 17 00:00:00 2001 From: sheshtawy Date: Fri, 8 Jun 2018 11:59:17 -0400 Subject: [PATCH 4/4] Use select instead of find_all --- challenge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge.rb b/challenge.rb index 8e9efe4..3287210 100644 --- a/challenge.rb +++ b/challenge.rb @@ -4,7 +4,7 @@ def capitalize_each_string(input) end def fetch_the_dog(input) - input.find_all { |animal| animal == "dog" } + input.select { |animal| animal == "dog" } end def no_dogs_allowed(input)