Skip to content

Commit 121ffb6

Browse files
committed
feat: Add bus factor solution.
1 parent d2f4e64 commit 121ffb6

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ codingcircle contains the examples from the YouTube Coding Cirle.
77
So far, the following exercises have been covered:
88

99
- [Autocomplete](./autocomplete/) – implements an autocomplete feature using a trie
10+
- [Busfactor](./busfactor/) – calculates the the maximum load of a bus based on events
1011
- [Cons, Car, Cdr & co.](./cons) – implements a cons cell and the corresponding functions
1112
- [Continous maximum](./continuousmax/) – calculates the maximum of a sliding window
1213
- [Floyd](./floyd/) – implements Floyd's cycle-finding "Tortoise and Hare" algorithm

busfactor/documentation.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package busfactor calculates the maximum load of a bus based on events.
2+
package busfactor

busfactor/get_maximum_load.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package busfactor
2+
3+
import (
4+
"errors"
5+
"time"
6+
)
7+
8+
type Event struct {
9+
Time time.Time
10+
Type string
11+
Count int
12+
}
13+
14+
type Result struct {
15+
Count int
16+
From time.Time
17+
Until time.Time
18+
}
19+
20+
func GetMaximumLoad(events []Event) Result {
21+
currentLoad := 0
22+
maxLoad := 0
23+
maxLoadStartTime := events[0].Time
24+
maxLoadEndTime := events[0].Time
25+
26+
for i, event := range events {
27+
switch event.Type {
28+
case "joined":
29+
currentLoad += event.Count
30+
case "left":
31+
currentLoad -= event.Count
32+
default:
33+
panic(errors.New("unknown event type"))
34+
}
35+
36+
if currentLoad > maxLoad {
37+
maxLoad = currentLoad
38+
maxLoadStartTime = event.Time
39+
if i+1 < len(events) {
40+
maxLoadEndTime = events[i+1].Time
41+
} else {
42+
maxLoadEndTime = event.Time
43+
}
44+
}
45+
}
46+
47+
return Result{
48+
Count: maxLoad,
49+
From: maxLoadStartTime,
50+
Until: maxLoadEndTime,
51+
}
52+
}

busfactor/get_maximum_load_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package busfactor_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/thenativeweb/codingcircle/busfactor"
9+
)
10+
11+
func TestGetMaximumLoad(t *testing.T) {
12+
t.Run("with a single event", func(t *testing.T) {
13+
now := time.Now()
14+
15+
result := busfactor.GetMaximumLoad([]busfactor.Event{
16+
{Time: now, Type: "joined", Count: 3},
17+
})
18+
19+
assert.Equal(t, 3, result.Count)
20+
assert.Equal(t, now, result.From)
21+
assert.Equal(t, now, result.Until)
22+
})
23+
24+
t.Run("with multiple events", func(t *testing.T) {
25+
now := time.Now()
26+
27+
result := busfactor.GetMaximumLoad([]busfactor.Event{
28+
{Time: now, Type: "joined", Count: 3}, // 3
29+
{Time: now.Add(1 * time.Second), Type: "left", Count: 1}, // 2
30+
{Time: now.Add(2 * time.Second), Type: "joined", Count: 5}, // 7
31+
{Time: now.Add(3 * time.Second), Type: "left", Count: 4}, // 3
32+
{Time: now.Add(4 * time.Second), Type: "joined", Count: 2}, // 5
33+
{Time: now.Add(5 * time.Second), Type: "left", Count: 4}, // 1
34+
})
35+
36+
assert.Equal(t, 7, result.Count)
37+
assert.Equal(t, now.Add(2*time.Second), result.From)
38+
assert.Equal(t, now.Add(3*time.Second), result.Until)
39+
})
40+
}

0 commit comments

Comments
 (0)