多重度テスト


21

プログラミング言語を使用して、1〜99の数字(両方を含む)を次のように表示します。

  • 数字は単一のスペースで区切られています。
  • 数値が3で割り切れる場合は、括弧で囲む必要があります。
  • 数値が4で割り切れる場合は、角括弧で囲む必要があります。
  • 数値が3と4の両方で割り切れる場合は、括弧と角括弧の両方で囲む必要があります(角括弧は数字に近い)。

プログラムは正確に表示する必要があります。

1 2 (3) [4] 5 (6) 7 [8] (9) 10 11 ([12]) 13 14 (15) [16] 17 (18) 19 [20] (21) 22 23 ([24]) 25 26 (27) [28] 29 (30) 31 [32] (33) 34 35 ([36]) 37 38 (39) [40] 41 (42) 43 [44] (45) 46 47 ([48]) 49 50 (51) [52] 53 (54) 55 [56] (57) 58 59 ([60]) 61 62 (63) [64] 65 (66) 67 [68] (69) 70 71 ([72]) 73 74 (75) [76] 77 (78) 79 [80] (81) 82 83 ([84]) 85 86 (87) [88] 89 (90) 91 [92] (93) 94 95 ([96]) 97 98 (99)


3
各エントリを新しい行に出力できますか、それともすべてを1行に出力する必要がありますか?
ETHproductions

4
出力はスペースで終了できますか。いくつかの答えはそう仮定しているようです。
デニス

回答:





4

ゼリー21 20バイト

³Ṗµ3,4ṚƬḍד([“])”j)K

オンラインでお試しください!

使い方

³Ṗµ3,4ṚƬḍד([“])”j)K  Main link. No arguments.

³                     Set the return value to 100.
 Ṗ                    Pop; yield [1, ..., 99].
  µ               )   Map the chain in between over [1, ..., 9]; for each integer k:
   3,4                    Set the return value to [3, 4].
      ṚƬ                  Reverse until a loop is reached. Yields [[3, 4], [4, 3]].
        ḍ                 Test k for divisibility by [[3, 4], [4, 3]], yielding a
                          matrix of Booleans.
         ד([“])”         Repeat the characters of [['(', '['], [']', ')']] as many
                          times as the Booleans indicate.
                 j        Join the resulting pair of strings, separated by k.
                   K  Join the resulting array of strings, separated by spaces.


3

、30バイト

⪫EEE⁹⁹I⊕ι⎇﹪⊕κ⁴ι⪫[]ι⎇﹪⊕κ³ι⪫()ι 

オンラインでお試しください!リンクは、コードの詳細バージョンです。説明:

    ⁹⁹                          Literal 99
   E                            Map over implicit range
        ι                       Current value
       ⊕                        Incrementd
      I                         Cast to string
  E                             Map over list of strings
            κ                   Current index
           ⊕                    Incremented
             ⁴                  Literal 4
          ﹪                     Modulo
              ι                 Current value
                []              Literal string `[]`
                  ι             Current value
               ⪫                Join i.e wrap value in `[]`
         ⎇                      Ternary
 E                              Map over list of strings
                      κ         Current index
                     ⊕          Incremented
                       ³        Literal 3
                    ﹪           Modulo
                        ι       Current value
                          ()    Literal string `()`
                            ι   Current value
                         ⪫      Join i.e wrap value in `()`
                   ⎇            Ternary
                                Literal space
⪫                               Join list
                                Implicitly print

3

J54 53バイト

@Jonahのおかげで1バイト少ない

(*stdout(3|.0=4 3 1 3 4&|,1=":)#3|.']) ([',":)@>i.100

オンラインでお試しください!


これをしてくれてありがとう。また、なぜあなたはstdoutここでやらなければならないのですか...私はそれを見たことがありません。@FrownyFrog
ジョナ

@Jonah完全な文字列として出力することはできません。切り捨てられます(。。。)stdoutはそれを行わず、改行も印刷しないため、各番号を個別に印刷することもできます。何らかの理由で、末尾のスペースが表示されます(4つあり、意図的に1つだけがあります)
FrownyFrog

このアプローチは、回転と使用する選択の両方で、本当に巧妙#です。私はサラウンドに助動詞を導入していた()し、[]g=. {.@[ , ":@] , {:@[。冗長さ
ジョナ

もう一つ質問:あなたがLF代わりに使用した理由_。後者も動作するようです。
ヨナ

3

C、C ++、136 133 131 129 128 124バイト

-5バイト、ザカリーのおかげで、D言語のwrite()関数に触発されました(ザカリーの回答を参照)

-mriklojnのおかげで2バイト

mriklojnによるCバージョンの場合は-12バイト

ceilingcatのおかげで-4バイト

#include<cstdio>
void f(){for(int i=0;i++<99;)printf("(%s%d%s%s%s"+!!(i%3),i%4?"":"[",i,i%4?"":"]",i%3?"":")",i%99?" ":"");}

C固有の最適化:115バイト

#include<stdio.h>
i;f(){for(;i++<99;)printf("(%s%d%s%s%s"+!!(i%3),i%4?"":"[",i,i%4?"":"]",i%3?"":")",i%99?" ":"");}

MSVCはあなたにそれをさせますinf f()か?申し訳ありませんが私のコメントを削除するために、私はもっと短いものがあると思った(私はしませんでした)
ザカリー

@Zacharýいいえ、関数は単純すぎて、「f must int int」を生成すると思います。ところで、あなたのソリューションは3バイト短くなりました(iの増分の移動と組み合わせた圧縮を含む)
-HatsuPointerKun

1
ダン、ドットのように忘れてしまったprintfこと。その場合、C stdioを使用できませんでしたか?
ザカリー

2
使用/活用できるもう1つのことは、少なくともgcc 5.3.1では、#includeは不要であり、関数の戻り値の型も削除できるという事実です。さらに、int i関数の外側(グローバルスコープ内)を宣言すると、その値のデフォルトは0になり、データ型のデフォルトはになりintます。これにより、ループは0から始まります。これを修正するには、forループ内の条件式に増分を移動し、次のようにしますi;f(){for(;++i<=99;)
。– mriklojn

1
")\0"+i%3代わりに提案するi%3?"":")"。また、i=0ループの最初に追加する必要があると思います。
ceilingcat

3

Powershell、60バイト

"$(1..99|%{($_,"($_)","[$_]","([$_])")[!($_%3)+2*!($_%4)]})"

説明:

  • 4つの要素を持つ配列: $_, "($_)", "[$_]", "([$_])"
  • インデックス: [!($_%3)+2*!($_%4)]
  • 数字ごとに繰り返す
  • 結果を文字列に変換します

ゴルフの少ないテストスクリプト:

$f = {

$r = 1..99|%{
    ($_, "($_)", "[$_]", "([$_])") [!($_%3)+2*!($_%4)]
}
"$($r)"

}

$expected = '1 2 (3) [4] 5 (6) 7 [8] (9) 10 11 ([12]) 13 14 (15) [16] 17 (18) 19 [20] (21) 22 23 ([24]) 25 26 (27) [28] 29 (30) 31 [32] (33) 34 35 ([36]) 37 38 (39) [40] 41 (42) 43 [44] (45) 46 47 ([48]) 49 50 (51) [52] 53 (54) 55 [56] (57) 58 59 ([60]) 61 62 (63) [64] 65 (66) 67 [68] (69) 70 71 ([72]) 73 74 (75) [76] 77 (78) 79 [80] (81) 82 83 ([84]) 85 86 (87) [88] 89 (90) 91 [92] (93) 94 95 ([96]) 97 98 (99)'
$result = &$f
$result-eq$expected
$result

出力:

True
1 2 (3) [4] 5 (6) 7 [8] (9) 10 11 ([12]) 13 14 (15) [16] 17 (18) 19 [20] (21) 22 23 ([24]) 25 26 (27) [28] 29 (30) 31 [32] (33) 34 35 ([36]) 37 38 (39) [40] 41 (42) 43 [44] (45) 46 47 ([48]) 49 50 (51) [52] 53 (54) 55 [56] (57) 58 59 ([60]) 61 62 (63) [64] 65 (66) 67 [68] (69) 70 71 ([72]) 73 74 (75) [76] 77 (78) 79 [80] (81) 82 83 ([84]) 85 86 (87) [88] 89 (90) 91 [92] (93) 94 95 ([96]) 97 98 (99)

3

MathGolf41 40 34 29 バイト

♀({îû)(î+╫îa_'(\')ßyΓî34α÷ä§ 

注:末尾にスペースがあります

私の2番目のMathGolfの回答のみ。. @ JoKingの
おかげで-5バイト

オンラインでお試しください。

説明:

♀(             # Push 99 (100 decreased by 1)
  {            # Start a loop, which implicitly loops down to (and excluding) 0
   û)(         #  Push string ")("
      î+       #  Append the 1-indexed index
              #  Rotate the string once towards the right
   îa          #  Push the 1-indexed index of the loop, wrap in a list
   _           #  Duplicate it
    '(        '#  Push string "("
      \        #  Swap the top two items of the stack
       ')     '#  Push string ")"
         ßy    #  Wrap all three into a list, and join them
   Γ           #  Wrap all four into a list
               #  (We now have a list [N, "(N)", "[N]", "([N])"], where N is the index)
   î           #  Push the 1-indexed index of the loop
    34         #  Push 3 and 4 to the stack
      α        #  Wrap all three into a list
       ÷       #  Check for each if the index is divisible by it
               #  (resulting in either [0,0], [0,1], [1,0], or [1,1]
        ä      #  Convert from binary to integer
               #  (resulting in either 0, 1, 2, or 3
         §     #  Push the string at that index from the array
               #  Push a space
               # (After the loop, output the entire stack joined together implicitly)

@JoKingありがとう!q省略できることを知らず、ループで暗黙的に行われました。また、2/3/4文字列のビルトインがあることも知りませんでした。回転配列がラップされた配列では機能しないのは残念です。
ケビンCruijssen

まあ、それは私が代わりに、プログラムの終了時に明示的に出力暗黙の出力用に各反復を取引したことをより多くのだ
ジョー・キング

@JoKingええ、しかし、トップだけでなく、スタック全体が結合されて出力されるとは知りませんでした。:)
ケビンクルーッセン

私のソリューションは40バイトに近づいていましたが、角括弧ではなく中括弧を使用すべきだと誤解し、考えていました。ソリューションの良い仕事!
maxb


2

Lua、161 123バイト

b=""for i=1,99 do;c,d=i%3==0,i%4==0;b=b..(c and"("or"")..(d and"["or"")..i..(d and"]"or"")..(c and")"or"").." "end;print(b)

オンラインでお試しください!

ゴルフをしていない:

b = ""
for i = 1, 99 do
    c = 1 % 3 == 0
    d = 1 % 4 == 0
    a = ""
    if c then
        a = a .. "("
    end
    if d then
        a = a .. "["
    end
    a = a .. i
    if d then
        a = a .. "]"
    end
    if c then
        a = a .. ")"
    end
    b = b .. a .. " "
end
print(b)


2

C(gcc)、84バイト

main(i){while(99/i++)printf("%s%s%d%s%s ","("+i%3,"["+i%4,i-1,"]"+i%4,")"+i%3);}

各「ブラケット文字列」の先頭にヌルバイトがあります。

オンラインでお試しください!


そして、 "(" + i%3で、i = 2のアドレスがゼロのchar値を指していることをどのように知っていますか?{2,3}のiの "[" + i%4でも同じですか?
RosLuP

PPCGは実装によって言語を定義するため、gccで機能します。
デニス

私はあなたが(あまりなく、可能ではない)それはGCCコンパイラのすべての実装で[OK]をコンパイルされたコードは、おそらく唯一のあなたのPCで実行を言うことはできないと思います
RosLuP

@RosLuP gccがないほとんどのコンピュータ上と同じように、少なくとも同じアーキテクチャを持つもので、けれども仕事を
ASCIIのみの

スペースまたは速度のために最適化されてコンパイルされた場合、@ ASCIIのみが可能です...結果が異なるかどうかはわかりません
...-RosLuP

2

PowerShell67 62バイト

"$(1..99|%{'('*!($x=$_%3)+'['*!($y=$_%4)+$_+']'*!$y+')'*!$x})"

オンラインでお試しください!

基本的に、文字列の乗算とブール変数を使用したFizzBu​​zz(暗黙的に1または0にキャスト)。これらの文字列はパイプラインに残され、引用符内のスクリプトブロック内に収集されます。$OutputFieldSeparator配列のデフォルトはスペースなので、これは暗黙的にスペースで区切られた配列要素を提供します。


2

C#、124 117 123バイト

Kevin Cruijssenのおかげで-5バイト

x=>{for(int i=0;i++<99;)System.Console.Write((i%3<1?"(":"")+(i%4<1?"[":"")+i+(i%4<1?"]":"")+(i%3<1?")":"")+(i>98?"":" "));}

でテストする:

Action<int> t = x=>{for(int i=0;i++<99;)System.Console.Write((i%3<1?"(":"")+(i%4<1?"[":"")+i+(i%4<1?"]":"")+(i%3<1?")":"")+(i>98?"":" "));}
t.Invoke(0);
Console.ReadKey();

C#の進出に、私はわかります。C#では、三項演算子の左引数として整数を使用できますか、それともブール値にする必要がありますか?
ザカリー

私はC#についてあまり知りませんxi、代わりに使用できますので、心配する必要はありint ませんか?(もちろん、設定する必要があります)。
ザカリー

@Zacharýいいえ、CS0029エラー「暗黙的にintをブール値に変換できません」を生成します。そして、はい、私は使用iすることができ、私は0のときにそれを初期化できるという事実Invoke。しかし、それは、バイトカウントにt(Action<int>)とcall(t.Invoke(0))の宣言を含める必要があることを意味しませんか?
-HatsuPointerKun

何かx=>{for(x=0;x++<99;)Console.Write((x%3==0?"(":"")+(x%4==0?"[":"")+x+(x%4==0?"]":"")+(x%3==0?")":"")+(x%99==0?"":" "));};がうまくいくかどうかを尋ねてい ます。
ザカリー

1
5つすべて==0が可能<1です。
ケビンクルーイッセン


2

ルビー72 66バイト

p [*1..99].map{|x|a=x
a="[#{x}]"if x%4<1
a="(#{a})"if x%3<1
a}*' '

追加のトリミングをしてくれた@ jonathan-frechと@ conor-obrienに感謝します。


こんにちは、PPCGへようこそ!70バイト
ジョナサンフレッチ

PPCGへようこそ!ここでは、@JonathanFrenchの提案のオフ別の4バイトだ66のバイトので、a.join bアレイのためのa文字列は、bと等価であるa*b
コナー・オブライエン

2

PowerShell、98 82 74 67 63 62バイト

@Veskahのおかげでなんと-31バイト、@ ASCIIのみのおかげで-5バイト

(1..99|%{(($a=($_,"[$_]")[!($_%4)]),"($a)")[!($_%3)]})-join' '

オンラインでお試しください!

ここで何をしたかはまだよくわかりません。


70バイトのちょっとしたゴルフ。$ aを文字列としてキャストする必要はありませんが"$a"、値の代わりに使用できます。(注:単一引用符は$foo、二重引用符のみを置き換えません)。もう1つのトリックは、ifsが0または1のみを気にするため、ブールロジックを使用してバイトを保存できることです
-Veskah

リストインデックスも使用する場合は67バイト
ヴェスカー




1

perl -E、60バイト

$,=$";say map$_%12?$_%3?$_%4?$_:"[$_]":"($_)":"([$_])",1..99

数字の間に改行を使用できる場合は、いくつかのバイトを節約できます。その場合、を削除してループに$,=$";変更mapし、forループに移動できsayます。


1
あなたアビゲイルですか?の発明者/^1$|^(11+?)\1+$/
msh210

1
ワオ。ここにいることはなんと名誉なことでしょう!
msh210


1

バッチ、145バイト

@set s=
@for /l %%i in (1,1,99)do @set/an=%%i,b=n%%4,p=n%%3&call:c
@echo%s%
:c
@if %b%==0 set n=[%n%]
@if %p%==0 set n=(%n%)
@set s=%s% %n%

コードはサブルーチンに落ちますが、この時点ですでに文字列が出力されているため、コードは無害に実行されます。





1

Bash、61バイト

-14バイト、デニスのおかげ

seq 99|awk '{ORS=" ";x=$1%4?$1:"["$1"]";print$1%3?x:"("x")"}'

explanation

とても簡単です:

  • seq 1..99を生成します
  • 出力が1行になるように、出力awkレコード区切り文字(ORS)をスペースに設定してパイプします。
  • メインのawk本体は、数値が4で割り切れるときに「[]」を追加し、さらに3で割り切れるときに「()」を追加します。

オンラインでお試しください!



1

PHP, 65 bytes

while($i++<99)echo$i%4?$i%3?$i:"($i)":($i%3?"[$i]":"([$i])")," ";

or

while($i++<99)echo"("[$i%3],"["[$i%4],$i,"]"[$i%4],")"[$i%3]," ";

(requires PHP 5.5 or later)

Run with -nr or try them online.


1

Python 2, 78 bytes

i=0
exec"i+=1;u=i%3/-2*(i%4/-3-1);print'([%0d])'[u:7-u:1+(i%3<1<=i%4)]%i,;"*99

Try it online!

I envisioned this cool approach slicing '([%0d])' but I can't get the expressions any shorter.


1

Java 8, 92 91 bytes

-1 byte thanks to @Dana

i->{for(;i++<99;)out.printf((i>1?" ":"")+(i%12<1?"([%d])":i%3<1?"(%d)":i%4<1?"[%d]":i),i);}

Try it online!

Alternative solution, 82 bytes (with trailing space in the output - not sure if that's allowed):

i->{for(;i++<99;)out.printf((i%12<1?"([%d])":i%3<1?"(%d)":i%4<1?"[%d]":i)+" ",i);}

Explanation:

for(;i++<99;) - a for loop that goes from the value of i (reused as input, taken to be 0 in this case) to 99

out.printf(<part1>+<part2>,i); - formats the string before immediately printing it to stdout with the value of i

where <part1> is (i>1?" ":"") - prints the space before printing the number unless that number is 1, in which case it omits the space

and <part2> is (i%12<1?"([%d])":i%3<1?"(%d)":i%4<1?"[%d]":i) - if i is divisible by both 3 and 4, i has both square and round brackets around it; else if i is divisible by 3, i has round brackets; else if i is divisible by 4, i has square brackets; else, i has no brackets.


Save a byte by moving the space to the beginning of each loop iteration (i>1:" ":"")
dana

That would only work if I printed the result in reverse (see this) but would actually save 2 bytes instead of 1.
NotBaal

Unfortunately that's not the same as the expected output as per the question, but thank you for the suggestion nevertheless!
NotBaal

1
The "try it online" links seem to be broken. I was thinking i->{for(;i++<99;)out.printf((i>1?" ":"")+(i%12<1?"([%d])":i%3<1?"(%d)":i%4<1?"[%d]":i),i);} ?
dana

1
Ohhhh you're right that does work! Thanks for that!
NotBaal
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.