mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
882 B
55 lines
882 B
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
func matchBrackets(exp string) bool {
|
||
|
rn := []rune(exp)
|
||
|
var opened []rune
|
||
|
ptr := -1
|
||
|
for _, c := range rn {
|
||
|
if c == '(' || c == '[' || c == '{' {
|
||
|
opened = append(opened, c)
|
||
|
ptr++
|
||
|
} else if c == ')' {
|
||
|
if ptr < 0 || opened[ptr] != '(' {
|
||
|
return false
|
||
|
} else {
|
||
|
opened = opened[:len(opened)-1]
|
||
|
ptr--
|
||
|
}
|
||
|
} else if c == ']' {
|
||
|
if ptr < 0 || opened[ptr] != '[' {
|
||
|
return false
|
||
|
} else {
|
||
|
opened = opened[:len(opened)-1]
|
||
|
ptr--
|
||
|
}
|
||
|
} else if c == '}' {
|
||
|
if ptr < 0 || opened[ptr] != '{' {
|
||
|
return false
|
||
|
} else {
|
||
|
opened = opened[:len(opened)-1]
|
||
|
ptr--
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return len(opened) == 0
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
if len(os.Args) > 1 {
|
||
|
for _, v := range os.Args[1:] {
|
||
|
if matchBrackets(v) {
|
||
|
fmt.Println("OK")
|
||
|
} else {
|
||
|
fmt.Println("Error")
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
fmt.Println()
|
||
|
}
|
||
|
}
|