codewars練習(xí)記錄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.
翻譯:
接收到數(shù)據(jù)流,需要反轉(zhuǎn)。
每個(gè)段長(zhǎng)8位,這意味著這些段的順序需要顛倒,例如:
11111111 00000000 00001111 10101010
(字節(jié)1)(字節(jié)2)(字節(jié)3)(字節(jié)4)
應(yīng)變成:
10101010 00001111 00000000 11111111
(字節(jié)4)(字節(jié)3)(字節(jié)2)(字節(jié)1)
比特總數(shù)將始終是8的倍數(shù)。
數(shù)據(jù)以數(shù)組形式給出:
[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語言中,您會(huì)得到第三個(gè)參數(shù),即段塊的數(shù)量。
解一:

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]]

翻譯:
您的任務(wù)是創(chuàng)建參數(shù)中提供的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.

翻譯:
給定一個(gè)只有字母字符且沒有空格的小寫字符串,返回輔音子字符串的最高值。輔音是字母表中除“aeiou”之外的任何字母。
我們將指定以下值:a=1,b=2,c=3,….z=26。
例如,對(duì)于單詞“zodiacs”,讓我們劃掉元音。我們得到:“z o d ia cs”
--輔音子串為:“z”、“d”和“cs”,值為z=26、d=4和cs=3+19=22。最高值為26。
solve(“zodiacs”)=26
對(duì)于單詞“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是一個(gè)簡(jiǎn)單的字母替換密碼,用字母表中字母后面的13個(gè)字母替換字母。ROT13是凱撒密碼的一個(gè)例子。
創(chuàng)建一個(gè)函數(shù),該函數(shù)接受一個(gè)字符串并返回用Rot13加密的字符串。如果字符串中包含數(shù)字或特殊字符,則應(yīng)按原樣返回。只有拉丁/英語字母表中的字母才應(yīng)該移位,就像在最初的Rot13“實(shí)現(xiàn)”中一樣。
解一:

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,你現(xiàn)在就知道Fibonacc先生至少有一個(gè)哥哥。如果沒有,請(qǐng)快速查看它,了解其工作原理。

好了,是時(shí)候再擴(kuò)展一下這個(gè)家族了:想想一個(gè)以4個(gè)元素的簽名開始的四分之一波納奇,后面的每個(gè)元素都是前面4個(gè)元素之和,一個(gè)五分之一波納奇(Cinquebonacci可能聽起來有點(diǎn)意大利語,但聽起來也很糟糕),簽名是5個(gè)元素,后面的每一個(gè)元素都是之前5個(gè)元素之總和,等等。

你猜怎么著?您必須構(gòu)建一個(gè)Xbonacci函數(shù),該函數(shù)接受X個(gè)元素的簽名,并記住每個(gè)下一個(gè)元素都是最后X個(gè)元素之和,然后返回種子序列的前n個(gè)元素。
解一:

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);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容