Write a function named shuffle() that randomizes the order of a deck of 52 cards stored in a list. To achieve this, visit each element in the list and swap it with another random element in the list. You must write your own loop to shuffle the cards. You cannot make use of Python’s built-in shuffle function.
Any idea how to go about doing this I've been googling all day and have been messing around with stuff but so far nothing
You have some boilerplate code for this challenge?
Code #1.
def createDeck():
deckComplete = [As, Ah, Ad, Ac, Ks, Kh, Kd, Kc, Js , Jh, Jd, Jc, Qs, Qh, Qd, Qc, Ts, Th, Td, Tc, 9s, 9h, 9d, 9c, 8s, 8h, 8d, 8c, 7s, 7h, 7d, 7c, 6s, 6h, 6d, 6c, 5s, 5h, 5d, 5c, 4s, 4h, 4d, 4c, 3s, 3h, 3d, 3c, 2s, 2h, 2d, 2c]
return deckComplete
#2.
def shuffle():
index = 0
while index < len(deckcomplete):
Feb 14, 2017 05:18 PM
(This post was last modified: Feb 14, 2017 05:19 PM by Goodies.)
Well, it's not that hard:
PHP Code
<?php from random import SystemRandom random = SystemRandom()
def shuffle(lst): size = len(lst) for i in range(size): r = random.randint(0, size - 1) lst[r], lst[i] = lst[i], lst[r]
Usage:
PHP Code
<?php Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from random import SystemRandom >>> random = SystemRandom() >>> >>> def shuffle(lst): ... size = len(lst) ... for i in range(size): ... r = random.randint(0, size - 1) ... lst[r], lst[i] = lst[i], lst[r] ... >>> x = list(range(10)) >>> x [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> shuffle(x) >>> x [4, 9, 0, 7, 5, 2, 8, 3, 1, 6] >>> shuffle(x) >>> x [7, 8, 9, 1, 3, 2, 0, 6, 5, 4] >>>
Code #1.
def createDeck():
deckComplete = [As, Ah, Ad, Ac, Ks, Kh, Kd, Kc, Js , Jh, Jd, Jc, Qs, Qh, Qd, Qc, Ts, Th, Td, Tc, 9s, 9h, 9d, 9c, 8s, 8h, 8d, 8c, 7s, 7h, 7d, 7c, 6s, 6h, 6d, 6c, 5s, 5h, 5d, 5c, 4s, 4h, 4d, 4c, 3s, 3h, 3d, 3c, 2s, 2h, 2d, 2c]
return deckComplete
#2.
from random import SystemRandom
random = SystemRandom()
def shuffle(deckComplete):
size = len(deckComplete)
for i in range(size):
r = random.randint(0, size - 1)
deckComplete[r], deckComplete[i] = deckComplete[i], deckComplete[r]
return deckComplete
So would that possibly work? this is using python 3.6.0b3
Feb 15, 2017 07:11 AM
(This post was last modified: Feb 15, 2017 01:55 PM by Riԁdle.)
Here's my go probably not better than goodies.
PHP Code
<?php from random import randint
ordered_list = ["A", "B", "C", "D", "E", "F"]
def shuffle_deck(unshuffed_deck): shuffled_deck = [] for _ in unshuffed_deck: shuffled_deck.append(unshuffed_deck[randint(0, len(unshuffed_deck) - 1)]) return shuffled_deck
for _ in range(10): print(shuffle_deck(ordered_list))
output:
Code ['E', 'B', 'C', 'C', 'E', 'B']
['C', 'D', 'B', 'D', 'D', 'B']
['D', 'E', 'E', 'B', 'B', 'D']
['B', 'E', 'D', 'B', 'E', 'F']
['D', 'B', 'D', 'B', 'E', 'E']
['B', 'B', 'E', 'D', 'F', 'A']
['D', 'D', 'A', 'C', 'B', 'A']
['C', 'E', 'A', 'C', 'B', 'B']
['B', 'C', 'A', 'F', 'B', 'B']
['A', 'B', 'B', 'B', 'A', 'C']
i like python lua js
|