使用python实现拉钩网上的FizzBuzzWhizz问题示例
最近好多分享这个问题的代码,题目说的是用面向对象或者函数式编程,下面是python的实现示例
#!/usr/bin/python
#encoding:utf8
'''
the game of "fizzbuzzwhizz"
author : wang.jiankui89@gmail.com
mobile : 130-2199-5152
'''
import sys
class teacher:
def __init__(self, student_num):
self.student_num = student_num
def gamestart(self, numlist):
for i in range (1, self.student_num + 1):
stu = student(i)
print stu.answer(numlist)
class student:
def __init__(self, my_num):
self.my_num = my_num
def judgemod(self, numlist):
modres = ""
for num in numlist:
if self.my_num % num == 0:
modres += rule[num]
return modres if modres else self.my_num
def judgecontain(self, first_num):
conres = ""
if str(first_num) in str(self.my_num) :
conres = rule[first_num]
return conres
def answer(self, numlist):
conres = self.judgecontain(numlist[0])
return conres if conres else self.judgemod(numlist)
def getops():
'''parse options'''
if len(sys.argv) != 4 :
print "use as fizzbuzzwhizz.py [0-9] [0-9] [0-9]"
sys.exit()
else:
first_num = int(sys.argv[1])
second_num = int(sys.argv[2])
third_num = int(sys.argv[3])
return first_num, second_num, third_num
def main():
first_num, second_num, third_num = getops()
global rule
rule = {first_num:"fizz", second_num:"buzz", third_num:"whizz"}
student_num = 100
tea = teacher(100)
tea.gamestart( (first_num, second_num, third_num) )
if __name__ == "__main__":
main()
上一篇: 这么小的床,我媳妇睡哪?
下一篇: 你的车呢?
推荐阅读
-
Python实现类似jQuery使用中的链式调用的示例
-
Python实现类似jQuery使用中的链式调用的示例
-
使用python的pexpect模块,实现远程免密登录的示例
-
使用CodeMirror实现Python3在线编辑器的示例代码
-
Python实现迭代时使用索引的方法示例
-
Python实现的基于优先等级分配糖果问题算法示例
-
使用python实现拉钩网上的FizzBuzzWhizz问题示例
-
Python 使用PIL numpy 实现拼接图片的示例
-
Python使用matplotlib和pandas实现的画图操作【经典示例】
-
Python实现在tkinter中使用matplotlib绘制图形的方法示例