7 kyu: Sliding windows
題目很難懂是在做什麼?
尤期是length等於0到底是什麼?
因此嘗試了有點多次,
最後是length = 0的情況,
當做是list table多了一個0的值在一開始,
來解決。
另外就是有關Lua的table,
用insert加入元素,如下code:
> a = {}
> a:insert(123)
stdin:1: attempt to call a nil value (method 'insert')
stack traceback:
stdin:1: in main chunk
[C]: in ?
> table.insert(a, 123)
不能用a:insert(123),
查ChatGPT是說:
預設的
table.insert
是屬於全域的table
模組,不是附在每個表(table)上的方法。
也就是說,除非你自己把 insert
方法掛到 a
表上,否則 a:insert(123)
會報錯。
要用table.insert(a, 123)
或者
✅ 正確寫法示範:
執行後:
我Pass的code如下:
local function window(length, offset, list)
local a, temp, l = {}, {}, {}
if 0 == length then
for ii = 0, #list do
if 0 == ii then
l[1] = 0
else
l[ii + 1] = list[ii]
end
end
else
l = list
end
for i = 1, #l, offset do
if i + length - 1 <= #l then
for j = i, i + length - 1 do
table.insert(temp, l[j])
end
table.insert(a, temp)
temp = {}
end
end
return a
end
return window
沒有留言:
張貼留言