文章目录

  • [6 kyu] Simple Encryption #1 - Alternating Split
  • [6 kyu] Kebabize
  • [6 kyu] Decipher this!
  • [6 kyu] A Rule of Divisibility by 13
  • [6 kyu] Array Helpers
[6 kyu] Simple Encryption #1 - Alternating Split

Implement a pseudo-encryption algorithm which given a string S and an integer N concatenates all the odd-indexed characters of S with all the even-indexed characters of S, this process should be repeated N times.

Examples:

encrypt(“012345”, 1) => “135024”
encrypt(“012345”, 2) => “135024” -> “304152”
encrypt(“012345”, 3) => “135024” -> “304152” -> “012345”
encrypt(“01234”, 1) => “13024”
encrypt(“01234”, 2) => “13024” -> “32104”
encrypt(“01234”, 3) => “13024” -> “32104” -> “20314”

Together with the encryption function, you should also implement a decryption function which reverses the process.

If the string S is an empty value or the integer N is not positive, return the first argument without changes.
翻译:
实现一个伪加密算法,给定一个字符串S和一个整数N,将S的所有奇数索引字符与S的所有偶数索引字符连接起来,这个过程应该重复N次。

与加密函数一起,您还应该实现一个解密函数,该函数可以反转过程。
如果字符串S是空值或整数N不是正数,则返回第一个参数而不进行更改。
解一:

function encrypt (text, n) {if (text == null) return nullfor (let k = 0; k < n; k++) {let odd = text.split('').filter((x, i) => i % 2 != 0)let even = text.split('').filter((x, i) => i % 2 == 0)text = odd.concat(even).join('')}return text
}function decrypt (encryptedText, n) {if (!encryptedText) {return encryptedText}var char = encryptedText.split('')for (let i = 0; i < n; i++) {var odd = char.slice(0, char.length / 2)var even = char.slice(char.length / 2)char = []while (odd.length || even.length) {if (even.length) {char.push(even.shift())}if (odd.length) {char.push(odd.shift())}}}return char.join('')
}

解二:

function encrypt(text, n) {for (let i = 0; i < n; i++) {text = text && text.replace(/.(.|$)/g, '$1') + text.replace(/(.)./g, '$1') }return text
}function decrypt(text, n) {let l = text && text.length / 2 | 0for (let i = 0; i < n; i++) {text = text.slice(l).replace(/./g, (_, i) => _ + (i < l ? text[i] : ''))}return text
}
[6 kyu] Kebabize

Modify the kebabize function so that it converts a camel case string into a kebab case.

kebabize(‘camelsHaveThreeHumps’) // camels-have-three-humps
kebabize(‘camelsHave3Humps’) // camels-have-humps

翻译:
修改kebabize函数,使其将camel大小写字符串转换为kebab大小写。
解一:

function kebabize(str) {let newstr = str.replace(/[^a-zA-Z]/g, '') // 将所有非字母元素删掉for (let i = 0; i < newstr.length; i++) {if (newstr[i] >= 'A' && newstr[i] <= 'Z')newstr = newstr.replace(newstr[i], '-' + newstr[i].toLowerCase())else if (newstr[i] >= 'a' && newstr[i] <= 'z')continueelsenewstr = newstr.replace(newstr[i], '')}if (newstr[0] == '-') newstr = newstr.substring(1)return newstr
}

解二:

function kebabize(str) {return str.replace(/[^a-z]/ig, '').replace(/^[A-Z]/, c => c.toLowerCase()).replace(/[A-Z]/g, c => `-${c.toLowerCase()}`);
}
[6 kyu] Decipher this!

You are given a secret message you need to decipher. Here are the things you need to know to decipher it:
For each word:
the second and the last letter is switched (e.g. Hello becomes Holle)
the first letter is replaced by its character code (e.g. H becomes 72)
Note: there are no special characters used, only letters and spaces
Examples

decipherThis(‘72olle 103doo 100ya’); // ‘Hello good day’
decipherThis(‘82yade 115te 103o’); // ‘Ready set go’

翻译:
你会收到一条需要破译的秘密信息。以下是你需要知道的事情来破译它:
对于每个单词:
切换第二个和最后一个字母(例如Hello变成Holle)
第一个字母被其字符代码替换(例如H变为72)
注意:没有使用特殊字符,只有字母和空格
解一:

function decipherThis(str) {let brr = str.split(' ').map(x => x.replace(x.replace(/[^\d]/g, ''), String.fromCharCode(x.replace(/[^\d]/g, ''))))  //字符替换return brr.map(function (y) {  //交换位置let b = y.split('')let temp = b[1]b[1] = b[b.length - 1]b[b.length - 1] = tempreturn b.join('')}).join(' ')
};

解二:

function decipherThis(str) {return str.split(" ").map(w =>w.replace(/^\d+/, c => String.fromCharCode(c)).replace(/^(.)(.)(.*)(.)$/, "$1$4$3$2")).join(" ")
}
[6 kyu] A Rule of Divisibility by 13

From Wikipedia:

“A divisibility rule is a shorthand way of determining whether a given integer is divisible by a fixed divisor without performing the division, usually by examining its digits.”

When you divide the successive powers of 10 by 13 you get the following remainders of the integer divisions:

1, 10, 9, 12, 3, 4 because:

10 ^ 0 ->  1 (mod 13)
10 ^ 1 -> 10 (mod 13)
10 ^ 2 ->  9 (mod 13)
10 ^ 3 -> 12 (mod 13)
10 ^ 4 ->  3 (mod 13)
10 ^ 5 ->  4 (mod 13)

Then the whole pattern repeats. Hence the following method:

Multiply

  • the right most digit of the number with the left most number in the sequence shown above,
  • the second right most digit with the second left most digit of the number in the sequence.

The cycle goes on and you sum all these products. Repeat this process until the sequence of sums is stationary.

Example:

What is the remainder when 1234567 is divided by 13?

7      6     5      4     3     2     1  (digits of 1234567 from the right)
×      ×     ×      ×     ×     ×     ×  (multiplication)
1     10     9     12     3     4     1  (the repeating sequence)

Therefore following the method we get:

7×1 + 6×10 + 5×9 + 4×12 + 3×3 + 2×4 + 1×1 = 178

We repeat the process with the number 178:

8x1 + 7x10 + 1x9 = 87

and again with 87:

7x1 + 8x10 = 87

From now on the sequence is stationary (we always get 87) and the remainder of 1234567 by 13 is the same as the remainder of 87 by 13 ( i.e 9).

Task:

Call thirt the function which processes this sequence of operations on an integer n (>=0). thirt will return the stationary number.

thirt(1234567) calculates 178, then 87, then 87 and returns 87.

thirt(321) calculates 48, 48 and returns 48
(个人理解)大意就是将例数(例如:1234567)的倒序(7654321)的每一位数和 数列1,10,9,12,3,4,1 (该数列由10的连续幂除以13取模 则取余数所得 固定的)的每一位数相乘再相加,再循环操作所得结果,直到循环前后结果相同,输出最终值
解:

function thirt(n) {var arr=[1,10,9,12,3,4]while(n>=100){var sum = nn=0for(let i=0;sum!=0;n=n+Math.floor(sum%10) * arr[i%6],sum=sum/10,i++);}return n
}

解:

function thirt(n) {const nums = [1,10,9,12,3,4]var sum = (''+n).split('').reverse().reduce((sum,v,i) => sum + v * nums[i%nums.length], 0)return sum === n ? n : thirt(sum)
}
[6 kyu] Array Helpers

This kata is designed to test your ability to extend the functionality of built-in classes. In this case, we want you to extend the built-in Array class with the following methods: square(), cube(), average(), sum(), even() and odd().

Explanation:

square() must return a copy of the array, containing all values squared
cube() must return a copy of the array, containing all values cubed
average() must return the average of all array values; on an empty array must return NaN (note: the empty array is not tested in Ruby!)
sum() must return the sum of all array values
even() must return an array of all even numbers
odd() must return an array of all odd numbers
Note: the original array must not be changed in any case!

Example

var numbers = [1, 2, 3, 4, 5];
numbers.square(); // must return [1, 4, 9, 16, 25]
numbers.cube(); // must return [1, 8, 27, 64, 125]
numbers.average(); // must return 3
numbers.sum(); // must return 15
numbers.even(); // must return [2, 4]
numbers.odd(); // must return [1, 3, 5]

翻译:
说明:
此kata旨在测试您扩展内置类功能的能力。在本例中,我们希望您使用以下方法扩展内置Array类:square()、cube()、average()、sum()、even()和odd()。
说明:
square()必须返回数组的副本,其中包含所有平方值
cube()必须返回数组的副本,其中包含所有立方值
average()必须返回所有数组值的平均值;在空数组上必须返回NaN(注意:空数组未在Ruby中测试!)
sum()必须返回所有数组值的总和
even()必须返回所有偶数的数组
odd()必须返回所有奇数的数组
注意:在任何情况下都不能更改原始数组!
解:

Object.assign(Array.prototype, {square() {return this.map(n => n * n);},cube() {return this.map(n => Math.pow(n, 3));},sum() {return this.reduce((p,n) => p + n, 0);},average() {return this.reduce((p,n) => p + n, 0) / this.length;},even() {return this.filter(n => !(n % 2));},odd() {return this.filter(n => n % 2);}
});

解:

Array.prototype.square  = function () { return this.map(function(n) { return n*n; }); }
Array.prototype.cube    = function () { return this.map(function(n) { return n*n*n; }); }
Array.prototype.average = function () { return this.sum() / this.length; }
Array.prototype.sum     = function () { return this.reduce(function(a, b) { return a + b; }, 0); }
Array.prototype.even    = function () { return this.filter(function(item) { return 0 == item % 2; }); }
Array.prototype.odd     = function () { return this.filter(function(item) { return 0 != item % 2; }); }

codewars练习记录20 js相关推荐

  1. codewars练习记录8 js

    目录 [7 kyu]Exes and Ohs [8 kyu]Do you speak "English"? [8 kyu]Remove First and Last Charact ...

  2. 记录一些js面试题以及解法

    记录一些js面试题,解法,感悟. 1.将字符串"aaaabbbbcccc"转换"4a4b4c" 解法1 var str = "aaaabbbbcccc ...

  3. 计算机论文指导记录范本,论文指导内容记录怎么写 3篇 论文指导记录20篇

    论文指导内容记录怎么写 3篇 论文指导记录20篇 论文指导内容记录怎么写 3篇 论文指导记录20篇精品文档,仅供参考论文指导内容记录怎么写 3篇 论文指导记录20篇指导是一个汉语词语,读音为zhdo, ...

  4. 使用 Winston 和 Log4js 记录 Node.js 应用程序

    了解 Node.js 日志记录器并开始使用两个最受欢迎的 Node 日志包 运行服务器端应用程序时,会发生很多事件,其中大多数事件都会写入日志文件中.日志文件可能是磁盘上文件的集合,也可能是数据库中的 ...

  5. JS笔记(20): JS中的同步编程和异步编程

    铺垫:关于定时器 定时器:设定一个定时器,并且设定了等到的时间,当到达指定的时间,浏览器会把对应的方法执行 1)常用的定时器 1.setTimeout(function,intarval) 执行一次 ...

  6. 2015-04-22记录--一些JS疑惑

    记录一:变量的作用域:变量的作用域由函数限定,要么是全局的,要么是局部的.全局的变量处处都可以访问,局部变量只有在声明他的地方才可以访问.如果在函数中声明的局部变量忘记了些var关键字,那么就创建了全 ...

  7. vue实现查询多条记录_vue.js 实现天气查询

    效果预览:http://songothao.gitee.io/weather_query_based_on_vuejs/ 项目已上传码云:叁贰壹/vuejs实现天气查询 知乎视频​www.zhihu. ...

  8. [Javascript 高级程序设计]学习心得记录9 js面向对象

    感觉最难的部分就是面向对象了,大学期间学习的是面向过程的c/c++,工作之后也没有深入了解过面向对象,通过这次的学习和回顾,也算是对面向对象有了新的认识.不过,就我在书上学到了结合个人理解随便说说,很 ...

  9. [Javascript 高级程序设计]学习心得记录10 js函数表达式

    在前面说对象的时候已经提到了函数对象,对函数的定义参数的传递包括通过argumentd.callee实现递归.这篇博客我会继续深入讲解js中的函数表达式. 一,闭包 关于闭包的概念,可以先看看http ...

最新文章

  1. 部署war包到阿里云liunx的tomcat时报错:zip END header not found
  2. C语言用char数组存储一串整数时的一个陷阱
  3. [原]一步一步自己制作弹出框
  4. 电视剧潜伏的真正结局
  5. oracle 批量插入 mysql 区别,Oracle 和 mysql 的批量操作Sql语句 的区别
  6. 计算机应用属于工程与工程经济类,计算机及信息技术和电子技术应用哪种属于工程类或工程经济类专业?...
  7. Devpress.XtraGrid.GridControl.GridView属性
  8. 关于动态语言 静态语言 静态类型语言 动态类型语言的区别
  9. OpenCV 实现分水岭算法
  10. 蓝桥杯历年真题及答案汇总整理(Java、C语言、Python)
  11. Android开发笔记(一百四十一)读取PPT和PDF文件
  12. Marlin固件 ---- G_Code 命令解析
  13. 基于站长之家(CNZZ)的网站流量统计分析 (以vue代码为例)
  14. MXNet网络模型(四)GAN神经网络
  15. .frm mysql_mysqlfrm使用
  16. 新版Vue项目配置项目名称-publicPath-前端_v1.0.2
  17. linux虚拟机之ubuntu的软件包管理(6/10)
  18. 抖音直播行为规范考试
  19. 昇腾 (Ascend) AI 处理器:达芬奇架构
  20. 2020年腾讯实习生算法笔试题目(感触良多)

热门文章

  1. 【资源】部分稀有资源
  2. 2022年3月17日美国Embarcadero公司正式发布 RAD Studio Delphi 11.1 Alexandria
  3. 剑指offer系列-代码的鲁棒性
  4. Swoole 使用WSS协议无法正常连接
  5. Redhat 7.2操作系统上Oracle 12C R2 RAC安装配置
  6. 张宇1000题高等数学 第十四章 二重积分
  7. 色素疤痕应该如何修复?(色素沉着疤痕怎么修复)
  8. 怎么搭建个人私有网盘(高速企业网盘源码平台搭建教程)
  9. 【go语言】两个翻译引擎来提高响应速度
  10. Java —— 打印杨辉三角形