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. For the case of no more than two rejects, the probability is given by $$P_{x \le 2} = \sum_{i=0}^2 b(i, 10, 0.12),$$ while for the case of at least two rejects it is $$P_{x \ge 2} = \sum_{i=2}^{10} b(i, 10, 0.12).$$ ...

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

30 days of code in Go: Day 17 - Standard Deviation

Hi there! Today’s challenge was to compute standard deviation of an array of data given the formula $$\sigma = \sqrt{\frac{\sum_{i=0}^{N-1} (x_i - \mu)^2}{N}},$$ where $$\mu = \frac{\sum_{i=0}^{N-1} x_i}{N}.$$ The first input to the program is N and then N integers are fed to the program in sequence. My solution is given below. package main import ( "fmt" "math" ) func mean(a []int) float64 { n := float64(len(a)) sum := 0.0 for i := 0; i < len(a); i++ { sum += float64(a[i]) } return sum / n } func sigma(a []int) float64 { mu := mean(a) sum := 0.0 n := float64(len(a)) for i := 0; i < len(a); i++ { x := float64(a[i]) sum += (x - mu) * (x - mu) } sigma := math.Sqrt(sum / n) return sigma } func main() { N := 0 fmt.Scanf("%d", &N) // allocate memory X := make([]int, N) // read numbers from stdin for i := 0; i < N; i++ { fmt.Scanf("%d", &X[i]) } y := sigma(X) fmt.Printf("%.1f\n", y) } The problem I had this time was with HackerRank. On my computer I had a result, but their system was saying it had a different result. After that I ran it again on HackerRank using the option custom input, but with the same input that gave me a bad answer error, but this time the answer was ok. Bugs appear everywhere, right? I will submit it with another language, still thinking about it. I wrote something to them, hopefully they will fix this bug. ...

October 4, 2016 · 2 min

30 days of code in Go: Day 16 - Interquartile Range

Hi there! Today’s challenge is quite similar to the last one. Again, there are quartiles, but the input is a little different and the output is the interquartile range: $Q_3 - Q_1$. The first input is $n$ followed by an array $X$ of size $n$ with our data, but the frequencies of each point are included in another array of $n$ elements, $F$, that is the next input. After reading this we need to construct the actual data array $S$. My solution is below. ...

October 3, 2016 · 2 min

30 days of code in Go: Day 15 - Quartiles

Hi there! The problem for today required me to find the quartiles of set of data values. The concept is actually new to me but, once I learned it, it resembled a lot the median of a data set. Actually, the second quartile $Q_2$ is the median itself, while the other two quartiles, $Q_1$ and $Q_2$, are basically medians of sub datasets divided around the second quartile. The three quartiles divide the data in four regions instead of two regions using only the median. ...

October 1, 2016 · 2 min

Throwing Exceptions in Cpp and the idea of a virtual method

Hi there! Well, the 30 days of code from HackerRank had some challenges that didn’t allow me to use Go and this is one of them. The idea is to create a custom exception and throw it. My choice of language was C++ and the solution is given below. Notice that not all code is mine, the IO handling was given by HackerRank (probably to force me use a class Calculator and an exception haha). ...

October 1, 2016 · 3 min

30 days of code in Go: Day 14 - Weighted mean

Hi there! Today’s problem is quite simple. The challenge is to compute the weighted mean given two arrays. The first input is $N$, the number of elements of each array, and then the first array is given and then the second whose components are the weights. My solution is given below. package main import "fmt" func main() { N := 0 fmt.Scanf("%d", &N) a := make([]float64, N) b := make([]float64, N) for i := 0; i < N; i++ { fmt.Scanf("%f", &a[i]) } for i := 0; i < N; i++ { fmt.Scanf("%f", &b[i]) } numerator := 0.0 denominator := 0.0 for i := 0; i < N; i++ { numerator += a[i] * b[i] denominator += b[i] } mean := numerator / denominator fmt.Printf("%.1f\n", mean) } The only problem I had was that I forgot to read the arrays initially 😆. The rest of the code was quite easy given what I learned so far. I made use of type inference in Go which is quite nice. If you have any comments, don’t hesitate to post it on disqus! See you!

September 30, 2016 · 1 min

30 days of code in Go: Day 13 - Mean, Median and Mode

This post is a little bit different from the last ones. Days 1-12 followed more or less strictly what HackerRank 30 days of code Tutorial had, but starting on day 13 there was a series of challenges that didn’t allow me to use Go. They are more specific to object-oriented languages like C++ and Java. As Go doesn’t have all the constructions these languages allow, like abstract classes, at least not without a workaround, I decided to take other challenges on HackerRank. The one for today is about statistics and I will introduce some of the concepts before showing the code solution. ...

September 29, 2016 · 3 min

30 days of code in Go: Day 12 - Inheritance (kind of)

Inheritance is a nice concept that object-oriented languages have. It is basically the ability for one type, many times called a class or even a struct depending on the language, to inherit the behaviour of another type. HackerRank doesn’t allow me, at least at the moment this post is being written, to submit code in Go for this challenge of inheritance. It seems like Go doesn’t support inheritance, though there are some workarounds that let us mimic the behaviour. The code below is what I tested to mimic the behaviour of a Student class that inherits from the Person class. The Student class has a different printPerson method that calls its super method (the method of the parents class) and it also inherits the sayHello function which is just called by the Student instance object. ...

September 18, 2016 · 3 min

30 days of code in Go: Day 11 - 2D Arrays

Hi there! The challenge of today confused me a little in their definition, but they had a good example so it was not hard to get it. Basically I had to read a $6 \times 6$ matrix and iterate over it summing a pattern of elements like abc d efg that is called an hourglass. So, I just had to iterate through an array and sum 7 elements of it for each iteration. My solution is below. ...

September 17, 2016 · 2 min