You are working with a patient's body which has many cells.
The patient's body is a matrix where every row represents a cell.
Each cell contains just uppercase and lowercase letters,
and every cell in the body should be the same.
Oh no! It seems that one of the cells have mutated!
It is your job to locate the mutation so that the chemo specialists can fix it!
return the location [i,j] within the matrix...
before it's too late! :(
example:
cellscellscellscodecodecells
cellscellscellscodecodecells
cellscellscellscodecodecells
cellscellscellscodecodecells
cellscellscellscodecodecells
cellscellscellscodecodecells
cellscellscellscodecodecells
cellscellscellscodecodecells
cellscellscellscodecodecells
cellscellscellscodecadecells <- here it is! [9, 20]
cellscellscellscodecodecells
cellscellscellscodecodecells
cellscellscellscodecodecells
cellscellscellscodecodecells
no bodies will have less than 3 cells.
if the diagnose was a false alarm, return an empty array.
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
class JomoPipi {
public static int[] mutationLocation(char[][] body) {
Map<string, integer=""> map = new HashMap<>();
for (int i = 0; i < 3; i++) {
map.put(String.valueOf(body[i]), 1 + map.getOrDefault(String.valueOf(body[i]),0));
}
char [] dna = map.entrySet().stream()
.sorted(Map.Entry.<string, integer="">comparingByValue().reversed())
.findFirst().get().getKey().toCharArray();
for (int x = 0; x < body.length; x++) {
if(!Arrays.equals(dna, body[x]))
for (int y = 0; y < body[x].length; y++) {
if(dna[y] != body[x][y]) {
return new int [] {x,y};
}
}
}
return new int [0];
}
}
The wave (known as the Mexican wave in the English-speaking world outside North America) is an example of metachronal rhythm achieved in a packed stadium when successive groups of spectators briefly stand, yell, and raise their arms. Immediately upon stretching to full height, the spectator returns to the usual seated position. The result is a wave of standing spectators that travels through the crowd, even though individual spectators never move away from their seats. In many large arenas the crowd is seated in a contiguous circuit all the way around the sport field, and so the wave is able to travel continuously around the arena; in discontiguous seating arrangements, the wave can instead reflect back and forth through the crowd. When the gap in seating is narrow, the wave can sometimes pass through it. Usually only one wave crest will be present at any given time in an arena, although simultaneous, counter-rotating waves have been produced. (Source Wikipedia)
Task
In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.
Rules
1. The input string will always be lower case but maybe empty.
2. If the character in the string is whitespace then pass over it as if it was an empty seat.
You are given a node that is the beginning of a linked list. This list always contains a tail and a loop.
Your objective is to determine the length of the loop.
For example in the following picture the tail's size is 3 and the loop size is 11.
// Use the `getNext()` method to get the following node.
node.getNext()
Note: do NOT mutate the nodes!
Thanks to shadchnev, I broke all of the methods from the Hash class.
Don't miss dmitry's article in the discussion after you pass the Kata !!
import java.util.ArrayList;
import java.util.List;
public class LoopInspector {
public int loopSize(Node node) {
List<node> list = new ArrayList<>();
int index =0;
while(true){
index = list.indexOf(node);
if(index == -1)
list.add(node);
else
break;
node = node.getNext();
}
return list.size()-index;
}
}
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
Your friend won't stop texting his girlfriend. It's all he does. All day. Seriously. The texts are so mushy too! The whole situation just makes you feel ill. Being the wonderful friend that you are, you hatch an evil plot. While he's sleeping, you take his phone and change the autocorrect options so that every time he types "you" or "u" it gets changed to "your sister."
Write a function called autocorrect that takes a string and replaces all instances of "you" or "u" (not case sensitive) with "your sister" (always lower case).
Return the resulting string.
Here's the slightly tricky part: These are text messages, so there are different forms of "you" and "u".
For the purposes of this kata, here's what you need to support:
"youuuuu" with any number of u characters tacked onto the end
"u" at the beginning, middle, or end of a string, but NOT part of a word
"you" but NOT as part of another word like youtube or bayou
public class Kata {
public static String autocorrect(String input) {
return input.replaceAll("(?i)\\b(u|you+)\\b", "your sister");
}
}
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 ");
}
}
My grandfather always predicted how old people would get, and right before he passed away he revealed his secret!
In honor of my grandfather's memory we will write a function using his formula!
Take a list of ages when each of your great-grandparent died.
Multiply each number by itself.
Add them all together.
Take the square root of the result.
Divide by two.
Example
predictAge(65, 60, 75, 55, 60, 63, 64, 45) === 86
Note: the result should be rounded down to the nearest integer.
import java.util.Arrays;
public class PredictYourAge {
public static int predictAge(int ... ages) {
// your code goes here
return (int) (Math.sqrt(Arrays.stream(ages).boxed().mapToInt(x -> x*x).sum())/2);
}
}
n this Kata, you will be given a string with brackets and an index of an opening bracket and your task will be to return the index of the matching closing bracket. Both the input and returned index are 0-based except in Fortran where it is 1-based. An opening brace will always have a closing brace. Return -1 if there is no answer (Haskell return Nothing, Fortran: return 0)
For example
solve("((1)23(45))(aB)", 0) = 10 // the opening brace at index 0 matches the closing brace at index 10
solve("((1)23(45))(aB)", 1) = 3
solve("((1)23(45))(aB)", 2) = -1 // there is no opening bracket at index 2, so return -1
solve("((1)23(45))(aB)", 6) = 9
solve("((1)23(45))(aB)", 11) = 14
solve("((>)|?(*'))(yZ)", 11) = 14
import java.util.Stack;
public class Indice {
public static int solve(String str, int index){
// write your code
Stack<Integer> stack = new Stack<Integer>();
for(int i =0 ; i < str.length(); i++){
if(str.charAt(i) == '('){
stack.add(i);
}else if(str.charAt(i) == ')'){
if(stack.pop() == index)
return i;
}
}
return -1;
}
}