Skip to content

Commit

Permalink
peek-iterator updated
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyanxing committed Feb 17, 2015
1 parent d4894b3 commit 88ea88e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions app/problems/peek-iterator.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
],
"code_cpp": "/*\nclass Iterator {\npublic:\n int get_next();\n bool has_next();\n};\n*/\n\nclass PeekIterator {\npublic:\n PeekIterator(Iterator& it) {\n }\n \n int peek() {\n }\n \n bool has_next() {\n }\n \n int get_next() {\n }\n};",
"code_java": "public class PeekIterator {\n public PeekIterator(Iterator it) {\n }\n \n public int peek() {\n }\n \n public boolean has_next() {\n }\n \n public int get_next() {\n }\n}",
"code_ruby": "# class Iterator\n# def get_next()\n# ...\n# end \n#\n# def has_next()\n# ...\n# end\n# end\n\nclass PeekIterator\n # @param it: Iterator object\n def initialize(it)\n end\n \n # @return integer\n def peek()\n end\n \n # @return boolean\n def has_next()\n end\n \n # @return integer\n def get_next()\n end\nend",
"code_python": "# class Iterator:\n# def get_next(self):\n# ...\n#\n# def has_next(self):\n# ...\n\nclass PeekIterator:\n # @param it: Iterator object\n def __init__(self, it):\n \n # @return integer\n def peek(self):\n \n # @return boolean\n def has_next(self):\n \n # @return integer\n def get_next(self):",
"code_lua": "-- Definition for an iterator\n--\n-- Iterator = class()\n-- \n-- function Iterator:has_next()\n-- \t...\n-- end\n-- \n-- function Iterator:get_next()\n-- \t...\n-- end\n\n-- Declaration of PeekIterator class\nPeekIterator = class()\n\n-- Constructor method\n-- @param it: iterator object\nfunction PeekIterator:ctor(it)\n\tself.it = it\nend\n\n-- @return integer\nfunction PeekIterator:peek()\nend\n\n-- @return boolean\nfunction PeekIterator:has_next()\nend\n\n-- @return integer\nfunction PeekIterator:get_next()\nend\n",
"code_ruby": "# class Iterator\n# def get_next()\n# end \n#\n# def has_next()\n# end\n# end\n\nclass PeekIterator\n # @param it: Iterator object\n def initialize(it)\n end\n \n # @return integer\n def peek()\n end\n \n # @return boolean\n def has_next()\n end\n \n # @return integer\n def get_next()\n end\nend",
"code_python": "# class Iterator:\n# def get_next(self):\n#\n# def has_next(self):\n\nclass PeekIterator:\n # @param it: Iterator object\n def __init__(self, it):\n \n # @return integer\n def peek(self):\n \n # @return boolean\n def has_next(self):\n \n # @return integer\n def get_next(self):",
"code_lua": "-- Definition for an iterator\n--\n-- Iterator = class()\n-- \n-- function Iterator:has_next()\n-- end\n-- \n-- function Iterator:get_next()\n-- end\n\n-- Declaration of PeekIterator class\nPeekIterator = class()\n\n-- Constructor method\n-- @param it: iterator object\nfunction PeekIterator: ctor(it)\n\tself.it = it\nend\n\n-- @return integer\nfunction PeekIterator: peek()\nend\n\n-- @return boolean\nfunction PeekIterator: has_next()\nend\n\n-- @return integer\nfunction PeekIterator: get_next()\nend\n",
"in_type_cpp": [
"vector<int>"
],
Expand Down
17 changes: 9 additions & 8 deletions judge/lua/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
local _class={}

function class(super)
local class_type={}
class_type.ctor=false
class_type.super=super
class_type.new=function(...)
local obj={}
local class_type = {}
class_type.ctor = false
class_type.super = super
class_type.new = function(...)
local obj = {}
do
local create
create = function(c,...)
Expand All @@ -22,20 +22,20 @@ function class(super)

create(class_type,...)
end
setmetatable(obj,{ __index=_class[class_type] })
setmetatable(obj,{ __index = _class[class_type] })
return obj
end
local vtbl={}
_class[class_type]=vtbl

setmetatable(class_type,{__newindex=
setmetatable(class_type,{__newindex =
function(t,k,v)
vtbl[k]=v
end
})

if super then
setmetatable(vtbl,{__index=
setmetatable(vtbl,{__index =
function(t,k)
local ret=_class[super][k]
vtbl[k]=ret
Expand Down Expand Up @@ -77,6 +77,7 @@ TreeNode = {
end
}

------------------------------------------------------------------------
-- Definition for an iterator
Iterator = class()

Expand Down
8 changes: 4 additions & 4 deletions judge/lua/solution.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@

PeekIterator = class()

function PeekIterator:ctor(it)
function PeekIterator: ctor(it)
self.it = it
self.peeks = nil
end

function PeekIterator:peek()
function PeekIterator: peek()
if self.peeks == nil then
self.peeks = self.it:get_next()
end
return self.peeks
end

function PeekIterator:has_next()
function PeekIterator: has_next()
return self.it:has_next() or self.peeks
end

function PeekIterator:get_next()
function PeekIterator: get_next()
if self.peeks == nil then
return self.it:get_next()
else
Expand Down

0 comments on commit 88ea88e

Please sign in to comment.