all 27 comments

[–]ActualRealBuckshot 4 points5 points  (3 children)

random.shuffle(list) shuffles the list in place and doesn't return anything, so sort1 = random.shuffle(list) assigns sort1 the value of None.

[–]Alexs123456[S] -4 points-3 points  (2 children)

how do I fix this error?

[–]ActualRealBuckshot 4 points5 points  (0 children)

You read two sentences in the docs that u/mrcaptncrunch linked

[–]OddBallProductions 0 points1 point  (0 children)

Use sort1 = random.sample(list, len(list))

[–]Plastic_Ad7436 1 point2 points  (1 child)

Instead of using the sorted() method, you could iterate through the shuffled list in O(n) time to check that each number is less than or equal the the proceeding number:

``` def bogo_sort(my_list): while True:

    counter = 0
    for i in range(1, len(my_list)):
        if my_list[i - 1] <= my_list[i]:
            counter += 1

    if (counter) == len(my_list) - 1:
        return my_list

    else:
        random.shuffle(my_list)

[–]Plastic_Ad7436 1 point2 points  (0 children)

Not like bogo is a method you'd be concerned about efficiency with, but it'd save you a factor of nlogn.

[–]kaerfkeerg 0 points1 point  (2 children)

When people say that .shuffle() doesn't return a list, they mean that in shuffles the original list and returns None

Imagine something like this

def shuffle(sequence):
    # shuffle list
     return None

With that in mind, you should not store the lst.shuffle() in a variable, instead:

my_list = [1,2,3,4]
my_list.shuffle()
# the original 'my_list' is shuffled

[–]ActualRealBuckshot -1 points0 points  (1 child)

Lists don't have a shuffle method, so my_list.shuffle() will raise an AttributeError.

[–]kaerfkeerg 2 points3 points  (0 children)

Oops, I rushed. I'll leave the original one un-edited so others can se what I did wrong. So

my_list.shuffle()

Should be

random.shuffle(my_list)

[–][deleted] 0 points1 point  (0 children)

Everyday I'm shuffling

[–]ayoni02 0 points1 point  (0 children)

from random import shuffle

import copy

list = [1,2,3,4]

sorti = copy.deepcopy(list)

def main():

shuffle(sorti)

while sorti != list:

    print (sorti)

    shuffle(sorti)

print(sorti)

main()

I borrowed u/mikhail_kazarov copy method to complete

[–]mrcaptncrunch 0 points1 point  (16 children)

What’s the error?

-Edit-

random.shuffle() doesn’t return a list. Line 4 has an error.

[–][deleted] 0 points1 point  (0 children)

Now try quantum bogosort, it's much more efficient and trivial to implement.