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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education