Question

Python: Suppose you develop a code that replaces a string with a new string that consists...

Python: Suppose you develop a code that replaces a string with a new string that consists of all the even indexed characters of the original followed by all the odd indexed characters. For example, the string 'computers' would be encoded as 'cmuesoptr'. Write a function

encode(word)

that returns the encoded version of the string named word.

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

def encode(word):

    # string to store characters at even index

    even = ''

   

    # string to store characters at odd index

    odd = ''

    i = 1

   

    # compute string with characters at odd index

    while i - 1 < len(word):

   

        odd += word[i - 1]

       

        i += 2

       

    i = 2

   

    # compute string with characters at even index

    while i - 1 < len(word):

   

        even += word[i - 1]

       

        i += 2

       

    ans = odd + even

   

    return ans

   

print(encode('computers'))


Sample Output

Add a comment
Know the answer?
Add Answer to:
Python: Suppose you develop a code that replaces a string with a new string that consists...
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
  • Define a Python function named getResponse() that takes a string as its only argument and returns...

    Define a Python function named getResponse() that takes a string as its only argument and returns a new string. If the argument ends with a question mark and has an even number of characters, the function should return the string "Yes". If the argument ends with a question mark and contains an odd number of characters, the function should return the string "No". If the argument ends with an exclamation point, the function should return the string "Wow". Otherwise, the...

  • PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set...

    PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA is a run of 10 A’s. We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run. As another example, the string KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG would be encoded...

  • please help, language is python You’re creating a secret code to share with your friends such...

    please help, language is python You’re creating a secret code to share with your friends such that every word should be written with the second and last letters swapped. Write a function called encode that receives a string (input) and prints out another string with the second and last letters swapped. Example function calls: encode(secret) # should print “the code for secret is stcree” encode(city) # should print “the code for city is cyti”

  • Using Python 3.6 You’re creating a secret code to share with your friends such that every...

    Using Python 3.6 You’re creating a secret code to share with your friends such that every word should be written with the second and last letters swapped. Write a function called encode that receives a string (input) and prints out another string with the second and last letters swapped. Example function calls: encode(secret) # should print “the code for secret is stcree” encode(city) # should print “the code for city is cyti

  • D.1 [3] Define a function called funD1...) that receives a string and returns a new string...

    D.1 [3] Define a function called funD1...) that receives a string and returns a new string with all the characters in the original string in an EVEN position. As an example, the following code fragment: print (funD1('abcde')) should produce the output: ace D.2 [6] Define a function funD2(...) which receives a list ist that contains single letters, single digits and single special characters and the list contains at least one element of each type). The function returns a string that...

  • Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions...

    Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions you created. Only use one value of r and h for all the function. Ask the user for the r and h in the main() as an input, and h for all the functions. You should put the functions for areas in a separate file. ​ For example, firstDigtec7ze Write a function e digits n) (returning the number of digits in the argument returning...

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • 8.4 in python function that checks whether a string is a valid password. Suppose the pas...

    8.4 in python function that checks whether a string is a valid password. Suppose the pas rules are as follows: . A password must have at least eight characters - A password must consist of only letters and digits. ■ A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise (Occurrences of a specified character) Write a function...

  • Write a python function that takes a string as input, and returns a dictionary of frequencies...

    Write a python function that takes a string as input, and returns a dictionary of frequencies of all the characters in the string. For example: freq("dabcabcdbbcd") would return {"d":3, "a":2, "b":4, "c":3}. Then write a driver to demonstrate your function.

  • In C++ 1.Define a function named "sumCodes" that accepts two input parameters: "s "and "pos". "s"...

    In C++ 1.Define a function named "sumCodes" that accepts two input parameters: "s "and "pos". "s" is an object of the type "string" and "pos" is a boolean value with the default argument of "true". If "pos" is "true", the function returns the summation of the ASCII codes of all the characters located at the even indices of "s" and if "pos" is "false", the function returns the summation of the ASCII codes of all the characters located at the...

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