From 1d2671e55a55def511c94762cb02227aa54da9e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Santos?= Date: Sun, 31 Oct 2021 13:29:30 +0000 Subject: [PATCH] Add exemplar and tests --- .../bird-watcher/.docs/instructions.md | 2 +- .../concept/bird-watcher/.meta/config.json | 28 +++-- .../concept/bird-watcher/.meta/exemplar.go | 32 ++++++ .../concept/bird-watcher/bird_watcher.go | 2 +- .../concept/bird-watcher/bird_watcher_test.go | 104 ++++++++++++++++++ exercises/concept/bird-watcher/go.mod | 2 +- 6 files changed, 159 insertions(+), 11 deletions(-) diff --git a/exercises/concept/bird-watcher/.docs/instructions.md b/exercises/concept/bird-watcher/.docs/instructions.md index a47758506..128eada05 100644 --- a/exercises/concept/bird-watcher/.docs/instructions.md +++ b/exercises/concept/bird-watcher/.docs/instructions.md @@ -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 diff --git a/exercises/concept/bird-watcher/.meta/config.json b/exercises/concept/bird-watcher/.meta/config.json index 2edadd1b4..96b9c916c 100644 --- a/exercises/concept/bird-watcher/.meta/config.json +++ b/exercises/concept/bird-watcher/.meta/config.json @@ -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" + ] +} \ No newline at end of file diff --git a/exercises/concept/bird-watcher/.meta/exemplar.go b/exercises/concept/bird-watcher/.meta/exemplar.go index e69de29bb..304f21be4 100644 --- a/exercises/concept/bird-watcher/.meta/exemplar.go +++ b/exercises/concept/bird-watcher/.meta/exemplar.go @@ -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 +} diff --git a/exercises/concept/bird-watcher/bird_watcher.go b/exercises/concept/bird-watcher/bird_watcher.go index e38be705a..cfeacb862 100644 --- a/exercises/concept/bird-watcher/bird_watcher.go +++ b/exercises/concept/bird-watcher/bird_watcher.go @@ -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") } diff --git a/exercises/concept/bird-watcher/bird_watcher_test.go b/exercises/concept/bird-watcher/bird_watcher_test.go index e69de29bb..c1623faaa 100644 --- a/exercises/concept/bird-watcher/bird_watcher_test.go +++ b/exercises/concept/bird-watcher/bird_watcher_test.go @@ -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) + } + }) + } +} diff --git a/exercises/concept/bird-watcher/go.mod b/exercises/concept/bird-watcher/go.mod index 2062ab0af..675b1676c 100644 --- a/exercises/concept/bird-watcher/go.mod +++ b/exercises/concept/bird-watcher/go.mod @@ -1,3 +1,3 @@ module birdwatcher -go 1.13 +go 1.14