-8
\$\begingroup\$

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
-------------------------
grade >= 7: Approved
grade >= 3: Summer School
grade  < 3: Failed

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

Fastest working answer wins.

\$\endgroup\$

closed as unclear what you're asking by ericw31415, NoOneIsHere, DJMcMayhem, a spaghetto, Rɪᴋᴇʀ May 13 '16 at 0:21

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ What about list indexing? \$\endgroup\$ – Volatility Jul 5 '13 at 5:40
  • \$\begingroup\$ @Volatility Tricky and worky, but ew... let's think of something better =P ("Unfortunately" would still count as a correct answer) \$\endgroup\$ – BernaMariano Jul 5 '13 at 5:45
  • 8
    \$\begingroup\$ 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. \$\endgroup\$ – Peter Taylor Jul 5 '13 at 7:19

10 Answers 10

5
\$\begingroup\$

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

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

Ruby

Using regular expressions:

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

Plain array indexing:

puts ["Failed","Summer School","Approved"][(gets.to_i+1)/4]
\$\endgroup\$
5
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

C

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

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

const char *grade_to_string(int grade)
{
    const char * grades[3] = { "Failed", "Summer school", "Approved" };
    return grades[min(((grade + 1) >> 2), 2)];
}

int main(int argc, char *argv[])
{
    int grade = atoi(argv[1]);
    printf("%d = %s\n", grade, grade_to_string(grade));
    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
$
\$\endgroup\$
  • \$\begingroup\$ I count one ternary operator and one if. \$\endgroup\$ – Peter Taylor Jul 5 '13 at 8:09
  • \$\begingroup\$ 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. ;-) \$\endgroup\$ – Paul R Jul 5 '13 at 9:42
  • \$\begingroup\$ 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 ;) \$\endgroup\$ – Peter Taylor Jul 5 '13 at 9:51
  • 1
    \$\begingroup\$ Challenge accepted ! \$\endgroup\$ – Paul R Jul 5 '13 at 9:53
2
\$\begingroup\$

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

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

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
-3
\$\begingroup\$

Java

public static String getFeedback(int grad) {
    String str = grad>=7? "Approved" : grad>=3 ?  "Summer School" : grad<3 ? "Failed"  : "";
    return str;
}
\$\endgroup\$
  • 6
    \$\begingroup\$ 1) That in not a program, just a snippet. At least post the whole method. 2) There are 3 ternary operators while only 1 is allowed. \$\endgroup\$ – manatwork Jul 5 '13 at 12:36

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