一、要求

第一题

气象预报时,一般按照风速对飓风进行分级,下表是飓风风速(英里/小时)与飓风分级对照表。请在指定位置完成函数编写,根据用户输入的风速值,输出其飓风级别。

飓风级别风速(英里/小时)
174-95
296-110
3111-130
4131-154
5155及以上

第二题

信用卡号是否合法的判断规则为:
a) 对给定的8位信用卡号码,如43589795,从最右边数字开始,隔一位取一个数相加,如5+7+8+3=23。 b) 将卡号中未出现在第一步中的每个数字乘2,然后将相乘的结果的每位数字相加。例如,对上述例子,未出现在第一步中的数字乘2后分别为(从右至左)1818108,则将所有数字相加为1+8+1+8+1+0+8=27。 c) 将上述两步得到的数字相加,如果得数个位为0,则输入的信用卡号是有效的。

请在指定位置完成函数的编写,判断用户输入的8位信用卡号码是否合法。

第三题

国际标准书号(ISBN)用10位数字唯一标识一本书。最右边的数字为效验和,可由其他9位数字计算出来,且d1​+2d2​+3d3​+...+10d10​必须是11的倍数(di​的下标i表示从右边起第i个数)。效验和必须是介于010中的一个数字,用X表示10。例如:020131452的效验和是5,因为对于以下11的倍数的公式,5是唯一的介于010之间的数:d1 + 2*2 + 3*5 + 4*4 + 5*1 + 6*3 + 7*1 + 8*0 + 9*2 + 10*0

请在指定位置完成函数的编写,以前9位数字为基础,计算效验和并返回ISBN号。

第四题

在指定位置完成函数编写,根据年和月,打印该月的日历。(计算某一天是星期几的公式见Python表达式问题求解实训)。试题中已定义了三个函数,请完成这三个函数,其中函数day用于计算并返回某年某月某日是星期几;函数isLeapYear用于判断某年是否是闰年;函数calendar打印所给年月的日历,注意输出格式要求和空格的对齐,如下图所示,具体的空格数请参见本题代码。

第五题

Srinivasa Ramanujan是一个因他在数字上的天分而出名的印度数学家。有一天,英国数学家G.H.Hardy来拜访他,Hardy提到自己乘坐的出租车标有一个相当无聊的数字1729Ramanujan 当即回答道,“不,Hardy!不,Hardy!这是一个非常有趣的数字!”。Ramanujan说:“对1729这个数字,存在2组不同的数,且每组只有2个数值,每一组数值的立方和等于1729,且1729是能够被两组不同的数按上述条件表达的数中最小的那个!”。

请在指定位置完成函数编写,验证这句话是否正确。程序将整数N作为输入,按一定格式返回所有能够被上述方法表示的小于或等于N的整数。换句话说,就是找到4个不同的正整数abcda3+b3=c3+d3。(提示:用4个嵌套循环)。

第六题

请在指定位置完成函数编写,将用户输入的一个0~999的整数转换成其对应的英文表示,例如,729将被转换成seven hundred and twenty nine。要求,在程序中尽可能地使用函数封装一些常用的转换,不得少于3个函数。试题中已定义了三个函数,请完成这三个函数,unit_to_word09的数字转换成英文,并返回转换后的英文;tens_to_word利用unit_to_word,将1019、以及2099的十位部分数字转换成英文,并返回转换后的英文;hundreds_to_word利用unit_to_wordtens_to_word进行转换,并返回转换后结果。

请注意,得到的英文字符串前后没有空格。输出格式已经设定好,按要求返回英文即可。

二、代码

#第一题

def rankHurricane(velocity):
    #请在下面编写代码
    # ********** Begin ********** #
    if velocity < 74:
        rank = None
    elif velocity < 96:
        rank = 1
    elif velocity < 111:
        rank = 2
    elif velocity < 131:
        rank = 3
    elif velocity < 155:
        rank = 4
    else:
        rank = 5
    # ********** End ********** #
    #请不要修改下面的代码
    return rank


#第二题

def validCreditCard(num):
    #请在下面编写代码
    # ********** Begin ********** #
    valid = False  
    if 1e+8 > num > 1e+7:
        a = 0
        b = 0
        b1 = 0
        while not(num == 0):
            a += num % 10
            b += num % 100 // 10
            num //= 100
            b = b * 2
            b1 += b % 10 + b // 10
            b = 0
        if (a + b1) % 10 == 0:
            valid = True
    # ********** End ********** #    
    #请不要修改下面的代码
    return valid

#第三题

def ISBN(n):
    # 请在下面编写代码
    # ********** Begin ********** #
    total = 0
    num = n
    for i  in range (2, 11):
        digit = num % 10
        total += i * digit
        num //= 10
    strn = str(n)
    if len(strn) < 9:
        strn = '0' * (9 - len(strn)) + strn
    trueISBN = ''
    if total % 11 == 1:
        trueISBN = strn + 'X'
    elif total % 11 == 0:
        trueISBN = strn + '0'
    else:
        trueISBN = strn + str(11 -(total % 11))
    # ********** End ********** #    
    # 请不要修改下面的代码
    return (trueISBN)

#第四题

def day(y, m, d):#计算y年m月d日是星期几
    # 请在下面编写代码
    # ********** Begin ********** #
    y0=y-(14-m)//12
    x=y0+y0//4-y0//100+y0//400
    m0=m+12*((14-m)//12)-2
    d0=(d+x+(31*m0)//12)%7
    # ********** End ********** #    
    # 请不要修改下面的代码
    return d0

def isLeapYear(year): #判断year年是否闰年
    # 请在下面编写代码
    # ********** Begin ********** #
    isLeapYear = (year % 4 == 0)
    isLeapYear = isLeapYear and (year % 100 != 0)
    isLeapYear = isLeapYear or (year % 400 == 0)
    # ********** End ********** #    
    # 请不要修改下面的代码
    return isLeapYear

def calendar(y, m): #打印y年m月日历
    print('       {}年{}月'.format(y,m))
    print('Su\tM\tTu\tW\tTh\tF\tSa')
    # 请在下面编写代码
    # ********** Begin ********** #
    date=day(y,m,1)
    days = 0
    if m in [1, 3, 5, 7, 8, 10, 12]:
        days = 31
    elif m in [4, 6, 9, 11]:
        days = 30
    else:
        if isLeapYear(y):
            days = 29
        else:
            days = 28
    count = date
    for i in range(date):
        print('\t', end='')
    for d in range(1, days + 1):
        print(str(d) + '\t', end="")
        count = (count + 1) % 7
        if count == 0:
            print()
    print()
    # ********** End ********** #
    # 请不要修改下面的代码

#第五题

def ramanujan(n):
    results = []
    #请在下面编写代码
    # ********** Begin ********** #
    for a in range (1, n + 1):
        a3 = a * a * a
        if a3 > n:
            break
        for b in range (a, n + 1):
            b3 = b * b * b
            if a3 + b3 > n:
                break;
            for c in range (a + 1, n + 1):
                c3 = c * c * c
                if c3 > a3 + b3:
                    break
                for d in range (c, n + 1):
                    d3 = d * d * d
                    if c3 + d3 > a3 + b3:
                        break
                    if c3 + d3 == a3 +b3:
                        result = str(a3+b3) + ' = ' + str(a) + '^3 + ' + str(b) + '^3 = ' + str(c) + '^3 + ' + str(d) + '^3'
                        results.append(result)
    # ********** End ********** #    
    # 请不要修改下面的代码
    return results


#第六题

def unit_to_word(u): #将0~9的数字转换成英文,并返回转换后的英文
    # 请在下面编写代码
    # ********** Begin ********** #
    convert_table = {0:"zero",
                1:"one",
                2:"two",
                3:"three",
                4:"four",
                5:"five",
                6:"six",
                7:"seven",
                8:"eight",
                9:"nine",}
    return convert_table[u]
    # ********** End ********** #
    # 请不要修改下面的代码


def tens_to_word(t): #利用unit_to_word,将10~19、以及20~99的十位部分数字转换成英文,并返回转换后的英文
    # 请在下面编写代码
    # ********** Begin ********** #
    convert_table = {
                0:"",
                10:"ten",
                11:"eleven",
                12:"twelve",
                13:"thirteen",
                14:"fourteen",
                15:"fifteen",
                16:"sixteen",
                17:"seventeen",
                18:"eighteen",
                19:"nineteen",
                2:"twenty",
                3:"thirty",
                4:"forty",
                5:"fifty",
                6:"sixty",
                7:"seventy",
                8:"eighty",
                9:"ninety",}
    if 9 < t < 20:
        return convert_table[t]
    else:
        tens = convert_table[t//10] + " " + unit_to_word(t%10)
    return tens.lstrip()
    # ********** End ********** #
    # 请不要修改下面的代码

def hundreds_to_word(h): #利用unit_to_word、tens_to_word进行转换,并返回转换后结果的函数
    # 请在下面编写代码
    # ********** Begin ********** #
    if h > 99:
        word = unit_to_word(h//100) + " hundred"
        tens = h % 100
        if tens == 0:
            return word
        else:
            return word + " and " + tens_to_word(tens)
    else:
            return tens_to_word(h)
    # ********** End ********** #    
    # 请不要修改下面的代码

if __name__ == '__main__':
    for v in [60, 74, 95, 96, 110, 111, 130, 131, 154, 170]:
        rank = rankHurricane(v)
        print(rank)
    print('\n***********************\n')

    for num in [1234567, 43589795, 87539319, 123456789]:
        valid = validCreditCard(num)
        print(valid)
    print('\n***********************\n')

    for num in [201314525, 488888913, 977889994, 753231846, 701134069]:
        trueISBN = ISBN(num)
        print(trueISBN)
    print('\n***********************\n')

    for (y,m) in [(2017,8), (2017,10),(2015,8), (2017,2), (2016,2)]:
        calendar(y, m)
        print('---------------------------')

    print('\n***********************\n')

    for num in [2000, 10000, 100000]:
        st = ramanujan(num)
        for item in st:
            print(item)

    print('\n***********************\n')

    for test in [0, 5, 19, 23, 100, 700, 711, 729]:
        print(test, "=>", hundreds_to_word(test))

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐