codewars练习记录25 js

[6 kyu] Dashatize it

Given a variable n,
If n is an integer, Return a string with dash’-'marks before and after each odd integer, but do not begin or end the string with a dash mark. If n is negative, then the negative sign should be removed.
If n is not an integer, return the string “NaN”.
Ex:
dashatize(274) -> ‘2-7-4’
dashatize(6815) -> ‘68-1-5’
翻译:
如果n是整数,则返回每个奇数整数前后带有短划线“-”标记的字符串,但不要以短划线开始或结束字符串。
如果n为负,则应去掉负号。
如果n不是整数,则返回字符串“NaN”。
例子:
dashatize(274)->“2-7-4”
dashatize(6815)->“68-1-5”
解:

function dashatize(num) {
  return String(num)
    .replace(/([13579])/g, "-$1-")
    .replace(/--+/g, "-")
    .replace(/(^-|-$)/g, "")
}

解:

function dashatize(num) {
  if (num % 1 !== 0) { return 'NaN' }
  num = Math.abs(num)
  num = num.toString().split('').map((x, i, y) => {
    return x % 2 !== 0 ? y[i - 1] % 2 == 0 ? '-' + x + '-' : x + '-' : x
  }
  ).join('')
  return num[num.length - 1] == '-' ? num.slice(0, num.length - 1) : num
}
[5 kyu] What’s a Perfect Power anyway?

A [perfect power] is a classification of positive integers:
In mathematics, a perfect power is a positive integer that can be expressed as an integer power of another positive integer. More formally, n is a perfect power if there exist natural numbers m > 1, and k > 1 such that mk = n.
Your task is to check wheter a given integer is a perfect power. If it is a perfect power, return a pair m and k with mk = n as a proof. Otherwise return Nothing, Nil, null, NULL, None or your language’s equivalent.
Note: For a perfect power, there might be several pairs. For example 81 = 3^4 = 9^2, so (3,4) and (9,2) are valid solutions. However, the tests take care of this, so if a number is a perfect power, return any pair that proves it.
Examples:

Test.describe(“perfect powers”, function(){
Test.it(“should work for some examples”,function(){
Test.assertSimilar(isPP(4), [2,2], “4 = 2^2”);
Test.assertSimilar(isPP(9), [3,2], “9 = 3^2”);
Test.assertEquals( isPP(5), null, “5 isn’t a perfect number”);
});
});

完美幂是正整数的分类:
在数学中,完美次方是一个正整数,可以表示为另一正整数的整数次方。更正式地说,如果存在自然数m>1,并且k>1使得mk=n,则n是完美幂。
您的任务是检查给定的整数是否为完美幂。如果是完美幂,则返回一对m和k,其中mk=n作为证明。否则返回Nothing、Nil、null、null、None或您的语言的等效值。
注意:对于完美的幂,可能有几个对。例如,81=34=92,所以(3,4)和(9,2)是有效的解。然而,测试会注意到这一点,所以如果一个数字是一个完美的幂,请返回任何一对证明它的数字。
解:

var isPP = function(n) {
  for (var m = 2; m * m <= n; ++ m)
    for (var k = 2; Math.pow(m, k) <= n; ++ k)
      if (Math.pow(m, k) == n) return [m, k];
  return null;
}

解:

var isPP = function(n){
   let m = Math.sqrt(n)
  for (let i = 2; i < m + 1; i++) {
    let k = 0
    while (i ** k < n) {
      k += 1
      if (i ** k == n) {
        return [i, k]
      }
    }
  }
  return null
}
[6 kyu] String transformer

Given a string, return a new string that has transformed based on the input:
Change case of every character, ie. lower case to upper case, upper case to lower case.
Reverse the order of words from the input.
Note: You will have to handle multiple spaces, and leading/trailing spaces.
For example:

“Example Input” ==> “iNPUT eXAMPLE”

You may assume the input only contain English alphabet and spaces.
给定一个字符串,返回一个基于输入转换的新字符串:
更改每个字符的大小写,即小写到大写,大写到小写。
颠倒输入的单词顺序。
注意:您必须处理多个空格和前导/尾随空格。
解:

function stringTransformer(str) {
  return str
    .split(' ')
    .reverse()
    .join(' ')
    .split('')
    .map(v => v == v.toUpperCase() ?
      v.toLowerCase() :
      v.toUpperCase())
    .join('');
}
[5 kyu] Integers: Recreation One

1, 246, 2, 123, 3, 82, 6, 41 are the divisors of number 246. Squaring these divisors we get: 1, 60516, 4, 15129, 9, 6724, 36, 1681. The sum of these squares is 84100 which is 290 * 290.
Task
Find all integers between m and n (m and n integers with 1 <= m <= n) such that the sum of their squared divisors is itself a square.
We will return an array of subarrays or of tuples (in C an array of Pair) or a string. The subarrays (or tuples or Pairs) will have two elements: first the number the squared divisors of which is a square and then the sum of the squared divisors.
Example:
list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
list_squared(42, 250) --> [[42, 2500], [246, 84100]]
The form of the examples may change according to the language, see “Sample Tests”.
Note
In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings.
1、246、2、123、3、82、6、41是数字246的除数。对这些除数进行平方,我们得到:1,60516,4,15129,9,6724,36,1681。这些平方的和是84100,即290*290。
任务
找到m和n之间的所有整数(m和n个整数,其中1<=m<=n),使得它们的平方因子之和本身就是一个平方。
我们将返回子数组或元组数组(在C中为Pair数组)或字符串。子阵列(或元组或对)将有两个元素:首先是其平方因子为平方的数,然后是平方因子的和。
例子:

列表平方(1250)–>[[1,1],[42,2500],[246,84100]]
列表平方(42250)–>[[422500],[24684100]]

示例的形式可能会因语言而异,请参见“样本测试”。
笔记
在Fortran中,与任何其他语言一样,返回的字符串不允许包含任何多余的尾随空格:您可以使用动态分配的字符串。
解:

function listSquared(m, n) {
  var arr = [];
  for (var i = m; i <= n; i++){
    var temp = 0;
    for (var j = 1; j <= i; j++) {
      if ( i % j == 0) temp += j*j;  
    };
    if ( Math.sqrt(temp) % 1 == 0) arr.push([i, temp]);
  };
  return arr;
}
[6 kyu] Bit Counting

Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative.

Example:

The binary representation of 1234 is 10011010010, so the function should return 5 in this case

翻译:
编写一个以整数为输入的函数,并返回该数字的二进制表示中等于1的位数。您可以保证输入是非负的。
示例:1234的二进制表示是10011010010,因此在这种情况下,函数应该返回5
解:

var countBits = function(n) {
 return  n.toString(2).split('0').join('').length
};

解:

var countBits = function(n) {
  var count = 0
  while (n !== 0) {
    count++
    n = (n - 1) & n
  }
  return count 
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值