using System; using System.Linq; using System.Linq.Expressions; public class ArtihmeticParser { public static int EvaluateSum(string input) { // [1,3,4,6] --> 0+1 = 1, 1+3 = 4, 4+4 = 8, 8+6 = 14 return input.Split("+").Select(EvaluateProduct).Aggregate(0,(a,b) => a+b); } private static int EvaluateProduct(string input) { // [1,3,4,6] --> 1*1 = 1, 1*3 = 3, 3*4 = 12, 12*6 = 72 return input.Split("*").Select(EvaluateInteger).Aggregate(1,(a,b) => a*b); } private static int EvaluateInteger(string input) { return int.Parse(input); } private static int EvaluateDifference(string input) { /* Expression subtractExpr = Expression.Subtract { Expression.Constant(a), Expression.Constant(b) }; Console.WriteLine(Expression.Lambda<Func<int>>(subtractExpr).Compile().Invoke()); */ //return input.Split("-").Select(EvaluateSum).Aggregate(0,(a,b) => a-b); // int numOfValues; // int numOfValues = 0; input.Split("-"); int a = input[0]; int b = input[1]; // int answer = a - b; // for(int i = 0; i < input.Length; i++) { // int result = a - b; // return result; // do I need a while loop? while i > 1 } // return result; } }
- using System;
- using System.Linq;
- using System.Linq.Expressions;
- public class ArtihmeticParser
- {
public static int Evaluate(string input)- public static int EvaluateSum(string input)
- {
- // [1,3,4,6] --> 0+1 = 1, 1+3 = 4, 4+4 = 8, 8+6 = 14
- return input.Split("+").Select(EvaluateProduct).Aggregate(0,(a,b) => a+b);
- }
- private static int EvaluateProduct(string input)
- {
- // [1,3,4,6] --> 1*1 = 1, 1*3 = 3, 3*4 = 12, 12*6 = 72
- return input.Split("*").Select(EvaluateInteger).Aggregate(1,(a,b) => a*b);
- }
- private static int EvaluateInteger(string input)
- {
- return int.Parse(input);
- }
}- private static int EvaluateDifference(string input)
- {
- /* Expression subtractExpr = Expression.Subtract {
- Expression.Constant(a),
- Expression.Constant(b)
- };
- Console.WriteLine(Expression.Lambda<Func<int>>(subtractExpr).Compile().Invoke());
- */
- //return input.Split("-").Select(EvaluateSum).Aggregate(0,(a,b) => a-b);
- // int numOfValues;
- // int numOfValues = 0;
- input.Split("-");
- int a = input[0];
- int b = input[1];
- // int answer = a - b;
- // for(int i = 0; i < input.Length; i++) {
- // int result = a - b;
- // return result;
- // do I need a while loop? while i > 1
- }
- // return result;
- }
- }
using NUnit.Framework; using System; [TestFixture] public class SolutionTest { [Test] public void TestOneNumber3() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("3"), 3); } [Test] public void TestOneNumber10() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("10"), 10); } [Test] public void TestOneNumber729() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("729"), 729); } [Test] public void TestOneNumber0() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("0"), 0); } [Test] public void TestAdditionOfTwoNumbers2() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("1+1"), 2); } [Test] public void TestAdditionOfTwoNumbers22() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("10+12"), 22); } [Test] public void TestAdditionOfThreeNumbers6() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("1+2+3"), 6); } [Test] public void TestAdditionOfFiveNumbers() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("1+2+3+4+5"), 15); } [Test] public void TestProductOfTwoNumbers1() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("1*1"), 1); } [Test] public void TestProductOfTwoNumbers120() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("10*12"), 120); } [Test] public void TestProductOfThreeNumbers6() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("1*2*3"), 6); } [Test] public void TestProductOfFiveNumbers() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("1*2*3*4*5"), 120); } // ------------ [Test] public void TestDifferenceOfTwoNumbers() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("4-2"), 2); } // ------------ [Test] public void TestSumAndProduct() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("2+4*1"), 6); } [Test] public void TestOrderOfOperations() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("2+4*6"), 26); } [Test] public void TestLongerInput() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("1000*3*2+100*2*2+2*2*2*2*5+9"), 6489); } [Test] [Ignore("Brackets")] public void TestBrackets() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("(2+4)*6"), 36); } [Test] public void TestWhitespace() { Assert.AreEqual(ArtihmeticParser.EvaluateSum(" 2 + 4 *6 "), 26); } [Test] [Ignore("Subtraction")] public void TestSubtraction() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("1+3-2+4"), 6); } [Test] [Ignore("Subtraction")] public void TestOrderOfOperationsWithSubtraction() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("10+15-2*2+10-3*4"), 19); } [Test] [Ignore("Subtraction and brackets")] public void TestBracketsAndSubtraction() { Assert.AreEqual(ArtihmeticParser.EvaluateSum("100-2*(2+10-3)*4"), 28); } }
- using NUnit.Framework;
- using System;
- [TestFixture]
- public class SolutionTest
- {
- [Test]
- public void TestOneNumber3()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("3"), 3);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("3"), 3);
- }
- [Test]
- public void TestOneNumber10()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("10"), 10);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("10"), 10);
- }
- [Test]
- public void TestOneNumber729()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("729"), 729);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("729"), 729);
- }
- [Test]
- public void TestOneNumber0()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("0"), 0);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("0"), 0);
- }
- [Test]
- public void TestAdditionOfTwoNumbers2()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("1+1"), 2);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("1+1"), 2);
- }
- [Test]
- public void TestAdditionOfTwoNumbers22()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("10+12"), 22);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("10+12"), 22);
- }
- [Test]
- public void TestAdditionOfThreeNumbers6()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3"), 6);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("1+2+3"), 6);
- }
- [Test]
- public void TestAdditionOfFiveNumbers()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3+4+5"), 15);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("1+2+3+4+5"), 15);
- }
- [Test]
- public void TestProductOfTwoNumbers1()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("1*1"), 1);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("1*1"), 1);
- }
- [Test]
- public void TestProductOfTwoNumbers120()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("10*12"), 120);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("10*12"), 120);
- }
- [Test]
- public void TestProductOfThreeNumbers6()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3"), 6);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("1*2*3"), 6);
- }
- [Test]
- public void TestProductOfFiveNumbers()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3*4*5"), 120);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("1*2*3*4*5"), 120);
- }
- // ------------
- [Test]
- public void TestDifferenceOfTwoNumbers()
- {
- Assert.AreEqual(ArtihmeticParser.EvaluateSum("4-2"), 2);
- }
- // ------------
- [Test]
- public void TestSumAndProduct()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*1"), 6);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("2+4*1"), 6);
- }
- [Test]
- public void TestOrderOfOperations()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*6"), 26);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("2+4*6"), 26);
- }
- [Test]
- public void TestLongerInput()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("1000*3*2+100*2*2+2*2*2*2*5+9"), 6489);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("1000*3*2+100*2*2+2*2*2*2*5+9"), 6489);
- }
- [Test]
- [Ignore("Brackets")]
- public void TestBrackets()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("(2+4)*6"), 36);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("(2+4)*6"), 36);
- }
- [Test]
[Ignore("Whitespace")]- public void TestWhitespace()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate(" 2 + 4 *6 "), 26);- Assert.AreEqual(ArtihmeticParser.EvaluateSum(" 2 + 4 *6 "), 26);
- }
- [Test]
- [Ignore("Subtraction")]
- public void TestSubtraction()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("1+3-2+4"), 6);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("1+3-2+4"), 6);
- }
- [Test]
- [Ignore("Subtraction")]
- public void TestOrderOfOperationsWithSubtraction()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("10+15-2*2+10-3*4"), 19);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("10+15-2*2+10-3*4"), 19);
- }
- [Test]
- [Ignore("Subtraction and brackets")]
- public void TestBracketsAndSubtraction()
- {
Assert.AreEqual(ArtihmeticParser.Evaluate("100-2*(2+10-3)*4"), 28);- Assert.AreEqual(ArtihmeticParser.EvaluateSum("100-2*(2+10-3)*4"), 28);
- }
- }
public class Kata { public static boolean detectPalindrome(String userWord) { // Detect whether this string is a palindrome String backwardsWord = new StringBuilder(userWord).reverse().toString(); if (!userWord.equals(backwardsWord)) { return false; } return true; } }
- public class Kata {
- public static boolean detectPalindrome(String userWord) {
- // Detect whether this string is a palindrome
- String backwardsWord = new StringBuilder(userWord).reverse().toString();
- if (!userWord.equals(backwardsWord)) {
- return false;
- }
- return true;
- }
- }
public class Kata { public static boolean detectPalindrome(String userWord) { // Detect whether this string is a palindrome // turn userWord into array of characters // Create array that = string length char[] ch = new char[userWord.length()]; // Copy character by character into array for (int i = 0; i < ch.length; i++) { ch[i] = userWord.charAt(i); } // reverse the string and convert it to char[] array char[] chBackwards = new StringBuilder(userWord).reverse().toString().toCharArray(); // iterate through both indexes and see if (for ex.) index0 = index0 for(int i = 0; i < ch.length; i++) { if(ch[i] != chBackwards[i]) { return false; } } return true; } } // -------------------- // make ch array for og word // make ch array for og word backwards // iterate through both array lists index // if all indexs are = to eachother // return true
- public class Kata {
- public static boolean detectPalindrome(String userWord) {
- // Detect whether this string is a palindrome
- // turn userWord into array of characters
- // Create array that = string length
- char[] ch = new char[userWord.length()];
- // Copy character by character into array
- for (int i = 0; i < ch.length; i++) {
- ch[i] = userWord.charAt(i);
- }
- // reverse the string and convert it to char[] array
- char[] chBackwards = new StringBuilder(userWord).reverse().toString().toCharArray();
- // iterate through both indexes and see if (for ex.) index0 = index0
- for(int i = 0; i < ch.length; i++) {
- if(ch[i] != chBackwards[i]) {
- return false;
- }
- }
- return true;
- }
}- }
- // --------------------
- // make ch array for og word
- // make ch array for og word backwards
- // iterate through both array lists index
- // if all indexs are = to eachother
- // return true