GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — daily_coding_problem/problems/43 at master · ihossai4/daily_coding_problem

Expand file tree

/

43

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

43

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
README.md
README.md
 
 
problem_43.py
problem_43.py
 
 
solution_43.py
solution_43.py
 
 

README.md

<< [43] Implement a stack with a ma >>

Implement a stack that has the following methods:

  • push(val), which pushes an element onto the stack
  • pop(), which pops off and returns the topmost element of the stack. If there are no elements in the stack, then it should throw an error or return null.
  • max(), which returns the maximum value in the stack currently. If there are no elements in the stack, then it should throw an error or return null.

Each method should run in constant time.

Example:

>>> max_stack = coding_problem_43()
>>> max_stack.push(1)
>>> max_stack.max()
1

>>> max_stack.push(3)
>>> max_stack.max()
3

>>> max_stack.push(2)
>>> max_stack.max()
3

>>> max_stack.pop()
2

>>> max_stack.max()
3

>>> _ = max_stack.pop()
>>> max_stack.max()
1

>>> _ = max_stack.pop()
>>> max_stack.max()
Traceback (most recent call last):
...
IndexError: list index out of range

>>> _ = max_stack.pop()
Traceback (most recent call last):
...
IndexError: pop from empty list
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.