...

View Full Version : Code Challenge!



ca_redwards
11-04-2004, 10:54 PM
In the fewest bytes of JavaScript code, how would you write a function that accepts a phone number and returns an array of phone words?

function phoneWord(pn){/* your code here */}

phoneWord(2255288) should return an array with 2187 (3*3*3*3*3*3*3) different items, one of which should be 'CALLATT'.

Let's call this contest by 12/7/2004.

Good Luck!

liorean
11-05-2004, 01:07 AM
I really think it's better if this is a new thread instead of a years old one. Splitting.

Garadon
11-05-2004, 11:04 AM
whats a phone word?

Basscyst
11-05-2004, 05:57 PM
Took me a minute too, but still not sure. Do you mean the words that could be made, based on the letters assigned to the numbers of the phone number?

Basscyst

fci
11-05-2004, 06:15 PM
I would think a serverside method would be more effective due to the possible combinations.
here is one that runs a serverside although closed source - http://www.phonespell.org/
although it only find mnemonics.

jkd
11-05-2004, 06:32 PM
Array.prototype.map = function(func) {
if (this.length) return [func(this[0])].concat(this.slice(1).map(func));
return [];
}
function phoneWord(pn) {
return (function(func, running, arr) {
if (arr.length)
return arguments.callee(func, func(arr[0], running), arr.slice(1));
return running;
})(function(a1, a2) {
if (a1.length)
return a2.map(function(el) { return el+a1[0] }).concat(arguments.callee(a1.slice(1), a2));
return [];
}, [""], pn.toString().split("").map(parseInt).map(
function(button) {
if (button)
return [3*button+59, 3*button+60, 3*button+61].map(String.fromCharCode);
return ["Y","Z"];
}));
}


The map could be phased out but actually defining it instead of using anonymous lambda functions shortens the code, as the script relies on map in several areas. Also, the code could be compressed a good deal.

ca_redwards
11-08-2004, 07:32 PM
Array.prototype.map = function(func) {
if (this.length) return [func(this[0])].concat(this.slice(1).map(func));
return [];
}
function phoneWord(pn) {
return (function(func, running, arr) {
if (arr.length)
return arguments.callee(func, func(arr[0], running), arr.slice(1));
return running;
})(function(a1, a2) {
if (a1.length)
return a2.map(function(el) { return el+a1[0] }).concat(arguments.callee(a1.slice(1), a2));
return [];
}, [""], pn.toString().split("").map(parseInt).map(
function(button) {
if (button)
return [3*button+59, 3*button+60, 3*button+61].map(String.fromCharCode);
return ["Y","Z"];
}));
}


The map could be phased out but actually defining it instead of using anonymous lambda functions shortens the code, as the script relies on map in several areas. Also, the code could be compressed a good deal.

Excellent! :)

RoyW
11-22-2004, 05:02 PM
The function


<script>
function phoneWord( num, str, result)
{
var s = ["0", "1", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"][num.charAt(0)];
str = str == undefined ? "" : str;
result = result == undefined ? new Array() : result;
for(var i=0 ; i<s.length ; i++)
if(num.length > 1)
phoneWord(num.slice(1) , str + s.charAt(i), result)
else
result[result.length] = str + s.charAt(i);
return result;
}
</script>


The test


<script>
var x=phoneWord("2255288");
var y=phoneWord("769");

document.write("<textarea rows=40 cols=100>" + x + "</textarea>");
document.write("<textarea rows=3 cols=100>" + y + "</textarea>");
document.write("<textarea rows=3 cols=100>" + y[34] + "," + x[1674] + "</textarea>");
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum