分享一下codewars里的算法题

闲的没事,总是会去codewars练两把,做几道算法题,学习一下国外的大牛的牛逼之处,这里也算记录分享一下


[TOC]
可以不急着看答案,先去上手做几道,至于答案,如果你有更好更简洁的也请提个issue,共同学习~

#Kata

[6 kyu]Write Number in Expanded Form

Intructions:
You will be given a number and you will need to return it as a string in Expanded Form. For example:

1
2
3
expandedForm(12); // Should return '10 + 2'
expandedForm(42); // Should return '40 + 2'
expandedForm(70304); // Should return '70000 + 300 + 4'

NOTE: All numbers will be whole numbers greater than 0.

Solution:

1
2
3
function expandedForm(num) {
// Your code here
}

SampleTests:

1
2
3
Test.assertEquals(expandedForm(12), '10 + 2');
Test.assertEquals(expandedForm(42), '40 + 2');
Test.assertEquals(expandedForm(70304), '70000 + 300 + 4');

[6 kyu]CamelCase Method

Intructions:
Write simple .camelCase method (camel_case function in PHP, CamelCase in C# or camelCase in Java) for strings. All words must have their first letter capitalized without spaces.
For instance:

1
2
"hello case".camelCase() => HelloCase
"camel case word".camelCase() => CamelCaseWord

Don’t forget to rate this kata! Thanks :)

Solution:

1
2
3
String.prototype.camelCase=function(){
//your code here
}

SampleTests:

1
2
3
4
5
6
7
Test.describe("Basic tests",_=>{
Test.assertEquals("test case".camelCase(), "TestCase");
Test.assertEquals("camel case method".camelCase(), "CamelCaseMethod");
Test.assertEquals("say hello ".camelCase(), "SayHello");
Test.assertEquals(" camel case word".camelCase(), "CamelCaseWord");
Test.assertEquals("".camelCase(), "");
})

[6 kyu] Reversed Words

Intructions:

Complete the solution so that it reverses all of the words within the string passed in.
Example:

1
2
reverseWords("The greatest victory is that which requires no battle")
// should return "battle no requires which that is victory greatest The"

Solution:

1
2
function reverseWords(){
}

SampleTests:

1
2
3
4
5
6
7
8
9
describe("reverseWords",function(){
it("should work for some examples", function(){
Test.assertEquals(reverseWords("hello world!"), "world! hello")
Test.assertEquals(reverseWords("yoda doesn't speak like this" ), "this like speak doesn't yoda")
Test.assertEquals(reverseWords("foobar" ), "foobar")
Test.assertEquals(reverseWords("kata editor" ), "editor kata")
Test.assertEquals(reverseWords("row row row your boat" ), "boat your row row row")
});
});

[6 kyu] Replace With Alphabet Position

Intructions:
Welcome.
In this kata you are required to, given a string, replace every letter with its position in the alphabet.
If anything in the text isn’t a letter, ignore it and don’t return it.
a being 1, b being 2, etc.
As an example:

1
alphabet_position("The sunset sets at twelve o' clock.")

Should return “20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11” as a string.

Solution:

1
2
function alphabetPosition() {
}

SampleTests:

1
2
Test.assertEquals(alphabetPosition("The sunset sets at twelve o' clock."), "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11");
Test.assertEquals(alphabetPosition("The narwhal bacons at midnight."), "20 8 5 14 1 18 23 8 1 12 2 1 3 15 14 19 1 20 13 9 4 14 9 7 8 20");

[6 kyu] Triple trouble

Write a function

1
tripledouble(num1,num2)

which takes in numbers num1 and num2 and returns 1 if there is a straight triple of a number at any place in num1 and also a straight double of the same number in num2.
For example:

1
2
3
4
5
6
7
8
tripledouble(451999277, 41177722899) == 1 // num1 has straight triple 999s and
// num2 has straight double 99s
tripledouble(1222345, 12345) == 0 // num1 has straight triple 2s but num2 has only a single 2
tripledouble(12345, 12345) == 0
tripledouble(666789, 12345667) == 1

If this isn’t the case, return 0

Solution:

1
2
3
function tripledouble(num1, num2) {
}

SampleTests:

1
2
3
4
5
6
7
describe('Initial Tests',_=>{
Test.assertEquals(tripledouble(673816111777444900,9674451144),1);
Test.assertEquals(tripledouble(1222345, 12345),0);
Test.assertEquals(tripledouble(12345, 12345),0);
Test.assertEquals(tripledouble(666789, 12345667),1);
Test.assertEquals(tripledouble(10560002, 100),1);
});

[5 kyu] Where my anagrams at?

Intructions:
What is an anagram? Well, two words are anagrams of each other if they both contain the same letters. For example:

1
2
3
4
5
'abba' & 'baab' == true
'abba' & 'bbaa' == true
'abba' & 'abbba' == false

Write a function that will find all the anagrams of a word from a list. You will be given two inputs a word and an array with words. You should return an array of all the anagrams or an empty array if there are none. For example:

1
2
3
4
5
anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']) => ['aabb', 'bbaa']
anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer']) => ['carer', 'racer']
anagrams('laser', ['lazing', 'lazy', 'lacer']) => []

Solution:

1
2

Test Driven Development (TDD)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// TODO: Replace examples and use TDD development by writing your own tests
// These are some CW specific test methods available:
// Test.expect(boolean, [optional] message)
// Test.assertEquals(actual, expected, [optional] message)
// Test.assertSimilar(actual, expected, [optional] message)
// Test.assertNotEquals(actual, expected, [optional] message)
// NodeJS assert is also automatically required for you.
// assert(true)
// assert.strictEqual({a: 1}, {a: 1})
// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
// You can also use Chai (http://chaijs.com/) by requiring it yourself
// var expect = require("chai").expect;
// var assert = require("chai").assert;
// require("chai").should();
describe("Solution", function(){
it("should test for something", function(){
Test.assertEquals("actual", "expected", "This is just an example of how you can write your own TDD tests");
});
});

[6 kyu] Find the missing term in an Arithmetic Progression

Intructions:
An Arithmetic Progression is defined as one in which there is a constant difference between the consecutive terms of a given series of numbers. You are provided with consecutive elements of an Arithmetic Progression. There is however one hitch: exactly one term from the original series is missing from the set of numbers which have been given to you. The rest of the given series is the same as the original AP. Find the missing term.
You have to write the function findMissing(list), list will always be at least 3 numbers. The missing term will never be the first or last one.
Example

1
findMissing([1, 3, 5, 9, 11]) == 7

PS: This is a sample question of the facebook engineer challenge on interviewstreet. I found it quite fun to solve on paper using math, derive the algo that way. Have fun!

Solution:

1
2
3
var findMissing = function (list) {
return list [0] ;
}

Sample Tests:

1
2
3
4
5
6
7
fixture.random(10,0);
fixture.random(10,10);
fixture.random(10,100);
fixture.random(10,1000);
Test.describe("Testing with [1, 3, 4]", function() {
Test.assertEquals(findMissing([1, 3, 4]), 2);
});

坚持原创技术分享,您的支持将鼓励我继续创作!