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.
52 lines
841 B
52 lines
841 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
|
||
|
}
|
||
5 years ago
|
opened = opened[:len(opened)-1]
|
||
|
ptr--
|
||
5 years ago
|
} else if c == ']' {
|
||
|
if ptr < 0 || opened[ptr] != '[' {
|
||
|
return false
|
||
|
}
|
||
5 years ago
|
opened = opened[:len(opened)-1]
|
||
|
ptr--
|
||
5 years ago
|
} else if c == '}' {
|
||
|
if ptr < 0 || opened[ptr] != '{' {
|
||
|
return false
|
||
|
}
|
||
5 years ago
|
opened = opened[:len(opened)-1]
|
||
|
ptr--
|
||
5 years ago
|
}
|
||
|
}
|
||
|
return len(opened) == 0
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
5 years ago
|
if len(os.Args) == 1 {
|
||
|
fmt.Println()
|
||
|
} else {
|
||
5 years ago
|
for _, v := range os.Args[1:] {
|
||
|
if matchBrackets(v) {
|
||
|
fmt.Println("OK")
|
||
|
} else {
|
||
|
fmt.Println("Error")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|