lecture 18 – problem solving and modular design. at the end of this lecture, students should be...

12
Lecture 18 – Problem Solving and Modular Design COMPSCI 101 Principles of Programming

Upload: britney-lyons

Post on 21-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

  • Slide 1
  • Lecture 18 Problem Solving and Modular Design
  • Slide 2
  • At the end of this lecture, students should be able to: understand complicated examples on file input output process the content of text files inside a large program design a larger program by breaking it up into manageable parts 2COMPSCI 101 - Principles of Programming
  • Slide 3
  • Complete the update_quantity function below, which updates the quantity value of an item by an integer (either positive or negative). Use the split function to break up the item string into a list of attributes, where item_list[3] represents the quantity value. After calculating the new quantity value, we need to regenerate the item string and update it back into the stock list. 3COMPSCI 101 - Principles of Programming def update_quantity(stock_list, index, value): item = stock_list[index] # take the item out item_list = item.split(",") item_list[3] = str(int(item_list[3])+value) item = item_list[0] for i in range(1, len(item_list)): item = item + "," + item_list[i] stock_list[index] = item # put the item back
  • Slide 4
  • Break down a large program into smaller parts (functions). 4COMPSCI 101 - Principles of Programming
  • Slide 5 0: print(item) display_separator()">
  • The display_stock function prints out all the available items on sale in the stock. Uses a for loop with the list name to scan through the items list, For each item, use the split function to break down the item into a list of attributes, where item_list[3] stores the quantity value. Only prints out the items with quantity value greater than 0. 5COMPSCI 101 - Principles of Programming def display_stock(stock_list): display_separator() print("List of the items on sale") display_separator() for item in stock_list: item_list = item.split(",") if int(item_list[3]) > 0: print(item) display_separator()
  • Slide 6 0: cart = cart + [item] print("Item - " + item_list[1] + " is added to cart.") update_quantity(stock_list, index, -1) else: print("Sorry, item "+ item_list[0] + " is out of stock.") print("Please select a different item.") display_separator() return cart">
  • The add_to_cart function adds an item into the shopping cart with an user given item code. If the item exists and available, put item in the cart list and update its quantity. 6COMPSCI 101 - Principles of Programming def add_to_cart(stock_list, cart): index = input_code(stock_list) item = stock_list[index] item_list = item.split(",") if int(item_list[3]) > 0: cart = cart + [item] print("Item - " + item_list[1] + " is added to cart.") update_quantity(stock_list, index, -1) else: print("Sorry, item "+ item_list[0] + " is out of stock.") print("Please select a different item.") display_separator() return cart
  • Slide 7
  • The remove_from_cart function removes an unwanted item from the shopping cart with an user given item code. If the item exists in the cart, remove it in the cart list and update its quantity. 7COMPSCI 101 - Principles of Programming def remove_from_cart(stock_list, cart): index = input_code(stock_list) item = stock_list[index] item_list = item.split(",") cart_index = find_item(cart, item_list[0]) if cart_index != -1: cart.remove(cart[cart_index]) print("Item - " + item_list[1] + " is removed from cart.") update_quantity(stock_list, index, 1) else: print("This item is not in the shopping cart.") display_separator() return cart
  • Slide 8
  • The check_out function prints out the bill with the items bought and sums up the total amount due. Use a for loop with list to scan through the items in the shopping cart, display the description together with price, and sum up the total costs. 8COMPSCI 101 - Principles of Programming def check_out(cart): display_separator() print("Your Shopping Bill") display_separator() sum = 0 for item in cart: item_list = item.split(",") print(item_list[1] + "/$" + item_list[2]) sum = sum + float(item_list[2]) display_separator() print("Amount due: $" + str(format(sum, ".2f"))) display_separator()
  • Slide 9
  • The discard_cart function put the items in the shopping cart back to shelf, by simply updating the quantity value of the corresponding items in the stock list. The save_stock function stores the items list back to a file. 9COMPSCI 101 - Principles of Programming def discard_cart(stock_list, cart): for item in cart: item_list = item.split(",") index = find_item(stock_list, item_list[0]) update_quantity(stock_list, index, 1) display_separator() def save_stock(stock_list, filename): out_file = open(filename, 'w') for item in stock_list: out_file.write(item + "\n") out_file.close()
  • Slide 10
  • def main_menu(stock_list): cart = [] option = get_user_input(1,5) while (option != 4 and option !=5): if option == 1: display_stock(stock_list) elif option == 2: cart = add_to_cart(stock_list, cart) else: cart = remove_from_cart(stock_list, cart) option = get_user_input(1,5) if option == 4 and len(cart)>0: check_out(cart) print("Thank you for shopping with us.") else: discard_cart(stock_list, cart) print("Nothing bought. Thank you.") 10COMPSCI 101 - Principles of Programming def main(): stock_list = load_stock("stock.txt") display_separator() display_intro() display_separator() display_menu() display_separator() main_menu(stock_list) display_separator() save_stock(stock_list, "stock2.txt") main() Modularity emphasizes on separating the functionality of a program into independent and interchangeable modules.
  • Slide 11
  • A complex task can be accomplished by dividing it into a number of smaller sub tasks to achieve individually. This technique of divide and conquer strategy is very effective and popularly used in problem solving. A program represents a solution to a problem, which consists of a set of instructions written in a specific programming language that is executable on a computer. Therefore, it is natural and particularly useful to design a larger program by breaking it up into smaller and manageable parts. In Python, we can achieve this with functions and assemble them together to provide a modularized solution. COMPSCI 101 - Principles of Programming11
  • Slide 12
  • Opening a file f = open( filename[, mode] ) Closing a file f.close() Reading a file f.read() Writing a file f.write( string ) The split function item = "BC009,Broccoli,1.47,11" item_list = item.split(",") # item_list is ['BC009', 'Broccoli', '1.47', '11'] 12COMPSCI 101 - Principles of Programming