0
\$\begingroup\$

Write a program or a function in your favorite programming language that will take as input a number n (integer or non-integer) between 0 and 100 inclusive, and output the corresponding grade as outlined below:

  • If 0 < n <= 40, then output E.
  • If 40 < n <= 55, then output D.
  • If 55 < n <= 65, then output C.
  • If 65 < n <= 70, then output C+.
  • If 70 < n <= 75, then output B-.
  • if 75 < n <= 80, then output B.
  • If 80 < n <= 85, then output B+.
  • If 85 < n <= 90, then output A-.
  • If 90 < n <= 95, then output A.
  • If 95 < n <= 100, then output A+.

You are guaranteed that there are no other cases to test, and that n is guaranteed to be within 0 and 100.

The input n can be provided via stdin or as an argument to the program.

Lines in the output are not allowed to have trailing spaces and/or newlines. You must output exactly the corresponding grade, nothing else.

Shortest code in bytes wins!

\$\endgroup\$
  • 1
    \$\begingroup\$ Odd, I've never received an E grade before... Is that for ease of coding, or did you intend F? \$\endgroup\$ – Steven H. Aug 11 '16 at 2:05
  • \$\begingroup\$ @StevenH. I actually do mean E there (that's how my university denotes failing grades). \$\endgroup\$ – Ryan Aug 11 '16 at 2:06
  • \$\begingroup\$ Some education systems use E as the absolute lowest without an F \$\endgroup\$ – Quill Aug 11 '16 at 2:06
  • 1
    \$\begingroup\$ I think we've already had a challenge to convert numbers grades to letters. \$\endgroup\$ – xnor Aug 11 '16 at 2:26
2
\$\begingroup\$

Python, 74 66 73 bytes

lambda n,l="E "*8+"D D D C C C+ B- B B+ A- A A+ A+":l.split()[int(n-1)/5]

Unnamed lambda, uses a list of all grades in groups of 5. (I'm sure there's a way to handle n=100 without adding the extra 'A+' at the end, I'm just not sure how)

Edit: Thanks to @LeakyNun for 8 bytes

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Question says n could be non-integer? \$\endgroup\$ – Neil Aug 11 '16 at 2:25
  • \$\begingroup\$ Logically you shouldn't have to add an extra A+ at the end. This gives wrong output for 95.1 \$\endgroup\$ – Leaky Nun Aug 11 '16 at 2:50
  • \$\begingroup\$ Instead of doing (n-1)/5, you can do 20-n/5 which shifts thing correctly \$\endgroup\$ – Leaky Nun Aug 11 '16 at 2:50
  • \$\begingroup\$ @LeakyNun Could you post a fuller example of what you mean? Because substituting 20-n/5 still gives a bunch of weird behavior at "edge-numbers" \$\endgroup\$ – Theo Aug 11 '16 at 3:03
  • \$\begingroup\$ @Theo Have a look at Neil's answer. \$\endgroup\$ – Leaky Nun Aug 11 '16 at 3:04
1
\$\begingroup\$

JavaScript (ES6), 57 bytes

n=>`A+ A A- B+ B B- C+ C C D D D`.split` `[20-n/5|0]||`E`
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Pyth, 36 bytes

?<Q41\E+C/-1075Q15?<Q66k-@"-+ "/_Q5d

Test suite.

Explanation

Here is the equivalent program in Python 3.

lambda Q:[chr(int(1075-Q)//15)+["-+ "[int(99-Q/5)%3],""][Q<66],"E"][Q<41].split()[0]

Ideone it!

| improve this answer | |
\$\endgroup\$

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