Skip to content

Instantly share code, notes, and snippets.

@halllllll halllllll/utility.go
Last active Jan 15, 2018

Embed
What would you like to do?
go utilities for competitive programming
package main
import (
"fmt"
"os"
"bufio"
"strconv"
"reflect"
)
var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
func main(){
sc.Split(bufio.ScanWords)
defer out.Flush() // !!!!coution!!!! you must use Fprint(out, ) not Print()
/* --- code ---*/
}
func next() string{
sc.Scan()
return sc.Text()
}
func nextInt() int{
a, _ := strconv.Atoi(next())
return a
}
func nextFloat64() float64{
a, _ := strconv.ParseFloat(next(), 64)
return a
}
func nextInts(n int) []int{
ret := make([]int, n)
for i:=0; i<n; i++{
ret[i] = nextInt()
}
return ret
}
func nextFloats(n int) []float64{
ret := make([]float64, n)
for i:=0; i<n; i++{
ret[i] = nextFloat64()
}
return ret
}
func nextStrings(n int) []string{
ret := make([]string, n)
for i:=0; i<n; i++{
ret[i] = next()
}
return ret
}
// Genericsがないので筋肉で...
// interfaceでの取扱がようわからんし
func containStr(arr []string, target string) bool{
for _, v := range arr{
if v==target{
return true
}
}
return false
}
func PrintOut(src interface{}, joinner string){
switch reflect.TypeOf(src).Kind(){
case reflect.Slice:
s := reflect.ValueOf(src)
for idx := 0; idx<s.Len(); idx++{
fmt.Fprintf(out, "%v", s.Index(idx))
if idx<s.Len()-1{
fmt.Fprintf(out, "%s", joinner)
}else{
fmt.Fprintln(out)
}
}
default:
fmt.Fprintln(out, "fuck")
}
}
// mapからkeysとvaluesを返す感じのやつ
// なんかしらんけどmap[interface{}]interface{}しか受け付けない。なんのためのinterface{}やねん
// なので呼ぶときのmapはmap[int]stringとかじゃダメ
func splitKeyValue(m map[interface{}]interface{})(keys, values []interface{}){
switch reflect.TypeOf(m).Kind(){
case reflect.Map:
s := reflect.ValueOf(m)
for _, v := range s.MapKeys(){
keys = append(keys, v)
values = append(values, s.MapIndex(v))
}
default:
fmt.Fprintln(out, "fuck")
}
return
}
@halllllll

This comment has been minimized.

Copy link
Owner Author

commented Jan 8, 2018

あと追加したいもの

  • ソート
@halllllll

This comment has been minimized.

Copy link
Owner Author

commented Jan 8, 2018

その前にRound追加した。小数点n桁で丸めるやつ

@halllllll

This comment has been minimized.

Copy link
Owner Author

commented Jan 10, 2018

出力をバッファして最後にflushするやつ追加した

@halllllll

This comment has been minimized.

Copy link
Owner Author

commented Jan 10, 2018

出力をバッファするやつ間違えてるので書き直す

@halllllll

This comment has been minimized.

Copy link
Owner Author

commented Jan 15, 2018

任意のスライス(基本型のみ)を任意の文字列区切りで出力するやつ追加した

@halllllll

This comment has been minimized.

Copy link
Owner Author

commented Jan 15, 2018

packageがわかりにくくなってきたのでmain含む全部足したったわ

@halllllll

This comment has been minimized.

Copy link
Owner Author

commented Jan 15, 2018

map[interface{}]interface{}をkeyとvalueのスライスにわけて返すやつ足した。なんでmap[interface{}]interface{}のみしか使えないのかは一切不明。 ↓参照

@halllllll

This comment has been minimized.

Copy link
Owner Author

commented Jan 15, 2018

↑のやつちょっと使いづらいというかmap[interface{}]interface{}で帰ってきたやつをどう使えばいいのかまったくわからんのだが・・?保留にするわ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.