陷入困境的大学生的GPA计算器


13

GPA计算器

(GPA =平均绩点)

在决赛周,你是一个压力很大的大学生。您无需决定即将进行的考试,而是决定最好在学期末确定将获得的GPA。这样,您就有数据来支持您熬夜决定在微积分中获得A而不是B保留在院长名单上的决定!

作为计算机科学专业的学生,​​您想找到一种确定该GPA的最酷方法。当然,最酷的方法是用最短的代码!这是,因此以字节为单位的最短代码胜出!

细节

  • 您要去的大学使用基本的GPA和学分。
  • A的字母等级为4.0,B为3.0,C为2.0,D为1.0,F为0.0
  • 您的GPA是加权GPA,因此4个学分时级的A的数量是1个学分时级的A的4倍(请参见下面的示例以了解更多的体重说明)
  • 学时从1-4
  • 您的程序将需要包含两个命令行输入的列表,即成绩和学分。您可以确定通过命令行将它们输入到程序中的最佳方法。您无需担心输入过多,但是请确保您的代码可以处理19个学时的学期。

    • 即输入:A 1 B 4 C 2…
  • 您的程序必须使用3位数(即X.XX)输出GPA。

  • 您的GPA需要四舍五入到小数点后两位。以您喜欢的任何方式(地板,天花板,底座等)四舍五入

输入示例(选择最适合您的设计的输入示例)

  • A1B3C2F3B4
  • A1 B3 C2 F3 B4
  • A 1 B 3 C 2 F 3 B 4
  • A,1,B,3,C,2,F,3,B,4
  • A1,B3,C2,F3,B4

或以上任何一种组合,其中您使用列出所有成绩的格式,然后列出其学时:

  • 即ABAA 3 4 1 1

例子

Input - A 3 B 4 A 1 A 1
Output - 3.56
Explanation: (4.0 * 3 + 3.0 * 4 + 4.0 * 1 + 4.0 * 1)/(3+4+1+1) = 3.555556 rounded off to 3.56 

Input - A 4 F 2 C 3 D 4
Output - 2.00
Explanation: (4.0 * 4 + 0.0 * 2 + 2.0 * 3 + 1.0 * 4)/(4+2+3+4) = 2 rounded off to 2.00

2
@DevelopingDeveloper请尝试避免使用繁琐的I / O格式
JungHwan Min

1
@JungHwanMin请让我知道您未指定的I / O选项吗?我提供了大约6种不同的灵活选项,但需要指定它才能真正适合这种情况。
DevelopingDeveloper

3
@DevelopingDeveloper Your GPA needs to be rounded to two decimal places:要实现这一点,人们需要添加与GPA计算无关的其他代码。
JungHwan Min

2
欢迎来到PPCG。以我的拙见,很好的第一个问题。
ElPedro '17

5
尽管这是一个非常有趣的问题,但我还是投下了赞成票,因为正如我之前说过的那样,强制以舍入0进行舍入和输出使这成为一个多方面的挑战。答案是必须不必要地增加字节,只是为了符合输出规范,这将使挑战变得无聊且使整个IMO变得更糟。
caird coinheringaahing

Answers:


5

果冻 15  21 字节(12不取整)

+6个字节用于严格格式化(几乎肯定可以用更少的时间,但这是就寝时间)

Oạ69.Ḟ×S×ȷ2÷⁹S¤RLDż”.

包含成绩和相应学分的完整程序,可以打印出计算出的GPA(注意:四舍五入方法是根据操作数的限制)。

在线尝试!

没有舍入为12个字节

Oạ69.Ḟ×S÷⁹S¤

怎么样?

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

@nimi-好点,我错过了那一点。固定。
乔纳森·艾伦


4

Perl 5中57 53 + 2(-an)= 59 55个字节

$c+=$F[1];$\+=$F[1]*=!/F/&&69-ord}{printf'%.2f',$\/$c

在线尝试!

编辑:交换输入以节省4个字节

输入格式:行分隔,学分后跟成绩:

grade credits

例:

A 3
B 4
A 1
A 1


3

Wolfram语言(Mathematica),39个字节

N[(5-LetterNumber@#2/.-1->0).#/Tr@#,3]&

列出一个学分,然后是一连串的成绩。

在TIO上不起作用,因为TIO使用Mathematica内核(不想打印任意精度数字)


2
-3个字节(如果您使用)Tr

3
这对于第二个测试用例失败。它返回1.85,因为您计数F = -1
J42161217 '12

2
这是一个41bytes的修复程序:N[(5-(LetterNumber@#2/. 6->5)).#/Tr@#,3]&
J42161217 '12

@Jenny_mathy好收获。我不知道我是怎么想念的...但是,通过移动/.,可以使多余的括号掉下来。
JungHwan Min

2

JavaScript(ES6),72个字节

输入格式: A1B3C2F3B4

f=([c,d,...s],a=b=0)=>c?f(s,a+~'DCBA'.search(c,b-=d)*d):(a/b).toFixed(2)

测试用例


在什么时候search变得比高尔夫球手parseInt
尼尔,

@Neil parseInt可能会成为高尔夫球手,并且只有几个受支持的成绩。但是,一个问题是F = 0和D = 1之间的差距。
Arnauld

,我什至没有注意到没有E ...
Neil


2

Java,211字节

输入格式:A1B3C2F3B4

打高尔夫球

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);}}

不戴手套

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*/
}

其他版本

我的直觉告诉我,使用其他输入格式(ABCF1324)将使代码更短。好像没有。下面的版本长234个字节。

打高尔夫球

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

不打高尔夫球

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*/
}

你好,欢迎光临!您不需要回答完整的程序,因此可以减少很多字节。
妮莎(Nissa)

2

的Java 1.8287个 249字节

-38字节的感谢

打高尔夫球

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));}

不打高尔夫球

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));
}
}

1
通过使用这个来减少长度?静态浮点数m(字符串l){返回l.equals(“ F”)?0 :('E'-l.charAt(0));}
夸张的Q Bangwhistle

1

朱0.646个43 42字节

g%h=round(max.(69-Int.(g),0)⋅h/sum(h),2)

在线尝试!

说明

输入格式::g等级向量;h:学时的向量

  • g%h:重新定义%运算符。
  • 69-Int.(g):分别转换'F','D','C','B','A'-1,1,2,3,4g的每个元素。
  • max.( ,0):将范围钳到0:4(按元素)。
  • 其余的是简单的矢量数学。
  • 舍入为9个字节。
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.