艾菲尔铁塔:从“ A”中创建一个大的“ A”


20

创建该给多条线路的功能n,使一个bigA

  • 的水平条bigA必须在中间行,如果n是偶数,则水平条必须在中间的下一行
  • 假设要输出等宽字体

输出应该是一个字符串(或类似的字符数组,例如),具有清晰的换行符以分隔行,并为左填充使用正确的空格(您可以假定\ t为4个空格)。右边可以有任何空格。

例子

n = 1

A

n = 2

 A
AAA

n = 3

  A
 AAA
A   A

n = 4

   A
  A A
 AAAAA
A     A

n = 5

    A
   A A
  AAAAA
 A     A
A       A

这是受较小的“ H”创建“ H”的启发


我可以在右边添加空格吗?另外,是否允许尾随换行符?
Bubbler

@Bubbler,右边的任何空格都可以,尽管没有尾随换行符
Budd

是否允许我们返回2D字符数组而不是字符串?(提示:通常建议允许任何形式的输出)
OlivierGrégoire18年

1
@OlivierGrégoire当然,只要行之间有明显的中断(例如“ \ n”元素,嵌套数组)
Budd

1
@TonHospel,不,这真的打败了这个目的
Budd

Answers:


12

05AB1E,13个字节

码:

Ð;î¹)'A1376SΛ

使用05AB1E编码。在线尝试!

说明:

Ð                  # Triplicate the input.
 ;î                # Compute ceiling(n / 2).
   ¹               # Push the first input again.
    )              # Wrap into an array. For input 7, this would result in:
                     [7, 7, 4, 7].
     'A            # Push the character 'A'
       1376S       # Push the array [1, 3, 7, 6]. These are the directions of the canvas.
                     This essentially translates to [↗, ↘, ↖, ←].
            Λ      # Write to canvas using the previous three parameters.

帆布

我可能应该对画布进行更多记录(以及许多其他功能),但这基本上是对它的总结。画布根据给定的参数类型具有不同的“模式”。canvas命令具有三个参数:<length> <string> <direction>

由于长度和方向参数是列表,因此它将这些列表“压缩”以创建一组要执行的指令。字符串参数只是字母A,所以这是所有指令使用的填充字符。画布将其解释为以下指令集(对于输入7):

  • 画一条线长度的7与琴弦方向
  • 画一条线长度的7与琴弦方向
  • 画一条线长度的4与串方向
  • 沿方向用字符串A画一条长度为7的线

指示以以下方式翻译:

7   0   1
  ↖ ↑ ↗
6 ← X → 2
  ↙ ↓ ↘
5   4   3

如果未输出任何内容,则05AB1E自动输出画布结果。


1
非常感谢您对画布的解释,这是一个出色的功能:-)
Kaldo

TIL一词一词
Quintec,

@ thecoder16一式四份,一式五份,...,一式重复
魔术

哇。我对非重复性表示怀疑,但它与其他所有方式一样存在。当然,我们在英语xD中有如此无用的单词
Quintec,

1
@KevinCruijssen嘿,很抱歉收到我的所有回复,过去几周一直忙着我(最近72小时哈哈我只能睡8个小时),所以我认为我做不到现在可以进行任何操作,但是如果需要,可以随时将其添加到提示页面。
阿德南(Adnan)'18

6

木炭17 15字节

NθP×θAM⊘θ↗P^×θA

在线尝试!链接是详细版本的代码。说明:

Nθ

输入n

P×θA

打印big的水平条A。(对于偶数,n+1th始终与右侧重叠。)

M⊘θ↗

移到大的顶部A

P^×θA

双面打印大A


4

Python 2,80个字节

lambda n:'\n'.join(' '*(n+~i)+('A'+' A'[i==n/2]*n*2)[:i*2]+'A'for i in range(n))

在线尝试!

将所需的输出分为左侧空白,左侧A加上中间空白或As和右侧A。使用对固定字符串的切片计算中间部分。这允许使用相同的方式来生成第一行。


4

Stax,15 个字节

┴3╬*ôP^x'┌_╓J²♫

运行并调试

该程序在未打包,未打包和注释的情况下看起来像这样。

m       map over [1 .. input] using rest of the program, output each result
'A      "A" literal
xhi=    is the iteration index equal to (integer) half the input?
65*     multiply by 65 (character code of "A")
]i*     repeat that character (" " or  "A") i times
+       concat to initial "A"
x)      left pad to the original input
|p      palindromize (concatenate the reverse minus the last character)

运行这个


4

JavaScript(ES6),77个字节

此源代码具有矩形形状!哦,等等...错误的挑战:-/

f=(n,k=n>>1,p='A')=>--n?f(n,k,' '+p)+`
${p}${(k-n?' ':'A').repeat(n*2-1)}A`:p

在线尝试!


4

Python 3.6、79字节或73字节

使用f字符串对齐字母的水平部分:

lambda n:'\n'.join(f"{'A'+' A'[i==n//2]*2*i:>{n+i}}"[:-1]+'A'for i in range(n))

随着\b用于删除一个A(可能是作弊):

lambda n:'\n'.join(f"{'A'+' A'[i==n//2]*2*i:>{n+i}}\bA"for i in range(n))


3

J,65个字节

f=:3 :''' A''{~1(([:(<@;]+i.@+:)<.@-:)y)}([:(}:@|."1,.])=/~@i.)y'

在线尝试!

可以减少约。通过简单地使动词默认为12个字节,但是这样做有问题。

说明:

3 : '...' 表示一个明确的单线动词

y 是论点

=/~@i. 创建一个大小为参数的单位矩阵

    =/~@i. 4
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

([:(}:@|."1,.]) 在单位矩阵的镜像副本前加上每行的最后一个元素。

    ]a =. ([:(}:@|."1,.])=/~@i.) 4
0 0 0 1 0 0 0
0 0 1 0 1 0 0
0 1 0 0 0 1 0
1 0 0 0 0 0 1

1(...)}(...) 将其右参数中的位置更改为1,由左一个选择

([:(<@;]+i.@+:)<.@-:) -通过执行以下操作准备选择:

               <.@-: - halves the argument and finds the floor (finds the row number)
    <@;              - box the row, followed by a list of columns:  
        ]+i.@+:      - a list form the argumnt to the doubled row number

    ([:(<@;]+i.@+:)<.@-:) 4
┌───────────┐
│┌─┬───────┐│
││2│2 3 4 5││
│└─┴───────┘│
└───────────┘

    1(([:(<@;]+i.@+:)<.@-:) 4)}a
0 0 0 1 0 0 0
0 0 1 0 1 0 0
0 1 1 1 1 1 0
1 0 0 0 0 0 1

' A'{~ 在0和'A'的地方渲染一个空格,那里有1

    ' A'{~1(([:(<@;]+i.@+:)<.@-:) 4)}a
   A   
  A A  
 AAAAA 
A     A



2

SOGL V0.12,12字节

 A*:╚╥≤.»I:ž

在这里尝试!

说明:

 A*           repeat "A" input times
   :          duplicate it
    ╚         create a "/" diagonal of one of the copies of As
     ╥        palindromize it horizontally
      ≤       get the other copy of the "A"s on top
       .»I:   push floor(input/2)+1 twice
           ž  and at those coordinates in the palindromized diagonals place in the row of As

2

Japt -R20 19字节

Çç" A"gZ¶Uz¹i'A êÃû

试试吧


说明

                        :Implicit input of integer U
Ç                       :Create the range [0,U) and pass each Z through a function
         Uz             :  Floor divide U by 2
       Z¶               :  Test for equality with Z (true=1, false=0)
  " A"g                 :  Get the character in the string " A" at that index
 ç                      :  Repeat Z times
           ¹            :  (Closes a few nested methods)
            i'A         :  Prepend an "A"
                ê       :  Palindromise
                 Ã      :End function
                  û     :Centre pad each element to the length of the longest element
                        :Implicitly join with newlines and output

另类

(希望它可以帮助我节省一些钱!)

Æ'AúXÄ" A"gX¶Uz¹êÃû

1
另一个替代方案是一个字节长:ç h'AUz)¬íp ®i'A êÃû
ETHproductions

@ETHproductions替换p ²,它也是19个字节。
粗野的

+1比我的怪物更好。
奥利弗


1

果冻23 20 19 18字节

=þ`o\L‘HĊƲ¦UŒBị⁾A 

在线尝试!

=þ`创建大小的单位矩阵n

L‘HĊƲ查找水平条的行索引,然后¦挑选出该行并将其应用于o\创建水平条。

U反转每一行,这样我们就不会倒置“ A”,并且ŒB(palindromize;矢量化)成为“ A”的后半部分。

ị⁾A(格式中已修剪0的空格)将s 替换为空格,将s 替换1As。


1

T-SQL182个 177字节

DECLARE @n INT=5DECLARE @ INT=0a:DECLARE @s VARCHAR(9)=STR(POWER(10,@),@n)PRINT REPLACE(REPLACE(@s+REVERSE(LEFT(@s,@n-1)),'1','A'),'0',IIF(@=@n/2,'A',' '))SET @+=1IF @<@n GOTO a

第一版(182字节):

DECLARE @n INT=5DECLARE @ INT=0WHILE @<@n BEGIN DECLARE @s VARCHAR(9)=STR(POWER(10,@),@n)PRINT REPLACE(REPLACE(@s+REVERSE(LEFT(@s,@n-1)),'1','A'),'0',IIF(@=@n/2,'A',' '))SET @+=1 END

上面的版本可以使用@ n = 9。

这是另一个版本,其最高工作频率为@ n = 23,但有2个额外的字节:

DECLARE @n INT=5DECLARE @ INT=0WHILE @<@n BEGIN DECLARE @s VARCHAR(23)=STR(POWER(10.,@),@n)PRINT REPLACE(REPLACE(@s+REVERSE(LEFT(@s,@n-1)),'1','A'),'0',IIF(@=@n/2,'A',' '))SET @+=1 END

取消高尔夫:

DECLARE @n INT=5

DECLARE @i INT=0
WHILE @i<@n BEGIN
    DECLARE @s VARCHAR(9)=STR(POWER(10,@i),@n)
    PRINT REPLACE(REPLACE(@s+REVERSE(LEFT(@s,@n-1)),'1','A'),'0',IIF(@i=@n/2,'A',' '))
    SET @i+=1
END

1

Haskell中98个 97 95字节和109个字节

两种截然不同的方法。第一(95字节):

c!n=([1..n]>>c)++"A"
f n=unlines[" "!(n-x)++drop 3([" "!(abs$n`div`2-x+1)!!0]!(2*x))|x<-[1..n]]

和第二个(109字节):

m True='A'
m _=' '
g n=unlines[[m(abs(n-j)==l||l==q&&elem j[q+1..q+n])|j<-[1..2*n]]|l<-[0..n-1],q<-[n`div`2]]

在这里尝试!; 在这里尝试修改版本!

在这里尝试第三个版本!


欢迎来到PPCG!通过定义l为infix运算符,可以在第一种方法上节省一个字节。
Laikoni

m True='A'缩短为m b|b='A'
Laikoni '18

事实证明,甚至可以保存两个字节。谢谢!:)
Radek '18

1

Python 2,70个字节或65个字节

字符串列表是可接受的结果,如@Budd在注释中所述。

lambda n:['%*sA\n'%(n+i,('A'+i*2*' A'[i==n/2])[:-1])for i in range(n)]

在线尝试!


似乎是骗人的解决方案,使用\b。在TIO中看起来很时髦,在控制台中可以完成任务。

lambda n:['%*s\bA\n'%(n+i,'A'+i*2*' A'[i==n/2])for i in range(n)]

在线尝试!


0

Javascript,124个字节

一个相当幼稚的解决方案,让它可以练习js技能。

for(i=-1,p=" ".repeat(n-1)+"A ";++i<n;console.log(i-~~(n/2)?p:p.slice(0,i)+"A".repeat(n)),p=p.slice(1,n)+" "+p.slice(n-1)){}

开箱

for(
 //create the first line
 i=-1, p=" ".repeat(n-1)+"A "; 
 ++i<n;
 console.log( 
 //if we are not at the bar
      i-~~(n/2)?
 //otherwise, use the modified previous line
      p
 //slice the start of the previous line and add As
      :p.slice(0,i)+"A".repeat(n)), 
 //add a space in between the previous line and remove padding on each side
 p=p.slice(1,n)+" "+p.slice(n-1)){}


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.