#include <iostream> #include <string> std::string voice(){ std::string a = "value"; a = "another " + a; return a; }
- #include <iostream>
- #include <string>
int voice(){std::string a,b;std::cin>>a>>b;std::cout<<"another value";return 0;- std::string voice(){
- std::string a = "value";
- a = "another " + a;
- return a;
- }
// TODO: Replace examples and use TDD by writing your own tests Describe(any_group_name_you_want) { It(should_do_something) { Assert::That(voice(), Equals("another value")); } };
- // TODO: Replace examples and use TDD by writing your own tests
- Describe(any_group_name_you_want)
- {
- It(should_do_something)
- {
Assert::That("another value", Equals("another value"));- Assert::That(voice(), Equals("another value"));
- }
- };
Parsing
Algorithms
Logic
Strings
Data Types
Given an expression as a string, calculate its value. For example, given the string "1+2+3", return 6.
Order of operations should be respected: multiplication happens before addition.
Possible avenues for future exploration:
- Adding support for subtraction and division
- Stripping whitespace
- Adding support for brackets
using System;
using System.Linq;
public class ArtihmeticParser
{
public static int Evaluate(string input)
{
return input.Split("+").Select(EvaluateProduct).Aggregate(0,(a,b) => a+b);
}
private static int EvaluateProduct(string input)
{
return input.Split("*").Select(EvaluateInteger).Aggregate(1,(a,b) => a*b);
}
private static int EvaluateInteger(string input)
{
return int.Parse(input);
}
}
using NUnit.Framework;
using System;
[TestFixture]
public class SolutionTest
{
[Test]
public void TestOneNumber3()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("3"), 3);
}
[Test]
public void TestOneNumber10()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("10"), 10);
}
[Test]
public void TestOneNumber729()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("729"), 729);
}
[Test]
public void TestOneNumber0()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("0"), 0);
}
[Test]
public void TestAdditionOfTwoNumbers2()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("1+1"), 2);
}
[Test]
public void TestAdditionOfTwoNumbers22()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("10+12"), 22);
}
[Test]
public void TestAdditionOfThreeNumbers6()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3"), 6);
}
[Test]
public void TestAdditionOfFiveNumbers()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3+4+5"), 15);
}
[Test]
public void TestProductOfTwoNumbers1()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("1*1"), 1);
}
[Test]
public void TestProductOfTwoNumbers120()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("10*12"), 120);
}
[Test]
public void TestProductOfThreeNumbers6()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3"), 6);
}
[Test]
public void TestProductOfFiveNumbers()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3*4*5"), 120);
}
[Test]
public void TestSumAndProduct()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*1"), 6);
}
[Test]
public void TestOrderOfOperations()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("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);
}
[Test]
[Ignore("Brackets")]
public void TestBrackets()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("(2+4)*6"), 36);
}
[Test]
[Ignore("Whitespace")]
public void TestWhitespace()
{
Assert.AreEqual(ArtihmeticParser.Evaluate(" 2 + 4 *6 "), 26);
}
[Test]
[Ignore("Subtraction")]
public void TestSubtraction()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("1+3-2+4"), 6);
}
[Test]
[Ignore("Subtraction")]
public void TestOrderOfOperationsWithSubtraction()
{
Assert.AreEqual(ArtihmeticParser.Evaluate("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);
}
}
Fundamentals
Numbers
Data Types
Integers
Nagative numbers can be odd too.
using System; public static class Kata { public static bool IsOdd(int input) => Math.Abs(input)%2==1; }
- using System;
- public static class Kata
- {
public static bool IsOdd(int input) => input%2==1;- public static bool IsOdd(int input) => Math.Abs(input)%2==1;
- }
namespace Solution { using NUnit.Framework; using System; // TODO: Replace examples and use TDD by writing your own tests [TestFixture] public class SolutionTest { [Test] public void MyTest() { Assert.AreEqual(true, Kata.IsOdd(1)); Assert.AreEqual(false, Kata.IsOdd(2)); Assert.AreEqual(true, Kata.IsOdd(13)); Assert.AreEqual(false, Kata.IsOdd(18284)); Assert.AreEqual(true, Kata.IsOdd(-5)); Assert.AreEqual(false, Kata.IsOdd(-6)); } } }
- namespace Solution {
- using NUnit.Framework;
- using System;
- // TODO: Replace examples and use TDD by writing your own tests
- [TestFixture]
- public class SolutionTest
- {
- [Test]
- public void MyTest()
- {
- Assert.AreEqual(true, Kata.IsOdd(1));
- Assert.AreEqual(false, Kata.IsOdd(2));
- Assert.AreEqual(true, Kata.IsOdd(13));
- Assert.AreEqual(false, Kata.IsOdd(18284));
- Assert.AreEqual(true, Kata.IsOdd(-5));
- Assert.AreEqual(false, Kata.IsOdd(-6));
- }
- }
- }