How to read integers from Console in Go using Scanf? - go

So I'm trying to do a codeforces problem (my first!) and I'm trying to read the input.
I initially tried using os.Args, but that caused a runtime error.
I am now trying this
package main
import (
"fmt"
)
func main() {
var n int
var m int
var a int
fmt.Scanf("%d", &n)
fmt.Scanf("%d", &m)
fmt.Scanf("%d", &a)
rectangleArea := n*m
squareArea := a*a
nSquares := 0
for i := 0; i < rectangleArea + squareArea; i = i + squareArea {
nSquares++
}
fmt.Println(nSquares)
}
But this seems to go into infinite loop when I run it with the arguments 6 6 4. Well, it goes into infinite loop with any argument.
What's going on?

Related

Need help about concurrency programming in Go

For this task i need to find min sum in list of numbers. Then i must print number that have min sum. This must be done with Mutex and WaitGroups. I can't find where is the mistake or why is output different.
Logic: Scanf n and make vector with len(n). Then create funcion for sum of number and forward that function to second where we in one FOR cycle give goroutines function to.
I run this code a few times, and sometimes give different answer for same input.
Input:
3
13
12
11
Output:
Sometimes 12
Sometimes 11
package main
import (
"fmt"
"math"
"runtime"
"sync"
)
var wg sync.WaitGroup
var mutex sync.Mutex
var vector []int
var i int
var n int
var firstsum int
var p int //Temp sum
var index_result int
func sumanajmanjih(broj int) int {
var br int
var suma int
br = int(math.Abs(float64(broj)))
suma = 0
for {
suma += br % 10
br = br / 10
if br <= 0 {
break
}
}
return suma
}
func glavna(rg int) {
var index int
firstsum = sumanajmanjih(vector[0])
for {
mutex.Lock()
if i == n {
mutex.Unlock()
break
} else {
index = i
i += 1
mutex.Unlock()
}
fmt.Printf("Procesor %d radni indeks %d\n", rg, index)
p = sumanajmanjih(vector[index])
if p < firstsum {
firstsum = p
index_result = index
}
}
wg.Done()
}
func main() {
fmt.Scanf("%d", &n)
vector = make([]int, n)
for i := 0; i < n; i++ {
fmt.Scanf("%d", &vector[i])
}
fmt.Println(vector)
brojGR := runtime.NumCPU()
wg.Add(brojGR)
for rg := 0; rg < brojGR; rg++ {
go glavna(rg)
}
wg.Wait()
fmt.Println(vector[index_result])
}
Not a full answer to your question, but a few suggestions to make code more readable and stable:
Use English language for names - glavna, brojGR are hard to understand
Add comments to code explaining intent
Try to avoid shared/global variables, especially for concurrent code. glavna(rg) is executed concurrently, and you assign global i and p inside that function, that is a race condition. Sends all the data in and out into function explicitly as argument or function result.
Mutex easily can lock the code, and it is complicated to debug. Simplify its usage. Often defer mutex.Unlock() in the next line after Lock() is good enough.

hackerrank Circular Array Rotation Go Runtime Error

I did solved a hackerrank problem which is "Circular Array Rotation" using Go
when i run the code it gives me a correct response and when i try to submit the code all the test passes except one and it says Runtime Error
I tried the failing test localy and it passes
my code is
package main
import "fmt"
func CircularArrayRotation() {
var n, k, q int
fmt.Scanf("%d%d%d", &n, &k, &q)
a := make([]int, n)
for i := range a {
fmt.Scanf("%d", &a[i])
}
var i int
for t:=0; t<q; t++ {
fmt.Scanf("%d", &i)
j := (i - k)
if j<0 {
j+=n
}
fmt.Println(a[j])
}
}
func main() {
//Enter your code here. Read input from STDIN. Print output to STDOUT
CircularArrayRotation()
}
For those that want more information you can look here: https://www.hackerrank.com/challenges/circular-array-rotation/problem
The Input for his failing case is this and the Expected Output is this
Your run time error is this:
panic: runtime error: index out of range
goroutine 1 [running]: main.CircularArrayRotation() solution.go:22
+0x349 main.main() solution.go:29 +0x20
So your issue is on line 22, where your index is out of range: fmt.Println(a[j])
This happens because your code currently cannot handle multiple rotations, so in your code you end up executing the following:
fmt.Println(a[-99477])
This happens when i is 8 and k is 100000
Imagine you had this input:
n = 3
k = 10
q = 1
i = 2
When you perform i - k we get -8, we then try to add n which gives us -5 (-8 + 3), then we try to access an index that does not exist. a[-5]
If you wish to fix this issue you can do the below (inside spoiler in case you want to work this out yourself):
put this above your i - k
k = k % n
The reason this fixes your code is:
It works out how many rotations are left after we've fully looped x times. That way we don't have to worry about multiple rotations.
--
As a side note for this challenge there's some interesting stuff you can do with slices for rotating an array using some of the stuff covered here: https://tour.golang.org/moretypes/10
Most of your code is correct, however it fails because you only check if j < 0 once. After adding n to it is still negative, so it fails.
Running your code and printing the values when it fails gives:
n: 515, k:100000, q:500, j:-99477
The fix is simple, change your if to a for. This will keep adding n until your index is postive again.
Fixed:
package main
import "fmt"
func CircularArrayRotation() {
var n, k, q int
fmt.Scanf("%d%d%d", &n, &k, &q)
a := make([]int, n)
for i := range a {
fmt.Scanf("%d", &a[i])
}
var i int
for t := 0; t < q; t++ {
fmt.Scanf("%d", &i)
j := i - k
for j < 0 {
j += n
}
fmt.Println(a[j])
}
}
func main() {
//Enter your code here. Read input from STDIN. Print output to STDOUT
CircularArrayRotation()
}

Big int ranges in Go

Is there a way to loop over intervals between two big int values, x and y, in Go?
for i: = x; i < y; i++ {
// do something
}
Working with big numbers can be kind of clunky because you need to create a big.Int for constants. Other than that, it is a straight forward replacement of each segment of the for statement to one made to deal with big ints.
http://play.golang.org/p/pLSd8yf9Lz
package main
import (
"fmt"
"math/big"
)
var one = big.NewInt(1)
func main() {
start := big.NewInt(1)
end := big.NewInt(5)
// i must be a new int so that it does not overwrite start
for i := new(big.Int).Set(start); i.Cmp(end) < 0; i.Add(i, one) {
fmt.Println(i)
}
}

What is the correct way to find the min between two integers in Go?

I imported the math library in my program, and I was trying to find the minimum of three numbers in the following way:
v1[j+1] = math.Min(v1[j]+1, math.Min(v0[j+1]+1, v0[j]+cost))
where v1 is declared as:
t := "stackoverflow"
v1 := make([]int, len(t)+1)
However, when I run my program I get the following error:
./levenshtein_distance.go:36: cannot use int(v0[j + 1] + 1) (type int) as type float64 in argument to math.Min
I thought it was weird because I have another program where I write
fmt.Println(math.Min(2,3))
and that program outputs 2 without complaining.
so I ended up casting the values as float64, so that math.Min could work:
v1[j+1] = math.Min(float64(v1[j]+1), math.Min(float64(v0[j+1]+1), float64(v0[j]+cost)))
With this approach, I got the following error:
./levenshtein_distance.go:36: cannot use math.Min(int(v1[j] + 1), math.Min(int(v0[j + 1] + 1), int(v0[j] + cost))) (type float64) as type int in assignment
so to get rid of the problem, I just casted the result back to int
I thought this was extremely inefficient and hard to read:
v1[j+1] = int(math.Min(float64(v1[j]+1), math.Min(float64(v0[j+1]+1), float64(v0[j]+cost))))
I also wrote a small minInt function, but I think this should be unnecessary because the other programs that make use of math.Min work just fine when taking integers, so I concluded this has to be a problem of my program and not the library per se.
Is there anything that I'm doing terrible wrong?
Here's a program that you can use to reproduce the issues above, line 36 specifically:
package main
import (
"math"
)
func main() {
LevenshteinDistance("stackoverflow", "stackexchange")
}
func LevenshteinDistance(s string, t string) int {
if s == t {
return 0
}
if len(s) == 0 {
return len(t)
}
if len(t) == 0 {
return len(s)
}
v0 := make([]int, len(t)+1)
v1 := make([]int, len(t)+1)
for i := 0; i < len(v0); i++ {
v0[i] = i
}
for i := 0; i < len(s); i++ {
v1[0] = i + 1
for j := 0; j < len(t); j++ {
cost := 0
if s[i] != t[j] {
cost = 1
}
v1[j+1] = int(math.Min(float64(v1[j]+1), math.Min(float64(v0[j+1]+1), float64(v0[j]+cost))))
}
for j := 0; j < len(v0); j++ {
v0[j] = v1[j]
}
}
return v1[len(t)]
}
Until Go 1.18 a one-off function was the standard way; for example, the stdlib's sort.go does it near the top of the file:
func min(a, b int) int {
if a < b {
return a
}
return b
}
You might still want or need to use this approach so your code works on Go versions below 1.18!
Starting with Go 1.18, you can write a generic min function which is just as efficient at run time as the hand-coded single-type version, but works with any type with < and > operators:
func min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
func main() {
fmt.Println(min(1, 2))
fmt.Println(min(1.5, 2.5))
fmt.Println(min("Hello", "世界"))
}
There's been discussion of updating the stdlib to add generic versions of existing functions, but if that happens it won't be until a later version.
math.Min(2, 3) happened to work because numeric constants in Go are untyped. Beware of treating float64s as a universal number type in general, though, since integers above 2^53 will get rounded if converted to float64.
There is no built-in min or max function for integers, but it’s simple to write your own. Thanks to support for variadic functions we can even compare more integers with just one call:
func MinOf(vars ...int) int {
min := vars[0]
for _, i := range vars {
if min > i {
min = i
}
}
return min
}
Usage:
MinOf(3, 9, 6, 2)
Similarly here is the max function:
func MaxOf(vars ...int) int {
max := vars[0]
for _, i := range vars {
if max < i {
max = i
}
}
return max
}
For example,
package main
import "fmt"
func min(x, y int) int {
if x < y {
return x
}
return y
}
func main() {
t := "stackoverflow"
v0 := make([]int, len(t)+1)
v1 := make([]int, len(t)+1)
cost := 1
j := 0
v1[j+1] = min(v1[j]+1, min(v0[j+1]+1, v0[j]+cost))
fmt.Println(v1[j+1])
}
Output:
1
Though the question is quite old, maybe my package imath can be helpful for someone who does not like reinventing a bicycle. There are few functions, finding minimal of two integers: ix.Min (for int), i8.Min (for int8), ux.Min (for uint) and so on. The package can be obtained with go get, imported in your project by URL and functions referred as typeabbreviation.FuncName, for example:
package main
import (
"fmt"
"<Full URL>/go-imath/ix"
)
func main() {
a, b := 45, -42
fmt.Println(ix.Min(a, b)) // Output: -42
}
As the accepted answer states, with the introduction of generics in go 1.18 it's now possible to write a generic function that provides min/max for different numeric types (there is not one built into the language). And with variadic arguments we can support comparing 2 elements or a longer list of elements.
func Min[T constraints.Ordered](args ...T) T {
min := args[0]
for _, x := range args {
if x < min {
min = x
}
}
return min
}
func Max[T constraints.Ordered](args ...T) T {
max := args[0]
for _, x := range args {
if x > max {
max = x
}
}
return max
}
example calls:
Max(1, 2) // 2
Max(4, 5, 3, 1, 2) // 5
Could use https://github.com/pkg/math:
import (
"fmt"
"github.com/pkg/math"
)
func main() {
a, b := 45, -42
fmt.Println(math.Min(a, b)) // Output: -42
}
Since the issue has already been resolved, I would like to add a few words. Always remember that the math package in Golang operates on float64. You can use type conversion to cast int into a float64. Keep in mind to account for type ranges. For example, you cannot fit a float64 into an int16 if the number exceeds the limit for int16 which is 32767. Last but not least, if you convert a float into an int in Golang, the decimal points get truncated without any rounding.
If you want the minimum of a set of N integers you can use (assuming N > 0):
import "sort"
func min(set []int) int {
sort.Slice(set, func(i, j int) bool {
return set[i] < set[j]
})
return set[0]
}
Where the second argument to min function is your less function, that is, the function that decides when an element i of the passed slice is less than an element j
Check it out here in Go Playground: https://go.dev/play/p/lyQYlkwKrsA

Read n integers / float / string from standard input

Algorithm competition have questions that provide the input in multiple lines, with the first line specifying the count of the inputs. Example -
3
78
42
99
The first line tells that there will be 3 integers followed by the three integers.
Currently, I have the following code to read them -
package main
import "fmt"
func main() {
var num []int
var input int
var count int
fmt.Scanf("%d", &count)
for {
if (count == 0) {
break
}
fmt.Scanf("%d", &input)
num = append(num, input)
count--
}
}
Is there a better way to carry this out? The above approach feels clumsy for some reason.
This code pushes everything into the loop header, as well as puts input into the most local scope possible. You should be checking the error returned by Scanf too:
package main
import "fmt"
func main() {
var num []int
var count int
var err error
for _, err = fmt.Scanf("%d\n", &count); err == nil && count > 0; count-- {
var input int
_, err = fmt.Scanf("%d\n", &input)
num = append(num, input)
}
if err != nil {
panic(err)
}
}
There are about a million ways to write equivalent code, this seemed the best to me. An argument could be made for putting the error check in the loop before the append, but since encountering an error presumably invalidates the list, I thought it looked prettier this way.
package main
import (
"bufio"
"os"
"fmt"
)
func main() {
reader := bufio.NewReader(os.Stdin)
a:= read(reader,100000)
fmt.Println(a)
}
func read (reader *bufio.Reader, n int)([]uint32) {
a := make([]uint32, n)
for i:=0; i<n; i++ {
fmt.Fscan(reader, &a[i])
}
return a
}

Resources