FizzBuzz 号码分组
Grouping of FizzBuzz numbers
我正尝试在 Java8 中编写 FizzBuzz 问题。它工作正常,我得到了想要的输出。对于能被“3”整除的数,应该return"Fizz",对于能被“5”整除的数,应该return"Buzz",对于能被两者整除的数, 它应该 return "FizzBuzz".
如果我将值作为“15”传递,它 returns:
["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
现在,我被困在一件事上。如果我将值作为“15”传递,我想获得如下输出:
{"Fizz": [3, 6, 9, 12],"Buzz": [5, 10],"FizzBuzz": [15]}
我想按 Fizz、Buzz 和 FizzBuzz 对数字进行分组。
这是我的代码:
public class FizzBuzzService {
private Map<Rule, String> ruleContainers = new HashMap();
private Set<Rule> rules = new HashSet();
public FizzBuzzService(){
addRule(i -> i % 15 == 0, "FizzBuzz");
addRule(i -> i % 3 == 0, "Fizz");
addRule(i -> i % 5 == 0, "Buzz");
}
public void addRule(Rule rule, String res) {
rules.add(rule);
ruleContainers.put(rule, res);
}
public String getValue(int i) {
for (Rule rule : rules) {
if (rule.apply(i)) {
return ruleContainers.get(rule);
}
}
return String.valueOf(i);
}
//then the origin code should be as follows:
public List<String> fizzBuzz(int n) {
List<String> res = new ArrayList();
for(int i = 1; i <= n; i++){
res.add(getValue(i));
}
return res;
}
interface Rule{
boolean apply(int i);
}
}
如果有人能指导我,我将不胜感激。谢谢
我会 return Map<String, List<Integer>>
(使用 LinkedHashMap
而不是 HashMap
并且您的密钥将保留插入顺序)创建 List<Integer>
(s ) 对于 fizz
、buzz
和 fizzbuzz
值。它可以用一个 static
方法完成,例如
public static Map<String, List<Integer>> fizzBuzz(int n) {
Map<String, List<Integer>> map = new HashMap<>();
List<Integer> fizz = new ArrayList<>(), //
buzz = new ArrayList<>(), //
fizzbuzz = new ArrayList<>();
IntStream.rangeClosed(1, n).forEachOrdered(i -> {
boolean f = i % 3 == 0, b = i % 5 == 0;
if (f && b) {
fizzbuzz.add(i);
} else if (f) {
fizz.add(i);
} else if (b) {
buzz.add(i);
}
});
map.put("Fizz", fizz);
map.put("Buzz", buzz);
map.put("FizzBuzz", fizzbuzz);
return map;
}
Map<String, List<Integer>> map =
IntStream.rangeClosed(1, 15)
.mapToObj(n -> {
String str = n % 15 == 0 ? "FizzBuzz" :
(n % 5 == 0) ? "Buzz" : (n % 3 == 0) ? "Fizz" : null;
return str == null ? null : new SimpleEntry<>(str, n);
}).filter(Objects::nonNull)
.collect(groupingBy(Entry::getKey, mapping(Entry::getValue, toList())));
System.out.println(map);
这里有一种使用流的方法:
Map<String, List<String>> result = IntStream.rangeClosed(1, n)
.filter(i -> i % 3 == 0 || i % 5 == 0)
.boxed()
.collect(Collectors.groupingBy(i ->
i % 15 == 0 ? "FizzBuzz" :
i % 3 == 0 ? "Fizz" :
"Buzz"));
没有流:
Map<String, List<String>> result = new HashMap<>();
for (int i = 0; i < n; i++) {
if (i % 3 == 0 || i % 5 == 0) {
String key = i % 15 == 0 ? "FizzBuzz" :
i % 3 == 0 ? "Fizz" :
"Buzz";
result.computeIfAbsent(key, k -> new ArrayList<>()).add(i);
}
}
第 1 步:对号码应用规则。
步骤 2: 找到第一个匹配规则。
第三步:过滤不符合任何规则的元素。
第 4 步: 按规则关键字分组
public static void main(String[] args) {
FizzBuzzService();
Map<String, List<Integer>> fizzBuzzMap = IntStream.range(1, 31)
.mapToObj(number->applRule(number))
.filter(obj ->obj!=null)
.collect(Collectors.groupingBy(Entry::getKey,Collectors.mapping(Entry::getValue, Collectors.toList())));
System.out.println("FizzBuzzMap : " + fizzBuzzMap);
}
private static SimpleEntry<String, Integer> applRule(Integer number) {
Optional<Entry<Rule, String>> findFirst = ruleContainers.entrySet()
.stream()
.filter(rule -> rule.getKey().apply(number))
.findFirst();
if (findFirst.isPresent()) {
System.out.println("Number : " + number + " First Matching Rule : " +findFirst.get().getValue());
return new SimpleEntry<>(findFirst.get().getValue(), number);
}
return null;
}
}
public static void FizzBuzzService(){
addRule(i -> i % 3 == 0, "Fizz");
addRule(i -> i % 5 == 0, "Buzz");
addRule(i -> i % 15 == 0, "FizzBuzz");
}
输出:
Number : 3 First Matching Rule : Fizz
Number : 5 First Matching Rule : Buzz
Number : 6 First Matching Rule : Fizz
Number : 9 First Matching Rule : Fizz
Number : 10 First Matching Rule : Buzz
Number : 12 First Matching Rule : Fizz
Number : 15 First Matching Rule : FizzBuzz
Number : 18 First Matching Rule : Fizz
Number : 20 First Matching Rule : Buzz
Number : 21 First Matching Rule : Fizz
Number : 24 First Matching Rule : Fizz
Number : 25 First Matching Rule : Buzz
Number : 27 First Matching Rule : Fizz
Number : 30 First Matching Rule : FizzBuzz
FizzBuzzMap : {FizzBuzz=[15, 30], Fizz=[3, 6, 9, 12, 18, 21, 24, 27], Buzz=[5, 10, 20, 25]}
我正尝试在 Java8 中编写 FizzBuzz 问题。它工作正常,我得到了想要的输出。对于能被“3”整除的数,应该return"Fizz",对于能被“5”整除的数,应该return"Buzz",对于能被两者整除的数, 它应该 return "FizzBuzz".
如果我将值作为“15”传递,它 returns:
["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
现在,我被困在一件事上。如果我将值作为“15”传递,我想获得如下输出:
{"Fizz": [3, 6, 9, 12],"Buzz": [5, 10],"FizzBuzz": [15]}
我想按 Fizz、Buzz 和 FizzBuzz 对数字进行分组。
这是我的代码:
public class FizzBuzzService {
private Map<Rule, String> ruleContainers = new HashMap();
private Set<Rule> rules = new HashSet();
public FizzBuzzService(){
addRule(i -> i % 15 == 0, "FizzBuzz");
addRule(i -> i % 3 == 0, "Fizz");
addRule(i -> i % 5 == 0, "Buzz");
}
public void addRule(Rule rule, String res) {
rules.add(rule);
ruleContainers.put(rule, res);
}
public String getValue(int i) {
for (Rule rule : rules) {
if (rule.apply(i)) {
return ruleContainers.get(rule);
}
}
return String.valueOf(i);
}
//then the origin code should be as follows:
public List<String> fizzBuzz(int n) {
List<String> res = new ArrayList();
for(int i = 1; i <= n; i++){
res.add(getValue(i));
}
return res;
}
interface Rule{
boolean apply(int i);
}
}
如果有人能指导我,我将不胜感激。谢谢
我会 return Map<String, List<Integer>>
(使用 LinkedHashMap
而不是 HashMap
并且您的密钥将保留插入顺序)创建 List<Integer>
(s ) 对于 fizz
、buzz
和 fizzbuzz
值。它可以用一个 static
方法完成,例如
public static Map<String, List<Integer>> fizzBuzz(int n) {
Map<String, List<Integer>> map = new HashMap<>();
List<Integer> fizz = new ArrayList<>(), //
buzz = new ArrayList<>(), //
fizzbuzz = new ArrayList<>();
IntStream.rangeClosed(1, n).forEachOrdered(i -> {
boolean f = i % 3 == 0, b = i % 5 == 0;
if (f && b) {
fizzbuzz.add(i);
} else if (f) {
fizz.add(i);
} else if (b) {
buzz.add(i);
}
});
map.put("Fizz", fizz);
map.put("Buzz", buzz);
map.put("FizzBuzz", fizzbuzz);
return map;
}
Map<String, List<Integer>> map =
IntStream.rangeClosed(1, 15)
.mapToObj(n -> {
String str = n % 15 == 0 ? "FizzBuzz" :
(n % 5 == 0) ? "Buzz" : (n % 3 == 0) ? "Fizz" : null;
return str == null ? null : new SimpleEntry<>(str, n);
}).filter(Objects::nonNull)
.collect(groupingBy(Entry::getKey, mapping(Entry::getValue, toList())));
System.out.println(map);
这里有一种使用流的方法:
Map<String, List<String>> result = IntStream.rangeClosed(1, n)
.filter(i -> i % 3 == 0 || i % 5 == 0)
.boxed()
.collect(Collectors.groupingBy(i ->
i % 15 == 0 ? "FizzBuzz" :
i % 3 == 0 ? "Fizz" :
"Buzz"));
没有流:
Map<String, List<String>> result = new HashMap<>();
for (int i = 0; i < n; i++) {
if (i % 3 == 0 || i % 5 == 0) {
String key = i % 15 == 0 ? "FizzBuzz" :
i % 3 == 0 ? "Fizz" :
"Buzz";
result.computeIfAbsent(key, k -> new ArrayList<>()).add(i);
}
}
第 1 步:对号码应用规则。
步骤 2: 找到第一个匹配规则。
第三步:过滤不符合任何规则的元素。
第 4 步: 按规则关键字分组
public static void main(String[] args) {
FizzBuzzService();
Map<String, List<Integer>> fizzBuzzMap = IntStream.range(1, 31)
.mapToObj(number->applRule(number))
.filter(obj ->obj!=null)
.collect(Collectors.groupingBy(Entry::getKey,Collectors.mapping(Entry::getValue, Collectors.toList())));
System.out.println("FizzBuzzMap : " + fizzBuzzMap);
}
private static SimpleEntry<String, Integer> applRule(Integer number) {
Optional<Entry<Rule, String>> findFirst = ruleContainers.entrySet()
.stream()
.filter(rule -> rule.getKey().apply(number))
.findFirst();
if (findFirst.isPresent()) {
System.out.println("Number : " + number + " First Matching Rule : " +findFirst.get().getValue());
return new SimpleEntry<>(findFirst.get().getValue(), number);
}
return null;
}
}
public static void FizzBuzzService(){
addRule(i -> i % 3 == 0, "Fizz");
addRule(i -> i % 5 == 0, "Buzz");
addRule(i -> i % 15 == 0, "FizzBuzz");
}
输出:
Number : 3 First Matching Rule : Fizz
Number : 5 First Matching Rule : Buzz
Number : 6 First Matching Rule : Fizz
Number : 9 First Matching Rule : Fizz
Number : 10 First Matching Rule : Buzz
Number : 12 First Matching Rule : Fizz
Number : 15 First Matching Rule : FizzBuzz
Number : 18 First Matching Rule : Fizz
Number : 20 First Matching Rule : Buzz
Number : 21 First Matching Rule : Fizz
Number : 24 First Matching Rule : Fizz
Number : 25 First Matching Rule : Buzz
Number : 27 First Matching Rule : Fizz
Number : 30 First Matching Rule : FizzBuzz
FizzBuzzMap : {FizzBuzz=[15, 30], Fizz=[3, 6, 9, 12, 18, 21, 24, 27], Buzz=[5, 10, 20, 25]}