Question

Python Function Name: unscramble Parameters: a string Returns: a string Description: Write a function, unscramble, that...

Python

Function Name: unscramble
Parameters: a string
Returns: a string
Description: Write a function, unscramble, that takes an input string, and returns a the unscrambled version of the argument.
To unscramble the string:

  • When the string has an odd number of characters, middle character is the first character in the unscrambled result
  • Pairs of remaining characters are added to the result, proceeding left to right from inner-most to outer-most characters.

Example Call: unscramble('3cis1') Expected result: ics31

Example Call: unscramble('ocicssol') Expected result: csiscool

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def unscrample(strg):


#finds length of string
length=len(strg)
#for storing new string
unscrampleString=" "
#if length of string is odd
if(length%2==1):


#finds mid index
midIndex=int(length/2)
#store character at midindex
unscrampleString+=strg[midIndex]
#for each left and right charcters
for i in range(1,midIndex+1):


unscrampleString+=strg[midIndex-i]
unscrampleString+=strg[midIndex+i]


#if length of string is even
else:


#finds mid index
midIndex=int(length/2)
#a copy of mid index
newMidIndex=midIndex
#for each left and right charcters
for i in range(1,midIndex+1):


unscrampleString+=strg[midIndex-i]
unscrampleString+=strg[newMidIndex]
#increment for next character
newMidIndex+=1


return unscrampleString


print(unscrample('3cis1'))
print(unscrample('ocicssol'))

Add a comment
Know the answer?
Add Answer to:
Python Function Name: unscramble Parameters: a string Returns: a string Description: Write a function, unscramble, that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT