The purpose of this programming assignment is to help you get up-to-speed with the Python programming language. This is an individual assignment.
Get started by completing the online tutorials in http://www.learnpython.org. You should complete all of the tutorials in the "Learn the Basics Section" with the exception of "Classes and Objects".
Next, complete the Introduction to Classes tutorial provided by www.codecademy.com.
Step 10 of that tutorial ("It's Not All Animals and Fruits") introduces a shopping cart class. Copy that class into the file carts.py and complete the following improvements:
items_in_cart
from a class-level variable to an object-level variable. I.e.:
def __init__(self, customer_name): self.customer_name = customer_name self.items_in_cart = {}Instead of:
items_in_cart = {} def __init__(self, customer_name): self.customer_name = customer_name
A class-level variable doesn't really make sense in this context. Each cart object should have its own collection of items.
most_expensive
method to ShoppingCart
class that returns the most expensive item in the cart.
ShoppingCart
named FrugalCart
. The FrugalCart
class
should have the same functionality as the ShoppingCart
class, except that it should not allow any item with a price greater
than 10 dollars into the cart. If the add_item
is
passed an item that costs more than 10 dollars, the message "Too
expensive." should be printed, and the contents of the cart should
be unmodified.
carts.py
and execute it as a Python script.
Feel free to skim the tutorials above if you are already comfortable programming in Python. If you are new to Python, I strongly encourage you to work through each step of the tutorials carefully. It will probably take a couple of hours, but the time spent will pay dividends throughout the semester.
For this portion of the project you will complete a simulator for a simple robot. This robot begins its existence at position (0,0) on an infinite two dimensional plane. It's actions are governed by a simple programming language named BRL (Bad Robot Language).
Here is a sample program that includes the full set of BRL primitive instructions:
NORTH 10 EAST 20 SOUTH 5 WEST 10
In this example, "NORTH 10" will increase the robot's y-coordinate by 10, "EAST 20" will increase the robot's x-coordinate by 20, "SOUTH 5" will decrease the robot's y coordinate by 5 and "WEST 10" will decrease the robot's x coordinate by 10. The robot's position after completing this program will therefore be (10, 5). The language only supports motion in one of these four cardinal directions.
Distances must be specified as integer values.
Indentation is not syntactically significant.
There may be at most one instruction on each line. Empty lines are not allowed.
In addition to primitive instructions, the BRL language allows for the creation of macros that associate a sequence of instructions with an identifier. The following program segment illustrates the syntax of macro declarations:
def UPLEFT NORTH 10 WEST 10 end
Again, the indentation is not syntactically significant, but the newlines are required. Identifiers are case-sensitive and may not contain white-space.
Once a macro has been defined it can be executed directly and it may also be included within other macros. The following example illustrates a complete BRL program:
def UPLEFT <- Creates a macro named UPLEFT NORTH 10 WEST 10 end UPLEFT <- Moves the robot north then west. Robot is at (-10, 10) NORTH 10 <- Moves the robot north. Robot is at (-10, 20). def UPLEFT_TWO <- Creates a macro named UPLEFT_TWO UPLEFT UPLEFT end UPLEFT_TWO <- Robot will execute UPLEFT twice. Robot is at (-30, 40)
You can use the file robot_sim.py as a
starting point for your implementation. You may add as many
additional instance variables and helper functions as you like, but
you must not modify the existing method signatures in
the RobotSim
class. You do not need to worry about error
handling.
Here is a file containing the example program above: example.txt. You should be able to test your implementation using this sample file:
$ python robot_sim.py example.txt ROBOT FINAL POSITION: (-30, 40)
Upload your completed Python files through Canvas before the deadline. Your code should be well organized and clearly documented. Make sure to include an acknowledgment statement describing any resources that you used or assistance that you received.