def sum_string (string: str) -> int: Given a string , return the sum of this string, as computed by the formula given below: To compute the sum of a string as defined by this function, we add every odd positioned digit, and subtract every even positioned digit. For example, if we had an input of "54321", we would add 5, subtract 4, add 3, subtract 2, and add 1, bringing us to a total of 3. In other words, you would add the first digit, subtract the second digit, and keep alternating until you reach the end of the string. If the string is empty, then the sum is 0. Note: since we give the input as a string, yoU will have to do some type conversion in order to solve this problem. Precondition: will only contain the characters 0-9. pass

CMPTR
3rd Edition
ISBN:9781337681872
Author:PINARD
Publisher:PINARD
Chapter10: Creating A Document
Section: Chapter Questions
Problem 2QY
icon
Related questions
Question
LAB RESTRICTIONS, PLEASE READ:
Do not add any imports, the ones that you need will be given to you.
You may not use any lists or list methods, or any while loops.
Within your loops, you MUST NOT use any break or continue statements.
Please also do not use try-except statements, you should be able to anticipate
or prevent any errors from happening at all!
Transcribed Image Text:LAB RESTRICTIONS, PLEASE READ: Do not add any imports, the ones that you need will be given to you. You may not use any lists or list methods, or any while loops. Within your loops, you MUST NOT use any break or continue statements. Please also do not use try-except statements, you should be able to anticipate or prevent any errors from happening at all!
Edef sum string(string: str) -> int:
Given a string <string>, return the sum of this string, as computed by the
formula given below:
To compute the sum of a string as defined by this function, we add every
odd positioned digit, and subtract every even positioned digit. For example,
if we had an input of "54321", we would add 5, subtract 4, add 3, subtract
2, and add 1, bringing us to a total of 3. In other words, you would add the
first digit, subtract the second digit, and keep alternating until you reach
the end of the string. If the string is empty, then the sum is 0.
Note: since we give the input as a string, you will have to do some type
conversion in order to solve this problem.
Precondition: <string> will only contain the characters 0-9.
pass
Transcribed Image Text:Edef sum string(string: str) -> int: Given a string <string>, return the sum of this string, as computed by the formula given below: To compute the sum of a string as defined by this function, we add every odd positioned digit, and subtract every even positioned digit. For example, if we had an input of "54321", we would add 5, subtract 4, add 3, subtract 2, and add 1, bringing us to a total of 3. In other words, you would add the first digit, subtract the second digit, and keep alternating until you reach the end of the string. If the string is empty, then the sum is 0. Note: since we give the input as a string, you will have to do some type conversion in order to solve this problem. Precondition: <string> will only contain the characters 0-9. pass
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
CMPTR
CMPTR
Computer Science
ISBN:
9781337681872
Author:
PINARD
Publisher:
Cengage