Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@
"conditionals"
],
"status": "wip"
},
{
"slug": "elyses-enchantments",
"name": "Elyses Enchantments",
"uuid": "ffc7f40e-71e7-4f5f-a022-703df85c0ec4",
"concepts": [
"arrays"
],
"prerequisites": [
"looping"
],
"status": "wip"
}
],
"practice": [
Expand Down
70 changes: 70 additions & 0 deletions exercises/concept/elyses-enchantments/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Hints

## General

- The number of cards is not known in advance, so store them in an array instead of in separate variables.
- The `case` structure and the argument handling are written for you; you only have to complete each branch.
- That argument handling uses the [`shift` builtin][builtins] to discard the arguments it has already read, so that `"$@"` holds only the ones still to come.
`shift 2` discards two of them.
You do not have to write `shift` yourself.
- Create an empty array with `array=()`, and add an element with `array+=("$value")`.
- To put each remaining command-line argument into the array in turn, loop over `"$@"`.
- Quote your expansions, so that a card containing whitespace or glob characters stays in one piece.
- `echo` prints all of its arguments on one line, separated by single spaces.
Since `"${cards[@]}"` expands to the array's values as separate words, `echo "${cards[@]}"` prints the whole stack on one line.

## 1. Retrieve a card from a stack

- Access an element with `${cards[parameter]}`.
The curly braces are required.
- The index is an arithmetic context, so you can use a variable name without the `$`.
- If the index does not exist in the array, the expansion is an empty string, not an error.

## 2. Exchange a card in the stack

- Assign to a single element with `cards[parameter]=$card`.
- As with every assignment, there must be no spaces around the `=`.

## 3. Insert a card at the top of the stack

- Append with the `+=` operator and parentheses: `cards+=("$parameter")`.

## 4. Remove a card from the stack

- Use the [`unset` builtin][builtins]: `unset "cards[parameter]"`.
- Quote the argument, otherwise bash tries to expand the square brackets as a filename pattern.
- The remaining elements are not renumbered, so the array now has a gap.
Printing `"${cards[@]}"` skips the gap, so the printed stack is still correct.

## 5. Remove the top card from the stack

- The number of elements in the array is `${#cards[@]}`, with a `#` before the array name.
- Indices start at zero, so the bottom card's index is one less than the length.
- The index may be an arithmetic expression, so `unset "cards[${#cards[@]}-1]"` removes the bottom card.
- An empty stack has no bottom card.
Guard the removal with an `if (( ... ))` test on the length, otherwise `unset` writes a `bad array subscript` error message, which the tests count as output.

## 6. Insert a card at the bottom of the stack

- Bash cannot insert an element at the start of an array, so build a new array instead.
- Start the new array with the new card: `new_cards=("$parameter")`.
- Then append each card of the old stack in turn, with a `for` loop over `"${cards[@]}"`.

## 7. Remove a card from the bottom of the stack

- Start with an empty array: `new_cards=()`.
- Loop over the _indices_ with an arithmetic `for` loop that starts at index 1, and append each value:

```bash
size=${#cards[@]}
for (( index = 1; index < size; index++ )); do
new_cards+=("${cards[index]}")
done
```

## 8. Check the size of the stack

- Compare two numbers inside an arithmetic conditional, `(( ... ))`.
- Use an `if` command with an `else` branch to choose between `true` and `false`.

[builtins]: https://www.gnu.org/software/bash/manual/bash.html#Bash-Builtins
153 changes: 153 additions & 0 deletions exercises/concept/elyses-enchantments/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Instructions

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, so her stack of cards can be represented by a list of numbers.
The position of a card corresponds to the index in the array: position 0 is the top card, position 1 is the second card, and the highest position is the bottom card.

You have eight tasks, one for each operation that Elyse wants to perform.
Do not print anything other than the requested output.

This program uses a _subcommand interface_.
The first argument names the operation to perform, and the remaining arguments are that operation's own inputs.

The starter code selects the operation with a `case` command, which you read about in the [Conditionals][conditionals] concept.
That structure is already written for you: you only have to fill in the branches.

The cards of the stack are always the last arguments, ordered from the top card to the bottom card.
When a task asks you to print the stack, print all of its cards on a single line, separated by one space, with the top card first.

## 1. Retrieve a card from a stack

Print the card at the given position.

In the example below, the given position is the `2` that follows `get_card`, and the stack of cards is `3 2 6 4 8`.
The `2` inside the stack is just one of Elyse's cards, not a position.

```bash
bash elyses_enchantments.sh get_card 2 3 2 6 4 8
```

```text
6
```

## 2. Exchange a card in the stack

Perform some sleight of hand and exchange the card at the given position with the given card.
Print the adjusted stack.

In the example below, the given position is `2`, the card to exchange with is `9`, and the stack of cards is `3 2 6 4 8`.

```bash
bash elyses_enchantments.sh set_card 2 9 3 2 6 4 8
```

```text
3 2 9 4 8
```

## 3. Insert a card at the top of the stack

Make a card appear by inserting the given card at the top of the stack.
The top of the stack is the _end_ of the array.
Print the adjusted stack.

In the example below, the card to insert is `9`, and the stack of cards is `3 2 6 4 8`.

```bash
bash elyses_enchantments.sh insert_at_top 9 3 2 6 4 8
```

```text
3 2 6 4 8 9
```

## 4. Remove a card from the stack

Make a card disappear by removing the card at the given position.
Print the adjusted stack.

Removing an element does not renumber the remaining elements, so the array keeps a gap.
That does not matter here: when you print the _values_ of the array, the gap is skipped.

In the example below, the given position is `2`, and the stack of cards is `3 2 6 4 8`.

```bash
bash elyses_enchantments.sh remove_card 2 3 2 6 4 8
```

```text
3 2 4 8
```

## 5. Remove the top card from the stack

Make a card disappear by removing the top card, whatever the size of the stack happens to be.
Print the adjusted stack.

~~~~exercism/note
An empty stack has no top card, and bash reports an error if you ask it to remove the element at index `-1`.
Remove the top card only when the stack actually has cards in it.
~~~~

In the example below, the stack of cards is `3 2 6 4 8`; this operation takes no other argument.

```bash
bash elyses_enchantments.sh remove_at_top 3 2 6 4 8
```

```text
3 2 6 4
```

## 6. Insert a card at the bottom of the stack

Make a card appear by inserting the given card at the bottom of the stack.
The bottom of the stack is the _start_ of the array, so every card that is already there moves to a higher position: index 0 now holds the new card `9`, index 1 holds `3`, and so on.
Print the adjusted stack.

In the example below, the card to insert is `9`, and the stack of cards is `3 2 6 4 8`.

```bash
bash elyses_enchantments.sh insert_at_bottom 9 3 2 6 4 8
```

```text
9 3 2 6 4 8
```

## 7. Remove a card from the bottom of the stack

Make a card disappear by removing the bottom card.
Every remaining card moves to a lower position: index 0 now holds `2`, index 1 holds `6`, and so on.
Unlike removing a card from the middle of the stack, this leaves no gap behind.
Print the adjusted stack.

In the example below, the stack of cards is `3 2 6 4 8`; this operation takes no other argument.

```bash
bash elyses_enchantments.sh remove_at_bottom 3 2 6 4 8
```

```text
2 6 4 8
```

## 8. Check the size of the stack

Check whether the size of the stack is equal to the given size.
Print `true` if it is, and `false` if it is not.

In the example below, the given size is `5`, and the stack of cards is `3 2 6 4 8`.

```bash
bash elyses_enchantments.sh check_size 5 3 2 6 4 8
```

```text
true
```

[conditionals]: https://exercism.org/tracks/bash/concepts/conditionals
Loading