From 5537865cd0be7e71f9988ef8f429c86ddd98dfaa Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 23 Feb 2020 05:15:52 -0800 Subject: [PATCH 1/4] enable targetting test data --- README.md | 9 +++++---- scripts/tests.py | 25 +++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2af7c1c..6785ece 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,13 @@ Run this once pipenv sync ``` -Then run any of these as many times as you want +Then run any of these ```bash -# example CLI calls +# example CLI calls, run with as many args so you want pipenv run tests -pipenv run tests $language -pipenv run tests rust +pipenv run tests $language $script_name $data +pipenv run tests python pipenv run tests python sort_insertion_sort +pipenv run tests python sort_insertion_sort first_names_shortened ``` diff --git a/scripts/tests.py b/scripts/tests.py index d205f0d..3207fe4 100644 --- a/scripts/tests.py +++ b/scripts/tests.py @@ -17,15 +17,18 @@ class TestRunner(object): language = "" # input_script is the name of a script you want to run input_script = "" + # input_data is the name of the data you want to run against + input_data = "" # base_directory is prepended to many of our paths base_directory = "" # config contains language specific configuration config = {} # __init__ reads off of sys.argv, and the first input arg is the name of this file - def __init__(self, _, language="python", input_script=""): + def __init__(self, _, language="python", input_script="", input_data=""): self.language = language self.input_script = input_script + self.input_data = input_data self.base_directory = os.getcwd() with open(f"{self.base_directory}/config.yml", "r") as obj: data = obj.read() @@ -43,6 +46,11 @@ def run_tests(self): if not os.path.isdir(data_folder_path): continue + # if input data file name was passed in, and this data is not the input data + # then continue to the next set of data + if inputs_are_truthy_and_different(self.input_data, data_folder_name): + continue + # run every sort script for script_path in glob.glob( f"{self.base_directory}/src/{self.language}/sort_*" @@ -73,7 +81,7 @@ def run_tests(self): # if an input script was passed in, and this script is not that input script # then continue on to the next script - if (self.input_script != "") and (script_name != self.input_script): + if inputs_are_truthy_and_different(self.input_script, script_name): continue # if an old script output file already exists, remove it @@ -165,6 +173,19 @@ def show_results(self): sys.exit(1) +def inputs_are_truthy_and_different(first, second): + # check if inputs are truthy + if (not first) or (not second): + return False + # cleanup + first_cleaned = first.replace("_", "-") + second_cleaned = second.replace("_", "-") + # check if inputs are different + if first_cleaned != second_cleaned: + return True + return False + + if __name__ == "__main__": # run tests runner = TestRunner(*sys.argv) From 413092eb137ff31f4db49ac763992c504b703996 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 23 Feb 2020 05:29:39 -0800 Subject: [PATCH 2/4] js insertion sort --- src/js/sort_insertion_sort.js | 75 +++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/js/sort_insertion_sort.js diff --git a/src/js/sort_insertion_sort.js b/src/js/sort_insertion_sort.js new file mode 100644 index 0000000..784815a --- /dev/null +++ b/src/js/sort_insertion_sort.js @@ -0,0 +1,75 @@ +var fs = require('fs'); + +/////////////////////// +// 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. + +function doSorting(inputList) { + return insertionSort(inputList); +} + +function insertionSort(inputList) { + outputList = []; + + inputList.forEach((element, idx) => { + outputList.push(element) + outputList = sortElementAtIndex(outputList, element, idx) + }); + + return outputList +} + +function sortElementAtIndex(inputList, element, idx) { + target_index = idx; + outputList = inputList; + + while ( + (target_index != 0) && + (element < outputList[target_index - 1]) + ) { + swapWithPrevious(outputList, target_index) + target_index -= 1 + } + + return outputList +} + +function swapWithPrevious(list, idx) { + tmp = list[idx - 1] + list[idx - 1] = list[idx] + list[idx] = tmp + return list +} + +///////////////////// +// sort script end // +///////////////////// + +// 👇🏽 copy pasted helpers + +// read input file +let inputFilePath = process.env.INPUT_PATH; +let inputString = fs.readFileSync(inputFilePath, 'utf8'); +let inputStringSplit = inputString.split(/\n/); + +// clean input data +if (inputStringSplit[inputStringSplit.length - 1] === "") { + inputStringSplit = inputStringSplit.slice(0, inputStringSplit.length - 1); +} + +// do sorting +sortedData = doSorting(inputStringSplit); + +// write output file +let outputFilePath = process.env.OUTPUT_PATH; +let outputString = sortedData.join("\n"); +outputString += "\n"; // trailing newline +fs.writeFileSync(outputFilePath, outputString); From 1edebb365d7b1576affb1c64d3226f7e7dce8ded Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 23 Feb 2020 05:41:36 -0800 Subject: [PATCH 3/4] go insertion sort --- src/go/sort_insertion_sort_test.go | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/go/sort_insertion_sort_test.go 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 From aae2d3d0e654b2c23625e1bb83fdfc961de0fd63 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sun, 23 Feb 2020 05:44:26 -0800 Subject: [PATCH 4/4] clip *test* --- scripts/tests.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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)