DEV Community

Discussion on: Check if the chain has the same amount

Collapse
 
bgadrian profile image
Adrian B.G.

Time for a Go kata you say?


func IsBalanced(dic map[rune]int, s string) bool {
        b := 0
        for _, r := range strings.ToLower(s) {
            i, ok := dic[r]
            if ok == false {
                continue
            }
            b += i
        }
        return b == 0
    }   

func main() {
    //because the world is bigger than Latin
    var step = map[rune]int{'x': 1, '🗴': 1, 'o': -1, 'ອ': -1}
    table := map[string]bool{
        "":true, //0x == 0o
        "x0x0": true,
        "ອXອXອ":  false,
        "H🗴H🗴H(╯° °)╯︵ ┻━┻)HoHo":true,
    }

    for input, exp := range table {
        fmt.Printf("'%s', exp: '%v', got: '%v'\n", input, exp, IsBalanced(step, input))
    }

}


play.golang.org/p/n8q9BYrsW4a