笔试题(十五):身高体重排序

# 身高从低到高,身高相同体重从轻到重,体重相同维持原来顺序。
import numpy as np


def sort_hw(n, height, weight):
    args = np.array(np.argsort(height))
    i = 0
    tmp = height[args[0]]  # 记录新值
    tmp_w = [weight[args[0]]]
    args_w = np.array([0])
    while i < n-1:
        i += 1
        if height[args[i]] == tmp:
            tmp_w.append(weight[args[i]])
            args_w = np.append(args_w, i)
            if i == n-1:  # 如果走到最后一步也需要做判断
                if len(tmp_w) > 1:
                    tmp_arg = np.argsort(tmp_w)  # 体重排序
                    args_w2 = args_w[tmp_arg]
                    args[args_w] = args[args_w2]  # 按照体重重排序
        else:
            if len(tmp_w) > 1:
                tmp_arg = np.argsort(tmp_w)  # 体重排序
                args_w2 = args_w[tmp_arg]
                args[args_w] = args[args_w2]  # 按照体重重排序
            tmp = height[args[i]]
            tmp_w = [weight[args[i]]]
            args_w = np.array([i])
    return [i+1 for i in args]


if __name__ == '__main__':
    n = 6
    height = [100, 100, 100, 120, 130, 120]
    weight = [40, 30, 20, 60, 50, 50]
    print(sort_hw(n, height, weight))

    n2 = 3
    height2 = [90, 110, 90]
    weight2 = [45, 60, 45]
    print(sort_hw(n2, height2, weight2))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trisyp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值