tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
15
Task/Run-length-encoding/JavaScript/run-length-encoding-1.js
Normal file
15
Task/Run-length-encoding/JavaScript/run-length-encoding-1.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
function encode(input) {
|
||||
var encoding = [];
|
||||
var prev, count, i;
|
||||
for (count = 1, prev = input[0], i = 1; i < input.length; i++) {
|
||||
if (input[i] != prev) {
|
||||
encoding.push([count, prev]);
|
||||
count = 1;
|
||||
prev = input[i];
|
||||
}
|
||||
else
|
||||
count ++;
|
||||
}
|
||||
encoding.push([count, prev]);
|
||||
return encoding;
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
function encode_re(input) {
|
||||
var encoding = [];
|
||||
input.match(/(.)\1*/g).forEach(function(substr){ encoding.push([substr.length, substr[0]]) });
|
||||
return encoding;
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
function decode(encoded) {
|
||||
var output = "";
|
||||
encoded.forEach(function(pair){ output += new Array(1+pair[0]).join(pair[1]) })
|
||||
return output;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue