Skip to content

Commit

Permalink
feat: Enable Miraclesort in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
macie committed Dec 5, 2023
1 parent 47e0838 commit 9051bb6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ e2e:
./dist/sortof -h
./dist/sortof bogo <test_case.unsorted | diff test_case.sorted -
./dist/sortof bogo -t 5s <test_case.unsorted | diff test_case.sorted -
./dist/sortof miracle <test_case.sorted | diff test_case.sorted -
./dist/sortof miracle -t 1ms <test_case.unsorted 2>&1 | grep '^sortof: '
./dist/sortof slow <test_case.unsorted | diff test_case.sorted -
./dist/sortof slow -t 100ms <test_case.unsorted | diff test_case.sorted -
./dist/sortof stalin <test_case.unsorted | diff test_case.stalinsorted -
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Implemented algorithms:

- [bogosort](https://en.wikipedia.org/wiki/Bogosort)
- [miraclesort](https://en.wikipedia.org/wiki/Bogosort#miracle_sort) (currently module only)
- [miraclesort](https://en.wikipedia.org/wiki/Bogosort#miracle_sort)
- [slowsort](https://en.wikipedia.org/wiki/Slowsort)
- [stalinsort](https://mastodon.social/@mathew/100958177234287431).

Expand Down
3 changes: 3 additions & 0 deletions cmd/sortof/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const helpMsg = "sortof - sort lines of text files\n" +
"\n" +
"Algorithms:\n" +
" bogo Bogosort\n" +
" miracle Miraclesort\n" +
" slow Slowsort\n" +
" stalin Stalinsort\n" +
"\n" +
Expand Down Expand Up @@ -70,6 +71,8 @@ func NewAppConfig(cliArgs []string) (AppConfig, error) {
switch cliArgs[0] {
case "bogo":
config.SortFunc = BogosortFile
case "miracle":
config.SortFunc = MiraclesortFile
case "slow":
config.SortFunc = SlowsortFile
case "stalin":
Expand Down
24 changes: 24 additions & 0 deletions cmd/sortof/sortfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ func BogosortFile(ctx context.Context, file io.ReadCloser) ([]string, error) {
return lines, nil
}

// MiraclesortFile returns a sorted lines from the file in ascending order.
// A context controls cancellation.
func MiraclesortFile(ctx context.Context, file io.ReadCloser) ([]string, error) {
lines := []string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
select {
case <-ctx.Done():
return []string{}, context.Cause(ctx)
default:
lines = append(lines, scanner.Text())
}
}
if err := scanner.Err(); err != nil {
return []string{}, err
}

if err := sortof.Miraclesort(ctx, lines); err != nil {
return []string{}, err
}

return lines, nil
}

// SlowsortFile returns a sorted lines from the file in ascending order.
// A context controls cancellation.
func SlowsortFile(ctx context.Context, file io.ReadCloser) ([]string, error) {
Expand Down

0 comments on commit 9051bb6

Please sign in to comment.