Ad
Code
Diff
  • 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;
Code
Diff
  • 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 index
    • return '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;
    • }
Code
Diff
  • 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";
    • }
    • }
Code
Diff
  • 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;
    • }
Code
Diff
  • 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;
    • }
    • }
Code
Diff
  • 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;
    • }
    • }