cipher [CodeSignal]

Consider the following ciphering algorithm:

  • For each character replace it with its code.
  • Concatenate all of the obtained numbers.

Given a ciphered string, return the initial one if it is known that it consists only of lowercase letters.

Note: here the character’s code means its decimal ASCII code, the numerical representation of a character used by most modern programming languages.

Example

For cipher = "10197115121", the output should be
decipher(cipher) = "easy".

Explanation: charCode('e') = 101charCode('a') = 97charCode('s') = 115and charCode('y') = 121.

Input/Output

  • [execution time limit] 3 seconds (java)
  • [input] string cipher

    A non-empty string which is guaranteed to be a cipher for some other string of lowercase letters.

    Guaranteed constraints:
    2 ≤ cipher.length ≤ 100.

  • [output] string

 

String decipher(String cipher) {
String sol = "";
for(int i = 0; i<cipher.length(); ){
if(cipher.charAt(i) == '1'){
sol += (char) Integer.parseInt(cipher.substring(i,i+3));
i+=3;
}
else{
sol += (char) Integer.parseInt(cipher.substring(i,i+2));
i +=2;
}
}
return sol;
}

lineEncoding [CodeSignal]

Given a string, return its encoding defined as follows:

  • First, the string is divided into the least possible number of disjoint substringsconsisting of identical characters
    • for example, "aabbbc" is divided into ["aa", "bbb", "c"]
  • Next, each substring with length greater than one is replaced with a concatenation of its length and the repeating character
    • for example, substring "bbb" is replaced by "3b"
  • Finally, all the new strings are concatenated together in the same order and a new string is returned.

Example

For s = "aabbbc", the output should be
lineEncoding(s) = "2a3bc".

Input/Output

  • [execution time limit] 3 seconds (java)
  • [input] string s

    String consisting of lowercase English letters.

    Guaranteed constraints:
    4 ≤ s.length ≤ 15.

  • [output] string
    • Encoded version of s.

 

String lineEncoding(String s) {
String sol = "";
while(s.length() >0){
int len = 0;
int i;
char startC = s.charAt(0);
for(i = 0; i<s.length(); i++){
if(startC == s.charAt(i))
len++;
else{

break;
}
}
if(len >1)
sol += len+""+ s.charAt(0);
else
sol += s.charAt(0);
s = s.substring(i);
System.out.println(sol);
}
return sol;
}

differentDigitsNumberSearch [CodeSignal]

Given an array of integers, find the leftmost number that has a decimal representation which doesn’t contain any digit more than once. If there is no such number, return -1instead.

Example

  • For inputArray = [22, 111, 101, 124, 33, 30], the output should be
    differentDigitsNumberSearch(inputArray) = 124;
  • For inputArray = [1111, 404], the output should be
    differentDigitsNumberSearch(inputArray) = -1.

Input/Output

  • [execution time limit] 3 seconds (java)
  • [input] array.integer inputArray

    Array of positive integers.

    Guaranteed constraints:
    2 ≤ inputArray.length ≤ 10,
    10 ≤ inputArray[i] ≤ 104.

  • [output] integer

 

int differentDigitsNumberSearch(int[] inputArray) {
for(int num: inputArray){
HashSet<Integer> hs = new HashSet<>();
int sol = num;
int len = 0;
while(num!=0){
len++;
hs.add(num%10);
num/=10;
}
System.out.println(hs.toString());
if(hs.size() == len)
return sol;
else
hs = new HashSet<>();

}

return -1;
}

isMAC48Address [CodeSignal]

A media access control address (MAC address) is a unique identifier assigned to network interfaces for communications on the physical network segment.

The standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits (0 to 9or A to F), separated by hyphens (e.g. 01-23-45-67-89-AB).

Your task is to check by given string inputString whether it corresponds to MAC-48 address or not.

Example

  • For inputString = "00-1B-63-84-45-E6", the output should be
    isMAC48Address(inputString) = true;
  • For inputString = "Z1-1B-63-84-45-E6", the output should be
    isMAC48Address(inputString) = false;
  • For inputString = "not a MAC-48 address", the output should be
    isMAC48Address(inputString) = false.

Input/Output

  • [execution time limit] 3 seconds (java)
  • [input] string inputString

    Guaranteed constraints:
    15 ≤ inputString.length ≤ 20.

  • [output] boolean
    • true if inputString corresponds to MAC-48 address naming rules, falseotherwise.

 

boolean isMAC48Address(String inputString) {
String[] arr = inputString.split("-");
if(inputString.length() != 17) return false;
if(arr.length != 6)
return false;
for(String s: arr){
if(s.length() == 0 || s.length() >2)
return false;
for(char c: s.toCharArray()){
if((c >='A' && c<='F') ||(c >='0' && c<='9'))
System.out.println("OK");
else
return false;
}
}
return true;
}

dresses [CodeSignal]

URL : CodeSignal

Diana is planning to go to a party this weekend! She wants to show up looking fresh, so she decided to buy a new dress. She is very conscious of the color of her future dress and she really wants something more green than red or blue.

There are several dresses in the shop, and we know the RGB representations of their colors. Your task is help Diana by finding the indices of the dresses in which there is more green than either red or blue (ie: where green uniquely has the highest value).

Notes:

  • The RGB representation of a color is a 6-digit hexadecimal number. The first two digits are the red component, digits 3 and 4 are the green component, and the last two digits are the blue component.

There is more green in a color then red or blue if and only if the green component is strictly greater than the red component and is also strictly greater than the blue component. For example A0FC77 is a good color for Diana, but 90CACA is not, because both green and blue components are CA, so there isn’t more green than blue.

Examples

  • For colors = ["A0FC77", "90CACA"], the output should be dresses(colors) = [0]

    The dress at index 0 has more green than either red or blue, but the dress at index 1has an equal amount of green and blue, so it doesn’t qualify.

  • For colors = ["000000", "101110", "F01AC3", "0FFEF4"], the output should be dresses(colors) = [1, 3]
    • Dress 0 is completely black (no green at all).
    • Dress 1 has just slightly more green than red or blue (17 green vs 16 red and blue).
    • Dress 2 has a lot more red and blue than green.
    • Dress 3 has very little red, but a lot of blue, and just slightly more green

    Dresses 1 and 3 are the only two that meet the requirements.

  • For colors = ["FFFFFF"], the output should be dresses(colors) = []

    This dress is all white; it has the maximum amount of each of the colours, so there isn’t strictly more green than anything else.

Input / Output

  • [execution time limit] 3 seconds (java)
  • [input] array.string colors

    An array of strings of length 6, consisting of symbols 0 - 9 and A - F.

    Guaranteed constraints:
    1 ≤ colors.length ≤ 104,
    colors[i].length = 6 

  • [output] array.integer
    • The indices of the dresses for which there is more green than either red or blue.

 

int[] dresses(String[] colors) {
ArrayList<Integer> list = new ArrayList<>();
int i = 0;
for(String s:colors){
char[] arr = s.toCharArray();
int red = Integer.parseInt(arr[0]+""+arr[1],16) ;
int green = Integer.parseInt(arr[2]+""+arr[3],16);
int blue = Integer.parseInt(arr[4]+""+arr[5],16);

System.out.println(red+" "+green+" "+blue+ " "+ i);
if(green > red && green > blue){
System.out.println("Selected: "+ i);
list.add(i);
}
++i;

}

int[] sol = new int[list.size()];
int j = 0;
for(int n: list){
sol[j] = n;
j++;
}

return sol;

}

arrayMode [CodeSignal]

Given array of integers, find its mode.

Example

  • For sequence = [1, 3, 3, 3, 1], the output should be
    arrayMode(sequence) = 3;
  • For sequence = [1, 3, 2, 1], the output should be
    arrayMode(sequence) = 1.

Input/Output

  • [execution time limit] 3 seconds (java)
  • [input] array.integer sequence

    An unsorted array of integers.

    Guaranteed constraints:
    3 ≤ sequence.length ≤ 5 · 104,
    1 ≤ sequence[i] ≤ 1000.

  • [output] integer
    • The mode of sequence. It is guaranteed that there is an unambiguous answer for the given data.
int arrayMode(int[] sequence) {
int[] arr = new int[10000];
int max = 0;
int sol = 0;
for(int num:sequence){
arr[num]++;
if(max<arr[num]){
max = arr[num];
sol = num;
}

}
return sol;
}

isPangram [CodeSignal]

Implement the missing code, denoted by ellipses. You may not modify the pre-existing code.

Given a sentence, check whether it is a pangram or not.

Example

  • For sentence = "The quick brown fox jumps over the lazy dog.", the output should be
    isPangram(sentence) = true;
  • For sentence = "abcdefghijklmnopqrstuvwxya", the output should be
    isPangram(sentence) = false.

Input/Output

  • [execution time limit] 3 seconds (java)
  • [input] string sentence

    A string containing characters with their ASCII-codes in the range [32, 126].

    Guaranteed constraints:
    1 ≤ sentence.length ≤ 100.

  • [output] boolean
    • true if sentence is a pangram, falseotherwise.

 

boolean isPangram(String sentence) {
boolean[] found = new boolean[26];
boolean result = true;
for (int i = 0; i < sentence.length(); i++) {
int code = (int)sentence.charAt(i) ;
if ((int)'A' <= code && code <= (int)'Z') {
code += (int)'a' - (int)'A';
}
if ((int)'a' <= code && code <= (int)'z') {
found[code - (int)'a'] = true;
}
}

for (int i = 0; i < 26; i++) {
if (!found[i]) {
result = false;
}
}

return result;
}

appleBoxes [CodeSignal]

You have k apple boxes full of apples. Each square box of size m contains m × m apples. You just noticed two interesting properties about the boxes:

  1. The smallest box is size 1, the next one is size 2,…, all the way up to size k.
  2. Boxes that have an odd size contain only yellow apples. Boxes that have an even size contain only red apples.

Your task is to calculate the difference between the number of red apples and the number of yellow apples.

Example

For k = 5, the output should be
appleBoxes(k) = -15.

There are 1 + 3 * 3 + 5 * 5 = 35 yellow apples and 2 * 2 + 4 * 4 = 20 red apples, making the answer 20 - 35 = -15.

Input/Output

  • [execution time limit] 3 seconds (java)
  • [input] integer k

    A positive integer.

    Guaranteed constraints:
    1 ≤ k ≤ 40.

  • [output] integer
    • The difference between the two types of apples.

 

int appleBoxes(int k) {
int odd = 0;
int eve = 0;
for(int i = 1; i<=k; i++){
if(i%2==0)
eve +=(i*i);
else
odd +=(i*i);
}
return eve-odd;
}

CodeSignal

primeFactors [CodeSignal]

Write an algorithm that constructs an array of non unique prime factors of a number n.

Example

For n = 100, the output should be
primeFactors(n) = [2, 2, 5, 5].

Input/Output

  • [execution time limit] 3 seconds (java)
  • [input] integer nA positive integer.

    Guaranteed constraints:
    1 ≤ n ≤ 150.

  • [output] array.integer
    • An array of prime factors of n from the smallest to the largest.
int[] primeFactors(int n) {
ArrayList<Integer> factors = new ArrayList<>();
int divisor = 2;

while (n >= 2) {
if (n % divisor == 0) {
factors.add(divisor);
n /= divisor;
} else {
divisor++;
}
}

int[] ans = new int[factors.size()];
for (int i = 0; i < ans.length; ++i) {
ans[i] = factors.get(i);
}
return ans;
}

 

Ketan Ramteke