Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dgunjetti committed Aug 26, 2020
1 parent c1cde97 commit f056e19
Show file tree
Hide file tree
Showing 69 changed files with 440 additions and 397 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.DS_Store
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ func init() {
rand.Seed(time.Now().UnixNano())
}

// GenerateNumbers - random number generation
func GenerateNumbers(max int) []int {
rand.Seed(time.Now().UnixNano())
numbers := make([]int, max)
for i := 0; i < max; i++ {
numbers[i] = rand.Intn(10)
}
return numbers
}

// Add - sequential code to add numbers
func Add(numbers []int) int64 {
var sum int64
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
package counting

import (
"math/rand"
"testing"
)

func generateNumbers(max int) []int {
numbers := make([]int, max)
for i := 0; i < max; i++ {
numbers[i] = rand.Intn(10)
}
return numbers
}

func BenchmarkAdd(b *testing.B) {
numbers := generateNumbers(1e7)
numbers := GenerateNumbers(1e7)
for i := 0; i < b.N; i++ {
Add(numbers)
}
}

func BenchmarkAddConcurrent(b *testing.B) {
numbers := generateNumbers(1e7)
numbers := GenerateNumbers(1e7)
for i := 0; i < b.N; i++ {
AddConcurrent(numbers)
}
Expand Down
20 changes: 20 additions & 0 deletions 01-intro-solution/01-goroutines/04-add/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"time"

"github.com/andcloudio/go-concurrency-exercises/01-intro/01-goroutines/04-add/counting"
)

func main() {
numbers := counting.GenerateNumbers(1e7)

t := time.Now()
_ = counting.Add(numbers)
fmt.Printf("Sequential Add took: %s\n", time.Since(t))

t = time.Now()
_ = counting.AddConcurrent(numbers)
fmt.Printf("Concurrent Add took: %s\n", time.Since(t))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync"
)

//TODO: run the program and check that variable i
// run the program and check that variable i
// was pinned for access from goroutine even after
// enclosing function returns.

Expand All @@ -18,8 +18,9 @@ func main() {
go func() {
defer wg.Done()
i++
fmt.Println(i)
fmt.Printf("value of i: %v\n", i)
}()
fmt.Println("return from function")
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ func main() {
var wg sync.WaitGroup

// what is the output
//TODO: fix the issue.
// fix the issue.

for i := 0; i < 3; i++ {
for i := 1; i <= 3; i++ {
wg.Add(1)
go func() {
go func(i int) {
defer wg.Done()
fmt.Println(i)
}()
}(i)
}
wg.Wait()
}
15 changes: 15 additions & 0 deletions 01-intro-solution/02-channel/01-channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "fmt"

func main() {
ch := make(chan int)

go func(a, b int) {
c := a + b
ch <- c
}(1, 2)
// get the value computed from goroutine
c := <-ch
fmt.Printf("computed value %v\n", c)
}
18 changes: 18 additions & 0 deletions 01-intro-solution/02-channel/02-channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "fmt"

func main() {
ch := make(chan int)
go func() {
for i := 0; i < 6; i++ {
// send iterator over channel
ch <- i
}
close(ch)
}()
// range over channel to recv values
for i := range ch {
fmt.Println(i)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
)

func main() {
ch := make(chan int)
ch := make(chan int, 6)

go func() {
defer close(ch)

// TODO: send all iterator values on channel without blocking
// send all iterator values on channel without blocking
for i := 0; i < 6; i++ {
fmt.Printf("Sending: %d\n", i)
ch <- i
Expand Down
33 changes: 33 additions & 0 deletions 01-intro-solution/02-channel/04-channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import "fmt"

// TODO: Implement ping pong with Channel Direction

func ping(out chan<- string) {
// send message on ch1
out <- "ping"
}

func pong(in <-chan string, out chan<- string) {
// recv message on ch1
msg := <-in
msg = msg + " pong"
// send it on ch2
out <- msg
}

func main() {
// create ch1 and ch2
ch1 := make(chan string)
ch2 := make(chan string)

// spine goroutine ping and pong
go ping(ch1)
go pong(ch1, ch2)

// recv message on ch2
msg := <-ch2

fmt.Println(msg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ package main
import "fmt"

func main() {
//TODO: create channel owner goroutine which return channel and
// create channel owner goroutine which return channel and
// writes data into channel and
// closes the channel when done.

owner := func() chan int {
ch := make(chan int)
go func() {
defer close(ch)
for i := 0; i < 6; i++ {
ch <- i
}
}()
return ch
}

consumer := func(ch <-chan int) {
// read values from channel
for v := range ch {
Expand Down
26 changes: 0 additions & 26 deletions 01-intro-solution/02-join/02-join/main.go

This file was deleted.

26 changes: 0 additions & 26 deletions 01-intro-solution/03-closure/03-closure/main.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"time"
)

Expand All @@ -18,6 +19,14 @@ func main() {
ch2 <- "two"
}()

// TODO: multiplex recv on channel - ch1, ch2
// multiplex recv on channel - ch1, ch2

for i := 0; i < 2; i++ {
select {
case msg1 := <-ch1:
fmt.Println(msg1)
case msg2 := <-ch2:
fmt.Println(msg2)
}
}
}
23 changes: 23 additions & 0 deletions 01-intro-solution/03-select/02-select/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
"time"
)

func main() {
ch := make(chan string, 1)

go func() {
time.Sleep(2 * time.Second)
ch <- "one"
}()

//implement timeout for recv on channel ch
select {
case m := <-ch:
fmt.Println(m)
case <-time.After(1 * time.Second):
fmt.Println("timeout")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ func main() {
ch <- "one"
}()

// TODO: if there is no value on channel, do not block.
// if there is no value on channel, do not block.

m := <-ch
fmt.Println(m)
select {
case m := <-ch:
fmt.Println(m)
default:
fmt.Println("no message in channel")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ func main() {

var balance int
var wg sync.WaitGroup
var mu sync.Mutex

deposit := func(amount int) {
mu.Lock()
balance += amount
mu.Unlock()
}

withdrawal := func(amount int) {
mu.Lock()
defer mu.Unlock()
balance -= amount
}

// make 100 deposits of $1
// and 100 withdrawal of $1 concurrently.
// we are making 100 times deposits of $1
// and 100 times withdrawal of $1, concurrently.
// run the program and check result.

// TODO: fix the issue for consistent output.
Expand Down
Loading

0 comments on commit f056e19

Please sign in to comment.