Python day 14
-
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:
https://www.hackerrank.com
https://www.geeksforgeeks.org/python-sort-list-according-second-element-sublist/
网友评论