代码说明格式化程序


32

从本质上讲,成功提交高尔夫球规则的代码到处都是疯狂的符号。为了使提交的内容易于理解,许多代码高尔夫球手选择对代码进行解释。在他们的解释中,代码行变成了垂直分解图。

例如,如果这是我的代码:

1_'[3:~2@+]`

我可以创建的许多可能的图之一如下所示:

1           
 _'         
   [      ] 
   [3:    ] 
   [  ~   ] 
   [   2@ ] 
   [     +] 
           `

目标

在此挑战中,您将编写一个说明自动格式化工具,该工具采用一行代码并创建一个可轻松添加说明文字的图表。

为了使此挑战更为有用,用户将能够通过提供格式字符串来指定每行的内容。格式化字符串将是第二行,仅包含字母A-Za-z,与程序长度相同。字母表示在解释中应打印程序字符的顺序。

这是一个没有任何类似于括号的格式的I / O示例:

123423
AabcBC

1     
    2 
     3
 2    
  3   
   4  

括号

如果程序中有多个字符具有相同的优先级,则该字符集将充当单个代码块(如果它们构成一个组)或一组括号(如果它们之间包含其他字符)。一般规则很简单:

  1. 直到更高优先级的所有其他字符都已经出现在图表上方的行上,字符才会出现在图表的一行中。

  2. 优先级相同的字符始终打印在同一行上。如果某个字符出现在一行上,则所有其他优先级相同的字符都出现在该行上。

  3. 一组具有相同优先级的字符将继续出现在每一行上,直到其所包围的所有其他字符都至少出现过一次为止。这允许“类似托架”的构造。如果bceab是优先级,则b字符将出现在第二行(它们是第二高的优先级),并将继续出现,直到所有cea字符都出现为止。如果优先级字符串为abcadeafga,则所有优先级字符串bcdefg都视为包含在其中,则所有4 as将继续出现,直到出现为止g

更多格式要求

输出的所有行应具有相同的长度(输入行的长度),并在必要时用空格填充。输入程序行可能包含空格,尽管这些空格也将被赋予优先级字母。输出/输入上的尾随换行符是可选的。

计分

这是代码高尔夫,最少的字节数获胜。


例子

这是一段带有复杂格式的代码的注释示例。

1_'[3:~2@+]`
abbcddeffgch

1            #highest priority is denoted by the lowercase letter a
 _'          #priority b
   [      ]  #all characters with priority c
   [3:    ]  #priority d, but priority c still printed because it encloses more
   [  ~   ]  #priority e
   [   2@ ]  #priority f
   [     +]  #priority g, last line of c because all enclosed characters have appeared
           ` #priority h

Perl中的一个示例:

$_=<>;s/[^aeiou\W]/$&o$&/gi;print
aaaaaabbccccccccccbdddddbbbbeeeee

$_=<>;                           
      s/          /     /gi;     
      s/[^aeiou\W]/     /gi;     
      s/          /$&o$&/gi;     
                            print

以下是MartinBüttner提供的CJam中的一些示例:

l~2*{_2%{3*)}{2/}?_p_(}g;
aabbcdddefffeeggeehhiiccj

l~                       
  2*                     
    {                 }g 
    {_2%              }g 
    {   {   }{  }?    }g 
    {   {3*)}{  }?    }g 
    {   {   }{2/}?    }g 
    {             _p  }g 
    {               _(}g 
                        ;

q{_eu'[,66>"EIOU"-#)g{'o1$}*}/
abcccddddddeeeeeeefgghiijjhhbb

q                             
 {                          }/
 {_eu                       }/
 {   '[,66>                 }/
 {         "EIOU"-          }/
 {                #         }/
 {                 )g       }/
 {                   {    }*}/
 {                   {'o  }*}/
 {                   {  1$}*}/

这是一个疯狂的例子,只是惹你生气:

1_'[3:~2@+]`
azTABACBDCAT

   [ :    ] 
   [3: 2  ] 
   [3:~2 +] 
   [ :~ @+] 
  '        `
1           
 _          

这是当括号重叠时会发生什么的更明确的示例abab。(通常,这不是您选择格式化说明的方式。)

aabbccddaaeebb
aabbccddaaeebb

aa      aa    
aabb    aa  bb
aabbcc  aa  bb
aabb  ddaa  bb
  bb      eebb #"aa" no longer appears because all of "bbccdd" have already appeared.

Answers:


14

Pyth,33 40字节

JwFHS{Js.e?@zk&gHYsm&gdH}d>_>JxJYx_JYJdJ

在线尝试:Pyth编译器/执行器

说明:

用字符串生成aabbbbbzccdeeegfffqhjiiikkpnmmllloooohec

                                          implicit: z = first input line
Jw                                        J = second input line
  FHS{J                                   for H in sorted(set(J)):
        .e                             J    map each k,Y of enumerate(J) to:
        .e?                            J      .... if ... else ...
        .e @zk                        dJ      z[k] if ... else " "
                                              condition: 
        .e @zk gHY                    dJ        H >= Y
        .e @zk&                       dJ        and
        .e @zk     m                 JdJ        map each d of J to:
        .e @zk     m gdH             JdJ          d >= H
        .e @zk     m&                JdJ          and
        .e @zk     m    }d           JdJ          d in ...
        .e @zk     m          xJY    JdJ          index of Y in J
        .e @zk     m        >J       JdJ          substring of J (from index to end)
        .e @zk     m       _         JdJ          reverse substring
        .e @zk     m             x_JYJdJ          index of Y in reversed J
        .e @zk     m      >          JdJ          substring of reversed (from index to end)
        .e @zk    s                   dJ       sum up the booleans (acts as any)
       s                                    sum up the chars and print

因此,第一条输入线是z,第二条输入线是J

循环J以排序顺序遍历所有字符,且没有重复。当前的char称为H

然后,每个YJ我打印的记者焦z或空白,这取决于如果同时满足下列条件:

  • Y <= H(第一个字符出现在该行中H
  • 有一个char d >= H,它出现在以Y(括号)开头和结尾的块中。

例子

这示出了如何将输入的第四行abcdaebabcdaeb被打印。第四行很好地表示了这一点,因为大多数可能的情况都发生了:

code input:  "abcdaeb"
order input: "abcdaeb"

printing the fourth line, H = "d":

   "a" is printed, because "a" <= "d" and ("d" >= "d" and "d" is in "abcda")
   "b" is printed, because "b" <= "d" and ("d" >= "d" and "d" is in "bcdaeb")
   "c" are not printed, because neither "d" nor "e" (chars >= "d") are not in "c"
   "d" is printed, because "d" <= "d" and ("d" >= "d" and "d" is in "d")
   "a" is printed, because "a" <= "d" and ("d" >= "d" and "d" is in "abcda")
   "e" is not printed, because "e" > "d"
   "b" is printed, because "b" <= "d" and ("d" >= "d" and "d" is in "bcdaeb")

therefore the fourth line is: aabb__ddaa__bb

还有一个基于测试用例的示例,@ Optimizer给了我。(这破坏了我的33解决方案)。

code input:  "acab"
order input: "acab"

printing the second line, H = "b":

   "a" is printed, because "a" <= "b" and ("c" >= "b" and "c" is in "aca")
   "c" is not printed, because "c" > "b"
   "a" is printed, because "a" <= "b" and ("c" >= "b" and "c" is in "aca")
   "b" is printed, because "b" <= "b" and ("b" >= "b" and "b" is in "b")

therefore the second line is: a_ab

旧版本:58 57 52字节

JwKNFHS{J=K.e?eS>_>JxJHx_JHqYH@KkJs.e?@zknYNdK=KXKHN

在线尝试:Pyth编译器/执行器

这将创建一个遮罩,我将在打印每行之前和之后对其进行修改。有关更多信息,请参见编辑历史记录。


8

CJam,82个字节

目前相当长,我想我可以再剃掉几个字节。

leel:F]z::+F$_&\f{{W=1$=},\;}{]_{0f=_W=),\0=>Lf&Ra#)},F,S*\:+{~;1$L+:L;t}%oNo~}%];

算法

基本算法如下:

  • leel:F]z::+ :将代码,格式和每个字符的索引分组在一起
  • F$_&\f{{W=1$=},\;}:使用格式字符串将上述三胞胎分组为优先打印。此代码还确保对优先级进行了排序。
  • ]_{0f=_W=),\0=>Lf&Ra#)},:对于三元组的每个优先组,获取边界索引范围,并查看是否尚未打印任何索引。如果存在未打印的索引,请将此优先级组包括在“此步骤中要打印”组中。
  • F,S*\:+{~;1$L+:L;t}%oNo~}%:在此步骤中打印完所有组后,将代码填充到空白字符串的正确索引中,然后打印该字符串。同时更新包含打印索引列表的数组。

打完高尔夫球后要遵循的代码说明。

这是在代码本身上运行的代码:

输入:

leel:F]z::+F$_&\f{{W=1$=},\;}{]_{0f=_W=),\0=>Lf&Ra#)},F,S*\:+{~;1$L+:L;t}%oNo~}%];
aaabbbcccccdddddeefgghhiffggejkklmmmnoooopppqrrrssssllttttuuuvwwxxxxxxxyvvzzzzjjjj

输出:

lee                                                                               
   l:F                                                                            
      ]z::+                                                                       
           F$_&\                                                                  
                f{          }                                                     
                f{{     },  }                                                     
                f{{W=   },\;}                                                     
                f{{W=1$ },\;}                                                     
                f{{W=  =},\;}                                                     
                             {                                                }%];
                             {]_                                              }%];
                             {  {                   },                        }%];
                             {  {0f=                },                        }%];
                             {  {   _               },                        }%];
                             {  {    W=),           },                        }%];
                             {  {        \0=        },                        }%];
                             {  {           >       },                        }%];
                             {  {            Lf&    },                        }%];
                             {  {               Ra#)},                        }%];
                             {                        F,S*                    }%];
                             {                            \:+                 }%];
                             {                               {          }%    }%];
                             {                               {~;        }%    }%];
                             {                               {  1$L+:L; }%    }%];
                             {                               {         t}%    }%];
                             {                                            oNo~}%];

在这里在线尝试


oNo可以nTIO中替换为。
硕果累累

8

CJam,48个字节

ll:i:T.{___T#TW%@#~T<>+:e>)1$-@*123Se]m>}z_|(;N*

说明

l                                                Code.
 l                                               Priority.
  :i                                             Convert priority to integer.
    :T                                           Save to T.
      .{                                }        For corresponding items:
      .{___                             }        Copy the current priority 3 times.
      .{   T#                           }        First position with this priority.
      .{     TW%                        }        Reverse T.
      .{        @#                      }        First (last) position with this priority.
      .{          ~T<                   }        Cut T at the end of this priority.
      .{             >                  }        Cut at the beginning of this priority.
      .{              +                 }        Insert the current priority to
                                                 prevent the array being empty.
      .{               :e>              }        Array maximum.
      .{                  )1$-          }        Count of integers between the current
                                                 priority and the maximum, inclusive.
      .{                      @*        }        That number of the current character.
      .{                        123Se]  }        Fill irrelevant priorities with spaces.
      .{                              m>}        Rotate the array to make non-spaces
                                                 starting at the current priority.
                                                 Returns a column containing 123 items.
                                         z       Zip to get the rows from columns.
                                          _|     Remove duplicate rows, including
                                                 unused priorities and all-space rows.
                                            (;   Remove the first row (an all-space row).
                                              N* Insert newlines.

6

IDL 8.4,316个 318 304字节

新版本,仍然太长,但更短!而且,按照IDL的真正精神,完全矢量化了,这意味着(因为没有for循环),一旦我将版本完全升级到8.4,我现在就可以将其作为一行运行并自己运行。稍后将对其进行编辑。

一线版本:

c=(f='')&read,c,f&l=[0:strlen(f)-1]&c=strmid(c,l,1)&s=strmid(f,l,1)&u=s.uniq()&k=value_locate(u,s)&n=[0:max(k)]&d=hash(n,u.map(lambda(x,y,z:max(z[(r=stregex(y,'('+x+'(.*))?'+x,len=w)):r+w-1])),f,k))&print,n.map(lambda(n,l,c,d,i:i.reduce(lambda(x,i,l,c,d,n:x+(d[l[i]]ge n?c[i]:' ')),l,c,d,n)),k,c,d,l)&end

带有换行符(相同的字节数,用\ n vs&减去),并注释:

c=(f='') ;initialize code and format as strings
read,c,f ;read two lines of input from the prompt
l=[0:strlen(f)-1] ;index array for the strings
c=strmid(c,l,1) ;split the code string into an array, via substrings of length 1
s=strmid(f,l,1) ;same for the format string, saving f for regex later
u=s.uniq() ;get the sorted unique values in the format string (sorts A->a)
k=value_locate(u,s) ;assign layer values to the format characters
n=[0:max(k)] ;index array for the unique format characters
d=hash(n,u.map(lambda(x,y,z:max(z[(r=stregex(y,'('+x+'(.*))?'+x,len=w)):r+w-1])),f,k))
print,n.map(lambda(n,l,c,d,i:i.reduce(lambda(x,i,l,c,d,n:x+(d[l[i]]ge n?c[i]:' ')),l,c,d,n)),k,c,d,l)
end ;end the script

这是第9行的算法分解:

r=stregex(y,'('+x+'(.*))?'+x,len=w) ; r, w = starting position & length of substring in y {format string} bracketed by x {character} (inclusive)
z[(r=...):r+w-1] ; grab a slice of z {layer array} from r to r+w-1 -> layer values for each character in the substring
max(z[...]) ; max layer (depth) of any characters in that slice
u.map(lambda(x,y,z:max(...)),f,k) ;map an inline function of the above to every element of the unique-formatting-character array
d=hash(n,u.map(...)) ; create a hash using the unique indices, the result is a hash of (character:max_substring_depth)

...和10:

x+(d[l[i]]ge n?c[i]:' ')) ; ternary concatenation: if maxdepth for this character >= current depth, add the character, otherwise add ' '
i.reduce(lambda(x,i,c,d,l,n:...)),,l,c,d,n) ;accumulate elements of i {code/format index array} by passing them through the inline ternary concatenation function
print,n.map(lambda(n,l,c,d,i:i.reduce(...)),k,c,d,l) ;map the depth index through the reduction, ending up with a string for each depth layer, then print it

第9行和第10行完成了实际工作,其余部分设置了最终所需的变量。我认为这将是一门必经之路,我找不到其他地方可以做得更好。

旧版本(以下所有内容均已过时):

这是远远不够的胜利,因为这是一种糟糕的高尔夫语言,但是没有人会回答IDL,所以我会去争取。

a=(b='')
read,a,b
c=[0:strlen(b)-1]
d=strmid(b,c,1)
a=strmid(a,c,1)
e=d[uniq(d,sort(d))]
f=list(value_locate(e,d),/e)
s=hash()
for i=0,max(f)do begin
g=stregex(b,'('+e[i]+'(.*))?'+e[i],l=l)
s[i]=max(f[g:g+l-1])
print,f.reduce(lambda(x,y,z:x+(s.haskey(y)?z[y]:' '),s,a)
s=s.filter(lambda(x,y:x[1]gt y),i)
endfor
end

我不确定是否有办法进一步减少它...我可以同时在a和b上调用strmid,但是随后我花了更多的字节索引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.