Runlength Decoding Algorithm - Scanning Strings Left to Right

  • Thread starter Thread starter sandy.bridge
  • Start date Start date
AI Thread Summary
The discussion focuses on implementing a run-length decoding algorithm to convert strings like "4A3B2H9P" into "AAAABBBHHPPPPPPPPP". Participants suggest methods for scanning the string left to right, emphasizing reading one character at a time to differentiate between digits and letters. C++ functions like scanf() and cin are mentioned, with a preference for cin in loops for better control. Regular expressions are also discussed as a potential solution, though some argue they may be unnecessarily complex for this problem. The conversation highlights the balance between using efficient coding practices and maintaining code readability.
sandy.bridge
Messages
797
Reaction score
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
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.
 
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).
 
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:
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.
 
I am using c++, btw. I updated my second post.
 
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.
 
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:
 
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)
 

Similar threads

Replies
17
Views
5K
Replies
2
Views
2K
Replies
6
Views
3K
Replies
3
Views
2K
Replies
4
Views
2K
Replies
7
Views
246
Back
Top