Skip to content

Commit

Permalink
feat: ✨ add a new solution for Two Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
bungambohlah committed Jun 30, 2023
1 parent f71deb2 commit 60a4675
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions codewars.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
firstnonrepeatingcharacter "codewars/solutions/first_non_repeating_character"
rectintorectangles "codewars/solutions/rect_into_rectangles"
romansnumeraldecoder "codewars/solutions/romans_numeral_decoder"
twosum "codewars/solutions/two_sum"
"fmt"
)

Expand All @@ -17,4 +18,5 @@ func main() {
fmt.Println("first_non_repeating_character :", firstnonrepeatingcharacter.FirstNonRepeating("aaabbc"))
fmt.Println("rect_into_rectangles :", rectintorectangles.RectIntoRects(22, 6))
fmt.Println("romans_numeral_decoder :", romansnumeraldecoder.Decode("IV"))
fmt.Println("two_sum :", twosum.TwoSum([]int{1, 2, 3}, 4))
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ go install github.com/onsi/ginkgo/v2/ginkgo
4. [Eureka!!](https://www.codewars.com/kata/5626b561280a42ecc50000d1)
5. [Equal Sides of an Array](https://www.codewars.com/kata/5679aa472b8f57fb8c000047)
6. [Roman Numerals Decoder](https://www.codewars.com/kata/51b6249c4612257ac0000005)
7. [Two Sum](https://www.codewars.com/kata/52c31f8e6605bcc646000082)

## Commands

Expand Down
13 changes: 13 additions & 0 deletions solutions/two_sum/mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package twosum

func TwoSum(numbers []int, target int) [2]int {
for i := 0; i < len(numbers)-1; i++ {
for j := i + 1; j < len(numbers); j++ {
if numbers[i]+numbers[j] == target {
return [2]int{i, j}
}
}
}

return [2]int{-1, -1}
}
22 changes: 22 additions & 0 deletions solutions/two_sum/two_sum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package twosum_test

import (
twosum "codewars/solutions/two_sum"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestCodewarsGoChallenges(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "TwoSum Suite")
}

var _ = Describe("Tests", func() {
It("Basic tests", func() {
Expect(twosum.TwoSum([]int{1, 2, 3}, 4)).To(Equal([2]int{0, 2}))
Expect(twosum.TwoSum([]int{1234, 5678, 9012}, 14690)).To(Equal([2]int{1, 2}))
Expect(twosum.TwoSum([]int{2, 2, 3}, 4)).To(Equal([2]int{0, 1}))
})
})

0 comments on commit 60a4675

Please sign in to comment.