Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added java version of FizzBuzz using sets #2027

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// FizzBuzz Java implementation using sets
// Author: @muhazafasa

import java.util.LinkedList;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;

interface Result {
String result(int x);
}

public class FizzBuzzSetImplementation {
public static void main(String[] args) {
class Pair<X, Y> {
public final X first;
public final Y second;
public Pair(X first, Y second) {
this.first = first;
this.second = second;
}
}

HashSet<Integer> fizz = new HashSet<Integer>();
for (int i = 3; i <= 100; i += 3)
fizz.add(i);

HashSet<Integer> buzz = new HashSet<Integer>();
for (int i = 5; i <= 100; i += 5)
buzz.add(i);

HashSet<Integer> fizzBuzz = new HashSet<Integer>(fizz);
fizzBuzz.retainAll(buzz);

HashSet<Integer> rest = new HashSet<Integer>();
for (int i = 1; i <= 100; ++i)
rest.add(i);
rest.removeAll(fizz);
rest.removeAll(buzz);

Result fizzResult = x -> "Fizz";
Result buzzResult = x -> "Buzz";
Result fizzBuzzResult = x -> "FizzBuzz";
Result restResult = x -> String.format("%d", x);

LinkedList<Pair<HashSet<Integer>, Result>> results = new LinkedList<Pair<HashSet<Integer>, Result>>();
results.add(new Pair(fizzBuzz, fizzBuzzResult));
results.add(new Pair(fizz, fizzResult));
results.add(new Pair(buzz, buzzResult));
results.add(new Pair(rest, restResult));

Random rand = new Random();
Scanner scan = new Scanner(System.in);

while (true) {
int number = rand.nextInt(99) + 1;
for (var type : results)
if (type.first.contains(number)) {
System.out.println(type.second.result(number));
break;
}
scan.nextLine();
}
}
}