# If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.# The sum of these multiples is 23.# Find the sum of all the multiples of 3 or 5 below 1000.class Multiples(object):def Multiples_three_and_five(self, num):num_list = []for i in range(1, num):if i % 3 == 0:num_list.append(i)elif i % 5 == 0:num_list.append(i)return num_listdef Sum_list(self, Num_list):sum = 0for i in Num_list:sum = i + sumprint 'sum of the list is %d' % (sum)return sumdef Multiple_list(self, Num_list):result = 1for i in Num_list:result = i * resultprint 'multiple of the list is %d' % resultreturn result# refactor code 2016年6月21日class Multiples_refactor(object):def Multiples_three_and_five(self, num):return [x for x in range(1, num) if x % 3 == 0 or x % 5 == 0]def Sum_list(self, Num_list):return sum(Num_list)def Multiple_list(self, Num_list):return reduce(lambda x, y: x * y, Num_list)if __name__ == '__main__':num = 100Myobject = Multiples()Num_list = Myobject.Multiples_three_and_five(num)print Num_listprint Myobject.Sum_list(Num_list)print "split line, refactor"num = 100Myobject = Multiples_refactor()Num_list = Myobject.Multiples_three_and_five(num)print Num_listprint Myobject.Sum_list(Num_list)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。