mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-07 09:58:03 +00:00
Added stack method is_empty().
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Implement a stack with push, pop, and peek methods using a linked list.\n",
|
"## Problem: Implement a stack with push, pop, peek, and is_empty methods using a linked list.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
@@ -42,7 +42,12 @@
|
|||||||
"### Peek\n",
|
"### Peek\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Peek on empty stack\n",
|
"* Peek on empty stack\n",
|
||||||
"* Peek on one or more element stack"
|
"* Peek on one or more element stack\n",
|
||||||
|
"\n",
|
||||||
|
"### Is Empty\n",
|
||||||
|
"\n",
|
||||||
|
"* Is empty on empty stack\n",
|
||||||
|
"* Is empty on one or more element stack"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -80,6 +85,14 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(1)\n",
|
"* Time: O(1)\n",
|
||||||
|
"* Space: O(1)\n",
|
||||||
|
"\n",
|
||||||
|
"### Is Empty\n",
|
||||||
|
"* If peek has a value, return False\n",
|
||||||
|
"* Else return True\n",
|
||||||
|
"\n",
|
||||||
|
"Complexity:\n",
|
||||||
|
"* Time: O(1)\n",
|
||||||
"* Space: O(1)"
|
"* Space: O(1)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -124,7 +137,10 @@
|
|||||||
" def peek(self):\n",
|
" def peek(self):\n",
|
||||||
" if self.top is not None:\n",
|
" if self.top is not None:\n",
|
||||||
" return self.top.data\n",
|
" return self.top.data\n",
|
||||||
" return None"
|
" return None\n",
|
||||||
|
"\n",
|
||||||
|
" def is_empty(self):\n",
|
||||||
|
" return self.peek() is None"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -164,8 +180,10 @@
|
|||||||
"print(stack.peek())\n",
|
"print(stack.peek())\n",
|
||||||
"print(stack.pop())\n",
|
"print(stack.pop())\n",
|
||||||
"print(stack.peek())\n",
|
"print(stack.peek())\n",
|
||||||
|
"print(stack.is_empty())\n",
|
||||||
"print(stack.pop())\n",
|
"print(stack.pop())\n",
|
||||||
"print(stack.peek())"
|
"print(stack.peek())\n",
|
||||||
|
"print(stack.is_empty())"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -24,3 +24,6 @@ class Stack(object):
|
|||||||
if self.top is not None:
|
if self.top is not None:
|
||||||
return self.top.data
|
return self.top.data
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def is_empty(self):
|
||||||
|
return self.peek() is None
|
||||||
Reference in New Issue
Block a user