Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

QUESTION : There are a variety of games possible with a deck of cards. For example, a deck of cards has four different suits, i.e.

QUESTION: There are a variety of games possible with a deck of cards. For example, a deck of cards has four different suits, i.e. spades (), clubs (), diamonds (), hearts (), and thirteen values for each suit. These decks of cards all have different number of categories and each category usually has the same number of cards.

Now write a function in python that uses list and for loop to create a deck of cards. Each element in the list will be a card, which is represented by a list containing the category as a string and the value as an int.

First, write a function called deck() that generates the deck of cards that contains all category in the categories list and each category should contain number_of_cards_per_cat starting from 1 to number_of_cards_per_cat as described in the doc string. You must follow the order of cards as listed in categories variable. If number_of_cards_per_cat is smaller than or equal to zero, then return an empty list.

Then write another function random_shuffle() that receives the list representing the deck of cards and return a randomly shuffled list but preserves the order of the categories. Your shuffled list should not be the same as the one generated from deck().

Finally, write a function reverse() that receives the list representing the deck of cards returns the list in reverse order.

TO DO: Download the file lab5.py, complete the functions inside according to their descriptions IMPORTANT: Do not change the file name or function names. Do not use input() or print(). Your file should not contain any additional lines of code outside the function definitions. Some example test cases are included in the docstrings.

HINTS:

You can generate N random integers between 1 and Max with the following lines of code: First, you should import the random library:

import random 

then:

random.sample(range(1, Max), N) 

return a list of N random numbers between 1 and Max

Couple of clarification points made:

1) In lab 5 random_shuffle() function, it is required to return a list that is not the same as the list passed in. For list that only has one item, namely, if in each category, you only have one card, then you don't need to shuffle that. You should return the same list as it is.

You can assume that the format of the input to random_shuffle() and reverse() are the same as the one generated from deck(), but the cards are not necessarily in order (e.g. [["spade", 1], ["spade", 3], ["spade", 2], ["heart", 3], ["heart", 1], ["heart", 2]]).

2) In random_shuffle() function, some of you randomize your list on the original list instead of creating a new list. This causes Markus to think that you return the same list instead of a shuffled list because we are comparing the input to your output. The lab handout does not explicitly state this is required, so we change our marking script to accommodate this situation. However, normally we don't want to change user inputs to a function (try this on your own if you did not implement it this way).

There is a small change to lab5 handout - we crossed out list comprehension - this was not taught so far, and we do not expect you to solve this lab with list comprehension (for loop and list methods will be just enough).

Finally, This is the initial code given:

import random

def deck(categories, num_of_cards_per_cat): """ (list, int) -> list Given a list of strings representation as category names and an integer indicating the number of cards in one category, return a deck of cards as a list. For example, with ["spades", "hearts"], 2, return a list of list: [["spades", 1],["spades",2], ["hearts", 1], ["hearts", 2]], where 1 and 2 are the index of the cards. >> deck(["spades", "hearts"], 2) [["spades", 1],["spades",2], ["hearts", 1], ["hearts", 2]] >> deck(["spades", "hearts"], -5) [] """

def random_shuffle(lst): """ (list) -> list Receives a deck of cards and returns a randomly ordered list containing all of the same elements. The returned list should preserve the order of the categories. See below console outputs as an example, where "s" and "d" stay in the same order as the input and only the numbers are shuffled. >> random_shuffle([["s",1],["s",2],["s",3],["d",1],["d",2],["d",3]]) [["s",3],["s",1],["s",2],["d",2],["d",1],["d",3]] """

def reverse(lst): """ (list) -> list Receives a list and returns a reverse ordered list containing all of the same elements. >> reverse([["spades", 1],["spades",2], ["hearts", 1], ["hearts", 2]]) [["hearts", 2], ["hearts", 1], ["spades", 2],["spades",1]] """

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

2. What are five kinds of nonproductive roles in teams? (LO 8-1)

Answered: 1 week ago

Question

7. Define cultural space.

Answered: 1 week ago

Question

8. Describe how cultural spaces are formed.

Answered: 1 week ago