Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace TreasureHunt
- {
- class MainClass
- {
- public static void Main(string[] args)
- {
- List<string> initialLoot = Console.ReadLine().Split('|').ToList();
- string command = string.Empty;
- while ((command = Console.ReadLine()) != "Yohoho!")
- {
- List<string> commandArray = command.Split().ToList();
- if (commandArray[0] == "Loot")
- {
- for (int i = 1; i <= commandArray.Count - 1; i++)
- {
- string item = commandArray[i];
- if (initialLoot.Exists(x => x == item))
- {
- continue;
- }
- else
- {
- initialLoot.Insert(0, item);
- }
- }
- }
- else if (commandArray[0] == "Drop")
- {
- int index = int.Parse(commandArray[1]);
- if (index >= 0 && index <= initialLoot.Count - 1)
- {
- string item = initialLoot[index];
- initialLoot.RemoveAt(index);
- initialLoot.Add(item);
- }
- }
- else if (commandArray[0] == "Steal")
- {
- int count = int.Parse(commandArray[1]);
- count = Math.Min(initialLoot.Count, count);
- List<string> listStolenItems = new List<string>();
- for (int i = initialLoot.Count - 1; i >= initialLoot.Count - count; i--)
- {
- string stolenItem = initialLoot[i];
- listStolenItems.Add(stolenItem);
- }
- initialLoot.RemoveRange(initialLoot.Count - count, count);// RemoveRange() from left to the right-from start index to count elements,never in the reverse order
- listStolenItems.Reverse();
- Console.WriteLine(string.Join(", ", listStolenItems));
- }
- }
- if (initialLoot.Count > 0)
- {
- decimal sumletters = 0;
- for (int i = 0; i <= initialLoot.Count - 1; i++)
- {
- string word = initialLoot[i];
- for (int k = 0; k <= word.Length - 1; k++)
- {
- char charOfWord = word[k];
- sumletters++;
- }
- }
- decimal result = sumletters / initialLoot.Count;
- Console.WriteLine($"Average treasure gain: {result:F2} pirate credits.");
- }
- else
- {
- Console.WriteLine("Failed treasure hunt.");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment