vkorbes / aprendago

Curso completo em português da linguagem Go, de zero a ninja! 🇧🇷
http://aprendago.com
1.03k stars 183 forks source link

Exercício: Capítulo 9, Exercício 5 (Nível: 4) #31

Open vkorbes opened 4 years ago

vkorbes commented 4 years ago

Exercício: Capítulo 9, Exercício 5 (Nível: 4)

Link para o vídeo:

Use esta thread para compartilhar sua solução, discutir o exercício com os colegas e pedir ajuda caso tenha dificuldades!

diegoparra commented 4 years ago

https://play.golang.org/p/9NDIac0sqKU

andersoncleyson commented 3 years ago

https://play.golang.org/p/BL3cK0afGqu

haystem commented 3 years ago
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)
}
an4kein commented 3 years ago

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:]...)
an4kein commented 3 years ago
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:]...)

alansantosmg commented 3 years ago

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)
}
Lucasmirandar commented 3 years ago

https://play.golang.org/p/UWu6oei1UtI

JPauloMoura commented 3 years ago

5. Começando com a seguinte slice:

x := []int{42, 43, 44, 45, 46, 47, 48, 49, 50, 51}

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:
```bash
slice: [42 43 44 48 49 50 51]

Resolução do Exercício


andersoncleyson commented 3 years ago
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)
}
tomashugo commented 2 years ago
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)
}
viniciussanchez commented 2 years ago

https://go.dev/play/p/EOjG6_kW697

AlissonAp commented 2 years ago

https://go.dev/play/p/-HF9KTowBKb

wfrsilva commented 2 years ago

Cap. 9 – Exercícios: Nível #4 – 5 https://go.dev/play/p/eTjb6h9cEUT

image

M3L1M commented 1 year ago

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) }

adelsonsljunior commented 1 year ago
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)

}
DominMFD commented 4 months ago

https://go.dev/play/p/yiNf1da24BS

thiagoCalazans-dev commented 4 months ago
// 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)

}
murilo-kendjy commented 4 months ago

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]

Solução no meu entendimento

Vitor-Zen commented 3 months ago

https://go.dev/play/p/iP6trhNztnY