从切割表LUA 5.1.5查找最小值

发布于 2025-01-18 00:05:00 字数 1646 浏览 0 评论 0原文

我有一个 Lua 脚本,可以将表格转换为段:

function tablecut(t, n)
    local result = {}
    local j = 0
    for i = 1, #t do
        if (i-1) % n == 0 then
            j = j + 1
            result[j] = {}
        end
        result[j][#result[j]+1] = t[i]
    end
    return result
end

output = tablecut({'15', '62', '14', '91', '33', '55', '29', '4'}, 4)
for i = 1, #output do
    for j = 1, #output[i] do
        io.write(tostring(output[i][j])..'  ')
    end
    print()
end

输出:

15  62  14  91  
33  55  29  4

我试图从切割列表中找到最小值,因此输出将如下所示:

15  62  14  91  
min = 14
33  55  29  4
min = 4

编辑:如果它有任何重要性,这就是我让它工作的方式Lua 5.3 但 Lua 5.1 上没有 table.move 函数。我不记得当我写这段代码时我的思维功能是如何工作的。

function indexOf(array, value)
  for i, v in ipairs(array) do
      if v == value then
          return i
      end
  end
  return nil
end


Indicies = {}
Answers = {}

function chunks(lst, size)
  local i = 1
  local count = 0
  return function()
    if i > #lst then return end
    local chunk = table.move(lst, i, i + size -1, 1, {})
    i = i + size
    count = count + 1
    return count, chunk
  end
end

local a = {91,52,19,59,38,29,58,11,717,91,456,49,30,62,43,8,17,15,26,22,13,10,2,23} --Test list
for i, chunk in chunks(a, 4) do
    x=math.min(a)
    print(string.format("#%d: %s", i, table.concat(chunk, ",")))
    table.sort(chunk)
    print(math.min(chunk[1]))
    table.insert(Answers, chunk[1])
    table.insert(Indicies, (indexOf(a, chunk[1])))

输出:

#1: 91,52,19,59
19
#2: 38,29,58,11
11
#3: 717,91,456,49
49

I have a Lua script that turns a table into segments:

function tablecut(t, n)
    local result = {}
    local j = 0
    for i = 1, #t do
        if (i-1) % n == 0 then
            j = j + 1
            result[j] = {}
        end
        result[j][#result[j]+1] = t[i]
    end
    return result
end

output = tablecut({'15', '62', '14', '91', '33', '55', '29', '4'}, 4)
for i = 1, #output do
    for j = 1, #output[i] do
        io.write(tostring(output[i][j])..'  ')
    end
    print()
end

output:

15  62  14  91  
33  55  29  4

And I am trying to find the minima from the cut lists so the output would look like this:

15  62  14  91  
min = 14
33  55  29  4
min = 4

Edit: If its of any importance this is how I got it to work on Lua 5.3 but there is no table.move function on Lua 5.1. I can't remember how my thought function worked when I wrote this code.

function indexOf(array, value)
  for i, v in ipairs(array) do
      if v == value then
          return i
      end
  end
  return nil
end


Indicies = {}
Answers = {}

function chunks(lst, size)
  local i = 1
  local count = 0
  return function()
    if i > #lst then return end
    local chunk = table.move(lst, i, i + size -1, 1, {})
    i = i + size
    count = count + 1
    return count, chunk
  end
end

local a = {91,52,19,59,38,29,58,11,717,91,456,49,30,62,43,8,17,15,26,22,13,10,2,23} --Test list
for i, chunk in chunks(a, 4) do
    x=math.min(a)
    print(string.format("#%d: %s", i, table.concat(chunk, ",")))
    table.sort(chunk)
    print(math.min(chunk[1]))
    table.insert(Answers, chunk[1])
    table.insert(Indicies, (indexOf(a, chunk[1])))

Output:

#1: 91,52,19,59
19
#2: 38,29,58,11
11
#3: 717,91,456,49
49

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2025-01-25 00:05:00

您的表剪切函数可以简化,并且如果您想像在 5.3 脚本中那样获得输出,则 for 循环的输出需要使用迭代器。

function cuttable(t,n)
  local binned = {}
  
  for i=1,#t,n do
      local bin = {}
        for j=1,n do
            table.insert(bin, t[i + ((j - 1) % n)])
        end
      table.insert(binned, bin)
  end
  
  return binned
end

对于 for 循环,我们可以在 cuttable 的输出上使用 ipairs ,让事情变得非常简单,然后我们只需执行相同的 concat 步骤,然后排序并打印结果。

for k, bin in ipairs(cuttable(a,4)) do
    local output = "#" .. k .. ":" .. table.concat(bin, ",")
    table.sort(bin)
    print(output)
    print(bin[1])
end

输出

#1:91,52,19,59
19
#2:38,29,58,11
11
#3:717,91,456,49
49
#4:30,62,43,8
8
#5:17,15,26,22
15
#6:13,10,2,23
2

your table cut function could be simplified, and your output for loop needs you use an iterator if you want to get an output simply like you do in your 5.3 script.

function cuttable(t,n)
  local binned = {}
  
  for i=1,#t,n do
      local bin = {}
        for j=1,n do
            table.insert(bin, t[i + ((j - 1) % n)])
        end
      table.insert(binned, bin)
  end
  
  return binned
end

For the for loop, we can use ipairs on the output of cuttable keeping things pretty simple, then we just do the same steps of concat then sort and print out our results.

for k, bin in ipairs(cuttable(a,4)) do
    local output = "#" .. k .. ":" .. table.concat(bin, ",")
    table.sort(bin)
    print(output)
    print(bin[1])
end

Output

#1:91,52,19,59
19
#2:38,29,58,11
11
#3:717,91,456,49
49
#4:30,62,43,8
8
#5:17,15,26,22
15
#6:13,10,2,23
2
日久见人心 2025-01-25 00:05:00

实现切割的一种方法是使用 for 循环和 for 循环。 解压。我已经处理了 for 循环后长度不能被 4 整除的情况,以 (1) 最大化性能(不需要每次迭代都进行检查)和 (2) 能够直接将值传递给 math.min,它不接受 nil

for i = 1, math.floor(#t / 4), 4 do
  print(unpack(t, i, i+4))
  print("min = " .. math.min(unpack(t, i, i+4)))
end
-- If #t is not divisible by 4, deal with the remaining elements
local remaining = #t % 4
if remaining > 0 then
  print(unpack(t, #t - remaining, remaining))
  print("min = " .. math.min(unpack(t, #t - remaining, remaining)))
end

One way to implement the cutting would be using a for loop & unpack. I have handled the case of the length not being divisible by 4 after the for loop to (1) maximize performance (check doesn't need to be done every iteration) and (2) be able to directly pass the values to math.min, which doesn't accept nils.

for i = 1, math.floor(#t / 4), 4 do
  print(unpack(t, i, i+4))
  print("min = " .. math.min(unpack(t, i, i+4)))
end
-- If #t is not divisible by 4, deal with the remaining elements
local remaining = #t % 4
if remaining > 0 then
  print(unpack(t, #t - remaining, remaining))
  print("min = " .. math.min(unpack(t, #t - remaining, remaining)))
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文