Skip to content

Instantly share code, notes, and snippets.

@jin
Last active July 17, 2019 14:57
Show Gist options
  • Save jin/55aeba3c86e8f70a83fe to your computer and use it in GitHub Desktop.
Save jin/55aeba3c86e8f70a83fe to your computer and use it in GitHub Desktop.
OCaml HackerRank template code
(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
let () =
let ary = ref [] in
try ( while true do ary := (!ary@[read_int()]) done )
with End_of_file ->
begin
List.fold_left (fun i x ->
if (i mod 2 != 0) then (print_int(x); print_string("\n"); (i + 1))
else (i + 1)
) 0 (!ary);
()
end
;;
(* returns an array of n elements *)
let make_array n = Array.make n 1
let () =
let len = read_int () in
let arr = make_array len in
Printf.printf "%d\n" (Array.length arr)
(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
let rec reverse xs =
match xs with
| [] -> []
| y::ys -> (reverse ys)@[y]
let () =
let ary = ref [] in
try ( while true do ary := (!ary@[read_int()]) done )
with End_of_file ->
begin
let reversed = reverse !ary in
List.iter (fun x -> print_int(x); print_string("\n")) reversed
end
;;
let () =
let ary = ref [] in
try ( while true do ary := (!ary@[read_int()]) done )
with End_of_file ->
begin
List.fold_left (fun i x ->
if (i mod 2 != 0) then (print_int(x); print_string("\n"); (i + 1))
else (i + 1)
) 0 (!ary);
()
end
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment