I’m stuck on Stage Combining Character Classes #sh9.
I’ve tried:
- not using regexp to match regexp
Here are my logs:
[tester::#OQ2] Running tests for Stage #OQ2 (Match digits)
[tester::#OQ2] $ echo -n '123' | ./your_program.sh -E '\d'
[tester::#OQ2] ✓ Received exit code 0.
[tester::#OQ2] $ echo -n 'apple' | ./your_program.sh -E '\d'
[tester::#OQ2] ✓ Received exit code 1.
[tester::#OQ2] $ echo -n 'abc_0_xyz' | ./your_program.sh -E '\d'
[tester::#OQ2] ✓ Received exit code 0.
[tester::#OQ2] Test passed.
[tester::#CQ2] Running tests for Stage #CQ2 (Match a literal character)
[tester::#CQ2] $ echo -n 'dog' | ./your_program.sh -E 'd'
[tester::#CQ2] ✓ Received exit code 0.
[tester::#CQ2] $ echo -n 'dog' | ./your_program.sh -E 'f'
[tester::#CQ2] ✓ Received exit code 1.
[tester::#CQ2] Test passed.
anti-cheat (ac1) failed.
Please contact us at hello@codecrafters.io if you think this is a mistake.
And here’s a snippet of my code:
func runeIter(start, end rune) iter.Seq[rune] {
return func(y func(rune) bool) {
for r := start; r <= end; r++ {
if !y(r) {
return
}
}
}
}
const (
positiveCharacterGroup = "POSITIVE_CHAR_GROUP" // [a-zA-Z0-9]+
negativeCharacterGroup = "NEGATIVE_CHAR_GROUP" // [^a-zA-Z0-9]+
wordCharacterClass = "WORD_CHAR_CLASS" // \w
digitCharcaterClass = "DITIT_CHAR_CLASS" // \d
singleCharacter = "SINGLE_CHARACTER" // [a-zA-Z0-9]
combinedCharacterClass = "COMBINED_CHAR_CLASS" // [\\d\\wA-Za-z0-9]+
)
func patternType(pattern string) string {
switch len(pattern) {
case 1:
return singleCharacter
case 2:
if pattern == `\w` {
return wordCharacterClass
}
if pattern == `\d` {
return digitCharcaterClass
}
default:
if strings.HasPrefix(pattern, "[") && strings.HasSuffix(pattern, "]") {
re := regexp.MustCompile("[a-zA-Z0-9]+")
pattern = pattern[1 : len(pattern)-1]
if re.Match([]byte(pattern)) {
return positiveCharacterGroup
}
}
if strings.HasPrefix(pattern, "[^") && strings.HasSuffix(pattern, "]") {
re := regexp.MustCompile("[a-zA-Z0-9]+")
pattern = pattern[2 : len(pattern)-1]
if re.Match([]byte(pattern)) {
return negativeCharacterGroup
}
}
if strings.ContainsAny(pattern, `\w\d[]`) {
s := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(pattern, `\w`, ""), `\d`, ""), "[", ""), "]", "")
raz := runeIter('a', 'z')
faz := func(r rune) bool {
for v := range raz {
if r != v {
continue
}
return true
}
return false
}
rAZ := runeIter('A', 'Z')
fAZ := func(r rune) bool {
for v := range rAZ {
if r != v {
continue
}
return true
}
return false
}
r09 := runeIter('0', '9')
f09 := func(r rune) bool {
for v := range r09 {
if r != v {
continue
}
return true
}
return false
}
if strings.ContainsFunc(s, fAZ) || strings.ContainsFunc(s, faz) || strings.ContainsFunc(s, f09) || strings.Contains(s, "_") {
return combinedCharacterClass
}
}
// re := regexp.MustCompile(`[\\w\\d A-Za-z0-9]+`)
// if re.Match([]byte(pattern)) {
// return combinedCharacterClass
// }
}
return "unknown"
}
func matchLine(line []byte, pattern string) (bool, error) {
// if utf8.RuneCountInString(pattern) != 2 {
// return false, fmt.Errorf("unsupported pattern: %q", pattern)
// }
switch patternType(pattern) {
case digitCharcaterClass:
// You can use print statements as follows for debugging, they'll be visible when running tests.
// fmt.Fprintln(os.Stderr, "Logs from your program will appear here!")
// ok = bytes.ContainsAny(line, pattern)
f := func(r rune) bool {
for _, ch := range []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'} {
if r != ch {
continue
}
return true
}
return false
}
return bytes.ContainsFunc(line, f), nil
case wordCharacterClass:
re := regexp.MustCompile(pattern)
if match := re.Match(line); !match {
return match, fmt.Errorf("no matchs found in %s for pattern %s", string(line), pattern)
}
return true, nil
case positiveCharacterGroup:
re := regexp.MustCompile(pattern)
if match := re.Match(line); !match {
return match, fmt.Errorf("no match foudn in %s for pattern %s", string(line), pattern)
}
return true, nil
case negativeCharacterGroup:
re := regexp.MustCompile(pattern)
if match := re.Match(line); !match {
return match, fmt.Errorf("no matchs found in %s for pattern %s", string(line), pattern)
}
return true, nil
case combinedCharacterClass:
fmt.Println("triggered case")
re := regexp.MustCompile(pattern)
if match := re.Match(line); !match {
return match, fmt.Errorf("no matchs found in %s for pattern %s", string(line), pattern)
}
return true, nil
case singleCharacter:
return bytes.ContainsAny(line, pattern), nil
default:
return false, fmt.Errorf("unsupported pattern: %s", pattern)
}
}