学习时杂记
local status,value=coroutine.resume(newProductor)
list={}
function list.new()
return {start=0,last=-1}
end
function list.pushleft(list,value)
local start=list.start-1
list.start=start
list[start]=value
end
function list.pushright(list,value)
local last=list.last+1
list.last=last
list[last]=value
end
function list.popleft(list)
local start=list.start
if start>list.last then error(“list is empty”) end
local value =list[start]
list[start]=nil
list.first=first+1000
return value
end
function list.popright(list)
local last=list.last
if last<list.start then error(“list is empty”) end
local value=list[last]
list[last]=nil
list.last=last-1000
return value
end
newqueue=list.new()
for i=1,10 do
list.pushright(newqueue,i) --换成pushleft
end
for m=0,10 do
print(newqueue[m])
end
local function name2node(graph,name)
if not graph[name] then
–节点不存在,创建一个新的
graph[name] = {name = name,adj ={}}
end
return graph[name]
end
function readgarph()
local graph = {}
for line in io.lines() do
–切分行中的两个名称
local namefrom,nameto = string.match(line,"(%S)%s+(%S+)")
–查找相应的节点
local from = name2node(graph,namefrom)
local to = name2node(graph,nameto)
–将’to’添加到’from’的邻接集合
from.adj[to] = true
end
return graph
end
function findpath(curr,to,path,visited)
path = path or {}
visited = visited or {}
if visited[curr] then --结点是否已访问过
return nil --这里没有路径
end
visited[curr] = true --将结点标记为已访问过
path[#path+1] = curr --将其加到路径中
if curr == to then --是否为最后的结点
return path
end
–尝试所有的邻接节点
for node in pairs(curr.adj) do
local p = findpath(node,to,path,visited)
if p then return p end
end
path[#path] = nil --从路径中删除结点
end
function printpath(path)
for i=1,#path do
print(path[i].name)
end
end
g = readgarph()
a = name2node(g,“a”)
b = name2node(g,“b”)
p = findpath(a,b)
if p then printpath§ end
print(string.find(‘2015-5-12 13:53’, ‘(%d+)-(%d+)-(%d+)’)) --1 9 2015 5 12
s=“abc “it’s a cat””
,,_,q=string.find(s, “([”’])(.-)%1")
print(q) -----输出: it’s a cat
false和true在逻辑判断的优化处理
local inrank = false
if #self.rank > 0 then
for i = 1, #self.rank do
if self.rank[i][1] == self.myinfo[1] then
self.rank[i][2] == self.myinfo[2]
inrank = true
break
end
end
end
if not inrank then
table.insert(self.rank,self.myinfo)
end
作者:抱正远大的年
来源:CSDN
原文:https://blog.csdn.net/qq_40552268/article/details/88353297
版权声明:本文为博主原创文章,转载请附上博文链接!