From 5d6299f71b16775237bfd48cdbcd946800bf1b5d Mon Sep 17 00:00:00 2001 From: Lavender Date: Sun, 4 Apr 2021 15:23:42 -0700 Subject: [PATCH] Initial commit --- fizzbuzz.c | 39 +++++++++++++++++++++++++++++++++++++++ fizzbuzz.cpp | 23 +++++++++++++++++++++++ fizzbuzz.js | 13 +++++++++++++ fizzbuzz.py | 11 +++++++++++ fizzbuzz.rs | 22 ++++++++++++++++++++++ 5 files changed, 108 insertions(+) create mode 100644 fizzbuzz.c create mode 100644 fizzbuzz.cpp create mode 100644 fizzbuzz.js create mode 100644 fizzbuzz.py create mode 100644 fizzbuzz.rs diff --git a/fizzbuzz.c b/fizzbuzz.c new file mode 100644 index 0000000..1d35d01 --- /dev/null +++ b/fizzbuzz.c @@ -0,0 +1,39 @@ +#include +#include + +#define BUZZLENGTH 5 + +struct Fizz { + int multiple; + char word[BUZZLENGTH]; +}; + +struct Fizz buzzes[] = { + { + .multiple = 3, + .word = "Fizz" + }, + { + .multiple = 5, + .word = "Buzz" + } +}; + +int main() { + for (int i = 1; i <= 100; i++) { + int buzz_amount = sizeof(buzzes) / sizeof(struct Fizz); + char output[buzz_amount * BUZZLENGTH]; + + for (int j = 0; j < buzz_amount; j++) + if (i % buzzes[j].multiple == 0) + strcat(output, buzzes[j].word); + + if (strlen(output) == 0) + printf("%d\n", i); + else { + printf("%s\n", output); + strcpy(output, ""); + } + } + return 0; +} diff --git a/fizzbuzz.cpp b/fizzbuzz.cpp new file mode 100644 index 0000000..a2b6de8 --- /dev/null +++ b/fizzbuzz.cpp @@ -0,0 +1,23 @@ +#include +#include + +std::pair buzzes[] = { + {3, "Fizz"}, + {5, "Buzz"} +}; + +int main() { + for (int i = 1; i <= 100; i++) { + std::string output; + + for (int j = 0; j < sizeof(buzzes) / sizeof(std::pair); j++) + if (i % buzzes[j].first == 0) + output.append(buzzes[j].second); + + if (output.length() == 0) + std::cout << i; + else + std::cout << output; + std::cout << std::endl; + } +} diff --git a/fizzbuzz.js b/fizzbuzz.js new file mode 100644 index 0000000..2826aee --- /dev/null +++ b/fizzbuzz.js @@ -0,0 +1,13 @@ +let buzzes = [{multiple: 3, word: "Fizz"}, {multiple: 5, word: "Buzz"}] + +for(i = 1; i <= 100; i++) { + let output = "" + buzzes.forEach(function(item) { + if (i % item.multiple === 0) + output += item.word + }) + if (output.length === 0) + print(i) + else + print(output) +} diff --git a/fizzbuzz.py b/fizzbuzz.py new file mode 100644 index 0000000..3c9bc33 --- /dev/null +++ b/fizzbuzz.py @@ -0,0 +1,11 @@ +buzzes = {3: "Fizz", 5: "Buzz"} + +for i in range(1, 101): + output = "" + for j in buzzes: + if i % j == 0: + output += buzzes[j] + if len(output) == 0: + print(i) + else: + print(output) diff --git a/fizzbuzz.rs b/fizzbuzz.rs new file mode 100644 index 0000000..b82643f --- /dev/null +++ b/fizzbuzz.rs @@ -0,0 +1,22 @@ +fn main() { + let buzzes = [(3, "Fizz"), (5, "Buzz")]; + + for i in 1..101 { + let output = (0..buzzes.len()).fold(String::new(), |string, j| { + if i % buzzes[j].0 == 0 { + string + buzzes[j].1 + } else { + string + } + }); + + println!( + "{}", + if output.len() == 0 { + i.to_string() + } else { + output + } + ); + } +}