Sum of values in loop and keeping highest value - ruby-on-rails

I am trying to calculate the shipping dimensions of multiple products. I want to return the largest value of #tempLength and #tempWidth and sum up all #tempHeights in my loop:
#tempLength = 0
#tempWidth = 0
#tempHeight = 0
params[:rate][:items].each do |item|
item = item[:product_id]
puts "Item details"
puts item
#productDimensions = Product.where(:product_id => item).first
#tempLength = #productDimensions.length
#tempWidth = #productDimensions.width
#tempHeight = #productDimensions.height
# tempLength = maximum length value
# tempWidth = maximum width value
# tempHeight = sum of all heights
end
I know I have to use sum(&:symbol), but I'm stuck. What's the best approach?

here probably can help
#tempLength = 0
#tempWidth = 0
#tempHeight = 0
params[:rate][:items].each do |item|
item = item[:product_id]
puts "Item details"
puts item
#productDimensions = Product.where(:product_id => item).first
#tempLength = #tempLength + #productDimensions.length
#tempWidth = #tempWidth + #productDimensions.width
if #productDimensions.height > #tempHeight
#tempHeight = #productDimensions.height
end
end
# #tempLength = total of all tempLength values
# #tempWidth = total of all tempHeight values
# #tempHeight = keep highest value

Maybe another suggestion that may result in better performance:
#tempLength = 0
#tempWidth = 0
#tempHeight = 0
product_ids = []
params[:rate][:items].each do |item|
product_ids << item[:product_id]
end
# Filter products according to collected product_ids
#products = Product.where(:product_id => product_ids)
# Let ActiveRecord do the work
#tempLength = #products.sum('length')
#tempWidth = #products.sum('width')
#tempHeight = #products.maximum('height')
# #tempLength = total of all tempLength values
# #tempWidth = total of all tempHeight values
# #tempHeight = keep highest value

Related

Lua Script: Convert Multiple "if" into simpler form

I am trying to make a condition where the percentage would be calculated based on the number of fan operated and the amount of airflow. This is what I come out with
function System01()
CFM_SHOP1 = addr_getword("#W_HDW1")
CFM_SHOP2 = addr_getword("#W_HDW2")
STATUS_SHOP1 = addr_getbit("#B_M1")
STATUS_SHOP2 = addr_getbit("#B_M2")
OUTPUT_SHOP1 = addr_getword("#W_HDW10")
OUTPUT_SHOP2 = addr_getword("#W_HDW11")
CFM_1 = CFM_SHOP1 + CFM_SHOP2
if STATUS_SHOP1 == 1 then
OUTPUT_SHOP1 = CFM_SHOP1 * 10000 / CFM_1
addr_setword("#W_HDW10", OUTPUT_SHOP1)
if STATUS_SHOP2 == 1 then
OUTPUT_SHOP2 = CFM_SHOP2 * 10000 / CFM_1
addr_setword("#W_HDW11", OUTPUT_SHOP2)
end
TOTAL_1 = OUTPUT_SHOP1 + OUTPUT_SHOP2
addr_setword("#W_HDW19", TOTAL_1)
end
if STATUS_SHOP2 == 1 then
OUTPUT_SHOP2 = CFM_SHOP2 * 10000 / CFM_1
addr_setword("#W_HDW11", OUTPUT_SHOP2)
if STATUS_SHOP1 == 1 then
OUTPUT_SHOP1 = CFM_SHOP1 * 10000 / CFM_1
addr_setword("#W_HDW10", OUTPUT_SHOP1)
end
TOTAL_1 = OUTPUT_SHOP1 + OUTPUT_SHOP2
addr_setword("#W_HDW19", TOTAL_1)
end
addr_setbit("#B_M1", STATUS_SHOP1)
addr_setbit("#B_M2", STATUS_SHOP2)
addr_setbit("#B_M3", STATUS_SHOP3)
end
Is there any way that I can simplified it? Please note that this is only two example I give. There is total of 9 fan so it will be really complicated if i just use 'if'. Thanks in advance
To simplify the code use for-loop
function System01()
local CFM_SHOP = {}
local CFM = 0
for j = 1, 9 do
CFM_SHOP[j] = addr_getword("#W_HDW"..tostring(j))
CFM = CFM + CFM_SHOP[j]
end
local STATUS_SHOP = {}
for j = 1, 9 do
STATUS_SHOP[j] = addr_getbit("#B_M"..tostring(j))
end
local OUTPUT_SHOP = {}
for j = 1, 9 do
OUTPUT_SHOP[j] = addr_getword("#W_HDW"..tostring(j+9))
end
local TOTAL = 0
for j = 1, 9 do
if STATUS_SHOP[j] == 1 then
OUTPUT_SHOP[j] = CFM_SHOP[j] * 10000 / CFM
addr_setword("#W_HDW"..tostring(j+9), OUTPUT_SHOP[j])
end
TOTAL = TOTAL + OUTPUT_SHOP[j]
end
addr_setword("#W_HDW19", TOTAL)
for j = 1, 9 do
addr_setbit("#B_M"..tostring(j), STATUS_SHOP[j])
end
end

Most optimized way to get value from table

I need to check if first value is >= 'from' and second value is <= 'to', if true then my function retun number. It's working but I don't know if this is the best and most optimized way to get value(number from table).
local table = {
{from = -1, to = 12483, number = 0},
{from = 12484, to = 31211, number = 1},
{from = 31212, to = 53057, number = 2},
{from = 53058, to = 90200, number = 3},
{from = 90201, to = 153341, number = 4},
{from = 153342, to = 443162, number = 5},
{from = 443163, to = 753380, number = 6},
{from = 753381, to = 1280747, number = 7},
{from = 1280748, to = 2689570, number = 8},
{from = 2689571, to = 6723927, number = 9},
{from = 6723928, to = 6723928, number = 10}
}
local exampleFromValue = 31244
local exampleToValue = 42057
local function getNumber()
local number = 0
for k, v in pairs(table) do
if (v.from and exampleFromValue >= v.from) and (v.to and exampleToValue <= v.to) then
number = v.number
break
end
end
return number
end
print(getNumber())
With this small amount of data, such function doesn't seem like a performace issue. However, you can compress the data a bit:
local t = {
12484, 31212, 53058, 90201, 153342, 443163, 753381, 1280748, 2689571, 6723928
}
local exampleFromValue = 31244
local exampleToValue = 42057
local function getNumber()
local last = -1
for i, v in ipairs(t) do
if exampleFromValue >= last and exampleToValue < v then
return i - 1
end
last = v
end
return 0
end

How to convert a binary number into integer in lua

i have a array of 1 and 0,
for example 10110
values = {1,0,1,1,0}
max = 0
for value = 6,1,-1 do
max = max + 2*index*value
end
but how could get the index of the array in order to calculate the max
Try this:
values = {1,0,1,1,0}
max = 0
for index = 1,#values,1 do
max = max + 2^(#values-index)*values[index]
end
print(max)

Rails add elements to array

I have a method to calculated the average for a given set of records:
input = params[:recommendation_ratings].values # The params are sent from radio_tags in my view.
input.each do |mini_params|
rating_id = mini_params[:rating_id]
l = Rating.find(rating_id) #Find record on Rating table.
l.rating #Get value associated with rating_id
total_rating = []
total_rating << l.rating
average = total_rating.inject{ |sum, el| sum + el }.to_f / total_rating.size
puts average
end
l.rating is not being appended to the total_rating array. The puts average is being printed as:
3.0
3.0
3.0
3.0
3.0
How do I append each of the ratings being returned to the array to calculated the average,and other math functions.
try:
total_rating = []
input.each do |mini_params|
rating_id = mini_params[:rating_id]
l = Rating.find(rating_id) #Find record on Rating table.
total_rating << l.rating
end
average = total_rating.sum / total_rating.size.to_f
this should solve your issue but there is a better way
total_rating = []
input.each do |mini_params|
rating_id = mini_params[:rating_id]
l = Rating.find(rating_id) #Find record on Rating table.
total_rating << l.rating
average = total_rating.inject(:+).to_f / total_rating.size
puts average
end
So given an array of ids, try the following
Rating.where(id: mini_params[:rating_id]).average(:rating)
UPDATE: fixing the one line version
rating_ids = input.map { |mini_params| mini_params[:rating_id] }
Rating.where(id: rating_ids).average(:rating)
You need to first get all of the ratings you are trying to average. I'm assuming you have already done this, and all of those ratings are stored in total_rating. Then you need to add the specific rating to that array:
rating_id = mini_params[:rating_id]
l = Rating.find(rating_id)
total_rating << l
average = total_rating.sum.to_f / total_rating.size
puts average

how to understand a table of tables in lua?

> polyline = {color = "blue", thickness = 2, npoints = 4, {x=0,y=0}, {x=-10,y=0}, {x=-10,y=1}, {x=0,y=1}}
> print(polyline[2])
table: 0x55ad5c0f3f90
> print(polyline[2].x)
-10
Why does print(polyline[2]) give out -10 ?
If you do not provide a key explicitly, table elements are assigned to numeric keys within the table constructor.
polyline = {color = "blue", thickness = 2, npoints = 4, {x=0,y=0}, {x=-10,y=0}, {x=-10,y=1}, {x=0,y=1}}
is equivalent to
do
polyline = {}
polyline.color = "blue"
polyline.thickness = 2
polyline.npoints = 4
do
polyline[1] = {}
polyline[1].x = 0
polyline[1].y = 0
end
do
polyline[2] = {}
polyline[2].x = -10
polyline[2].y = 0
end
do
polyline[3] = {}
polyline[3].x = -10
polyline[3].y = 1
end
do
polyline[4] = {}
polyline[4].x = 0
polyline[4].y = 1
end
end
Refer to
https://www.lua.org/manual/5.3/manual.html#3.4.9

Resources