Shell challenge in Go. Stuck on #NI6

I’m unable to pass the test:

[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.

[tester::#NI6] Running tests for Stage #NI6 (Quoting - Single quotes)
[tester::#NI6] Running ./your_program.sh
[your-program] $ echo 'example shell'
[your-program] example shell
[tester::#NI6] ✓ Received expected response
[your-program] $ echo shell     hello
[your-program] shell hello
[tester::#NI6] ✓ Received expected response
[your-program] $ echo 'world     test' 'hello''example'
[your-program] world     test hello example
[tester::#NI6] Output does not match expected value.
[tester::#NI6] Expected: "world     test helloexample"
[tester::#NI6] Received: "world     test hello example"
[your-program] $ 
[tester::#NI6] Assertion failed.
[tester::#NI6] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

And I’m completely out of ideas. I even checked out code examples and copied one of the solutions (currently on my repository) but that also failed. I’m open to any ideas. Here is my repo: GitHub - danilovict2/go-shell

Hi @danilovict2, could you briefly explain in words how you’re currently parsing quoted strings? Then we can brainstorm potential solutions together.

I’m sorry I only just realized the repo was private. I’ve changed it to public, so you can go check out the code there, it’s in internal/reader/reader.go

@danilovict2 To be honest, I’m also a beginner in Go, so I might not be able to help you effectively just by reviewing code that’s not working yet.

How about we try some rubber duck debugging? If you could briefly explain your current approach for parsing quoted strings, we can work together to brainstorm potential solutions.

Or let me know if you need help understanding this error message:

[tester::#NI6] Output does not match expected value.
[tester::#NI6] Expected: "world     test helloexample"
[tester::#NI6] Received: "world     test hello example"

I’m don’t know Go well, but as far as I see, you treat each quoted as separate token:

	for {
		start := strings.Index(s, "'")
		if start == -1 {
			tokens = append(tokens, strings.Fields(s)...)
			break
		}

		tokens = append(tokens, strings.Fields(s[:start])...)
		s = s[start+1:]
		end := strings.Index(s, "'")
		token := s[:end]
		tokens = append(tokens, token)
		s = s[end+1:]
	}

but in the case of shell, it’s one token since no whitespace between, so you need to track state somehow and add token when you in proper state, but your code adds part before opening ' and text between quotes every time it finds quote

1 Like

here are a few examples

"before'after'"            => ["beforeafter"]
"before 'after'"           => "[before", "after"]
"'before'         'after'" => ["before", "after"]
"'before'after"            => ["beforeafter"]
1 Like

It might be helpful to first observe how a real shell parses the examples above:

but for his stage it needs to remove "

❯ echo before'after'
beforeafter
❯ echo before 'after'
before after
❯ echo 'before'                    'after'
before after
❯ echo 'before'after
beforeafter
1 Like

@stanykey You’re right! My bad for copying the double quotes when running the examples.

As I’ve mentioned before, this is a passing solution in code examples. Specifically the solution of a guy called ProjectElon. Still, thanks for the suggestion and I’ll make sure to try parsing tokens that aren’t separated with space as one

@danilovict2 Sorry for not explaining this earlier:

We recently added a test case for two quoted strings without spaces between them. As a result, the code you copied no longer passes all tests.

Let me know if you’d like to discuss potential ways to adjust your own code!

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.