Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 10 additions & 36 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ func sender(c chan signalData) {
}

func receiver(c chan signalData) {
idleTimeout := time.Second
idleTimer := time.NewTimer(idleTimeout)
defer idleTimer.Stop()

for {
idleTimer.Reset(idleTimeout)
select {
case signalData := <-c:
signalData.status = validateSignal(signalData.signal)
fmt.Println("Receiving Signal ", signalData.signal, " with status ", signalData.status)
case <-time.After(time.Second * 1):
case <-idleTimer.C:
fmt.Println("Got timeout while receiving the signal")
return
}
Expand All @@ -60,50 +65,19 @@ func generateSignal() []int {
}

func validateSignal(signal []int) string {
// if the signal begin with 1 and end with 1, it indicates the signal is good
// if the signal begin with 0 and end with 0, it inditates the signal is bad
// if doesn't meet 2 conditions above, evaluate from all node
// in my understanding, based on the logic explained, we only need to check the last digit of the signal to get the desired outcome
// less complexity means less code means less bug potential, saving us from future headache of debugging

switch {
case signal[0] == 1 && signal[maxDigit-1] == 1:
case signal[maxDigit-1] == 1:
return "good"
case signal[0] == 0 && signal[maxDigit-1] == 0:
case signal[maxDigit-1] == 0:
return "bad"
default:
return evaluateSignal(signal)
}
}

func evaluateSignal(signal []int) string {
var result int
for i := range signal {
if i == 0 {
result = evaluateNode(signal[i], signal[i+1])
} else if i == maxDigit-1 {
break
} else {
result = evaluateNode(result, signal[i+1])
}
}

if result == 1 {
return "good"
} else {
return "bad"
}
}

func evaluateNode(node1, node2 int) int {
switch {
case node2 > node1:
return 1
case node2 < node1:
return 0
default:
return 0
}
}

func randInt(min int, max int) int {
return min + rand.Intn(max-min+1)
}
70 changes: 4 additions & 66 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,80 +28,18 @@ func Test_validateSignal(t *testing.T) {
},
want: "bad",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validateSignal(tt.args.signal); got != tt.want {
t.Errorf("validateSignal() = %v, want %v", got, tt.want)
}
})
}
}

func Test_evaluateSignal(t *testing.T) {
type args struct {
signal []int
}
tests := []struct {
name string
args args
want string
}{
{
name: "good signal",
args: args{
signal: []int{0, 1, 1, 0, 0, 1},
},
want: "good",
},
{
name: "bad signal",
name: "unexpected signal",
args: args{
signal: []int{1, 0, 1, 1, 0, 0},
signal: []int{0, 0, 1, 0, 0, 7},
},
want: "bad",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := evaluateSignal(tt.args.signal); got != tt.want {
t.Errorf("calculateSignal() = %v, want %v", got, tt.want)
}
})
}
}

func Test_evaluateNode(t *testing.T) {
type args struct {
node1 int
node2 int
}
tests := []struct {
name string
args args
want int
}{
{
name: "test1",
args: args{
node1: 1,
node2: 0,
},
want: 0,
},
{
name: "test2",
args: args{
node1: 0,
node2: 1,
},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := evaluateNode(tt.args.node1, tt.args.node2); got != tt.want {
t.Errorf("evaluateNode() = %v, want %v", got, tt.want)
if got := validateSignal(tt.args.signal); got != tt.want {
t.Errorf("validateSignal() = %v, want %v", got, tt.want)
}
})
}
Expand Down