Typewriter sort returns!

in code


I am revisiting 2014’s Typewriter Permutation Simulator. Now powered by Node.js, because I want to learn more JavaScript. I have a healthy start made for a web-based performant bogosorter.

var args = process.argv.slice(2);

var permute = function() {
    var letters = this.split(''),
        count = letters.length;

    while (--count > 0) {
        var rand = Math.floor(Math.random() * count);
        [letters[count], letters[rand]] = [letters[rand], letters[count]]
    }

    return letters.join('');
}

var word = {
    unsort: args[0],
    lastsort: args[0],
    sort: args[0].split('').sort().join(''),
    permutations: 0
};

while (word.lastsort !== word.sort) {
    word.lastsort = permute.apply(word.unsort);
    word.permutations++;
}

console.log(
    'It took %s permutations to alphabetize %s (%s)!',
    word.permutations,
    word.lastsort,
    word.sort
);
mark@lucy ~ $ ./bogosort.js typewriter
It took 894352 permutations to alphabetize typewriter (eeiprrttwy)!
mark@lucy ~ $ ./bogosort.js typewriter
It took 53756 permutations to alphabetize eeiprrttwy (eeiprrttwy)!
mark@lucy ~ $ ./bogosort.js typewriter
It took 227804 permutations to alphabetize eeiprrttwy (eeiprrttwy)!


A Summer of Running

in running

Achill Half Marathon

in running


Your email address will not be published. Required fields are marked *