Skip to content

Instantly share code, notes, and snippets.

@dhcmrlchtdj
Last active May 27, 2018 04:06
Show Gist options
  • Select an option

  • Save dhcmrlchtdj/33c332e4116ff2db02e116326034d994 to your computer and use it in GitHub Desktop.

Select an option

Save dhcmrlchtdj/33c332e4116ff2db02e116326034d994 to your computer and use it in GitHub Desktop.
OCaml helper for hackerrank
(* $ ocaml str.cma template.ml *)
module H = struct
module P = Pervasives
let split s : string list = Str.split (Str.regexp " +") s
let seq len = Array.make len 0 |> Array.to_list
let rint () : int = P.read_int ()
let rfloat () : float = P.read_float ()
let rstring () : string = P.read_line ()
let rlist (cast: string -> 'a) : 'a list =
P.read_line () |> split |> List.map cast
let rlist_int () : int list = rlist int_of_string
let rlist_float () : float list = rlist float_of_string
let rlist_string () : string list = rlist (fun x -> x)
let rlist_i64 () : Int64.t list = rlist_string () |> List.map Int64.of_string
let rnlist (n: int) (f: unit -> 'a list) : 'a list list =
seq n |> List.rev_map (fun _ -> f ())
let print_int64 n = n |> Int64.to_string |> print_string
let pint n = print_int n ; print_newline ()
let pfloat n = print_float n ; print_newline ()
let pstring n = print_string n ; print_newline ()
let pi64 n = print_int64 n ; print_newline ()
let plist (cast: 'a -> unit) lst : unit =
List.iter (fun x -> cast x ; print_char ' ') lst ;
print_newline ()
let plist_int = plist print_int
let plist_float = plist print_float
let plist_string = plist print_string
let plist_i64 = plist print_int64
let pnlist (f: 'a list -> unit) lst = List.iter f lst
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment