Created
May 10, 2020 16:43
-
-
Save ericwindmill/a3e3c3b014b1286e5ee51ad37581cd1f to your computer and use it in GitHub Desktop.
fbe_dart_iterables_remove_elements_stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Peek should show the element that was added least recently, but not remove it |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// solution | |
class Stack<T> { | |
List<T> _internal = <T>[]; | |
T peek() { | |
// implement peek | |
} | |
T pop() { | |
// implement pop | |
} | |
void push(T value) { | |
// implement push | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// solution | |
class Stack<T> { | |
List<T> _internal = <T>[]; | |
T peek() => _internal.last; | |
T pop() => _internal.removeLast(); | |
void push(T value) => _internal.add(value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
final stack = Stack<int>(); | |
stack.push(1); | |
stack.push(2); | |
if (stack.peek() != 2) { | |
_result(false, ["peek did not return the top element in the stack"]); | |
} | |
if (stack._internal.length != 2) { | |
_result(false, ["expected stack of length 2 after adding two elements and peeking"]); | |
} | |
final popped = stack.pop(); | |
if (popped != 2) { | |
_result(false, ["expected the last element added to the stack when `push` is called."]); | |
} | |
_result(true, ["Good work, fam"]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment