どの番号セットに属していますか?

前書き

数学者は多くの場合、数値セットを扱います。これらは、共通のプロパティを共有する数字のグループです。これらの数字セットは、最も具体的なものから順に、

Natural numbers   (symbol N): These are the counting numbers: 1, 2, 3, ...
Integers          (symbol Z): These are all the numbers without a fractional part: ..., -3, -2, -1, 0, 1, 2, 3, ...
Rational numbers  (symbol Q): These numbers are the quotients of two integers, such as: 1.1 (11/10), 5.67 (567/100), or 0.333333333... (1/3)
-----The following two numbers sets are equally specific in describing numbers-----
Real numbers      (symbol R): These are positive, zero, and negative numbers, such as: π (3.1415926...), e (2.7182818...), or √2 (1.4142135...)
Imaginary numbers (symbol I): Numbers that give negative values when squared; for this reason they aren't very useful in everyday life.
                              These numbers contain the letter i in them, with i representing the √-1.
                              Examples: 2i, -1.5i 78i, 9i, -3.1415i, i
-----------------------------------------------------------------------------------
Complex numbers   (symbol C): The sum of a real and an imaginary number in the form a + bi, examples: 2 + 7i, 3 + 3i, 1 - i, -8 + i

ただ1つの i がある場合、 1i
には書かれていないことに注意してください。

チャレンジ

これらの番号セットのうちの1つの番号を指定すると、それを表す可能性のある最も特定の番号セットのシンボルが印刷されます。

テストケース

Input              Output
73.0            -> N
4/2             -> N
-73             -> Z
-1/3            -> Q
0.56            -> Q
0.555...        -> Q
0.123123123...  -> Q
3.1415926535... -> R
-1.37i          -> I
7892/5 + 23/4i  -> C

他の情報

  • Natural numbers and integers may be given with a decimal point
    followed by a string of zeros
  • To add on to the previous point, any input that can be
    converted into a simpler form shall have its output corresponding
    to the simplified input
  • Rational numbers may be given with a / to
    represent a fraction, or a sequence of numbers occurring 3 or more
    times followed by ...
  • Numbers ending with ... represent repeated
    decimals or un-terminated decimal real numbers
  • Any number sets less specific than Q can also have
    inputs be given as a fraction
  • You do not need to handle repeated decimals or un-terminated
    decimals for number sets other than Q and R
  • Any error caused by lack of precision in floating point numbers
    will not invalidate a submission

これはコードゴルフなので、最短のコードが勝ちます。不明な点がある場合は、コメント欄にご連絡いただくか、質問を編集してください。

ベストアンサー

JavaScript(ES6)、 121 110バイト

s=>[...'QRCI'].find((_,i)=>[/(.+)11../,/../,/.+[+-].*i/,/i/][i].test(s))||'QNZ'[k=eval(s),k%1?0:k>0?1:2]

テストケース

<div class=”snippet” data-lang=”js” data-hide=”true”
data-console=”true” data-babel=”false”> <div
class=”snippet-code snippet-currently-hidden”>

let f =

s=>[...'QRCI'].find((_,i)=>[/(.+)11../,/../,/.+[+-].*i/,/i/][i].test(s))||'QNZ'[k=eval(s),k%1?0:k>0?1:2]

console.log(f('73.0'           ))//-> N
console.log(f('4/2'            ))//-> N
console.log(f('-73'            ))//-> Z
console.log(f('-1/3'           ))//-> Q
console.log(f('0.56'           ))//-> Q
console.log(f('0.555...'       ))//-> Q
console.log(f('0.123123123...' ))//-> Q
console.log(f('3.1415926535...'))//-> R
console.log(f('-1.37i'         ))//-> I
console.log(f('7892/5 + 23/4i' ))//-> C

</div> </div>

書式設定およびコメント付き

s =>                 //given the input string s
  [...'QRCI'].find(  //we first apply some regular expressions:
    (_, i) => [      //  i = index
      /(.+)11../,//    i = 0 (Q): 3x the same pattern, followed by '..' -> rational
      /../,        //    i = 1 (R): '..' without the repeated pattern -> real
      /.+[+-].*i/,   //    i = 2 (C): a real part +/- an imaginary part -> complex
      /i/            //    i = 3 (I): just an imaginary part -> imaginary
    ][i].test(s)     //  this is a find(), so we exit as soon as something matches
  ) || 'QNZ'[        //if all regular expressions failed:
    k = eval(s),     //  assume the expression can now be safely evaluated as JS code
    k % 1 ?          //  if there's a decimal part:
      0              //    -> rational
    :                //  else:
      k > 0 ?        //    if strictly positive:
        1            //      -> natural
      :              //    else:
        2            //      -> integer
  ]                   //

返信を残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です