12
\$\begingroup\$

In MATLAB and Octave, the default output looks like this (I guess this is relevant for other languages as well):

'This is a string'

ans =

This is a string

x = 'This is a string'

x =

This is a string

Using disp will give:

disp('This is a string')
This is a string

There are, as far as I can tell, (at least) three types of challenges where this is relevant:

  1. The output should look exactly like this (maybe newlines and trailing spaces are OK)
    • This one is bankers: ans = ... is not OK
  2. Remove elements, remove letters, etc. from an input string.
    • I believe ans = ... should be OK, since the output format is not specified.
  3. "Output: A is greater than B if A > B", copy from STDIN to STDOUT
    • ?

What do we do when the question simply states:

  • "Output a string"
  • "Print to STDOUT"

This is quite important, since disp('String') is much longer than just String. Also, for those cases where ans = ... is OK, will x = ... also be OK?


Update:

There are some cases that have come up recently:

  1. The following function returns no arguments, but will write x = 1 2 3 4 5 in the command window:

    function f;x=1:5
    

    I'm assuming this is disallowed?

  2. The following is not a function, but also doesn't use disp. It results in the integers 1-10 being printed like i = 1, i = 2 etc. clearing the screen between each new number.

    for i=1:10,pause(1);clc;i,end
    

    I'm assuming this is disallowed?

\$\endgroup\$
  • 1
    \$\begingroup\$ This can also apply to ex. Ruby. p"Hello, World!" saves three chars over puts"Hello, World!" but outputs the string with quotes around it (and would also escape any backslashes, quotes, etc. in the string if there were any). \$\endgroup\$ – Doorknob Nov 2 '15 at 21:28
  • \$\begingroup\$ Note that is a REPL output, which is invalid anyway. :P \$\endgroup\$ – Rɪᴋᴇʀ Apr 28 '16 at 19:11
  • \$\begingroup\$ Also Mathematica's Echo which could be used instead of Print if not for the >> \$\endgroup\$ – Pavel Dec 10 '16 at 2:53
  • \$\begingroup\$ On this answer, I raise something similar in my new language. \$\endgroup\$ – caird coinheringaahing Mar 28 '17 at 21:10
9
\$\begingroup\$

"Output a string" typically implies that it can be returned from a function, in which case extraneous output like ans = and x = is irrelevant. But in any situation in which a full program is used, be it required or by choice, or a function that prints to STDOUT rather than returning the value is used, disp is required.

\$\endgroup\$
  • \$\begingroup\$ Not sure if I understand where the limit goes here... -in your opinion, is it OK to have ans = cat goes "Meow" in the cat-challenge? Is it OK to have x = cat ...? \$\endgroup\$ – Stewie Griffin Nov 2 '15 at 16:35
  • \$\begingroup\$ A function never prints to STDOUT without the prefix ans = ..., unless the function is disp() of course. \$\endgroup\$ – Stewie Griffin Nov 2 '15 at 16:44
  • 1
    \$\begingroup\$ @StewieGriffin Right. I mean that ans = and x = shouldn't be there if the function is supposed to print something rather than return something. \$\endgroup\$ – Alex A. Nov 2 '15 at 16:45
  • \$\begingroup\$ Ok, just a little final clarification: In the A is greater than B challenge, ans = ... is OK. \$\endgroup\$ – Stewie Griffin Nov 2 '15 at 16:50
  • 2
    \$\begingroup\$ @StewieGriffin It appears in that case you opted for a full program rather than a function, so I would say that ans shouldn't be there. \$\endgroup\$ – Alex A. Nov 3 '15 at 0:25
  • \$\begingroup\$ I've added an update to the question. What's right here (in your opinion)? \$\endgroup\$ – Stewie Griffin Dec 10 '16 at 0:29
  • \$\begingroup\$ @StewieGriffin If I'm not mistaken, both of the examples you cite in the update rely on Matlab being run in the REPL, right? That is, neither would print if the program were run as a script instead? \$\endgroup\$ – Alex A. Dec 10 '16 at 19:53
  • \$\begingroup\$ MATLAB is both a language and a program. You don't (normally) run a MATLAB script/function from outside the GUI (or CLI if Octave). Functions and "regular scripts" behaves the same way in MATLAB, the (main) difference is that variables inside a function are private. A line such as: x=1 that's not terminated by a semicolon will print x = 1 to the command window either way. The difference is, if it's a function and you don't explicitly return it then you can't use x to anything afterwards. \$\endgroup\$ – Stewie Griffin Dec 10 '16 at 20:50
  • \$\begingroup\$ @StewieGriffin Thanks for the explanation. I'd be inclined to say that the examples you give in the post should be okay then. \$\endgroup\$ – Alex A. Dec 13 '16 at 3:46
4
\$\begingroup\$

This depends on how strict the output requirements of the challenge are.

  • Some challenges have very relaxed requirements on the output format. E.g. some just specify "any convenient, unambiguous format". In those cases, including ans = or x = in the output is fair game as it does in no way obscure the actual output and is quite a natural output for Matlab as well.
  • Other challenges require a very exact output format, e.g. a specific string (think "Hello, World!"). Those will usually state that the output must contain nothing except what is specified. In this case ans = and x = are not fine unless they happen to match exactly the required output format.
  • Some challenges will require such an exact output format, but make an exception for any fixed interpreter output which cannot possibly be suppressed. This exception would not include Matlab, as ans = and x = can be suppressed using ; and printing with disp or similar instead.
\$\endgroup\$
  • \$\begingroup\$ Apologies if this is a stupid question, but what to do in challenges such as Write a "program that copies all STDIN to STDOUT, but removes capital letters"? \$\endgroup\$ – Stewie Griffin Nov 2 '15 at 17:05
  • \$\begingroup\$ @StewieGriffin Such a challenge will probably specify that there should be no extraneous output, so you'd have to use disp. If the challenge does not specify that, you might want to ask for clarification. \$\endgroup\$ – Martin Ender Nov 2 '15 at 17:15
  • 2
    \$\begingroup\$ I don't like reading into the exact wording used by the poster to determine what's allowed. Chances are the choice of phrase for the output is incidental, and in any case, made without thought as to this issue. In your examples, the phrasing is very specific, but I imagine few posts would go into that level of detail of how they want the output, nor would I want to have to do so in writing a challenge. I'd prefer any consistent default ruling over this. \$\endgroup\$ – xnor Nov 2 '15 at 21:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .