半对角字母


35

给定英文字母,您的任务是为输入构建半对角字母。

如何建立半对角字母?

简要说明:首先,您将字母放在字母表中的位置PP此处为1索引)。然后,您将每个字母打印到一行上的输入(含)为止,在P-1该字母之前和重复该字母P时间,并与空格进行交织。

例子

  • 给定F,您的程序应输出:

    一种 
     BB 
      CCC 
       DDDD 
        电子电气设备 
         FFFFFF 
    
  • 给定K,您的程序应输出:

    一种
     BB 
      CCC 
       DDDD 
        电子电气设备 
         FFFFFF 
          GG 
           HHHHHHHH 
            IIIIIIIII 
             JJJJJJJJJJ 
              KKKKKKKKKKKK 
    
  • 给定A,您的程序应输出:

    A
    

规则

  • 您可以选择小写或大写字符,但是应该保持一致。

  • 您可能会有多余的空间,如下所示:

    • 一个一致的引导空间(每行)。
    • 尾随或前导换行符。
    • 尾随空格。
  • 输入和输出可以采用任何标准均值进行,并且存在默认漏洞。

  • 只要您还提供版本,就可以输出行列表。

  • 这是,因此以字节为单位的最短代码胜出!

受此挑战启发


输出为字符串列表可以吗?
亚当

2
为什么要下票?我可以改善什么?

1
当您说“ P在此处为1索引”时,是指挑战还是示例?
戴夫

3
@pizzakingme不,你可能不会。

1
打高尔夫球时,我意外地得到了一个有趣的图案:tio.run
##K0nO@f

Answers:


10

Python 3,59字节

lambda l:[' '*i+'%c '%(i+65)*-~i for i in range(ord(l)-64)]

在线尝试!

Python 3,61字节

lambda l:[' '*i+-~i*(chr(i+65)+' ')for i in range(ord(l)-64)]

在线尝试!(链接到精美印刷版)


8
我绝对没有理由不赞成。@downvoter可以解释吗?
Xcoder先生17年

1
我以为这只是一个错误的点击,或者是有人不喜欢缺乏解释(后者不太可能是IMO)
Conor O'Brien

我不喜欢Python,无法使用Python实现,所以答案对我没有用吗?只是在开玩笑,但按钮工具提示可能不符合该网站的规则
Thomas Weller

仅仅是我还是说Xcoder先生有1个代表...?
斯坦·史特鲁姆



7

PowerShell45 42字节

65..$args[0]|%{" "*$i+++"$([char]$_) "*$i}

在线尝试!

将输入作为文字字符,然后循环通过大写字母到该点,每次迭代都在适当数量的空格之前添加,然后是char \ space混合。

感谢TessellatingHeckler,节省了3个字节。


@TessellatingHeckler确实。我一直在打"$args"那么多球,在这里是行不通的,我忘记了这种[0]方法。哈哈。
AdmBorkBork

5

JavaScript(ES6),85个字节

输入和输出均小写。在每行上输出前导空格和尾随空格。

f=(c,k=10,C=k.toString(36),r=s=>`${s} `.repeat(k-9))=>r``+r(C)+(C==c?'':`
`+f(c,k+1))

演示版


`${s} `可以替换(s+"")为节省一个字节
路加福音

@卢克,我需要这个空间。可以用代替(s+" "),但是就那样长。
Arnauld

5

APL(Dyalog),26字节

提示输入标量字符。打印行列表。

(∊⍴∘'',' ',¨⍨⊢⍴⊃∘⎕A)¨⍳⎕A⍳⎕

在线尝试!(在另一个字节处具有ASCII艺术版本)

 提示输入

⎕A⍳ 找到ɩ ndex在一个 lphabet

 第一,许多ɩ ntegers

(……  对每个应用以下默认功能:

⊃∘⎕A 接从该argument'th信信 lphabet

⊢⍴ 周期性地将其重塑为参数长度

' ',¨⍨ 在每个位置附加一个空格

⍴∘'', 在参数长度的前面加上字符串(用空格填充)

ε NLIST(扁平化)


ASCII艺术作品版本的最左侧是一个;将字符串列表混合到字符表中。


4

Perl 5,31个字节

30个字节的代码+ 1个-l

print$"x$-,"$_ "x++$-for A..<>

在线尝试!


您可以通过使用say而不是-l标志来减少这种情况:在线尝试!
Xcali

@Xcali我在-E/上被撕裂了-M5.01say过去我已经使用了很多,可能会滥用一个事实,say它可以替代print受限来源挑战或类似的挑战,但是为了-3,我会继续现在就这样。有关公正的论点,请参见此meta帖子。赞赏输入!
Dom Hastings

3

Dyalog APL,38个字节

{↑{(y/' '),(2×y←⎕A⍳⍵)⍴⍵,' '}¨⎕A↑⍨⎕A⍳⍵}

在线尝试!

怎么样?

⎕A↑⍨ -拿字母直到

⎕A⍳⍵ -输入字符

¨ -每个字符

    ⍵,' ' -取个字符和一个空格

    (...)⍴ -重塑为

    2×y←⎕A⍳⍵ -字母中char的索引的两倍

    (y/' ') -并在字符索引前添加空格

-然后展平


3

APL(Dyalog Classic),26个字节

{↑{(≠\⍵<⍳3×⍵)\⍵⊃⎕A}¨⍳⎕A⍳⍵}

在线尝试!

说明

                      ⍳⎕A⍳⍵  generate indexes up to position of right arg 
{                          on each index apply function
   (≠\⍵<⍳3×⍵)                generate boolean mask for expansion (each line has a length 3 times its index ⍵, starting with  blanks and then alternating letter blank)
             \⍵⊃⎕A          expand character in position 
                             mix result into text matrix

天哪... 4 APL-er同时解决同一问题!:)我认为在codegolf中,您可以删除外部{},替换,并声称它是“完整程序”而不是函数。这将使您的解决方案成为最好的(到目前为止)。
ngn

必须是一个好兆头:)感谢您的建议。我已经看过了,但是不确定在哪里划界线。我想如果除去卷发并混合,我可以节省3个字节。
吉尔(Gil)

3

V28,26,25,23个字节(竞争

¬A[/a
lDÓ./& ò
ò-Ûä$Û>

在线尝试!

请注意,尽管我一直在计划添加某些功能长一段时间,但是这个挑战才使我最终决定这样做。

输出在每行上包含一个前导空格,在末尾包含一个换行符。

十六进制转储:

00000000: ac41 5b2f 1261 0a6c 44d3 2e2f 2620 f20a  .A[/.a.lD../& ..
00000010: f22d dbe4 24db 3e                        .-..$.>

说明:

¬A[         " Insert 'ABCDEFGHIJKLMNOPQRSTUVWXYZ['
   /        " Search for...
    <C-r>a  "   The input
l           " Move one character to the right
 D          " And delete every character after the cursor
  Ó         " Search for...
   .        "   Any character
    /       " And replace it with...
     & ò    "   That character followed by a space and a newline
ò           " Recursively...
 -          "   Move to the beginning of the next line up
  Ûä$       "   Make *line number* copies of the current line
     Û>     "   And indent this line by *line number* spaces

1
这是竞争。您可以删除标题备注。

对于那些不了解新元数据的人(例如我自己)很有用
Conor O'Brien

3

05AB1E,10个字节

A¹¡н«ðâƶāú

在线尝试!

-2感谢Adnan

附加»以使其分开打印。


您可以忽略这个,<因为一个一致的领导空间是可以的。
Emigna '17


A¹¡н«ðâƶāú应该工作10个字节
Adnan

@Adnan我认为这¹¡将使其无法正常工作...哦,这就是为什么里面有一个的原因«。:p
暴民埃里克(Erik the Outgolfer)'17年

3

R,94 88字节

-6字节,感谢Giuseppe

function(x,l=LETTERS)for(i in 1:match(x,l))cat(rep(' ',i-1),rep(paste(l[i],' '),i),'\n')}

取消高尔夫:

f=function(x,l=letters){
  for(i in 1:which(l==x)){
    A=paste(l[i],' ')
    cat(rep(' ',i-1),rep(A,i),'\n')
  }
}

88个字节:返回匿名函数就可以了,因为它f是单行的,所以您可以删除括号,并使用match而不是which保存一个字节。
朱塞佩


68个字节从stdin输入。
朱塞佩

3

Haskell,52 44字节

f k=[[" ",s:" "]>>=(['A'..s]>>)|s<-['A'..k]]

返回行列表。

在线尝试!

f k=                  -- main function is f, input parameter k
  [   |s<-['A'..k]]   -- for each s from ['A'..k]
     >>=              -- map (and collect the results in a single string) the function: 
         (['A'..s]>>) --  replace each element in ['A'..s] with
    [  ,  ]           --  over the list, containing
     " "              --   a single space to get the indent
        s:" "         --   s followed by space to get the letter sequence

编辑:@jferard:保存了三个字节。谢谢!


49个字节:f k=[tail$[" ",s:" "]>>=(['A'..s]>>)|s<-['A'..k]]
jferard

@jferard:非常感谢。重读挑战后,我注意到每行允许有一个前导空格,因此我们不需要tail$
nimi

2

JavaScript(ES8),92个字节

c=>(g=n=>n>9?[...g(n-1),`${n.toString(36)} `.repeat(n-=9).padStart(n*3)]:[])(parseInt(c,36))

使用小写字母。线有一个前导空间和一个尾随空间。返回行数组。

测试片段

let f=

c=>(g=n=>n>9?[...g(n-1),`${n.toString(36)} `.repeat(n-=9).padStart(n*3)]:[])(parseInt(c,36))

;O.innerText=f("k").join`\n`
<pre id=O></pre>


2

外壳,13个字节

z+ḣ∞øzRNC1…'A

将单引号中的字符作为命令行参数,将结果打印到STDOUT。 在线尝试!

说明

我正在利用Husk打印字符串列表的方式:将内部列表与空格连接,将外部列表与换行符连接。

z+ḣ∞øzRNC1…'A  Implicit input, say 'C'
          …'A  Range from A: "ABC"
        C1     Cut into strings of length 1: ["A","B","C"]
     z N       Zip with positive integers
      R        using repetition: x = [["A"],["B","B"],["C","C","C"]]
   ∞ø          The empty string repeated infinitely: ["","","",...
  ḣ            Prefixes: [[],[""],["",""],["","",""],...
z+             Zip with x using concatenation: [["A"],["","B","B"],["","","C","C","C"]]
               Implicitly join each inner list with spaces, join the resulting strings with newlines and print.

2

05AB1E15 14 13字节

感谢Adnan,节省了1个字节

A¹¡н«ƶ€S»¶¡āú»

在线尝试!Ascii艺术版本

说明

A                # push lowercase alphabet
 ¹¡              # split at input
   н             # get the first part
    «            # append the input
     ƶ           # repeat each a number of times corresponding to its 1-based index
      €S         # split each to a list of chars
        »        # join on spaces and newlines
         ¶¡      # split on newlines
           āú    # prepend spaces to each corresponding to its 1-based index
             »   # join on newlines


@EriktheOutgolfer:我们做得非常类似,但是您非常好的主意是在提升之前添加一个空格,消除了对连接的需要,这使您的连接更短了。我没有读过前导/后缀空格,也没有按列表输出,所以希望可以教我在实施之前阅读全部挑战:P
Emigna


A¹¡н«而不是ADIk>£应该工作
Adnan

@Adnan:谢谢!我确实有,A¹¡н但是没有考虑«拿到最后一封信,所以它不够好:P
Emigna


2

QBasic,79 74 72字节

感谢泰勒·斯科特节省字节(两次!)

FOR i=1TO ASC(INPUT$(1))-64
?TAB(i)
FOR j=1TO i
?CHR$(64+i)" ";
NEXT j,i

使用大写。输入是通过按键输入的,不会回显到屏幕上。

说明

We loop i from 1 up to the limiting letter's position in the alphabet (1-based). For each i, we move to column i of the screen using TAB; then, i times, we print the ith letter of the alphabet followed by a space.


As it turns out you can use the INPUT$(1) command as a direct replacement for the variable z$ for a delta of -2 bytes
Taylor Scott

@TaylorScott Good idea, thanks!
DLosc

2

Japt -R, 24 23 17 15 bytes

Outputs an array, includes a leading newline and a leading & trailing space on each line.

IòUc ÏçSiXd¹iYç

Test it

  • 1 byte saved with help from Oliver and a further 6 thanks to him pointing out a better way to generate the initial array.

1

Charcoal, 18 bytes

F⁺⌕αθ¹«P×⁺§αι ⁺ι¹↘

Try it online!


Nah, you can't let 05AB1E beat Charcoal... :P
totallyhuman


Sadly arbitrary leading whitespace isn't allowed otherwise E…·?θ⁺× κ⪫× κι would do the job in 14 bytes.
Neil

@Neil One leading whitespace is allowed, but I'm not sure how ? got in there. It should be A instead I think. Oh wait, ohhhhh I see what you mean.
Erik the Outgolfer

1

Braingolf, 65 bytes

a#a-# 7-,-~vc<!?>[$_]:$_|&,(.#a-!?.>[# M]1+>[.M# M]:$_!@|v#
&@R);

Try it online!

Lowercase.

Contains 1 trailing space on each line, and a trailing newline at the end of output.



1

JavaScript, 102 94 bytes

2 bytes saved thanks to Neil

f=
a=>[...Array(parseInt(a,36)-9)].map((a,b)=>''.padEnd(b).padEnd(b*3+1,(b+10).toString(36)+' '))

console.log(f('k').join`\n`)


1

Retina, 51 bytes

^.
$&$&
}T`L`_L`^.
.
$.`$* $&$.`$* ¶
+`(\w) \B
$&$1

Try it online! Explanation:

^.
$&$&

Duplicate the (first) letter.

}T`L`_L`^.

Rotate it back 1 in the alphabet, or delete it if it's a duplicate A. Keep duplicating and rotating until we duplicate A, at which point the deletion undoes the duplication and the loop completes.

.
$.`$* $&$.`$* ¶

Replace each letter with a line with the letter padded on both sides.

+`(\w) \B
$&$1

Insert duplicate letters between all pairs of padding spaces to the right of existing letters.



1

Charcoal, 15 bytes

F…·AS«P⪫E…@ιι ↘

Try it online! Link is to verbose version of code. Explanation:

 …·AS           Inclusive character range from A to the input
F    «          Loop over each character
         …@ι    Exclusive range from @ to the current character
        E   ι   Replace each element with the current character
       ⪫        Join with spaces
      P         Print without moving the cursor.
              ↘ Move the cursor down and right.

If extra padding was legal, this would work for 14 bytes:

E…·?θ⁺× κ⪫× κι

Try it online! Link is to verbose version of code.


1

Mathematica, 70 bytes

(T=Table)[""<>{" "~T~i,T[Alphabet[][[i]]<>" ",i]},{i,LetterNumber@#}]&

lowercase

outputs a list

thanx @ngenisis for corrections

For version place Column@ at the beginning


1

Excel VBA, 72 Bytes

Anonymous VBE immediate window function that takes input from cell A1 and outputs to the VBE immediate window

For i=1To Asc([A1])-64:[B1]=i:?Space(i-1)[REPT(CHAR(B1+64)&" ",B1)]:Next

1

Pyth, 17 bytes

.e+*kd*+bdhk<GhxG

Try it here (pretty print version).


How does this work?

  • hxG - Takes the index of the input in the lowercase alphabet.

  • <G - Trims every character after the input from the alphabet.

  • .e - Enumerated Map. Maps over the trimmed alphabet with the indexes as k and the letters as b.

  • *kd - Append k spaces.

  • +bd - b + a space (the current letter + space).

  • *...hk - Repeat k+1 times.

  • +(...)(...) - Concatenate.


1
One of my favorite things about Pyth is writing an answer and finding that someone wrote the same answer, character for character. It hits that Python "there is a best answer" spot perfectly!
Dave

@pizzakingme Yeah, I wonder if I can do better
Mr. Xcoder

the space addition feels wrong, I think better is possible
Dave

@pizzakingme I could get .e+*kdjd*bhk<GhxG as 17 bytes as well
Mr. Xcoder

16 bytes: .e+*kd*+bdhkhcGQ
Dave

1

C++ (gcc), 164 bytes

#include<iostream>
#define f for(int i=0;i<o-'`';i++)
using namespace std;int main(){char c;cin>>c;for(char o='a';o<=c;o++){f cout<<' ';f cout<<o<<' ';cout<<'\n';}}

My first attempt after a long time lurking!

Ungolfed code below:

#include <iostream>

using namespace std;
#define f for (auto i = 0; i < output - '`'; i++)

int main()
{
  char input;

  cin >> input;

  for (char output = 'a'; output <= input; output++)
  {
    f cout << ' ';

    f cout << output << ' ';

    cout << endl;
  }
}

Try it online!


I know there has to be a bunch of golfing things to do, but so far, that's the smallest I've gotten.
Drise
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.