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 " please do this on python and don't use any import functions"
def 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
" please do this on python and don't use any import functions"
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images