[关闭]
@lwxyfer 2015-08-05T08:03:26.000000Z 字数 8991 阅读 2919

CodeWars练习与笔记

记录一些有趣的问题,好玩的问题。


1. Description:

You must create a function, spread, that takes a function and a list of arguments to be applied to that function. You must make this function return the result of calling the given function/lambda with the given arguments.

eg:

  1. spread(someFunction, [1, true, "Foo", "bar"] )
  2. // is the same as...
  3. someFunction(1, true, "Foo", "bar")

solve:

  1. function spread(func, args) {
  2. return func.apply(null, args);
  3. }

konwledge:

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply MDN
Function.prototype.apply()

syntax:

fun.apply(thisArg[, argsArray])

2.运算符用法

  1. return result > 0 ? "Battle Result: Good triumphs over Evil" :
  2. result < 0 ? "Battle Result: Evil eradicates all trace of Good" :
  3. "Battle Result: No victor on this battle field";

三元运算符扩展 con0 ? res0 : con1 ? res1 : res2;

关于运算符的嵌套与扩展

3.Description:

You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.

solution:

  1. function isValidWalk(walk) {
  2. var dx = 0
  3. var dy = 0
  4. var dt = walk.length
  5. for (var i = 0; i < walk.length; i++) {
  6. switch (walk[i]) {
  7. case 'n': dy--; break
  8. case 's': dy++; break
  9. case 'w': dx--; break
  10. case 'e': dx++; break
  11. }
  12. }
  13. return dt === 10 && dx === 0 && dy === 0
  14. }

这题有点意思,开始没反应过来,原点与坐标轴。
上面解法最简单了,还很多有趣的在:
http://www.codewars.com/kata/take-a-ten-minute-walk/solutions/javascript

4.Description:

In this kata, you should calculate type of triangle with three given sides a, b and c (given in any order).

Input parameters are sides of given triangle. All input values are non-negative floating point or integer numbers (or both).

Obtuse
Examples:

  1. triangleType(2, 4, 6); // return 0 (Not triangle)
  2. triangleType(8, 5, 7); // return 1 (Acute, angles are approx. 82°, 38° and 60°)
  3. triangleType(3, 4, 5); // return 2 (Right, angles are approx. 37°, 53° and exactly 90°)
  4. triangleType(7, 12, 8); // return 3 (Obtuse, angles are approx. 34°, 106° and 40°)

刚开始绕了一个圈子,本来对的,又错了,使用三元只是获得了最大值,还是要用数组的sort()来排序。
然后我使用arguments.sort()来排序,这是一个错误,arguments是一个类数组对象,不是数组,所以不能用sort()方法
可以用[a,b,c].sort(),而我又创建新的数组,就变得麻烦了。

solution:

  1. function triangleType(a, b, c){
  2. var cmp, sides = [a, b, c].sort(function (a, b) { return a - b });
  3. a = sides[0]; b = sides[1]; c = sides[2];
  4. if (a && a + b > c) {
  5. cmp = a * a + b * b - c * c;
  6. if (cmp > 0) return 1;
  7. if (cmp == 0) return 2;
  8. if (cmp < 0) return 3;
  9. }
  10. return 0;
  11. }

codewars:http://www.codewars.com/kata/53907ac3cd51b69f790006c5/solutions/javascript

MDN : https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/arguments

5.Description:

You're working in a number zoo, and it seems that one of the numbers has gone missing!

Zoo workers have no idea what number is missing, and are too incompetent to figure it out, so they're hiring you to do it for them.

In case the zoo loses another number, they want your program to work regardless of how many numbers the zoo has in total.

Write the function findNumber(array) that takes an array of numbers. The numbers will be unsorted values between 1 and one more than the length of the array. No values will be repeated within the array. Return the number that is missing.

Examples:

findNumber( [1,3] ) => 2

findNumber( [2,3,4] ) => 1

findNumber( [13,11,10,3,2,1,4,5,6,9,7,8] ) => 12

此题比较简单,但一个好方法才是重要的啦。

solution:

  1. function findNumber(arr) {
  2. for(var i = 1; i < arr.length+1; i+=1){
  3. if(arr.indexOf(i) < 0) return i;
  4. }
  5. }

indexOF()方法。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

6.Description:

[http://www.codewars.com/kata/christmas-tree/solutions?show-solutions=1][1]

Create a function christmasTree(height) that returns a christmas tree of the correct height

christmasTree(5) should return:

  1. -
  2. *
  3. ***
  4. *****
  5. *******
  6. *********

Height passed is always an integer between 0 and 100.

Use \n for newlines between each line.

Pad with spaces so each line is the same length. The last line having only stars, no spaces.

solution:

  1. function christmasTree(height) {
  2. var tree = [];
  3. for(var i = 1; i <= height; i++) {
  4. tree.push(" ".repeat(height - i) + "*".repeat((i - 1) * 2 + 1) + " ".repeat(height - i));
  5. }
  6. return tree.join("\n");
  7. }
  8. String.prototype.repeat = function(n)
  9. {
  10. return new Array(n+1).join(this);
  11. }

此方法使用了str.repeat(count)
注意这里的String.prototype.repeat 属于ECMAScrip 6新增的。
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
此方法直接把所有的'*'和空格都放到一个数字,然后数组的join()输出tree。
还是是push的利用。

7.Description:

Take the following IPv4 address: 128.32.10.1 This address has 4 octets where each octet is a single byte (or 8 bits).

1st octet 128 has the binary representation: 10000000
2nd octet 32 has the binary representation: 00100000
3rd octet 10 has the binary representation: 00001010
4th octet 1 has the binary representation: 00000001
So 128.32.10.1 == 10000000.00100000.00001010.00000001

Because the above IP address has 32 bits, we can represent it as the 32 bit number: 2149583361.

Write a function ip_to_int32(ip) ( JS: ipToInt32(ip) ) that takes an IPv4 address and returns a 32 bit number.

ipToInt32("128.32.10.1") => 2149583361

此题比较简单吧。主要是看别人clever的解法。

solution:

  1. function ipToInt32(ip){
  2. return ip.split(".").reduce(function(int,v){ return int*256 + +v } )
  3. }

1.reduce: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
2.里面的 + +v

8.Description:

http://www.codewars.com/kata/525f50e3b73515a6db000b83/solutions/javascript

Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.

Example:
createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) => returns "(123) 456-7890"

The returned format must be correct in order to complete this challenge.
Don't forget the space after the closing parenthese!

solution:

此题感觉没啥重点的啦。不过主要是看别人的解法啦。
1.String.prototype.substring()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
还可以用: String.prototype.substr()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
注意两者的区别!

  1. function createPhoneNumber(numbers){
  2. numbers = numbers.join('');
  3. return '(' + numbers.substring(0, 3) + ') '
  4. + numbers.substring(3, 6)
  5. + '-'
  6. + numbers.substring(6);
  7. }

2.看这个就感觉正则必须会啊
String.prototype.replace()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

  1. function createPhoneNumber(numbers){
  2. return numbers.join('').replace(/(...)(...)(.*)/, '($1) $2-$3');
  3. }

全部都是基本语法的熟练使用。

9.Description:

http://www.codewars.com/kata/526571aae218b8ee490006f4/solutions/javascript

Write a function that takes an (unsigned) integer as input, and returns the number of bits that are equal to one in the binary representation of that number.

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

solution:

1.秒啊,秒啊。

  1. function countBits(n) {
  2. for(c=0;n;n>>=1)c+=n&1
  3. return c;
  4. }

2.

  1. var countBits = function(n)
  2. {
  3. a = n.toString(2).match(/1/g);
  4. return a == null ? 0 : a.length;
  5. };

3.我的

  1. var countBits = function(n) {
  2. var num = n.toString(2);
  3. var j = 0;
  4. for ( var i=0 ;i <num.length; i++) {
  5. if( num.charAt(i) == 1 ) {
  6. j = j+1;
  7. }
  8. }
  9. return j;
  10. };

看到第一种解法,简直就是绝了。巧妙的利用操作符啊。
而第二种则是使用正则,属于普通的解法,但也是我喜欢的解法。
而看我的,是最普通的解法。按步骤一步步来。

经搜索此题比较出名,而且第一种做法通过不同语言得到普遍应用。

此题点:
1. 操作符,
2. 正则
3. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Number/toString

10.Description:

http://www.codewars.com/kata/can-you-keep-a-secret/solutions?show-solutions=1

There's no such thing as private properties on a javascript object! But, maybe there are?

Implement a function createSecretHolder(secret) which accepts any value as secret and returns an object with ONLY two methods

  1. getSecret() which returns the secret
  2. setSecret() which sets the secret
  3. var obj = createSecretHolder(5);
  4. obj.getSecret(); // returns 5
  5. obj.setSecret(2);
  6. obj.getSecret(); // returns 2

solution:

  1. function createSecretHolder(secret) {
  2. return {
  3. getSecret: function() { return secret; },
  4. setSecret: function(v) { secret = v; }
  5. };
  6. }

从题看是对象的私有属性。
此题对我来说关键点比较多:
对象,函数,私有。

此题Mark

11.Description:

http://www.codewars.com/kata/54e6533c92449cc251001667/solutions/javascript

Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.

For example:

  1. uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
  2. uniqueInOrder('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
  3. uniqueInOrder([1,2,2,3,3]) == [1,2,3]

solution:

  1. var uniqueInOrder = function (iterable)
  2. {
  3. return [].filter.call(iterable, (function (a, i) { return iterable[i - 1] !== a }));
  4. }

上面是最简单的解法了。正是非常需要学习的。
一般解法与我自己的解法相似。

  1. var uniqueInOrder=function(it){
  2. if ( typeof it == "string" ) {
  3. var it = it.split("")
  4. }
  5. var arr = new Array();
  6. var j=0;
  7. for ( var i=0; i< it.length;i++) {
  8. if( it[i] !== it[i+1]) {
  9. arr[j] = it[i];
  10. j = j+1;
  11. }
  12. }
  13. return arr;
  14. }

里面改为:arr.push(it[i])就更简洁了。

解法知识点:
Array.prototype.filter()
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
Function.prototype.call():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
重点在于理解call,这很关键,涉及函数的构造和调用,还有原型。这是我的梗。

此题Mark

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注