Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I think we've all done this as a kid: some websites require a minimum age of 18, so we just add a few years to 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
share|improve this question
    
Yes, it's been in the Sandbox (for 7 days) – Kevin Cruijssen Sep 29 at 9:07
    
Can we assume that the input is free of leading zeros? – Toby Speight Sep 29 at 12:45
    
@TobySpeight Yes, no leading zeros. – Kevin Cruijssen Sep 29 at 12:48
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 Sep 29 at 13:54
2  
"[...] 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 8 hours ago

43 Answers 43

Python 2.7, 34 bytes

lambda x:max(x,[18,1.4]['.'in`x`])
share|improve this answer
    
What does this return for 2.0? – Adám Sep 29 at 12:20
2  
@Adám max(2.0,[18,1.4][True]) == max(2.0,1.4) == 2.0 – Lynn Sep 29 at 12:42
    
Doesn't it remove the decimal? – Adám Sep 29 at 13:33
1  
Nope, it doesn’t. Why don’t you try it yourself? :) – Lynn Sep 29 at 13:47
4  
@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. – mbomb007 Sep 29 at 14:12

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

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

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

share|improve this answer
2  
You're kinda slow. Still took you 1.5 minutes. ;P (read: Damn, that was fast.) It's pretty straightforward of course. – Kevin Cruijssen Sep 29 at 9:12
2  
@KevinCruijssen: Yeah, the implementation is quite fast in a lang not requiring many key-presses :P – Emigna Sep 29 at 9:13
    
@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 Sep 29 at 13:00
    
@Emigna I just added two missing crucial spaces. – Erik the Golfer Sep 29 at 13:02
2  
@FrancescoCasula: I found a shorter solution which does work on TIO :) – Emigna Sep 29 at 13:53

Java 7, 90 bytes

String c(String i){return""+(i.contains(".")?new Float(i)<1.4?1.4:i:new Long(i)<18?18:i);}

Ungolfed & test cases:

Try it here.

class M{
  static String c(String i){
    return ""+(i.contains(".")
                ? new Float(i) < 1.4
                   ? 1.4
                   : i
                : new Long(i) < 18
                   ? 18
                   : i);
  }

  public static void main(final String[] a) {
    System.out.println(c("0"));
    System.out.println(c("1"));
    System.out.println(c("2"));
    System.out.println(c("12"));
    System.out.println(c("18"));
    System.out.println(c("43"));
    System.out.println(c("122"));

    System.out.println(c("0.0"));
    System.out.println(c("1.04"));
    System.out.println(c("1.225"));
    System.out.println(c("1.399"));
    System.out.println(c("1.4"));
    System.out.println(c("1.74"));
    System.out.println(c("2.0"));
    System.out.println(c("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
share|improve this answer
    
Is it necessary to put brackets around the if/else operator? – Roman Gräf Sep 29 at 18:18
1  
@RomanGräf Yes; the ternary has lower precedence than +, which means that if you removed the parentheses, it would turn into (""+i.contains(...)) ? – QPaysTaxes Sep 30 at 0:23

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);
share|improve this answer
    
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 Sep 29 at 16:05
    
@user59178 I could use is_numeric on a string but not is_int – Jörg Hülsermann Sep 29 at 16:18
    
that's why there's the +0, to convert it to a numeric type. – user59178 2 days ago
1  
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? – user59178 2 days ago
1  
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. – user59178 2 days ago

Pyth, 14 bytes

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

Try it online.

share|improve this answer

EXCEL: 26 31 Bytes

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

Formula can go anywhere except A1, the input cell.

Fixed errors, and replaced with Emigna's suggestion.

share|improve this answer
1  
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. – Emigna Sep 29 at 14:52
    
@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. – tuskiomi Sep 29 at 14:55
    
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 Sep 29 at 14:56
    
Hi! How do you calculate the bytes? Save the file with the formula with the default name or somehow else? – Vityata 2 days ago

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

share|improve this answer

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
share|improve this answer

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
share|improve this answer

CJam, 18 14 bytes

Saved 4 bytes thanks to Martin Ender.

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

Online interpreter

share|improve this answer

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.

share|improve this answer
    
Given the constraints i think you can get away with replacing s.Contains(".") with s[1]=='.' – Phaeze Sep 29 at 17:26
    
Hmm nvm, forgot the 0 test case. so close too :( – Phaeze Sep 29 at 17:29
    
@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 2 days ago
    
Oo I like that, will try to remember. Thanks for turning my idiocy into a learning oppurtunity :) – Phaeze 2 days ago

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.

share|improve this answer
    
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 2 days ago
    
@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 2 days ago
    
Really cool idea saving the parens with infix fmap! – Cubic 2 days ago

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";
}
share|improve this answer
    
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 8 hours ago

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 
share|improve this answer
    
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 Sep 29 at 11:57
3  
@KevinCruijssen 18 pushes a 1 and an 8. To push a literal 18, you'd use :18, which is longer than . – Mego Sep 29 at 11:58
    
Ah of course, stack-based languages. Thanks for the explanation! +1 – Kevin Cruijssen Sep 29 at 11:59

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

share|improve this answer

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

share|improve this answer

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
share|improve this answer
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 Sep 29 at 11:47
    
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 Sep 29 at 14:30
    
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; – Phaeze Sep 29 at 17:36

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)
share|improve this answer
2  
This returns 18 for 2.0, sadly :( – Lynn Sep 29 at 12:44
    
Ah, the complexity of Jelly is superior. – Erik the Golfer Sep 29 at 13:00
    
@Lynn thanks, fixed at whopping cost; maybe there is a shorter way than this. – Jonathan Allan Sep 29 at 14:35

C, 119 111 105 99

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
share|improve this answer
    
This is invalid... An input of 12 should output 18 – Beta Decay 16 hours ago

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.

share|improve this answer

PHP, 36 35 bytes

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

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 '<?=max($a=$argv[1],$a[1]&p?18:1.4);' | php -- 11 2>/dev/null;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.

share|improve this answer

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 
share|improve this answer
1  
Welcome to PPCG! Please note that input (by default) has to be via STDIN, function arguments, or full program arguments. – aross 2 days ago

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
share|improve this answer

Python 2, 49 bytes

lambda a:(a,1.4)[a<1.4]if"."in`a`else(a,18)[a<18]
share|improve this answer
1  
Did it one byte shorter: lambda a:((a,18)[a<18],(a,1.4)[a<1.4])["."in`a`] – Erik the Golfer Sep 29 at 10:51
    
Thanks @Erik. I tried that and it came out exactly the same length. Guess I must have had a stray space in there ;-) – ElPedro Sep 29 at 10:54
    
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 Golfer Sep 29 at 10:56
1  
lambda a:(max(a,18),max(a,1.4))["."in`a`] is considerably shorter. – Mego Sep 29 at 11:49
1  
@ElPedro I don't think so, but here's the list of languages you can use syntax highlighting with if you wanna check. – daHugLenny Sep 29 at 16:23

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);};
share|improve this answer
2  
Our standard is to allow any kind of functions, including lambdas. – corvus_192 Sep 29 at 21:29
    
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 6 hours ago

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);
}
share|improve this answer

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##}";
share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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