Skip to content

Instantly share code, notes, and snippets.

@gphg
Last active May 14, 2025 04:55
Show Gist options
  • Save gphg/decf99978d2609ea20123e743ae58004 to your computer and use it in GitHub Desktop.
Save gphg/decf99978d2609ea20123e743ae58004 to your computer and use it in GitHub Desktop.
Generated from Google Gemini conversation.
--- Calculates the lowest and highest numerical indices and the count of numerical indices in a table.
--
-- Iterates over all keys using `pairs` and finds the minimum and maximum
-- among the numerical keys. Non-numerical keys are ignored.
-- Also counts the total number of numerical keys.
-- Returns 0, 0, 0 for tables with no numerical keys (including empty tables).
--
---@param tbl table The input table.
---@return number The lowest numerical index found, or 0 if none.
---@return number The highest numerical index found, or 0 if none.
---@return number The total count of numerical indices found, or 0 if none.
local function range(tbl)
local lowest_index = nil
local highest_index = nil
local count = 0 -- Initialize count of numerical indices
-- Localize frequently used functions for potential micro-optimization
local type = type
local math_min = math.min
local math_max = math.max
-- Iterate over all key-value pairs in the table
for key, _ in pairs(tbl) do
-- Check if the current key is a number
if type(key) == 'number' then
count = count + 1 -- Increment count for each numerical key
-- If this is the first numerical key found, initialize both min and max
if lowest_index == nil then
lowest_index = key
highest_index = key
else
-- Otherwise, update min and max with the current key
lowest_index = math_min(lowest_index, key)
highest_index = math_max(highest_index, key)
end
end
end
-- Return the found range and count, or 0, 0, 0 if no numerical keys were found
-- The count is already 0 if no numerical keys were found, so no 'or 0' is needed for count.
return lowest_index or 0, highest_index or 0, count
end
--- --- Test Cases ---
-- Using assert to verify the function's output automatically.
-- If an assert fails, the program will stop and report an error.
local testTable1 = { "foo", "bar", "test", notNumber = "this get ignored" } -- Indices 1, 2, 3
local testTable2 = { [-13] = "foo", "bar" } -- Indices -13, 1
local testTable3 = { [100] = "the value" } -- Index 100
local testTable4 = {} -- intentionally empty
local testTable5 = { ["a"] = 1, ["b"] = 2 } -- Only non-numerical keys
local testTable6 = { [5] = "five", [1] = "one", [3] = "three" } -- Indices 1, 3, 5
local testTable7 = { [-5] = "neg five", [-1] = "neg one", [-3] = "neg three" } -- Indices -5, -3, -1
local testTable8 = { [10] = "ten", [-20] = "neg twenty", [5] = "five" } -- Indices -20, 5, 10
local testTable9 = { [0] = "zero" } -- Index 0
local testTable10 = { [50] = "fifty" } -- Index 50
local testTable11 = { [1] = "one", [3] = "three", [5] = "five", [2] = nil, [4] = nil } -- Holey table with nil values
local low, high, count
-- Test Case 1: Sequential positive indices with non-numeric key
low, high, count = range(testTable1)
assert(low == 1 and high == 3 and count == 3, "Test Case 1 Failed: Expected 1, 3, 3 but got " .. low .. ", " .. high .. ", " .. count)
-- Test Case 2: Negative index and sequential positive index
low, high, count = range(testTable2)
assert(low == -13 and high == 1 and count == 2, "Test Case 2 Failed: Expected -13, 1, 2 but got " .. low .. ", " .. high .. ", " .. count)
-- Test Case 3: Single positive index
low, high, count = range(testTable3)
assert(low == 100 and high == 100 and count == 1, "Test Case 3 Failed: Expected 100, 100, 1 but got " .. low .. ", " .. high .. ", " .. count)
-- Test Case 4: Empty table
low, high, count = range(testTable4)
assert(low == 0 and high == 0 and count == 0, "Test Case 4 Failed: Expected 0, 0, 0 but got " .. low .. ", " .. high .. ", " .. count)
-- Test Case 5: Only non-numerical keys
low, high, count = range(testTable5)
assert(low == 0 and high == 0 and count == 0, "Test Case 5 Failed: Expected 0, 0, 0 but got " .. low .. ", " .. high .. ", " .. count)
-- Test Case 6: Non-contiguous positive indices
low, high, count = range(testTable6)
assert(low == 1 and high == 5 and count == 3, "Test Case 6 Failed: Expected 1, 5, 3 but got " .. low .. ", " .. high .. ", " .. count)
-- Test Case 7: Non-contiguous negative indices
low, high, count = range(testTable7)
assert(low == -5 and high == -1 and count == 3, "Test Case 7 Failed: Expected -5, -1, 3 but got " .. low .. ", " .. high .. ", " .. count)
-- Test Case 8: Mixed positive and negative indices
low, high, count = range(testTable8)
assert(low == -20 and high == 10 and count == 3, "Test Case 8 Failed: Expected -20, 10, 3 but got " .. low .. ", " .. high .. ", " .. count)
-- Test Case 9: Table with index 0
low, high, count = range(testTable9)
assert(low == 0 and high == 0 and count == 1, "Test Case 9 Failed: Expected 0, 0, 1 but got " .. low .. ", " .. high .. ", " .. count)
-- Test Case 10: Table with a single index
low, high, count = range(testTable10)
assert(low == 50 and high == 50 and count == 1, "Test Case 10 Failed: Expected 50, 50, 1 but got " .. low .. ", " .. high .. ", " .. count)
-- Test Case 11: Holey table with nil values
low, high, count = range(testTable11)
assert(low == 1 and high == 5 and count == 3, "Test Case 11 Failed: Expected 1, 5, 3 but got " .. low .. ", " .. high .. ", " .. count)
print("All test cases passed!") -- This line will only be reached if all asserts pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment