Skip to content

Golang implementation with tests for these problems #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
## Go Approach

<br/>
Go must be installed to run these implementations<br/>
To run a problem: Go to the selected folder and call : `go run main.go` <br/>
To run the tests, by default , the command : `go test` will run all the tests<br/>
To run a specific test : `go test -run main_test.go` on the main file on desired problem.<br/>

## 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.

```
Expand All @@ -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 |
3 changes: 3 additions & 0 deletions problem1/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module problem1

go 1.22.5
29 changes: 29 additions & 0 deletions problem1/main.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 additions & 0 deletions problem1/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
20 changes: 20 additions & 0 deletions problem2/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions problem2/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
3 changes: 3 additions & 0 deletions problem3/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module problem3

go 1.22.5
41 changes: 41 additions & 0 deletions problem3/main.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 31 additions & 0 deletions problem3/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}