Sort by

recency

|

184 Discussions

|

  • + 0 comments

    It can be done like this too : Range(1,arr.length,2).map(x=>arr(x)).toList

  • + 0 comments

    Solution in Ocaml without using builtins function:

    let read_inputs () =
      let rec get_inputs liste =
        try
          let tmp = read_int () in
          get_inputs (liste @ [tmp])  (* Accumulate input into the list *)
        with
        | End_of_file -> liste
      in
      get_inputs [] ;;
      
     let rec filter_odd_position_element liste n = 
        if n <> List.length liste && (n mod 2) = 1 then
            Printf.printf "%d\n" (List.nth liste n);
        if n <> List.length liste then
             filter_odd_position_element liste (n + 1) ;;
    let liste = read_inputs ();;
     filter_odd_position_element liste 0;;
     
    
  • + 0 comments

    Scala one liner: arr.grouped(2).map(_.tail).flatten.toList

  • + 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;;
    
  • + 0 comments

    Thanks for the solution,https://www.customflanges.com/ I was looking for it for my site