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

30 days of code in Go: Day 10 - Binary Numbers

The challenge for today is interesting, but I keep wondering if I could do it more efficiently. Let me know in the comments if you have any suggestion after reading my solution. The problem is counting the maximum number of consecutive binary ones in an integer. It is easier to understand with an example: Consider the number 25 represented in base 10 Its representation in binary is 11001 The max number of consecutive ones we can see is 2 What I did was shifting to the right and checking the least significant bit until every single bit of it was checked....

September 16, 2016 · 1 min

30 days of code in Go: Day 9 - Recursion

Hi there! Here I am coding again. For those of you readers who may not know what recursion is, I have a brief explanation: a function is called a recursive function when it calls itself. A very nice example is the factorial, which is also what I am going to code today. For the factorial of $N$, $f(N)$, we can state that $f(N) = N\times f(N-1)$. What we see is that the function is represented by itself, but with other argument and it can keep this going up to a certain point for which we know the answer like $f(1)=f(0)=1$....

September 15, 2016 · 1 min

30 days of code in Go: Day 8 - Dictionaries and Maps

Hi there! The task of today will be coding a phone book. The program reads an integer $N$ that represents the number of people in the phone book. After that, it reads the $N$ entries from standard output and generates the dictionary/map. For newbies: dictionaries are the same as maps. Java usually calls the structure with (key, value) pairs as a map, while Python calls it a dictionary. After having the phone book generated, the program will read an unknown number of names....

September 14, 2016 · 3 min

30 days of code in Go: Day 7 - Arrays

Hi there! A couple days ago I wrote a post about a simple array sum in Go and I said I didn’t need an array in memory to do the sum, but just an integer that would store the inputs the user gave the program. This time, I will really need an array. The goal is to read an array $A$ of $N$ elements and print $A$ elements in reverse order....

September 13, 2016 · 2 min

30 days of code in Go: Day 6 - Let's review

Some challenges require us to review basic concepts to make them work. This one is no different, but the main purpose of it is to be a review! The goal is to separate the characters of a string. The even-indexed characters will form a string and the odd-indexed characters will form another. These two strings must be printed to the screen with a space between them. Not hard, but an important review....

September 12, 2016 · 2 min

30 days of code in Go: Day 5 - Loops

Hi there! Welcome to Day 5 of 30 days of code in Go! One interesting thing about loops in Go is that there is only one looping construct: for. This challenge will receive an integer input $N$ and it will print results of the form $N\times i = \mathrm{result}$, where $1 \le i \le 10$. By the way, there was a loop on the last day of code, but it was actually part of the given code....

September 11, 2016 · 1 min