-
-
Notifications
You must be signed in to change notification settings - Fork 658
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
e8b40a1
commit 1d2671e
Showing
6 changed files
with
159 additions
and
11 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
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 |
---|---|---|
@@ -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" | ||
] | ||
} |
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,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 | ||
} |
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
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,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) | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module birdwatcher | ||
|
||
go 1.13 | ||
go 1.14 |