Skip to content

Instantly share code, notes, and snippets.

@hew
Forked from jin/hackerrank.ml
Last active January 18, 2019 05:10
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 hew/d5ba70a41c8a2a6df92b1cbb629dd66e to your computer and use it in GitHub Desktop.
Save hew/d5ba70a41c8a2a6df92b1cbb629dd66e to your computer and use it in GitHub Desktop.
OCaml HackerRank template code

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