0

So I am trying to sum a total from an array, but once that total is above 8000 it should reduce the amount added by 50%. I seem to be getting the sum of the total from before and after the if condition. Can anyone explain why and how to fix it?

arr = [{ add_on: "AWD Drivetrain", price: 2500 }, { add_on: "Sport Package", price: 3500 }, { add_on: "Winter Tire Package", price: 2000 }, { add_on: "GPS Navigation", price: 2000 },]

def calculate_price_recursive(arr)
  prices = arr.sort_by { |x| -x[:price] }.map { |x| x[:price] }
  return recur_sum(prices, 0)
end


def recur_sum(prices, total)
  puts "#{prices}"
  return total if prices.count == 0
  if total < 8000
    prices[0] + recur_sum(prices[1..-1], total + prices[0])
  else
    (prices[0] / 2) + recur_sum(prices[1..-1], total + prices[0])
  end
end
  • If the running total is 7990 and the next element to be added is 20, is 100% percent of 20 added to the total? Your first sentence says nothing about sorting. Why are you sorting? Why must you use recursion? – Cary Swoveland Dec 18 '19 at 02:37
  • Yes 100% of 20 is to be added. I sorted because I want to add the highest values first so that it will divide the lesser values first. I am using recursion to learn how to use recursion. – thelacaning Dec 18 '19 at 03:29

3 Answers3

1

Ruby does not have TCO enabled by default. To enable it explicitly one should do this:

RubyVM::InstructionSequence.compile_option = {
  tailcall_optimization: true,
  trace_instruction: false
}

That said, the result might look like

RubyVM::InstructionSequence.compile_option = {
  tailcall_optimization: true,
  trace_instruction: false
}

arr = [
  { add_on: "AWD Drivetrain", price: 2500 },
  { add_on: "Sport Package", price: 3500 },
  { add_on: "Winter Tire Package", price: 2000 },
  { add_on: "GPS Navigation", price: 2000 }
]

def calculate_price_recursive(arr)
  prices = arr.map { |x| x[:price] }.sort.reverse
  recur_sum(prices)
end

def recur_sum(prices, total = 0)
  return total if prices.count == 0
  recur_sum(
    prices[1..-1],
    total + prices[0] / (total < 8000 ? 1 : 2)
  )
end
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
0

I was adding the stuff twice.

def recur_sum(prices, total)
  puts "#{prices}"
  return total if prices.count == 0
  if total < 8000
    recur_sum(prices[1..-1], total + prices[0])
  else
    recur_sum(prices[1..-1], total + prices[0] / 2)
  end
end

I found a way to do the whole thing recusrively in one function:

def recur_sum_holistic(arr, base, check, constant)
  if check == 1
    constant = arr.map { |x| x[:add_on] }.join(', ')
  end
  return "The cost for this car is $#{((base + (((base - 24999) * 0.02) + 1200)) * 1.13).round(2)} with the following configuration: #{constant}" if arr.count == 0
  recur_sum_holistic(arr[1..-1], base + (arr[0][:price] / (base < (24999 + 8000) ? 1 : 2)), 2, constant)
end
-2

\\for Element in Arr:

\\\\if ElementSum < 8300

\\\\\\ElementSum = ElementSum + Element

\\\\else

\\\\\\ElementSum = ElementSum + (Element / 2)

  • Welcome to stackoverflow. Please note that there are many markdown options available to you when formatting your posts: https://stackoverflow.com/editing-help – anothermh Dec 18 '19 at 02:27