1 Star 1 Fork 1

taylortaurus/Python-Study

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
problem1.py 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
Danielyan_1398 提交于 2019-03-19 15:26 +08:00 . update python
# 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_list
def Sum_list(self, Num_list):
sum = 0
for i in Num_list:
sum = i + sum
print 'sum of the list is %d' % (sum)
return sum
def Multiple_list(self, Num_list):
result = 1
for i in Num_list:
result = i * result
print 'multiple of the list is %d' % result
return 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 = 100
Myobject = Multiples()
Num_list = Myobject.Multiples_three_and_five(num)
print Num_list
print Myobject.Sum_list(Num_list)
print "split line, refactor"
num = 100
Myobject = Multiples_refactor()
Num_list = Myobject.Multiples_three_and_five(num)
print Num_list
print Myobject.Sum_list(Num_list)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/taylortaurus/Python-Study.git
git@gitee.com:taylortaurus/Python-Study.git
taylortaurus
Python-Study
Python-Study
master

搜索帮助