Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto #37, #38, #39, #40 - go #6295

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Retos/Reto #37 - COLORES HEX Y RGB [Media]/go/jassoncu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"fmt"
"image/color"
"regexp"
"strconv"
)

func hexToRGB(hex string) (int, int, int, error) {
// Verificar el formato del color HEX
hexPattern := regexp.MustCompile(`^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$`)
if !hexPattern.MatchString(hex) {
return 0, 0, 0, fmt.Errorf("Formato de color HEX no válido")
}

// Quitar el carácter '#' si está presente
if hex[0] == '#' {
hex = hex[1:]
}

// Convertir a valores RGB
var r, g, b int
var err error
if len(hex) == 3 {
// Manejar formato corto de 3 caracteres
r, err = strconv.Atoi(hex[0:1] + hex[0:1])
if err != nil {
return 0, 0, 0, err
}
g, err = strconv.Atoi(hex[1:2] + hex[1:2])
if err != nil {
return 0, 0, 0, err
}
b, err = strconv.Atoi(hex[2:3] + hex[2:3])
if err != nil {
return 0, 0, 0, err
}
} else {
// Manejar formato largo de 6 caracteres
r, err = strconv.Atoi(hex[0:2])
if err != nil {
return 0, 0, 0, err
}
g, err = strconv.Atoi(hex[2:4])
if err != nil {
return 0, 0, 0, err
}
b, err = strconv.Atoi(hex[4:6])
if err != nil {
return 0, 0, 0, err
}
}

return r, g, b, nil
}

func rgbToHex(r, g, b int) string {
// Convertir valores RGB a formato HEX
return fmt.Sprintf("#%02X%02X%02X", r, g, b)
}

func main() {
// Ejemplos de uso
rgbExample := color.RGBA{0, 0, 0, 255}
hexExample := "#000000"

// Convertir RGB a HEX
hexResult := rgbToHex(int(rgbExample.R), int(rgbExample.G), int(rgbExample.B))
fmt.Printf("RGB a HEX: r: %d, g: %d, b: %d -> %s\n", rgbExample.R, rgbExample.G, rgbExample.B, hexResult)

// Convertir HEX a RGB
r, g, b, err := hexToRGB(hexExample)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("HEX a RGB: hex: %s -> (r: %d, g: %d, b: %d)\n", hexExample, r, g, b)
}
}
52 changes: 52 additions & 0 deletions Retos/Reto #38 - LAS SUMAS [Media]/go/jassoncu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"sort"
)

func findCombinations(nums []int, target int) [][]int {
// Ordenar la lista para manejar duplicados de manera eficiente
sort.Ints(nums)

var result [][]int
var currentCombination []int

var backtrack func(start, remain int)
backtrack = func(start, remain int) {
if remain == 0 {
// Se encontró una combinación que suma el objetivo
result = append(result, append([]int{}, currentCombination...))
return
}

for i := start; i < len(nums); i++ {
// Evitar duplicados
if i > start && nums[i] == nums[i-1] {
continue
}

// Continuar explorando la combinación actual
currentCombination = append(currentCombination, nums[i])
backtrack(i+1, remain-nums[i])
currentCombination = currentCombination[:len(currentCombination)-1]
}
}

backtrack(0, target)
return result
}

func main() {
// Ejemplo de uso
nums := []int{1, 5, 3, 2}
target := 6

result := findCombinations(nums, target)

if len(result) > 0 {
fmt.Printf("Soluciones para el objetivo %d: %v\n", target, result)
} else {
fmt.Printf("No hay combinaciones que sumen el objetivo %d\n", target)
}
}
34 changes: 34 additions & 0 deletions Retos/Reto #39 - TRIPLES PITAGÓRICOS [Media]/go/jassoncu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
)

func findPythagoreanTriples(max int) [][]int {
var triples [][]int

for a := 1; a <= max; a++ {
for b := a + 1; b <= max; b++ {
c := int(1.0 + (float64(a*a) + float64(b*b))) // Calcular la hipotenusa c

// Verificar si es un triple pitagórico
if c <= max && c*c == a*a+b*b {
triples = append(triples, []int{a, b, c})
}
}
}

return triples
}

func main() {
// Ejemplo de uso
max := 19
result := findPythagoreanTriples(max)

if len(result) > 0 {
fmt.Printf("Triples pitagóricos menores o iguales a %d: %v\n", max, result)
} else {
fmt.Printf("No hay triples pitagóricos menores o iguales a %d\n", max)
}
}
20 changes: 20 additions & 0 deletions Retos/Reto #40 - TABLA DE MULTIPLICAR [Fácil]/go/jassoncu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
)

func main() {
var numero int

// Solicitar un número al usuario
fmt.Print("Ingrese un número para mostrar su tabla de multiplicar: ")
fmt.Scan(&numero)

// Imprimir la tabla de multiplicar
fmt.Printf("Tabla de multiplicar del %d:\n", numero)
for i := 1; i <= 10; i++ {
resultado := numero * i
fmt.Printf("%d x %d = %d\n", numero, i, resultado)
}
}