Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here\'s how the assignment needs function to be set up: Write a function that sh

ID: 3639664 • Letter: H

Question

Here's how the assignment needs function to be set up:

Write a function that shuffles a string of characters, myString, using binKey. Here's the idea. We form a new empty string myShufString, which we then build by concatenating characters from either the beginning or end of myString, depending on whether binKey[i] is 0 or 1, starting with i=0 and cycling through binKey as necessary. Your function should be of the form shuffleString(myString, binKey) and should return myShufString.

Hint: You will probably want to form a list of the characters from myString and use the list method pop within a for loop over the length of myString. This way you will not have to keep track of which characters from the ends of myString have already been used and added to myShufString. You will call this function in step 5 to shuffle your alphabet that you created in step 1.

Here's an example. Suppose myString = 'abcdefgh' and binKey = '101'.

Then myShufString = 'hagfbedc'

Reason why:

We build myShufString as follows.

0) Form a list of the characters in myString, and call it myList, so:

myList = ['a','b','c','d','e','f','g','h'].

1) Start with an empty string: myShufString = ''

2) Since binKey[0] = '1', pop the 'h' from the end of myList and concatenate to myShufString.

3) Since binKey[1] = '0', pop the 'a' from the beginning of myList and concatenate to myShufString.

4) Since binKey[2] = '1', pop the 'g' from the end of myList and concatenate to myShufString.

5) Now we have reached the end of binKey, so we cycle back to the beginning.

6) Since binKey[0] = '1', pop the 'f' from the end of myList and concatenate to myShufString.

7) Since binKey[1] = '0', pop the 'b' from the beginning of myList and concatenate to myShufString.

I got this so far:

def shuffleString(myString, binKey):
myShufString = ""
myList = list(myString)
for string in range(len(myString)):
if binKey[i] == '0':
myShufString += myList.pop(0)
else:
myShufString += myList.pop(-1)

Not sure how to do the rest...

I know I have problems with my variables i and string, I'm just not sure what.
I really don't know what I'm doing, thank you for your time! I know this is a lot of information...

Explanation / Answer

def shuffleString(myString, binKey): myShufString = "" myList = list(myString) for string in range(len(myString)): if binKey[i] == '0': myShufString += myList.pop(0) else: myShufString += myList.pop(-1)