我忘了毛巾日


15

我忘了毛巾日

沙盒链接

两个示例的PHP脚本(错误,格式化字符串)

更正了PHP脚本,输入内容为自解释

PHP脚本具有更多输入注释/注释$argv,可查看不同结果

如您所知,5月25日被称为“ 毛巾日”,因为毛巾有很多用途。

摘自《银河系旅行者指南》一书的简单摘录(我个人认为这本书是《日常指南》),其中指出:

“毛巾是星际旅行者可以拥有的最有用的东西。部分它具有很大的实用价值。”

如果您需要有关毛巾的更多信息,请检查此SE.scifi答案


挑战

希望您能用一条毛巾作为腿毛做毛巾。

用任何需要两次输入的有效编程语言编写完整的程序或函数

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
     """"""""""""""""""""""""   #  

接受的答案标准

  • 这是代码高尔夫,因此适用常规规则。

规则

  • 您可以确保所有输入字符串都适合大小,因此不能输入size=1; message="This string is just tooooooooooooooooooooo long to fit the width and height of your towel"

  • 字符串格式取决于您,例如,如果您想将子字符串居中。

  • 禁止使用断字

编辑

对于任何混淆,我深表歉意,因为我绘制的ansii毛巾与参数不匹配,为两个示例都添加了PHP脚本,供您检查预期的输出。

也感谢所有投票并考虑了我的第一个挑战的人:D。


给我几分钟时间修复它(刚刚看到了)。发生的事情是我在php上编写了一个程序来画毛巾,但它比我介绍的程序要简单得多,我有一个用于示例的程序,但我忘记对其进行修改。
弗朗西斯科·哈恩

@Lynn在两个测试用例中都添加了php ungolfed解决方案,对于任何混淆,我们深表歉意。
弗朗西斯科·哈恩

我们可以选择换行的地方吗?
Jakob

当行数current line + next word超过我的毛巾@Jakob所允许的限制时,应添加换行符,这是我发布的PHP脚本所决定的。
Francisco Hahn

我们可以采用适当的文本宽度和/或高度作为参数而不是计算它们吗?
JoshM

Answers:


4

Python 2中223个 210 209 204字节

def f(s,m):w=12*s-2;l='    |';print'[===|'+' '*w+'|====]';exec"j=(m+' ').rfind(' ',0,-~w);print l+'%*s||'%(w,m[:j]);m=m[j+1:];"*s*5;print(l+w*'='+'||\n')*2+' '*4+'"'*12*s+'|\n '+l+' '*w+'|\n    ','"'*12*s

在线尝试!


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)347345343337337334328326字节

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ι⁻δ² ι

打印每个单词而不会溢出宽度。


太好了,感谢您抽出宝贵的时间来应对挑战。
弗朗西斯科·哈恩

1

V142128121,119个字节

:let @h=5*@a
:let @w=12*@a-2
O[===|@wá 4á=á]
:set tw=w
Vgq@hï{+@hjI    |kf|@hjjyt=pjp+@hjkl@wr=Ùjlr|Ù@w««r"YkP0xÁ|

在线尝试!


1
我想说的是这种代码“嘿,如果this您可以保存n Bytes”,但是我还不能... D:D。
弗朗西斯科·哈恩
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.