44
\$\begingroup\$

I think we've all done this as a kid: some websites require a minimum age of 18, so we just subtract a few years from the year of birth and voilà, we 'are' 18+.
In addition, for most rides at amusement parks the minimum height to enter is 1.40 meters (here in The Netherlands it as at least). Of course this can be cheated less easily than age, but you could wear shoes with thick heels, put your hair up, wear a hat, stand on your toes, etc.

Input:

Your program/function accepts a positive integer or decimal.

Output:

  • Is the input an integer >= 18? Simply print the input.
  • Is the input an integer 0-17? Print 18.
  • Is the input a decimal >= 1.4? Simply print the input.
  • Is the input a decimal 0.0-1.4? Print 1.4.

Challenge rules:

  • Assume the input will always be in the range of 0-122 (oldest woman ever was 122) or 0.0-2.72 (tallest man ever was 2.72).
  • You are allowed to take the input as a String, object, or anything else you prefer.
  • The decimal inputs will never have more than three decimal places after the decimal point.
  • 2 or 2. both aren't valid outputs for 2.0. You are free to output 2.00 or 2.000 instead of 2.0 however.
    Just like the input the output will never have more than three decimal places after the point.

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.

Test cases:

0      ->  18
1      ->  18
2      ->  18
12     ->  18
18     ->  18
43     ->  43
115    ->  115
122    ->  122

0.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
\$\endgroup\$
  • \$\begingroup\$ Can we assume that the input is free of leading zeros? \$\endgroup\$ – Toby Speight Sep 29 '16 at 12:45
  • \$\begingroup\$ @TobySpeight Yes, no leading zeros. \$\endgroup\$ – Kevin Cruijssen Sep 29 '16 at 12:48
  • 2
    \$\begingroup\$ 0.0-2.72 (tallest man ever was 2.72). - You aren't 0.0 when you're born... \$\endgroup\$ – Johan Karlsson Sep 29 '16 at 13:49
  • 1
    \$\begingroup\$ @JohanKarlsson I know, thought about adding a minimum, but I decided to just let it start at 0 and 0.0. :) The added tallest man ever was 2.72 and oldest woman ever was 122 was just added as informational facts for those interested. \$\endgroup\$ – Kevin Cruijssen Sep 29 '16 at 13:54
  • 9
    \$\begingroup\$ "[...] so we just add a few years to the year of birth [...]" Shouldn't you subtract a few years from the year of birth? \$\endgroup\$ – wythagoras Oct 2 '16 at 17:35

54 Answers 54

1 2
2
\$\begingroup\$

Powershell : 58 Bytes

Bit longer than i'd expected, so many brackets required to have it run correctly.

.({({1.4},$i)[$i-gt1.4]},{({18},$i)[$i-gt18]})[$i-is[int]]

Input as $i

. #Execute the code.. required.
( #First block
    { #Value if-false of first block
        (
        {1.4} #Return Val
        , #Or
        $i #Return Input
        )[$i-gt1.4] #If input is greater than Val
    },{ #Next Block
    ({18},$i)[$i-gt18]} #Same with different val
)[$i-is[int]] #Decides based on if val is default int

Test Cases:

@(0,1,2,12,18,43,115,122,0.0,1.04,1.225,1.399,1.4,1.74,2.0,2.72) | % {
$i = $_
Write-Host $i`t`t -NoNewline
.({({1.4},$i)[$i-gt1.4]},{({18},$i)[$i-gt18]})[$i-is[int]]
}

returns :

0       18
1       18
2       18
12      18
18      18
43      43
115     115
122     122
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      (this is a value of type 'double' - the default formatting of powershell doesn't add decimals, but the returned value is of the correct type)
2.72    2.72

Proof of 2 == 2.0

$i = 2.0
$q = .({({1.4},$i)[$i-gt1.4]},{({18},$i)[$i-gt18]})[$i-is[int]]
$q.GetType().Name
Double
\$\endgroup\$
  • 1
    \$\begingroup\$ Assuming that the input is stored in a pre-defined variable isn't an allowed input method. You'd need to save it as a .ps1 file and do a param($i) or $i=$args[0] or similar. Additionally, with it in a file, you can remove the . and many of the curly braces -- param($i)(((1.4,$i)[$i-gt1.4]),((18,$i)[$i-gt18]))[$i-is[int]] for 62 bytes -- so only a few bytes longer than you've got currently. \$\endgroup\$ – AdmBorkBork Oct 7 '16 at 19:03
2
\$\begingroup\$

Python 2, 49 bytes

lambda a:(a,1.4)[a<1.4]if"."in`a`else(a,18)[a<18]
\$\endgroup\$
  • 1
    \$\begingroup\$ Did it one byte shorter: lambda a:((a,18)[a<18],(a,1.4)[a<1.4])["."in`a`] \$\endgroup\$ – Erik the Outgolfer Sep 29 '16 at 10:51
  • \$\begingroup\$ Thanks @Erik. I tried that and it came out exactly the same length. Guess I must have had a stray space in there ;-) \$\endgroup\$ – ElPedro Sep 29 '16 at 10:54
  • \$\begingroup\$ Maybe. The most suspicious place is da a:((a, because, by instinct, you write da a: ((a instead. And it is a unique place. \$\endgroup\$ – Erik the Outgolfer Sep 29 '16 at 10:56
  • 1
    \$\begingroup\$ lambda a:(max(a,18),max(a,1.4))["."in`a`] is considerably shorter. \$\endgroup\$ – Mego Sep 29 '16 at 11:49
  • 1
    \$\begingroup\$ @ElPedro I don't think so, but here's the list of languages you can use syntax highlighting with if you wanna check. \$\endgroup\$ – acrolith Sep 29 '16 at 16:23
2
\$\begingroup\$

Java, 95 bytes

void p(double d){if(d%1>0)System.out.print(d<1.4?1.4:d);else System.out.print(d<18?18:(int)d);}

5 worse than Kevin's answer, but it has the print statement inside the function so maybe I get some pity points for that.

If this is allowed in Java, you could also use

d->{if(d%1>0)System.out.print(d<1.4?1.4:d);else System.out.print(d<18?18:d);};

Which could be used like

Consumer<Double> func = d->{if(d%1>0)System.out.print(d<1.4?1.4:d);else System.out.print(d<18?18:d);};
\$\endgroup\$
  • 2
    \$\begingroup\$ Our standard is to allow any kind of functions, including lambdas. \$\endgroup\$ – corvus_192 Sep 29 '16 at 21:29
  • \$\begingroup\$ Instead of printing, you could always just return it and specify returning a string instead of void. So get rid of the if and just return(d%1>0?d<1.4?1.4:d:d<18?18:(int)d)+""; is what, 44? then tack on the String p(double d){} with those 44 in the middle and you've got 20 more for a total of 64. \$\endgroup\$ – corsiKa Oct 2 '16 at 19:56
  • \$\begingroup\$ @corsiKa unfortunately the (?:) statements need to have the same return type in Java. Your code would print 18.0 instead of 18. That said, you can do d%1>0?""+(d<1.4?1.4:d):(d<18?18:(int)d)+"".replace(".0","") But surrounded with a print statement and method header, it becomes 96 bytes \$\endgroup\$ – dpa97 Oct 3 '16 at 16:56
  • \$\begingroup\$ @dpa97 Don't print. Return a string. \$\endgroup\$ – corsiKa Oct 3 '16 at 17:02
  • \$\begingroup\$ @corsiKa I'm pretty new to code golf, doesn't the OP want the output to be printed? Seems like returning just a string is a step short. \$\endgroup\$ – dpa97 Oct 3 '16 at 18:54
2
\$\begingroup\$

Ruby - 34 bytes

Very straight-forward. Could possibly save 2 bytes by transforming that ternary.

->(x){x%1>0?x<1.4?1.4:x:x<18?18:x}
\$\endgroup\$
  • 1
    \$\begingroup\$ Does not work for 1.0. \$\endgroup\$ – G B Mar 3 '17 at 16:49
2
\$\begingroup\$

Scala, 44 bytes

def?(x:Int)=18 max x
def?(x:Float)=1.4 max x

Defines two methods using overloading.

\$\endgroup\$
2
\$\begingroup\$

Cheddar, 23 21 bytes

n->[n,n%1?1.4:18].max

Nothing super-new but I'll post it as it's pretty short.

\$\endgroup\$
2
\$\begingroup\$

PHP, 36 35 34 bytes

echo max($a=$argn,$a[1]&p?1.4:18);

Checks whether the character in the second position is a digit (binary and with p). If so, use 18 as the minimum, otherwise 1.4. Then take the bigger of the 2 numbers.

Run like this:

echo 11 | php -nR 'echo max($a=$argn,$a[1]&p?1.4:18);';echo

Explanation

Here's a table of the possible results of the &"p" operation:

input  binary      &   "p"         result      chr   Truthy/falsy
"."    0010 1110   &   0111 0000   0010 0000   " "   TRUTHY
"0"    0011 0000   &   0111 0000   0011 0000   "0"   FALSY
"1"    0011 0001   &   0111 0000   0011 0000   "0"   FALSY
"2"    0011 0010   &   0111 0000   0011 0000   "0"   FALSY
"3"    0011 0011   &   0111 0000   0011 0000   "0"   FALSY
"4"    0011 0100   &   0111 0000   0011 0000   "0"   FALSY
"5"    0011 0101   &   0111 0000   0011 0000   "0"   FALSY
"6"    0011 0110   &   0111 0000   0011 0000   "0"   FALSY
"7"    0011 0111   &   0111 0000   0011 0000   "0"   FALSY
"8"    0011 1000   &   0111 0000   0011 0000   "0"   FALSY
"9"    0011 1001   &   0111 0000   0011 0000   "0"   FALSY
null   0000 0000   &   0111 0000   0000 0000   0     FALSY

When the input is 1 digit, then taking the second digit will result in null. null&"string" will also conveniently evaluate to false. So only when the second char is a . will the expression evaluate to true and 1.4 will be used as the minimum instead of 18.

Tweaks

  • Saved a byte by using $argn
\$\endgroup\$
2
\$\begingroup\$

Brachylog v2, 11 bytes

ℤ;18⌉|;1.4⌉

Try it online!

Essentially Fatalize's answer, just a bit shorter because it's a newer version of the language with a non-ASCII codepage.

               The output is
    ⌉          the largest of
  18           eighteen
 ;             and
               the input
ℤ              which is an integer,
     |         or
               the output is
          ⌉    the largest of
       1.4     1.4
      ;        and
               the input.
\$\endgroup\$
1
\$\begingroup\$

Pyke, 15 bytes

,16.6*1.4+Qb]Se

Try it here!

Takes input in quotes. is_numeric returns 1 if the input doesn't contain a ., otherwise it returns 0

,16.6*          - 16.6*is_numeric(input)
      1.4+      - ^ + 1.4
          Qb]   - [^, float(input)] 
             Se - sorted(^)[-1]
\$\endgroup\$
1
\$\begingroup\$

Racket 76 bytes

(λ(n)(cond[(integer? n)(if(>= n 18)n 18)][(number? n)(if(>= n 1.4)n 1.4)]))

Ungolfed version:

(define f
  (λ(n)
    (cond
      [(integer? n) (if(>= n 18) n 18)]
      [(number? n) (if(>= n 1.4) n 1.4)]
      )))

Testing:

(f 1.3)
(f 15)
(f 19)
(f 1.5)

Output:

1.4
18
19
1.5
\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 31 bytes

Prompt O,Str1
Disp max(18,O
Str1
If 1.4>expr(Ans
"1.4
Ans

Pretty self-explanatory. Only thing I would note is that in TI-Basic, whatever the last line evaluates to is implicitly displayed. If nothing is evaluated on the last line it will print Done.

\$\endgroup\$
1
\$\begingroup\$

GameMaker Language, 74 bytes

a=argument1;if real(a)<1.4a="1.4"show_message(string(max(argument0,18))+a)
\$\endgroup\$
1
\$\begingroup\$

SQL, 105 bytes

IF @v like'%.%'select MAX(p)from(values(@v),(1.400))as X(p)ELSE select MAX(p)from(values(@v),(18))as X(p)

tested on MS SQL Server 2008 R2

usage:

DECLARE @v as varchar(255) = '1.405'
IF @v like'%.%'select MAX(p)from(values(@v),(1.400))as X(p)ELSE select MAX(p)from(values(@v),(18))as X(p)
\$\endgroup\$
1
\$\begingroup\$

k, 20 bytes

Returns the maximum of the input, x, and 18 if x is integral or 1.4 otherwise.

{x|$[-7h=@x;18;1.4]}
\$\endgroup\$
1
\$\begingroup\$

Julia, 35 bytes

f(x)=max(x,1.4)
f(x::Int)=max(x,18)

Using multiple dispatch.

\$\endgroup\$
  • \$\begingroup\$ While it's certainly the more Julian way of solving the problem, it's not the shortest. You're better off using isa(x,Int) in a single function for Code Golf purposes. Or better yet, x!==x+0., to save an extra two bytes (x+0. will convert an integer to a float, and !== checks to see if the two are the same thing - Float and Int won't register as the same). \$\endgroup\$ – Glen O Oct 2 '16 at 12:43
  • \$\begingroup\$ @GlenO You're not wrong. \$\endgroup\$ – Lyndon White Oct 2 '16 at 14:30
1
\$\begingroup\$

Logy, 74 bytes (non-competing)

m[X,Y]->X>Y&X|Y;f[X]->include["@stdlib.logy"]~X==ceil[X]&m[X,18]|m[X,1.4];

Wow... Outgolfed by Java

max[X, Y] -> X > Y & X | Y;
f[X] -> include["@stdlib.logy"]
      ~ X == ceil[X] & max[X, 18] | max[X, 1.4];
\$\endgroup\$
1
\$\begingroup\$

C++14, 52 bytes

As unnamed lambda:

[](auto t){auto l=sizeof t-4?1.4:18;return t<l?l:t;}

Assumes sizeof int==4 and sizeof double!=4 which is true on most systems. Only side effect: the returned value is always a double.

Usage:

//assign to variable
auto f=[](auto t){auto l=sizeof t-4?1.4:18;return t<l?l:t;};
f(10)
f(2.0)

//use directly
[](auto t){auto l=sizeof t-4?1.4:18;return t<l?l:t;}(10);
[](auto t){auto l=sizeof t-4?1.4:18;return t<l?l:t;}(2.0);
\$\endgroup\$
1
\$\begingroup\$

Ruby, 31 bytes

->x{[x.to_s=~/\./?1.4:18,x].max}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C and C++ : 47 bytes

#define a(b)(sizeof b>4?b>=1.4?b:1.4:b<18?18:b)

Please let me know if this is considered a default loophole. The macro assumes that an integer literal is four bytes and a double floating point literal is greater than that.

Test using GCC 6.3.1 (C++) :

#include <iostream>

#define a(b)(sizeof b>4?b>=1.4?b:1.4:b<18?18:b)

void print_a (const auto &t) {
  std::cout << a(t) << '\n';
}

template <class T, class...P>
void print_a (const T &t, const P&...p) {
  print_a (t);
  print_a (p...);
}

int main () {

  print_a (0, 1, 2, 12, 18, 43, 115, 122, 0.0, 1.04,
   1.225, 1.399, 1.4, 1.74, 2.0, 2.72);

  return 0;
}
\$\endgroup\$
1
\$\begingroup\$

C++ (gcc), 49 bytes

-DK(T,u)T=f(T&x){x<u?x=u:x;}

K(int,18)K(float,1.4)

Try it online!

C++ (gcc), 56 bytes

-DK(T,u)T=f(T(x)){return+x<u?u:x;}

K(int,18)K(double,1.4)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Since you've included the compile flag in the byte-count, I don't see why it wouldn't be allowed. As for the 50-byte answer, it doesn't work in TIO? \$\endgroup\$ – Kevin Cruijssen Apr 5 '18 at 15:13
  • \$\begingroup\$ @KevinCruijssen The compile flag is allowed in meta, I just didn't know if overloading is allowed \$\endgroup\$ – l4m2 Apr 5 '18 at 15:18
1
\$\begingroup\$

Ruby, 30 bytes

->x{[x.integer??18:1.4,x].max}

I'm very surprised that an 8-character method call seems to be the shortest way to switch modes (at least if we want to treat 1.0 as a decimal).

\$\endgroup\$
1
\$\begingroup\$

dc, 34 bytes

[1.4]s1[18]s8[d18>8]s*dX0=*d1.4>1p

Try it online! Note: test cases as defined in the footer do include 0.0, but dc prints 0.0 as 0, so that test case looks like the input is wrong. Also the footer just defines the main chunk as a macro and adds some printing to tidy up the test cases a bit.

Aside from the case of 0.0 as mentioned above, dc prints trailing zeroes in decimals, so we don't really have to do anything special beyond testing for a decimal point and our two less-than tests.

Because conditionals must run macros stored in registers, some bytes are spent just storing our min values - 1.4 in macro/register 1, and 18 in macro/register 8. Macro * just checks whether or not our value is less than 18, and if so runs the aforementioned macro 8 to put 18 on the stack. The main bit is dX0=*d1.4>1. This uses the X command to see how many digits are to the right of the decimal point. If it's 0, we run the aforementioned macro * to either leave the number be, or place 18 on the stack. Fortunately, since 18 is greater than 1.4, we don't really have to worry about finagling else statements or anything (if we needed the integer 1 to pass through unharmed, things would be different). So from here we just test if we're less than 1.4; if so we run macro 1 to put 1.4 on the stack, and finally we print with p.

\$\endgroup\$
1
\$\begingroup\$

Lua, 71 bytes

function Z(a,b)print(a+0<b and b or a)end R=io.read Z(R(),18)Z(R(),1.4)

Tested in Lua 5.1.5. TIO throws an error at a+0

Ungolfed

function print_min(input,min_value)
    print(input+0<min_value and min_value or input)
end

R=io.read
print_min(R(),18)
print_min(R(),1.4)

Explanation

a+0 is the shortest way to convert a string to a number. The syntax a+0<b and b or a is a replacement for Lua's lack of a ternary operator; equivalent to a+0<b?b:a.

\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Compiler), 69 67 bytes

s=>int.TryParse(s,out int n)?n<18?"18":s:float.Parse(s)<1.4?"1.4":s

Try it online!

Edit: Saved 2 bytes thanks to Kevin Cruijssen:

  1. Changed double to float
  2. Removed trailing semi-colon
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! The trailing semi-colon doesn't have to be counted in the byte-count for Java/C# .NET lambdas. Also, you can save 1 byte changing the double to float. Enjoy your stay! :) \$\endgroup\$ – Kevin Cruijssen Apr 6 '18 at 8:17
1 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.