aprendago
aprendago copied to clipboard
Exercício: Capítulo 9, Exercício 5 (Nível: 4)
Exercício: Capítulo 9, Exercício 5 (Nível: 4)
Use esta thread para compartilhar sua solução, discutir o exercício com os colegas e pedir ajuda caso tenha dificuldades!
https://play.golang.org/p/9NDIac0sqKU
https://play.golang.org/p/BL3cK0afGqu
package main
import("fmt")
func main(){
x := []int{42,43,44,45,46,47,48,49,50,51}
y := append(x[:3],x[6:]...)
fmt.Println(y)
}
https://play.golang.org/p/voFHHlAsMXL
package main
import (
"fmt"
)
func main() {
fmt.Printf(`- Comece com essa slice:
- x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}`)
fmt.Println("")
fmt.Println("")
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}
fmt.Println("Utilizando slicing e append, crie uma slice y que contenha os valores:")
fmt.Println("[42, 43, 44, 48, 49, 50, 51]")
fmt.Println("")
z := x[:3]
b := x[6:]
y := append(z, b...)
fmt.Println("FINAL:", y)
}
Output
- Comece com essa slice:
- x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}
Utilizando slicing e append, crie uma slice y que contenha os valores:
[42, 43, 44, 48, 49, 50, 51]
FINAL: [42 43 44 48 49 50 51]
Program exited.
Tres maneiras legais de fazer
z := x[:3]
b := x[6:]
y := append(z, b...)
or
y := append(x[:3], x[6:]...)
or
x = append(x[:3], x[len(x)-4:]...)
package main import("fmt") func main(){ x := []int{42,43,44,45,46,47,48,49,50,51} y := append(x[:3],x[6:]...) fmt.Println(y) }
Achei interessante essa forma de declarar, economiza mais linhas:
y := append(x[:3], x[6:]...)
o que ela fez no video, tbm achei maneiro
x = append(x[:3], x[len(x)-4:]...)
My solution:
package main
import "fmt"
func main() {
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}
y := append(x[:3], x[6:]...)
fmt.Println(y)
}
https://play.golang.org/p/UWu6oei1UtI
5. Começando com a seguinte slice:
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}
- Utilizando slicing e append, crie uma slice y que contenha os valores: [42, 43, 44, 48, 49, 50, 51]
package main
import "fmt"
func main() {
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}
x = append(x[:3], x[6:]...)
fmt.Printf("slice: %v\n", x)
}
Resultado:
slice: [42 43 44 48 49 50 51]
package main
import "fmt"
func main() {
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}
y := []int{}
y = append(y, x[:3]...)
y = append(y, x[6:]...)
fmt.Println(y)
}
package main
import (
"fmt"
)
func main() {
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}
y := append(x[:3],x[6:]...)
fmt.Println(y)
}
https://go.dev/play/p/EOjG6_kW697
https://go.dev/play/p/-HF9KTowBKb
Cap. 9 – Exercícios: Nível #4 – 5 https://go.dev/play/p/eTjb6h9cEUT
func main() { x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51} y := []int{} y = append(x[:3], x[6:]...) fmt.Println(y) }
package main
import (
"fmt"
)
func main() {
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}
y := append(x[0:3], x[6:]...)
fmt.Println(y)
}
https://go.dev/play/p/yiNf1da24BS
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
//- Comece com essa slice:
// - x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}
//- Utilizando slicing e append, crie uma slice y que contenha os valores:
// - [42, 43, 44, 48, 49, 50, 51]
func main() {
x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}
leftSlice := x[:3]
size := len(x)
rightSlice := x[size-4:]
newSlice := append(leftSlice, rightSlice...)
fmt.Println(leftSlice)
fmt.Println(size)
fmt.Println(newSlice)
}
Esse exercício me pareceu um pouco ambíguo. Pelo meu entendimento a ideia é começar com o slice x
e criar um slice y
com o resultado esperado, mas ao mesmo tempo preservando o slice x
original.
Resultado esperado dos dois slices: [42, 43, 44, 45, 46, 47, 48, 49, 50, 51] [42, 43, 44, 48, 49, 50, 51]
https://go.dev/play/p/iP6trhNztnY