- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if item can be measured using a scale and some weights in Python
Suppose we have some weights like a^0, a^1, a^2, …, a^100, here 'a' is an integer, and we also have a weighing scale where weights can be put on both the sides of that scale. We have to check whether a particular item of weight W can be measured using these weights or not.
So, if the input is like a = 4, W = 17, then the output will be True the weights are a^0 = 1, a^1 = 4, a^2 = 16, we can get 16 + 1 = 17.
To solve this, we will follow these steps −
- found := False
- Define a function util() . This will take idx, itemWt, weights, N
- if found is true, then
- return
- if itemWt is same as 0, then
- found := True
- return
- if idx > N, then
- return
- util(idx + 1, itemWt, weights, N)
- util(idx + 1, itemWt + weights[idx], weights, N)
- util(idx + 1, itemWt - weights[idx], weights, N)
- From the main method do the following −
- if a is either 2 or 3, then
- return True
- weights := a list of size 100 and fill with 0
- total_weights := 0
- weights[0] := 1, i := 1
- Do the following infinitely, do
- weights[i] := weights[i - 1] * a
- total_weights := total_weights + 1
- if weights[i] > 10^7
- come out from loop
- i := i + 1
- util(0, W, weights, total_weights)
- if found true, then
- return True
- return False
Example
Let us see the following implementation to get better understanding −
found = False def util(idx, itemWt, weights, N): global found if found: return if itemWt == 0: found = True return if idx > N: return util(idx + 1, itemWt, weights, N) util(idx + 1, itemWt + weights[idx], weights, N) util(idx + 1, itemWt - weights[idx], weights, N) def solve(a, W): global found if a == 2 or a == 3: return True weights = [0] * 100 total_weights = 0 weights[0] = 1 i = 1 while True: weights[i] = weights[i - 1] * a total_weights += 1 if weights[i] > 10**7: break i += 1 util(0, W, weights, total_weights) if found: return True return False a = 4 W = 17 print(solve(a, W))
Input
4, 17
Output
True
- Related Articles
- How can Keras be used to create a callback and save the weights using Python?
- How can Keras be used to manually save the weights using Python?
- Check if number can be displayed using seven segment led in Python
- Check if a two-character string can be made using given words in Python
- Check if a queue can be sorted into another queue using a stack in Python
- Check if a string can be formed from another string using given constraints in Python
- Check if a number can be expressed as a^b in Python
- How can Keras be used to load weights from checkpoint and re-evaluate the model using Python?
- Check if the array can be sorted using swaps between given indices only in Python
- How to check if a string can be converted to float in Python?
- Check if a string can be rearranged to form special palindrome in Python
- Check if a string can be repeated to make another string in Python
- C++ Program to check given candies can be split with equal weights or not
- Check if array can be sorted with one swap in Python
- How can I check if some text exist or not in the page using Selenium?

Advertisements