科协python讲座摸鱼(Science and Technology Association Python lecture fishing)

py现在对我最大的作用可能是解决一些离谱的算法题,一些数据结构之类的
比如说寒假学高精度的时候接触到了py的大整数类biginteger
听讲座时摸鱼来玩一玩:

排序:

冒泡排序:

def bubble_sort(data, flag) :
    if not flag:
        for i in range(len(data) - 1):
            for j in range(len(data) - 1 - i) :
                if data[j] > data[j + 1] :
                    data[j], data[j + 1] = data[j + 1], data[j]
        return data
    else :
        for i in range(len(data) - 1):
            for j in range(len(data) - 1 - i) :
                if data[j] < data[j + 1] :
                    data[j], data[j + 1] = data[j + 1], data[j]
        return data

运行时候输入元组:

a = [1, 2, 3, 4, 5, 6]
bubble_sort(a, 0)
#`[1, 2, 3, 4, 5, 6]
a = [1, 2, 3, 4, 5, 6]
bubble_sort(a, 1)
#`[6, 5, 4, 3, 2, 1]`
————————

Py’s biggest effect on me now may be to solve some outrageous algorithm problems, some data structures and so on
For example, when learning high precision in winter vacation, I came into contact with the big integer class BigInteger of py
Fish while listening to the lecture:

Sort:

Bubble sort:

def bubble_sort(data, flag) :
    if not flag:
        for i in range(len(data) - 1):
            for j in range(len(data) - 1 - i) :
                if data[j] > data[j + 1] :
                    data[j], data[j + 1] = data[j + 1], data[j]
        return data
    else :
        for i in range(len(data) - 1):
            for j in range(len(data) - 1 - i) :
                if data[j] < data[j + 1] :
                    data[j], data[j + 1] = data[j + 1], data[j]
        return data

Runtime input tuple:

a = [1, 2, 3, 4, 5, 6]
bubble_sort(a, 0)
#`[1, 2, 3, 4, 5, 6]
a = [1, 2, 3, 4, 5, 6]
bubble_sort(a, 1)
#`[6, 5, 4, 3, 2, 1]`