#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

#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

#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 구조의 차이에 따라 성능 차이가 발생 할 수 있으나 유의미한 정도의 차이는 아닌 것 같다. 

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



#66 Inside Out Strings


You are given a string of words (x), for each word within the string you need to turn the word 'inside out'. By this I mean the internal letters will move out, and the external letters move toward the centre.


If the word is even length, all letters will move. If the length is odd, you are expected to leave the 'middle' letter of the word where it is.


An example should clarify:


'taxi' would become 'atix' 'taxis' would become 'atxsi'



#65 Split Strings 


Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').


Examples:


solution('abc') // should return ['ab', 'c_']

solution('abcdef') // should return ['ab', 'cd', 'ef']



#64 Strip Comments


Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out.


Example:


Given an input string of:


apples, pears # and bananas

grapes

bananas !apples

The output expected would be:


apples, pears

grapes

bananas

The code would be called like so:


var result = solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"])

// result should == "apples, pears\ngrapes\nbananas"




+ Recent posts