英国伦敦大学-复试题目「Python」

AI悦创原创2022年7月20日
大约 3 分钟

题目

class Grade:
    def __init__(self, letter, mark):
        self.letter = letter
        self.mark = mark

studentMarks = {'Ann': Grade('B', 65), 'Bo': Grade('A', 70), 'Cam':Grade('C', 50), 'Dee': Grade('B', 62)}

问题:

  1. 找出学生中的最高分是谁?
  2. 找出学生们的平均分?
  3. 超出 ABCDF 出现的频率?

答案

class Grade(object):
    def __init__(self, letter, mark):
        """
        first:存自己本身的数据
        rest:存下一个节点,也就下一个节点是谁
        """
        self.letter = letter
        self.mark = mark
        self.rest = None
    
    def size(self):
        """获取链表长度"""
        if self.rest is None:
            return 1
        else:
            return 1 + self.rest.size()
    
    def max_value(self):
        """寻找最大值"""
        """成功successful"""
        p = self
        max_value = int()
        while p is not None:
            if max_value < p.mark:
                max_value = p.mark
            p = p.rest
        return max_value
        """successful"""
        
        # max_value = self.mark
        # max_value = self.mark
        # if self.rest is None:
        #     if self.mark > max_value:
        #         max_value = self.mark
        #         return max_value
        # elif max_value < self.rest.mark:
        #     max_value = self.rest.mark
        #     return self.rest.max_value(max_value)
        # else:
        #     return self.rest.max_value(max_value)
    
    def average(self, total_num):
        """平均数"""
        p = self
        sum_num = 0
        while p is not None:
            sum_num += p.mark
            p = p.rest
        return sum_num / total_num
    
    def frequency(self):
        p = self
        letter_dict = {}
        while p is not None:
            if p.letter in letter_dict:
                letter_dict[p.letter] += 1
            else:
                letter_dict[p.letter] = 1
            p = p.rest
        return letter_dict
        
studentMarks = {
    'Ann': Grade('B', 65),
    'Bo': Grade('A', 70),
    'Cam': Grade('C', 50),
    'Dee': Grade('B', 62)
}
# studentMarks["Ann"].rest = studentMarks["Bo"]
# studentMarks["Bo"].rest = studentMarks["Cam"]
# studentMarks["Cam"].rest = studentMarks["Dee"]
# print(studentMarks["Cam"].size())
l1 = studentMarks["Ann"]
l2 = studentMarks["Bo"]
l3 = studentMarks["Cam"]
l4 = studentMarks["Dee"]
l1.rest = l2
l2.rest = l3
l3.rest = l4
print(l1.size())
print(l1.max_value())
print(l1.average(l1.size()))
print(l1.frequency())
print("l1:>>>", l1.mark)
print("l2:>>>", l1.rest.mark)
print("l3:>>>", l1.rest.rest.mark)
print("l4:>>>", l1.rest.rest.rest.mark)

上课代码

class Grade():
    def __init__(self, letter, mark):
        self.letter = letter
        self.mark = mark
        self.rest = None
    
    def size(self):
        if self.rest is None:
            return 1
        else:
            return 1 + self.rest.size()
    
    def max_value(self):
        p = self
        # p.mark == self.mark
        max_value = 0
        while p is not None:
            if max_value < p.mark:  # p.rest.mark >>> self.rest.mark
                max_value = p.mark
            # p.rest.mark
            p = p.rest
        return max_value
    
    def average(self, total_num):
        """平均数"""
        p = self
        sum_num = 0
        while p is not None:
            sum_num += p.mark
            p = p.rest
        return sum_num / total_num
    
    def frequency(self):
        p = self
        letter_frequency = {}
        while p is not None:
            if p.letter in letter_frequency:
                letter_frequency[p.letter] += 1
            else:
                letter_frequency[p.letter] = 1
        return letter_frequency


studentMarks = {
    'Ann': Grade('B', 65),
    'Bo': Grade('A', 70),
    'Cam': Grade('C', 500),
    'Dee': Grade('B', 62)
}
# 链表
l1 = studentMarks["Ann"]
l2 = studentMarks["Bo"]
l3 = studentMarks["Cam"]
l4 = studentMarks["Dee"]
l1.rest = l2
l2.rest = l3
l3.rest = l4
print(l1.size())
print(l1.max_value())

欢迎关注我公众号:AI悦创,有更多更好玩的等你发现!

公众号:AI悦创【二维码】

AI悦创·编程一对一

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh

C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh

方法一:QQopen in new window

方法二:微信:Jiabcdefh