CodeWar Weekly - 0210~0216

CodeWar Weekly - 0210~0216

6kyu Unique In Order

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
2
3
unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
unique_in_order([1,2,2,3,3]) == [1,2,3]

解題思考:輸入值 iterable 可能是「字串」或「陣列」,三元運算式,輸出結果可知不能用 uniq 來解

參考方法 & 學習筆記

Ruby

chunk

enum.chunk {|elt| key }.each {|key, ary| … }
enum.chunk(initial_state) {|elt, state| key }.each {|key, ary| … }

For example, consecutive even numbers and odd numbers can be splitted as follows.

1
2
3
4
5
6
7
8
9
10
11
[3,1,4,1,5,9,2,6,5,3,5].chunk {|n|
n.even?
}.each {|even, ary|
p [even, ary]
}

#=> [false, [3, 1]]
# [true, [4]]
# [false, [1, 5, 9]]
# [true, [2, 6]]
# [false, [5, 3, 5]]

JavaScript

Array.prototype.push()

The push() method adds one or more elements to the end of an array and returns the new length of the array.

1
2
3
4
5
6
7
8
9
const animals = ['pigs', 'goats', 'sheep'];

const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push('chickens', 'cats', 'dogs');

Array.prototype.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

1
2
3
4
5
6
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

Spread syntax […x] ES6

Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

1
2
3
4
5
6
7
8
9
10
11
12
function sum(x, y, z) {
return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6

console.log(sum.apply(null, numbers));
// expected output: 6

(下方是我寫的一些解題方法,有雷請小心服用ouo~~)


Ruby

Solution(1):

1
2
3
4
def unique_in_order(iterable)
iterable.is_a?(String) ? iterable.split('').chunk{ |x| x }.map{ |x, ary| x }
: iterable.chunk{ |x| x }.map{ |x, ary| x }
end

Solution(2):

1
2
3
4
5
def unique_in_order(iterable)
(iterable.is_a?(String) ? iterable.chars
: iterable )
.chunk { |x| x }.map{ |x| x }
end

JavaScript

Solution(1):

1
2
3
4
5
6
7
8
9
10
11
12
var uniqueInOrder = function(iterable){
var result = [];
var last = '';

for(var i = 0; i < iterable.length; i++){
if(iterable[i] !== last){
last = iterable[i]
result.push(last);
}
}
return result;
}

Solution(2):

1
2
3
var uniqueInOrder=function(iterable){
return iterable.split('').filter((a, i) => a !== iterable[i-1])
}

Solution(3):

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

參考資料