숫자 발음


14

개념

숫자를 기억하는 것은 어려울 수 있습니다. 단어를 기억하는 것이 더 쉬울 수 있습니다. 큰 숫자를 암기하기 위해 나는 leetspeak와 같은 방식으로 발음하는 방법을 만들었습니다.

규칙

각 숫자는 먼저 해당 문자로 대체됩니다.

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

교체 후 발음 개선을 위해 두 가지 추가 작업이 수행됩니다.

  • 두 자음 사이에 a U가 추가됩니다.

  • 두 모음 사이에 a N가 추가됩니다.

예제 / 테스트 사례

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

불가능한 것

  • 문자와 숫자가 같은 단어로 혼합
  • 두 개의 연속 자음 또는 두 개의 연속 모음
  • 위 목록에없는 편지
  • 다른 캐릭터

규칙

의 목표는 이 개념에 대한 양방향 번역기를 만드는 것입니다.

  • 프로그램은 문자 대 숫자 또는 숫자 대 문자 변환인지 먼저 이해해야합니다.
  • 항목이 올바르게 형성되었는지 확인해야합니다.
  • 모든 것이 정확하면 번역을 표시하십시오.
  • 그렇지 않으면 오류 메시지를 표시하거나 아무것도 표시하지 않거나 잘못된 값을 반환하거나 프로그램을 중단시킵니다.

세부

  • 입력 번호 / 문자열은 원하는 형식 (stdin, argument, ...)으로 입력 할 수 있습니다
  • 이것은 이므로 가장 짧은 답변이 이깁니다.
  • 표준 허점은 금지되어 있습니다.

11
GIGATESTERGIGATESUTER?
kamoroso94 2016 년

5
일반적으로 적절한 이유없이 추가 코드 일 뿐이므로 항목이 '적절하게 구성'되도록 요구하지 않습니다 (좋은 질문에 대한 메타 질문 참조). 또한 '적절하게 형성되었다'는 것은 무엇을 의미합니까?
Okx

9
입력 유효성 검사가 필요한 경우 유효하지 않은 입력 (특히 거의 문자 인 입력)에 대한 적절한 테스트 사례를 포함해야 합니다. 나는 이것이 실제로 많은 언어에서 도전의 주요 부분이 될 것으로 기대합니다.
Martin Ender 2016 년

2
@MartinEnder에 동의하여 유효하지 않은 경우에 대해 몇 가지 테스트 사례를 추가해야합니다 AB23. AEI; BB; Z; ACE; giga; !@#$; -123; 또한 유효성 검사 규칙에 따라를 변환 할 수 6164735732 => GIGATESTER있지만 (두 개의 연속 자음 규칙)으로 GIGATESTER인해 잘못된 값이 발생 ST합니다. 현재 챌린지가 설정된 방식에 따라 챌린지의 주요 부분은 변환이 아닌 유효성 검사입니다. 나는 괜찮지 만, 그 경우 유효성 검사를 조금 더 잘 정의해야합니다.
Kevin Cruijssen

2
프로그램이 문자-숫자 또는 숫자-문자 변환 인 경우에는 프로그램 자체가 먼저 시작되어야합니다. 번역이 양방향이어야합니까? 위의 텍스트와 테스트 사례는 숫자로만 제안합니다
Luis Mendo

답변:


5

자바 스크립트 (ES6), 130 바이트

두 가지 번역 방법 모두에서 입력을 문자열로 사용합니다. 번역을 문자열로 반환하거나 false입력이 유효하지 않은 경우를 반환합니다 .

f=(n,k)=>(t=n.replace(/./g,(c,i)=>1/n?(!i|p^(p=27>>c&1)?'':'UN'[p])+s[c]:~(x=s.search(c))?x:'',p=s='OIREASGTBP'),k)?t==k&&n:f(t,n)

데모


완벽하게 작동하지 않으면 게시하지 마십시오.
Okx

바라건대 지금 예상대로 작동합니다.
Arnauld

... 또는 입력에 정규 표현식 문자가 있으면 충돌합니다. 그래도 유효
edc65

2

Japt , 61 59 92 85 84 바이트

나는 대부분의 (긴) 주말 동안 오프라인 상태입니다.이 문제와 관련하여 더 이상 문제가 발견되지 않으면, 그것을 고칠 수있을 때까지 모드를 삭제 해주세요.

두 작업에 대한 문자열로 입력을 받아 아니라, 또는 두 가지 모두에 대한 문자열을 반환 false교체 1 바이트 추가, 숫자 입력은 항상 여러 개의 숫자를 포함 무효 input.Assumes을 위해 함께 Un<space>그 유효하지 있는지를. false테스트 케이스에 대해 리턴 GIGATESTER하지만 규칙에 따라 유효하지 않은 입력 이어야합니다 .


V="OIREASGTBP"UÉ?¡VgXÃe"%v"²_i1'NÃe"%V"²_i1'UÃ:!Uè"%v%v|%V%V|[^{V}NU]" ©Ur"N|U" £VaX

그것을보십시오 : 숫자-> 문자 또는 문자-> 숫자


  • 2 4 바이트는 obarakon 에게 감사를 표했습니다. obarakon 은 이전에 포기한 후에 다시 가져 가도록 설득했습니다. 나는 그가하지 않았 으면 좋겠다!
  • 33 26 25 (!) 바이트는 입력 유효성을 확인하기 위해 빠른 수정 (즉, 아직 완전 골프화)을 구현하여 희생되었습니다.

설명

(아직 최신 버전으로 업데이트)

                          :Implicit input of string U.
V="..."                   :Assign the string of letters to variable V, in order.
UÉ                        :Subtract 1 from U, which will give a number (truthy) if the input is a number or NaN (falsey) if the input is a string.
?                         :If it's a number then
¡                         :    Map over the input string, replacing each character (digit) with ...
VgX                       :      the character in string V at index X, the current digit.
à                        :    End mapping.
e                         :    Recursively replace ...
"%v"²                     :      every occurrence of 2 vowels (RegEx) ...
_i1'N                     :      with the current match with an "N" inserted at index 1.
à                        :    End replacement.
e                         :    Another recursive replacement of ...
"%V"²                     :      every occurrence of 2 non-vowel characters (i.e., consonants) ...
_i1'U                     :      with the current match with a "U" inserted at index 1.
à                        :    End replacement.
:                         :Else, if it's a string then
Uè"%v%v|%V%V|[^{V}NU]"    :    Count the number of matches of 2 successive vowels OR 2 successive non-vowels OR any character not in contained in string V plus N & U.
                          :    (The longest part of this code is the fecking input validation!)
?                         :    If that count is greater than 0 then
T                         :       Return 0.
:                              Else
Ur"N|U"                   :        Replace every occurrence of "N" OR "U" in string U with nothing.
£                         :        Map over the string, replacing each character (letter) with ...
VaX                       :         the index of the current character X in string V.
                          :Implicit output of resulting string

다음과 같은 잘못된 입력을 처리하지 않는 것 같습니다AEI
Emigna

@Emigna : 아, 젠장! 처음에 "규칙"에 불타고 나면 나머지 규칙을 읽는다고 생각했을 것입니다. : \ 나는 "불가능한 일"섹션을 취하여 우리가 그 요점을 처리 할 필요가 없음을 암시했습니다.
Shaggy


1

자바 (오픈 JDK 8) , 416 (410) 399 382 376 370 바이트

@Cyoce 덕분에 -2 바이트

@Cyoce의 아이디어 덕분에 -17 바이트 더

@KevinCruijssen 덕분에 -6 바이트

s->{String c="[RSGTBP]",v="[OIEA]",o="([256789])",e="([0134])";boolean b=s.matches("(c$|v$|(c|vN)(?=v)|(cU|v)(?=c))+".replace("c",c).replace("v",v));int i=-1;for(s=b?s.replaceAll("[UN]",""):s.matches("[0-9]+")?s.replaceAll(e+"(?="+e+")","$1N").replaceAll(o+"(?="+o+")","$1U"):i/0+"";i<9;s=b?s.replace(v,c):s.replace(c,v)){c=++i+"";v="OIREASGTBP".charAt(i)+"";}return s;}

온라인으로 사용해보십시오!

어쨌든 자바 대체는 너무 장황하다.

문자열을 가져와 숫자-> 문자 또는 그 반대로 변환 된 문자열을 반환하는 함수입니다. 유효하지 않은 입력에서 충돌이 발생 함

ungolfed (가독성을 위해 for 루프의 첫 번째 및 마지막 용어를 빼냄)

s-> {
    String c="[RSGTBP]", v="[OIEA]", o="([256789])", e="([0134])"; 
    boolean b=s.matches("(c$|v$|(c|vN)(?=v)|(cU|v)(?=c))+".replace("c",c).replace("v",v)); // lovely regex, explained below
    int i=-1;
    s= b? 
        s.replaceAll("[UN]",""); // remove N's and U's
        :s.matches("[0-9]+")?
        s.replaceAll(e+"(?="+e+")","$1N").replaceAll(o+"(?="+o+")","$1U"); // add N's and U's for separating vowels and consonants
        :i/0+""; // throw an error, looks like a sting for the ternary
    for(;i<9;) { 
        c=++i+"";
        v="OIREASGTBP".charAt(i)+"";
        s=b?s.replace(v,c):s.replace(c,v); // if it started with numbers, go to letters, or vice versa
    }
    return s;
}

숫자를 일치시키는 정규식은 간단하지만 문자를 숫자로 일치시키는 정규식은 다음과 같습니다.

(c$|v$|(c|vN)(?=v)|(cU|v)(?=c))+
(                             )+   every part of the word is
 c$                                a consonant at the end of the word
   |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]

하지 않는 것이 그것이 상당히 대규모 바이트 수를 향상,하지만 당신은 주위에 괄호를 제거 할 수 있습니다(s)->
Cyoce

@Cyoce 모든 바이트 도움
PunPun1000

의 모든 지점부터 if문 (값을 반환) 할당하고, 일 교체 시도 if... else if... else조건 연산자와 ?:함께 앞에 추가, Object _=그것은 유효한 진술하게 할 수 있습니다. 이것이 실제로 바이트 수에 도움이되는지 확실하지 않지만 그렇게 할 것이라고 생각합니다.
Cyoce

골프 할 수있는 두 가지 작은 것들. t한 번만 사용하므로 String을 제거 할 수 있습니다 . 그래서 t.charAt(i)+""이된다 "OIREASGTBP".charAt(i)+""( -4 바이트 ). 그리고 i<9;for-loop 선언 후 for-loop 안에 마지막 줄을 넣을 수 있습니다 . 따라서 for(;i<9;s=b?s.replace(v,c):s.replace(c,v)){( -1 바이트 )가됩니다. 아, 그리고 for 루프 내부 s=b?...바로 다음에 오는 것을 넣을 수 int i=-1;있습니다 : for(s=b?...;i<9;...( -1 byte ).
Kevin Cruijssen

1

PHP; 129 127 267 259 228 바이트

$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;

파이프로 실행 -nR하거나 온라인으로 사용해보십시오 .

고장

$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)
        :$r;        # else print letters

1

자바 (8) 312 308 304 301 294 290 바이트

s->{String r="",x="([AEIOU])",y="([BGNPRST])",z="0O1I2R3E4A5S6G7T8B9P";for(int c:s.getBytes())r+=c!=78&c!=85?z.charAt((c=z.indexOf(c)+(c<58?1:-1))<0?0:c):"";return s.matches("(("+x+y+")*"+x+"?)|(("+y+x+")*"+y+"?)|\\d*")?r.replaceAll(x+"(?="+x+")","$1N").replaceAll(y+"(?="+y+")","$1U"):"";}

버그 수정을위한 -4 바이트 (308 → 304)

편집 : @ PunPun1000의 Java 답변 과 다른 접근법을 사용합니다. 문자에 대해 for-loop에서 return-String을 먼저 생성하여 을 사용하고 더 추상적 인 정규식을 사용하여 return-ternary에서 유효성을 검사합니다 (입력은 모두 숫자, 또는 주어진 모음과 자음이 번갈아 가며 (따라서 인접한 모음이나 자음없이).

설명:

여기에서 시도하십시오.

s->{                                   // Method with String parameter and String return-type
  String r="",                         //  Result-String
    x="([AEIOU])",y="([BGNPRST])",     //  Two temp Strings for the validation-regex
    z="0O1I2R3E4A5S6G7T8B9P";          //  And a temp-String for the mapping
  for(int c:s.getBytes())              //  Loop over the characters of the input-String
    r+=                                //   Append to the result-String:
       c!=78&c!=85?                    //    If the character is not 'N' nor 'U':
        z.charAt(                      //     Get the character from the temp-String `z`
         (c=z.indexOf(c)+              //      by getting the character before or after the current character
            +(c<58?1:-1))              //      based on whether it's a digit or not
             <0?0:c)                   //      and a 0-check to prevent errors on incorrect input like '!@#'
       :                               //    Else:
        "";                            //     Append nothing
                                       //  End of loop (implicit / single-line body)
  return s.matches("(("+x+y+")*"+x+"?)|(("+y+x+")*"+y+"?)|\\d*")?
                                       //  If the input is valid
                                       //  (Only containing the vowels and consonants of `x` and `y`, without any adjacent ones. Or only containing digits)
    r                                  //   Return the result
     .replaceAll(x+"(?="+x+")","$1N")  //    after we've added 'N's if necessary
     .replaceAll(y+"(?="+y+")","$1U")  //    and 'U's if necessary
   :"";                                //  Or return an Empty String if invalid
}                                      // End of method

검증 정규식 :

(([AEIOU][BGNPRST])*[AEIOU]?)|(([BGNPRST][AEIOU])*[BGNPRST]?)|\\d*


0

세게 때리다 ,241238 235 바이트

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

온라인으로 사용해보십시오!

덜 골프 :

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

0

PHP, 268 바이트

$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));

온라인으로 사용해보십시오!


0

펄, 127 바이트

126 바이트 + 1 바이트 명령 줄

$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

용법:

 echo -n "512431" | perl -p entry.pl

모든 챌린지 규칙을 따라야합니다-문자 나 숫자를 받아 들일 수 있으며 유효성 검사에 실패하면 오류 (0으로 나눔)


입력 NO및 에 대한 유효성 검사에 오 탐지가 US있습니다.
Value Ink

0

루비 , 205 + 1 = 206 바이트

-p+1 바이트에 플래그를 사용합니다 . 이제 철저한 입력 검증 시스템이 있습니다.

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)

온라인으로 사용해보십시오!


이것은 문자를 숫자로 변환하지 않으며 검증도하지 않습니다!
Jarmex

@Jarmex 죄송합니다, 유효성 검사가 추가되었습니다! 그것은 큰 검증 검사이지만 문제에 대한 선택의 여지가 없습니다
Value Ink

0

파이썬 3, 741 바이트

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)

온라인으로 사용해보십시오!

개선의 여지가 많이 있습니다.


0

sed, 123 바이트

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

설명

먼저 숫자를 _(모음) 또는 =(자음)으로 묶습니다.

우리가 어떤 대체를하지 않은 경우, 우리는 그렇게 간단한 대체의, 숫자를 문자로 변환하고, 삭제할 수 있습니다 UN. 그런 다음 종료하십시오.

그렇지 않으면 a연속 모음과 연속 자음을 처리 하는 label로 분기합니다 . 그런 다음 숫자를 문자로 변환하고 첫 번째 단계에서 소개 한 마커 문자를 삭제하십시오.


0

Python3, 246 바이트

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))    

설명:

  • 입력을 int 목록에 매핑
  • 알파벳의 위치에 대한 int의 맵 목록
  • 누산기, dict 튜플 요소 및 현재 요소 를 추가하여 목록을 줄입니다.
    • 딕셔너리의 튜플 두 요소 인 모음의 여부에 기초하여 진리표

0

자바 스크립트 (ES6), 120

입력을 문자열로받는 함수입니다. 입력이 유효하면 올바르게 변환 된 문자열을 반환하고, 그렇지 않으면 false이거나 함수가 충돌합니다.

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

덜 골프

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
}

테스트

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>

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.