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);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容