Skip to content

Instantly share code, notes, and snippets.

@yukpiz
Created December 8, 2016 04:23
Show Gist options
  • Save yukpiz/249045ff0db4ab6d24584c4d9b2a9f89 to your computer and use it in GitHub Desktop.
Save yukpiz/249045ff0db4ab6d24584c4d9b2a9f89 to your computer and use it in GitHub Desktop.
package main
/*
$ cat in
8 3 7 1 2 5 6 0 9 4
decode
0728
> 7240
$ cat in
8 1 4 7 2 3 9 5 6 0
encode
257
> 435
*/
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func scan(sc *bufio.Scanner) string {
sc.Scan()
return sc.Text()
}
func atoi(v string) int {
i, _ := strconv.Atoi(v)
return i
}
func main() {
sc := bufio.NewScanner(os.Stdin)
sc.Split(bufio.ScanWords)
codes := []int{}
for i := 0; i < 10; i++ {
codes = append(codes, atoi(scan(sc)))
}
s := scan(sc)
val := scan(sc)
out := []string{}
if s == "encode" {
for _, v := range val {
intv := atoi(string(v))
out = append(out, fmt.Sprintf("%d", codes[intv]))
}
} else { //decode
for _, v := range val {
intv := atoi(string(v))
for i, c := range codes {
if c == intv {
out = append(out, fmt.Sprintf("%d", i))
}
}
}
}
fmt.Println(strings.Join(out, ""))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment