Skip to content

Commit

Permalink
Fix .shuffled() (#53)
Browse files Browse the repository at this point in the history
* Fix `.shuffled()`

* Prefer `.copy()` to `[:]`

* fix: test regression

---------

Co-authored-by: qexat <contact@qexat.com>
  • Loading branch information
InSyncWithFoo and qexat committed Apr 23, 2024
1 parent 535c54f commit e7694cb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
9 changes: 2 additions & 7 deletions magic_list/prelude.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,8 @@ def shuffled(self) -> typing_extensions.Self:
[]
"""

result = self.__class__()

while len(result) != len(self):
item = random.choice(self)

if item not in result:
result.append(item)
result = self.copy()
random.shuffle(result)

return result

Expand Down
4 changes: 2 additions & 2 deletions tests/list_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ def test_sorted_ok(prebuild_list, kwargs, result):
@pytest.mark.parametrize(
["prebuild_list", "result"],
[
["list_int_filled", list((-1, 3, 20, 5))],
["list_str_filled", list(("ciao", "hello", "holá", "bonjour"))],
["list_int_filled", list((20, 3, 5, -1))],
["list_str_filled", list(('holá', 'hello', 'bonjour', 'ciao'))],
["list_empty", list()],
],
indirect=["prebuild_list"],
Expand Down

0 comments on commit e7694cb

Please sign in to comment.