Skip to content

Instantly share code, notes, and snippets.

@darmawan01
Created January 6, 2020 00:07
Show Gist options
  • Save darmawan01/3eb977f7f7fc39b533e74cf887409e3a to your computer and use it in GitHub Desktop.
Save darmawan01/3eb977f7f7fc39b533e74cf887409e3a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strings"
)
func main() {
// Our Test
args := "1 1 0 0 2 1"
count, argBoms := addBoms(args)
printOut(args, count, argBoms...)
args = "1 0 0 0 0 0 1 2 1 0"
count, argBoms = addBoms(args)
printOut(args, count, argBoms...)
args = "0 1 1 0 0 0 0 "
count, argBoms = addBoms(args)
printOut(args, count, argBoms...)
args = "0 0 0 0 0 0 0 0 0 0 0 0 0"
count, argBoms = addBoms(args)
printOut(args, count, argBoms...)
}
// Formating the output
func printOut(args string, count int, argBoms ...string) {
fmt.Printf("Args is %s \nAdded %d boms in args %s \n",
strings.ReplaceAll(args, " ", "|"),
count,
strings.Join(argBoms, "|"),
)
fmt.Println("---------------------End-----------------------")
}
// Counting how much boms to add
func addBoms(args string) (count int, temps []string) {
// Convert string args to slice
no := strings.Split(args, " ")
// Set default count to 0
count = 0
// Loop the splitted args
for _, n := range no {
// Local temps used for putting the boms
var temp []string
// Check is bom was exist in temps last element
isBomExist := (len(temps) > 0 &&
temps[len(temps)-1] == "R")
// Added one boms on the right of n
if n == "1" && (len(temps) == 0 || !isBomExist) {
temp = []string{n, "R"}
// Putting to temps as new value with boms was added
temps = append(temps, temp...)
count += 1
} else if n == "2" {
// Added two boms on left and right to of n
temp = []string{"R", n, "R"}
count += 2
// If bom is exist in temps last element, remove the left bom for n and
// reduce last count
if isBomExist {
temp = []string{n, "R"}
count -= 1
}
// Putting to temps as new value with boms was added
temps = append(temps, temp...)
} else {
// Putting the n to temps without boms around n
temps = append(temps, n)
}
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment