mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update stack min challenge (#128)
Rename variable min_vals to stack_of_mins to improve clarity. Rename method min to minimum to avoid using the same name as the built-in min.
This commit is contained in:
parent
385bc54efa
commit
2399eb60d3
|
@ -103,7 +103,7 @@
|
|||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def min(self):\n",
|
||||
" def minimum(self):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
|
@ -146,26 +146,26 @@
|
|||
" stack = StackMin()\n",
|
||||
" stack.push(5)\n",
|
||||
" assert_equal(stack.peek(), 5)\n",
|
||||
" assert_equal(stack.min(), 5)\n",
|
||||
" assert_equal(stack.minimum(), 5)\n",
|
||||
" stack.push(1)\n",
|
||||
" assert_equal(stack.peek(), 1)\n",
|
||||
" assert_equal(stack.min(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" stack.push(3)\n",
|
||||
" assert_equal(stack.peek(), 3)\n",
|
||||
" assert_equal(stack.min(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" stack.push(0)\n",
|
||||
" assert_equal(stack.peek(), 0)\n",
|
||||
" assert_equal(stack.min(), 0)\n",
|
||||
" assert_equal(stack.minimum(), 0)\n",
|
||||
"\n",
|
||||
" print('Test: Pop on non-empty stack')\n",
|
||||
" assert_equal(stack.pop(), 0)\n",
|
||||
" assert_equal(stack.min(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" assert_equal(stack.pop(), 3)\n",
|
||||
" assert_equal(stack.min(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" assert_equal(stack.pop(), 1)\n",
|
||||
" assert_equal(stack.min(), 5)\n",
|
||||
" assert_equal(stack.minimum(), 5)\n",
|
||||
" assert_equal(stack.pop(), 5)\n",
|
||||
" assert_equal(stack.min(), sys.maxsize)\n",
|
||||
" assert_equal(stack.minimum(), sys.maxsize)\n",
|
||||
"\n",
|
||||
" print('Test: Pop empty stack')\n",
|
||||
" assert_equal(stack.pop(), None)\n",
|
||||
|
|
|
@ -129,23 +129,23 @@
|
|||
"\n",
|
||||
" def __init__(self, top=None):\n",
|
||||
" super(StackMin, self).__init__(top)\n",
|
||||
" self.min_vals = Stack()\n",
|
||||
" self.stack_of_mins = Stack()\n",
|
||||
"\n",
|
||||
" def min(self):\n",
|
||||
" if self.min_vals.top is None:\n",
|
||||
" def minimum(self):\n",
|
||||
" if self.stack_of_mins.top is None:\n",
|
||||
" return sys.maxsize\n",
|
||||
" else:\n",
|
||||
" return self.min_vals.peek()\n",
|
||||
" return self.stack_of_mins.peek()\n",
|
||||
"\n",
|
||||
" def push(self, data):\n",
|
||||
" super(StackMin, self).push(data)\n",
|
||||
" if data < self.min():\n",
|
||||
" self.min_vals.push(data)\n",
|
||||
" if data < self.minimum():\n",
|
||||
" self.stack_of_mins.push(data)\n",
|
||||
"\n",
|
||||
" def pop(self):\n",
|
||||
" data = super(StackMin, self).pop()\n",
|
||||
" if data == self.min():\n",
|
||||
" self.min_vals.pop()\n",
|
||||
" if data == self.minimum():\n",
|
||||
" self.stack_of_mins.pop()\n",
|
||||
" return data"
|
||||
]
|
||||
},
|
||||
|
@ -184,26 +184,26 @@
|
|||
" stack = StackMin()\n",
|
||||
" stack.push(5)\n",
|
||||
" assert_equal(stack.peek(), 5)\n",
|
||||
" assert_equal(stack.min(), 5)\n",
|
||||
" assert_equal(stack.minimum(), 5)\n",
|
||||
" stack.push(1)\n",
|
||||
" assert_equal(stack.peek(), 1)\n",
|
||||
" assert_equal(stack.min(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" stack.push(3)\n",
|
||||
" assert_equal(stack.peek(), 3)\n",
|
||||
" assert_equal(stack.min(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" stack.push(0)\n",
|
||||
" assert_equal(stack.peek(), 0)\n",
|
||||
" assert_equal(stack.min(), 0)\n",
|
||||
" assert_equal(stack.minimum(), 0)\n",
|
||||
"\n",
|
||||
" print('Test: Pop on non-empty stack')\n",
|
||||
" assert_equal(stack.pop(), 0)\n",
|
||||
" assert_equal(stack.min(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" assert_equal(stack.pop(), 3)\n",
|
||||
" assert_equal(stack.min(), 1)\n",
|
||||
" assert_equal(stack.minimum(), 1)\n",
|
||||
" assert_equal(stack.pop(), 1)\n",
|
||||
" assert_equal(stack.min(), 5)\n",
|
||||
" assert_equal(stack.minimum(), 5)\n",
|
||||
" assert_equal(stack.pop(), 5)\n",
|
||||
" assert_equal(stack.min(), sys.maxsize)\n",
|
||||
" assert_equal(stack.minimum(), sys.maxsize)\n",
|
||||
"\n",
|
||||
" print('Test: Pop empty stack')\n",
|
||||
" assert_equal(stack.pop(), None)\n",
|
||||
|
|
|
@ -8,26 +8,26 @@ class TestStackMin(object):
|
|||
stack = StackMin()
|
||||
stack.push(5)
|
||||
assert_equal(stack.peek(), 5)
|
||||
assert_equal(stack.min(), 5)
|
||||
assert_equal(stack.minimum(), 5)
|
||||
stack.push(1)
|
||||
assert_equal(stack.peek(), 1)
|
||||
assert_equal(stack.min(), 1)
|
||||
assert_equal(stack.minimum(), 1)
|
||||
stack.push(3)
|
||||
assert_equal(stack.peek(), 3)
|
||||
assert_equal(stack.min(), 1)
|
||||
assert_equal(stack.minimum(), 1)
|
||||
stack.push(0)
|
||||
assert_equal(stack.peek(), 0)
|
||||
assert_equal(stack.min(), 0)
|
||||
assert_equal(stack.minimum(), 0)
|
||||
|
||||
print('Test: Pop on non-empty stack')
|
||||
assert_equal(stack.pop(), 0)
|
||||
assert_equal(stack.min(), 1)
|
||||
assert_equal(stack.minimum(), 1)
|
||||
assert_equal(stack.pop(), 3)
|
||||
assert_equal(stack.min(), 1)
|
||||
assert_equal(stack.minimum(), 1)
|
||||
assert_equal(stack.pop(), 1)
|
||||
assert_equal(stack.min(), 5)
|
||||
assert_equal(stack.minimum(), 5)
|
||||
assert_equal(stack.pop(), 5)
|
||||
assert_equal(stack.min(), sys.maxsize)
|
||||
assert_equal(stack.minimum(), sys.maxsize)
|
||||
|
||||
print('Test: Pop empty stack')
|
||||
assert_equal(stack.pop(), None)
|
||||
|
|
Loading…
Reference in New Issue
Block a user