From a97cf9582bb9905f7b25fcc6bf2b76a5d40f9553 Mon Sep 17 00:00:00 2001 From: "lynn [they]" Date: Sun, 23 Feb 2020 05:32:07 -0800 Subject: [PATCH] Js insertion sort (#61) * enable targetting test data * js insertion sort --- README.md | 9 +++-- scripts/tests.py | 25 +++++++++++- src/js/sort_insertion_sort.js | 75 +++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 src/js/sort_insertion_sort.js 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) 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);