美文网首页Python3
Calculate the biggest value and

Calculate the biggest value and

作者: JaedenKil | 来源:发表于2019-06-18 16:44 被阅读0次
    def main(x):
        # x should at least has two elements
        m1, m2 = (x[0], x[1]) if x[0] > x[1] else (x[1], x[0])
        for i in range(2, len(x)):
            if x[i] > m1:
                m2 = m1
                m1 = x[i]
            elif x[i] > m2:
                m2 = x[i]
        return m1, m2
    
    
    if __name__ == "__main__":
        x = [1, 2, 3, 4, 10, -1]
        y = [10, 9, 3, 1]
        z = [3, 9, 9, 1]
        print(main(x))
        print(main(y))
        print(main(z))
    
    10, 4)
    (10, 9)
    (9, 9)
    

    相关文章

      网友评论

        本文标题:Calculate the biggest value and

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