Skip to content

Instantly share code, notes, and snippets.

@zkxs
Last active January 8, 2022 07:39
Show Gist options
  • Save zkxs/5265f9571d692291cf2e2f84302760f5 to your computer and use it in GitHub Desktop.
Save zkxs/5265f9571d692291cf2e2f84302760f5 to your computer and use it in GitHub Desktop.
Alas, no interviewer has ever asked me to fizzbuzz
import java.util.function.IntPredicate;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.List;
/**
* Here's the same program, but in Java this time to demonstrate the
* boilerplate difference between Java and Scala.
*
* My solution to the classic fizz-buzz problem.
*
* While the traditional problem only uses two conditions, (the input's
* divisibility by two and five) this implementation is generic, in that any
* number of conditions can be checked.
*
* Want to also print "Bazz" for numbers divisible by 7? That's a one-line
* addition.
*
* @author Michael Ripley
*/
public class FizzBuzz {
private static final class Test {
private final IntPredicate test;
private final String flag;
Test(IntPredicate test, String flag) {
this.test = test;
this.flag = flag;
}
public IntPredicate getTest() {
return test;
}
public String getFlag() {
return flag;
}
}
public static void main(String[] args) {
Test[] conditions = {
new Test(n -> n % 3 == 0, "Fizz"),
new Test(n -> n % 5 == 0, "Buzz")
};
for (int i = 1; i <= 100; i++) {
final int iButFinal = i; // the Java compiler in its infinite wisdom refuses to use mutable variables in lambdas
final List<Test> matchingConditions = Arrays.stream(conditions)
.filter(test -> test.getTest().test(iButFinal))
.collect(Collectors.toList());
if (matchingConditions.isEmpty()) {
System.out.println(i); // if no conditions are met, just print the input
} else {
matchingConditions.forEach(condition -> System.out.print(condition.getFlag()));
System.out.println();
}
} // end of main loop
}
}
/**
* My solution to the classic fizz-buzz problem.
*
* While the traditional problem only uses two conditions, (the input's
* divisibility by two and five) this implementation is generic, in that any
* number of conditions can be checked.
*
* Want to also print "Bazz" for numbers divisible by 7? That's a one-line
* addition.
*
* @author Michael Ripley
*/
object FizzBuzz {
sealed case class Test(val test: Int => Boolean, val flag: String)
def main(args: Array[String]): Unit = {
val conditions = Seq(
Test(_ % 3 == 0, "Fizz"),
Test(_ % 5 == 0, "Buzz")
)
for (i <- 1 to 100) {
val matchingConditions = conditions.filter(_.test(i))
if (matchingConditions.isEmpty) {
println(i) // if no conditions are met, just print the input
} else {
matchingConditions.foreach(condition => print(condition.flag))
println
}
} // end of main loop
}
}
/*
* And again, this time in Rust.
*
* While the traditional problem only uses two conditions, (the input's
* divisibility by two and five) this implementation is generic, in that any
* number of conditions can be checked.
*
* Want to also print "Bazz" for numbers divisible by 7? That's a one-line
* addition.
*/
struct Test {
test: fn(i32) -> bool,
flag: &'static str,
}
const TESTS: [Test; 2] = [
Test { test: |i| i % 3 == 0, flag: "Fizz" },
Test { test: |i| i % 5 == 0, flag: "Buzz" },
];
fn main() {
for i in 1..=100 {
let mut flags = TESTS.iter()
.filter(|test| (test.test)(i))
.map(|test| test.flag)
.peekable();
if flags.peek().is_none() {
println!("{}", i);
} else {
for flag in flags {
print!("{}", flag);
}
println!();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment