49
\$\begingroup\$

We already now how to strip a string from its spaces.

However, as proper gentlemen/ladies, we should rather undress it.


Undressing a string is the same as stripping it, only more delicate. Instead of removing all leading and trailing spaces at once, we remove them one by one. We also alternate between leading and trailing, so as not to burn steps.

Example, starting with " codegolf " (five leading and trailing spaces):

     codegolf     
    codegolf     
    codegolf    
   codegolf    
   codegolf   
  codegolf   
  codegolf  
 codegolf  
 codegolf 
codegolf 
codegolf

  1. First output the string unchanged. Then, output every step. Begin by removing a leading space (if applicable - see rule #2).

  2. The input may have a different number of leading and trailing spaces. If you run out of spaces on one side, keep undressing the other until the string is bare.

  3. The input may have no leading nor trailing spaces. If that's the case, output it as-is.

  4. Use PPCG's default I/O methods. PPCG Default loopholes are forbidden.

  5. Undefined behaviour on empty input, or input that only contains spaces, is OK.

  6. You can assume that the string will only contain characters from the ASCII printable space (0x20 to 0x7E).


Examples - spaces are replaced by dots . for better readability:

4 leading spaces, 5 trailing: "....Yes, Sir!....."
....Yes, Sir!.....
...Yes, Sir!.....
...Yes, Sir!....
..Yes, Sir!....
..Yes, Sir!...
.Yes, Sir!...
.Yes, Sir!..
Yes, Sir!..
Yes, Sir!.
Yes, Sir!

6 leading, 3 trailing: "......Let's go golfing..."
......Let's go golfing...
.....Let's go golfing...
.....Let's go golfing..
....Let's go golfing..
....Let's go golfing.
...Let's go golfing.
...Let's go golfing
..Let's go golfing
.Let's go golfing
Let's go golfing

0 leading, 2 trailing: "Hello.."
Hello..
Hello.
Hello

0 leading, 0 trailing: "World"
World

21 leading, 5 trailing: ".....................a....."
.....................a.....
....................a.....
....................a....
...................a....
...................a...
..................a...
..................a..
.................a..
.................a.
................a.
................a
...............a
..............a
.............a
............a
...........a
..........a
.........a
........a
.......a
......a
.....a
....a
...a
..a
.a
a

A gentleman/lady is concise, so the shortest answer in bytes wins.

\$\endgroup\$
14
  • \$\begingroup\$ From Sandbox: codegolf.meta.stackexchange.com/a/13765/71426 \$\endgroup\$ Commented Sep 21, 2017 at 9:19
  • \$\begingroup\$ Can we assume that there will be at least one non-space character? \$\endgroup\$ Commented Sep 21, 2017 at 9:34
  • 2
    \$\begingroup\$ @KevinCruijssen You only have to handle ASCII characters in the printable space (0x20 to 0x7E). The other ones are Undefined Behavior. \$\endgroup\$ Commented Sep 21, 2017 at 10:54
  • 1
    \$\begingroup\$ @KevinCruijssen Yes, there will be no test case like this. There will be no things like " test\r " or " \v test" either. \$\endgroup\$ Commented Sep 21, 2017 at 10:57
  • 1
    \$\begingroup\$ Is this a valid test case ".....................a....."? If so I suggest to add it since some answers seems to fail this kind of test. (dots are for better readability of course) \$\endgroup\$
    – Cinaski
    Commented Sep 21, 2017 at 13:54

41 Answers 41

11
\$\begingroup\$

Retina, 26 bytes

{m`^ (.+)\z
$&¶$1
 $
 ¶$%`

Try it online! (Test suite uses periods for clarity. The footer and header convert them to and from spaces for the main code.)

Explanation

It would be nice if we could just alternate between dropping a leading and a trailing space and printing the intermediate result each time. The problem is that currently Retina can't print conditionally, so it would even print this intermediate result if there are no leading or no trailing spaces left, generating duplicates. (Retina 1.0 will get an option that only prints the result if the string was changed by the operation, but we're not there yet...)

So instead, we're building up a single string containing all intermediate results and printing that at the end.

{m`^ (.+)\z
$&¶$1

The { wraps both stages of the program in a loop which repeats until the string stops changing (which means there are no leading/trailing spaces left). The stage itself matches a leading space on the final line of the string, and that final line, and then writes back the match, as well as the stuff after the space on a new line (thereby dropping the leading space in the copy).

 $
 ¶$%`

Removing the trailing space is a bit easier. If we just match the final space, we can access the stuff in front of it (on the same line) with $%` which is a line-aware variant of the prefix substitution $`.

\$\endgroup\$
11
\$\begingroup\$

Python 2, 122 107 103 102 98 95 93 91 90 88 87 bytes

s=input()+' '
a=0
while-a*s!=id:
 if a:id=s
 a=~a
 if'!'>s[a]:s=s[1+a:len(s)+a];print s

Try it online!


Python 3, 97 95 93 90 bytes

s=input()
a=p=print
p(s)
while s!=a:
 a=s
 if'!'>s:s=s[1:];p(s)
 if'!'>s[-1]:s=s[:-1];p(s)

Try it online!

\$\endgroup\$
12
  • \$\begingroup\$ Using s=input() instead of a function would take less bytes. \$\endgroup\$ Commented Sep 21, 2017 at 10:23
  • \$\begingroup\$ Refering to 5. Undefined behaviour on empty input, or input that only contains spaces, is OK., 98 bytes. \$\endgroup\$ Commented Sep 21, 2017 at 10:42
  • \$\begingroup\$ Python 3 saves a byte. \$\endgroup\$
    – LyricLy
    Commented Sep 21, 2017 at 10:42
  • \$\begingroup\$ @JonathanFrech I hadn't seen that; thanks :) \$\endgroup\$
    – TFeld
    Commented Sep 21, 2017 at 10:49
  • 2
    \$\begingroup\$ You can further golf the Python 2 code by replacing a with the built-in function id to save having to define it at the start. -2 bytes. \$\endgroup\$
    – LyricLy
    Commented Sep 21, 2017 at 11:00
7
\$\begingroup\$

Perl 6, 55 bytes

Saved 3 bytes thanks to @nwellnhof.

{($_,{$++%2??S/" "$//!!S/^" "//}...*)[^.comb*2].unique}

Try it online!

Explanation: ($_,{$++%2??S/" "$//!!S/^" "//}...*) is a recursive infinite sequence that starts with the original string ($_) and the next element is given by the block called on the previous element.

The block itself gets the string in the $_ variable. The operator S/(regex)/(string)/ will search for the first occurence of (regex) in $_, replaces it with (string), and returns the result. If there is no match, it returns the content of $_ unchanged. We use the ternary operator ?? !! with the condition $++%2, which alternates between False and True ($ is a free variable that conserves its contents across calls to the block.)

In the worst case (all spaces on one side and 1 other character), we remove 1 space every 2 steps. So we can be sure that in 2*(length of the string) steps, all spaces will have been removed. We take that many elements from the recursive sequence with [^.comb*2] and finally discard duplicates (which occur whenever a space should have been removed but it isn't there) with .unique. This returns the list of strings, progressively stripped of spaces.

\$\endgroup\$
2
  • \$\begingroup\$ [^.comb*2] saves 2 bytes. For some reason this works, but [^2*.comb] doesn't. No idea why. Using a ternary ?? !! to select the regex saves another byte. \$\endgroup\$
    – nwellnhof
    Commented Sep 21, 2017 at 13:50
  • \$\begingroup\$ Thanks! I tried [^2*.comb] and it didn't work, so I just used [0..2*.comb]. And thanks for the ternary, I just thought it's too expensive and it didn't occur to me that I replaced it with something even more expensive... \$\endgroup\$
    – Ramillies
    Commented Sep 21, 2017 at 14:08
7
\$\begingroup\$

05AB1E, 21 15 bytes

=v¬ðQi¦=}¤ðQi¨=

Try it online!

Explanation^

=                 # print input
 v                # for each character in input
  ¬ðQi  }         # if the first char in the current string is a space
      ¦=          # remove it and print without popping
         ¤ðQi     # if the last char in the current string is a space
             ¨=   # remove it and print without popping
\$\endgroup\$
5
  • \$\begingroup\$ Dang, I tried something similar but for some reason I was sure head/tail didn't work on strings, and I was about to raise an issue about it on github. Must have read the debug logs wrong. :-) \$\endgroup\$
    – scottinet
    Commented Sep 21, 2017 at 14:17
  • 1
    \$\begingroup\$ @scottinet: I just found a way to get around the end-check :) \$\endgroup\$
    – Emigna
    Commented Sep 21, 2017 at 15:00
  • \$\begingroup\$ oh... why didn't we think about that before? since we print conditionally there is no need to loop exactly the right number of times, we only need to loop over enough times. I'm borrowing that idea to improve my answer :-) \$\endgroup\$
    – scottinet
    Commented Sep 21, 2017 at 15:10
  • 1
    \$\begingroup\$ @scottinet: Yeah. It's obvious when you think about it, but sometimes it's easy to miss those things :P \$\endgroup\$
    – Emigna
    Commented Sep 21, 2017 at 15:27
  • \$\begingroup\$ TFW the clunky redundant answer gets the lead... \$\endgroup\$ Commented Sep 21, 2017 at 16:36
7
\$\begingroup\$

C (gcc), 89 84 bytes

Recursive version is shorter ;-)

j;f(char*s){puts(s);*s^32||puts(++s);s[j=strlen(s)-1]<33?s[j]=0,f(s):*s^32||f(s+1);}

Try it online!

C (gcc), 107 102 101 100 99 bytes

Saved 2 bytes thanks to @Jonathan Frech using spaces and ~

i,j,k;f(char*s){for(i=~++k,puts(s);i^k;k=s[j=strlen(s)-1]<33?s[j]=0,puts(s):0)*s^32?i=0:puts(++s);}

Try it online!

\$\endgroup\$
8
  • 2
    \$\begingroup\$ I think the question really wants you to remove spaces rather than dots. There is even an advantage to using spaces; you can replace ==46 with <33 as the space is the smallest printable character and you only have to handle those. \$\endgroup\$ Commented Sep 21, 2017 at 14:20
  • \$\begingroup\$ What does the ++k+ do? \$\endgroup\$ Commented Sep 21, 2017 at 14:36
  • \$\begingroup\$ @JonathanFrech It pre-increments k and adds one, which is equivalent to k = k + 1; i = k + 1; or i = k + 2; k = k + 1. \$\endgroup\$
    – hyperneutrino
    Commented Sep 21, 2017 at 14:37
  • \$\begingroup\$ Technically i=k+++2 works too which I would have used because the +++ looks weird :P \$\endgroup\$
    – hyperneutrino
    Commented Sep 21, 2017 at 14:38
  • \$\begingroup\$ @HyperNeutrino Yeah, I know what the pre-increment operator does; though I do not get how the code works without it. So really I was asking what role it plays, rather than how it is defined. \$\endgroup\$ Commented Sep 21, 2017 at 14:42
6
\$\begingroup\$

JavaScript (ES6) 92

@Upvoters: have a look at the other JS answer down below that is 76 bytes long

(s,q,l=2,p=0)=>{for(alert(s);l--;p=!p)s[+p&&s.length-p]<'!'&&alert(s=s.slice(!p,-p||q,l=2))}

A loop looking for a space at front or at end. If found, remove space and output string. If no space found 2 times, stop.

F=
(s,q,l=2,p=0)=>{for(alert(s);l--;p=!p)s[+p&&s.length-p]<'!'&&alert(s=s.slice(!p,-p||q,l=2))}

// some trick to show dots instead of spaces, for test
alert=x=>console.log(x
  .replace(/^ +/g,z=>'.'.repeat(z.length))
  .replace(/ +$/g,z=>'.'.repeat(z.length))
)

function go() {F(I.value.replace(/\./g,' '))}

go()
<input ID=I value='....yes Sir!....'> (use dot instead of space)
<button onclick='go()'>Go</button>

\$\endgroup\$
2
  • \$\begingroup\$ You could save a byte by checking for space with <'!'. To make your snippet still work you can replace periods with spaces before passing to your function. \$\endgroup\$ Commented Sep 21, 2017 at 15:16
  • \$\begingroup\$ @JustinMariner ok now, because OP stated no char expected less than ' '. Thanks \$\endgroup\$
    – edc65
    Commented Sep 21, 2017 at 15:56
6
\$\begingroup\$

Perl 5, 32 bytes

Saved 4 bytes due to @Abigail.

1while s/^ /!say/e+s/ $/!say/e

Requires -pl counted as 2, invoked with -E.

Sample Usage

$ echo '   test   ' | perl -plE'1while s/^ /!say/e+s/ $/!say/e'
   test   
  test   
  test  
 test  
 test 
test 
test

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Doesn't work correctly for strings without trailing spaces. \$\endgroup\$
    – nwellnhof
    Commented Sep 21, 2017 at 13:35
  • \$\begingroup\$ print;s/^ //&&print,s/ $//&&print while/^ | $/ works with -n flag, also -l is not needed \$\endgroup\$ Commented Sep 21, 2017 at 15:56
  • \$\begingroup\$ @nwellnhof fixed. \$\endgroup\$
    – primo
    Commented Sep 23, 2017 at 11:54
5
\$\begingroup\$

C# (.NET Core), 192 183 182 181 179 178 bytes

-3 bytes thanks to Kevin Cruijssen

n=>{var o=n+"\n";for(var e=1;n.Trim()!=n;){if(1>(e^=1))if(n[0]<33)n=n.Remove(0,1);else continue;else if(n.TrimEnd()!=n)n=n.Remove(n.Length-1);else continue;o+=n+"\n";};return o;}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Some things to golf: var e=1;while(n.Trim()!=n) -> for(var e=1;n.Trim()!=n;); if(n[0]==' ') -> if(n[0]<33) \$\endgroup\$ Commented Sep 21, 2017 at 10:42
  • \$\begingroup\$ I've thought about second one, but what if the test string contains newlines? \$\endgroup\$ Commented Sep 21, 2017 at 10:44
  • \$\begingroup\$ Ok, <33 is possible due to OP's newly added rule: "You can assume that the string will only contain characters from the ASCII printable space (0x20 to 0x7E)." \$\endgroup\$ Commented Sep 21, 2017 at 10:58
5
\$\begingroup\$

Java 8, 150 146 145 137 bytes

s->{String r=s;for(int f=0;s!=s.trim();f^=1)r+="\n"+(s=f+s.charAt(0)<33|!s.endsWith(" ")?s.substring(1):s.replaceAll(" $",""));return r;}

-4 bytes thanks to @Nevay changing (f<1&s.charAt(0)<33) to f+s.charAt(0)<33.
-1 byte by using the !s.trim().equals(s) trick from @someone's C# .NET answer instead of s.matches(" .*|.* ").
-8 bytes thanks to @Nevay again by changing !s.trim().equals(s) to s!=s.trim(), because String#trim will return "A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space", thus the reference stays the same and != can be used to check if they are the same reference, instead of .equals to check the same value.

Explanation:

Try it here (or try a more visual version here with # instead of spaces).

s->{                               // Method with String as both parameter and return-type
  String r=s;                      //  Result-String (starting at the input)
  for(int f=0;                     //  Flag-integer (starting at 0)
      s!=s.trim();                 //  Loop as long as `s` contains leading/trailing spaces
      f^=1)                        //    And XOR(1) `f` after every iteration (0->1; 1->0)
    r+="\n"                        //   Append the result with a new-line
       +(                          //    Followed by:
         s=f+                      //     If `f` is 0,
             s.charAt(0)<33        //     and `s` starts with a space
           |!s.endsWith(" ")?      //     Or doesn't end with a space
            s.substring(1)         //      Remove the first leading space
           :                       //     Else:
            s.replaceAll(" $",""));//      Remove the last trailing space
                                   //  End of loop (implicit / single-line body)
  return r;                        //  Return the result-String
}                                  // End of method
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can use s=f+s.charAt(0)<33 instead of (f<1&s.charAt(0)<33) (-4 bytes). \$\endgroup\$
    – Nevay
    Commented Sep 21, 2017 at 12:01
  • 1
    \$\begingroup\$ You can use s!=s.trim() instead of !s.trim().equals(s); (-8 bytes). \$\endgroup\$
    – Nevay
    Commented Sep 21, 2017 at 12:24
4
\$\begingroup\$

C, 91 90 bytes

i,l;f(char*s){for(i=puts(s);i;i=(s[l=strlen(s)-1]*=s[l]>32)?i:puts(s))i=*s<33&&puts(++s);}

Try it online!

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

Jelly, 16 bytes

Ḋ=⁶Ḣ$¡UµÐĿ¹Ṛƭ€QY

Try it online!

-2 bytes thanks to Erik the Outgolfer
-1 byte thanks to miles

Explanation

Ḋ=⁶Ḣ$¡UµÐĿ¹Ṛƭ€QY  Main link
       µÐĿ        While the results are unique (collecting intermediate results), apply the last link (`µ` creates a new monadic link):
Ḋ=⁶Ḣ$¡            Remove a space from the beginning if there is one
 =⁶Ḣ$             If the first character is a space, then 1, else 0
 =                Compare each character to
  ⁶               ' '
   Ḣ              Get the first comparison
Ḋ                 Then Dequeue the string (s -> s[1:])
    ¡             That many times
     U            And reverse the string (the next time this is called, it will remove spaces from the end instead)
             €    For each string
            ƭ     Alternate between two commands:
          ¹       Identity (do nothing), and
           Ṛ      Reverse
          ¹Ṛƭ€    Correct all strings that are reversed to remove the trailing space
              Q   Remove duplicates (where there was no space to remove)
               Y  Join on newlines
\$\endgroup\$
6
  • \$\begingroup\$ ḣ1Ḣ=⁶ -> =⁶Ḣ \$\endgroup\$ Commented Sep 21, 2017 at 14:09
  • \$\begingroup\$ @EriktheOutgolfer Thanks, edit coming. \$\endgroup\$
    – hyperneutrino
    Commented Sep 21, 2017 at 14:10
  • \$\begingroup\$ Cool idea with the alternating commands of reverse/identity! \$\endgroup\$
    – Emigna
    Commented Sep 21, 2017 at 15:02
  • \$\begingroup\$ @Emigna Thanks! :D I mostly just wanted an excuse to use the new quick... heh :P \$\endgroup\$
    – hyperneutrino
    Commented Sep 21, 2017 at 17:21
  • \$\begingroup\$ ƭ only needs a nilad if the chain is longer than two. ¹Ṛƭ works fine here. \$\endgroup\$
    – miles
    Commented Sep 22, 2017 at 11:26
3
\$\begingroup\$

Ruby, 63 bytes

->s{*x=s;(s=~/^ /&&x<<s=$';s=~/ $/&&x<<s=$`)while s=~/^ | $/;x}

Try it online!

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

Java (OpenJDK 8), 161 147 146 bytes

x->{for(int l=0,r=x.length(),k=-1,u,v;((u=32-x.charAt(l)>>k)*(v=32-x.charAt(r-1)>>-1))<1;x+="\n"+x.substring(l-=k&~u|v,r+=(k=~k)&~v|u));return x;}

Try it online!

-1 byte thanks to @Kevin Cruijssen!

x -> {
    /*
     * l: left index (inclusive)
     * r: right index (exclusive)
     * k: side to remove from, -1:=left, 0:=right
     * u: left character   0:=space, <0:=no space (-1 if k is left side)
     * v: right character  0:=space, -1:=no space
     */
    for (int l = 0, r = x.length(), k = -1, u, v;
            ((u = 32 - x.charAt(l) >> k)
           * (v = 32 - x.charAt(r - 1) >> -1)) < 1; // loop while left or right has space(s)
            x += "\n" + x.substring(                // append newline and substring
                    l -= k & ~u | v,                // inc. left  if k is left side
                                                    //               and left has space
                                                    //            or right has no space
                    r += (k = ~k) & ~v | u));       // dec. right if k is right side
                                                    //               and right has space
                                                    //            or left has no space
    return x;
}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Hehe, I saw your deleted answer and was wondering when you were below my 150 bytes and would undelete it. ;) \$\endgroup\$ Commented Sep 21, 2017 at 11:49
  • 1
    \$\begingroup\$ I'm not entirely sure, but I think you can golf a byte by changing (u=32-x.charAt(l)>>-1) to (u=32-x.charAt(l)>>k) \$\endgroup\$ Commented Sep 21, 2017 at 11:53
  • \$\begingroup\$ @KevinCruijssen Won't work, k is 0 every second iteration. \$\endgroup\$
    – Nevay
    Commented Sep 21, 2017 at 12:00
  • 1
    \$\begingroup\$ Yes, but the weird part is that the TIO works and gives the correct result for all test cases with this change for u. It doesn't when I also change -1 to k for v. I'm confused why it works though, since k will indeed become 0 after k=~k.. :S \$\endgroup\$ Commented Sep 21, 2017 at 12:05
  • 1
    \$\begingroup\$ @KevinCruijssen For the k=0 scenario: If left has spaces left, then u has the same value as before (0); if left has no spaces left, then (k=~k)&~v|u evaluates to -1|u (~0&-1|u), thus the undefined (negative) value of u does not matter (-1|x==-1). \$\endgroup\$
    – Nevay
    Commented Sep 21, 2017 at 12:56
3
\$\begingroup\$

05AB1E, 25 17 bytes

-8 bytes by borrowing the no-need-for-an-end-check idea from Emigna

,v2F¬ðQi¦DNiR},}R

Try it online!

I'm pretty sure a less straightforward approach can beat that solution easily. For now...

Explanations:

,v2F¬ðQi¦DNiR},}R           Full Programm
,                           Print the input string
 v                          For each char of the string
                               (we don't really care, we only need to loop
                                enough times to accomplish our task, since
                                we print conditionally we can loop more
                                times than necessary)
  2F...........}            Two times...
    ¬õQi                       Is 1st item a space?
        ¦D                        Remove 1st item + duplicate
          NiR}                    If on the second pass: reverse the list
              ,                   Pop & print with newline
               }               End If
                 R          Reverse the list
\$\endgroup\$
3
  • \$\begingroup\$ I like your approach with the loop :) I've been trying to figure out a way to do everything in one pass without multiple ifs, but I haven't figured it out yet. Also, your explanation seem to have an empty string instead of a space. \$\endgroup\$
    – Emigna
    Commented Sep 21, 2017 at 13:34
  • \$\begingroup\$ Thanks! I fixed the explanation, I forgot to edit the "is empty" part when I golfed my code, using S instead of # (-1 byte). The loop... well... it saves a whopping 1 byte compared to a straightforward approach. I'm currently looking for a shorter way to detect the end of the task (5 bytes for this is a lot), and I'm also considering a different approach altogether. I do think there is a more clever way to solve this challenge. \$\endgroup\$
    – scottinet
    Commented Sep 21, 2017 at 13:51
  • \$\begingroup\$ If you try and do everything in one pass (as I'm currently looking into), the best check I have for exiting the loop is 8 bytes... \$\endgroup\$
    – Emigna
    Commented Sep 21, 2017 at 14:00
3
\$\begingroup\$

R, 145 133 111 bytes

-12 bytes thanks to @Giuseppe, by storing the result of sub in a new variable and testing for whether it has changed

-22 bytes by returning a vector of strings rather than a string with newlines

function(s){L=s
while(grepl("^ | $",s)){if((x=sub("^ ","",s))!=s)L=c(L,x)
if((s=sub(" $","",x))!=x)L=c(L,s)}
L}

Try it online!

Explanation on a partially ungolfed version:

function(s){
  L=s                          # Initialise a vector with the original string
  while(grepl("^ | $",s)){     # While there are leading or trailing spaces...
    if((x=sub("^ ","",s))!=s){ # Check whether we can remove a leading space
      L=c(L,x)                 # If so, add the shortened string to the vector
    }
    if((s=sub(" $","",x))!=x){ # Check whether we can remove a trailing space
      L=c(L,x)                 # If so, add the shortened string to the vector
    }
  }
  L                            # Return the vector
}                              
\$\endgroup\$
6
  • \$\begingroup\$ can't you use C(s<-sub(),\n) instead of a separate print statement? Ah, no, because of sep=" " \$\endgroup\$
    – Giuseppe
    Commented Sep 21, 2017 at 13:38
  • \$\begingroup\$ @Giuseppe Yes, I think it works out slightly longer to include it all in a single statement because of the need to add sep="". In most challenges the extra trailing space would not matter, but here unfortunately it does! \$\endgroup\$ Commented Sep 21, 2017 at 13:45
  • \$\begingroup\$ 133 bytes -- something about your using sub just suggested this, IDK why \$\endgroup\$
    – Giuseppe
    Commented Sep 21, 2017 at 14:26
  • \$\begingroup\$ @Giuseppe Very elegant! \$\endgroup\$ Commented Sep 21, 2017 at 14:55
  • \$\begingroup\$ Could you just set L=s and return a vector of strings? \$\endgroup\$
    – Giuseppe
    Commented Sep 21, 2017 at 15:14
3
\$\begingroup\$

Java (OpenJDK 8), 137 125 121 120 124 bytes

s->{int i=1;do System.out.println(s);while(s!=(s=s.substring(s.charAt(0)<33?i:(i=0),s.length()-(s.endsWith(" ")?i^=1:0))));}

Try it online!

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

MATL, 21 16 bytes

tnE:"t@o&)w46-?x

This uses dots instead of spaces for greater clarity. For spaces replace 46 by 32.

Try it online!

Explanation

tn      % Input (implicit). Duplicate and push length, say L
E       % Multiply by 2
:       % Push range [1 2 ... 2*L]
"       % For each k in that array
  t     %   Duplicate the string at the top of the stack
  @     %   Push k
  o     %   Parity: gives 1 or 0
  &)    %   Two-ouput indexing. Pushes the k-th entry of the string and then
        %   the rest of the string. The 1-st output is the first, the 0-th
        %   is the last (indexing is 1-based dand modular)
  w     %   Swap
  46-   %   Subtract 46, which ias ACII for '.'
  ?     %   If non-zero
    x   %     Delete sub-string that was obained by removing that entry
        %   End (implicit)
        % End (implicit)
        % Display stack (implicit)
\$\endgroup\$
0
3
\$\begingroup\$

Husk, 23 22 bytes

u§↑L`G`I¢e₁ȯ↔₁↔
?tI<"!

Thanks to Leo for -1 byte.

Try it online!

Explanation

The function `G`I should really be a built-in...

?tI<"!  Helper function: remove initial space.
?  <"!  If less than the string "!",
 t      remove first character,
  I     else return as is.
u§↑L`G`I¢e₁ȯ↔₁↔  Main function.
         e       List containing
          ₁      the helper function
           ȯ↔₁↔  and the composition reverse-helper-reverse.
        ¢        Repeat it cyclically.
    `G`I         Cumulative reduce from left by function application
                 using input string as initial value.
 §↑L             Take first length(input) values.
u                Remove duplicates.
\$\endgroup\$
2
  • \$\begingroup\$ Nice! Indeed we would need more builtins for applying functions cyclically... btw I've found a sligthly shorter way to remove the first space: tio.run/##yygtzv7/v/… \$\endgroup\$
    – Leo
    Commented Sep 21, 2017 at 21:54
  • \$\begingroup\$ @Leo Thanks! Using ? seems obvious in hindsight... \$\endgroup\$
    – Zgarb
    Commented Sep 23, 2017 at 20:27
3
\$\begingroup\$

C++, 196 193 189 186 183 bytes

-10 bytes thanks to Jonathan Frech
-3 bytes thanks to Zacharý

#include<iostream>
#include<string>
#define D std::cout<<s<<'\n'
#define R ~-s.size()
auto u=[](auto s){D;while(s[0]<33||s[R]<33){if(s[0]<33)s.erase(0,1),D;if(s[R]<33)s.erase(R),D;}};

Compilation with MSVC requires the un-activation of SDL checks

\$\endgroup\$
10
  • \$\begingroup\$ You may be able to replace ==32 with <33. \$\endgroup\$ Commented Sep 21, 2017 at 12:56
  • \$\begingroup\$ I am no C++ master, though is #include<string> really necessary? \$\endgroup\$ Commented Sep 21, 2017 at 13:00
  • \$\begingroup\$ if(...){...;D;} -> if(...)...,D;. \$\endgroup\$ Commented Sep 21, 2017 at 13:04
  • \$\begingroup\$ @JonathanFrech What you did there was compiler specific, not guarranted by the standard. VC++ can't find a definition of the << operators without the explicit inclusion of string. \$\endgroup\$ Commented Sep 21, 2017 at 13:09
  • \$\begingroup\$ #define R ...<33, ||R){ and if(R){ -> #define R ...<33), ||R{ and if(R{. \$\endgroup\$ Commented Sep 21, 2017 at 13:10
2
\$\begingroup\$

C# (.NET Core), 176 170 bytes

using System;s=>{Action o=()=>Console.WriteLine(s);o();Func<int>l=()=>s.Length-1;while(s!=s.Trim()){if(s[0]<33){s=s.Remove(0,1);o();}if(s[l()]<33){s=s.Remove(l());o();}}}

Try it online!

This is an alternative to @someone's answer, and just outputs the strings directly.

\$\endgroup\$
2
  • \$\begingroup\$ Your program doesn't output the string unmodified before removing spaces. \$\endgroup\$ Commented Sep 21, 2017 at 15:18
  • \$\begingroup\$ @Nathan.EilishaShiraini I corrected that mistake and golfed a few bytes to reduce the byte count anyway. \$\endgroup\$
    – BgrWorker
    Commented Sep 21, 2017 at 15:26
2
\$\begingroup\$

JavaScript (ES6), 76 bytes

f=(s,r,n,l=s.length)=>s[r?--l:0]<"!"?s+`
`+f(s.slice(!r,l),!r):n?s:f(s,!r,1)

Outputs as a multiline string.

Test Cases

Using dots instead of spaces, as most answers are doing.

f=(s,r,n,l=s.length)=>s[r?--l:0]<"!"?s+`
`+f(s.slice(!r,l),!r):n?s:f(s,!r,1)

// converting to and from dots and spaces
let dots=s=>s.replace(/^\.+|\.+$/gm,x=>" ".repeat(x.length));
let spaces=s=>s.replace(/^ +| +$/gm,x=>".".repeat(x.length));

["....Yes, Sir!.....", "......Let's go golfing...", "Hello..", "World", ".....................a....."]
.forEach(test=>O.innerHTML+=spaces( f(dots(test)) ) + "\n\n");
<pre id=O></pre>

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

Sed, 24 bytes

p;:s s/ //p;s/ $//p;ts;D

Try It Online !

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

Octave, 88 83 bytes

5 bytes off thanks to Stewie Griffin!

x=[input('') 0];for p=mod(1:sum(x),2)if x(~p+end*p)<33,disp(x=x(2-p:end-p)),end,end

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Very nice. "Anyway, see if you can remove a couple of bytes" :-P \$\endgroup\$ Commented Sep 22, 2017 at 10:42
  • \$\begingroup\$ @StewieGriffin I meant in your answer... :-D Good idea, thanks! \$\endgroup\$
    – Luis Mendo
    Commented Sep 22, 2017 at 10:48
  • \$\begingroup\$ I might delete mine... It's so uninspired compared to this... \$\endgroup\$ Commented Sep 22, 2017 at 10:54
  • \$\begingroup\$ @StewieGriffin Here's an idea to remove two bytes. Pity that min is needed because of s being dynamically shrunk \$\endgroup\$
    – Luis Mendo
    Commented Sep 22, 2017 at 11:06
2
\$\begingroup\$

x86 machine code for Linux, 60 bytes

e8 1f 00 00 00 31 c0 80 3f 20 75 09 47 4d 74 10
e8 0f 00 00 00 80 7c 2f ff 20 74 05 84 c0 75 e5
c3 4d eb dc 6a 04 58 50 31 db 43 89 f9 89 ea cd
80 58 6a 0a 89 e1 89 da cd 80 58 c3

This is a function for Linux x86. It takes as input pointer to the string in edi and string length in ebp.

Ungolfed, with some infrastructure to test (compile with FASM, run with the string as program argument; look for undress: label for actual function code):

format ELF executable
segment executable
SYS_WRITE = 4
    jmp     callUndress
; -------------------- the function itself --------------------------------
; Input:
;   edi=string
;   ebp=length
undress:
undressLoopPrint:
    call    print
undressLoop:
    xor     eax, eax    ; flag of having printed anything on this iteration
    cmp     byte [edi], ' '
    jne     startsWithoutSpace
    inc     edi
    dec     ebp
    jz      quit
    call    print
startsWithoutSpace:
    cmp     byte [edi+ebp-1], ' '
    je      endsWithSpace
    test    al, al      ; if print has been called, then we have 0x0a in eax
    jnz     undressLoop
quit:
    ret
endsWithSpace:
    dec     ebp
    jmp     undressLoopPrint
print:
    push    SYS_WRITE
    pop     eax
    push    eax
    xor     ebx, ebx
    inc     ebx ; STDOUT
    mov     ecx, edi
    mov     edx, ebp
    int     0x80
    pop     eax
    push    0x0a    ; will print newline
    mov     ecx, esp
    mov     edx, ebx ; STDOUT=1, which coincides with the length of newline
    int     0x80
    pop     eax
    ret
; --------------------- end undress ---------------------------------------
SYS_EXIT = 1
STDERR = 2
callUndress:
    pop     eax     ; argc
    cmp     eax, 2
    jne     badArgc
    pop     eax     ; argv[0]
    pop     edi
    mov     al, 0
    cld
    mov     ecx, -1
    repne   scasb
    lea     edi, [edi+ecx+1] ; argv[1]
    neg     ecx
    sub     ecx, 2
    mov     ebp, ecx     ; strlen(argv[1])
    call    undress
    xor     ebx, ebx
exit:
    mov     eax, SYS_EXIT
    int     0x80
    ud2
badArgc:
    mov     esi, eax
    mov     eax, SYS_WRITE
    mov     ebx, STDERR
    mov     ecx, badArgcMsg
    mov     edx, badArgcMsgLen
    int     0x80
    mov     ebx, esi
    neg     ebx
    jmp     exit
badArgcMsg:
    db      "Usage: undress YourString",0x0a,0
badArgcMsgLen = $-badArgcMsg
segment readable writable
string:
    db      100 dup(0)
    stringLen = $-string
\$\endgroup\$
9
  • \$\begingroup\$ sys_write() makes eax non-zero (specifically 1, the number of characters written, assuming it's not -errno), So will print if you don't pop eax at the end. You could just xor eax,eax before the cmp byte [edi], ' ' and save the mov al,1, and maybe some eax save/restore. Although you don't actually save it until after clobbering with SYS_WRITE. Hmm, instead of 0, you could use SYS_WRITE vs. 1, since cmp al, imm8 is the same size as test al,al. \$\endgroup\$ Commented Sep 24, 2017 at 22:23
  • \$\begingroup\$ Can you put a '\n' into the array with mov byte [ecx + edx], '\n' instead of doing the 2nd write()? (And decrement the length after printing?) Might save you a few instructions. \$\endgroup\$ Commented Sep 24, 2017 at 22:29
  • \$\begingroup\$ Actually, print() currently leaves '\n' in eax, which is different from SYS_WRITE, so you could still check that. I thought you were saving/restoring eax, but that was just saving bytes copying a constant around. For long strings, sys_write() can leave the high bytes of eax non-zero, so that unfortunately rules out just using mov al, SYS_WRITE. \$\endgroup\$ Commented Sep 24, 2017 at 23:05
  • \$\begingroup\$ @PeterCordes actually yes, mov al, 1 was extraneous. -2 bytes now, thanks. \$\endgroup\$
    – Ruslan
    Commented Sep 25, 2017 at 6:07
  • \$\begingroup\$ A register calling convention would save you the load instructions. In code-golf, a custom calling convention is normally fair game for asm. OTOH, if you'd rather golf the standard stack-args calling convention, that's interesting too. \$\endgroup\$ Commented Sep 25, 2017 at 12:41
2
\$\begingroup\$

PHP, 117 bytes

I add an extra space at the start so it will take the space out and show the original without any extra code.

Kinda new to this... would the <?php and the space at the start of the PHP file add 6 extra bytes or do I get that for free?

$s=" $argn";while($r!=$s){$r=$s;if($s[0]==" ")echo($s=substr($s,1))."
";if($s[-1]==" ")echo($s=substr($s,0,-1))."
";}

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Using your method 6 bytes can be reduced: Try it online! \$\endgroup\$
    – Night2
    Commented Aug 27, 2019 at 10:04
  • 1
    \$\begingroup\$ You can omit the PHP's opening tag since you can run it with a command like this: php -r "echo 1;" But if you want to use something like <?=1; you have to include the tag in bytes count. \$\endgroup\$
    – Night2
    Commented Aug 27, 2019 at 10:07
1
\$\begingroup\$

Pyth, 28 bytes

QW<lrKQ6lQ=hZ?&%Z2qdhQ=tQ=PQ

Try it here! or Verify all test cases!

Explanation

QW<lrKQ6lQ=hZ?&%Z2qdhQ=tQ=PQ   ~ Full program. Q is autoinitialized to input.

Q                              ~ Output the input.
 W<lrKQ6lQ                     ~ Loop while the condition is met.
  <                            ~ Is smaller?
   lrKQ6                       ~ The length of the original input, stripped on both sides.
        lQ                     ~ The length of the current Q.
          =hZ                  ~ Increment a variable Z, initially 0
             ?&%Z2qdhQ         ~ If Z % 2 == 1 and Q[0] == " ", then:
                      =tQ      ~ Make Q equal to Q[1:] and output, else:
                         =PQ   ~ Make Q equal to Q[:-1] and output.
\$\endgroup\$
1
\$\begingroup\$

Python 2, 79 bytes

-1 byte thanks to @JonathanFrech

f=lambda s,i=1:[s]+(s>i*'!'and'!'>s[-1]and f(s[:-1])or'!'>s and f(s[1:],0)or[])

Try it online!

The test suit replaces "." with " " before calling the function and replaces " " back to "." before printing the results for clarity.

\$\endgroup\$
1
  • \$\begingroup\$ '!'*i and -> i*'!'and. \$\endgroup\$ Commented Sep 21, 2017 at 12:12
1
\$\begingroup\$

C# - yet again, 125 bytes

while(s.Trim()!=s){if(s[0]==' '){yield return s=s.Substring(1);}if(s.Last()==' '){yield return s=s.Substring(0,s.Length-1);}}

Cheers!

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Laikoni
    Commented Sep 22, 2017 at 19:06
1
\$\begingroup\$

Octave, 89 bytes

s=input('');while any(s([1,end])<33)if s(1)<33,s(1)=[],end,if s(end)<33,s(end)=[],end,end

Try it online!

I'll add an explanation later, when I have the time. I might be able to golf off some bytes if I change the approach completely, but I can't see how unfortunately.

The last letters here spell out: "sendsendendend". I wish there was a way to store end as a variable and use that, but guess what ...

\$\endgroup\$
2
  • \$\begingroup\$ Is it valid to output with s = ...? (The usual question, I know) \$\endgroup\$
    – Luis Mendo
    Commented Sep 21, 2017 at 23:24
  • \$\begingroup\$ Anyway, see if you can remove a couple of bytes :-P \$\endgroup\$
    – Luis Mendo
    Commented Sep 21, 2017 at 23:36
1
\$\begingroup\$

Bash, 98 94 bytes

Saved 4 bytes using subshell instead of sequences (poor performances)

r()(s=$1;[[ $s = $b ]]||([[ $s = $a ]]||echo "$s"
b=$a a=$s;((i=!i))&&r "${s# }"||r "${s% }"))

First answer

r(){ s=$1;[[ $s = $b ]]||{ [[ $s = $a ]]||echo "$s"
b=$a a=$s;((i=!i))&&r "${s# }"||r "${s% }";};}

Note the ! must be escaped in interactive mode

\$\endgroup\$
0

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.