| Bytes | Lang | Time | Link |
|---|---|---|---|
| 068 | JavaScript Node.js | 241213T035839Z | l4m2 |
| 070 | JavaScript ES6 | 171211T173419Z | Arnauld |
| 051 | Perl 5 | 171211T193734Z | Xcali |
| 042 | Julia 0.6 | 171220T210737Z | LukeS |
| 249 | Java 1.8 | 171211T222719Z | Developi |
| 211 | Java | 171212T142324Z | PStigerI |
| 064 | R | 171211T200350Z | Giuseppe |
| 2112 | Jelly | 171211T203046Z | Jonathan |
| 039 | Wolfram Language Mathematica | 171211T172702Z | JungHwan |
| 066 | Python 3 | 171211T172025Z | totallyh |
| 069 | Python 2 | 171211T171629Z | Rod |
JavaScript (Node.js), 68 bytes
f=([c,d,...s],a=b=0)=>c?f(s,a-14%('0x'+c)%7*d,b-=d):(a/b).toFixed(2)
JavaScript (ES6), 72 70 bytes
Input format: A1B3C2F3B4
f=([c,d,...s],a=b=0)=>c?f(s,a-`0x${c+c}0`%7%6*d,b-=d):(a/b).toFixed(2)
Grade conversion
grade | hexa | decimal | mod 7 | mod 6
-------+-------+---------+-------+-------
A | 0xAA0 | 2720 | 4 | 4
B | 0xBB0 | 2992 | 3 | 3
C | 0xCC0 | 3264 | 2 | 2
D | 0xDD0 | 3536 | 1 | 1
F | 0xFF0 | 4080 | 6 | 0
Perl 5, 57 53 50 + 1 (-n) = 59 55 51 bytes
/ /;$c+=$';$\+=$'*(69+/F/-ord)}{printf'%.2f',$\/$c
Input format: line separated, credits followed by grade:
grade credits
Example:
A 3
B 4
A 1
A 1
Julia 0.6, 46 43 42 bytes
g%h=round(max.(69-Int.(g),0)⋅h/sum(h),2)
Explanation
Input format: g: vector of grades; h: vector of credit hours
g%h: Redefine%operator.69-Int.(g): Convert'F','D','C','B','A'to-1,1,2,3,4respectively for each element of g.max.( ,0): Clamp range to0:4(element-wise).- The rest is simple vector math.
- Rounding costs 9 bytes.
Java 1.8, 287 249 Bytes
-38 bytes thanks to Bumptious
Golfed
static String N(String[]j){float g=0;float b=0;for(int i=0;i<j.length;i+=2){g=((m(j[i])*Float.parseFloat(j[i+1])+g));b+=Double.parseDouble(j[i+1]);}return String.format("%.2f",g/b);}static float m(String l){return l.equals("F")?0:('E'-l.charAt(0));}
Ungolfed
interface C {
static void main(String[] z) throws Exception {
String[] j = {"A", "4", "B", "3", "C", "2", "D", "1", "F", "1"};
System.out.println(N(j));
}
static String N(String[] j) {
float g = 0;
float b = 0;
for (int i = 0; i < j.length; i += 2) {
g = ((m(j[i]) * Float.parseFloat(j[i + 1]) + g));
b += Double.parseDouble(j[i + 1]);
}
return String.format("%.2f", g / b);
}
static float m(String l) {
return l.equals("F") ? 0 : ('E' - l.charAt(0));
}
}
Java, 211 bytes
Input format: A1B3C2F3B4
Golfed
interface A{static void main(String[] a){int p=0,t=0,h=0,s=0;for(int c:a[0].toCharArray())if(p++%2==0)t=c=='A'?4:c=='B'?3:c=='C'?2:c=='D'?1:0;else{s+=(c-=48)*t;h+=c;}System.out.print(Math.ceil(100d*s/h)/100);}}
Unglofed
static void main(String[] a) {
int p=0, //position in string
t=0, //temp var, used to store the grade between iterations
h=0, //credit sum
s=0; //sum of weighted grade
for(int c:a[0].toCharArray())
if(p++%2==0)
//map c to grade value, assign to temp variable t
t=c=='A'?4:c=='B'?3:c=='C'?2:c=='D'?1:0;
else{
//map c to credit value, add temp variable (grade from previous char) * value of this char (credit) to sum
s+=(c-=48)*t;
//also, add credit to credit sum
h+=c;
}
System.out.print(Math.ceil(100d*s/h)/100); //grade sum / credit hours sum, to 2dp*/
}
Other version
My gut frealing told me that using a different input format (ABCF1324) would make the code shorter. It seems like it didn't. The version below is 234 bytes long.
Golfed
interface A{static void main(String[] b){char[] a=b[0].toCharArray();int l=a.length/2,h=0,s=0,g,c,i;for(i=0;i<l;i++){g=a[i];g=g=='A'?4:g=='B'?3:g=='C'?2:g=='D'?1:0;c=a[i+l]-48;s+=g*c;h+=c;}System.out.print(Math.ceil(100d*s/h)/100);}}a
Ungolfed
static void main(String[] b) {
char[] a=b[0].toCharArray(); //char array
int l=a.length/2, //first grade char
h=0, //credit sum
s=0, //sum of weighted grade
g,c, //avoid declaration in for loop. grade and credit being iterated
i; //avoid declaration in for loop
for(i=0;i<l;i++) {
g=a[i];//get char representing grade from array
g=g=='A'?4:g=='B'?3:g=='C'?2:g=='D'?1:0; //convert to grade
c=a[i+l]-48;//get char representing grade from array and convert to credit (48 is value of '0')
s+=g*c; //add weighted grade to sum
h+=c; //add credit to sum
}
System.out.print(Math.ceil(100d*s/h)/100); //grade sum / credit hours sum, to 2dp*/
}
R, 64 bytes
function(G,H)sprintf("%.2f",(5-match(G,LETTERS[-5]))%*%H/sum(H))
thanks to user2390246 for fixing a bug!
Jelly, 15 21 bytes (12 with no rounding)
+6 bytes for the strict formatting (almost certainly possible in less but it's bed time)
Oạ69.Ḟ×S×ȷ2÷⁹S¤RLDż”.
A full program taking the grades and the respective credit hours which prints the calculated GPA (Note: the rounding method is to floor, as allowed in the OP).
With no rounding for 12 bytes:
Oạ69.Ḟ×S÷⁹S¤
How?
Oạ69.Ḟ×S×ȷ2÷⁹S¤RLDż”. - Link: list of characters, grades; list of number, creditHours
- e.g. "AFBDC", [5, 2, 4, 1, 2]
O - cast to ordinals (vectorises) [65,70,66,68,67]
69. - literal 69.5
ạ - absolute difference (vectorises) [4.5,0.5,3.5,1.5,2.5]
Ḟ - floor (vectorises) [4,0,3,1,2]
× - multiply by creditHours (vectorises) [20,0,12,1,4]
S - sum 37
ȷ2 - literal 100
× - multiply 3700
¤ - nilad followed by link(s) as a nilad:
⁹ - chain's right argument, creditHours [5, 2, 4, 1, 2]
S - sum 14
÷ - division 264.2857142857143
R - range [1,2,3,...,264]
L - length 264
D - digits [2,6,4]
”. - literal '.'
ż - zip together [[2,'.'],6,4]
- implicit print (smashing) 2.64
Wolfram Language (Mathematica), 39 bytes
N[(5-LetterNumber@#2/.-1->0).#/Tr@#,3]&
Takes a list of credit hours, and then a string of grades.
Does not work on TIO because TIO uses Mathematica kernel (which doesn't want to print arbitrary precision numbers)
Python 3, 66 bytes
-5 bytes thanks to Rod.
lambda g,c:'%.2f'%sum('FDCBA'.find(i)*j/sum(c)for i,j in zip(g,c))
Python 2, 69 bytes
lambda x:'%.2f'%sum('FDCBA'.find(a)*b*1./sum(zip(*x)[1])for a,b in x)