5
I have this code :
tr ' ' '\n'|sort -n|head -1
The code is pretty simple, it takes a list of integer in the standard input stream (like 5 9 8 7 5 2 -12 -30
), it sort the integers and display only the first integer. But i need to ouput 0
if the given list is empty. And i can't find a short way to that. I have a solution with a ternary operator and a echo 0
, but i feel i can do far better. I just need to replace the input stream by 0
if this stream is empty.
Sidenote : I don't need to call read to get the user input. The program is called with the input stream directly like cmd < 1 5 6 -20 45
Here my current code :
read l
[ -n "$l" ]&&echo $l|tr ' ' '\n'|sort -n|head -1||echo 0
@Pietu1998 i added my current code – Magus – 2016-02-15T13:16:29.067
In this case you can cheat by always outputting a
0
first, if leading zeroes are allowed. – orlp – 2016-02-15T13:47:13.893@orlp nice thinking but sadly leading zeroes are not allowed – Magus – 2016-02-15T13:59:49.723
This looks like a programming problem to go on Stack Overflow. I'm not sure though; I haven't done any action on the question in terms of close/up/down voting. – HyperNeutrino – 2016-02-15T19:40:13.213
@AlexL. Make code X shorter will be very poorly received on Stack Overflow. – Dennis – 2016-02-15T19:41:36.340
@Dennis That's for sure. But for one thing, this question isn't marked with the tag
code-golf
. Also, language-specific code-golf problems are generally not advised. – HyperNeutrino – 2016-02-15T19:43:33.023@AlexL. This isn't a code golf contest, it's a tips question, which is one of the con-challenges that are considered on topic.
– Dennis – 2016-02-15T19:47:33.803@Dennis Whoops. Sorry for the confusion. I'm relatively new to the PPCG community and how it works. Thanks! – HyperNeutrino – 2016-02-15T19:50:52.650