用字母概述单词


14

出于当前挑战的目的,“概述”一个单词的意思是先用最后一个字母依次包围它自己的字母,最后用空格替换中心的原始单词:

       oooooo 
       onnnno 
on ->  on  no 
       onnnno
       oooooo

任务:

给定一个仅由小写和/或大写英文字母组成的单词列表,勾勒出每个单词的轮廓,并水平相邻显示所有生成的块,并用一列单空格隔开,在块的中心垂直对齐。

您可以编写完整的程序或函数。

输入:

单词列表,或者如果您愿意-用空格或其他符号分隔的字符串

输出:

轮廓词块的ASCII表示形式。允许前导/尾随空格。

测试用例:

Input 1: ["code", "golf"] (or "code golf")
Output 1:

    cccccccccccc gggggggggggg
    cooooooooooc goooooooooog
    coddddddddoc gollllllllog
    codeeeeeedoc golfffffflog
    code    edoc golf    flog
    codeeeeeedoc golfffffflog
    coddddddddoc gollllllllog
    cooooooooooc goooooooooog
    cccccccccccc gggggggggggg

Input 2: ["I", "am", "just", "a", "man"]  (or "I am just a man")
Output 2: 

           jjjjjjjjjjjj
           juuuuuuuuuuj     mmmmmmmmm
    aaaaaa jussssssssuj     maaaaaaam
III ammmma justtttttsuj aaa mannnnnam
I I am  ma just    tsuj a a man   nam  
III ammmma justtttttsuj aaa mannnnnam
    aaaaaa jussssssssuj     maaaaaaam 
           juuuuuuuuuuj     mmmmmmmmm 
           jjjjjjjjjjjj

获奖标准:

每种语言中以字节为单位的最短代码获胜。如果您评论/解释您的代码和方法,我将不胜感激。


我们可以假设至少有一个词吗?
PurkkaKoodari '18

@ Pietu1998是的,总是至少有一个字
Galen Ivanov

1
@Kevin Cruijssen转置?
Galen Ivanov

Answers:


7

画布22 20 字节

l *;±21*{;l└*e⟳} ]r⤢

在这里尝试!

说明:

{                 ]    map over the inputs
 l *                     array of length spaces - the canvas of the current word
    ;                    get the word back on top
     ±                   reverse it
      21*                repeat each character twice
         {      }        for each character
          ;l└              push the height of the item below (the canvas)
             *             repeat the character that many times vertically
              e            and encase the canvas in that char column
               ⟳           and rotate it clockwise for encasing the next time
                 ∙      push another space as the separator of words
                   r   center the words
                    ⤢  and transpose the final output (as everything was built vertically)

5

木炭,35字节

FA«≔LιθMθ↑Fθ«B⁻׳θ⊗κ⊕⊗⁻θκ§ικ↘»M⊕⊗θ→

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

FA«

循环输入列表。

≔Lιθ

获取当前单词的长度。

Mθ↑

移动到结果轮廓的左上角。

Fθ«

每个字符循环一次。

B⁻׳θ⊗κ⊕⊗⁻θκ§ικ

画一个合适的高度,宽度和字符的框。

↘»

移动到下一个框的左上角。

M⊕⊗θ→

移至下一个大纲。


4

哈斯克尔188 183 174 171 167字节

-9 -13字节感谢Laikoni

e=[]:e
c#a=k c<$>k(c<$a!!0)a
k c s=c:s++[c]
f w=foldr(#)[p w]w
p=(' '<$)
s w=unlines.map unwords.foldr(zipWith(:))e$until(\a->all((p a>=).p)$f<$>w)(k=<<p.head)<$>f<$>w

在线尝试!


\a->and[p a>=p x|x<-f<$>w]可以\a->all((p a>=).p)$f<$>w并且k c=(++[c]).(c:)可以k c s=c:s++[c]
Laikoni '18

3

Pyth,34 33字节

Jsm+;uCjR*2HG_.iddm\ dQjCm.[lJd;J

在线尝试。

转储一堆额外的空白,但这是挑战所允许的。

说明

  • m… 对输入中的Q每个单词执行以下操作:dQ

    • m\ d用映射单词x => " ",本质上创建的列表[" ", ..., " "]与单词包含字母的数量一样多。
    • .idd将单词与自身交织在一起,重复单词的字母两次。_反转此字符串。word成为ddrrooww
    • uG=空格数组开头,并对以下交错字符串中的每个字母应用以下内容H
      • *2H 重复字符两次。
      • jRG将每个字符串放在字符对G之间。
      • C交换行和列。当使用中的相同字符完成这三个步骤两次时,将H概述该字符中的行G
    • 现在,我们有了概述单词的列d+;前面加了一个空格列。
  • s展平每个单词的列数组,并将其J保存在变量中J
  • mJ对输出的每一列执行以下操作:
    • .[lJd;在列的两边填充空格,以使列的长度等于列数。这始终足以使列垂直对齐。
  • C将列转换为行,j并将行与换行符连接。

替代解决方案,33字节

j.tsm.[L\ l+dsQ+;uCjR*2HG_.iddm\ 

在线尝试。

请注意有一个尾随空格。除了只填充顶部的列,然后以空格填充进行转置外,其他大部分算法都是相同的。


3

R,189字节

function(x,S=32,`+`=rbind,`*`=cbind)cat(intToUtf8(Reduce(`+`,Map(function(s,K=utf8ToInt(s),o=S-!K){for(i in rev(K))o=i+i*o*i+i
for(j in(0:(max(nchar(x))-nchar(s)))[-1])o=S*o*S
o+S},x))+10))

在线尝试!

digEmAll和我在聊天中的合作

function(x){
 S <- 32			# space
 `+` <- rbind			# alias for rbind
 `*` <- cbind			# alias for cbind
 outlineWord <- function(s){	# function to construct the outline for each word
  chars <- utf8ToInt(s)		# convert to code points
  output <- S - !chars		# replace each char with 32 (space)
  for(i in rev(chars))
   o <- i + i * o * i + i	# o <- rbind(i,cbind(i,o,i),i)
  for(j in(0:(max(nchar(x))-nchar(s)))[-1])
   o <- S * o * S		# pad with spaces
   o + S}			# return with an additional row of spaces between words
 outlines <- Map(outlineWord,x)	# apply outlineWord to each element of x
 outlines <- Reduce(`+`,outlines)# reduce by rbind
 outlines <- outlines+10	# add row of newlines
 cat(intToUtf8(outlines))	# convert back to strings and print
}

187,带有明显的别名
J.Doe

@ J.Doe它的社区维基可以随意编辑在:-)
朱塞佩·



1

05AB1E,46个字节

εg©;ò<Uyη央∍«®>∍}y𫩪®Xиª˜».º.∊}¶«».C.B€SζJ»

对此不太满意,但我很高兴它正在工作。

在线尝试验证所有测试用例

说明:

ε                             # Map `y` over the (implicit) input-list
 g                            #  Take the length of the current item
  ©                           #  Store it in the register (without popping)
   ;                          #  Halve it
    ò                         #  Ceil and cast to integer at the same time
     <                        #  Decrease it by 1
      U                       #  Pop and store it in variable `X`
 yη                           #  Take the prefixes of the current string `y`
   ε       }                  #  Map over these prefixes:
    ¤                         #   Take the last character of the string
     ®×                       #   Increase it to a size equal to the length from the register
       «                      #   Append it to the current prefix
        ®>                    #   Take the length from the register, and add 1
                             #   Shorten the string to that size
 y                            #  Push the string `y` again
  ð«                          #  Append a space
    ©                         #  Store it in the register (without popping)
     ª                        #  Append it at the end of the list of modified prefixes
      ®                       #  Push the string with space from the register again
       Xи                     #  Repeat it `X` amount of times
         ª                    #  Append them to the list
          ˜                   #  Flatten to remove the empty appended list if `X` was 0
           »                  #  Join by newlines
            .º.∊              #  Intersect mirror both horizontally and vertically
                }             # Close outer map
                 ¶«           # Append a newline after each (for the space delimiters)
                   »          # Join everything by newlines
                    .C        # Centralize it horizontally
                              # (too bad a centralize vertically isn't available..)
                      .B      # Split on newlines again
                        S    # Convert each line to a list of characters
                          ζ   # Zip, swapping rows/columns (with space filler by default)
                           J  # Join the loose characters of every line to a string again
                            » # Join the lines by newlines (and output implicitly)
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.