66
\$\begingroup\$

COBOL is a very old language, at the time of writing it is 58 years old. It is so old, in fact, that it has a very interesting quirk: the first six characters of each line are comments.

Why is this, you ask? Well, those 6 characters were intended to be used as line numbers, back in the day where programs weren't completely digital and typed out on a computer.

In addition, the seventh character could only be part of a very small set (it is usually * to comment out the line or a space to separate the line number from the code)

But what if you're on a more digital system, and you just want the raw program?

The comment system

There are two types of comments in COBOL: line comments and the aforementioned "line number" comments.

Uncommenting line numbers is simple: just take the first seven (six plus a single space) characters off each line.

000000 apple
000001 banana
celery donuts

would become:

apple
banana
donuts

Line comments make it a bit more difficult. A line comment is started with an asterisk * placed in the seventh character position on the line, like so:

000323* this is a comment

This is not a line comment:

*00000 this isn't a comment

To uncomment a line comment, just remove the whole line.

An example commented "program":

000000 blah blah
000001* apples
000002 oranges?
000003* yeah, oranges.
000*04 love me some oranges

The uncommented version:

blah blah
oranges?
love me some oranges

In other words, to uncomment a string, remove the first six characters of each line, then return all but the first character of every line that does not begin with a star.

The challenge

Create a program or function that takes a commented program and returns its uncommented variant.

Clarifications

  • Asterisks (*) will never be found anywhere outside the first seven characters on a line (we're not asking you to verify syntax)
  • Each line will always have at least 7 characters.
  • You may assume the seventh character is always an asterisk or a space.
  • Input or output may be a matrix or list.
  • Only printable ASCII characters (plus newline) must be handled.
  • You may output with a trailing newline. You may also assume that the input will have a trailing newline, if you so choose.

Scoring

Since this is , the answer with the least bytes wins!

DISCLAIMER: I do not actually know COBOL and do not claim to. If any of the claims about COBOL I have made in this question are incorrect, I take no responsibility.

\$\endgroup\$
  • 23
    \$\begingroup\$ Line numbers are not comments. They are a column. Terminology please. \$\endgroup\$ – user207421 Aug 24 '17 at 5:47
  • 2
    \$\begingroup\$ Your examples all have a space after the *. Is this a coincidence? \$\endgroup\$ – Neil Aug 24 '17 at 10:13
  • 1
    \$\begingroup\$ @Neil Yes, it is. The eighth character can be anything. \$\endgroup\$ – LyricLy Aug 24 '17 at 10:17
  • 6
    \$\begingroup\$ Old does not automatically imply bad. I have worked in an Agile COBOL shop. They could do things on the AS/400 we could not do in Java. \$\endgroup\$ – Thorbjørn Ravn Andersen Aug 24 '17 at 12:06
  • 4
    \$\begingroup\$ Can there be a space in the first 6 characters? \$\endgroup\$ – user4768 Aug 24 '17 at 15:30

40 Answers 40

103
\$\begingroup\$

COBOL (GnuCOBOL), 191 + 17 = 208 bytes

I "learned" COBOL for this answer, so it's probably not fully golfed.

This is a full program, taking input on what I presume to be standard input and writing to what I presume to be standard output. Perhaps one day I'll return to this and (1) determine whether COBOL has functions and, if so, (2) see whether a function solution would be shorter.

Byte count includes program and compiler flags (-free and -frelax-syntax).

program-id.c.select i assign keyboard line sequential.fd i. 1 l pic X(80). 88 e value 0.open input i perform until e read i end set e to true end-read if not e and l(7:1)<>'*'display l(8:73).

Try It Online

Ungolfed program

program-id. c.

select i assign to keyboard organization line sequential.

fd i.
    1 l pic X(80).
    88 e value 0.

open input i
perform until e
    read i
        end set e to true
    end-read
    if not e and l(7:1) <> '*'
        display l(8:73).

Limitations

The output is, technically speaking, not correct. From my cursory research, it seems the only practical way to store a string in COBOL is in a fixed-size buffer. I have chosen a buffer size of 80 characters, since this is the line length limit for fixed-format programs. This presents two limitations:

  • Lines longer than 80 characters are truncated.
  • Lines shorter than 80 characters are right-padded with spaces.

I'm guessing this is acceptable since, well, it's COBOL. If not, I'd be willing to look into alternatives.

Acknowledgments

  • -166 bytes thanks to Edward H
  • -2 bytes thanks to hornj
\$\endgroup\$
  • 10
    \$\begingroup\$ Asterisks (*) will never be found anywhere outside the first seven characters on a line ... and yet ... ;) \$\endgroup\$ – Cœur Aug 24 '17 at 17:43
  • \$\begingroup\$ @Cœur Haha yes...but my solution doesn't use that assumption, so maybe it's appropriate! \$\endgroup\$ – Jakob Aug 24 '17 at 17:50
  • 8
    \$\begingroup\$ You win one internet. \$\endgroup\$ – Joshua Aug 25 '17 at 18:58
  • \$\begingroup\$ @Cœur except in a COMPUTE statement. \$\endgroup\$ – ClickRick Aug 25 '17 at 19:21
  • 1
    \$\begingroup\$ Congratulations on your gold badge! \$\endgroup\$ – caird coinheringaahing Dec 21 '17 at 17:25
20
\$\begingroup\$

Python 2, 39 38 37 bytes

-1 byte thanks to LyricLy. -1 byte thanks to Mego.

lambda s:[i[7:]for i in s if'*'>i[6]]

Try it online!

I/O as lists of strings.

\$\endgroup\$
  • 2
    \$\begingroup\$ Save a byte by replacing != with <, since a space's code point is lower than an asterisk's one, and the seventh character will always be a space or an asterisk. \$\endgroup\$ – LyricLy Aug 24 '17 at 2:34
  • \$\begingroup\$ So, the 7th character will always be a space or an asterisk? \$\endgroup\$ – totallyhuman Aug 24 '17 at 2:35
  • \$\begingroup\$ Yes. You may assume the seventh character is always an asterisk or a space. \$\endgroup\$ – LyricLy Aug 24 '17 at 2:37
  • 1
    \$\begingroup\$ Save 1 byte with if'*'!=i[6] \$\endgroup\$ – Mego Aug 24 '17 at 2:43
13
\$\begingroup\$

Perl 5, 19 + 1 (-p) = 20 16 bytes

-4 bytes with Pavel's suggestions

s/.{6}( |.*)//s

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ You can save three bytes if you replace (\*.*$| ) with ( |.*) \$\endgroup\$ – Pavel Aug 24 '17 at 6:10
  • \$\begingroup\$ Not as short as @Pavel's comment, but / /;$_=/\* /?$,:$' is another alternative \$\endgroup\$ – Dom Hastings Aug 24 '17 at 7:35
  • \$\begingroup\$ You also don't need the ^. \$\endgroup\$ – Pavel Aug 24 '17 at 7:39
11
\$\begingroup\$

V, 13 11 10 bytes

Î6x<<
çª/d

Try it online!

Explanation

Î       ' On every line
  x     ' delete the first...
 6      ' 6 characters
   <<   ' and unindent the line (removes the leading space)
ç /     ' on every line
 ª      ' matching \*
   d    ' delete the line

Hexdump:

00000000: ce36 783c 3c0a e7aa 2f64                 .6x<<.../d
\$\endgroup\$
  • \$\begingroup\$ Couldn't you do 7x instead of 6x<<? \$\endgroup\$ – DJMcMayhem Aug 24 '17 at 2:43
  • 1
    \$\begingroup\$ Then it deletes the * \$\endgroup\$ – nmjcman101 Aug 24 '17 at 2:43
  • \$\begingroup\$ Would it work to delete the lins with * first and then do Î7x ? (assuming a * can not not in the chars 0-5) \$\endgroup\$ – 12431234123412341234123 Aug 28 '17 at 11:00
  • \$\begingroup\$ @12431234123412341234123 unfortunately no, because there can be a * in the first 6 characters. \$\endgroup\$ – nmjcman101 Aug 28 '17 at 14:37
9
\$\begingroup\$

Paradoc (v0.2.8+), 8 bytes (CP-1252)

µ6>(7#;x

Takes a list of lines, and results in a list of uncommented lines.

Explanation:

μ        .. Map the following block over each line (the block is terminated
         .. by }, but that doesn't exist, so it's until EOF)
 6>      .. Slice everything after the first six characters
   (     .. Uncons, so now the stack has the 6th character on top
         .. and the rest of the line second
    7#   .. Count the multiplicity of factors of 7 in the character
         .. (treated as an integer, so '*' is 42 and ' ' is 32)
      ;  .. Pop the top element of the stack (the rest of the line)...
       x .. ...that many times (so, don't pop if the 6th character was a
         .. space, and do pop if it was an asterisk)

Hi, I wrote a golfing programming language. :)

I'm still developing this and added/tweaked a bunch of built-ins after trying to write this so that there are more reasonable ways to differentiate between a space and an asterisk than "7#", but I feel like that would make this noncompeting. It's fortunate that it still worked out (this only uses features from v0.2.8, which I committed three days ago).

\$\endgroup\$
  • \$\begingroup\$ "Hi, I wrote a golfing programming language." Was the version you're using released before or after this challenge was posted? \$\endgroup\$ – Mast Aug 24 '17 at 19:18
  • 1
    \$\begingroup\$ It works on this version from three days ago: github.com/betaveros/paradoc/releases/tag/v0.2.8 \$\endgroup\$ – betaveros Aug 24 '17 at 19:23
  • \$\begingroup\$ Right, you mentioned that, but somehow it didn't register explicitly... \$\endgroup\$ – Mast Aug 24 '17 at 19:38
  • 1
    \$\begingroup\$ @Mast It doesn't really matter. \$\endgroup\$ – Martin Ender Aug 25 '17 at 14:09
7
\$\begingroup\$

Octave, 23 bytes

@(s)s(s(:,7)~=42,8:end)

Try it online!

\$\endgroup\$
  • 4
    \$\begingroup\$ I never knew Octave could do strings like that... Take that, MATLAB. xD \$\endgroup\$ – Sanchises Aug 24 '17 at 7:47
  • \$\begingroup\$ Since R2016b's introduction of string arrays, I'm pretty sure this would work in MATLAB too @Sanchises! I only have access to R2015b currently so can't confirm though. MATLAB can do it in 74 bytes, probably fewer @(s)cellfun(@(r)r(8:end),s(cellfun(@(r)r(7)~=42,s)),'uniformoutput',false), where s is a cell array, not a string array. Using regexp or something would likely be shorter, but the method I've written is comparable to the methodology in this answer just for old MATLAB \$\endgroup\$ – Wolfie Aug 25 '17 at 15:51
6
\$\begingroup\$

Jelly, 11 9 bytes

ṫ€7Ḣ⁼¥Ðf⁶

Try it online!

Inputs and outputs as a list of lines.

-2 bytes thanks to @EriktheOutgolfer and @JonathanAllan

How it works

ṫ€7Ḣ=¥Ðf⁶
 €           On each line:
ṫ 7            Replace the line with line[7:]
      Ðf     Keep all lines that meet condition:
     ¥         Dyad:
   Ḣ             First Element (modifies line)
    =            Equals
        ⁶    Space
\$\endgroup\$
  • \$\begingroup\$ 7$€ can be €7 \$\endgroup\$ – Erik the Outgolfer Aug 24 '17 at 9:29
  • \$\begingroup\$ take it down to 9 like so: ṫ€7Ḣ⁼¥Ðf⁶ \$\endgroup\$ – Jonathan Allan Aug 24 '17 at 12:40
5
\$\begingroup\$

PowerShell, 32 bytes

$input-replace'^.{6}( |.*)'-ne''

Try it online!

Pipeline input comes in as an array of strings, -replace works on every string, and -ne '' (not equal to empty string) applied to an array, acts to filter out the blank lines.

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

C, 63 59 55 48 47 46 bytes

Thanks to "an anonymous user" for getting rid off yet another byte.

Thanks to Felix Palmen for reminding me of "You may assume the seventh character is always an asterisk or a space.", which shaved off one more byte.

f(char**a){for(;*a;++a)(*a)[6]&2||puts(*a+7);}

Use like:

char** program = { "000000 apple", "000001 banana", "celery donuts", 0 };
f(program);

Try it online!

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

Actually, 13 bytes

⌠6@tp' =*⌡M;░

Input and output is done as a list of strings.

Explanation:

⌠6@tp' =*⌡M;░
⌠6@tp' =*⌡M    for each line:
 6@t             discard the first 6 characters
    p            pop the first character of the remainder
     ' =         is it a space?
        *        multiply the string by the boolean - returns the string if true, and an empty string if false
           ;░  filter out empty strings

Try it online!

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

Gaia, 9 bytes

6>¦'*«⁈ḥ¦

A function accepting a list of strings and returning a list of strings.

Try it online!

Explanation

6>¦        Remove the first 6 characters of each string
   '*«⁈    Filter out ones that start with *
       ḥ¦  Remove the initial space from each
\$\endgroup\$
  • \$\begingroup\$ I count ten characters, and since three are non-ASCII, do they not take more than a byte? \$\endgroup\$ – WGroleau Aug 24 '17 at 6:35
  • \$\begingroup\$ @WGroleau and « are both 1 character. Out golfing languages that use non-ascii character (maybe except for Neim) use custom encodings, that allow all those non-ASCIIs to be counted as single bytes. Here is Gaia's code page. \$\endgroup\$ – Mr. Xcoder Aug 24 '17 at 9:19
  • \$\begingroup\$ @Mr.Xcoder Neim has an encoding too. \$\endgroup\$ – Erik the Outgolfer Aug 24 '17 at 9:58
3
\$\begingroup\$

Pyth, 9 bytes

Note that this only works if at least 1 line is not a comment and at least 1 line is a comment. All the other solutions work in all cases.

-2 bytes thanks to @pizzakingme!

m>d7.m@b6

Try it here!

Explanation

m>d7.m@b6     - Full program with implicit input. Takes input as a list of Strings.

m>d7          - All but the first 7 letters of 
    .m   (Q)  - The input, filtered for its minimal value using the < operator on
      @b6     - the 7th character -- note that "*" is greater than " "
              - Implicitly Output the result.

Pyth, 11 bytes

tMfqhTdm>d6

Try it here!

Explanation

tMfqhTdm>d6 - Full Program with implicit input. Takes input as a list of Strings.

       m>d6 - Remove the first 6 characters of each line.
    hT      - Get the first character of each.
  fq  d     - Keep those that have the first character an asterisk.
tM          - Remove the first character of each.
            - Output Implicitly.

Pyth, 11 bytes

m>d7fqd@T6Q

Try it here!

Explanation

m>d7fq@T6dQ  - Full program. Takes input as a list of Strings.

      @T6    - The sixth character of each.
    fq   dQ  - Keep the lines that have a space as ^.
m>d7         - Crop the first 7 characters.
             - Output implicitly.

Pyth, 12 bytes

tMfnhT\*m>d6

Try it here!

Explanation

tMfnhT\*m>d6 - Full Program with implicit input. Takes input as a list of Strings.

        m>d6 - Remove the first 6 characters of each line.
    hT       - Get the first character of each.
  fn  \*     - Filter those that aren't equal to an asterisk.
tM           - Remove the first character of each.
             - Output Implicitly.

Pyth, 12 bytes

m>d7fn@T6\*Q

Try it here!

Explanation

m>d7fn@T6\*Q  - Full program. Takes input as a list of Strings.

      @T6     - Get the sixth character of each string
    fn   \*Q  - And filter those that aren't equal to an asterisk.
m>d7          - Crop the first 7 characters.
              - Output implicitly.
\$\endgroup\$
  • \$\begingroup\$ "Keep those that have the first character an asterisk." I think you meant "Keep those of which the first character is NOT an asterisk." \$\endgroup\$ – Kevin Cruijssen Aug 24 '17 at 13:25
  • \$\begingroup\$ m>d7.m@b6 should work at 9 bytes, abusing that * is after space in lexicographical order \$\endgroup\$ – Dave Aug 24 '17 at 13:27
  • \$\begingroup\$ I can edit it in with explanation / test link if you want! \$\endgroup\$ – Dave Aug 24 '17 at 13:36
  • \$\begingroup\$ @pizzakingme I'd be glad if you would edit in, because I am on mobile. Thanks a lot and don't forget to credit yourself for the new solution! \$\endgroup\$ – Mr. Xcoder Aug 24 '17 at 13:40
  • \$\begingroup\$ Does this work if all lines are line comments? (I'm not sure if you have to handle this case) \$\endgroup\$ – betaveros Aug 24 '17 at 18:50
3
\$\begingroup\$

GNU Sed, 19 + 2 = 21 characters

Requires -E argument to sed to enable extended regular expressions.

/^.{6}\*/d;s/^.{7}/
\$\endgroup\$
  • \$\begingroup\$ you could do the same thing as the perl guy, s/^.{6}( |.*)//g \$\endgroup\$ – markasoftware Aug 24 '17 at 21:01
3
\$\begingroup\$

Java 8, 40 bytes

Regular expressions: just about, but not quite, the wrong tool for the job. Lambda from String to String (assign to Function<String, String>). Input must have a trailing newline.

s->s.replaceAll("(?m)^.{6}( |.*\\n)","")

Try It Online

Acknowledgments

\$\endgroup\$
  • \$\begingroup\$ The right tool! :) \$\endgroup\$ – Olivier Grégoire Aug 25 '17 at 8:41
3
\$\begingroup\$

Haskell, 27 25 bytes

Laikoni's version is shorter than mine:

f n=[x|' ':x<-drop 6<$>n]

Try it online!

My version:

f n=[drop 7x|x<-n,x!!6<'*']

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 25 bytes: f n=[x|' ':x<-drop 6<$>n]. \$\endgroup\$ – Laikoni Aug 24 '17 at 17:24
  • \$\begingroup\$ @Laikoni That's nice!! I didn't know it was possible to match patterns in a generator of a list comprehension. \$\endgroup\$ – jferard Aug 24 '17 at 19:43
3
\$\begingroup\$

C (gcc), 53 48 46 bytes

x;main(y){for(y=&x;gets(y-6);x&2||puts(y+1));}

Try it online!

-5 bytes: It was very tricky to get this "whole program" down to the same size as gurka's function. It's now writing out of bounds (in both directions) of an array of wrong type and relies on little endian and 4 byte integers to find the asterisk ... but hey, it works ;)

-2 bytes: Well, if we already write to some "random" .bss location, why bother declaring an array at all! So here comes the string handling program that uses neither the char type nor an array.

\$\endgroup\$
  • \$\begingroup\$ Nice! And the *x&2 made me remember "You may assume the seventh character is always an asterisk or a space.", so I should be able to shave some bytes off my answer :-) \$\endgroup\$ – simon Aug 25 '17 at 18:56
  • \$\begingroup\$ @gurka thanks :D -2, hehe \$\endgroup\$ – Felix Palmen Aug 26 '17 at 5:39
3
\$\begingroup\$

R, 47 45 bytes

function(x)gsub("(?m)^.{6}( |.*\\n)","",x,,T)
\$\endgroup\$
  • \$\begingroup\$ If you take the input as a list of strings I think you can shorten the regex to "^.{6}( |.*$)" for -6. \$\endgroup\$ – CriminallyVulgar Aug 24 '17 at 11:37
  • \$\begingroup\$ @CriminallyVulgar Correct. In this case, I could also drop the pe=T argument. However, I am not sure whether input as a list of strings is allowed. \$\endgroup\$ – Sven Hohenstein Aug 24 '17 at 12:18
  • \$\begingroup\$ From the OP: Input or output may be a matrix or list. \$\endgroup\$ – totallyhuman Aug 24 '17 at 13:05
  • \$\begingroup\$ @CriminallyVulgar The problem is the presence of empty strings in the output. \$\endgroup\$ – Sven Hohenstein Aug 24 '17 at 13:24
  • \$\begingroup\$ @SvenHohenstein Ah of course, I hadn't considered that. \$\endgroup\$ – CriminallyVulgar Aug 24 '17 at 14:57
3
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 72 70 66 50 bytes

R	INPUT POS(6) (' '  REM . OUTPUT | '*') :S(R)
END

Try it online!

Pattern matching in SNOBOL is quite different from regex but the idea here is still the same: If a line matches "six characters and then an asterisk", remove it, otherwise, remove the first seven characters of the line and print the result.

This now actually takes better advantage of SNOBOL's conditional assignment operator.

The pattern is POS(6) (' ' REM . OUTPUT | '*') which is interpreted as:

Starting at position 6, match a space or an asterisk, and if you match a space, assign the rest of the line to OUTPUT.

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

Vim, 14 bytes

Ctrl-VG5ld:%g/\*/dEnter

Loading the input file as the buffer to edit, then enter the above commands. Output is the new buffer.

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

Ruby, 39 38 36 29 23 22 20 + 1 = 21 bytes

$_[/.{6}( |.*
)/]=''

Try it online!

Uses -p flag.

Explanation:

The -p flag adds an implicit block around the code, so the code that actually gets run looks like this:

while gets
    $_[/.{6}( |.*
)/]=''

    puts $_
end

gets reads a line of text and stores its result in $_.

$_[/.../]='' removes the first occurence of the regex ... in $_.

/.{6}( |.*\n)/ matches 6 of any character at the start of a line, followed by either a space or the rest of the line. Because the space appears first, it will try to remove only the first 6 characters and a space before attempting to remove the entire line.

$_ is then printed out, and this process is repeated for each line.

\$\endgroup\$
  • 1
    \$\begingroup\$ Method calls in Ruby don't need parentheses, removing them will save you a byte. \$\endgroup\$ – m-chrzan Aug 24 '17 at 3:07
2
\$\begingroup\$

Pyke, 9 bytes

36 4C 3E 23 64 DE 29 6D 74

Try it here!

Readable:

6L>#d.^)mt

Try it here!

6L>        -   [i[6:] for i in input]
   #d.^)   -  filter(i.startswith(" ") for  i in ^)
        mt - [i[-1:] for i in ^]
\$\endgroup\$
2
\$\begingroup\$

Retina, 23 15 bytes

5 bytes saved thanks to nmjcman101
1 byte saved thanks to Neil

m`^.{6}( |.*¶)

Try it online!

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

JavaScript (ES6), 48 bytes

s=>s.map(c=>c[6]<"*"?console.log(c.substr(7)):1)

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ This is neither a function, nor a full program as it assumes the input is stored in z, which isn't allowed here. You could however turn it into an anonymous arrow function, in order to make it valid. \$\endgroup\$ – caird coinheringaahing Aug 27 '17 at 11:35
  • 1
    \$\begingroup\$ @cairdcoinheringaahing you're absolutely right. Updated the solution - not sure what the rules are regarding the ( and ) surrounding the fn, added them to be sure. \$\endgroup\$ – sgtdck Aug 27 '17 at 13:03
  • 1
    \$\begingroup\$ You don't need the () around the function, but otherwise it looks fine. \$\endgroup\$ – caird coinheringaahing Aug 27 '17 at 13:06
2
\$\begingroup\$

><>, 57 53 Bytes

>i~i~i~i~i~i~i67*=\
<o$/?:$/?=a:;?(0:i<
\~$/~\ $
/  <o\?/

try it online

Explanation

>i~i~i~i~i~i~i67*=    Read in the first seven bytes of the line
 i~i~i~i~i~i~         Read, and discard 6 characters
             i        Read the seventh
              67*=    Check if the seventh character was an 
                      asterisk (and leave that value on the stack );

<o$/?:$/?=a:;?(0:i<    Read characters until a newline or eof
                 i     Read the next character of the line
            ;?(0:      If it's a -1, terminate the program
       /?=a:           If it's a newline, break out of the loop
   /?:$                If the seventh character was not an asterisk
<o$                    Output this character
\~$/                   otherwise discard it

   /~\ $    Having reached the end of the line, output
/  <o\?/    the newline only if it was not a comment

Edit: 53 bytes

>   i~i~i~i~i~i~i67*=\
/?=a<o/?$@:$@:$;?(0:i<
~   \~/

Basically the same stuff as before, but restructured slightly

As a side note: I'm disappointed no-one's done this in cobol yet.

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

C#, 160 145 90 89 bytes

t=>{var o="";foreach(var s in i.Split('\n'))if(s[6]!=42)o+=s.Substring(7)+"\n";return o;}

Thanks to Pavel & auhmaan for reducing the size.

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! I would suggest adding a try it online link to your answer so that others can test your code. Besides that, great first (well, second) answer! \$\endgroup\$ – LyricLy Aug 24 '17 at 3:52
  • \$\begingroup\$ You can make this shorter by writing a lambda in the form t=>{...} \$\endgroup\$ – Pavel Aug 24 '17 at 5:46
  • \$\begingroup\$ @LyricLy I tried doing that, actually, but for some reason, this doesn't work there. It works perfectly fine in a VS console app, though. \$\endgroup\$ – snorepion Aug 24 '17 at 13:07
  • \$\begingroup\$ @Pavel Like so? I'm not sure if I did it completely correctly; I've never needed to use a lambda expression before. \$\endgroup\$ – snorepion Aug 24 '17 at 13:08
  • \$\begingroup\$ Yes, exactly. You can test it by assigning it to a func<string, string>. \$\endgroup\$ – Pavel Aug 24 '17 at 16:36
2
\$\begingroup\$

Python 3, 71 bytes (no regexp)

def f(s):
 for w in s.split('\n'):
  t=w[6:]
  if t[0]!='*':print(t[1:])

It works!

>>> s="""000000 blah blah
000001* apples
000002 oranges?
000003* yeah, oranges.
000*04 love me some oranges"""
>>> f(s)
blah blah
oranges?
love me some oranges
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 11 bytes

|ʒ6èðQ}ε7F¦

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 10 bytes: |ε6.$ćðQi, \$\endgroup\$ – Grimmy Sep 25 at 12:59
  • \$\begingroup\$ @Grimy Heh, .$ didn't exist back then. :P \$\endgroup\$ – Erik the Outgolfer Sep 25 at 13:13
1
\$\begingroup\$

JavaScript, 44 34 bytes

Crossed-out 44 is still regular 44.

6 bytes saved thanks to tsh

a=>a.replace(/^.{6}( |.*\n)/gm,'')

Try it online!

\$\endgroup\$
  • \$\begingroup\$ s=>s.replace(/^.{6}( |\*.*\s)?/mg,'') \$\endgroup\$ – tsh Aug 24 '17 at 2:44
  • \$\begingroup\$ s.match(/(?<=^.{6} ).*/mg) ESNext (Nonstandard, Stage3) Chrome62+ \$\endgroup\$ – tsh Aug 24 '17 at 2:52
  • \$\begingroup\$ @tsh. Until there is a stable interpreter which allows it, I suppose it doesn't count as a valid programming language. \$\endgroup\$ – user72349 Aug 24 '17 at 3:02
  • \$\begingroup\$ It doesn't seem like this gives the correct output if the last line is a comment line. \$\endgroup\$ – LyricLy Aug 24 '17 at 3:42
  • \$\begingroup\$ @LyricLy. It is because I assumed that input will always contain a trailing new line. You can see that it works is there is an empty line after the input. If I should not assume it, then fix will cost 1 byte (adding ? after \n). \$\endgroup\$ – user72349 Aug 24 '17 at 3:55
1
\$\begingroup\$

C++ (GCC), 121 112 bytes

Thanks to @gurka for saving 9 bytes!

#import<bits/stdc++.h>
void f(std::list<std::string>l){for(auto s:l)if(s[6]-42)std::cout<<s.substr(7,s.size());}

Takes input as a list of lines.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ #import? Also, I think that it's OK to omit standard includes. \$\endgroup\$ – simon Aug 24 '17 at 14:05
  • \$\begingroup\$ #import isn't standard C++, but at least GCC and MSVC support it. Omitting some includes works with C, but not with C++. The code doesn't work without the includes, so they have to be counted in the total bytecount. \$\endgroup\$ – Steadybox Aug 24 '17 at 14:15
  • \$\begingroup\$ Aha, I thought you could just skip includes since you don't see any import in python answers or using in C# answers. Also, wouldn't #include <bits/stdc++.h> be shorter for your answer? \$\endgroup\$ – simon Aug 24 '17 at 14:26
  • \$\begingroup\$ @gurka Yes, it would be shorter. Thanks! \$\endgroup\$ – Steadybox Aug 24 '17 at 14:53
  • \$\begingroup\$ @gurka the imports are counted in Python answers, it's just that Python has a lot of functions that don't need importing. C# tends to not have using statement because it's generally shorter to write system.foo() than using system;foo() \$\endgroup\$ – Pavel Aug 24 '17 at 19:45
1
\$\begingroup\$

Java 8, 95 54 53 bytes

s->s.filter(x->x.charAt(6)<33).map(x->x.substring(7))

-42 bytes thanks to @OliverGrégoire, by using a Stream<String> instead of String as in- and output.

Explanation:

Try it here.

s->                          // Method with Stream<String> as parameter and return-type
  s.filter(x->x.charAt(6)<33)//  Filter out all lines containing an asterisk as 7th char
   .map(x->x.substring(7))   //  And remove the first 7 characters from the remaining lines
                             // End of method (implicit / single-line body)
\$\endgroup\$
  • \$\begingroup\$ Looks like you can use a String[] or List<String> as input for -12 bytes. \$\endgroup\$ – Jakob Aug 24 '17 at 17:06
  • \$\begingroup\$ Or Stream<String> if that can help. Example : s->s.map(x->x.charAt(6)!=42?x.substring(7):"") \$\endgroup\$ – Olivier Grégoire Aug 24 '17 at 23:28
  • 1
    \$\begingroup\$ Oh, it's needed to filter... then s->s.filter(x->x.charAt(6)!=42).map(x->x.substring(7)) for 54 bytes. \$\endgroup\$ – Olivier Grégoire Aug 25 '17 at 7:28
  • 1
    \$\begingroup\$ Use <42 instead of !=42 because "You may assume the seventh character is always an asterisk or a space." \$\endgroup\$ – Olivier Grégoire Aug 25 '17 at 10:15
  • 1
    \$\begingroup\$ @OlivierGrégoire Ah, missed that rule, otherwise I would have done so myself. Thanks for the correction. \$\endgroup\$ – Kevin Cruijssen Aug 25 '17 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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