Table-6 Counting Multiple Things

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.


table6-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 (not one if inside another).

Expressions with counters

Same code as up above, but now we want to add a line of output of which gender has more names that end in y


table6-2

 

Solution:

if (boyCount > girlCount) {
    print("More boys names end in y")
}
if (girlCount > boyCount) {
    print("More girls names end in y")
}
if (girlCount == boyCount) {
    print("The same number of boys and girls names end in y")
}

The pattern is the same as above, except now we have three if statements at the end, one for counter1 being bigger, one for counter2 being bigger, and one for their being the same. Our pattern is that the order of the counters flip, but we always use a > sign.

3 Counters - ending in a, i, o?

Code example with 3 counters, just showing the natural extension of the 2-counters code above.


table6-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-2017.csv". This also illustrates that .csv format's role as an interchange format between systems.

Some data cleanup to make the answers consistent: changed "coca-cola" to "coke", "Navy" to "blue", "Dr. pepper" spelled with a period. Print the raw rows to see what the data looks like

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. Therefore our query code should always look for 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 counter pattern.

1. Write code to just print the soda field of each row, so we can get a feel for what people typed in. Note the effect of the convertToLowerCase() operation. Look at "color" and "sport" fields too.

2. Count 2 soda favorites: coke vs. sprite

3. Variant on (2) above, look only at people whose gender is not male.

4. Variant on (2), use || to lump together for counting "coke" with "dr. pepper"

5. Variant on (2), now output whether more people like coke or sprite instead of the counts

6. (You Try It) Count colors: green, purple, and blue

7. (You Try It) Variant of (6), count only gender male rows, then change to count fluid, then change to count female


table6-3

 

Table code solutions:

// 1. just print the soda field

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

// 2. count coke, sprite

cokeCounter = 0;
spriteCounter = 0;
for (row: table) {

  if (row.getField("soda") == "coke") {
    cokeCounter = cokeCounter + 1;
  }

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

}
print("coke count:", cokeCounter);
print("sprite count:", spriteCounter);

// 3. gender variant with &&
cokeCounter = 0;
spriteCounter = 0;
for (row: table) {

  if (!(row.getField("gender") == "male") &&
      row.getField("soda") == "coke") {
    cokeCounter = cokeCounter + 1;
  }

  if (!(row.getField("gender") == "male") &&
      row.getField("soda") == "sprite") {
    spriteCounter = spriteCounter + 1;
  }

}
print("coke count:", cokeCounter);
print("sprite count:", spriteCounter);

// 4. Use || to lump together multiple things

cokeCounter = 0;
spriteCounter = 0;
for (row: table) {

  if (row.getField("soda") == "coke" ||
      row.getField("soda") == "dr. pepper") {
    cokeCounter = cokeCounter + 1;
  }

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

}
print("coke + dr. pepper:", cokeCounter);
print("sprite:", spriteCounter);


// 5. Print which soda is more popular

cokeCounter = 0;
spriteCounter = 0;
for (row: table) {

  if (row.getField("soda") == "coke" ||
      row.getField("soda") == "dr. pepper") {
    cokeCounter = cokeCounter + 1;
  }

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

}

if (cokeCounter > spriteCounter) {
    print("coke/dr. pepper is more popular than sprite for CS101 students");
}
if (spriteCounter > cokeCounter) {
    print("sprite is more popular than coke/dr. pepper for CS101 students");
}
if (cokeCounter == spriteCounter) {
    print("CS101 students like coke and sprite equally");
}

// 6. Count green vs. purple vs. blue

greenCounter = 0;
purpleCounter = 0;
blueCounter = 0;
for (row: table) {

  if (row.getField("color") == "green") {
    greenCounter = greenCounter + 1;
  }

  if (row.getField("color") == "purple") {
      purpleCounter = purpleCounter + 1;
  }

  if (row.getField("color") == "blue") {
    blueCounter = blueCounter + 1;
  }

}
print("green count:", greenCounter);
print("purple count:", purpleCounter);
print("blue count:", blueCounter);

// 7. count colors, only looking at male
// to change to looking at fluid or female
// change "male" below to "fluid" or "female"

greenCounter = 0;
purpleCounter = 0;
blueCounter = 0;
for (row: table) {

  if (row.getField("gender") == "male" &&
      row.getField("color") == "green") {
    greenCounter = greenCounter + 1;
  }

  if (row.getField("gender") == "male" &&
      row.getField("color") == "purple") {
      purpleCounter = purpleCounter + 1;
  }

  if (row.getField("gender") == "male" &&
      row.getField("color") == "blue") {
    blueCounter = blueCounter + 1;
  }

}
print("green count:", greenCounter);
print("purple count:", purpleCounter);
print("blue count:", blueCounter);


> exercises