OCaml HackerRank template code
-
-
Save hew/d5ba70a41c8a2a6df92b1cbb629dd66e to your computer and use it in GitHub Desktop.
OCaml HackerRank template code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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