using System; using System.Linq; public class ArtihmeticParser { //use a Stack for public static int Evaluate(string input) { // add function to remove white spaces pretty sure this doesn't remove whitespace string trimmed = String.Concat(input.Where(c => !Char.IsWhiteSpace(c))); return input.Split("+").Select(EvaluateProduct).Aggregate(0,(a,b) => a+b); } private static int EvaluateProduct(string input) { string trimmed = String.Concat(input.Where(c => !Char.IsWhiteSpace(c))); return input.Split("*").Select(EvaluateInteger).Aggregate(1,(a,b) => a*b); } //should be able to subtract and divide public static int EvaluateSubtraction(string input) { return input.Split("-").Select(EvaluateInteger).Aggregate(0,(a,b) => a-b); } private static int EvaluateDivision(string input) { return input.Split("/").Select(EvaluateInteger).Aggregate(1,(a,b) => a/b); } private static int EvaluateInteger(string input) { return int.Parse(input); } //code for whitepsace that doesn't work smh //public static string TrimWhiteSpace(string whiteSpace) //{ // return string.Trim(whiteSpace); //} }
- using System;
- using System.Linq;
- public class ArtihmeticParser
- {
- //use a Stack for
- public static int Evaluate(string input)
{- { // add function to remove white spaces pretty sure this doesn't remove whitespace
- string trimmed = String.Concat(input.Where(c => !Char.IsWhiteSpace(c)));
- return input.Split("+").Select(EvaluateProduct).Aggregate(0,(a,b) => a+b);
- }
- private static int EvaluateProduct(string input)
- {
- string trimmed = String.Concat(input.Where(c => !Char.IsWhiteSpace(c)));
- return input.Split("*").Select(EvaluateInteger).Aggregate(1,(a,b) => a*b);
- }
- //should be able to subtract and divide
- public static int EvaluateSubtraction(string input)
- {
- return input.Split("-").Select(EvaluateInteger).Aggregate(0,(a,b) => a-b);
- }
- private static int EvaluateDivision(string input)
- {
- return input.Split("/").Select(EvaluateInteger).Aggregate(1,(a,b) => a/b);
- }
- private static int EvaluateInteger(string input)
- {
- return int.Parse(input);
- }
}- //code for whitepsace that doesn't work smh
- //public static string TrimWhiteSpace(string whiteSpace)
- //{
- // return string.Trim(whiteSpace);
- //}
- }
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] public void TestWhitespace() { Assert.AreEqual(ArtihmeticParser.Evaluate(" 2 + 4 *6 "), 26); } [Test] public void TestSubtraction() { Assert.AreEqual(ArtihmeticParser.Evaluate("1+3-2+4"), 6); } [Test] [Ignore("OrderOfOperationsWithSubtraction")] 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); } }
- 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")]- [Ignore("OrderOfOperationsWithSubtraction")]
- 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);
- }
- }
public class Kata { public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) { //Implement the urinal problem, described as "given x number of people walking //into a bathroom that contains y number of urinals, describe the optimate //placement of people so that none is standing next to the other" //In this case, the urinals are represented by a boolean array with //true representing a person at a urinal, and false meaning the urinal is empty int y = 0 if ( y == 3 ) { idk how to solve this } return new boolean[] {true, false, true}; } }
- public class Kata {
public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) {// Implement the urinal problem, described as "given x number of people walking// into a bathroom that contains y number of urinals, describe the optimate// placement of people so that none is standing next to the other"// In this case, the urinals are represented by a boolean array with// true representing a person at a urinal, and false meaning the urinal is empty- public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) {
- //Implement the urinal problem, described as "given x number of people walking
- //into a bathroom that contains y number of urinals, describe the optimate
- //placement of people so that none is standing next to the other"
- //In this case, the urinals are represented by a boolean array with
- //true representing a person at a urinal, and false meaning the urinal is empty
- int y = 0
- if ( y == 3 ) {
- idk how to solve this
- }
- return new boolean[] {true, false, true};
- }
- }
Pointers at the beginning and end of our index list (i = 0, j = end of index)
While i is less than j
if there is a mismatch return false !=
if not add i and take away -1 from j until i is equal to or larger than j
public class Kata { public static boolean detectPalindrome(String userWord){ int i = 0, j = userWord.length() - 1; while (i < j) { if (userWord.charAt(i) != userWord.charAt(j)) return false; i++; j--; } return true; } }
public class Kata {public static boolean detectPalindrome(String userWord) {// Detect whether this string is a palindromereturn true;}}- public class Kata {
- public static boolean detectPalindrome(String userWord){
- int i = 0, j = userWord.length() - 1;
- while (i < j) {
- if (userWord.charAt(i) != userWord.charAt(j))
- return false;
- i++;
- j--;
- }
- return true;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { @Test void testSomething() { assertEquals(Kata.detectPalindrome("nursesrun"), true); } @Test void testSomethingElse() { assertEquals(Kata.detectPalindrome("turtle"), false); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests- class SolutionTest {
- @Test
- void testSomething() {
assertEquals(Kata.detectPalindrome("madam"), true);- assertEquals(Kata.detectPalindrome("nursesrun"), true);
- }
- @Test
- void testSomethingElse() {
assertEquals(Kata.detectPalindrome("batman"), false);- assertEquals(Kata.detectPalindrome("turtle"), false);
- }
- }
public class Kata { public static int findIndex (int[] my_array, int t) { for(int i=0; i< my_array.length; i++){ if(my_array[i]==t){ return i; } } return 4; } }
- public class Kata {
- public static int findIndex (int[] my_array, int t) {
// Find the index of treturn 4;- for(int i=0; i< my_array.length; i++){
- if(my_array[i]==t){
- return i;
- }
- }
- return 4;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { @Test void testSomething() { int[] randomArray = {1, 2, 5, 7,10 }; assertEquals(4, Kata.findIndex(randomArray, 10)); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests- class SolutionTest {
- @Test
- void testSomething() {
int[] randomArray = {1, 2, 5, 7};assertEquals(2, Kata.findIndex(randomArray, 5));- int[] randomArray = {1, 2, 5, 7,10 };
- assertEquals(4, Kata.findIndex(randomArray, 10));
- }
- }