Problem 3. Inventory
Loading...
+ Нов въпрос
bparvanova avatar bparvanova 6 Точки

03. Inventory - MidExam - C# Fundamentals

Здравейте,

Бихте ли ми помогнали със задачата. Предварително благодаря!

Това е кода ми: https://pastebin.com/gc0v7YsJ

Ето го условието:

Problem 3. Inventory

As a young traveler, you gather items and craft new items.

Input / Constraints

You will receive a journal with some Collecting items, separated with ', ' (comma and space). After that, until receiving "Craft!" you will be receiving different commands.

Commands (split by " - "):

  • "Collect - {item}" – Receiving this command, you should add the given item in your inventory. If the item already exists, you should skip this line.
  • "Drop - {item}" – You should remove the item from your inventory, if it exists.
  • "Combine Items - {oldItem}:{newItem}" – You should check if the old item exists, if so, add the new item after the old one. Otherwise, ignore the command.
  • "Renew – {item}" – If the given item exists, you should change its position and put it last in your inventory.

Output

After receiving "Craft!" print the items in your inventory, separated by ", " (comma and space).

Examples

Input

Output

Iron, Wood, Sword

Collect - Gold

Drop - Wood

Craft!

Iron, Sword, Gold

Input

Output

Iron, Sword

Drop - Bronze

Combine Items - Sword:Bow

Renew - Iron

Craft!

Sword, Bow, Iron

Тагове:
0
C# Fundamentals
Axiomatik avatar Axiomatik 1472 Точки
Best Answer
            List<string> items = Console.ReadLine()
                // You will receive a journal with some Collecting items, separated with ', ' (comma and space)
                //.Split(',', StringSplitOptions.RemoveEmptyEntries)
                .Split(", ", StringSplitOptions.RemoveEmptyEntries)
                .ToList();

 

                else if (command == "Combine Items")
                {
                    string[] itemsToCombine = item.Split(":");

                    string oldItem = itemsToCombine[0];

                    string newItem = itemsToCombine[1];

                    if (items.Contains(oldItem))
                    {
                        // "Combine Items - {oldItem}:{newItem}" – You should check if the old item exists,
                        // if so, add the new item after the old one. Otherwise, ignore the command.
                        //items.Insert((items.IndexOf(item) + 1), newItem);
                        items.Insert((items.IndexOf(oldItem) + 1), newItem);
                    }
                    continue;
                }

 

1
bparvanova avatar bparvanova 6 Точки

Thanks!

 

It was just the space after comma and I got 100/100.

1