- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
package com.engineerdollery;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.*;
public class FizzBuzz {
private static final String NUMBER = "\\d+";
public String basic() {
return IntStream.rangeClosed(1, 20)
.parallel()
.mapToObj(i -> i % 15 == 0 ? "fizzbuzz"
: i % 3 == 0 ? "fizz"
: i % 5 == 0 ? "buzz"
: Integer.toString(i))
.collect(joining(" "));
}
public String lucky() {
return IntStream.rangeClosed(1, 20)
.parallel()
.mapToObj(i -> Integer.toString(i).contains("3") ? "lucky" // this is the only change from basic()
: i % 15 == 0 ? "fizzbuzz"
: i % 3 == 0 ? "fizz"
: i % 5 == 0 ? "buzz"
: Integer.toString(i))
.collect(joining(" "));
}
public String counter() {
List<String> fizzBuzzList = IntStream.rangeClosed(1, 20)
.parallel()
.mapToObj(i -> Integer.toString(i).contains("3") ? "lucky"
: i % 15 == 0 ? "fizzbuzz"
: i % 3 == 0 ? "fizz"
: i % 5 == 0 ? "buzz"
: Integer.toString(i))
.collect(Collectors.toList());
Map<String, Long> countMap = fizzBuzzList
.parallelStream()
.collect(groupingBy(s -> s.matches(NUMBER) ? "integer" : s, counting()));
// reports
String fizzbuzz = fizzBuzzList.parallelStream().collect(joining(" "));
String counts = countMap.entrySet().parallelStream()
.map(e -> e.getKey() + ": " + e.getValue())
.collect(joining("\n"));
return fizzbuzz + "\n" + counts;
}
}
someone 28.04.2016 15:13 # +4
3.14159265 28.04.2016 15:37 # +4
3_14dar 28.04.2016 22:35 # 0
Не говно?
Hanazono-Satoshi 29.04.2016 17:08 # +1
twisted_rain 29.05.2016 11:18 # −4
j123123 29.05.2016 11:53 # +2
guesto 29.05.2016 15:12 # +1
gost 29.05.2016 15:28 # +2
> 303 вопроса
За вопрос по доллару - сделаем.