As a magician-to-be, Elyse needs to practice some basics. She has a stack of cards that she wants to manipulate.
To make things a bit easier she only uses the cards 1 to 10.
Implement the function getCard(at:from:) that returns the card at position index from the given stack.
let index = 2
getCard(at: index, from: [1, 2, 4, 1])
// => 4Implement the function setCard(at:in:to) that returns a new stack that is a copy of the input stack but which has the card at position index changed to the new card provided. If the given index is not a valid index in the stack, the original stack should be returned, unchanged.
let index = 2
let newCard = 6
setCard(at: index, in: [1, 2, 4, 1], to: newCard)
// => [1, 2, 6, 1]Implement the function insert(_:atTopOf:) that returns a copy of the stack with the new card provided added to the top of the stack.
let newCard = 8
insert(newCard, atTopOf: [5, 9, 7, 1])
// => [5, 9, 7, 1, 8]Implement the function removeCard(at:from:) that returns a copy of the stack which has had the card at position index removed. If the given index is not a valid index in the stack, the original stack should be returned, unchanged.
let index = 2
removeCard(at: index, from: [3, 2, 6, 4, 8])
// => [3, 2, 4, 8]Implement the function removeTopCard(_:) that returns a copy of the stack which has had the card at the top of the stack removed. If the given stack is empty, the original stack should be returned, unchanged.
removeTopCard([3, 2, 6, 4, 8])
// => [3, 2, 6, 4]Implement the function insert(_:atBottomOf:) that returns a copy of the stack with the new card provided added to the bottom of the stack.
let newCard = 8
insert(newCard, atBottomOf: [5, 9, 7, 1])
// => [8, 5, 9, 7, 1]Implement the function removeBottomCard(_:) that returns a copy of the stack which has had the card at the bottom of the stack removed. If the given stack is empty, the original stack should be returned, unchanged.
removeBottomCard([8, 5, 9, 7, 1])
// => [5, 9, 7, 1]Implement the function checkSizeOfStack(_:_:) that checks whether the size of the stack is equal to a given stackSize or not.
let stackSize = 4
checkSizeOfStack([3, 2, 6, 4, 8], stackSize)
// => falseImplement the function evenCardCount(_:) that steps through the stack and counts the number of even cards in it.
evenCardCount([3,8,4,5,1,6,10])
// => 4