Skip to content

Instantly share code, notes, and snippets.

@agrison
Last active December 13, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agrison/4961817 to your computer and use it in GitHub Desktop.
Save agrison/4961817 to your computer and use it in GitHub Desktop.
Coding challenge Cédric Beust - OCaml
(*
See: http://beust.com/weblog/2008/06/27/
---
compile & run:
$ ocamlfind ocamlc -package batteries -linkpkg challenge.ml -o challenge
$ ./challenge
*)
open Batteries;
(* Test whether a number have repeating digits *)
let no_repeat n = let digits = String.to_list (string_of_int n) in
List.length(List.unique digits) == List.length digits;;
(* Get the max gap *)
let max_gap l = let x = ref 0 in
List.max (List.map (fun e -> let r = e - !x in (x := e; r)) l);;
(* Solve the problem *)
let () = let range = List.of_enum (1--10000) in
let filtered = List.filter no_repeat range in
List.iter (Printf.printf "%d, ") filtered; (* print numbers *)
Printf.printf "\nNumber: %d" (List.length filtered); (* print max number *)
Printf.printf "\nMax Gap: %d" (max_gap filtered) (* print max gap *)
;;
(* Output:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15,
...
9867, 9870, 9871, 9872, 9873, 9874, 9875, 9876,
Number: 5274
Max Gap: 105
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment