hackerrank "arithmetic expressions"

using System.CodeDom.Compiler; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Serialization; using System.Text.RegularExpressions; using System.Text; using System; class Solution { //********SLOWSLOWSLOWSLOWSLOWSLOW****** // outputs Operation[] that, when applied to // int[] arr, results in a mutiple of 101 private static Operation[] FindOps(int[] arr) { Operation[] ops = new Operation[arr.Length - 1]; int opsCounter = 0; // tried applying all ops so far at each stage, // multiplying the rest of numbers because 101*bla*blah % 101 == 0 // while OpsOnArr(arr.Take(i + 2).ToArray(), ops.Take(i + 1).ToArray()) % 101 != 0) // also while (OpsOnArr(arr, ops) % 101 != 0) // for (int opsCounter = 0; ) { // count in base 3 (binary from 0 to 2) // modulo 3 for number and subtract from original // divide by 3, rounding down to nearest multiple of 3 // repeat for each subsequent number int tmp = opsCounter; for (int i = 0; i < ops.Length; ++i) { // convert opsCount to 'binary' but in base 3 // i.e. 14 -> 2, 1, 1 // 1s, 3s, 9s ops[i] = (Operation)(tmp % 3); tmp = (tmp - (tmp % 3)) / 3; // is it a faster method } // (never used, all examples given are possible) // return null if exceeded possible choices if (opsCounter == Math.Pow(3, ops.Length)) { return null; } // -------------------- // increment counter ++opsCounter; } return ops; } //***********ENDOFSLOW********************* // Operations as ints private enum Operation { MULTIPLY, PLUS, MINUS } // Returns String version of an equation created with given Operations and ints private static String EquationToString(Operation[] ops, int[] arr) { String s = arr[0].ToString(); for (int i = 0; i < ops.Length; ++i) { switch (ops[i]) { case Operation.MULTIPLY: s += "*"; break; case Operation.PLUS: s += "+"; break; case Operation.MINUS: s += "-"; break; } s += arr[i + 1]; } return s; } // applies Operations on given int[] arr // NOTE: arr.Length = ops.Length + 1 private static int OpsOnArr(int[] arr, Operation[] ops) { int result = arr[0]; for (int i = 0; i < ops.Length; ++i) { switch (ops[i]) { case Operation.MULTIPLY: result *= arr[i + 1]; break; case Operation.PLUS: result += arr[i + 1]; break; case Operation.MINUS: result -= arr[i + 1]; break; } } return result; } static void Main(string[] args) { TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true); Console.ReadLine(); int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp)); textWriter.WriteLine(EquationToString(FindOps(arr), arr)); // // tests: // Operation[] opsTestOne = new Operation[3] // { // Operation.PLUS, // Operation.MULTIPLY, // Operation.MINUS // }; // int[] arrTestOne = new int[4] { 20, 55, 2, 49 }; // Operation[] opsTestTwo = new Operation[] // { // Operation.MINUS, // Operation.MINUS, // Operation.MINUS, // Operation.MINUS, // Operation.MINUS, // Operation.MINUS, // Operation.MINUS, // Operation.MINUS, // Operation.MINUS, // Operation.MINUS, // Operation.MINUS, // Operation.MINUS // }; // int[] arrTestTwo = new int[] {49, 24, 13, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1}; // textWriter.WriteLine(OpsOnArr(arrTestTwo, FindOps(arrTestTwo))); // textWriter.WriteLine(EquationToString(FindOps(arrTestTwo), arrTestTwo)); // // -- EquationToString (check) // textWriter.WriteLine("EquationToString:"); // textWriter.WriteLine(EquationToString(opsTestOne, arrTestOne)); // // -- FindOps (check?) negatives?? // textWriter.WriteLine("FindOps:"); // foreach (Operation o in FindOps(arr)) // { // textWriter.Write(o + " "); // } // // -- OpsOnArr (check) // textWriter.WriteLine("\nEquation result:"); // textWriter.WriteLine(OpsOnArr(arrTestOne, opsTestOne)); textWriter.Flush(); textWriter.Close(); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.