#88 Which are in?


Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.


#Example 1: a1 = ["arp", "live", "strong"]


a2 = ["lively", "alive", "harp", "sharp", "armstrong"]


returns ["arp", "live", "strong"]


#Example 2: a1 = ["tarp", "mice", "bull"]


a2 = ["lively", "alive", "harp", "sharp", "armstrong"]


returns []


Notes:

Arrays are written in "general" notation. See "Your Test Cases" for examples in your language.


In Shell bash a1 and a2 are strings. The return is a string where words are separated by commas.


Beware: r must be without duplicates.

Don't mutate the inputs.


#87 Extract the IDs from the data set 



Complete the method so that it returns an array of all ID's passed in. The data structure will be similar to the following:


var data = {
  id: 1,
  items: [
    {id: 2},
    {id: 3, items: [
      {id: 4},
      {id: 5}
    ]}
  ]
}



extractIds(data) // should return [1,2,3,4,5]

The method should be able to handle the case of empty data being passed in.


Note: The only arrays that need to be traversed are those assigned to the "items" property.


 #85 Circularly Sorted Array


Write a method, isCircleSorted(int[] A) (Java, JavaScript), or Array#circularly_sorted? (Ruby) that determines if A is circularly sorted. An Array is circularly sorted if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps.


For Example:


// True:

isCircleSorted([2,3,4,5,0,1]);

isCircleSorted([4,5,6,9,1]);

isCircleSorted([10,11,6,7,9]);

isCircleSorted([1,2,3,4,5]);

isCircleSorted([5,7,43,987,-9,0]);



// False:

isCircleSorted([4,1,2,5]);

isCircleSorted([8,7,6,5,4,3]);

isCircleSorted([6,7,4,8]);

isCircleSorted([7,6,5,4,3,2,1]);



#81 Format words into a sentence


Complete the method so that it formats the words into a single comma separated value. The last word should be separated by the word 'and' instead of a comma. The method takes in an array of strings and returns a single formatted string. Empty string values should be ignored. Empty arrays or null/nil values being passed into the method should result in an empty string being returned.


Kata.formatWords(new String[] {"ninja", "samurai", "ronin"}) => "ninja, samurai and ronin"

Kata.formatWords(new String[] {"ninja", "", "ronin"}) => "ninja and ronin"

Kata.formatWords(new String[] {}) => ""



import java.util.Arrays;
import java.util.stream.Collectors;

public class Kata {

	 public static String replaceLast(String str, String regex, String replacement) {
	        int regexIndexOf = str.lastIndexOf(regex);
	        if(regexIndexOf == -1){
	            return str;
	        }else{
	            return str.substring(0, regexIndexOf) + replacement + str.substring(regexIndexOf + regex.length());
	        }
	}

  public static String formatWords(String[] words) {
    // Do the things...
    if(words == null) return "";
    return replaceLast(Arrays.stream(words).filter(word -> !word.equals("")).collect(Collectors.joining(", ")),", "," and ");
  }

}

Task

You are working for NoVirus Security Solutions and they ask you to make a scanner that scans a file inputted by the user with the function scanFile(File,VirusDB) that takes a File and a VirusDB object and return whether a file is safe or not. Remember: the searches need to be non-Case-Sensitive


Your class also has the function setScanIntensity(int) which changes the scan intensity. This will only receive values 0, 1, 2 or 3. This has been done for you.


The scan intensity determines the arrays from the database that will be used. i.e.:


scanIntensity 0 means off(every file is considered safe)

scanIntensity 1 means that only the array intensity1Signatures will be used

scanIntensity 2 means that the arrays intensity1Signatures and intensity2Signatures will be used

scanIntensity 3 means that all 3 arrays will be used


Outputs

The outputs should be: "Filename is safe" or "Filename is not safe" (Filename is the name of the file that you can get with file.getName() )


File Class


class File{
  private String name;
  private String data;

  public File(String name,String data){
    this.name = name;
    this.data = data;
  }

  //used in output
  public String getName(){
    return this.name;
  }

  //the String that you need to scan.
  public String getData(){
    return this.data;
  }
}


VirusDB Class


class VirusDB{
   private String[] intensity1Signatures;
   private String[] intensity2Signatures;
   private String[] intensity3Signatures;

   public VirusDB(String[] intensity1Signatures,String[] intensity2Signatures,String[] intensity3Signatures){
     this.intensity1Signatures = intensity1Signatures;
     this.intensity2Signatures = intensity2Signatures;
     this.intensity3Signatures = intensity3Signatures;
   }

   public String[] getSignatures(int arrayNum){
     switch (arrayNum){
       case 1:return this.intensity1Signatures;
       case 2:return this.intensity2Signatures;
       case 3:return this.intensity3Signatures;
       default:return new String[0];
     }
   }
}

Examples



 String[] intensity1signatures = new String[]{
        "malware",
        "virus",
        "infect"
      };

      String[] intensity2signatures = new String[]{
        "ransomware",
        "trojan",
        "trojanHorse",
        "worm",
        "spyware",
        "keystrokelogger",
        "adware",
        "botnet",
        "rootkit",
      };

      String[] intensity3signatures = new String[]{
        "DeleteSys32",
        "OverideMBR",
        "EncryptAll",
        "openrandomwebsite",
        "openrandwebsite",
        "sendalldata",
        "recordKeyboard",
        "recordmouse",
        "destroy",
        "overheat",
        "getfullcontrol",
        "uploadharddrive",
        "uploadharddisk",
        "overload",
        "changeOS",
        "encrypt",
        "changeDesktop",
        "ddos",
        "dos",
        "hide",
        "inject",
        "ransom",
        "getcreditcardinfo",
        "getpasswords",
        "getpass",
      };

#60 Two Sum


Write a function that takes an array of numbers (integers for the tests) and a target number. It should find two different items in the array that, when added together, give the target value. The indices of these items should then be returned in an array like so: [index1, index2].


For the purposes of this kata, some tests may have multiple answers; any valid solutions will be accepted.


The input will always be valid 

(numbers will be an array of length 2 or greater, and all of the items will be numbers; target will always be the sum of two different items from that array).



public class Solution
{
    public static int[] twoSum(final int[] numbers, int target)
    {
      for(int i = 0; i < numbers.length;i++ ) {
        for(int j = i + 1; j < numbers.length;j++) {
          if(numbers[i] + numbers[j] == target) {
              return new int []{i,j};
            }
          }
        }
        return null; // Do your magic!
    }
}

#52 Are they the "same"?


Given two arrays a and b write a function comp(a, b) (compSame(a, b) in Clojure) that checks whether the two arrays have the "same" elements, with the same multiplicities. "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.


Examples

Valid arrays

a = [121, 144, 19, 161, 19, 144, 19, 11]  

b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:


a = [121, 144, 19, 161, 19, 144, 19, 11] 

b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]

Invalid arrays

If we change the first number to something else, comp may not return true anymore:


a = [121, 144, 19, 161, 19, 144, 19, 11]  

b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 132 is not the square of any number of a.


a = [121, 144, 19, 161, 19, 144, 19, 11]  

b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 36100 is not the square of any number of a.



+ Recent posts