タオルの日を忘れた


15

タオルの日を忘れた

サンドボックスリンク

両方の例のPHPスクリプト(エラー、文字列のフォーマット)

修正されたphpスクリプト、入力は自明です

$argv異なる結果を表示するための入力コメント/コメント解除の多いPHPスクリプト

ご存知かもしれませんが、5月25日はタオルの日としてよく知られています。これは、タオルの用途が多いためです。

「ギャラクシーへのヒッチハイカーのガイド」の簡単な抜粋(個人的に私はこの本を「毎日のガイド」として取りました):

「タオルは、星間ヒッチハイカーが持つことができる最も有用なものに関するものです。部分的には、実用的な価値が非常に高いです。」

タオルに関する詳細情報が必要な場合は、このSE.scifiの回答を確認してください


チャレンジ

足の毛布として使用するタオルでこれを行うことを願っています。

2つの入力を受け取る有効なプログラミング言語で完全なプログラムまたは関数を記述します

size        Integer  : #The size
message     string   : #A sentence

これらの値でタオルを描く方法は?

最初の使用size

入力したサイズに応じてタオルを描きます。タオルの

  width = 12*size

  #the line that has the "bar" its always present with different sizes

  Width is defined as:
  from the first | to the second | has to be equal to size*12 including both ||

      <-   width-> 
  [===|          |====]  
      | 12 chars ||   1  height starts here, just after the "bar"
      | SIZE = 1 ||   2
      |          ||   3   height = 5*size
      |          ||   4
      |          ||   5  height ends here just before the first line |=======|
      |==========||   # the lines with the # 
      |==========||   # are always fixed
      """"""""""""|   # it means, every towel
       |          |   # always has this 5 rows
       """"""""""""   # no matter the size

第二に、それを与える message

あなたはそれにメッセージを与える必要があります、黄金の糸で縫われた美しいメッセージのないタオルは何ですか?

例1

  input: size=1, message="a simple message can stay with size"

  width = 12*size

  #the line that has the "bar" it's always present with different sizes
  #no words allowed at the bar level


  [===|          |====]  
      | a simple ||   1  height starts here, just after the "bar"
      | message  ||   2
      | can stay ||   3   height = 5*size
      | with size||   4
      |          ||   5  height ends here just before the first line |=======|
      |==========||   # the lines with the # 
      |==========||   # are always fixed
      """"""""""""|   # it means, every towel
       |          |   # always has this 5 rows
       """"""""""""   # no matter the size

例2

input size=2 
message="Don't Panic and bring a towel to SE Programming Puzzles and CodeGolf"

The size is 2
That means 24 width and 10 heigth

    <-    24 chars width  ->
[===|                      |====]
    | Don't Panic and bring||   1
    | a towel to SE        ||   2
    | Programming Puzzles  ||   3
    | and CodeGolf         ||   4
    |                      ||   5
    |                      ||   6
    |                      ||   7
    |                      ||   8
    |                      ||   9
    |                      ||   10
    |======================||   #  The lines with the "#"
    |======================||   #  always present and
    """"""""""""""""""""""""|   #  adapted to 
     |                      |   #  the towel width
     """"""""""""""""""""""""   #  

受け入れられた回答基準

  • これはcodegolfなので、通常の規則が適用されます。

ルール

  • すべての入力文字列がサイズに適合することが保証されているため、などの入力はありませんsize=1; message="This string is just tooooooooooooooooooooo long to fit the width and height of your towel"

  • たとえば、部分文字列を中央に配置する場合、文字列の形式はユーザー次第です。

  • 単語の区切りは許可されていません。

編集

私が描いたansiiタオルがパラメーターと一致せず、予想される出力を確認するために両方の例にPHPスクリプトを追加したため、混乱について本当に申し訳ありません。

また、投票して私の最初の挑戦を検討したすべての人々に感謝します:D。


それを修正するために数分待ってください(これを見ただけです)起こったことは、タオルを描くためにphpでプログラムを作成しましたが、提示したものよりもはるかに簡単で、例のプログラムがあり、それを変更するのを忘れていました。
フランシスコハーン

@Lynnは、両方のテストケースにPHPの未解決のソリューションを追加しました。混乱をおかけして申し訳ありません。
フランシスコハーン

改行位置を選択できますか?
ヤコブ

current line + next word私が投稿したPHPスクリプトが決定するように、タオルが@Jakobに許可されている制限を超える場合、改行を追加する必要があります。
フランシスコハーン

適切なテキストの幅や高さを計算する代わりに引数として取ることができますか?
JoshM

回答:



2

JavaScript(Node.js)、375バイト

これはあなたが得る最悪の提出ですが、少なくともxDの半分のバイトはスペースでしたが

(s,_,w=12*s-2,h=5*s)=>`
[===|${j=' '.repeat(w)}|====]
${[...Array(h)].map((g,i)=>_.split` `.reduce((a,b)=>((l=a.split`,`)[l.length-1]+b).length>w-1?a+','+b:a+' '+b,'').split`,`[i]).map(a=>`    |${a?(r=a.length)<w?a+' '.repeat(w-r):a:j}||    `).join`\n`+
`
    |${'='.repeat(w)}||    `.repeat(2)}
    ${y='"'.repeat(w+2)}|    
     |${j}|    
     ${y}
`

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


2

JavaScript(Node.js)347 345 343 337 334 328 328バイト

b=>d=>`
[===|${M=" "[X="repeat"](c=12*b-2)}|====]
${[...Array(5*b)].map((a,b)=>d[Y="split"]` `.reduce((a,b)=>((l=a[Y]`,`)[l[Z="length"]-1]+b)[Z]>c-1?a+","+b:a+" "+b)[Y]`,`[b]).map(a=>`    |${a?(r=a[Z])<c?a+" "[X](c-r):a:" "[X](c)}||    `).join`
`+`
    |${"="[X](c)}||    `[X](2)}
    ${y='"'[X](c+2)}|
     |${M}|
     ${y}
`

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


説明 :

b =>                                   // lambda function taking arg 1 : size
    d =>                               // arg 2 : message
        `                              // begin string template for drawing
[===|${                                // draw first part and now open literal 
        M = ' '[X='repeat'(c=12*b-2)]  // set 3 variables, M, X , c to be used again
    }|====]                            // close and draw the next part ====]
${[...Array(5*b)]                      // open and create an array of length 5 * b =width
.map(a,b=>                             // begin map function two args : a,b
        d[Y='split']` `                // use d(message), split at whitespace, set to Y
.reduce((a,b) =>                       // reduce to single value, arg 1 : a, arg 2 : b
        ((l = a[Y]`,`)                 // declare l and then find in l
        [l[Z='length']-1]              // set Z as length
        + b)                           // add value of b
        [Z]                            // find the length 
        > c-1 ?                        // check if it's less than c - 1
        a+','+b                        // then add `${a},${b}`
        : a + ' ' + b                  // otherwise `${a} ${b}`
        )[Y]`,`                        // close and split at comma
        [b]                            // use b again
        )                              // close
        .map(a =>                      // map over that arg 1 : a
                `    |${               // four space + | and open 
        a ?                            // if a is true or a truthy value
            (r=a[Z])                   // set value of r as a's length
            < c ?                      // check if it's less than c
                a+' '[X](c-r)          // then draw a + space repeated c-r times
                : a + ' '[X](c)        // else draw a + space repeated c times
            }                          // close
            ||    `                    // add || and 4 spaces and close
        )                              // end
        .join`
`                                      // and turn to string with new line as separator 
    +                                  // add to that
    `
    |{                                 // new line , 4 spaces and | 
        '='[X](c)}                     // repeat = c times
        ||    `[X](2)}                 // and repeat that 2 times
    ${                                 // new line + 4 space
    y = '"'[X](c+2)                    // repeat " c + 2 times and set to y
    }|                                 // close and add |
    |{                                 // add | and open
        M}|                            // put M and close and add |
    {y}                                // new line , 4 spaces and variable y
`                                      // end with new line.

2

、79バイト

Nθ≔×⁵θε≔×¹²θδ←P←====[↓⁺³ε×"δ↖P↑⁺³ε←G↑²←⁻δ²↓²=↓↓↓¹×"δ↖↑⁺⁴ε====]F⪪S «×⸿›⁺ⅈLι⁻δ² ι

オンラインでお試しください!リンクは、コードの詳細バージョンです。説明:

Nθ≔×⁵θε≔×¹²θδ

タオルのサイズを計算します。

←P←====[↓⁺³ε×"δ↖P↑⁺³ε←G↑²←⁻δ²↓²=↓↓↓¹×"δ↖↑⁺⁴ε====]

タオルを描きます。

F⪪S «

メッセージの各単語をループします。

×⸿›⁺ⅈLι⁻δ² ι

幅をオーバーフローさせずに各単語を印刷します。


素晴らしい、そして挑戦に時間を割いてくれてありがとう。
フランシスコハーン

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