From 5792e6be3053080330efd416703b2f64f3c3c35f Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Wed, 30 Nov 2022 10:11:27 -0800 Subject: [PATCH] initial commit --- shuffle.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 shuffle.py diff --git a/shuffle.py b/shuffle.py new file mode 100755 index 0000000..f5ec125 --- /dev/null +++ b/shuffle.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3.11 +import random + +# Here is an example of an orderd playlist with the artists of the songs. +# Some artists have more songs in the playlist than others, and currently +# they're all grouped together +examplePlaylist: list[str] = [ + "Artist A", "Artist B", "Artist B", "Artist C", "Artist D", "Artist E", "Artist E", "Artist E", "Artist E", + "Artist F", "Artist F", "Artist F", "Artist G", "Artist H", "Artist H", "Artist I", "Artist J", "Artist J", +] + +# This method to shuffle selects one item randomly and then appends it to a new list +# This method is not effective, when one item in an array appears more frequently, it +# is more likely to be picked in a row and will appear in a sequence with itself in the +# 'shuffled' list. +def shuffle_one(pl: list[str]) -> list[str]: + s: list = [] + for _ in range(len(pl)): + i = pl[random.randint(0, len(pl)) - 1] + pl.remove(i) + s.append(i) + return s + +# This algorithm is much better, it applies randomness to every element, selecting a random +# spot to shuffle with every time, this enables items previously shuffled to once again be +# shuffles to make a seemingly more 'random' shuffle. +def shuffle_two(pl: list[str]) -> list[str]: + for i in range(len(pl)): + swap_idx = random.randrange(i, len(pl)) + pl[i], pl[swap_idx] = pl[swap_idx], pl[i] + return pl + +def main() -> None: + print(shuffle_one(list(examplePlaylist))) + print(shuffle_two(list(examplePlaylist))) + +if __name__ == '__main__': + main() \ No newline at end of file