Skip to content
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

Go insertion sort #62

Merged
merged 5 commits into from
Feb 23, 2020
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
8 changes: 6 additions & 2 deletions scripts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 54 additions & 0 deletions src/go/sort_insertion_sort_test.go
Original file line number Diff line number Diff line change
@@ -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
}
Comment on lines +39 to +42
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is overkill, but it helps with visually scanning across scripts of different languages


/////////////////////
// sort script end //
/////////////////////

func TestInsertionSort(t *testing.T) {
inputList := getInputList()
outputList := insertionSort(inputList)
writeOutputList(outputList)
}

// ☝🏽 per-script helpers