Skip to content

move problem description for arrays #79

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

Merged
merged 2 commits into from
Jul 7, 2023
Merged
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
3 changes: 1 addition & 2 deletions array/add_two_numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package array

import "math"

// AddTwoNumbers adds two numbers which are represented as an array and returns the results
// In the same format. For example [2,5], and [3,5] would add up to[6,0] because 25 + 35 = 60.
// AddTwoNumbers solves the problem in O(n) time and O(1) space.
func AddTwoNumbers(num1, num2 []int) []int {
num1, num2 = equalizeLengths(num1, num2)
carry := false
Expand Down
8 changes: 8 additions & 0 deletions array/add_two_numbers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import (
"testing"
)

/*
TestAddTwoNumbers tests solution(s) with the following signature and problem description:

AddTwoNumbers(num1, num2 []int) []int

Adds two numbers which are represented as an array and returns the results
In the same format. For example [2,5], and [3,5] would add up to[6,0] because 25 + 35 = 60.
*/
func TestAddTwoNumbers(t *testing.T) {
tests := []struct {
num1, num2, sum []int
Expand Down
2 changes: 1 addition & 1 deletion array/bubble_sort.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package array

// BubbleSort implements the Bubble Sort algorithm.
// BubbleSort solves the problem in O(n^2) time and O(1) space.
func BubbleSort(input []int) {
for i := range input {
for j := range input {
Expand Down
7 changes: 7 additions & 0 deletions array/bubble_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"testing"
)

/*
TestBubbleSort tests solution(s) with the following signature and problem description:

BubbleSort(input []int)

Implements the Bubble Sort algorithm.
*/
func TestBubbleSort(t *testing.T) {
tests := []struct {
input, sorted []int
Expand Down
3 changes: 1 addition & 2 deletions array/equal_sum_subarrays.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package array

// EqualSubArrays given a list of integers returns two sub arrays that have
// equal sums without changing the order of the items in the list.
// EqualSubArrays solves the problem in O(n^2) time and O(1) space.
func EqualSubArrays(list []int) [][]int {
output := make([][]int, 0)
if len(list) < 2 {
Expand Down
8 changes: 8 additions & 0 deletions array/equal_sum_subarrays_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import (
"testing"
)

/*
TestEqualSumSubArrays tests solution(s) with the following signature and problem description:

func EqualSubArrays(list []int) [][]int {

Given a list of integers returns two sub arrays that have equal sums without changing the
order of the items in the list.
*/
func TestEqualSumSubArrays(t *testing.T) {
tests := []struct {
list []int
Expand Down
2 changes: 1 addition & 1 deletion array/find_duplicate_in_array.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package array

// FindDuplicate Finds the duplicate in a list of integers (1,n).
// FindDuplicate solves the problem in O(n) time and O(1) space.
func FindDuplicate(list []int) int {
for _, item := range list {
itemIndex := item - 1
Expand Down
7 changes: 7 additions & 0 deletions array/find_duplicate_in_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import (
"testing"
)

/*
TestFindDuplicate tests solution(s) with the following signature and problem description:

FindDuplicate(list []int) int

Finds the duplicate in a list of integers (1,n).
*/
func TestFindDuplicate(t *testing.T) {
tests := []struct {
list []int
Expand Down
3 changes: 1 addition & 2 deletions array/product_of_all_other_elements.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package array

// ProductOfAllOtherElements returns an array such that n[i] is the product of all
// elements of the array except n[i]. Division operation is not allowed.
// ProductOfAllOtherElements solves the problem in O(n) time and O(1) space.
func ProductOfAllOtherElements(list []int) []int {
if len(list) == 0 {
return list
Expand Down
8 changes: 8 additions & 0 deletions array/product_of_all_other_elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import (
"testing"
)

/*
TestProductOfAllOtherElements tests solution(s) with the following signature and problem description:

ProductOfAllOtherElements(list []int) []int

Returns an array such that n[i] is the product of all elements of the array except n[i].
Division operation is not allowed.
*/
func TestProductOfAllOtherElements(t *testing.T) {
tests := []struct {
list []int
Expand Down
6 changes: 3 additions & 3 deletions array/reverse_inplace.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package array

// ReverseInplace reverses parts of the array in place.
func ReverseInplace(nums []int, start, end int) {
// ReverseInPlace solves the problem in O(n) time and O(1) space.
func ReverseInPlace(list []int, start, end int) {
for i := start; i <= start+end/2 && i < end-i+start; i++ {
nums[i], nums[end-i+start] = nums[end-i+start], nums[i]
list[i], list[end-i+start] = list[end-i+start], list[i]
}
}
11 changes: 9 additions & 2 deletions array/reverse_inplace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import (
"testing"
)

func TestReverseInplace(t *testing.T) {
/*
TestReverseInPlace tests solution(s) with the following signature and problem description:

ReverseInPlace(list []int, start, end int)

Reverses parts of the array in place.
*/
func TestReverseInPlace(t *testing.T) {
tests := []struct {
list []int
start int
Expand All @@ -22,7 +29,7 @@ func TestReverseInplace(t *testing.T) {
}

for i, test := range tests {
ReverseInplace(test.list, test.start, test.end)
ReverseInPlace(test.list, test.start, test.end)
if !reflect.DeepEqual(test.list, test.reversed) {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.reversed, test.list)
}
Expand Down
10 changes: 5 additions & 5 deletions array/rotate_k_steps.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package array

// RotateKSteps rotates a given array k steps.
func RotateKSteps(nums []int, k int) {
ReverseInplace(nums, 0, len(nums)-k-1)
ReverseInplace(nums, 0, len(nums)-1)
ReverseInplace(nums, 0, k-1)
// RotateKSteps solves problem in O(n) time and O(1) space.
func RotateKSteps(list []int, k int) {
ReverseInPlace(list, 0, len(list)-k-1)
ReverseInPlace(list, 0, len(list)-1)
ReverseInPlace(list, 0, k-1)
}
7 changes: 7 additions & 0 deletions array/rotate_k_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"testing"
)

/*
TestRotateKSteps tests solution(s) with the following signature and problem description:

RotateKSteps(list []int, k int)

Rotates a given array k steps.
*/
func TestRotateKSteps(t *testing.T) {
tests := []struct {
list []int
Expand Down
1 change: 1 addition & 0 deletions array/zero_sum_triplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package array

import "sort"

// ZeroSumTriplets solves the problem in O(n^2) time and O(1) space.
func ZeroSumTriplets(list []int) [][]int {
output := make([][]int, 0)
if len(list) < 3 {
Expand Down
7 changes: 7 additions & 0 deletions array/zero_sum_triplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"testing"
)

/*
TestZeroSumTriplets tests solution(s) with the following signature and problem description:

ZeroSumTriplets(list []int) [][]int

Finds all triplets in a list that sum to zero.
*/
func TestZeroSumTriplets(t *testing.T) {
tests := []struct {
list []int
Expand Down