Skip to content

Commit

Permalink
Merge pull request #6559 from qwik-zgheib/main
Browse files Browse the repository at this point in the history
Reto #11:20-[18] - go
  • Loading branch information
Roswell468 authored Jun 6, 2024
2 parents c14966a + 274f8ef commit 28b0462
Show file tree
Hide file tree
Showing 9 changed files with 663 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Retos/Reto #11 - URL PARAMS [Fácil]/go/qwik-zgheib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
"strings"
)

type URLParameterExtractor interface {
Extract(url string) ([]string, error)
}

type SimpleURLParameterExtractor struct{}

func NewSimpleURLParameterExtractor() *SimpleURLParameterExtractor {
return &SimpleURLParameterExtractor{}
}

func (e *SimpleURLParameterExtractor) Extract(url string) ([]string, error) {
if !strings.Contains(url, "?") {
return nil, fmt.Errorf("no parameters found in URL")
}

parts := strings.Split(url, "?")
if len(parts) < 2 {
return nil, fmt.Errorf("invalid URL format")
}

paramsPart := parts[1]
params := strings.Split(paramsPart, "&")
values := []string{}

for _, param := range params {
pair := strings.Split(param, "=")
if len(pair) == 2 {
values = append(values, pair[1])
} else {
return nil, fmt.Errorf("invalid parameter format")
}
}

return values, nil
}

func main() {
url := "https://retosdeprogramacion.com?year=2023&challenge=0"
extractor := NewSimpleURLParameterExtractor()

values, err := extractor.Extract(url)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Extracted values:", values)
}
48 changes: 48 additions & 0 deletions Retos/Reto #12 - VIERNES 13 [Fácil]/go/qwik-zgheib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"time"
)

type Friday13thDetector interface {
HasFriday13th(month, year int) (bool, error)
}

type SimpleFriday13thDetector struct{}

func NewSimpleFriday13thDetector() *SimpleFriday13thDetector {
return &SimpleFriday13thDetector{}
}

func (d *SimpleFriday13thDetector) HasFriday13th(month, year int) (bool, error) {
if month < 1 || month > 12 {
return false, fmt.Errorf("invalid month: %d", month)
}
if year < 1 {
return false, fmt.Errorf("invalid year: %d", year)
}

date := time.Date(year, time.Month(month), 13, 0, 0, 0, 0, time.UTC)

return date.Weekday() == time.Friday, nil
}

func main() {
month := 10
year := 2023

detector := NewSimpleFriday13thDetector()

hasFriday13th, err := detector.HasFriday13th(month, year)
if err != nil {
fmt.Println("Error:", err)
return
}

if hasFriday13th {
fmt.Printf("There is a Friday 13th in %d/%d\n", month, year)
} else {
fmt.Printf("There is no Friday 13th in %d/%d\n", month, year)
}
}
111 changes: 111 additions & 0 deletions Retos/Reto #13 - ADIVINA LA PALABRA [Media]/go/qwik-zgheib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"fmt"
"math/rand"
"strings"
"time"
)

type WordSelector interface {
GetRandomWord() string
}

type WordGame interface {
Start()
}

type SimpleWordSelector struct {
words []string
}

func NewSimpleWordSelector(words []string) *SimpleWordSelector {
return &SimpleWordSelector{words: words}
}

func (s *SimpleWordSelector) GetRandomWord() string {
src := rand.NewSource(time.Now().UnixNano())
random := rand.New(src)
return s.words[random.Intn(len(s.words))]
}

type SimpleWordGame struct {
selector WordSelector
attempts int
}

func NewSimpleWordGame(selector WordSelector, attempts int) *SimpleWordGame {
return &SimpleWordGame{selector: selector, attempts: attempts}
}

func (swg *SimpleWordGame) Start() {
word := swg.selector.GetRandomWord()
maskedWord := maskWord(word)
fmt.Printf("Guess the word: %s\n", maskedWord)
fmt.Printf("You have %d attempts\n", swg.attempts)

for swg.attempts > 0 {
var guess string
fmt.Print("Enter a letter or the full word: ")
fmt.Scanln(&guess)
guess = strings.TrimSpace(guess)

if len(guess) == 1 {
if strings.Contains(word, guess) {
maskedWord = revealLetters(word, maskedWord, guess)
fmt.Printf("Correct! %s\n", maskedWord)
} else {
swg.attempts--
fmt.Printf("Incorrect. Attempts left: %d\n", swg.attempts)
}
} else if guess == word {
fmt.Println("Congratulations! You guessed the word!")
return
} else {
swg.attempts--
fmt.Printf("Incorrect. Attempts left: %d\n", swg.attempts)
}

if maskedWord == word {
fmt.Println("Congratulations! You guessed all the letters!")
return
}
}

fmt.Printf("You lost! The word was: %s\n", word)
}

func maskWord(word string) string {
src := rand.NewSource(time.Now().UnixNano())
rand.New(src)
lettersToHide := len(word) * 60 / 100
letters := []rune(word)
for lettersToHide > 0 {
index := rand.Intn(len(letters))
if letters[index] != '_' {
letters[index] = '_'
lettersToHide--
}
}
return string(letters)
}

func revealLetters(word, maskedWord, guess string) string {
result := []rune(maskedWord)
for i, c := range word {
if string(c) == guess {
result[i] = c
}
}
return string(result)
}

func main() {
words := []string{
"abracadabra", "banana", "dinosaur", "elephant", "giraffe",
"hippopotamus", "kangaroo", "pineapple", "rhinoceros", "umbrella",
}
selector := NewSimpleWordSelector(words)
game := NewSimpleWordGame(selector, 5)
game.Start()
}
98 changes: 98 additions & 0 deletions Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/go/qwik-zgheib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)

type NumberConverter interface {
ToOctal(decimal int) string
ToHexadecimal(decimal int) string
}

type CustomNumberConverter struct{}

func NewCustomNumberConverter() *CustomNumberConverter {
return &CustomNumberConverter{}
}

func (c *CustomNumberConverter) ToOctal(decimal int) string {
if decimal == 0 {
return "0"
}

isNegative := false
if decimal < 0 {
isNegative = true
decimal = -decimal
}

octal := ""
for decimal > 0 {
remainder := decimal % 8
octal = fmt.Sprintf("%d%s", remainder, octal)
decimal = decimal / 8
}

if isNegative {
octal = "-" + octal
}

return octal
}

func (c *CustomNumberConverter) ToHexadecimal(decimal int) string {
if decimal == 0 {
return "0"
}

isNegative := false
if decimal < 0 {
isNegative = true
decimal = -decimal
}

hexChars := "0123456789ABCDEF"
hexadecimal := ""
for decimal > 0 {
remainder := decimal % 16
hexadecimal = fmt.Sprintf("%c%s", hexChars[remainder], hexadecimal)
decimal = decimal / 16
}

if isNegative {
hexadecimal = "-" + hexadecimal
}

return hexadecimal
}

func readInput(prompt string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Print(prompt)
input, _ := reader.ReadString('\n')
return strings.TrimSpace(input)
}

func getValidNumber() int {
for {
input := readInput("Enter a valid decimal number: ")
number, err := strconv.Atoi(input)
if err == nil {
return number
}
fmt.Println("Invalid input. Please enter a valid decimal number.")
}
}

func main() {
converter := NewCustomNumberConverter()

decimalNumber := getValidNumber()
fmt.Printf("Decimal: %d\n", decimalNumber)
fmt.Printf("Octal: %s\n", converter.ToOctal(decimalNumber))
fmt.Printf("Hexadecimal: %s\n", converter.ToHexadecimal(decimalNumber))
}
Loading

0 comments on commit 28b0462

Please sign in to comment.