2
Origins
tl;dw (too lazy, didn't write): thought of it right before I fell asleep
Challenge
Breaking a number is defined as the following steps:
- Take a number or a parseable data type as input in any allowed way.
- Duplicate the first digit (64 -> 664)
- Subtract 4 from the last digit (664 -> 660)
- If the subtraction gets a negative last digit, move the negative sign (-) to the front of the number (73 -> 773 -> -771)
- If the original number was negative, and the last digit becomes negative, remove all negative signs entirely. (-44441 -> 444443)
- Output the number in any allowed way.
Here are a few test cases:
64 -> 660
73 -> -771
thisisnotanumber -> (handle the situation)
432188 -> 4432184
-83213 -> 883211 (two negative signs turn into a positive)
0 -> -4 (0 -> 00 -> 0-4 -> -04 -> -4)
Clarifications
Standard loopholes are disallowed.
By "parseable data type", I mean any data type (String, Float, etc) that can be converted into the number type your program/function uses (Integer, Number, etc).
Your answer can be a function or a program. In both cases, provide an explanation of the code.
If it is a function, give a usage of it.
Test cases (or online interpreter) would be a nice plus to your answer.
Preceding 0s (-04) aren't allowed.
Behavior should be exactly replicated from the test cases above.
Since it wasn't specified, I've assumed this is code golf and added the tag. If you intended a different winning criterion please edit to whatever you prefer. – trichoplax – 2016-07-30T23:48:51.230
@trichoplax i'd say falling gracefully is preferred, but hey, it's your code – InitializeSahib – 2016-07-30T23:52:41.383
3The trouble with "preferred" is that it isn't defined in the scoring, so no one will do it if it is not required, as it will adversely affect their score. – trichoplax – 2016-07-30T23:53:58.520
@trichoplax added to question – InitializeSahib – 2016-07-30T23:54:52.557
Could you define what "fail gracefully" means in this context? Should code return a particular string or give an error message? I usually post to the sandbox first to go through this clarification process before posting to main. I can recommend it for future challenges.
– trichoplax – 2016-07-31T00:03:29.967@trichoplax defined – InitializeSahib – 2016-07-31T00:04:30.883
5It's still unclear to me. Also, I don't think that required behavior for invalid inputs really adds something to the challenge. – Dennis – 2016-07-31T00:07:13.227
8Note to self: will put in sandbox first. – InitializeSahib – 2016-07-31T00:08:12.587
Are languages that don't have number types allowed to participate? (Since you require input to be either a number type or something that can be converted to a number type?) – Martin Ender – 2016-07-31T08:11:34.633
Is -04 ok or does it have to be -4? – betseg – 2016-07-31T11:35:05.493