Skip to content

Commit 983391e

Browse files
authored
move problem description for arrays (#79)
1 parent 1289e69 commit 983391e

16 files changed

+75
-18
lines changed

array/add_two_numbers.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package array
22

33
import "math"
44

5-
// AddTwoNumbers adds two numbers which are represented as an array and returns the results
6-
// In the same format. For example [2,5], and [3,5] would add up to[6,0] because 25 + 35 = 60.
5+
// AddTwoNumbers solves the problem in O(n) time and O(1) space.
76
func AddTwoNumbers(num1, num2 []int) []int {
87
num1, num2 = equalizeLengths(num1, num2)
98
carry := false

array/add_two_numbers_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import (
55
"testing"
66
)
77

8+
/*
9+
TestAddTwoNumbers tests solution(s) with the following signature and problem description:
10+
11+
AddTwoNumbers(num1, num2 []int) []int
12+
13+
Adds two numbers which are represented as an array and returns the results
14+
In the same format. For example [2,5], and [3,5] would add up to[6,0] because 25 + 35 = 60.
15+
*/
816
func TestAddTwoNumbers(t *testing.T) {
917
tests := []struct {
1018
num1, num2, sum []int

array/bubble_sort.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package array
22

3-
// BubbleSort implements the Bubble Sort algorithm.
3+
// BubbleSort solves the problem in O(n^2) time and O(1) space.
44
func BubbleSort(input []int) {
55
for i := range input {
66
for j := range input {

array/bubble_sort_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import (
55
"testing"
66
)
77

8+
/*
9+
TestBubbleSort tests solution(s) with the following signature and problem description:
10+
11+
BubbleSort(input []int)
12+
13+
Implements the Bubble Sort algorithm.
14+
*/
815
func TestBubbleSort(t *testing.T) {
916
tests := []struct {
1017
input, sorted []int

array/equal_sum_subarrays.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package array
22

3-
// EqualSubArrays given a list of integers returns two sub arrays that have
4-
// equal sums without changing the order of the items in the list.
3+
// EqualSubArrays solves the problem in O(n^2) time and O(1) space.
54
func EqualSubArrays(list []int) [][]int {
65
output := make([][]int, 0)
76
if len(list) < 2 {

array/equal_sum_subarrays_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import (
55
"testing"
66
)
77

8+
/*
9+
TestEqualSumSubArrays tests solution(s) with the following signature and problem description:
10+
11+
func EqualSubArrays(list []int) [][]int {
12+
13+
Given a list of integers returns two sub arrays that have equal sums without changing the
14+
order of the items in the list.
15+
*/
816
func TestEqualSumSubArrays(t *testing.T) {
917
tests := []struct {
1018
list []int

array/find_duplicate_in_array.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package array
22

3-
// FindDuplicate Finds the duplicate in a list of integers (1,n).
3+
// FindDuplicate solves the problem in O(n) time and O(1) space.
44
func FindDuplicate(list []int) int {
55
for _, item := range list {
66
itemIndex := item - 1

array/find_duplicate_in_array_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import (
44
"testing"
55
)
66

7+
/*
8+
TestFindDuplicate tests solution(s) with the following signature and problem description:
9+
10+
FindDuplicate(list []int) int
11+
12+
Finds the duplicate in a list of integers (1,n).
13+
*/
714
func TestFindDuplicate(t *testing.T) {
815
tests := []struct {
916
list []int

array/product_of_all_other_elements.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package array
22

3-
// ProductOfAllOtherElements returns an array such that n[i] is the product of all
4-
// elements of the array except n[i]. Division operation is not allowed.
3+
// ProductOfAllOtherElements solves the problem in O(n) time and O(1) space.
54
func ProductOfAllOtherElements(list []int) []int {
65
if len(list) == 0 {
76
return list

array/product_of_all_other_elements_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import (
55
"testing"
66
)
77

8+
/*
9+
TestProductOfAllOtherElements tests solution(s) with the following signature and problem description:
10+
11+
ProductOfAllOtherElements(list []int) []int
12+
13+
Returns an array such that n[i] is the product of all elements of the array except n[i].
14+
Division operation is not allowed.
15+
*/
816
func TestProductOfAllOtherElements(t *testing.T) {
917
tests := []struct {
1018
list []int

array/reverse_inplace.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package array
22

3-
// ReverseInplace reverses parts of the array in place.
4-
func ReverseInplace(nums []int, start, end int) {
3+
// ReverseInPlace solves the problem in O(n) time and O(1) space.
4+
func ReverseInPlace(list []int, start, end int) {
55
for i := start; i <= start+end/2 && i < end-i+start; i++ {
6-
nums[i], nums[end-i+start] = nums[end-i+start], nums[i]
6+
list[i], list[end-i+start] = list[end-i+start], list[i]
77
}
88
}

array/reverse_inplace_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import (
55
"testing"
66
)
77

8-
func TestReverseInplace(t *testing.T) {
8+
/*
9+
TestReverseInPlace tests solution(s) with the following signature and problem description:
10+
11+
ReverseInPlace(list []int, start, end int)
12+
13+
Reverses parts of the array in place.
14+
*/
15+
func TestReverseInPlace(t *testing.T) {
916
tests := []struct {
1017
list []int
1118
start int
@@ -22,7 +29,7 @@ func TestReverseInplace(t *testing.T) {
2229
}
2330

2431
for i, test := range tests {
25-
ReverseInplace(test.list, test.start, test.end)
32+
ReverseInPlace(test.list, test.start, test.end)
2633
if !reflect.DeepEqual(test.list, test.reversed) {
2734
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.reversed, test.list)
2835
}

array/rotate_k_steps.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package array
22

3-
// RotateKSteps rotates a given array k steps.
4-
func RotateKSteps(nums []int, k int) {
5-
ReverseInplace(nums, 0, len(nums)-k-1)
6-
ReverseInplace(nums, 0, len(nums)-1)
7-
ReverseInplace(nums, 0, k-1)
3+
// RotateKSteps solves problem in O(n) time and O(1) space.
4+
func RotateKSteps(list []int, k int) {
5+
ReverseInPlace(list, 0, len(list)-k-1)
6+
ReverseInPlace(list, 0, len(list)-1)
7+
ReverseInPlace(list, 0, k-1)
88
}

array/rotate_k_steps_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import (
55
"testing"
66
)
77

8+
/*
9+
TestRotateKSteps tests solution(s) with the following signature and problem description:
10+
11+
RotateKSteps(list []int, k int)
12+
13+
Rotates a given array k steps.
14+
*/
815
func TestRotateKSteps(t *testing.T) {
916
tests := []struct {
1017
list []int

array/zero_sum_triplets.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package array
22

33
import "sort"
44

5+
// ZeroSumTriplets solves the problem in O(n^2) time and O(1) space.
56
func ZeroSumTriplets(list []int) [][]int {
67
output := make([][]int, 0)
78
if len(list) < 3 {

array/zero_sum_triplets_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import (
55
"testing"
66
)
77

8+
/*
9+
TestZeroSumTriplets tests solution(s) with the following signature and problem description:
10+
11+
ZeroSumTriplets(list []int) [][]int
12+
13+
Finds all triplets in a list that sum to zero.
14+
*/
815
func TestZeroSumTriplets(t *testing.T) {
916
tests := []struct {
1017
list []int

0 commit comments

Comments
 (0)