Skip to content

Commit

Permalink
Add initial code and configuration files for the "Build Your Own <fil…
Browse files Browse the repository at this point in the history
…l_in_course_name>"

Challenge in Go, Python, and Rust. Uncomment the relevant code in the main files to pass the
first stage. Push the changes to the repository to submit the solution.
  • Loading branch information
rohitpaulk committed Jul 8, 2024
1 parent 19b999d commit e6a6448
Show file tree
Hide file tree
Showing 29 changed files with 809 additions and 0 deletions.
1 change: 1 addition & 0 deletions solutions/go/01-oq4/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
33 changes: 33 additions & 0 deletions solutions/go/01-oq4/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/<fill_in_course_slug>.png)

This is a starting point for Go solutions to the
["Build Your Own <fill_in_course_name>" Challenge](https://app.codecrafters.io/courses/<fill_in_course_slug>/overview).

_Add a description of your course here_

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your `<fill_in_executable_name>` implementation is in
`cmd/mygrep/main.go`. Study and uncomment the relevant code, and push your
changes to pass the first stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `go (1.19)` installed locally
1. Run `./<fill_in_script_name>.sh` to run your program, which is implemented in
`cmd/mygrep/main.go`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
49 changes: 49 additions & 0 deletions solutions/go/01-oq4/code/cmd/mygrep/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"bytes"
"fmt"
"io"
"os"
"unicode/utf8"
)

// Usage: echo <input_text> | your_grep.sh -E <pattern>
func main() {
if len(os.Args) < 3 || os.Args[1] != "-E" {
fmt.Fprintf(os.Stderr, "usage: mygrep -E <pattern>\n")
os.Exit(2) // 1 means no lines were selected, >1 means error
}

pattern := os.Args[2]

line, err := io.ReadAll(os.Stdin) // assume we're only dealing with a single line
if err != nil {
fmt.Fprintf(os.Stderr, "error: read input text: %v\n", err)
os.Exit(2)
}

ok, err := matchLine(line, pattern)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(2)
}

if !ok {
os.Exit(1)
}

// default exit code is 0 which means success
}

func matchLine(line []byte, pattern string) (bool, error) {
if utf8.RuneCountInString(pattern) != 1 {
return false, fmt.Errorf("unsupported pattern: %q", pattern)
}

var ok bool

ok = bytes.ContainsAny(line, pattern)

return ok, nil
}
11 changes: 11 additions & 0 deletions solutions/go/01-oq4/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Go version used to run your code
# on Codecrafters.
#
# Available versions: go-1.19
language_pack: go-1.19
11 changes: 11 additions & 0 deletions solutions/go/01-oq4/code/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// DON'T EDIT THIS!
//
// Codecrafters relies on this file being intact to run tests successfully. Any changes
// here will not reflect when CodeCrafters tests your code, and might even cause build
// failures.
//
// DON'T EDIT THIS!

module github.com/codecrafters-io/grep-starter-go

go 1.16
Empty file added solutions/go/01-oq4/code/go.sum
Empty file.
15 changes: 15 additions & 0 deletions solutions/go/01-oq4/code/your_grep.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
set -e

tmpFile=$(mktemp)

( cd $(dirname "$0") &&
go build -o "$tmpFile" ./cmd/mygrep )

exec "$tmpFile" "$@"
57 changes: 57 additions & 0 deletions solutions/go/01-oq4/diff/cmd/mygrep/main.go.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@@ -1,54 +1,49 @@
package main

import (
- // Uncomment this to pass the first stage
- // "bytes"
+ "bytes"
"fmt"
"io"
"os"
"unicode/utf8"
)

// Usage: echo <input_text> | your_grep.sh -E <pattern>
func main() {
if len(os.Args) < 3 || os.Args[1] != "-E" {
fmt.Fprintf(os.Stderr, "usage: mygrep -E <pattern>\n")
os.Exit(2) // 1 means no lines were selected, >1 means error
}

pattern := os.Args[2]

line, err := io.ReadAll(os.Stdin) // assume we're only dealing with a single line
if err != nil {
fmt.Fprintf(os.Stderr, "error: read input text: %v\n", err)
os.Exit(2)
}

ok, err := matchLine(line, pattern)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(2)
}

if !ok {
os.Exit(1)
}

// default exit code is 0 which means success
}

func matchLine(line []byte, pattern string) (bool, error) {
if utf8.RuneCountInString(pattern) != 1 {
return false, fmt.Errorf("unsupported pattern: %q", pattern)
}

var ok bool

- // You can use print statements as follows for debugging, they'll be visible when running tests.
- fmt.Println("Logs from your program will appear here!")
-
- // Uncomment this to pass the first stage
- // ok = bytes.ContainsAny(line, pattern)
+ ok = bytes.ContainsAny(line, pattern)

return ok, nil
}
21 changes: 21 additions & 0 deletions solutions/go/01-oq4/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The entry point for your writers-stg implementation is in `cmd/mygrep/main.go`.

Study and uncomment the relevant code:

```go
// Uncomment this to pass the first stage
"bytes"
```

```go
// Uncomment this to pass the first stage
ok = bytes.ContainsAny(line, pattern)
```

Push your changes to pass the first stage:

```
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```
1 change: 1 addition & 0 deletions solutions/python/01-oq4/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
153 changes: 153 additions & 0 deletions solutions/python/01-oq4/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
21 changes: 21 additions & 0 deletions solutions/python/01-oq4/code/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pyparsing = "~=3.0"
lark = "~=1.1"

[dev-packages]

[requires]
python_version = "3"
Loading

0 comments on commit e6a6448

Please sign in to comment.