mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-05 08:58:02 +00:00
Added stack method is_empty().
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"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",
|
||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
@@ -42,7 +42,12 @@
|
||||
"### Peek\n",
|
||||
"\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",
|
||||
"Complexity:\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)"
|
||||
]
|
||||
},
|
||||
@@ -124,7 +137,10 @@
|
||||
" def peek(self):\n",
|
||||
" if self.top is not None:\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.pop())\n",
|
||||
"print(stack.peek())\n",
|
||||
"print(stack.is_empty())\n",
|
||||
"print(stack.pop())\n",
|
||||
"print(stack.peek())"
|
||||
"print(stack.peek())\n",
|
||||
"print(stack.is_empty())"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -23,4 +23,7 @@ class Stack(object):
|
||||
def peek(self):
|
||||
if self.top is not None:
|
||||
return self.top.data
|
||||
return None
|
||||
return None
|
||||
|
||||
def is_empty(self):
|
||||
return self.peek() is None
|
||||
Reference in New Issue
Block a user