您的位置:首页 > 其它

computer science 101-unit2 some exercise

2018-03-04 21:36 190 查看
1.中位数函数
def bigger(a,b):
        if  a > b:

                return a

        else:

                return b

def biggest(a,b,c):
        return bigger(a,bigger(b,c))

def median(a,b,c):
        big = biggest(a,b,c)

        if big == a:

                return bigger(b,c)
        if big == b:

                return bigger(a,c)

        else:

               return bigger(a,b)

2. find_last 函数
def find_last(s,t):
        last_pos = -1

        while True:

             pos = s.find(t, last_pos + 1)

               if pos == -1:

                        return last_pos

               last_pos = pos 

3. 乘法表
def print_multiplication_table(n):
        i  = 1

        while i <= n:

                j = 1

                while j <= n:

                        print  str(i) + ' * ' + str(j) + ' = ' + str(i * j)

                        j = j + 1

                i = i + 1
输出结果:

#print_multiplication_table(2)
#>>> 1 * 1 = 1
#>>> 1 * 2 = 2
#>>> 2 * 1 = 2
#>>> 2 * 2 = 4

    

    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: