我正在 ConTeXt 中使用条目创建随机测验。我需要仅显示前 10 个条目,并丢弃其余条目,从不显示它们。
\starttext
\startitemize[n]
\item This is the first question.
\item This is the second question.
\item ...
\stopitemize
\stopttext
我尝试使用\doboundtext
但\limitatetext
它们似乎不是为这种特定形式的裁剪而设计的。
有没有办法让 ConTeXt 仅显示项目中的前 n 个项目?
答案1
类似的事情在 Lua 中很容易实现。下面的代码不仅打印列表中的前五个项目,而且还打乱了项目的顺序,使问题的顺序随机化。
\starttext
\startluacode
-- https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
local function shuffle(list)
for i = #list,2,-1 do
local j = math.random(i)
list[i], list[j] = list[j], list[i]
end
end
local items = {
"This is the first question",
"This is the second question",
"This is the third question",
"This is the fourth question",
"This is the fifth question",
"This is the sixth question",
"This is the seventh question",
"This is the eighth question",
"This is the nineth question",
"This is the tenth question",
}
shuffle(items)
context.startitemize{ "n", "packed" }
for i = 1, 5 do
context.item(items[i])
end
context.stopitemize()
\stopluacode
\stoptext