A document from MCS 275 Spring 2023, instructor Emily Dumas. You can also get the notebook file.

MCS 275 Spring 2023 Worksheet 2

  • Course instructor: Emily Dumas

Topics

Like worksheet 1, this worksheet consists of Python programming exercises on topics typically covered in prerequisite courses of MCS 275. Compared to worksheet 1, this one is a bit more complex and the problems are more open-ended.

Where's the new material?

Each worksheet covers the previous week's lecture material. Last week's lectures were used to review some MCS 260 topics, so we still don't have new material to cover this time.

Resources

These things might be helpful while working on the problems. Remember that for worksheets, we don't strictly limit what resources you can consult, so these are only suggestions.

1. Increasing run length

Suppose you have a list of integers. Even if the list is not entirely in increasing order, there are sometimes parts of the list that are in increasing order.

For example, consider L = [1,21,4,17,39,35,22,0,6,2,5]. The list is not in increasing order, but the first two elements (i.e. L[0:2] or [1,21]) are in increasing order. Also, if you take the slice L[2:6], which is [4,17,38,39], that is in increasing order.
Let's call a slice of a list that is in increasing order an increasing run. In the example given above, the longest increasing run is L[2:6], of length 4.

Write a function IRL(L) that takes a list L whose elements can be compared to one another (e.g. a mix of integers and floats, or a collection of strings) and returns the length of the longest increasing run within L.

Special cases (not necessarily requiring special handling, but explained here to clarify the extremes):

  • If L is empty, the function should return 0
  • If L is in decreasing order, the function should return 1 because a slice with one element can be considered to be in "increasing order", and that would be the longest increasing run.

Here are some sample outputs:

In [47]:
IRL([40, 1, 63, 20, 53, 9])  # [1,63] and [20,53] are the longest, each has length 2
Out[47]:
2
In [48]:
IRL([22, 40, 47, 46, 26, 45, 25, 30, 47, 40]) # [22,40,47] and [25, 30, 47] are the longest
Out[48]:
3
In [49]:
IRL([78, 26, 1, 20, 72, 19, 44, 50, 59, 67]) # [19, 44, 50, 59, 67] is the longest
Out[49]:
5
In [53]:
IRL([1,2,3,4,5,6,7,8]) # The entire list, of length 8, is increasing!
Out[53]:
8
In [80]:
IRL([]) # no elements = no increasing runs = max increasing run length 0
Out[80]:
0
In [55]:
IRL([275]) # [275] is the longest increasing run
Out[55]:
1
In [81]:
IRL([10,8,6,4,2]) # [10] and [8] and [6] and [4] and [2] are the longest, each has length 1
Out[81]:
1

Hint: Here is one possible strategy: Use a for loop to scan through the list, keeping track of the current element and the previous one. As you go, make note of how many successive elements have been increasing. Use another variable to hold the largest number of increasing elements seen so far, and update it as needed on each iteration.

2. Zitterbewegung

Zitterbewegung is a German word meaning "jittery motion".

This is an exercise in object-oriented programming.

Make a class ZBPoint that stores a point (x,y) in the plane with integer coordinates.

The constructor should take (x,y) as arguments and store them as attributes.

Add a __str__ method Printing an instance of this class should produce a nice string like

ZBPoint(17,-2)

Also include a method .jitter() that will move the point up, down, left or right by one unit. The direction should be chosen at random (e.g. using random.choice or random.randint from the Python random module that you can import with import random.)

The code below demonstrates how you would use this class to create a point at (20,22) and then let it jitter about for 30 steps, printing the location each time.

In [61]:
P = ZBPoint(20,22)
for _ in range(30):
    print(P)
    P.jitter()
ZBPoint(20,22)
ZBPoint(20,21)
ZBPoint(20,20)
ZBPoint(19,20)
ZBPoint(19,19)
ZBPoint(18,19)
ZBPoint(18,18)
ZBPoint(18,19)
ZBPoint(18,18)
ZBPoint(19,18)
ZBPoint(19,19)
ZBPoint(20,19)
ZBPoint(19,19)
ZBPoint(19,20)
ZBPoint(19,21)
ZBPoint(18,21)
ZBPoint(18,22)
ZBPoint(18,23)
ZBPoint(17,23)
ZBPoint(17,22)
ZBPoint(17,23)
ZBPoint(18,23)
ZBPoint(17,23)
ZBPoint(16,23)
ZBPoint(17,23)
ZBPoint(17,22)
ZBPoint(17,23)
ZBPoint(17,22)
ZBPoint(17,23)
ZBPoint(17,24)

3. Text circles

Write a program circle.py that takes one command line argument, r, and then prints 40 lines of text, each containing 80 characters. The output should make a picture of a circle, using characters as pixels. When r=20 the circle should be slightly too big to fit in the block of text, and when r=1 it should be nearly too small to have any empty space inside.

For example, here is possible output for r=5. (It will only look right if lines of width 80 characters don't wrap around on your browser display. If it looks wrong, try zooming out or maximizing the window.)
















                                   @@@@@@@@@@@                                  
                                 @@@@       @@@@                                
                               @@@             @@@                              
                              @@@               @@@                             
                             @@@                 @@@                            
                             @@@                 @@@                            
                             @@@                 @@@                            
                              @@@               @@@                             
                               @@@             @@@                              
                                 @@@@       @@@@                                
                                   @@@@@@@@@@@

And here is possible output for r=15:






                                @@@@@@@@@@@@@@@@@                               
                           @@@@@@@             @@@@@@@                          
                       @@@@@                         @@@@@                      
                     @@@@                               @@@@                    
                  @@@@                                     @@@@                 
                 @@@                                         @@@                
               @@@                                             @@@              
              @@@                                               @@@             
             @@                                                   @@            
            @@                                                     @@           
           @@                                                       @@          
          @@@                                                       @@@         
          @@                                                         @@         
          @@                                                         @@         
         @@@                                                         @@@        
         @@@                                                         @@@        
         @@@                                                         @@@        
          @@                                                         @@         
          @@                                                         @@         
          @@@                                                       @@@         
           @@                                                       @@          
            @@                                                     @@           
             @@                                                   @@            
              @@@                                               @@@             
               @@@                                             @@@              
                 @@@                                         @@@                
                  @@@@                                     @@@@                 
                     @@@@                               @@@@                    
                       @@@@@                         @@@@@                      
                           @@@@@@@             @@@@@@@                          
                                @@@@@@@@@@@@@@@@@

This exercise is deliberately a little vague about how to go about this, but here is a hint: Each character you print corresponds to a certain point (x,y) in the plane, with character 40 on line 20 corresponding to (20,20). Calculating those coordinates inside of the main loop will be useful. Then, how would you decide whether to print a " " or a "@"?

Note: The reason for using 40 lines of 80 characters is that a character in the terminal is often approximately twice as tall as it is wide.

Bonus round: Skip this part unless you have extra time, or want an extra challenge after lab ends. Modify the program so that it makes circles with softer edges, like this:





                                 ...............                                
                           ..***@@@@@@@@@@@@@@@@@***..                          
                       ..**@@@@@@@******.******@@@@@@@**..                      
                     .*@@@@@**...               ...**@@@@@*.                    
                  .**@@@@*..                         ..*@@@@**.                 
                .*@@@@*..                               ..*@@@@*.               
               .*@@@*.                                     .*@@@*.              
             .*@@@*.                                         .*@@@*.            
            .*@@@*.                                           .*@@@*.           
           .*@@*.                                               .*@@*.          
          .*@@*.                                                 .*@@*.         
         .*@@*.                                                   .*@@*.        
         .@@@*.                                                   .*@@@.        
        .*@@*.                                                     .*@@*.       
        .*@@*.                                                     .*@@*.       
        .@@@*                                                       *@@@.       
        .@@@.                                                       .@@@.       
        .@@@*                                                       *@@@.       
        .*@@*.                                                     .*@@*.       
        .*@@*.                                                     .*@@*.       
         .@@@*.                                                   .*@@@.        
         .*@@*.                                                   .*@@*.        
          .*@@*.                                                 .*@@*.         
           .*@@*.                                               .*@@*.          
            .*@@@*.                                           .*@@@*.           
             .*@@@*.                                         .*@@@*.            
               .*@@@*.                                     .*@@@*.              
                .*@@@@*..                               ..*@@@@*.               
                  .**@@@@*..                         ..*@@@@**.                 
                     .*@@@@@**...               ...**@@@@@*.                    
                       ..**@@@@@@@******.******@@@@@@@**..                      
                           ..***@@@@@@@@@@@@@@@@@***..                          
                                 ...............

4. Recurrence time histogram

This builds on the ZBPoint class from problem 2.

Suppose you start a ZBPoint at (0,0) and then repeatedly call the method .jitter() until the first time it returns to (0,0). The number of steps it takes to do this is called the recurrence time. It's possible that this may never happen, but that is very unlikely. (It can be shown that the probability it will eventually return to (0,0) is 100%.)

Write a program that runs this process 1000 times and records the recurrence times. (It's OK to give up on a point if it's taken 200 steps and hasn't yet returned to (0,0).) The program should then print a table showing how many times each recurrence time was seen (a histogram). Here's an example of what it might look like:

Ran 1000 random walks starting at (0,0).  Of these:
 235 returned to (0,0) after   2 steps
  76 returned to (0,0) after   4 steps
  51 returned to (0,0) after   6 steps
  31 returned to (0,0) after   8 steps
  24 returned to (0,0) after  10 steps
  17 returned to (0,0) after  12 steps
  18 returned to (0,0) after  14 steps
   7 returned to (0,0) after  16 steps
   6 returned to (0,0) after  18 steps
  13 returned to (0,0) after  20 steps
   7 returned to (0,0) after  22 steps
   8 returned to (0,0) after  24 steps
   9 returned to (0,0) after  26 steps
   4 returned to (0,0) after  28 steps
   2 returned to (0,0) after  30 steps
   5 returned to (0,0) after  32 steps
   3 returned to (0,0) after  34 steps
   5 returned to (0,0) after  36 steps
   2 returned to (0,0) after  38 steps
   2 returned to (0,0) after  40 steps
   3 returned to (0,0) after  42 steps
   2 returned to (0,0) after  44 steps
   1 returned to (0,0) after  46 steps
   4 returned to (0,0) after  48 steps
   2 returned to (0,0) after  50 steps
   1 returned to (0,0) after  52 steps
   7 returned to (0,0) after  54 steps
   1 returned to (0,0) after  58 steps
   3 returned to (0,0) after  60 steps
   4 returned to (0,0) after  62 steps
   4 returned to (0,0) after  64 steps
   2 returned to (0,0) after  66 steps
   1 returned to (0,0) after  68 steps
   2 returned to (0,0) after  70 steps
   1 returned to (0,0) after  72 steps
   3 returned to (0,0) after  76 steps
   2 returned to (0,0) after  78 steps
   1 returned to (0,0) after  80 steps
   2 returned to (0,0) after  82 steps
   1 returned to (0,0) after  84 steps
   2 returned to (0,0) after  86 steps
   2 returned to (0,0) after  92 steps
   2 returned to (0,0) after  94 steps
   1 returned to (0,0) after  96 steps
   1 returned to (0,0) after  98 steps
   2 returned to (0,0) after  106 steps
   1 returned to (0,0) after  108 steps
   2 returned to (0,0) after  110 steps
   4 returned to (0,0) after  116 steps
   2 returned to (0,0) after  118 steps
   1 returned to (0,0) after  122 steps
   1 returned to (0,0) after  124 steps
   1 returned to (0,0) after  126 steps
   1 returned to (0,0) after  128 steps
   3 returned to (0,0) after  130 steps
   1 returned to (0,0) after  134 steps
   2 returned to (0,0) after  136 steps
   2 returned to (0,0) after  138 steps
   1 returned to (0,0) after  142 steps
   1 returned to (0,0) after  144 steps
   2 returned to (0,0) after  146 steps
   1 returned to (0,0) after  156 steps
   1 returned to (0,0) after  158 steps
   1 returned to (0,0) after  160 steps
   3 returned to (0,0) after  162 steps
   1 returned to (0,0) after  166 steps
   1 returned to (0,0) after  174 steps
   2 returned to (0,0) after  178 steps
   1 returned to (0,0) after  180 steps
   3 returned to (0,0) after  184 steps
   1 returned to (0,0) after  190 steps
   2 returned to (0,0) after  198 steps
 376 didn't return to (0,0) after 200 steps