为了账号安全,请及时绑定邮箱和手机立即绑定

python 小目标8

Python day 14

  1. Who is the True Lucky Guy?

In Wechat red-packet game, the best luck winner usually ends up in with a negative balance, because he/she has to send out a red-packet after winning the Best Luck Red Packet. That leaves the Second best luck guy to be the true best luck overall in the game.

Suppose we have a  list of input

2 3 6 6 5

We want to pick out 5.

How do we find the second best luck guy?
let’s try to work in python

Solution:

# Second Best Luck Star
    n = int(input())
    arr = list(map(int, input().split()))
    lis=list(arr)[:n]
    lis1=sorted(set(lis))
    length=len(lis1)
    print(lis1[length-2])

# Alternative Solution
    zes = max(arr)
    i=0
    while(i<n):
        if zes ==max(arr):
            arr.remove(max(arr))
        i+=1
print(max(arr))
5

Note: I used a long time to figure out why int() or input() doesnt work on my spyder, it turns out after any line includes int or input, we need to actually input the value before move on to next code time!

[caption id=“attachment_1984” align=“alignnone” width=“550”]

PublicDomainPictures / Pixabay[/caption]

2. Second Lowest grade

We are learning Excel 😱😱😱 this semester, i think this is the biggest joke in the universe. The sad thing is I dont know how to use it, and i dont want to learn. I am still hoping i won’t be the student with the lowest grade in the class. Second lowest it is ok! So i will program with python to find out who has the second lowest grade!

Suppose we have input

Holly
32
Berry
31.2
Tina
31
Amy
40
Hope
39 

Solution

marksheet = []
for _ in range(0,int(input())):
    marksheet.append([input(), float(input())])

def low2(t2):
    #take the second lowest
    t3=(sorted(t2, key=lambda x:(x[1] , x[0])))[1]
    #Pick the Scores
    t4=t3[1]
    return('\n'.join([a for a,b in sorted(marksheet) if b == t4]))
print(sort(marksheet))

# Alternative Solution
second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]
print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))

Output:
Berry

Happy Practicing!!! except for Excel! 😡

Reference:

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消