diff options
| author | Shawn <shawnw.mobile@gmail.com> | 2020-08-29 10:19:55 -0700 |
|---|---|---|
| committer | Shawn <shawnw.mobile@gmail.com> | 2020-08-29 10:24:36 -0700 |
| commit | faabd66e0dfbb34f93f5fd65112c96cadd5887ad (patch) | |
| tree | 4db0c5a9eeefa5dc5b3ebb6a5b04bedae5f6b654 | |
| parent | f65bfda1d2ec835040e5c531639dcc672a0197ef (diff) | |
| download | perlweeklychallenge-club-faabd66e0dfbb34f93f5fd65112c96cadd5887ad.tar.gz perlweeklychallenge-club-faabd66e0dfbb34f93f5fd65112c96cadd5887ad.tar.bz2 perlweeklychallenge-club-faabd66e0dfbb34f93f5fd65112c96cadd5887ad.zip | |
Challenge 075 task1 solution in ocaml
| -rw-r--r-- | challenge-075/shawn-wagner/ocaml/ch-1.ml | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-075/shawn-wagner/ocaml/ch-1.ml b/challenge-075/shawn-wagner/ocaml/ch-1.ml new file mode 100644 index 0000000000..05d4052ff0 --- /dev/null +++ b/challenge-075/shawn-wagner/ocaml/ch-1.ml @@ -0,0 +1,28 @@ +let sum = List.fold_left (+) 0 + +let rec solve coins s = + if s = 0 then + [[]] + else begin + let solutions = ref [] in + List.iter (function coin -> + let f solution = if (sum solution) + coin = s then + Some (coin :: solution) + else + None in + if s - coin >= 0 then + solutions := + List.append (List.filter_map f (solve coins (s - coin))) + !solutions) coins; + !solutions + end + +let task1 coins s = + let solutions= solve coins s in + let solutions = List.map (List.sort Int.compare) solutions in + let solutions = List.sort_uniq compare solutions in + Printf.printf "There are %d possible ways to make sum %d\n" (List.length solutions) s + +let _ = + task1 [1;2;4] 6 + |
