Skip to content

Commit 67cb0d2

Browse files
authored
bird-watcher: add "for loop" concept (#1775)
1 parent 6d1151a commit 67cb0d2

File tree

13 files changed

+481
-0
lines changed

13 files changed

+481
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"blurb": "Execute the same piece of code several times",
3+
"authors": [
4+
"sachinmk27",
5+
"andrerfcsantos"
6+
],
7+
"contributors": []
8+
}

concepts/for-loops/about.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# About
2+
3+
## General syntax
4+
5+
The for loop is one of the most commonly used statements to repeatedly execute some logic. In Go it consists of the `for` keyword, a header and a code block that contains the body of the loop wrapped in curly brackets. The header consists of 3 components separated by semicolons `;`: init, condition and post.
6+
7+
```go
8+
for init; condition; post {
9+
// loop body - code that is executed repeatedly as long as the condition is true
10+
}
11+
```
12+
13+
- The **init** component is some code that runs only once before the loop starts.
14+
- The **condition** component must be some expression that evaluates to a boolean and controls when the loop should stop. The code inside the loop will run as long as this condition evaluates to true. As soon as this expression evaluates to false, no more iterations of the loop will run.
15+
- The **post** component is some code that will run at the end of each iteration.
16+
17+
**Note:** Unlike other languages, there are no parentheses `()` surrounding the three components of the header. In fact, inserting such parenthesis is a compilation error. However, the braces `{ }` surrounding the loop body are always required.
18+
19+
## For Loops - An example
20+
21+
The init component usually sets up a counter variable, the condition checks whether the loop should be continued or stopped and the post component usually increments the counter at the end of each repetition.
22+
23+
```go
24+
for i := 1; i < 10; i++ {
25+
fmt.Println(i)
26+
}
27+
```
28+
29+
This loop will print the numbers from `1` to `9` (including `9`).
30+
Defining the step is often done using an increment or decrement statement, as shown in the example above.
31+
32+
## Optional components of the header
33+
34+
The init and post components of the header are optional:
35+
36+
```go
37+
var sum = 1
38+
for sum < 1000 {
39+
sum += sum
40+
}
41+
fmt.Println(sum)
42+
// Output: 1024
43+
```
44+
45+
By omitting the init and post component in a for loop like shown above, you create a while loop in Go.
46+
There is no `while` keyword.
47+
This is an example of Go's principle that concepts should be orthogonal. Since there is already a concept to achieve the behavior of a while loop, namely the for loop, `while` was not added as an additional concept.
48+
49+
## Break and Continue
50+
51+
Inside a loop body you can use the `break` keyword to stop the execution of the loop entirely:
52+
53+
```go
54+
for n := 0; n <= 5; n++ {
55+
if n == 3 {
56+
break
57+
}
58+
fmt.Println(n)
59+
}
60+
// Output:
61+
// 0
62+
// 1
63+
// 2
64+
```
65+
66+
In contrast, the keyword `continue` only stops the execution of the current iteration and continues with the next one:
67+
68+
```go
69+
for n := 0; n <= 5; n++ {
70+
if n%2 == 0 {
71+
continue
72+
}
73+
fmt.Println(n)
74+
}
75+
// Output:
76+
// 1
77+
// 3
78+
// 5
79+
```
80+
## Infinite for loop
81+
82+
The condition part of the loop header is also optional. In fact, you can write a loop with no header:
83+
84+
```go
85+
for {
86+
// Endless loop...
87+
}
88+
```
89+
90+
This loop will only ever finish if the program exits or has a `break` in its body.
91+
92+
## Labels and goto
93+
94+
When we use `break`, Go will stop running the most inner loop. Similarly, when we use `continue`, Go will run the next iteration of the most inner loop.
95+
96+
However, this is not always desirable. We can use labels together with `break` and `continue` to specifcy exactly from which loop we want to exit or continue, respectively.
97+
98+
In this example we are creating a label `OuterLoop`, that will refer to the most outter loop. In the most inner loop, to say that we want exit from the most outter loop, we then use `break` followed by the name of the label of the most outter loop:
99+
100+
```go
101+
OuterLoop:
102+
for i := 0; i < 10; i++ {
103+
for j := 0; j < 10; j++ {
104+
// ...
105+
break OuterLoop
106+
}
107+
}
108+
```
109+
110+
Using labels with `continue` would also work, in which case, Go would continue in the next iteration of the loop referenced by the label.
111+
112+
Go also has a `goto` keyword that works in a similar way and allows us to jump to from a piece of code to another labeled piece of code.
113+
114+
**Warning:** Even though Go allows to jump to a piece of code marked with a label, using this feature of the language can easily make the code very hard to read. For this reason, using labels is often not recommended.

concepts/for-loops/introduction.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Introduction
2+
3+
4+
## General syntax
5+
6+
The for loop is one of the most commonly used statements to repeatedly execute some logic. In Go it consists of the `for` keyword, a header and a code block that contains the body of the loop wrapped in curly brackets. The header consists of 3 components separated by semicolons `;`: init, condition and post.
7+
8+
```go
9+
for init; condition; post {
10+
// loop body - code that is executed repeatedly as long as the condition is true
11+
}
12+
```
13+
14+
- The **init** component is some code that runs only once before the loop starts.
15+
- The **condition** component must be some expression that evaluates to a boolean and controls when the loop should stop. The code inside the loop will run as long as this condition evaluates to true. As soon as this expression evaluates to false, no more iterations of the loop will run.
16+
- The **post** component is some code that will run at the end of each iteration.
17+
18+
**Note:** Unlike other languages, there are no parentheses `()` surrounding the three components of the header. In fact, inserting such parenthesis is a compilation error. However, the braces `{ }` surrounding the loop body are always required.
19+
20+
## For Loops - An example
21+
22+
The init component usually sets up a counter variable, the condition checks whether the loop should be continued or stopped and the post component usually increments the counter at the end of each repetition.
23+
24+
```go
25+
for i := 1; i < 10; i++ {
26+
fmt.Println(i)
27+
}
28+
```
29+
30+
This loop will print the numbers from `1` to `9` (including `9`).
31+
Defining the step is often done using an increment or decrement statement, as shown in the example above.

concepts/for-loops/links.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"url": "https://tour.golang.org/flowcontrol/1",
4+
"description": "Tour of Go: For"
5+
},
6+
{
7+
"url": "https://gobyexample.com/for",
8+
"description": "Go by Example: For"
9+
},
10+
{
11+
"url": "https://yourbasic.org/golang/for-loop/",
12+
"description": "Article: 5 basic for loop patterns"
13+
}
14+
]

config.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@
214214
],
215215
"status": "wip"
216216
},
217+
{
218+
"slug": "bird-watcher",
219+
"name": "Bird Watcher",
220+
"uuid": "bdb493de-b07d-4669-9890-5bebeddbf1e8",
221+
"concepts": [
222+
"for-loops"
223+
],
224+
"prerequisites": [
225+
"numbers",
226+
"conditionals-if",
227+
"slices"
228+
],
229+
"status": "beta"
230+
},
217231
{
218232
"name": "Card Tricks",
219233
"slug": "card-tricks",
@@ -1907,6 +1921,11 @@
19071921
"slug": "floating-point-numbers",
19081922
"uuid": "542b0434-59fd-4976-9d5b-c3c5c07e78d7"
19091923
},
1924+
{
1925+
"name": "For Loops",
1926+
"slug": "for-loops",
1927+
"uuid": "9cf34401-7993-4b09-8a5e-55347acda4ca"
1928+
},
19101929
{
19111930
"name": "Iteration",
19121931
"slug": "iteration",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Hints
2+
3+
## 1. Determine the total number of birds that you counted so far
4+
5+
- Refer to the exercise introduction for an example how to use a for loop to iterate over a slice.
6+
- Use a helper variable to store the total count and increase that variable as you go through the slice.
7+
- Think about the correct initial value for that helper variable.
8+
9+
## 2. Calculate the number of visiting birds in a specific week
10+
11+
- This task is similar to the first one, you can copy your code as a starting point.
12+
- Think about which indexes in the slice you would need to take into account for week number 1 and 2, respectively.
13+
- Now find a general way to calculate the first and the last index that should be considered.
14+
- With that you can set up the for loop to only iterate over the relevant section of the slice.
15+
16+
## 3. Fix a counting mistake
17+
18+
- Again you need to set up a for loop to iterate over the whole bird count slice.
19+
- This time you only need to visit every second entry in the slice.
20+
- Change the step so the counter variable is increased accordingly after each iteration.
21+
- In the body of the for loop you can use the increment operator to change the value of an element in a slice in-place.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Instructions
2+
3+
You are an avid bird watcher that keeps track of how many birds have visited your garden.
4+
Usually you use a tally in a notebook to count the birds, but to better work with your data, you've digitalized the bird counts for the past weeks.
5+
6+
## 1. Determine the total number of birds that you counted so far
7+
8+
Let us start analyzing the data by getting a high level view.
9+
Find out how many birds you counted in total since you started your logs.
10+
11+
Implement a function `TotalBirdCount` that accepts a slice of `int`s that contains the bird count per day.
12+
It should return the total number of birds that you counted.
13+
14+
```go
15+
birdsPerDay := []int{2, 5, 0, 7, 4, 1, 3, 0, 2, 5, 0, 1, 3, 1}
16+
TotalBirdCount(birdsPerDay)
17+
// => 34
18+
```
19+
20+
## 2. Calculate the number of visiting birds in a specific week
21+
22+
Now that you got a general feel for your bird count numbers, you want to make a more fine-grained analysis.
23+
24+
Implement a function `BirdsInWeek` that accepts a slice of bird counts per day and a week number.
25+
26+
It returns the total number of birds that you counted in that specific week.
27+
You can assume weeks are always tracked completely.
28+
29+
```go
30+
birdsPerDay := []int{2, 5, 0, 7, 4, 1, 3, 0, 2, 5, 0, 1, 3, 1}
31+
BirdsInWeek(birdsPerDay, 2)
32+
// => 12
33+
```
34+
35+
## 3. Fix a counting mistake
36+
37+
You realized that all the time you were trying to keep track of the birds, there was one bird that was hiding in a far corner of the garden.
38+
39+
You figured out that this bird always spent every second day in your garden.
40+
41+
You do not know exactly where it was in between those days but definitely not in your garden.
42+
43+
Your bird watcher intuition also tells you that the bird was in your garden on the first day that you tracked in your list.
44+
45+
Given this new information, write a function `FixBirdCountLog` that takes a slice of birds counted per day as an argument and returns the slice after correcting the counting mistake.
46+
47+
```go
48+
birdsPerDay := []int{2, 5, 0, 7, 4, 1}
49+
FixBirdCountLog(birdsPerDay)
50+
// => [3 5 1 7 5 1]
51+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Introduction
2+
3+
## General syntax
4+
5+
The for loop is one of the most commonly used statements to repeatedly execute some logic. In Go it consists of the `for` keyword, a header and a code block that contains the body of the loop wrapped in curly brackets. The header consists of 3 components separated by semicolons `;`: init, condition and post.
6+
7+
```go
8+
for init; condition; post {
9+
// loop body - code that is executed repeatedly as long as the condition is true
10+
}
11+
```
12+
13+
- The **init** component is some code that runs only once before the loop starts.
14+
- The **condition** component must be some expression that evaluates to a boolean and controls when the loop should stop. The code inside the loop will run as long as this condition evaluates to true. As soon as this expression evaluates to false, no more iterations of the loop will run.
15+
- The **post** component is some code that will run at the end of each iteration.
16+
17+
**Note:** Unlike other languages, there are no parentheses `()` surrounding the three components of the header. In fact, inserting such parenthesis is a compilation error. However, the braces `{ }` surrounding the loop body are always required.
18+
19+
## For Loops - An example
20+
21+
The init component usually sets up a counter variable, the condition checks whether the loop should be continued or stopped and the post component usually increments the counter at the end of each repetition.
22+
23+
```go
24+
for i := 1; i < 10; i++ {
25+
fmt.Println(i)
26+
}
27+
```
28+
29+
This loop will print the numbers from `1` to `9` (including `9`).
30+
Defining the step is often done using an increment or decrement statement, as shown in the example above.
31+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"blurb": "Count the birds in your garden with for loops.",
3+
"authors": [
4+
"sachinmk27"
5+
],
6+
"contributors": [
7+
"andrerfcsantos"
8+
],
9+
"files": {
10+
"solution": [
11+
"bird_watcher.go"
12+
],
13+
"test": [
14+
"bird_watcher_test.go"
15+
],
16+
"exemplar": [
17+
".meta/exemplar.go"
18+
]
19+
},
20+
"forked_from": [
21+
"javascript/bird-watcher"
22+
]
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package birdwatcher
2+
3+
// TotalBirdCount return the total bird count by summing
4+
// the individual day's counts.
5+
func TotalBirdCount(birdsPerDay []int) int {
6+
count := 0
7+
for i := 0; i < len(birdsPerDay); i++ {
8+
count += birdsPerDay[i]
9+
}
10+
return count
11+
}
12+
13+
// BirdsInWeek returns the total bird count by summing
14+
// only the items belonging to the given week.
15+
func BirdsInWeek(birdsPerDay []int, week int) int {
16+
count := 0
17+
startOfWeek := (week - 1) * 7
18+
endOfWeek := startOfWeek + 7
19+
for i := startOfWeek; i < endOfWeek; i++ {
20+
count += birdsPerDay[i]
21+
}
22+
return count
23+
}
24+
25+
// FixBirdCountLog returns the bird counts after correcting
26+
// the bird counts for alternate days.
27+
func FixBirdCountLog(birdsPerDay []int) []int {
28+
for i := 0; i < len(birdsPerDay); i += 2 {
29+
birdsPerDay[i]++
30+
}
31+
return birdsPerDay
32+
}

0 commit comments

Comments
 (0)