【每日一题0928】魔方比赛成绩

:woman_mage: 在大部分的魔方速拧比赛中,会记录一个选手的5次成绩,平均成绩的方式为去掉最高分和最低分后的平均分。请编写一个函数,给定一个包含5个浮点数的成绩列表times,求出他的平均成绩和最佳成绩(最多保留2位小数)。

示例:
输入: [9.5, 7.6, 11.4, 10.5, 8.1],输出: (9.37, 7.6)
因为平均成绩是:(9.5 + 10.5 + 8.1) / 3 = 9.37,最佳成绩是:7.6

题目难度:简单
题目来源:codewars

def cube_times(times: list) -> tuple:
    pass

assert cube_times([9.5, 7.6, 11.4, 10.5, 8.1]) == (9.37, 7.6)
assert cube_times([13.4, 12.3, 9.5, 11.9, 20.8]) == (12.53, 9.5)
assert cube_times([28.3, 14.5, 17.3, 8.9, 10.1]) == (13.97, 8.9)
def cube_times(times: list) -> tuple:
    min_score = min(times)
    max_score = max(times)
    while max_score in times:
        times.remove(max(times))
    while min_score in times:
        times.remove(min(times))
    return round(sum(times)/len(times),2),min_score
1 Like
 public List<String> cubeTimes(List<Double> list) {
        List<String> res = new ArrayList<>();
        Double max = list.stream().mapToDouble(x -> x).max().getAsDouble();
        List<Double> collect = list.stream().sorted(Double::compareTo).collect(Collectors.toList());
        double asDouble = collect.subList(1, collect.size() - 1).stream().mapToDouble(x -> x).average().getAsDouble();
        res.add(0, String.format("%.2f", asDouble));
        res.add(1, String.format("%.2f", max));
        return res;


    }

    @Test
    public void testCubeTimes() {
        //cubeTimes(Arrays.asList(9.5, 7.6, 11.4, 10.5, 8.1)).stream().forEach(System.out::println);
        assert cubeTimes(Arrays.asList(9.5, 7.6, 11.4, 10.5, 8.1)).equals(Arrays.asList("9.37","11.40"));
        assert cubeTimes(Arrays.asList(13.4, 12.3, 9.5, 11.9, 20.8)).equals(Arrays.asList("12.53","20.80"));
        assert cubeTimes(Arrays.asList(28.3, 14.5, 17.3, 8.9, 10.1)).equals(Arrays.asList("13.97","28.30"));
    }
"""
__author__ = '晨&宇'
__time__ = '2021/9/28 21:23'
 在大部分的魔方速拧比赛中,会记录一个选手的5次成绩,平均成绩的方式为去掉最高分和最低分后的平均分。请编写一个函数,
 给定一个包含5个浮点数的成绩列表times,求出他的平均成绩和最佳成绩(最多保留2位小数)。
示例:
输入: [9.5, 7.6, 11.4, 10.5, 8.1],输出: (9.37, 7.6)。
因为平均成绩是:(9.5 + 10.5 + 8.1) / 3 = 9.37,最佳成绩是:7.6。
题目难度:简单
题目来源:codewars 1
"""


def cube_times(times: list) -> tuple:
    list_a = []
    x = min(times)
    times.sort()
    times.pop(0)
    times.pop(-1)
    s = 0
    for i in times:
        s += i
    y = round(s / len(times), 2)
    list_a.append(y)
    list_a.append(x)
    return tuple(list_a)


assert cube_times([9.5, 7.6, 11.4, 10.5, 8.1]) == (9.37, 7.6)
assert cube_times([13.4, 12.3, 9.5, 11.9, 20.8]) == (12.53, 9.5)
assert cube_times([28.3, 14.5, 17.3, 8.9, 10.1]) == (13.97, 8.9)
def cube_times(times: list) -> tuple:
    times.sort(reverse=True)
    times.pop(0)
    min=times.pop(-1)
    sum=0
    for i in times:
        sum+=float(i)
    avg=round(sum/len(times),2)
    return avg,min


def test_cube_times():
    assert cube_times([9.5, 7.6, 11.4, 10.5, 8.1]) == (9.37, 7.6)
    assert cube_times([13.4, 12.3, 9.5, 11.9, 20.8]) == (12.53, 9.5)
    assert cube_times([28.3, 14.5, 17.3, 8.9, 10.1]) == (13.97, 8.9)
def cube_times(times: list) -> tuple:
    return (float("{:.2f}".format((sum(times)- min(times) - max(times))/(len(times)-2))) , min(times))

assert cube_times([9.5, 7.6, 11.4, 10.5, 8.1]) == (9.37, 7.6)
assert cube_times([13.4, 12.3, 9.5, 11.9, 20.8]) == (12.53, 9.5)
assert cube_times([28.3, 14.5, 17.3, 8.9, 10.1]) == (13.97, 8.9)
def cube_times(times: list) -> tuple:
    # 获取列表长度
    n = len(times)-1
    m = n
    # 用冒泡算法排序
    for i in range(0, n):
        for j in range(0, m):
            if times[j] > times[j+1]:
                times[j], times[j + 1] = times[j + 1], times[j]
        # 优化冒泡算法代码
        m = m - 1
    # 计算掐头去尾的平均值,返回两位小数
    avg = round(sum(times[1:-1])/(len(times)-2), 2)
    # 获取最佳成绩
    min = times[0]
    # 返回元组(平均成绩,最佳成绩)
    return (avg, min)


assert cube_times([9.5, 7.6, 11.4, 10.5, 8.1]) == (9.37, 7.6)
assert cube_times([13.4, 12.3, 9.5, 11.9, 20.8]) == (12.53, 9.5)
assert cube_times([28.3, 14.5, 17.3, 8.9, 10.1]) == (13.97, 8.9)
def cube_times(times: list) -> tuple:
    min_time = min(times)
    avg_time = round((sum(sorted(times)[1:4]) / 3), 2)
    return (avg_time, min_time)


assert cube_times([9.5, 7.6, 11.4, 10.5, 8.1]) == (9.37, 7.6)
assert cube_times([13.4, 12.3, 9.5, 11.9, 20.8]) == (12.53, 9.5)
assert cube_times([28.3, 14.5, 17.3, 8.9, 10.1]) == (13.97, 8.9)
def cube_times(times: list) -> tuple:
    times.sort()
    avg=round(sum(times[1:4])/3,2)
    min=times[0]
    return avg,min
def cube_times(times: list) -> tuple:
    a = max(times)
    b = min(times)
    avg = round(sum([x for x in times if x != a and x != b]) / 3, 2)
    return (avg, b)


assert cube_times([9.5, 7.6, 11.4, 10.5, 8.1]) == (9.37, 7.6)
assert cube_times([13.4, 12.3, 9.5, 11.9, 20.8]) == (12.53, 9.5)
assert cube_times([28.3, 14.5, 17.3, 8.9, 10.1]) == (13.97, 8.9)
def cube_times(times: list) -> tuple:
    r1 = min(times)
    del times[times.index(r1)]
    del times[times.index(max(times))]
    return (round(sum(times)/3,2),r1)
关闭