COMP151 - Lab 1 & Homework 1
The goal today is to get back into programming with python and to get some code that can be used as objects of study. Don’t worry about being rusty. Just take stock of where you’re at, ask questions, ask for help, and take note of any gaps in you skill set that might need attention. We’ll get this started in lab, continue a bit in class, and then you’ll wrap it up as homework.
Setup
For this lab (and all future labs and projects) you need:
- a python development environment (python + vs-code)
- the pytest module
- git version control
- a github account
It is assumed that you have all of this from COMP151 and prior COMP courses. If you don’t, or if you need help getting things setup, then please ask for help. You’ll know you’re ready to go if you got this lab from github classroom and can run lab1.py
.
The Problems
Below are two sets of problems. The first are straight forward. The second are a bit more interesting and will prove to be more interesting objects of study. You need to write functions and pytest unit tests for all of them. Use basic python. Stay away from modules other than typing
and pytest
. When in doubt, ask if you can use something. We’ll play with python’s toys eventually, but we also want to be able to code from first principles.
Tier 1
Solutions to these problems follow from iterate and accumulate.
- Compute the product of all the numbers in a list.
- Compute the product of all the numbers in a list that are not multiples of 3.
- Transform (in-place) a list of strings to a list of first-letter…last-letter or only-letter. For example, apple becomes a…e and a becomes a.
- Like above but produce a new list, don’t modify the given list.
- Given a list of strings, compute a new list containing all the odd length strings from that list.
- Compute a function that returns true or false if a list of strings contains a string that begins and ends with the same letter/symbol.
Tier 2
These problems can be solved with the iterate and accumulate strategy, but they are not as formulaic and the resultant solution is not necessarily “the best”.
- Prefix sum: Given a list of numbers, compute a new list where location i in the computed list is the sum of everything from 0 to i in the given list.
- Compute true or false if a given list of numbers contains any duplicates.
- Compute true or false if a string is a palindrome.
- Compute a list of numbers that is the reverse of a given list of numbers.
- Repeat the above reverse problem but do it in-place.