这只是一个输入错误


17

继续我的那只是一个错误挑战:

输入:

一个字符串,由可打印的ASCII字符组成,没有空格和换行符。

输出:

首先,通过还原输入并将其添加到自身之前,将输入转换为回文,不包括中间字符(即使用输入1234567890,它将变为0987654321234567890))。

然后输出此文本:

0        9        8        7        6        5        4        3        2        1        2        3        4        5        6        7        8        9        0
         0       9       8       7       6       5       4       3       2       1       2       3       4       5       6       7       8       9       0
                  0      9      8      7      6      5      4      3      2      1      2      3      4      5      6      7      8      9      0
                           0     9     8     7     6     5     4     3     2     1     2     3     4     5     6     7     8     9     0
                                    0    9    8    7    6    5    4    3    2    1    2    3    4    5    6    7    8    9    0
                                             0   9   8   7   6   5   4   3   2   1   2   3   4   5   6   7   8   9   0
                                                      0  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  0
                                                               0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
                                                                        0987654321234567890
                                                               0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
                                                      0  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  0
                                             0   9   8   7   6   5   4   3   2   1   2   3   4   5   6   7   8   9   0
                                    0    9    8    7    6    5    4    3    2    1    2    3    4    5    6    7    8    9    0
                           0     9     8     7     6     5     4     3     2     1     2     3     4     5     6     7     8     9     0
                  0      9      8      7      6      5      4      3      2      1      2      3      4      5      6      7      8      9      0
         0       9       8       7       6       5       4       3       2       1       2       3       4       5       6       7       8       9       0
0        9        8        7        6        5        4        3        2        1        2        3        4        5        6        7        8        9        0

(从两个方向的中间向外,每个字符比上一行多隔开一个空格。)

挑战规则:

  • 您需要打印输入长度的两倍,减去3行。所以用输入1234567890,上面显示的输出为17行(长度为10 * 2-3)。
  • 输入将仅包含可打印的ASCII(不包括空格,制表符和换行符)。
  • 尾随空格是可选的。
  • 一条尾随的换行符是可选的。
  • (不允许)前导空格或前导换行符。
  • 您可以假设输入将始终至少包含四个字符。
  • 与我不同的,这只是一个错误挑战,输入和输出格式都很灵活。因此,您可以将结果输出为字符串数组,字符串列表等。

通用规则:

  • 这是,因此最短答案以字节为单位。
    不要让代码高尔夫球语言阻止您发布使用非代码高尔夫球语言的答案。尝试针对“任何”编程语言提出尽可能短的答案。
  • 标准规则适用于您的答案,因此您可以使用STDIN / STDOUT,具有正确参数的函数/方法和返回类型的完整程序。你的来电。
  • 默认漏洞是禁止的。
  • 如果可能的话,请添加一个带有测试代码的链接。
  • 另外,如有必要,请添加说明。

测试用例:

Input: 1234567890
Output:
0        9        8        7        6        5        4        3        2        1        2        3        4        5        6        7        8        9        0
         0       9       8       7       6       5       4       3       2       1       2       3       4       5       6       7       8       9       0
                  0      9      8      7      6      5      4      3      2      1      2      3      4      5      6      7      8      9      0
                           0     9     8     7     6     5     4     3     2     1     2     3     4     5     6     7     8     9     0
                                    0    9    8    7    6    5    4    3    2    1    2    3    4    5    6    7    8    9    0
                                             0   9   8   7   6   5   4   3   2   1   2   3   4   5   6   7   8   9   0
                                                      0  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  0
                                                               0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
                                                                        0987654321234567890
                                                               0 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 0
                                                      0  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9  0
                                             0   9   8   7   6   5   4   3   2   1   2   3   4   5   6   7   8   9   0
                                    0    9    8    7    6    5    4    3    2    1    2    3    4    5    6    7    8    9    0
                           0     9     8     7     6     5     4     3     2     1     2     3     4     5     6     7     8     9     0
                  0      9      8      7      6      5      4      3      2      1      2      3      4      5      6      7      8      9      0
         0       9       8       7       6       5       4       3       2       1       2       3       4       5       6       7       8       9       0
0        9        8        7        6        5        4        3        2        1        2        3        4        5        6        7        8        9        0

Input: ABCD
Output:
D  C  B  A  B  C  D 
   D C B A B C D
      DCBABCD
   D C B A B C D
D  C  B  A  B  C  D

Input: =>)}]
Output:
]   }   )   >   =   >   )   }   ]
    ]  }  )  >  =  >  )  }  ]
        ] } ) > = > ) } ]
            ]})>=>)}]
        ] } ) > = > ) } ]
    ]  }  )  >  =  >  )  }  ]
]   }   )   >   =   >   )   }   ]

Input: XXxxXX
Output:
X    X    x    x    X    X    X    x    x    X    X
     X   X   x   x   X   X   X   x   x   X   X
          X  X  x  x  X  X  X  x  x  X  X
               X X x x X X X x x X X
                    XXxxXXXxxXX
               X X x x X X X x x X X
          X  X  x  x  X  X  X  x  x  X  X
     X   X   x   x   X   X   X   x   x   X   X
X    X    x    x    X    X    X    x    x    X   X

每行的前导空格是否相等?
亚当

@Adám抱歉,没有。任意数量的尾随空格都可以,但前导空格不行。我将在挑战中指定此规则。
凯文·克鲁伊森

2
“您需要打印输入的长度减去3行。” 然后是““(长度10 * 2-3)”。第一句说“不加倍”,第二句说“加倍”。那是什么呢?
奥利维尔·格雷戈尔

@OlivierGrégoire糟糕,第一行的长度应为长度减去负3的2

Answers:


5

木炭,14字节

E⁻Lθ¹⪫θ× ι‖O←↑

在线尝试!

AST:

Program
├Print
│└E: Map
│ ├⁻: Difference
│ │├L: Length
│ ││└θ: Identifier θ
│ │└1: Number 1
│ └⪫: Join
│  ├θ: Identifier θ
│  └×: Product
│   ├' ': String ' '
│   └ι: Identifier ι
└‖O: Reflect overlap
 └Multidirectional
  ├←: Left
  └↑: Up

可能是16个代码点,但是正如UTF-8 Python 3报告的那样,它是41个字节。您使用哪个字符集将其设置为16个字节?
Janus Troelsen

@JanusTroelsen这是一个自定义字符集。
暴民埃里克(Erik the Outgolfer)'17年

⪫在弦上工作吗?必须记住,下次……
尼尔(Neil)

@Neil是的,不是吗?
暴民埃里克(Erik the Outgolfer)

从技术上讲,您的编辑是没有竞争的,因为相关的提交将挑战提出了日期。
尼尔

7

Japt22 21字节

¬Å£¬qYîÃy w ê y w ê ·

在线测试!

说明

前8个字节生成模式的右下象限:

 ¬ Å  £    ¬ qYîÃ
Uq s1 mXY{Uq qYî} 

Uq                 : Split the input into characters.
   s1              : Slice off the first.
      mXY{      }  : Map each item X and index Y to
          Uq       :   the input split into chars,
             q     :   joined with
              Yî   :     Y spaces.

在这一点上,我们有一个例如数组["ABCD", "A B C D", "A B C D"]。不幸的是,这需要13个字节才能平方:

y w ê y w ê ·
y w ê y w ê qR
y                : Pad each line to the same length with spaces and transpose.
  w              : Reverse the array, and
    ê            : palindromize. Now we have the bottom half of the output transposed.
      y          : Transpose back.
        w ê      : Reverse and palindromize again, giving the full output.
            qR   : Join with newlines.
                 : Implicit: output result of last expression

希望我想使用转置-做得很好:)
Shaggy

我可以问一个我不知道答案的问题吗?您和这里的其他人是否实际上以缩小版本进行编码?
gdbj

如果你敢。可能不会。
CalculatorFeline

1
@gdbj我通常会做...这是一个不好的迹象吗?:P
ETHproductions'7

@ETHproductions只是在告诉一个朋友,这类似于真实的高尔夫:沮丧地学习,奇怪地上瘾以及在其中表现出的技巧引起人们的尊重。
gdbj


5

的JavaScript(ES6) 159个 136 129 127字节

f=(i,x=1-(l=i.length-1),y=x<0?-x:x,[,...b]=i)=>l>x?''.padEnd(l*(l+~y))+[...b.reverse(),...i].join(''.padEnd(y))+`
`+f(i,x+1):''

在线尝试!以下说明

// This is a recursive function
// First, inputs and various variable initializations
// by using defaults
let func = (
  // Text input, will not be modified through recursion
  input,

  // Current line, for the first function call we start from -lines to +lines
  // It's roughly equivalent to lines*2 but this helps us computing the spacing
  // Also computing the total amount of lines
  currentLine = 1 - (totalLines = input.length - 1),

  // Getting the absolute value of the current line (like Math.floor)
  absCurrentLine = currentLine < 0 ? -currentLine : currentLine,

  // Getting the input without it's first letter, useful for the palidrome of the input
  [,...slicedInput] = input

// Base case, stopping the recursion if the current line
// is still below the total amount of lines
) => totalLines > currentLine

  // Leading spacing
  ? ''.padEnd(totalLines * (totalLines + ~absCurrentLine)) + 

  // Putting together the palindrome version and adding spaces between the chars
    [...slicedInput.reverse(), ...input].join(''.padEnd(absCurrentLine)) + `

  // Line return + recursion call
` + f(input, currentLine + 1)
  : ''

首先进入codegolf,对于任何明显的错误,我深表歉意。

感谢Justin Mariner节省了23个字节!感谢Craig Ayre节省了11个字节并提供了错误报告。


不错的第一答案,我+1,欢迎您使用PPCG。我对Js不太熟悉,但是可以从x-2而不是-1开始,然后更改x<=l为,++x<=l以便可以删除x++和保存一个字节吗?
凯文·克鲁伊森

我尝试过但没有设法解决,因为它lines也与空间纠缠在一起,因此最终要花费更多的字节才能使它们重新工作。但我相当确定,有一种更好的方法来组织代码并添加您的建议。
萨明

1
欢迎来到PPCG,非常好的第一篇文章和解释!在这里打低到133字节。包括更改内容的说明。
贾斯汀·马里纳

不幸的是你的输出似乎并没有预期的输出完全匹配:在这里看到的
克雷格·艾尔

1
太好了,我添加了您的新功能并更新了说明
Saming

4

SOGL V0.1222 18 个字节

ā,⁄H{,čFH@*∑Κ}▓±╬-

在这里尝试!

说明:

ā                   push an empty array - canvas
 ,⁄H{        }      input length-1 times do
     ,                push the input
      č               chop it into chars
       FH@*           get the current iteration-1 amount of spaces
           ∑          join the chopped input with the spaces
            Κ         prepend it to the array
              ▓     space to a square
               ±    reverse each string in that list
                ╬-  quad-palindromize with 1 X and 1 Y overlap and without swapping characters

╬-四向palindromize命令吗?我只是在这里希望Japt有类似的东西...:P
ETHproductions'Jul

@ETHproductions FWIW,很多东西
dzaima

我知道,因此我为什么要指定╬-。但是,谢谢,由于某种原因我找不到该页面
ETHproductions'Jul

@ETHproductions哦。它仅存在于解释器的SOGLOnline副本中:/在某些时候,我需要将两者结合在一起:|
dzaima

嗯......你们应该实现自动F是否需要这种耐心。
暴民埃里克(Erik the Outgolfer)

4

Python 3中149个141 95字节

def f(s):l=len(s)-1;[print((' '*abs(i)).join(s[:0:-1]+s).center(2*l*l+1))for i in range(1-l,l)]

在线尝试!

感谢@KevinCruijssen和@ETHproductions节省了一些字节

特别感谢@notjagan节省了46个字节!


1
更改l=len(s);l=len(s)-1;然后更改所有涉及的值l(即l-1-> l; -l+2-> -l+1;等)要短8个字节。在这里尝试。
凯文·克鲁伊森

1
-l+2-> 2-l:)
ETHproductions'Jul

谢谢@KevinCruijssen,很抱歉,我现在无法编辑,请您编辑吗?
Xcoder先生17年

@KevinCruijssen非常感谢!
Xcoder先生17年

3
下到95个字节的使用center,而不是for循环!
notjagan

4

PHP,145131字节

花了一些心思去打高尔夫球。但它是值得的。

while($y<=2*$e=strlen($a=$argn)-1)echo($p=str_pad)("
",$e*($e-$d=abs($y++-$e))+1),chunk_split($a.substr(strrev($a),1),1,$p("",$d));

打印领先的换行符。与管道一起运行-nR在线尝试

分解

while($y<=2*$e=strlen($a=$argn)-1)  # $e=length-1, loop through rows
                                        # 1. print linebreak and left padding
    echo($p=str_pad)("\n",$e*($e-$d=abs($y++-$e))+1),
        chunk_split(
            $a.substr(strrev($a),1)     # 2. palindromize input
            ,1,$p("",$d));              # 3. insert $e..0..$e spaces between characters

相同长度的替代解决方案

for($d=-$e=strlen($a=$argn)-1;$d<$e;)echo($p=str_pad)("
",$e*($e-$b=abs($d++))+1),chunk_split($a.substr(strrev($a),1),1,$p("",$b));


3

APL(Dyalog),37字节

要求⎕IO←0在许多系统上是默认设置。

{⍉m(2-d)↓⍉(m←⊖⍪1↓⊢)↑∊¨(1↓⍳d←≢⍵)↑¨¨⊂⍵}

{} 匿名函数,其中的参数由represented表示

()↑¨¨⊂⍵ 每个¨以下数字中的每个(¨),请使用()整个()参数的每个()中的许多字符,并在必要时添加空格:

  ≢w 参数中的字符数

  d← 将其存储在d中

   许多ɩ ndices(0 ... d − 1)

  1↓ 减一(零)

∊¨ϵ nlist(展平)每个

 提高排名(将列表列表转换为矩阵)

(m←) 应用以下隐式函数m,定义为:

   倒置论证

   在之上

  1 一[行]

   从

   论点

 转置

(…… )↓ 掉落:

  2-d = −(d − 2),即从底部开始的d − 2行

m 申请

 转置

在线尝试!


3

的Java(OpenJDK的8) 201个 196字节

s->{for(int l=s.length()-1,i=-l,x=0;++i<l;x+=i<0?l:-l)System.out.printf("%1$"+(x<1?"":x)+"s"+s.join("%1$"+(i<0?-i:i>0?i:"")+"s",(new StringBuffer(s.substring(1)).reverse()+s).split(""))+"%n","");}

在线尝试!

它与我上一个挑战所用的想法相同,只是生成器字符串现在更长了一点,并且处理案件更加困难。

%1$Ns0%1$Ns9%1$Ns8%1$Ns7%1$Ns6%1$Ns5%1$Ns4%1$Ns3%1$Ns2%1$Ns1%1$Ns2%1$Ns3%1$Ns4%1$Ns5%1$Ns6%1$Ns7%1$Ns8%1$Ns9%1$Ns0%n

3

Python 3中134个 124字节

f=lambda s:'\n'.join([' '*(len(s)-1)*abs(len(s)-abs(i)-2)+(' '*abs(i)).join(s[::-1]+s[1:]) for i in range(2-len(s),len(s)-1)])

在线尝试!

潜伏了一段时间后首次发布给PPCG。寻找任何建议/建议!


感谢@LyricLy和@ŁukaszRogalski的改进!


1
您不必在函数内部打印输出,可以在返回值中给出输出。另外,您不必f=在字节数中计数,只需提供匿名函数即可。
LyricLy

1
另外,-len(s)+2就是2-len(s),少一个字节。
卢卡斯Rogalski

谢谢@ŁukaszRogalski和@LyricLy; 您已经为我节省了10个字节!
Chase Vogeli '17

3

Haskell,177163字节

import Data.List
k n=[1..n]>>" "
f s=let n=length s in map(\x->(k(((n-1)*(n-(abs x)))))++(intercalate (k(abs x))$map(\z->[z])$((++)=<<reverse.tail) s))[n,n-1.. -n]

功能 f是质询函数,它返回一个字符串列表([String]),unlines在其上使用时应提供与测试用例相同的视觉输出(main = putStr $ unlines $ f "test string"以进行编译)。

在线尝试!

-14字节感谢@nimi


1
k可以定义为k n=[1..n]>>" ",并pp=(++)=<<reverse.tail(你甚至可以内联这个定义)。
nimi

2

Mathematica,141个字节

Column[Join[Reverse@(s=Row/@Table[Riffle[Reverse@Rest@b~Join~b,""<>Table[" ",i]],{i,0,Length[b=Characters@#]-1}]),Rest@s],Alignment->Center]&
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.