--- title: Lab 08 tags: Labs-F20, 2020 --- # Lab 8: Python Practice ![](https://i.pinimg.com/originals/00/fa/37/00fa37d042809448703f88a5e35afe0d.jpg) ## Learning Objective We want you to get set up with PyCharm, and we want to get some Python practice with functions, lists, and for loops in! As always, if you need help with any of these concepts, please come to TA hours! ## Part 0 - Installing PyCharm - This is our first time using PyCharm, the editor we'll be using to write our Python programs! Yay! You have a choice: you can either create a new project in PyCharm for ALL your files, or create a new project for each assignment. Just make sure to include `testlight.py` into each project you create. - Follow the [PyCharm Installation and Project Setup](https://cs.brown.edu/courses/csci0111/spring2020/docs/pycharm_setup.html) guide! If you get stuck or run into problems at any step, call over a TA! ___ ### *CHECKPOINT:* Call over a TA once you reach this point. ___ ## Part 1 - Functions and `return` Functions in Python look a little bit different than they do in Pyret. For instance, if we wanted to write a function that takes in a `Number` representing the time of day (in [military time](https://en.wikipedia.org/wiki/24-hour_clock)) and output either `"Day"` or `"Night"` in Pyret, we'd do something like this: ``` fun day-or-night(time :: Number) -> String: if (time >= 500) and (time <= 1800): "Day" else: "Night" end end ``` In Python, we could write it like this: ``` def day_or_night(time: int) -> str: if time >= 500 and time <= 1800: return "Day" else: return "Night" ``` Note that when we want our functions to give us an output when we call them in Python, we have to write `return` before the value we'd like to output. --- 1. Take a look at the following Python function, `function_one`: ``` def function_one(nums: list, n1: int, n2: int) -> bool: for num in nums: if num < n1 or num > n2: return False return True ``` In words, describe what this function returns for each of these inputs. In particular, *when* does it return `False`? When does it return `True`? ``` >>> function_one([1, 2, 3], 1, 3) >>> function_one([1, 2, 3], 0, 4) >>> function_one([1, 2, 3], 1, 2) ``` 2. Now, take a look at this Python function, `function_two`: ``` def function_two(nums: list, n1: int, n2: int) -> bool: for num in nums: if num < n1 or num > n2: return False return True ``` What does this function do? How is it different than `function_one`? Again, what does it return for these inputs: ``` >>> function_two([1, 2, 3], 1, 3) >>> function_two([1, 2, 3], 0, 4) >>> function_two([1, 2, 3], 1, 2) ``` 3. Lastly, consider this Python function, `function_three`: ``` def function_three(nums: list, n1: int, n2: int) -> bool: for num in nums: if num < n1 or num > n2: return False return True ``` What does this function do? How is it different than `function_one` and `function_two`? What does it return for these inputs: ``` >>> function_three([1, 2, 3], 1, 3) >>> function_three([1, 2, 3], 0, 4) >>> function_three([1, 2, 3], 1, 2) ``` ___ ### *CHECKPOINT:* Call over a TA once you reach this point. ___ ## Part 2 - Lists & For Loops :::warning If you know how to solve any of these problems using techniques you've learned outside of 111, don't! Make sure to only use Python concepts we've talked about in class. ::: Create a new Python file called `lab8_code.py`. Remember your resources! - [Python Testing Design and Clarity Guide ](https://hackmd.io/@cs111/python_guide) - [Code Clarity Guide](https://cs.brown.edu/courses/csci0111/fall2020/docs/python_testing_design_guide.html) --- ### Monsieur Gusteau's Favorite Desserts In Monsieur Gusteau's bakery, the staff are preparing for the holiday season. In order to optimize the time they spend in the kitchen, they want to know which dessert is their customers' favorite! They've decided to try and sort through the order slips from the past month to figure out which dessert is most popular. For the following tasks, you can assume an order is just the name of a dessert (though capitalization might be inconsistent!) 1. **Task:** Write a function `dessert_matches` that takes a `order` (string) and a `dessert` (string) and returns a boolean indicating whether the order is for the given dessert, regardless of how the order and dessert have been capitalized (that is, ignore case). *Note:* You can lowercase a string `s` in Python by writing `s.lower()` (where `s` gets replaced by the string you want to lowercase). As in Pyret, you have Boolean operators `and`, `or`, and `not`. 2. **Task:** Write a function `any_orders_for` that takes a `dessert` (string) and `orders` (list of orders) and returns a boolean indicating whether any orders were for the given dessert (case insensitive). 3. **Task:** Write a function `count_orders_for` that takes a `dessert` (string) and a `orders`, a list of orders, and returns the number of orders that were for the given dessert (case insensitive). 4. **Task:** Write a function `record_order` that takes in `orders`, a list of orders, and `new_order`, a new order, and adds the order to the end of the list. This function should not return anything! ### *CHECKPOINT:* Call over a TA once you reach this point. ___ ## Part 3 - Slicing :::spoiler **String Slicing** (click for more details!) To do many of the following problems, you will need to do something called *string slicing*. String slicing is the Python equivalent of Pyret's `string-substring`, but winds up being a lot more powerful.<br> Let's say you have the string `str = "Beep!"`. Just like in Pyret, strings in Python are **0-indexed:** - The "B" is at **index 0** - The "e"s are at **indices 1 and 2** - The "p" is at **index 3** - The "!" is at **index 4** To access the characters at those indices, use the notation: `str[index]`. For example, ``` >>> str[0] "B" >>>str[4] "!" ``` If you want to slice a string into a substring of *more than one character*, use the notation: `str[start:end]` where `start` represents the first index of the substring (inclusive) and `end` represents the last index of the substring (exclusive). For example, ``` >>> str[0:5] "Beep!" >>> str[1:4] "eep" >>> str[2:3] # note that this is equivalent to str[2] "e" >>> str[2:2] # no characters in range, so this outputs the empty string "" ``` That's all you need to know for this assignment, but Python has a few additional shortcuts for string slicing that might be useful in the long run: - If you **leave off the start or end index**, Python assumes you want to start at 0 and end at the end of the string: ``` >>> str[:3] # equivalent to str[0:3] "Bee" >>> str[3:] # equivalent to str[3:5] "p!" >>> str[:] # equivalent to str[0:5] "Beep!" ``` - **Negative numbers** let you index from the end of a string ``` >>> str[-1] # equivalent to str[4] "!" >>> str[-5] # equivalent to str[0] "B" >>> str[-3:-1] # equivalent to str[2:4] "ep" ``` ::: --- **Task:** Write a function `first_three_letters(dessert: str, orders: list)` that takes in a dessert name and all orders and returns a list of all orders for desserts with the same first three letters in its name as `dessert` (regardless of capitalization). You can assume all strings in this function are at least three letters long. ___ ### *CHECKPOINT:* Call over a TA once you reach this point. ___ ## Success! Congratulations! You've gotten more practice with Python, and working at Gusteau's bakery has shown you that [Anyone Can Cook](https://www.youtube.com/watch?v=HVZvD3TZcCI)! ------ > Brown University CSCI 0111 (Fall 2020) > Do you have feedback? Fill out [this form](https://forms.gle/BuehRpxWnX97xYB68).