Finding the minimum value... (not math.min)
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Finding the minimum value... (not math.min)
bartbes's is better.
Help us help you: attach a .love.
Re: Finding the minimum value... (not math.min)
Or, again:
I LOVE programming because it's almost impossible to have only one solution to a specific problem!
Code: Select all
function get_min(t)
min = math.min(unpack(t))
for i, v in ipairs(t) do
if v == min then
return v, i
end
end
end
Re: Finding the minimum value... (not math.min)
Code: Select all
function min_index(first, ...)
return math.min(first, ...) == first and 1 or min_index(...) + 1
end
function test(...)
print(min_index(...))
end
test(1, 2, 3, 4) -- prints 1
test(4, 3, 1, 5, 6, 7) -- prints 3
Who is online
Users browsing this forum: No registered users and 2 guests