字母数字领结


14

输出此确切的文本:

1                i
12              hi
123            ghi
1234          fghi
12345        efghi
123456      defghi
1234567    cdefghi
12345678  bcdefghi
123456789abcdefghi
12345678  bcdefghi
1234567    cdefghi
123456      defghi
12345        efghi
1234          fghi
123            ghi
12              hi
1                i

单个尾随的换行符是可以接受的,但不允许其他格式更改。

规则和I / O

  • 无输入
  • 输出可以通过任何方便的方法给出。
  • 完整的程序或功能都是可以接受的。如果是函数,则可以返回输出而不是打印输出。
  • 禁止出现标准漏洞
  • 这是因此所有常用的高尔夫规则都适用,并且最短的代码(以字节为单位)获胜。

1
我们可以改用大写字母吗?
Kritixi Lithos

3
@Cowsquack这将是一个规则更改。它说输出此确切文本
丹尼斯

@Cowsquack Nope-需要小写。
AdmBorkBork

Answers:


13

C,87 85 81 80字节

j;main(i){for(;++i<19;)for(j=19;j--;)putchar(j?j<i^j<20-i?32:106-j-j/10*39:10);}

在线尝试!

说明

j; // same as int j;
main(i){ // same as int main(int i){, where i = argc (1 with no arguments)
  for(;++i<19;) // loop over rows, i = 2..18
    for(j=19;j--;) // loop over chars, j = 19..0
      putchar(j?j<i^j<20-i?32:106-j-j/10*39:10); // output characters:
      //      j?                           :10 // on last char (j=0), output \n
      //        j<i                            // check for top/left half
      //            j<20-i                     // check for bottom/left half
      //           ^                           // 1 if only one half matched
      //                  ?32:                 // if so, output space
      //                      106              // char code for j
      //                         -j            // get desired letter
      //                           -j/10*39    // subtract 39 for j>9 (numbers)
}

令我惊讶的是,^它的优先级比<…… 低!
林恩

@Lynn C(甚至Java / JS等)中的按位运算符的优先级都低于比较。这既适合代码高尔夫,又是一个非常好的错误源(认为if (x & 2 == 0)总是错误的源0
PurkkaKoodari





3

QBasic,72个字节

基于Taylor Scott的陈述

FOR y=-8TO 8
z=ABS(y)
?"123456789abcdefghi";
LOCATE,10-z
?SPC(2*z)"
NEXT

基本说明

在每一行上,我们打印完整的字符串123456789abcdefghi。然后,我们返回并用空格覆盖它的一部分。

完整说明

代码略有偏离:

FOR y = -8 TO 8           ' Loop for 17 rows
 z = ABS(y)               ' z runs from 8 to 0 and back to 8
 ? "123456789abcdefghi";  ' Print the full string and stay on the same line (important!)
 LOCATE , 10-z            ' Go back to column 10-z on that line
 ? SPC(2*z); ""           ' Print 2*z spaces
                          ' (SPC keeps the cursor on the same line unlesss you print
                          ' something after it, so we'll use the empty string)
NEXT                      ' Go to the next y value

这是Locate命令的真正巧妙用法
Taylor Scott

2

T-SQL,108个字节

DECLARE @ INT=8a:
PRINT STUFF('123456789abcdefghi',10-abs(@),2*abs(@),SPACE(2*abs(@)))
SET @-=1IF @>-9GOTO a

退货仅出于可读性。

尝试了很多其他变体,包括数字表,这是最短的。



2

Japt,20字节

9Æ9Ç>YÃê1 Ë?S:°EsH
ê

Japt口译员

输出为字符数组的数组。该-R标志不是必需的,它只是使输出看起来更好。

说明:

9Æ9Ç                    create a 9x9 2D array 
    >YÃ                 fill bottom left triangle with "false", upper right with "true"
       ê1               mirror horizontally
          Ë?S           replaces "true" with a space
             :°EsH      replaces "false" with the horizontal index + 1 converted to base 32
                  \n    Store the result in U (saves bytes by not closing braces)
                    ê   palindromize vertically

1

Stax,18 个字节

â4+╤jo♂▐▀3bkWíæß╝╖

运行并调试

说明:

9R$|[|<Va17T|]r|>\|pm Full program
9R$                   Produce "123456789"
   |[|<               Left-aligned prefixes (["1        ", "12       ", ...])
       Va17T          Produce "abcdefghi"
            |]        Suffixes (["abcdefghi", "bcdefghi", ...])
              r|>     Reverse and left-align (["        i", "       hi", ...])
                 \    Zip both arrays (["1                i", "12              hi", ...])
                  |p  Palindromize array
                    m Map over array, printing each with a newline                        

1

APL(Dyalog Unicode),30字节

(⊢⍪1↓⊖)(↑,\1↓⎕d),⌽↑,\⌽8199↑⎕a

在线尝试!

转换为矩阵(带空格的自动填充)

  • ,\ 的前缀

  • 1↓ 第一个元素从

  • ⎕d 这个字符串 '0123456789'

  • 这给出了字符矩阵

1个        
12       
123      
1234     
12345    
123456   
1234567  
12345678 
123456789

,

  • 倒转

  • 矩阵化

  • ,\ 的前缀

  • 倒转

  • 819⌶ 和小写

  • 9↑ 的前9个元素

  • ⎕a 这个字符串 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

  • 这给出了字符矩阵

        一世
       你好
      吉
     g
    efghi
   defghi
  克德菲
 bcdefghi
abcdefghi

而这个结果

1我
12喜
123吉
1234 fghi
12345 efghi
123456 defghi
1234567 cdefghi
12345678 bcdefghi
123456789abcdefghi

进行以下训练 (⊢⍪1↓⊖)

正确的论点

垂直连接

1↓ 第一行从中删除(这避免了中间行的重复)

正确的论点垂直反转


其他解决方案

33字节

(⊢⍪1↓⊖)(↑,\⍕¨q),⌽↑,\⎕ucs 106-q←⍳9

在线尝试!

33字节

(⊢⍪1↓⊖)(↑,\⍕¨q),⌽↑,\⌽ucs 96+q←⍳9

在线尝试!


1

木炭22 17字节

G↗↓←⁹β←G↖↓⁹⭆χι‖O↓

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

G↗↓←⁹β

绘制一个右下三角形,并使用小写字母填充它。(填充是基于用字母平铺平面,然后复制绘制的区域。)

向左移动以绘制数字三角形。

G↖↓⁹⭆χι

画一个左下三角形,并用数字填充。(由于三角形是在原点的左侧绘制的,因此数字是右对齐的,因此仅使用数字1到9。)

‖O↓

反映完成下半部分。


1

V25,21个字节

¬19¬ai8ñHÄ/á
r ge.YGp

在线尝试!

感谢nmjcman101,节省了2-4个字节!

十六进制转储:

00000000: ac31 39ac 6169 38f1 48c4 2fe1 0a72 2067  .19.ai8.H./..r g
00000010: 652e 5947 70                             e.YGp

我知道我今天所做的一切都在跟踪您的答案,但是我认为这对23人有效:在线尝试!
nmjcman101

@ nmjcman101无论出于何种原因,我都无法理解该版本的工作方式。但是我想出了一个更短的,所以谢谢!
詹姆斯(James)

/\d*搜索结束
nmjcman101

1

J,44字节

(m]\u:49+i.9),.(m=.,}.@|.)]\&.(|."1)u:97+i.9

在线尝试!

我试图以数字方式生成掩码1和零以用于索引编制,但摆脱多余行的成本很高,我放弃了:

   (9-.~i.18){0<:-/~(,|.)i.9
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1
1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1
1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1
1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1
1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1
1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1
1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1


1

Japt,24个字节

返回行数组

9Æ´AçXÄ
c¡A°îYd#a
Vù y ê

测试一下


说明

9Æ            :Map each X in the range [0,9)
  ´A          :  Prefix decrement A (initially 10)
    ç         :  Repeat
     XÄ       :  X+1
\n            :Assign to variable U
 ¡            :Map each element at index Y in U
  A°          :  Postfix increment A
    î         :  Repeat
      d       :  The character at codepoint
     Y #a     :  Y+97
c             :Concatenate with U
\n            :Assign to variable V
Vù            :Left pad each element in V to the length of the longest element
   y          :Transpose
     ê        :Palindromise

备择方案

9õÈîZqÃú Ë+EòdEn#i)¬ù9Ãê

测试一下

9ÇòdZn#i)cZòÄ)¬Ãú ®éJ´Ãê

测试一下


I'm wondering now if building this horizontally might not have led to a shorter solution! :\
Shaggy

1

QBasic, 87 bytes

An anonymous function that takes no input and outputs to the console.

For y=-8To 8:z=Abs(y):a$="123456789abcdefghi":?Mid$(a$,1,9-z)Spc(2*z)Mid$(a$,10+z):Next

This answer is technically a polyglot, and will function in VBA



1

Befunge-93, 314 308 bytes

<p0+3*67p0+4*77p0+3*77p0-7*88p0-6*88"#v#v>"
"i        "11g1-21p56+1g1+56+1p28*1g1+28*1p  ^       >25*
"        1"92g1+82p56+2g1-56+2p28*2g1-28*2p91g00g`#v_^   >
"ihgfedcba "93p26*3g1-26*3p">^"88*7-0p88*7-4pv     >25*
"987654321 "14p26*4g1+26*4p26*4g12g`#v_            ^
                             >:#,_@#:<

Try it online!

Golfed 6 bytes by placing a > with the p instruction


1

Matlab, 122 bytes

function[r]=f,s=[49:57,'a':'i'];r=[];for i=1:9,r=[r;s(1:i),repmat(' ',[1,18-2*i]),s(19-i:18)];end,r=[r;flip(r(1:8,:))];end

Try it Online!


1

PowerShell 5.1, 70 69 64 57 Bytes

Thanks Mazzy for -7 bytes

1..9+8..1|%{-join(1..$_+'  '*(9-$_)+' ihgfedcba'[$_..1])}

Turns out gluing it together manually saves a byte. Making it all one mega-join also saves 5 more. Also works by turning a range of ints into a char[] to get the a-i. Using a range over the actual letters is 5 bytes better.


1
try this: 1..9+8..1|%{-join(1..$_+' '*(9-$_)+' ihgfedcba'[$_..1])}. Note ' '*(9-$_) contains 2 space symbols
mazzy

1
@mazzy ooof, missing that double space trick. I was thinking of a variety of math statements but the obvious solution never occurred to me.
Veskah

1

C (gcc), 143 142 127+10=137 136+10=146 (compiler flags) bytes

-1 byte by replacing logical OR with bitwise operator.

-5 bytes thanks to Logern.

+9 bytes to fix the median line, that was output twice.

char*s="123456789abcdefghi";G{for(;j<18;++j)putchar(i>j|j>17-i?s[j]:32);puts("");}f(){int i=0,j=0;for(;i++<8;)G;g(i+1,j);for(;i-->1;)G;}

Compiler flag:

-DG=g(i,j)

This macro factorizes the occurences of g(i,j): function declaration and calls.

Try it online!

Different approach than Pietu1998's great answer, more straightforward (and readable), but higher score.

Entry point is function f(); function g() handles the printing of each consecutive line.

Could be made a full program by renaming f to main, but it would yet increase the score.

Pretty version, macro G expanded:

char *s = "123456789abcdefghi";
int g(int i, int j) {
    for(; j < 18; ++j)
        putchar((i > j | j > 17 - i) ? s[j] : 32);
    puts(""); // Break the line -- shorter than putchar(10) or printf("\n")
}
int f() {
    int i = 0, j = 0; // j is constant, declared here to not have to declare and init it inside g()
    for(; i++ < 8;) // Upper half of the tie
        g(i, j);
    g(i + 1, j); // Median line
    for(; i-- > 1;) // Lower half; --i > 0 would also work for the condition
        g(i, j);
}


137 but +10 for the compiler flags though
joH1

137 is the total, 127 bytes of code and 10 bytes of compiler flags.
Logern

Oops sorry, my bad. Updating now!
joH1

@Logern I took the liberty to rename the macro to G, to match the function name.
joH1



0

VBA, 75 bytes

An anonymous VBE immediate window function that takes no input and outputs to the console.

For y=-8To 8:z=Abs(y):a="123456789abcdefghi":Mid(a,10-z)=Space(2*z):?a:Next

0

Jelly, 22 21 bytes

9R€z⁶Zµạ106Ọ$Ṡ¡€Uṭ)ŒḄ

Try it online!

Relies on the (likely) unintended behavior that when (sign) acts on a character it yields Python's None. Because of this, is a one byte check for whether it's argument is a nonzero integer since None is falsey in Python. If this behavior is changed then works as well for one more byte.

Function that returns a list of lines.



0

Python 2, 97 94 bytes

i=o="123456789abcdefghi";c=8
while c:i=i[:c]+' '*(9-c)*2+i[-c:];o=i+'\n'+o+'\n'+i;c-=1
print o

Try it online!

Only posted as an alternative to using eval() and because I finally got it under 100. Basically starts with the middle row then works both up and down at the same time.



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.