1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Runlength Decoding (

  1. Mar 20, 2013 #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.
     
  2. jcsd
  3. Mar 20, 2013 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    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.
     
  4. Mar 20, 2013 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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).
     
  5. Mar 20, 2013 #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: Mar 20, 2013
  6. Mar 20, 2013 #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 (Text):
    >>> 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 (Text):

    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.
     
  7. Mar 20, 2013 #6
    I am using c++, btw. I updated my second post.
     
  8. Mar 20, 2013 #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.
     
  9. Mar 20, 2013 #8

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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:
     
  10. Mar 20, 2013 #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.
     
  11. Mar 21, 2013 #10

    harborsparrow

    User Avatar
    Gold Member

    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.
     
  12. Mar 21, 2013 #11

    berkeman

    User Avatar

    Staff: Mentor

    (Thread moved to HH)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Runlength Decoding (
  1. Address Decoder (Replies: 1)

  2. Decoding fail (Replies: 7)

  3. Decoding into image (Replies: 15)

Loading...