DEV Community

Discussion on: Daily Challenge #184 - Form the Minimum

Collapse
 
cipharius profile image
Valts Liepiņš • Edited

Ruby:

def minValue arr
    set = arr.uniq.sort
    set[0..1] = set[0..1].rotate if set[0] == 0 # Edge case for sets with 0
    set.join.to_i
end

Edit: Fixed the 0 edge case. Instead of adding 0 to the end of the number, put it in the 2. position.

Collapse
 
hyftar profile image
Simon Landry

Wouldn't rotating the array bring the biggest number at the front?

I think swapping the 1st and 2nd number would be ideal.

Also is having a leading zero really against the rules?

Collapse
 
cipharius profile image
Valts Liepiņš

Yeah, swapping seems to be better, would keep the large digits in the lesser valuable positions.

I interpreted the rules this way, because it didn't seem right to lose a digit in the resulting number. That would be like assuming input set [1,2,0] as equivelent to [1,2].