最近在学习python,也看过了几本书,偶然在群里面有人介绍checkio这个东东,就自己去玩了一下,感受还是不错的,以下记录一下自己做的几道题目,让日后自己回看过来,才会发现现在写的东西是多么垃圾,那时候也证明我已经进步了很多了。
def checkio(data): num = {'one':'1','two':'2','three':'3','four':'4','five':'5', \ 'six':'6', 'seven':'7', 'eight':'8', 'nine':'9'} numt = {'ten':'10', 'eleven':'11', 'twelve':'12', 'thirteen':'13', 'fourteen':'14', \ 'fifteen':'15', 'sixteen':'16', 'seventeen':'17', 'eighteen':'18',\ 'nineteen':'19' } nums = {'twenty':'2', 'thirty':'3', 'forty':'4', 'fifty':'5',\ 'sixty':'6', 'seventy':'7', 'eighty':'8', 'ninety':'9'} lengnum = len(num) lengnumt = len(numt) lengnums = len(nums) data = str(data) data = list(data) lenght = len(data) if lenght == 3: for i in num.keys(): if num[i] == data[0]: data[0] = i if int(data[1]) == 0: data[1] = '' if int(data[2]) == 0: data[2] = '' if data[1]!= '' and int(data[1]) == 1: for i in numt.keys(): if numt[i] == (data[1]+data[2]): data[1] = i data[2] = '' elif data[1]!= '' and int(data[1]) > 1: for i in nums.keys(): if nums[i] == data[1]: data[1] = i if data[2]!= '' and int(data[2]) >= 1: for i in num.keys(): if num[i] == data[2]: data[2] = i if data[1] == '': if data[2] == '': return data[0] + ' hundred' else: return data[0] + ' hundred ' + data[2] else: if data[2] == '': return data[0] + ' hundred ' + data[1] else: return data[0] + ' hundred ' + data[1] + ' ' + data[2] if lenght == 2: if int(data[0]) == 1: for i in numt.keys(): if numt[i] == (data[0]+data[1]): data[0] = i data[1] = '' elif int(data[0]) > 1: for i in nums.keys(): if nums[i] == data[0]: data[0] = i if int(data[1]) >= 1: for i in num.keys(): if num[i] == data[1]: data[1] =i else: data[1] = '' if data[1] == '': return data[0] else: return data[0] + ' ' + data[1] if lenght == 1: if int(data[0]) == 0: return 'zero' else: for i in num.keys(): if num[i] == data[0]: data[0] = i return data[0]#These "asserts" using only for self-checking and not necessary for auto-testingif __name__ == '__main__': assert checkio(4) == 'four', "1st example" assert checkio(133) == 'one hundred thirty three', "2nd example" assert checkio(12) == 'twelve', "3rd example" assert checkio(101) == 'one hundred one', "4th example" assert checkio(212) == 'two hundred twelve', "5th example" assert checkio(40) == 'forty', "6th example"