Conditions:
a + b + c = 100
a,b,c positive integers or 0
Desired output:
[
[0,0,100],
[0,1,99 ],
... # all other permutations
[99,1,0 ],
[100,0,0]
]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’d write:
Site note 1: this is a classical example where list-comprehensions shine (and even more if there were a condition somewhere). Since Ruby has no LC we have to do the typical conversion to OOP: N-1
flat_map‘s + 1map. It would be awesome to have LCs in Ruby (check this feature request), Scala has proven that even a pure OOP language greatly benefits from this syntactic sugar (though I can understand prevention from the devs because of the implicit iterable protocol/method). On an imaginary Ruby that supported them you’d write:Side note 2: Imagine that you prefer a less memory-consuming solution (you probably don’t need the whole array). A lazy solution with Ruby 2.0 requires just to add a couple of
[lazy][2]proxies:Side note 3: Just for completeness, in the line of @akuhn answer, another lazy solution using enumerators: