본문 바로가기
알고리즘/Codewars

[Codewars] Are they the "same"? (6 kyu) / JavaScript

by fluss 2022. 10. 13.

https://www.codewars.com/kata/550498447451fbbd7600041c

 

Codewars - Achieve mastery through coding practice and developer mentorship

Coding practice for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Given two arrays a and b write a function comp(a, b) (or compSame(a, b)) that checks whether the two arrays have the "same" elements, with the same multiplicities (the multiplicity of a member is the number of times it appears). "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.

 

Examples

Valid arrays
a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:

a = [121, 144, 19, 161, 19, 144, 19, 11] 
b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]
Invalid arrays

If, for example, we change the first number to something else, comp is not returning true anymore:

a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 132 is not the square of any number of a.

a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 36100 is not the square of any number of a.

 

Remarks

  • a or b might be [] or {} (all languages except R, Shell).
  • a or b might be nil or null or None or nothing (except in C++, COBOL, Crystal, D, Dart, Elixir, Fortran, F#, Haskell, Nim, OCaml, Pascal, Perl, PowerShell, Prolog, PureScript, R, Racket, Rust, Shell, Swift).

If a or b are nil (or null or None, depending on the language), the problem doesn't make sense so return false.

 

Note for C

The two arrays have the same size (> 0) given as parameter in function comp.

 

설명:

주어진 두 배열 a, b이 같은 중복도(멤버의 중복도는 그것이 나타나는 횟수)를 가진 "같은" 원소인지 확인하는 함수 comp(a, b)(또는 compSame(a, b))를 작성하세요. 여기에서 "같은"의 의미는 b의 원소가 순서에 관계없이 제곱된 원소라는 것을 의미합니다. 

예시

유효한 배열

 

a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a, b)는 b에서 121는 11의 제곱이고, 14641는 121의 제곱, 20736는 144의 제곱, 361는 19의 제곱, 25921는 161의 제곱 등이기 때문에 true를 반환한다. b의 요소를 제곱으로 쓰면 분명해집니다:

a = [121, 144, 19, 161, 19, 144, 19, 11] 
b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]

 

유효하지 않은 배열

만약 예를 들어 첫 번째 숫자를 다른 것으로 바꾼다면, comp는 더 이상 true를 반환하지 않습니다:

a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a, b)는 132가 a의 어떤 숫자의 제곱도 아니므로 false를 반환합니다.

a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]

comp(a, b)는 36100이 a의 어떤 숫자의 제곱도 아니므로 false를 반환합니다.

 

주목

  • a 또는 b [] 또는 {}일수있습니다. (R, Shell을 제외한 모든 언어에서).
  • a 또는 b nil 또는 null 또는 None 또는 nothing일수 있습니다. (C++, COBOL, Crystal, D, Dart, Elixir, Fortran, F#, Haskell, Nim, OCaml, Pascal, Perl, PowerShell, Prolog, PureScript, R, Racket, Rust, Shell, Swift 제외).

만약 a 또는 b nil (언어에 따라 null 또는 None)이라면, 문제가 말이 되지 않으므로 false를 반환합니다.

 

C를 위한 참고

함수 comp의 매개변수로 주어진 두 배열은 같은 크기(> 0)입니다.

 

풀이

function comp(array1, array2){
  if(!array1 || !array2) return false;
  array1.sort((a, b) => a - b);
  array2.sort((a, b) => a - b);
  for(let i = 0; i < array1.length; i++){
    if(array2[i] !== Math.pow(array1[i], 2)) return false;
  }
  return true;
}

array1이나 array2가 null일 경우 false를 반환한다. 그리고 array1과 array2를 작은 수부터 정렬한다. array1의 길이만큼 반복문을 돌면서 array2[i]의 값이 array[i]의 값의  제곱과 같은지 확인하고 그렇지 않다면 false를 반환, 그리고 모두 같다면 true를 반환한다.

다른 사람의 좋았던 풀이

function comp(array1, array2) {
  if(array1 == null || array2 == null) return false;
  array1.sort((a, b) => a - b); array2.sort((a, b) => a - b);
  return array1.map(v => v * v).every((v, i) => v == array2[i]);
}

every로 배열안의 모든 요소가 주어진 조건을 통과하는지 확인하고 수행 결과를 반환한다.

 

참고

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/every

 

Array.prototype.every() - JavaScript | MDN

every() 메서드는 배열 안의 모든 요소가 주어진 판별 함수를 통과하는지 테스트합니다. Boolean 값을 반환합니다.

developer.mozilla.org

 

댓글