# Concept

Remembering numbers can be difficult. Remembering a word may be easier. In order to memorize big numbers, I created a way to pronounce them in a leetspeak-like way.

# Rules

Each digit is first replaced by its corresponding letter:

0 => O
1 => I
2 => R
3 => E
4 => A
5 => S
6 => G
7 => T
8 => B
9 => P


After the replacement, two additional things are done to improved pronunciation:

• Between two consonants, a U is added.

• Between two vowels, a N is added.

# Examples/test cases

512431 => SIRANENI
834677081 => BENAGUTUTOBI
3141592 => ENINANISUPUR
1234567890 => IRENASUGUTUBUPO
6164817 => GIGABIT


# What's impossible

• Letters and numbers mixed in the same word
• Two successive consonants or two successive vowels
• Letters that are not in the list above
• Other characters

# Rules

The goal of this is to create a 2-way translator for this concept.

• Your program must first understand by itself if it's letter-to-number or number-to-letter translation.
• It must check for the entry to be properly formed.
• If everything is correct, display the translation.
• Else, display an error message, nothing, return a falsey value or crash the program.

# Details

• The input number/string can be entered in whatever format you want (stdin, argument, ...)
• This is , so shortest answer wins.
• Standard loopholes are forbidden.
• Shouldn't GIGATESTER be GIGATESUTER? – kamoroso94 Jun 2 '17 at 10:35
• Usually, we don't ask for the entry to be 'properly formed', as it's just extra code for no good reason (see the meta question for good challenge recommendations). Additionally, what does 'properly formed' mean? – Okx Jun 2 '17 at 10:39
• If input validation is required, you should include a decent batch of test cases of invalid inputs (especially letter-only inputs that are almost valid). I expect that this will actually be the main part of the challenge in many languages. – Martin Ender Jun 2 '17 at 10:44
|v$or a vowel at the end of the word |(c|vN)(?=v) or a consonant or a vowel + N followed by a vowel |(cU|v)(?=c) or a consonant + U or a vowel followed by a consonant with c = [RSGTBP] and v = [OIEA]  • Not that it significantly improves your massive byte count, but you can remove the parentheses around (s)-> – Cyoce Jun 2 '17 at 17:29 • @Cyoce every byte helps – PunPun1000 Jun 2 '17 at 17:38 • since all your branches of the if statement are assignments (which return a value), try replacing the if...else if...else with the conditional operator ?:, prefacing it with Object _= to make it a valid statement. Not sure if this would actually help the byte count, but I think it will. – Cyoce Jun 2 '17 at 17:57 • Two small things you can golf. You can remove the String t, because you only use it once. So t.charAt(i)+"" becomes "OIREASGTBP".charAt(i)+"" (-4 bytes). And you can place the last line inside the for-loop after the i<9; inside the for-loop declaration. So it becomes for(;i<9;s=b?s.replace(v,c):s.replace(c,v)){ (-1 byte). Oh, and you can put the s=b?... that comes right after the int i=-1; inside the for-loop as well: for(s=b?...;i<9;... (-1 byte). – Kevin Cruijssen Jun 6 '17 at 6:51 # PHP; 129 127 267 259 228 bytes $l=IOREASGTBP;$n=1023456789;ctype_digit($s=$argn)?:$s=preg_replace("#U|N#","",strtr($o=$s,$l,$n));for($r=$c=($t=strtr($s,$n,$l))[$i++];$d=$t[$i++];)$r.=((trim($c,AEIO)xor$x=trim($d,AEIO))?X:UN[!$x]).$c=$d;echo$o?$o==$r?$s:"":$r;


Run as pipe with -nR or try it online.

breakdown

$l=IOREASGTBP;$n=1023456789;
# if not digits, translate letters to digits and remember original
ctype_digit($s=$argn)?:$s=preg_replace("#U|N#","",strtr($o=$s,$l,$n)); # translate digits to letters: for($r=$c=($t=strtr($s,$n,$l)) # result = first letter [$i++];$d=$t[$i++];) # loop through letters$r.=((trim($c,AEIO)xor$x=trim($d,AEIO))?"":UN[!$x]) # append delimiter if needed
.$c=$d;                                         # append next letter
#
echo
$o # if original was remembered, ?$o==$r # compare original to final result ?$s         # if equal, print digits
:X          # else print X (as error message)
.replaceAll(y+"(?="+y+")","$1U") // and 'U's if necessary :""; // Or return an Empty String if invalid } // End of method  The validation regex: (([AEIOU][BGNPRST])*[AEIOU]?)|(([BGNPRST][AEIOU])*[BGNPRST]?)|\\d*  # 05AB1E, 76 bytes .•.Ÿ^91Ý•Ul„nuSKDXSKg0QVvXyk}JYP©0Êi®}¤«SXuèŒ2ùv…U NSy0èìžMuyåOè})Já¨¹d_iQ®P  Try it online! Returns the falsy value 0 for invalid input. # Bash, 241 238 235 bytes q=OIREASGTBP;[[$1 == +([0-9]) ]]&&(x=tr 0-9 $q<<<$1;m={B,G,P,R,S,T};n={A,E,I,O};for i in eval echo $m$m$n$n;{ a=${i::1};b=${i:1:1};x=${x//$a$b/$a'U'$b};a=${i:2:1};b=${i:3:1};x=${x//$a$b/$a'N'$b};};echo $x)||tr$q 0-9<<<$1|tr -d UN  Try it online! Less golfed: q=OIREASGTBP; save string in q [[$1 == +([0-9]) ]]&&(                if argument 1 is only digits
x=tr 0-9 $q<<<$1;                    save in x each digit translated to corresponding letter
m={B,G,P,R,S,T};
n={A,E,I,O};
for i in eval echo $m$m$n$n;{        generates all combinations of vowels and consonants
BBAA BBAE ... TTOI TTOO
a=${i::1}; saves first consonant in a b=${i:1:1};                         saves second consonant in b
x=${x//$a$b/$a'U'$b}; insets U between consonants a=${i:2:1};                         saves first vowel in a
b=${i:3:1}; saves second vowel in b x=${x//$a$b/$a'N'$b};               inserts N between vowels
};
echo $x echoes result )|| if argument contains letters tr$q 0-9<<<$1|tr -d UN translates letter to corresponding number and deletes U and N  # PHP, 268 Bytes $v=OIEA;$l=RSGTBP;$d="0134256789";$c=ctype_digit;$p=preg_replace;echo!$c($a=$argn)?$c($e=strtr($p(["#\d|[$v]{2,}|[$l]{2,}#","#[$v]\KN(?=[$v])#","#[$l]\KU(?=[$l])#"],[_,"",""],$a),$v.$l,$d))?$e:_:$p(["#[$v](?=[$v])#","#[$l](?=[$l])#"],["$0N","$0U"],strtr($a,$d,$v.$l));


Try it online!

# Perl, 127 bytes

126 bytes + 1 byte command line

$i="AEIOU]";$;=OIREASGTBP;1/!/[^$;NU\d]|[$i{2}|[^\d$i{2}/;eval"y/0-9$;NU/$;0-9/d";s/[$i\K(?=[$i)/N/g;s/[^N\d$i\K(?=[^\d$i)/U/g  Usage:  echo -n "512431" | perl -p entry.pl  Should follow all the challenge rules - can accept letters or numbers and will error (division by zero) if validation fails • Validation has false positives on input NO and US. – Value Ink Jun 3 '17 at 1:05 # Ruby, 205+1 = 206 bytes Uses the -p flag for +1 byte. Now with an exhaustive input validation system. d,w=%w"0-9 OIREASGTBP" ~/^\d+$/?($_.tr!d,w gsub /([#{a='AEIO])(?=\g<1>)'}/,'\0N' gsub /([^#{a}/,'\0U'):(+~/^(([AEIO])(N(?=[AEIO])|(?=\g<4>)|$)|([RSGTBP])(U(?=\g<4>)|(?=\g<2>|$)))+$/;\$_.tr!("NU","").tr!w,d)


Try it online!

• This doesn't seem to convert letters to numbers, nor does it do any validation! – Jarmex Jun 3 '17 at 0:03
• @Jarmex oops, added validation! It's a huge validation check but I don't have much choice in the matter – Value Ink Jun 3 '17 at 0:58

# Python 3, 741 bytes

from functools import reduce;c=0;e="";n="NU";v="AEIOU";l="OIREASGTBPNU";x=('0','O'),('1','I'),('2','R'),('3','E'),('4','A'),('5','S'),('6','G'),('7','T'),('8','B'),('9','P');s=input()
try:
int(s);y=reduce(lambda a,kv:a.replace(*kv),x,s)
for i in y:
e+=i
if i in v:
z=True
try:
if y[c+1] in v:e+="N"
except:
pass
else:
z=False
try:
if not y[c+1] in v:e+="U"
except:
pass
c+=1
except:
for i in s:
if not i in l:
p
y=reduce(lambda a,kv:a.replace(*kv[::-1]),x,s)
for i in y:
if not i in n:e+=i
print(e)


Try it online!

There's a lot of room for improvement, I know.

# sed, 123 bytes

s/[0134]/_&_/g
s/[25-9]/=&=/g
ta
y/OIREASGTBPU/0123456789N/
s/N//g
q
:a
s/__/N/g
s/==/U/g
y/0123456789_/OIREASGTBP=/
s/=//g


## Explanation

First, we surround digits with _ (for vowels) or = (for consonants).

If we didn't make any substitutions, we are converting letters to digits, so it's a simple substitution, and delete U and N. Then quit.

Otherwise, we branch to label a, where we deal with consecutive vowels and then consecutive consonants. Then transform digits to letters, and delete the marker characters we introduced in the first step.

## Python3, 246 bytes

v=lambda x:x in"AEIO"
V="OIREASGTBP"
i=input()
r=__import__("functools").reduce
print(r(lambda x,y:x+(("U",""),("","N"))[v(x[-1])][v(y)]+y,map(lambda x:V[x],map(int,i)))if i.isdigit()else r(lambda x,y:x*10+V.index(y),filter(lambda x:x in V,i),0))


explanation:

• map input to a list of int
• map list of int to their position in the alphabet
• reduce list by adding accumulator, plus an element of a dicttuple, plus the current element
• the dicttuple is a truth table based on two elements, being vowel or not

# JavaScript (ES6), 120

A function taking input as a string. It returns the properly translated string if the input is valid, else false or the function crashes.

n=>(t=n=>n.replace(/./g,d=>1/d?(v-(v=d<5&d!=2)?'':'UN'[v])+z[d]:~(a=z.search(d))?a:'',v=2,z='OIREASGTBP'))(q=t(n))==n&&q


Less golfed

n =>
{
var t = n => { // function to translate, no check for invalid input
var v = 2; // 1 = digit map to vowel, 0 = digit map to consonant, start with 2
var z = 'OIREASGTBP'; // digits mapping
return n.replace(/./g,
d => 1/d // digit / alpha check
? ( // if digit
w = v, // save previous value of v
v = d < 5 & d != 2, // check if current digit will map to wovel or consonant
(w != v
? '' // if different - wovel+consonant or consonant+wovel or start of input
: 'UN'[v] // if equal, insert required separator
) + z[d] // add digit translation
)
: ( // if alpha
a = z.search(d), // look for original digit. Could crash if d is a reserved regexp char (not valid input)
a != -1 ? a : '' // if digit found add to output, else do nothing
)
)
}

var q = t(n); // translate input an put in q
if (t(q) == n) // translate again, result must be == to original input
return q; // if ok return result
else
return false; // else return false
}


Test

var F=
n=>(t=n=>n.replace(/./g,d=>1/d?(v-(v=d<5&d!=2)?'':'UN'[v])+z[d]:~(a=z.search(d))?a:'',v=2,z='OIREASGTBP'))(q=t(n))==n&&q

;512431 => SIRANENI
834677081 => BENAGUTUTOBI
3141592 => ENINANISUPUR
1234567890 => IRENASUGUTUBUPO
6164817 => GIGABIT
.split('\n')
.forEach(x => {
var [a,b] = x.match(/\w+/g)
var ta = F(a)
var tb = F(b)
console.log(a==tb ? 'OK':'KO', a + ' => '+ ta)
console.log(b==ta ? 'OK':'KO', b + ' => '+ tb)
})

function go() {
O.textContent = F(I.value)
}

go()
<input id=I value='NUNS' oninput='go()'>
<pre id=O></pre>