Skip to content

Instantly share code, notes, and snippets.

@dvidsilva
Last active March 8, 2020 15:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvidsilva/9b28ec94bf76d4e951a7 to your computer and use it in GitHub Desktop.
Save dvidsilva/9b28ec94bf76d4e951a7 to your computer and use it in GitHub Desktop.
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.

http://www.reddit.com/r/dailyprogrammer/comments/34rxkc/20150504_challenge_213_easy_pronouncing_hex/

(function Main() {
'use strict';
var input, tens, ones;
// the input is the puzzle, or things that we want to resolve.
input = ['0xF5', '0xB3', '0xE4', '0xBBBB', '0xA0C9', '0xBEF0FF'];
// we'll say each number comes in pairs.
// x0 is a prefix, base 16 hex.
// http://stackoverflow.com/questions/8186965/what-do-numbers-using-0x-notation-mean
// tens, is the name we're gonna give each value if is on the first position
tens = {
'A': 'atta',
'B': 'bibbity',
'C': 'city',
'D': 'dickety',
'E': 'ebbity',
'F': 'fleventy',
'0': ''
};
// this is the names of the values that we're gonna give them if they're
// in the second position
ones = {
'0': '',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
'7': 'seven',
'8': 'eight',
'9': 'nine',
'A': 'ehh',
'B': 'bee',
'C': 'cee',
'D': 'dee',
'E': 'eee',
'F': 'eff'
};
// first we iterate in the array of inputs, because we need to solve each one.
for (var k in input) {
// creating local scope variables, one for pairs, because we're reading hex in pairs
// output is the string where we're gonna store the value for this input, it resets each time
var pairs, output;
// when you iterate in an object, you should check whether the property is original or was inherited
// from the prototype.
// Which now that I think about it wasn't needed here because we're iterating an array. must've been that I'm used to it.
if (input.hasOwnProperty(k)) {
// we're using a regular expresion literal, it says any letter from A to F and any number
// from 0 to 9, which is repeated twice, the g modifier makes it so it returns all ocurrences and
// not just the first one.
// pairs will be equal to an array of pairs that we can try and read.
pairs = input[k].match(/[A-F0-9]{2}/g);
// if pairs is null, it means the regex didn't match and the input is invalid.
if (pairs === null) {
continue;
}
//first part of output is going to be the original value.
output = input[k] + " ";
// now we iterate in the array of pairs we just created.
for (var i = 0; i < pairs.length; i++) {
// using ternary operators because they look nice.
// this say, if i (the index of current pair) is less than 1, then output is empty
// if not, it is bitey, because in the rules say "Above a full byte you add "bitey" to the name"
output += i < 1 ? '' : 'bitey ';
// the first element of the pair is named in the array tens, so we look for tens[current pair][first character]
// you can access the characters on a string using the [] notation
output += tens[pairs[i][0]] + "-";
// the second element of the pair is named in the array ones, so we look for ones[current pair][second character]
output += ones[pairs[i][1]];
// if the first char of the pair is a zero we add a space, I don't remember why.
output += pairs[i][0] == '0' ? " " : "";
}
// show the output to the user either in browser or shell.
console.log(output);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment