From 9285316d791050a46c139fa4f24d913a83aa1177 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Thu, 15 Aug 2024 16:14:37 +0300 Subject: [PATCH 01/14] [SOLUTION] - Initial solution for Problem 1 --- problem1/main.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 problem1/main.go diff --git a/problem1/main.go b/problem1/main.go new file mode 100644 index 0000000..e69de29 From 009e86d444952f872245c4fe46132592cd150ec2 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Thu, 15 Aug 2024 16:18:13 +0300 Subject: [PATCH 02/14] [SOLUTION] - Initial code for Problem 1 --- problem1/main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problem1/main.go b/problem1/main.go index e69de29..eb02ec7 100644 --- a/problem1/main.go +++ b/problem1/main.go @@ -0,0 +1,22 @@ +package main + +import "fmt" + +func main() { + n := 10 + evenSum := 0 + + arr := []int{} + + for i := range n { + arr = append(arr, i) + } + + for _, num := range arr { + if num%2 == 0 { + evenSum += num + } + } + + fmt.Printf("The sum of even nums is: %d \n", evenSum) +} From cfc6a4c6bbb423f2d63bc39b14b15599525bb472 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Thu, 15 Aug 2024 16:26:08 +0300 Subject: [PATCH 03/14] [SOLUTION] - Initial solution for Problem 2 --- problem2/main.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 problem2/main.go diff --git a/problem2/main.go b/problem2/main.go new file mode 100644 index 0000000..2aeca95 --- /dev/null +++ b/problem2/main.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func main() { + arr := []int{'a', 'b', 'c', 'd', 'f'} + fmt.Printf("Missing letter: \"%s\" \n", findMissingLetter(arr)) +} + +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 string(char) +} From 731a326a69bdbb49d659b63e1b66d1e3a470c5d9 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Thu, 15 Aug 2024 16:51:10 +0300 Subject: [PATCH 04/14] [SOLUTION] - Initial solution for Problem 3 --- problem3/main.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 problem3/main.go diff --git a/problem3/main.go b/problem3/main.go new file mode 100644 index 0000000..b2e4a5c --- /dev/null +++ b/problem3/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "slices" +) + +func main() { + a := []int{'A', 'C', 'A'} + b := []int{'B', 'C', 'F'} + 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...) + + fmt.Println("Rachel collection: ", outputCollection(r)) +} + +func outputCollection(arr []int) []string { + charArr := []string{} + for _, char := range arr { + charArr = append(charArr, string(char)) + } + return charArr +} From 041f85f721fe4ad3799e7a49498c1ce64a6b9a5b Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Thu, 15 Aug 2024 21:06:07 +0300 Subject: [PATCH 05/14] [REFACTORING] - Used functions instead of old approach , useful for tests later --- problem1/main.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/problem1/main.go b/problem1/main.go index eb02ec7..ad307d5 100644 --- a/problem1/main.go +++ b/problem1/main.go @@ -4,19 +4,26 @@ import "fmt" func main() { n := 10 - evenSum := 0 - arr := []int{} - for i := range n { - arr = append(arr, i) - } + 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 { - evenSum += num + sum += num } } + return sum +} - fmt.Printf("The sum of even nums is: %d \n", evenSum) +func generateArray(arr []int, size int) []int { + for i := range size { + arr = append(arr, i) + } + return arr } From 3b07d606b0af71f87f7961bee4635bdf16856ce3 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Thu, 15 Aug 2024 21:42:58 +0300 Subject: [PATCH 06/14] [TESTING] - First tests written for problem1 --- problem1/main_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 problem1/main_test.go 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) + } +} From c826c0f7c4d4c663ac08827f28eaf6a56eae0629 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Thu, 15 Aug 2024 21:43:55 +0300 Subject: [PATCH 07/14] [FILE] - go.mod init for problem1 folder --- problem1/go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 problem1/go.mod 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 From c0cf22c843ea2643330b0380c350e7930a099eb3 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Fri, 16 Aug 2024 19:56:43 +0300 Subject: [PATCH 08/14] [TESTING & FIX] - Added tests for problem 2 and also quick modification on casting the return variable in main.go --- problem2/main.go | 5 +++-- problem2/main_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 problem2/main_test.go diff --git a/problem2/main.go b/problem2/main.go index 2aeca95..d9220ec 100644 --- a/problem2/main.go +++ b/problem2/main.go @@ -4,7 +4,8 @@ import "fmt" func main() { arr := []int{'a', 'b', 'c', 'd', 'f'} - fmt.Printf("Missing letter: \"%s\" \n", findMissingLetter(arr)) + s := fmt.Sprint(findMissingLetter(arr)) + fmt.Printf("Missing letter: \"%s\" \n", s) } func findMissingLetter(list []int) string { @@ -15,5 +16,5 @@ func findMissingLetter(list []int) string { break } } - return string(char) + 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) + } + }) + } +} From 38a6944d801340bb9ebbbddaa8e870a54fe69173 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Fri, 16 Aug 2024 20:13:44 +0300 Subject: [PATCH 09/14] [REFACTORING] - Separate collection logic on its own function --- problem3/main.go | 24 +++++++++++++++--------- problem3/main_test.go | 0 2 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 problem3/main_test.go diff --git a/problem3/main.go b/problem3/main.go index b2e4a5c..fbd086b 100644 --- a/problem3/main.go +++ b/problem3/main.go @@ -8,6 +8,20 @@ import ( func main() { a := []int{'A', 'C', 'A'} b := []int{'B', 'C', 'F'} + r := rCollection(a, b) + + fmt.Println("Rachel collection: ", outputCollection(r)) +} + +func outputCollection(arr []int) []string { + charArr := []string{} + for _, char := range arr { + charArr = append(charArr, string(char)) + } + return charArr +} + +func rCollection(a []int, b []int) []int { r := []int{} for len(a) > 0 && len(b) > 0 { @@ -23,13 +37,5 @@ func main() { r = append(r, a...) r = append(r, b...) - fmt.Println("Rachel collection: ", outputCollection(r)) -} - -func outputCollection(arr []int) []string { - charArr := []string{} - for _, char := range arr { - charArr = append(charArr, string(char)) - } - return charArr + return r } diff --git a/problem3/main_test.go b/problem3/main_test.go new file mode 100644 index 0000000..e69de29 From 72c199dc004fa4bc830a6e6176df83fa9dc1d7ba Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Fri, 16 Aug 2024 20:32:05 +0300 Subject: [PATCH 10/14] [TESTING & FIX] - Tests for problem 3 and some minor change with casting in the main file --- problem3/go.mod | 3 +++ problem3/main.go | 6 +++--- problem3/main_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 problem3/go.mod 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 index fbd086b..b224a38 100644 --- a/problem3/main.go +++ b/problem3/main.go @@ -10,13 +10,13 @@ func main() { b := []int{'B', 'C', 'F'} r := rCollection(a, b) - fmt.Println("Rachel collection: ", outputCollection(r)) + fmt.Println("Rachel collection: ", castCollection(r)) } -func outputCollection(arr []int) []string { +func castCollection(arr []int) []string { charArr := []string{} for _, char := range arr { - charArr = append(charArr, string(char)) + charArr = append(charArr, fmt.Sprint(char)) } return charArr } diff --git a/problem3/main_test.go b/problem3/main_test.go index e69de29..3b42422 100644 --- a/problem3/main_test.go +++ 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) + } + }) + } +} From 948972686bb25a7ecd41da7318e9317599ff5af1 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Fri, 16 Aug 2024 20:41:33 +0300 Subject: [PATCH 11/14] [UPDATE README] - Steps to run the Go code for these solutions --- README.md | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index e5e8ba8..c76e9f4 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,19 @@ +## 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 +30,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 | From 46e10825ccfb06f377f3bc9aa85acb22a02ef6ac Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Fri, 16 Aug 2024 20:43:48 +0300 Subject: [PATCH 12/14] [UPDATE README] - Formatted README.md --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c76e9f4..6b1c65a 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ -## Go Approach +## Go Approach\n -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. +\n +Go must be installed to run these implementations\n +To run a problem: Go to the selected folder and call : `go run main.go`\n +To run the tests, by default , the command : `go test` will run all the tests\n +To run a specific test : `go test -run main_test.go` on the main file on desired problem.\n ## Problem 1 From c002700a8a95cdc064306f7d60602353bfde9fc2 Mon Sep 17 00:00:00 2001 From: vcosmin2701 Date: Fri, 16 Aug 2024 20:45:59 +0300 Subject: [PATCH 13/14] [UPDATE README] - Formatted README.md final --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6b1c65a..2cba1da 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ ## Go Approach\n -\n -Go must be installed to run these implementations\n -To run a problem: Go to the selected folder and call : `go run main.go`\n -To run the tests, by default , the command : `go test` will run all the tests\n -To run a specific test : `go test -run main_test.go` on the main file on desired problem.\n +
+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 From 10f9ac7a04469824dadfcfd591ec09bc2d8f4a09 Mon Sep 17 00:00:00 2001 From: vcosmin2701 <38065643+vcosmin2701@users.noreply.github.com> Date: Fri, 16 Aug 2024 20:47:54 +0300 Subject: [PATCH 14/14] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2cba1da..97905cb 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -## Go Approach\n +## 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 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.