Problem 2. Fancy Barcodes
Loading...
IttyBitty avatar IttyBitty 26 Точки

02. Fancy Barcodes

Problem 2. Fancy Barcodes

Your first task is to determine if the given sequence of characters is a valid barcode or not.

Each line must not contain anything else but a valid barcode. A barcode is valid when:

  • Is surrounded with a "@" followed by one or more "#"

  • Is at least 6 characters long (without the surrounding "@" or "#")

  • Starts with a capital letter

  • Contains only letters (lower and upper case) and digits

  • Ends with a capital letter

Examples of valid barcodes: @#FreshFisH@#, @###Brea0D@###, @##Che46sE@##, @##Che46sE@###

Examples of invalid barcodes: ##InvaliDiteM##, @InvalidIteM@, @#Invalid_IteM@#

Next you have to determine the product group of the item from the barcode. The product group is obtained by concatenating all the digits found in the barcode. If there are no digits present in the barcode, the default product group is "00".

Examples:

@#FreshFisH@# -> product group: 00

@###Brea0D@### -> product group: 0

@##Che4s6E@## -> product group: 46

Input

On the first line you will be given an integer n – the count of barcodes that you will be receiving next.

On the next n lines, you will receive different strings.

Output

For each barcode that you process, you need to print a message.

If the barcode is invalid:

  • "Invalid barcode"

If the barcode is valid:

  • "Product group: {product group}" 

 

 

Input

Output

3

@#FreshFisH@#

@###Brea0D@###

@##Che4s6E@##

Product group: 00

Product group: 0

Product group: 46

Input

Output

6

@###Val1d1teM@###

@#ValidIteM@#

##InvaliDiteM##

@InvalidIteM@

@#Invalid_IteM@#

@#ValiditeM@#

Product group: 11

Product group: 00

Invalid barcode

Invalid barcode

Invalid barcode

Product group: 00

 

 

https://pastebin.com/m2vrfh2h

Кода ми дава 80/100 и не намирам грешката.. Опитах да счупя регекса и всичко друго, но нищо. По аутпут вярно, а Джъдж се сърди. Ще се радвам някой да ми помогне. И знам, че задачата я има по лекциите, ама ми се искаше да си я реша сама. :D

П.С.: Опитах няколко пъти да си едитна табличката с input и output, но нещо не ми се получи. Не съм особено активна във форума - също ще бъда щастлива някой да ме навигира как да пействам адекватни таблици :D

0
Fundamentals of Programming (with C#) 05/12/2020 07:52:34
Axiomatik avatar Axiomatik 1472 Точки

That solution is OK, only your regex shouldn't use backreference for the final part of the string   "Is surrounded with a "@" followed by one or more "#"" => doesn't say that the same pattern has to be repeated.

using System;
using System.Text.RegularExpressions;

namespace ExamPrep_02_FancyBarcodes
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = int.Parse(Console.ReadLine());

            //string pattern = @"(@#+)([A-Z][A-Za-z0-9]{4,}[A-Z])\1";
            string pattern = @"^@#+([A-Z][A-Za-z0-9]{4,}[A-Z])@#+$"; // No Backreference
            Regex barcodeRegex = new Regex(pattern);

            while (count-- > 0)
            {
                string input = Console.ReadLine();
                Match match = Regex.Match(input, pattern);

                if (match.Success)
                {
                    string productGroup = "";

                    for (int i = 0; i < match.Value.Length; i++)
                    {
                        if (char.IsDigit(match.Value[i]))
                        {
                            productGroup += match.Value[i];
                        }
                    }

                    if (productGroup != "")
                    {
                        Console.WriteLine($"Product group: {productGroup}");
                    }
                    else
                    {
                        Console.WriteLine($"Product group: 00");
                    }
                }
                else
                {
                    Console.WriteLine("Invalid barcode");
                }
            }
        }
    }
}

 

1
IttyBitty avatar IttyBitty 26 Точки

Oh, i see it now. Thank you very much. 100/100. 

0