Skip to content

Commit 1266c63

Browse files
authored
Add details to DNC problems (#183)
1 parent 76bedf6 commit 1266c63

6 files changed

+39
-6
lines changed

dnc/binary_search_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ TestBinarySearch tests solution(s) with the following signature and problem desc
77
88
func BinarySearch(list []int, search int) int
99
10-
Given a sorted set of integers like {1,2,3,4,6}, and a target int like 4 find its
11-
position in the set like 3 using Binary Search.
10+
Given a sorted slice of integers like and a target int find the position of the target in the
11+
slice.
1212
1313
In Binary Search we start with the middle element of the set and compare it with
1414
the target. If the target is greater than the middle element we search the right
1515
half of the set, otherwise we search the left half of the set. We repeat this
1616
process until we find the target or we exhaust the set.
17+
18+
For example given {1,2,3,4,6} and 4 as a target, return 3 because 4 is at index 3.
1719
*/
1820
func TestBinarySearch(t *testing.T) {
1921
tests := []struct {

dnc/merge_sort_test.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ TestMergeSort tests solution(s) with the following signature and problem descrip
1010
1111
func MergeSort(list []int) []int
1212
13-
Given a list of integers like {3,1,2}, return a sorted set like {1,2,3} using Merge Sort.
13+
Given a slice of integers, return a sorted slice using Merge Sort.
14+
15+
Merge sort is a divide and conquer algorithm that works by dividing the
16+
input array into two halves, recursively sorting each half, and then merging
17+
the two sorted halves back together. The merge step is where the actual
18+
sorting takes place.
19+
20+
For example given {3,1,2}, return {1,2,3}. The merge sort algorithm would first
21+
divide the array into two halves: {3} and {1,2}. It would then it would divide {1,2}
22+
into {1} and {2}. Finally, it would merge the two halves back together
23+
into a single sorted array: {1,2,3}.
1424
*/
1525
func TestMergeSort(t *testing.T) {
1626
tests := []struct {

dnc/quick_sort_test.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ TestQuickSort tests solution(s) with the following signature and problem descrip
1010
1111
func QuickSort(list []int) []int
1212
13-
Given a list of integers like {3,1,2}, return a sorted set like {1,2,3} using Quick Sort.
13+
Given a slice of integers like, return a sorted slice of integers using quick sort.
14+
15+
Quick sort works by selecting a 'pivot' element from the array and partitioning
16+
the other elements into two sub-arrays, according to whether they are less than
17+
or greater than the pivot. The sub-arrays are then sorted recursively. The
18+
base case of the recursion is an array of size 0 or 1, which is always sorted.
19+
20+
For example given {3,1,2}, return a {1,2,3}. The quick sort algorithm would first
21+
choose a pivot element, say 2. It would then partition the array into two
22+
sub-arrays: {1} and {3}. It would then recursively sort the two sub-arrays
23+
and combine them with the pivot element to produce the final sorted.
1424
*/
1525
func TestQuickSort(t *testing.T) {
1626
tests := []struct {

dnc/rate_limit_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ TestRateLimiter tests solution(s) with the following signature and problem descr
1414
Given a number of allowed requests calls per second (calls/time) write an IsAllowed
1515
function which returns false if the request should be rate limited because it exceeds the
1616
limit and true if the request should be allowed.
17+
18+
For example given limit of 2 requests per second, if 3 requests are made in the first second
19+
two calls to IsAllowed would return true and the third would return false.
1720
*/
1821
func TestRateLimiter(t *testing.T) {
1922
tests := []struct {

dnc/square_root_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ TestSquareRoot tests solution(s) with the following signature and problem descri
1212
1313
Given a number and precision, return the square root of the number using the binary
1414
search algorithm.
15+
16+
For example given 9 and 3, it should return 3.
1517
*/
1618
func TestSquareRoot(t *testing.T) {
1719
tests := []struct {

dnc/towers_of_hanoi_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ TestTowerOfHanoi tests solution(s) with the following signature and problem desc
1010
1111
func TowerOfHanoi(n, start, end int) [][2]int
1212
13-
Given n, number of disks, and start and end tower, return the moves it takes to move all
14-
disks from start to end tower. The disks are stacked on top of each other with the
13+
Given n, number of disks, and start and end tower, return the number of moves it takes to
14+
move all disks from start to end tower. The disks are stacked on top of each other with the
1515
lightest being on top and heaviest being in the bottom. A heavier disk cannot be placed
1616
on a lighter disk. You can move one disk at a time.
17+
18+
For example given 2 disks, start of 1 and end of 3 return {1, 2}, {1, 3}, {2, 3}.
19+
The first move is to move the top disk from tower 1 to tower 2.
20+
The second move is to move the top disk from tower 1 to tower 3.
21+
The final move is to move the top disk from 2 to 3.
22+
By the end all disks are moved from tower 1 to tower 3.
1723
*/
1824
func TestTowerOfHanoi(t *testing.T) {
1925
tests := []struct {

0 commit comments

Comments
 (0)