取消缩音


13

Pyth也许是最成功的通用高尔夫球语言。尽管由于更新的语言而有所下降,但从2014年到2016年,Pyth简洁的语法,不断的更新,重载以及(对于其时代而言)许多内置函数使它成为大多数问题的最爱。

Pyth代码通常很难阅读。甚至调试模式(已编译的Python)的输出通常也由一长行组成,有时会在括号内嵌套十个深处。但是,正确格式化的Pyth可读性强。

这是一段由@isaacg在Play the Word Chain中编写的Pyth代码。

.MlZfqhMtTeMPT+Lzs.pMyQ

这样更具可读性。

.M                     Filter by gives-maximal-value of
   l Z                   lambda Z:length(Z) over
   f                     filter by (lambda T:
     q                     equal
       hM t T                head-map tail T
       eM P T                end-map Pop T)
     +L                    Append z to each element in
        z                        
        s .pM y Q            flattened permutations of each subset of Q

对于此挑战,我们消除了对Pyth字符进行分类的方面,而专注于格式设置。输入将不是Pyth代码,而是由中的字符组成0123456789M。该数字n表示arity的功能n,并且M表示一个运算符。例如,上面的代码表示为210221M101M102M011M10。以下是取消最小化的步骤:

将字符串分成令牌。

令牌匹配[0-9]M*0M在输入中不会发生。

添加尾随0。

当参数不足时,Pyth Q会在代码中追加必要的隐式变量(lambda变量或s),以填充程序的参数。这些应以0s 表示。

将令牌分组为行。

令牌的Arity是其数字的值。

  • arity-0令牌(即0)结束一行。

  • 对于arity-1令牌,下一个令牌应在同一行上,并用空格隔开。

  • 对于Arity> = 2令牌,其参数以它们在代码中出现的顺序排在单独的行上,每个参数后跟自己的子参数,依此类推。令牌的参数缩进到该令牌的末尾加上一个空格。

输入值

由组成的非空字符串(或char数组,length-1字符串数组等,由标准I / O方法允许)0123456789M,其中将不包含substring 0M

输出量

根据上述规则格式化的字符串。

测试用例

210221M101M102M011M10

2
  1 0
  2
    2
      1M 1 0
      1M 1 0
    2M
       0
       1 1M 1 0


123M4M

1 2
    3M
       4M
          0
          0
          0
          0
       0
       0
    0


2MM

2MM
    0
    0


11011100

1 1 0
1 1 1 0
0


9000000

9
  0
  0
  0
  0
  0
  0
  0
  0
  0


我可以将输入作为数字/字符串数组吗?例如210221M101M102M011M10将是[2,1,0,2,2,1,'M',1,0,1,'M',1,0,2,'M',0,1,1,'M',1,0]
路易斯·费利佩·穆尼奥斯耶稣

@LuisfelipeDejesusMunoz否,除非标准I / O规则要求允许您这样做(我不认为这样做)。如果M允许s是不同于整数的数据类型,则IMO将稍微改变一下挑战。
lirtosiast

@lirtosiast因此,一个字符/单字符字符串数组很好,只是不要在数字和M?之间使用不同的数据类型。
卡米尔·德拉卡里

1
@LeakyNun空字符串现在是未定义的行为。
lirtosiast

Answers:


1

的JavaScript(ES8),160个 159字节

f=(s,a=[d=0,p=[1]])=>s.replace(/(.)M*/g,(s,c)=>(g=_=>a[d]?s+(P=p[d]-=c--&&~s.length,c?`
`.padEnd(P):' '):g(d--))(a[d]--,a[++d]=+c,p[d]=p[d-1]))+(d?f('0',a):'')

在线尝试!

已评论

f = (                          // f = recursive function taking:
  s,                           //   s   = input string
  a = [                        //   a[] = array holding the number of expected arguments
    d = 0,                     //   d   = current depth, initialized to 0
    p = [1]                    //   p[] = array holding the padding values
  ]                            //
) =>                           //
  s.replace(                   // search in s all substrings
    RegExp('(.)M*', 'g'),      // consisting of a digit followed by 0 to N 'M' characters
    (s, c) =>                  // for each substring s beginning with the digit c:
      ( g = _ =>               //   g = recursive function
          a[d] ?               //     if we're still expecting at least one argument at
                               //     this depth:
            s + (              //       append s
              P = p[d] -=      //       update the padding value P = p[d] for this depth:
                c-- &&         //         decrement c; unless c was equal to 0,
                ~s.length,     //         add the length of s + 1 to p[d]
              c ?              //       if c is not equal to 0 (i.e. was not equal to 1):
                `\n`.padEnd(P) //         append a linefeed followed by P - 1 spaces
              :                //       else:
                ' '            //         append a single space
            )                  //
          :                    //     else (all arguments have been processed):
            g(d--)             //       decrement the depth and call g again
      )(                       //   before the initial call to g:
        a[d]--,                //     decrement the number of arguments at depth d
        a[++d] = +c,           //     set the number of arguments for the next depth
        p[d] = p[d - 1]        //     set the padding value for the next depth,
      )                        //     using a copy of the previous depth
  ) + (                        // end of replace()
    d ?                        // if we're not back at depth 0:
      f('0', a)                //   do a recursive call to f with an extra '0'
    :                          // else:
      ''                       //   stop recursion
  )                            //

1

Haskell中192个 190 187字节

unlines.snd.f
f(n:r)|(m,t)<-span(>'9')r,(s,l)<-n#t=(s,n?((n:m):map((' '<$(n:n:m))++)l))
f e=(e,["0"])
'1'?(a:b:r)=(a++drop(length a)b):r
_?s=s
'0'#s=(s,[])
n#s|(r,l)<-f s=(l++)<$>pred n#r

在线尝试!

必须要有一种更好的方法来处理arity-1情况,它目前占用45个字节。

编辑:

  • 通过切换到另一种处理1的方法来获得-2字节,尽管先前的方法可能具有更大的优化潜力。
  • 不将字符数字转换为数字并使用pred代替-3个字节n-1
unlines.snd.f
f(n:r)|(m,t)<-span(>'9')r,(s,l)<-read[n]#t,w<-map((' '<$(n:n:m))++)=(s,last$((n:m):w l):[(n:m++' ':h):w t|n<'2',h:t<-[l]])
f e=(e,["0"])
0#s=(s,[])
n#s|(r,l)<-f s=(l++)<$>(n-1)#r

在线尝试!


1

木炭,75字节

FS⊞υ⎇⁼ιM⁺⊟υιι≔⮌υυ≔⟦⟧θ≔⟦⟧ηW∨υη«≔⎇υ⊟υ0ιι¿⊖Σι↘→⊞θι⊞ηΣιW∧η¬§η±¹«⊟ηM⊕L⊟θ←¿η⊞η⊖⊟η

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

FS⊞υ⎇⁼ιM⁺⊟υιι

循环输入字符,并将它们转换为带有可选M后缀的数字列表。

≔⮌υυ

反转此列表,以便我们可以使用Pop它。

≔⟦⟧θ

此变量是令牌的堆栈,其Arity尚未实现。

≔⟦⟧η

此变量是未完成令牌的剩余arity的堆栈。

W∨υη«

重复直到我们消耗完所有令牌并清空堆栈。

     ≔⎇υ⊟υ0ι

获取下一个令牌,0如果没有。

     ι¿⊖Σι↘→

打印令牌,然后水平移动光标(如果它1以对角线开头)。

     ⊞θι⊞ηΣι

将令牌及其Arity添加到适当的变量中。

     W∧η¬§η±¹«

当arity堆栈为非空但最高arity为零时,重复上述步骤。

              ⊟η

丢弃零Arity。

              M⊕L⊟θ←

删除其令牌并向左移动许多字符。

              ¿η⊞η⊖⊟η

如果还有残料,则减少最高残料。

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.