26
\$\begingroup\$

Pronouncing Hex

For those of you uninitiated with the show Silicon Valley, this challenge is inspired by an exchange that goes like this (YouTube):

Kid -
  Here it is: Bit… soup. It’s like alphabet soup, BUT… it’s ones and zeros 
  instead of letters.
Erlich Bachman -
  {silence}
Kid -
  ‘Cause it’s binary? You know, binary’s just ones and zeroes.
Erlich Bachman -
  Yeah, I know what binary is. Jesus Christ, I memorized the hexadecimal 
  times tables when I was fourteen writing machine code. Okay? Ask me 
  what nine times F is. It’s fleventy-five. I don’t need you to tell me what binary is.

It should be noted that technically, 0x9 * 0xF = 0x87, not 'fleventy-five', but this brings up an important question - how would you actually pronounce hex in conversation? It's not like oh ex eff eff flows off the tongue easily, so what should we do? Here's a handy pronunciation chart we will follow.

A = ay       A0 = atta-       
B = bee      B0 = bibbity-    
C = cee      C0 = city-       
D = dee      D0 = dickety-    
E = ee       E0 = ebbity-     
F = eff      F0 = fleventy-   

We can split a 4-length hex number into two groups of two, and determine the pronunciation from the table above, as well as common English pronunciation for numbers. So, for the example 0xFFAB, we would get Fleventy-eff bitey atta-bee.

If a number is included, such as 0xF5AB, you would print Fleventy-five bitey atta-bee. Also, if a number starts one of the groups, you should use it's "tens" pronunciation. For example, 0x5FAA would become Fifty-eff bitey atta-ay. In the case where you have something like 0x1FAC, this would be Effteen bitey atta-cee. But, if this rule were to be used for 0x1AF4, a-teen could be confused for eighteen, so you must prepend a Y. So, the correct output would be Yayteen bitey fleventy-four

In the case of 0xD0F4, instead of doing Dickety-zero bitey fleventy-four, we would ignore the zero and print Dickety-bitey fleventy-four.

Hyphens should only appear within the groups of two, i.e. bitey should not be connected to either group with a hyphen unless the first group is only one word! So 0x04F4 would be four-bitey fleventy-four, but 0x44F4 would be forty-four bitey fleventy-four. As trichoplax said, bitey should only be hyphened when following a round number.

For a comprehensive look at how this will work, check out the example I/O below.

Objective

Create a program or function that will take a hexadecimal string as input or a function argument and produce it's pronunciation. The output must have proper capitalization. You may assume that the length of this number will always be 4.

Example I/O

"0xFFFF" -> "Fleventy-eff bitey fleventy-eff"
"0x0000" -> "Zero"
"0x0010" -> "Ten"
"0x0100" -> "One-bitey zero"
"0x1110" -> "Eleven-bitey ten"
"0xBEEF" -> "Bibbity-ee bitey ebbity-eff"
"0x9999" -> "Ninety-nine bitey ninety-nine"
"0xA1B2" -> "Atta-one bitey bibbity-two"
"0x3C4F" -> "Thirty-cee bitey forty-eff"
"0x17AB" -> "Seventeen-bitey atta-bee"
"0x1AFB" -> "Yayteen-bitey fleventy-bee"
"0xAAAA" -> "Atta-ay bitey atta-ay"

This is , so least number of bytes wins.

\$\endgroup\$
13
  • 6
    \$\begingroup\$ Here's a handy trick for anyone learning the hex times tables: The F times table can be calculated on your 16 fingers and toes (excluding thumbs and big toes). Simply line them up in a row and fold the nth one down to calculate F x n. The number of digits to the left of the folded down digit is the first digit, and the number of digits to the right of the folded down digit is the second digit, allowing you to calculate any 2 digit multiple. For example, fold the Cth digit down to give F x C = Bibbity four. \$\endgroup\$ Commented Jul 17, 2015 at 13:51
  • 2
    \$\begingroup\$ @trichoplax My kid was amazed when I first showed him how to do that for nines on his fingers :D \$\endgroup\$
    – Geobits
    Commented Jul 17, 2015 at 13:58
  • \$\begingroup\$ @Geobits that's what I extrapolated it from - I loved that trick for 9s as a kid. \$\endgroup\$ Commented Jul 17, 2015 at 13:58
  • 1
    \$\begingroup\$ This pronunciation method takes twice as many syllables than the lerfu (borrowed lojban word for letter pronunciations) themselves, but it is more fun to use. \$\endgroup\$
    – lirtosiast
    Commented Jul 19, 2015 at 14:29
  • 5
    \$\begingroup\$ you don't mention "bitey" anywhere other than examples \$\endgroup\$
    – Sparr
    Commented Jul 19, 2015 at 22:24

3 Answers 3

9
\$\begingroup\$

Pyth - 312 293 286 251 bytes

Time to start reducing the data, more coming.

Holy biggest Pyth program I've ever written! There is still huge compression possible with the data, both algortihimically and base conversion, but just wanted to put this up.

Kcs@LGjC"qNjÆÉý(7läQ«I?sfçÂØ^Ðûü»   u$ÐÃoó}QÛã´Hf®?Nâ²-YÜZÔ7ÂþæX#\"åØ"26\q=+K>K11J.e?+?bnb\|@K+16k"ty"-kTbc"  twen | for | | | | | atta bibbi ci dicke ebbi fleven"d.srj"bitey "m?++@Kd?k<d18"teen"\-<d32+j\-@V,JKfT.Dd16\ ?Gh=G.DQ256tG4\-

I interpret input as hex literal through Q which it automatically detect with the 0x. Then I divmod it 256 into the bytes, and remove the first byte if its zero, then map it through two arrays, one for 0-1F and then 20-F0. The second one gets a divmod through the first array also. The first option gets a hypen at the end, and the second get a hyphen at the middle and a space at the end. Join by "bitey ", capitalize with range, and strip hyphens with .s and we're good.

The base compression part is also interesting because I first started out with basic 128 -> 256 conversion. But now, what I am doing is using "q" as the separator instead of space. Thus I can now consider the string as a base 26 string with xLG, improving compression tremendously.

Try it here online.

Test suite.

\$\endgroup\$
6
  • \$\begingroup\$ I know it's not in the question, but I think you should try extrapolating to more numbers, like 0x112233 and 0x93FBAC09 \$\endgroup\$
    – vrwim
    Commented Jul 17, 2015 at 17:48
  • 2
    \$\begingroup\$ @I think this pronunciation model loses it's, uh, viability at that point, haha. Could you imagine actually saying Ninety-three bitey fleventy-bee halfy atta-cee bitey nine? \$\endgroup\$
    – Kade
    Commented Jul 17, 2015 at 19:41
  • \$\begingroup\$ I think something has broken, right now your code only produce a massive amount of Truebiteys and Falsebiteys. \$\endgroup\$
    – Kade
    Commented Jul 21, 2015 at 17:45
  • \$\begingroup\$ @Vioz- Ternaries are broken. Isaacg reversed their order a couple days ago. I could go and change them and in fact save a couple bytes, but their are like 14. \$\endgroup\$
    – Maltysen
    Commented Jul 21, 2015 at 17:47
  • \$\begingroup\$ Ah, I see. So what version of Pyth preserves the functionality? \$\endgroup\$
    – Kade
    Commented Jul 21, 2015 at 17:48
5
\$\begingroup\$

Java - 856 bytes

Not short but at least the second answer ;)

It's a method called String p(String n) which will do the job:

String p(String n){String[]b={"","ten","twen","thir","for","fif","six","seven","eigh","nine",
"atta","bibbi","ci","dicke","ebbi","fleven"};String[]s={"zero","one","two","three","four",
"five","six","seven","eight","nine","ay","bee","cee","dee","ee","eff"};String[]t={"ten",
"eleven","twelve","thir","four","fif","six","seven","eigh","nine","yay","bee","cee","dee",
"ee","eff"};int w=Byte.valueOf(n.substring(2,3),16);int x=Byte.valueOf(n.substring(3,4),16);
int y=Byte.valueOf(n.substring(4,5),16);int z=Byte.valueOf(n.substring(5,6),16);String r=(w==
1?t[x]+(x>2?"teen":""):((w==0?"":b[w]+(w>1&&w!=10?"ty":"")+"-")+(w==0&&x==0?"":s[x])))+((w==0
&&x>0||w==1&&x<3||w>0&&x==0)?"-":w==0&&x==0?"":" ")+(w>0||x>0?"bitey ":"")+(y==1?t[z]+(z>2?
"teen":""):((y==0?"":b[y]+(y>1&&y!=10?"ty":"")+"-")+(y>1&&z!=0||y==0?s[z]:"")));return (char)
(r.charAt(0)-32)+r.substring(1);}
\$\endgroup\$
4
\$\begingroup\$

Javascript - 577 719 bytes

function h(e){for(e=e.match(/.{1,2}/g),r="",i=1;i<e.length;i++)j=parseInt("0x"+e[i]),0!=j?r+=c(parseInt("0x"+e[i]))+"bitey ":i>1?r+="zero-bitey":0;return r=""==r?"Zero":r[0].toUpperCase()+r.substr(1,r.length-7),null!==r[r.length-1].match(/[-\s]$/g)?r=r.substr(0,r.length-1):0,r}function c(e){return d=["zero","one","two","three","four","five","six","seven","eight","nine","ay","bee","cee","dee","ee","eff","ten","eleven","twelve","thir","four","fif","six","seven","eigh","nine","yay","bee","cee","dee","ee","eff"],p=["twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety","atta","bibbity","city","dickety","ebbity","fleventy"],1>e?"":e>31?p[Math.floor(e/16)-2]+"-"+d[e%16]+" ":17>e?d[e]+"-":d[e]+"teen-"}

I'm sure there are improvements to be made. Added bonus, it parses arbitrary length hex strings.

EDIT: Whoops, it doesn't work right when there are leading zeros. Hm.

EDIT 2: Fixed, I think. JSFiddle

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.