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.
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 ");
}
}
Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.
The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.
It is much easier to understand with an example:
TimeFormatter.formatDuration(62) //returns "1 minute and 2 seconds"
TimeFormatter.formatDuration(3662) //returns "1 hour, 1 minute and 2 seconds"
For the purpose of this Kata, a year is 365 days and a day is 24 hours.
Note that spaces are important.
Detailed rules
The resulting expression is made of components like 4 seconds, 1 year, etc. In general, a positive integer and one of the valid units of time, separated by a space. The unit of time is used in plural if the integer is greater than 1.
The components are separated by a comma and a space (", "). Except the last component, which is separated by " and ", just like it would be written in English.
A more significant units of time will occur before than a least significant one. Therefore, 1 second and 1 year is not correct, but 1 year and 1 second is.
Different components have different unit of times. So there is not repeated units like in 5 seconds and 1 second.
A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0 seconds is not valid, but it should be just 1 minute.
A unit of time must be used "as much as possible". It means that the function should not return 61 seconds, but 1 minute and 1 second instead. Formally, the duration specified by of a component must not be greater than any valid more significant unit of time.
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
public class TimeFormatter {
public static String formatDuration(int seconds) {
// your code goes here
int d = seconds / 86400;
seconds %= 86400;
int h = seconds / 3600;
seconds %= 3600;
int m = seconds / 60;
seconds %= 60;
List<String> list = new ArrayList<>();
if(d != 0)
if(d > 1)
list.add(d+" days");
else
list.add(d+" days");
if(h != 0)
if(h > 1)
list.add(h+" hours");
else
list.add(h+" hour");
if(m != 0)
if(m > 1)
list.add(m+" minutes");
else
list.add(m+" minute");
if(seconds!= 0)
if(seconds > 1)
list.add(seconds+" seconds");
else
list.add(seconds+" second");
String format;
if(list.size() ==4){
format = "{0}, {1}, {2} and {3}";
}else if(list.size()==3) {
format = "{0}, {1} and {2}";
}else if(list.size() ==2) {
format = "{0} and {1}";
}else {
format = "{0}";
}
return MessageFormat.format(format, list.toArray());
}
}
Complete the function/method so that it takes CamelCase string and returns the string in snake_case notation. Lowercase characters can be numbers. If method gets number, it should return string.
Examples:
// returns test_controller
toUnderscore('TestController');
// returns movies_and_books
toUnderscore('MoviesAndBooks');
// returns app7_test
toUnderscore('App7Test');
// returns "1"
toUnderscore(1);
function toUnderscore(string) {
return string.toString().replace(/\.?([A-Z])/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, "");
}