Skip to content

Instantly share code, notes, and snippets.

@kkchu791
Created October 30, 2015 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkchu791/21301a4597b616523f0b to your computer and use it in GitHub Desktop.
Save kkchu791/21301a4597b616523f0b to your computer and use it in GitHub Desktop.
def sort num
res= []
res2= []
arr = num.to_s.split("")
arr.each { |x| res << x.to_i }
big_array = res.permutation.to_a
big_array.each {|x| res2 << x.join.to_i }
p res2
end
sort(123)
@charliemcelfresh
Copy link

Kirk,

Whenever someone asks me to write a function, I assume they mean I should return a value from that function, and not print out a value. I want to invite you to do the same with me -- return a value, don't print it.

Your solution is otherwise good, but I want to invite you to chain methods together, instead of storing temporary values in local variables.

I also want to invite you to follow the Github Ruby styleguide, which would have you put your arguments in parentheses:

def sort(num)

So altogether, something like this:

def sort(num)
  num.to_s.split("")
  .map{|n| n.to_i}
  .permutation.to_a
  .map{|a| a.join}
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment