Results 1 to 10 of 91

Thread: Beginner Programming Challenge #11

Threaded View

  1. #1
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Beginner Programming Challenge #11

    Beginner Programming Challenge #11

    This challenge is closed. You're welcome to submit an entry and ask for feedback, but the winner has already been selected.

    Welcome to the 11th Beginner Programming Challenge! Although I did not win the 10th programming challenge, Bachstelze has graciously allowed me to host this one.

    If you need help, feel free to drop by on IRC on irc.freenode.net in #ubuntu-beginners-dev.

    Let's get started.

    Task:

    Implement a Reverse Polish Notation (RPN) calculator.

    What is RPN?
    You're probably used to calculators using infix notation. With infix notation, you place an operator between two operands, and press enter to see the result. An example of infix notation:
    Code:
    3 + 4
    Infix notation works well for basic expressions, but it is difficult to write complicated expressions without parentheses.

    RPN, on the other hand, places operators after their operands. For example:
    Code:
    3 4 +
    While this may seem odd at first, it has several convenient properties:
    • It is possible to write arbitrarily long expressions in RPN which are unambiguous, and do not require parentheses.
    • There is no need for an operator precedence hierarchy
    • It is relatively easy for a computer to parse
    • Calculations can be done as soon as an operator is specified - you do not need to consider the entire expression before beginning to evaluate it.


    Here are a few more examples of RPN, with their infix notation counterparts
    Code:
    3 4 - 5 +
    (3 - 4) + 5
    
    3 4 5 * -
    3 - (4 * 5)
    
    5 1 2 + 4 * + 3 −
    5 + ((1 + 2) * 4) − 3
    Requirements:
    • Your program must be able to evaluate any valid RPN expression of reasonable length (~20 tokens) with the following operators: + - * / % ^ (division is true division. 10 3 / = 3.33)
    • If the entered expression's syntax is invalid, your program should handle it gracefully (no segfaults or uncaught exceptions)
    • Your program must handle divide by zero errors gracefully


    Extra Credit:
    • If interactive, allow users to quit without resorting to ^C.
    • Plan for the future: make your code flexible so that it can be incorporated into other projects you may work on easily.
    • Add more operators: anything that makes sense
    • Gracefully handle all user input, from random characters to just a newline.


    Extra Extra Credit:
    • Add support for an infix notation mode by converting the expression to RPN and evaluating it with your RPN code.


    Example output, for testing:
    Code:
    eric@river:~$ ./rpn
    > 3 4 +
    7.0
    > 10 3 /
    3.33333333333
    > 2 5 ^
    32.0
    >  10 2 %
    0.0
    > 24 12 3 + 4 % / 6 - 4 * 3 ^
    512.0
    > 10 2 2 % /
    Divided by Zero
    // Incorrect inputs:
    > 10 + 10
    Not enough operands
    > 10 10 10 +
    Too many operands
    > 
    > aw4ojghsiu5esrgs56u7ikdyjdt drthisu 5 hrtgh 5 5 +
    Unrecognized token
    > 45 6 + / & 4
    Unrecognized token
    > q
    eric@river:~$
    Caveats:
    The wikipedia page has a good explanation of the algorithm for evaluating RPN, as well as a sample implementation in Python. Please try to think for yourself and only reference these if you're stuck. Submissions which are suspiciously similar probably won't win, and more importantly, you won't learn anything.

    As always:
    Any overly obfuscated code will be immediately disqualified without account for programmers skill. Please remember that these challenges are for beginners and therefore the code should be easily readable and well commented.

    Any non-beginner entries will not be judged. Please use common sense when posting code examples. Please do not give beginners a copy paste solution before they have had a chance to try this for themselves.

    Please provide instructions for how to run your program along with your submission.

    And with that...
    Have fun!
    Last edited by schauerlich; April 18th, 2010 at 08:11 AM.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •