Write a function that will read scaleArr
. In this array, there will be two elements. The first will contain two positive integers that represent the left and right sides of a scale respectively. The second array represents the available weights that can be added to either side.
Balance the scale by using the least amount of weight from the list. You can only use an integer from available weights once.
For example: If scaleArr
is ["[9,4]","[1,2,6,7]"], you must add 2 to the left side and 7 to the right side to balance the scale. Both sides will equal 11!
If it is not possible to balance the scale using the weight available, return not possible
Test cases:
["[1,2]","[10,3,6,6]"]
["[20,5]","[1,6,10,4]"]
["[0,13]","[4,6,3,7]"]
Good luck, happy coding!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (5)
Haskell
Note: I'm really unsure about what I have done since I'm pretty new to Haskell and that there are no expected result for the test cases (or I'm both blinded & tired).
Note 2: it seems like in the example given, 1 & 6 can be used to balance the result. Since 1 & 6 are lower than 2 & 7, it makes them the least amounts to use for balance. Or I didn't understand the problem well enough.
Note 3: this implementation assumes that there will always be an input of format which is described above. Of course this will fail if the strings are differents.
Playground
Try it online here.
Just spent a few moments reading through your code. Here is some feedback I have, but I'm also kinda new so don't take it too seriously.
You should consider converting the first input array to a 2-tuple. You use it as a tuple in
equalizer
so having the type be a tuple will make the code clearer. It also forces you to do a little bit of input validation, which is always nice :DSome functions don't have a great name. Specifically,
equalizer
andcombinationPairs
. Personally, they should probably be verbs.combinations = combinePairs weights
sounds a bit nicer, in my opinion.Also, I like the use of applicative functors for the
combinePairs
function :)Thanks for taking some time to help me on my journey.
C#!
Try it: [repl.it/repls/MajesticBadDifference]
Naive solution here - basically construct a dictionary with the sum of all possible weight combinations for initial weight
a
and then add all possible combinations of weights to initial weightb
to determine which is the smallest number of weights to achieve equilibrium.I will be working on refactoring to make this more efficient, but I figured I'd code out a naive implementation as a baseline first before moving on and improving time complexity.
Python