diff --git a/clojure/412.clj b/clojure/412.clj deleted file mode 100644 index 4698221..0000000 --- a/clojure/412.clj +++ /dev/null @@ -1,31 +0,0 @@ -(ns clojure.412 - "Leetcode: 412 - Name: Fizz Buzz" - (:require [clojure.test :refer [deftest is run-tests]])) - -(defn fizz-buzz [n] - {:pre [(and (integer? n) (<= 1 n (Math/pow 10 4)))]} - (loop [vector-n (range 1 (inc n)) - result []] - (if (empty? vector-n) - result - (let [current-num (first vector-n) - next-vector (rest vector-n) - fizz-buzz-result (cond - (and (zero? (mod current-num 3)) - (zero? (mod current-num 5))) "FizzBuzz" - (zero? (mod current-num 3)) "Fizz" - (zero? (mod current-num 5)) "Buzz" - :else (str current-num))] - (recur next-vector (conj result fizz-buzz-result)))))) - -(deftest test-fizz-buzz - (is (= (fizz-buzz 1) ["1"])) - (is (= (fizz-buzz 3) ["1" "2" "Fizz"])) - (is (= (fizz-buzz 5) ["1" "2" "Fizz" "4" "Buzz"])) - (is (= (fizz-buzz 15) ["1" "2" "Fizz" "4" "Buzz" "Fizz" "7" "8" "Fizz" "Buzz" "11" "Fizz" "13" "14" "FizzBuzz"])) - (is (thrown? AssertionError (fizz-buzz 10001))) - (is (thrown? AssertionError (fizz-buzz 0))) - (is (thrown? AssertionError (fizz-buzz -1)))) - -(run-tests) \ No newline at end of file diff --git a/ruby/412.rb b/ruby/412.rb deleted file mode 100644 index 537202d..0000000 --- a/ruby/412.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'minitest/autorun' - -def fizz_buzz(n) - pre_condition(n) - result = [] - - (1..n).each do |i| - if i % 3 == 0 && i % 5 == 0 - result << "FizzBuzz" - elsif i % 3 == 0 - result << "Fizz" - elsif i % 5 == 0 - result << "Buzz" - else - result << i.to_s - end - end - - result -end - -def pre_condition(value) - raise ArgumentError, "Argument must be an integer" unless value.is_a? Integer -end - -class TestFizzBuzz < Minitest::Test - def test_fizz_buzz - assert_equal ["1", "2", "Fizz"], fizz_buzz(3) - assert_equal ["1", "2", "Fizz", "4", "Buzz"], fizz_buzz(5) - assert_equal ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"], fizz_buzz(15) - end - - def test_pre_condition - assert_raises ArgumentError do - fizz_buzz("a") - end - end -end diff --git a/solutions/0401-0500/0412-fizz-buzz/README.md b/solutions/0401-0500/0412-fizz-buzz/README.md new file mode 100644 index 0000000..0d3f421 --- /dev/null +++ b/solutions/0401-0500/0412-fizz-buzz/README.md @@ -0,0 +1,15 @@ +# 412. Fizz Buzz + +[LeetCode problem 412](https://leetcode.com/problems/fizz-buzz/) + +- **Difficulty**: Easy +- **Tags**: math, string, simulation + +## Description + +Given an integer `n`, return a string array `answer` (1-indexed) where: + +- `answer[i] == "FizzBuzz"` if `i` is divisible by 3 and 5. +- `answer[i] == "Fizz"` if `i` is divisible by 3. +- `answer[i] == "Buzz"` if `i` is divisible by 5. +- `answer[i] == i` (as a string) otherwise. diff --git a/solutions/0401-0500/0412-fizz-buzz/solution.cpp b/solutions/0401-0500/0412-fizz-buzz/solution.cpp new file mode 100644 index 0000000..a4a7981 --- /dev/null +++ b/solutions/0401-0500/0412-fizz-buzz/solution.cpp @@ -0,0 +1,24 @@ +#include "solution.hpp" + +namespace leetcode::p0412 { + +std::vector solve(int n) { + std::vector result; + result.reserve(static_cast(n)); + + for (int i = 1; i <= n; ++i) { + if (i % 15 == 0) { + result.emplace_back("FizzBuzz"); + } else if (i % 3 == 0) { + result.emplace_back("Fizz"); + } else if (i % 5 == 0) { + result.emplace_back("Buzz"); + } else { + result.push_back(std::to_string(i)); + } + } + + return result; +} + +} // namespace leetcode::p0412 diff --git a/solutions/0401-0500/0412-fizz-buzz/solution.hpp b/solutions/0401-0500/0412-fizz-buzz/solution.hpp new file mode 100644 index 0000000..2f18156 --- /dev/null +++ b/solutions/0401-0500/0412-fizz-buzz/solution.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +namespace leetcode::p0412 { + +// optimized: O(n) time, O(n) space (excluding the output) — one pass. +std::vector solve(int n); + +} // namespace leetcode::p0412 diff --git a/solutions/0401-0500/0412-fizz-buzz/test.cpp b/solutions/0401-0500/0412-fizz-buzz/test.cpp new file mode 100644 index 0000000..5d29b5a --- /dev/null +++ b/solutions/0401-0500/0412-fizz-buzz/test.cpp @@ -0,0 +1,26 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 412", "[p0412]") { + CHECK(leetcode::p0412::solve(1) == std::vector{"1"}); + CHECK(leetcode::p0412::solve(3) == std::vector{"1", "2", "Fizz"}); + CHECK(leetcode::p0412::solve(5) == std::vector{"1", "2", "Fizz", "4", "Buzz"}); + CHECK(leetcode::p0412::solve(15) == std::vector{ + "1", + "2", + "Fizz", + "4", + "Buzz", + "Fizz", + "7", + "8", + "Fizz", + "Buzz", + "11", + "Fizz", + "13", + "14", + "FizzBuzz", + }); +}