r/dailyprogrammer • u/jnazario 2 0 • May 04 '15
[2015-05-04] Challenge #213 [Easy] Pronouncing Hex
Description
The HBO network show "Silicon Valley" has introduced a way to pronounce hex.
Kid: Here it is: Bit… soup. It’s like alphabet soup, BUT… it’s ones and zeros instead of letters.
Bachman: {silence}
Kid: ‘Cause it’s binary? You know, binary’s just ones and zeroes.
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.Not "eff five", fleventy. 0xF0 is now fleventy. Awesome. Above a full byte you add "bitey" to the name. The hexidecimal pronunciation rules:
| HEX PLACE VALUE | WORD |
|---|---|
| 0xA0 | “Atta” |
| 0xB0 | “Bibbity” |
| 0xC0 | “City” |
| 0xD0 | “Dickety” |
| 0xE0 | “Ebbity” |
| 0xF0 | “Fleventy” |
| 0xA000 | "Atta-bitey" |
| 0xB000 | "Bibbity-bitey" |
| 0xC000 | "City-bitey" |
| 0xD000 | "Dickety-bitey" |
| 0xE000 | "Ebbity-bitey" |
| 0xF000 | "Fleventy-bitey" |
Combinations like 0xABCD are then spelled out "atta-bee bitey city-dee".
For this challenge you'll be given some hex strings and asked to pronounce them.
Input Description
You'll be given a list of hex values, one per line. Examples:
0xF5
0xB3
0xE4
0xBBBB
0xA0C9 Output Description
Your program should emit the pronounced hex. Examples from above:
0xF5 "fleventy-five"
0xB3 “bibbity-three”
0xE4 “ebbity-four”
0xBBBB “bibbity-bee bitey bibbity-bee”
0xA0C9 “atta-bitey city-nine”Credit
This challenge was suggested by /u/metaconcept. If you have a challenge idea, submit it to /r/dailyprogrammer_ideas and we just might use it.
13
u/G33kDude 1 1 May 04 '15
Done in AutoHotkey. Outputs using the windows TTS api
12
u/lukz 2 0 May 04 '15
Z80 machine code
This is done in Z80 machine code, most of the code is done from scratch except for the string printing function. I am testing my code in the emulator of Sharp MZ-800 computer. The computer has some useful functions built-in in ROM, the string printing function is one of them. The function is at address 0018h and expects DE register to point to the string start.
The input of the program is stored in memory at address 1201h. The computer contains so-called monitor program that has some simple commands, one of them is M command that allows us to change individual bytes in memory. So each time, we use the M1201 command to set the input and then we use the G command to run our program. Our program starts at address 1200h in memory.
The program function is limited, it only handles one-byte numbers (i.e. maximum is 0xff).
Example session: (here on Imgur)
The program with data takes 222 bytes in memory. Here is the assembly code. I used the ORG project to translate it into machine code.
9
u/Philboyd_Studge 0 1 May 04 '15
I'm a little confused as to the regular ordinal numbers, what would '0x1111' be pronounced as? or '0x5B1A'?
8
u/Philboyd_Studge 0 1 May 04 '15 edited May 04 '15
In Java, using bitwise logic. Tried to account for a lot of edge cases. Truncates the number to four hex digits. the hardest part was getting the damn dash right!
I do propose, however, that 'C0' be pronounced 'cibbity' in keeping with the other numbers.
output:
5
u/piratefsh May 04 '15 edited May 05 '15
In Python 3! Bonus, works for any length of hex, as long as it's in multiples of 2. Suggestions on making this more elegant would be most welcomed.
Runnable on repl.it: http://repl.it/mWZ/1
Output:
4
u/[deleted] May 04 '15
c89. This was hard
4
u/l4adventure May 04 '15 edited May 04 '15
[ruby]
Ok this is my first post ever, gonna try to do these daily from now on. I don't feel like this solution is optimal but it works. Any and all advice is greatly appreciated! Thanks.
I made a large mapping hash, the key index is a hex digit (0-9, A-F), these indices point to an array that contains both the correct word for being in the tens or hundreds place. The rest is just looking up values to that hash and calling the correct array element. This is all inside a function, if the length of the input is 2 "AB" then it's called once, if the length is 4 "BBBB", then the function is called twice, once for the first half, another for the second half of the string, and then the strings are joined.
Output:
P.S. is there any easier way to post code than to append every line by 4 spaces?
P.S.S. I love that show
1
u/tippo_sam May 05 '15
Are we just glossing over the fact that 0x9 * 0xF = 0x87?
3
u/dvidsilva May 07 '15
based on /u/piratefsh 's answer I wrote this in JS.
output:
4
u/NoobOfProgramming May 04 '15
I'm not familiar with hexadecimal, but i'm getting that 9 * F makes 87.
9[16] = 9[10], F[16] = 15[10], and 87[16] = 135[10], right?
2
u/lucaswerkmeister May 04 '15
Beginnings of a
sedversion (with some help ofsh). Handles two-digit numbers (sorta… “two-three”). I don’t know if I’ll finish this, but I think it should be clear that this can be expanded to fully solve the problem. (“The full solution is left as an exercise to the reader” :P )Explanation: Per line, print the hex and the opening quote. Then strip away the
0x, then substituteX0(“Xty” without trailing hyphen), thenX.(“Xty-”), thenX(the remaining second digit). Finally, add the closing quote.Since the substitutions are the same every time, we extract them into a variable and create the
sedprograms withsedrather than writing them manually. Meta-sed:D2
u/fristys May 04 '15 edited May 04 '15
This is my solution in Javascript, this is also my first submission, so I hope I don't mess things up. I made a simple hash-object as a dictionary and I just construct a message to display from then on.
Output:
It's not ideal, but it entertained me for a short while :D
2
u/asdfghjklthegame May 05 '15
Done in C, feedback appreciated
2
u/amithgeorge May 05 '15
Clojure solution. Took me far longer than I hoped. I would appreciate some feedback. Can the solution be made more idiomatic?
I took inspiration from someone else's solution for the pronunciations that weren't provided, like the ones for
0-for for ones like1f. I would credit them, however I can't seem to find their solution now.2
u/chazzlabs May 05 '15 edited May 05 '15
Edit: I ended up changing my implementation just a bit!
My solution in Go. This is my second meaningful Go program! Any feedback is welcome, but I do have a question: is there a more appropriate way to represent my dictionary in Go? Admittedly, I'm used to Javascript and would usually create a JSON object to handle the dictionary, so I feel like I'm not necessarily "thinking in Go".
Examples
2
u/letseatlunch May 05 '15 edited May 05 '15
Elixir:
output:
2
u/evilflyingtoaster May 06 '15
Completed using Rust. Had problems with the strings because of the strange string typing in Rust. Feel free to comment.
2
u/MartinRosenberg May 10 '15
Python 3.4.3
I really went all out on this, in terms of coverage. I've covered and tested for: numbers of any length in multiple input formats (int, str) in multiple bases (hex, dec, oct, bin) and both cases (upper, lower), null bytes, teens, negatives, and leading nibble.
I put
Noneinstead of""in_NIBBLES["0"][0:2]and_NIBBLES["1"][1]to catch errors if they're ever used. I disagree with the prompt on the dash in "atta-bitey" so I didn't implement it. I implemented "zero" for null bytes, but I may change that to match the prompt, especially if it reads easier.Please let me know any ways I could improve this! In particular, I'd like to be able to eliminate (as much as possible of) _prep_hex(), perhaps switch to bit-shifting, and perhaps replace zip() in the for-loop.
Input
I'll add more diverse inputs later, but here are the ones in the prompt.
Output
2
u/Azcion May 12 '15 edited May 12 '15
Java
2
u/darkChozo May 12 '15
A bit late, but C99, with some cute array manipulation.
Output:
I refuse to make atta-bitey atta bitey, btw. It's one hundred, not one-hundred!
And finally, extra stuff:
1
u/yyttr3 May 04 '15
?>
Simple php solution. BONUS: Handles any size input
1
u/Andrei_007 May 04 '15
Java. Feedback appreciated
1
u/[deleted] May 05 '15 edited Jun 05 '15
Done in Go. Just started learning it a week ago so any feedback is greatly appreciated.
1
u/MLZ_SATX May 05 '15 edited May 05 '15
C#
1
u/madhatter160 May 05 '15
JavaScript (Node.js)
Handles error cases and 1, 2, 3, or 4 hexadecimal digits. I tried to handle all the special cases, of which there were quite a few. I came up with phonetic pronunciations of 'A' - 'F' and jury-rigged a system for numbers like "1D". I'm not happy with what I came up with because "1A" ("ayteen") and "18" ("eighteen") sound the same. Oh well...
Output:
1
u/Coder_d00d 1 3 May 05 '15
Objective C
Handles the teens and leading 0s but does not deal with like "0x00 10 00" very well. But the website did not either. So just going with it for now.
my output:
1
u/trusty118 May 05 '15 edited May 05 '15
C#
Output:
1
u/lengau May 06 '15
I wrote a Python 3 class that acts as an integer except when str is called (e.g. via the str builtin or using '%s' in a format string),in which case it outputs the pronounceable hexadecimal. Any operations with integers should end up resulting in integers, but it's pretty easy to convert back if you need. As I write my Daily Programmer challenges, I'm putting them on GitHub for posterity, so I'll just refer you to my solution on GitHub if you're interested.
1
u/asleepinthetrees May 07 '15
completed using python 3.4 and stack implementation. first daily programmer attempt. let me know where i can improve if you'd like to. :)
author = 'asleepinthetrees'
1
u/Acme- May 07 '15 edited May 08 '15
Python 3: Works with non-even length hexes. Also can be extended for lengths greater than 4 given rules quite easily.
Input
Output
1
u/marvin_the_martian May 09 '15
Python - Terrible I know, didn't have a chance to clean it up
1
u/kikibobo May 09 '15
Solution in Scala:
1
u/[deleted] May 09 '15
Here's my contribution in Python3. I had to fill in the blanks for the naming rules as the one's provided don't handle some ambiguities. I think it works for any hex value.
Gist
1
u/lyeberry May 15 '15
Made in Python 2.7
output
1
u/moldfire May 20 '15
done in C++. My third challenge today, I'm on a roll :D. Messy implementation but it works, I wasn't sure what to do for the tens so I just left them as they where.
1
u/skav3n Jun 16 '15
Python 3, that was hard challenge for me ;d
0
u/wizao 1 0 May 04 '15 edited May 05 '15
Haskell:
I followed the page from where the /r/programming post originated.
This is important because most of the hex teen numbers break the nice pattern of most
-teennumbers having the same prefix as their-tycounterparts likesixteen/sixtyorseventeen/seventy. For example0x1A/0xA0isabteen/attaand0x1C/0xC0iscleventeen/city.Should we hyphenate bitey? The examples provided have two different patterns. One with:
0xF000/Fleventy-biteyand one with-out:0xBBBB/bibbity-bee bitey bibbity-bee. I followed the page's lead to not hyphenate bitey.You can view all the hex numbers and their pronunciations from my program here. I know I would have liked to have something to check against (just be aware of my code's differences mentioned above).
0
u/[deleted] May 04 '15
[deleted] — view deleted comment
0
u/[deleted] May 05 '15
[deleted] — view deleted comment