Yes, of course I'm an adult!

44

1

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

Kevin Cruijssen

Posted 2016-09-29T09:07:15.277

Reputation: 67 575

Can we assume that the input is free of leading zeros? – Toby Speight – 2016-09-29T12:45:12.617

@TobySpeight Yes, no leading zeros. – Kevin Cruijssen – 2016-09-29T12:48:20.770

20.0-2.72 (tallest man ever was 2.72). - You aren't 0.0 when you're born... – Johan Karlsson – 2016-09-29T13:49:53.713

1@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. – Kevin Cruijssen – 2016-09-29T13:54:31.967

According to meta, you can overload a function for different types of input. link

– corvus_192 – 2016-09-30T05:47:19.353

9"[...] so we just add a few years to the year of birth [...]" Shouldn't you subtract a few years from the year of birth? – wythagoras – 2016-10-02T17:35:30.257

@wythagoras Sharp. I'll edit it. – Kevin Cruijssen – 2016-10-03T13:42:27.987

Answers

3

Convex, 9 bytes

_1ÛI1.4?»

Try it online!

GamrCorps

Posted 2016-09-29T09:07:15.277

Reputation: 7 058

45

Python 2.7, 34 bytes

lambda x:max(x,[18,1.4]['.'in`x`])

Lynn

Posted 2016-09-29T09:07:15.277

Reputation: 55 648

What does this return for 2.0? – Adám – 2016-09-29T12:20:51.877

2@Adám max(2.0,[18,1.4][True]) == max(2.0,1.4) == 2.0 – Lynn – 2016-09-29T12:42:45.607

Doesn't it remove the decimal? – Adám – 2016-09-29T13:33:40.917

3Nope, it doesn’t. Why don’t you try it yourself? :) – Lynn – 2016-09-29T13:47:08.110

Because I don't have Python installed, and http://tryitonline.net/ doesn't include Python.

– Adám – 2016-09-29T14:08:03.373

5

@Adám I use https://repl.it or https://ideone.com for Python. View any Python answer I've ever posted, and it probably has a link to one of those two.

– mbomb007 – 2016-09-29T14:12:04.740

@matsjoyce That returns 18 when passed 1.0 – Lynn – 2016-09-29T19:48:06.543

@Lynn Whoops, sorry. I was looking at the JS answer and missed that the code was labeled "wrong". – matsjoyce – 2016-09-29T19:55:17.650

I'm not familiar with python. Can someone explain what's going on? – Alexandru Pupsa – 2016-10-03T14:44:02.950

1nevermind, got it. so basically true or false maps to 0 or 1 index in that array and then applies max on the two numbers. – Alexandru Pupsa – 2016-10-03T15:01:07.440

15

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

edc65

Posted 2016-09-29T09:07:15.277

Reputation: 31 086

2Result for 2.0 should be 2.0, not 18. – Neil – 2016-09-29T10:09:48.393

indeed. 2.0%1 and 1.0%1 will result in 0 – aross – 2016-09-29T10:11:51.267

4'javascript(es6) is verbose', you only see that at codegolf – dwana – 2016-09-29T10:56:23.397

@Neil on second thought you're probably right – edc65 – 2016-09-29T12:48:20.880

By my reckoning your solution is now as long as the verbose /./.test(x) but maybe x[1]<'0' is shorter? – Neil – 2016-09-29T13:36:08.907

@neil ... boring. The x+0-x seemed clever, it's a pity it fails with '0'. Thanks for the hint – edc65 – 2016-09-29T13:47:23.910

31/(x+x) - now that's imaginative! – Neil – 2016-09-29T17:03:35.207

13

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

Emigna

Posted 2016-09-29T09:07:15.277

Reputation: 50 798

2You're kinda slow. Still took you 1.5 minutes. ;P (read: Damn, that was fast.) It's pretty straightforward of course. – Kevin Cruijssen – 2016-09-29T09:12:06.933

2@KevinCruijssen: Yeah, the implementation is quite fast in a lang not requiring many key-presses :P – Emigna – 2016-09-29T09:13:02.703

@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. – Emigna – 2016-09-29T13:00:57.747

@Emigna I just added two missing crucial spaces. – Erik the Outgolfer – 2016-09-29T13:02:28.360

Is the input a decimal 0-1.4? Print 1.4. I just ran this and it returned 18. http://05ab1e.tryitonline.net/ – Francesco Casula – 2016-09-29T13:40:30.017

@FrancescoCasula: yes, does not work in the version that's on TIO. But in the offline compiler it does (and did before this challenge was posted). That is why I didn't add a TIO link. There are however at least one other 13 bytes solution that do work on TIO, but this was the first I thought of. – Emigna – 2016-09-29T13:42:32.087

2@FrancescoCasula: I found a shorter solution which does work on TIO :) – Emigna – 2016-09-29T13:53:03.880

8

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

Kevin Cruijssen

Posted 2016-09-29T09:07:15.277

Reputation: 67 575

Is it necessary to put brackets around the if/else operator? – Roman Gräf – 2016-09-29T18:18:56.487

1@RomanGräf Yes; the ternary has lower precedence than +, which means that if you removed the parentheses, it would turn into (""+i.contains(...)) ? – Fund Monica's Lawsuit – 2016-09-30T00:23:00.810

7

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);

Jörg Hülsermann

Posted 2016-09-29T09:07:15.277

Reputation: 13 026

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 – user59178 – 2016-09-29T16:05:46.837

@user59178 I could use is_numeric on a string but not is_int – Jörg Hülsermann – 2016-09-29T16:18:02.107

that's why there's the +0, to convert it to a numeric type. – user59178 – 2016-09-30T07:59:22.247

@user59178 Try 1.0+0 the result is 18 and should be 1.4 – Jörg Hülsermann – 2016-09-30T08:26:06.800

1It 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? – user59178 – 2016-09-30T10:43:46.623

1Actually, 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. – user59178 – 2016-09-30T11:01:46.313

@user59178 sorry it was too early to think. I'd forgot the swap. – Jörg Hülsermann – 2016-09-30T14:33:50.903

Mine is 5 bytes shorter, but somehow this one keeps getting the votes. – aross – 2016-10-03T08:21:18.910

@aross Nice version of advertisement. Your version works for the given test cases. I have expand my version to work with test cases like 11.0 – Jörg Hülsermann – 2016-10-03T14:32:18.227

@JörgHülsermann well that was unexpected... It says in the challenge that the range for length is 0.0-2.72, not anything above 10.0, so my answer is perfectly valid. – aross – 2016-10-03T14:45:50.023

@aross Don't worry. Sometimes it is better to concentrate giving a better solution as required else shorten the code until it exists no longer way – Jörg Hülsermann – 2016-10-03T14:54:53.160

@JörgHülsermann It's codegolf

– aross – 2016-10-04T09:39:38.023

7

Pyth, 14 bytes

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

Try it online.

PurkkaKoodari

Posted 2016-09-29T09:07:15.277

Reputation: 16 699

6

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

user56309

Posted 2016-09-29T09:07:15.277

Reputation:

1Also, 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. – Emigna – 2016-09-29T14:52:47.757

@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. – None – 2016-09-29T14:55:02.013

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 :) – Emigna – 2016-09-29T14:56:45.497

Hi! How do you calculate the bytes? Save the file with the formula with the default name or somehow else? – Vityata – 2016-09-30T09:30:44.377

1You 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. – Alexandru Pupsa – 2016-10-03T15:15:59.723

@AlexandruPupsa thanks! I edited it in. – None – 2016-10-03T15:37:40.490

5

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

Fatalize

Posted 2016-09-29T09:07:15.277

Reputation: 32 976

5

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

Ton Hospel

Posted 2016-09-29T09:07:15.277

Reputation: 14 114

5

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

Toby Speight

Posted 2016-09-29T09:07:15.277

Reputation: 5 058

5

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.

nimi

Posted 2016-09-29T09:07:15.277

Reputation: 34 639

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 – Cubic – 2016-09-30T21:24:26.027

@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. – nimi – 2016-09-30T21:42:59.883

Really cool idea saving the parens with infix fmap! – Cubic – 2016-09-30T21:48:52.023

5

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).

corvus_192

Posted 2016-09-29T09:07:15.277

Reputation: 1 889

1

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. :)

– Kevin Cruijssen – 2016-10-01T07:47:54.910

Yes, of course they are. How could I forgot them... – corvus_192 – 2016-10-01T21:28:46.507

I love this! Why code the logic yourself when you can offload that to the compiler! – corsiKa – 2016-10-03T17:03:05.010

4

CJam, 18 14 bytes

Saved 4 bytes thanks to Martin Ender.

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

Online interpreter

Neorej

Posted 2016-09-29T09:07:15.277

Reputation: 179

4

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

ElPedro

Posted 2016-09-29T09:07:15.277

Reputation: 5 301

4

Jelly, 16 15 13 bytes

ŒṘċ”.ị1.4,18»

TryItOnline
Or see all test cases, also at TryItOnline

How?

ŒṘċ”.ị1.4,18» - Main link: n
      1.4,18 - pair literals 1.4 and 18:   [1.4,18]
     ị       - index (1-based & modular ie:^  1, 0^)
  ċ          -     count (occurrences of)
   ”.        -         string "." (present in)
ŒṘ           -         string representation of n
           » - maximum (of this and n)

Jonathan Allan

Posted 2016-09-29T09:07:15.277

Reputation: 67 804

2This returns 18 for 2.0, sadly :( – Lynn – 2016-09-29T12:44:29.987

Ah, the complexity of Jelly is superior. – Erik the Outgolfer – 2016-09-29T13:00:57.747

@Lynn thanks, fixed at whopping cost; maybe there is a shorter way than this. – Jonathan Allan – 2016-09-29T14:35:45.877

4

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 

Mego

Posted 2016-09-29T09:07:15.277

Reputation: 32 998

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? – Kevin Cruijssen – 2016-09-29T11:57:09.913

3@KevinCruijssen 18 pushes a 1 and an 8. To push a literal 18, you'd use :18, which is longer than . – Mego – 2016-09-29T11:58:19.490

Ah of course, stack-based languages. Thanks for the explanation! +1 – Kevin Cruijssen – 2016-09-29T11:59:09.103

4

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";
}

Cody

Posted 2016-09-29T09:07:15.277

Reputation: 447

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.

– Martin Ender – 2016-10-02T17:33:27.190

All that text duplication! You can save several characters by generating the two definitions via macro. – None – 2016-10-03T07:09:21.037

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. – Cody – 2016-10-03T15:49:14.497

@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. – Martin Ender – 2016-10-03T15:50:31.770

4

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##}";

milk

Posted 2016-09-29T09:07:15.277

Reputation: 3 043

4

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.

Lord Yuuma

Posted 2016-09-29T09:07:15.277

Reputation: 587

4

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.

Cubic

Posted 2016-09-29T09:07:15.277

Reputation: 411

3

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.

adrianmp

Posted 2016-09-29T09:07:15.277

Reputation: 1 592

Given the constraints i think you can get away with replacing s.Contains(".") with s[1]=='.' – None – 2016-09-29T17:26:39.277

Hmm nvm, forgot the 0 test case. so close too :( – None – 2016-09-29T17:29:14.320

1@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. – adrianmp – 2016-09-30T06:39:15.063

1Oo I like that, will try to remember. Thanks for turning my idiocy into a learning oppurtunity :) – None – 2016-09-30T06:47:51.943

3

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

Adám

Posted 2016-09-29T09:07:15.277

Reputation: 37 779

3

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?

Robert Benson

Posted 2016-09-29T09:07:15.277

Reputation: 1 339

3

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

cleblanc

Posted 2016-09-29T09:07:15.277

Reputation: 3 360

This is invalid... An input of 12 should output 18 – Beta Decay – 2016-10-02T10:13:58.047

@BetaDecay you're right. I need to add an additional & character. Thanks for pointing that out. – cleblanc – 2016-10-03T12:59:33.133

3

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);
}

ecatmur

Posted 2016-09-29T09:07:15.277

Reputation: 1 675

3

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

grabthefish

Posted 2016-09-29T09:07:15.277

Reputation: 161

2

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.

Unrelated String

Posted 2016-09-29T09:07:15.277

Reputation: 5 300

2

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

Pete Arden

Posted 2016-09-29T09:07:15.277

Reputation: 1 151

1

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! :)

– Kevin Cruijssen – 2016-09-29T11:47:04.093

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. – Pete Arden – 2016-09-29T14:30:05.293

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; – None – 2016-09-29T17:36:38.157

2

Python 2, 49 bytes

lambda a:(a,1.4)[a<1.4]if"."in`a`else(a,18)[a<18]

ElPedro

Posted 2016-09-29T09:07:15.277

Reputation: 5 301

1Did it one byte shorter: lambda a:((a,18)[a<18],(a,1.4)[a<1.4])["."in`a`] – Erik the Outgolfer – 2016-09-29T10:51:37.593

Thanks @Erik. I tried that and it came out exactly the same length. Guess I must have had a stray space in there ;-) – ElPedro – 2016-09-29T10:54:39.080

Maybe. The most suspicious place is da a:((a, because, by instinct, you write da a: ((a instead. And it is a unique place. – Erik the Outgolfer – 2016-09-29T10:56:18.497

1lambda a:(max(a,18),max(a,1.4))["."in\a`]` is considerably shorter. – Mego – 2016-09-29T11:49:41.650

Thanks @Mego. Have used this idea to update my IBM/Lotus answer. Now nicely symetrical at 49 bytes each ;-) – ElPedro – 2016-09-29T12:20:19.863

Thanks @daHugLenny . I didn't know you could do that. Just checked out the source and will use that in future. Wonder if there is one for my Lotus Notes answer as well? :-) – ElPedro – 2016-09-29T16:19:43.180

1

@ElPedro I don't think so, but here's the list of languages you can use syntax highlighting with if you wanna check.

– acrolith – 2016-09-29T16:23:19.123

Guess I may have to contribute one myself. I'll probably be the only one on here using it anyway :-) – ElPedro – 2016-09-29T16:32:05.153

why not just use max and min? – Cyoce – 2017-07-02T22:14:03.753

2

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.

Neil

Posted 2016-09-29T09:07:15.277

Reputation: 95 035

2

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

aross

Posted 2016-09-29T09:07:15.277

Reputation: 1 583

2

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 

rb101

Posted 2016-09-29T09:07:15.277

Reputation: 31

1Welcome to PPCG! Please note that input (by default) has to be via STDIN, function arguments, or full program arguments. – aross – 2016-09-30T07:40:46.067

2

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

colsw

Posted 2016-09-29T09:07:15.277

Reputation: 3 195

1

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.

– AdmBorkBork – 2016-10-07T19:03:44.607

2

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);};

dpa97

Posted 2016-09-29T09:07:15.277

Reputation: 151

2Our standard is to allow any kind of functions, including lambdas. – corvus_192 – 2016-09-29T21:29:58.167

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. – corsiKa – 2016-10-02T19:56:09.203

@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
– dpa97 – 2016-10-03T16:56:30.443

@dpa97 Don't print. Return a string. – corsiKa – 2016-10-03T17:02:03.083

@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. – dpa97 – 2016-10-03T18:54:47.487

2

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.

Downgoat

Posted 2016-09-29T09:07:15.277

Reputation: 27 116

2

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}

Peter Lenkefi

Posted 2016-09-29T09:07:15.277

Reputation: 1 577

1Does not work for 1.0. – G B – 2017-03-03T16:49:48.600

2

Scala, 44 bytes

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

Defines two methods using overloading.

corvus_192

Posted 2016-09-29T09:07:15.277

Reputation: 1 889

1

Ruby, 31 bytes

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

Try it online!

G B

Posted 2016-09-29T09:07:15.277

Reputation: 11 099

1

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;
}

ben

Posted 2016-09-29T09:07:15.277

Reputation: 11

1

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!

l4m2

Posted 2016-09-29T09:07:15.277

Reputation: 5 985

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? – Kevin Cruijssen – 2018-04-05T15:13:31.957

@KevinCruijssen The compile flag is allowed in meta, I just didn't know if overloading is allowed – l4m2 – 2018-04-05T15:18:57.827

1

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).

histocrat

Posted 2016-09-29T09:07:15.277

Reputation: 20 600

1

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.

brhfl

Posted 2016-09-29T09:07:15.277

Reputation: 1 291

1

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.

dragonite44

Posted 2016-09-29T09:07:15.277

Reputation: 91

1

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

Hyarus

Posted 2016-09-29T09:07:15.277

Reputation: 251

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! :) – Kevin Cruijssen – 2018-04-06T08:17:09.957

1

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]

Blue

Posted 2016-09-29T09:07:15.277

Reputation: 26 661

1

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

rnso

Posted 2016-09-29T09:07:15.277

Reputation: 1 635

1

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.

Timtech

Posted 2016-09-29T09:07:15.277

Reputation: 12 038

1

GameMaker Language, 74 bytes

a=argument1;if real(a)<1.4a="1.4"show_message(string(max(argument0,18))+a)

Timtech

Posted 2016-09-29T09:07:15.277

Reputation: 12 038

1

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)

grabthefish

Posted 2016-09-29T09:07:15.277

Reputation: 161

1

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]}

skeevey

Posted 2016-09-29T09:07:15.277

Reputation: 4 139

1

Julia, 35 bytes

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

Using multiple dispatch.

Lyndon White

Posted 2016-09-29T09:07:15.277

Reputation: 1 021

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). – Glen O – 2016-10-02T12:43:49.897

@GlenO You're not wrong. – Lyndon White – 2016-10-02T14:30:31.683

1

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];

TuxCrafting

Posted 2016-09-29T09:07:15.277

Reputation: 4 547

1

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);

Karl Napf

Posted 2016-09-29T09:07:15.277

Reputation: 4 131