Sorting 在Lua中排序,计算项目数

Sorting 在Lua中排序,计算项目数,sorting,lua,Sorting,Lua,两个简短的问题(我希望…)和下面的代码。下面的脚本检查一个数字是否为素数,如果不是,则返回该数字的所有因子,否则只返回该数字为素数。不要注意zs。脚本中的内容,因为这是特定于客户端的,与脚本功能无关 除了两个小细节之外,脚本本身的工作几乎非常出色——第一个是因子列表,它不会返回自己的排序结果。。。也就是说,对于24,它将返回1、2、12、3、8、4、6和24,而不是1、2、3、4、6、8、12和24。我无法将其打印为表,因此需要将其作为列表返回。如果必须先将其排序为一个表,然后将其转换为一个列表

两个简短的问题(我希望…)和下面的代码。下面的脚本检查一个数字是否为素数,如果不是,则返回该数字的所有因子,否则只返回该数字为素数。不要注意zs。脚本中的内容,因为这是特定于客户端的,与脚本功能无关

除了两个小细节之外,脚本本身的工作几乎非常出色——第一个是因子列表,它不会返回自己的排序结果。。。也就是说,对于24,它将返回1、2、12、3、8、4、6和24,而不是1、2、3、4、6、8、12和24。我无法将其打印为表,因此需要将其作为列表返回。如果必须先将其排序为一个表,然后将其转换为一个列表,我可以处理这个问题。重要的是最终的结果是名单

另一个细节是,我需要检查列表中是否只有两个或更多数字。如果只有两个数字,它就是一个素数(1和数字)。我现在的方式不起作用。有没有办法做到这一点?我感谢所有的帮助

function get_all_factors(number)
  local factors = 1
  for possible_factor=2, math.sqrt(number), 1 do
    local remainder = number%possible_factor

    if remainder == 0 then
      local factor, factor_pair = possible_factor, number/possible_factor
      factors = factors .. ", " .. factor
      if factor ~= factor_pair then
        factors = factors .. ", " ..  factor_pair
      end
    end
  end

  factors = factors .. ", and " .. number
  return factors
end

local allfactors = get_all_factors(zs.param(1))
if zs.func.numitems(allfactors)==2 then
  return zs.param(1) .. " is prime."
else
  return zs.param(1) .. " is not prime, and its factors are: " .. allfactors
end

如果我正确理解了你的问题,我建议把你的逻辑分开一点。想法是首先创建一个包含分数的表,然后进行排序,然后创建字符串表示

-- Creates a table containing all the factors for a number. function createFactors(n) local factors = {} -- The for loop etc. would be here. If you find a factor then add -- it in the table. -- ... table.insert(factors, n) -- ... --# Once you've found all the factors just return the table. return factors end -- Lua offers a method for sorting tables called table.sort. local factors = createFactors(139) table.sort(factors) -- There is a method for creating a string representation of a table -- called table.concat, the first parameter is the table and the second -- is the character that is used to delimit the values. table.concat(factors, ", ") --创建一个包含数字的所有因子的表。 函数createFactors(n) 局部因子={} --for循环等将在这里。如果你找到一个因素,然后加上 --它在桌子上。 -- ... 表.插入(系数,n) -- ... --#一旦你找到了所有的因素,就返回表格。 回报率 结束 --Lua提供了一种对表进行排序的方法,称为table.sort。 局部系数=createFactors(139) 表2.排序(系数) --有一种方法可以创建表的字符串表示形式 --第一个参数是table,第二个参数是table.concat --用于分隔值的字符。 表.混凝土(系数,“,”)
如果我正确理解了你的问题,我建议把你的逻辑分开一点。想法是首先创建一个包含分数的表,然后进行排序,然后创建字符串表示

-- Creates a table containing all the factors for a number. function createFactors(n) local factors = {} -- The for loop etc. would be here. If you find a factor then add -- it in the table. -- ... table.insert(factors, n) -- ... --# Once you've found all the factors just return the table. return factors end -- Lua offers a method for sorting tables called table.sort. local factors = createFactors(139) table.sort(factors) -- There is a method for creating a string representation of a table -- called table.concat, the first parameter is the table and the second -- is the character that is used to delimit the values. table.concat(factors, ", ") --创建一个包含数字的所有因子的表。 函数createFactors(n) 局部因子={} --for循环等将在这里。如果你找到一个因素,然后加上 --它在桌子上。 -- ... 表.插入(系数,n) -- ... --#一旦你找到了所有的因素,就返回表格。 回报率 结束 --Lua提供了一种对表进行排序的方法,称为table.sort。 局部系数=createFactors(139) 表2.排序(系数) --有一种方法可以创建表的字符串表示形式 --第一个参数是table,第二个参数是table.concat --用于分隔值的字符。 表.混凝土(系数,“,”)
来自庞佐的安瑟尔很好。为了对结果进行最后的润色,下面是一个通用例程,用于将Lua中的字符串列表转换为英文字符串,并使用“and”表示该列表:

function string.commafy(t, andword)
  andword = andword or 'and'
  local n = #t
  if n == 1 then
    return t[1]
  elseif n == 2 then
    return table.concat { t[1], ' ', andword, ' ', t[2] }
  else
    local last = t[n]
    t[n] = andword .. ' ' .. t[n]
    local answer = table.concat(t, ', ')
    t[n] = last
    return answer
  end
end

不要使用
table.concat(factors,,)
而使用
string.commafy(factors)

来自ponzao的Nice ansewr。为了对结果进行最后的润色,下面是一个通用例程,用于将Lua中的字符串列表转换为英文字符串,并使用“and”表示该列表:

function string.commafy(t, andword)
  andword = andword or 'and'
  local n = #t
  if n == 1 then
    return t[1]
  elseif n == 2 then
    return table.concat { t[1], ' ', andword, ' ', t[2] }
  else
    local last = t[n]
    t[n] = andword .. ' ' .. t[n]
    local answer = table.concat(t, ', ')
    t[n] = last
    return answer
  end
end

不要使用
table.concat(factors,,)
而使用
string.commafy(factors)

不知道table.concat。谢谢数一数物品的数量怎么样?没问题。堆栈溢出似乎不喜欢带有注释的Lua代码:(我发现使用for Lua代码更清晰,因为代码着色问题。内置代码着色显然采用了类似C的语言,Lua的颜色都错了。我不知道table.concat。谢谢!计算项目数怎么样?没问题。堆栈溢出似乎不喜欢带有注释的Lua代码:(我发现使用Lua代码更清晰,因为代码着色问题。内置代码着色显然采用了类似C的语言,Lua的颜色都错了。太棒了。这也解决了我想要的一件事!:D谢谢!太棒了。这也解决了我想要的一件事!:D谢谢!