Last active
November 7, 2022 17:38
-
-
Save t-eckert/56a9f33613b613d4839d494e61dee009 to your computer and use it in GitHub Desktop.
Solution to the latest cassidoo challenge
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"os" | |
"strconv" | |
) | |
func main() { | |
if len(os.Args) == 1 { | |
fmt.Println("Missing input integer.") | |
os.Exit(1) | |
} | |
n, err := strconv.Atoi(os.Args[1]) | |
if err != nil { | |
fmt.Println("Input must be integer.") | |
os.Exit(1) | |
} | |
fmt.Println(antidivisors(n)) | |
} | |
func antidivisors(n int) []int { | |
advs := []int{} | |
for k := 2; k < n; k++ { | |
if k%2 == 0 && n%k == k/2 { | |
advs = append(advs, k) | |
} else if k%2 != 0 && (n%k == (k-1)/2 || n%k == (k+1)/2) { | |
advs = append(advs, k) | |
} | |
} | |
return advs | |
} |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"testing" | |
) | |
func TestAntidivisor(t *testing.T) { | |
cases := []struct { | |
input int | |
expected []int | |
}{ | |
{2, []int{}}, | |
{3, []int{2}}, | |
{4, []int{3}}, | |
{5, []int{2, 3}}, | |
{6, []int{4}}, | |
{13, []int{2, 3, 5, 9}}, | |
{38, []int{3, 4, 5, 7, 11, 15, 25}}, | |
} | |
for _, tc := range cases { | |
t.Run(fmt.Sprintf("%d", tc.input), func(t *testing.T) { | |
actual := antidivisors(tc.input) | |
if len(tc.expected) != len(actual) { | |
fmt.Printf("expected:\t%+v\nactual:\t\t%+v\n", tc.expected, actual) | |
t.Fatal() | |
} | |
for i, expected := range tc.expected { | |
if expected != actual[i] { | |
fmt.Printf("expected:\t%+v\nactual:\t\t%+v\n", tc.expected, actual) | |
t.Fail() | |
} | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment