02. Shopping List
Input
You will receive an initial list with groceries separated by an exclamation mark "!".
After that, you will be receiving 4 types of commands until you receive "Go Shopping!".
- "Urgent {item}" - add the item at the start of the list. If the item already exists, skip this command.
- "Unnecessary {item}" - remove the item with the given name, only if it exists in the list. Otherwise, skip this command.
- "Correct {oldItem} {newItem}" - if the item with the given old name exists, change its name with the new one. Otherwise, skip this command.
- "Rearrange {item}" - if the grocery exists in the list, remove it from its current position and add it at the end of the list. Otherwise, skip this command.
Constraints
- There won't be any duplicate items in the initial list
Output
- Print the list with all the groceries, joined by ", ":
"{firstGrocery}, {secondGrocery}, … {nthGrocery}"
Examples
Input |
Output |
Tomatoes!Potatoes!Bread Unnecessary Milk Urgent Tomatoes Go Shopping! |
Tomatoes, Potatoes, Bread |
Input |
Output |
Milk!Pepper!Salt!Water!Banana Urgent Salt Unnecessary Grapes Correct Pepper Onion Rearrange Grapes Correct Tomatoes Potatoes Go Shopping! |
Milk, Onion, Salt, Water, Banana |
using System;
using System.Linq;
namespace _02._Shopping_List
{
class Program
{
static void Main(string[] args)
{
string[] products = Console.ReadLine()
.Split("!", StringSplitOptions.RemoveEmptyEntries)
.ToArray();
string input = string.Empty;
string[] urgentArray;
string[] unessesaryArray;
string[] saveArray=products;
string[] rearrangeArray;
int count = 0;
while ((input=Console.ReadLine())!= "Go Shopping!")
{
string[] inputArray = input
.Split(" ", StringSplitOptions.RemoveEmptyEntries)
.ToArray();
string command = inputArray[0];
if (command== "Urgent")
{
string item = inputArray[1];
if (products.Contains(item))
{
continue;
}
count++;
urgentArray = new string[products.Length+count];
urgentArray[0] = item;
for (int i = 1; i < urgentArray.Length; i++)
{
if (i<products.Length)
{
urgentArray[i] = products[i - 1];
}
}
saveArray = urgentArray;
}
else if (command== "Unnecessary")
{
string item = inputArray[1];
unessesaryArray = new string[products.Length - 1];
if (saveArray.Contains(item))
{ int j = 0;
for (int i = 0; i < saveArray.Length; i++)
{
if (saveArray[i]!=item)
{
unessesaryArray[j] = saveArray[i];
j++;
}
}
saveArray = unessesaryArray;
}
continue;
}
else if (command== "Correct")
{
string oldName = inputArray[1];
string newName = inputArray[2];
if (saveArray.Contains(oldName))
{
for (int i = 0; i < saveArray.Length; i++)
{
if (saveArray[i]==oldName)
{
saveArray[i] = newName;
}
}
}
continue;
}
else if (command== "Rearrange")
{
string item = inputArray[1];
rearrangeArray = saveArray;
if (saveArray.Contains(item))
{
for (int i = 0; i < saveArray.Length; i++)
{
if (saveArray[i]==item && i+1<rearrangeArray.Length)
{
rearrangeArray[i] = saveArray[i + 1];
}
else
{
rearrangeArray[i] = saveArray[i];
}
rearrangeArray[rearrangeArray.Length - 1] = item;
}
saveArray = rearrangeArray;
}
continue;
}
}
Console.WriteLine(string.Join(", ",saveArray));
}
}
}
Ако накой разбере начина ми на решение добре ако не може да покажете свое решение на задачата.Условието е ,че искам задачата да се реши със МАСИВ а не със ЛИСТ с лист ще е елементарно.Ако някой я е решавал с МАСИВИ моля да сподели Благодаря.
Линк към ДЖЪДЖ -https://judge.softuni.org/Contests/Practice/Index/2031#1
Благодаря!
Това е моят код на задачата, в Intellij всичко е както трябва, но judje ми дава 60/100. Виждате ли къде може да греша ?