Looking for assistance with this question:
Fulcrum
Each element in items is a physical weight, a positive integer instead of some sort of helium balloon. Your task is to 0ind and return a fulcrum position in this list of weights so that when balanced on that position, the total torque of the items to the left of that position equals the total torque of the items to the right of that position. The item on the fulcrum is assumed to be centered symmetrically on the fulcrum, and does not participate in the torque calculation.
In physics, the torque of an item with respect to the fulcrum equals its weight times distance from the fulcrum. If a fulcrum position exists, return that position. Otherwise return -1 to arti0icially indicate that the given items cannot be balanced, at least without rearranging them.
items
Expected result
2
1
-1
[42]
0
The problem of 0inding the fulcrum position when rearranging elements is allowed would be an interesting but a far more advanced problem normally suitable for a third year computer science course. However, this algorithm could be built in an effective (although not as ef.icient) brute force fashion around this function by using the generator permutations in the Python standard library module itertools to try out all possible permutations in an outer loop until you 0ind one permutation that works. In fact, quite a few problems of this style can be solved with this “generate and test” approach without the backtracking algorithms from third year courses.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here