Skip to content

Commit

Permalink
Add exemplar and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerfcsantos committed Oct 31, 2021
1 parent e8b40a1 commit 1d2671e
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 11 deletions.
2 changes: 1 addition & 1 deletion exercises/concept/bird-watcher/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Usually you use a tally in a notebook to count the birds, but to better work wit
Let us start analyzing the data by getting a high level view.
Find out how many birds you counted in total since you started your logs.

Implement a function `TotalBirdCount` that accepts an array that contains the bird count per day.
Implement a function `TotalBirdCount` that accepts an slice that contains the bird count per day.
It should return the total number of birds that you counted.

```go
Expand Down
28 changes: 20 additions & 8 deletions exercises/concept/bird-watcher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
{
"blurb": "Professionalize counting the birds in your garden with for loops.",
"authors": ["sachinmk27"],
"contributors": [],
"blurb": "Count the birds in your garden with for loops.",
"authors": [
"sachinmk27"
],
"contributors": [
"andrerfcsantos"
],
"files": {
"solution": ["bird_watcher.go"],
"test": ["bird_watcher_test.go"],
"exemplar": [".meta/exemplar.go"]
"solution": [
"bird_watcher.go"
],
"test": [
"bird_watcher_test.go"
],
"exemplar": [
".meta/exemplar.go"
]
},
"forked_from": ["javascript/bird-watcher"]
}
"forked_from": [
"javascript/bird-watcher"
]
}
32 changes: 32 additions & 0 deletions exercises/concept/bird-watcher/.meta/exemplar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package birdwatcher

// TotalBirdCount return the total bird count by summing
// the individual day's counts
func TotalBirdCount(birdsPerDay []int) int {
count := 0
for i := 0; i < len(birdsPerDay); i++ {
count += birdsPerDay[i]
}
return count
}

// BirdsInWeek returns the total bird count by summing
// only the items belonging to the given week
func BirdsInWeek(birdsPerDay []int, week int) int {
count := 0
startOfWeek := (week - 1) * 7
endOfWeek := startOfWeek + 7
for i := startOfWeek; i < endOfWeek; i++ {
count += birdsPerDay[i]
}
return count
}

// FixBirdCountLog returns the bird counts after correcting
// the bird counts for alternate days
func FixBirdCountLog(birdsPerDay []int) []int {
for i := 0; i < len(birdsPerDay); i += 2 {
birdsPerDay[i] += 1
}
return birdsPerDay
}
2 changes: 1 addition & 1 deletion exercises/concept/bird-watcher/bird_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func TotalBirdCount(birdsPerDay []int) int {

// BirdsInWeek returns the total bird count by summing
// only the items belonging to the given week
func BirdsInWeek(birdsPerDay []int) int {
func BirdsInWeek(birdsPerDay []int, week int) int {
panic("Please implement the BirdsInWeek() function")
}

Expand Down
104 changes: 104 additions & 0 deletions exercises/concept/bird-watcher/bird_watcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package birdwatcher

import (
"reflect"
"testing"
)

func TestTotalBirdCount(t *testing.T) {
tests := []struct {
name string
birdCounts []int
want int
}{
{
name: "calculates the correct total number of birds",
birdCounts: []int{9, 0, 8, 4, 5, 1, 3},
want: 30,
},
{
name: "works for a short bird count list",
birdCounts: []int{2},
want: 2,
},
{
name: "works for a long bird count list",
birdCounts: []int{2, 8, 4, 1, 3, 5, 0, 4, 1, 6, 0, 3, 0, 1, 5, 4, 1, 1, 2, 6},
want: 57,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TotalBirdCount(tt.birdCounts); got != tt.want {
t.Errorf("TotalBirdCount(%v) = %v; want %v", tt.birdCounts, got, tt.want)
}
})
}
}

func TestBirdsInWeek(t *testing.T) {
tests := []struct {
name string
birdCounts []int
week int
want int
}{
{
name: "calculates the number of birds in the first week",
birdCounts: []int{3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0, 8, 0},
week: 1,
want: 14,
},
{
name: "calculates the number of birds for a week in the middle of the log",
birdCounts: []int{4, 7, 3, 2, 1, 1, 2, 0, 2, 3, 2, 7, 1, 3, 0, 6, 5, 3, 7, 2, 3},
week: 2,
want: 18,
},
{
name: "works when there is only one week",
birdCounts: []int{3, 0, 3, 3, 2, 1, 0},
week: 1,
want: 12,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := BirdsInWeek(tt.birdCounts, tt.week); got != tt.want {
t.Errorf("BirdsInWeek(%v) = %v; want %v", tt.birdCounts, got, tt.want)
}
})
}
}

func TestFixBirdCount(t *testing.T) {
tests := []struct {
name string
birdCounts []int
week int
want []int
}{
{
name: "returns a bird count list with the corrected values",
birdCounts: []int{3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0},
want: []int{4, 0, 6, 1, 1, 4, 2, 0, 4, 4, 4, 0},
},
{
name: "works for a short bird count list",
birdCounts: []int{4, 2},
want: []int{5, 2},
},
{
name: "works for a long bird count list",
birdCounts: []int{2, 8, 4, 1, 3, 5, 0, 4, 1, 6, 0, 3, 0, 1, 5, 4, 1, 1, 2, 6},
want: []int{3, 8, 5, 1, 4, 5, 1, 4, 2, 6, 1, 3, 1, 1, 6, 4, 2, 1, 3, 6},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FixBirdCountLog(tt.birdCounts); !reflect.DeepEqual(tt.want, got) {
t.Errorf("FixBirdCountLog(%v) = %v; want %v", tt.birdCounts, got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion exercises/concept/bird-watcher/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module birdwatcher

go 1.13
go 1.14

0 comments on commit 1d2671e

Please sign in to comment.