#19 The Clockwise Spiral


##Do you know how to make a spiral? Let's test it!

Classic definition: A spiral is a curve which emanates from a central point, getting progressively farther away as it revolves around the point.


Your objective is to complete a function createSpiral(N) that receives an integer N and returns an NxN two-dimensional array with numbers 1 through N^2 represented as a clockwise spiral.


Return an empty array if N < 1 or N is not int/number


Examples:


N = 3 Output: [[1,2,3],[8,9,4],[7,6,5]]


1    2    3    

8    9    4    

7    6    5

N = 4 Output: [[1,2,3,4],[12,13,14,5],[11,16,15,6],[10,9,8,7]]


1   2   3   4

12  13  14  5

11  16  15  6

10  9   8   7

N = 5 Output: [[1,2,3,4,5],[16,17,18,19,6],[15,24,25,20,7],[14,23,22,21,8],[13,12,11,10,9]]


1   2   3   4   5    

16  17  18  19  6    

15  24  25  20  7    

14  23  22  21  8    

13  12  11  10  9




#15 Bagels 


Here's a seemingly simple challenge. We're giving you a class called bagel, exactly as it appears below. All it really does is return an int, specifically 3.


public class Bagel {
    public final int getValue() {
        return 3;
    }
}


The catch? For the solution, we're testing that the result is equal to 4. But as a little hint, the solution to the this Kata is exactly the same as the example test cases.

통과조건...
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class BagelTest {

    @Test
    public void testBagel() {
        Bagel bagel = BagelSolver.getBagel();

        assertEquals(
            bagel.getValue() == 4,
            java.lang.Boolean.TRUE
        );
    }

}

...별짓을 다했다 리플랙션 , 프록시, bci

설마이걸까 했는데 -.- 으음 빡친다.

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class BagelSolver {

  public static Bagel getBagel() {
	  try {
		    Field f = Boolean.class.getField("TRUE");
		    Field modifiers = Field.class.getDeclaredField("modifiers");
		    modifiers.setAccessible(true);
		    modifiers.setInt(f, f.getModifiers() & ~Modifier.FINAL);
		    f.set(null, false);
		} catch (Exception e) {
			// TODO: handle exception
		}
    return new Bagel();
  }

}




#14 Calculator


Create a simple calculator that given a string of operators (+ - * and /) and numbers separated by spaces returns the value of that expression


Example:


Calculator.evaluate("2 / 2 + 3 * 4 - 6") // => 7

Remember about the order of operations! Multiplications and divisions have a higher priority and should be performed left-to-right. Additions and subtractions have a lower priority and should also be performed left-to-right.


#13 Anagram Detection


An anagram is the result of rearranging the letters of a word to produce a new word (see wikipedia).


Note: anagrams are case insensitive


Complete the function to return true if the two arguments given are anagrams of theeach other; return false otherwise.


Examples :


"foefet" is an anagram of "toffee"

"Buckethead" is an anagram of "DeathCubeK"



'매일매일개발 > Codewars' 카테고리의 다른 글

Codewars #15 Bagels (4kyu)  (0) 2018.04.06
Codewars #14 Calculator (3kyu)  (0) 2018.04.03
Codewars #12 Range Extraction (4kyu)  (0) 2018.03.30
Codewars #11 Double Cola (5kyu)  (0) 2018.03.30
Codewars #10 Scramblies (5kyu)  (0) 2018.03.28

# Double Cola



Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on.


For example, Penny drinks the third can of cola and the queue will look like this:


Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny

Write a program that will return the name of the person who will drink the n-th cola.


Note that in the very beginning the queue looks like that:


Sheldon, Leonard, Penny, Rajesh, Howard

##Input


The input data consist of an array which contains at least 1 name, and single integer n.


(1 ≤ n ≤ 1000000000).


##Output / Examples Return the single line — the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.


 string[] names = new string[] { "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" };

 int n = 1;

 Line.WhoIsNext(names, n) --> "Sheldon"


 int n = 6;

 Line.WhoIsNext(names, n) --> "Sheldon"


 int n = 52;

 Line.WhoIsNext(names, n) --> "Penny"


 int n = 7230702951;

 Line.WhoIsNext(names, n) --> "Leonard"



# Scramblies


Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.


Notes:


Only lower case letters will be used (a-z). No punctuation or digits will be included.

Performance needs to be considered


Examples

scramble('rkqodlw', 'world') ==> True

scramble('cedewaraaossoqqyt', 'codewars') ==> True

scramble('katas', 'steak') ==> False




#Large Factorials


In mathematics, the factorial of integer n is written as n!. It is equal to the product of n and every integer preceding it. For example: 5! = 1 x 2 x 3 x 4 x 5 = 120


Your mission is simple: write a function that takes an integer n and returns the value of n!.


You are guaranteed an integer argument. For any values outside the non-negative range, return null, nil or None (return an empty string "" in C and C++). For non-negative numbers a full length number is expected for example, return 25! = "15511210043330985984000000" as a string.


For more on factorials, see http://en.wikipedia.org/wiki/Factorial



# Middle Permutation


Task

You are given a string s. Every letter in s appears once.


Consider all strings formed by rearranging the letters in s. After ordering these strings in dictionary order, return the middle term. (If the sequence has a even length n, define its middle term to be the (n/2)th term.)


Example

For s = "abc", the result should be "bac".


The permutations in order are:

"abc", "acb", "bac", "bca", "cab", "cba"

So, The middle term is "bac".

Input/Output


[input] string s

unique letters (2 <= length <= 26)


[output] a string

middle permutation.




# Josephus Permutation


This problem takes its name by arguably the most important event in the life of the ancient historian Josephus: according to his tale, he and his 40 soldiers were trapped in a cave by the Romans during a siege.


Refusing to surrender to the enemy, they instead opted for mass suicide, with a twist: they formed a circle and proceeded to kill one man every three, until one last man was left (and that it was supposed to kill himself to end the act).


Well, Josephus and another man were the last two and, as we now know every detail of the story, you may have correctly guessed that they didn't exactly follow through the original idea.


You are now to create a function that returns a Josephus permutation, taking as parameters the initial array/list of items to be permuted as if they were in a circle and counted out every k places until none remained.


Tips and notes: it helps to start counting from 1 up to n, instead of the usual range 0..n-1; k will always be >=1.


For example, with n=7 and k=3 josephus(7,3) should act this way.


[1,2,3,4,5,6,7] - initial sequence

[1,2,4,5,6,7] => 3 is counted out and goes into the result [3]

[1,2,4,5,7] => 6 is counted out and goes into the result [3,6]

[1,4,5,7] => 2 is counted out and goes into the result [3,6,2]

[1,4,5] => 7 is counted out and goes into the result [3,6,2,7]

[1,4] => 5 is counted out and goes into the result [3,6,2,7,5]

[4] => 1 is counted out and goes into the result [3,6,2,7,5,1]

[] => 4 is counted out and goes into the result [3,6,2,7,5,1,4]



So our final result is: josephus([1,2,3,4,5,6,7],3)==[3,6,2,7,5,1,4]




# Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....¡Eureka!!


The number 89 is the first integer with more than one digit that fulfills the property partially introduced in the title of this kata. What's the use of saying "Eureka"? Because this sum gives the same number.


In effect: 89 = 8^1 + 9^2


The next number in having this property is 135.


See this property again: 135 = 1^1 + 3^2 + 5^3


We need a function to collect these numbers, that may receive two integers a, b that defines the range [a, b] (inclusive) and outputs a list of the sorted numbers in the range that fulfills the property described above.


Let's see some cases:


sum_dig_pow(1, 10) == [1, 2, 3, 4, 5, 6, 7, 8, 9]


sum_dig_pow(1, 100) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 89]

If there are no numbers of this kind in the range [a, b] the function should output an empty list.


sum_dig_pow(90, 100) == []

Enjoy it!!



+ Recent posts