AtCoder Green tried to solve with Go

Is Go suitable for AtCoder?

I recently heard that Go's standard package doesn't have a max function, type binding is pretty tight, and there are a lot of strict aspects, so I tried to see how strict it really is compared to Python3. ..

Try to solve ABC188

A https://atcoder.jp/contests/abc188/tasks/abc188_a The problem is that if you decide three points in basketball, you will decide whether or not the development will be reversed.

It seems that the input can be entered with fmt.Scan (& a) for the declared type.

a.go


package main

import (
	"fmt"
	"math"
)

func main() {
	var X, Y int
	fmt.Scan(&X, &Y)
	if math.Abs(X-Y) < 3 {
		fmt.Print("Yes")
	} else {
		fmt.Print("No")
	}
}

I thought this would be fine, but apparently math.Abs ​​() only accepts float64 as an argument. So if you declare var X, Y float64 instead of var X, Y int, it works. https://golang.org/pkg/math/ The math package seems to only accept the basic float64, so using int doesn't seem to make much sense. I decided to use float64 for all subsequent declarations. By the way, if you write this in Python3

a.py


X, Y = map(int, input().split())
if abs(X - Y) < 3:
    print("Yes")
else:
    print("No")

Of course, Python's abs () can be int or float.

B https://atcoder.jp/contests/abc188/tasks/abc188_b The question of whether the dot product of Vectors is zero. Assuming that you can input the array with fmt.Scan (& A),

var X float64
var A, B []float64
fmt.Scan(&X, &A, &B)

I thought that there was a way to write something like this, but there was no such thing, so I wrote it as Gugu.

b.go


package main

import "fmt"

func main() {
	var N int
	fmt.Scan(&N)
	A, B := make([]float64, N), make([]float64, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&A[i])
	}
	for i := 0; i < N; i++ {
		fmt.Scan(&B[i])
	}

	total := float64(0)
	for i := 0; i < N; i++ {
		total += A[i] * B[i]
	}
	if total == 0 {
		fmt.Print("Yes")
	} else {
		fmt.Print("No")
	}
}

Using a for statement with standard input for arrays is a bit tricky. I want to make a template around here so that I can easily put it in.

b.py


_ = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))

total = 0
for i in range(len(X)):
    total += X[i] * Y[i]

if total == 0:
    print("Yes")
else :
    print("No")

In Python, it's nice to call it with one line of list (map (type, input (). Split ())).

C https://atcoder.jp/contests/abc188/tasks/abc188_c

It seems like dividing the array into the first half and the second half, holding the maximum value and index of each, and comparing them at the end.

c.go


package main

import (
	"fmt"
	"math"
)

func main() {
	var N int
	fmt.Scan(&N)
	sN := int(math.Pow(2, float64(N)))
	A := make([]int64, sN)
	for i := 0; i < sN; i++ {
		fmt.Scan(&A[i])
	}
	fmax, smax := int64(0), int64(0)
	findex, sindex := -1, -1
	for i, rating := range A {
		if i < sN/2 {
			if rating > fmax {
				fmax = rating
				findex = i
			}
		} else {
			if rating > smax {
				smax = rating
				sindex = i
			}
		}
	}
	if fmax < smax {
		fmt.Println(findex + 1)
	} else {
		fmt.Println(sindex + 1)
	}
}

You have to think about whether to use int or float64 depending on the scene, such as Pow becoming float64 and i of the for loop index becoming int. The brain death float64 didn't work. If you use slice and your own max function, you can write it a little smarter.

c.py


N = int(input())
sNh = 2 ** (N - 1)
A = list(map(int, input().split()))
fA = A[:sNh]
sA = A[sNh:]
print(A.index(min(max(fA), max(sA))) + 1)

As for Python3, it was easy to implement because it has min, max functions and Array index functions.

Personal impression

I have only answered 3 questions,

--Use of int and float64 properly --There seems to be no function in the standard package that seems to be in any language, such as the max function. --Array initialization is lazy

The above three points are at a level that seems to be solved once a template is made, so it seems unlikely that it will be difficult to do AtCoder with Go, but neither Go nor AtCoder are recommended for beginners. It's more fun to learn only the minimum Python notation and concentrate on solving AtCoder problems without stumbling.

Recommended Posts

AtCoder Green tried to solve with Go
I tried to solve TSP with QAOA
I tried to solve the soma cube with python
Connect to Postgresql with GO
I tried to solve the problem with Python Vol.1
Solve AtCoder ABC 186 with Python
I tried to solve AOJ's number theory with Python
I tried to solve a combination optimization problem with Qiskit
Solve AtCoder Problems Recommendation with python (20200517-0523)
I tried to solve the ant book beginner's edition with python
I tried to make a mechanism of exclusive control with Go
I tried to implement Autoencoder with TensorFlow
I tried to get started with Hy
I wanted to solve ABC160 with Python
I tried to let optuna solve Sudoku
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Solve AtCoder ABC168 with python (A ~ D)
I tried to implement CVAE with PyTorch
I wanted to solve ABC172 with Python
How to write offline real time I tried to solve E11 with python
How to write offline real time I tried to solve E12 with python
I tried to predict next year with AI
Try to solve the fizzbuzz problem with Keras
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
I tried to detect Mario with pytorch + yolov3
I tried to implement reading Dataset with PyTorch
I tried to use lightGBM, xgboost with Boruta
I tried to learn logical operations with TF Learn
I tried to move GAN (mnist) with keras
I wanted to solve NOMURA Contest 2020 with Python
Former image processing engineer tried to solve Saizeriya's spot the difference with OpenCV
Python with Go
I tried to save the data with discord
Try to solve the man-machine chart with Python
I tried to detect motion quickly with OpenCV
I tried to integrate with Keras in TFv1.1
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried running faiss with python, Go, Rust
I tried to detect an object with M2Det!
I tried to automate sushi making with python
I tried to predict Titanic survival with PyCaret
I want to solve APG4b with Python (Chapter 2)
I tried to study DP with Fibonacci sequence
I tried to start Jupyter with Amazon lightsail
I tried to judge Tsundere with Naive Bayes
Solve "AtCoder version! Ant book (beginner)" with Python!
[Fenwick_Tree] AtCoder Library-Reading with Green Coder-Implementation in Python-
I tried to solve the virtual machine placement optimization problem (simple version) with blueqat
[For beginners in competition professionals] I tried to solve 40 AOJ "ITP I" questions with python
The 15th offline real-time I tried to solve the problem of how to write with python
I tried to learn the sin function with chainer
I tried to move machine learning (ObjectDetection) with TouchDesigner
I tried to create a table only with Django
I tried to extract features with SIFT of OpenCV
I tried to move Faster R-CNN quickly with pytorch
I tried to implement and learn DCGAN with PyTorch
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
Try to solve the internship assignment problem with Python