#94 The Hashtag Generator


The marketing team are spending way too much time typing in hashtags.

Let's help them with out own Hashtag Generator!


Here's the deal:


- If the final result is longer than 140 chars it must return false.

- If the input is a empty string it must return false.

- It must start with a hashtag (#).

- All words must have their first letter capitalized.


Example Input to Output:


" Hello there thanks for trying my Kata" => "#HelloThereThanksForTryingMyKata"


" Hello World " => "#HelloWorld"




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

codewars #93 Number Format (6kyu)  (0) 2018.08.08
codewars #92 Bit calculator (5kyu)  (0) 2018.08.07
codewars #91 Find X (6kyu)  (0) 2018.08.06
codewars #90 Cure Cancer (6kyu)  (0) 2018.08.01
codewars #89 Mexican Wave (6kyu)  (0) 2018.07.31

#93 Number Format


Format any integer provided into a string with "," (commas) in the correct places.


Example:


numberFormat(100000); // return '100,000'

numberFormat(5678545); // return '5,678,545'

numberFormat(-420902); // return '-420,902'


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

codewars #94 The Hashtag Generator (5kyu)  (0) 2018.08.09
codewars #92 Bit calculator (5kyu)  (0) 2018.08.07
codewars #91 Find X (6kyu)  (0) 2018.08.06
codewars #90 Cure Cancer (6kyu)  (0) 2018.08.01
codewars #89 Mexican Wave (6kyu)  (0) 2018.07.31

운영서버에 간헐적으로 CPU 사용량이 100%가 되어 내려오지 않는 현상이 발생하였고 몇주동안이나 원인을 찾지 못하다가 

우연히 서버에 CSV 파일을 업로드하다 무한루프가 있음을 발견하였다.


CSV 한 행에서 열의 갯수가 3개 이하일 경우 3개로 맞춰주는 부분을 아래와 같이 while문을 통해 작성하였는데,


String [] row = getRows(); // csv 파일의 한 행을 String [] 형태로 반환한다;
while(row.length != 3) // 
   Arrays.append(row,"");


열의 갯수가 3보다 큰 경우를 고려하지 않아서 열의 갯수가 3보다 큰경우의 무한루프에 빠지게 되었다.

조건문을 수정하여 row.length != 3 -> row.length <3  문제는 해결 하였지만 


기본적인 부분에서 테스트 미흡 , 무한루프를 고려하지 못한점이 많이 아쉽다.



요약


원인 : while문을 사용할때 잘못 된 조건문 사용

해결 : 조건문 수정 / while 문사용시 무한 루프 고려 및 테스트 철저히

#92 Bit calculator 


In this kata your task is to create bit calculator. Function arguments are two bit representation of numbers ("101","1","10"...), and you must return their sum in decimal representation.


Test.expect(calculate("10","10") == 4);

Test.expect(calculate("10","0") == 2);

Test.expect(calculate("101","10") == 7);

parseInt and some Math functions are disabled.


Those Math functions are enabled: pow,round,random


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

codewars #94 The Hashtag Generator (5kyu)  (0) 2018.08.09
codewars #93 Number Format (6kyu)  (0) 2018.08.08
codewars #91 Find X (6kyu)  (0) 2018.08.06
codewars #90 Cure Cancer (6kyu)  (0) 2018.08.01
codewars #89 Mexican Wave (6kyu)  (0) 2018.07.31

#91 Find X 


We have a function that takes in an integer n, and returns a number x.


Lets call this function findX(n)/find_x(n) (depending on your language):


function findX(n) {

  let x = 0;

  for (let i = 0; i < n; i++) {

    for (let j = 0; j < 2*n; j++)

      x += i + j;

  }

  return x;

}

The functions loops throught the number n and at every iteration, performs a nested loop on 2*n, at each iteration of this nested loop it increments x with the (nested loop index + parents loop index).


This works well when the numbers are reasonably small.


findX(2) //=> 16

findX(3) //=> 63

findX(5) //=> 325

But may be slow for numbers > 103


So your task is to optimize the function findX/find_x, so it works well for large numbers.


Input Range

1 <= n <= 106 (105 in JS)


Note: This problem is more about logical reasoning than it is about finding a mathematicial formula, infact there are no complex math formula involved




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

codewars #93 Number Format (6kyu)  (0) 2018.08.08
codewars #92 Bit calculator (5kyu)  (0) 2018.08.07
codewars #90 Cure Cancer (6kyu)  (0) 2018.08.01
codewars #89 Mexican Wave (6kyu)  (0) 2018.07.31
codewars #88 Which are in? (6kyu)  (0) 2018.07.30

 #90 Cure Cancer


Now you are a doctor.


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.




#89 Mexican Wave


Introduction

 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.


Example

wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]


Good luck and enjoy!




#88 Which are in?


Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.


#Example 1: a1 = ["arp", "live", "strong"]


a2 = ["lively", "alive", "harp", "sharp", "armstrong"]


returns ["arp", "live", "strong"]


#Example 2: a1 = ["tarp", "mice", "bull"]


a2 = ["lively", "alive", "harp", "sharp", "armstrong"]


returns []


Notes:

Arrays are written in "general" notation. See "Your Test Cases" for examples in your language.


In Shell bash a1 and a2 are strings. The return is a string where words are separated by commas.


Beware: r must be without duplicates.

Don't mutate the inputs.


#87 Extract the IDs from the data set 



Complete the method so that it returns an array of all ID's passed in. The data structure will be similar to the following:


var data = {
  id: 1,
  items: [
    {id: 2},
    {id: 3, items: [
      {id: 4},
      {id: 5}
    ]}
  ]
}



extractIds(data) // should return [1,2,3,4,5]

The method should be able to handle the case of empty data being passed in.


Note: The only arrays that need to be traversed are those assigned to the "items" property.


find() vs children()


children() - 해당 노드의 자식들만 탐색

find() - 해당 노드의 자손들까지 탐색 ( 전체 DOM )


DOM 구조의 차이에 따라 성능 차이가 발생 할 수 있으나 유의미한 정도의 차이는 아닌 것 같다. 

상황에 맞게 선택해서 사용 하는 것이 좋을것 같다.



+ Recent posts