Initial commit
This commit is contained in:
commit
5d6299f71b
5 changed files with 108 additions and 0 deletions
39
fizzbuzz.c
Normal file
39
fizzbuzz.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
23
fizzbuzz.cpp
Normal file
23
fizzbuzz.cpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
std::pair<int, std::string> 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<int, std::string>); 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;
|
||||
}
|
||||
}
|
13
fizzbuzz.js
Normal file
13
fizzbuzz.js
Normal file
|
@ -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)
|
||||
}
|
11
fizzbuzz.py
Normal file
11
fizzbuzz.py
Normal file
|
@ -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)
|
22
fizzbuzz.rs
Normal file
22
fizzbuzz.rs
Normal file
|
@ -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
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue