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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Sum string
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
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY