私は来ることを見ました


19

1より大きい整数または-1より小さい整数を取り込むプログラムまたは関数を作成します。つまり、入力は0、1、または-1にはなりません。

入力がの2場合、出力は次のようになります。

|\_/|
|___|

入力がの3場合、出力は次のようになります。

|\_/\_/|
|______|

入力がの4場合、出力は次のようになります。

|\_/\_/\_/|
|_________|

パターンは、より大きな入力に対しても同じ方法で継続します。たとえば、入力がの10場合、出力は次のようになります。

|\_/\_/\_/\_/\_/\_/\_/\_/\_/|
|___________________________|

入力がの-2場合、出力は次のようになります。

 ____
|    |
|_/\_|

入力がの-3場合、出力は次のようになります。

 _______
|       |
|_/\_/\_|

入力がの-4場合、出力は次のようになります。

 __________
|          |
|_/\_/\_/\_|

パターンは、より小さい入力に対してもまったく同じ方法で続きます。たとえば、入力がの-10場合、出力は次のようになります。

 ____________________________
|                            |
|_/\_/\_/\_/\_/\_/\_/\_/\_/\_|

出力は、オプションの末尾の改行を含む文字列として印刷または返すことができます。負の入力の出力の右上の「空の」コーナーはスペースであるか、空のままである場合があります。

バイト単位の最短コードが優先されます。


12
あなたがそこでしたことを見ました。
アレックスA.

回答:


1

Pyth、45バイト

jtW!J<Q0.b+.[YN+h*3t.aQJY.>[d.<"\_/"J\_)J" ||

オンラインで試す:デモンストレーションまたはテストスイート

説明:

jtW!J<Q0.b+.[YN+h*3t.aQJY.>[d.<"\_/"J\_)J" ||  implicit: Q = input number
    J<Q0                                       assign Q < 0 to J
                           [           )       create a list with
                            d                    * the string " "
                             .<"\_/"J            * the string "\_/" rotated to 
                                                   the left by J
                                     \_          * the string "_"
                         .>             J      rotate to the right by J
                                         " ||  the string " ||"
        .b                                     binary mapping, N iterates
                                               over list, Y over string:
           .[YN+h*3t.aQJ                         pad Y with N to reach a string
                                                 of length 3*(abs(Q)-1)+1-J
          +             Y                        and append Y
 tW!J                                           remove the first line if Q > 0
j                                               print each on separate line

4

CJam、56 50 49バイト

ri_(z"\_/"*'_@0>{\4>W<_,@*SooNoS}|1$,*]${'|\'|N}/

CJamインタープリターでオンラインで試してください

使い方

ri     e# Read an integer from STDIN and push it on the stack.
_(z    e# Push a copy, decrement it and apply absolute value.
       e# For positive n, (n -> n-1) and (-n -> n+1).
"\_/"* e# Repeat the string that many times.
'_     e# Push an underscore.
@0>    e# Check if the original integer is positive.
{      e# If it isn't:
  \    e#   Swap the generated string with the underscore.
  4>W< e#   Discard the string's first 4 and last character.
       e#   This makes the pattern of the bottom row start and end with an
       e#   underscore, truncating it to the correct length in the process.
  _,   e#   Push the length of a copy.
  @*   e#   Repeat the underscore that many times.
  So   e#   Print a space.
  oNo  e#   Print the underscores, then a linefeed.
  S    e#   Push a space.
}|     e#
1$,    e# Retrieve the strings length.
*      e# Repeat the underscore or space that many times.
]$     e# Wrap the two generated strings in an array and sort it.
{      e# For each string:
  '|\  e#   Push a vertical bar and swap the string on top of it.
  '|N  e#   Push a vertical bar and a linefeed.
}/     e#

3

Pyth、56 54バイト

オンライン通訳と電話でPythをゴルフしています。それは完全に素晴らしいアイデアです。

2015年10月15 日更新:ものを書き直し(まだ携帯電話で、笑)、2バイトを保存しましたが、そのうちの1つはオリジナルでもできました。

J<Q0Ljb"||"jPW!J_WJ[y<>*K+J*3t.aQ"\_/"JKy*K?Jd\_+d*K\_

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


2

Minkolang 0.8、100バイト

"|"nd0`u!vbd3*["_"]"|"25*"|"1g["\_/"]"|"(O).
"[d~g1"_"<.)O(" "D*3R~1"_"*52"|"D*3R1dg2"| "*52"|"]"\/_

スタックを構築し、一度にすべてを印刷します。これはゴルフができると確信していますが、これにはすでに多くの時間を費やしました...


1

JavaScript(ES6)、111 98バイト

最適なテクニックが発見されました!テンプレート文字列からこれらの補間関数をすべて削除すると、多くのバイトを節約できます。おそらくもっと短くすることができたかもしれませんが、おそらくそうではありません。いずれにせよ、ES6テンプレート文字列(および矢印関数)は素晴らしいです。:)

x=>(x>0?`|\\_/A|
|___A|`:` ___A_
|   A |
|_/\\A_|`).replace(/(...)A/g,(_,y)=>y.repeat(x>0?x-1:~x))

0

Python 2.7、144バイト

これには予想よりも多くのバイトがかかりました。コードは次のとおりです。

c=int(input())
p,w,n,u=list('| \n_')
a=abs(c)-1
d=3*a
if c>0:
 s=p+"\\_/"*a+p+n+p+u*d+p
else:
 d=d+1
 s=w+u*d+n+p+w*d+p+n+p+"_/\\"*a+u+p
print s

0

Java、272バイト

String f(int i) {
String p = i>0?"\\_/":"_/\\_",x = "|"+new String(new char[(i<0?-i:i)-1]).replace("\0",p)+"|",
l=new String(new char[x.length()-2]).replace("\0","_");
return i>0?x+"\n|"+l+"|":" "+l+" \n|"+new String(new char[x.length()-2]).replace("\0"," ")+"|\n"+x;
}

0

SpecBAS-167バイト

1 INPUT n: DIM s$="\_/","_/\": LET t$=s$(2-(n>0))*(ABS n-1)+("_"*(n<0)),u$="_"*LEN t$
2 TEXT IIF$(n>0,"|"+t$+"|"#13"|"+u$+"|"," "+u$+#13"|"+" "*LEN t$+"|"#13"|"+t$+"|")

IIF$はインラインIF文で#13あり、文字列に改行を埋め込む方法です(ハードコードされた文字列の間にある場合は常に「+」は必要ありません)。

数リリース前から、SpecBASを使用すると、1つのLETステートメントに複数の割り当てを行うことができます。これにより、一部の文字を節約できます。


0

Python 2.7、118バイト

n=input()*3-3
a=-n-6
s=' %s_\n| %s|\n|%s_|'%(a*'_',a*' ',a/3*'_/\\')
if n>0:s='|%s|\n|%s|'%(n/3*'\\_/',n*'_')
print s

120から118に降りるのは楽しかったです!


0

ルビー-113バイト

長すぎるようです。これをもう少しゴルフしようと思います。

n=gets.to_i;p,h=n.abs-1,?|;n>0 ? (puts h+'\\_/'*p+h,h+'___'*p+h):(k=p*3+1;puts' '+?_*k,h+' '*k+h,'|_'+'/\\_'*p+h)

0

C#、185バイト

C#はゴルフの繰り返し文字列に苦労しています。

完全にゴルフ:

string S(int n){int m=n>0?n:-n;return string.Format(n>0?"|{0}\n|{1}|":" {1}\n|{2}|\n|_{0}","|".PadLeft(m).Replace(" ",n>0?@"\_/":@"/\_"),"".PadLeft(m=m*3-(n>0?3:2),'_'),"".PadLeft(m));}

わかりやすくするために、インデントと新しい行が追加されました。

string S(int n){
    int m=n>0?n:-n;
    return string.Format(n>0?"|{0}\n|{1}|":" {1}\n|{2}|\n|_{0}",
        "|".PadLeft(m).Replace(" ",n>0?@"\_/":@"/\_"),
        "".PadLeft(m=m*3-(n>0?3:2),'_'),
        "".PadLeft(m)
    );
}

0

PowerShellの- 200 190 186 168 154

方程式(4-(($ n-2)3))から(3 $ n-6)に、いくつかの無関係な括弧とセミコロンを追加しました。

`nはに相当し[Environment]::NewLine、それ$s -f [args]はに相当することがわかりました[String]::Format

$n=$args;if($n-gt0){$s="|{1}|{0}|{2}|";$a=$n;$b=$n*3}else{$n*=-1;$s=" {2}{0}|{3}|{0}|_/{1}\_|";$a=$n-2;$b=$c=3*$n-2};$s-f"`n",("\_/"*$a),("_"*$b),(" "*$c)

説明では、括弧を明確にします。

$n=$args;

// Basically a way of coming up with a string to format and the 
// necessary counts of repeated characters
if($n-gt0){
  // Placeholder format
  $s="|{1}|{0}|{2}|{3}";
  // Number of repeated "\_/" instances
  $a=$n;
  // Number of repeated "_" instances
  $b=$n*3
} else { 
  $n*=-1;
  $s=" {2}{0}|{3}|{0}|_/{1}\_|";
  $a=($n-2);
  $b=(4+(($n-2)*3));
  // Number of repeated " " instances .. not needed for "positive" saw
  $c=$b;
};
[String]::Format($s,[Environment]::NewLine,"\_/"*$a,"_"*$b," "*$c)
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.