使用python实现拉钩网上的FizzBuzzWhizz问题示例

时间:2021-06-18 08:41:38 

最近好多分享这个问题的代码,题目说的是用面向对象或者函数式编程,下面是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,拉钩网,FizzBuzzWhizz问题
0
投稿

猜你喜欢

  • python解析库Beautiful Soup安装的详细步骤

    2023-06-23 04:29:21
  • Python实现Harbor私有镜像仓库垃圾自动化清理详情

    2021-02-08 17:09:46
  • 基于Python代码编辑器的选用(详解)

    2022-08-13 23:44:39
  • PHP垃圾回收机制超详细介绍

    2023-11-21 23:11:44
  • python多线程编程方式分析示例详解

    2023-05-13 04:56:01
  • django处理select下拉表单实例(从model到前端到post到form)

    2023-09-02 13:47:09
  • 解析MSSQL跨数据库查询的实现方法

    2024-01-21 20:19:19
  • python网络爬虫 Scrapy中selenium用法详解

    2023-02-03 06:08:46
  • mysql5.5数据库data目录迁移方法详解

    2024-01-23 10:56:27
  • 浅谈Python3实现两个矩形的交并比(IoU)

    2021-09-06 19:31:56
  • Python爬取动态网页中图片的完整实例

    2023-11-12 20:29:27
  • 回顾Javascript React基础

    2023-07-13 00:57:00
  • MySQL 5.5.x my.cnf参数配置优化详解

    2024-01-27 02:12:48
  • 使用已经得到的keras模型识别自己手写的数字方式

    2021-04-03 17:41:11
  • 使用python实现抓取腾讯视频所有电影的爬虫

    2022-05-09 12:19:03
  • 认识MySQL数据库对服务器端光标的限制

    2009-03-25 17:35:00
  • OverFlow – 一个秘密武器

    2010-09-25 12:51:00
  • 如何防止未经注册的用户绕过注册界面直接进入应用系统?

    2009-11-22 19:22:00
  • Python + Tkinter连接本地MySQL数据库简单实现注册登录

    2023-04-24 22:07:07
  • python中强制关闭线程与协程与进程方法

    2023-05-11 10:24:48
  • asp之家 网络编程 m.aspxhome.com