Skip to content

Commit

Permalink
Js insertion sort (#61)
Browse files Browse the repository at this point in the history
* enable targetting test data

* js insertion sort
  • Loading branch information
coilysiren authored Feb 23, 2020
1 parent 50a60d6 commit a97cf95
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
25 changes: 23 additions & 2 deletions scripts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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_*"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
75 changes: 75 additions & 0 deletions src/js/sort_insertion_sort.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit a97cf95

Please sign in to comment.