teaching machines

CS 145 Lecture 27 – Array Miscellany

November 3, 2014 by . Filed under cs145, fall 2014, lectures.

Agenda

TODO

Program This 1

You like to run 5Ks, maybe an occasional 10K. Sadly, you are too cheap to actually compete in races. However, that doesn’t stop you from thinking where you might place, were you to have run. You like to download race statistics, such as this one that your instructor participated in this past summer in San Diego, and find where you would have finished.

How might you teach a machine to determine your hypothetical place in the race, given your average running time?

Program This 2

Your work for U-WE-C, a digital security firm. You are inspecting the log files on a system, but you only care about the last 10 entries in the file. How do you print just these last 10? Your file reading API lets you read lines sequentially, from beginning to end.

Code

HypoRace.java

package lecture1103;

public class HypoRace {
  public static void main(String[] args) {
    double[] times = {
      16.533333,
      16.783333,
      16.9,
      17.233333,
      17.516667,
      17.616667,
      18.183333,
      18.333333,
      18.35,
      18.366667,
      18.466667,
      18.5,
      18.55,
      18.85,
      18.95,
      19.466667,
      19.483333,
      19.55,
      19.6,
      19.633333,
      19.85,
      20.0,
      20.016667,
      20.05,
      20.133333,
      20.216667,
      20.216667,
      20.25,
      20.516667,
      20.516667,
      20.533333,
      20.6,
      20.633333,
      20.7,
      20.716667,
      20.783333,
      20.816667,
      20.833333,
      20.85,
      20.933333,
      20.95,
      21.016667,
      21.016667,
      21.033333,
      21.05,
      21.05,
      21.066667,
      21.15,
      21.183333,
      21.233333,
      21.233333,
      21.233333,
      21.35,
      21.35,
      21.366667,
      21.4,
      21.416667,
      21.433333,
      21.433333,
      21.433333,
      21.433333,
      21.45,
      21.466667,
      21.466667,
      21.466667,
      21.466667,
      21.5,
      21.566667,
      21.566667,
      21.6,
      21.616667,
      21.683333,
      21.7,
      21.716667,
      21.816667,
      21.866667,
      21.883333,
      21.916667,
      21.933333,
      22.016667,
      22.033333,
      22.066667,
      22.1,
      22.166667,
      22.183333,
      22.2,
      22.25,
      22.35,
      22.35,
      22.383333,
      22.4,
      22.433333,
      22.433333,
      22.466667,
      22.466667,
      22.483333,
      22.483333,
      22.5,
      22.516667,
      22.533333,
      22.533333,
      22.583333,
      22.6,
      22.7,
      22.733333,
      22.783333,
      22.783333,
      22.783333,
      22.816667,
      22.833333,
      22.85,
      22.866667,
      22.9,
      23.083333,
      23.083333,
      23.1,
      23.133333,
      23.25,
      23.266667,
      23.283333,
      23.333333,
      23.366667,
      23.383333,
      23.383333,
      23.416667,
      23.45,
      23.466667,
      23.533333,
      23.55,
      23.733333,
      23.75,
      23.766667,
      23.783333,
      23.783333,
      23.85,
      23.866667,
      23.883333,
      23.95,
      23.95,
      23.95,
      24.0,
      24.016667,
      24.016667,
      24.033333,
      24.033333,
      24.133333,
      24.166667,
      24.2,
      24.2,
      24.216667,
      24.233333,
      24.25,
      24.283333,
      24.333333,
      24.35,
      24.35,
      24.45,
      24.466667,
      24.466667,
      24.483333,
      24.5,
      24.533333,
      24.533333,
      24.583333,
      24.633333,
      24.633333,
      24.666667,
      24.666667,
      24.683333,
      24.7,
      24.7,
      24.733333,
      24.733333,
      24.783333,
      24.783333,
      24.8,
      24.8,
      24.816667,
      24.833333,
      24.85,
      24.883333,
      24.95,
      25.033333,
      25.033333,
      25.066667,
      25.066667,
      25.1,
      25.116667,
      25.133333,
      25.166667,
      25.166667,
      25.183333,
      25.233333,
      25.333333,
      25.333333,
      25.35,
      25.383333,
      25.383333,
      25.416667,
      25.45,
      25.483333,
      25.516667,
      25.533333,
      25.55,
      25.566667,
      25.566667,
      25.6,
      25.6,
      25.6,
      25.633333,
      25.633333,
      25.65,
      25.666667,
      25.683333,
      25.7,
      25.7,
      25.7,
      25.7,
      25.716667,
      25.75,
      25.766667,
      25.766667,
      25.833333,
      25.833333,
      25.866667,
      25.9,
      25.916667,
      25.95,
      25.966667,
      25.983333,
      26.0,
      26.016667,
      26.016667,
      26.016667,
      26.1,
      26.1,
      26.1,
      26.116667,
      26.133333,
      26.133333,
      26.15,
      26.183333,
      26.283333,
      26.316667,
      26.333333,
      26.383333,
      26.4,
      26.433333,
      26.433333,
      26.483333,
      26.5,
      26.5,
      26.516667,
      26.533333,
      26.533333,
      26.633333,
      26.716667,
      26.766667,
      26.783333,
      26.9,
      26.933333,
      26.966667,
      27.0,
      27.0,
      27.016667,
      27.033333,
      27.066667,
      27.066667,
      27.1,
      27.116667,
      27.133333,
      27.183333,
      27.2,
      27.2,
      27.3,
      27.316667,
      27.333333,
      27.333333,
      27.35,
      27.35,
      27.35,
      27.433333,
      27.45,
      27.45,
      27.466667,
      27.483333,
      27.533333,
      27.55,
      27.566667,
      27.566667,
      27.583333,
      27.583333,
      27.6,
      27.683333,
      27.7,
      27.75,
      27.766667,
      27.766667,
      27.783333,
      27.983333,
      28.033333,
      28.066667,
      28.25,
      28.266667,
      28.283333,
      28.333333,
      28.383333,
      28.4,
      28.4,
      28.416667,
      28.416667,
      28.466667,
      28.483333,
      28.516667,
      28.516667,
      28.533333,
      28.533333,
      28.566667,
      28.583333,
      28.583333,
      28.6,
      28.65,
      28.65,
      28.666667,
      28.666667,
      28.7,
      28.733333,
      28.766667,
      28.766667,
      28.783333,
      28.8,
      28.816667,
      28.866667,
      28.916667,
      28.933333,
      28.933333,
      28.95,
      28.966667,
      29.016667,
      29.016667,
      29.033333,
      29.116667,
      29.166667,
      29.183333,
      29.283333,
      29.366667,
      29.466667,
      29.6,
      29.616667,
      29.65,
      29.683333,
      29.733333,
      29.816667,
      29.85,
      29.866667,
      29.9,
      29.9,
      29.95,
      29.966667,
      30.016667,
      30.016667,
      30.033333,
      30.05,
      30.066667,
      30.1,
      30.116667,
      30.166667,
      30.183333,
      30.233333,
      30.233333,
      30.233333,
      30.25,
      30.25,
      30.266667,
      30.316667,
      30.333333,
      30.35,
      30.35,
      30.483333,
      30.5,
      30.5,
      30.6,
      30.616667,
      30.633333,
      30.783333,
      30.783333,
      30.816667,
      30.833333,
      30.95,
      31.05,
      31.116667,
      31.133333,
      31.25,
      31.283333,
      31.316667,
      31.383333,
      31.4,
      31.4,
      31.55,
      31.7,
      31.733333,
      31.766667,
      31.766667,
      31.833333,
      31.883333,
      32.05,
      32.216667,
      32.283333,
      32.316667,
      32.383333,
      32.383333,
      32.7,
      32.733333,
      32.75,
      32.833333,
      32.833333,
      32.95,
      32.966667,
      33.0,
      33.066667,
      33.066667,
      33.283333,
      33.316667,
      33.333333,
      33.35,
      33.416667,
      33.566667,
      33.566667,
      33.733333,
      33.9,
      33.916667,
      33.933333,
      34.0,
      34.033333,
      34.116667,
      34.166667,
      34.233333,
      34.3,
      34.366667,
      34.45,
      34.516667,
      34.55,
      34.583333,
      34.733333,
      34.766667,
      34.85,
      34.966667,
      35.0,
      35.15,
      35.333333,
      35.333333,
      35.4,
      35.433333,
      35.533333,
      35.9,
      35.966667,
      36.066667,
      36.216667,
      36.266667,
      36.283333,
      36.433333,
      36.666667,
      36.866667,
      37.083333,
      37.15,
      37.166667,
      37.35,
      37.516667,
      37.866667,
      38.116667,
      38.333333,
      38.416667,
      38.533333,
      38.65,
      38.65,
      38.8,
      38.883333,
      38.9,
      39.2,
      39.3,
      39.3,
      39.3,
      39.45,
      39.466667,
      39.766667,
      40.033333,
      40.083333,
      40.15,
      40.733333,
      40.733333,
      40.766667,
      40.866667,
      41.033333,
      41.1,
      41.366667,
      41.666667,
      42.316667,
      42.366667,
      42.483333,
      42.816667,
      42.85,
      42.866667,
      44.133333,
      44.45,
      44.85,
      45.25,
      46.05,
      46.05,
      46.066667,
      46.083333,
      46.166667,
      46.6,
      47.483333,
      47.716667,
      47.816667,
      48.25,
      48.883333,
      48.9,
      49.75,
      50.816667,
      51.233333,
      51.233333,
      51.333333,
      52.5,
      53.983333,
      53.983333
    };
    
    int place = findPlaceInTimes(18.383333, times);
    System.out.println(place);
  }
  
  public static int findPlaceInTimes(double myTime, double[] times) {
    for (int i = 0; i < times.length; ++i) {
      if (myTime < times[i]) {
        return i + 1;
      }
    }
    
    return times.length + 1;
  }
}

Birthday.java

package lecture1103;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Birthday {
  public static void main(String[] args) throws FileNotFoundException {
    Scanner in = new Scanner(new File("/Users/johnch/Desktop/bdays.csv"));

    int[][] counts = new int[12][31];
//    counts[0] = new int[31];
//    counts[1] = new int[29];
//    counts[2] = new int[31];
//    counts[3] = new int[30];

    while (in.hasNextInt()) {
      int month = in.nextInt();
      int day = in.nextInt();
      ++counts[month - 1][day - 1];
    }

    in.close();
    
//    System.out.println(Arrays.deepToString(counts));
    
    System.out.println("P2");
    System.out.println("31 12");
    System.out.println("2");
    for (int m = 0; m < counts.length; ++m) {
      for (int d = 0; d < counts[m].length; ++d) {
        System.out.println(counts[m][d]);
      }
    }
  }
}

Haiku

on CDL at 16:
Dad bought my first car
Not a car, but a school bus
“You might need the room”