做紫菜刀


23

紫菜包饭(Korean)是韩国料理,有点像寿司卷。

这是代表朝鲜拌饭的韩国图释: @))))))))))

您的任务是制造ASCII Gimbap切割机。

规则

输入是仅由@和组成的字符串)

输出使用剪切每个有效的Gimbap ),然后 @在它们之间添加。因此,例如@))@) @)

有效紫菜饭以开头,@后跟任意数量的)

如果没有有效的拌饭,则输出为空白。

输入输出

Input | Output
@))))) | @) @) @) @) @)
@))))))) | @) @) @) @) @) @) @)
@))@))) | @) @) @) @) @)
@) | @)
)) | 
@ | 
@@)@@@))) | @) @) @) @)
@)@)@)) | @) @) @) @)
@@@)) | @) @)
))@) | @)

获奖条件

这是,因此最短的代码获胜。

沙盒


2
该规则是否等效于“ 在输入中@) 每输出一个副本,)不计算任何副本@?我们的输出是否可以包含尾随空格,例如"@) @) "
xnor

它是。输出可以包含尾随空格。
LegenDUST

我们必须输出一个以空格分隔的字符串还是可以输出一个切片数组?另外,我们是否限于这两个字符,还是可以使用我们选择的任何两个字符?
毛茸茸的

我第一次尝试阅读标题“金普蝙蝠混乱?什么?”
Draco18s

Answers:




5

C(gcc),53个字节

i;f(char*_){for(i=1;*_;!i&*_++&&printf("@) "))i&=*_;}

在线尝试!

i;f(char*_){for(    *_;                      )      }   //loop over the string:
                i=1;   !i&                    i&=*_;    //skip leading `)`s
                       !i&*_++&&printf("@) ")           //and print "@) "for each `)` thereafter







2

05AB1E,12个字节

')Û'@KS'@ìðý

在线尝试!

说明

')Û            # trim leading ")"
   '@K         # remove all "@"
      S        # split to list of characters
       '@ì     # prepend "@" to each
          ðý   # join on spaces

2

批处理,58个字节

@set s=%1@
@set s=%s:*@=(%
@set s=%s:@=%
@echo%s:)=@) %

将输入作为命令行参数。说明:

@set s=%1@

@在输入不包含任何后缀的情况下添加一个后缀。

@set s=%s:*@=(%

删除第一个@,用代替,(以确保字符串不为空(因为%:%不适用于空字符串)。该(也使得echo工作,如果该字符串的其余部分是空的。

@set s=%s:@=%

删除所有剩余@的。

@echo%s:)=@) %

展开所有剩余)的。



2

Japt v2.0a0,15 -S个字节

r/^\)+|@/ ¬mi'@

试试吧

r/^\)+|@/ ¬mi'@     :Implicit input of string
r                   :Remove
 /^\)+|@/           :  "@"s and leading ")"s
          ¬         :Split
           m        :Map
            i'@     :  Prepend "@"
                    :Implicit output, joined with spaces

另类

e/^\)/ è\) Æ"@)

试试吧

e/^\)/ è\) Æ"@)     :Implicit input of string
e                   :Recursively remove
 /^\)/              :  Leading ")"
       è\)          :Count remaining ")"s
           Æ        :Map the range [0,Count)
            "@)     :  Literal string
                    :Implicit output, joined with spaces





1

Ruby -p,28个字节

$_= ~/@/&&'@) '*$'.count(?))

在线尝试!

说明

                                # -p gets a line of STDIN
$_=                             # Set output to
    ~/@/                        # Find first '@' in input
                                # nil (falsey) if not found
        &&                      # If found, set output to
          '@) '                 # Sliced gimbap
               *                # Repeat
                $'              # In the string after the first '@',
                  .count(?))    # ... count the number of ')'
                                # -p outputs the contents of $_
                                # nil outputs as a blank string


1

sed,30个字节

s/)\?@\()\?\)/\1/g; s/)/@) /gp

在线尝试!


欢迎来到PPCG。不幸的是,您的代码无法正确处理前导)和多个@。而且,如何使用“ 在线试用”呢
LegenDUST

1
正如您在第5个或最后一个示例中看到的那样,)必须忽略前导s。
LegenDUST

@LegenDUST,你是对的!这不是那么容易。我猜工作版本要
难看

28个字符:s / ^)* //; s / [^)] // g; s /./@)/ gp
jnfnt


1

Pyth,20个字节

*?}\@z/>zxz\@\)0"@) 

在线尝试!请注意,程序结尾处有一个尾随空格。这是Python 2答案的直接翻译(或者说,起初是这样)(尽管lstrip部分非常困难)。

说明:

*            # repeat string
  ?          # repeat count: ternary
    }\@z     # condition: check whether input contains @
    /        # if condition is true: count occurrences of one string in another
      >      # array slice: all elements of array (or string) from a specific index and upwards
        z    # the thing to slice (input)
        xz\@ # the index first occurrence of \@ in z
      \)     # string to count occurrences of (\x is shorthand for "x")
    0        # value when ternary condition is false
  "@) "      # the string to be repeated (automatically terminated by end-of-line)

1

krrp,63个字节

^":\L,^*':?#?E'E!-@1#!r'?=#!f'$64.-?*L$64.L$41.L$32.-@0#!r'.0".

在线尝试!


说明

^":                   ~ take the string as a parameter named `"`
 \L                   ~ import the list module
 ,^*':                ~ apply a binary function
  ?#?E'               ~  if the string is empty,
   E                  ~   return the empty string; else
   !-@1#!r'           ~   define `-` as the cut Gimbap
   ?=#!f'$64.         ~    if an at sign is seen,
    -                 ~    return the cut Gimbap; else
    ?*                ~    if an at sign has been seen,
     L$64.L$41.L$32.- ~     return a Gimbap piece together
                      ~     with freshly cut Gimbap; else
     @0#!r'           ~     proceed to cut
 .0".                 ~ to zero and the above taken string

在线尝试!


1

PowerShell,42个字节

''+($args|sls '(?<=@.*)\)'-a|% m*|%{'@)'})

在线尝试!

展开:

$arrayOfCuttedGimbaps = $args|select-string '(?<=@.*)\)' -AllMatches|% Matches|%{'@)'}
''+($arrayOfCuttedGimbaps)    # toString and output
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.