In Class: Week 6


Create a week06 subdirectory in your cs21/class directory, and copy over my week06 files into this new directory:

    $ cd 
    $ cd cs21/class
    $ pwd
      /home/your_user_name/cs21/class
    $ mkdir week06        
    $ cd week06
    $ pwd
      /home/your_user_name/cs21/class/week06

    $ cp ~newhall/public/cs21/week06/* .
    $ ls
      fivequestions.py  randOps.py      stringOps.py
      listOps.py        squareevens.py  syracuse.py
	
After we do in-class work together, you can copy over my in-class solutions into your cs21/class/week06 directory by doing the following (my solutions will be in files named with the in-class file name prefixed with "tias_"):
$ cd
$ cd cs21/class/week06
$ cp ~newhall/public/cs21/week06/tia* .
It is good to wait a couple hours after class to do this because I occationally add additional comments to these files for you.
We are going to do some of the following together in class:
    Practice with while loops

  1. open syracuse.py in vim. We will implement a function syracuse, that takes a starting value and computes the Syracuse sequence for the value (printing out each term that is computed).

    Once we get that to work, lets see what happens if the user enters a negative value.

    To kill a program with an infinite loop, hold down the CNTRL and the c keys (CNTL-C).

    Now, lets write a function GetPositiveInt, that prompts the user to enter a positive int value and returns it to the caller. The function will keep prompting the user to enter a vaild value until s/he enters a positive value, only then will the function return the value to the caller. We will test this function by replacing the call to input in main with a call to the GetPositiveInt function.

  2. open fivequestions.py. We will write the getYesOrNo function together.

    strings and lists as objects

  3. On the class schedule for week6 is a link to information about using strings and lists as objects. Read through this, and look at the sample code that you copied over from my week06 subdirectoy. We will talk about using lists and strings as objects in class, but you may want to look at this and try out some of the example code you copied over (stringOps.py, listOps.py).

    Some practice with functions with list and object parameters:

  4. open squareevens.py.
    1. We are going to add two functions:
      1. squareEvens that takes a list of integers and squares all the even elements in the list.
      2. printList, that takes a list of integers and prints it to the terminal, prining 10 list elements per line.

    2. Now let's change squareEvens so that it returns the number of elements it changed in the list.

      the random library

    3. One of the files you copied over, randomOps.py, contains some examples of using the random library. We will start by looking at the documentation about using the random library off the week6 schedule, and then take a look at this code and running it to make sure we understand what it is doing.

    4. Now let's add a new function, createRandomList, to squareevens.py. This is a function that will return a list of some some number of elements between 10 and 20 (use the random library to pick the number of elements value), where each element in the list is some value between 0 and 1000 (again, use the random library to pick the value of each item you add to the list).

      Next, let's change the main function to make a call to our new function to create the list, main should print the list out, call squareEvens, and then print out the list again.