Skip to content

Commit fbcbc80

Browse files
example to pass tests, with test change to allow negative numbers
1 parent 8e2524f commit fbcbc80

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
let classify n =
2-
if n < 1 then
3-
Error "Classification is only possible for positive integers."
2+
let aliquot = function
3+
| 1 -> 0
4+
| n when n > 1 ->
5+
let rec sum_factors acc factor =
6+
if factor > n / 2 then acc
7+
else if n mod factor = 0 then
8+
sum_factors (acc + factor) (factor + 1)
9+
else
10+
sum_factors acc (factor + 1)
11+
in
12+
sum_factors 0 1
13+
| _ -> 0
14+
in
15+
match n with
16+
| _ when n < 1 -> Error "Classification is only possible for positive integers."
17+
| n ->
18+
let aliquot_sum = aliquot n in
19+
if aliquot_sum = n then Ok "perfect"
20+
else if aliquot_sum > n then Ok "abundant"
21+
else Ok "deficient"

exercises/practice/perfect-numbers/test.ml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ let ae exp got _test_ctxt = assert_equal ~printer:option_printer exp got
99

1010
let tests = [
1111
"Smallest perfect number is classified correctly" >::
12-
ae (Ok "perfect") (classify 6);
12+
ae (Ok "perfect") (classify (6));
1313
"Medium perfect number is classified correctly" >::
14-
ae (Ok "perfect") (classify 28);
14+
ae (Ok "perfect") (classify (28));
1515
"Large perfect number is classified correctly" >::
16-
ae (Ok "perfect") (classify 33550336);
16+
ae (Ok "perfect") (classify (33550336));
1717
"Smallest abundant number is classified correctly" >::
18-
ae (Ok "abundant") (classify 12);
18+
ae (Ok "abundant") (classify (12));
1919
"Medium abundant number is classified correctly" >::
20-
ae (Ok "abundant") (classify 30);
20+
ae (Ok "abundant") (classify (30));
2121
"Large abundant number is classified correctly" >::
22-
ae (Ok "abundant") (classify 33550335);
22+
ae (Ok "abundant") (classify (33550335));
2323
"Smallest prime deficient number is classified correctly" >::
24-
ae (Ok "deficient") (classify 2);
24+
ae (Ok "deficient") (classify (2));
2525
"Smallest non-prime deficient number is classified correctly" >::
26-
ae (Ok "deficient") (classify 4);
26+
ae (Ok "deficient") (classify (4));
2727
"Medium deficient number is classified correctly" >::
28-
ae (Ok "deficient") (classify 32);
28+
ae (Ok "deficient") (classify (32));
2929
"Large deficient number is classified correctly" >::
30-
ae (Ok "deficient") (classify 33550337);
30+
ae (Ok "deficient") (classify (33550337));
3131
"Edge case (no factors other than itself) is classified correctly" >::
32-
ae (Ok "deficient") (classify 1);
32+
ae (Ok "deficient") (classify (1));
3333
"Zero is rejected (as it is not a positive integer)" >::
34-
ae (Error "Classification is only possible for positive integers.") (classify 0);
34+
ae (Error "Classification is only possible for positive integers.") (classify (0));
3535
"Negative integer is rejected (as it is not a positive integer)" >::
36-
ae (Error "Classification is only possible for positive integers.") (classify -1);
36+
ae (Error "Classification is only possible for positive integers.") (classify (-1));
3737
]
3838

3939
let () =

templates/perfect-numbers/test.ml.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let tests = [
1111
{{#cases}}
1212
{{#cases}}
1313
"{{description}}" >::
14-
ae {{#input}}{{{expected}}} (classify {{{number}}}){{/input}};
14+
ae {{#input}}{{{expected}}} (classify ({{number}})){{/input}};
1515
{{/cases}}
1616
{{/cases}}
1717
]

0 commit comments

Comments
 (0)