Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. package com.ivanasen.playground;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class Emojis {
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         String input = scanner.nextLine();
  13.  
  14.         solve(input);
  15.     }
  16.  
  17.     public static void solve(String input) {
  18.         BigInteger threshold = findCoolnessThreshold(input);
  19.  
  20.         System.out.println("Cool threshold: {" + threshold + "}");
  21.  
  22.         List<String> emojis = findEmojis(input);
  23.  
  24.         System.out.println(emojis.size() + " emojis found in the text. The cool ones are:");
  25.  
  26.         emojis.forEach(emoji -> {
  27.             BigInteger emojiCoolness = findCoolnessOfEmoji(emoji);
  28.             if (emojiCoolness.compareTo(threshold) >= 0) {
  29.                 System.out.println(emoji);
  30.             }
  31.         });
  32.     }
  33.  
  34.     public static BigInteger findCoolnessOfEmoji(String emoji) {
  35.         BigInteger result = BigInteger.ONE;
  36.  
  37.         for (int i = 2; i < emoji.length() - 2; i++) {
  38.             BigInteger charValue = BigInteger.valueOf(emoji.charAt(i));
  39.             result = result.add(charValue);
  40.         }
  41.  
  42.         return result;
  43.     }
  44.  
  45.     public static BigInteger findCoolnessThreshold(String input) {
  46.         BigInteger result = BigInteger.ONE;
  47.  
  48.         for (int i = 0; i < input.length(); i++) {
  49.             char c = input.charAt(i);
  50.  
  51.             if (Character.isDigit(c)) {
  52.                 result = result.multiply(new BigInteger(c + ""));
  53.             }
  54.         }
  55.  
  56.         return result;
  57.     }
  58.  
  59.     public static List<String> findEmojis(String input) {
  60.         List<String> emojis = new ArrayList<>();
  61.  
  62.         int currentStart = -1;
  63.         for (int i = 0; i < input.length(); i++) {
  64.             char c = input.charAt(i);
  65.  
  66.             if (currentStart == -1) {
  67.                 if (c == '*' || c == ':') {
  68.                     if (i < input.length() - 1 && input.charAt(i + 1) == c) {
  69.                         currentStart = i;
  70.                     }
  71.                     i++;
  72.                 }
  73.             } else {
  74.                 if (c == '*' || c == ':') {
  75.                     if (input.charAt(i - 1) == c) {
  76.                         if (isValidEmoji(input, currentStart, i + 1)) {
  77.                             emojis.add(input.substring(currentStart, i + 1));
  78.                         }
  79.                     } else if (i < input.length() - 1 && input.charAt(i + 1) == c) {
  80.                         if (isValidEmoji(input, currentStart, i + 2)) {
  81.                             emojis.add(input.substring(currentStart, i + 2));
  82.                         }
  83.                         i++;
  84.                     }
  85.  
  86.                     currentStart = -1;
  87.                 }
  88.             }
  89.         }
  90.  
  91.         return emojis;
  92.     }
  93.  
  94.     public static boolean isValidEmoji(String s, int start, int end) {
  95.         if (end - start < 7) {
  96.             return false;
  97.         }
  98.  
  99.         char usedChar = s.charAt(start);
  100.         if (usedChar != '*' && usedChar != ':') {
  101.             return false;
  102.         }
  103.  
  104.         if (s.charAt(start + 1) != usedChar) {
  105.             return false;
  106.         }
  107.  
  108.         char capital = s.charAt(start + 2);
  109.         if (!Character.isLetter(capital) || !Character.isUpperCase(capital)) {
  110.             return false;
  111.         }
  112.  
  113.         int innerCount = 1;
  114.  
  115.         for (int i = start + 3; i < end - 2; i++) {
  116.             char c = s.charAt(i);
  117.             if (!Character.isLetter(c) || !Character.isLowerCase(c)) {
  118.                 return false;
  119.             }
  120.             innerCount++;
  121.         }
  122.  
  123.         return innerCount >= 3 && s.charAt(end - 1) == usedChar && s.charAt(end - 2) == usedChar;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement