Compsci 101, Fall 2012, October 10

By entering your name/net-id below you indicate you are in class on October 10 to answer these questions and that you have answered them. Your name should not appear unless you are in class when answering these questions.
Name_________________   net-id _______   Name_________________   net-id _______


Name_________________   net-id _______   Name_________________   net-id _______

Snarf class-work stockstufff to work with these problems, see code here.

The class has been hired to test the random number generator in Python and to find trends in how some stocks trade over time. In particular we need to run two tests:

  1. Simulate tossing a coin and determine how many runs there are of either heads or tails and what the lengths of these runs are.

  2. Find runs in which the closing price of Google (traded as GOOG) increased over several days, e.g., GOOG closed at $706.04 on Sept 13 and the closing price increased on 9/14, 9/17, 9/18, 9/19, 9/20, 9/21, 9/24, 9/25, but then dropped. That's a run of nine increasing days. Find all the runs over a year of trading.

Coin Tosses

(see CoinTosser.py)

If the simulated coins are tossed and diagrammed as "HTTTHHTTHHHHTT" then there is a run of three tails and a run of four heads. Find all such runs of length three.

Class answers are the same in the code below because the random number seed is set to 1.

  1. In a run of 10,000 coins, what's the longest run of heads? of tails?

  2. How many runs of 10 are there (heads or tails)

    Stock Trading

    We have stock data code that uses the Yahoo Finance API to get data for any stock. The code gets all data for a stock, e.g., open, high, low, volume, etc., but just reports closing price and volume for the stock. The data is given in a list of tuples (a tuple is just like a list, but it's immutable, it can't change --- and uses parentheses in display instead of brackets). Here's the list returned by the call data = getWebData("GOOG", "20120913", "20121009")

    [
     ('2012-09-13', 706.04, 2659000),
     ('2012-09-14', 709.68, 2618500),
     ('2012-09-17', 709.98, 1508300),
     ('2012-09-18', 718.28, 2066800),
     ('2012-09-19', 727.5, 3098300),
     ('2012-09-20', 728.12, 2907400),
     ('2012-09-21', 733.99, 6359100),
     ('2012-09-24', 749.38, 3563800),
     ('2012-09-25', 749.16, 6058500),
     ('2012-09-26', 753.46, 5672900),
     ('2012-09-27', 756.5, 3931100),
     ('2012-09-28', 754.5, 2783500),
     ('2012-10-01', 761.78, 3168000),
     ('2012-10-02', 756.99, 2790200),
     ('2012-10-03', 762.5, 2208300),
     ('2012-10-04', 768.05, 2454200),
     ('2012-10-05', 767.65, 2735900),
     ('2012-10-08', 757.84, 1958600),
     ('2012-10-09', 744.09, 3003200)]
    
    
  3. Create a list comprehension that is just the closing/float values from a list data in the format above.
    
    
    
    
    
    
  4. Write code to find the longest run (just its length) of increasing closing prices for Google as traded in 2011.