-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter5.lua
More file actions
110 lines (94 loc) · 2.29 KB
/
chapter5.lua
File metadata and controls
110 lines (94 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function P_5_1()
sunday = "monday"; monday = "sunday"
print("sunday val = ", sunday)
print("monday val = ", monday)
t = { sunday = "monday", [sunday] = monday }
print(t.sunday, t[sunday], t[t.sunday])
for k, v in pairs(t) do
print(k, v)
end
-- 预测:monday monday sunday
-- 为什么?因为 .x 等价于 ['x']
end
function P_5_3()
local str = "<![CDATA/[\n Hello world\n]]"
local a = {
["<![CDATA/[\\n Hello world\\n]]"] = str,
['\\n'] = "\n"
}
for k, v in pairs(a)
do
print(k, v)
end
end
function P_5_4(a, x)
local ret_val = 0
for k, v in ipairs(a) do
ret_val = ret_val + v * x ^ (k - 1)
end
return P_5_4
end
function P_5_5(a, x)
return P_5_4(a, x)
end
function isEffSeq(a)
for i = 1, #a do
if (a[i] == nil) then
return false
end
end
return true
end
function P_5_6()
a = { 1, nil, 2, 90, -1, nil }
print(isEffSeq(a), #a)
end
-- 该函数将指定列表的所有元素插入到另一个列表的指定位置 。
function insert_to_list(list, target_list, pos)
if pos > #target_list then
return {}
end
for i = 1, #list do
table.insert(target_list, pos + i, list[i])
end
return target_list
end
function P_5_7()
for i = 1, 4 do
local list = insert_to_list({ 1, 2, 3, 4 }, { 10, 20, 30 }, i)
print(table.concat(list, ", "))
end
end
function functionA(a)
local ans = table.concat(a)
end
function functionB(a)
local ans = ''
local sz = #a
for i = 1, sz do
ans = ans .. tostring(a[i])
end
-- print (ans)
end
function P_5_8()
-- 差得有点多
-- local million = 1000000/
local million = 10
local largeTable = {}
for i = 1, million do
largeTable[i] = i
end
-- 现在 largeTable 是一个从1到1,000,000的整数序列
local startTimeA = os.clock()
functionA(largeTable)
local endTimeA = os.clock()
local timeA = endTimeA - startTimeA
print("Function A took " .. timeA .. " seconds")
-- 测量函数B的执行时间
local startTimeB = os.clock()
functionB(largeTable)
local endTimeB = os.clock()
local timeB = endTimeB - startTimeB
print("Function B took " .. timeB .. " seconds")
end
P_5_8()