Runlength Decoding Algorithm - Scanning Strings Left to Right

  • Thread starter sandy.bridge
  • Start date
In summary, you can read an input string left to right and stop reading at the first character that doesn't match the format specified. You can do this in any language by reading one character at a time.
  • #1
sandy.bridge
798
1
Hey. I am working on an algorithm that receives an input in the form "4A3B2H9P" (depending on the users choice of input), and I want it to output "AAAABBBHHPPPPPPPPP". I have tried searching online for ways to "scan" the string, but I cannot seem to find anything. Same goes for me textbook.

Is there a way to scan, from left to right, the string, such that it stops scanning the number once a letter is reached? For example "12A4B", stop scanning after the two, detects 12. Then, scans further and scans until 4, detects A.

Or is there some easier method I can employ? Thanks in advance.
 
Physics news on Phys.org
  • #2
It's trivial. It DOES of course depend, in the specifics, on what language you are using as to what specific system function(s) you use.
 
  • #3
You can do this is any language by reading one character at a time. If it is a digit, use it to update the number of repetitions. If not, write some output.

In C or C++, the functions scanf() will do what you want to read the input. It stops reading at the first character that doesn't match the fiormat you specify, so the format "%d%c" will read an integer (with any number of digits) and then a single character. It also returns the amount of data it read, so you can check for invalid input like "12AB" (with no integers before the B).
 
  • #4
I decided to stay away from using scanf(). I am now using cin with a loop.

PHP:
int N; char ch; char dy;
	cin >> N;
	cin >> dy; // Read the colon
	cin >> ch;
	for (int k=0; k<N; k++)
	{
	 cout << ch;
	}
This code ends up getting the first set of characters displayed. For example, if 5:B6:G was input, BBBBB was output. Can I employ a loop to get the rest of string to display?
 
Last edited:
  • #5
In school you might be taught to do this by reading the characters one at a time then doing what you will with them

Code:
>>> ip = "12A3B"
>>> for x in range (len (ip)):
...   print ip [x]
... 
1
2
A
3
B

So here you would look at the number, read it, convert it to int, emit that many of the next character. As was said before me, scanf() will do this too without needing to convert to int, but it's about as secure and robust as leaving the door open at burglars convention.

In a commercial environment you would be slapped hard by your boss for not using regex.

Code:
lisa@veeshan:$ cat er.py
import re

ip = "4A3B2H9P"
while ip:
    match = re.match ("(\d+)([A-Z])", ip)
    if match:
        print int (match.group (1)) * match.group (2)
        ip = ip [len (match.group(0)):]
lisa@veeshan:$ 
lisa@veeshan:$ python er.py
AAAA
BBB
HH
PPPPPPPPP
lisa@veeshan:$

"regex cheatsheet" in Google.

(\d+) matches one or more digits ... (\d) is digit and + means "or more"

[A-Z] matches a letter (case sensitive)

The brackets break the expression into groups. Group 0 is the whole matched part, group 1 is the digit and group 2 is the letter.
 
  • #6
I am using c++, btw. I updated my second post.
 
  • #7
You're not "supposed" to use scanf() in C++. It's not depreciated but it is no longer recommended. Your compiler probably provides a "safe" version of scanf() so that hackers don't pwn your computer with dodgy inputs :-)

However, in C++ you are "supposed" to use <istringstream> and/or <regex> from the standard library.
 
  • #8
d3mm said:
You're not "supposed" to use scanf() in C++. ... However, in C++ you are "supposed" to use <istringstream> and/or <regex> from the standard library.

In any "serious" C++ code, I would never use the >> and << I/O operators for anythng whatever. I don't have time to learn how to avoid all the 32767 "features" caused by the standards comittee overdosing on operator overloading and automatic conversions. (and the number of features probably goes up to 2147483647 on 64-bit hardware) :smile:
 
  • #9
I ended up posting what I have so far in the homework section (engineering and computer science). I am really close to having this working to perfection, however, I am having an error somewhere in my assignment of the 2D array that I am using.
 
  • #10
This belongs in Homework. Also, using Regex (as was suggested by one reader) is overkill in this case. Regular expressions, while powerful and sometimes needed, also make code more difficult to read later, and so should probably be avoided for simple problems like this one. At least IMHO.
 
  • #11
(Thread moved to HH)
 

1. What is a runlength decoding algorithm?

A runlength decoding algorithm is a method used to decode strings of data that have been compressed using a runlength encoding technique. This algorithm works by scanning the string from left to right and replacing sequences of repeated characters with the actual character followed by the number of times it appears in the sequence.

2. How does the scanning process work in a runlength decoding algorithm?

In the scanning process, the algorithm reads each character in the string and checks if it is a number or a letter. If it is a number, it is added to a counter until a letter is encountered. The letter and the counter value are then used to reconstruct the original string. This process is repeated until the entire string has been decoded.

3. What are the advantages of using a runlength decoding algorithm?

Runlength decoding algorithms are efficient in terms of both time and space complexity. They are able to decompress large amounts of data quickly, making them useful in applications that require fast data processing. Additionally, they do not require a lot of memory to store the decoded data, making them suitable for use in memory-constrained systems.

4. What are some common use cases for a runlength decoding algorithm?

A runlength decoding algorithm is commonly used in data compression applications, such as image and video compression. It is also used in data transmission and storage systems to reduce the size of data and improve transfer speeds. Runlength decoding can also be used in data backup systems to save storage space.

5. Are there any limitations to using a runlength decoding algorithm?

One limitation of runlength decoding algorithms is that they are not suitable for compressing data that does not have repeated characters or patterns. In such cases, the compressed data may end up being larger than the original data. Additionally, runlength decoding algorithms are not very effective for compressing highly random data, such as encrypted data.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Precalculus Mathematics Homework Help
Replies
13
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
Back
Top