10
1
Your challenge today is to implement a t9-like functionality.
You will implement a function that will only have 2 parameters.
You will receive 1 phone number in a string and the content of a text file with a list of words (don't assume a specific newline style).
You can use the link https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt to test the functionality, or use /usr/share/dict/words
(check A text file with a list of words [closed] for more information).
You can assume that you will always receive at least 2 numbers.
Given the number, you will read from a list of words and returns the words starting with the letters mapping to those words. This means that the input should be only numbers from 2 to 9.
You can do whatever you want if you receive invalid input.
If no match is found, you can return an empty list, null
/nil
or 0
.
Remember that the cellphone keys are mapped to their equivalent chars:
- 0 and 1 are invalid
- 2 matches [abc]
- 3 matched [def]
- 4 matches [ghi]
- 5 matches [jkl]
- 6 matches [mno]
- 7 matches [pqrs]
- 8 matches [tuv]
- and 9 matches [wxyz]
Examples:
f('52726')
//returns ["Japan","japan","Japanee","Japanese","Japanesque"...,"larbowlines"]
f('552')
//returns ["Kjeldahl","kjeldahlization","kjeldahlize"...,"Lleu","Llew"]
f('1234')
//makes demons fly out your nose or divide by 0
f('9999')
//returns ["Zyzzogeton"]
f('999999')
//returns [] or null/nil or 0
After you run your function, you can print it in any way you wish.
Rules:
- Standard loopholes are INVALID
- You must return something, even if it is
null
/nil
Javascript will returnundefined
if you don't return something, therefore this rule. - You cannot use or re-implement other's answers or copy my implementation.
- You can assume, for Javascript, that the browser will be already opened and that the
innerText
/textContent
of the automatic element will be passed as the 2nd parameter - For compiled languages, you cannot pass special arguments to the compiler
- You can receive the file name over compiler arguments
- Variables, macros, global variables, constants, non-standard classes and all the sort passing other values inside the function will be considered invalid.
- In Javascript, variables without the keyword
var
render your code invalid - Your function will be named
f
- You can only and only have 2 arguments on your function
- Try to keep your code under 500 seconds to run.
- You don't have to worry about whitespace
- You must use only ASCII printable characters.
Exceptions are languages that only use non-printable characters (APL and whitespace are 2 examples).
Scoring:
- Lowest number of bytes win
- Having invalid ASCII printable characters in your answer, will count as the answer being encoded in UTF-32
The exception to the encoding will make your answer be count by characters. - Only the function body counts, don't count anything else you do outside it
- Bonus of -30% if you make a prediction system based on the neighborhood or most common words
- Bonus of -20% in size if you only return the first 5 matches for each letter corresponding to the first number (e.g.: 245 would returns 5 words starting with 'a', 5 starting with 'b' and 5 starting with 'c').
Here is an example of an implementation, using Javascript:
function f(phone, words)
{
var keypad=['','','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz'];
var regex='';
for(var i=0,l=phone.length;i<l;i++)
{
regex+='['+keypad[phone[i]]+']';
}
var regexp=new RegExp('\\s('+regex+'[a-z]*)\\s','gi');
return words.match(regexp);
}
To run it, open the list link and run, for example:
f('9999',document.getElementsByTagName('pre')[0].innerText);
//returns [" Zyzzogeton "]
This example was tested and works under Opera 12.17 64bits on Windows 7 Home Edition 64bits.
Is the second argument to the program a file name containing the words or the list of words itself ? – Optimizer – 2014-12-06T20:54:53.700
@MartinBüttner UTF-8 isn't unfair (it still counts ASCII chars as being 1 byte), but I changed the rule. – Ismael Miguel – 2014-12-06T20:57:29.257
@Optimizer The 2nd argument is a list of the words. You can pass the filename over a compiler argument and read the file, if you want to. But the only thing that counts is the function body. – Ismael Miguel – 2014-12-06T20:58:24.357
@MartinBüttner By counting as ASCII, it is being counted as bytes. You want me to say that APL code will have 1 byte having the size of 8 bits? – Ismael Miguel – 2014-12-06T21:00:06.017
@MartinBüttner I think it is more fair now. The required encoding is specified (printable ASCII characters) but APL answers will be count by characters. – Ismael Miguel – 2014-12-06T21:06:20.717
@MartinBüttner Now it is more fair: All languages that only use non-printable ascii chars are counted by chars. All the others are counted by bytes. – Ismael Miguel – 2014-12-06T21:09:44.917
Can you explain your first bonus a bit more please ? – Optimizer – 2014-12-06T21:41:05.847
I'm pretty sure space, tab and newline (which are the only characters Whitespace uses) are printable. Also, correct me if I'm wrong, but by "You don't have to worry about whitespace" I assume you mean whitespace doesn't count. In that case, a Whitespace answer would instantly win with a score of 0. – nyuszika7h – 2014-12-06T21:48:05.473
@Optimizer The t9 function tries to predict the most used words. You can use this to, for example, list the most commonly used words in english. An example would be to only return
["Japan", "japan", "Japanese"]
with the example number "52726". – Ismael Miguel – 2014-12-06T21:48:10.990@nyuszika7h The whitespace being returned don't count. you can return
["a"]
or[" a "]
. And you made a nice point about whitespace. In this case, what I am referring to is characters that show when you print. A space won't be printed, unless you put a background from a different color than the paper (some printers optimize its printing based on the paper color), but you don't have to go on this extend. – Ismael Miguel – 2014-12-06T21:51:16.9532-1 for inappropriate restrictions – AJMansfield – 2014-12-08T03:47:31.520
@AJMansfield "-1 for inappropriate restrictions" --> please, develop more about this. This type of comment is as useful as going to a concert with an air guitar. – Ismael Miguel – 2014-12-08T04:20:05.030
@IsmaelMiguel Specifically, rules 2, 3, 5, 6, 7, 8, and 9 are all inappropriate. Additionally, rules 4, 12, S2, and S3 are also poor. – AJMansfield – 2014-12-09T14:10:36.827
@AJMansfield rule
2
: if it doesn't return something, it isn't a function.3
: you want to copy people's answers?5
: the goal is to create a function, not a full-blown program.6
: that is to enforce the scoring, to avoid cheaters.7
: refer to rule 6.8
: this is to avoid multiple function names.9
: check the implementations: you only need 2 arguments. 'poor rules':4
: indeed the wording is poor, but check my javascript implementation for the reason why.12
: indeed it is badly worded.s2
: refer to rule 13.s3
: the goal is to make a function, this is FAIR – Ismael Miguel – 2014-12-09T14:24:51.240@IsmaelMiguel Which leads to the heart of the issue. Limiting allowed programs to just functions is unreasonable, as that makes a large class of possible solutions 'invalid', such as shell scripts or command-line applications. Those rules get in the way of the fun of making super-optimized programs. – AJMansfield – 2014-12-09T14:30:13.217
So 2 is invalid because the goal shouldn't be to make a function. 3 is invalid because it means that I can't post a super-tightly optimized version of another solution. 5 is simply wrong. 6 is already covered by standard loopholes, and your version of it is bad. Same for 7, plus the 'function' thing. 8 is overly restrictive. 9 but that may preclude certain types of optimization, such as multi-argument recursion. 4 is just an excuse to let you shorten your code. You should just use one of the regular scoring rules; S2 is worse than any of them. S3 is bad because of the function thing. – AJMansfield – 2014-12-09T14:41:44.120
@AJMansfield 2 is perfectly fine. Look up at the tags. 3 is perfectly fine too. The goal is exactly that. If you want to answer, make your own answer. 5 is fine: no need for compiler arguments when all you have to do is a function. Without the rule 8, I would have to count the whole code and making a function wouldn't make any sense. 9: if you want recursion, use
eval
or lambda functions. 4 is not an excuse but a facility. If you post an answer in javascript, you can count that the page will already be opened on the file. s3 is perfect. you can make shorter functions with this. – Ismael Miguel – 2014-12-09T20:40:39.170