Duke Computer Science Shield

CompSci 201: Data Structures & Algorithms

Spring 2013
Duke University Computer Science

Recitation 2: Maps and Sets

January 18, 2013

  1. The method below correctly counts and returns the number of different/unique words in an array.

    public int uniqueCount(String[] list) { Arrays.sort(list); String last = list[0]; int count = 1; // Invariant: count is number of unique words in list[0..k) for (int k = 1; k < list.length; k++) { if (!list[k].equals(last)) { last = list[k]; count++; } } return count; }

      Continuation of Question 3

      For example:
        23   apple
        5    banana
        16   lemon
        37   mango
      
      
      Don't worry about making the numbers line up. Write your code without adding a new loop or creating an array, but by adding a variable and print statements so unique words are printed in alphabetical order, with each word preceded by its number of occurrences. Complete your code by copying the code below into the textbox above. Don't forget to ensure your code prints the last unique value and its number of occurrences.

      public void uniqueCounts(String[] list) { Arrays.sort(list); String last = list[0]; int count = 1; // Invariant: count is number of occurrences of last seen so far for (int k = 1; k < list.length; k++) { if (!list[k].equals(last)) {

  2. The following questions are about the code Frequencies.java. Look at the code and observe the differences between doFreqsA and doFreqsB from that code.

    When the program is run using Melville's Bartleby The Scrivener with 4,256 unique words it generates the output as shown below (it's not side by side, but vertical when actually run-- below) doFreqsA on the left and doFreqsB on the right. This is just a small part of the complete output.
    
    total # words read: 14353			     
    6	A				      6	A				     
    1	According			      1	According			     
    1	Accordingly			      1	Accordingly			     
    1	Accordingly,			      1	Accordingly,			     
    1	Acting				      1	Acting				     
    1	Adam				      1	Adam				     
    time to complete: 1.859000		      time to completed 0.020000 
    


    Here's the output from Hamlet with 7,806 unique words
    
    total # words read: 31955
    1	&c.'                                  1	&c.'		  
    1	''Tis				      1	''Tis		  
    5	'A				      5	'A		  
    1	'A-down				      1	'A-down		  
    1	'Adieu,				      1	'Adieu,		  
    1	'And				      1	'And		  	  
    time to complete: 7.534000		      time to completed 0.037000
    


    Here's the output from A Scarlet Letter with 14,124 unique words:
    
    total # words read: 85754
    1	                                      1			  
    98	"				      98  "		  
    9	"A				      9	  "A		  
    1	"Adorn				      1	  "Adorn		  
    1	"Advise				      1	  "Advise		  
    1	"Ah!				      1	  "Ah!		  
    2	"Ah,				      2	  "Ah,		  	  
    time to complete: 36.026000		      time to completed 0.097000
    
    

    This code pertains to the previous two questions.

    The single line below occurs in main String[] words = s.useDelimiter("\\Z").next().split("\\s+"); We could replace this line with the following lines: ArrayList<String> list = new ArrayList<String>(); while (s.hasNext()){ list.add(s.next()); } String[] words = list.toArray(new String[0]);