Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Robot
using System; using System.Collections.Generic; using System.Linq; /* Arpana Mansfield - ROBOT submission * * Assumptions & Comments: * 1) More chips would be coming... so making sure that interfaces of the Chip are easy to modify and easy to understand * - the Chip interface could have the "Execute" method as a generic method, however, to simpilified the implementation (because the type would be returned) used an results object instead. * 2) Sorting Ascending and Descending... without more requirements, used the constructor to set whether or not the chip would be ascending or descending. It is possible to have that set within * the "Execute" method, with separate interfaces for each chip to extend the functionality. If the future chips had similar results and could reuse the return types, then this method is * probably better. However, if most of the chips would be creating new return values, then going the generic method route or extending specific device interfaces would be good. * 3) Assumed that the array would be changed by the chips. (Original value not kept) * 4) Testing is in the menu as "Run Diagnostics" * 5) Used Int[] for array. However, using a List would make more sense if generics are used. */ namespace Rextester { public enum ExecutionResultValue { Value, Array, Error }; public enum ExecutionSort { ASC, DESC }; public struct ExecutionResult { public ExecutionResultValue result; public int value; public int[] array; } public interface IChip { string Name { get; set; } ExecutionResult Execute(int[] numbers); } public class Robot { IChip currentChip; List<IChip> uniqueOperations = new List<IChip>(); int count = 0; //technically count can be gotten from the size of the uniqueOperations list, but since it was in the req, put it here public void Install(IChip chip) { currentChip = chip; //Saves the unique chips to the List of prior chips and increments the count. if (!(uniqueOperations.Exists( x => x.Name == chip.Name))) { uniqueOperations.Add(chip); count++; } } public ExecutionResult Execute(int[] numbers) { ExecutionResult r = new ExecutionResult(); if (currentChip == null) { r.result = ExecutionResultValue.Error; return r; } r = currentChip.Execute(numbers); return r; } public int Count() { return count; } //To allow the Chip information to be displayed on main menu public string Chip() { if (currentChip!=null) return currentChip.Name; return null; } } public class TotalChip : IChip { public string Name { get; set; } public TotalChip() { this.Name = "Total Chip"; } public ExecutionResult Execute(int[] numbers) { int sum = 0; foreach (int i in numbers) { sum += i; } ExecutionResult r = new ExecutionResult(); r.result = ExecutionResultValue.Value; r.value = sum; return r; } } public class ChipofSorts : IChip { public string Name { get; set; } public ExecutionSort sort = ExecutionSort.ASC; public ChipofSorts() { this.Name = "Chip of Sorts"; } //constructor with sort direction public ChipofSorts(ExecutionSort sortBy) { this.Name = "Chip of Sorts"; this.sort = sortBy; } public ExecutionResult Execute(int[] numbers) { ExecutionResult r = new ExecutionResult(); r.result = ExecutionResultValue.Array; //not reinventing the wheel, using LINQ sort if (sort == ExecutionSort.DESC) r.array = numbers.OrderByDescending(d => d).ToArray(); else r.array = numbers.OrderBy(d => d).ToArray(); return r; } } public class Program { static string Array2String(int[] numbers) { string s=""; for (int i = 0; i < numbers.Length; i++) s += ((i == 0 ? "" : ",") + numbers[i].ToString()); return s; } static void RunDiagnostics() { ChipofSorts sa = new ChipofSorts(); ChipofSorts sd = new ChipofSorts(ExecutionSort.DESC); TotalChip t = new TotalChip(); Robot robot = new Robot(); int[] a = { 1, 2, 5, 3, 2 }; ExecutionResult r; Console.WriteLine("Testing Robot..."); Console.WriteLine(); Console.WriteLine("***********************************************"); Console.WriteLine(); Console.WriteLine("Running Chip of Sorts Ascending..."); Console.WriteLine("Array being tested: {0}", Array2String(a)); Console.WriteLine("Expect Results: 1,2,2,3,5"); robot.Install(sa); r = robot.Execute(a); Console.Write("Robot Returned: {0}", Array2String(r.array)); Console.WriteLine(); Console.WriteLine("***********************************************"); Console.WriteLine(); Console.WriteLine("Running Chip of Sorts Descending..."); Console.WriteLine("Array being tested: {0}", Array2String(a)); Console.WriteLine("Expect Results: 5,3,2,2,1"); robot.Install(sd); r=robot.Execute(a); Console.Write("Robot Returned: {0}", Array2String(r.array)); Console.WriteLine(); Console.WriteLine("***********************************************"); Console.WriteLine(); Console.WriteLine("Running Total..."); Console.WriteLine("Array being tested: {0}", Array2String(a)); Console.WriteLine("Expect Results: 13"); robot.Install(t); r = robot.Execute(a); Console.Write("Robot Returned: {0}", r.value); Console.WriteLine(); Console.WriteLine("***********************************************"); Console.WriteLine(); Console.WriteLine("Finished Testing!"); } static void StartRobot() { ChipofSorts sa = new ChipofSorts(); ChipofSorts sd = new ChipofSorts(ExecutionSort.DESC); TotalChip t = new TotalChip(); Robot mrrobot = new Robot(); string command=""; int[] numbers = new int[0]; while (command != "Q") { Console.WriteLine("***********************************************"); Console.WriteLine(" Array: {0}", Array2String(numbers)); Console.WriteLine(" Chip installed: {0}", mrrobot.Chip()); Console.WriteLine(" Chip count : {0}", mrrobot.Count()); Console.WriteLine("-----------------------------------------------"); Console.WriteLine("COMMANDS:"); Console.WriteLine(); Console.WriteLine(" (A) Enter Array"); if (numbers.Length>0) { Console.WriteLine(" (1) Install Sort Ascending Chip"); Console.WriteLine(" (2) Install Sort Descending Chip"); Console.WriteLine(" (3) Install Total Chip"); if (mrrobot.Chip()!=null) Console.WriteLine(" (X) Execute"); } Console.WriteLine(" (T) Run Diagnostics"); Console.WriteLine(" (Q) Quit"); Console.WriteLine(); Console.Write("Enter command: "); command = Console.ReadLine(); Console.WriteLine(); if (command.ToUpper() == "A") { Console.WriteLine("Enter array (separated by commas): "); string entry = Console.ReadLine(); if (entry.Length>0) numbers = Array.ConvertAll(entry.Split(','), int.Parse); } else if (command.ToUpper() == "T") { RunDiagnostics(); } else if (command.ToUpper() == "Q") { Console.WriteLine("Okie Dokie... I'm outta here!"); } else if (numbers.Length > 0 ) { if (command =="1") { mrrobot.Install(sa); } else if (command == "2") { mrrobot.Install(sd); } else if (command == "3") { mrrobot.Install(t); } else if (command.ToUpper() == "X" && mrrobot.Chip()!=null) { ExecutionResult r = mrrobot.Execute(numbers); if (r.result == ExecutionResultValue.Value) { Console.WriteLine("Robot Returned: " + r.value.ToString()); } else if (r.result == ExecutionResultValue.Array) { numbers = r.array; Console.Write("Robot Returned: {0}", Array2String(numbers)); } } } Console.WriteLine(); Console.WriteLine(); } } public static void Main(string[] args) { RunDiagnostics(); // StartRobot(); } } }
run
|
edit
|
history
|
help
0
ex8 For_Loop
FREE V-BUCKS GENERATOR NO HUMAN VERIFICATION 2022 [zT4]
15
Timer1
Jennifer Lawrence hot season Live cam
For - box
array integration
multiplicação
value tuple
Fibonacci and finding nth fibo number