aprendago
aprendago copied to clipboard
Exercício: Capítulo 17, Exercício 5 (Nível: 8)
Exercício: Capítulo 17, Exercício 5 (Nível: 8)
Use esta thread para compartilhar sua solução, discutir o exercício com os colegas e pedir ajuda caso tenha dificuldades!
Esse exercicio foi tenso, mas deu para aprender muito. 😸 ⚔️
https://play.golang.org/p/SY5rT1PdZKP
https://play.golang.org/p/cNT7CGhJCao
https://play.golang.org/p/Ry2DoU8nEBr
Fiz da forma que eu entendi no pedido realizado kkkk, "O cliente pedi um pato e voce entende que ele quer um jacare"
package main
/* - Partindo do código abaixo, ordene os []user por idade e sobrenome.
- https://play.golang.org/p/BVRZTdlUZ_
- Os valores no campo Sayings devem ser ordenados tambem, e demonstrados de maneira esteticamente harmoniosa. */
import (
"fmt"
"sort"
)
type user struct {
First string
Last string
Age int
Sayings []string
}
func main() {
u1 := user{
First: "James",
Last: "Bond",
Age: 32,
Sayings: []string{
"Shaken, not stirred",
"Youth is no guarantee of innovation",
"In his majesty's royal service",
},
}
u2 := user{
First: "Miss",
Last: "Moneypenny",
Age: 27,
Sayings: []string{
"James, it is soo good to see you",
"Would you like me to take care of that for you, James?",
"I would really prefer to be a secret agent myself.",
},
}
u3 := user{
First: "M",
Last: "Hmmmm",
Age: 54,
Sayings: []string{
"Oh, James. You didn't.",
"Dear God, what has James done now?",
"Can someone please tell me where James Bond is?",
},
}
users := []user{u1, u2, u3}
sort.SliceStable(users, func(i, j int) bool { return users[i].Age < users[j].Age })
fmt.Println("Por idade: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
for x := 0; x < len(users); x++ {
fmt.Println("\n", users[x])
}
sort.SliceStable(users, func(i, j int) bool { return users[i].Last < users[j].Last })
fmt.Println("\nPor sobrenome: +++++++++++++++++++++++++++++++++++++++++++++++++++++++")
for x := 0; x < len(users); x++ {
fmt.Println("\n", users[x])
}
fmt.Println("\nPor Sayings: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
sort.Strings(users[2].Sayings)
for y := 0; y < len(users); y++ {
fmt.Println("\n", useusers[y].Sayings)
}
}
Output
Por idade: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{Miss Moneypenny 27 [James, it is soo good to see you Would you like me to take care of that for you, James? I would really prefer to be a secret agent myself.]}
{James Bond 32 [Shaken, not stirred Youth is no guarantee of innovation In his majesty's royal service]}
{M Hmmmm 54 [Oh, James. You didn't. Dear God, what has James done now? Can someone please tell me where James Bond is?]}
Por sobrenome: +++++++++++++++++++++++++++++++++++++++++++++++++++++++
{James Bond 32 [Shaken, not stirred Youth is no guarantee of innovation In his majesty's royal service]}
{M Hmmmm 54 [Oh, James. You didn't. Dear God, what has James done now? Can someone please tell me where James Bond is?]}
{Miss Moneypenny 27 [James, it is soo good to see you Would you like me to take care of that for you, James? I would really prefer to be a secret agent myself.]}
Por Sayings: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[Shaken, not stirred Youth is no guarantee of innovation In his majesty's royal service]
[Oh, James. You didn't. Dear God, what has James done now? Can someone please tell me where James Bond is?]
[I would really prefer to be a secret agent myself. James, it is soo good to see you Would you like me to take care of that for you, James?]
Program exited.
package main
import (
"fmt"
"sort"
)
type user struct {
First string
Last string
Age int
Sayings []string
}
func main() {
u1 := user{
First: "James",
Last: "Bond",
Age: 32,
Sayings: []string{
"Shaken, not stirred",
"Youth is no guarantee of innovation",
"In his majesty's royal service",
},
}
u2 := user{
First: "Miss",
Last: "Moneypenny",
Age: 27,
Sayings: []string{
"James, it is soo good to see you",
"Would you like me to take care of that for you, James?",
"I would really prefer to be a secret agent myself.",
},
}
u3 := user{
First: "M",
Last: "Hmmmm",
Age: 54,
Sayings: []string{
"Oh, James. You didn't.",
"Dear God, what has James done now?",
"Can someone please tell me where James Bond is?",
},
}
users := []user{u1, u2, u3}
sort.SliceStable(users, func(i, j int) bool {
return users[i].Age < users[j].Age
})
sort.SliceStable(users, func(i, j int) bool {
return users[i].Last < users[j].Last
})
sort.SliceStable(users, func(i, j int) bool {
return users[i].Last < users[j].Last
})
for _, user := range users {
sort.Strings(user.Sayings)
fmt.Println("First:", user.First)
fmt.Println("Last:", user.Last)
fmt.Println("Age:", user.Age)
fmt.Println("Sayings:", user.Sayings)
}
// fmt.Println(users)
}
```go
https://play.golang.org/p/hpCaCBE5ciw
Cap. 17 – Exercícios: Nível #8 – 5 https://go.dev/play/p/qXS-6owE9hg
https://go.dev/play/p/fXXahc_H6qR
https://go.dev/play/p/gjCK0hJDYHi
package main
import (
"fmt"
"sort"
)
type ser struct {
First string
Last string
Age int
Sayings []string
}
var k = "i"
func main() {
users := []ser{
ser{First: "James", Last: "Bond", Age: 32, Sayings: []string{"Batido, não mexido", "Juventude não é garantia de inovação", "No serviço real de sua majestade"}},
ser{First: "Miss", Last: "Moneypenny", Age: 27, Sayings: []string{"James, é tão bom ver você", "Você gostaria que eu cuidasse disso para você, James?",
"Eu realmente preferiria ser um agente secreto."}},
ser{First: "M", Last: "Hmmmm", Age: 54, Sayings: []string{"Oh, James. Você não fez isso.", "Querido Deus, o que James fez agora?",
"Alguém pode me dizer onde está James Bond?"}},
}
if k == "i" {
for _, v := range users {
sort.Strings(v.Sayings)
}
sort.Sort(ordenaPorIdade(users))
mostra(users)
} else if k == "s" {
for _, v := range users {
sort.Strings(v.Sayings)
}
sort.Sort(ordenaPorsobrNome(users))
mostra(users)
}
}
type ordenaPorIdade []ser
func (f ordenaPorIdade) Len() int { return len(f) }
func (f ordenaPorIdade) Less(i, j int) bool { return f[i].Age < f[j].Age }
func (f ordenaPorIdade) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
type ordenaPorsobrNome []ser
func (h ordenaPorsobrNome) Len() int { return len(h) }
func (h ordenaPorsobrNome) Less(i, j int) bool { return h[i].Last < h[j].Last }
func (h ordenaPorsobrNome) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func mostra(m []ser) {
for i, v := range m {
fmt.Println(i, "Idade:", v.Age, "\nNome Completo:", v.First, v.Last)
for _, c := range v.Sayings {
fmt.Println(c)
}
fmt.Println()
}
}
https://go.dev/play/p/f2awFMxkNQL