Compound Balance Problem Description Amir was given an unbalanced chemical compound in…
- User
Compound Balance Problem Description Amir was given an unbalanced chemical compound in his chemistry class, and he was asked to balance them based on their valency and present them.
The chemical compound can have two distinct elements and he must balance them such that they reach the equivalent point.
Here the compounds are in the form of upper-case alphabets Ex AB, CS, ZO etc., and their valency is the sum of their ascii reduced to one digit. These compounds are balanced by generating the multiple of each of their valences such that the summation of those multiples reaches to an equivalent point. If any compound cannot be balanced to the equivalent point, then print "Not Possible" else print all the combinations in descending order of the multiple of valency of the first element. See example section for better understanding.
Constraints Compounds formed are between [A-Z].
1<=Equivalent point<=10^5
Input The first line contains the non-balanced compound.
The second line contains the equivalent point.
Output Print all the combination of the combinations in descending order of the multiple of valency of the first element or "Not Possible".
Time Limit (secs) 1
Examples Example 1
Input
AZ
100
Output
A41 Z2
A32 Z4
A23 Z6
A14 Z8
A5 Z10
Explanation
First we need to compute the valency of A and Z. Ascii A = 65 and Z = 90.
Hence valency of A = 6 + 5 = 11
1 + 1 = 2.
Similarly, valency of Z = 9 + 0 = 9.
Since, valency of A=2 and valency of Z=9 therefore the possible combination of the compounds is A41 Z2 that is 241 + 92 = 100. Similarly, A32 Z4 that is 232 + 94 = 100 and so on.
Mathematically A50 Z0 is also possible but as A50 Z0 means 50 molecules of A and 0 molecules of Z it is not really a compound. Hence A50 Z0 is not an output.
Example 2
Input
HZ
100
Output
Not Possible
Explanation
First we need to compute the valency of H and Z. Ascii H = 72 and Z = 90.
Hence valency of H = 7 + 2 = 9.
Similarly, valency of Z = 9 + 0 = 9.
Since, valency of H = 9 and Z = 9, therefore the closest it can get to the equivalent point is by multiplying 95+96 = 99 therefore it's not possible to balance.
- Assistant
Here's how we could write a Python program to solve the above problem.
We need to calculate the valencies, find all possible combinations that sum up to the equivalent point, and then print them in the required order. For two compounds with valencies
v1andv2, we're looking for integer solutions to the equationv1 * n1 + v2 * n2 = equivalent_point, wheren1andn2are the multipliers for each compound.python - User
- Assistant
- User
make the code run and check for input 5
1:2 2:3 3:4 4:3 5:2
1
2 5 0
3 5 60
2
1 5 180
3 5 120
4 5 60
3
2 5 300
4 5 0
5 5 60
1 5 240
4
2 5 240
3 5 180
5 5 120
5
3 5 240
4 5 300
1 5
- Assistant