From a15cc503f808db288255e72c9a48954db1cca727 Mon Sep 17 00:00:00 2001 From: Ryan Maleki Date: Mon, 7 Apr 2025 18:36:38 -0700 Subject: [PATCH] Add details to DNC problems --- dnc/binary_search_test.go | 6 ++++-- dnc/merge_sort_test.go | 12 +++++++++++- dnc/quick_sort_test.go | 12 +++++++++++- dnc/rate_limit_test.go | 3 +++ dnc/square_root_test.go | 2 ++ dnc/towers_of_hanoi_test.go | 10 ++++++++-- 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/dnc/binary_search_test.go b/dnc/binary_search_test.go index d78660f..454cdd0 100644 --- a/dnc/binary_search_test.go +++ b/dnc/binary_search_test.go @@ -7,13 +7,15 @@ TestBinarySearch tests solution(s) with the following signature and problem desc func BinarySearch(list []int, search int) int -Given a sorted set of integers like {1,2,3,4,6}, and a target int like 4 find its -position in the set like 3 using Binary Search. +Given a sorted slice of integers like and a target int find the position of the target in the +slice. In Binary Search we start with the middle element of the set and compare it with the target. If the target is greater than the middle element we search the right half of the set, otherwise we search the left half of the set. We repeat this process until we find the target or we exhaust the set. + +For example given {1,2,3,4,6} and 4 as a target, return 3 because 4 is at index 3. */ func TestBinarySearch(t *testing.T) { tests := []struct { diff --git a/dnc/merge_sort_test.go b/dnc/merge_sort_test.go index 61e56df..629e262 100644 --- a/dnc/merge_sort_test.go +++ b/dnc/merge_sort_test.go @@ -10,7 +10,17 @@ TestMergeSort tests solution(s) with the following signature and problem descrip func MergeSort(list []int) []int -Given a list of integers like {3,1,2}, return a sorted set like {1,2,3} using Merge Sort. +Given a slice of integers, return a sorted slice using Merge Sort. + +Merge sort is a divide and conquer algorithm that works by dividing the +input array into two halves, recursively sorting each half, and then merging +the two sorted halves back together. The merge step is where the actual +sorting takes place. + +For example given {3,1,2}, return {1,2,3}. The merge sort algorithm would first +divide the array into two halves: {3} and {1,2}. It would then it would divide {1,2} +into {1} and {2}. Finally, it would merge the two halves back together +into a single sorted array: {1,2,3}. */ func TestMergeSort(t *testing.T) { tests := []struct { diff --git a/dnc/quick_sort_test.go b/dnc/quick_sort_test.go index 1a33c43..ff377af 100644 --- a/dnc/quick_sort_test.go +++ b/dnc/quick_sort_test.go @@ -10,7 +10,17 @@ TestQuickSort tests solution(s) with the following signature and problem descrip func QuickSort(list []int) []int -Given a list of integers like {3,1,2}, return a sorted set like {1,2,3} using Quick Sort. +Given a slice of integers like, return a sorted slice of integers using quick sort. + +Quick sort works by selecting a 'pivot' element from the array and partitioning +the other elements into two sub-arrays, according to whether they are less than +or greater than the pivot. The sub-arrays are then sorted recursively. The +base case of the recursion is an array of size 0 or 1, which is always sorted. + +For example given {3,1,2}, return a {1,2,3}. The quick sort algorithm would first +choose a pivot element, say 2. It would then partition the array into two +sub-arrays: {1} and {3}. It would then recursively sort the two sub-arrays +and combine them with the pivot element to produce the final sorted. */ func TestQuickSort(t *testing.T) { tests := []struct { diff --git a/dnc/rate_limit_test.go b/dnc/rate_limit_test.go index 1c90130..d0a1c13 100644 --- a/dnc/rate_limit_test.go +++ b/dnc/rate_limit_test.go @@ -14,6 +14,9 @@ TestRateLimiter tests solution(s) with the following signature and problem descr Given a number of allowed requests calls per second (calls/time) write an IsAllowed function which returns false if the request should be rate limited because it exceeds the limit and true if the request should be allowed. + +For example given limit of 2 requests per second, if 3 requests are made in the first second +two calls to IsAllowed would return true and the third would return false. */ func TestRateLimiter(t *testing.T) { tests := []struct { diff --git a/dnc/square_root_test.go b/dnc/square_root_test.go index 8da47a9..c9132a6 100644 --- a/dnc/square_root_test.go +++ b/dnc/square_root_test.go @@ -12,6 +12,8 @@ TestSquareRoot tests solution(s) with the following signature and problem descri Given a number and precision, return the square root of the number using the binary search algorithm. + +For example given 9 and 3, it should return 3. */ func TestSquareRoot(t *testing.T) { tests := []struct { diff --git a/dnc/towers_of_hanoi_test.go b/dnc/towers_of_hanoi_test.go index a71c30f..c9736a8 100644 --- a/dnc/towers_of_hanoi_test.go +++ b/dnc/towers_of_hanoi_test.go @@ -10,10 +10,16 @@ TestTowerOfHanoi tests solution(s) with the following signature and problem desc func TowerOfHanoi(n, start, end int) [][2]int -Given n, number of disks, and start and end tower, return the moves it takes to move all -disks from start to end tower. The disks are stacked on top of each other with the +Given n, number of disks, and start and end tower, return the number of moves it takes to +move all disks from start to end tower. The disks are stacked on top of each other with the lightest being on top and heaviest being in the bottom. A heavier disk cannot be placed on a lighter disk. You can move one disk at a time. + +For example given 2 disks, start of 1 and end of 3 return {1, 2}, {1, 3}, {2, 3}. +The first move is to move the top disk from tower 1 to tower 2. +The second move is to move the top disk from tower 1 to tower 3. +The final move is to move the top disk from 2 to 3. +By the end all disks are moved from tower 1 to tower 3. */ func TestTowerOfHanoi(t *testing.T) { tests := []struct {