Skip to content

Commit

Permalink
Update concept
Browse files Browse the repository at this point in the history
  • Loading branch information
meatball133 committed Jan 6, 2025
1 parent f200f48 commit 3701026
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 46 deletions.
3 changes: 2 additions & 1 deletion concepts/for-loops/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"blurb": "For loops can be used to iterate over a sequence of values.",
"authors": [
"wneumann"
"wneumann",
"meatball133"
],
"contributors": []
}
120 changes: 120 additions & 0 deletions concepts/for-loops/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# For loops

Looping is a fundamental concept in programming that allows you to execute a block of code multiple times.
In Swift, there are two types of loops: [`for-in` loops][for-loops] and `while` loops.
In this chapter, you'll learn about `for-in` loops.

For loops allows you to iterate over a sequence of values, taking each element in turn, binding it to a variable of your choosing.
Swift allows you to iterate over a variety of sequences, such as ranges, arrays, and strings (and more types which will be covered later).
When every element of the sequence has been iterated over, the loop exits.

For loops are declared by using the `for` keyword, followed by a variable name, the `in` keyword, and a sequence of values to iterate over.
The variable given in the `for-in` loop is inmutable, meaning you can't change its value inside the loop.
Here's an example of a `for-in` loop that iterates over an array of numbers:

```swift
let numbers = [3, 10, 7, 11]

for number in numbers {
print(number)
}
print("Done with numbers")

// prints:
// 3
// 10
// 7
// 11
// Done with numbers
```

~~~~exercism/note
The `number` variable is declared in the `for-in` loop and is only available within the loop's scope.
```swift
let numbers = [3, 10, 7, 11]
for number in numbers {
number + 1
}
number + 1 // Error: Use of unresolved identifier 'number'
```
~~~~

## Iterating over a range

You can also iterate over a range of numbers using a `for-in` loop.
This allows you to execute a block of code a specific number of times, for example, the range `1...5` will iterate over the numbers 1, 2, 3, 4, and 5, so the loop will execute 5 times.
Sometimes you might want to iterate over indexes, in a datastructure like an array, then you can use a `0..<array.count` range.

```swift
let numbers = [3, 10, 7, 11]

for i in 0..<numbers.count {
print(numbers[i])
}

// prints:
// 3
// 10
// 7
// 11
```

## Iterating over a string

You can also iterate over a string using a `for-in` loop.
This allows you to iterate over each character in the string, and note specifically that the type given in the loop is a `Character`.

```swift
let message = "Hello!"

for character in message {
print(character)
}

// prints:
// H
// e
// l
// l
// o
// !
```

## Unusued variables

If you don't need the value of the variable in the loop, you can use an underscore `_` to ignore it.

```swift
let numbers = [3, 10, 7, 11]

for _ in numbers {
print("Hello!")
}
```

## stride

Swift also provides a `stride` function that allows you to create a sequence over a range with a specific step.
Which can be then iterated over using a `for-in` loop.
`stride` is defined as [`stride(from:to:by:)`][stride-to] or [`stride(from:through:by:)`][stride-through], the first one is exclusive and the second one is inclusive.

```swift
for i in stride(from: 0, to: 10, by: 2) {
print(i)
}

// prints:
// 0
// 2
// 4
// 6
// 8
```

Note that the `to` parameter is exclusive, so the loop will iterate until the number before the `to` parameter, while the `through` parameter is inclusive, so in this case it would also include the `10`.

[stride-to]: https://developer.apple.com/documentation/swift/stride(from:to:by:)
[stride-through]: https://developer.apple.com/documentation/swift/stride(from:through:by:)
[for-loops]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow/#For-In-Loops
103 changes: 96 additions & 7 deletions concepts/for-loops/introduction.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,108 @@
# Introduction
# For loops

For-in loops are used to iterate over a sequence of values, taking each element in turn, binding it to a variable or constant name of the developer's choosing, then executes a block of code that may refer to the element. When every element of the sequence has been iterated over, the loop exits and execution begins with the first line following the body of the loop.
Looping is a fundamental concept in programming that allows you to execute a block of code multiple times.
In Swift, there are two types of loops: [`for-in` loops][for-loops] and `while` loops.
In this chapter, you'll learn about `for-in` loops.

For loops allows you to iterate over a sequence of values, taking each element in turn, binding it to a variable of your choosing.
Swift allows you to iterate over a variety of sequences, such as ranges, arrays, and strings (and more types which will be covered later).
When every element of the sequence has been iterated over, the loop exits.

For loops are declared by using the `for` keyword, followed by a variable name, the `in` keyword, and a sequence of values to iterate over.
The variable given in the `for-in` loop is inmutable, meaning you can't change its value inside the loop.
Here's an example of a `for-in` loop that iterates over an array of numbers:

```swift
let numbers = [3, 10, 7, 11]

for number in numbers {
print("\(number) / 2 = \(number / 2)")
print(number)
}
print("Done with numbers")

// prints:
// 3 / 2 = 1
// 10 / 2 = 5
// 7 / 2 = 3
// 11 / 2 = 5
// 3
// 10
// 7
// 11
// Done with numbers
```

~~~~exercism/note
The `number` variable is declared in the `for-in` loop and is only available within the loop's scope.
```swift
let numbers = [3, 10, 7, 11]
for number in numbers {
number + 1
}
number + 1 // Error: Use of unresolved identifier 'number'
```
~~~~

## Iterating over a range

You can also iterate over a range of numbers using a `for-in` loop.
This allows you to execute a block of code a specific number of times, for example, the range `1...5` will iterate over the numbers 1, 2, 3, 4, and 5, so the loop will execute 5 times.
Sometimes you might want to iterate over indexes, in a datastructure like an array, then you can use a `0..<array.count` range.

```swift
let numbers = [3, 10, 7, 11]

for i in 0..<numbers.count {
print(numbers[i])
}

// prints:
// 3
// 10
// 7
// 11
```

## Iterating over a string

You can also iterate over a string using a `for-in` loop.
This allows you to iterate over each character in the string, and note specifically that the type given in the loop is a `Character`.

```swift
let message = "Hello!"

for character in message {
print(character)
}

// prints:
// H
// e
// l
// l
// o
// !
```

## stride

Swift also provides a `stride` function that allows you to create a sequence over a range with a specific step.
Which can be then iterated over using a `for-in` loop.
`stride` is defined as [`stride(from:to:by:)`][stride-to] or [`stride(from:through:by:)`][stride-through], the first one is exclusive and the second one is inclusive.

```swift
for i in stride(from: 0, to: 10, by: 2) {
print(i)
}

// prints:
// 0
// 2
// 4
// 6
// 8
```

Note that the `to` parameter is exclusive, so the loop will iterate until the number before the `to` parameter, while the `through` parameter is inclusive, so in this case it would also include the `10`.

[stride-to]: https://developer.apple.com/documentation/swift/stride(from:to:by:)
[stride-through]: https://developer.apple.com/documentation/swift/stride(from:through:by:)
[for-loops]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow/#For-In-Loops
12 changes: 11 additions & 1 deletion concepts/for-loops/links.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
[]
[
{
"url": "https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow/#For-In-Loops",
"description": "Swift Book: For-In Loops"
},
{
"url": "https://developer.apple.com/documentation/swift/stride(from:through:by:)",
"description": "Swift docs: stride"
}
]

26 changes: 14 additions & 12 deletions exercises/concept/bird-watcher/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# Hints

## General
## 1. Determine the total number of birds that you counted so far

- The syntax for defining a closure is `{ (parameter list) -> ReturnType in bodyOfClosure }`
- Refer to the exercise introduction for an example how to use a for loop to iterate over a slice.
- Use a helper variable to store the total count and increase that variable as you go through the slice.
- Think about the correct initial value for that helper variable.

## 1. Write a closure to flip two wires
## 2. Calculate the number of visiting birds in a specific week

- You can use tuple decomposition to name the individual components of a tuple
- This task is similar to the first one, you can copy your code as a starting point.
- Think about which indexes in the slice you would need to take into account for week number 1 and 2, respectively.
- Now find a general way to calculate the first and the last index that should be considered.
- With that you can set up the for loop to only iterate over the relevant section of the slice.

## 2. Write a closure to rotate the wires
## 3. Fix a counting mistake

- You can use tuple decomposition to name the individual components of a tuple

## 3. Implement a wire shuffle generator

- You can return the last bit of a number, `n`, by computing `n % 2`
- You can remove the last bit of a number, `n`, by computing `n / 2`
- You need to make variable copies of function and closure parameters if you want to mutate them.
- Again you need to set up a for loop to iterate over the whole bird count slice.
- This time you only need to visit every second entry in the slice.
- Change the step so the counter variable is increased accordingly after each iteration.
- In the body of the for loop you can use the increment operator to change the value of an element in a slice in-place.
Loading

0 comments on commit 3701026

Please sign in to comment.