Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
14
Task/Run-length-encoding/Ruby/run-length-encoding-1.rb
Normal file
14
Task/Run-length-encoding/Ruby/run-length-encoding-1.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# run_encode("aaabbbbc") #=> [["a", 3], ["b", 4], ["c", 1]]
|
||||
def run_encode(string)
|
||||
string
|
||||
.chars
|
||||
.chunk{|i| i}
|
||||
.map {|kind, array| [kind, array.length]}
|
||||
end
|
||||
|
||||
# run_decode([["a", 3], ["b", 4], ["c", 1]]) #=> "aaabbbbc"
|
||||
def run_decode(char_counts)
|
||||
char_counts
|
||||
.map{|char, count| char * count}
|
||||
.join
|
||||
end
|
||||
9
Task/Run-length-encoding/Ruby/run-length-encoding-2.rb
Normal file
9
Task/Run-length-encoding/Ruby/run-length-encoding-2.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def encode(string)
|
||||
string.scan(/(.)(\1*)/).collect do |char, repeat|
|
||||
[1 + repeat.length, char]
|
||||
end.join
|
||||
end
|
||||
|
||||
def decode(string)
|
||||
string.scan(/(\d+)(\D)/).collect {|length, char| char * length.to_i}.join
|
||||
end
|
||||
11
Task/Run-length-encoding/Ruby/run-length-encoding-3.rb
Normal file
11
Task/Run-length-encoding/Ruby/run-length-encoding-3.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def encode(string)
|
||||
string.scan(/(.)(\1*)/).inject("") do |encoding, (char, repeat)|
|
||||
encoding << (1 + repeat.length).to_s << char
|
||||
end
|
||||
end
|
||||
|
||||
def decode(string)
|
||||
string.scan(/(\d+)(\D)/).inject("") do |decoding, (length, char)|
|
||||
decoding << char * length.to_i
|
||||
end
|
||||
end
|
||||
7
Task/Run-length-encoding/Ruby/run-length-encoding-4.rb
Normal file
7
Task/Run-length-encoding/Ruby/run-length-encoding-4.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def encode(str)
|
||||
str.gsub(/(.)\1*/) {$&.length.to_s + $1}
|
||||
end
|
||||
|
||||
def decode(str)
|
||||
str.gsub(/(\d+)(\D)/) {$2 * $1.to_i}
|
||||
end
|
||||
4
Task/Run-length-encoding/Ruby/run-length-encoding-5.rb
Normal file
4
Task/Run-length-encoding/Ruby/run-length-encoding-5.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
orig = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
|
||||
p enc = encode(orig)
|
||||
p dec = decode(enc)
|
||||
puts "success!" if dec == orig
|
||||
Loading…
Add table
Add a link
Reference in a new issue