Ruby Print Pyramid with 2D array (stuck in infinite loop)


  • Trying to create a pyramid with a 2D Array, they give us the bottom row of the pyramid. We get the next rows by adding the numbers of the row below. Cannot seem to get the top number of the pyramid. Goes into an infinite loop.

    Here is the description of the assignment:

    "Pyramid Sum
    Write a method pyramid_sum that takes in an array of numbers representing the base of a pyramid. The function should return a 2D array representing a complete pyramid with the given base. To construct a level of the pyramid, we take the sum of adjacent elements of the level below."

    def pyramid_sum(base)
      new_array = []
      sub_array =[]
      temp_array = base
      loop do
        # temp_array = [5, 10]
        puts "Top"
        print temp_array
        puts
        puts 
        temp_array.each_with_index do |num, idx|
          puts ".each loop"
         if idx+1 < temp_array.length 
            puts "adding..."
           sum = temp_array[idx] + temp_array[idx+1]
           sub_array << sum
         end # sub_array = [15]
        end
        puts "End of .each"
        new_array << sub_array
        temp_array = sub_array
        puts "Bottom"
        print temp_array
        puts
        puts 
        if temp_array.length == 1
         break
        end
      end
      return new_array
    end
    
    print pyramid_sum([1, 4, 6]) #=> [[15], [5, 10], [1, 4, 6]]
    puts
    
    #print pyramid_sum([3, 7, 2, 11]) #=> [[41], [19, 22], [10, 9, 13], [3, 7, 2, 11]]
    #puts
    


  • Hey, I think I figured it out. There are three changes that needs to be done in order to get the results you are looking for.

    1. new_array has to be initialized to whatever the user provides for base
    2. sub_array needs to be set to an empty array every-time it used to add to new_array
    3. The last step is just to reverse the final array new_array so it looks like what they expect the results to be

    Here is working version:

    def pyramid_sum(base)
      new_array = []
      sub_array =[]
      temp_array = base
      # Change 1
      new_array << base
      loop do
        temp_array.each_with_index do |num, idx|
         if idx+1 < temp_array.length 
           sum = temp_array[idx] + temp_array[idx+1]
           sub_array << sum
         end # sub_array = [15]
        end
        new_array << sub_array
        temp_array = sub_array
        # Change 2
        sub_array = []
        if temp_array.length == 1
         break
        end
      end
      # Change 3 
      return new_array.reverse
    end
    


  • Hey, I think I figured it out. There are three changes that needs to be done in order to get the results you are looking for.

    1. new_array has to be initialized to whatever the user provides for base
    2. sub_array needs to be set to an empty array every-time it used to add to new_array
    3. The last step is just to reverse the final array new_array so it looks like what they expect the results to be

    Here is working version:

    def pyramid_sum(base)
      new_array = []
      sub_array =[]
      temp_array = base
      # Change 1
      new_array << base
      loop do
        temp_array.each_with_index do |num, idx|
         if idx+1 < temp_array.length 
           sum = temp_array[idx] + temp_array[idx+1]
           sub_array << sum
         end # sub_array = [15]
        end
        new_array << sub_array
        temp_array = sub_array
        # Change 2
        sub_array = []
        if temp_array.length == 1
         break
        end
      end
      # Change 3 
      return new_array.reverse
    end
    


  • Thanks Avan!