Skip to content

Commit

Permalink
Add solution for cw-compare-margin
Browse files Browse the repository at this point in the history
  • Loading branch information
skosovsky committed Dec 17, 2023
1 parent 58df32c commit 9129bb3
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cw-compare-margin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Compare within margin

Create a function `close_compare` that accepts 3 parameters: `a`, `b`, and an optional `margin`. The function should return whether `a` is lower than, close to, or higher than `b`.

Please note the following:

- When `a` is close to `b`, return `0`.
- For this challenge, `a` is considered "close to" `b` if `margin` is greater than or equal to the absolute distance between `a` and `b`.

Otherwise...

- When `a` is less than `b`, return `-1`.

- When `a` is greater than `b`, return `1`.


If `margin` is not given, treat it as if it were zero.

Assume: `margin >= 0`

---

### Example 1

If `a = 3`, `b = 5`, and `margin = 3`, then `close_compare(a, b, margin)` should return `0`.

This is because `a` and `b` are no more than 3 numbers apart.

### Example 2

If `a = 3`, `b = 5`, and `margin = 0`, then `close_compare(a, b, margin)` should return `-1`.

This is because the distance between `a` and `b` is greater than 0, and `a` is less than `b`.
1 change: 1 addition & 0 deletions cw-compare-margin/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module github.com/skosovsky/algo/cw-compare-margin
32 changes: 32 additions & 0 deletions cw-compare-margin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
)

func main() {
fmt.Println(closeCompare(4.0, 5.0, 0.0)) //nolint:gomnd
}

func closeCompare(a, b, margin float64) int {
sub := a - b // или math.Abs(a-b), тогда упрощение в if

if (sub <= 0 && sub >= (0-margin)) || (sub >= 0 && sub <= margin) {
return 0
} else if a < b {
return -1
}

return 1
}

func closeCompareClever(a, b, margin float64) int {
switch {
case a-b > margin:
return 1
case b-a > margin:
return -1
default:
return 0
}
}
64 changes: 64 additions & 0 deletions cw-compare-margin/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import "testing"

func Test_closeCompare(t *testing.T) {
type args struct {
a float64
b float64
margin float64
}
tests := []struct {
name string
args args
want int
}{
{
name: "Example 1",
args: args{4.0, 5.0, 0.0},
want: -1,
},
{
name: "Example 2",
args: args{5.0, 5.0, 0.0},
want: 0,
},
{
name: "Example 3",
args: args{6.0, 5.0, 0.0},
want: 1,
},
{
name: "Example 4",
args: args{2.0, 5.0, 3.0},
want: 0,
},
{
name: "Example 5",
args: args{5.0, 5.0, 3.0},
want: 0,
},
{
name: "Example 6",
args: args{8.0, 5.0, 3.0},
want: 0,
},
{
name: "Example 7",
args: args{8.1, 5.0, 3.0},
want: 1,
},
{
name: "Example 8",
args: args{1.99, 5.0, 3.0},
want: -1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := closeCompare(tt.args.a, tt.args.b, tt.args.margin); got != tt.want {
t.Errorf("closeCompare() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 9129bb3

Please sign in to comment.