From b252724749a4b8f3e7a47f1385f56f9ccbf36d94 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 2 Dec 2026 07:44:10 -0500 Subject: [PATCH 1/6] Extract associative arrays into their own concept Move the associative-arrays section out of the arrays concept into a new associative-arrays concept, a sibling of more-arrays in the syllabus. Also add to arrays: checking for an element with -v, removing one with unset, and a few wording fixes. Add the suggested exercises to the concept exercise road-map, and make looping the prerequisite for arrays. Co-Authored-By: Claude Sonnet 5.5 --- concepts/arrays/about.md | 2 +- concepts/arrays/introduction.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/concepts/arrays/about.md b/concepts/arrays/about.md index e2692f83..4a31280c 100644 --- a/concepts/arrays/about.md +++ b/concepts/arrays/about.md @@ -15,7 +15,7 @@ There are two kinds of arrays: (Other languages might call this a "map" or a "dictionary".) This document covers numerically-indexed arrays (hereafter simply known as "arrays"). -Associative arrays will be discussed later. +Associative arrays will be learned later. ~~~~exercism/caution Bash arrays can _only_ store scalar values. diff --git a/concepts/arrays/introduction.md b/concepts/arrays/introduction.md index 24b0ce8e..cd037442 100644 --- a/concepts/arrays/introduction.md +++ b/concepts/arrays/introduction.md @@ -15,7 +15,7 @@ There are two kinds of arrays: (Other languages might call this a "map" or a "dictionary".) This document covers numerically-indexed arrays (hereafter simply known as "arrays"). -Associative arrays will be discussed later. +Associative arrays will be learned later. ~~~~exercism/caution Bash arrays can _only_ store scalar values. From 518814d37d6a5b9f704d74ec420a1756be4631dc Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 2 Dec 2026 20:12:11 -0500 Subject: [PATCH 2/6] Add the Elyses Enchantments concept exercise for arrays Fork the JavaScript elyses-enchantments story for the arrays concept, with looping as its prerequisite. The exercise uses a subcommand interface: the first argument names the operation, and the remaining arguments are that operation's inputs. The stub provides the `case` dispatch and the argument handling, so the student only writes the array logic in each branch. There are eight tasks, each printing a single line: retrieving a card by index, exchanging a card, inserting and removing at the top and at the bottom of the stack, and checking the size of the stack. Co-Authored-By: Claude Sonnet 5.5 --- config.json | 12 + .../elyses-enchantments/.docs/hints.md | 70 ++ .../elyses-enchantments/.docs/instructions.md | 153 +++++ .../elyses-enchantments/.docs/introduction.md | 243 +++++++ .../.docs/introduction.md.tpl | 3 + .../elyses-enchantments/.meta/config.json | 23 + .../elyses-enchantments/.meta/design.md | 44 ++ .../elyses-enchantments/.meta/exemplar.sh | 91 +++ .../elyses-enchantments/bats-extra.bash | 642 ++++++++++++++++++ .../elyses_enchantments.bats | 173 +++++ .../elyses_enchantments.sh | 100 +++ 11 files changed, 1554 insertions(+) create mode 100644 exercises/concept/elyses-enchantments/.docs/hints.md create mode 100644 exercises/concept/elyses-enchantments/.docs/instructions.md create mode 100644 exercises/concept/elyses-enchantments/.docs/introduction.md create mode 100644 exercises/concept/elyses-enchantments/.docs/introduction.md.tpl create mode 100644 exercises/concept/elyses-enchantments/.meta/config.json create mode 100644 exercises/concept/elyses-enchantments/.meta/design.md create mode 100644 exercises/concept/elyses-enchantments/.meta/exemplar.sh create mode 100644 exercises/concept/elyses-enchantments/bats-extra.bash create mode 100644 exercises/concept/elyses-enchantments/elyses_enchantments.bats create mode 100644 exercises/concept/elyses-enchantments/elyses_enchantments.sh diff --git a/config.json b/config.json index 0d3d6805..4ae3ef12 100644 --- a/config.json +++ b/config.json @@ -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": [ diff --git a/exercises/concept/elyses-enchantments/.docs/hints.md b/exercises/concept/elyses-enchantments/.docs/hints.md new file mode 100644 index 00000000..b7db3bab --- /dev/null +++ b/exercises/concept/elyses-enchantments/.docs/hints.md @@ -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 diff --git a/exercises/concept/elyses-enchantments/.docs/instructions.md b/exercises/concept/elyses-enchantments/.docs/instructions.md new file mode 100644 index 00000000..9382c009 --- /dev/null +++ b/exercises/concept/elyses-enchantments/.docs/instructions.md @@ -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 diff --git a/exercises/concept/elyses-enchantments/.docs/introduction.md b/exercises/concept/elyses-enchantments/.docs/introduction.md new file mode 100644 index 00000000..59b9f71b --- /dev/null +++ b/exercises/concept/elyses-enchantments/.docs/introduction.md @@ -0,0 +1,243 @@ +# Introduction + +## Arrays + +Bash provides two types of parameters: "scalar" values and arrays. +A scalar value can be a string or a number; it represents a single "thing". + +Most of the time, scalar values are sufficient to contain your data. +But sometimes, you need to store a list of scalar values. +In this case you will use an _array_. + +There are two kinds of arrays: + +* Numerically-indexed arrays map an integer key to a string value. + (Other languages might call this a "list" or a "sequence".) +* Associative arrays map a string key to a string value. + (Other languages might call this a "map" or a "dictionary".) + +This document covers numerically-indexed arrays (hereafter simply known as "arrays"). +Associative arrays will be learned later. + +~~~~exercism/caution +Bash arrays can _only_ store scalar values. +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. + +```bash +myarray=("one" "two" "three") +``` + +As is the case with every variable assignment, there must be no spaces around the equal sign. + +Note that there are no comma separators between the elements; it is a whitespace-separated list of strings. + +In fact, the amount of whitespace inside the parentheses is completely arbitrary. +Newlines are allowed; use as many as you want to improve readability. + +```bash +myarray=( + "first element" + "second element" + "this is the last element" +) +``` + +Assigning a list of strings to an array variable in this manner will store the first string at **index zero**, the next string at index one, the next at index two, and so on. + +It is possible to initialize a "sparse array" by specifying the indices you need. + +```bash +raindrops=([3]="Pling" [5]="Plang" [7]="Plong") +``` + +~~~~exercism/note +To reiterate: array indices are zero-based. +This snippet outputs "true": + +```bash +myarray=("one" "two" "three") +if [[ "${myarray[0]}" == "one" ]]; then + echo true +else + echo false +fi +``` +~~~~ + +You can use the `declare` (or `local` inside a function) command to specify that a variable will hold an array. + +```bash +declare -a myarray +``` + +It is more idiomatic to just assign a (perhaps empty) parenthesized list to initialize the variable. + +### Accessing Elements of an Array + +As you saw in the note above, the syntax to access an array element is `${myarray[$index]}`. +The index is given in square brackets. +The curly braces are required. + +~~~~exercism/note +For numerically-indexed arrays, the index is an arithmetic expression. +The Bash manual [states][arithmetic]: + +> Within an arithmetic expression, shell variables may also be referenced by name without using the parameter expansion syntax. + +This means that the `$` is not required for "simple" variable expansion. +You can access elements like this, which is a bit easier to read: + +```bash +i=5 +echo "${myarray[i]}" +# ..............^ +``` + +[arithmetic]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Arithmetic +~~~~ + +If the given index does not exist in the array, the result of the expansion is **an empty string**, not an error. + +#### Checking whether an Element Exists + +To find out whether an element exists, use the `-v` test in a conditional command (see the [Conditionals][conditionals] concept). +This distinguishes a missing element from an element that holds an empty string. + +```bash +myarray=(one "" three) + +if [[ -v myarray[1] ]]; then + echo "index 1 is set" +else + echo "index 1 is not set" +fi +# => index 1 is set + +if [[ -v myarray[5] ]]; then + echo "index 5 is set" +else + echo "index 5 is not set" +fi +# => index 5 is not set +``` + +### Assigning to an Array + +To assign to a particular array entry, use this syntax: + +```bash +myarray[10]="hello" +``` + +To _append_ to an array, use the `+=` concatenating-assigment operator, and use parentheses: + +```bash +myarray+=("new element 1" "new element 2") +``` + +### Removing an Element + +To remove an element, use the `unset` command. +Quote the argument so the shell does not try to expand the brackets as a filename pattern. + +```bash +myarray=(x y z) +unset 'myarray[1]' +declare -p myarray +# => declare -a myarray=([0]="x" [2]="z") +``` + +The remaining elements are **not** renumbered. +Removing an element from the middle leaves a gap, so the array becomes a sparse array. +The length is now 2, but the indices are 0 and 2. + +To remove the entire array, give `unset` the name without brackets: `unset myarray`. + +### Iterating over an Array + +You will typically need to iterate over the _values_ of an array. +Use a for loop (that you saw in the [Looping][looping] concept): + +```bash +myarray=("one" "two" "three") +for value in "${myarray[@]}"; do + echo "$value" +done +``` + +Note the special syntax to expand the array: `"${myarray[@]}"` +This form of parameter expansion substitutes the array values as separate words. +It is somewhat equivalent to: + +```bash +for value in "${myarray[0]}" "${myarray[1]}" ... "${myarray[n]}" +``` + +The `"${myarray[@]}"` form works with sparse arrays, expanding to only defined values. + +This special expansion, where the index is an "at sign" (`@`), is **only special within double quotes**. + +##### Other Special Parameter Expansions + +Here are another couple of bits of special syntax that we need for iterating over an array. +These are described in the manual in [Shell Parameter Expansion][man-expn]. + +To find the _length_ of an array, use `${#myarray[@]}`, with a `#` before the name. +The "length" of an array means the number of elements in it. + +To extract the list of _indices_ of an array, use `"${!myarray[@]}"`, with a `!` before the name and the expansion in double quotes. +For numerically-indexed arrays, the indices are expanded in numerical order. + +#### Iterating over Array Indices + +Sometimes you need the index as well as the value. +There are a couple of ways to do this. + +1. When you know the indices form an uninterrupted sequence, you can use an _arithmetic_ for loop. + + ```bash + len=${#myarray[@]} + for ((i = 0; i < len; i++)); do + echo "$i -> ${myarray[i]}" + done + ``` + + Note the `${#myarray[@]}` syntax, with the `#` character in front of the array name. + Since arrays use zero-based indexing, the array's last index is one less than the array length. + +2. When the array is sparse, you can use this form: + + ```bash + for index in "${!raindrops[@]}"; do + echo "$index -> ${raindrops[index]}" + done + ``` + + Note the `${!myarray[@]}` syntax, with the `!` character in front of the array name. + +### Inspecting an Array + +If you want to look at the contents of an array, use the `declare` command with the `-p` option: + +```bash +a=("the 'first' element" + second + third + 'the "last" element') +declare -p a +``` + +This outputs the contents with canonical shell quoting. + +```bash +declare -a a=([0]="the 'first' element" [1]="second" [2]="third" [3]="the \"last\" element") +``` + +[conditionals]: https://exercism.org/tracks/bash/concepts/conditionals +[looping]: https://exercism.org/tracks/bash/concepts/looping +[man-expn]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion diff --git a/exercises/concept/elyses-enchantments/.docs/introduction.md.tpl b/exercises/concept/elyses-enchantments/.docs/introduction.md.tpl new file mode 100644 index 00000000..44b5e267 --- /dev/null +++ b/exercises/concept/elyses-enchantments/.docs/introduction.md.tpl @@ -0,0 +1,3 @@ +# Introduction + +%{concept:arrays} diff --git a/exercises/concept/elyses-enchantments/.meta/config.json b/exercises/concept/elyses-enchantments/.meta/config.json new file mode 100644 index 00000000..712cd2e6 --- /dev/null +++ b/exercises/concept/elyses-enchantments/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "elyses_enchantments.sh" + ], + "test": [ + "elyses_enchantments.bats" + ], + "exemplar": [ + ".meta/exemplar.sh" + ], + "invalidator": [ + "bats-extra.bash" + ] + }, + "forked_from": [ + "javascript/elyses-enchantments" + ], + "blurb": "Learn about arrays while helping Elyse practise her card tricks." +} diff --git a/exercises/concept/elyses-enchantments/.meta/design.md b/exercises/concept/elyses-enchantments/.meta/design.md new file mode 100644 index 00000000..1fad0c77 --- /dev/null +++ b/exercises/concept/elyses-enchantments/.meta/design.md @@ -0,0 +1,44 @@ +# Design + +## Goal + +The goal of this exercise is to introduce numerically-indexed arrays in bash scripts. + +## Learning objectives + +- Create an array by assigning a parenthesized list of elements to a variable. +- Create an empty array with `array=()`. +- Append elements to an array with `array+=("$value")`. +- Collect a variable number of command-line arguments into an array. +- Access an element with `${array[index]}`, and know that a missing index expands to an empty string. +- Use a variable as an index without the `$`: `${array[position]}`. +- Assign to a single element with `array[index]=$value`. +- Remove an element with `unset 'array[index]'`, and know that the remaining elements are not renumbered. +- Find the number of elements in an array with `${#array[@]}`. +- Iterate over the values of an array with `for value in "${array[@]}"`. +- Iterate over the indices of an array with an arithmetic `for` loop. +- Print the elements of an array on one line by passing `"${array[@]}"` to `echo` as arguments. +- Complete the branch of a `case` command that the stub provides. +- Read the `shift` builtin in the provided stub, and understand that it discards already-used positional parameters so `"$@"` holds the rest. + +## Out of scope + +- Writing a `case` command: the subcommand dispatch structure is provided in the stub, and `case` is documented in the `conditionals` concept. +- Associative arrays. +- Joining the elements into a single string with `"${array[*]}"`, and the `IFS` variable. +- Array slices, such as `"${array[@]:1:2}"` and `"${@:2}"`. +- Sparse arrays created with an explicit index, such as `array=([3]="Pling")`. +- Testing whether an element exists with `[[ -v array[index] ]]`. +- Passing arrays to functions, and namerefs (`local -n`). +- Inspecting an array with `declare -p`. +- Functions and `local` variables. + +## Concepts + +The Concepts this exercise unlocks are: + +- `arrays` + +## Prerequisites + +- `looping` diff --git a/exercises/concept/elyses-enchantments/.meta/exemplar.sh b/exercises/concept/elyses-enchantments/.meta/exemplar.sh new file mode 100644 index 00000000..7599a966 --- /dev/null +++ b/exercises/concept/elyses-enchantments/.meta/exemplar.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash + +subcommand=$1 +shift + +# `cards` holds the stack of cards. +# Operations that take a parameter collect it here first. +parameter= +cards=("$@") + +case ${subcommand} in + get_card) + # Task 1 + parameter=$1 + shift + cards=("$@") + echo "${cards[parameter]}" + ;; + + set_card) + # Task 2 + parameter=$1 + card=$2 + shift 2 + cards=("$@") + cards[parameter]=$card + echo "${cards[@]}" + ;; + + insert_at_top) + # Task 3 + parameter=$1 + shift + cards=("$@") + cards+=("$parameter") + echo "${cards[@]}" + ;; + + remove_card) + # Task 4 + parameter=$1 + shift + cards=("$@") + unset "cards[parameter]" + echo "${cards[@]}" + ;; + + remove_at_top) + # Task 5 + size=${#cards[@]} + # `unset "cards[-1]"` is an error, so only remove a card when there is one. + if (( size > 0 )); then + unset "cards[size-1]" + fi + echo "${cards[@]}" + ;; + + insert_at_bottom) + # Task 6 + parameter=$1 + shift + cards=("$@") + new_cards=("$parameter") + for card in "${cards[@]}"; do + new_cards+=("$card") + done + echo "${new_cards[@]}" + ;; + + remove_at_bottom) + # Task 7 + size=${#cards[@]} + new_cards=() + for (( index = 1; index < size; index++ )); do + new_cards+=("${cards[index]}") + done + echo "${new_cards[@]}" + ;; + + check_size) + # Task 8 + parameter=$1 + shift + cards=("$@") + if (( ${#cards[@]} == parameter )); then + echo true + else + echo false + fi + ;; +esac diff --git a/exercises/concept/elyses-enchantments/bats-extra.bash b/exercises/concept/elyses-enchantments/bats-extra.bash new file mode 100644 index 00000000..7cef63ea --- /dev/null +++ b/exercises/concept/elyses-enchantments/bats-extra.bash @@ -0,0 +1,642 @@ +# This is the source code for bats-support and bats-assert, concatenated +# * https://github.com/bats-core/bats-support +# * https://github.com/bats-core/bats-assert +# +# Comments have been removed to save space. See the git repos for full source code. + +############################################################ +# +# bats-support - Supporting library for Bats test helpers +# +# Written in 2016 by Zoltan Tombol +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. +# +# You should have received a copy of the CC0 Public Domain Dedication +# along with this software. If not, see +# . +# + +# shellcheck disable=SC2120 # (warning): fail references arguments, but none are ever passed. +fail() { + # shellcheck disable=2015 # (info): Note that A && B || C is not if-then-else. C may run when A is true. + (( $# == 0 )) && batslib_err || batslib_err "$@" + return 1 +} + +batslib_is_caller() { + local -i is_mode_direct=1 + + # Handle options. + while (( $# > 0 )); do + case "$1" in + -i|--indirect) is_mode_direct=0; shift ;; + --) shift; break ;; + *) break ;; + esac + done + + # Arguments. + local -r func="$1" + + # Check call stack. + if (( is_mode_direct )); then + [[ $func == "${FUNCNAME[2]}" ]] && return 0 + else + local -i depth + for (( depth=2; depth<${#FUNCNAME[@]}; ++depth )); do + [[ $func == "${FUNCNAME[$depth]}" ]] && return 0 + done + fi + + return 1 +} + +batslib_err() { + { if (( $# > 0 )); then + echo "$@" + else + cat - + fi + } >&2 +} + +batslib_count_lines() { + local -i n_lines=0 + local line + while IFS='' read -r line || [[ -n $line ]]; do + (( ++n_lines )) + done < <(printf '%s' "$1") + echo "$n_lines" +} + +batslib_is_single_line() { + for string in "$@"; do + (( $(batslib_count_lines "$string") > 1 )) && return 1 + done + return 0 +} + +batslib_get_max_single_line_key_width() { + local -i max_len=-1 + while (( $# != 0 )); do + local -i key_len="${#1}" + batslib_is_single_line "$2" && (( key_len > max_len )) && max_len="$key_len" + shift 2 + done + echo "$max_len" +} + +batslib_print_kv_single() { + local -ir col_width="$1"; shift + while (( $# != 0 )); do + printf '%-*s : %s\n' "$col_width" "$1" "$2" + shift 2 + done +} + +batslib_print_kv_multi() { + while (( $# != 0 )); do + printf '%s (%d lines):\n' "$1" "$( batslib_count_lines "$2" )" + printf '%s\n' "$2" + shift 2 + done +} + +batslib_print_kv_single_or_multi() { + local -ir width="$1"; shift + local -a pairs=( "$@" ) + + local -a values=() + local -i i + for (( i=1; i < ${#pairs[@]}; i+=2 )); do + values+=( "${pairs[$i]}" ) + done + + if batslib_is_single_line "${values[@]}"; then + batslib_print_kv_single "$width" "${pairs[@]}" + else + local -i i + for (( i=1; i < ${#pairs[@]}; i+=2 )); do + pairs[i]="$( batslib_prefix < <(printf '%s' "${pairs[$i]}") )" + done + batslib_print_kv_multi "${pairs[@]}" + fi +} + +batslib_prefix() { + local -r prefix="${1:- }" + local line + while IFS='' read -r line || [[ -n $line ]]; do + printf '%s%s\n' "$prefix" "$line" + done +} + +batslib_mark() { + local -r symbol="$1"; shift + # Sort line numbers. + # shellcheck disable=SC2046 # (warning): Quote this to prevent word splitting. + set -- $( sort -nu <<< "$( printf '%d\n' "$@" )" ) + + local line + local -i idx=0 + while IFS='' read -r line || [[ -n $line ]]; do + if (( ${1:--1} == idx )); then + printf '%s\n' "${symbol}${line:${#symbol}}" + shift + else + printf '%s\n' "$line" + fi + (( ++idx )) + done +} + +batslib_decorate() { + echo + echo "-- $1 --" + cat - + echo '--' + echo +} + +############################################################ + +assert() { + if ! "$@"; then + batslib_print_kv_single 10 'expression' "$*" \ + | batslib_decorate 'assertion failed' \ + | fail + fi +} + +assert_equal() { + if [[ $1 != "$2" ]]; then + batslib_print_kv_single_or_multi 8 \ + 'expected' "$2" \ + 'actual' "$1" \ + | batslib_decorate 'values do not equal' \ + | fail + fi +} + +assert_failure() { + : "${output?}" + : "${status?}" + + (( $# > 0 )) && local -r expected="$1" + if (( status == 0 )); then + batslib_print_kv_single_or_multi 6 'output' "$output" \ + | batslib_decorate 'command succeeded, but it was expected to fail' \ + | fail + elif (( $# > 0 )) && (( status != expected )); then + { local -ir width=8 + batslib_print_kv_single "$width" \ + 'expected' "$expected" \ + 'actual' "$status" + batslib_print_kv_single_or_multi "$width" \ + 'output' "$output" + } \ + | batslib_decorate 'command failed as expected, but status differs' \ + | fail + fi +} + +assert_line() { + local -i is_match_line=0 + local -i is_mode_partial=0 + local -i is_mode_regexp=0 + : "${lines?}" + + # Handle options. + while (( $# > 0 )); do + case "$1" in + -n|--index) + if (( $# < 2 )) || ! [[ $2 =~ ^([0-9]|[1-9][0-9]+)$ ]]; then + echo "\`--index' requires an integer argument: \`$2'" \ + | batslib_decorate 'ERROR: assert_line' \ + | fail + return $? + fi + is_match_line=1 + local -ri idx="$2" + shift 2 + ;; + -p|--partial) is_mode_partial=1; shift ;; + -e|--regexp) is_mode_regexp=1; shift ;; + --) shift; break ;; + *) break ;; + esac + done + + if (( is_mode_partial )) && (( is_mode_regexp )); then + echo "\`--partial' and \`--regexp' are mutually exclusive" \ + | batslib_decorate 'ERROR: assert_line' \ + | fail + return $? + fi + + # Arguments. + local -r expected="$1" + + if (( is_mode_regexp == 1 )) && [[ '' =~ $expected ]] || (( $? == 2 )); then + echo "Invalid extended regular expression: \`$expected'" \ + | batslib_decorate 'ERROR: assert_line' \ + | fail + return $? + fi + + # Matching. + if (( is_match_line )); then + # Specific line. + if (( is_mode_regexp )); then + if ! [[ ${lines[$idx]} =~ $expected ]]; then + batslib_print_kv_single 6 \ + 'index' "$idx" \ + 'regexp' "$expected" \ + 'line' "${lines[$idx]}" \ + | batslib_decorate 'regular expression does not match line' \ + | fail + fi + elif (( is_mode_partial )); then + if [[ ${lines[$idx]} != *"$expected"* ]]; then + batslib_print_kv_single 9 \ + 'index' "$idx" \ + 'substring' "$expected" \ + 'line' "${lines[$idx]}" \ + | batslib_decorate 'line does not contain substring' \ + | fail + fi + else + if [[ ${lines[$idx]} != "$expected" ]]; then + batslib_print_kv_single 8 \ + 'index' "$idx" \ + 'expected' "$expected" \ + 'actual' "${lines[$idx]}" \ + | batslib_decorate 'line differs' \ + | fail + fi + fi + else + # Contained in output. + if (( is_mode_regexp )); then + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + [[ ${lines[$idx]} =~ $expected ]] && return 0 + done + { local -ar single=( 'regexp' "$expected" ) + local -ar may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + batslib_print_kv_single_or_multi "$width" "${may_be_multi[@]}" + } \ + | batslib_decorate 'no output line matches regular expression' \ + | fail + elif (( is_mode_partial )); then + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + [[ ${lines[$idx]} == *"$expected"* ]] && return 0 + done + { local -ar single=( 'substring' "$expected" ) + local -ar may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + batslib_print_kv_single_or_multi "$width" "${may_be_multi[@]}" + } \ + | batslib_decorate 'no output line contains substring' \ + | fail + else + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + [[ ${lines[$idx]} == "$expected" ]] && return 0 + done + { local -ar single=( 'line' "$expected" ) + local -ar may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + batslib_print_kv_single_or_multi "$width" "${may_be_multi[@]}" + } \ + | batslib_decorate 'output does not contain line' \ + | fail + fi + fi +} + +assert_output() { + local -i is_mode_partial=0 + local -i is_mode_regexp=0 + local -i is_mode_nonempty=0 + local -i use_stdin=0 + : "${output?}" + + # Handle options. + if (( $# == 0 )); then + is_mode_nonempty=1 + fi + + while (( $# > 0 )); do + case "$1" in + -p|--partial) is_mode_partial=1; shift ;; + -e|--regexp) is_mode_regexp=1; shift ;; + -|--stdin) use_stdin=1; shift ;; + --) shift; break ;; + *) break ;; + esac + done + + if (( is_mode_partial )) && (( is_mode_regexp )); then + echo "\`--partial' and \`--regexp' are mutually exclusive" \ + | batslib_decorate 'ERROR: assert_output' \ + | fail + return $? + fi + + # Arguments. + local expected + if (( use_stdin )); then + expected="$(cat -)" + else + expected="${1-}" + fi + + # Matching. + if (( is_mode_nonempty )); then + if [ -z "$output" ]; then + echo 'expected non-empty output, but output was empty' \ + | batslib_decorate 'no output' \ + | fail + fi + elif (( is_mode_regexp )); then + # shellcheck disable=SC2319 # (warning) `$?` in a conditional expression: https://github.com/koalaman/shellcheck/wiki/SC2319 + if [[ '' =~ $expected ]] || (( $? == 2 )); then + # the regex matches an empty string or it is syntactically incorrect. + echo "Invalid extended regular expression: \`$expected'" \ + | batslib_decorate 'ERROR: assert_output' \ + | fail + elif ! [[ $output =~ $expected ]]; then + batslib_print_kv_single_or_multi 6 \ + 'regexp' "$expected" \ + 'output' "$output" \ + | batslib_decorate 'regular expression does not match output' \ + | fail + fi + elif (( is_mode_partial )); then + if [[ $output != *"$expected"* ]]; then + batslib_print_kv_single_or_multi 9 \ + 'substring' "$expected" \ + 'output' "$output" \ + | batslib_decorate 'output does not contain substring' \ + | fail + fi + else + if [[ $output != "$expected" ]]; then + batslib_print_kv_single_or_multi 8 \ + 'expected' "$expected" \ + 'actual' "$output" \ + | batslib_decorate 'output differs' \ + | fail + fi + fi +} + +assert_success() { + : "${output?}" + : "${status?}" + + if (( status != 0 )); then + { local -ir width=6 + batslib_print_kv_single "$width" 'status' "$status" + batslib_print_kv_single_or_multi "$width" 'output' "$output" + } \ + | batslib_decorate 'command failed' \ + | fail + fi +} + +refute() { + if "$@"; then + batslib_print_kv_single 10 'expression' "$*" \ + | batslib_decorate 'assertion succeeded, but it was expected to fail' \ + | fail + fi +} + +refute_line() { + local -i is_match_line=0 + local -i is_mode_partial=0 + local -i is_mode_regexp=0 + : "${lines?}" + + # Handle options. + while (( $# > 0 )); do + case "$1" in + -n|--index) + if (( $# < 2 )) || ! [[ $2 =~ ^([0-9]|[1-9][0-9]+)$ ]]; then + echo "\`--index' requires an integer argument: \`$2'" \ + | batslib_decorate 'ERROR: refute_line' \ + | fail + return $? + fi + is_match_line=1 + local -ri idx="$2" + shift 2 + ;; + -p|--partial) is_mode_partial=1; shift ;; + -e|--regexp) is_mode_regexp=1; shift ;; + --) shift; break ;; + *) break ;; + esac + done + + if (( is_mode_partial )) && (( is_mode_regexp )); then + echo "\`--partial' and \`--regexp' are mutually exclusive" \ + | batslib_decorate 'ERROR: refute_line' \ + | fail + return $? + fi + + # Arguments. + local -r unexpected="$1" + + if (( is_mode_regexp == 1 )) && [[ '' =~ $unexpected ]] || (( $? == 2 )); then + echo "Invalid extended regular expression: \`$unexpected'" \ + | batslib_decorate 'ERROR: refute_line' \ + | fail + return $? + fi + + # Matching. + if (( is_match_line )); then + # Specific line. + if (( is_mode_regexp )); then + if [[ ${lines[$idx]} =~ $unexpected ]]; then + batslib_print_kv_single 6 \ + 'index' "$idx" \ + 'regexp' "$unexpected" \ + 'line' "${lines[$idx]}" \ + | batslib_decorate 'regular expression should not match line' \ + | fail + fi + elif (( is_mode_partial )); then + if [[ ${lines[$idx]} == *"$unexpected"* ]]; then + batslib_print_kv_single 9 \ + 'index' "$idx" \ + 'substring' "$unexpected" \ + 'line' "${lines[$idx]}" \ + | batslib_decorate 'line should not contain substring' \ + | fail + fi + else + if [[ ${lines[$idx]} == "$unexpected" ]]; then + batslib_print_kv_single 5 \ + 'index' "$idx" \ + 'line' "${lines[$idx]}" \ + | batslib_decorate 'line should differ' \ + | fail + fi + fi + else + # Line contained in output. + if (( is_mode_regexp )); then + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + if [[ ${lines[$idx]} =~ $unexpected ]]; then + { local -ar single=( 'regexp' "$unexpected" 'index' "$idx" ) + local -a may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + if batslib_is_single_line "${may_be_multi[1]}"; then + batslib_print_kv_single "$width" "${may_be_multi[@]}" + else + may_be_multi[1]="$( printf '%s' "${may_be_multi[1]}" | batslib_prefix | batslib_mark '>' "$idx" )" + batslib_print_kv_multi "${may_be_multi[@]}" + fi + } \ + | batslib_decorate 'no line should match the regular expression' \ + | fail + return $? + fi + done + elif (( is_mode_partial )); then + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + if [[ ${lines[$idx]} == *"$unexpected"* ]]; then + { local -ar single=( 'substring' "$unexpected" 'index' "$idx" ) + local -a may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + if batslib_is_single_line "${may_be_multi[1]}"; then + batslib_print_kv_single "$width" "${may_be_multi[@]}" + else + may_be_multi[1]="$( printf '%s' "${may_be_multi[1]}" | batslib_prefix | batslib_mark '>' "$idx" )" + batslib_print_kv_multi "${may_be_multi[@]}" + fi + } \ + | batslib_decorate 'no line should contain substring' \ + | fail + return $? + fi + done + else + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + if [[ ${lines[$idx]} == "$unexpected" ]]; then + { local -ar single=( 'line' "$unexpected" 'index' "$idx" ) + local -a may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + if batslib_is_single_line "${may_be_multi[1]}"; then + batslib_print_kv_single "$width" "${may_be_multi[@]}" + else + may_be_multi[1]="$( printf '%s' "${may_be_multi[1]}" | batslib_prefix | batslib_mark '>' "$idx" )" + batslib_print_kv_multi "${may_be_multi[@]}" + fi + } \ + | batslib_decorate 'line should not be in output' \ + | fail + return $? + fi + done + fi + fi +} + +refute_output() { + local -i is_mode_partial=0 + local -i is_mode_regexp=0 + local -i is_mode_empty=0 + local -i use_stdin=0 + : "${output?}" + + # Handle options. + if (( $# == 0 )); then + is_mode_empty=1 + fi + + while (( $# > 0 )); do + case "$1" in + -p|--partial) is_mode_partial=1; shift ;; + -e|--regexp) is_mode_regexp=1; shift ;; + -|--stdin) use_stdin=1; shift ;; + --) shift; break ;; + *) break ;; + esac + done + + if (( is_mode_partial )) && (( is_mode_regexp )); then + echo "\`--partial' and \`--regexp' are mutually exclusive" \ + | batslib_decorate 'ERROR: refute_output' \ + | fail + return $? + fi + + # Arguments. + local unexpected + if (( use_stdin )); then + unexpected="$(cat -)" + else + unexpected="${1-}" + fi + + if (( is_mode_regexp == 1 )) && [[ '' =~ $unexpected ]] || (( $? == 2 )); then + echo "Invalid extended regular expression: \`$unexpected'" \ + | batslib_decorate 'ERROR: refute_output' \ + | fail + return $? + fi + + # Matching. + if (( is_mode_empty )); then + if [ -n "$output" ]; then + batslib_print_kv_single_or_multi 6 \ + 'output' "$output" \ + | batslib_decorate 'output non-empty, but expected no output' \ + | fail + fi + elif (( is_mode_regexp )); then + if [[ $output =~ $unexpected ]]; then + batslib_print_kv_single_or_multi 6 \ + 'regexp' "$unexpected" \ + 'output' "$output" \ + | batslib_decorate 'regular expression should not match output' \ + | fail + fi + elif (( is_mode_partial )); then + if [[ $output == *"$unexpected"* ]]; then + batslib_print_kv_single_or_multi 9 \ + 'substring' "$unexpected" \ + 'output' "$output" \ + | batslib_decorate 'output should not contain substring' \ + | fail + fi + else + if [[ $output == "$unexpected" ]]; then + batslib_print_kv_single_or_multi 6 \ + 'output' "$output" \ + | batslib_decorate 'output equals, but it was expected to differ' \ + | fail + fi + fi +} diff --git a/exercises/concept/elyses-enchantments/elyses_enchantments.bats b/exercises/concept/elyses-enchantments/elyses_enchantments.bats new file mode 100644 index 00000000..cb931c5e --- /dev/null +++ b/exercises/concept/elyses-enchantments/elyses_enchantments.bats @@ -0,0 +1,173 @@ +#!/usr/bin/env bats +load bats-extra + +# Each test runs one subcommand of the script. +# The cards are given from the top of the stack to the bottom. + +# bats test_tags=task:1 +test_card_at_the_given_position() { # @test + run bash elyses_enchantments.sh get_card 2 3 2 6 4 8 + + assert_success + assert_output '6' +} + +# bats test_tags=task:1 +test_card_at_the_top_position() { # @test + run bash elyses_enchantments.sh get_card 0 3 2 6 4 8 + + assert_success + assert_output '3' +} + +# bats test_tags=task:1 +test_card_at_the_bottom_position() { # @test + run bash elyses_enchantments.sh get_card 4 3 2 6 4 8 + + assert_success + assert_output '8' +} + +# bats test_tags=task:1 +test_position_beyond_the_bottom_of_the_stack() { # @test + run bash elyses_enchantments.sh get_card 7 3 2 6 4 8 + + assert_success + refute_output +} + +# bats test_tags=task:2 +test_exchanging_the_card_at_the_given_position() { # @test + run bash elyses_enchantments.sh set_card 2 9 3 2 6 4 8 + + assert_success + assert_output '3 2 9 4 8' +} + +# bats test_tags=task:2 +test_exchanging_the_top_card() { # @test + run bash elyses_enchantments.sh set_card 0 9 3 2 6 4 8 + + assert_success + assert_output '9 2 6 4 8' +} + +# bats test_tags=task:2 +test_exchanging_the_bottom_card() { # @test + run bash elyses_enchantments.sh set_card 4 9 3 2 6 4 8 + + assert_success + assert_output '3 2 6 4 9' +} + +# bats test_tags=task:3 +test_inserting_a_card_at_the_top() { # @test + run bash elyses_enchantments.sh insert_at_top 9 3 2 6 4 8 + + assert_success + assert_output '3 2 6 4 8 9' +} + +# bats test_tags=task:3 +test_inserting_a_card_at_the_top_of_a_single_card_stack() { # @test + run bash elyses_enchantments.sh insert_at_top 9 5 + + assert_success + assert_output '5 9' +} + +# bats test_tags=task:4 +test_removing_the_card_at_the_given_position() { # @test + run bash elyses_enchantments.sh remove_card 2 3 2 6 4 8 + + assert_success + assert_output '3 2 4 8' +} + +# bats test_tags=task:4 +test_removing_the_top_card_by_position() { # @test + run bash elyses_enchantments.sh remove_card 0 3 2 6 4 8 + + assert_success + assert_output '2 6 4 8' +} + +# bats test_tags=task:5 +test_removing_the_top_card() { # @test + run bash elyses_enchantments.sh remove_at_top 3 2 6 4 8 + + assert_success + assert_output '3 2 6 4' +} + +# bats test_tags=task:5 +test_removing_the_top_card_of_a_two_card_stack() { # @test + run bash elyses_enchantments.sh remove_at_top 5 7 + + assert_success + assert_output '5' +} + +# bats test_tags=task:5 +test_removing_the_top_card_of_an_empty_stack() { # @test + run bash elyses_enchantments.sh remove_at_top + + assert_success + refute_output +} + +# bats test_tags=task:6 +test_inserting_a_card_at_the_bottom() { # @test + run bash elyses_enchantments.sh insert_at_bottom 9 3 2 6 4 8 + + assert_success + assert_output '9 3 2 6 4 8' +} + +# bats test_tags=task:6 +test_inserting_a_card_at_the_bottom_of_an_empty_stack() { # @test + run bash elyses_enchantments.sh insert_at_bottom 9 + + assert_success + assert_output '9' +} + +# bats test_tags=task:7 +test_removing_the_bottom_card() { # @test + run bash elyses_enchantments.sh remove_at_bottom 3 2 6 4 8 + + assert_success + assert_output '2 6 4 8' +} + +# bats test_tags=task:7 +test_removing_the_bottom_card_of_a_two_card_stack() { # @test + run bash elyses_enchantments.sh remove_at_bottom 5 7 + + assert_success + assert_output '7' +} + +# bats test_tags=task:8 +test_stack_size_matches() { # @test + run bash elyses_enchantments.sh check_size 5 3 2 6 4 8 + + assert_success + assert_output 'true' +} + +# bats test_tags=task:8 +test_stack_size_does_not_match() { # @test + run bash elyses_enchantments.sh check_size 4 3 2 6 4 8 + + assert_success + assert_output 'false' +} + +# bats test_tags=task:8 +test_an_empty_stack_has_size_zero() { # @test + run bash elyses_enchantments.sh check_size 0 + + assert_success + assert_output 'true' +} diff --git a/exercises/concept/elyses-enchantments/elyses_enchantments.sh b/exercises/concept/elyses-enchantments/elyses_enchantments.sh new file mode 100644 index 00000000..5128f6a7 --- /dev/null +++ b/exercises/concept/elyses-enchantments/elyses_enchantments.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2034 # the student completes the case branches below + +# This script helps Elyse practise her card tricks. +# +# It uses a subcommand interface: the first argument names the operation, +# and the remaining arguments are that operation's own inputs. +# The cards of the stack are always the last arguments, ordered from the +# top card to the bottom card. +# +# The `case` structure and the argument handling below are written for you. +# Add your code above the `;;` that ends each branch: the branch's comment +# says what it must print. +# Print only the requested output. + +subcommand=$1 +# `shift` discards $1, so that `"$@"` now holds the operation's own arguments. +shift + +# `cards` holds the stack of cards. +# Operations that take a parameter collect it here first. +parameter= +cards=("$@") + +case ${subcommand} in + get_card) + # Task 1: print the card at the given position. + # $1 the position + # $2 ... the cards in the stack + parameter=$1 + shift + cards=("$@") + # add your code here + ;; + + set_card) + # Task 2: replace the card at the given position, then print the stack. + # $1 the position + # $2 the card to exchange with + # $3 ... the cards in the stack + parameter=$1 + card=$2 + shift 2 + cards=("$@") + # add your code here + ;; + + insert_at_top) + # Task 3: add the given card to the top of the stack, then print the stack. + # $1 the card to insert + # $2 ... the cards in the stack + parameter=$1 + shift + cards=("$@") + # add your code here + ;; + + remove_card) + # Task 4: remove the card at the given position, then print the stack. + # $1 the position + # $2 ... the cards in the stack + parameter=$1 + shift + cards=("$@") + # add your code here + ;; + + remove_at_top) + # Task 5: remove the top card, then print the stack. + # $1 ... the cards in the stack + # add your code here + ;; + + insert_at_bottom) + # Task 6: add the given card to the bottom of the stack, then print the stack. + # $1 the card to insert + # $2 ... the cards in the stack + parameter=$1 + shift + cards=("$@") + # add your code here + ;; + + remove_at_bottom) + # Task 7: remove the bottom card, then print the stack. + # $1 ... the cards in the stack + # add your code here + ;; + + check_size) + # Task 8: print "true" if the stack holds the given number of cards, + # and "false" if it does not. + # $1 the expected size of the stack + # $2 ... the cards in the stack + parameter=$1 + shift + cards=("$@") + # add your code here + ;; +esac From 0dd5d8c83e387bb255ba25798416af37493d97b9 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Fri, 4 Dec 2026 13:27:46 -0500 Subject: [PATCH 3/6] undo a rebase decision --- concepts/arrays/about.md | 2 +- concepts/arrays/introduction.md | 2 +- .../elyses-enchantments/.docs/introduction.md | 29 ++++++++++--------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/concepts/arrays/about.md b/concepts/arrays/about.md index 4a31280c..e2692f83 100644 --- a/concepts/arrays/about.md +++ b/concepts/arrays/about.md @@ -15,7 +15,7 @@ There are two kinds of arrays: (Other languages might call this a "map" or a "dictionary".) This document covers numerically-indexed arrays (hereafter simply known as "arrays"). -Associative arrays will be learned later. +Associative arrays will be discussed later. ~~~~exercism/caution Bash arrays can _only_ store scalar values. diff --git a/concepts/arrays/introduction.md b/concepts/arrays/introduction.md index cd037442..24b0ce8e 100644 --- a/concepts/arrays/introduction.md +++ b/concepts/arrays/introduction.md @@ -15,7 +15,7 @@ There are two kinds of arrays: (Other languages might call this a "map" or a "dictionary".) This document covers numerically-indexed arrays (hereafter simply known as "arrays"). -Associative arrays will be learned later. +Associative arrays will be discussed later. ~~~~exercism/caution Bash arrays can _only_ store scalar values. diff --git a/exercises/concept/elyses-enchantments/.docs/introduction.md b/exercises/concept/elyses-enchantments/.docs/introduction.md index 59b9f71b..2645c47f 100644 --- a/exercises/concept/elyses-enchantments/.docs/introduction.md +++ b/exercises/concept/elyses-enchantments/.docs/introduction.md @@ -6,7 +6,7 @@ Bash provides two types of parameters: "scalar" values and arrays. A scalar value can be a string or a number; it represents a single "thing". Most of the time, scalar values are sufficient to contain your data. -But sometimes, you need to store a list of scalar values. +Sometimes, you need to store a list of scalar values. In this case you will use an _array_. There are two kinds of arrays: @@ -17,7 +17,7 @@ There are two kinds of arrays: (Other languages might call this a "map" or a "dictionary".) This document covers numerically-indexed arrays (hereafter simply known as "arrays"). -Associative arrays will be learned later. +Associative arrays will be discussed later. ~~~~exercism/caution Bash arrays can _only_ store scalar values. @@ -49,23 +49,26 @@ myarray=( Assigning a list of strings to an array variable in this manner will store the first string at **index zero**, the next string at index one, the next at index two, and so on. -It is possible to initialize a "sparse array" by specifying the indices you need. +Bash arrays do not need to use consecutive indices. +A "sparse array" is one with gaps in its indices: some positions have a value, and the positions between or before them do not exist at all. +A missing element is not the same as an element holding an empty string. +It is possible to initialize a sparse array by specifying the indices you need. ```bash raindrops=([3]="Pling" [5]="Plang" [7]="Plong") ``` +This array has only three elements, at indices 3, 5 and 7. +There are no elements at indices 0, 1, 2, 4 or 6, so `"${#raindrops[@]}"` (the number of elements) is 3, not 8. + ~~~~exercism/note To reiterate: array indices are zero-based. -This snippet outputs "true": +This snippet outputs "one": ```bash myarray=("one" "two" "three") -if [[ "${myarray[0]}" == "one" ]]; then - echo true -else - echo false -fi +echo "${myarray[0]}" +# => one ``` ~~~~ @@ -79,7 +82,7 @@ It is more idiomatic to just assign a (perhaps empty) parenthesized list to init ### Accessing Elements of an Array -As you saw in the note above, the syntax to access an array element is `${myarray[$index]}`. +As you saw in the note above, the syntax to access an array element is `${myarray[index]}`. The index is given in square brackets. The curly braces are required. @@ -105,7 +108,7 @@ If the given index does not exist in the array, the result of the expansion is * #### Checking whether an Element Exists -To find out whether an element exists, use the `-v` test in a conditional command (see the [Conditionals][conditionals] concept). +To check whether an element exists, use the `-v` test in a conditional command (see the [Conditionals][conditionals] concept). This distinguishes a missing element from an element that holds an empty string. ```bash @@ -161,7 +164,7 @@ To remove the entire array, give `unset` the name without brackets: `unset myarr ### Iterating over an Array You will typically need to iterate over the _values_ of an array. -Use a for loop (that you saw in the [Looping][looping] concept): +Use a for loop (discussed in the [Looping][looping] concept): ```bash myarray=("one" "two" "three") @@ -184,7 +187,7 @@ This special expansion, where the index is an "at sign" (`@`), is **only special ##### Other Special Parameter Expansions -Here are another couple of bits of special syntax that we need for iterating over an array. +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]. To find the _length_ of an array, use `${#myarray[@]}`, with a `#` before the name. From a598f6ee9ac9edbcdb023d86605208374cd36046 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Sat, 5 Dec 2026 07:44:39 -0500 Subject: [PATCH 4/6] make the case branches more consistent --- exercises/concept/elyses-enchantments/.meta/exemplar.sh | 7 ++----- .../concept/elyses-enchantments/elyses_enchantments.sh | 7 ++----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/exercises/concept/elyses-enchantments/.meta/exemplar.sh b/exercises/concept/elyses-enchantments/.meta/exemplar.sh index 7599a966..fbd2aa43 100644 --- a/exercises/concept/elyses-enchantments/.meta/exemplar.sh +++ b/exercises/concept/elyses-enchantments/.meta/exemplar.sh @@ -3,11 +3,6 @@ subcommand=$1 shift -# `cards` holds the stack of cards. -# Operations that take a parameter collect it here first. -parameter= -cards=("$@") - case ${subcommand} in get_card) # Task 1 @@ -47,6 +42,7 @@ case ${subcommand} in remove_at_top) # Task 5 + cards=("$@") size=${#cards[@]} # `unset "cards[-1]"` is an error, so only remove a card when there is one. if (( size > 0 )); then @@ -69,6 +65,7 @@ case ${subcommand} in remove_at_bottom) # Task 7 + cards=("$@") size=${#cards[@]} new_cards=() for (( index = 1; index < size; index++ )); do diff --git a/exercises/concept/elyses-enchantments/elyses_enchantments.sh b/exercises/concept/elyses-enchantments/elyses_enchantments.sh index 5128f6a7..687f359e 100644 --- a/exercises/concept/elyses-enchantments/elyses_enchantments.sh +++ b/exercises/concept/elyses-enchantments/elyses_enchantments.sh @@ -17,11 +17,6 @@ subcommand=$1 # `shift` discards $1, so that `"$@"` now holds the operation's own arguments. shift -# `cards` holds the stack of cards. -# Operations that take a parameter collect it here first. -parameter= -cards=("$@") - case ${subcommand} in get_card) # Task 1: print the card at the given position. @@ -68,6 +63,7 @@ case ${subcommand} in remove_at_top) # Task 5: remove the top card, then print the stack. # $1 ... the cards in the stack + cards=("$@") # add your code here ;; @@ -84,6 +80,7 @@ case ${subcommand} in remove_at_bottom) # Task 7: remove the bottom card, then print the stack. # $1 ... the cards in the stack + cards=("$@") # add your code here ;; From f8b50bc3151575cba78babef32deb67dbd35b17e Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Sat, 5 Dec 2026 07:59:32 -0500 Subject: [PATCH 5/6] Fix review findings in the arrays concept exercise - Describe the top of the stack consistently as the end of the array - Make the two empty-output tests fail against the unsolved stub - State what to print for a position with no card - Give away less of the solution in the hints - Fix a heading level and typos in the arrays concept docs Co-Authored-By: Claude Sonnet 5.5 --- concepts/arrays/about.md | 6 ++-- concepts/arrays/introduction.md | 6 ++-- .../elyses-enchantments/.docs/hints.md | 25 ++++++---------- .../elyses-enchantments/.docs/instructions.md | 7 +++-- .../elyses-enchantments/.docs/introduction.md | 6 ++-- .../elyses_enchantments.bats | 29 ++++++++++++++----- .../elyses_enchantments.sh | 2 +- 7 files changed, 45 insertions(+), 36 deletions(-) diff --git a/concepts/arrays/about.md b/concepts/arrays/about.md index e2692f83..a1612645 100644 --- a/concepts/arrays/about.md +++ b/concepts/arrays/about.md @@ -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") @@ -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") @@ -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]. diff --git a/concepts/arrays/introduction.md b/concepts/arrays/introduction.md index 24b0ce8e..47ddaaff 100644 --- a/concepts/arrays/introduction.md +++ b/concepts/arrays/introduction.md @@ -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") @@ -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") @@ -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]. diff --git a/exercises/concept/elyses-enchantments/.docs/hints.md b/exercises/concept/elyses-enchantments/.docs/hints.md index b7db3bab..547c4372 100644 --- a/exercises/concept/elyses-enchantments/.docs/hints.md +++ b/exercises/concept/elyses-enchantments/.docs/hints.md @@ -8,7 +8,6 @@ `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. @@ -39,28 +38,22 @@ ## 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. +- 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. -- 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[@]}"`. +- 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 -- 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 - ``` +- 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 diff --git a/exercises/concept/elyses-enchantments/.docs/instructions.md b/exercises/concept/elyses-enchantments/.docs/instructions.md index 9382c009..42668549 100644 --- a/exercises/concept/elyses-enchantments/.docs/instructions.md +++ b/exercises/concept/elyses-enchantments/.docs/instructions.md @@ -4,7 +4,7 @@ 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. +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. @@ -15,12 +15,13 @@ The first argument names the operation to perform, and the remaining arguments a 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. +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. diff --git a/exercises/concept/elyses-enchantments/.docs/introduction.md b/exercises/concept/elyses-enchantments/.docs/introduction.md index 2645c47f..091ef931 100644 --- a/exercises/concept/elyses-enchantments/.docs/introduction.md +++ b/exercises/concept/elyses-enchantments/.docs/introduction.md @@ -26,7 +26,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") @@ -137,7 +137,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") @@ -185,7 +185,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]. diff --git a/exercises/concept/elyses-enchantments/elyses_enchantments.bats b/exercises/concept/elyses-enchantments/elyses_enchantments.bats index cb931c5e..4a5d3a6d 100644 --- a/exercises/concept/elyses-enchantments/elyses_enchantments.bats +++ b/exercises/concept/elyses-enchantments/elyses_enchantments.bats @@ -2,7 +2,7 @@ load bats-extra # Each test runs one subcommand of the script. -# The cards are given from the top of the stack to the bottom. +# The cards are given from the bottom of the stack to the top. # bats test_tags=task:1 test_card_at_the_given_position() { # @test @@ -13,7 +13,7 @@ test_card_at_the_given_position() { # @test } # bats test_tags=task:1 -test_card_at_the_top_position() { # @test +test_card_at_the_bottom_position() { # @test run bash elyses_enchantments.sh get_card 0 3 2 6 4 8 assert_success @@ -21,7 +21,7 @@ test_card_at_the_top_position() { # @test } # bats test_tags=task:1 -test_card_at_the_bottom_position() { # @test +test_card_at_the_top_position() { # @test run bash elyses_enchantments.sh get_card 4 3 2 6 4 8 assert_success @@ -29,7 +29,15 @@ test_card_at_the_bottom_position() { # @test } # bats test_tags=task:1 -test_position_beyond_the_bottom_of_the_stack() { # @test +test_position_beyond_the_top_of_the_stack() { # @test + # add a "positive test" so the "negative test" does not + # inadvertently pass with an unimplemented solution. + + run bash elyses_enchantments.sh get_card 4 3 2 6 4 8 + + assert_success + assert_output '8' + run bash elyses_enchantments.sh get_card 7 3 2 6 4 8 assert_success @@ -45,7 +53,7 @@ test_exchanging_the_card_at_the_given_position() { # @test } # bats test_tags=task:2 -test_exchanging_the_top_card() { # @test +test_exchanging_the_bottom_card() { # @test run bash elyses_enchantments.sh set_card 0 9 3 2 6 4 8 assert_success @@ -53,7 +61,7 @@ test_exchanging_the_top_card() { # @test } # bats test_tags=task:2 -test_exchanging_the_bottom_card() { # @test +test_exchanging_the_top_card() { # @test run bash elyses_enchantments.sh set_card 4 9 3 2 6 4 8 assert_success @@ -85,7 +93,7 @@ test_removing_the_card_at_the_given_position() { # @test } # bats test_tags=task:4 -test_removing_the_top_card_by_position() { # @test +test_removing_the_bottom_card_by_position() { # @test run bash elyses_enchantments.sh remove_card 0 3 2 6 4 8 assert_success @@ -110,6 +118,13 @@ test_removing_the_top_card_of_a_two_card_stack() { # @test # bats test_tags=task:5 test_removing_the_top_card_of_an_empty_stack() { # @test + # again, a positive test before a negative test + + run bash elyses_enchantments.sh remove_at_top 5 7 + + assert_success + assert_output '5' + run bash elyses_enchantments.sh remove_at_top assert_success diff --git a/exercises/concept/elyses-enchantments/elyses_enchantments.sh b/exercises/concept/elyses-enchantments/elyses_enchantments.sh index 687f359e..7f13da3c 100644 --- a/exercises/concept/elyses-enchantments/elyses_enchantments.sh +++ b/exercises/concept/elyses-enchantments/elyses_enchantments.sh @@ -6,7 +6,7 @@ # It uses a subcommand interface: the first argument names the operation, # and the remaining arguments are that operation's own inputs. # The cards of the stack are always the last arguments, ordered from the -# top card to the bottom card. +# bottom card to the top card. # # The `case` structure and the argument handling below are written for you. # Add your code above the `;;` that ends each branch: the branch's comment From 328f03acbe39f2f7e2017e4260d58ffe23115e6f Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Sat, 5 Dec 2026 13:43:39 -0500 Subject: [PATCH 6/6] Address review comments on Elyses Enchantments Name the case branch variables for what they hold instead of "parameter", and drop the empty-stack note from the instructions since the hints already cover it. Co-Authored-By: Claude Sonnet 5.5 --- .../elyses-enchantments/.docs/hints.md | 8 +++---- .../elyses-enchantments/.docs/instructions.md | 5 ---- .../elyses-enchantments/.meta/exemplar.sh | 24 +++++++++---------- .../elyses_enchantments.sh | 12 +++++----- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/exercises/concept/elyses-enchantments/.docs/hints.md b/exercises/concept/elyses-enchantments/.docs/hints.md index 547c4372..57fabbb1 100644 --- a/exercises/concept/elyses-enchantments/.docs/hints.md +++ b/exercises/concept/elyses-enchantments/.docs/hints.md @@ -14,23 +14,23 @@ ## 1. Retrieve a card from a stack -- Access an element with `${cards[parameter]}`. +- 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[parameter]=$card`. +- 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+=("$parameter")`. +- Append with the `+=` operator and parentheses: `cards+=("$new_card")`. ## 4. Remove a card from the stack -- Use the [`unset` builtin][builtins]: `unset "cards[parameter]"`. +- 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. diff --git a/exercises/concept/elyses-enchantments/.docs/instructions.md b/exercises/concept/elyses-enchantments/.docs/instructions.md index 42668549..fea25d24 100644 --- a/exercises/concept/elyses-enchantments/.docs/instructions.md +++ b/exercises/concept/elyses-enchantments/.docs/instructions.md @@ -88,11 +88,6 @@ bash elyses_enchantments.sh remove_card 2 3 2 6 4 8 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 diff --git a/exercises/concept/elyses-enchantments/.meta/exemplar.sh b/exercises/concept/elyses-enchantments/.meta/exemplar.sh index fbd2aa43..7a2479cc 100644 --- a/exercises/concept/elyses-enchantments/.meta/exemplar.sh +++ b/exercises/concept/elyses-enchantments/.meta/exemplar.sh @@ -6,37 +6,37 @@ shift case ${subcommand} in get_card) # Task 1 - parameter=$1 + index=$1 shift cards=("$@") - echo "${cards[parameter]}" + echo "${cards[index]}" ;; set_card) # Task 2 - parameter=$1 + index=$1 card=$2 shift 2 cards=("$@") - cards[parameter]=$card + cards[index]=$card echo "${cards[@]}" ;; insert_at_top) # Task 3 - parameter=$1 + new_card=$1 shift cards=("$@") - cards+=("$parameter") + cards+=("$new_card") echo "${cards[@]}" ;; remove_card) # Task 4 - parameter=$1 + index=$1 shift cards=("$@") - unset "cards[parameter]" + unset "cards[index]" echo "${cards[@]}" ;; @@ -53,10 +53,10 @@ case ${subcommand} in insert_at_bottom) # Task 6 - parameter=$1 + new_card=$1 shift cards=("$@") - new_cards=("$parameter") + new_cards=("$new_card") for card in "${cards[@]}"; do new_cards+=("$card") done @@ -76,10 +76,10 @@ case ${subcommand} in check_size) # Task 8 - parameter=$1 + size=$1 shift cards=("$@") - if (( ${#cards[@]} == parameter )); then + if (( ${#cards[@]} == size )); then echo true else echo false diff --git a/exercises/concept/elyses-enchantments/elyses_enchantments.sh b/exercises/concept/elyses-enchantments/elyses_enchantments.sh index 7f13da3c..0653bef4 100644 --- a/exercises/concept/elyses-enchantments/elyses_enchantments.sh +++ b/exercises/concept/elyses-enchantments/elyses_enchantments.sh @@ -22,7 +22,7 @@ case ${subcommand} in # Task 1: print the card at the given position. # $1 the position # $2 ... the cards in the stack - parameter=$1 + index=$1 shift cards=("$@") # add your code here @@ -33,7 +33,7 @@ case ${subcommand} in # $1 the position # $2 the card to exchange with # $3 ... the cards in the stack - parameter=$1 + index=$1 card=$2 shift 2 cards=("$@") @@ -44,7 +44,7 @@ case ${subcommand} in # Task 3: add the given card to the top of the stack, then print the stack. # $1 the card to insert # $2 ... the cards in the stack - parameter=$1 + new_card=$1 shift cards=("$@") # add your code here @@ -54,7 +54,7 @@ case ${subcommand} in # Task 4: remove the card at the given position, then print the stack. # $1 the position # $2 ... the cards in the stack - parameter=$1 + index=$1 shift cards=("$@") # add your code here @@ -71,7 +71,7 @@ case ${subcommand} in # Task 6: add the given card to the bottom of the stack, then print the stack. # $1 the card to insert # $2 ... the cards in the stack - parameter=$1 + new_card=$1 shift cards=("$@") # add your code here @@ -89,7 +89,7 @@ case ${subcommand} in # and "false" if it does not. # $1 the expected size of the stack # $2 ... the cards in the stack - parameter=$1 + size=$1 shift cards=("$@") # add your code here