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....

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$....

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....

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....

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....

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....

September 17, 2016 · 2 min

Archiving and compressing files in Linux

Archiving and compressing are two different concepts. Archiving is basically collecting files in a single package and a popular tool for that is called tar. Compressing is actually reducing file size by means of an algorithm and popular ones include bzip2 and gzip. I will talk about those, but first I would like to talk about another tool for creating archives: cpio. cpio The cpio command can be used to create a backup of the /home/ directory for instance:...

September 17, 2016 · 3 min

More admin commands for users in Linux

This post is a collection of commands basically, I hope it works a reference for me and other people who pass by. To delete users, we have deluser and userdel. For me, it was really weird learning that we have two commands with similar names that basically do the same thing. I present some examples below. # removes user and home directory $ deluser -r user $ userdel user # remover user from group $ deluser user group # deleting a group $ deluser --group group Creating or deleting groups, adding a user to them or changing primary group:...

September 17, 2016 · 2 min