由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - set operation in lua
相关主题
C++ key/value 排序a c++ question
[合集] 和大家再讨论一道面试算法题(MS) (转载)how to change a variable's value in a const function
请问关于hash table的大小设定问题。 (转载)请问在class member function中如何调用overloaded function ca
[C++] 入门级问题 increment and decrement operatorsoverload "++i"里的operator“++”,怎么declare?
Scala的operator似乎不太好读C++重载<<错误?
问个基本的design问题friend function 不能virtual 怎么搞呢?
C++ operator = overloading用copy & swap有啥优点请问c++中操作符可以声明为虚函数吗?
why use static function here?operator overloading (C++)
相关话题的讨论汇总
话题: res话题: set话题: end话题: pairs话题: do
进入Programming版参与讨论
1 (共1页)
a*****g
发帖数: 19398
1
Set = {}
-- create a new set with the values of the given list
function Set.new (l)
local set = {}
for _, v in ipairs(l) do set[v] = true end
return set
end
function Set.union (a, b)
local res = Set.new{}
for k in pairs(a) do res[k] = true end
for k in pairs(b) do res[k] = true end
return res
end
function Set.intersection (a, b)
local res = Set.new{}
for k in pairs(a) do
res[k] = b[k]
end
return res
end
a*****g
发帖数: 19398
2
比起 c语言来是简单很多啊

【在 a*****g 的大作中提到】
: Set = {}
: -- create a new set with the values of the given list
: function Set.new (l)
: local set = {}
: for _, v in ipairs(l) do set[v] = true end
: return set
: end
: function Set.union (a, b)
: local res = Set.new{}
: for k in pairs(a) do res[k] = true end

h**********c
发帖数: 4120
3
c 三十年以前就有hashset/hashtable
好象最近几年很多 comeidan改刷题了吧,
在好几个地方见过研究生毕业怎么算两个set intersection不会.
你这个union, java Set::addall 就解决了。

【在 a*****g 的大作中提到】
: 比起 c语言来是简单很多啊
a*****g
发帖数: 19398
4
我主要是考虑拿基本语言去实现的思路,
还不是考虑有没有现成的可调用的函数

【在 h**********c 的大作中提到】
: c 三十年以前就有hashset/hashtable
: 好象最近几年很多 comeidan改刷题了吧,
: 在好几个地方见过研究生毕业怎么算两个set intersection不会.
: 你这个union, java Set::addall 就解决了。

h**********c
发帖数: 4120
5
这种基础库,都是开发团队反复review过的并在生产中大量应用,做个CS201练习还可
以。

【在 a*****g 的大作中提到】
: 我主要是考虑拿基本语言去实现的思路,
: 还不是考虑有没有现成的可调用的函数

1 (共1页)
进入Programming版参与讨论
相关主题
operator overloading (C++)Scala的operator似乎不太好读
what is this ^ operator used in the function call问个基本的design问题
It is better to have 100 functions operate on one data struC++ operator = overloading用copy & swap有啥优点
一个关于methodology的问题why use static function here?
C++ key/value 排序a c++ question
[合集] 和大家再讨论一道面试算法题(MS) (转载)how to change a variable's value in a const function
请问关于hash table的大小设定问题。 (转载)请问在class member function中如何调用overloaded function ca
[C++] 入门级问题 increment and decrement operatorsoverload "++i"里的operator“++”,怎么declare?
相关话题的讨论汇总
话题: res话题: set话题: end话题: pairs话题: do