栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 小目标8

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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:

https://www.hackerrank.com
https://www.geeksforgeeks.org/python-sort-list-according-second-element-sublist/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/220723.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号