5318008 - Fun with Calculators

In schools across the world, children type a number into their LCD calculator, turn it upside down and erupt into laughter after creating the word 'Boobies'. Of course, this is the most popular word, but there are many other words which can be produced.

All words must be less than 10 letters long, however (the dictionary does contain words long than this however, so you must perform a filter in your program). In this dictionary, there are some uppercase words, so convert the all words the lowercase.

Using, an English language dictionary, create a list of numbers which can be typed into an LCD calculator and makes a word. As with all code golf questions, the shortest program to complete this task wins.

For my tests, I used the UNIX wordlist, gathered by typing:

ln -s /usr/dict/words w.txt


Or alternatively, get it here.

For example, the image above was created by typing the number 35007 into the calculator and turning it upside down.

The letters and their respective numbers:

• b: 8
• g: 6
• l: 7
• i: 1
• o: 0
• s: 5
• z: 2
• h: 4
• e: 3

Note that if the number starts with a zero, a decimal point is required after that zero. The number must not start with a decimal point.

I think this is MartinBüttner's code, just wanted to credit you for it :)

/* Configuration */

var QUESTION_ID = 51871; // Obtain this from the url
// It will be like http://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page

/* App */

var answers = [], page = 1;

return "http://api.stackexchange.com/2.2/questions/" +  QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}

jQuery.ajax({
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
else process();
}
});
}

8
38
338
5338
638
5638
36138
31738
531738
7738
$ • "Belie" and "Belies" are words? The more you know... – clismique Mar 16 '16 at 6:17 Python 2, 271216211 205 Bytes This is the only idea I've had so far.. I will update this once I think of something else! I assumed we needed to read from a file, but if not let me know so I can update :) Big thanks to Dennis for saving me 55 bytes :) Also thanks to Sp3000 for saving 6 bytes :) d,f,g='oizehsglb',[x.lower()for x in open('w.txt').read().split('\n')if len(x)<10],[] for x in f: c=x[::-1] for b in d:c=c.replace(b,d.find(b)) g=[g,g+[['0.'+c[1:],c][c[0]!='0']]][c.isdigit()] print g  • I don't know a lot of Python, but wouldn't something like "oizehsglb".index(b) be shorter? – Dennis Jun 19 '15 at 18:20 • d[b] == "oizehsglb".index(b). Possibly lacking a cast to string/character. – Dennis Jun 19 '15 at 18:29 • Oh, wow, it never occurred to me that the numbers we were able to replace had numerical values in order.. Yes, that will definitely work! Thanks! – Kade Jun 19 '15 at 18:35 • Haven't tested but: 1) .find is shorter than .index, 2) Depending on which version you have, at least in 2.7.10 open without a mode argument defaults to r, 3) Doesn't just for x in open(...) work? (may need to remove a trailing newline) If it doesn't, then .split('\n') is shorter than .splitlines() – Sp3000 Jun 20 '15 at 1:05 • Also g+=[['0.'+c[1:],c][c[0]!='0']]*c.isdigit(), and you can save a few more by reversing in f then doing for c in f instead of having c=x[::-1]. Also you only use f once, so you don't need to save it as a variable – Sp3000 Jun 20 '15 at 3:33 JavaScript (ES7), 73 bytes This can be done in ES7 a mere 73 bytes: s=>[for(w of s)'oizehsglb'.search(w)].reverse().join.replace(/^0/,'0.')  Ungolfed: var b = function b(s) { return s.length < 10 && /^[bglioszhe]*$/.test(s) ? s.replace(/./g, function (w) {
return 'oizehsglb'.search(w);
}).reverse().join('').replace(/^0/, '0.') : '';
};


Usage:

t('hello'); // 0.7734
t('loose'); // 35007
t('impossible'); //


Function:

t=s=>                       // Create a function 't' with an argument named 's'
[                        // Return this array  comprehension
for(i of s)            // Loops through each letter in the string
'oizehsglb'.search(w)  // Converts it to it's corresponding number
]
.reverse().join         // Reverse the array and make it a string
.replace(/^0/,'0.')       // If the first character is a 0, add a decimal after it


I ran this on the UNIX wordlist and have put the results in a paste bin:

Results

The code used to obtain the results on Firefox:

document.querySelector('pre').innerHTML.split('\n').map(i => t(i.toLowerCase())).join('\n').replace(/^\s*[\r\n]/gm, '');

• What happens with t('Impossible')? – Arturo Torres Sánchez Jun 21 '15 at 0:31
• @ArturoTorresSánchez You're right, I've fixed that – Downgoat Jun 21 '15 at 15:24
• is join ES2015 or is it pre-ES2015? – WallyWest Jul 14 '15 at 1:41
• @WallyWest That's an ES6 feature. It's supported in most major browsers – Downgoat Jul 24 '15 at 22:48
• What is ES7 specific in this? – Arjun May 29 '16 at 0:58

Python 2, 121 bytes

for s in open("w.txt"):
L=map("oizehsglb".find,s[-2::-1].lower())
if-min(L)<1>len(L)-9:printL[0]+"."[L[0]:]+L[4::3]


Assumes that the dictionary file w.txt ends with a trailing newline, and has no empty lines.

GNU sed, 82

(including 1 for -r)

Thanks to @TobySpeight for the golfing help.

s/$/:/ : s/(.)(:.*)/\2\1/ t s/:// y/oizehsglb/012345678/ /.{11}|[^0-9]/d;s/^0/&./  Input wordlist is taken from STDIN: $ sed -rf 5318008.sed /usr/share/dict/words | tail
3705
53705
1705
0.705
50705
5705
505
2
0.02
5002
$ TI-BASIC, 75 88 bytes edit 2: never mind, this is still technically invalid, as it only accepts one word at a time (not a dictionary). I'll try to fix it to allow more than one word as input... edit: oops; I originally made it show a .0 at the end if the last number was 0, not the other way around. Fixed, although this is a kinda bad workaround (displays "0." alongside the number if it starts with 0, otherwise displays two spaces in the same place). On the bright side, it correctly handles words like "Otto" (displays both 0s) since it's not actually displaying a decimal number! I can't think of a better language to do this in. Can definitely be golfed more, but I'm too tired right now. The tilde is the negation symbol [the ( - ) button]. Input is taken from the calculator's answer variable, meaning whatever was last evaluated (like _ in the interactive python shell) so you have to type a string on the homescreen (quote mark is on ALPHA+), press ENTER, then run the program. Alternatively, you can use a colon to separate commands, so if you name the program, say, "CALCTEXT" and you want to run it on the string "HELLO", you can type "HELLO":prgmCALCTEXT instead of doing them separately. seq(inString("OIZEHSGLB",sub(Ans,X,1))-1,X,length(Ans),1,~1 Text(0,0,sub("0. ",1+2(0 or Ans(1)),2),sum(seq(Ans(X)10^(dim(Ans)-X),X,1,dim(Ans  Python 2, 147158 156 bytes I was missing this '0.' requirement. Hope now it works allright. edit: Removed ".readlines()" and it still works ;p edit2: Removed some spaces and move print to the 3rd line edit3: Saved 2 bytes thanks to Sp3000 (removed space after print and changed 'index' to 'find') for x in open("w.txt"): a="oizehsglb";g=[a.find(b)for b in x[::-1].lower()if b in a] if len(g)==len(x)-1<10: if g[0]=="0":g[0]="0." print"".join(g)  Python 2, 184 174 bytes for s in open('w.txt'): try:a=''.join(map(lambda c:dict(zip('bglioszhe','867105243'))[c],s[:-1][::-1]));a=[a,'0.'+a[1:]][a[0]=='0'];print['',''.join(a)][len(s)<11] except:0  Ruby 2, 88 86 bytes x="oizehsglb" puts$_.tr(x,"0-8").reverse.sub /^0/,"0." if$_.size<11&&$_.delete(x)<?A


Byte count includes 2 for the ln options on the command line:

2212
0.2
0.802
0.602
7702
37702
0.02
321607002
515002
0.02002

$./a.out < words.txt | wc -l 550  • Won't *s|32 work as lowercase conversion in this context? – Hagen von Eitzen Jun 21 '15 at 13:50 • Great idea! Thanks! – some user Jun 21 '15 at 23:34 Haskell, 175 bytes without imports (229 bytes with imports) Relevant code (say in File Calc.hs): import Data.Char(toLower) import Data.Maybe(mapMaybe) s="oizehsglb\n" g('0':r)="0."++r g x=x main=mapM_(putStrLn.g.reverse.mapMaybe(lookupzip s['0'..'8'])).filter(\l->length l<10&&all(elems)l).lines.map toLower=<<getContents$ cat /usr/share/dict/words | runghc Calc.hs


Java, 208200 176 bytes

String f(char[] w){String o="",l="oizehsglb";for(int i=w.length;i>0;i--)o+=l.indexOf(w[i-1]|32);if(o.contains("-")||o.length()>8)o="  ";return o.charAt(0)+"."+o.substring(1);}


Expanded

String f(char[] w)
{
String o = "", l = "oizehsglb";
for(int i = w.length; i > 0; i--)
o+=l.indexOf(w[i-1]|32);
if(o.contains("-")||o.length() > 8)
o = "  ";
return o.charAt(0) + "." + o.substring(1);
}


It always adds the decimal, and when invalid returns " . ". But otherwise works like it should. :P

Thanks @LegionMammal978!

• You can save 7 bytes by changing ;String l= to ,l= and =o+ to +=. – LegionMammal978 Jun 20 '15 at 18:30