question archive I need help on a CS50 teetering question, where they've asked make a python program that accepts list of numbers and checks if they are balance-able or not
Subject:LawPrice: Bought3
I need help on a CS50 teetering question, where they've asked make a python program that accepts list of numbers and checks if they are balance-able or not.
This is the questions:
Let's assume that four staff members (Analysia, Bernardo, Christie, and Diana) want to teeter (i.e., sit) on these totters, and let's further assume that Analysia weighs 30kg, Bernardo weighs 35kg, Christie weighs 25kg, and Diana weighs 30kg. Is it possible to arrange the staff in some way on the teeter-totter such that it would be balanced? The answer is yes: with Analysia and Diana on one side, and Bernard and Christie on the other, each side of the teeter-totter would have 60kg of weight on it.
Now let's imagine we add a fifth staff member: Elijah, who weighs 50kg. Can the teeter-totter be balanced now? Again, yes. With Bernard and Elijah on one side, and with Analysia, Christie, and Diana on the other, each side of the teeter-totter has 85kg of weight on it, even though the two sides don't have the same number of staff.
Equivalently, we can consider a list in Python "balanceable" if it is possible to permute its elements and put an imaginary fulcrum between two elements such that the sum of elements on the left side of the fulcrum is equal to the sum of the elements on the right half of the fulcrum.
For example,
a = [9, 26, 2, 19]
is balanceable, because we could rearrange the elements of a to be [9, 19, 2, 26] and place the fulcrum between a[1] and a[2] and the sum of the elements on each side of the fulcrum would be 28. (Note that other arrangements of the elements of a are possible as well.) Similarly,
b = [1, 2, 3, 9, 3]
is also balanceable, because we could, for instance, rearrange the elements of b to be [9, 3, 3, 1, 2] and place the fulcrum between b[0] and b[1], in which case the sum of the elements on each side of the fulcrum would be 9. Note that the list is balanceable even though the number of elements on each side of the fulcrum is different, with one element on the left and four elements on the right.
By contrast, note that
c = [19, 8, 12]
is not balanceable. There is no way to permute the elements of c and place a fulcrum between any two of them and have the list be balanced.
It'll be greatly appreciated if you can help me on this question.