Question

please use pythonQuestion 4: Define a function named q40 that accepts a string as a parameter. After verifying that the string includes only includes numeric digits, count the number of even (E) and odd (O) digits. Finally, create a new string made up of these values and their sum. Repeat this process until your end result is a string containing 123 For example, 15327 contains 1 even digit (E-1), 4 odd digits(O-4) and the sum of these is 5 (E+O). The new string would be 145. Repeating the process (E-1, О-2, E+0-3) gives me .123. The function q40 should return the number of repetitions it takes to create the string .123 Example output of function q4(0 at the interactive Python prompt: >4(123) Already 123 so takes zero repetitions q4(5 2 4471 1 >>>q4 (1234567890) 2 NOTE: Your q40 function should return only the number of repetitions, it should not print anything.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def q4(s):
    count = 0
    while s != "123":
        evens = 0
        odds = 0
        for ch in s:
            d = int(ch)
            if d % 2 == 0:
                evens += 1
            else:
                odds += 1
        s = str(evens) + str(odds) + str(evens + odds)
        count += 1
    return count


print(q4('123'))
print(q4('5'))
print(q4('471'))
print(q4('1234567890'))
print(q4('0'))

02132

Add a comment
Know the answer?
Add Answer to:
please use python Question 4: Define a function named q40 that accepts a string as a...
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...

  • PYTHON Define a function named sum_products(...) which receives a string containing only digits (and the string...

    PYTHON Define a function named sum_products(...) which receives a string containing only digits (and the string contains with at least two digits) , and returns a number, which is the sum resulting from adding the multiplication all the two contiguous digits. For example, sum_products("1234") will return the number 20 because 20 = (1*2 + 2*3 + 3*4)

  • Use function expression syntax to write a function named whereToPark that accepts a string. The function...

    Use function expression syntax to write a function named whereToPark that accepts a string. The function should return the string “Lot A” when the provided string is “employee”. It should return the string “Lot B” when the provided string is “student”. If the provided string is anything other than “employee” or “student”, the function should return “Lot C”. Use an if else if structure, not three separate if statements in your function. c) This time include three tests of your...

  • Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument,...

    Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument, and returns an integer. This function should add the digits of a string that are in an even position. If there are no digits, the function should return -1. As an example, the following code fragment: string = "a12b056jk"; result=AddEvenPosDigs(string); print(result) should produce the output: 8

  • c++ Write a function Count_m_z that takes a string as an input parameter argument and counts...

    c++ Write a function Count_m_z that takes a string as an input parameter argument and counts the number of m, n, o, ... z characters in it. The function returns an integer value for the number of times those 14 lowercase letters appear in the input string. Your function should be named Count_m_z Your function should take one string parameter Your function should return the number of m, n, o, ... z characters as an integer Your function should not...

  • Define a function named double_add_digits_in_string(...) which receives a string as a parameter and returns the sum...

    Define a function named double_add_digits_in_string(...) which receives a string as a parameter and returns the sum of the digits multiplied by two. A solution using a loop is expected. As an example, the following code fragment: total = double_add_digits_in_string("xx1xx2xx3xx") print (total) should produce the output: 12

  • Write a python function called gen_pattern that accepts a string as an argument. Your function should...

    Write a python function called gen_pattern that accepts a string as an argument. Your function should generate a string pattern based on argument provided: If the string provided is a number and that number is even generate a pattern of hash signs. ex. gen_pattern(‘4’): #### If the string provided is odd, generate pattern of stars. Ex: gen_pattern(3): *** If string is not a number just output “ Not a number”

  • PYTHON 4. Define a function that takes two arguments: a string called strText and a number...

    PYTHON 4. Define a function that takes two arguments: a string called strText and a number called intNumber. This function will use a repetition structure (a While or For loop) to print strText intNumber of times. Call this function. 5. Get an input from the user that is a file name with an extension (e.g., "myfile.ipynb" or "myfile.txt"). Print only the characters that follow the "." in the file name (e.g., "ipynb" and "txt"). 6. Ask the user for 5...

  • Write a python function that takes in a string to use as a prompt and shows...

    Write a python function that takes in a string to use as a prompt and shows it to the user. Then get input from the user. If they entered a single sign out of the set +,-,*,/,% return it. If they entered anything else, give them a warning: “You may only enter one of the characters: +- * /%” and then repeat until they enter a correct option.

  • Make a public class called ManyExamples and in the class... Write a function named isEven which...

    Make a public class called ManyExamples and in the class... Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd. Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in 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