Tsuki11

За румен

Apr 5th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class A1_Task_2_Group_1 {
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.  
  11.         String findAllNumbers = "([0-9]{1})";
  12.         Pattern patternDigits = Pattern.compile(findAllNumbers);
  13.         String input = scan.nextLine();
  14.  
  15.         Matcher matcher = patternDigits.matcher(input);
  16.  
  17.         long sumCoolNum = 1;
  18.         while (matcher.find()) {
  19.             int digits = Integer.parseInt(matcher.group());
  20.             sumCoolNum *= digits;
  21.         }
  22.         System.out.println("Cool threshold: " + sumCoolNum);
  23.  
  24.         String emojRegex = "([*]{2}|[:]{2})([A-Z][a-z]{2,})(\\1)";
  25.         Pattern pattern = Pattern.compile(emojRegex);
  26.         matcher = pattern.matcher(input);
  27.         int found = 0;
  28.         int length = input.length();
  29.         List<String> coolEmojis = new ArrayList<>();
  30.         while (length > 0) {
  31.  
  32.             if (matcher.find()) {
  33.                 found++;
  34.                 String currentMatch = matcher.group(2);
  35.                 boolean isCool = isCool(sumCoolNum, currentMatch);
  36.                 if(isCool){
  37.                     coolEmojis.add(matcher.group(1) + currentMatch + matcher.group(3));
  38.                 }
  39.             }
  40.  
  41.             length--;
  42.         }
  43.         System.out.println(found + " emojis found in the text. The cool ones are:");
  44.         coolEmojis.forEach(System.out::println);
  45.  
  46.     }
  47.  
  48.     private static boolean isCool(long sumCoolNum, String currentMatch) {
  49.         String result = "";
  50.         long sum = 0;
  51.         for (int i = 0; i < currentMatch.length(); i++) {
  52.             int ascii = currentMatch.charAt(i);
  53.             sum += ascii;
  54.         }
  55.  
  56.         if (sum >= sumCoolNum) {
  57.             return true;
  58.         } else {
  59.             return false;
  60.         }
  61.     }
  62. }
Add Comment
Please, Sign In to add comment