Skip to content

Instantly share code, notes, and snippets.

@onokonem
Created April 26, 2020 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onokonem/98976063d6486eb50d0a717e5ab6a7f0 to your computer and use it in GitHub Desktop.
Save onokonem/98976063d6486eb50d0a717e5ab6a7f0 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strconv"
)
func main() {
//f, err := os.Create("profile.data")
//if err != nil {
// panic(err)
//}
//pprof.StartCPUProfile(f)
//defer pprof.StopCPUProfile()
mergeArraysA2I(os.Stdin, os.Stdout)
}
var intMap = make([]int, 101)
var arraySep = []byte(" ")
func checkWriteError(_ int, err error) {
checkError(err)
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
func readLine(r *bufio.Reader) []byte {
l, _, err := r.ReadLine()
if err != nil {
panic(err)
}
return l
}
func readLineNum(r *bufio.Reader) int {
return atoi(string(readLine(r)))
}
func readLineArrayInt(r *bufio.Reader) []int {
l := readLine(r)
numbersText := bytes.Split(l, arraySep)
numbers := atoi(string(numbersText[0]))
res := make([]int, numbers)
for i := 1; i < numbers; i++ {
res[i] = atoi(string(numbersText[i]))
}
return res
}
func atoi(l string) int {
n, err := strconv.Atoi(l)
if err != nil {
panic(err)
}
return n
}
func mergeArraysA2I(reader io.Reader, out io.Writer) {
r := bufio.NewReaderSize(reader, 102400)
w := bufio.NewWriter(out)
for arrays := readLineNum(r); arrays > 0; arrays-- {
for _, n := range readLineArrayInt(r) {
intMap[n]++
}
}
for i := 0; i <= 100; i++ {
iStr := []byte(fmt.Sprintf("%d ", i))
for j := 0; j < intMap[i]; j++ {
checkWriteError(w.Write(iStr))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment