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 NN that represents the number of people in the phone book. After that, it reads the NN 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. The fact that we can query as many names as we want means that the program needs to check for an end of file before leaving, which is basically trying to read until the user cancels the program. Well, other options would be typing a special quit command, but that’s not the case for this problem. ...

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 AA of NN elements and print AA elements in reverse order. Surely I could put everything in a file and then read it backwards, but as I don’t even know how to use files in Go at the moment, I will be creating an array in memory and then printing it backwards to the screen. My solution is just below. ...

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

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 NN and it will print results of the form N×i=resultN\times i = \mathrm{result}, where 1≤i≤101 \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. The one for today will be quite similar. ...

September 11, 2016 · 1 min

30 days of code in Go: Day 4 - Class vs Instance

Here I am again for the 30 Days of Code in Go. This is Saturday, but that doesn’t mean I will not code =). First of all, before starting to code I was thinking about the theme: classes. I was wondering if Go had support for it and I found some interesting material to read. First I read Why Go’s structs are superior to class-based inheritance and I found out the the inheritance model of Go is different from that of Java. Basically, you do not actually specify which class implements an interface, but the compiler. A class can implement several interfaces, not being limited to one super-class like in Java or C++. Another materials I read were Go by Example Structs, methods and interfaces. I am not so well versed on the syntax, but it is not hard to grasp it and start coding. Nevertheless, HackerRank was good to me and gave some starter code and I just implemented the methods. ...

September 10, 2016 · 2 min

Simple Array Sum in Go

Hello again! I saw a problem in HackerRank to sum an array of elements. I have done this before in a couple of languages, but never in Go, so I was not sure exactly what to do. I coded the solution and it is: package main import "fmt" func main() { var n uint32 fmt.Scanf("%d\n", &n) sum := 0 current := 0 for n > 0 { fmt.Scanf("%d", &current) sum += current n-- } fmt.Printf("%d\n", sum) } Here I run the program and I see a correct solution: ...

September 9, 2016 · 1 min

30 days of code in Go: Day 3 - Conditional Statements

The HackerRank challenge of today was a problem with conditionals. Given an integer nn, the goal was to perform the following actions: If nn is odd, print Weird If nn is even and in the range [2,5][2, 5], print Not Weird If nn is even and in the range [6,20][6, 20], print Weird If nn is even and greater than 2020, print Weird The input to the program is simply the number nn and the result is a single line saying that the number is weird or not. My solution was ...

September 9, 2016 · 1 min

Replace regex matched substring by another substring in Java

The goal of this post is to show how to match words and replace them in a string using java. We are looking for words of three characters that start with a lower case letter and are followed by two digits. It took me a while to remember about the word boundaries (those represented by \\b). Pay attention to that. By the way, this exercise I found on the book Competitive Programming. ...

September 8, 2016 · 1 min

How to eval a math expression entered as an input string in Java

One way to evaluate simple math expressions in Java just takes a couple lines of code. It uses Javascript’s engine to do that and it is quite useful. // to use eval import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; // to use scanner import java.util.*; class Main { public static void main(String[] args) throws Exception { ScriptEngineManager mgr; mgr = new ScriptEngineManager(); ScriptEngine engine; engine = mgr.getEngineByName("JavaScript"); Scanner scan = new Scanner(System.in); String foo = scan.nextLine(); System.out.println(engine.eval(foo)); } } If you want to compile and run this example, you need to have Java JDK installed. The commands to compile and run a file called eval.java with the code above are: ...

September 8, 2016 · 1 min

Print double in Java with variable precision

There are some details I never cared about that I started to learn. One of them is just printing to the screen with variable precision. The solution is quite simple. import java.util.*; /** * Prints pi with n decimal places. * n is given by the user */ class Main { public static void main(String[] args) { double pi = Math.PI; Scanner scanner = new Scanner(System.in); int precision = scanner.nextInt(); scanner.close(); String format = "%." + precision + "f\n"; System.out.printf(format, pi); } } The difficulty is just thinking that you can define a string with the precision you want and then use it to print to the screen. ...

September 8, 2016 · 1 min