borislavsimonov 6 Точки

## 02.Emoji Detector,Programming Fundamentals Final Exam - 04 April 2020 Group 1

Здравейте,

някой може ли да помогне ,докарвам го до 80/100 и не мога да разбера къде е проблема.

https://pastebin.com/pQYt8Ndf

https://judge.softuni.bg/Contests/Practice/Index/2302#1

# Problem 2. Emoji Detector

Your task is to write program which extracts emojis from a text and find the threshold based on the input.

You have to get your cool threshold. It is obtained by multiplying all the digits found in the input.  The cool threshold could be a very big number, so be mindful.

An emoji is valid when:

• Is surrounded by either :: or ** (exactly 2)
• Is at least 3 characters long (without the surrounding symbols)
• Starts with a capital letter
• Continues with lowercase letters only

Examples of valid emojis: ::Joy::, **Banana**, ::Wink::

Examples of invalid emojis: ::Joy**, ::fox:es:, **Monk3ys**, :Snak::Es::

You need to count all valid emojis in the text and calculate their coolness. The coolness of the emoji is determined by summing all the ASCII values of all letters in the emoji.

Examples: ::Joy:: - 306, **Banana** - 577, ::Wink:: - 409

You need to print the result of cool threshold and after that to take all emojis out of the text, count them and print the only the cool ones on the console.

### Input

• On the single input you will receive a piece of string.

### Output

• On the first line of the output print the obtained Cool threshold in format:
• Cool threshold: {coolThresholdSum}

On the next line print the count of all emojis found in the text in format:

• {countOfAllEmojis} emojis found in the text. The cool ones are:
• {cool emoji 1}
• {cool emoji 2}
• {…}

If there are no cool ones, just don't print anything in the end.

### Constraints

There will always be at least one digit in the text!

### Examples

 Input Output In the Sofia Zoo there are 311 animals in total! ::Smiley:: This includes 3 **Tigers**, 1 ::Elephant:, 12 **Monk3ys**, a **Gorilla::, 5 ::fox:es: and 21 different types of :Snak::Es::. ::Mooning:: **Shy** Cool threshold: 540 4 emojis found in the text. The cool ones are: ::Smiley:: **Tigers** ::Mooning:: Comments You can see all the valid emojis in green. There are various reasons why the rest are not valid, examine them carefully. The "cool threshold" is 3*1*1*3*1*1*2*3*5*2*1 = 540. ::Smiley:: -> 83 + 109 + 105 + 108 + 101 + 121 = 627 > 540 -> cool **Tigers** -> 84 + 105 + 103 + 101 + 114 + 115 = 622 > 540 -> cool ::Mooning:: -> 77 + 111 + 111 + 112 + 105 + 112 + 103 = 727 > 540 -> cool **Shy** -> 83 + 104 + 121 = 308 < 540 -> not cool At the end we print the count of all valid emojis found and each of the cool ones on a new line. Input Output 5, 4, 3, 2, 1, go! The 1-th consecutive banana-eating contest has begun! ::Joy:: **Banana** ::Wink:: **Vali** ::valid_emoji:: Cool threshold: 120 4 emojis found in the text. The cool ones are: ::Joy:: **Banana** ::Wink:: **Vali** Input Output It is a long established fact that 1 a reader will be distracted by 9 the readable content of a page when looking at its layout. The point of using ::LoremIpsum:: is that it has a more-or-less normal 3 distribution of 8 letters, as opposed to using 'Content here, content 99 here', making it look like readable **English**. Cool threshold: 17496 1 emojis found in the text. The cool ones are:

Тагове:
0
MartinBG 3981 Точки

Проблемът е, че не нулирате променливата sumCharEmoji винаги, а само когато е по-гляма от coolThreshold:

``````        long sumCharEmoji = 0;
for (String s : emojiList) {
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isLetter(ch)) {
sumCharEmoji += s.charAt(i);
}
}
if (sumCharEmoji >= sum) {
System.out.println(s);
sumCharEmoji = 0; // BUG!!!
}

}``````

Променете кода така и ще вземе 100/100:

``````        long sumCharEmoji = 0L;
for (String s : emojiList) {
sumCharEmoji = 0L; // proper initialization
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isLetter(ch)) {
sumCharEmoji += s.charAt(i);
}
}
if (sumCharEmoji >= sum) {
System.out.println(s);
}
}``````

Ето и алтернативно решение на задачата:

``````import java.util.List;
import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class EmojiDetector {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

String text = scanner.nextLine();

List<String> emojiList = Pattern.compile("(:{2}|\\*{2})[A-Z][a-z]{2,}\\1")
.matcher(text)
.results()
.map(MatchResult::group)
.collect(Collectors.toList());

long coolThreshold = Pattern.compile("\\d")
.matcher(text)
.results()
.mapToLong(matchResult -> Long.parseLong(matchResult.group()))
.reduce(1L, (a, b) -> a * b);

System.out.printf("Cool threshold: %d%n", coolThreshold);
System.out.printf("%d emojis found in the text. The cool ones are:%n", emojiList.size());

long finalCoolThreshold = coolThreshold;
emojiList.stream()
.filter(emoji -> emoji.substring(2, emoji.length() - 2).chars().sum() >= finalCoolThreshold)
.forEach(System.out::println);
}
}
``````

0
06/12/2020 23:38:54