using System.Collections.Generic; using System.Diagnostics; public class TowerOfHanoi { public static int Tower(int numOfDisks) { //only numOfDisks is given, fill in the other gaps, call the other method that moves disks, and return the return statement from the other method return Tower(numOfDisks, "source", "aux", "dest", new Dictionary<int, int>()); } private static int Tower(int n, string source, string aux, string dest, Dictionary<int, int> cache) { //dictionary has keys and values. if (n > 0) // if one or more disks { if (cache.ContainsKey(n)) //check if dictionary contains an element with the specified key { return cache[n]; } //variable declared at method scope //outcome will be the number of moves in test var outcome = Tower(n - 1, source, aux, dest, cache); //move disk from rod to rod using recursion outcome += Tower(n - 1, aux, dest, source, cache); outcome += 1; cache.Add(n, outcome); return outcome; } else { //if no disks given, no moves possible return 0; } } } //mathematical way to calculate moves //using System; //static int moves = 0; //method body // return moves = (int) Math.Pow(2,numOfDisks)-1;
- using System.Collections.Generic;
- using System.Diagnostics;
- public class TowerOfHanoi
- {
- public static int Tower(int numOfDisks)
- {
return 0;- //only numOfDisks is given, fill in the other gaps, call the other method that moves disks, and return the return statement from the other method
- return Tower(numOfDisks, "source", "aux", "dest", new Dictionary<int, int>());
- }
- private static int Tower(int n, string source, string aux, string dest, Dictionary<int, int> cache)
- {
return 0;- //dictionary has keys and values.
- if (n > 0) // if one or more disks
- {
- if (cache.ContainsKey(n)) //check if dictionary contains an element with the specified key
- {
- return cache[n];
- }
- //variable declared at method scope
- //outcome will be the number of moves in test
- var outcome = Tower(n - 1, source, aux, dest, cache); //move disk from rod to rod using recursion
- outcome += Tower(n - 1, aux, dest, source, cache);
- outcome += 1;
- cache.Add(n, outcome);
- return outcome;
- }
- else { //if no disks given, no moves possible
- return 0;
- }
- }
- }
}- //mathematical way to calculate moves
- //using System;
- //static int moves = 0;
- //method body
- // return moves = (int) Math.Pow(2,numOfDisks)-1;
using NUnit.Framework; using System; [TestFixture] public class TowerOfHanoi_Tests { [Test] public void GreaterThanOne_Test() { var 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); } [Test] public void One_Test() { var moves = TowerOfHanoi.Tower(1); Assert.AreEqual(1, moves); } [Test] public void Zero_Test() { var moves = TowerOfHanoi.Tower(0); Assert.AreEqual(0, moves); } }
- using NUnit.Framework;
- using System;
- [TestFixture]
- public class TowerOfHanoi_Tests
- {
- [Test]
public void Hanoi_Test()- public void GreaterThanOne_Test()
- {
var moves = TowerOfHanoi.Tower(1);Assert.AreEqual(1, moves);moves = TowerOfHanoi.Tower(2);- var 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);
- }
}- [Test]
- public void One_Test()
- {
- var moves = TowerOfHanoi.Tower(1);
- Assert.AreEqual(1, moves);
- }
- [Test]
- public void Zero_Test()
- {
- var moves = TowerOfHanoi.Tower(0);
- Assert.AreEqual(0, moves);
- }
- }
function removeEveryThird(str) { const stringArray = str.split(" "); var wordArray = []; for (var element of stringArray) { word = element.slice(0, 3) + element.slice(4); wordArray.push(word); } let words = wordArray.join(" "); return words; }
- function removeEveryThird(str) {
// Remove the third character from every work in the string// using a zero based indexreturn 'heloword';- const stringArray = str.split(" ");
- var wordArray = [];
- for (var element of stringArray) {
- word = element.slice(0, 3) + element.slice(4);
- wordArray.push(word);
- }
- let words = wordArray.join(" ");
- return words;
- }
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("This test passes", function() { assert.strictEqual(removeEveryThird('hello world'), 'helo word'); }); it("This test fails (fix code - not test)", function() { assert.strictEqual(removeEveryThird('how you doing'), 'how you doig'); }); 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'); }); });
- const chai = require("chai");
- const assert = chai.assert;
- describe("Solution", function() {
- it("This test passes", function() {
assert.strictEqual(removeEveryThird('hello world'), 'heloword');- assert.strictEqual(removeEveryThird('hello world'), 'helo word');
- });
- it("This test fails (fix code - not test)", function() {
assert.strictEqual(removeEveryThird('how you doing'), 'howyoudoin');- assert.strictEqual(removeEveryThird('how you doing'), 'how you doig');
- });
- 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) { if (number % 3 == 0 && number % 5 == 0) { return "FizzBuzz"; } else if (number % 3 == 0) { return "Fizz"; } else if (number % 5 == 0) { return "Buzz"; } else { return number.ToString(); } // 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. } }
- public class FizzBuzz
- {
- public string GetOutput(int number) {
- if (number % 3 == 0 && number % 5 == 0) {
- return "FizzBuzz";
- }
- else if (number % 3 == 0) {
- return "Fizz";
- }
- else if (number % 5 == 0) {
- return "Buzz";
- }
- else {
- return number.ToString();
- }
- // 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() { var fizzbuzz = new FizzBuzz(); var result = fizzbuzz.GetOutput(0); Assert.That(result, Is.EqualTo("FizzBuzz")); result = fizzbuzz.GetOutput(17); Assert.That(result, Is.EqualTo("17")); result = fizzbuzz.GetOutput(50); Assert.That(result, Is.EqualTo("Buzz")); result = fizzbuzz.GetOutput(73); Assert.That(result, Is.EqualTo("73")); result = fizzbuzz.GetOutput(99); Assert.That(result, Is.EqualTo("Fizz")); result = fizzbuzz.GetOutput(100); Assert.That(result, Is.EqualTo("Buzz")); // 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")); } } }
- namespace Solution {
- using NUnit.Framework;
- [TestFixture]
- public class SolutionTest
- {
- [Test]
- public void GetOutput_InputIsAll()
- {
- var fizzbuzz = new FizzBuzz();
- var result = fizzbuzz.GetOutput(0);
- Assert.That(result, Is.EqualTo("FizzBuzz"));
- result = fizzbuzz.GetOutput(17);
- Assert.That(result, Is.EqualTo("17"));
- result = fizzbuzz.GetOutput(50);
- Assert.That(result, Is.EqualTo("Buzz"));
- result = fizzbuzz.GetOutput(73);
- Assert.That(result, Is.EqualTo("73"));
- result = fizzbuzz.GetOutput(99);
- Assert.That(result, Is.EqualTo("Fizz"));
- result = fizzbuzz.GetOutput(100);
- Assert.That(result, Is.EqualTo("Buzz"));
- // 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"));
- }
- }
- }
const parse = (value = '') => { // if valid email, return true, else return false let validEmail; const validCharacters = ["a","A", "b","B","c","C","d","D","e","E","f","F", "g","G","h","H","i","I","j","J","k","K","l","L", "m","M","n","N","o","O","p","P","q","Q","r","R", "s","S","t","T","u","U","v","V","w","W","x","X", "y","Y","z","Z","0","1","2","3","4","5","6","7", "8","9","@","."] const emailArray = value.split(""); for (let i = 0; i < emailArray.length; i++) { if (validCharacters.includes(value[i]) == true) { validEmail = true; } else { validEmail = false; break; } } return validEmail; }
- const parse = (value = '') => {
- // if valid email, return true, else return false
return true;- let validEmail;
- const validCharacters = ["a","A", "b","B","c","C","d","D","e","E","f","F",
- "g","G","h","H","i","I","j","J","k","K","l","L",
- "m","M","n","N","o","O","p","P","q","Q","r","R",
- "s","S","t","T","u","U","v","V","w","W","x","X",
- "y","Y","z","Z","0","1","2","3","4","5","6","7",
- "8","9","@","."]
- const emailArray = value.split("");
- for (let i = 0; i < emailArray.length; i++) {
- if (validCharacters.includes(value[i]) == true) {
- validEmail = true;
- }
- else {
- validEmail = false;
- break;
- }
- }
- return validEmail;
- }
import java.util.*; import java.util.LinkedList; public class Kata { public int index = -1; public LinkedList<String> linkedList; public Kata() { linkedList = new LinkedList<String>(); } public LinkedList<String> AddItem(String itemToAdd, String whereToAddIt) { // When given a string, whereToAddIt, find that string in the LinkedList for (int i = 0; i < linkedList.size(); i++) { // Extracting each element in the Linked List String element = linkedList.get(i); // Checking if the extracted element is equal to // the element to be searched if (element == whereToAddIt) { // Assigning the index of the element to a variable index = i; break; } } // add itemToAdd directly after whereToAddIt in the LinkedList int position = index + 1; linkedList.add(position, itemToAdd); return linkedList; } public LinkedList<String> RemoveItem(String whereToRemoveIt) { // When given a string, whereToRemoveIt, find that string // in the LinkedList and remove it from the linked list for (int i = 0; i < linkedList.size(); i++) { // Extracting each element in the Linked List String element = linkedList.get(i); // Checking if the extracted element is equal to // the element to be searched if (element == whereToRemoveIt) { // Assigning the index of the element to a variable index = i; linkedList.remove(index); break; } } return linkedList; } public LinkedList<String> MergeLists(LinkedList<String> listToMerge) { // Merge this linked list with the previous linkedList.addAll(listToMerge); return linkedList; } public void setList(LinkedList<String> newList) { linkedList = newList; } }
- import java.util.*;
- import java.util.LinkedList;
- public class Kata {
- public int index = -1;
- public LinkedList<String> linkedList;
- public Kata()
- {
- linkedList = new LinkedList<String>();
- }
- public LinkedList<String> AddItem(String itemToAdd, String whereToAddIt) {
- // When given a string, whereToAddIt, find that string in the LinkedList
- for (int i = 0; i < linkedList.size(); i++) {
private LinkedList<String> linkedList;- // Extracting each element in the Linked List
- String element = linkedList.get(i);
public static void AddItem(String itemToAdd, String whereToAddIt) {// When given a string, whereToAddIt, find that string// in the LinkedList and add itemToAdd directly after whereToAddIt// in the LinkedList- // Checking if the extracted element is equal to
- // the element to be searched
- if (element == whereToAddIt) {
- // Assigning the index of the element to a variable
- index = i;
- break;
- }
- }
- // add itemToAdd directly after whereToAddIt in the LinkedList
- int position = index + 1;
- linkedList.add(position, itemToAdd);
- return linkedList;
- }
public static void RemoveItem(String whereToRemoveIt) {- public LinkedList<String> RemoveItem(String whereToRemoveIt) {
- // When given a string, whereToRemoveIt, find that string
- // in the LinkedList and remove it from the linked list
- for (int i = 0; i < linkedList.size(); i++) {
- // Extracting each element in the Linked List
- String element = linkedList.get(i);
- // Checking if the extracted element is equal to
- // the element to be searched
- if (element == whereToRemoveIt) {
- // Assigning the index of the element to a variable
- index = i;
- linkedList.remove(index);
- break;
- }
- }
- return linkedList;
- }
public static void MergeLists(LinkedList<String> listToMerge) {- public LinkedList<String> MergeLists(LinkedList<String> listToMerge) {
- // Merge this linked list with the previous
- linkedList.addAll(listToMerge);
- return linkedList;
- }
- public void setList(LinkedList<String> newList) {
- linkedList = newList;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import java.util.*; import java.util.LinkedList; // TODO: Write valid tests to prove your code class SolutionTest { private Kata systemUnderTest; @BeforeEach void createList() { this.systemUnderTest = new Kata(); this.systemUnderTest.setList(new LinkedList<String>(Arrays.asList("dog","bird","cat"))); } @Test void testAddItem() { assertEquals(Arrays.asList("dog", "hamster", "bird", "cat"),this.systemUnderTest.AddItem("hamster", "dog")); } @Test void testRemoveItem() { assertEquals(Arrays.asList("dog", "bird"),this.systemUnderTest.RemoveItem("cat")); } @Test void testMergeLists() { assertEquals(Arrays.asList("dog", "bird", "cat", "penguin","fish","bear"),this.systemUnderTest.MergeLists(new LinkedList<String>(Arrays.asList("penguin","fish","bear")))); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import org.junit.jupiter.api.BeforeEach;
- import java.util.*;
- import java.util.LinkedList;
- // TODO: Write valid tests to prove your code
- class SolutionTest {
- private Kata systemUnderTest;
- @BeforeEach
- void createList() {
- this.systemUnderTest = new Kata();
- this.systemUnderTest.setList(new LinkedList<String>(Arrays.asList("dog","bird","cat")));
- }
- @Test
- void testAddItem() {
- assertEquals(Arrays.asList("dog", "hamster", "bird", "cat"),this.systemUnderTest.AddItem("hamster", "dog"));
- }
- @Test
- void testRemoveItem() {
- assertEquals(Arrays.asList("dog", "bird"),this.systemUnderTest.RemoveItem("cat"));
- }
- @Test
void testSomething() {// assertEquals("expected", "actual");- void testMergeLists() {
- assertEquals(Arrays.asList("dog", "bird", "cat", "penguin","fish","bear"),this.systemUnderTest.MergeLists(new LinkedList<String>(Arrays.asList("penguin","fish","bear"))));
- }
- }
import java.util.Arrays; public class Kata { public static int[] sortValues(int[] my_array, int size) { // Given an array of size N containing only 0s, 1s, and 2s; // sort the array in ascending order. Arrays.sort(my_array); return my_array; } }
- import java.util.Arrays;
- public class Kata {
- public static int[] sortValues(int[] my_array, int size) {
- // Given an array of size N containing only 0s, 1s, and 2s;
- // sort the array in ascending order.
int[] returnArray = {0,0,0,0,1,1,2,2};return returnArray;- Arrays.sort(my_array);
- return my_array;
- }
- }