# Balanced Numbers (4 points) Part II: Binary Addition… 1 answer below »

Part 1: Balanced Numbers (4 points) Part II: Binary Addition (5 points)
A balanced number is one where the sum of the digits in the first half of the number equals the sum of the digits in the second half of the number (assume that the original number always has an even number of digits). For example, 3562 is balanced because 3 + 5 = 8 and 6 + 2 = 8. 1479 is not balanced; 1 + 4 = 5, but 7 + 9 = 16.
Complete the is_balanced () function, which takes a single string argument representing an integer value. If the ar-gument is balanced according to the definition above, the function returns the Boolean value True; otherwise, it returns False (note that these values are Python "built-ins" or predefined words, not strings).
The easiest way to solve this problem is to use a while loop with two variables that represent index values (one index value initially starts with the value 0, while the other initially starts at the final index of the string). While the "left" index is less than the "right' index, add the digit at each index (don't forget to convert it from a string to an integer using the int ( ) function) to a separate sum variable (one for the first half of the number, and one for the second halt) and then update each index value (increment the left index and decrement the right index). When the loop ends (because the indices have met in the middle), compare the two sums.
For example, consider the input "202400". We proceed as follows:
Round Left index Right index Value at left index Value at right index Left sum Right sum 0 (prior to loop) – 0 0 1 0 5 2 0 2 0 2 1 4 0 0 2 0 3 2 3 2 4 4 4 4 (loop ends) 3 2 4 4
When the loop ends, the sum for each half is 4. They are equal, so this is a balanced number and we will return True.
Examples:
Function Call Return Value
is_balanced ( "1221" ) True is_balanced ( "624091") False is_balanced ( "125354") False is_balanced ( "745718") True
In lecture, we discussed data representation, including binary (base 2) representation. Addition is a common arithmetic operation, and it can be performed on numbers in any base. In fact, binary addition is simpler than base 10 addition because there are fewer rules. Like base 10 addition, binary addition works from right-to-left, adding one pair or column of digits at a time, according to the following rules:
• 0 + 0 = 0 • 0 + 1 = 1 • 1 + 0 = 1 • I + I = 0, plus a carry bit of 1 (for the next column) — recall that I + I = 2, and 2 is represented as 10 in binary
If you have three Is to add (1 + 1, plus a carry bit of 1 from the previous column), the sum is 1, with a carry bit of 1 (1 + 1 + 1 = 3, and 3 is represented as 11 in binary).
For example, consider the following addition problem (1 1 + 11, in base 10):
1011
+1011
We start by adding the rightmost column. I plus 1 gives us 0, plus a carry bit of 1:
0
The second column has two Is, plus a carry bit of 1. 1 + I + I equals 1, plus a new carry bit of 1:
10
The third column (counting from the right) has a I (the carry bit) plus two Os. This adds up to I (with nothing to carry to the next column, which is effectively a carry bit of 0):
1011
+1011
110
CSE 101 (Section 01) – Spring 2019 Lab #3 Page 2 CSE 101 (Section 01) – Spring 2019 Lab #3 Page 3

NEED HELP WITH SUCH A TASK? WE HAVE THE BEST TUTORS FOR YOU. ORDER NOW FROM 10\$/PG 