Week 12
Loop before you Leap (Loops, part 2)
Weekly Summary
Recall from our last week that that we reminded ourselves of the definition of loops as:
This week we will expand our work to include loops that process Strings (an ordered sequence of characters) and Lists (an ordered sequence of data). These are not only common data structures in programming, but are often the types of data structures that "require" the use of loops in order to most efficiently solve the problems.
Learning Outcomes
By the end of the week students should be able to:
- Explain and/or properly use the following details of working with Strings and Lists:
- How to identify the length of a String or list using - len()
- How to identify a single item in a String or a List using indices - myString[index] or myList[index]
- How to access a slice of items in a String or a list - myString[index1:index2] or myList[index1:index2]
- How to create an empty list - mylist = []
- How to create an empty String - myString = ""
- How to "add to" a list - myList.append(newItem)
- How to "add to" a String - myString = myString + newString
- Reading Code
- Given a snippet of code that includes a String, indicate the results of the code.
- Given a snippet of code that includes a List, indicate the results of the code.
- Writing Code
- Given a problem statement that includes the name of a function and the parameters required by that function, write the Python code/definition of the provided function such that it solves the problem by returning correct output values.
- Debugging Code
- Given a problem statement and a non-working Python function that attempts to solve the problem, identify where the function is incorrect and explain how to fix it.
Learning Materials
POGIL Materials from the CoP
Prior to starting this week's materials you should make sure that you have done some activities that were completed at the Community of Practice event on Saturday, November 2nd. These include:
- Complete the POGIL models 3 through 5
- If helpful, watch the debrief videos that I made for your facilitators to help them facilitate the discussions about these Models:
- Model 3 - Lists of data
- Model 4 - Strings
- Model 5 - Positive indices, negative indices, and slicing
Textbook Readings
This week consists of two chapters from the textbook. But DON'T PANIC. A significant portion of this was covered in the POGIL activities already. Take your time and work through this material carefully.
- Log on to your online textbook
- Read the following sections from your textbook
- 7.1. A string is a sequence
- 7.2. Getting the length of a string using len()
- 7.3. Traversal through a string with a loop
- 7.4. String slices
- 7.5. Strings are immutable
- 7.6. Looping and counting
- 7.7. The in operator
- 7.8. String comparison
- 7.9. String methods
- 7.10. Parsing strings
- 7.11. Format operator
- 7.12. Debugging
- 9.1. A list is a sequence
- 9.2. Lists are mutable
- 9.3. Traversing a list
- 9.4. List operations
- 9.5. List slices
- 9.6. List methods
- 9.7. Deleting elements
- 9.8. Lists and functions
- 9.9. Lists and strings
- You may stop here. We will read the rest of the chapter later in the course.
Lesson Videos
In many of the following activities I will start by giving you the description of a program that you COULD try to write. If you do this, I strongly encourage you to give this an honest effort. Most of you will succeed. When you are done, or if you hit a stumbling block and can't finish it, you can/should watch the followup video where I show you how I would do this. This is kind of a step between the lesson videos and the paired programming activity. I want you to think about the problem. But I also will use these as a way to teach you something "new."
Again, use these in whatever manner most helps you. If you it helps for you to try it first, do that. If you would rather treat them as striaght out lecture videos, no problems.
- Using a for loop to solve String problems
- Counting Sentences
- Yes, I know that your textbook describes a .count() function that will do this. But I wanted ut to look at what that function might do.
- Problem Description
- My solution video, version 1
- My solution video, versions 2 and 3
- Where is this character?
- Yes, I know that your textbook describes a .find() function that will do this. But I wanted ut to look at what that function might do.
- Program Description
- My solution video, version 1
- My solution video, version 2 (that you might be tempted to try, but doesn't actually work)
- Decoding a secret message
- Counting Sentences
- Using a for loop to solve list problems
- Calculating the length of an enclosure
- My pizza bill at the food court
- Using a list (without a loop) to solve the Zodiac problem from week 8
Practice Materials
Paired Programming Activity
Please complete the following Paired Programming activity with your assigned partner for the week.
Additional Textbook Activities
These are great for some additional practice. You should complete these on your own rather than with a partner.
- 7.14. Multiple Choice Questions
- 7.15. Mixed-up Code Questions
- 7.16. Write-code questions
- 9.16. Multiple Choice Questions
- 9.17. Mixed-Up Code Questions
- 9.18. Write Code Questions
Code Walkthroughs
The following are videos and code solutions for the programs discussed in the Paired Programming Activity and Individual Program Activity. You SHOULD NOT view these until you have either solved the activities yourself - and you want to see how I would do it - or you have tried at least two different times and still are stumped. Don't jump to reading the "correct answer" too quickly. I think it is a GOOD thing to struggle with a problem for a day or two. We improve when we meet and overcome resistance. Learning comes from practice. You need to TRY before jumping to the solutions.
- LOOP 4 - countTimes()
- LOOP 5 - isIn()
- LOOP 6 - calculateBill.py [Somehow the video for this got lost and I want to quickly post a solution. So this is working code.]
Graded Materials
The following programs are formally graded as part of the Unit 3 "Problem Set." You should work on these only as an individual. While working on these problems you may refer to your notes, your textbook, any programs you wrote, and even my videos. However, you should limit your discussion of these programs with classmates. It is acceptable to discuss how you solved a problem in grand scheme of things, but you should never show your code to a classmate either as the person who is struggling or as the person who is helping a struggling classmate. You can talk ideas, but not specific solutions.
- Graded Lists and Strings problem set (Part 3.2 of Problem Set #3).