Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Compressor] RLE Compressor implementation#1671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
raklaptudirm merged 10 commits intoTheAlgorithms:masterfromddaniel27:master
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletionsCompression/RLE.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
/*
* RLE (Run Length Encoding) is a simple form of data compression.
* The basic idea is to represent repeated successive characters as a single count and character.
* For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".
*
* @author - [ddaniel27](https://github.com/ddaniel27)
*/

function Compress(str) {
let compressed = ''
let count = 1

for (let i = 0; i < str.length; i++) {
if (str[i] !== str[i + 1]) {
compressed += count + str[i]
count = 1
continue
}

count++
}

return compressed
}

function Decompress(str) {
let decompressed = ''
let match = [...str.matchAll(/(\d+)(\D)/g)] // match all groups of digits followed by a non-digit character

match.forEach((item) => {
let [count, char] = [item[1], item[2]]
decompressed += char.repeat(count)
})

return decompressed
}

export { Compress, Decompress }
13 changes: 13 additions & 0 deletionsCompression/test/RLE.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
import { Compress, Decompress } from '../RLE'

describe('Test RLE Compressor/Decompressor', () => {
it('Test - 1, Pass long repetitive strings', () => {
expect(Compress('AAAAAAAAAAAAAA')).toBe('14A')
expect(Compress('AAABBQQQQQFG')).toBe('3A2B5Q1F1G')
})

it('Test - 2, Pass compressed strings', () => {
expect(Decompress('14A')).toBe('AAAAAAAAAAAAAA')
expect(Decompress('3A2B5Q1F1G')).toBe('AAABBQQQQQFG')
})
})
Loading

[8]ページ先頭

©2009-2025 Movatter.jp