codewars练习记录14 js

[6 kyu] Data Reverse

A stream of data is received and needs to be reversed.

Each segment is 8 bits long, meaning the order of these segments needs to be reversed, for example:

11111111 00000000 00001111 10101010
(byte1) (byte2) (byte3) (byte4)
should become:
10101010 00001111 00000000 11111111
(byte4) (byte3) (byte2) (byte1)
The total number of bits will always be a multiple of 8.

The data is given in an array as such:

[1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0]

Note: In the C and NASM languages you are given the third parameter which is the number of segment blocks.
翻译:
接收到数据流,需要反转。
每个段长8位,这意味着这些段的顺序需要颠倒,例如:
11111111 00000000 00001111 10101010
(字节1)(字节2)(字节3)(字节4)
应变成:
10101010 00001111 00000000 11111111
(字节4)(字节3)(字节2)(字节1)
比特总数将始终是8的倍数。
数据以数组形式给出:
[1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0]
注:在C和NASM语言中,您会得到第三个参数,即段块的数量。
解一:

const dataReverse = data => {
  let arr = [];
  for (let i = 0; i < data.length; i += 8) {
    arr.unshift(...data.slice(i, i + 8));
  }
  return arr;
};

解二:

function dataReverse(data) {
  return (data.join('').match(/.{8}/g)||[]).reverse().join('').split('').map(n=>+n);
}
[6 kyu] Multiplication table

Your task, is to create NxN multiplication table, of size provided in parameter.

for example, when given size is 3:

1 2 3
2 4 6
3 6 9
for given example, the return value should be: [[1,2,3],[2,4,6],[3,6,9]]

翻译:
您的任务是创建参数中提供的NxN乘法表。
解:

multiplicationTable = function(size) {
  var result = [];
  for (var i = 0; i < size; i++) {
    result[i] = [];
    for(var j = 0; j < size; j++) {
      result[i][j] = (i + 1) * (j + 1);
    }
  }
  
  return result
}
[6 kyu] Consonant value

Given a lowercase string that has alphabetic characters only and no spaces, return the highest value of consonant substrings. Consonants are any letters of the alphabet except “aeiou”.

We shall assign the following values: a = 1, b = 2, c = 3, … z = 26.

For example,

for the word “zodiacs”, let’s cross out the vowels. We get: “z o d ia cs”
– The consonant substrings are: “z”, “d” and “cs” and the values are z = 26, d = 4 and cs = 3 + 19 = 22. The highest is 26.
solve(“zodiacs”) = 26

For the word “strength”, solve(“strength”) = 57

– The consonant substrings are: “str” and “ngth” with values “str” = 19 + 20 + 18 = 57 and “ngth” = 14 + 7 + 20 + 8 = 49. The highest is 57.

翻译:
给定一个只有字母字符且没有空格的小写字符串,返回辅音子字符串的最高值。辅音是字母表中除“aeiou”之外的任何字母。
我们将指定以下值:a=1,b=2,c=3,….z=26。
例如,对于单词“zodiacs”,让我们划掉元音。我们得到:“z o d ia cs”
–辅音子串为:“z”、“d”和“cs”,值为z=26、d=4和cs=3+19=22。最高值为26。
solve(“zodiacs”)=26
对于单词“strength”,solve(“strength)=57
–辅音子串为:“str”和“ngth”,值为“str“=19+20+18=57,”ngth“=14+7+20+8=49。最高值为57。
解:

function solve(s) {
  let arr = s.split(/[aeiou]/g).map(x => [...x].reduce((a, b) => a + b.charCodeAt() - 96, 0))
  return Math.max(...arr)
  };
[5 kyu] Rot13

ROT13 is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher.

Create a function that takes a string and returns the string ciphered with Rot13. If there are numbers or special characters included in the string, they should be returned as they are. Only letters from the latin/english alphabet should be shifted, like in the original Rot13 “implementation”.
翻译:
ROT13是一个简单的字母替换密码,用字母表中字母后面的13个字母替换字母。ROT13是凯撒密码的一个例子。
创建一个函数,该函数接受一个字符串并返回用Rot13加密的字符串。如果字符串中包含数字或特殊字符,则应按原样返回。只有拉丁/英语字母表中的字母才应该移位,就像在最初的Rot13“实现”中一样。
解一:

function rot13(message){
   var str1 = []
  for (var i = 0; i < message.length; i++) {
    var num = message[i].charCodeAt()
    if (num >= 97 && num <= 109 || num >= 65 && num <= 77)
      num = num + 13
    else if (num > 77 && num < 91 || num > 109 && num < 123)
      num = num - 13
    str1.push(String.fromCharCode(num))
  }
  return str1.join('')
}

解二:

function rot13(message) {
  var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  var b = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
  return message.replace(/[a-z]/gi, c => b[a.indexOf(c)])
}
[6 kyu] Fibonacci, Tribonacci and friends

If you have completed the Tribonacci sequence kata, you would know by now that mister Fibonacci has at least a bigger brother. If not, give it a quick look to get how things work.

Well, time to expand the family a little more: think of a Quadribonacci starting with a signature of 4 elements and each following element is the sum of the 4 previous, a Pentabonacci (well Cinquebonacci would probably sound a bit more italian, but it would also sound really awful) with a signature of 5 elements and each following element is the sum of the 5 previous, and so on.

Well, guess what? You have to build a Xbonacci function that takes a signature of X elements - and remember each next element is the sum of the last X elements - and returns the first n elements of the so seeded sequence.

xbonacci {1,1,1,1} 10 = {1,1,1,1,4,7,13,25,49,94}
xbonacci {0,0,0,0,1} 10 = {0,0,0,0,1,1,2,4,8,16}
xbonacci {1,0,0,0,0,0,1} 10 = {1,0,0,0,0,0,1,2,3,6}
xbonacci {1,1} produces the Fibonacci sequence

翻译:
如果你完成了Tribonacci序列的kata,你现在就知道Fibonacc先生至少有一个哥哥。如果没有,请快速查看它,了解其工作原理。

好了,是时候再扩展一下这个家族了:想想一个以4个元素的签名开始的四分之一波纳奇,后面的每个元素都是前面4个元素之和,一个五分之一波纳奇(Cinquebonacci可能听起来有点意大利语,但听起来也很糟糕),签名是5个元素,后面的每一个元素都是之前5个元素之总和,等等。

你猜怎么着?您必须构建一个Xbonacci函数,该函数接受X个元素的签名,并记住每个下一个元素都是最后X个元素之和,然后返回种子序列的前n个元素。
解一:

function Xbonacci(signature,n){
   let len = signature.length
  if (n <= len) {
    return signature.slice(0, n)
  }else {
    for (let i = 0; i < n - len; i++) {
      signature.push(signature.slice(i, signature.length).reduce((a, b) => a + b))
    }
    return signature
  }
}

解二:

const Xbonacci = (sig, n) => {
  let len = sig.length;
  for (let i = len; i < n; i++) 
    sig[i] = sig.slice(i - len).reduce((a, b) => a + b);
  return sig.slice(0, n);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值