• + 0 comments

    Solution in OCaml

    (* Enter your code here. Read input from STDIN. Print output to STDOUT *)
    let rec read_lines acc = try
        let line = input_line stdin |> int_of_string in
        read_lines (line :: acc)
      with End_of_file -> List.rev acc;;
    
    let arr = read_lines [];;
    
    (* This commented code below increased space complexity in my previous submission
    let len = List.length arr;;
    
    let rec range start stop step acc =
      if start >= stop then
        List.rev acc
      else
        range (start + step) stop step (start :: acc);;
    
    let indices = range 0 len 1 [];;
    
    let pairs = List.combine indices arr;;*)
    
    (* Better code is here below *)
    let enumerate lst =
      List.mapi (fun index elem -> (index, elem)) lst;;
    
    let pairs = enumerate arr;;
    
    let result = List.filter (fun (index,el) -> index mod 2 <> 0) pairs;;
    
    let () = 
      List.iter (fun (index, el) -> print_endline (string_of_int el)) result;;