mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-16 18:21:16 +00:00
first
This commit is contained in:
35
course/13-channels/exercises/6-range/code.go
Normal file
35
course/13-channels/exercises/6-range/code.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func concurrrentFib(n int) {
|
||||
// ?
|
||||
}
|
||||
|
||||
// TEST SUITE - Don't touch below this line
|
||||
|
||||
func test(n int) {
|
||||
fmt.Printf("Printing %v numbers...\n", n)
|
||||
concurrrentFib(n)
|
||||
fmt.Println("==============================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10)
|
||||
test(5)
|
||||
test(20)
|
||||
test(13)
|
||||
}
|
||||
|
||||
func fibonacci(n int, ch chan int) {
|
||||
x, y := 0, 1
|
||||
for i := 0; i < n; i++ {
|
||||
ch <- x
|
||||
x, y = y, x+y
|
||||
time.Sleep(time.Millisecond * 10)
|
||||
}
|
||||
close(ch)
|
||||
}
|
||||
40
course/13-channels/exercises/6-range/complete.go
Normal file
40
course/13-channels/exercises/6-range/complete.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func concurrrentFib(n int) {
|
||||
ch := make(chan int)
|
||||
go fibonacci(n, ch)
|
||||
|
||||
for v := range ch {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
|
||||
// TEST SUITE - Don't touch below this line
|
||||
|
||||
func test(n int) {
|
||||
fmt.Printf("Printing %v numbers...\n", n)
|
||||
concurrrentFib(n)
|
||||
fmt.Println("==============================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10)
|
||||
test(5)
|
||||
test(20)
|
||||
test(13)
|
||||
}
|
||||
|
||||
func fibonacci(n int, ch chan int) {
|
||||
x, y := 0, 1
|
||||
for i := 0; i < n; i++ {
|
||||
ch <- x
|
||||
x, y = y, x+y
|
||||
time.Sleep(time.Millisecond * 10)
|
||||
}
|
||||
close(ch)
|
||||
}
|
||||
56
course/13-channels/exercises/6-range/expected.txt
Normal file
56
course/13-channels/exercises/6-range/expected.txt
Normal file
@@ -0,0 +1,56 @@
|
||||
Printing 10 numbers...
|
||||
0
|
||||
1
|
||||
1
|
||||
2
|
||||
3
|
||||
5
|
||||
8
|
||||
13
|
||||
21
|
||||
34
|
||||
==============================
|
||||
Printing 5 numbers...
|
||||
0
|
||||
1
|
||||
1
|
||||
2
|
||||
3
|
||||
==============================
|
||||
Printing 20 numbers...
|
||||
0
|
||||
1
|
||||
1
|
||||
2
|
||||
3
|
||||
5
|
||||
8
|
||||
13
|
||||
21
|
||||
34
|
||||
55
|
||||
89
|
||||
144
|
||||
233
|
||||
377
|
||||
610
|
||||
987
|
||||
1597
|
||||
2584
|
||||
4181
|
||||
==============================
|
||||
Printing 13 numbers...
|
||||
0
|
||||
1
|
||||
1
|
||||
2
|
||||
3
|
||||
5
|
||||
8
|
||||
13
|
||||
21
|
||||
34
|
||||
55
|
||||
89
|
||||
144
|
||||
==============================
|
||||
21
course/13-channels/exercises/6-range/readme.md
Normal file
21
course/13-channels/exercises/6-range/readme.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# Range
|
||||
|
||||
Similar to slices and maps, channels can be ranged over.
|
||||
|
||||
```go
|
||||
for item := range ch {
|
||||
// item is the next value received from the channel
|
||||
}
|
||||
```
|
||||
|
||||
This example will receive values over the channel (blocking at each iteration if nothing new is there) and will exit only when the channel is closed.
|
||||
|
||||
## Assignment
|
||||
|
||||
It's that time again, Mailio is hiring and we've been assigned to do the interview. For some reason, the [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number) is Mailio's interview problem of choice. We've been tasked with building a small toy program we can use in the interview.
|
||||
|
||||
Complete the `concurrrentFib` function. It should:
|
||||
|
||||
* Create a new channel of `int`s
|
||||
* Call `fibonacci` in a goroutine, passing it the channel and the number of Fibonacci numbers to generate, `n`
|
||||
* Use a `range` loop to read from the channel and print out the numbers one by one, each on a new line
|
||||
Reference in New Issue
Block a user