High & Low Numbers From A String (Ruby) -


good evening,

i'm trying solve problem on codewars:

in little assignment given string of space separated numbers, , have return highest , lowest number.

example:

high_and_low("1 2 3 4 5")  # return "5 1" high_and_low("1 2 -3 4 5") # return "5 -3" high_and_low("1 9 3 4 -5") # return "9 -5" 

notes:

all numbers valid int32, no need validate them. there @ least 1 number in input string. output string must 2 numbers separated single space, , highest number first.

i came following solution cannot figure out why method returning "542" , not "-214 542". tried using #at, #shift , #pop, same result.

is there missing? hope can point me in right direction. understand why happening.

def high_and_low(numbers)     numberarray = numbers.split(/\s/).map(&:to_i).sort     numberarray[-1]     numberarray[0] end  high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6") 

edit

i tried , receive failed test "nil":

def high_and_low(numbers)   numberarray = numbers.split(/\s/).map(&:to_i).sort   puts "#{numberarray[-1]}" + " " + "#{numberarray[0]}" end 

when omitting return statement function return result of last expression within body. return both array write:

def high_and_low(numbers)     numberarray = numbers.split(/\s/).map(&:to_i).sort     return numberarray[0], numberarray[-1] end  puts high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6") # => [-214, 542] 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -