我看到了即将到来


19

编写一个使用大于1或小于-1的整数的程序或函数。也就是说,输入将不是0、1或-1。

如果输入为2,则输出应为:

|\_/|
|___|

如果输入为3,则输出应为:

|\_/\_/|
|______|

如果输入为4,则输出应为:

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

对于较大的输入,该模式以相同的精确方式继续。例如,如果输入为10,则输出应为:

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

如果输入为-2,则输出应为:

 ____
|    |
|_/\_|

如果输入为-3,则输出应为:

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

如果输入为-4,则输出应为:

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

对于较小的输入,该模式以相同的精确方式继续。例如,如果输入为-10,则输出应为:

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

输出可以打印或作为字符串返回,并带有可选的尾随换行符。负输入的输出的右上角“空”角可能是空格,也可能保持为空。

以字节为单位的最短代码获胜。


12
看到了你在那里做的事。
Alex A.

Answers:


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

佩思,56 54字节

我正在和在线口译员在电话上打高尔夫球。那是个好主意。

2015年10月15日更新:我重写了该文件(仍在我的手机上,大声笑)并保存了2个字节,其中一个也可以用原始文件完成。

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允许您对一个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

Ruby-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)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.