mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-17 10:41:17 +00:00
first
This commit is contained in:
26
course/7-loops/exercises/1-intro/code.go
Normal file
26
course/7-loops/exercises/1-intro/code.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func bulkSend(numMessages int) float64 {
|
||||
// ?
|
||||
}
|
||||
|
||||
// don't edit below this line
|
||||
|
||||
func test(numMessages int) {
|
||||
fmt.Printf("Sending %v messages\n", numMessages)
|
||||
cost := bulkSend(numMessages)
|
||||
fmt.Printf("Bulk send complete! Cost = %.2f\n", cost)
|
||||
fmt.Println("===============================================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10)
|
||||
test(20)
|
||||
test(30)
|
||||
test(40)
|
||||
test(50)
|
||||
}
|
||||
30
course/7-loops/exercises/1-intro/complete.go
Normal file
30
course/7-loops/exercises/1-intro/complete.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func bulkSend(numMessages int) float64 {
|
||||
totalCost := 0.0
|
||||
for i := 0; i < numMessages; i++ {
|
||||
totalCost += 1 + (float64(i) * 0.01)
|
||||
}
|
||||
return totalCost
|
||||
}
|
||||
|
||||
// don't edit below this line
|
||||
|
||||
func test(numMessages int) {
|
||||
fmt.Printf("Sending %v messages\n", numMessages)
|
||||
cost := bulkSend(numMessages)
|
||||
fmt.Printf("Bulk send complete! Cost = %.2f\n", cost)
|
||||
fmt.Println("===============================================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10)
|
||||
test(20)
|
||||
test(30)
|
||||
test(40)
|
||||
test(50)
|
||||
}
|
||||
15
course/7-loops/exercises/1-intro/expected.txt
Normal file
15
course/7-loops/exercises/1-intro/expected.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
Sending 10 messages
|
||||
Bulk send complete! Cost = 10.45
|
||||
===============================================================
|
||||
Sending 20 messages
|
||||
Bulk send complete! Cost = 21.90
|
||||
===============================================================
|
||||
Sending 30 messages
|
||||
Bulk send complete! Cost = 34.35
|
||||
===============================================================
|
||||
Sending 40 messages
|
||||
Bulk send complete! Cost = 47.80
|
||||
===============================================================
|
||||
Sending 50 messages
|
||||
Bulk send complete! Cost = 62.25
|
||||
===============================================================
|
||||
41
course/7-loops/exercises/1-intro/readme.md
Normal file
41
course/7-loops/exercises/1-intro/readme.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# Loops in Go
|
||||
|
||||
The basic loop in Go is written in standard C-like syntax:
|
||||
|
||||
```go
|
||||
for INITIAL; CONDITION; AFTER{
|
||||
// do something
|
||||
}
|
||||
```
|
||||
|
||||
`INITIAL` is run once at the beginning of the loop and can create
|
||||
variables within the scope of the loop.
|
||||
|
||||
`CONDITION` is checked before each iteration. If the condition doesn't pass
|
||||
then the loop breaks.
|
||||
|
||||
`AFTER` is run after each iteration.
|
||||
|
||||
For example:
|
||||
|
||||
```go
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Println(i)
|
||||
}
|
||||
// Prints 0 through 9
|
||||
```
|
||||
|
||||
## Assignment
|
||||
|
||||
At Textio we have a dynamic formula for determining how much a batch of bulk messages costs to send.
|
||||
|
||||
### Complete the `bulkSend()` function
|
||||
|
||||
This function should return the total cost (as a `float64`) to send a batch of `numMessages` messages. Each message costs `1.0`, plus an additional fee. The fee structure is:
|
||||
|
||||
* 1st message: `1.0 + 0.00`
|
||||
* 2nd message: `1.0 + 0.01`
|
||||
* 3rd message: `1.0 + 0.02`
|
||||
* 4th message: `1.0 + 0.03`
|
||||
|
||||
Use a loop to calculate the total cost and return it.
|
||||
26
course/7-loops/exercises/2-omit_condition/code.go
Normal file
26
course/7-loops/exercises/2-omit_condition/code.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func maxMessages(thresh float64) int {
|
||||
// ?
|
||||
}
|
||||
|
||||
// don't edit below this line
|
||||
|
||||
func test(thresh float64) {
|
||||
fmt.Printf("Threshold: %.2f\n", thresh)
|
||||
max := maxMessages(thresh)
|
||||
fmt.Printf("Maximum messages that can be sent: = %v\n", max)
|
||||
fmt.Println("===============================================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10.00)
|
||||
test(20.00)
|
||||
test(30.00)
|
||||
test(40.00)
|
||||
test(50.00)
|
||||
}
|
||||
32
course/7-loops/exercises/2-omit_condition/complete.go
Normal file
32
course/7-loops/exercises/2-omit_condition/complete.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func maxMessages(thresh float64) int {
|
||||
totalCost := 0.0
|
||||
for i := 0; ; i++ {
|
||||
totalCost += 1 + (float64(i) * 0.01)
|
||||
if totalCost > thresh {
|
||||
return i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// don't edit below this line
|
||||
|
||||
func test(thresh float64) {
|
||||
fmt.Printf("Threshold: %.2f\n", thresh)
|
||||
max := maxMessages(thresh)
|
||||
fmt.Printf("Maximum messages that can be sent: = %v\n", max)
|
||||
fmt.Println("===============================================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10.00)
|
||||
test(20.00)
|
||||
test(30.00)
|
||||
test(40.00)
|
||||
test(50.00)
|
||||
}
|
||||
15
course/7-loops/exercises/2-omit_condition/expected.txt
Normal file
15
course/7-loops/exercises/2-omit_condition/expected.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
Threshold: 10.00
|
||||
Maximum messages that can be sent: = 9
|
||||
===============================================================
|
||||
Threshold: 20.00
|
||||
Maximum messages that can be sent: = 18
|
||||
===============================================================
|
||||
Threshold: 30.00
|
||||
Maximum messages that can be sent: = 26
|
||||
===============================================================
|
||||
Threshold: 40.00
|
||||
Maximum messages that can be sent: = 34
|
||||
===============================================================
|
||||
Threshold: 50.00
|
||||
Maximum messages that can be sent: = 41
|
||||
===============================================================
|
||||
24
course/7-loops/exercises/2-omit_condition/readme.md
Normal file
24
course/7-loops/exercises/2-omit_condition/readme.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# Omitting conditions from a for loop in Go
|
||||
|
||||
Loops in Go can omit sections of a for loop. For example, the `CONDITION` (middle part) can be omitted which causes the loop to run forever.
|
||||
|
||||
```go
|
||||
for INITIAL; ; AFTER {
|
||||
// do something forever
|
||||
}
|
||||
```
|
||||
|
||||
## Assignment
|
||||
|
||||
Complete the `maxMessages` function. Given a cost threshold, it should calculate the maximum number of messages that can be sent.
|
||||
|
||||
Each message costs `1.0`, plus an additional fee. The fee structure is:
|
||||
|
||||
* 1st message: `1.0 + 0.00`
|
||||
* 2nd message: `1.0 + 0.01`
|
||||
* 3rd message: `1.0 + 0.02`
|
||||
* 4th message: `1.0 + 0.03`
|
||||
|
||||
## Browser freeze
|
||||
|
||||
If you lock up your browser by creating an infinite loop that isn't breaking, just click the `cancel` button.
|
||||
37
course/7-loops/exercises/3-while/code.go
Normal file
37
course/7-loops/exercises/3-while/code.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func getMaxMessagesToSend(costMultiplier float64, maxCostInPennies int) int {
|
||||
actualCostInPennies := 1.0
|
||||
maxMessagesToSend := 0
|
||||
for {
|
||||
maxMessagesToSend++
|
||||
actualCostInPennies *= costMultiplier
|
||||
}
|
||||
return maxMessagesToSend
|
||||
}
|
||||
|
||||
// don't touch below this line
|
||||
|
||||
func test(costMultiplier float64, maxCostInPennies int) {
|
||||
maxMessagesToSend := getMaxMessagesToSend(costMultiplier, maxCostInPennies)
|
||||
fmt.Printf("Multiplier: %v\n",
|
||||
costMultiplier,
|
||||
)
|
||||
fmt.Printf("Max cost: %v\n",
|
||||
maxCostInPennies,
|
||||
)
|
||||
fmt.Printf("Max messages you can send: %v\n",
|
||||
maxMessagesToSend,
|
||||
)
|
||||
fmt.Println("====================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(1.1, 5)
|
||||
test(1.3, 10)
|
||||
test(1.35, 25)
|
||||
}
|
||||
37
course/7-loops/exercises/3-while/complete.go
Normal file
37
course/7-loops/exercises/3-while/complete.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func getMaxMessagesToSend(costMultiplier float64, maxCostInPennies int) int {
|
||||
actualCostInPennies := 1.0
|
||||
maxMessagesToSend := 0
|
||||
for actualCostInPennies <= float64(maxCostInPennies) {
|
||||
maxMessagesToSend++
|
||||
actualCostInPennies *= costMultiplier
|
||||
}
|
||||
return maxMessagesToSend
|
||||
}
|
||||
|
||||
// don't touch below this line
|
||||
|
||||
func test(costMultiplier float64, maxCostInPennies int) {
|
||||
maxMessagesToSend := getMaxMessagesToSend(costMultiplier, maxCostInPennies)
|
||||
fmt.Printf("Multiplier: %v\n",
|
||||
costMultiplier,
|
||||
)
|
||||
fmt.Printf("Max cost: %v\n",
|
||||
maxCostInPennies,
|
||||
)
|
||||
fmt.Printf("Max messages you can send: %v\n",
|
||||
maxMessagesToSend,
|
||||
)
|
||||
fmt.Println("====================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(1.1, 5)
|
||||
test(1.3, 10)
|
||||
test(1.35, 25)
|
||||
}
|
||||
12
course/7-loops/exercises/3-while/expected.txt
Normal file
12
course/7-loops/exercises/3-while/expected.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
Multiplier: 1.1
|
||||
Max cost: 5
|
||||
Max messages you can send: 17
|
||||
====================================
|
||||
Multiplier: 1.3
|
||||
Max cost: 10
|
||||
Max messages you can send: 9
|
||||
====================================
|
||||
Multiplier: 1.35
|
||||
Max cost: 25
|
||||
Max messages you can send: 11
|
||||
====================================
|
||||
38
course/7-loops/exercises/3-while/readme.md
Normal file
38
course/7-loops/exercises/3-while/readme.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# There is no while loop in Go
|
||||
|
||||
Most programming languages have a concept of a `while` loop. Because Go allows for the omission of sections of a `for` loop, a `while` loop is just a `for` loop that only has a CONDITION.
|
||||
|
||||
```go
|
||||
for CONDITION {
|
||||
// do some stuff while CONDITION is true
|
||||
}
|
||||
```
|
||||
|
||||
For example:
|
||||
|
||||
```go
|
||||
plantHeight := 1
|
||||
for plantHeight < 5 {
|
||||
fmt.Println("still growing! current height:", plantHeight)
|
||||
plantHeight++
|
||||
}
|
||||
fmt.Println("plant has grown to ", plantHeight, "inches")
|
||||
```
|
||||
|
||||
Which prints:
|
||||
|
||||
```
|
||||
still growing! current height: 1
|
||||
still growing! current height: 2
|
||||
still growing! current height: 3
|
||||
still growing! current height: 4
|
||||
plant has grown to 5 inches
|
||||
```
|
||||
|
||||
## Assignment
|
||||
|
||||
We have an interesting new cost structure from our SMS vendor. They charge exponentially more money for each consecutive text we send! Let's write a function that can calculate how many messages we can send in a given batch given a `costMultiplier` and a `maxCostInPennies`.
|
||||
|
||||
In a nutshell, the first message costs a penny, and each message after that costs the same as the previous message multiplied by the `costMultiplier`. That gets expensive!
|
||||
|
||||
There is an infinite loop in the code! Let's add a condition to fix the bug. The loop should exit **before** incrementing `maxMessagesToSend` if the cost of the next message would go over the max cost.
|
||||
11
course/7-loops/exercises/4-loops_fizzbuzz/code.go
Normal file
11
course/7-loops/exercises/4-loops_fizzbuzz/code.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
func fizzbuzz() {
|
||||
// ?
|
||||
}
|
||||
|
||||
// don't touch below this line
|
||||
|
||||
func main() {
|
||||
fizzbuzz()
|
||||
}
|
||||
23
course/7-loops/exercises/4-loops_fizzbuzz/complete.go
Normal file
23
course/7-loops/exercises/4-loops_fizzbuzz/complete.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func fizzbuzz() {
|
||||
for i := 1; i < 101; i++ {
|
||||
if i%3 == 0 && i%5 == 0 {
|
||||
fmt.Println("fizzbuzz")
|
||||
} else if i%3 == 0 {
|
||||
fmt.Println("fizz")
|
||||
} else if i%5 == 0 {
|
||||
fmt.Println("buzz")
|
||||
} else {
|
||||
fmt.Println(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// don't touch below this line
|
||||
|
||||
func main() {
|
||||
fizzbuzz()
|
||||
}
|
||||
100
course/7-loops/exercises/4-loops_fizzbuzz/expected.txt
Normal file
100
course/7-loops/exercises/4-loops_fizzbuzz/expected.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
1
|
||||
2
|
||||
fizz
|
||||
4
|
||||
buzz
|
||||
fizz
|
||||
7
|
||||
8
|
||||
fizz
|
||||
buzz
|
||||
11
|
||||
fizz
|
||||
13
|
||||
14
|
||||
fizzbuzz
|
||||
16
|
||||
17
|
||||
fizz
|
||||
19
|
||||
buzz
|
||||
fizz
|
||||
22
|
||||
23
|
||||
fizz
|
||||
buzz
|
||||
26
|
||||
fizz
|
||||
28
|
||||
29
|
||||
fizzbuzz
|
||||
31
|
||||
32
|
||||
fizz
|
||||
34
|
||||
buzz
|
||||
fizz
|
||||
37
|
||||
38
|
||||
fizz
|
||||
buzz
|
||||
41
|
||||
fizz
|
||||
43
|
||||
44
|
||||
fizzbuzz
|
||||
46
|
||||
47
|
||||
fizz
|
||||
49
|
||||
buzz
|
||||
fizz
|
||||
52
|
||||
53
|
||||
fizz
|
||||
buzz
|
||||
56
|
||||
fizz
|
||||
58
|
||||
59
|
||||
fizzbuzz
|
||||
61
|
||||
62
|
||||
fizz
|
||||
64
|
||||
buzz
|
||||
fizz
|
||||
67
|
||||
68
|
||||
fizz
|
||||
buzz
|
||||
71
|
||||
fizz
|
||||
73
|
||||
74
|
||||
fizzbuzz
|
||||
76
|
||||
77
|
||||
fizz
|
||||
79
|
||||
buzz
|
||||
fizz
|
||||
82
|
||||
83
|
||||
fizz
|
||||
buzz
|
||||
86
|
||||
fizz
|
||||
88
|
||||
89
|
||||
fizzbuzz
|
||||
91
|
||||
92
|
||||
fizz
|
||||
94
|
||||
buzz
|
||||
fizz
|
||||
97
|
||||
98
|
||||
fizz
|
||||
buzz
|
||||
27
course/7-loops/exercises/4-loops_fizzbuzz/readme.md
Normal file
27
course/7-loops/exercises/4-loops_fizzbuzz/readme.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# Fizzbuzz
|
||||
|
||||
Go supports the standard [modulo operator](https://en.wikipedia.org/wiki/Modulo_operation):
|
||||
|
||||
```go
|
||||
7 % 3 // 1
|
||||
```
|
||||
|
||||
Logical [AND operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND):
|
||||
|
||||
```go
|
||||
true && false // false
|
||||
true && true // true
|
||||
```
|
||||
|
||||
Logical [OR operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR):
|
||||
|
||||
```go
|
||||
true || false // true
|
||||
false || false // false
|
||||
```
|
||||
|
||||
## Assignment
|
||||
|
||||
We're hiring engineers at Textio, so time to brush up on the classic "Fizzbuzz" game, a coding exercise that has been dramatically overused in coding interviews across the world.
|
||||
|
||||
Complete the `fizzbuzz` function that prints the numbers 1 to 100 inclusive each on their own line, but substitutes multiples of 3 for the text `fizz` and multiples of 5 for `buzz`. For multiples of 3 AND 5 print instead `fizzbuzz`.
|
||||
23
course/7-loops/exercises/5-continue_and_break/code.go
Normal file
23
course/7-loops/exercises/5-continue_and_break/code.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func printPrimes(max int) {
|
||||
// ?
|
||||
}
|
||||
|
||||
// don't edit below this line
|
||||
|
||||
func test(max int) {
|
||||
fmt.Printf("Primes up to %v:\n", max)
|
||||
printPrimes(max)
|
||||
fmt.Println("===============================================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10)
|
||||
test(20)
|
||||
test(30)
|
||||
}
|
||||
43
course/7-loops/exercises/5-continue_and_break/complete.go
Normal file
43
course/7-loops/exercises/5-continue_and_break/complete.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func printPrimes(max int) {
|
||||
for n := 2; n <= max; n++ {
|
||||
if n == 2 {
|
||||
fmt.Println(n)
|
||||
continue
|
||||
}
|
||||
|
||||
if n%2 == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
isPrime := true
|
||||
for i := 3; i*i <= n; i++ {
|
||||
if n%i == 0 {
|
||||
isPrime = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if isPrime {
|
||||
fmt.Println(n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// don't edit below this line
|
||||
|
||||
func test(max int) {
|
||||
fmt.Printf("Primes up to %v:\n", max)
|
||||
printPrimes(max)
|
||||
fmt.Println("===============================================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10)
|
||||
test(20)
|
||||
test(30)
|
||||
}
|
||||
28
course/7-loops/exercises/5-continue_and_break/expected.txt
Normal file
28
course/7-loops/exercises/5-continue_and_break/expected.txt
Normal file
@@ -0,0 +1,28 @@
|
||||
Primes up to 10:
|
||||
2
|
||||
3
|
||||
5
|
||||
7
|
||||
===============================================================
|
||||
Primes up to 20:
|
||||
2
|
||||
3
|
||||
5
|
||||
7
|
||||
11
|
||||
13
|
||||
17
|
||||
19
|
||||
===============================================================
|
||||
Primes up to 30:
|
||||
2
|
||||
3
|
||||
5
|
||||
7
|
||||
11
|
||||
13
|
||||
17
|
||||
19
|
||||
23
|
||||
29
|
||||
===============================================================
|
||||
64
course/7-loops/exercises/5-continue_and_break/readme.md
Normal file
64
course/7-loops/exercises/5-continue_and_break/readme.md
Normal file
@@ -0,0 +1,64 @@
|
||||
# Continue
|
||||
|
||||
## `continue`
|
||||
|
||||
The `continue` keyword stops the current iteration of a loop and continues to the next iteration. `continue` is a powerful way to use the "guard clause" pattern within loops.
|
||||
|
||||
```go
|
||||
for i := 0; i < 10; i++ {
|
||||
if i % 2 == 0 {
|
||||
continue
|
||||
}
|
||||
fmt.Println(i)
|
||||
}
|
||||
// 1
|
||||
// 3
|
||||
// 5
|
||||
// 7
|
||||
// 9
|
||||
```
|
||||
|
||||
## `break`
|
||||
|
||||
The `break` keyword stops the current iteration of a loop and exits the loop.
|
||||
|
||||
```go
|
||||
for i := 0; i < 10; i++ {
|
||||
if i == 5 {
|
||||
break
|
||||
}
|
||||
fmt.Println(i)
|
||||
}
|
||||
// 0
|
||||
// 1
|
||||
// 2
|
||||
// 3
|
||||
// 4
|
||||
```
|
||||
|
||||
## Assignment
|
||||
|
||||
As an easter egg, we decided to reward our users with a free text message if they send a [prime number](https://en.wikipedia.org/wiki/Prime_number) of text messages this year.
|
||||
|
||||
Complete the `printPrimes` function. It should print all of the prime numbers up to and including `max`. It should skip any numbers that are not prime.
|
||||
|
||||
Here's the psuedocode:
|
||||
|
||||
```
|
||||
printPrimes(max):
|
||||
for n in range(2, max+1):
|
||||
if n is 2:
|
||||
n is prime, print it
|
||||
if n is even:
|
||||
n is not prime, skip to next n
|
||||
for i in range (3, sqrt(n) + 1):
|
||||
if i can be multiplied into n:
|
||||
n is not prime, skip to next n
|
||||
n is prime, print it
|
||||
```
|
||||
|
||||
### Breakdown
|
||||
|
||||
* We skip even numbers because they can't be prime
|
||||
* We only check up to the square root because anything higher than the square root has no chance of multiplying evenly into `n`
|
||||
* We start checking at 2 because 1 is not prime
|
||||
Reference in New Issue
Block a user