[S S N
_Push_0][S N
S _Duplicate_0][T N
T T _Read_STDIN_as_integer][T T T _Retrieve_input][T N
S T _Print_as_integer]
Letters S
(space), T
(tab), and N
(new-line) added as highlighting only.
[..._some_action]
added as explanation only.
Try it online. You'll have to copy-paste the code yourself (note that SE converts the tabs to a bunch of spaces!) in order to run the code at the online Whitespace-compiler vii5ard. When clicking run, it will ask for an input (i.e. -285.5
), and after clicking enter it will continue and output -283
.
Explanation in pseudo-code:
Integer i = STDIN as integer
Print i as integer
Whitespace can only use I/O as integers or single characters, so in this case it would read the input as integer and ignore any other trailing characters. I.e. -283.5
or -283abc5
would both be input (and thus output) as -283
.
Unfortunately this above doesn't work on TIO for two reasons (all Whitespace compilers are slightly different..):
- It will give a
no parse
error when we try to read an input as integer, which isn't an a valid integer. So, instead we'll read one character at a time, and stop (with an error) as soon as we've encountered the .
or there is no more input (i.e. 50
/-50
).
- In the vii5ard compiler it's also possible to push 0 with just
SSN
, whereas on TIO it requires an additional S
or T
: SSSN
/SSTN
. The first S
is Enable Stack Manipulation; the second S
is Push what follows as integer; the third S
/T
is positive/negative respectively; and any S
/T
after that (followed by an N
) is the number we want to push in binary, where S=0
and T=1
. For integer 0 this binary part doesn't matter, since it's 0 by default. But on TIO we'd still have to specify the positive/negative, and with most other Whitespace compilers like vii5ard not.
Whitespace (with TIO compiler), 48 bytes
[N
S S N
_Create_Label_LOOP][S S S N
_Push_0][S N
S _Duplicate_0][T N
T S _Read_STDIN_as_character][T T T _Retrieve_input][S N
S _Duplicate_input][S S S T S T T T S N
_Push_46_.][T S S T _Subtract][N
T S S N
_If_0_Jump_to_Label_EXIT][T N
S S _Print_as_character][N
S N
N
_Jump_to_Label_LOOP]
Letters S
(space), T
(tab), and N
(new-line) added as highlighting only.
[..._some_action]
added as explanation only.
Try it online (with raw spaces, tabs and new-lines only).
Explanation in pseudo-code:
Start LOOP:
Character c = STDIN as character
If(c == '.'):
Exit program
Print c as character
Go to the next iteration of LOOP
Can we leave a trailing
.
on the output? – Xcali – 2019-08-22T23:50:24.167Similarly, may we assume the input always contains a
.
? Or that the input won't start with a.
? – Jo King – 2019-08-23T01:34:14.323@Xcali Yes you may. – connectyourcharger – 2019-08-23T01:54:07.917
@JoKing The input may not always contain a
.
(as in50
) and the input should not start with a.
. – connectyourcharger – 2019-08-23T01:54:49.5203May i output
3.00
for3.14
? – tsh – 2019-08-23T05:33:23.517Can we leave trailing unprintables on the output and leave with an error? – None – 2019-08-23T05:54:17.147
1@A_ If error messages are in stderr. And your output are in stdout. It is allowed by default. – tsh – 2019-08-23T06:59:25.813
Can you add test cases for
9.99
and-9.99
? Should round to9
and-9
right? – roblogic – 2019-08-23T10:04:00.5171Also
0.01
and-0.01
should yield0
... – roblogic – 2019-08-23T10:33:36.700more Jimmy challenges coming soon
+1 – LegenDUST – 2019-08-23T12:29:49.0772Hmm, this seems unreasonably trivial for a code golf. Most langs will have a builtin for this, no? It looks like we are to assume all input and output are strings? – Octopus – 2019-08-23T19:13:45.937
@tsh 3.00 isn't an integer. – Octopus – 2019-08-23T19:30:16.987
23.00 certainly is an integer. More precisely, in standard mathematical notation as well as in many programming languages, the notation "3.00" denotes the number 3, which is an integer; but in many programming languages, it indicates that the number is to be stored in a floating-point format. (But it's an integer regardless of the format it's stored in.) – Tanner Swett – 2019-08-23T20:06:06.063
does it need to handle the weird floating point edge cases? – qwr – 2019-08-23T20:27:00.970
1Rounding towards zero, that is, discarding the fractional part, is called truncation and is a standard operation in almost all languages. Typically, doing this kind of rounding is more "basic" than doing a ceil or a floor. – Andreas Rejbrand – 2019-08-24T15:00:20.703
Does this need to work with very large numbers? If so, somewhat less trivial. – TLW – 2019-08-24T16:53:21.807