Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.util.LinkedHashMap;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class EmojiDetector {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String initialLine = scanner.nextLine();
- String regExForDigits = "(?<digit>[\\d])";
- String regExForEmoji = "(?<emoji>([:]{2}|[*]{2})[A-Z][a-z]{2,}(\\2))";
- Pattern patternForDigits = Pattern.compile(regExForDigits);
- Matcher matcherForDigits = patternForDigits.matcher(initialLine);
- BigInteger bigIntegerCoolThreshold = new BigInteger("0");
- if (matcherForDigits.find()) {
- bigIntegerCoolThreshold = bigIntegerCoolThreshold
- .add(new BigInteger(matcherForDigits.group("digit")));
- while (matcherForDigits.find()) {
- bigIntegerCoolThreshold = bigIntegerCoolThreshold
- .multiply(new BigInteger(matcherForDigits.group("digit")));
- }
- }
- LinkedHashMap<String, String> mapListOfEmojiAndCoolnessCheck = new LinkedHashMap<>();
- Pattern patternForEmoji = Pattern.compile(regExForEmoji);
- Matcher matcherForEmoji = patternForEmoji.matcher(initialLine);
- while (matcherForEmoji.find()) {
- String emojiToAdd = matcherForEmoji.group("emoji");
- BigInteger currentEmojiCoolness = new BigInteger("0");
- for (int i = 2; i < emojiToAdd.length() - 2; i++) {
- int charValueToAdd = emojiToAdd.charAt(i);
- currentEmojiCoolness = currentEmojiCoolness.add(new BigInteger("" + charValueToAdd));
- }
- if (currentEmojiCoolness.compareTo(bigIntegerCoolThreshold) >= 0) {
- String isCoolEmoji = "yes";
- mapListOfEmojiAndCoolnessCheck.put(emojiToAdd, isCoolEmoji);
- } else {
- String isCoolEmoji = "no";
- mapListOfEmojiAndCoolnessCheck.put(emojiToAdd, isCoolEmoji);
- }
- }
- System.out.println("Cool threshold: " + bigIntegerCoolThreshold);
- System.out.println(String.format("%d emojis found in the text. The cool ones are:",
- mapListOfEmojiAndCoolnessCheck.size()));
- mapListOfEmojiAndCoolnessCheck
- .entrySet()
- .stream()
- .filter(e -> e.getValue().equals("yes"))
- .forEach(e -> System.out.println(e.getKey()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement