Skip to content

Commit 8ea7e1d

Browse files
authored
Move problem description to test files (spring1843#80)
1 parent 983391e commit 8ea7e1d

File tree

154 files changed

+698
-161
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+698
-161
lines changed

backtracking/generate_parenthesis.go

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

3-
// GenerateParenthesis returns all possible variation of n pairs of valid parenthesis.
3+
// GenerateParenthesis solves the problem in O(2^n) time and O(n) space.
44
func GenerateParenthesis(n int) []string {
55
return generateParenthesisRecursive([]string{}, "", 0, 0, n)
66
}

backtracking/generate_parenthesis_test.go

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

9+
/*
10+
TestGenerateParenthesis tests solution(s) with the following signature and problem description:
11+
12+
GenerateParenthesis(n int) []string
13+
14+
Returns all possible variation of n pairs of valid parenthesis.
15+
*/
916
func TestGenerateParenthesis(t *testing.T) {
1017
tests := []struct {
1118
n int

backtracking/n_queens.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const (
88
// Chessboard represents a chessboard.
99
type Chessboard [][]int
1010

11-
// NQueens returns possible solutions to the n-queen puzzle in an n x n chessboard
12-
// where n queens are placed on the chessboard such that none attacks another.
11+
// NQueens solves the problem in O(n!) time and O(n) space.
1312
func NQueens(n int) []Chessboard {
1413
output := nQueensRecursive(0, n, make([]int, n), make(Chessboard, 0))
1514
return toPrettyChessboard(output, n)

backtracking/n_queens_test.go

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

8+
/*
9+
TestNQueens tests solution(s) with the following signature and problem description:
10+
11+
func NQueens(n int) []Chessboard
12+
13+
Returns possible solutions to the n-queen puzzle in an n x n chessboard
14+
where n queens are placed on the chessboard such that none attacks another.
15+
*/
816
func TestNQueens(t *testing.T) {
917
tests := []struct {
1018
n int

backtracking/permutations.go

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

3-
// Permutations intakes a list of numbers and returns all possible permutations of their orders
4-
// For example for {1,2} it would return {1,2}, {2,1}.
3+
// Permutations solves the problem in O(n!) time and O(n) space.
54
func Permutations(input []int) [][]int {
65
permutations := make([][]int, 0)
76
permutationsRecursive(input, 0, &permutations)

backtracking/permutations_test.go

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

8+
/*
9+
TestPermutations tests solution(s) with the following signature and problem description:
10+
11+
func Permutations(input []int) [][]int {
12+
13+
Intakes a list of numbers and returns all possible permutations of their orders
14+
For example for {1,2} it would return {1,2}, {2,1}.
15+
*/
816
func TestPermutations(t *testing.T) {
917
tests := []struct {
1018
nums []int

backtracking/phone_letter_combinations.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ var phone = map[byte]string{
1111
'9': "wxyz",
1212
}
1313

14-
// PhoneLetterCombinations intakes the digits from 2 to 9 that represent phone buttons
15-
// and returns all possible combinations of letters that could be generated from those.
14+
// PhoneLetterCombinations solves the problem in O(3^n) time and O(3^n) space.
1615
func PhoneLetterCombinations(digits string) []string {
1716
combinations := []string{}
1817
if len(digits) > 0 {

backtracking/phone_letter_combinations_test.go

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

9+
/*
10+
TestPhoneLetterCombinations tests solution(s) with the following signature and problem description:
11+
12+
func PhoneLetterCombinations(digits string) []string
13+
14+
Intakes the digits from 2 to 9 that represent phone buttons
15+
and returns all possible combinations of letters that could be generated from those.
16+
*/
917
func TestPhoneLetterCombinations(t *testing.T) {
1018
tests := []struct {
1119
digits string

backtracking/sudoku.go

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

3-
// Sudoku solves a given partially filled or empty 9x9 Soduku board by placing
4-
// integers between 1 and 9 in empty spot designated by 0 such that
5-
// In each row, column, and 3x3 sub square the values are unique.
3+
// Sudoku solves the problem in O(9^(n*n)) time and O(n*n) space.
64
func Sudoku(board [][]int) bool {
75
for i := 0; i < len(board); i++ {
86
for j := 0; j < len(board[0]); j++ {

backtracking/sudoku_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ type boardAndSolution struct {
1010
solution [][]int
1111
}
1212

13+
/*
14+
TestSudoku tests solution(s) with the following signature and problem description:
15+
16+
func Sudoku(board [][]int) bool {
17+
18+
Solves a given partially filled or empty 9x9 Sudoku board by placing integers
19+
between 1 and 9 in empty spot designated by 0 such that In each row, column, and
20+
3x3 sub square the values are unique.
21+
*/
1322
func TestSudoku(t *testing.T) {
1423
tests := []boardAndSolution{
1524
testBoard1(),

bit/addition_without_operators.go

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

3-
// Add adds to numbers without using any arithmetic operators.
3+
// Add solves the problem in O(1) time and O(1) space.
44
func Add(x, y int) int {
55
for y != 0 {
66
sum := x ^ y

bit/addition_without_operators_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ package bit
22

33
import "testing"
44

5+
/*
6+
TestAdd tests solution(s) with the following signature and problem description:
7+
8+
func Add(x, y int) int
9+
10+
Adds to numbers without using any arithmetic operators.
11+
*/
512
func TestAdd(t *testing.T) {
613
tests := []struct {
714
a, b int

bit/division_without_operators.go

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

3-
// Divide divides two numbers without using division or multiplication symbols.
3+
// Divide solves the problem in O(1) time and O(1) space.
44
func Divide(x, y int) int {
55
quotient := 0
66
power := uint64(32)

bit/division_without_operators_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ package bit
22

33
import "testing"
44

5+
/*
6+
TestDivision tests solution(s) with the following signature and problem description:
7+
8+
func Divide(x, y int) int {
9+
10+
Divides two numbers without using division or multiplication symbols.
11+
*/
512
func TestDivision(t *testing.T) {
613
tests := []struct {
714
a, b int

bit/find_anagrams.go

-1
This file was deleted.

bit/find_anagrams_test.go

-1
This file was deleted.

bit/max_function_without_conditions.go

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

3-
// Max returns the maximum of two numbers without using if conditions.
3+
// Max solves the problem in O(1) time and O(1) space.
44
func Max(x, y int) int {
55
k := 1 ^ (((x - y) >> 31) & 0x1)
66
q := 1 ^ k

bit/max_function_without_conditions_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ package bit
22

33
import "testing"
44

5+
/*
6+
TestMax tests solution(s) with the following signature and problem description:
7+
8+
func Max(x, y int) int
9+
10+
Returns the maximum of two numbers without using if conditions.
11+
*/
512
func TestMax(t *testing.T) {
613
tests := []struct {
714
a, b, max int

bit/middle_without_division.go

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

3-
// MiddleWithoutDivision finds the middle integer between two integers.
3+
// MiddleWithoutDivision solves the problem in O(1) time and O(1) space.
44
func MiddleWithoutDivision(min, max int) int {
55
return (min + max) >> 1
66
}

bit/middle_without_division_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ package bit
22

33
import "testing"
44

5+
/*
6+
TestMiddleWithoutDivision tests solution(s) with the following signature and problem description:
7+
8+
func MiddleWithoutDivision(min, max int)
9+
10+
Finds the middle integer between two integers.
11+
*/
512
func TestMiddleWithoutDivision(t *testing.T) {
613
tests := []struct {
714
a, b, mid int

bit/oddly_repeated_number.go

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

3-
// OddlyRepeatedNumber repeated number finds an element in a given array that is
4-
// repeated an odd number of times.
3+
// OddlyRepeatedNumber solves the problem in O(n) time and O(1) space.
54
func OddlyRepeatedNumber(list []int) int {
65
if len(list) == 0 {
76
return -1

bit/oddly_repeated_number_test.go

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

7+
/*
8+
TestOddlyRepeatedNumber tests solution(s) with the following signature and problem description:
9+
10+
func OddlyRepeatedNumber(list []int) int
11+
12+
Repeated number finds an element in a given array that is repeated an
13+
odd number of times.
14+
*/
715
func TestOddlyRepeatedNumber(t *testing.T) {
816
tests := []struct {
917
list []int

dnc/binary_search.go

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

3-
// BinarySearch finds the index a given number in an ordered slice of sorted integers
4-
// using Binary Search.
3+
// BinarySearch solves the problem in O(log n) time and O(1) space.
54
func BinarySearch(list []int, search int) int {
65
return binarySearchRecursive(list, 0, len(list), search)
76
}

dnc/binary_search_test.go

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

7+
/*
8+
TestBinarySearch tests solution(s) with the following signature and problem description:
9+
10+
func BinarySearch(list []int, search int) int
11+
12+
Finds the index a given number in an ordered slice of sorted integers
13+
using Binary Search.
14+
*/
715
func TestBinarySearch(t *testing.T) {
816
tests := []struct {
917
sortedNumbers []int

dnc/merge_sort.go

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

3-
// MergeSort sorts a list of integers using Merge Sort.
3+
// MergeSort solves the problem in O(n log n) time and O(n) space.
44
func MergeSort(list []int) []int {
55
if len(list) <= 1 {
66
return list

dnc/merge_sort_test.go

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

8+
/*
9+
TestMergeSort tests solution(s) with the following signature and problem description:
10+
11+
func MergeSort(list []int) []int
12+
13+
Sorts a list of integers using Merge Sort.
14+
*/
815
func TestMergeSort(t *testing.T) {
916
tests := []struct {
1017
list []int

dnc/rate_limit.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
var rateLimitEvents []int64
88

9+
// IsAllowed solves the problem in O(1) time and O(n) space.
910
func IsAllowed(limitPerSecond int) bool {
1011
now := time.Now().Unix()
1112
removeOldERateLimitEvents(now)

dnc/rate_limit_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import (
66
"time"
77
)
88

9+
/*
10+
TestRateLimiter tests solution(s) with the following signature and problem description:
11+
12+
IsAllowed(limitPerSecond int) bool
13+
14+
Returns wether or not the caller is allowed to perform an action. The caller is allowed
15+
to perform an action if the number of actions performed in the last second is less than
16+
or equal to limitPerSecond.
17+
*/
918
func TestRateLimiter(t *testing.T) {
1019
tests := []struct {
1120
limitPerSecond int

dnc/square_root.go

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

3-
// SquareRoot binary search to find the square root of a number up to a precision point.
3+
// SquareRoot solves the problem in O(log n) time and O(1) space.
44
func SquareRoot(number, precision int) float64 {
55
start := 0
66
end := number

dnc/square_root_test.go

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

8+
/*
9+
TestSquareRoot tests solution(s) with the following signature and problem description:
10+
11+
func SquareRoot(number, precision int) float64
12+
13+
Uses Binary search to find the square root of a number up to a precision point.
14+
*/
815
func TestSquareRoot(t *testing.T) {
916
tests := []struct {
1017
number int

dnc/towers_of_hanoi.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ package dnc
22

33
type moves [][2]int
44

5-
// TowerOfHanoi will return the moves it takes to move all disks from start to end with respect
6-
// to the rules of the Tower of Hanoi game where:
7-
// n is the number of disks stacked on top of each other
8-
// heavier disks can never be placed on a lighter disk
9-
// There are 3 towers, all disks are initially places at the start tower.
5+
// TowerOfHanoi solves the problem in O(2^n) time and O(n) space.
106
func TowerOfHanoi(n, start, end int) [][2]int {
117
m := make(moves, 0)
128
towerOfHanoiRecursive(n, start, end, &m)

dnc/towers_of_hanoi_test.go

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

8+
/*
9+
TestTowerOfHanoi tests solution(s) with the following signature and problem description:
10+
11+
func TowerOfHanoi(n, start, end int) [][2]int {
12+
13+
Returns the moves it takes to move all disks from start to end with respect
14+
to the rules of the Tower of Hanoi game where:
15+
n is the number of disks stacked on top of each other
16+
heavier disks can never be placed on a lighter disk
17+
There are 3 towers, all disks are initially places at the start tower.
18+
*/
819
func TestTowerOfHanoi(t *testing.T) {
920
tests := []struct {
1021
n, start, end int

dp/house_robber.go

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

3-
// MaxHouseRobber given an array representing the amount of wealth inside a house
4-
// and given that the robber can steal only non-consecutive houses
5-
// returns the maximum amount of wealth the robber can steal.
3+
// MaxHouseRobber solves the problem in O(n) time and O(1) space.
64
func MaxHouseRobber(wealth []int) int {
75
if len(wealth) == 0 {
86
return 0

dp/house_robber_test.go

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

7+
/*
8+
TestMaxHouseRobber tests solution(s) with the following signature and problem description:
9+
10+
func MaxHouseRobber(wealth []int) int
11+
12+
given an array representing the amount of wealth inside a house
13+
and given that the robber can steal only non-consecutive houses
14+
returns the maximum amount of wealth the robber can steal.
15+
*/
716
func TestMaxHouseRobber(t *testing.T) {
817
tests := []struct {
918
houses []int

dp/minimum_deletion_to_make_palindrome.go

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

3-
// MinimumDeletionsToMakePalindrome returns how many deletions can be done in the input string
4-
// to make it a palindrome.
3+
// MinimumDeletionsToMakePalindrome solves the problem in O(n^2) time and O(n^2) space.
54
func MinimumDeletionsToMakePalindrome(input string) int {
65
if len(input) <= 1 {
76
return 0

0 commit comments

Comments
 (0)