46
\$\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\$
7
  • \$\begingroup\$ Can we assume that the input is free of leading zeros? \$\endgroup\$ Sep 29 '16 at 12:45
  • \$\begingroup\$ @TobySpeight Yes, no leading zeros. \$\endgroup\$ 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\$ 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\$ Sep 29 '16 at 13:54
  • 10
    \$\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

55 Answers 55

4
\$\begingroup\$

Convex, 9 bytes

_1ÛI1.4?»

Try it online!

\$\endgroup\$
0
44
\$\begingroup\$

Python 2.7, 34 bytes

lambda x:max(x,[18,1.4]['.'in`x`])
\$\endgroup\$
10
  • \$\begingroup\$ What does this return for 2.0? \$\endgroup\$
    – Adám
    Sep 29 '16 at 12:20
  • 2
    \$\begingroup\$ @Adám max(2.0,[18,1.4][True]) == max(2.0,1.4) == 2.0 \$\endgroup\$
    – Lynn
    Sep 29 '16 at 12:42
  • 3
    \$\begingroup\$ Nope, it doesn’t. Why don’t you try it yourself? :) \$\endgroup\$
    – Lynn
    Sep 29 '16 at 13:47
  • 5
    \$\begingroup\$ @Adám I use repl.it or ideone.com for Python. View any Python answer I've ever posted, and it probably has a link to one of those two. \$\endgroup\$
    – mbomb007
    Sep 29 '16 at 14:12
  • 1
    \$\begingroup\$ nevermind, got it. so basically true or false maps to 0 or 1 index in that array and then applies max on the two numbers. \$\endgroup\$ Oct 3 '16 at 15:01
15
\$\begingroup\$

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

\$\endgroup\$
7
  • 2
    \$\begingroup\$ Result for 2.0 should be 2.0, not 18. \$\endgroup\$
    – Neil
    Sep 29 '16 at 10:09
  • \$\begingroup\$ indeed. 2.0%1 and 1.0%1 will result in 0 \$\endgroup\$
    – aross
    Sep 29 '16 at 10:11
  • 4
    \$\begingroup\$ 'javascript(es6) is verbose', you only see that at codegolf \$\endgroup\$
    – dwana
    Sep 29 '16 at 10:56
  • \$\begingroup\$ @Neil on second thought you're probably right \$\endgroup\$
    – edc65
    Sep 29 '16 at 12:48
  • 3
    \$\begingroup\$ 1/(x+x) - now that's imaginative! \$\endgroup\$
    – Neil
    Sep 29 '16 at 17:03
14
\$\begingroup\$

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

\$\endgroup\$
7
  • 2
    \$\begingroup\$ You're kinda slow. Still took you 1.5 minutes. ;P (read: Damn, that was fast.) It's pretty straightforward of course. \$\endgroup\$ Sep 29 '16 at 9:12
  • 2
    \$\begingroup\$ @KevinCruijssen: Yeah, the implementation is quite fast in a lang not requiring many key-presses :P \$\endgroup\$
    – Emigna
    Sep 29 '16 at 9:13
  • \$\begingroup\$ @EriktheGolfer: Better? If not feel free to edit it. I've been experimenting with a few different ways of formatting and haven't decided on a strictly best one. Suggestions welcome. \$\endgroup\$
    – Emigna
    Sep 29 '16 at 13:00
  • \$\begingroup\$ @Emigna I just added two missing crucial spaces. \$\endgroup\$ Sep 29 '16 at 13:02
  • 2
    \$\begingroup\$ @FrancescoCasula: I found a shorter solution which does work on TIO :) \$\endgroup\$
    – Emigna
    Sep 29 '16 at 13:53
8
\$\begingroup\$

Java 8, 90 61 57 bytes

i->(i+"").contains(".")?(float)i<1.4?1.4:i:(int)i<18?18:i

-4 bytes returning Object instead of String; and some additional bytes converting Java 7 to 8.
-4 byte taking the input as Object instead of String as well.

Explanation:

Try it here.

i->                      // Method with Object as both parameter and return-type
  (i+"").contains(".")?  //  If the input as String contains a dot:
   (float)i<1.4?         //   If the input is a float below 1.4:
    1.4                  //    Return double 1.4
   :                     //   Else:
    i                    //    Return the input-float as is
  :(int)i<18?            //  Else if the input is an integer below 18:
   18                    //   Return integer 18
  :                      //  Else:
   i                     //   Return the input-integer as is
\$\endgroup\$
2
  • \$\begingroup\$ Is it necessary to put brackets around the if/else operator? \$\endgroup\$ Sep 29 '16 at 18:18
  • 2
    \$\begingroup\$ @RomanGräf Yes; the ternary has lower precedence than +, which means that if you removed the parentheses, it would turn into (""+i.contains(...)) ? \$\endgroup\$
    – Nic
    Sep 30 '16 at 0:23
7
\$\begingroup\$

Pyth, 14 bytes

eS,@,18h.4}\.`

Try it online.

\$\endgroup\$
7
\$\begingroup\$

PHP, 40 Bytes

modified by @user59178 Thank You

<?=max(is_int(0+$i=$argv[1])?18:1.4,$i);

PHP, 42 Bytes first Version

<?=max(strpos($i=$argv[1],".")?1.4:18,$i);
\$\endgroup\$
12
  • \$\begingroup\$ is_int($i=$argv[1]+0) is 2 bytes shorter than strpos($i=$argv[1],".") and can serve the same purpose if you swap the 1.4 and the 18 \$\endgroup\$
    – user59178
    Sep 29 '16 at 16:05
  • \$\begingroup\$ @user59178 I could use is_numeric on a string but not is_int \$\endgroup\$ Sep 29 '16 at 16:18
  • \$\begingroup\$ that's why there's the +0, to convert it to a numeric type. \$\endgroup\$
    – user59178
    Sep 30 '16 at 7:59
  • 1
    \$\begingroup\$ It works correctly for me (tried both php 5.5 and 7.0 on windows). Note that it has opposite true/false conditions to the strpos($i=$argv[1],".") version, did you remember to swap the outputs of the ternary? \$\endgroup\$
    – user59178
    Sep 30 '16 at 10:43
  • 1
    \$\begingroup\$ Actually, on closer reading, it needs to be <?=max(is_int(0+$i=$argv[1])?18:1.4,$i); rather than <?=max(is_int($i=$argv[1]+0)?18:1.4,$i); to avoid outputting 2 when given 2.0. \$\endgroup\$
    – user59178
    Sep 30 '16 at 11:01
6
\$\begingroup\$

Perl, 29 27 bytes

Includes +2 for -lp

Give input on STDIN

adult.pl <<< 1.24

adult.pl:

#!/usr/bin/perl -lp
$_>($a=/\./?1.4:18)or*_=a

If you don't mind an extra newline if you really were a full adult then leaving out the l option for 26 bytes works too

\$\endgroup\$
6
\$\begingroup\$

EXCEL: 26 31 29 Bytes

=MAX(A1;IF(MOD(A1;1);1,4;18))

Formula can go anywhere except A1, the input cell.

Fixed errors, and replaced with Emigna's suggestion.

Thanks to Alexandru for saving me some bytes using truths

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Also, Isn't it better to define a name n for the input cell? It can be anywhere in the document then and it's also 2 bytes shorter. \$\endgroup\$
    – Emigna
    Sep 29 '16 at 14:52
  • \$\begingroup\$ @Emigna I could, but at that point it gets a bit cheat-y I feel. 1 byte per name is nothing to scrounge at, and If I keep this format, people can copy and paste with ease. \$\endgroup\$
    – user56309
    Sep 29 '16 at 14:55
  • \$\begingroup\$ I don't see how that's any different than using a 1-letter input variable in a lambda in python for example. But it's your call :) \$\endgroup\$
    – Emigna
    Sep 29 '16 at 14:56
  • \$\begingroup\$ Hi! How do you calculate the bytes? Save the file with the formula with the default name or somehow else? \$\endgroup\$
    – Vityata
    Sep 30 '16 at 9:30
  • 1
    \$\begingroup\$ You can remove the '=0' and switch the results: 1.4 first, 18 second Also, as you have coma as decimal separator, this might not work for most people. \$\endgroup\$ Oct 3 '16 at 15:15
5
\$\begingroup\$

Brachylog, 14 bytes

#$:18ot|:1.4ot

Try it online!

Explanation

    #$             Input is an integer
      :18o         Sort the list [Input, 18]
          t        Take the last element
|              Or
    :1.4o          Sort the list [Input, 1.4]
         t         Take the last element
\$\endgroup\$
5
\$\begingroup\$

GNU sed, 40 + 1 = 41 bytes

(score +1 for use of -r flag to interpreter)

s/^.$|^1[^9]$/18/
/^0|1\.[0-3]/s/.*/1.4/

Annotated:

#!/bin/sed -rf

# First, anything that's a single digit or is '1' followed by a
# digit other than '9' is replaced with '18'.
s/^.$|^1[^9]$/18/

# Now, any line beginning with '0' or containing '1.0' to '1.3' is
# replaced with '1.4'.
/^0|1\.[0-3]/s/.*/1.4/

We take advantage of the constraints on input, so don't have to test the start of string when we see '1.' - we know there's only one digit before the decimal point.

Test result:

$ ./94832.sed <<END
> 0
> 1
> 2
> 12
> 18
> 43
> 122
> 
> 0.0
> 1.04
> 1.225
> 1.399
> 1.4
> 1.74
> 2.0
> 2.72
> END
18
18
18
18
18
43
122

1.4
1.4
1.4
1.4
1.4
1.74
2.0
2.72
\$\endgroup\$
5
\$\begingroup\$

Haskell, 50 bytes

x#y=show$max x$read y 
f s|elem '.'s=1.4#s|1<2=18#s

Usage example: f "1.0" -> "1.6".

Haskell's strict type requires usage of strings as input and output. Howevers, read, max and show are polymorphic and handle all numeric types.

\$\endgroup\$
3
  • \$\begingroup\$ I thought I'd be clever and do it without the guards, but that ended up making it slightly longer instead :( My version: (#)x=map(show.max x.fst).reads;f s=head$18#s++1.4#s \$\endgroup\$
    – Cubic
    Sep 30 '16 at 21:24
  • \$\begingroup\$ @Cubic: Nice use of reads. With a slight modification it's one byte shorter than mine. Please post it as a separate answer. x#y=show.max x.fst<$>reads y;f s=head$18#s++1.4#s. \$\endgroup\$
    – nimi
    Sep 30 '16 at 21:42
  • \$\begingroup\$ Really cool idea saving the parens with infix fmap! \$\endgroup\$
    – Cubic
    Sep 30 '16 at 21:48
5
\$\begingroup\$

Java, 79 70 bytes

int f(int x){return x<18?18:x;}
float f(float x){return x<1.4?1.4f:x;}

Defines two methods with overloading, which use the conditional operator.

Call it like f(5) or f(1.4f).

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Hi. x<18?18:x and x<1.4f?1.4f:x are shorter than the Math.max. I think you might find tips for golfing in Java interesting to read through. :) \$\endgroup\$ Oct 1 '16 at 7:47
  • \$\begingroup\$ Yes, of course they are. How could I forgot them... \$\endgroup\$
    – corvus_192
    Oct 1 '16 at 21:28
  • \$\begingroup\$ I love this! Why code the logic yourself when you can offload that to the compiler! \$\endgroup\$
    – corsiKa
    Oct 3 '16 at 17:03
4
\$\begingroup\$

CJam, 18 14 bytes

Saved 4 bytes thanks to Martin Ender.

q~_`'.&1.4I?e>

Online interpreter

\$\endgroup\$
0
4
\$\begingroup\$

IBM/Lotus Notes Formula Language, 58 49 bytes

@If(@Like(@Text(a);"%.%");@If(a<1.4;1.4;a);@If(a<18;18;a))

Computed field formula where a is an editable numeric field.

EDIT

@If(@Like(@Text(a);"%.%");@Max(1.4;a);@Max(18;a))

Alternative inspired by @Mego

\$\endgroup\$
4
\$\begingroup\$

AWK - 29 bytes

($0<c=$0~/\./?1.4:18){$0=c}1

Usage:

awk '{c=$0~/\./?1.4:18}($0<c){$0=c}1' <<< number

Testing was performed with gawk on RHEL 6. I tried with all the test cases, unfortunately I don't have AWK on the machine that has internet access, so copy-paste is not possible.

Is there a more compact way to do this in AWK?

\$\endgroup\$
4
\$\begingroup\$

C#, 58 bytes

x=>x is int?(int)x>17?x:18:(float)x<1.4?"1.4":$"{x:.0##}";

No crazy string parsing needed for C#. Input is expected to be an int or float (unfortunately C# can't cast a double to float if the double is in an object). Output will be either int or string in an object.

(almost missed the at least 1 decimal requirement, added that now)

Ungolfed:

/*Func<object, object> Lambda = */ x =>
    x is int // if parameter is an int
        ? (int)x > 17 // check if x is at least 18
            ? x // at least 18 so return x
            : 18 // less than 18 so return 18
        : (float)x < 1.4 // x is float, check if at least 1.4
            ? "1.4" // less than 1.4 so return 1.4
            : $"{x:.0##"} // at least 1.4 so return x and ensure at least 1 decimal place
;

Alternate implementation which is also 58 bytes.

x=>x is int?(int)x>17?x:18:$"{((float)x<1.4?1.4:x):.0##}";
\$\endgroup\$
4
\$\begingroup\$

Actually, 16 bytes

;:.7τ9τ($'.íuIkM

Try it online!

Explanation:

;:.7τ9τ($'.íuIkM
;                 dupe input
 :.7τ             1.4 (.7*2) - note that :1.4 is the same length, but an additional delimiter would be needed to separate it from the following 1
     9τ           18 (9*2)
       ($'.íu     1-based index of "." in string representation of input, 0 if not found
             I    1.4 if input contains a "." else 18
              kM  maximum of remaining values on stack 
\$\endgroup\$
3
  • \$\begingroup\$ I've never programmed in Actually before, but why use instead of just 18? I know it's the same byte-count so it doesn't really matter, but 18 seems more readable. Or is there a reason it won't work in the current implementation/programming language? \$\endgroup\$ Sep 29 '16 at 11:57
  • 3
    \$\begingroup\$ @KevinCruijssen 18 pushes a 1 and an 8. To push a literal 18, you'd use :18, which is longer than . \$\endgroup\$
    – user45941
    Sep 29 '16 at 11:58
  • \$\begingroup\$ Ah of course, stack-based languages. Thanks for the explanation! +1 \$\endgroup\$ Sep 29 '16 at 11:59
4
\$\begingroup\$

Emacs Lisp, 37 bytes

(lambda(x)(max(if(floatp x)1.4 18)x))

Guesses from the "datatype" whether the integer or float version should be used. (floatp returns t for 1.0, but not for 1.) The parameter is a number as integer or float, i.e. it should satisfy numberp.

\$\endgroup\$
4
\$\begingroup\$

Haskell, 49 bytes

x#y=show.max x.fst<$>reads y;f s=head$18#s++1.4#s

Basically, this first attempts to read the input as an integer and then as a double if that fails. Then proceeds to compare it to the respective comparison baseline.

\$\endgroup\$
4
\$\begingroup\$

Jelly, 16 15 13 12 bytes

-1 thanks to caird coinheringaahing (take input as a string).

ċ”.ị1.4,18»V

TryItOnline
Or see all test cases, also at TryItOnline

How?

ċ”.ị1.4,18»V - Main link: string, S
 ”.          - '.' character
ċ            - count (occurrences of '.' in S)
    1.4,18   - pair literals 1.4 and 18:  [1.4,  18]
   ị         - index into (1-indexed)
           V - evaluate (S) as Jelly code
          »  - maximum
\$\endgroup\$
4
  • 2
    \$\begingroup\$ This returns 18 for 2.0, sadly :( \$\endgroup\$
    – Lynn
    Sep 29 '16 at 12:44
  • \$\begingroup\$ Ah, the complexity of Jelly is superior. \$\endgroup\$ Sep 29 '16 at 13:00
  • \$\begingroup\$ @Lynn thanks, fixed at whopping cost; maybe there is a shorter way than this. \$\endgroup\$ Sep 29 '16 at 14:35
  • \$\begingroup\$ -1 byte taking input as a string \$\endgroup\$ Feb 9 at 22:41
3
\$\begingroup\$

C#, 69 bytes

s=>s.Contains(".")?float.Parse(s)<1.4?"1.4":s:int.Parse(s)<18?"18":s;

Try it online!

Full program with test cases:

using System;

namespace YesImAnAdult
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string,string>f= s=>s.Contains(".")?float.Parse(s)<1.4?"1.4":s:int.Parse(s)<18?"18":s;
            
            Console.WriteLine(f("0"));  //18
            Console.WriteLine(f("1"));  //18
            Console.WriteLine(f("2"));  //18
            Console.WriteLine(f("12")); //18
            Console.WriteLine(f("18")); //18
            Console.WriteLine(f("43")); //43
            Console.WriteLine(f("122"));    //122

            Console.WriteLine(f("0.0"));    //1.4
            Console.WriteLine(f("1.04"));   //1.4
            Console.WriteLine(f("1.225"));  //1.4
            Console.WriteLine(f("1.399"));  //1.4
            Console.WriteLine(f("1.4"));    //1.4
            Console.WriteLine(f("1.74"));   //1.74
            Console.WriteLine(f("2.0"));    //2.0
            Console.WriteLine(f("2.72"));   //2.72
        }
    }
}

A pretty straightforward solution. Note that on some systems float.Parse() might return incorrect results. Pass CultureInfo.InvariantCulture as the second argument according to this answer.

\$\endgroup\$
4
  • \$\begingroup\$ Given the constraints i think you can get away with replacing s.Contains(".") with s[1]=='.' \$\endgroup\$
    – user19547
    Sep 29 '16 at 17:26
  • \$\begingroup\$ Hmm nvm, forgot the 0 test case. so close too :( \$\endgroup\$
    – user19547
    Sep 29 '16 at 17:29
  • 1
    \$\begingroup\$ @Phaeze: Yes, it would fail on any 1-digit input with an IndexOutOfRangeException. Otherwise you can shave a byte off with s[1]==46 ('.' has an ASCII code of 46), or an even more aggressive approach (presuming you only have digits and a '.' character at index 1): s[1]<47. \$\endgroup\$
    – adrianmp
    Sep 30 '16 at 6:39
  • 1
    \$\begingroup\$ Oo I like that, will try to remember. Thanks for turning my idiocy into a learning oppurtunity :) \$\endgroup\$
    – user19547
    Sep 30 '16 at 6:47
3
\$\begingroup\$

Dyalog APL, 14 bytes Rendered invalid by further specifications

⎕IO←0 which is default on many systems. Takes string as argument.

⍎⌈18 1.4⊃⍨'.'∘∊

⍎⌈ max of the evaluated argument and

18 1.4⊃⍨ {18,1.4} selected by

'.'∘∊ whether the argument contains a period

\$\endgroup\$
3
\$\begingroup\$

C++, 68 bytes

int A(int a){return a<18?18:a;}float A(float h){return h<1.4?1.4:h;}

This answer is actually 2 functions with the same name, and the compiler works out for me which to call, so it acts as one function without me having to take in one input and decide which it is. Since input on the floating point is guaranteed to be of the same precision as the output, I can return it safely without having to truncate it either.

Ungolfed + tests

#include <iostream>

int A(int a)
{
   return a < 18 ? 18 : a;
}

float A(float h)
{
   return h < 1.4 ? 1.4 : h;
}

int main()
{
  std::cout << 0 << " " << A(0) << "\n";
  std::cout << 19 << " " << A(19) << "\n";
  std::cout << 1.1 << " " << A(1.1f) << "\n";
  std::cout << 2.2 << " " << A(2.2f) << "\n";
}
\$\endgroup\$
4
  • \$\begingroup\$ User Szali Szali suggested saving two bytes by turning the floats into autos. I've rejected the edit according to policy but feel free to edit it in yourself if you've confirmed that it works. \$\endgroup\$ Oct 2 '16 at 17:33
  • \$\begingroup\$ All that text duplication! You can save several characters by generating the two definitions via macro. \$\endgroup\$
    – user16488
    Oct 3 '16 at 7:09
  • \$\begingroup\$ Thanks @MartinEnder. Their edit doesn't compile in all C++ compilers, and introduces all sorts of weird cases with other types suddenly being able to be passed in, so I'm going to accept my 2 bytes in order to keep my answer a little more portable. \$\endgroup\$
    – Cody
    Oct 3 '16 at 15:49
  • \$\begingroup\$ @Cody It's your call, but as far as PPCG is concerned languages are defined by their implementations, so answers don't need to be portable or particularly safe. As long as there's a compiler where the program works (and the required inputs work, regardless of whether invalid ones would work as well), that's completely fine. \$\endgroup\$ Oct 3 '16 at 15:50
3
\$\begingroup\$

C, 50 bytes:

#define A(x)(x/2+(x+1)/2-x?x<1.4?1.4:x:x<18?18:x)

The byte count includes the newline at the end of the macro definition.

Test:

#define A(x)(x/2+(x+1)/2-x?x<1.4?1.4:x:x<18?18:x)
#include <assert.h>
int main() {
  assert(A(0) == 18);
  assert(A(1) == 18);
  assert(A(2) == 18);
  assert(A(12) == 18);
  assert(A(18) == 18);
  assert(A(43) == 43);
  assert(A(115) == 115);
  assert(A(122) == 122);
  assert(A(0.0) == 1.4);
  assert(A(1.04) == 1.4);
  assert(A(1.225) == 1.4);
  assert(A(1.399) == 1.4);
  assert(A(1.4) == 1.4);
  assert(A(1.74) == 1.74);
  assert(A(2.0) == 2.0);
  assert(A(2.72) == 2.72);
}
\$\endgroup\$
3
\$\begingroup\$

C#, 71 bytes

object A(object i){return i is int?(int)i>18?i:18:(double)i>1.4?i:1.4;}

try it here

\$\endgroup\$
3
\$\begingroup\$

C, 119 111 105 100

m;f(char*s){float atof(),l=atof(s);for(m=s;*s&&*s++!=46;);puts(*s?l<1.4?"1.4":m:atoi(m)>18?m:"18");}

Tested with

main(c,v)char**v;{
    f("0");
    f("1");
    f("2");
    f("12");
    f("18");
    f("44");
    f("115");
    f("122");
    f("0.0");
    f("1.04");
    f("1.225");
    f("1.339");
    f("1.4");
    f("1.74");
    f("2.0");
    f("2.72");
}

Output

18
18
18
12
18
44
115
122
1.4
1.4
1.4
1.4
1.4
1.74
2.0
2.72
\$\endgroup\$
2
  • \$\begingroup\$ This is invalid... An input of 12 should output 18 \$\endgroup\$
    – Beta Decay
    Oct 2 '16 at 10:13
  • \$\begingroup\$ @BetaDecay you're right. I need to add an additional & character. Thanks for pointing that out. \$\endgroup\$
    – cleblanc
    Oct 3 '16 at 12:59
2
\$\begingroup\$

Batch, 102 bytes

@set/ps=
@if %s:.=%==%s% (if %s% lss 18 set s=18)else if %s:~0,1%%s:~2,1% lss 14 set s=1.4
@echo %s%

First determines whether the input is an integer by checking whether deleting all .s has any effect on the string. If it is then the value is readily compared against 18, otherwise the first and third characters are combined into a number which is compared against 14.

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

C#, 95 Bytes

Golfed:

string y(string p){int a;return int.TryParse(p,out a)?a>17?p:"18":double.Parse(p)<1.4?"1.4":p;}

Ungolfed:

class YesOfCourseImAnAdult
  {
    public string y(string p)
    {
      int a;
      return int.TryParse(p, out a) ? a > 17 ? p : "18"
       : double.Parse(p) < 1.4 ? "1.4" : p;
    }
  }

Test cases:

var codeGolf = new YesOfCourseImAnAdult();
Console.WriteLine(codeGolf.y("0"));
Console.WriteLine(codeGolf.y("1"));
Console.WriteLine(codeGolf.y("2"));
Console.WriteLine(codeGolf.y("12"));
Console.WriteLine(codeGolf.y("18"));
Console.WriteLine(codeGolf.y("43"));
Console.WriteLine(codeGolf.y("122"));

Console.WriteLine(codeGolf.y("0.0"));
Console.WriteLine(codeGolf.y("1.04"));
Console.WriteLine(codeGolf.y("1.225"));
Console.WriteLine(codeGolf.y("1.399"));
Console.WriteLine(codeGolf.y("1.4"));
Console.WriteLine(codeGolf.y("1.74"));
Console.WriteLine(codeGolf.y("2.0"));
Console.WriteLine(codeGolf.y("2.72"));

Output:

18
18
18
18
18
43
122

1.4
1.4
1.4
1.4
1.4
1.74
2.0
2.72
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Hi, welcome to PPCG! Your current approach can be shortened a bit like this: string y(string p){int a;return int.TryParse(p,out a)?a<1?"18":p:double.Parse(p)<1.4?"1.4":p;} (removed parenthesis; >=1.4 to <1.4 by swapping the "1.4" and p; changed decimal to double so the M is gone. Also, someone else just posted a different approach in C# that is slightly shorter. You might find Tips for golfing in C# interesting to read through. Again, welcome! :) \$\endgroup\$ Sep 29 '16 at 11:47
  • \$\begingroup\$ Hi, thanks for the helpful comments! I totally forgot about those extra brackets that I had in to stop myself losing track from the Ternary-Ternary operator! I've saved 5 bytes now overall. \$\endgroup\$
    – Pete Arden
    Sep 29 '16 at 14:30
  • \$\begingroup\$ You can save one byte by using float.Parse instead of double.Parse. And also if you move the declaration of a into the method arguments with a default value you can drop your return statement by using expression bodied member. eg: string f(string s,int a=0)=>int.TryParse(s,out a)?a>17?s:"18":float.Parse(s)<1.4?"1.4":s; \$\endgroup\$
    – user19547
    Sep 29 '16 at 17:36
2
\$\begingroup\$

PHP: 40 bytes

$i=is_int($i)?$i>17?$i:18:$i>1.4?$i:1.4;

psuedocode, (nested turnary) :

if (i is an integer) then 
  if (i is bigger than 17) then i=18 else i=i  
otherwise (its a decimal)   
  if (i is bigger than 1.4) then i=i else i=1.4 
end if 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to PPCG! Please note that input (by default) has to be via STDIN, function arguments, or full program arguments. \$\endgroup\$
    – aross
    Sep 30 '16 at 7:40

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.