-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincrementIntegerAsArray.rb
More file actions
37 lines (33 loc) · 901 Bytes
/
incrementIntegerAsArray.rb
File metadata and controls
37 lines (33 loc) · 901 Bytes
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
puts 'Hello incrementIntAsArray'
puts 'INIT VALUES'
puts '========================='
allSamples = {
:emptyArray => [], # invalide
:minValueArray => [ 0 ], # 0
:minLngArray => [ 9 ], # 9
:randomAsArray => [ 1, 3, 6 ], # 136
:oneCurry => [ 1, 3, 9 ], # 139
:twoCurry => [ 1, 9, 9 ], # 199
:allCurry => [ 9, 9, 9 ] } # 999
def incrementIntAsArray arr
i = arr.length-1; curry = arr.length
while i >= 0 and curry > 0 do
arr[i] += 1 and curry = 0 if curry > 0
arr[i] = 0 and curry = 1 if arr[i] == 10
i += (-1)
end
arr = ([1] + arr) if curry > 0
arr
end
allSamples.keys.each { |k|
puts [ k.to_s.rjust(14), allSamples[k]].join(', ')
}
puts ''
puts 'INCREMENTED'
puts '========================='
allSamples.keys.each { |k|
puts [
k.to_s.rjust(14),
incrementIntAsArray(allSamples[k])
].join(', ')
}