7
You will be given a positive integer as input.
The integer is the board of a seesaw.
Th integer will not have leading zeroes. You may take this input however you like.
Your task is to output the location of the pivot point of this seesaw, such that the board would balance.
A board balances if the moments on each side are equal in magnitude. A moment is calculated by multiplying the force by the distance from the pivot point. In this case, the force is equal to the digit.
For the integer 100 with a pivot point below the first 0, the moment on the left is 1x1=1. The moment on the right is 0x1=0. If the pivot point was below the second 0, the moment on the left would be 2x1=2 (because the 1 is now 2 away from the pivot).
For example, for the integer 31532 the pivot goes underneath the number 5. This is because the moments on the left are 3x2 + 1x1 = 7 and on the right the moments are 3x1 + 2x2 = 7.
The output for this integer is 3 because the pivot goes underneath the 3rd digit.
If the integer cannot be balanced, your code does not have to do anything - it can hang, error, output nothing - whatever you want.
Note that a pivot cannot go between two numbers. The pivot location must be an integer.
Test cases:
31532 -> 3
101 -> 2
231592 -> 4
8900311672 -> 5
Standard loopholes apply, this is code golf so shortest answer wins.
Can I not handle the last testcase if it is too large? – Leaky Nun – 2017-07-15T02:46:14.013
1Can the output be 0-indexed? – Dennis – 2017-07-15T04:41:27.897
Can the output be indexed from the front of the number? Your test cases seem to do this. – Post Rock Garf Hunter – 2017-07-15T04:42:47.167
Related – Peter Taylor – 2017-07-15T06:52:25.440
1Is a list of digits a valid input format? – Lynn – 2017-07-15T09:07:16.817
Duplicate? (balancing words on ascii value instead of numbers) – nimi – 2017-07-15T10:01:48.760
@LeakyNun that’s fine – Tim – 2017-07-15T11:02:08.273
@Dennis no, 1 indexed please – Tim – 2017-07-15T11:02:26.757
@WheatWizard it has to be indexed from the left, with the left most digit = 1 – Tim – 2017-07-15T11:03:11.960