Skip to content

Commit

Permalink
implement bogosort
Browse files Browse the repository at this point in the history
  • Loading branch information
jd-develop committed Dec 7, 2024
1 parent e04ac22 commit a4bf31f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ keeping track of the changes relative to the Nougaro Calculator under the same
## upcoming

### Added
* Added two modes in the `sort` built-in function: `slow` and `slow-verbose`.
* Added two modes to the `sort` built-in function: `slow` and `slow-verbose`.
Both are [slowsort](https://en.wikipedia.org/wiki/Slowsort) implementations,
the former is silent and the latter prints how much elements are already
sorted.
* Added one other mode to the `sort` built-in function: `bogo`.
It is a [bogosort](https://en.wikipedia.org/wiki/Bogosort) implementation.
* Added `math.gcd`, that takes exactly two integer arguments and return their
gcd.

Expand Down
12 changes: 12 additions & 0 deletions src/runtime/values/functions/sort_builtin_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# built-in python imports
from typing import Coroutine, Any
import sys
import random # for bogosort


def _get_comparison_gt(list_to_sort_: list[Value], index_: int) -> tuple[Number, None] | tuple[None, RunTimeError]:
Expand Down Expand Up @@ -182,11 +183,22 @@ async def wait_and_append(i_: int):
elif mode == "slow" or mode == "slow-verbose": # slowsort
sorted_ = _true_list_copy(list_to_sort)
_slow_sort(sorted_, 0, len(sorted_)-1, int(mode == "slow-verbose"))
elif mode == "bogo":
sorted_ = _true_list_copy(list_to_sort)
is_sorted_, error = _is_sorted(sorted_)
if error is not None:
return result.failure(error)
while not is_sorted_:
random.shuffle(sorted_)
is_sorted_, error = _is_sorted(sorted_)
if error is not None:
return result.failure(error)
else: # mode is none of the above
return result.failure(RunTimeError(
mode_noug.pos_start, mode_noug.pos_end,
"this mode does not exist. Available modes:\n"
" * 'timsort' (default),\n"
" * 'bogo',\n"
" * 'miracle',\n"
" * 'panic',\n"
" * 'sleep',\n"
Expand Down
6 changes: 6 additions & 0 deletions tests/test_file.noug
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,12 @@ def test_nougaro(print_OK)
assert sort([], "slow") == []
assert sort([0, -1], "slow") == [-1, 0]

print("Executing bogosort, this may take an infinite amount of time…")
assert sort([1, 2, 3, 4, 3, 2, 1, 5, 2], "bogo") == [1, 1, 2, 2, 2, 3, 3, 4, 5]
assert sort([], "bogo") == []
assert sort([0, -1], "bogo") == [-1, 0]
print("Done.")

assert sort([1, 2, 3, 4, 3, 2, 1, 5, 2, 6, 1, 2, 4, 3, 7], "stalin") == [1, 2, 3, 4, 5, 6, 7]
assert sort([1, 2, 3, 4, 3, 2, 1, 5, 0, 6, -400, 2, 4, 3, 7], "stalin") == [1, 2, 3, 4, 5, 6, 7]
assert sort([], "stalin") == []
Expand Down

0 comments on commit a4bf31f

Please sign in to comment.