From 8fc8f591c3c164bd148c31fdb982882c5c695d26 Mon Sep 17 00:00:00 2001 From: Qwik Zgheib Mossad Bashit <155261549+qwik-zgheib@users.noreply.github.com> Date: Sun, 9 Jun 2024 23:06:57 -0500 Subject: [PATCH 1/5] feat: reto #26 - go --- .../go/qwik-zgheib.go | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Retos/Reto #26 - TESTING [Media]/go/qwik-zgheib.go diff --git a/Retos/Reto #26 - TESTING [Media]/go/qwik-zgheib.go b/Retos/Reto #26 - TESTING [Media]/go/qwik-zgheib.go new file mode 100644 index 0000000000..1a50e6fc62 --- /dev/null +++ b/Retos/Reto #26 - TESTING [Media]/go/qwik-zgheib.go @@ -0,0 +1,41 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +/* create file with 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) + } +} + From 7648cd38be5585ffaec4f466890b9aebd0fe7620 Mon Sep 17 00:00:00 2001 From: Qwik Zgheib Mossad Bashit <155261549+qwik-zgheib@users.noreply.github.com> Date: Sun, 9 Jun 2024 23:20:59 -0500 Subject: [PATCH 2/5] feat: reto #27 - go --- .../go/qwik-zgheib.go" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "Retos/Reto #27 - CUENTA ATR\303\201S [Media]/go/qwik-zgheib.go" diff --git "a/Retos/Reto #27 - CUENTA ATR\303\201S [Media]/go/qwik-zgheib.go" "b/Retos/Reto #27 - CUENTA ATR\303\201S [Media]/go/qwik-zgheib.go" new file mode 100644 index 0000000000..f770c7e120 --- /dev/null +++ "b/Retos/Reto #27 - CUENTA ATR\303\201S [Media]/go/qwik-zgheib.go" @@ -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() +} From eb40b63ea01e9b710644c6d3c61e4680e3ab587f Mon Sep 17 00:00:00 2001 From: Qwik Zgheib Mossad Bashit <155261549+qwik-zgheib@users.noreply.github.com> Date: Sun, 9 Jun 2024 23:33:10 -0500 Subject: [PATCH 3/5] feat: reto #28 - go --- .../go/qwik-zgheib.go" | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 "Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/go/qwik-zgheib.go" diff --git "a/Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/go/qwik-zgheib.go" "b/Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/go/qwik-zgheib.go" new file mode 100644 index 0000000000..2be0294c31 --- /dev/null +++ "b/Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/go/qwik-zgheib.go" @@ -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 _test.go, with this content and execute with go test -v + * install package 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) + } +} +*/ \ No newline at end of file From 3850661c61be1b19700545565e77071264d15d68 Mon Sep 17 00:00:00 2001 From: Qwik Zgheib Mossad Bashit <155261549+qwik-zgheib@users.noreply.github.com> Date: Sun, 9 Jun 2024 23:46:04 -0500 Subject: [PATCH 4/5] feat: reto #29 - go --- .../go/qwik-zgheib.go" | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 "Retos/Reto #29 - EL CAR\303\201CTER INFILTRADO [F\303\241cil]/go/qwik-zgheib.go" diff --git "a/Retos/Reto #29 - EL CAR\303\201CTER INFILTRADO [F\303\241cil]/go/qwik-zgheib.go" "b/Retos/Reto #29 - EL CAR\303\201CTER INFILTRADO [F\303\241cil]/go/qwik-zgheib.go" new file mode 100644 index 0000000000..1e6a61436e --- /dev/null +++ "b/Retos/Reto #29 - EL CAR\303\201CTER INFILTRADO [F\303\241cil]/go/qwik-zgheib.go" @@ -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 _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) + } + } +} +*/ \ No newline at end of file From 8e91a5bd9d1aa3a55ad0a7c8199872c3d3b8fa5b Mon Sep 17 00:00:00 2001 From: Qwik Zgheib Mossad Bashit <155261549+qwik-zgheib@users.noreply.github.com> Date: Sun, 9 Jun 2024 23:53:31 -0500 Subject: [PATCH 5/5] feat: reto #30 - go --- .../go/qwik-zgheib.go | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 Retos/Reto #30 - EL TECLADO T9 [Media]/go/qwik-zgheib.go diff --git a/Retos/Reto #30 - EL TECLADO T9 [Media]/go/qwik-zgheib.go b/Retos/Reto #30 - EL TECLADO T9 [Media]/go/qwik-zgheib.go new file mode 100644 index 0000000000..bd27bf8422 --- /dev/null +++ b/Retos/Reto #30 - EL TECLADO T9 [Media]/go/qwik-zgheib.go @@ -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) +}