単語を文字で囲みます


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

受賞基準:

各言語のバイト単位の最短コードが優先されます。コードとアプローチをコメント/説明していただければ幸いです。


少なくとも1つの単語があると仮定できますか?
PurkkaKoodari

@ Pietu1998はい、常に少なくとも1つの単語があります
ガレンイワノフ

1
@Kevin Cruijssen Transpose?
ガレンイワノフ

回答:


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θ«

文字ごとに1回ループします。

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

適切な高さ、幅、文字のボックスを描画します。

↘»

次のボックスの左上隅に移動します。

M⊕⊗θ→

次のアウトラインに移動します。



3

Pyth、34 33バイト

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

オンラインでお試しください。

余分な空白をすべて削除しますが、それはチャレンジによって許可されています。

説明

  • m… 入力内のQ各単語に対して以下を実行dしますQ

    • m\ dは、単語をx => " "にマップします。基本的に[" ", ..., " "]は、単語に含まれる文字と同じ数のアイテムを含むリストを作成します。
    • .idd単語自体をインターリーブし、単語の文字を2回繰り返します。_この文字列を反転します。wordになりddrroowwます。
    • uG=のスペースの配列で始まり、のインターリーブされた文字列の各文字に以下を適用しますH
      • *2H 文字を2回繰り返します。
      • jRG各文字列を文字Gのペアの間に配置します。
      • C行と列を交換します。これらの3つのステップがで同じ文字で2回実行されると、その文字で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コミュニティWikiなので、自由に編集してください:
ジュゼッペ



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)
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.