#86 Remove Duplicates


Remove Duplicates

You are to write a function called unique that takes an array of integers and returns the array with duplicates removed. It must return the values in the same order as first seen in the given array. Thus no sorting should be done, if 52 appears before 10 in the given array then it should also be that 52 appears before 10 in the returned array.


Assumptions

All values given are integers (they can be positive or negative).

You are given an array but it may be empty.

They array may have duplicates or it may not.

You cannot use the uniq method on Arrays (don't even try it), or the nub function from Data.List.

Example

UniqueArray.unique([1, 5, 2, 0, 2, -3, 1, 10]) 

// -> [1, 5, 2, 0, -3, 10]



 #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]);



#84Can you get the loop ?


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.


Image and video hosting by TinyPic


// 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 !!



 #83 Stop gninnipS My sdroW!


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.



Examples:


spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw" 

spinWords( "This is a test") => returns "This is a test" 

spinWords( "This is another test" )=> returns "This is rehtona test"


 #82Evil Autocorrect Prank


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


#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 ");
  }

}

#80 Calculating Batting Average


In baseball, the batting average is a simple and most common way to measure a hitter's performace. Batting average is calculated by taking all the players hits and dividing it by their number of at_bats, and it is usually displayed as a 3 digit decimal (i.e. 0.300).


Given a yankees table with the following schema,


-player_id STRING


-player_name STRING


-primary_position STRING


-games INTEGER


-at_bats INTEGER


-hits INTEGER


return a table with player_name, games, and batting_average.


We want batting_average to be rounded to the nearest thousandth, since that is how baseball fans are used to seeing it. Format it as text and make sure it has 3 digits to the right of the decimal (pad with zeroes if neccesary).


Next, order our resulting table by batting_average, with the highest average in the first row.


Finally, since batting_average is a rate statistic, a small number of at_bats can change the average dramatically. To correct for this, exclude any player who doesn't have at least 100 at bats.


Expected Output Table


-player_name STRING


-games INTEGER


-batting_average STRING



#79 Human readable duration format


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.




Underscores in Numeric Literals


긴 숫자를 선언할때 숫자사이에 언더스코어를 넣어서 가독성을 향상 시킬수 있다. 


숫자 구두점처럼 3자리마다 찍을 필요는 없고 숫자와 숫자 사이에서만 유효하다.

int price = 1000000;
int price =1_000_000;


 #78 Convert PascalCase string into snake_case


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);


+ Recent posts