From ff5a34ae986a1cf6a45837939890e70d1b2bca13 Mon Sep 17 00:00:00 2001 From: Carl Date: Thu, 2 Nov 2023 13:41:48 +0100 Subject: [PATCH 1/5] Started --- concepts/arrays/.meta/config.json | 3 +- concepts/arrays/about.md | 179 ++++++++++++++--- concepts/arrays/introduction.md | 182 +++++++++++++++--- concepts/arrays/links.json | 2 +- .../.docs/instructions.md | 85 ++++---- .../.docs/introduction.md | 182 +++++++++++++++--- .../MagicianInTrainingExemplar.swift | 39 ++-- .../magician-in-training/.meta/config.json | 3 +- .../magician-in-training/Package.swift | 42 ++-- .../MagicianInTraining.swift | 16 +- .../Tests/LinuxMain.swift | 6 - .../Tests/MagicianInTrainingTests.swift | 135 +++++++++++++ .../MagicianInTrainingTests.swift | 135 ------------- .../XCTestManifests.swift | 9 - 14 files changed, 672 insertions(+), 346 deletions(-) delete mode 100644 exercises/concept/magician-in-training/Tests/LinuxMain.swift create mode 100644 exercises/concept/magician-in-training/Tests/MagicianInTrainingTests.swift delete mode 100644 exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/MagicianInTrainingTests.swift delete mode 100644 exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/XCTestManifests.swift diff --git a/concepts/arrays/.meta/config.json b/concepts/arrays/.meta/config.json index 9e4e1a395..bfbbdacd4 100644 --- a/concepts/arrays/.meta/config.json +++ b/concepts/arrays/.meta/config.json @@ -1,7 +1,8 @@ { "blurb": "Arrays are a collection of multiple values of the same type.", "authors": [ - "wneumann" + "wneumann", + "meatball133" ], "contributors": [] } diff --git a/concepts/arrays/about.md b/concepts/arrays/about.md index 0c83b2cdc..4a8422995 100644 --- a/concepts/arrays/about.md +++ b/concepts/arrays/about.md @@ -1,54 +1,173 @@ # About -[Arrays][array] are one of Swift's three primary collection types. Arrays are ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type. +[Arrays][array] are one of Swift's three primary collection types. +Arrays are ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type. +Arrays are mutable when assigned to a variable, meaning that the elements of an array can be modified after the array is created. +This is not the case when an array is assigned to a constant, in which case the array is immutable. -Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. Empty arrays are just a pair of square brackets. Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in thee array. When creating an empty array, the type must be specified. +Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. +Swift will infer the type of the array from the types of the elements in the array literal. + +```swift +let evenInts = [2, 4, 6, 8, 10, 12] +var oddInts = [1, 3, 5, 7, 9, 11, 13] +let greetings = ["Hello!", "Hi!", "¡Hola!"] +``` + +Arrays can also be explicitly typed by specifying the type of the elements in the array. +Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in thee array. ```swift let evenInts: Array = [2, 4, 6, 8, 10, 12] var oddInts: [Int] = [1, 3, 5, 7, 9, 11, 13] -let greetings = ["Hello!", "Hi!", "¡Hola!"] -var myStringArray: [String] = [] +let greetings : [String] = ["Hello!", "Hi!", "¡Hola!"] +``` + +## Size of an Array + +The number of elements in an array can be determined using the [`count`][count] property. + +```swift +evenInts.count +// returns 6 +``` + +## Empty Arrays + +When wanting an empty array, the type must be specified. +This can be done by using either the array initializer syntax or by using the type annotation syntax. + +```swift +let emptyArray = [Int]() +let emptyArray2 = Array() +let emptyArray3: [Int] = [] +``` + +## Multi-dimensional Arrays + +Arrays can be nested to create multi-dimensional arrays. +When explicitly typing a multi-dimensional array, the type of the elements of the innermost array must be specified, using: `Array>` or `[[T]]`. + +```swift +let multiDimArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] +let multiDimArray2: [[Int]] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ``` -Elements of an array can be accessed individually by supplying the index of the element inside square brackets following the array; array indices are `Int`s and start with `0` for the first (leftmost) element. This subscript notation can be used to get the element at that index as well as to set the element at that index, provided the array was defined as a variable (i.e. using `var`). +## Append to an Array -Trying to access elements at indices outside the valid range of indices will result in a runtime error that crashes the program. Since any invalid array index access will crash a program, it is common to test arrays to see if the are empty before working with them by checking the `isEmpty` property or checking if an index is valid by ensuring that it is greater than or equal to 0 and less than the array's `count` property. +Elements can be appended to the end of an array using the [`append(_:)`][append] method. +The `append(_:)` method takes a single argument, the element to be appended to the array. ```swift -guard !evenInts.isEmpty, !oddInts.isEmpty else { return } +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.append(15) +// oddInts is now [1, 3, 5, 7, 9, 11, 13, 15] +``` + +## Insert into an Array + +Elements can be inserted into an array using the [`insert(_:at:)`][insert] method. +The `insert(_:at:)` method takes two arguments, the element to be inserted into the array and the index at which the element should be inserted. + +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.insert(0, at: 0) +// oddInts is now [0, 1, 3, 5, 7, 9, 11, 13] +``` + +## Add an Array to an Array + +Array can be added to the end of an array using the `+` operator. +It is important to note that the `+` operator creates a new array and does not modify the original array, which is different from the `append(_:)` or `insert(_:at:)` methods. + +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts + [15, 17, 19] +// returns [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] + +print(oddInts) +// prints [1, 3, 5, 7, 9, 11, 13] +``` + +## Accessing Elements of an Array + +Elements of an array can be accessed individually by supplying the index of the element inside square brackets following the array. +The index of an element is an `Int` and starts with `0` for the first (leftmost) element. +If the index is outside the valid range of indices, a runtime error will occur and the program will crash. + +```swift +let evenInts = [2, 4, 6, 8, 10, 12] +let oddInts = [1, 3, 5, 7, 9, 11, 13] + evenInts[2] -// => 6 -oddInts[0] = 27 -// oddInts is now [27, 3, 5, 7, 9, 11, 13] +// returns 6 -// these operations are not allowed -greetings[3] -// error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). -evenInts[1] = 0 -// Cannot assign through subscript: 'evenInts' is a 'let' constant +oddInts[7] +// Fatal error: Index out of range ``` -Arrays in Swift are not fixed size (though constant arrays, defined using `let` cannot be modified, including adding and removing elements). Elements can quickly be appended or dropped from the end of an array, and elements can be inserted or removed at any other location, though these operations are slower. The entire contents of another array can also be inserted at a given position in the original array. +## Modifying Elements of an Array -The elements of an array can be stepped through one at a time using a for-in loop. This type of loop takes each element of the array, in order, and binds the element to a specified name for further processing inside the loop body. For example, to print out all of the odd integers in an array one can write: +Elements of an array can be modified by assigning a new value to the element at a given index. +The index of an element is an `Int` and starts with `0` for the first (leftmost) element. +If the index is outside the valid range of indices, a runtime error will occur and the program will crash. ```swift -let ints = [1, 3, 6, 14, 17, 8, 23, 5, 18, 11] +var evenInts = [2, 4, 6, 8, 10, 12] -for int in ints { - if !int.isMultiple(of: 2) { - print(int) - } -} +evenInts[2] = 0 +// evenInts is now [2, 4, 0, 8, 10, 12] +``` -// prints out: -// 1 -// 3 -// 17 -// 23 -// 5 -// 11 +## Converting an Array to a String and Back + +An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method. +The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array. +The separator must be a string. + +```swift +let evenInts = ["2", "4", "6", "8", "10", "12"] +let evenIntsString = evenInts.joined(separator: ", ") +// returns "2, 4, 6, 8, 10, 12" +``` + +An array can be converted from a string using the [`split(separator:)`][split] method. +The `split(separator:)` method takes a single argument, the separator to be used between elements of the array. + +```swift +let evenIntsString = "2, 4, 6, 8, 10, 12" +let evenInts = evenIntsString.split(separator: ", ") +// returns ["2", "4", "6", "8", "10", "12"] +``` + +## Delete an Element from an Array + +Elements can be deleted from an array using the [`remove(at:)`][remove] method. +The `remove(at:)` method takes a single argument, the index of the element to be removed from the array. +The index of an element is an `Int` and starts with `0` for the first (leftmost) element. +If the index is outside the valid range of indices, a runtime error will occur and the program will crash. + +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.remove(at: 3) +// oddInts is now [1, 3, 5, 9, 11, 13] +``` + +If the last element of an array is to be removed, the [`removeLast()`][removeLast] method can be used. +The `removeLast()` method takes no arguments. +If the array is empty, a runtime error will occur and the program will crash. + +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.removeLast() +// oddInts is now [1, 3, 5, 7, 9, 11] ``` [array]: https://developer.apple.com/documentation/swift/array +[count]: https://developer.apple.com/documentation/swift/array/count +[insert]: https://developer.apple.com/documentation/swift/array/insert(_:at:)-3erb3 +[remove]: https://developer.apple.com/documentation/swift/array/remove(at:)-1p2pj +[removeLast]: https://developer.apple.com/documentation/swift/array/removelast() +[append]: https://developer.apple.com/documentation/swift/array/append(_:)-1ytnt +[joined]: https://developer.apple.com/documentation/swift/array/joined(separator:)-5do1g +[split]: https://developer.apple.com/documentation/swift/string/2894564-split diff --git a/concepts/arrays/introduction.md b/concepts/arrays/introduction.md index 7ad8685ea..4a8422995 100644 --- a/concepts/arrays/introduction.md +++ b/concepts/arrays/introduction.md @@ -1,49 +1,173 @@ -# Introduction +# About -Arrays are one of Swift's three primary collection types. Arrays are ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type. +[Arrays][array] are one of Swift's three primary collection types. +Arrays are ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type. +Arrays are mutable when assigned to a variable, meaning that the elements of an array can be modified after the array is created. +This is not the case when an array is assigned to a constant, in which case the array is immutable. -Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. Empty arrays are just a pair of square brackets. Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in the array. When creating an empty array, the type must be specified. +Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. +Swift will infer the type of the array from the types of the elements in the array literal. + +```swift +let evenInts = [2, 4, 6, 8, 10, 12] +var oddInts = [1, 3, 5, 7, 9, 11, 13] +let greetings = ["Hello!", "Hi!", "¡Hola!"] +``` + +Arrays can also be explicitly typed by specifying the type of the elements in the array. +Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in thee array. ```swift let evenInts: Array = [2, 4, 6, 8, 10, 12] var oddInts: [Int] = [1, 3, 5, 7, 9, 11, 13] -let greetings = ["Hello!", "Hi!", "¡Hola!"] -var myStringArray: [String] = [] +let greetings : [String] = ["Hello!", "Hi!", "¡Hola!"] ``` -Elements of an array can be accessed individually by supplying the index of the element inside square brackets following the array; array indices are `Int`s and start with `0` for the first (leftmost) element. This subscript notation can be used to get the element at that index as well as to set the element at that index, provided the array was defined as a variable (i.e. using `var`). +## Size of an Array -Trying to access elements at indices outside the valid range of indices will result in a runtime error that crashes the program. Since any invalid array index access will crash a program, it is common to test arrays to see if they are empty before working with them by checking the `isEmpty` property or checking if an index is valid by ensuring that it is greater than or equal to 0 and less than the array's `count` property. +The number of elements in an array can be determined using the [`count`][count] property. ```swift +evenInts.count +// returns 6 +``` + +## Empty Arrays + +When wanting an empty array, the type must be specified. +This can be done by using either the array initializer syntax or by using the type annotation syntax. + +```swift +let emptyArray = [Int]() +let emptyArray2 = Array() +let emptyArray3: [Int] = [] +``` + +## Multi-dimensional Arrays + +Arrays can be nested to create multi-dimensional arrays. +When explicitly typing a multi-dimensional array, the type of the elements of the innermost array must be specified, using: `Array>` or `[[T]]`. + +```swift +let multiDimArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] +let multiDimArray2: [[Int]] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] +``` + +## Append to an Array + +Elements can be appended to the end of an array using the [`append(_:)`][append] method. +The `append(_:)` method takes a single argument, the element to be appended to the array. + +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.append(15) +// oddInts is now [1, 3, 5, 7, 9, 11, 13, 15] +``` + +## Insert into an Array + +Elements can be inserted into an array using the [`insert(_:at:)`][insert] method. +The `insert(_:at:)` method takes two arguments, the element to be inserted into the array and the index at which the element should be inserted. + +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.insert(0, at: 0) +// oddInts is now [0, 1, 3, 5, 7, 9, 11, 13] +``` + +## Add an Array to an Array + +Array can be added to the end of an array using the `+` operator. +It is important to note that the `+` operator creates a new array and does not modify the original array, which is different from the `append(_:)` or `insert(_:at:)` methods. + +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts + [15, 17, 19] +// returns [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] + +print(oddInts) +// prints [1, 3, 5, 7, 9, 11, 13] +``` + +## Accessing Elements of an Array + +Elements of an array can be accessed individually by supplying the index of the element inside square brackets following the array. +The index of an element is an `Int` and starts with `0` for the first (leftmost) element. +If the index is outside the valid range of indices, a runtime error will occur and the program will crash. + +```swift +let evenInts = [2, 4, 6, 8, 10, 12] +let oddInts = [1, 3, 5, 7, 9, 11, 13] + evenInts[2] -// => 6 -oddInts[0] = 27 -// oddInts is now [27, 3, 5, 7, 9, 11, 13] +// returns 6 + +oddInts[7] +// Fatal error: Index out of range +``` + +## Modifying Elements of an Array + +Elements of an array can be modified by assigning a new value to the element at a given index. +The index of an element is an `Int` and starts with `0` for the first (leftmost) element. +If the index is outside the valid range of indices, a runtime error will occur and the program will crash. + +```swift +var evenInts = [2, 4, 6, 8, 10, 12] + +evenInts[2] = 0 +// evenInts is now [2, 4, 0, 8, 10, 12] +``` + +## Converting an Array to a String and Back + +An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method. +The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array. +The separator must be a string. + +```swift +let evenInts = ["2", "4", "6", "8", "10", "12"] +let evenIntsString = evenInts.joined(separator: ", ") +// returns "2, 4, 6, 8, 10, 12" +``` + +An array can be converted from a string using the [`split(separator:)`][split] method. +The `split(separator:)` method takes a single argument, the separator to be used between elements of the array. -// these operations are not allowed -greetings[3] -// error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). -evenInts[1] = 0 -// Cannot assign through subscript: 'evenInts' is a 'let' constant +```swift +let evenIntsString = "2, 4, 6, 8, 10, 12" +let evenInts = evenIntsString.split(separator: ", ") +// returns ["2", "4", "6", "8", "10", "12"] ``` -The elements of an array can be stepped through one at a time using a for-in loop. This type of loop takes each element of the array, in order, and binds the element to a specified name for further processing inside the loop body. For example, to print out all of the odd integers in an array one can write: +## Delete an Element from an Array + +Elements can be deleted from an array using the [`remove(at:)`][remove] method. +The `remove(at:)` method takes a single argument, the index of the element to be removed from the array. +The index of an element is an `Int` and starts with `0` for the first (leftmost) element. +If the index is outside the valid range of indices, a runtime error will occur and the program will crash. ```swift -let ints = [1, 3, 6, 14, 17, 8, 23, 5, 18, 11] +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.remove(at: 3) +// oddInts is now [1, 3, 5, 9, 11, 13] +``` -for int in ints { - if !int.isMultiple(of: 2) { - print(int) - } -} +If the last element of an array is to be removed, the [`removeLast()`][removeLast] method can be used. +The `removeLast()` method takes no arguments. +If the array is empty, a runtime error will occur and the program will crash. -// prints out: -// 1 -// 3 -// 17 -// 23 -// 5 -// 11 +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.removeLast() +// oddInts is now [1, 3, 5, 7, 9, 11] ``` + +[array]: https://developer.apple.com/documentation/swift/array +[count]: https://developer.apple.com/documentation/swift/array/count +[insert]: https://developer.apple.com/documentation/swift/array/insert(_:at:)-3erb3 +[remove]: https://developer.apple.com/documentation/swift/array/remove(at:)-1p2pj +[removeLast]: https://developer.apple.com/documentation/swift/array/removelast() +[append]: https://developer.apple.com/documentation/swift/array/append(_:)-1ytnt +[joined]: https://developer.apple.com/documentation/swift/array/joined(separator:)-5do1g +[split]: https://developer.apple.com/documentation/swift/string/2894564-split diff --git a/concepts/arrays/links.json b/concepts/arrays/links.json index 555894303..86dae2a61 100644 --- a/concepts/arrays/links.json +++ b/concepts/arrays/links.json @@ -4,7 +4,7 @@ "description": "Apple Swift Documentation: Array" }, { - "url": "https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html#", + "url": "https://docs.swift.org/swift-book/documentation/the-swift-programming-language/collectiontypes/#Arrays", "description": "The Swift Language Guide: Collection Types" } ] \ No newline at end of file diff --git a/exercises/concept/magician-in-training/.docs/instructions.md b/exercises/concept/magician-in-training/.docs/instructions.md index 8f2acbf44..f23a75d4f 100644 --- a/exercises/concept/magician-in-training/.docs/instructions.md +++ b/exercises/concept/magician-in-training/.docs/instructions.md @@ -1,93 +1,94 @@ # Instructions -As a magician-to-be, Elyse needs to practice some basics. She has a stack of cards that she wants to manipulate. +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. +To make things a bit easier she only uses the cards 1 to 10 so her stack of cards can be represented by an array of numbers. +The position of a certain card corresponds to the index in the array. +That means position 0 refers to the first card, position 1 to the second card etc. + +~~~~exercism/note +All functions should update the array of cards and then return the modified array - a common way of working known as the Builder pattern, which allows you to nicely daisy-chain functions together. +~~~~ ## 1. Retrieve a card from a stack -Implement the function `getCard(at:from:)` that returns the card at position `index` from the given stack. +To pick a card, return the card at index `position` from the given stack. + +Implement the function `getCard(at:from:)` that takes two arguments: `at` which is the position of the card in the stack, and `from` which is the stack of cards. +The function should return the card at position `index` from the given stack. ```swift let index = 2 getCard(at: index, from: [1, 2, 4, 1]) -// => 4 +// returns 4 ``` ## 2. Change a card in the stack -Implement the function `setCard(at:in:to)` that returns a new stack that is a copy of the input stack but which has the card at position `index` changed to the new card provided. If the given `index` is not a valid index in the stack, the original stack should be returned, unchanged. +Perform some sleight of hand and exchange the card at index `position` with the replacement card provided. + +Implement the function `setCard(at:in:to)` that takes three arguments: `at` which is the position of the card in the stack, `in` which is the stack of cards, and `to` which is the new card to replace the card at position `index`. +The function should return a copy of the stack with the card at position `index` replaced with the new card. +If the given `index` is not a valid index in the stack, the original stack should be returned, unchanged. ```swift let index = 2 let newCard = 6 setCard(at: index, in: [1, 2, 4, 1], to: newCard) -// => [1, 2, 6, 1] +// returns [1, 2, 6, 1] ``` ## 3. Insert a card at the of top the stack -Implement the function `insert(_:atTopOf:)` that returns a copy of the stack with the new card provided added to the top of the stack. +Make a card appear by inserting a new card at the top of the stack. + +Implement the function `insert(_:atTopOf:)` that takes two arguments: the new card to be inserted, and the stack of cards. +The function should returns a copy of the stack with the new card provided added to the top of the stack. ```swift let newCard = 8 insert(newCard, atTopOf: [5, 9, 7, 1]) -// => [5, 9, 7, 1, 8] +// returns [5, 9, 7, 1, 8] ``` ## 4. Remove a card from the stack -Implement the function `removeCard(at:from:)` that returns a copy of the stack which has had the card at position `index` removed. If the given `index` is not a valid index in the stack, the original stack should be returned, unchanged. +Make a card disappear by removing the card at the given `position` from the stack. + +Implement the function `removeCard(at:from:)` that takes two arguments: `at` which is the position of the card in the stack, and `from` which is the stack of cards. +The function should return a copy of the stack with the card at position `index` removed. +If the given `index` is not a valid index in the stack, the original stack should be returned, unchanged. ```swift let index = 2 removeCard(at: index, from: [3, 2, 6, 4, 8]) -// => [3, 2, 4, 8] +// returns [3, 2, 4, 8] ``` -## 5. Remove the top card from the stack - -Implement the function `removeTopCard(_:)` that returns a copy of the stack which has had the card at the top of the stack removed. If the given stack is empty, the original stack should be returned, unchanged. - -```swift -removeTopCard([3, 2, 6, 4, 8]) -// => [3, 2, 6, 4] -``` +## 5. Insert a card in the stack -## 6. Insert a card at the bottom of the stack +Make a card appear by inserting a new card at the given `position` in the stack. -Implement the function `insert(_:atBottomOf:)` that returns a copy of the stack with the new card provided added to the bottom of the stack. +Implement the function `insert(_:at:from:)` that takes three arguments: the new card to be inserted, the position at which the new card should be inserted, and the stack of cards. +The function should return a copy of the stack with the new card provided added at the given position. +If the given `index` is not a valid index in the stack, the original stack should be returned, unchanged. ```swift let newCard = 8 -insert(newCard, atBottomOf: [5, 9, 7, 1]) -// => [8, 5, 9, 7, 1] +insert(newCard, at: 2, from: [5, 9, 7, 1]) +// returns [5, 9, 8, 7, 1] ``` -## 7. Remove a card from the bottom of the stack +## 6. Check size of the stack -Implement the function `removeBottomCard(_:)` that returns a copy of the stack which has had the card at the bottom of the stack removed. If the given stack is empty, the original stack should be returned, unchanged. +Check whether the size of the stack is equal to `stackSize` or not. -```swift -removeBottomCard([8, 5, 9, 7, 1]) -// => [5, 9, 7, 1] -``` - -## 8. Check size of the stack - -Implement the function `checkSizeOfStack(_:_:)` that checks whether the size of the stack is equal to a given `stackSize` or not. +Implement the function `checkSizeOfStack(_:_:)` that takes two arguments: `stack` which is the stack of cards, and `stackSize` which is the size of the stack. +The function should return `true` if the size of the stack is equal to `stackSize` and `false` otherwise. ```swift let stackSize = 4 checkSizeOfStack([3, 2, 6, 4, 8], stackSize) -// => false -``` - -## 9. Count the number of even cards in the stack - -Implement the function `evenCardCount(_:)` that steps through the stack and counts the number of even cards in it. - -```swift -evenCardCount([3,8,4,5,1,6,10]) -// => 4 +// returns false ``` diff --git a/exercises/concept/magician-in-training/.docs/introduction.md b/exercises/concept/magician-in-training/.docs/introduction.md index 7ad8685ea..4a8422995 100644 --- a/exercises/concept/magician-in-training/.docs/introduction.md +++ b/exercises/concept/magician-in-training/.docs/introduction.md @@ -1,49 +1,173 @@ -# Introduction +# About -Arrays are one of Swift's three primary collection types. Arrays are ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type. +[Arrays][array] are one of Swift's three primary collection types. +Arrays are ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type. +Arrays are mutable when assigned to a variable, meaning that the elements of an array can be modified after the array is created. +This is not the case when an array is assigned to a constant, in which case the array is immutable. -Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. Empty arrays are just a pair of square brackets. Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in the array. When creating an empty array, the type must be specified. +Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. +Swift will infer the type of the array from the types of the elements in the array literal. + +```swift +let evenInts = [2, 4, 6, 8, 10, 12] +var oddInts = [1, 3, 5, 7, 9, 11, 13] +let greetings = ["Hello!", "Hi!", "¡Hola!"] +``` + +Arrays can also be explicitly typed by specifying the type of the elements in the array. +Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in thee array. ```swift let evenInts: Array = [2, 4, 6, 8, 10, 12] var oddInts: [Int] = [1, 3, 5, 7, 9, 11, 13] -let greetings = ["Hello!", "Hi!", "¡Hola!"] -var myStringArray: [String] = [] +let greetings : [String] = ["Hello!", "Hi!", "¡Hola!"] ``` -Elements of an array can be accessed individually by supplying the index of the element inside square brackets following the array; array indices are `Int`s and start with `0` for the first (leftmost) element. This subscript notation can be used to get the element at that index as well as to set the element at that index, provided the array was defined as a variable (i.e. using `var`). +## Size of an Array -Trying to access elements at indices outside the valid range of indices will result in a runtime error that crashes the program. Since any invalid array index access will crash a program, it is common to test arrays to see if they are empty before working with them by checking the `isEmpty` property or checking if an index is valid by ensuring that it is greater than or equal to 0 and less than the array's `count` property. +The number of elements in an array can be determined using the [`count`][count] property. ```swift +evenInts.count +// returns 6 +``` + +## Empty Arrays + +When wanting an empty array, the type must be specified. +This can be done by using either the array initializer syntax or by using the type annotation syntax. + +```swift +let emptyArray = [Int]() +let emptyArray2 = Array() +let emptyArray3: [Int] = [] +``` + +## Multi-dimensional Arrays + +Arrays can be nested to create multi-dimensional arrays. +When explicitly typing a multi-dimensional array, the type of the elements of the innermost array must be specified, using: `Array>` or `[[T]]`. + +```swift +let multiDimArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] +let multiDimArray2: [[Int]] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] +``` + +## Append to an Array + +Elements can be appended to the end of an array using the [`append(_:)`][append] method. +The `append(_:)` method takes a single argument, the element to be appended to the array. + +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.append(15) +// oddInts is now [1, 3, 5, 7, 9, 11, 13, 15] +``` + +## Insert into an Array + +Elements can be inserted into an array using the [`insert(_:at:)`][insert] method. +The `insert(_:at:)` method takes two arguments, the element to be inserted into the array and the index at which the element should be inserted. + +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.insert(0, at: 0) +// oddInts is now [0, 1, 3, 5, 7, 9, 11, 13] +``` + +## Add an Array to an Array + +Array can be added to the end of an array using the `+` operator. +It is important to note that the `+` operator creates a new array and does not modify the original array, which is different from the `append(_:)` or `insert(_:at:)` methods. + +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts + [15, 17, 19] +// returns [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] + +print(oddInts) +// prints [1, 3, 5, 7, 9, 11, 13] +``` + +## Accessing Elements of an Array + +Elements of an array can be accessed individually by supplying the index of the element inside square brackets following the array. +The index of an element is an `Int` and starts with `0` for the first (leftmost) element. +If the index is outside the valid range of indices, a runtime error will occur and the program will crash. + +```swift +let evenInts = [2, 4, 6, 8, 10, 12] +let oddInts = [1, 3, 5, 7, 9, 11, 13] + evenInts[2] -// => 6 -oddInts[0] = 27 -// oddInts is now [27, 3, 5, 7, 9, 11, 13] +// returns 6 + +oddInts[7] +// Fatal error: Index out of range +``` + +## Modifying Elements of an Array + +Elements of an array can be modified by assigning a new value to the element at a given index. +The index of an element is an `Int` and starts with `0` for the first (leftmost) element. +If the index is outside the valid range of indices, a runtime error will occur and the program will crash. + +```swift +var evenInts = [2, 4, 6, 8, 10, 12] + +evenInts[2] = 0 +// evenInts is now [2, 4, 0, 8, 10, 12] +``` + +## Converting an Array to a String and Back + +An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method. +The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array. +The separator must be a string. + +```swift +let evenInts = ["2", "4", "6", "8", "10", "12"] +let evenIntsString = evenInts.joined(separator: ", ") +// returns "2, 4, 6, 8, 10, 12" +``` + +An array can be converted from a string using the [`split(separator:)`][split] method. +The `split(separator:)` method takes a single argument, the separator to be used between elements of the array. -// these operations are not allowed -greetings[3] -// error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). -evenInts[1] = 0 -// Cannot assign through subscript: 'evenInts' is a 'let' constant +```swift +let evenIntsString = "2, 4, 6, 8, 10, 12" +let evenInts = evenIntsString.split(separator: ", ") +// returns ["2", "4", "6", "8", "10", "12"] ``` -The elements of an array can be stepped through one at a time using a for-in loop. This type of loop takes each element of the array, in order, and binds the element to a specified name for further processing inside the loop body. For example, to print out all of the odd integers in an array one can write: +## Delete an Element from an Array + +Elements can be deleted from an array using the [`remove(at:)`][remove] method. +The `remove(at:)` method takes a single argument, the index of the element to be removed from the array. +The index of an element is an `Int` and starts with `0` for the first (leftmost) element. +If the index is outside the valid range of indices, a runtime error will occur and the program will crash. ```swift -let ints = [1, 3, 6, 14, 17, 8, 23, 5, 18, 11] +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.remove(at: 3) +// oddInts is now [1, 3, 5, 9, 11, 13] +``` -for int in ints { - if !int.isMultiple(of: 2) { - print(int) - } -} +If the last element of an array is to be removed, the [`removeLast()`][removeLast] method can be used. +The `removeLast()` method takes no arguments. +If the array is empty, a runtime error will occur and the program will crash. -// prints out: -// 1 -// 3 -// 17 -// 23 -// 5 -// 11 +```swift +var oddInts = [1, 3, 5, 7, 9, 11, 13] +oddInts.removeLast() +// oddInts is now [1, 3, 5, 7, 9, 11] ``` + +[array]: https://developer.apple.com/documentation/swift/array +[count]: https://developer.apple.com/documentation/swift/array/count +[insert]: https://developer.apple.com/documentation/swift/array/insert(_:at:)-3erb3 +[remove]: https://developer.apple.com/documentation/swift/array/remove(at:)-1p2pj +[removeLast]: https://developer.apple.com/documentation/swift/array/removelast() +[append]: https://developer.apple.com/documentation/swift/array/append(_:)-1ytnt +[joined]: https://developer.apple.com/documentation/swift/array/joined(separator:)-5do1g +[split]: https://developer.apple.com/documentation/swift/string/2894564-split diff --git a/exercises/concept/magician-in-training/.meta/Sources/MagicianInTraining/MagicianInTrainingExemplar.swift b/exercises/concept/magician-in-training/.meta/Sources/MagicianInTraining/MagicianInTrainingExemplar.swift index ca2fbb202..0feaee6e1 100644 --- a/exercises/concept/magician-in-training/.meta/Sources/MagicianInTraining/MagicianInTrainingExemplar.swift +++ b/exercises/concept/magician-in-training/.meta/Sources/MagicianInTraining/MagicianInTrainingExemplar.swift @@ -3,7 +3,9 @@ func getCard(at index: Int, from stack: [Int]) -> Int { } func setCard(at index: Int, in stack: [Int], to newCard: Int) -> [Int] { - guard index >= 0 && index < stack.count else { return stack } + if index > stack.count { + return stack + } var newStack = stack newStack[index] = newCard return newStack @@ -14,42 +16,23 @@ func insert(_ newCard: Int, atTopOf stack: [Int]) -> [Int] { } func removeCard(at index: Int, from stack: [Int]) -> [Int] { - guard index >= 0 && index < stack.count else { return stack } + if index > stack.count { + return stack + } var newStack = stack newStack.remove(at: index) return newStack } -func removeTopCard(_ stack: [Int]) -> [Int] { - guard !stack.isEmpty else { return stack } - var newStack = stack - newStack.removeLast() - return newStack -} - -func insert(_ newCard: Int, atBottomOf stack: [Int]) -> [Int] { - var newStack = stack - newStack.insert(newCard, at: 0) - return newStack -} - -func removeBottomCard(_ stack: [Int]) -> [Int] { - guard !stack.isEmpty else { return stack } +func insert(_ newCard: Int, at index: Int, from stack: [Int]) -> [Int] { + if index > stack.count { + return stack + } var newStack = stack - newStack.removeFirst() + newStack.insert(newCard, at: index) return newStack } func checkSizeOfStack(_ stack: [Int], _ size: Int) -> Bool { return stack.count == size } - -func evenCardCount(_ stack: [Int]) -> Int { - var evens = 0 - for card in stack { - if card.isMultiple(of: 2) { - evens += 1 - } - } - return evens -} diff --git a/exercises/concept/magician-in-training/.meta/config.json b/exercises/concept/magician-in-training/.meta/config.json index a3b7e7915..e8b957ce2 100644 --- a/exercises/concept/magician-in-training/.meta/config.json +++ b/exercises/concept/magician-in-training/.meta/config.json @@ -1,6 +1,7 @@ { "authors": [ - "wneumann" + "wneumann", + "meatball133" ], "files": { "solution": [ diff --git a/exercises/concept/magician-in-training/Package.swift b/exercises/concept/magician-in-training/Package.swift index 0c2c2abcf..022fc1f67 100644 --- a/exercises/concept/magician-in-training/Package.swift +++ b/exercises/concept/magician-in-training/Package.swift @@ -4,25 +4,25 @@ import PackageDescription let package = Package( - name: "MagicianInTraining", - products: [ - // Products define the executables and libraries a package produces, and make them visible to other packages. - .library( - name: "MagicianInTraining", - targets: ["MagicianInTraining"]), - ], - dependencies: [ - // Dependencies declare other packages that this package depends on. - // .package(url: /* package url */, from: "1.0.0"), - ], - targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. - .target( - name: "MagicianInTraining", - dependencies: []), - .testTarget( - name: "MagicianInTrainingTests", - dependencies: ["MagicianInTraining"]), - ] + name: "MagicianInTraining", + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "MagicianInTraining", + targets: ["MagicianInTraining"]) + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "MagicianInTraining", + dependencies: []), + .testTarget( + name: "MagicianInTrainingTests", + dependencies: ["MagicianInTraining"]), + ] ) diff --git a/exercises/concept/magician-in-training/Sources/MagicianInTraining/MagicianInTraining.swift b/exercises/concept/magician-in-training/Sources/MagicianInTraining/MagicianInTraining.swift index 51fe79d03..29c310764 100644 --- a/exercises/concept/magician-in-training/Sources/MagicianInTraining/MagicianInTraining.swift +++ b/exercises/concept/magician-in-training/Sources/MagicianInTraining/MagicianInTraining.swift @@ -14,22 +14,10 @@ func removeCard(at index: Int, from stack: [Int]) -> [Int] { fatalError("Please implement the removeCard(at:from:) function") } -func removeTopCard(_ stack: [Int]) -> [Int] { - fatalError("Please implement the removeTopCard(_) function") -} - -func insert(_ newCard: Int, atBottomOf stack: [Int]) -> [Int] { - fatalError("Please implement the insert(_:atBottomOf:) function") -} - -func removeBottomCard(_ stack: [Int]) -> [Int] { - fatalError("Please implement the removeBottomCard(_) function") +func insert(_ newCard: Int, at index: Int, from stack: [Int]) -> [Int] { + fatalError("Please implement the insert(_:at:from:) function") } func checkSizeOfStack(_ stack: [Int], _ size: Int) -> Bool { fatalError("Please implement the checkSizeOfStack(_:_:) function") } - -func evenCardCount(_ stack: [Int]) -> Int { - fatalError("Please implement the evenCardCount(_) function") -} diff --git a/exercises/concept/magician-in-training/Tests/LinuxMain.swift b/exercises/concept/magician-in-training/Tests/LinuxMain.swift deleted file mode 100644 index 4e9800ff5..000000000 --- a/exercises/concept/magician-in-training/Tests/LinuxMain.swift +++ /dev/null @@ -1,6 +0,0 @@ -import MagicianInTrainingTests -import XCTest - -var tests = [XCTestCaseEntry]() -tests += MagicianInTrainingTests.allTests() -XCTMain(tests) diff --git a/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests.swift b/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests.swift new file mode 100644 index 000000000..5e69c22e4 --- /dev/null +++ b/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests.swift @@ -0,0 +1,135 @@ +import XCTest + +@testable import MagicianInTraining + +let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false + +class TaskGetCardTests: XCTestCase { + func testGetFirstCard() { + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(getCard(at: 0, from: stack), 1) + } + + func testGetMiddleCard() { + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(getCard(at: 2, from: stack), 3) + } + + func testGetLastCard() { + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(getCard(at: 4, from: stack), 5) + } +} + +class TaskSetCardTests: XCTestCase { + func testSetFirstCard() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(setCard(at: 0, in: stack, to: 10), [10, 2, 3, 4, 5]) + } + + func testSetMiddleCard() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(setCard(at: 2, in: stack, to: 10), [1, 2, 10, 4, 5]) + } + + func testSetLastCard() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(setCard(at: 4, in: stack, to: 10), [1, 2, 3, 4, 10]) + } + + func testSetCardIndexOutOfBounds() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(setCard(at: 5, in: stack, to: 10), stack) + } +} + +class TaskInsertAtTopTests: XCTestCase { + func testInsertAtTop() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(insert(10, atTopOf: stack), [1, 2, 3, 4, 5, 10]) + } + + func testInsertAnotherAtTop() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [6, 7, 8, 9, 10] + XCTAssertEqual(insert(5, atTopOf: stack), [6, 7, 8, 9, 10, 5]) + } + + func testInsertAtTopOfEmptyStack() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [Int]() + XCTAssertEqual(insert(5, atTopOf: stack), [5]) + } +} + +class TaskRemoveCardTests: XCTestCase { + func testRemoveFirstCard() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(removeCard(at: 0, from: stack), [2, 3, 4, 5]) + } + + func testRemoveMiddleCard() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(removeCard(at: 2, from: stack), [1, 2, 4, 5]) + } + + func testRemoveLastCard() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(removeCard(at: 4, from: stack), [1, 2, 3, 4]) + } + + func testRemoveCardIndexOutOfBounds() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1, 2, 3, 4, 5] + XCTAssertEqual(removeCard(at: 5, from: stack), stack) + } +} + +class TaskInsertAtTask: XCTestCase{ + func testInsertAtTask() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1,2,3,4,5] + XCTAssertEqual(insert(10, at: 2, from: stack), [1,2,10,3,4,5]) + } + + func testInsertAnotherAtTask() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [6,7,8,9,10] + XCTAssertEqual(insert(5, at: 3, from: stack), [6,7,8,5,9,10]) + } + + func testInsertAtTaskOfEmptyStack() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [Int]() + XCTAssertEqual(insert(5, at: 0, from: stack), [5]) + } + + func testInsertAtTaskIndexOutOfBounds() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1,2,3,4,5] + XCTAssertEqual(insert(10, at: 6, from: stack), stack) + } +} + +class TaskCheckSizeOfStackTests: XCTestCase { + func testCheckSizeOfStackTrue() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1,2,3,4,5] + XCTAssertTrue(checkSizeOfStack(stack, 5)) + } + + func testCheckSizeOfStackFalse() throws { + try XCTSkipIf(true && !runAll) // change true to false to run this test + let stack = [1,2,3,4,5] + XCTAssertFalse(checkSizeOfStack(stack, 4)) + } +} +0 \ No newline at end of file diff --git a/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/MagicianInTrainingTests.swift b/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/MagicianInTrainingTests.swift deleted file mode 100644 index fefb02a97..000000000 --- a/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/MagicianInTrainingTests.swift +++ /dev/null @@ -1,135 +0,0 @@ -import XCTest - -@testable import MagicianInTraining - -final class MagicianInTrainingTests: XCTestCase { - let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false - - func testGetCard() { - let stack = (1...10).shuffled() - guard let idx = stack.indices.randomElement() else { fatalError("test suite failure") } - XCTAssertEqual(getCard(at: idx, from: stack), stack[idx]) - } - - func testSetCard() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [9, 4, 3, 6, 1, 7, 2, 8, 5] - let idx = 5 - XCTAssertEqual(setCard(at: idx, in: stack, to: 10), [9, 4, 3, 6, 1, 10, 2, 8, 5]) - } - - func testSetCardIndexTooLow() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [9, 4, 3, 6, 1, 7, 2, 8, 5] - let idx = -3 - XCTAssertEqual(setCard(at: idx, in: stack, to: 10), stack) - } - - func testSetCardIndexTooHigh() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [9, 4, 3, 6, 1, 7, 2, 8, 5] - let idx = 50 - XCTAssertEqual(setCard(at: idx, in: stack, to: 10), stack) - } - - func testInsertAtTop() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [1, 7, 5, 8, 3, 9, 6, 4, 2] - XCTAssertEqual(insert(10, atTopOf: stack), [1, 7, 5, 8, 3, 9, 6, 4, 2, 10]) - } - - func testRemoveCard() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [9, 2, 1, 6, 5, 7, 4, 3, 8] - let idx = 2 - XCTAssertEqual(removeCard(at: idx, from: stack), [9, 2, 6, 5, 7, 4, 3, 8]) - } - - func testRemoveCardIndexTooLow() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [9, 2, 1, 6, 5, 7, 4, 3, 8] - let idx = -2 - XCTAssertEqual(removeCard(at: idx, from: stack), stack) - } - - func testRemoveCardIndexTooHigh() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [9, 2, 1, 6, 5, 7, 4, 3, 8] - let idx = 20 - XCTAssertEqual(removeCard(at: idx, from: stack), stack) - } - - func testRemoveTopCard() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [2, 7, 4, 6, 9, 1, 8, 3, 5] - XCTAssertEqual(removeTopCard(stack), [2, 7, 4, 6, 9, 1, 8, 3]) - } - - func testRemoveTopCardFromEmptyStack() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [Int]() - XCTAssertEqual(removeTopCard(stack), stack) - } - - func testInsertAtBottom() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [4, 3, 8, 9, 1, 7, 6, 5, 2] - XCTAssertEqual(insert(10, atBottomOf: stack), [10, 4, 3, 8, 9, 1, 7, 6, 5, 2]) - } - - func testRemoveBottomCard() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [8, 7, 4, 2, 6, 5, 3, 1, 9] - XCTAssertEqual(removeBottomCard(stack), [7, 4, 2, 6, 5, 3, 1, 9]) - } - - func testRemoveBottomCardFromEmptyStack() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [Int]() - XCTAssertEqual(removeTopCard(stack), stack) - } - - func testCheckSizeTrue() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [6, 9, 7, 8, 2, 3, 4, 5, 1] - XCTAssertTrue(checkSizeOfStack(stack, 9)) - } - - func testCheckSizeFalse() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [6, 9, 7, 8, 2, 3, 4, 5, 1] - XCTAssertFalse(checkSizeOfStack(removeBottomCard(stack), 9)) - } - - func testEvenCardCount() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [4, 6, 3, 7, 1, 9, 5, 8, 2] - XCTAssertEqual(evenCardCount(stack), 4) - } - - func testEvenCardCountZero() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [7, 3, 7, 1, 5, 5, 3, 9, 9] - XCTAssertEqual(evenCardCount(stack), 0) - } - - static var allTests = [ - ("testGetCard", testGetCard), - ("testSetCard", testSetCard), - ("testSetCardIndexTooLow", testSetCardIndexTooLow), - ("testSetCardIndexTooHigh", testSetCardIndexTooHigh), - ("testInsertAtTop", testInsertAtTop), - ("testRemoveCard", testRemoveCard), - ("testRemoveCardIndexTooLow", testRemoveCardIndexTooLow), - ("testRemoveCardIndexTooHigh", testRemoveCardIndexTooHigh), - ("testRemoveTopCard", testRemoveTopCard), - ("testRemoveTopCardFromEmptyStack", testRemoveTopCardFromEmptyStack), - ("testInsertAtBottom", testInsertAtBottom), - ("testRemoveBottomCard", testRemoveBottomCard), - ("testRemoveBottomCardFromEmptyStack", testRemoveBottomCardFromEmptyStack), - ("testCheckSizeTrue", testCheckSizeTrue), - ("testCheckSizeFalse", testCheckSizeFalse), - ("testEvenCardCount", testEvenCardCount), - ("testEvenCardCountZero", testEvenCardCountZero), - ] -} diff --git a/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/XCTestManifests.swift b/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/XCTestManifests.swift deleted file mode 100644 index b1ebd20f7..000000000 --- a/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/XCTestManifests.swift +++ /dev/null @@ -1,9 +0,0 @@ -import XCTest - -#if !canImport(ObjectiveC) - public func allTests() -> [XCTestCaseEntry] { - return [ - testCase(MagicianInTrainingTests.allTests) - ] - } -#endif From 0a0aa79db8a63cd8b5594dc6ef1ef6013d3b1c14 Mon Sep 17 00:00:00 2001 From: Carl Date: Thu, 2 Nov 2023 13:42:46 +0100 Subject: [PATCH 2/5] Remove unnecessary line in MagicianInTrainingTests.swift --- .../magician-in-training/Tests/MagicianInTrainingTests.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests.swift b/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests.swift index 5e69c22e4..69164af72 100644 --- a/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests.swift +++ b/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests.swift @@ -132,4 +132,3 @@ class TaskCheckSizeOfStackTests: XCTestCase { XCTAssertFalse(checkSizeOfStack(stack, 4)) } } -0 \ No newline at end of file From 2cb0e9548ac37cd62e45927600edbcdff60ae46e Mon Sep 17 00:00:00 2001 From: Carl Date: Thu, 2 Nov 2023 13:44:24 +0100 Subject: [PATCH 3/5] Fix folder --- .../{ => MagicianInTrainingTests}/MagicianInTrainingTests.swift | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename exercises/concept/magician-in-training/Tests/{ => MagicianInTrainingTests}/MagicianInTrainingTests.swift (100%) diff --git a/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests.swift b/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/MagicianInTrainingTests.swift similarity index 100% rename from exercises/concept/magician-in-training/Tests/MagicianInTrainingTests.swift rename to exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/MagicianInTrainingTests.swift From b9f08317e8e72b653b05709b3c7e394775117ec8 Mon Sep 17 00:00:00 2001 From: meatball Date: Sat, 28 Dec 2024 21:50:53 +0100 Subject: [PATCH 4/5] Update changes based on feedback, update test to swift-testing --- concepts/arrays/.meta/config.json | 4 +- concepts/arrays/about.md | 8 +- concepts/arrays/introduction.md | 8 +- .../.docs/introduction.md | 8 +- .../MagicianInTrainingExemplar.swift | 4 +- .../magician-in-training/Package.swift | 2 +- .../MagicianInTrainingTests.swift | 138 +++++++++--------- 7 files changed, 84 insertions(+), 88 deletions(-) diff --git a/concepts/arrays/.meta/config.json b/concepts/arrays/.meta/config.json index bfbbdacd4..7ab60f86f 100644 --- a/concepts/arrays/.meta/config.json +++ b/concepts/arrays/.meta/config.json @@ -4,5 +4,7 @@ "wneumann", "meatball133" ], - "contributors": [] + "contributors": [ + "heitara" + ] } diff --git a/concepts/arrays/about.md b/concepts/arrays/about.md index 4a8422995..29c373259 100644 --- a/concepts/arrays/about.md +++ b/concepts/arrays/about.md @@ -15,12 +15,12 @@ let greetings = ["Hello!", "Hi!", "¡Hola!"] ``` Arrays can also be explicitly typed by specifying the type of the elements in the array. -Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in thee array. +Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in the array. ```swift let evenInts: Array = [2, 4, 6, 8, 10, 12] var oddInts: [Int] = [1, 3, 5, 7, 9, 11, 13] -let greetings : [String] = ["Hello!", "Hi!", "¡Hola!"] +let greetings: [String] = ["Hello!", "Hi!", "¡Hola!"] ``` ## Size of an Array @@ -121,8 +121,8 @@ evenInts[2] = 0 ## Converting an Array to a String and Back -An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method. -The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array. +An array of `n` strings can be converted to a single string using the [`joined(separator:)`][joined] method. +The `joined(separator:)` method takes a single argument, the separator to be used between elements of the array. The separator must be a string. ```swift diff --git a/concepts/arrays/introduction.md b/concepts/arrays/introduction.md index 4a8422995..29c373259 100644 --- a/concepts/arrays/introduction.md +++ b/concepts/arrays/introduction.md @@ -15,12 +15,12 @@ let greetings = ["Hello!", "Hi!", "¡Hola!"] ``` Arrays can also be explicitly typed by specifying the type of the elements in the array. -Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in thee array. +Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in the array. ```swift let evenInts: Array = [2, 4, 6, 8, 10, 12] var oddInts: [Int] = [1, 3, 5, 7, 9, 11, 13] -let greetings : [String] = ["Hello!", "Hi!", "¡Hola!"] +let greetings: [String] = ["Hello!", "Hi!", "¡Hola!"] ``` ## Size of an Array @@ -121,8 +121,8 @@ evenInts[2] = 0 ## Converting an Array to a String and Back -An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method. -The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array. +An array of `n` strings can be converted to a single string using the [`joined(separator:)`][joined] method. +The `joined(separator:)` method takes a single argument, the separator to be used between elements of the array. The separator must be a string. ```swift diff --git a/exercises/concept/magician-in-training/.docs/introduction.md b/exercises/concept/magician-in-training/.docs/introduction.md index 4a8422995..29c373259 100644 --- a/exercises/concept/magician-in-training/.docs/introduction.md +++ b/exercises/concept/magician-in-training/.docs/introduction.md @@ -15,12 +15,12 @@ let greetings = ["Hello!", "Hi!", "¡Hola!"] ``` Arrays can also be explicitly typed by specifying the type of the elements in the array. -Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in thee array. +Type names for arrays are written in one of two ways: `Array` or `[T]` where `T` is the type of the elements in the array. ```swift let evenInts: Array = [2, 4, 6, 8, 10, 12] var oddInts: [Int] = [1, 3, 5, 7, 9, 11, 13] -let greetings : [String] = ["Hello!", "Hi!", "¡Hola!"] +let greetings: [String] = ["Hello!", "Hi!", "¡Hola!"] ``` ## Size of an Array @@ -121,8 +121,8 @@ evenInts[2] = 0 ## Converting an Array to a String and Back -An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method. -The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array. +An array of `n` strings can be converted to a single string using the [`joined(separator:)`][joined] method. +The `joined(separator:)` method takes a single argument, the separator to be used between elements of the array. The separator must be a string. ```swift diff --git a/exercises/concept/magician-in-training/.meta/Sources/MagicianInTraining/MagicianInTrainingExemplar.swift b/exercises/concept/magician-in-training/.meta/Sources/MagicianInTraining/MagicianInTrainingExemplar.swift index 0feaee6e1..0446294b5 100644 --- a/exercises/concept/magician-in-training/.meta/Sources/MagicianInTraining/MagicianInTrainingExemplar.swift +++ b/exercises/concept/magician-in-training/.meta/Sources/MagicianInTraining/MagicianInTrainingExemplar.swift @@ -3,7 +3,7 @@ func getCard(at index: Int, from stack: [Int]) -> Int { } func setCard(at index: Int, in stack: [Int], to newCard: Int) -> [Int] { - if index > stack.count { + if index >= stack.count { return stack } var newStack = stack @@ -16,7 +16,7 @@ func insert(_ newCard: Int, atTopOf stack: [Int]) -> [Int] { } func removeCard(at index: Int, from stack: [Int]) -> [Int] { - if index > stack.count { + if index >= stack.count { return stack } var newStack = stack diff --git a/exercises/concept/magician-in-training/Package.swift b/exercises/concept/magician-in-training/Package.swift index 022fc1f67..655b1cc14 100644 --- a/exercises/concept/magician-in-training/Package.swift +++ b/exercises/concept/magician-in-training/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription diff --git a/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/MagicianInTrainingTests.swift b/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/MagicianInTrainingTests.swift index 69164af72..c7dbf7795 100644 --- a/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/MagicianInTrainingTests.swift +++ b/exercises/concept/magician-in-training/Tests/MagicianInTrainingTests/MagicianInTrainingTests.swift @@ -1,134 +1,128 @@ -import XCTest +import Testing +import Foundation @testable import MagicianInTraining -let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "true"]) ?? false -class TaskGetCardTests: XCTestCase { +@Suite struct MagicianInTrainingTest { + @Test("Can get the first card") func testGetFirstCard() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(getCard(at: 0, from: stack), 1) + #expect(getCard(at: 0, from: stack) == 1) } + @Test("Can get a middle card", .enabled(if: RUNALL)) func testGetMiddleCard() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(getCard(at: 2, from: stack), 3) + #expect(getCard(at: 2, from: stack) == 3) } + @Test("Can get the last card", .enabled(if: RUNALL)) func testGetLastCard() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(getCard(at: 4, from: stack), 5) + #expect(getCard(at: 4, from: stack) == 5) } -} -class TaskSetCardTests: XCTestCase { - func testSetFirstCard() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Can set the first card", .enabled(if: RUNALL)) + func testSetFirstCard() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(setCard(at: 0, in: stack, to: 10), [10, 2, 3, 4, 5]) + #expect(setCard(at: 0, in: stack, to: 10) == [10, 2, 3, 4, 5]) } - func testSetMiddleCard() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Can set a middle card", .enabled(if: RUNALL)) + func testSetMiddleCard() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(setCard(at: 2, in: stack, to: 10), [1, 2, 10, 4, 5]) + #expect(setCard(at: 2, in: stack, to: 10) == [1, 2, 10, 4, 5]) } - func testSetLastCard() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Can set the last card", .enabled(if: RUNALL)) + func testSetLastCard() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(setCard(at: 4, in: stack, to: 10), [1, 2, 3, 4, 10]) + #expect(setCard(at: 4, in: stack, to: 10) == [1, 2, 3, 4, 10]) } - func testSetCardIndexOutOfBounds() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Cannot set a card index out of bounds", .enabled(if: RUNALL)) + func testSetCardIndexOutOfBounds() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(setCard(at: 5, in: stack, to: 10), stack) + #expect(setCard(at: 5, in: stack, to: 10) == stack) } -} -class TaskInsertAtTopTests: XCTestCase { - func testInsertAtTop() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Can insert a card at the top", .enabled(if: RUNALL)) + func testInsertAtTop() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(insert(10, atTopOf: stack), [1, 2, 3, 4, 5, 10]) + #expect(insert(10, atTopOf: stack) == [1, 2, 3, 4, 5, 10]) } - func testInsertAnotherAtTop() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Can insert another card at the top", .enabled(if: RUNALL)) + func testInsertAnotherAtTop() { let stack = [6, 7, 8, 9, 10] - XCTAssertEqual(insert(5, atTopOf: stack), [6, 7, 8, 9, 10, 5]) + #expect(insert(5, atTopOf: stack) == [6, 7, 8, 9, 10, 5]) } - func testInsertAtTopOfEmptyStack() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Can insert a card at the top of an empty stack", .enabled(if: RUNALL)) + func testInsertAtTopOfEmptyStack() { let stack = [Int]() - XCTAssertEqual(insert(5, atTopOf: stack), [5]) + #expect(insert(5, atTopOf: stack) == [5]) } -} -class TaskRemoveCardTests: XCTestCase { - func testRemoveFirstCard() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Can remove the first card", .enabled(if: RUNALL)) + func testRemoveFirstCard() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(removeCard(at: 0, from: stack), [2, 3, 4, 5]) + #expect(removeCard(at: 0, from: stack) == [2, 3, 4, 5]) } - func testRemoveMiddleCard() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Can remove a middle card", .enabled(if: RUNALL)) + func testRemoveMiddleCard() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(removeCard(at: 2, from: stack), [1, 2, 4, 5]) + #expect(removeCard(at: 2, from: stack) == [1, 2, 4, 5]) } - func testRemoveLastCard() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Can remove the last card", .enabled(if: RUNALL)) + func testRemoveLastCard() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(removeCard(at: 4, from: stack), [1, 2, 3, 4]) + #expect(removeCard(at: 4, from: stack) == [1, 2, 3, 4]) } - func testRemoveCardIndexOutOfBounds() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Cannot remove a card index out of bounds", .enabled(if: RUNALL)) + func testRemoveCardIndexOutOfBounds() { let stack = [1, 2, 3, 4, 5] - XCTAssertEqual(removeCard(at: 5, from: stack), stack) + #expect(removeCard(at: 5, from: stack) == stack) } -} -class TaskInsertAtTask: XCTestCase{ - func testInsertAtTask() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [1,2,3,4,5] - XCTAssertEqual(insert(10, at: 2, from: stack), [1,2,10,3,4,5]) + @Test("Can insert a card at a specific index", .enabled(if: RUNALL)) + func testInsertAt() { + let stack = [1, 2, 3, 4, 5] + #expect(insert(10, at: 2, from: stack) == [1, 2, 10, 3, 4, 5]) } - func testInsertAnotherAtTask() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [6,7,8,9,10] - XCTAssertEqual(insert(5, at: 3, from: stack), [6,7,8,5,9,10]) + @Test("Can insert another card at a specific index", .enabled(if: RUNALL)) + func testInsertAnotherAt() { + let stack = [6, 7, 8, 9, 10] + #expect(insert(5, at: 3, from: stack) == [6, 7, 8, 5, 9, 10]) } - func testInsertAtTaskOfEmptyStack() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test + @Test("Can insert a card at a specific index of an empty stack", .enabled(if: RUNALL)) + func testInsertAtOfEmptyStack() { let stack = [Int]() - XCTAssertEqual(insert(5, at: 0, from: stack), [5]) + #expect(insert(5, at: 0, from: stack) == [5]) } - func testInsertAtTaskIndexOutOfBounds() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [1,2,3,4,5] - XCTAssertEqual(insert(10, at: 6, from: stack), stack) + @Test("Cannot insert a card at an index out of bounds", .enabled(if: RUNALL)) + func testInsertAtIndexOutOfBounds() { + let stack = [1, 2, 3, 4, 5] + #expect(insert(10, at: 6, from: stack) == stack) } -} -class TaskCheckSizeOfStackTests: XCTestCase { - func testCheckSizeOfStackTrue() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [1,2,3,4,5] - XCTAssertTrue(checkSizeOfStack(stack, 5)) + @Test("Can check the size of a stack", .enabled(if: RUNALL)) + func testCheckSizeOfStackTrue() { + let stack = [1, 2, 3, 4, 5] + #expect(checkSizeOfStack(stack, 5) == true) } - func testCheckSizeOfStackFalse() throws { - try XCTSkipIf(true && !runAll) // change true to false to run this test - let stack = [1,2,3,4,5] - XCTAssertFalse(checkSizeOfStack(stack, 4)) + @Test("Can check the size of a stack", .enabled(if: RUNALL)) + func testCheckSizeOfStackFalse() { + let stack = [1, 2, 3, 4, 5] + #expect(checkSizeOfStack(stack, 4) == false) } } From 5291926b5678cfa5224100914a03b1408500761f Mon Sep 17 00:00:00 2001 From: meatball Date: Mon, 30 Dec 2024 19:55:23 +0100 Subject: [PATCH 5/5] Update based on feedback --- concepts/arrays/.meta/config.json | 3 ++- concepts/arrays/about.md | 10 +++++----- concepts/arrays/introduction.md | 10 +++++----- .../concept/magician-in-training/.docs/introduction.md | 10 +++++----- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/concepts/arrays/.meta/config.json b/concepts/arrays/.meta/config.json index 7ab60f86f..5fc5cc4ec 100644 --- a/concepts/arrays/.meta/config.json +++ b/concepts/arrays/.meta/config.json @@ -5,6 +5,7 @@ "meatball133" ], "contributors": [ - "heitara" + "heitara", + "BNAndras" ] } diff --git a/concepts/arrays/about.md b/concepts/arrays/about.md index 29c373259..a3287bbbf 100644 --- a/concepts/arrays/about.md +++ b/concepts/arrays/about.md @@ -5,8 +5,8 @@ Arrays are ordered lists of elements where the elements can be of any type, howe Arrays are mutable when assigned to a variable, meaning that the elements of an array can be modified after the array is created. This is not the case when an array is assigned to a constant, in which case the array is immutable. -Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. -Swift will infer the type of the array from the types of the elements in the array literal. +Array literals are written as a series of elements, each separated by a comma, enclosed in square brackets. +Swift will infer the type of the array from the type of the elements in the array literal. ```swift let evenInts = [2, 4, 6, 8, 10, 12] @@ -77,7 +77,7 @@ oddInts.insert(0, at: 0) ## Add an Array to an Array -Array can be added to the end of an array using the `+` operator. +An array can be added to the end of another array using the `+` operator. It is important to note that the `+` operator creates a new array and does not modify the original array, which is different from the `append(_:)` or `insert(_:at:)` methods. ```swift @@ -110,7 +110,7 @@ oddInts[7] Elements of an array can be modified by assigning a new value to the element at a given index. The index of an element is an `Int` and starts with `0` for the first (leftmost) element. -If the index is outside the valid range of indices, a runtime error will occur and the program will crash. +If the given index is outside the valid range of indices for the array, a runtime error will occur and the program will crash. ```swift var evenInts = [2, 4, 6, 8, 10, 12] @@ -145,7 +145,7 @@ let evenInts = evenIntsString.split(separator: ", ") Elements can be deleted from an array using the [`remove(at:)`][remove] method. The `remove(at:)` method takes a single argument, the index of the element to be removed from the array. The index of an element is an `Int` and starts with `0` for the first (leftmost) element. -If the index is outside the valid range of indices, a runtime error will occur and the program will crash. +If the given index is outside the valid range of indices for the array, a runtime error will occur and the program will crash. ```swift var oddInts = [1, 3, 5, 7, 9, 11, 13] diff --git a/concepts/arrays/introduction.md b/concepts/arrays/introduction.md index 29c373259..a3287bbbf 100644 --- a/concepts/arrays/introduction.md +++ b/concepts/arrays/introduction.md @@ -5,8 +5,8 @@ Arrays are ordered lists of elements where the elements can be of any type, howe Arrays are mutable when assigned to a variable, meaning that the elements of an array can be modified after the array is created. This is not the case when an array is assigned to a constant, in which case the array is immutable. -Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. -Swift will infer the type of the array from the types of the elements in the array literal. +Array literals are written as a series of elements, each separated by a comma, enclosed in square brackets. +Swift will infer the type of the array from the type of the elements in the array literal. ```swift let evenInts = [2, 4, 6, 8, 10, 12] @@ -77,7 +77,7 @@ oddInts.insert(0, at: 0) ## Add an Array to an Array -Array can be added to the end of an array using the `+` operator. +An array can be added to the end of another array using the `+` operator. It is important to note that the `+` operator creates a new array and does not modify the original array, which is different from the `append(_:)` or `insert(_:at:)` methods. ```swift @@ -110,7 +110,7 @@ oddInts[7] Elements of an array can be modified by assigning a new value to the element at a given index. The index of an element is an `Int` and starts with `0` for the first (leftmost) element. -If the index is outside the valid range of indices, a runtime error will occur and the program will crash. +If the given index is outside the valid range of indices for the array, a runtime error will occur and the program will crash. ```swift var evenInts = [2, 4, 6, 8, 10, 12] @@ -145,7 +145,7 @@ let evenInts = evenIntsString.split(separator: ", ") Elements can be deleted from an array using the [`remove(at:)`][remove] method. The `remove(at:)` method takes a single argument, the index of the element to be removed from the array. The index of an element is an `Int` and starts with `0` for the first (leftmost) element. -If the index is outside the valid range of indices, a runtime error will occur and the program will crash. +If the given index is outside the valid range of indices for the array, a runtime error will occur and the program will crash. ```swift var oddInts = [1, 3, 5, 7, 9, 11, 13] diff --git a/exercises/concept/magician-in-training/.docs/introduction.md b/exercises/concept/magician-in-training/.docs/introduction.md index 29c373259..a3287bbbf 100644 --- a/exercises/concept/magician-in-training/.docs/introduction.md +++ b/exercises/concept/magician-in-training/.docs/introduction.md @@ -5,8 +5,8 @@ Arrays are ordered lists of elements where the elements can be of any type, howe Arrays are mutable when assigned to a variable, meaning that the elements of an array can be modified after the array is created. This is not the case when an array is assigned to a constant, in which case the array is immutable. -Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. -Swift will infer the type of the array from the types of the elements in the array literal. +Array literals are written as a series of elements, each separated by a comma, enclosed in square brackets. +Swift will infer the type of the array from the type of the elements in the array literal. ```swift let evenInts = [2, 4, 6, 8, 10, 12] @@ -77,7 +77,7 @@ oddInts.insert(0, at: 0) ## Add an Array to an Array -Array can be added to the end of an array using the `+` operator. +An array can be added to the end of another array using the `+` operator. It is important to note that the `+` operator creates a new array and does not modify the original array, which is different from the `append(_:)` or `insert(_:at:)` methods. ```swift @@ -110,7 +110,7 @@ oddInts[7] Elements of an array can be modified by assigning a new value to the element at a given index. The index of an element is an `Int` and starts with `0` for the first (leftmost) element. -If the index is outside the valid range of indices, a runtime error will occur and the program will crash. +If the given index is outside the valid range of indices for the array, a runtime error will occur and the program will crash. ```swift var evenInts = [2, 4, 6, 8, 10, 12] @@ -145,7 +145,7 @@ let evenInts = evenIntsString.split(separator: ", ") Elements can be deleted from an array using the [`remove(at:)`][remove] method. The `remove(at:)` method takes a single argument, the index of the element to be removed from the array. The index of an element is an `Int` and starts with `0` for the first (leftmost) element. -If the index is outside the valid range of indices, a runtime error will occur and the program will crash. +If the given index is outside the valid range of indices for the array, a runtime error will occur and the program will crash. ```swift var oddInts = [1, 3, 5, 7, 9, 11, 13]