Question: Problem 4 : Now You're Doing The CS Student Shuffle We'll start with another question from pedagogical programming's great canon: the list shuffle. 1 .
Problem : Now You're Doing The CS Student Shuffle
We'll start with another question from pedagogical programming's great canon: the list
shuffle.
The first, shufflecreate will accept one list parameter and return a new
list with the same elements from the list that was passed in but shuffled in any
random order.
The second, shuffleinplace will accept one list parameter and shuffle
its elements inplace that is it does not create a new list in any random order.
See the sample behavior below:
def main:
listone Jean Valjean", "Javert", "Fantine", "Cosette", "Marius Pontmercy",
"Eponine", "Enjolras"
printORIGINAL LISTONE: formatlistone
# First function execution
printLIST CREATED BY SHUFFLECREATE:
formatshufflecreatelistone
listtwo A
printORIGINAL LISTTWO: formatlisttwo
# Second function execution
shuffleinplacelisttwo
printLISTTWO AFTER SHUFFLEINPLACE: formatlisttwo
main
A possible output since the behavior is pseudorandom:
ORIGINAL LISTONE: Jean Valjean', 'Javert', 'Fantine',
'Cosette', 'Marius Pontmercy', 'Eponine', 'Enjolras'
LIST CREATED BY SHUFFLECREATE: Enjolras 'Fantine', 'Jean
Valjean', 'Eponine', 'Cosette', 'Javert', 'Marius Pontmercy'
ORIGINAL LISTTWO: A
LISTTWO AFTER SHUFFLEINPLACE: A
Some explanation as to what qualifies as a "shuffle" is probably in order. For
shufflecreate, it may be worth considering the use of random and list functions,
such as randrangerandint pop, and append. That is taking elements randomly
from your original list, and adding them to your new list, which of course starts empty.
For shuffling in place, it's worth remembering what inplace actually means. We're not
creating a new list, but rather editing the original list. In other words, we're replacing the
element at index x with the element at index y Think about how to achieve this until
none of the elements from the original list are in the same place as they were before.
Restrictions
You may not use the shuffle method from the random module
You may not use the sample method
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
