2

I am solving this problem on hacker rank https://www.hackerrank.com/challenges/mini-max-sum/problem

Its asking: Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than 32 bit integer.)

If I am doing string interpolations as in my code below, its giving me an error: Your code did not pass this test case. I know I can't use multiple variables in a single puts/p line.

array = gets.split(" ")

def get_max_and_min_sum(input_array)
    return "0 0" if input_array.size < 1

    input_array = input_array.map{|i| i.to_i}

    input_array = input_array.sort

    return "#{sum(input_array[0..3])} #{sum(input_array[1..4])}"



end

def sum(array)
    return array.inject(0){|sum,i| sum+=i}
end

p get_max_and_min_sum(array)

My question is how can I print multiple integers in one line separated by one space. I want to print 10 14 and not "10 14"

enter image description here

Saurav Prakash
  • 1,177
  • 1
  • 11
  • 25
  • what about `sum(input_array[0..3]).to_s + " " + sum(input_array[1..4]).to_s` – max pleaner Dec 29 '17 at 21:57
  • 1
    What is the purpose of using `p` to print the debugging representation of the result instead of just printing the result? – Jörg W Mittag Dec 29 '17 at 22:19
  • @maxpleaner same error printing "10 14" as shown in figure above – Saurav Prakash Dec 29 '17 at 22:28
  • @JörgWMittag It would print nothing on stdout, but only give return value. The console of Hackerank needs a printed value or it would raise error: no response on stdout – Saurav Prakash Dec 29 '17 at 22:29
  • 1
    Yes, but why print the debugging representation instead of the normal representation? – Jörg W Mittag Dec 29 '17 at 22:54
  • 1
    Why the rush to select an answer? Quick selections can discourage other answers and do not allow for the possibility that a better answer may follow shortly. There's no rush. Many here wait at least a couple of hours--some much longer--to award the greenie. This is not a criticism of the selected answer (which I upvoted) or a back-handed way of suggesting that you should select my answer. My answer does not even address your question directly. I omitted the explanation because others had already provided it. – Cary Swoveland Dec 29 '17 at 23:44

3 Answers3

4

Your problem is not in the string interpolation but in the method you use to print out the result to stdout. You should use print or puts instead of p since print and puts call to_s which simply converts to string, while p calls inspect which shows you more information (like the quotes to indicate that it is a string and in other cases stuff like hidden characters) and is more useful for debugging.

As for the difference between print and puts - puts simply inserts a newline at the end while print does not and prints exactly what you give it.

Kys Plox
  • 774
  • 2
  • 10
  • 23
1

The issue is the difference between p and print:

irb(main):003:0> p "1 2"
"1 2"

irb(main):005:0> print "1 2"
1 2

use print and your problem should be solved

max pleaner
  • 26,189
  • 9
  • 66
  • 118
1

Given

str = "21 4 8 13 11"

compute

arr = str.split.map(&:to_i)
  #=> [21, 4, 8, 13, 11]
smallest, largest = arr.minmax
  #=> [4, 21]
tot = arr.sum
  #=> 57
print "%d %d" % [tot-largest, tot-smallest]
36 53

Change print to puts in the last line if a carriage return is to be printed.

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100