-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6567 from qwik-zgheib/main
Reto #26-30 - go
- Loading branch information
Showing
5 changed files
with
371 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
/* create file with name: <any_name>_test.go */ | ||
|
||
func TestHasFriday13th(t *testing.T) { | ||
detector := NewSimpleFriday13thDetector() | ||
|
||
tests := []struct { | ||
month int | ||
year int | ||
expected bool | ||
expectedErr bool | ||
}{ | ||
{10, 2023, false, false}, /* 13th October 2023 is not a Friday */ | ||
{1, 2023, false, false}, /* 13th January 2023 is a Friday */ | ||
{13, 2023, false, true}, /* Invalid month */ | ||
{0, 2023, false, true}, /* Invalid month */ | ||
{5, 2022, true, false}, /* 13th May 2022 is a Friday */ | ||
{6, 2022, false, false}, /* 13th June 2022 is not a Friday */ | ||
{11, 2023, true, false}, /* 13th November 2023 is a Friday */ | ||
{-1, 2023, false, true}, /* Invalid month */ | ||
{4, -1, false, true}, /* Invalid year */ | ||
{8, 1999, true, false}, /* 13th August 1999 is a Friday */ | ||
} | ||
|
||
for _, test := range tests { | ||
result, err := detector.HasFriday13th(test.month, test.year) | ||
if test.expectedErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, test.expected, result) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type Printer interface { | ||
Print(count int) | ||
} | ||
|
||
type ConsolePrinter struct{} | ||
|
||
func (cp *ConsolePrinter) Print(count int) { | ||
fmt.Println(count) | ||
} | ||
|
||
type Countdown struct { | ||
start int | ||
interval int | ||
printer Printer | ||
} | ||
|
||
func NewCountdown(start, interval int, printer Printer) (*Countdown, error) { | ||
if start <= 0 || interval <= 0 { | ||
return nil, fmt.Errorf("invalid start or interval") | ||
} | ||
|
||
return &Countdown{ | ||
start: start, | ||
interval: interval, | ||
printer: printer, | ||
}, nil | ||
} | ||
|
||
func (cd *Countdown) Start() { | ||
for i := cd.start; i >= 0; i-- { | ||
cd.printer.Print(i) | ||
time.Sleep(time.Duration(cd.interval) * time.Second) | ||
} | ||
} | ||
|
||
func main() { | ||
printer := &ConsolePrinter{} | ||
countdown, err := NewCountdown(10, 1, printer) | ||
if err != nil { | ||
fmt.Println("Err:", err) | ||
return | ||
} | ||
|
||
countdown.Start() | ||
} |
79 changes: 79 additions & 0 deletions
79
Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/go/qwik-zgheib.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
/* | ||
type MathExpressionValidator interface { | ||
Validate(expression string) bool | ||
} | ||
*/ | ||
|
||
type SimpleMathExpressionValidator struct{} | ||
|
||
func NewSimpleMathExpressionValidator() *SimpleMathExpressionValidator { | ||
return &SimpleMathExpressionValidator{} | ||
} | ||
|
||
func (v *SimpleMathExpressionValidator) Validate(expression string) bool { | ||
regex := regexp.MustCompile(`^-?\d+(\.\d+)?([ \t]+[+\-*/%][ \t]+-?\d+(\.\d+)?)*$`) | ||
return regex.MatchString(expression) | ||
} | ||
|
||
func ValidateMathExpression(expression string) bool { | ||
validator := NewSimpleMathExpressionValidator() | ||
return validator.Validate(expression) | ||
} | ||
|
||
func main() { | ||
expressions := []string{ | ||
"5 + 6 / 7 - 4", | ||
"5 a 6", | ||
"-3.5 * 2 + 4.2 / -1.1", | ||
"10 +", | ||
"5 5 + 6", | ||
} | ||
|
||
for _, expr := range expressions { | ||
valid := ValidateMathExpression(expr) | ||
fmt.Printf("Expression: \"%s\" is valid --> %t\n", expr, valid) | ||
} | ||
} | ||
|
||
|
||
/* | ||
* if use to test: create file <name>_test.go, with this content and execute with go test -v | ||
* install package <github.com/stretchr/testify/assert> with go get -u github.com/stretchr/testify/assert | ||
package main | ||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
func TestValidateMathExpression(t *testing.T) { | ||
tests := []struct { | ||
expression string | ||
expected bool | ||
}{ | ||
{"5 + 6 / 7 - 4", true}, | ||
{"5 a 6", false}, | ||
{"-3.5 * 2 + 4.2 / -1.1", true}, | ||
{"10 +", false}, | ||
{"5 5 + 6", false}, | ||
{"10 / 2 - 3 * 4", true}, | ||
{"-10 + -20 - -30", true}, | ||
{"1 + 1.1.1", false}, | ||
} | ||
for _, test := range tests { | ||
result := ValidateMathExpression(test.expression) | ||
assert.Equal(t, test.expected, result) | ||
} | ||
} | ||
*/ |
97 changes: 97 additions & 0 deletions
97
Retos/Reto #29 - EL CARÁCTER INFILTRADO [Fácil]/go/qwik-zgheib.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
/* | ||
type StringComparator interface { | ||
Compare(str1, str2 string) ([]string, error) | ||
} | ||
*/ | ||
|
||
type SimpleStringComparator struct{} | ||
|
||
func NewSimpleStringComparator() *SimpleStringComparator { | ||
return &SimpleStringComparator{} | ||
} | ||
|
||
func (c *SimpleStringComparator) Compare(str1, str2 string) ([]string, error) { | ||
if len(str1) != len(str2) { | ||
return nil, fmt.Errorf("strings must be of equal length") | ||
} | ||
|
||
var differences []string | ||
for i := 0; i < len(str1); i++ { | ||
if str1[i] != str2[i] { | ||
differences = append(differences, string(str1[i])) | ||
} | ||
} | ||
|
||
return differences, nil | ||
} | ||
|
||
func FindDifferences(str1, str2 string) ([]string, error) { | ||
comparator := NewSimpleStringComparator() | ||
return comparator.Compare(str1, str2) | ||
} | ||
|
||
func main() { | ||
str1 := "I'm maher majar" | ||
str2 := "I'm mahar majer" | ||
|
||
differences, err := FindDifferences(str1, str2) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
|
||
fmt.Printf("Differences between \"%s\" and \"%s\": %v\n", str1, str2, differences) | ||
|
||
str3 := "I'm.Maher Majar" | ||
str4 := "I'm maher majar" | ||
|
||
differences, err = FindDifferences(str3, str4) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
|
||
fmt.Printf("Differences between \"%s\" and \"%s\": %v\n", str3, str4, differences) | ||
} | ||
|
||
/** | ||
* if use to test: create file <name>_test.go, with this content and execute with go test -v | ||
* otherwise install package testify with: go get github.com/stretchr/testify/assert | ||
package main | ||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
func TestFindDifferences(t *testing.T) { | ||
tests := []struct { | ||
str1 string | ||
str2 string | ||
expected []string | ||
shouldFail bool | ||
}{ | ||
{"I'm maher majar", "I'm mahar majer", []string{"e", "a"}, false}, | ||
{"I'm.Maher Majar", "I'm maher majar", []string{".", "M", "M"}, false}, | ||
{"abc", "abcd", nil, true}, | ||
} | ||
for _, test := range tests { | ||
result, err := FindDifferences(test.str1, test.str2) | ||
if test.shouldFail { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.expected, result) | ||
} | ||
} | ||
} | ||
*/ |
102 changes: 102 additions & 0 deletions
102
Retos/Reto #30 - EL TECLADO T9 [Media]/go/qwik-zgheib.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type T9Translator interface { | ||
Translate(input string) string | ||
} | ||
|
||
type BasicT9Translator struct { | ||
t9Map map[string]string | ||
} | ||
|
||
func NewBasicT9Translator() *BasicT9Translator { | ||
return &BasicT9Translator{ | ||
t9Map: map[string]string{ | ||
"2": "A", "22": "B", "222": "C", | ||
"3": "D", "33": "E", "333": "F", | ||
"4": "G", "44": "H", "444": "I", | ||
"5": "J", "55": "K", "555": "L", | ||
"6": "M", "66": "N", "666": "O", | ||
"7": "P", "77": "Q", "777": "R", "7777": "S", | ||
"8": "T", "88": "U", "888": "V", | ||
"9": "W", "99": "X", "999": "Y", "9999": "Z", | ||
}, | ||
} | ||
} | ||
|
||
func (t *BasicT9Translator) Translate(input string) string { | ||
blocks := strings.Split(input, "-") | ||
var result strings.Builder | ||
for _, block := range blocks { | ||
if char, exists := t.t9Map[block]; exists { | ||
result.WriteString(char) | ||
} else { | ||
result.WriteString("?") | ||
} | ||
} | ||
return result.String() | ||
} | ||
|
||
type Validator interface { | ||
Validate(input string) error | ||
} | ||
|
||
type BasicValidator struct{} | ||
|
||
func (v *BasicValidator) Validate(input string) error { | ||
blocks := strings.Split(input, "-") | ||
for _, block := range blocks { | ||
if len(block) == 0 { | ||
return fmt.Errorf("empty block found") | ||
} | ||
char := block[0] | ||
for i := 1; i < len(block); i++ { | ||
if block[i] != char { | ||
return fmt.Errorf("block '%s' contains different numbers", block) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type T9Service struct { | ||
translator T9Translator | ||
validator Validator | ||
} | ||
|
||
func NewT9Service(translator T9Translator, validator Validator) *T9Service { | ||
return &T9Service{translator: translator, validator: validator} | ||
} | ||
|
||
func (s *T9Service) Process(input string) (string, error) { | ||
if err := s.validator.Validate(input); err != nil { | ||
return "", err | ||
} | ||
return s.translator.Translate(input), nil | ||
} | ||
|
||
func main() { | ||
translator := NewBasicT9Translator() | ||
validator := &BasicValidator{} | ||
service := NewT9Service(translator, validator) | ||
|
||
input := "6-666-88-777-33-3-33-888" | ||
output, err := service.Process(input) | ||
if err != nil { | ||
fmt.Println("err:", err) | ||
return | ||
} | ||
fmt.Println("output:", output) | ||
|
||
anotherInput := "77-9-444-55" | ||
anotherOutput, err := service.Process(anotherInput) | ||
if err != nil { | ||
fmt.Println("err:", err) | ||
return | ||
} | ||
fmt.Println("another output:", anotherOutput) | ||
} |