Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace fundamental14
- {
- class MainClass
- {
- public static void Main()
- {
- List<string> chest = Console.ReadLine().Split("|").ToList();
- string input = string.Empty;
- List<string> stolen = new List<string>();
- while ((input = Console.ReadLine()) != "Yohoho!")
- {
- List<string> commandArgs = input.Split().ToList();
- string command = commandArgs[0];
- switch (command)
- {
- case "Loot":
- string[] items = commandArgs.Skip(1).ToArray();
- LootCHest(items, chest);
- break;
- case "Drop":
- int index = int.Parse(commandArgs[1]);
- DropChest(index, chest);
- break;
- case "Steal":
- int count = int.Parse(commandArgs[1]);
- StealChest(count, chest);
- break;
- }
- }
- double average = GetAverageChest(chest);
- if (chest.Count != 0)
- {
- Console.WriteLine($"Average treasure gain: {average:f2} pirate credits.");
- }
- else
- {
- Console.WriteLine("Failed treasure hunt.");
- }
- //Console.WriteLine(string.Join(" ", chest));
- }
- private static double GetAverageChest(List<string> chest)
- {
- double sum = 0;
- foreach (var item in chest)
- {
- sum += item.Length;
- }
- double average = sum / chest.Count;
- return average;
- }
- private static void StealChest(int count, List<string> chest)
- {
- int index = chest.Count - count;
- string[] deletedItems = null;
- if (index >= 0)
- {
- deletedItems = chest.Skip(index).ToArray();
- chest.RemoveRange(index, count);
- }
- else
- {
- deletedItems = chest.ToArray();
- chest.Clear();
- }
- Console.WriteLine(string.Join(", ", deletedItems));
- }
- private static void DropChest(int index, List<string> chest)
- {
- if (IsValidIndex(index, chest))
- {
- string item = chest[index];
- chest.RemoveAt(index);
- chest.Add(item);
- }
- }
- private static bool IsValidIndex(int index, List<string> list)
- {
- return index < list.Count && index >= 0;
- }
- private static void LootCHest(string[] items, List<string> chest)
- {
- foreach (var item in items)
- {
- if (!chest.Contains(item))
- {
- chest.Insert(0, item);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement