美文网首页大数据
Python 小目标8

Python 小目标8

作者: 不连续小姐 | 来源:发表于2019-03-21 01:40 被阅读14次

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:

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

相关文章

  • Python 小目标8

    Python day 14 Who is the True Lucky Guy? In Wechat red-pa...

  • 8 小目标

    8/51 开始是成功的一半 万事开头难,只要开始,总会推动着你走。但是如果没有开始,心里想的再好也是枉然。 不是...

  • 立志精通Python

    今天小目标没坚持几个,到处瞎逛,全是python。 python可以做什么,python爬取…python零基础资...

  • Python 小目标11

    Python Day 17: Polar Coordinate in Python " You are round...

  • Python 小目标3

    Python Day 9 Today we will try the basic Arithmetic Opera...

  • Python 小目标7

    Today we will try to work with Python Strings Task1:Pytho...

  • Python 小目标 9

    Python Day 15 Love Earth Day ❤ the Earth day is coming! m...

  • Python 小目标10

    Python Day 16 1. distinct Average suppose we want to find...

  • python 小目标1

    Python Day 7 Problem 1: Given an array of integers, find ...

  • Python 小目标2

    Python Day 8 Problem 1: Factorial Write a factorial funct...

网友评论

    本文标题:Python 小目标8

    本文链接:https://www.haomeiwen.com/subject/rykamqtx.html