Last active
December 11, 2015 18:59
-
-
Save ErikCH/4645750 to your computer and use it in GitHub Desktop.
Facebook Hackers Cup: Beautiful strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Hackers Cup 2013 | |
//Node.js Submission | |
var fs = require('fs') | |
var totalValue | |
var stringInfo | |
var sortedStringInfo | |
fs.readFile("beautiful_stringstxt.txt","UTF-8",function(err,data) { | |
var split = data.split("\n") | |
var numOfLines = split[0] | |
if(numOfLines >= 5 && numOfLines <= 50 ) | |
for(var j=1; j <= split[0]; j++) { | |
line = split[j] | |
line = line.toLowerCase() | |
stringInfo = {} | |
if(line.length >= 2 && line.length <= 500){ | |
for(var i = 0; i < line.length; i++) { | |
if(/[a-z]/.test(line[i])) { | |
if(typeof(stringInfo[line[i]])== 'undefined') | |
stringInfo[line[i]] = 1 | |
else | |
stringInfo[line[i]]++ | |
} | |
} | |
sortedStringInfo = [] | |
for (var key in stringInfo){ | |
sortedStringInfo.push([key, stringInfo[key]]) | |
} | |
totalValue = 0 | |
sortedStringInfo.sort(function(a, b) { return a[1] < b[1] ? 1 : a[1] > b[1] ? -1 : 0 }) | |
for( var i =0; i < sortedStringInfo.length; i++) { | |
totalValue += sortedStringInfo[i][1]* (26 - i) | |
} | |
console.log("Case #" + j + ": " + totalValue) | |
} | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment