Created
October 27, 2013 18:56
-
-
Save jli-hashrocket/7186464 to your computer and use it in GitHub Desktop.
Number Shuffle problem in Ruby Monk 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def factorial(number) | |
| if number <= 1 | |
| 1 | |
| else | |
| number * factorial(number - 1) | |
| end | |
| end | |
| def number_shuffle(number) | |
| new_array = [] | |
| number = number.to_s | |
| num_array = number.split("") | |
| factorial_num = factorial(num_array.length) | |
| while new_array.length < factorial_num | |
| shuffle_array = num_array.shuffle | |
| num_array = num_array.join("") | |
| new_array.push num_array | |
| puts new_array.uniq | |
| end | |
| end | |
| number_shuffle(123) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment