#23 Prime Streaming


Create an endless stream of prime numbers - a bit like IntStream.of(2,3,5,7,11,13,17), but infinite. The stream must be able to produce 50 million primes in a few seconds.


당당하게 도전하고 ,당당하게 망했다 3시간동안 고민해봤는데 못풀겠다.

현재 레벨보다 높은 단계라 솔루션도 못본다. 포기하고 다른문제 풀어서 올릴까하다가 

들인시간이 아까워서 주말에 다시 풀어보려고 올림



#22 Fat Fingers


Freddy has a really fat left pinky finger, and every time Freddy tries to type an A, he accidentally hits the CapsLock key!


Given a string that Freddy wants to type, emulate the keyboard misses where each A supposedly pressed is replaced with CapsLock, and return the string that Freddy actually types. It doesn't matter if the A in the string is capitalized or not. When CapsLock is enabled, capitalization is reversed, but punctuation is not affected.


Examples:


"The quick brown fox jumps over the lazy dog."

-> "The quick brown fox jumps over the lZY DOG."


"The end of the institution, maintenance, and administration of government, is to secure the existence of the body politic, to protect it, and to furnish the individuals who compose it with the power of enjoying in safety and tranquillity their natural rights, and the blessings of life: and whenever these great objects are not obtained, the people have a right to alter the government, and to take measures necessary for their safety, prosperity and happiness."

-> "The end of the institution, mINTENnce, ND dministrTION OF GOVERNMENT, IS TO SECURE THE EXISTENCE OF THE BODY POLITIC, TO PROTECT IT, nd to furnish the individuLS WHO COMPOSE IT WITH THE POWER OF ENJOYING IN Sfety ND TRnquillity their nTURl rights, ND THE BLESSINGS OF LIFE: nd whenever these greT OBJECTS re not obtINED, THE PEOPLE Hve  RIGHT TO lter the government, ND TO Tke meSURES NECESSry for their sFETY, PROSPERITY nd hPPINESS."


"aAaaaaAaaaAAaAa"

-> ""

If the given string is null, return null.


If the given string is "", the answer should be evident.


Happy coding!



 #21  Number of trailing zeros of N! 


Write a program that will calculate the number of trailing zeros in a factorial of a given number.


N! = 1 * 2 * 3 * ... * N


Be careful 1000! has 2568 digits...


For more info, see: http://mathworld.wolfram.com/Factorial.html


Examples

zeros(6) = 1

# 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zero


zeros(12) = 2

# 12! = 479001600 --> 2 trailing zeros

Hint: You're not meant to calculate the factorial. Find another way to find the number of zeros.


팩토리얼을 구하고 0의 갯수를 세면... 타임 초과가 난다 이틀 연속 타임 초과



 #20 The Millionth Fibonacci Kata 


The year is 1214. One night, Pope Innocent III awakens to find the the archangel Gabriel floating before him. Gabriel thunders to the pope:


Gather all of the learned men in Pisa, especially Leonardo Fibonacci. In order for the crusades in the holy lands to be successful, these men must calculate the millionth number in Fibonacci's recurrence. Fail to do this, and your armies will never reclaim the holy land. It is His will.


The angel then vanishes in an explosion of white light.


Pope Innocent III sits in his bed in awe. How much is a million? he thinks to himself. He never was very good at math.


He tries writing the number down, but because everyone in Europe is still using Roman numerals at this moment in history, he cannot represent this number. If he only knew about the invention of zero, it might make this sort of thing easier.


He decides to go back to bed. He consoles himself, The Lord would never challenge me thus; this must have been some deceit by the devil. A pretty horrendous nightmare, to be sure.


Pope Innocent III's armies would go on to conquer Constantinople (now Istanbul), but they would never reclaim the holy land as he desired.


In this kata you will have to calculate fib(n) where:


fib(0) := 0

fib(1) := 1

fin(n + 2) := fib(n + 1) + fib(n)

Write an algorithm that can handle n where 1000000 ≤ n ≤ 1500000.


Your algorithm must output the exact integer answer, to full precision. Also, it must correctly handle negative numbers as input.


HINT I: Can you rearrange the equation fib(n + 2) = fib(n + 1) + fib(n) to find fib(n) if you already know fin(n + 1) and fib(n + 2)? Use this to reason what value fib has to have for negative values.


HINT II: See http://mitpress.mit.edu/sicp/chapter1/node15.html




#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




 #18 Convert string to camel case



Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized.


Examples:


// returns "theStealthWarrior"

toCamelCase("the-stealth-warrior") 


// returns "TheStealthWarrior"

toCamelCase("The_Stealth_Warrior")





계속 크롬만 써서 익스를 신경 안썼는데 익스에서 파라미터에 한글이 들어가면 에러가 발생했다.


인코딩을 해야하는 부분이 좀 있어서 나눠서 코딩을 하기로 했는데, 각각 쓴 함수가 달랐다.


- encodeURI()

- encodeURIComponent()


그래서 검색해본결과 각각 쓰임새가 약간 다르다. ( 출처 : 생활코딩 )



encodeURI 

encodeURIComponent 

 인터넷 주소에서 사용하는 특수문자는 변환하지 않는다.

: ; / = ? &

모든 문자를 변환 

 인터넷 주소 전체를 인코딩할 때

주소를 파라미터로 사용할 때

예) 'http://opentutorials.org/?uri='+encodeURIComponent(url); 



# 17 Strings Mix


Given two strings s1 and s2, we want to visualize how different the two strings are. We will only take into account the lowercase letters (a to z). First let us count the frequency of each lowercase letters in s1 and s2.


s1 = "A aaaa bb c"


s2 = "& aaa bbb c d"


s1 has 4 'a', 2 'b', 1 'c'


s2 has 3 'a', 3 'b', 1 'c', 1 'd'


So the maximum for 'a' in s1 and s2 is 4 from s1; the maximum for 'b' is 3 from s2. In the following we will not consider letters when the maximum of their occurrences is less than or equal to 1.


We can resume the differences between s1 and s2 in the following string: "1:aaaa/2:bbb" where 1 in 1:aaaa stands for string s1 and aaaa because the maximum for a is 4. In the same manner 2:bbb stands for string s2 and bbb because the maximum for b is 3.


The task is to produce a string in which each lowercase letters of s1 or s2 appears as many times as its maximum if this maximum is strictly greater than 1; these letters will be prefixed by the number of the string where they appear with their maximum value and :. If the maximum is in s1 as well as in s2 the prefix is =:.


In the result, substrings (a substring is for example 2:nnnnn or 1:hhh; it contains the prefix) will be in decreasing order of their length and when they have the same length sorted in ascending lexicographic order (letters and digits - more precisely sorted by codepoint); the different groups will be separated by '/'. See examples and "Example Tests".


Hopefully other examples can make this clearer.


s1 = "my&friend&Paul has heavy hats! &"

s2 = "my friend John has many many friends &"

mix(s1, s2) --> "2:nnnnn/1:aaaa/1:hhh/2:mmm/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss"


s1 = "mmmmm m nnnnn y&friend&Paul has heavy hats! &"

s2 = "my frie n d Joh n has ma n y ma n y frie n ds n&"

mix(s1, s2) --> "1:mmmmmm/=:nnnnnn/1:aaaa/1:hhh/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss"


s1="Are the kids at home? aaaaa fffff"

s2="Yes they are here! aaaaa fffff"

mix(s1, s2) --> "=:aaaaaa/2:eeeee/=:fffff/1:tt/2:rr/=:hh"



#16 Java format Unicode encoder/decoder 


We need a reusable program to encode/decode text to unicode value as represented in Java and vice-versa.


Encoder:


Input

"hola"


Output

"\u0068\u006f\u006c\u0061"


Decoder:


Input

"\u0068\u006f\u006c\u0061"


Output

"hola"


Observations:

Please note that unicode values have always a back slash \ the letter u and 4 hexadecimal digits corresponding to the value in the unicode character set.

Codewars has some trouble showing non ASCII characters so perhaps you should try it locally first.





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

}




+ Recent posts