Skip to content
Merged
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
6 changes: 3 additions & 3 deletions concepts/arrays/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ There is no ability to create a two-dimensional array (array of arrays).

## Declaring and Initializing an Array Variable

You can initialize an array by assigning a parenthesized list of elements (possibly empty) to an variable.
You can initialize an array by assigning a parenthesized list of elements (possibly empty) to a variable.

```bash
myarray=("one" "two" "three")
Expand Down Expand Up @@ -135,7 +135,7 @@ To assign to a particular array entry, use this syntax:
myarray[10]="hello"
```

To _append_ to an array, use the `+=` concatenating-assigment operator, and use parentheses:
To _append_ to an array, use the `+=` concatenating-assignment operator, and use parentheses:

```bash
myarray+=("new element 1" "new element 2")
Expand Down Expand Up @@ -183,7 +183,7 @@ The `"${myarray[@]}"` form works with sparse arrays, expanding to only defined v

This special expansion, where the index is an "at sign" (`@`), is **only special within double quotes**.

#### Other Special Parameter Expansions
### Other Special Parameter Expansions

Here is more special syntax that we use for iterating over an array.
These are described in the manual in [Shell Parameter Expansion][man-expn].
Expand Down
6 changes: 3 additions & 3 deletions concepts/arrays/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ There is no ability to create a two-dimensional array (array of arrays).

## Declaring and Initializing an Array Variable

You can initialize an array by assigning a parenthesized list of elements (possibly empty) to an variable.
You can initialize an array by assigning a parenthesized list of elements (possibly empty) to a variable.

```bash
myarray=("one" "two" "three")
Expand Down Expand Up @@ -135,7 +135,7 @@ To assign to a particular array entry, use this syntax:
myarray[10]="hello"
```

To _append_ to an array, use the `+=` concatenating-assigment operator, and use parentheses:
To _append_ to an array, use the `+=` concatenating-assignment operator, and use parentheses:

```bash
myarray+=("new element 1" "new element 2")
Expand Down Expand Up @@ -183,7 +183,7 @@ The `"${myarray[@]}"` form works with sparse arrays, expanding to only defined v

This special expansion, where the index is an "at sign" (`@`), is **only special within double quotes**.

#### Other Special Parameter Expansions
### Other Special Parameter Expansions

Here is more special syntax that we use for iterating over an array.
These are described in the manual in [Shell Parameter Expansion][man-expn].
Expand Down
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
63 changes: 63 additions & 0 deletions exercises/concept/elyses-enchantments/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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")`.
- 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[index]}`.
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[index]=$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+=("$new_card")`.

## 4. Remove a card from the stack

- Use the [`unset` builtin][builtins]: `unset "cards[index]"`.
- 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 think about how the top card's index relates to the length.
- The index may be an arithmetic expression.
- An empty stack has no top card.
Only remove a card when the length is greater than zero, 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.
- Think about which card has to go into the new array first.
- Every card of the old stack has to follow it, in the same order.

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

- Removing the element at index 0 with `unset` would leave the other cards where they are, so build a new array instead.
- Start with an empty array, and append every card except the one at index 0.
- Loop over the _indices_ with an arithmetic `for` loop, and think about which index it should start at.

## 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
149 changes: 149 additions & 0 deletions exercises/concept/elyses-enchantments/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# 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 bottom card, position 1 is the card above it, and the highest position is the top 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 bottom card to the top 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 bottom card first.

## 1. Retrieve a card from a stack

Print the card at the given position.
If there is no card at that position, print an empty line.

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.

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
Loading