Counting Multiple Things

< CS101

Now that we have table counting, the natural thing to want to do is count multiple things to compare them.

Do more boy or girl names end with "y"? We want a program which by the end prints "girl count:nn" and "boy count:nn", counting whatever characteristic we are looking for. This is beginning to look like an actual, practical use of computers to sift through data.

One approach is to use two (or more) counters, one for each case we want to count. Here we'll have one boy counter (count1) and one girl counter (count2). Initialize both counters to 0 before the loop. Then in the loop, have two if statements, one for each case we want to count. Inside each if-statement, increment the appropriate counter. At the end of the loop, print the counters.


table5-1

It's possible to write the above code in different ways, but we will use that simple pattern: one if-statement for each case we want to count, and the if-statements are all in the loop, one after the other.

Names ending in a, i o?

Code example with 3 counters.


table5-2

Class Survey

As another example of a table, we have the class survey of data from the live class at Stanford with people's favorite movies and what have you. The questions for this survey were:

The survey answers are automatically translated to a google spreadsheet which can be exported in csv table. This data is available in the file "survey-2013.csv". This also illustrates that .csv format's role as an interchange format between systems.

The field names in the table are: color, tv, movie, sport, book, soda. The convertToLowerCase() function of the table changes all the text the table contains to lower case. This simplifies our logic, so we don't have to worry if someone typed in "Blue" or "blue" .. in the table it will always be the lowercase "blue" form. I cleaned up the data a bit for consistency, changing "Dark Blue" to just "Blue" and the many spellings of "Coca-Cola" to just "Coke", and things like that.

Survey Code Example Problems

These can all be written using our "loop containing series of if-statements" form.

1. Write code to just print the soda field of each row, so we can get a feel for what people typed in.

2. Count soda favorites: sprite, root beer, coke

3. Variant on above: use || to lump together "coke" with "diet coke", "root beer" with "dr. pepper", "sprite" with "sierra mist"

4. Count sports: soccer and volleyball (top 2 in this survey)

5. Variant on above: look only at soccer players. Within that group, count "coke" vs. "sprite"


table5-3

Table code solutions:

// 1. just print the soda field
table = new SimpleTable("survey-2013.csv");
table.convertToLowerCase();

for (row: table) {
  print(row.getField("soda"));
}

// 2. count 3x sodas
table = new SimpleTable("survey-2013.csv");
table.convertToLowerCase();

count1 = 0;
count2 = 0;
count3 = 0;
for (row: table) {

  if (row.getField("soda") == "sprite") {
    count1 = count1 + 1;
  }

  if (row.getField("soda") == "root beer") {
    count2 = count2 + 1;
  }
  
  if (row.getField("soda") == "coke") {
    count3 = count3 + 1;
  }

}
print("sprite count:", count1);
print("root beer count:", count2);
print("coke count:", count3);

// 3. count 3x sodas, with variants
table = new SimpleTable("survey-2013.csv");
table.convertToLowerCase();
count1 = 0;
count2 = 0;
count3 = 0;
for (row: table) {

  if (row.getField("soda") == "sprite" ||
      row.getField("soda") == "sierra mist") {
    count1 = count1 + 1;
  }

  if (row.getField("soda") == "root beer" ||
      row.getField("soda") == "dr. pepper") {
    count2 = count2 + 1;
  }
  
  if (row.getField("soda") == "coke" ||
      row.getField("soda") == "diet coke") {
    count3 = count3 + 1;
  }
}
print("sprite + sierra mist count:", count1);
print("root beer + dr. pepper:", count2);
print("coke + diet coke count:", count3);


// 4. Count soccer vs. volleyball
table = new SimpleTable("survey-2013.csv");
table.convertToLowerCase();
count1 = 0;
count2 = 0;
for (row: table) {

  if (row.getField("sport") == "soccer") {
    count1 = count1 + 1;
  }

  if (row.getField("sport") == "volleyball") {
    count2 = count2 + 1;
  }
}
print("soccer count:", count1);
print("volleyball count:", count2);

// 5. Focus on soccer, coke vs. sprite
table = new SimpleTable("survey-2013.csv");
table.convertToLowerCase();
count1 = 0;
count2 = 0;
for (row: table) {

  if (row.getField("sport") == "soccer"&&
      row.getField("soda") == "coke") {
    count1 = count1 + 1;
  }

  if (row.getField("sport") == "soccer"  &&
      row.getField("soda") == "sprite") {
    count2 = count2 + 1;
  }
}
print("soccer coke:", count1);
print("soccer sprite:", count2);

> exercises