# Write a program with one & only if statement that [closed]

## Challenge: You Are The Teacher

Everyone knows that simple task of receiving a grade, and telling if the student:

• is approved,
• will attend the summer school, or
• failed.

In this challenge, you must write a program that does that, but using the maximum of one if statement (also ternary operators count as if).

The grade will be an integer from 0 to 10.

situation   outputs
-------------------------


You can do whatever you want, write in any language.

• What about list indexing? Jul 5, 2013 at 5:40
• @Volatility Tricky and worky, but ew... let's think of something better =P ("Unfortunately" would still count as a correct answer) Jul 5, 2013 at 5:45
• This problem is far too trivial to be assessable on speed. To be able to compare performance with any confidence, you need the programs to run for a minute, not for about ten clock cycles. Jul 5, 2013 at 7:19

### Ruby

Using regular expressions:

puts "Failed012Summer School3456Approved78910"[/\D*(?=\d*#{gets.chomp})/]


Plain array indexing:

puts ["Failed","Summer School","Approved"][(gets.to_i+1)/4]


### Ruby

def x(mark)
possible_results = ['','Failed', 'Failed', 'Summer school', 'Approved']

index = (mark + 1).to_s(2).length

possible_results[index]
end


This uses the fact that the length of the binary representation of the numbers 0-10 is pretty close to the required classification.

Online test: http://ideone.com/y0kLqY

Python2: 65

Bonus: it doesn't use any conditional expression.

g=input();print("Approved","Summer School","Failed")[(g<7)+(g<3)]


Example output:

>>> g=input();print("Approved","Summer School","Failed")[(g<7)+(g<3)]
10
Approved
>>> g=input();print("Approved","Summer School","Failed")[(g<7)+(g<3)]
7
Approved
>>> g=input();print("Approved","Summer School","Failed")[(g<7)+(g<3)]
6
Summer School
>>> g=input();print("Approved","Summer School","Failed")[(g<7)+(g<3)]
3
Summer School
>>> g=input();print("Approved","Summer School","Failed")[(g<7)+(g<3)]
2
Failed


# C

#include <stdio.h>
#include <stdlib.h>

int min(int a, int b)
{
return (a < b) * a + (a >= b) * b;
}

{
const char * grades[3] = { "Failed", "Summer school", "Approved" };
}

int main(int argc, char *argv[])
{
return 0;
}


Test:

$gcc -Wall -O3 grade.c$ time for i in $(seq -1 20) ; do ./a.out$i ; done
-1 = Failed
0 = Failed
1 = Failed
2 = Failed
3 = Summer school
4 = Summer school
5 = Summer school
6 = Summer school
7 = Approved
8 = Approved
9 = Approved
10 = Approved
11 = Approved
12 = Approved
13 = Approved
14 = Approved
15 = Approved
16 = Approved
17 = Approved
18 = Approved
19 = Approved
20 = Approved

real    0m0.120s
user    0m0.025s
sys     0m0.042s
$ • I count one ternary operator and one if. Jul 5, 2013 at 8:09 • Well main is just a test harness, so I don't count the if in that - I'll remove it though if it makes you happy. ;-) Jul 5, 2013 at 9:42 • The spec asks for a program, so I think you have to count main. It would be more interesting, though, to keep that if and remove the one in min ;) Jul 5, 2013 at 9:51 • Challenge accepted ! Jul 5, 2013 at 9:53 ## Perl sub f { my$grade = shift;
my $result = 0; for (++$grade ; $grade > 1;$grade >>= 1) {
++$result; } return ("Failed", "Failed", "Summer school", "Approved")[$result];
}


(The for-loop test constitutes the program's one permitted "if statement".)

# R

g=function(x)cut(x,c(0,3,7,10),
labels=c("Failed","Summer school","Approved"),
right=FALSE, include=TRUE)


Usage:

> g(2)
[1] Failed
Levels: Failed Summer school Approved
> g(7)
[1] Approved
Levels: Failed Summer school Approved
> g(3)
[1] Summer school
Levels: Failed Summer school Approved


Function cut in R converts a numeric into a factor (categorical) according to the interval in which it falls. It is vectorized:

> g(0:10)
[1] Failed       Failed       Failed       Summer school Summer school Summer school Summer school
[8] Approved      Approved      Approved      Approved
Levels: Failed Summer school Approved


# Awk

{
print check($1,0,2,"Failed") check($1,3,6,"Summer School") check($1,7,10,"Approved") } function check(grade,lower,upper,result) { if (grade>=lower && grade<=upper) return result }  (There are enough clever solutions, so I felt the need to post a brute force one too.) Sample run: bash-4.1$ for i in {0..10}; do echo -n $i\ ; awk grade.awk <<<$i; done
0 Failed
1 Failed
2 Failed
3 Summer School
4 Summer School
5 Summer School
6 Summer School
7 Approved
8 Approved
9 Approved
10 Approved


## APL

'Failed' 'Summer School' 'Approved'⌷⍨+/⎕≥0 3 7


Explanation:

• ⎕≥0 3 7: read input, see if it is bigger or equal to 0, 3 and 7 (this gives a vector of bits, i.e. if the input is 5 this gives 1 1 0)
• +/: sum the vector (giving an index into the list)
• ⌷⍨: select the corresponding string from the list

Using Javascript, Thanks to @Bakuriu

function decision(g) {
var d = ["Approved","Summer School","Failed"];
return d[(g<7)+(g<3)]
}

console.log(decision(2)); ==> Failed
console.log(decision(5)); ==> Summer School
console.log(decision(9)); ==> Approved
console.log(decision(1)); ==> Failed
console.log(decision(4)); ==> Summer School


# Java

public static String getFeedback(int grad) {