Created
November 17, 2019 12:54
-
-
Save ykon/9dc83849a134be545052ec26ade9f4f0 to your computer and use it in GitHub Desktop.
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
# typed: true | |
require 'sorbet-runtime' | |
class FizzBuzz | |
extend T::Sig | |
sig {params(n: Integer).void} | |
def self.check_number(n) | |
if n <= 0 then raise ArgumentError end | |
end | |
sig {params(n: Integer).returns(T::Boolean)} | |
def self.is_fizz(n) | |
check_number(n) | |
n % 3 == 0 | |
end | |
sig {params(n: Integer).returns(T::Boolean)} | |
def self.is_buzz(n) | |
check_number(n) | |
n % 5 == 0 | |
end | |
sig {params(n: Integer).returns(T::Boolean)} | |
def self.is_fizzbuzz(n) | |
check_number(n) | |
n % 15 == 0 | |
end | |
sig {params(n: Integer).returns(T::Array[T.any(Symbol, Integer)])} | |
def self.number_as(n) | |
if is_fizzbuzz(n) | |
[:FizzBuzz, n] | |
elsif is_fizz(n) | |
[:Fizz, n] | |
elsif is_buzz(n) | |
[:Buzz, n] | |
else | |
[:Number, n] | |
end | |
end | |
sig {params(fz: T::Array[T.any(Symbol, Integer)]).returns(String)} | |
def self.to_str(fz) | |
t, n = fz | |
case t | |
when :Fizz, :Buzz, :FizzBuzz | |
t.to_s | |
when :Number | |
n.to_s | |
else | |
raise ArgumentError | |
end | |
end | |
sig {void} | |
def self.main() | |
puts (1..100) | |
.map(&method(:number_as)) | |
.map(&method(:to_str)) | |
.zip((1..100).to_a) | |
.map{|fzs, n| "#{n} => #{fzs}"} | |
end | |
end | |
if __FILE__ == $PROGRAM_NAME | |
FizzBuzz.main() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment