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 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. The first, shuffle_create(), 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.
2. The second, shuffle_in_place(), will accept one list parameter and shuffle
its elements in-place (that is, it does not create a new list) in any random order.
See the sample behavior below:
def main():
list_one =["Jean Valjean", "Javert", "Fantine", "Cosette", "Marius Pontmercy",
"Eponine", "Enjolras"]
print("ORIGINAL LIST_ONE: {}".format(list_one))
# First function execution
print("LIST CREATED BY SHUFFLE_CREATE: {}
".format(shuffle_create(list_one)))
list_two =["A",0,0,5,1,3,2]
print("ORIGINAL LIST_TWO: {}".format(list_two))
# Second function execution
shuffle_in_place(list_two)
print("LIST_TWO AFTER SHUFFLE_IN_PLACE: {}".format(list_two))
main()
A possible output (since the behavior is pseudo-random):
ORIGINAL LIST_ONE: ['Jean Valjean', 'Javert', 'Fantine',
'Cosette', 'Marius Pontmercy', 'Eponine', 'Enjolras']
LIST CREATED BY SHUFFLE_CREATE: ['Enjolras', 'Fantine', 'Jean
Valjean', 'Eponine', 'Cosette', 'Javert', 'Marius Pontmercy']
ORIGINAL LIST_TWO: ['A',0,0,5,1,3,2]
LIST_TWO AFTER SHUFFLE_IN_PLACE: [0,1,2,'A',5,3,0]
Some explanation as to what qualifies as a "shuffle" is probably in order. For
shuffle_create, it may be worth considering the use of random and list functions,
such as randrange/randint, 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 in-place 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!