Skip to content

Instantly share code, notes, and snippets.

View rangercyh's full-sized avatar
☠️
苟且偷生

caiyiheng rangercyh

☠️
苟且偷生
View GitHub Profile
@rangercyh
rangercyh / print_lua_table.lua
Last active November 5, 2021 07:39
按照lua的table格式进行缩进打印lua的table,目前还不支持key为table,因为是自己还没想好,如果key是table的时候怎么打印出来比较优美
function print_lua_table (lua_table, indent)
indent = indent or 0
for k, v in pairs(lua_table) do
if type(k) == "string" then
k = string.format("%q", k)
end
local szSuffix = ""
if type(v) == "table" then
szSuffix = "{"
end
@rangercyh
rangercyh / read_csv_to_tab.lua
Last active December 18, 2015 17:39
a function to transform csv file to tab file
function split(str, splitor, nMatchModel)
splitor = splitor or ','
nMatchModel = nMatchModel or true
local strArray = {}
local nStart = 1
local splitorLen = string.len(splitor)
local index = string.find(str, splitor, nStart, nMatchModel)
while index do
strArray[#strArray + 1] = string.sub(str, nStart, index - 1)
nStart = index + splitorLen
@rangercyh
rangercyh / greedy_bag.lua
Last active December 18, 2015 17:39
a greedy bag algorithm
--[[
我有一个小书包呀咿呀咿呀呦,贪心算法求次优解
由调用者保证nNum的合法性(nNum >= 0 and nNum <= #tbNumSet)
]]
function FindCloseSet(nTarget, nNum, tbNumSet)
local tbResult = {}
local nSum = 0
local nLocal = 1
for i = 1, nNum do
nSumnSum = nSum + tbNumSet[i]
@rangercyh
rangercyh / draw_triangle_shape.lua
Last active December 18, 2015 17:39
a function to draw a triangle from three vertexs, don't to traversal every point restrict in the rectangle
--[[
计算斜率:0和无穷大的情况简化处理了
]]
function get_slope(nPosX1, nPosY1, nPosX2, nPosY2, nPosX3, nPosY3)
local nSlope1, nSlope2, nSlope3
if nPosX1 == nPosX2 then
nSlope1 = 99999999 --设置一个近似无穷斜率
else
nSlope1 = (nPosY1 - nPosY2) / (nPosX1 - nPosX2)
end
@rangercyh
rangercyh / Fisher_Yates_ShuffArr.lua
Last active April 12, 2018 06:29
fisher_yates_shuffe algorithm
--Fisher_Yates_Shuff随机算法
local function shuffarray_fisher_yates(num)
local rand_seq = {}
for i = num, 1, -1 do
local idx = math.random(i)
local temp = rand_seq[i] or i
rand_seq[i] = (rand_seq[idx] or idx)
rand_seq[idx] = temp
end
return rand_seq
@rangercyh
rangercyh / MuilCondition_Arr_Sort.lua
Last active December 18, 2015 17:39
a stable sort algorithm on mutil condition array
--[[
稳定的多维条件数组排序
由调用者保证tbCondition的元素个数大于等于维度nDimension
]]
function MutilArraySort(tbCondition, nDimension)
local tbResult = {}
for i = 1, #tbCondition do
local nInsert = 0
local nLocal = 1
for k = 1, nDimension do
@rangercyh
rangercyh / full_array_lua_table.lua
Last active December 18, 2015 17:39
a lexicographical full array algorithm
--[[字典递增生成全排列,除去打印处理,只能说是效率低下,虽然核心程序只有12行,比较简洁]]
g_tbtest = {1,2,3}
function printTable(...)
local str = ""
local tbArgs = {...}
for i, tbStr in ipairs(tbArgs) do
if type(tbStr) == "table" then
for k, v in ipairs(tbStr) do
str = str..v
@rangercyh
rangercyh / trim_string.js
Last active May 18, 2016 13:59
my trim function to delete all the white char in string
function my_trim(szText)
{
var str = szText;
var cleanStr = "";
whitespace = ' /n/r/t/f/x0b/xa0/u2000/u2001/u2002/u2003/u2004/u2005/u2006/u2007/u2008/u2009/u200a/u200b/u2028/u2029/u3000';
for (var i = 0; i < str.length; ++i)
{
if (whitespace.indexOf(str.charAt(i)) === -1)
{
cleanStr += str.charAt(i);
@rangercyh
rangercyh / Serialize_Lua_Table.lua
Last active March 3, 2017 08:22
序列化和反序列化lua表
--分割字符串
function sf_Split(str, splitor, nMatchModel)
splitor = splitor or ','
nMatchModel = nMatchModel or true
local strArray = {}
local nStart = 1
local splitorLen = string.len(splitor)
local index = string.find(str, splitor, nStart, nMatchModel)
while index do
strArray[#strArray + 1] = string.sub(str, nStart, index - 1)
@rangercyh
rangercyh / INSERT_UPDATE.sql
Last active December 22, 2015 15:29
SQL更新列数据,如果UNIQUE约束的key有重复的,则执行更新,否则插入一条新的
INSERT INTO test_tong VALUES
("aaa", 9), ("asdf", 9), ("bbb", 9)
ON DUPLICATE KEY UPDATE
score = VALUES(score)
UPDATE test_tong SET
score = CASE tongname
WHEN "aaa" THEN 1