2025年10月1日 星期三

7 kyu:Set the Alarms Up 別人簡潔的寫法

 嘗試練習7 kyu: Set the Alarms Up

使用Lua語言,題目是設定連續鬧鐘,如果不是寫成程式,
就數學上,大概只會用到小學數學。

我自己的寫法是:

local function set_the_alarms_up(time, n)
  local t = os.time{year=1970, month=1, day=1, hour=0}
  local hours = string.sub(time, 1, 2)
  local hour = tonumber(hours)
  local ms = string.sub(time, 4, 5)
  local m = tonumber(ms)
  t = t + m*60 + hour*3600
  local a = {os.date("%H:%M", t)}
  for i = 2, n do
    a[i] = os.date("%H:%M", t + 300*(i-1))
  end
  return a
end

return set_the_alarms_up

Note: os.time設定值,是以秒為單位

但是看到別人的寫法:

local function set_the_alarms_up(time, n)
  local res, h, m = {}, time:match "^(%d+):(%d+)$"
  for i = 1, n do
    res[i] = os.date("%H:%M", (h * 60 + m + 5 * (i - 1)) * 60)
  end
  return res
end

return set_the_alarms_up

其中local res, h, m = {}, time:match "^(%d+):(%d+)$"

這行我覺得寫得很精簡,

GPT 5解釋如下:

這一行 Lua 寫法:

local res, h, m = {}, time:match "^(%d+):(%d+)$"

可以分成兩部分來看:


1. 左邊變數定義

local res, h, m = ...
  • local:表示這些變數是區域變數 (scope 只在當前程式塊內)

  • res:被初始化為 {},也就是一個新的空 table。

  • h, m:會由右邊 time:match(...) 的結果來賦值。


2. 右邊字串模式匹配

time:match "^(%d+):(%d+)$"

這是 Lua 的 字串方法,等同於:

string.match(time, "^(%d+):(%d+)$")
  • ^:匹配字串開頭

  • %d+:匹配一個或多個數字

  • ::匹配冒號字元本身

  • $:匹配字串結尾

所以這個 pattern 會匹配 「數字:數字」 這種格式,例如 "12:34"

由於有兩個括號捕獲組 (%d+)string.match 會返回兩個結果:

  • 第一個捕獲 → h

  • 第二個捕獲 → m


3. 綜合起來

如果:

time = "12:34"

那麼執行後:

res = {} -- 空表 h = "12" -- 字串 "12" m = "34" -- 字串 "34"

👉 簡單說:
這行程式碼同時做了兩件事:

  1. 建立一個空表 res

  2. 從字串 time 中解析出「小時」和「分鐘」(字串型態),分別放到 hm 裡。


另外為何time:match "^(%d+):(%d+)$"的函式不用括號?

GPT 5 解釋如下:

這個是 Lua 的語法糖 (syntactic sugar) 👍

在 Lua 裡,如果一個函式呼叫 只有一個參數,而這個參數是:

  • 一個 字串常值

  • 或是一個 table 常值

那麼可以省略小括號 ()


範例:

print("hello") -- 一般寫法 print "hello" -- 語法糖,省略括號 f({x=1, y=2}) -- 一般寫法 f {x=1, y=2} -- 語法糖,省略括號

套用到你的程式

time:match("^(%d+):(%d+)$") -- 正常呼叫 time:match "^(%d+):(%d+)$" -- 語法糖,省略括號

這兩行 完全一樣,只是簡潔寫法。


👉 所以你看到的 time:match "^(%d+):(%d+)$" 只是 Lua 的簡化呼叫方式,函式其實還是有接到參數的。


沒有留言:

張貼留言

7 kyu:Set the Alarms Up 別人簡潔的寫法

 嘗試練習7 kyu:  Set the Alarms Up 使用Lua語言,題目是設定連續鬧鐘,如果不是寫成程式, 就數學上,大概只會用到小學數學。 我自己的寫法是: local function set_the_alarms_up ( time , n ) local...