+ 2
Go downloader
Currently I'm confused on the "Downloader" project in the Go course. I'm not sure if the project is just generally hard, or if I'm misunderstanding it. But I'm confused with getting the value to return from the `download()` function.
13 ответов
+ 4
Get the sum of range 0-N (inclusive) with the call of download(N). The sum should send to the channel passed in download.
After 3 download() complete, output the sum of them.
Example: download(4, ch1) makes ch1 have the value 6.
+ 15
package main
import "fmt"
//define the download() function
func download(s int, c chan int) {
var sum int
sum = 0
for i:=0; i<=s; i++ {
sum += i
}
c<-sum
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
var s1, s2, s3 int
fmt.Scanln(&s1)
fmt.Scanln(&s2)
fmt.Scanln(&s3)
go download(s1, ch1)
go download(s2, ch2)
go download(s3, ch3)
//output the sum of all results
fmt.Println(<-ch2 + <-ch1 + <-ch3)
}
+ 1
Match Results
any help guys plz ?
You are making a program to analyze sport match results and calculate the points of the given team.
The match results are stored in an array called results.
Each match has one of the following results:
"w" - won
"l" - lost
"d" - draw
A win adds three points, a draw adds one point, and a lost match does not add any points.
Your program needs to take the last match result as input and append it to the results array. After that, calculate and output the total points the team gained from the results
package main
import "fmt"
func main() {
results := []string{"w", "l", "w", "d", "w", "l", "l", "l", "d", "d", "w", "l", "w", "d"}
}
+ 1
package main
import "fmt"
//define the download() function
func download(file_size int, chanel chan int) {
chanel <- (file_size * (file_size + 1)) / 2
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
var s1, s2, s3 int
fmt.Scanln(&s1)
fmt.Scanln(&s2)
fmt.Scanln(&s3)
go download(s1, ch1)
go download(s2, ch2)
go download(s3, ch3)
//output the sum of all results
fmt.Println(<-ch1 + <-ch2 + <-ch3)
}
+ 1
package main
import "fmt"
//define the download() function
func download(s int, ch chan int){
var som int
for i:=0; i<=s; i++{
som+=i
}
ch<-som
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
var s1, s2, s3 int
fmt.Scanln(&s1)
fmt.Scanln(&s2)
fmt.Scanln(&s3)
go download(s1, ch1)
go download(s2, ch2)
go download(s3, ch3)
//output the sum of all results
fmt.Println(<-ch1 + <-ch2 + <-ch3)
}
0
CarrielForle : I still do not understand...
0
Yo, I am also stuck here; Pls send the code and it's logic because if we don't no the logic we can't ever solve it.
0
Thanks bro
0
if u dont understant , that s gonna clarify things to you we wait for the ch1 and ch2 and ch3 to be completed so that printline can be executed
0
there is no need to write a loop for the sum of all integers from 0 to "s", it a triangular number which means we can use its formula to calculate the value without a loop, like
```
package main
import "fmt"
//define the download() function
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
var s1, s2, s3 int
fmt.Scanln(&s1)
fmt.Scanln(&s2)
fmt.Scanln(&s3)
go download(s1, ch1)
go download(s2, ch2)
go download(s3, ch3)
//output the sum of all results
fmt.Println(<-ch1 + <-ch2 + <-ch3)
}
func download(i int, ch chan int){
ch <- (i * (i + 1)) /2
}
```
0
package main
import "fmt"
//define the download() function
func download(s int, c chan int) {
var sum int
sum = 0
for i:=0; i<=s; i++ {
sum += i
}
c<-sum
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
var s1, s2, s3 int
fmt.Scanln(&s1)
fmt.Scanln(&s2)
fmt.Scanln(&s3)
go download(s1, ch1)
go download(s2, ch2)
go download(s3, ch3)
//output the sum of all results
fmt.Println(<-ch2 + <-ch1 + <-ch3)
}
- 1
package main
import "fmt"
//define the download() function
func download(s int, c chan int) {
var sum int
for i:=0; i<=s; i++ {
sum += i
}
c<-sum
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
var s1, s2, s3 int
fmt.Scanln(&s1)
fmt.Scanln(&s2)
fmt.Scanln(&s3)
go download(s1, ch1)
go download(s2, ch2)
go download(s3, ch3)
//output the sum of all results
fmt. Println(<-ch2 + <-ch1 + <-ch3)
}
- 2
package main
import "fmt"
//define the download() function
func download(s int, c chan) {
var sum int
sum := 0
for i:=0; i<=s; i++ {
sum += i
}
c<-sum
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
var s1, s2, s3 int
fmt.Scanln(&s1)
fmt.Scanln(&s2)
fmt.Scanln(&s3)
go download(s1, ch1)
go download(s2, ch2)
go download(s3, ch3)
//output the sum of all results
fmt.Println(<-download)
}