Yes, of course I'm an adult!
Python 2.7, 34 bytes
lambda x:max(x,[18,1.4]['.'in`x`])
JavaScript (ES6), 27 31
Input taken as a string. To check if the input value has decimals, it's appended to itself: if there is no decimal point the result is still a valid number, else it's not. To recognize a valid number (including 0), I use division as in javascript 1/n
is numeric and not 0 for any numeric n
(eventually the value is Infinity
for n==0
), else it's NaN
x=>x<(y=1/(x+x)?18:1.4)?y:x
Test
f=
x=>x<(y=1/(x+x)?18:1.4)?y:x
;[
['0', '18' ],['1', '18' ],['2', '18' ],['12', '18' ],['18', '18' ],['43', '43' ],['115', '115'], ['122', '122' ]
,['0.0', '1.4'],['1.0', '1.4'],['1.04', '1.4'],['1.225', '1.4'],['1.399', '1.4'],['1.4', '1.4'],['1.74', '1.74'],['2.0', '2.0'],['2.72', '2.72']
].forEach(t=>{
var i=t[0],k=t[1],r=f(i)
console.log(i,k,r,k==r?'OK':'KO')
})
My prev (wrong) solution:
Taking input as a number, you can use remainder operator %
to check if the number is integer.
x=>x<(y=x%1?1.4:18)?y:x
or
x=>Math.max(x,x%1?1.4:18)
But this does not work as the challenge request to discriminate between, say, 2
and 2.0
and that's the same number. So it's not possibile to get the input as a number
05AB1E, 13 11 bytes
Uses CP-1252 encoding.
ÐîQ18*14T/M
Explanation
Ð # triplicate input
î # round up
Q # check for equality
18* # multiply 18 by this (18 if input is int, else 0)
14T/ # push 14 / 10
M # take max of stack (input and 1.4 or 18)
Try it online