-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65d96fc
commit 6aa6708
Showing
5 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Help | ||
|
||
## Running the tests | ||
|
||
For running the tests provided, you will need the Minitest gem. Open a | ||
terminal window and run the following command to install minitest: | ||
|
||
``` | ||
gem install minitest | ||
``` | ||
|
||
|
||
Run the tests from the exercise directory using the following command: | ||
|
||
``` | ||
ruby <snake-case-exercise>_test.rb | ||
``` | ||
|
||
Please replace `<snake-case-exercise>` with your exercise name in snake_case. | ||
|
||
## Color output | ||
|
||
You can `require 'minitest/pride'` or run the following command to get colored output: | ||
|
||
``` | ||
ruby -r minitest/pride <snake-case-exercise>_test.rb | ||
``` | ||
|
||
## Submitting your solution | ||
|
||
You can submit your solution using the `exercism submit bird_count.rb` command. | ||
This command will upload your solution to the Exercism website and print the solution page's URL. | ||
|
||
It's possible to submit an incomplete solution which allows you to: | ||
|
||
- See how others have completed the exercise | ||
- Request help from a mentor | ||
|
||
## Need to get help? | ||
|
||
If you'd like help solving the exercise, check the following pages: | ||
|
||
- The [Ruby track's documentation](https://exercism.org/docs/tracks/ruby) | ||
- The [Ruby track's programming category on the forum](https://forum.exercism.org/c/programming/ruby) | ||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) | ||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) | ||
|
||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. | ||
|
||
To get help if you're having trouble, you can use one of the following resources: | ||
|
||
- [Ruby Documentation](http://ruby-doc.org/) | ||
- [StackOverflow](http://stackoverflow.com/questions/tagged/ruby) | ||
- [/r/ruby](https://www.reddit.com/r/ruby) is the Ruby subreddit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Hints | ||
|
||
## General | ||
|
||
- The bird count per day is stored in a [instance variable][instance-variables] named `birds_per_day`. | ||
- The bird count per day is an array that contains exactly 7 integers. | ||
|
||
## 1. Check what the counts were last week | ||
|
||
- As this method does _not_ depend on the current week's count, it is defined as a [`class` method][class-method]. | ||
- There are [several ways to define an array][array-definition]. | ||
|
||
## 2. Check how many birds visited yesterday | ||
|
||
- Remember that the counts are ordered by day from oldest to most recent, with the last element representing today. | ||
- Accessing the second last element can be done either by using its (fixed) index (remember to start counting from zero) or by calculating its index using the [array's size][array-length]. | ||
|
||
## 3. Calculate the total number of visiting birds | ||
|
||
- It's possible to calculate the sum of a collection using the [Array#sum][array-sum] method. | ||
|
||
## 4. Calculate the number of busy days | ||
|
||
- Ruby also provides a method for [counting elements on a collection][array-count] | ||
|
||
## 5. Check if there was a day with no visiting birds | ||
|
||
- There are some methods that can be use to check the existence on an element on a collection. For example [Enumerable#any?][enumerable-any] and [Enumerable#all?][enumerable-all] | ||
|
||
[instance-variables]: http://ruby-for-beginners.rubymonstas.org/writing_classes/instance_variables.html | ||
[class-method]: http://www.rubyfleebie.com/2007/04/09/understanding-class-methods-in-ruby/ | ||
[array-definition]: https://ruby-doc.org/core-2.7.0/Array.html#class-Array-label-Creating+Arrays | ||
[array-length]: https://ruby-doc.org/core-2.7.0/Array.html#class-Array-label-Obtaining+Information+about+an+Array | ||
[array-sum]: https://ruby-doc.org/core-2.7.0/Array.html#method-i-sum | ||
[array-count]: https://ruby-doc.org/core-2.7.0/Array.html#method-i-count | ||
[enumerable-any]: https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-any-3F | ||
[enumerable-all]: https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-all-3F |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
# Bird Count | ||
|
||
Welcome to Bird Count on Exercism's Ruby Track. | ||
If you need help running the tests or submitting your code, check out `HELP.md`. | ||
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) | ||
|
||
## Introduction | ||
|
||
## Arrays | ||
|
||
In Ruby, **arrays** are ordered, integer-indexed collections of any object. | ||
Array indexing starts at `0`. | ||
A negative index is assumed to be relative to the end of the array — e.g.. an index of `-1` indicates the last element of the array, `-2` is the next to last element in the array, and so on. | ||
|
||
### Creating arrays | ||
|
||
Arrays are normally created using the `[]` notation. | ||
They can create any different type of object. | ||
|
||
```ruby | ||
array = [1, "two", 3.0] | ||
``` | ||
|
||
### Element Reference | ||
|
||
Elements in an array can be retrieved by their indexes using the `#[]` method. | ||
This returns the element at index, or returns a subarray starting at the start index and continuing for a specified length. | ||
Negative indices count backward from the end of the array. | ||
|
||
```ruby | ||
a = [ "a", "b", "c", "d", "e" ] | ||
|
||
a[2] #=> "c" | ||
a[6] #=> nil | ||
a[1, 3] #=> [ "b", "c", "d" ] | ||
|
||
a[-1] #=> "e" | ||
a[-2] #=> "d" | ||
a[-3, 2] #=> ["c", "d"] | ||
``` | ||
|
||
### Helper Methods | ||
|
||
There are lots of useful helper methods on arrays. | ||
Here are some of the more common: | ||
|
||
```ruby | ||
fibonacci = [0, 1, 1, 2, 3, 5, 8, 13] | ||
|
||
fibonacci.size #=> 8 | ||
fibonacci.sum #=> 33 | ||
fibonacci.reverse #=> [13, 8, 5, 3, 2, 1, 1, 0] | ||
``` | ||
|
||
## Enumeration | ||
|
||
Rather than using loops to iterate through collections, in Ruby we use enumeration. | ||
|
||
Enumeration is the act of stepping through a collection (in this case an array) and performing some action on each object. Enumeration is a key concept in Ruby and is used for things like sorting, grouping, mapping, reducing, and much more. | ||
|
||
An enumeration to print each word in an array would look like this: | ||
|
||
```ruby | ||
words = ["the", "cat", "sat"] | ||
words.each do |word| | ||
puts word | ||
end | ||
|
||
# Output: | ||
# the | ||
# cat | ||
# sat | ||
``` | ||
|
||
In this example, we have called the `.each` method on our array and passed in a _block_ that takes one parameter (`word`) and prints it out. | ||
|
||
We'll look at _blocks_ in much more depth later in the Track, but for now think of them as anonymous functions that can take zero or more arguments. | ||
They can be defined using the `do...end` syntax (above), or the `{}` syntax (below). | ||
|
||
Here are some other examples of array methods that use blocks: | ||
|
||
```ruby | ||
fibonacci = [0, 1, 1, 2, 3, 5, 8, 13] | ||
|
||
fibonacci.count { |number| number == 1 } #=> 2 | ||
fibonacci.any? { |number| number == 6 } #=> false | ||
fibonacci.select { |number| number.odd? } #=> [1, 1, 3, 5, 13] | ||
fibonacci.all? { |number| number < 20 } #=> true | ||
fibonacci.map { |number| number * 2 } #=> [0, 2, 2, 4, 6, 10, 16, 26] | ||
``` | ||
|
||
## Instructions | ||
|
||
You're an avid bird watcher that keeps track of how many birds have visited your garden in the last seven days. | ||
|
||
You have five tasks, all dealing with the numbers of birds that visited your garden. | ||
|
||
## 1. Check what the counts were last week | ||
|
||
For comparison purposes, you always keep a copy of last week's counts nearby, which were: 0, 2, 5, 3, 7, 8 and 4. Implement the `BirdCount.last_week` method that returns last week's counts: | ||
|
||
```ruby | ||
BirdCount.last_week | ||
# => [0, 2, 5, 3, 7, 8, 4] | ||
``` | ||
|
||
## 2. Check how many birds visited yesterday | ||
|
||
Implement the `BirdCount#yesterday` method to return how many birds visited your garden yesterday. The bird counts are ordered by day, with the first element being the count of the oldest day, and the last element being today's count. | ||
|
||
```ruby | ||
birds_per_day = [2, 5, 0, 7, 4, 1] | ||
bird_count = BirdCount.new(birds_per_day) | ||
bird_count.yesterday | ||
# => 4 | ||
``` | ||
|
||
## 3. Calculate the total number of visiting birds | ||
|
||
Implement the `BirdCount#total` method to return the total number of birds that have visited your garden: | ||
|
||
```ruby | ||
birds_per_day = [2, 5, 0, 7, 4, 1] | ||
bird_count = BirdCount.new(birds_per_day) | ||
bird_count.total | ||
# => 19 | ||
``` | ||
|
||
## 4. Calculate the number of busy days | ||
|
||
Some days are busier than others. A busy day is one where five or more birds have visited your garden. | ||
Implement the `BirdCount#busy_days` method to return the number of busy days: | ||
|
||
```ruby | ||
birds_per_day = [2, 5, 0, 7, 4, 1] | ||
bird_count = BirdCount.new(birds_per_day) | ||
bird_count.busy_days | ||
# => 2 | ||
``` | ||
|
||
## 5. Check if there was a day with no visiting birds | ||
|
||
Implement the `BirdCount#day_without_birds?` method that returns `true` if there was a day at which zero birds visited the garden; otherwise, return `false`: | ||
|
||
```ruby | ||
birds_per_day = [2, 5, 0, 7, 4, 1] | ||
bird_count = BirdCount.new(birds_per_day) | ||
bird_count.day_without_birds? | ||
# => true | ||
``` | ||
|
||
## Source | ||
|
||
### Created by | ||
|
||
- @pvcarrera |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class BirdCount | ||
def self.last_week | ||
[0, 2, 5, 3, 7, 8, 4] | ||
end | ||
|
||
def initialize(birds_per_day) | ||
@birds_per_day = birds_per_day | ||
end | ||
|
||
def yesterday | ||
@birds_per_day[-2] | ||
end | ||
|
||
def total | ||
@birds_per_day.sum | ||
end | ||
|
||
def busy_days | ||
@birds_per_day.count{|n| n >=5} | ||
end | ||
|
||
def day_without_birds? | ||
@birds_per_day.any?{|n| n ==0} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'minitest/autorun' | ||
require_relative 'bird_count' | ||
|
||
class BirdCountTest < Minitest::Test | ||
def test_last_week | ||
assert_equal [0, 2, 5, 3, 7, 8, 4], BirdCount.last_week | ||
end | ||
|
||
def test_yesterday_for_disappointing_week | ||
assert_equal 1, BirdCount.new([0, 0, 1, 0, 0, 1, 0]).yesterday | ||
end | ||
|
||
def test_yesterday_for_busy_week | ||
assert_equal 7, BirdCount.new([8, 8, 9, 5, 4, 7, 10]).yesterday | ||
end | ||
|
||
def test_total_for_disappointing_week | ||
assert_equal 2, BirdCount.new([0, 0, 1, 0, 0, 1, 0]).total | ||
end | ||
|
||
def test_total_for_busy_week | ||
assert_equal 65, BirdCount.new([5, 9, 12, 6, 8, 8, 17]).total | ||
end | ||
|
||
def test_busy_days_for_dissapointing_week | ||
assert_equal 0, BirdCount.new([1, 1, 1, 0, 0, 0, 0]).busy_days | ||
end | ||
|
||
def test_busy_days_for_busy_week | ||
assert_equal 5, BirdCount.new([4, 9, 5, 7, 8, 8, 2]).busy_days | ||
end | ||
|
||
def test_has_day_without_birds | ||
assert BirdCount.new([5, 5, 4, 0, 7, 6]).day_without_birds? | ||
end | ||
|
||
def test_has_day_without_birds_whith_no_day_without_birds | ||
refute BirdCount.new([4, 5, 9, 10, 9, 4, 3]).day_without_birds? | ||
end | ||
end |