In: Computer Science
Build a two dimensional array out of the following three lists. The array will represent a deck of cards. The values in dCardValues correspond to the card names in dCardNames. Note that when you make an array all data types must be the same. Apply dSuits to dCardValues and dCardNames by assigning a suit to each set of 13 elements.
Once assigned your two dimensional array should resemble this :
2 Clubs 2
3 Clubs 3
4 Clubs 4
5 Clubs 5
6 Clubs 6
7 Clubs 7
8 Clubs 8
9 Clubs 9
10 Clubs 10
J Clubs 11
Q Clubs 12
K Clubs 13
A Clubs 14
2 Spades 2
3 Spades 3
4 Spades 4
5 Spades 5
6 Spades 6
7 Spades 7
8 Spades 8
9 Spades 9
10 Spades 10
J Spades 11
Q Spades 12
K Spades 13
A Spades 14
2 Diamonds 2
3 Diamonds 3
4 Diamonds 4
5 Diamonds 5
6 Diamonds 6
7 Diamonds 7
8 Diamonds 8
9 Diamonds 9
10 Diamonds 10
J Diamonds 11
Q Diamonds 12
K Diamonds 13
A Diamonds 14
2 Hearts 2
3 Hearts 3
4 Hearts 4
5 Hearts 5
6 Hearts 6
7 Hearts 7
8 Hearts 8
9 Hearts 9
10 Hearts 10
J Hearts 11
Q Hearts 12
K Hearts 13
A Hearts 14
Once you have this two dimensional array, you should shuffle it either by function or by code to produce a shuffled deck of cards.
Randomly choose and extract five cards from the deck.
After that apply the selection sort to the two dimensional array returning it to it original state as listed above. Display the 5 cards selected and their indexes. Display the remaining deck with its indexes.
Reshuffle and apply the insertion and selection sort.
IN PYTHON
import random
dCardNames = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
dCardValues = ['2','3','4','5','6','7','8','9','10','11','12','13','14']
dSuits = ["Clubs","Spades","Diamonds","Hearts"]
def shuffle(deck):
for i in range(len(deck)-1,0,-1):
r = random.randint(0,i)
deck[i],deck[r]=deck[r],deck[i]
return deck
Deck = []
for suit in dSuits:
for index in range(13):
card = [dCardNames[index], suit, dCardValues[index]]
Deck.append(card)
# print(dCardNames[index], dCardValues[index])
print(Deck)
Deck = shuffle(Deck)
print(Deck)
selected = []
for i in range(5):
r = random.randint(0,56)
selected.append(Deck[i])
# print(selected)
selected_index= []
def sort(deck):
size = 0
for suit in dSuits:
for index in range(13):
card = [dCardNames[index], suit, dCardValues[index]]
for i in range(len(deck)):
if deck[i] == card:
if card in selected:
selected_index.append([i,card])
temp = deck[i]
deck[i] = deck[size]
deck[size] = temp
size = size+1
return deck
Deck = sort(Deck)
print(Deck)
print(selected_index)
/////////////////////////////////END////////////////////////
# Feel free to ask questions in the comment section
# Hit the like if my answer if it is worth helping to you.
# you can payback and help me with a like.