25
\$\begingroup\$

Gimbap(김밥) is Korean food, somewhat looks like sushi roll.

Here is Korean emoticon represent Gimbap : @))))))))))

Your quest is make ASCII Gimbap cutter.

Rule

Input is string made with only @ and ).

Output cuts every valid Gimbap with ), and then add @ between them. So for example @)) to @) @).

Valid Gimbap starts with @ and followed by any amount of ).

If there is no valid Gimbap, output is blank.

Input and output

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

Winning condition

This is , so shortest code wins.

from sandbox

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Is the rule equivalent to "output one copy of @) for every ) in the input not counting those before any @? May our output include a trailing space, like "@) @) "? \$\endgroup\$
    – xnor
    Jun 9, 2019 at 4:54
  • \$\begingroup\$ It is. And output can include a trailing space. \$\endgroup\$
    – LegenDUST
    Jun 9, 2019 at 4:56
  • \$\begingroup\$ Do we have to output a space delimited string or can we output an array of slices? Also, are we limited to those 2 characters or can we use any 2 characters of our choosing? \$\endgroup\$
    – Shaggy
    Jun 9, 2019 at 13:15
  • \$\begingroup\$ My first time trying to read the title, "Gimp-bat clutter? What?" \$\endgroup\$ Jun 9, 2019 at 17:13

24 Answers 24

10
\$\begingroup\$

Haskell, 32 bytes

f(')':s)=f s
f s=do ')'<-s;"@) "

Try it online!

Recursion beats using span for removing the initial ('s.

Haskell, 33 bytes

f s=do ')'<-snd$span(<'@')s;"@) "

Try it online!

\$\endgroup\$
7
\$\begingroup\$

JavaScript, 42 41 bytes

s=>s.split(/(?<=@.*)\)/).fill``.join`@) `

Try It Online!

\$\endgroup\$
5
\$\begingroup\$

C (gcc), 53 bytes

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

Try it online!

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

JavaScript (Node.js), 48 47 bytes

x=>"@) ".repeat(x.split(/(?<=@.*)\)/).length-1)

Try it online!

JavaScript (Node.js), 53 49 bytes

x=>[...x].map(y=>y<"0"?m+=s&&"@) ":s=1,s=m="")&&m

Try it online!

JavaScript (Node.js), 60 bytes

x=>x.replace(/(@?)(\)*)/g,(_,a,b)=>a&&b.replace(/./g,"@) "))

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Canvas, 10 9 bytes

@sj∑L@) ×

Try it here!

\$\endgroup\$
4
\$\begingroup\$

Retina 0.8.2, 14 bytes

^\)+|@

\)
@) 

Try it online! Link includes test cases. Explanation:

^\)+|@

Delete leading )s and all @s.

\)
@) 

Replace all )s with @)s. (Note: trailing space.)

\$\endgroup\$
3
\$\begingroup\$

Python 2, 39 bytes

lambda s:'@) '*s.lstrip(')').count(')')

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Jelly, 8 bytes

ṣṀḊẎ”@pK

Try it online!

-1 thanks to Jonathan Allan.

\$\endgroup\$
0
2
\$\begingroup\$

Perl 5 -p, 26 bytes

$_=s/^.*?@//&&'@) 'x y/)//

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 24 bytes \$\endgroup\$
    – wastl
    Jun 15, 2019 at 10:57
2
\$\begingroup\$

05AB1E, 12 bytes

')Û'@KS'@ìðý

Try it online!

Explanation

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

Batch, 58 bytes

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

Takes input as a command-line parameter. Explanation:

@set s=%1@

Suffix an @ in case the input doesn't contain any.

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

Delete up to the first @, replacing it with a ( to ensure that the string is not empty (because %:% doesn't work on empty strings). The ( also makes the echo work if the rest of the string is empty.

@set s=%s:@=%

Delete any remaining @s.

@echo%s:)=@) %

Expand any remaining )s.

\$\endgroup\$
2
\$\begingroup\$

05AB1E (legacy), 10 bytes

Z¡¦JS'@ìðý

Try it online!

This bug forces me to use the legacy version. This is the code for the current version of 05AB1E (11 bytes):

'@¡¦JS'@ìðý

Try it online!

Port of my Jelly answer.

\$\endgroup\$
2
\$\begingroup\$

Japt v2.0a0 -S, 15 bytes

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

Try it

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

Alternative

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

Try it

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

brainfuck, 49 bytes

,[[-<+>>++++<]>[[-]<<<[[.>]<---------.[-]]>[-]],]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Japt v2.0a0 -P, 15 bytes

f/@\)+/ ËÅç"@) 

Saved 2 bytes thanks to @Shaggy.

Try it

\$\endgroup\$
2
  • \$\begingroup\$ 15 bytes \$\endgroup\$
    – Shaggy
    Jun 9, 2019 at 8:56
  • \$\begingroup\$ @Shaggy Thanks, I haven't golfed in Japt recently \$\endgroup\$
    – Gymhgy
    Jun 9, 2019 at 17:07
1
\$\begingroup\$

Jelly, 12 bytes

œl”)ḟ”@⁾@ jⱮ

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 6, 34 bytes

{~map {'@)'xx.chars-1},m:g/\@\)+/}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Ruby -p, 28 bytes

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

Try it online!

Explanation

                                # -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
\$\endgroup\$
1
\$\begingroup\$

Java 10, 49 bytes

s->s.replaceAll("^\\)+|@+","").replace(")","@) ")

Try it online.

\$\endgroup\$
1
\$\begingroup\$

sed, 30 bytes

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

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Welcome to PPCG. Unfortunately, your code can't handle leading )s and multiple @ properly. And, how about using Try it online? \$\endgroup\$
    – LegenDUST
    Jun 11, 2019 at 13:00
  • 1
    \$\begingroup\$ As you can see in 5th or last example, leading )s have to be ignored. \$\endgroup\$
    – LegenDUST
    Jun 11, 2019 at 13:08
  • \$\begingroup\$ @LegenDUST, you are right! it was not that easy. I guess the working version is much uglier \$\endgroup\$ Jun 11, 2019 at 13:25
  • \$\begingroup\$ 28 chars: s/^)*//;s/[^)]//g;s/./@) /gp \$\endgroup\$
    – jnfnt
    Jun 13, 2019 at 2:22
1
\$\begingroup\$

Wolfram Language (Mathematica), 91 90 85 71 70 59 57 bytes

StringReplace@{(StartOfString~~")"..)|"@"->"",")"->"@) "}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Pyth, 20 bytes

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

Try it online! Note that there is a trailing space at the end of the program. This one is (or rather, started out as) a rather direct translation of the Python 2 answer (though the lstrip part was surprisingly difficult).

Explanation:

*            # 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)
\$\endgroup\$
1
\$\begingroup\$

krrp, 63 bytes

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

Try it online!


Explanation

^":                   ~ 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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PowerShell, 42 bytes

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

Try it online!

Unrolled:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.