-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gcd): make gcd solution a function instead of a program
- update tests accordingly
- Loading branch information
1 parent
b4a1739
commit 52b72ff
Showing
3 changed files
with
35 additions
and
47 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,12 @@ | ||
package solutions | ||
|
||
func Gcd(a, b uint) uint { | ||
for a != b { | ||
if a > b { | ||
a -= b | ||
} else { | ||
b -= a | ||
} | ||
} | ||
return a | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,30 +1,32 @@ | ||
package main | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/01-edu/go-tests/lib/challenge" | ||
"github.com/01-edu/go-tests/lib/random" | ||
"fmt" | ||
"os" | ||
student "student" | ||
) | ||
|
||
func main() { | ||
args := [][]string{ | ||
{"42", "10"}, | ||
{"42", "12"}, | ||
{"14", "77"}, | ||
{"17", "3"}, | ||
{"23"}, | ||
{"12", "23"}, | ||
{"25", "15"}, | ||
{"23043", "122"}, | ||
{"11", "77"}, | ||
} | ||
for i := 0; i < 5; i++ { | ||
a := strconv.Itoa(random.IntBetween(1, 100000)) | ||
b := strconv.Itoa(random.IntBetween(1, 100)) | ||
args = append(args, []string{a, b}) | ||
testCases := []struct { | ||
a uint | ||
b uint | ||
want uint | ||
}{ | ||
{42, 10, 2}, | ||
{42, 12, 6}, | ||
{14, 77, 7}, | ||
{17, 3, 1}, | ||
{12, 23, 1}, | ||
{25, 15, 5}, | ||
{23043, 122, 1}, | ||
{11, 77, 11}, | ||
} | ||
for _, v := range args { | ||
challenge.Program("gcd", v...) | ||
|
||
for _, tc := range testCases { | ||
got := student.Gcd(tc.a, tc.b) | ||
if got != tc.want { | ||
fmt.Printf("Gcd(%d, %d) = %d instead of %d\n", tc.a, tc.b, got, tc.want) | ||
os.Exit(1) | ||
} | ||
} | ||
} |