Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

meatball133
Copy link
Member

No description provided.

var oddInts = [1, 3, 5, 7, 9, 11, 13]
oddInts.append(15)
// oddInts is now [1, 3, 5, 7, 9, 11, 13, 15]
```
Copy link

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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link

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:

Suggested change
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.

Comment on lines -5 to +6
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.
Copy link

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just what the instructions say to make it easier, as you might notice this is not a new implementation of the exercise (I didn't write the exercise), more of a rewrite of the text. The idea is that the exercise stay the same.

@@ -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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[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] = []
```

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
An array can be checked if it's empty with [`isEmpty`][isEmpty] property.

Comment on lines 7 to 23
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)
}
}

Copy link

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.

@meatball133 meatball133 added this to the Swift 6.0 milestone Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants