雕刻一些ASCII珠宝!


25

3月13日被认为是“ 国家珠宝节”,这是这一挑战的主题。所以,给定一个整数n,其中n大于0,创建一个ASCII宝石。例如:

n = 1          n = 2             n = 3
                                       ______
                     ____             /      \
 __                 /    \            \      /
/  \                \    /             \    /
\  /                 \  /               \  /
 \/                   \/                 \/

底部定义为珠宝中最高的一对\/。其余的是顶部。对于上面的示例,其中n = 1

Bottom: \  /    Top:   __
         \/           /  \

如您所见,底部由n + 1多层构成,\/中间各层之间有(1 * lines from the bottom) * 2间隔,n距珠宝底部的线条最大。如果我们拿第二个珠宝(n = 2),我们可以看到:

 ____
/    \      
\    /  2 (or n) layers from the bottom with 1*2*2 or 4 spaces in between \/
 \  /   1 layer from the bottom with 1*1*2 or 2 spaces in between \/
  \/    The bottom (0 layers) with 1*0*2 spaces or 0 spaces in between \/

顶部由1对的/\n*2在之间的空间与n*2在顶部下划线。

规则

  • 必须能够接受任何非零的正整数作为用户输入
  • 必须创建具有上述规格的珠宝(此处重述):
    • 顶部由1对的/\n*2在之间的空间与n*2在顶部下划线。
    • 底部由n + 1层间的层组成,\/中间有(1 * lines from the bottom) * 2间隔,n距珠宝底部的线条最大。
  • 可以在珠宝后尾随换行符,也可以在每行尾随空格。
  • 不允许出现标准漏洞

获奖标准

最小字节获胜!


4
严格地说,“非零正数”是多余的-如果要包括0,则必须说“负数”。
基金莫妮卡的诉讼

答案可以在PETSCII中吗?
Shaun Bebbers

3
随着数字的增加,“珠宝”开始看起来不像珠宝,而更像披萨片,或者也许只是午餐时间。
Marijn Stevering

Answers:


27

木炭,17字节

码:

NβG←β_↙↙¹→↘⁺β¹‖M→

说明:

Nβ                      # Place the input into β
   G←β_                 # Draw a line of length β with _ as the filling character
        ↙                # Move the cursor one down and one left
         ↙¹              # Draw a line from the cursor position to one position ↙
           →             # Move the cursor 1 to the right
             ⁺β¹         # Add one to the input and..
            ↘            # Create a line pointing ↘, with the size calculated above
                ‖M→     # Mirror to the right

一个非常整洁的命令‖M,它也会自动镜像/\

使用木炭编码

在线尝试!


那个镜像命令真的很棒!它是否也反映了方括号和其他字符?有没有办法覆盖这种行为?
DJMcMayhem

2
@DJMcMayhem :)
Adnan

27
大声笑,你是用木炭做的钻石!
SteeveDroz

8

05AB1E27 20字节

ƒN·ð×…\ÿ/}¹·'_×)R.c

在线尝试!

说明

ƒ                      # for N in range[0 ... n]
 N·ð×                  # push N*2 spaces
     …\ÿ/              # push the string "\ÿ/" with "ÿ" replaced by the spaces 
         }             # end loop
          Â            # push a reversed copy of the top of the stack 
                       # (the largest row of the bottom of the diamond)
           ¹·'_×       # push input*2 underscores
                )      # wrap the stack in a list
                 R     # reverse the list
                  .c   # join the list on newlines, padding each row to equal length

哈哈,真好!相信您可以更改D„/\„\/‡Â
阿德南

@Adnan:是的,我刚刚意识到自己正在努力改进:P
Emigna

8

Python 2,101 98 95字节

lambda n:'\n'.join([' '+'__'*n,'/'+'  '*n+'\\']+[' '*i+'\\'+'  '*(n-i)+'/'for i in range(n+1)])

在线尝试!

接受正整数并返回字符串的匿名函数

Python 3.6,92个字节(感谢Ben Frankel)

lambda n:f' {"__"*n}\n/{"  "*n}\\\n'+'\n'.join(' '*i+'\\'+'  '*(n-i)+'/'for i in range(n+1))

我找不到此版本的在线解释器,但由于v3.6中的f字符串,它的长度略短


您可以在Python 3.6中保存三个字节:lambda n:f' {"__"*n}\n/{" "*n}\\\n'+'\n'.join(' '*i+'\\'+' '*(n-i)+'/'for i in range(n+1))。利用f弦。
本·弗兰克尔

我非常确定repl.it有一个适用于Python 3的测试套件
Anthony Pham

@AnthonyPham repl.it和TryItOnline都使用Python 3.5,我已经检查过
数学迷

哇,终于!我想知道Python花了这么长时间吗?每种语言都应进行字符串插值...
Felix Dombek

7

PHP,123字节

echo($s=str_pad)(" ",$z=1+2*$a=$argv[1],_).$s("\n/",$z+1," ")."\\\n";for($i=0;$i<=$a;)echo$s($s("",$i)."\\",$z-$i++)."/\n";

143字节第一版

for(;$i<3+$a=$argv[1];$i++)echo 1-$i?str_pad("",$i?$i-2:1):"/",str_pad($i>1?"\\":"",$i<2?2*$a:2*($a-$i+2)+1,$i?" ":_),$i<2?$i?"\\":"":"/","\n";

在这里尝试!


在哪里可以尝试?
Anthony Pham

@AnthonyPham 在这里
阿德南

您可以将其设置为119个字节:ideone.com/RPCVZe
Tschallacka

@Tschallacka如果我认为我只使用Linux系统。
约尔格Hülsermann

好吧,只要您不使用notepad.exe进行编辑,大多数编辑器就会使用linux行结尾... i.imgur.com/QZsmf4r.png Windows控制台会很乐意将\ n显示为真实的换行符。是的,您可以将您的答案剃掉一些字节。
Tschallacka

6

V28 27 26字节

@DJMcMayhem通过使用>而不是节省了1个字节É

Ài__<esc>É ÙÒ r/Á\Ùr\$r/òÙlxx>

<esc>0x1b

在线尝试!

十六进制转储:

00000000: c069 5f5f 1bc9 20d9 d220 722f c15c d972  .i__.. .. r/.\.r
00000010: 5c24 722f f2d9 6c78 783e                 \$r/..lxx>

说明

最佳:

Ài__<esc>              " Write argument times __
É<space>               " Prepend a space to the line
Ù                      " Duplicate line below cursor, cursor also moves down
Ò<space>               " Replace every character with a space
r/                     " Change the first character in the line to a /
Á\                     " Append a \ to the end of the line

底部:

Ù                      " Duplicate
r\                     " Change the first character in the line to a \
$r/                    " Replace the last character with a /
ò                      " Until a breaking error occurs do:
  Ù                    "  Duplicate
  lxx                  "  Remove 2 middle characters (spaces)
  >                    "  Indent by one space (implicit ending >)
                       " Implicit ending ò

好答案!您可以更改É<space>>其在宏观结束时隐式填充到>>
DJMcMayhem

@DJMcMayhem不错的建议!那么>缩进一个空格而不是一个制表符?
Kritixi Lithos

是的 这是因为我希望set expandtabset shiftwidth=1
DJMcMayhem


5

JavaScript(ES6),80个字节

f=
n=>` ${"_".repeat(n*2)}
/${s=" ".repeat(n)}${s}\\`+s.replace(/|/g,"\n$`\\$'$'/")
<input type=number oninput=o.textContent=f(this.value)><pre id=o>


3

Python 3中,107个 105字节

n,s=int(input())," "
print(s+n*"__","/"+n*2*s+"\\",*[i*s+"\\"+2*(n-i)*s+"/"for i in range(n+1)],sep="\n")

从Stdin获得一个整数


3

MATL,34个字节

QE:qgOO(t~E3O(GQXy3*tPgEhv'_/\ 'w)

MATL在线上尝试一下

说明

QE:qg   % Create array [0 1 1 ... 1 1] of size2*(n+1)
OO(     % Turns last 1 into a 0: [0 1 1 ... 1 0]
t~      % Duplicate and negate: [1 0 0 ... 0 1]
E3O(    % Multiply by 2, turn last 2 into 3: [2 0 0 ... 0 3]
GQXy    % Push identity matrix of size n+1
3*      % Multiply by 3
tPgE    % Duplicate, flip, turn 3 into 2
h       % Concatenate the two matrices horizontally
v       % Concatenate all arrays vertically into a matrix
'_/\ '  % Push this string
w)      % Index (modular, 1-based) with the matrix into the string. Implicitly display

3

PowerShell中76,74个字节

param($n)" "+'_'*2*$n;"/$(' '*$n*2)\";$n..0|%{' '*($n-$_)+"\$(' '*$_*2)/"}

注意:在线示例包含一些包装示例。放入PoSH函数或脚本中以执行。

在线尝试!


欢迎来到PPCG!不错的第一个答案,很高兴看到另一个PowerSheller!您可以在循环中使用一个递增变量来保存几个字节- ' '*$i++而不是' '*($n-$_)
AdmBorkBork

3

C,131字节

i;f(n){for(printf(" ",i=0);i++<n*2;)printf("_");for(printf("\n/%*c\n",n*2+1,92,i=0);i++<n+1;)printf("%*c%*c\n",i,92,(n-i)*2+3,47);}

在线尝试!


我在哪里可以测试?
Anthony Pham

@AnthonyPham Tio链接已添加。
Steadybox

使用printf的宽度填充空格的好方法。如果您为printf创建宏,则可以再保存9个字节,删除第一个i = 0并添加新的变量j,而不是在第二次运行时将i重新初始化为0:i,j;f(n){for(p(" ");i++<n*2;p("_"));for(p("\n/%*c\n",n*2+1,92);j++<n+1;p("%*c%*c\n",j,92,(n-j)*2+3,47));}
Claudiu

@Claudiu谢谢,但是只有在第一次调用该函数时,该函数才会产生正确的输出,并且IIRC违反了此处的规则。该函数无论调用多少次都应该起作用。
Steadybox

@Steadybox哦,我知道了,对此表示抱歉。这是否适用于所有代码高尔夫问题?仅看这个特定问题,似乎并不需要多个输入。
Claudiu

2

Pyth,44个字节

+" "*Q"__"++\/**2Qd\\jm+++*d\ \\**2-Qd\ \/hQ

试试吧!

说明

该代码包括3部分:

+" "*Q"__"               # pretty straightforward " "+input()*"__"
++\/**2Qd\\              # d is defined as " ":  "/"+2*input()*d+"\"
jm+++*d\ \\**2-Qd\ \/hQ  # The third part is a bit more complex so I'll explain it further:

jm                   hQ  # Map some lambda function onto range(input()+1) and join the result on newlines
  +++*d\ \\**2-Qd\ \/    # Here d is the lambda argument (so I can't use it for spaces -.-) 
  +++*d\ \\**2-Qd\ \/    # In Python: d*" "+"\\"+2*(Q-d)*" "+"/"

2

Python3,104个字节

n=int(input());print(" "+"__"*n+"\n/"+"  "*n+"\\")
for i in range(n+1):print(" "*i+"\\"+"  "*(n-i)+"/")

该程序从STDIN中获取一个整数,并将珠宝返回STDOUT。


2

,43字节

42个字节的代码,-n标志+1 。

Ps.'_Xa*2P"/\"JsXa*2sX_.'\.sXa-_X2.'/M,a+1

将输入作为命令行参数。 在线尝试!

说明

分别构造前两行,然后使用map操作构造其余的珠宝:

Ps.'_Xa*2
      a*2  Cmdline arg, times 2
   '_X     That many underscore characters
 s.        Concatenated to a space character
P          Print (with newline)

P"/\"JsXa*2
        a*2  Cmdline arg, times 2
      sX     That many space characters
 "/\"J       Join the string "/\" with the above as the separator
P            Print (with newline)

sX_.'\.sXa-_X2.'/M,a+1
                  ,a+1  Numbers from 0 up to and including a
                 M      Map the following lambda function:
sX_                      Space, repeated (fn arg) times
   .'\                   Concatenate \
      .                  Concatenate:
       sXa-_              Space, repeated (a - (fn arg)) times
            X2            repeated twice
              .'/        Concatenate /
                         Print result list, newline separated (implicit, -n flag)

另一种解决方案

同样是42 + 1个字节,这次带有-l标志:

Ys.tAL'_.sX2+,a.0(yALRVyRXD1-_)R0'\R1'/ZDs

蒂奥



2

C,115字节

#define p printf(
i;j;f(n){for(p" ");i++<n;p"__"));for(p"\n/%*c",2*n+1,92);j<=n;p"\n%*c%*c",++j,92,n*2-j*2+3,47));}

在线尝试!

C,123字节

尽管挑战不是必需的,但可以以8个字节为代价来使该函数可重用(第一个解决方案依靠全局变量的隐式初始化节省了8个字节)。

#define p printf(
i;f(n){for(i=0,p" ");i++<n;p"__"));for(i=0,p"\n/%*c\n",2*n+1,92);i<=n;p"%*c%*c\n",++i,92,n*2-i*2+3,47));}

在线尝试!


2

批处理,152字节

@set s=
@for /l %%i in (1,1,%1)do @call set s=  %%s%%
@echo  %s: =_%
@echo /%s%\
@set s=\%s%/
:l
@echo %s%
@if %s:~-2%==/ set s=%s:\  = \%&goto l

测试:

n = 1
 __
/  \
\  /
 \/

n = 2
 ____
/    \
\    /
 \  /
  \/

n = 3
 ______
/      \
\      /
 \    /
  \  /
   \/

我将需要一个测试套件来进行测试。
Anthony Pham

2

C#,187个字节

我敢肯定有一个更紧凑的解决方案,但这是我的第一次尝试:

var a=" "+new string('_',2*n)+"\n/"+new string(' ',2*n)+"\\\n";for(int i=n;i>0;i--){a+=new string(' ',n-i)+"\\"+new string(' ',2*i)+"/\n";}a+=new string(' ',n)+"\\/";Console.Write(a);

在线尝试。


我将需要一个测试套件来进行测试。
Anthony Pham

1

JavaScript(ES6),93个字节

n=>(` 0
/2\\`+`
1\\4/`.repeat(k=++n)).replace(/\d/g,c=>' _'[+!+c].repeat(c&1?k-n-2:+c+--n*2))

演示版


1

直流,121字节

?d1+sa2*sb32P[0sq[lvPlq1+dsqlj>h]shlj0<h]srlbsj95svlrx2607Plbsj32svlrx[\]p0sd[ldsjlrx92Plbsjlrxlb2-sb[/]pld1+dsdla>k]dskx

在线尝试!


1

Perl的5 109 94 + 1(用于标志-p)= 95字节

在线尝试!

$l=$_*2;$s=" "."_"x$l."\n/"." "x$l."\\\n";$s.=" "x$_."\\"." "x($l-$_*2)."/\n"for 0..$_;print$s

可以这样运行:

perl -p <name of file> <<< n

不打高尔夫球

$l=$_*2;
$s=" "."_"x$l."\n/"." "x$l."\\\n";
$s.=" "x$_."\\"." "x($l-$_*2)."/\n"for 0..$_;
print$s

说明

#Sets $l to twice the value of the input 'n'
$l=$_*2;  

#Top 2 rows of jewel adding $l underscores then newline  
#followed by '/' and $l spaces.  The '\\\n' is an escaped '\' and a newline
$s=" "."_"x$l."\n/"." "x$l."\\\n";

#The meat of the jewel generation.  It contains a for-loop
#that iterates from 0 to $_ (the input value 'n')
#The loop uses its iterator value ($_ (which overrides the outer $_))
#to determine how many leading spaces it needs to apply.
#Then it adds a '\' with '\\' followed by $l-$_*2 number of spaces
#(the inside of the jewel).  Again, while under the umbrella of the for-loop,
#the $_ refers to the iterator value of the for-loop.
#After the inner spaces, it goes on to add in the '/' and a new line
$s.=" "x$_."\\"." "x($l-$_*2)."/\n"for 0..$_;

#Lastly, it prints the compiled Scalar value $s.  (In Perl, Strings are Scalar values or references
print$s

我将需要一个测试套件来进行测试,因为不是每个人都非常了解或有能力按照您所说的那样进行操作
Anthony Pham

@AnthonyPham我添加了“在线试用”链接
CraigR8806 17-3-28
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.