Fulcrum
def can_balance(items):
Each item in the list of items is now considered to be a physical weight, and therefore guaranteed to be a positive integer. Your task is to find and return a fulcrum position in this list 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 both sides, and therefore does not participate in the torque calculation.)
As taught in any introductory physics textbook, the torque of an item with respect to the fulcrum equals its weight times its distance from the fulcrum. If a fulcrum position exists, return that position, otherwise return -1 to indicate that the given itemscannot be balanced, at least not without rearranging them. (That one, by the way, would be an interesting but a more advanced problem normally suitable for a third year computer science course... but in Python, this algorithm could easily be built around this function by using the generator permutations in the module itertools to try out all possible permutations in an outer loop until you find one permutation that works. In fact, quite a few problems of this style can be solved with this "generate and test" approach without needing the fancy backtracking algorithms from third year and up.)
items |
Expected result |
[6, 1, 10, 5, 4] |
2 |
[10, 3, 3, 2, 1] |
1 |
[7, 3, 4, 2, 9, 7, 4] |
-1 |
[42] |
0 |
The program is written in python
def can_balance(items):
for i in range(len(items)):
tl=0 # to calculate the left torque
for j in range(0,i):
tl+=items[j]*(i-j)
tr=0 # to calculate the right torque
for k in range(i+1,len(items)):
tr+=items[k]*(k-i)
if(tr==tl): #checking if both the torques are equal
return i
return -1 # if index not found then return -1
l=[int(x) for x in input().split(" ")] #input the list
print(can_balance(l)) #calling the function to return the index
I hope the answer will help you. Please give a positive rating if the answer helps you.
Fulcrum def can_balance(items): Each item in the list of items is now considered to be a...
From the list of items and provided, categorized each item in the table below as either a factor that relates to contract law or tort law. > List of items: 1. Auditor failed to take reasonable care in the performance of the audit. Auditor fails to live up to their responsibility implicit in agreeing to act as the auditor and explicit in 2. the engagement letter. The work was below the standard that may be reasonably expected from a designated...
c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...
//Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...
Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...
SECTION 1 (COMPULSORY) FOR EACH OF THE FOLLOWING 50 OBJECTIVE-ITEMS, CHOOSE THE MOST APPROPRATEANSWER AND MARK YOUR ANSWER AGAINST 1-50 ON THE MARK- READING SHEET 1 Which of the following are characteristics of quanttative research? a investigating the relationships between vanables b using rating scales to quantify responses mposing control to limit extraneous variables researchers being personally involved with the partıcipants e organising raw data into meaningful conceptual patterns 1 a bcde 2 ab c 3 bcd 4 a de...