Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,147,973 members, 7,799,311 topics. Date: Tuesday, 16 April 2024 at 06:42 PM

Weekend Challenge For Practice: RLE Encoding - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Weekend Challenge For Practice: RLE Encoding (3588 Views)

Recommend Site Where One Can Get Web Development Projects For Practice / Challenge For Javascript Beginners/intermidiate / Coding Challenge For Fun (2) (3) (4)

(1) (2) (Reply) (Go Down)

Weekend Challenge For Practice: RLE Encoding by Nobody: 4:38pm On Aug 05, 2017
Run Length Encoding
Given an input string, write a function that returns the Run Length Encoded string for the input string.

For example, if the input string is “wwwwaaadexxxxxx”, then the function should return “w4a3d1e1x6”.

You can use this to compile your code

https://ideone.com

Usage of this Algorithm

Compression of String if the need of character length is highly emphasized.
Re: Weekend Challenge For Practice: RLE Encoding by jidez007: 5:56pm On Aug 05, 2017
Took me about 5 minutes
C++

#include <iostream>

int main() {
std::string data;

std::cout << "Enter string: ";
std::cin >> data;

char last = NULL;
int count = 0;
for(int i = 0; i <= data.length(); i++) {

if(i !=0 && last != data[i]) {
std::cout << last << count;
count = 0;
}

last = data[i];
count++;
}
return 0;
}
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 6:18pm On Aug 05, 2017
Awesome one bro

https://ideone.com/Stbs0f

Am waiting for others to post theirs if interested, the goal is to get everyone acquainted with challenges so that when interviews start it will be easy to be in that state of mind.
Re: Weekend Challenge For Practice: RLE Encoding by silento(m): 10:31pm On Aug 05, 2017
30 MINUTES

c pointer is great and bad at same time


http://ideone.com/fzTQgH



<script src="http://ideone.com/e.js/fzTQgH" type="text/javascript" ></script>
Re: Weekend Challenge For Practice: RLE Encoding by MonsieurCoder: 11:49pm On Aug 05, 2017
__
Re: Weekend Challenge For Practice: RLE Encoding by Javanian: 11:53pm On Aug 05, 2017

input = "wwwwaaadexxxxxx"
list = [0] * 26
for i in input:
list[ord(i)-97]+=1
i=0
while i < len(input):
print input[i], list[ord(input[i])-97]
i += list[ord(input[i])-97]


https://ideone.com/rTAiHh

8 Lines
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 7:33am On Aug 06, 2017
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 10:45am On Aug 06, 2017
silento:
30 MINUTES

c pointer is great and bad at same time


http://ideone.com/fzTQgH



<script src="http://ideone.com/e.js/fzTQgH" type="text/javascript" ></script>

Am curious why did you go through the route of pointers, because i understand you're allocating the same size as the char, you could easily still use a char , yeah Pointers is cool but sometimes complex. Nice one guys
Re: Weekend Challenge For Practice: RLE Encoding by silento(m): 10:55am On Aug 06, 2017
pcguru1:


Am curious why did you go through the route of pointers, because i understand you're allocating the same size as the char, you could easily still use a char , yeah Pointers is cool but sometimes complex. Nice one guys

in c language if you return a regular
char[] you will get error. if the function is run twice because the function will be sharing the same memory space but will pointer you will get the error solved
Re: Weekend Challenge For Practice: RLE Encoding by orimion(m): 2:52pm On Aug 06, 2017
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 4:25pm On Aug 06, 2017
orimion:
https://ideone.com/DwUQSa

My God what language is this lol
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 4:29pm On Aug 06, 2017
I took just half a look, fainted, woke up, looked again, almost died and finally crawled away on all fours. . . . .yeah, what a language!
Re: Weekend Challenge For Practice: RLE Encoding by orimion(m): 4:44pm On Aug 06, 2017
pcguru1:


My God what language is this lol
Haskell grin
might be my inexperience that makes it look cryptic
Re: Weekend Challenge For Practice: RLE Encoding by CryptoCoinr(f): 4:46pm On Aug 06, 2017
pcguru1:
Run Length Encoding
Given an input string, write a function that returns the Run Length Encoded string for the input string.

For example, if the input string is “wwwwaaadexxxxxx”, then the function should return “w4a3d1e1x6”.

You can use this to compile your code

https://ideone.com

Usage of this Algorithm

Compression of String if the need of character length is highly emphasized.


Er, is the challenge language specific?
Re: Weekend Challenge For Practice: RLE Encoding by jidez007: 5:02pm On Aug 06, 2017
I have looked at that code like 10 times and I don't understand anything
Na Haskell language I go use now if I wan confuse my enemies.
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 5:12pm On Aug 06, 2017
orimion:

Haskell grin
might be my inexperience that makes it look cryptic

Actually far from it the syntax doesn't come across as familiar at all most times you'd get that c like or Python type but never saw this.
Re: Weekend Challenge For Practice: RLE Encoding by CryptoCoinr(f): 8:49pm On Aug 07, 2017
CryptoCoinr:


Er, is the challenge language specific?

pcguru1, did you ignore this or you just didn't see it? Either way, here's mine (in JS as usual):


function charcurrence (text)
{
let counter = 0;
let char = text[0];
let charcurredText = "";

for (let i = 0; i < text.length; i++)
{
if (char === text[i])
{
counter++;
}
else
{
charcurredText += char + counter;
char = text[i];
counter = 1;
}
}

return charcurredText += char + counter;
}
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 9:26pm On Aug 07, 2017
CryptoCoinr:


pcguru1, did you ignore this or you just didn't see it? Either way, here's mine (in JS as usual):


function charcurrence (text)
{
let counter = 0;
let char = text[0];
let charcurredText = "";

for (let i = 0; i < text.length; i++)
{
if (char === text[i])
{
counter++;
}
else
{
charcurredText += char + counter;
char = text[i];
counter = 1;
}
}

return charcurredText += char + counter;
}

4got to reply
Re: Weekend Challenge For Practice: RLE Encoding by CryptoCoinr(f): 9:37pm On Aug 07, 2017
pcguru1:


4got to reply

Okie dokie.

I'm glad I googled what RLE was. I actually completed the challenge within ~15mins - 20mins of posting that question but started wondering what we were meant to do if an already tagged character, say "w", occurred after a whole bunch of other characters. Long story short: many hours spent needlessly reiterating.
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 10:00pm On Aug 07, 2017
^^^that is why you guys are EXPERTS!
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 10:06pm On Aug 07, 2017
CryptoCoinr:


Okie dokie.

I'm glad I googled what RLE was. I actually completed the challenge within ~15mins - 20mins of posting that question but started wondering what we were meant to do if an already tagged character, say "w", occurred after a whole bunch of other characters. Long story short: many hours spent needlessly reiterating.

LOL Over-thinking, that way won't be able to track the position of where the text occurred, you get.
Re: Weekend Challenge For Practice: RLE Encoding by segxyhanxy(m): 11:01pm On Aug 07, 2017
a solution of it in java.

public static String rle(String data) {
String compressedData = "";

int counter = 1;
char lastChar = data.charAt(0);
for (int i = 1; i < data.length(); i++) {
char c = data.charAt(i);
if (c != lastChar) {
compressedData += data.substring(i - 1, i) + counter;
counter = 1;
lastChar = c;
} else {
counter++;
lastChar = c;
}
if (i == data.length() - 1) {
compressedData += data.substring(i - 1, i) + counter ;
}
}
return compressedData;
}
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 8:08am On Aug 08, 2017
segxyhanxy:
a solution of it in java.

public static String rle(String data) {
String compressedData = "";

int counter = 1;
char lastChar = data.charAt(0);
for (int i = 1; i < data.length(); i++) {
char c = data.charAt(i);
if (c != lastChar) {
compressedData += data.substring(i - 1, i) + counter;
counter = 1;
lastChar = c;
} else {
counter++;
lastChar = c;
}
if (i == data.length() - 1) {
compressedData += data.substring(i - 1, i) + counter ;
}
}
return compressedData;
}

Segzy baba
Re: Weekend Challenge For Practice: RLE Encoding by deedat205(m): 11:20am On Aug 09, 2017
Nice challenge. But who can come up with the RLE string decoding solution. Eg W4E2H3 should be WWWWEEHHH and A12T3Y4 should be AAAAAAAAAAAATTTYYYY
Re: Weekend Challenge For Practice: RLE Encoding by Nobody: 1:48pm On Aug 09, 2017
deedat205:
Nice challenge. But who can come up with the RLE string decoding solution.
Eg W4E2H3 should be WWWWEEHHH and A12T3Y4 should be AAAAAAAAAAAATTTYYYY

The reverse of it right.
Re: Weekend Challenge For Practice: RLE Encoding by orimion(m): 2:04pm On Aug 09, 2017
deedat205:
Nice challenge. But who can come up with the RLE string decoding solution.
Eg W4E2H3 should be WWWWEEHHH and A12T3Y4 should be AAAAAAAAAAAATTTYYYY

here ya go

import Data.Char

main = do
putStrLn $ decode "W4E2H3"
putStrLn $ decode "A12T3Y4"

decode :: String -> String
decode = concat . map expand . split

expand :: String -> String
expand "" = ""
expand (x:xs) = take (read xs) $ repeat x

split :: String -> [String]
split = words . tail . foldr fn ""
where fn x acc = if (ord x < 48 || ord x > 57) then (' ':x:acc) else (x:acc)


http://ideone.com/nq33Iy
Re: Weekend Challenge For Practice: RLE Encoding by deedat205(m): 6:05pm On Aug 12, 2017
We have strong programmers on Nairaland

my python 3 solution to RLE Encoding

a = 'wwwwaaadexxxxxx'

a += '!'
i,s = 0,''
while i < len(a)-1:
c = 1
while a[i] == a[i+1] and i < len(a)-1:
c,i = c+1, i+1
s += a[i]+str(c)
i += 1

print(s)

Solution to RLE decoding

import re
a = 'A12T3Y4'

x = re.compile(r'[A-Za-z]')
y = re.compile(r'\d+')

p = x.findall(a)
q = y.findall(a)

z = ''
for i in range(len(p)):
z += p[i]*int(q[i])

print(z)
Re: Weekend Challenge For Practice: RLE Encoding by deedat205(m): 6:15pm On Aug 12, 2017
There is a stubborn challenge I want you guys to try out on Hackerrank
https://www.hackerrank.com/challenges/separate-the-numbers/problem

The challenge was rated easy, but I think medium would have been appropriate to describe its complexity

The challenge is to determine if a string has numbers that are consecutive with a difference of 1.
Example 1234, 91011 and 99100 should all return YES
while something like 101113, 2325 should both return NO

(1) (2) (Reply)

Quiting My Job To Venture Into Tech At 28 - Advice Needed. / Sharing My Ocmjd Certification Experience / Programming Interview Test

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 37
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.