/
ch-2.rb
48 lines (39 loc) · 974 Bytes
/
ch-2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby
=begin
----------------------------------------
AUTHOR: Robert DiCicco
DATE : 2023-10-11
Challenge 238 Task 02 Persistence Sort ( Ruby )
----------------------------------------
=end
myints = [[15, 99, 1, 34],[50, 25, 33, 22]]
def Reduce ( num )
steps = 0
while num > 9
arr = num.to_s.chars.map(&:to_i)
num = arr.reject(&:zero?).inject(:*)
steps += 1
end
return steps
end
myints.each do |mints|
h = Hash.new()
puts("Input: @int = #{mints}")
for cnt in 0..mints.length() - 1
retval = Reduce(mints[cnt])
h[mints[cnt]] = retval
cnt += 1
end
h2 = h.sort_by {|_key, value| value}.to_h
puts("Output: #{h2.keys}\n\n")
end
=begin
----------------------------------------
SAMPLE OUTPUT
ruby .\Persistence.rb
Input: @int = [15, 99, 1, 34]
Output: [1, 15, 34, 99]
Input: @int = [50, 25, 33, 22]
Output: [22, 33, 50, 25]
----------------------------------------
=end