Last active
August 29, 2015 14:00
-
-
Save ZhangHanDong/1d1899fd46e8704a76e2 to your computer and use it in GitHub Desktop.
thoughtworks fizzbuzz test: https://www.jinshuju.net/f/EGQL3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# thoughtworks fizzbuzz test: https://www.jinshuju.net/f/EGQL3D | |
# just for fun | |
# by blackanger.z@gmail.com | |
class Student | |
attr_accessor :number, :seeds, :fizzs | |
def initialize(number, seeds=[]) | |
@number = number | |
@seeds = seeds | |
@fizzs = ['Fizz', 'Buzz', 'Whizz'] | |
end | |
def number_off | |
re = calculate | |
re.empty? ? number : re | |
end | |
private | |
def calculate | |
if special_rule(seeds[0]) | |
fizzs[0] | |
else | |
seeds.each_with_index.inject('') do |re, (seed, index)| | |
re << fizzs[index] if common_rule(seed) | |
re | |
end | |
end | |
end | |
def common_rule(seed); (number % seed) == 0 end | |
def special_rule(seed); number.to_s.match(/#{seed}/) end | |
end | |
# example: | |
# number_off_by_seeds(100, 3,5,7) | |
def number_off_by_seeds(students_number, *seeds) | |
queue = Array[] | |
1.upto(students_number) {|i| queue << Student.new(i, seeds) } | |
queue.each_with_index {|student, index| puts "number #{index+1} student number off: #{student.number_off}" } | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
题目:
FizzBuzzWhizz
你是一名体育老师,在某次课距离下课还有五分钟时,你决定搞一个游戏。此时有100名学生在上课。游戏的规则是:
现在,我们需要你完成一个程序来模拟这个游戏,它首先接受3个特殊数,然后输出100名学生应该报数的数或单词。比如,
输入
3,5,7
输出(片段)
1
2
Fizz
4
Buzz
Fizz
Whizz
8
Fizz
Buzz
11
Fizz
Fizz
Whizz
FizzBuzz
16
17
Fizz
19
Buzz
…
一直到100