-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.rb
88 lines (82 loc) · 2.5 KB
/
array.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#largest-number-formed-from-an-array geeksforgeeks https://practice.geeksforgeeks.org/problems/largest-number-formed-from-an-array/0
$array = [54,548,546,60]#[4,90,645,96,540,60,5]#[9,3,30,34,5]#[54,548,546,60]
$array.sort!
$b = $array
$final_array = []
def main
one_array =[]
tens_array = []
third_array =[]
max_length = $array.max.to_s.length
$array.each do |val|
p "val: #{val.to_s.length}"
c = $array
p "c #{c}"
if val.to_s.length == 1
one_array << val
elsif val.to_s.length == 2
tens_array << val
elsif val.to_s.length == 3
third_array << val
end
end
p "ones_array: #{one_array}"
p "Second_array: #{tens_array}"
p "third_array: #{third_array}"
p 'final-arry'
result = find_greater one_array, tens_array,third_array,$array,[]
p result
p "result: #{result.join}"
end
def array_nul_chk one_array,second_array,third_array
if !one_array.empty?
max_one = one_array.max
else
max_one= 0
end
if !second_array.empty?
max_second = second_array.max
else
max_second= 0
end
if !third_array.empty?
max_third = third_array.max
else
max_third= 0
end
return max_one,max_second,max_third
end
def find_greater one_array, second_array,third_array,array,final_array
if !array.empty?
array.each do |bc|
p array
if bc.to_s.length == 1
max_one,max_second,max_third = array_nul_chk one_array,second_array,third_array
if ((max_one <= bc) && (max_second <= (bc * 10)) && (max_third <= (bc * 100)))
final_array << bc
one_array.delete(bc)
array.delete(bc)
find_greater one_array, second_array,third_array,array,final_array
end
elsif bc.to_s.length == 2
max_one,max_second,max_third = array_nul_chk one_array,second_array,third_array
if (((max_one * 10) <= bc) && (max_second <= bc) && (max_third <= (bc * 10)))
final_array << bc
second_array.delete(bc)
array.delete(bc)
find_greater one_array, second_array,third_array,array,final_array
end
elsif bc.to_s.length == 3
max_one,max_second,max_third = array_nul_chk one_array,second_array,third_array
if ((( max_one * 100) <= bc) && ((max_second * 10) <= bc) && (max_third <= (bc)) )
final_array << bc
third_array.delete(bc)
array.delete(bc)
find_greater one_array, second_array,third_array,array,final_array
end
end
end
end
final_array
end
main