diff --git a/scripts/tests.py b/scripts/tests.py index 3207fe4..9b1023a 100644 --- a/scripts/tests.py +++ b/scripts/tests.py @@ -178,14 +178,18 @@ def inputs_are_truthy_and_different(first, second): if (not first) or (not second): return False # cleanup - first_cleaned = first.replace("_", "-") - second_cleaned = second.replace("_", "-") + first_cleaned = clean_string(first) + second_cleaned = clean_string(second) # check if inputs are different if first_cleaned != second_cleaned: return True return False +def clean_string(inp): + return inp.replace("-", "_").replace("_test", "") + + if __name__ == "__main__": # run tests runner = TestRunner(*sys.argv) diff --git a/src/go/sort_insertion_sort_test.go b/src/go/sort_insertion_sort_test.go new file mode 100644 index 0000000..cbfc40b --- /dev/null +++ b/src/go/sort_insertion_sort_test.go @@ -0,0 +1,54 @@ +package algozone + +import "testing" + +/////////////////////// +// sort script start // +/////////////////////// + +// insertion sort! +// +// docs: https://en.wikipedia.org/wiki/Insertion_sort +// +// Insertion sort steps through an input list, using it to grow an output list. +// On every step, it sorts the current element into its proper location on the +// output list.It continues until the input list is empty. + +func insertionSort(inputList []string) (outputList []string) { + + for idx, element := range inputList { + outputList = append(outputList, element) + outputList = sortElementAtIndex(outputList, element, idx) + } + + return outputList +} + +func sortElementAtIndex(inputList []string, element string, idx int) (outputList []string) { + outputList = inputList + targetIndex := idx + + for (targetIndex != 0) && (element < outputList[targetIndex-1]) { + outputList = swapWithPrevious(outputList, targetIndex) + targetIndex = targetIndex - 1 + } + + return outputList +} + +func swapWithPrevious(list []string, idx int) []string { + list[idx], list[idx-1] = list[idx-1], list[idx] + return list +} + +///////////////////// +// sort script end // +///////////////////// + +func TestInsertionSort(t *testing.T) { + inputList := getInputList() + outputList := insertionSort(inputList) + writeOutputList(outputList) +} + +// ☝🏽 per-script helpers