Teetering on the Edge

Consider the teeter-totters below, otherwise known as seesaws.

teeter-totter

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 Bernardo 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 Bernardo 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.

Questions

For each of the below questions, you may assume that all elements of input lists will be nonnegative integers.

  1. (1 point.) Is the list [5, 5] balanceable?

  2. (1 point.) Is the list [1, 0, 1, 0, 1, 0] balanceable?

  3. (1 point.) Is the list [3, 4, 5, 6, 8] balanceable?

  4. (2 points.) Identify at least two potential features or properties of a list of items that would allow you to relatively quickly determine that the list is not balanceable.

  5. (8 points.) Implement in teetering.py, in Python, a function called balanceable that accepts one parameter: a list of integers called numbers. The function should return True if numbers is balanceable and False otherwise. You may assume that len(numbers) is positive and greater than or equal to 2. You may write additional helper functions if you wish, but your balanceable function must be prototyped as described above.

In addition to submitting this subquestion using the instructions contained in the “How to Submit” section of the Test, you must also:

  • Include your definition of the balanceable function in a file called teetering.py (importing any libraries necessary to run your code), and
  • Submit teetering.py by running submit50 cs50/problems/2019/fall/test/teetering.