The Tower of Hanoi is a mathematical puzzle. It consists of three poles and a number of disks of different sizes which can slide onto any poles. The puzzle starts with the disk in a neat stack in ascending order of size in one pole, the smallest at the top thus making a conical shape. The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to another pole (say ‘destination pole’) with the help of the third pole (say auxiliary pole).
The puzzle has the following two rules:
1. You can’t place a larger disk onto a smaller disk
2. Only one disk can be moved at a time
using System.Collections.Generic;
using System.Diagnostics;
public class TowerOfHanoi
{
public static int Tower(int numOfDisks)
{
return 0;
}
private static int Tower(int n, string source, string aux, string dest, Dictionary<int, int> cache)
{
return 0;
}
}
using NUnit.Framework;
using System;
[TestFixture]
public class TowerOfHanoi_Tests
{
[Test]
public void Hanoi_Test()
{
var moves = TowerOfHanoi.Tower(1);
Assert.AreEqual(1, moves);
moves = TowerOfHanoi.Tower(2);
Assert.AreEqual(3, moves);
moves = TowerOfHanoi.Tower(3);
Assert.AreEqual(7, moves);
moves = TowerOfHanoi.Tower(4);
Assert.AreEqual(15, moves);
moves = TowerOfHanoi.Tower(5);
Assert.AreEqual(31, moves);
moves = TowerOfHanoi.Tower(6);
Assert.AreEqual(63, moves);
moves = TowerOfHanoi.Tower(7);
Assert.AreEqual(127, moves);
}
}
// Write a project that demonstrates inheritance in C#
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("expected", "actual");
}
}
}
Description:
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);
}
}
Description:
Your task is to remove every third char in a string,
using a zero based index
Example:
removeEverySecond('hello world'); // 'heloword'
removeEverySecond('how you doing') // 'howyoudoin'
function removeEveryThird(str) {
// Remove the third character from every work in the string
// using a zero based index
return 'heloword';
}
const chai = require("chai");
const assert = chai.assert;
describe("Solution", function() {
it("This test passes", function() {
assert.strictEqual(removeEveryThird('hello world'), 'heloword');
});
it("This test fails (fix code - not test)", function() {
assert.strictEqual(removeEveryThird('how you doing'), 'howyoudoin');
});
it("This test fails (fix code - not test)", function() {
assert.strictEqual(removeEveryThird('abc'), 'abc');
});
it("This test fails (fix code - not test)", function() {
assert.strictEqual(removeEveryThird('1234 56789 10112314'), '123 5679 1012314');
});
});
public class FizzBuzz
{
public string GetOutput(int number) {
// Fizz buzz is a popular computer science interview question.
// The function above is given a number - if the number is
// divisible by 3, return "fizz", if it's divisible by 5,
// return "buzz", if not divisble by 3 or 5 - return the
// number itself.
return "0";
}
}
namespace Solution {
using NUnit.Framework;
[TestFixture]
public class SolutionTest
{
[Test]
public void GetOutput_InputIsAll()
{
// write your own test to test all numbers from 0 to 100
}
[Test]
public void GetOutput_InputIs4_Return4()
{
var fizzbuzz = new FizzBuzz();
var result = fizzbuzz.GetOutput(4);
Assert.That(result, Is.EqualTo("4"));
}
[Test]
public void GetOutput_InputIsDivisibleBy3_ReturnFizz()
{
var fizzbuzz = new FizzBuzz();
var result = fizzbuzz.GetOutput(3);
Assert.That(result, Is.EqualTo("Fizz"));
}
[Test]
public void GetOutput_InputIsDivisibleBy5_ReturnBuzz()
{
var fizzbuzz = new FizzBuzz();
var result = fizzbuzz.GetOutput(5);
Assert.That(result, Is.EqualTo("Buzz"));
}
[Test]
public void GetOutput_InputIsDivisibleBy5And3_ReturnFizzBuzz()
{
var fizzbuzz = new FizzBuzz();
var result = fizzbuzz.GetOutput(15);
Assert.That(result, Is.EqualTo("FizzBuzz"));
}
}
}
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Write more tests to prove your code bleow class SolutionTest { @Test void testSomething() { assertEquals(Kata.findMax(new int[]{4, 5, 6, 7}), 7); } @Test void testSomethingElse() { assertEquals(Kata.findMax(new int[]{4, 5, 6, 7, 12}), 12); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Write more tests to prove your code bleow
- class SolutionTest {
- @Test
- void testSomething() {
- assertEquals(Kata.findMax(new int[]{4, 5, 6, 7}), 7);
- }
- @Test
- void testSomethingElse() {
- assertEquals(Kata.findMax(new int[]{4, 5, 6, 7, 12}), 12);
- }
- }
function find_unique_items(array1, array2)
{
//Find the unique elements from two arrays
return [1,2,4,6,8]
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("this test passes", function() {
// Test.assertEquals(1 + 1, 2);
assert.deepEqual(find_unique_items([1,2,3,5,7], [4,5,3,6,8,7]), [1,2,4,6,8]);
});
it("this test fails", function() {
// Test.assertEquals(1 + 1, 2);
assert.deepEqual(find_unique_items([1,3,4,5,78,233], [1,3,5,6,7,123,233]), [1,3,4,6,7,8]);
});
});
const parse = (value = '') => {
// if valid email, return true, else return false
return true;
}
describe("Solution", function() {
it("Debe pasar con Yjimenezar001@gmail.com", function() {
const email1 = "Yjimenezar001@gmail.com";
Test.assertEquals(true, parse(email1));
});
it("Debe fallar con Yjimenezar001@gmail.com with white space", function() {
const email1 = "Yjimenezar001@gmail.com ";
Test.assertEquals(false, parse(email1));
});
});
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
return new boolean[] {true, false, true};
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void testSomething() {
assertArrayEquals(Kata.populateBathroom(new boolean[3], 2), new boolean[] {true, false, true});
}
}
function prime_checker (n) {
// Write a javascript function to check if N is a prime number
}
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold=0;
describe("Example", function(){
it("test case", function(){
assert.strictEqual(prime_checker(653), true);
assert.strictEqual(prime_checker(654), false);
assert.strictEqual(prime_checker(5), true);
assert.strictEqual(prime_checker(777), false);
assert.strictEqual(prime_checker(977), true);
assert.strictEqual(prime_checker(125), false);
assert.strictEqual(prime_checker(997), true);
assert.strictEqual(prime_checker(709), true);
assert.strictEqual(prime_checker(15), false);
assert.strictEqual(prime_checker(1), false);
assert.strictEqual(prime_checker(0), false);
});
});
// Detect whether this string is a palindrome
public class Kata {
public static boolean detectPalindrome(String userWord) {
// Detect whether this string is a palindrome
return true;
}
}
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);
}
@Test
void testSomethingElse() {
assertEquals(Kata.detectPalindrome("batman"), false);
}
}
Find the max number of times an integer occurs in an array.
import java.util.Arrays;
class MaxOccurence {
public static int findMax(int[] nums) {
return 5;
}
}
import java.util.*;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
public class MaxOccurenceTest {
@Test
public void test1() {
int[] nums = {1, 2, 9, 3, 4, 3, 3, 1, 2, 4, 5, 3, 8, 3, 9, 0, 3, 2};
assertEquals(3, MaxOccurence.findMax(nums));
}
@Ignore
@Test
public void test2() {
int[] nums = {5, 4, 6 ,7, 9, 8, 2, 4, 3, 6, 5, 8, 6, 1, 2, 5, 3, 4, 7, 9, 0, 8, 5, 7, 3, 4, 6, 1, 3};
// there is no instruction what of the numbers should be printed first if there are multiple max numbers in the row
assertEquals(5, MaxOccurence.findMax(nums));
}
@Test
public void test3() {
int[] nums = {8};
assertEquals(8, MaxOccurence.findMax(nums));
}
@Test
public void test4() {
int[] nums = {};
assertEquals(-1, MaxOccurence.findMax(nums));
}
@Test
public void test5() {
int[] nums = {2, 2, 3, 3, 4, 4, 5, 5, 6, 6};
assertEquals(2, MaxOccurence.findMax(nums));
}
@Test
public void test6() {
int[] nums = {2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
assertEquals(5, MaxOccurence.findMax(nums));
}
}
// Change min and max below to be the highest and lowest values
// in the nums array provided to the function
interface HighLow {
static int[] findLargestAndSmallest(int[] nums) {
// Change min and max below to be the highest and lowest values
// in the nums array provided to the function
int min = 0;
int max = 100;
return new int[]{min, max};
}
}
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runners.JUnit4;
public class SolutionTest {
int[] input = new int[]{-1,-2,-3,-4,-5,-6,-7,-8,-9,-1};
int[] output = new int[]{-9,-1};
@Test
public void allNegatives() {
assertArrayEquals(output, HighLow.findLargestAndSmallest(input));
}
int[] input2 = new int[]{1,-2,3,-4,-5,-6,-7,-8,-9};
int[] output2 = new int[]{-9,3};
@Test
public void test2() {
assertArrayEquals(output2, HighLow.findLargestAndSmallest(input2));
}
int[] input123 = new int[]{1,2,3};
int[] output123 = new int[]{1,3};
@Test
public void test21() {
assertArrayEquals(output123, HighLow.findLargestAndSmallest(input123));
}
int[] input3 = new int[]{4};
int[] output3 = new int[]{4,4};
@Test
public void oneItem() {
assertArrayEquals(output3, HighLow.findLargestAndSmallest(input3));
}
int[] input4 = new int[]{9,-1};
int[] output4 = new int[]{-1,9};
@Test
public void twoItems() {
assertArrayEquals(output4, HighLow.findLargestAndSmallest(input4));
}
int[] input5 = new int[]{};
int[] output5 = null;
@Test
public void emptyArray() {
assertArrayEquals(output5, HighLow.findLargestAndSmallest(input5));
}
int[] input6 = null;
int[] output6 = null;
@Test
public void nullInput() {
assertArrayEquals(output6, HighLow.findLargestAndSmallest(input6));
}
}
import static java.util.stream.DoubleStream.of;
import static org.apache.commons.lang3.ArrayUtils.reverse;
interface DeleteDuplicates {
static double[] deleteDups(double[] a) {
// Given an array, eliminate duplicate values and return the array
return a;
}
}
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
class DeleteDuplicatesTest {
@Test
void sample() {
assertArrayEquals(new double[]{1, 4, 2, 3}, DeleteDuplicates.deleteDups(new double[]{2, 1, 4, 2, 3}), .1);
assertArrayEquals(new double[]{-1}, DeleteDuplicates.deleteDups(new double[]{2, 3, -1, 5}), .1);
assertArrayEquals(new double[]{1.234, 4.234, 2.2131, 3.0, 12.321, 2.2131141516},DeleteDuplicates.deleteDups(new double[]{2.2131141516, 1.234, 4.234, 2.2131, 3, 3, 12.321, 2.2131141516}), .1);
}
}
public class Kata {
public static int findMax(int[] my_array) {
// Write a method that returns the largest integer in the list.
// You can assume that the list has at least one element.
return 7;
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Write more tests to prove your code bleow
class SolutionTest {
@Test
void testSomething() {
assertEquals(Kata.findMax(new int[]{4, 5, 6, 7}), 7);
}
}