Java-Fundamentals-Regular-Expressions-Exercise
Java-Fundamentals-Regular-Expressions-Exercise
Problems for exercises and homework for the "Programming Fundamentals" course @ SoftUni.
You can check your solutions in Judge.
1. Furniture
Write a program to calculate the total cost of different types of furniture. You will be given some lines of input until
you receive the line "Purchase". For the line to be valid, it should be in the following format:
">>{furniture name}<<{price}!{quantity}"
The price can be a floating-point number or a whole number. Store the names of the furniture and the total price. In
the end, print each bought furniture on a separate line in the format:
"Bought furniture:
{1st name}
{2nd name}
…"
And on the last line, print the following: "Total money spend: {spend money}", formatted to the second
decimal point.
Examples
Input Output Comment
>>Sofa<<312.23!3 Bought furniture: Only the Sofa and the TV are valid, for
>>TV<<300!5 Sofa each of them we multiply the price by
the quantity and print the result.
>Invalid<<!5 TV
Purchase Total money spend: 2436.69
• Valid customer's name should be surrounded by '%' and must start with a capital letter, followed by lower-
case letters.
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 1 of 6
• Valid product contains any word character and must be surrounded by '<' and '>'.
• The valid count is an integer surrounded by '|'.
• The valid price is any real number followed by '$'.
The parts of a valid order should appear in the order given: customer, product, count, and price.
Between each part there can be other symbols, except ('|', '$', '%' and '. ')
For each valid line print on the console: "{customerName}: {product} - {totalPrice}"
When you receive "end of shift", print the total amount of money for the day rounded to 2 decimal places in
the following format: "Total income: {income}".
Input / Constraints
• Strings must be processed until you receive the text "end of shift".
Output
• Print all the valid lines in the format "{customerName}: {product} - {totalPrice}".
• After receiving "end of shift" print the total amount of money for the day rounded to 2 decimal places
in the following format: "Total income: {income}".
• Allowed working time / memory: 100ms / 16MB.
Examples
Input Output Comment
%InvalidName%<Croissant>|2|10.3$ Valid: Valid - 200.00 On the first line, the customer name
%Peter%<Gum>1.3$ Total income: 200.00 isn't valid, so we skip that line.
The second line is missing product
%Maria%<Cola>|1|2.4 count.
%Valid%<Valid>valid|10|valid20$ The third line doesn't have a valid
end of shift price.
And only the fourth line is valid
3. *Star Enigma
The war is at its peak, but you, young Padawan, can turn the tides with your programming skills. You are tasked to
create a program to decrypt the messages of The Order and prevent the death of hundreds of lives.
You will receive several messages, which are encrypted using the legendary star enigma. You should decrypt the
messages following these rules:
To properly decrypt a message, you should count all the letters [s, t, a, r] – case insensitive and remove the count
from the current ASCII value of each symbol of the encrypted message.
After decryption:
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 2 of 6
Each message should have a planet name, population, attack type ('A', as an attack or 'D', as destruction), and soldier
count.
The planet's name starts after '@' and contains only letters from the Latin alphabet.
The planet population starts after ':' and is an Integer;
The attack type may be "A"(attack) or "D"(destruction) and must be surrounded by "!" (Exclamation mark).
The soldier count starts after "->" and should be an Integer.
The order in the message should be: planet name -> planet population -> attack type -> soldier count. Each part can
be separated from the others by any character except: '@', '-', '!', ':' and '>'.
Input / Constraints
• The first line holds n – the number of messages – integer in the range [1…100].
• On the next n lines, you will be receiving encrypted messages.
Output
After decrypting all messages, you should print the decrypted information in the following format:
First print the attacked planets, then the destroyed planets.
"Attacked planets: {attackedPlanetsCount}"
"-> {planetName}"
"Destroyed planets: {destroyedPlanetsCount}"
"-> {planetName}"
The planets should be ordered by name alphabetically.
Examples
Input Output Comments
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 3 of 6
This is the invalid message, missing soldier
count, so we continue.
The third message has key 5.
@Cantonica:3000!D!->4000NM
4. *Nether Realms
A mighty battle is coming. In the stormy nether realms, demons fight against each other for supremacy in a duel
from which only one will survive.
Your job, however, is not so exciting. You must sign in all the participants in the nether realm's mighty battle's
demon book.
A demon's name contains his health and his damage.
The sum of the asci codes of all characters (excluding numbers (0-9), arithmetic symbols ('+', '-', '*', '/') and
delimiter dot ('.')) gives a demon's total health.
The sum of all numbers in his name forms his base damage. Note that you should consider the plus '+' and minus '-'
signs (e.g., +10 is 10 and -10 is -10). However, there are some symbols ('*' and '/') that can further alter the base
damage by multiplying or dividing it by 2 (e.g. in the name "m15*/c-5.0", the base damage is 15 + (-5.0) = 10 and
then you need to multiply it by 2 (e.g. 10 * 2 = 20) and then divide it by 2 (e.g. 20 / 2 = 10)).
So, multiplication and division are applied only after all numbers are included in the calculation and the order they
appear in the name.
Input
The input will be read from the console. The input consists of a single line containing all demon names separated by
commas and zero or more spaces in the format: "{demon name}, {demon name}, … , {demon name}"
Output
Print all demons, each on a separate line in the format:
• "{demon name} - {health points} health, {damage points} damage"
Constraints
• A demon's name will contain at least one character.
• A demon's name cannot contain blank spaces ' ' or commas ','.
• A floating-point number will always have digits before and after its decimal separator.
• The number in a demon's name is considered everything that is a valid integer or floating point number (with
a dot '.' used as separator). For example, all these are valid numbers: '4', '+4', '-4', '3.5', '+3.5', '-3.5'.
Examples
Input Output Comments
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 4 of 6
M3ph1st0**, Azazel M3ph1st0** - 524 health, M3ph1st0**:
16.00 damage Health - 'M' + 'p' + 'h' + 's' + 't' = 524 health.
Azazel - 615 health, 0.00 Damage - (3 + 1 + 0) * 2 * 2 = 16 damage.
damage
Azazel:
Health - 'A' + 'z' + 'a' + 'z' + 'e' + 'l' = 615 health.
Damage - no digits = 0 damage.
5. *Extract Emails
Write a program to extract all email addresses from a given text. The text comes at the only input line. Print the
emails on the console, each at a separate line. Emails are considered to be in format <user>@<host>, where:
• <user> is a sequence of letters and digits, where '.', '-' and '_' can appear between them.
o Examples of valid users: "stephan", "mike03", "s.johnson", "st_steward", "softuni-bulgaria",
"12345".
o Examples of invalid users: ''--123", ".....", "nakov_-", "_steve", ".info".
• <host> is a sequence of at least two words, separated by dots '.'. Each word is a sequence of letters and can
have hyphens '-' between the letters.
o Examples of hosts: "softuni.bg", "software-university.com", "intoprogramming.info",
"mail.softuni.org".
o Examples of invalid hosts: "helloworld", ".unknown.soft.", "invalid-host-", "invalid-".
• Examples of valid emails: "[email protected]", "[email protected]", "[email protected]",
"[email protected]", "[email protected]".
• Examples of invalid emails: "[email protected]", "…@mail.bg", "[email protected]", "[email protected]",
"mike@helloworld", "[email protected]. ", "s.johnson@invalid-".
Examples
Input Output
6. *Valid Password
Your first task is to determine if the given sequence of characters is a valid online banking password.
Each line must not contain anything else but a valid password. A password is valid when:
• It is surrounded by a "_" followed by one or more "."
• It is at least 6 characters long (without the surrounding "_" or ".")
• It starts with a capital letter
• It contains only letters (lower and upper case) and digits
• It ends with a capital letter
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 5 of 6
Examples of valid passwords: _...ChelseA_., _..Online1BankinG_., _.Valid1PasS_., _.A123f23A_.
Examples of invalid passwords: __InvalidPass.., _Invalid_, _.Invalid.IteM_., _.pass1InvaliD_.
Next, you have to determine which group the password is from. The group is obtained by concatenating all the
digits found in the password, if any. If there are no digits present in the password, the default group is "default".
Examples:
_...ChelseA_. -> group: default
_..Online1BankinG_. -> group: 1
_.A123f23A_.-> group: 12323
Input
On the first line, you will be given an integer n – the count of passwords that you will be receiving next.
On the following n lines, you will receive different strings.
Output
For each password that you process, you need to print a message.
If the password is invalid:
• "Invalid pass!"
If the password is valid:
• "Group: {group}"
Examples
Input Output
3 Group: default
_...ChelseA_. Group: 1
_.A1f23A_.
5 Group: 2
_...QAPassV_..
© SoftUni – about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 6 of 6