-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/skosovsky/algo/cw-compare-margin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |