Get number of common divisors in OCaml

Hi, everyone! In this challenge I used OCaml sets again. The goal was to get the number of common divisors for each pair of numbers in the test cases. The first function I wrote was to define a limit until when I would be checking if the number was divisible or not: (* Need to check divisors just up to sqrt x *) let max x = Int.of_float (sqrt x) |> (+) 1;; After that, I wrote a function to get the number of common divisors of a single number n....

March 28, 2017 · 3 min

Dedup chars in a string using OCaml

Hi there! The goal of this challenge is to remove all duplicate chars in a string. The following input/output will explain it by itself: # input abcabczabczabc # output abcz I tried to write the functions to explode and implode from what I remembered, but in the end I had to take another look at their code here. Those were auxiliary functions to help me to write remove_all_dups. open Core.Std (* string -> char list *) let explode str = let len = String....

March 27, 2017 · 2 min

Mingle strings in OCaml

Hi there! The problem that I solved this time was a little cumbersome initially, cause even though I knew well how to iterate over lists, I had no idea how to iterate over strings. The solution I found was inspired in the functions to explode and implode strings . These functions use the string indices to explode the string, creating a char list, or implode a char list, creating a string....

March 26, 2017 · 2 min

List Replication

Hi there! The idea of this problem is just to read some numbers from stdin and replicate them, but using lists. The input would be like this: 3 // # of repetitions 1 // first number ... 2 3 4 // end of input, last number Then the output would be a list with each number repeated the amount of times given. For the case above, it would show “1” three times, “2” three times and so on, each element on a different line....

March 24, 2017 · 2 min

Hello World printing in OCaml

Another simple problem from HackerRank is to print Hello World multiple times in a functional language. Well, I am using OCaml and I suppose I should avoid loops. The way I solved it is the following: open Core.Std let n = read_int ();; let rec print_hello n = if n > 0 then begin printf "Hello World\n"; print_hello (n-1); end else ();; print_hello n Problems I had: I lost some some time to discover the ;; was required to make it compile properly....

March 24, 2017 · 1 min

Compare Triplets in OCaml

Hi there! It has been a while since I played with OCaml, so I am solving a simple problem from HackerRank. The problem says the following “Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty”. For each category, we must check who had a greater score and give a point to that person for each category....

March 24, 2017 · 3 min

What I think is missing in C: Type Inference

The first programming language I learned was C and I found it pretty interesting back then. Nevertheless, I have seen a couple of languages in the last years and I really think C is missing something important: type inference. The concept is simple and it means that the compiler will try to infer what the variable type is for you. You need to give context in the code for this to happen, but I have seen it work well on OCaml and Golang....

September 6, 2016 · 1 min