Ad
Code
Diff
  • #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;
    • }
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);
  }
}
Fundamentals
Numbers
Data Types
Integers

Nagative numbers can be odd too.

Code
Diff
  • 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;
    • }