19
\$\begingroup\$

In the language dc, the - character is reserved strictly for subtraction, so it cannot be used to enter a negative number. An alternative format for entering negative numbers was created: precede the number with an underscore, as in _5. Many challenges require submissions to accept negative input.

This may be a trivial question, but I want to know whether dc is allowed to take negative input with underscores rather than minuses. As an example, in my submission for the challenge Calculate the sum of ILD, I left a buffer of numbers waiting on the stack to receive a leading - and report back whether the number was negative. Instead, I'd prefer to just assume that the number will be input as negative in the manner recognised by the interpreter. (I don't intend to change that particular submission; I'm just asking for future reference.)

\$\endgroup\$
  • \$\begingroup\$ If this isn't the right place for a question like this, or I need to change it somehow to make it appropriate, please let me know! :) \$\endgroup\$ – Joe Sep 17 '16 at 9:38
  • 5
    \$\begingroup\$ I think this is common practice in J which also uses _ as the minus sign of literals. In general I'd say that you should be able to work with your language's native formats unless the challenge says otherwise. (This is really the same as reading lists as [1,2], (1 2) or {1,2} depending on your language's syntax.) \$\endgroup\$ – Martin Ender Sep 17 '16 at 9:47
  • \$\begingroup\$ Brachylog also uses _ for negative numbers, and APL uses ¯. \$\endgroup\$ – Fatalize Sep 20 '16 at 11:41
24
\$\begingroup\$

Absolutely

Every language is different. Some take string input by default, some require double quotes around the string, some require single quotes around string, some have lists in this format

[1 2 3 4]

and others take lists in this format

(1, 2, 3, 4)

These types of language differences are trivial and normal, and you should format your input in whatever way is most natural to your language. It probably wouldn't hurt to mention this in your answer, just to make sure no one is confused. Something like:

Input is in this format...

#sample input

Note that negative numbers in dc are notated as _6, not as -6

Of course, the OP may override this and say something like:

Input will be in exactly this format:

[1, 2, -3, 4]

But in my opinion, this is a silly and useless rule to specify, as well as something to be discouraged. If a challenge had such a rule, I would certainly comment on it, ask them to change it and probably downvote if they did not.

\$\endgroup\$

You must log in to answer this question.

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