fatcat
BAN USERif( process.argv.length<4 ) {
console.log("USAGE: %s words_separated_by_comma orderding_separated_by_comma",process.argv[1]);
}
else {
const ordering = {};
process.argv[3].split(',').forEach(function(letter,ndx) {
ordering[letter[0]] = ndx;
});
//console.log(ordering);
let previous_value = Math.NEGATIVE_INFINITY;
// Among the words, find the first one that is "less" than the previoud
if( process.argv[2].split(',').find(function(word) {
let current_value = 0;
for( let n=0; n<word.length; n++ ) {
if( !ordering.hasOwnProperty(word[n]) ) {
console.log("Found letter '%s' that does not have an order",word[n]);
process.exit(-1);
}
current_value = (current_value*256)+ordering[word[n]];
}
//console.log("Value of %s is %d, previous_value is %d",word,current_value,previous_value);
if( current_value<previous_value )
return true;
previous_value = current_value;
return false;
}) ) {
// This means that we found the element that is "less" then the previous one
// This means that the words are not "ordered"
console.log("False");
}
else {
console.log("True");
}
}
class Space {
constructor( x, y, type ) {
this.x = x;
this.y = y;
this.type = type;
this.left = undefined;
this.right = undefined;
this.up = undefined;
this.down = undefined;
}
visit( visited ) {
let hash_code = this.toString();
if( !visited.hasOwnProperty(hash_code) ) {
['left','right','up','down'].forEach( element => {
if( this[element] ) {
this[element].visit(visited);
}
},this);
visited[hash_code] = this;
}
return visited;
}
toString() {
return "["+this.x+"_"+this.y+"]";
}
}
let start = {}; // Should be assigned elsewhere
start.visit({}).values().filter( element => {
return (element.type=='tree' || element.type=='house');
}).forEach( element => {
// This is going to call .toString() method
console.log(element);
});
- fatcat September 23, 2018