The goals of this lab are to:
This assignment consists of two parts: a written homework and a programming portion. Write your written solution using $\LaTeX$. You can use python, C, C++, or java for your coding portion, but I strongly encourage you to use python. Submit both the written and coding portion using github. This is an individual assignment. It is OK to discuss approaches at a high level, but you must do your own write-up. Do not share your write up with others, and do not read other student's solutions. Please refer to the syllabus or ask me if you have questions about the policy.
As with all lab assignments this semester, you will use git, a version control system, to get starting files as well as handing in your assignments. When you start an assignment, you'll execute a git command to grab some starting files. Similarly, when you want to submit an assignment, you'll execute a couple of git commands. Later on in the course, you'll be using git to facilitate paired lab assignments.
Git is a large piece of software, and there is a lot to learn with git. Fortunately, it is easy to get up and running on git with just a small number of commands.
cd ~/ mkdir cs46 cd cs46
git clone git@github.swarthmore.edu:CS46-S18/lab01-<user>.git ./lab01where <user> is your ITS username.
As a reminder, the git commit cycle is git add, git commit, git push. Don't forget to git push when you have completed an assignment.
$ python power.py 3 '' 'a' 'b' 'c' 'ab' 'ac' 'bc' 'abc' $ python power.py 1 '' 'a'Your algorithm should run in $O(n2^n)$ time. That may sound terrible, but recall that the size of the power set of $n$ elements is $2^n$, and each element is set of size $O(n)$, so you can't really make it faster and still display all the elements. Also, $n < 15$ is small. That said, what you can't do is produce the power set in unsorted order and then run a sort algorithm to put the elements in short lexicographical order. That would take a minimum of $O(n^2 2^n)$ time. So, you have to be clever and generate the elements of the power set in order. One way to do this is to consider that sets of size $k+1$ are generated by adding 1 element to sets of size $k$. Thus, if you have a list of all subsets of size $k$ you can inductively build larger lists. Again, you have to be a bit careful in the way you design this step so that it doesn't take too long and generate a bunch of elements that don't belong. Python has a set and frozenset class. You are not allowed to use these. Python lists and maybe a dictionary are the only tools you need.
import sys print("command: %s" % sys.argv[0]) if len(sys.argv) < 2: print("Usage %s <n>" % sys.argv[0]) sys.exit(1) n=int(sys.argv[1]) print nThe function ord(let) returns the integer ASCII value of character let; the function chr(val) returns the ASCII character with integer value val. Note ord(chr(val))==val and chr(ord(let))==let. You can construct a set of n letters using the following snippet
def shiftLet(ch, num): return chr(ord("a")+num) n=int(sys.argv[1]) alpha=[] for i in range(n): alpha.append(shiftLet("a",i))If L1 and L2 are two lists, you can combine the two sets using either L3=L1+L2 or L1.extend(L2). The first example makes a new list L3, while the second modifies L1 by appending all elements in L2.
Use strings to represent elements of the power set. Instead of writing {a, b, c} you can just use 'abc'
Sometimes it might be helpful to copy a list. Use the string slicing operator l[:] to do this, e.g.,
l=range(5) l2=l #this probably doesn't do what you want l.append(10) print(l) print(l2) #------------ l=range(5) l2=l[:] #OK l.append(10) print(l) print(l2)
Don't loop over a list that you are modifying inside the loop. This is weird, and often wrong, e.g.,
l = range(5) for i in l: l.append(2*i) #wee infinite loop #--better, non-infinite solution below -- l = range(5) newL = [] for i in l: newL.append(2*i) l.extend(newL)
Once you have edited the files, you should publish your changes using the following steps:
$ git add hw01.tex power.pyThe git add step adds modified files to be part of the next commit to the github server.
$ git commit -m "completed lab1"The git commit step makes a record of the recently added changes. The -m "completed lab1" part is a descriptive message describing what are the primary changes in this commit. Making a commit allows you to review or undo changes easily in the future, if needed.
$ git pushThe git push command sends your committed changes to the github server. If you do not run git push before the submission deadline, the instructor will not see your changes, even if you have finished coding your solution in your local directory.
If you make changes to files after your push and want to share these changes, repeat the add, commit, push loop again to update the github server.
To recap, the git commit cycle is git add, git commit, git push. Don't forget to git push when you have completed an assignment.
You can review the basic git commands on the github help pages. Eventually, you should get in the habit of using git status to see if everything has been published, but we will talk about this more throughout the semester.
Remember to add, commit, push. You may commit and push as often as you like, and only the most recent push will be graded.