它们是“相同的"吗?代码大战 [英] Are they the "same"? CodeWars

查看:25
本文介绍了它们是“相同的"吗?代码大战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是完整的问题描述

给定两个数组 a 和 b,编写一个函数 comp(a, b)(Clojure 中的 compSame(a, b))检查这两个数组是否具有相同"元素,具有相同的多重性.这里的相同"意味着b中的元素是平方中的元素,无论顺序如何.

Given two arrays a and b write a function comp(a, b) (compSame(a, b) in Clojure) that checks whether the two arrays have the "same" elements, with the same multiplicities. "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.

例子

有效数组

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

comp(a, b) 返回 true 因为在 b 中:

comp(a, b) returns true because in b:

  1. 121 是 11 的平方,
  2. 14641 是 121 的平方,
  3. 20736 144 的平方,
  4. 361 19 的平方,
  5. 25921 161 的平方,以此类推.

如果我们把 b 的元素写成正方形就很明显了:

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]

如果我们将第一个数字更改为其他数字,comp 可能不再返回 true:

If we change the first number to something else, comp may not return true anymore:

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

comp(a,b) 返回 false 因为在 b 中,132 不是任意数量的 a.

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) 返回 false 因为在 b 中,36100 不是任意数量 a 的平方.

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

备注

  1. ab 可能是 [](所有语言).
  2. ab 可能是 nilnullNone(除了在 Haskell、Elixir、C++、Rust 中).
  3. 如果 abnil(或 null 或 None),则问题没有意义,因此返回 false.
  4. 如果 abempty,则结果本身就很明显.
  1. a or b might be [] (all languages).
  2. a or b might be nil or null or None (except in Haskell, Elixir, C++, Rust).
  3. If a or b are nil (or null or None), the problem doesn't make sense so return false.
  4. If a or b are empty the result is evident by itself.

C 注释

  1. 这两个数组具有相同的大小 (> 0) 作为函数 comp 中的参数.

我的问题:

你能想出一个我不符合要求的测试用例吗规格??

Can you come up with a test case where I do not meet the desired specefications??

我卡在 1 项基本测试未通过(预计结果:真,但我的代码返回假)

I am stuck on 1 basic test not being passed (expected result: true but my code returns false)

我的代码尝试

function isTrue(el){
  return el === true;
}

function comp(array1, array2){
  if(array1.length === 0 || array2.length === 0){
    return false;
  }
  var arr = array1.map(function(num){return num*num});
  var arr2 = [];
  for(var i = 0; i < arr.length; i++){
    if(array2.includes(arr[i])){
      arr2.push(true);
      var a = array2.indexOf(arr[i]);
     array2.splice(a,1);
    } else{
      arr2.push(false);
    }
  }
  return arr2.includes(false) ? false : true;
}

推荐答案

最简单的方法:

const comp = (a1, a2) => {
  if (!a1 || !a2 || a1.length !== a2.length) return false;
  return a1.map(x => x * x).sort().toString() === a2.sort().toString();
}

这篇关于它们是“相同的"吗?代码大战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆