SoftUni Bar Income
Условие(Задача 2): https://softuni.bg/trainings/resources/officedocument/52671/regular-expressions-more-exercise-python-fundamentals-september-2020/3132
Решение: https://pastebin.com/SVb1T8qU
Условие(Задача 2): https://softuni.bg/trainings/resources/officedocument/52671/regular-expressions-more-exercise-python-fundamentals-september-2020/3132
Решение: https://pastebin.com/SVb1T8qU
Привет
Ето и моето решение:
import re
global_pattern = r"\%[A-Z][a-z]+\%[^\|\$\%\.]*?\<\w+\>[^\|\$\%\.]*?\|[0-9]+\|[\w\-]*?[0-9.]+[0-9](?=\$)"
result = []
while True:
get_name = input()
if get_name == 'end of shift':
break
if re.search(global_pattern, get_name):
match = re.search(global_pattern, get_name)
name = re.search(r"(?<=%)\w+(?=%)", match.group())
product = re.search(r"(?<=<)\w+(?=>)", match.group())
qty = re.search(r"(?<=\|)\d+(?=\|)", match.group())
last = match.group().split('|')
price = re.search(r"[\d.]+", last[-1])
result.append([name.group(), product.group(), int(qty.group()), float(price.group())])
total = sum(list(map(lambda x: x[2]*x[3],result)))
for item in result:
print(f"{item[0]}: {item[1]} - {(item[2] * item[3]):.2f}")
print(f"Total income: {total:.2f}")
import re text = input() income = 0 while not text == "end of shift": matches = re.search(r"(\%[A-Z][a-z]+\%)([^\|\$\%\.]*?)(\<\w+\>)([^\|\$\%\.]*?)(\|[0-9]+\|)([^\|\$\%\.]*?)([0-9.]+[0-9])(?=\$)", text) if matches: name = matches.group(1)[1:-1] product = matches.group(3)[1:-1] count = int(matches.group(5)[1:-1]) price = float(matches.group(7)) amount = count * price income += amount print(f"{name}: {product} - {amount:.2f}") text = input() print(f"Total income: {income:.2f}")