Completed and solved my first problem of the year. I know that today is only the first day of the year, but why wait when there are so many interesting challenges to pursue in life? Here is what I encountered on the RubyMonk website.
Problem Statement:
Given a 3 or 4 digit number with distinct digits, return a sorted array of all the unique numbers than can be formed with those digits.
Example:
Given: 123
Return: [123, 132, 213, 231, 312, 321]
My solution:
def number_shuffle(number) number.to_s.split(//).permutation.to_a.map {|a| a.join.to_i } end
Tutorial solution:
def number_shuffle(number) no_of_combinations = number.to_s.size == 3 ? 6 : 24 digits = number.to_s.split(//) combinations = [] combinations << digits.shuffle.join.to_i while combinations.uniq.size!=no_of_combinations combinations.uniq.sort end
My solution is not only conciser but in my opinion more elegant as well. Applying Occam's razor principle here, I'll let you decide for yourself