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 $n$, the goal was to perform the following actions: If $n$ is odd, print Weird If $n$ is even and in the range $[2, 5]$, print Not Weird If $n$ is even and in the range $[6, 20]$, print Weird If $n$ is even and greater than $20$, print Weird The input to the program is simply the number $n$ 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

30 days of code in Go: Day 2 - computing the total cost of a meal

Hi there! Today is the day 2 challenge. The goal is to compute the total cost of a meal given the meal price, tip percent and tax percent. Usually when I go to a restaurant in Brazil I only care about the first two, as taxes are already included in the meal price. Again: this is pretty basic, but as I am used to other languages syntax this simple tutorial from HackerRank is being quite useful to keep me training everyday and learn Go, so it is awesome! Now, let’s see my solution: ...

September 8, 2016 · 2 min

30 days of code in Go: Day 1 - summing numbers and concatenating strings

The goal of this second day is the following: Declare 3 variables: an int, a double and a string. Read 3 lines from stdin and save them in the variables you just defined Do some operations with the pre-defined $i$, $d$ and $s$ variables Print the sum of the integer $i$ with your integer variable Print the sum of the float $d$ with your double variable with one decimal place Concatenate $s$ with your string and print the result My solution was the following ...

September 7, 2016 · 1 min

30 days of code in Go: Day 0 - Reading from stdin

Today I am starting the 30 days of code challenge on Hackerrank. You will be able to see my progress at my profile. The language I chose is Go. I do not have experience in Go and I am pretty excited. I think the challenge is basic, but it will probably be fun, so bear with me. Later on I may take better challenges, when I get more experience with Hackerrank. Incredibly, this gave me more trouble than I expected. First of all I had to install go using homebrew: ...

September 6, 2016 · 2 min

What I think is missing in C: Type Inference

The first programming language I learned was C and I found it pretty interesting back then. Nevertheless, I have seen a couple of languages in the last years and I really think C is missing something important: type inference. The concept is simple and it means that the compiler will try to infer what the variable type is for you. You need to give context in the code for this to happen, but I have seen it work well on OCaml and Golang. As a simple example in Go: ...

September 6, 2016 · 1 min

Code a program to solve a partial differential equation using finite differences with an explicit method

The goal of this post is to create a program to solve a differential equation in 2D. I will present the programming in C++ but the concepts here are easily translated to other languages. I myself have used Julia a couple times. The equation that we are going to solve is the Poisson equation with Dirichlet boundary conditions in a $1\times 1$ square. The equation and the conditions are given by ...

September 3, 2016 · 6 min