给我围栏!


15

挑战

这是一个简单的挑战。给定两个正整数, wh创建一个宽度为w,高度为的ASCII栅栏h。围栏应使用以下规则构造:

  • +角色将代表一个职位。
  • -字符将用于表示围栏的宽度。
  • |将被用于表示围栏的高度。
  • 精确-输出三个字符之后,必须随后输出一个+字符。除四个角以外,您输出a的任何其他时间都是无效的。您可以从左或从右开始遵循此规则(请参见示例),但是必须保持一致。+
  • 准确|输出两个字符之后,必须随后输出一个+字符。除四个角以外,您输出a的任何其他时间都是无效的。允许您从顶部或底部开始遵循此规则(请参见示例),但是必须保持一致。+
  • 每个围栏都将恰好具有四个角,并且每个角将用表示+

换句话说:每三个-字符必须输出一个+。并且每两个|字符必须输出一个+

你可以假设栅栏将永远是一个矩形,并且这两个wh永远不会大于100或小于1。尾随和/或前导空格是允许的。

示例/测试用例

w = 1
h = 1

+-+ 
| |
+-+


w = 3
h = 2

+---+
|   |
|   |
+---+


w = 5
h = 7

+---+--+ or +--+---+
|      |    |      |
|      |    +      +
+      +    |      |
|      |    |      |
|      |    +      +
+      +    |      |
|      |    |      |
|      |    +      +
+      +    |      |
|      |    |      |
+---+--+    +--+---+

w = 10
h = 5

+---+---+---+-+  or +-+---+---+---+
|             |     |             |
|             |     +             +
+             +     |             |
|             |     |             |
|             |     +             +
+             +     |             |
|             |     |             |
+---+---+---+-+     +-+---+---+---+


w = 4
h = 4

+---+-+ or +-+---+
|     |    |     |
|     |    |     |
+     +    +     +
|     |    |     |
|     |    |     |
+---+-+    +-+---+

规则



3
我是否应该理解可能没有两人+的接触?
xnor

@xnor是的,这是正确的。
Christian Dean

3
顺便说一句,巨大的第一个挑战。
xnor

1
@LeakyNun你的权利。制定规则时我没有想到这种情况。我添加了一条规则来说明为什么+-+-+-+-+-+无效。对困惑感到抱歉。
Christian Dean

Answers:


9

C,131字节

#define L for(i=0,j=w;j;)putchar(i++%4?--j,45:43);puts("+")
f(w,h,i,j,c){L;for(j=1;h;printf("%c%*c\n",c,i,c))c=j++%3?--h,124:43;L;}

在线尝试!

说明:

// The first and the last line are always similar, so let's use a macro
// to avoid code duplication.
#define L

// Let's initialize 'i' and 'j'. 'i' will be used to keep track of which
// character ('+' or '-') we should print, whereas 'j' starts from the
// input width and the loop terminates when 'j' reaches zero.
for(i=0,j=w;j;)

// We post-increment 'i' and take a modulo 4 of its previous value.
// If i%4 == 0, we print '+' (ASCII 43), otherwise we decrement 'j'
// and print '-' (ASCII 45).
putchar(i++%4?--j,45:43);

// After the loop we print one more '+' and newline.
puts("+")

// The function declaration which takes the two input parameters, and
// also declares three integer variables. These three variables could
// also be declared as global variables with the same bytecount.
f(w,h,i,j,c)

// The start of the function body. We use the macro 'L' to print the 
// first line along with a newline.
{L;

// This loop prints all the lines between the first and the last. 'j'
// keeps track of when we should output a '+' instead of a '|'. 'h',
// which is the input parameter for height, serves as a terminator
// for the loop as it reaches zero.
for(j=1;h;<stuff missing from here>)

// We post-increment 'j' and check if its previous value is divisible
// by three, and if it isn't, we decrement 'h' and assign 124 (ASCII
// value for '|') to 'c'. Otherwise we assign '+' (ASCII 43) to 'c'.
c=j++%3?--h,124:43;

// The following is inside the 'increment' part of the 'for' loop.
// We print the character corresponding to the value of 'c', then
// the same character again, but padded with i-1  spaces before it 
// ('i' hasn't been touched since the first loop, so it still stores
// the length of the first line), then a newline.
printf("%c%*c\n",c,i,c)

// Lastly we print the first line again using the same macro 'L'.
L;}

5

Python 3中140个 137 128 119 106 105字节

def f(w,h):a=~-w//3-~w;b=("+---"*w)[:a]+'+';print(b,*[i+' '*~-a+i for i in"||+"*h][:h+~-h//2],b,sep='\n')

在线尝试!


2
现在更长了,但问题已解决。
GarethPW

1
您可以通过删除之间的空间中保存一个字节in,并[w+1+(w-1)//3]]在最后一部分。
Christian Dean

1
欢迎来到PPCG!您也可以删除其中的空间'\n') for。另外,您可以更改(w-1)~-w允许删除括号的位置,因为一元运算符的优先级高于二进制运算符。与(h-1)-> ~-h(a-1)->相同~-a在线尝试
-128

1
另外,由于您的所有输出均已打印, def f(w,h)因此长度与相同lambda w,h,但是如果可以帮助您进一步打码,则允许您使用多行代码
-musicman523

1
a=~-w//3-~w; 保存1个字节
Felipe Nardi Batista

4

Mathematica,165个字节

v=Column;d[w_,y_,m_,n_]:=Table[If[Mod[i,y]==0&&i!=w,m,n],{i,w}];(a="+"<>d[#,3,"-+","-"]<>"+";b=v@d[#2,2,"|\n+","|"];v[{a,Row[{b,""<>Table[" ",#+Floor[#/3]],b}],a}])&

4

点子,38字节

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

Ph:'-Xa<>3JW'+PsX#h-2WR:'|Xb<>2J'+^xh

将width和height作为命令行参数。 在线尝试!

说明

                         a,b are cmdline args; s is space; x is empty string (implicit)
Ph:'-Xa<>3JW'+
   '-Xa                  String of hyphens of length a
       <>3               grouped into substrings of (maximum) length 3
          JW'+           Join on +, also wrapping the result in +
 h:                      Assign that string to h (mnemonic: "header")
P                        and print it (with newline)

PsX#h-2WR:'|Xb<>2J'+^xh
          '|Xb           String of pipes of length b
              <>2        grouped into substrings of (maximum) length 2
                 J'+     joined on +
                    ^x   and split on empty string (i.e. converted to list of chars)
 sX#h-2                  String of len(h)-2 spaces
       WR:               Wrap the spaces with the list of chars
                         Note 1: WR operates itemwise on lists, so the result is a list,
                          each item of which consists of the spaces wrapped in an item
                          from the list of chars
                         Note 2: the : compute-and-assign meta-operator is here abused
                          to give WR: lower precedence than J and ^ and avoid parentheses
P                        Print that list, newline-separated (-n flag)
                      h  Autoprint the header a second time as the footer

4

木炭,47 45 40字节

F⁴«¿﹪ι³FIη↓⁺×+¬﹪κ²|FIθ⁺×+¬﹪κ³-P+¿⁼ι¹J⁰¦⁰

说明:依次绘制每侧的-s / |s,+在必要时插入s,然后以结束+。在绘制了顶部和右侧之后,跳回到起点以相反的顺序绘制它们,从而有效地绘制了左侧和底部。我不知道是否允许旋转对称,但是如果允许,则为27 25字节:

F⁴«FI⎇﹪ι²ηθ⁺×+¬﹪κ⁻³﹪ι²-⟲T

通过绘制顶部,向左旋转,绘制右侧,再次旋转,然后重复反向绘制底部和左侧,将上述想法发挥到极致。


1
@LeakyNun上一次我击败Pyth是为了Double赢一些钻石,即使如此,它也只是更短。
尼尔

4

的JavaScript(ES6),133个 132字节

w=>h=>(a="-".repeat(w).replace(/--?-?/g,"+$&")+`+`)+(`
|`.padEnd(a.length)+`|`).repeat(h).replace(/(\|( +)\|\n)\1/g,`$&+$2+
`)+`
`+a

以currying语法输入: f(width)(height)

测试片段

f=
w=>h=>(a="-".repeat(w).replace(/--?-?/g,"+$&")+`+`)+(`
|`.padEnd(a.length)+`|`).repeat(h).replace(/(\|( +)\|\n)\1/g,`$&+$2+
`)+`
`+a
O.innerHTML=f(W.value=5)(H.value=10)
<div oninput="O.innerHTML=f(+W.value)(+H.value)">
W <input id=W type=number min=1> H <input id=H type=number min=1>
</div>
<pre id=O>



2

爪哇(OpenJDK的8) 178个 177字节

w->h->{int i=0;String t="",m,r;for(;i<w;)t+=i++%3<1?"+-":"-";r=t+="+\n";m=t.format("|%"+(t.length()-3)+"s|\n","");for(i=0;i<h;)r+=i++%2<1&i>1?m.replace("|","+")+m:m;return r+t;}

在线尝试!

-1字节感谢@KevinCruijssen


您可以通过使用参数来保存字节:w->h-> 在此处尝试。
Kevin Cruijssen

是的,我一直忘了要冒犯……这不是我发现的自然现象:s
OlivierGrégoire17年

1

木炭47 45 37字节

A…+---÷⁺²×⁴N³αA…+||÷⁺¹×³N²βPα↓βα+↖↑⮌β

在线尝试!

  • 在字符串创建中播放符号后节省了2个字节。
  • Neil节省了8个字节,他提出了一种更简单的方法来计算围栏的长度。

@Neil的方法不同:首先,我创建字符串αβ在水平和垂直边界中包含字符,使用Range运算符创建字符串的重复直到达到给定的长度。然后按正确的顺序打印它们:

  • 不移动光标就打印α。
  • 向下打印β。
  • 打印α。
  • 打印一个“ +”。
  • 向上和向左移动光标。
  • 将β向上打印,反转。

链接到详细版本


1
感谢您提醒我有关Range,这第二种方法可节省3个字节!
尼尔

@Neil很好,因为我刚超越了你,我简直不敢相信。:-)
查理(Charlie)

1
更好的是,我设法优化了您的表达式,节省了8个字节:A…+---÷⁺²×⁴N³αA…+||÷⁺¹×³N²βPα↓βα+↖↑⮌β
尼尔

@尼尔哇。这样的优化。非常木炭。
查理

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.