Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

First of all, you are not supposed to modify the list in-place. Instead, return a shuffled copy of the original list. This is also checked by this doc test:

>>> cards[:12]  # Should not be changed
['A♡', 'A♢', 'A♤', 'A♧', '2♡', '2♢', '2♤', '2♧', '3♡', '3♢', '3♤', '3♧']

Furthermore, using recursion makes this overly complicated. After all, the solution to get the desired result is pretty simple: Cut the list in half and then take one item alternatively from each sublist. The first part is simple splicing, and ways to do the latter is covered in this questionin this question. So you could end up with this:

def shuffle (cards):
    # split the list in halves
    first, second = cards[:len(cards) // 2], cards[len(cards) // 2:]

    # combine them alternatively, e.g.
    return sum(map(list, zip(first, second)), [])

You can also do it without splitting the list by just iterating it alternatively from the beginning and from the middle:

def shuffle (cards):
    result = []
    half = len(cards) // 2
    for i in range(half):
        result.append(cards[i])
        result.append(cards[i + half])
    return result

First of all, you are not supposed to modify the list in-place. Instead, return a shuffled copy of the original list. This is also checked by this doc test:

>>> cards[:12]  # Should not be changed
['A♡', 'A♢', 'A♤', 'A♧', '2♡', '2♢', '2♤', '2♧', '3♡', '3♢', '3♤', '3♧']

Furthermore, using recursion makes this overly complicated. After all, the solution to get the desired result is pretty simple: Cut the list in half and then take one item alternatively from each sublist. The first part is simple splicing, and ways to do the latter is covered in this question. So you could end up with this:

def shuffle (cards):
    # split the list in halves
    first, second = cards[:len(cards) // 2], cards[len(cards) // 2:]

    # combine them alternatively, e.g.
    return sum(map(list, zip(first, second)), [])

You can also do it without splitting the list by just iterating it alternatively from the beginning and from the middle:

def shuffle (cards):
    result = []
    half = len(cards) // 2
    for i in range(half):
        result.append(cards[i])
        result.append(cards[i + half])
    return result

First of all, you are not supposed to modify the list in-place. Instead, return a shuffled copy of the original list. This is also checked by this doc test:

>>> cards[:12]  # Should not be changed
['A♡', 'A♢', 'A♤', 'A♧', '2♡', '2♢', '2♤', '2♧', '3♡', '3♢', '3♤', '3♧']

Furthermore, using recursion makes this overly complicated. After all, the solution to get the desired result is pretty simple: Cut the list in half and then take one item alternatively from each sublist. The first part is simple splicing, and ways to do the latter is covered in this question. So you could end up with this:

def shuffle (cards):
    # split the list in halves
    first, second = cards[:len(cards) // 2], cards[len(cards) // 2:]

    # combine them alternatively, e.g.
    return sum(map(list, zip(first, second)), [])

You can also do it without splitting the list by just iterating it alternatively from the beginning and from the middle:

def shuffle (cards):
    result = []
    half = len(cards) // 2
    for i in range(half):
        result.append(cards[i])
        result.append(cards[i + half])
    return result
Source Link
poke
  • 752
  • 8
  • 14

First of all, you are not supposed to modify the list in-place. Instead, return a shuffled copy of the original list. This is also checked by this doc test:

>>> cards[:12]  # Should not be changed
['A♡', 'A♢', 'A♤', 'A♧', '2♡', '2♢', '2♤', '2♧', '3♡', '3♢', '3♤', '3♧']

Furthermore, using recursion makes this overly complicated. After all, the solution to get the desired result is pretty simple: Cut the list in half and then take one item alternatively from each sublist. The first part is simple splicing, and ways to do the latter is covered in this question. So you could end up with this:

def shuffle (cards):
    # split the list in halves
    first, second = cards[:len(cards) // 2], cards[len(cards) // 2:]

    # combine them alternatively, e.g.
    return sum(map(list, zip(first, second)), [])

You can also do it without splitting the list by just iterating it alternatively from the beginning and from the middle:

def shuffle (cards):
    result = []
    half = len(cards) // 2
    for i in range(half):
        result.append(cards[i])
        result.append(cards[i + half])
    return result