Don't like this style? Click here to change it! blue.css

Daily Challenge 4:

So this was the first time a daily challenge was truly pushing you. Turn in anything, it's OK, but if you were able to animate your random walk then congratulations! If you were not able to, congratulations, you're more ready to understand the material deeply today! If you didn't make a real attempt (at least an hour of effort) then you should probably read up more at http://prof.ninja. You can't grow without consistent effort.

Without further ado:

This version was slightly hacked to allow being shown on this site. A version using our GitHub Repo as a starting point would look something like this:

Class 5: Lists

We're slowly getting used to storing data in variables and altering data with functions and loops and user input. Today we will learn about groups of related data that travel together called lists.

Lists: keeping it all together

We learned a little bit about lists when we saw for loops. I want to dive deeper and learn to THINK in lists.

First of all here are some basic, sensible, lists:

You can access particular elements with square brackets in several cool ways:

Access Task 1: Make a list containing the first 5 numbers (1-5). Use square brackets to print the 3rd number in your list. Use square brackets to print a list of the last 3 numbers. Use square brackets to print the 3rd, and 4th elements in a list.

Access Task 2: Alter your list from a list of numbers to a list of strings, and observe the outputs.

String Task 3: Alter your list into a string of five characters, like "SPRINT". Observe the outputs.

Prediction Task 4: Before you run the following snippet, try to determine what will be displayed. Then run and confirm.

Prediction Task 5: I've added two digits to the loop, now can you predict?

Often you need the length of a list. Python provides this with a function len. It works like this:

Practice Task 6: We didn't look at the extended version of range yet. It can take as many as three arguments which are range(start, stop, step). Use len to determine the number of elements in range(20, 1, -4). Then display the elements in range(20, 1, -4).

Building lists

If we had to type every list that we ever wanted to work with, they might not be very useful. But we can work with lists in code.

Research Task 7: Fire up cloud9 (any space will do) and launch ipython3. Create a list like this: mylist=[], use our tab completion trick to see all of the methods that we can do to a list. Read the documentation for mylist.append? and mylist.pop?. Can you phrase those actions in your own words to your neighbor?

By using append we can build and extend collections of data while our code runs. Here is an example:

Math Task 8: Extend the previous example by creating an array called sums. Add every number in the first list to every number in the second list and append their sum to sums. What pattern do you see? Discover a list method to determine the number of times that the number 30 appears in sums.

SPOILER ALERT: an extended solution on repl.it

Code Trace Task 9: What does the following code snippet do?

Just for fun here is an animated version:

Some coding patterns

For this mini-lesson we want to find the largest and smallest value in a list (Max and Min). This is a fairly common thing to do, it is also a nice thing to think about to help give you ways of using variables.

Note that I added a triple quote comment to open that function. In that comment I gave a contract which declares what someone using my function must provide and what my function will return.

If you provide your users a contract then life becomes smoother, you don't have to check for corner cases and they know what to expect. This is a good principle.

Another good principle is to write two or three test cases, before you even code your function. You can then state what value you expect in those test cases. This becomes a benchmark for your code.

Here is the same function using Python's built in assert keyword, which allows us to stop running the program if we're not getting expected behavior.

Assert Task 10: Change some of the assert lines so that they will fail. Observe the error message.

Creating Test Cases 11: I would like you to write a contract for a function which consumes a list of integers (with at least one integer) and returns the average of those integers. Begin by writing a "contract" and three assertions with some good test inputs and your expected outputs. For instance assert average([3, 5]) == 4.

Extend Pattern Task 12: Can you now create a function which takes the average of a list of values? (Follow the pattern of the min/max function)

Daily Challenge 5

Write a function, factors, which consumes a positive integer n and returns a list of all positive integers which divide n. Give three test cases. For instance assert factors(10) == [1, 2, 5, 10].