mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-11 15:51:16 +00:00
first
This commit is contained in:
35
course/11-pointers/exercises/2-pointers_practice/code.go
Normal file
35
course/11-pointers/exercises/2-pointers_practice/code.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func removeProfanity(message *string) {
|
||||
// ?
|
||||
}
|
||||
|
||||
// don't touch below this line
|
||||
|
||||
func test(messages []string) {
|
||||
for _, message := range messages {
|
||||
removeProfanity(&message)
|
||||
fmt.Println(message)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
messages1 := []string{
|
||||
"well shoot, this is awful",
|
||||
"dang robots",
|
||||
"dang them to heck",
|
||||
}
|
||||
|
||||
messages2 := []string{
|
||||
"well shoot",
|
||||
"Allan is going straight to heck",
|
||||
"dang... that's a tough break",
|
||||
}
|
||||
|
||||
test(messages1)
|
||||
test(messages2)
|
||||
}
|
||||
40
course/11-pointers/exercises/2-pointers_practice/complete.go
Normal file
40
course/11-pointers/exercises/2-pointers_practice/complete.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func removeProfanity(message *string) {
|
||||
messageVal := *message
|
||||
messageVal = strings.ReplaceAll(messageVal, "dang", "****")
|
||||
messageVal = strings.ReplaceAll(messageVal, "shoot", "*****")
|
||||
messageVal = strings.ReplaceAll(messageVal, "heck", "****")
|
||||
*message = messageVal
|
||||
}
|
||||
|
||||
// don't touch below this line
|
||||
|
||||
func test(messages []string) {
|
||||
for _, message := range messages {
|
||||
removeProfanity(&message)
|
||||
fmt.Println(message)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
messages1 := []string{
|
||||
"well shoot, this is awful",
|
||||
"dang robots",
|
||||
"dang them to heck",
|
||||
}
|
||||
|
||||
messages2 := []string{
|
||||
"well shoot",
|
||||
"Allan is going straight to heck",
|
||||
"dang... that's a tough break",
|
||||
}
|
||||
|
||||
test(messages1)
|
||||
test(messages2)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
well *****, this is awful
|
||||
**** robots
|
||||
**** them to ****
|
||||
well *****
|
||||
Allan is going straight to ****
|
||||
****... that's a tough break
|
||||
45
course/11-pointers/exercises/2-pointers_practice/readme.md
Normal file
45
course/11-pointers/exercises/2-pointers_practice/readme.md
Normal file
@@ -0,0 +1,45 @@
|
||||
# Pointers
|
||||
|
||||
Pointers hold the memory address of a value.
|
||||
|
||||
The `*` syntax defines a pointer:
|
||||
|
||||
```go
|
||||
var p *int
|
||||
```
|
||||
|
||||
A pointer's zero value is `nil`
|
||||
|
||||
The & operator generates a pointer to its operand.
|
||||
|
||||
```go
|
||||
myString := "hello"
|
||||
myStringPtr = &myString
|
||||
```
|
||||
|
||||
The * dereferences a pointer to gain access to the value
|
||||
|
||||
```go
|
||||
fmt.Println(*myStringPtr) // read myString through the pointer
|
||||
*myStringPtr = "world" // set myString through the pointer
|
||||
```
|
||||
|
||||
Unlike C, Go has no [pointer arithmetic](https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm)
|
||||
|
||||
## Just because you can doesn't mean you should
|
||||
|
||||
We're doing this exercise to understand that pointers **can** be used in this way. That said, pointers can be *very* dangerous. It's generally a better idea to have your functions accept non-pointers and return new values rather than mutating pointer inputs.
|
||||
|
||||
## Assignment
|
||||
|
||||
Complete the `removeProfanity` function.
|
||||
|
||||
It should use the [strings.ReplaceAll](https://pkg.go.dev/strings#ReplaceAll) function to replace all instances of the following words in the input `message` with asterisks.
|
||||
|
||||
* "dang" -> "****"
|
||||
* "shoot" -> "*****"
|
||||
* "heck" -> "****"
|
||||
|
||||
It should *mutate* the value in the pointer and return nothing.
|
||||
|
||||
Do *not* alter the function signature.
|
||||
Reference in New Issue
Block a user