diff --git a/README.md b/README.md index e5e8ba8..97905cb 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,20 @@ +## Go Approach + +
+Go must be installed to run these implementations
+To run a problem: Go to the selected folder and call : `go run main.go`
+To run the tests, by default , the command : `go test` will run all the tests
+To run a specific test : `go test -run main_test.go` on the main file on desired problem.
+ ## Problem 1 Given an integer array of size N, compute the sum of all even numbers in this array ## Problem 2 -Write a method that takes an array of consecutive (increasing) letters as input and that returns the missing letter in the array. +Write a method that takes an array of consecutive (increasing) letters as input and that returns the missing letter in the array. -You will always get a valid array. And it will be always exactly one letter be missing. +You will always get a valid array. And it will be always exactly one letter be missing. The length of the array will always be at least 2. The array will always contain letters in only one case. ``` @@ -23,16 +31,14 @@ They are cutting uppercase letters from newspapers, and each one of them has his One sunny day, Rachel visited Jenny and Stephanie. She saw their collections. She wondered what is the lexicographically minimal string made of those two collections. She can take a letter from a collection only when it is on the top of the stack. Rachel wants to use all of the letters in their collections. -As an example, assume Jenny has collected ```a = [A, C, A]``` and Stephanie has ```b = [B, C, F]```. The example shows the top at index ```0``` for each stack of letters. Assemble the string as follows: - - - -| Jenny | Stephanie | result | -| ------------- | ------------- | ------------- | -| ACA | BCF | | -| CA | BCF | A | -| CA | CF | AB | -| A | CF | ABC | -| | CF | ABCA | -| | F | ABCAC | -| | | ABCACF | +As an example, assume Jenny has collected `a = [A, C, A]` and Stephanie has `b = [B, C, F]`. The example shows the top at index `0` for each stack of letters. Assemble the string as follows: + +| Jenny | Stephanie | result | +| ----- | --------- | ------ | +| ACA | BCF | | +| CA | BCF | A | +| CA | CF | AB | +| A | CF | ABC | +| | CF | ABCA | +| | F | ABCAC | +| | | ABCACF | diff --git a/problem1/go.mod b/problem1/go.mod new file mode 100644 index 0000000..2ed5a2c --- /dev/null +++ b/problem1/go.mod @@ -0,0 +1,3 @@ +module problem1 + +go 1.22.5 diff --git a/problem1/main.go b/problem1/main.go new file mode 100644 index 0000000..ad307d5 --- /dev/null +++ b/problem1/main.go @@ -0,0 +1,29 @@ +package main + +import "fmt" + +func main() { + n := 10 + arr := []int{} + + arr = generateArray(arr, n) + + fmt.Printf("The sum of even nums is: %d \n", evenSumNums(arr)) +} + +func evenSumNums(arr []int) int { + sum := 0 + for _, num := range arr { + if num%2 == 0 { + sum += num + } + } + return sum +} + +func generateArray(arr []int, size int) []int { + for i := range size { + arr = append(arr, i) + } + return arr +} diff --git a/problem1/main_test.go b/problem1/main_test.go new file mode 100644 index 0000000..abfb805 --- /dev/null +++ b/problem1/main_test.go @@ -0,0 +1,27 @@ +package main + +import "testing" + +func TestArraySize(t *testing.T) { + arr := []int{} + arr = generateArray(arr, 10) + + result := len(arr) + expected := 10 + + if result != expected { + t.Errorf("Expected array size of %v, but got %v", expected, result) + } +} + +func TestSumEvenNums(t *testing.T) { + arr := []int{} + arr = generateArray(arr, 10) + + result := evenSumNums(arr) + expected := 20 + + if result != expected { + t.Errorf("Expected the result of sum : %v, but got %v", expected, result) + } +} diff --git a/problem2/main.go b/problem2/main.go new file mode 100644 index 0000000..d9220ec --- /dev/null +++ b/problem2/main.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +func main() { + arr := []int{'a', 'b', 'c', 'd', 'f'} + s := fmt.Sprint(findMissingLetter(arr)) + fmt.Printf("Missing letter: \"%s\" \n", s) +} + +func findMissingLetter(list []int) string { + char := 0 + for i := 0; i < len(list)-1; i++ { + if list[i+1]-list[i] > 1 { + char = list[i] + 1 + break + } + } + return fmt.Sprint(char) +} diff --git a/problem2/main_test.go b/problem2/main_test.go new file mode 100644 index 0000000..cc852cb --- /dev/null +++ b/problem2/main_test.go @@ -0,0 +1,25 @@ +package main + +import "testing" + +func TestMissingLetter(t *testing.T) { + testCases := []struct { + name string + input []int + expected string + }{ + {"Missing 'd'", []int{'a', 'b', 'c', 'e'}, "d"}, + {"Missing 'b'", []int{'a', 'c', 'd', 'e'}, "b"}, + {"Missing 'P'", []int{'O', 'Q', 'R', 'S'}, "P"}, + {"No missing letter", []int{'a', 'b', 'c', 'd', 'e'}, ""}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := findMissingLetter(tc.input) + if result != tc.expected { + t.Errorf("Expected %v, but got %v", tc.expected, result) + } + }) + } +} diff --git a/problem3/go.mod b/problem3/go.mod new file mode 100644 index 0000000..2b0f9e1 --- /dev/null +++ b/problem3/go.mod @@ -0,0 +1,3 @@ +module problem3 + +go 1.22.5 diff --git a/problem3/main.go b/problem3/main.go new file mode 100644 index 0000000..b224a38 --- /dev/null +++ b/problem3/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "slices" +) + +func main() { + a := []int{'A', 'C', 'A'} + b := []int{'B', 'C', 'F'} + r := rCollection(a, b) + + fmt.Println("Rachel collection: ", castCollection(r)) +} + +func castCollection(arr []int) []string { + charArr := []string{} + for _, char := range arr { + charArr = append(charArr, fmt.Sprint(char)) + } + return charArr +} + +func rCollection(a []int, b []int) []int { + r := []int{} + + for len(a) > 0 && len(b) > 0 { + if a[0] <= b[0] { + r = append(r, a[0]) + a = slices.Delete(a, 0, 1) + } else { + r = append(r, b[0]) + b = slices.Delete(b, 0, 1) + } + } + + r = append(r, a...) + r = append(r, b...) + + return r +} diff --git a/problem3/main_test.go b/problem3/main_test.go new file mode 100644 index 0000000..3b42422 --- /dev/null +++ b/problem3/main_test.go @@ -0,0 +1,31 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestCollection(t *testing.T) { + testCases := []struct { + name string + input1 []int + input2 []int + expected []string + }{ + {"Collection [A B C A C F]", []int{'A', 'C', 'A'}, []int{'B', 'C', 'F'}, []string{"A", "B", "C", "A", "C", "F"}}, + {"Collection [A B C D E F]", []int{'A', 'C', 'E'}, []int{'B', 'D', 'F'}, []string{"A", "B", "C", "D", "E", "F"}}, + {"Collection [A B C]", []int{'A'}, []int{'B', 'C'}, []string{"A", "B", "C"}}, + {"Collection [A B C]", []int{'A', 'B', 'C'}, []int{}, []string{"A", "B", "C"}}, + {"Collection [A B C]", []int{}, []int{'A', 'B', 'C'}, []string{"A", "B", "C"}}, + {"Collection [A A A B B B]", []int{'A', 'A', 'A'}, []int{'B', 'B', 'B'}, []string{"A", "A", "A", "B", "B", "B"}}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := castCollection(rCollection(tc.input1, tc.input2)) + if !reflect.DeepEqual(result, tc.expected) { + t.Errorf("Expected %v, but got %v", tc.expected, result) + } + }) + } +}