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

30 days of code in Go: Day 24 - Djikstra's algorithm

Hi there! Today I implemented Djikstra’s algorithm to find the minimum path between one source vertex and all the other vertices in the graph. This algorithm was devised in 1956 by the Dutch computer scientist Edsger W. Djikstra. It is quite fast having a complexity of $O(n\log n)$ when implemented with an auxiliary heap structure. To implement the heap in Go, I made use of the package heap. The package website also has an example of how to use and mine is basically a copy of it....

November 6, 2016 · 4 min

30 days of code in Go: Day 23 - Minimum Cut of Graph

Hi there! Today’s program aims to solve the minimum cut problem. It is another one that I learned in Algorithms: Design and Analysis, Part 1 course from Coursera. A cut is partition of a graph and partitioning a graph means dividing it into two disjoint subsets of vertices. The minimum cut then would be such that the number of edges going from one partition to the other is minimal. Below is a simple example of a graph with 4 nodes....

October 29, 2016 · 5 min

30 days of code in Go: Day 22 - QuickSort

Hi there! Today’s challenge is another algorithm that I learned more in detail in the Algorithms: Design and Analysis, Part 1 course from Coursera: QuickSort. This algorithm chooses a pivot and does a partial sort around that pivot, having all elements less than the pivot to its left and all greater to its right, considering we want the number in increasing order. The main function is qsort which calls the function to choose a pivot and another to partition around the pivot....

October 15, 2016 · 4 min

30 days of code in Go: Day 21 - Extra Long Factorials

One of the big limitations of simple number data types is the maximum number they can hold. A 64 bit unsigned long can hold up to the number $2^{64}-1$. This is a pretty huge number, but if you try to compute the factorial of a number like $25!$, it is not enough. The good news is that there are software libraries that allow us to surpass this limitation. The bad is that the computations take longer....

October 12, 2016 · 2 min

30 days of code in Go: Day 20 - Count inversions with Merge Sort

Hi there! Today’s post is not from HackerRank. This was an assignment that I solved for the Algorithms: Design and Analysis, Part 1 course from Coursera. The goal is to implement an algorithm presented in the videos. It was explained in high level and not considering edge cases. I first tried to do it in C++ but it was taking me more time than I wanted to spend and then I switched to Go and it was blazing fast to code....

October 9, 2016 · 4 min

30 days of code in Go: Day 19 - Binomial Distribution II

Hi there! Today’s problem is very similar to the last day, again using a binomial distribution. Question Given that 12% of the pistons of a manufacture are rejected because of incorrect sizing, what is the probability of batch of 10 pistons contain No more than two rejects? At least two rejects? Again, very similar. Now the probability of success for the Bernoulli trial is of the chance of the piston being reject, the same as being incorrectly sized....

October 6, 2016 · 2 min

30 days of code in Go: Day 18 - Binomial Distribution I

Hi there! Today’s challenge from HackerRank was a little more involved. The challenge is Given the ratio $1.09 : 1$ of boys to girls for babies born in a certain country, what is the proportion of families with exactly 6 children that will have at least 3 boys?. Before actually solving that, it is important to understand a Bernoulli trial and also the Binomial Distribution. Bernoulli trial According to Wikipedia, a Bernoulli trial is a random experiment with exactly two possible outcomes, “success” and “failure”, in which the probability of success is the same every time the experiment is conducted....

October 5, 2016 · 2 min