RunLengthEncoding 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/run_length_encoding.rb +25 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0e7efceaa0edb1d06463745729c5464befce4c6efe4fddd134e751c884325282
|
4
|
+
data.tar.gz: 83584f9f9628ad6c15475371b7d78ecee2ee61647e8863702cc2cd1e8b9da86f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1dec7e7928a9025efd9ab9ac0d700cd2f6278a980e5136357a2a4ffa65f65a06d1de229068a7bf41c4841cbed51d9b3fd01b64eede426c15e804cb8f39608083
|
7
|
+
data.tar.gz: ffc8655b7960a5ee8147d9399d357b259b23ee7d9a78a8b46b2b7d915875748b22cfad6b197612c02cdab730a47a337c59906c701067e055eecde4f9f3dc8057
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class RunLengthEncoding
|
2
|
+
def self.encode(input)
|
3
|
+
groups = []
|
4
|
+
encode_string = []
|
5
|
+
input.scan(/((.)\2*)/) { |x| groups.push(x[0]) }
|
6
|
+
groups.each do |x|
|
7
|
+
encode_string.push x.chars.length if x.chars.length >= 2
|
8
|
+
encode_string.push x.chars[0]
|
9
|
+
end
|
10
|
+
encode_string.join('')
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.decode(input)
|
14
|
+
out = []
|
15
|
+
groups = input.scan(/\d+|[A-Za-z\s]/)
|
16
|
+
(0..groups.length).each do |i|
|
17
|
+
if groups[i].to_i != 0
|
18
|
+
out.push groups[i + 1] * (groups[i].to_i-1)
|
19
|
+
else
|
20
|
+
out.push groups[i]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
out.join('')
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: RunLengthEncoding
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hamidreza Safari
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple algorithm for compressing text
|
14
|
+
email: hamidrezasafari@outlook.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/run_length_encoding.rb
|
20
|
+
homepage: http://rubygems.org/gems/run_length_encoding
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.7.3
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: RunLengthEncoding
|
44
|
+
test_files: []
|