Giter Club home page Giter Club logo

Comments (15)

diegoparra avatar diegoparra commented on August 22, 2024

O index começando do 0 confunde bastante, mas no final deu tudo certo xD, Acho que a dica aqui é sempre fazer (item - 1) = valor do index.

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

from aprendago.

andersoncleyson avatar andersoncleyson commented on August 22, 2024

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

from aprendago.

haystem avatar haystem commented on August 22, 2024

fiz com len para treinar mais sobre posicionamento :

package main

import("fmt")

func main(){
 array := []string{"Ana","Joana","Siraya","Carla","Vivian","Betina","Laritssa","Bianca","Muana","Juressica"} 

  fmt.Println(array[:3])
  fmt.Println(array[4:len(array)])
  fmt.Println(array[1:7])
  fmt.Println(array[2:len(array)-1])
  fmt.Println(array[len(array)-2])
  fmt.Println(len(array))
}

from aprendago.

an4kein avatar an4kein commented on August 22, 2024

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

package main

import (
	"fmt"
)

func main() {
	slice := []int{}
	fmt.Printf("- Atribua 10 valores a ela: \n")
	slice = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	fmt.Println(slice)
	fmt.Println("- Do primeiro ao terceiro item do slice (incluindo o terceiro item!)")
	fmt.Println(slice[:3])
	fmt.Println("- Do quinto ao último item do slice (incluindo o último item!)")
	fmt.Println(slice[4:])
	fmt.Println("- Do segundo ao sétimo item do slice (incluindo o sétimo item!)")
	fmt.Println(slice[1:7])
	fmt.Println("- Do terceiro ao penúltimo item do slice (incluindo o penúltimo item!)")
	fmt.Println(slice[2:9])
	fmt.Println("- Desafio: obtenha o mesmo resultado acima utilizando a função len() para determinar o penúltimo item")
	fmt.Println(slice[2:len(slice[1:])])

}

Output

- Atribua 10 valores a ela: 
[1 2 3 4 5 6 7 8 9 10]
- Do primeiro ao terceiro item do slice (incluindo o terceiro item!)
[1 2 3]
- Do quinto ao último item do slice (incluindo o último item!)
[5 6 7 8 9 10]
- Do segundo ao sétimo item do slice (incluindo o sétimo item!)
[2 3 4 5 6 7]
- Do terceiro ao penúltimo item do slice (incluindo o penúltimo item!)
[3 4 5 6 7 8 9]
- Desafio: obtenha o mesmo resultado acima utilizando a função len() para determinar o penúltimo item
[3 4 5 6 7 8 9]

Program exited.

Depois de ver o video, me lembrei que eh possivel usar o (-) com a quantidade que voce quer subtrair usando a funcao (len).

Eu tinha usado:

fmt.Println(slice[2:len(slice[1:])])

Depois de ver o video:

fmt.Println(slice[2:len(slice)-1])

from aprendago.

alansantosmg avatar alansantosmg commented on August 22, 2024

Minha solução:

package main

import "fmt"

func main() {

	umaSlice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

	fmt.Println(umaSlice[:3])
	fmt.Println(umaSlice[4:])
	fmt.Println(umaSlice[1:7])
	fmt.Println(umaSlice[2:9])
	fmt.Println(umaSlice[3 : len(umaSlice)-1])
}

from aprendago.

Lucasmirandar avatar Lucasmirandar commented on August 22, 2024

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

from aprendago.

JPauloMoura avatar JPauloMoura commented on August 22, 2024

3. Utilizando como base o exercício anterior, utilize slicing para demonstrar os valores:

  • Do primeiro ao terceiro item do slice (incluindo o terceiro item!)
  • Do quinto ao último item do slice (incluindo o último item!)
  • Do segundo ao sétimo item do slice (incluindo o sétimo item!)
  • Do terceiro ao penúltimo item do slice (incluindo o penúltimo item!)
  • Desafio: obtenha o mesmo resultado acima utilizando a função len() para determinar o penúltimo item
package main

import "fmt"

func main() {
	slice := []int{0, 10, 20, 30, 40, 50, 70, 80, 90, 100}
	fmt.Printf("item 1-3: %v\n", slice[:3])
	fmt.Printf("item 5-10: %v\n", slice[5:])
	fmt.Printf("item 2-7: %v\n", slice[2:7])
	fmt.Printf("item 3-9: %v\n", slice[3:9])
	//usando len()
	fmt.Printf("item 3-9: %v\n", slice[3:len(slice)-1])
}

Resultado:

item 1-3: [0 10 20]
item 5-10: [50 70 80 90 100]
item 2-7: [20 30 40 50 70]
item 3-9: [30 40 50 70 80 90]
item 3-9: [30 40 50 70 80 90]  

Resolução do Exercício


from aprendago.

andersoncleyson avatar andersoncleyson commented on August 22, 2024
package main

import "fmt"

func main() {
	slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	slice1 := append(slice[:3])
	fmt.Println(slice1)

	slice2 := append(slice[4:])
	fmt.Println(slice2)

	slice3 := append(slice[1:7])
	fmt.Println(slice3)

	slice4 := append(slice[2:9])
	fmt.Println(slice4)

	slice = append(slice[2 : len(slice)-1])
	fmt.Println(slice)
}

from aprendago.

tomashugo avatar tomashugo commented on August 22, 2024
package main

import (
	"fmt"
)

func main() {
	slice := []int{1,2,3,4,5,6,7,8,9,10}
	
	fmt.Println(slice[:3])
	fmt.Println(slice[4:])
	fmt.Println(slice[1:7])
	fmt.Println(slice[2:9])
	fmt.Println(slice[2:len(slice)-1])
}

from aprendago.

CaueFarias avatar CaueFarias commented on August 22, 2024

package main

import (
"fmt"
)

func main() {

slice := []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}

for índice, número := range slice {
	fmt.Println("No índice: ", índice, "Temos o valor: ", número)
}
fmt.Println(slice[:3])
fmt.Println(slice[4:])
fmt.Println(slice[1:7])
fmt.Println(slice[2:9])
fmt.Println(slice[2 : len(slice)-1])

}

from aprendago.

viniciussanchez avatar viniciussanchez commented on August 22, 2024

https://go.dev/play/p/8bDGcsq4w3k

from aprendago.

AlissonAp avatar AlissonAp commented on August 22, 2024

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

from aprendago.

wfrsilva avatar wfrsilva commented on August 22, 2024

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

image

from aprendago.

M3L1M avatar M3L1M commented on August 22, 2024

func main() {
slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

fmt.Println(slice[:3])
fmt.Println(slice[4:])
fmt.Println(slice[1:7])
fmt.Println(slice[2:9])
fmt.Println(slice[2 : len(slice)-1])

}

from aprendago.

adelsonsljunior avatar adelsonsljunior commented on August 22, 2024
package main

import (
	"fmt"
)

func main() {

	slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	fmt.Println(slice[0:3])
	fmt.Println(slice[4:])
	fmt.Println(slice[1:7])
	fmt.Println(slice[2:9])
	fmt.Println(slice[2 : len(slice)-1])
}

from aprendago.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.