0
def get_firstArray
    a = Array.new(5)

    number =Array.new(5){ |index| index * 2 }
    return  print number
    puts
end
def get_secondtArray
    a = Array.new(4)

    number =A rray.new(4){ |index| index * 3 }
    return print number
    puts
end
a = get_firstArray
b = get_secondtArray

def merge(a, b)

    mer= a.concat b
     return mer
end

Output [0, 2, 4, 6, 8][0, 3, 6, 9]

I need the output like [0, 0, 2, 3, 4, 6, 6, 8, 9] with sorte

5 Answers 5

2

a.concat(b) will forever change a.

Instead you can let merge return a new array containing the union of a and b

def get_first_array
  Array.new(5) {|index| index * 2 }
end

def get_second_array
  Array.new(4) {|index| index * 3 }
end

def merge(a, b)
  (a+b).sort
end

a = get_first_array    # [0, 2, 4, 6, 8]
b = get_second_array   # [0, 3, 6, 9]

c = merge(a, b)        # [0, 0, 2, 3, 4, 6, 6, 8, 9]  
Sign up to request clarification or add additional context in comments.

2 Comments

Glad to be of help :)
Work for me :) this time
1

The print method returns nil so you you want to return number not print number. Nothing after return is executed, so you can remove the puts at the end of the methods. A method returns the result of the last statement automatically, so you don't need the return or the number variable. You're creating the array twice in the methods, so we can get rid of the first one. You're never calling the merge method, which also doesn't need a return statement. The method names aren't standard camel case, i.e., something like get_first_array would match the Ruby standard. You're never sorting the merged array. Putting those together, you end up with something like:

def evens
  Array.new(5) { |index| index * 2 }
end

def triples
  Array.new(4) { |index| index * 3 }
end

e = evens
t = triples

def merge(a, b)
  a.concat b
end
puts merge(e, t).sort

As a side note, here's how I would write this from scratch (main difference is not using methods for one-liners:

evens = (0..4).map { |n| n * 2 }
triples = (0..3).map { |n| n * 3 }
puts (evens + triples).sort

Comments

0
def get_first_array
  Array.new(5) {|index| index * 2 }
end

def get_second_array
  Array.new(4) {|index| index * 3 }
end

def merge(a, b)
  (a+b).sort
end

a = get_first_array   
b = get_second_array   

c = merge(a, b)

print c

Comments

0

This problem boils down to one of combining sorted arrays a and b in such a way that the resulting array is sorted. A natural solution is (a+b).sort, but this is relatively inefficient, considering that a and b are already sorted. Merging two sorted arrays must be a fairly common requirement, so I gave some thought to how that could be done efficiently.

The way I settled on is very straightforward (which was somewhat of a disappointment); there may well be better (or at least cleaner) algorithms that I'm not aware of. Incidentally, I gave some thought as to whether Enumerable#slice_before could be used to advantage, but came up empty.

def merge_sorted(a,b)
  sa, sb = a.size, b.size
  ia = ib = 0
  c = []
  loop do
    (return c + a[ia..-1]) if (ib == sb)
    (return c + b[ib..-1]) if (ia == sa)
    if a[ia] < b[ib]
      c << a[ia]
      ia += 1
    else
      c << b[ib]
      ib += 1
    end
  end
end

merge_sorted([0, 2, 4, 6, 8], [0, 3, 6, 9])
  #=> [0, 0, 2, 3, 4, 6, 6, 8, 9]

Comments

0

As def merge_sorted(a,b) sa, sb = a.size, b.size ia = ib = 0 c = [] loop do (return c + a[ia..-1]) if (ib == sb) (return c + b[ib..-1]) if (ia == sa) if a[ia] < b[ib] c << a[ia] ia += 1 else c << b[ib] ib += 1 end end end

merge_sorted([0, 2, 4, 6, 8], [0, 3, 6, 9])
  #=> [0, 0, 2, 3, 4, 6, 6, 8, 9]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.