43
3
Write a program or function that takes in a nonempty list of positive integers. You may assume it is input in a reasonable convenient format such as "1 2 3 4"
or [1, 2, 3, 4]
.
The numbers in the input list represent the slices of a full pie chart where each slice size is proportional to its corresponding number and all slices are arranged around the chart in the order given.
For example, the pie for 1 2 3 4
is:
The question your code must answer is: Is the pie chart ever bisected? That is, is there ever a perfectly straight line from one side of the circle to the other, splitting it symmetrically in two?
You need to output a truthy value if there is at least one bisector and output a falsy value if there are none.
In the 1 2 3 4
example there is a bisection between 4 1
and 2 3
so the output would be truthy.
But for input 1 2 3 4 5
there is no bisector so the output would be falsy:
Additional Examples
Arranging numbers differently may remove bisectors.
e.g. 2 1 3 4
→ falsy:
If only one number is in the input list the pie is not bisected.
e.g. 10
→ falsy:
There may be multiple bisectors. As long as there are more than zero the output is truthy.
e.g. 6 6 12 12 12 11 1 12
→ truthy: (there are 3 bisectors here)
Bisections may exist even if they are not visually obvious.
e.g. 1000000 1000001
→ falsy:
e.g. 1000000 1000001 1
→ truthy:
(Thanks to nces.ed.gov for generating the pie charts.)
Test Cases
Truthy
1 2 3 4
6 6 12 12 12 11 1 12
1000000 1000001 1
1 2 3
1 1
42 42
1 17 9 13 2 7 3
3 1 2
10 20 10
Falsy
1 2 3 4 5
2 1 3 4
10
1000000 1000001
1
1 2
3 1 1
1 2 1 2 1 2
10 20 10 1
Scoring
The shortest code in bytes wins. Tiebreaker is earlier answer.
30I believe you mean piesected? – Alex A. – 2016-06-04T06:02:34.427
@HelkaHomba, can you rearrange the sectors to make it work, and is that what you meant by "arranging numbers differently may remove bisectors"? – Solomon Ucko – 2016-06-06T15:40:29.210
@SolomonUcko You may not rearrange the sectors. – Calvin's Hobbies – 2016-06-06T20:48:02.477
1Only [2 1 3 4] of the false cases has to actually be evaluated. The other false cases are easily rejected because their sum is odd (or their length is < 2). – Benny Jobigan – 2016-06-07T10:48:20.650