7 kyu: Pull your words together, man!
這題很簡單,但是我在寫code時,
想存取table資料結構的words裡面的最後一個元素,
發現不能用words[-1] (words[-1] 為 nil)
這點跟Python 3的list不同,
可行的替代寫法是
words[#words]
我寫的完整code如下:
return {
sentencify = function(words)
words[#words] = words[#words] .. "."
words[1] = string.upper(string.sub(words[1], 1, 1)) .. string.sub(words[1], 2)
return table.concat(words, " ")
end
}
別人寫的比較精簡,如下:
return {
sentencify = function(words)
return table.concat(words, ' '):gsub('^.', string.upper)..'.'
end
}
沒有留言:
張貼留言