Skip to content

Commit

Permalink
Stack underflow: specify 2 numbers are expected (#1395)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska authored Dec 9, 2023
1 parent a0e20d9 commit 63e38a1
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion exercises/concept/stack-underflow/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ raise StackUnderflowError, "when dividing"

## 3. Write a dividing function

Implement the `divide/1` function which takes a stack _(list of numbers)_ and:
Implement the `divide/1` function which takes a stack _(a list of two numbers)_ and:

- raises _stack underflow_ when the stack does not contain enough numbers
- raises _division by zero_ when the divisor is 0 (note the stack of numbers is stored in the reverse order)
Expand Down
4 changes: 2 additions & 2 deletions exercises/concept/stack-underflow/.meta/exemplar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ defmodule RPNCalculator.Exception do
end

def divide(stack) when length(stack) < 2, do: raise(StackUnderflowError, "when dividing")
def divide([divisor, _number | _]) when divisor == 0, do: raise(DivisionByZeroError)
def divide([divisor, number | _]), do: number / divisor
def divide([divisor, _number]) when divisor == 0, do: raise(DivisionByZeroError)
def divide([divisor, number]), do: number / divisor
end

0 comments on commit 63e38a1

Please sign in to comment.