-
-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Rework]: Array #704
base: main
Are you sure you want to change the base?
[Rework]: Array #704
Conversation
var oddInts = [1, 3, 5, 7, 9, 11, 13] | ||
oddInts.append(15) | ||
// oddInts is now [1, 3, 5, 7, 9, 11, 13, 15] | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the following way to append an item.
var x = [1]
let y = [2]
x += y
print(x)
I believe it's much easier to reason about.
An explanation why +=
works as expected.
struct Vector
{
var x: Double = 0
var y: Double = 0
init(x: Double, y: Double)
{
self.x = x
self.y = y
}
}
func += (left: inout Vector, right: Vector)
{
left.x += right.x
left.y += right.y
}
// 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method. | |
An array of n strings can be converted to a single string using the [`joined(separator:)`][joined] method. |
## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array. | |
The `joined(separator:)` method takes a single argument, the separator to be used between elements of the array. |
## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array. | |
The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array. | |
> Don't forget to convert you Array to `Array<String>`, otherwise `joined(separator:)` won't work. |
Here is a snippet, that shows the conversion. This might be too complicated at this early stage.
let evenInts = [2, 4, 6, 8, 10, 12]
let evenIntsString = evenInts.map({"\($0)"}).joined(separator: ", ")
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add a note how this is achieved and that Generics will be introduced later. For example:
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 ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type. | |
> All primary collection types are implemented using generics. This is the reason why they can work with elements from already defined types or new data types that you will define. |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using Strings. Each card can be represented by a String? What's the difference?
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
She has a stack of cards that she wants to manipulate. | |
She has a stack of cards that she wants to manipulate. The stack of cards can be represented by an array of strings, because regular cards contains numbers ( 1 to 10) and Jack, Queen and King, which we can denote with "J", "Q" and "K". To make it more fun you can use emojis. For example: "♦️J" |
``` | ||
|
||
[array]: https://developer.apple.com/documentation/swift/array | ||
[count]: https://developer.apple.com/documentation/swift/array/count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[count]: https://developer.apple.com/documentation/swift/array/count | |
[count]: https://developer.apple.com/documentation/swift/array/count | |
[isEmpty]: https://developer.apple.com/documentation/swift/array/isEmpty |
let emptyArray2 = Array<Int>() | ||
let emptyArray3: [Int] = [] | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An array can be checked if it's empty with [`isEmpty`][isEmpty] property. |
class TaskGetCardTests: XCTestCase { | ||
func testGetFirstCard() { | ||
let stack = [1, 2, 3, 4, 5] | ||
XCTAssertEqual(getCard(at: 0, from: stack), 1) | ||
} | ||
|
||
func testSetCard() throws { | ||
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) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the tests should be revisited if the cards are represented as strings.
No description provided.