21.3 (Collections Terminology) Describe each of the following:
ICollection
Array
IList
load factor
hash-table collision
space/time trade-off in hashing
dictionary
21.4 (Enumerator Members) Explain briefly the operation of each of the following enumerator-related methods:
GetEnumerator
Current
MoveNext
21.5 (IDictionary<K,V>
Methods and Properties) Explain briefly the operation of each of the following methods and properties of interface IDictionary<K,V>
, which both Dictionary
and Sort-edDictionary
implement:
Add
Keys
Values
ContainsKey
21.6 (LinkedList
without Duplicates) Write an app that reads in a series of first names and stores them in a LinkedList
. Do not store duplicate names. Allow the user to search for a first name.
21.7 (Summarizing Characters with SortedDictionary
) Modify the app in Fig. 21.4 to count the number of occurrences of each letter rather than of each word. For example, the string "HELLO THERE"
contains two H
s, three E
s, two L
s, one O
, one T
and one R
. Display the results.
21.8 (SortedDictionary
of Colors) Use a SortedDictionary
to create a reusable class for choosing from some of the predefined colors in class Color
(in the System.Drawing
namespace). The names of the colors should be used as keys, and the predefined Color
objects should be used as values. Place this class in a class library that can be referenced from any C# app. Use your new class in a Windows app that allows the user to select a color, then changes the background color of the Form
.
21.9 (Duplicate Words in a Sentence) Write an app that determines and displays the number of duplicate words in a sentence. Treat uppercase and lowercase letters the same. Ignore punctuation.
21.10 (Using a List
) Write an app that inserts 25 random integers from 0 to 100 in order into an object of class List
. The app should calculate the sum of the elements and the floating-point average of the elements. Use loops to help perform the calculations.
21.11 (Reversing a LinkedList
) Write an app that creates a LinkedList
object of 10 characters, then creates a second list object containing a copy of the first list, but in reverse order. Do not call LinkedList
method Reverse
.
21.12 (Prime Numbers and Prime Factorization) Write an app that takes a whole-number input from a user and determines whether it’s prime. If the number is not prime, display the unique prime factors of the number (use a List<int>
to store the factors). Remember that a prime number’s factors are only 1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied together, the result is 54. For the number 54, the prime factors output should be 2 and 3.
21.13 (Bucket Sort with LinkedList<int>
) In Exercise 18.7, you performed a bucket sort of int
s by using a two-dimensional array, where each row of the array represented a bucket. If you use a dynamically expanding data structure to represent each bucket, you do not have to write code that keeps track of the number of int
s in each bucket. Rewrite your solution to use a one-dimensional array of LinkedList<int>
buckets.
21.14 (Sieve of Eratosthenes with BitArray
) Investigate class BitArray
in namespace System.Collections.Specialized
, then use it to reimplement Exercise 8.27’s Sieve of Eratosthenes algorithm. Measure the performance of this version vs. the one in Exercise 8.27. Which is faster?
Sections 21.11–21.12 presented several functional programming examples with LINQ and PLINQ—showing simpler ways to implement tasks that you programmed in earlier chapters. The following exercises ask you to reimplement some other of the book’s earlier examples with LINQ extension methods, lambdas and functional-programming techniques.
21.15 (Generating the Values in an Array) Figure 8.4 filled an array with the even integers from 2 to 10. Use the technique shown in lines 13–15 of Fig. 21.9 to create an array containing the even integers from 2 to 10.
21.16 (Generating the Values in an Array) Modify Exercise 21.15 to fill an array of 20,000,000 elements. Perform the task using both LINQ and PLINQ and display the percentage difference in processing time as we did in Fig. 21.9.
21.17 (Processing Employees) Use LINQ to map the List<Employee>
in Fig. 12.9 to a List
of anonymous objects in which each object contains an Employee
’s name and earning. When a Base-PlusCommissionEmployee
is encountered, give a 10% base-salary increase without modifying the original BasePlusCommissionEmployee
object. Display the names and earnings.
21.18 (Credit-Inquiry Program) The class File
provides static
method ReadLines
, which returns an IEnumerable<string>
representing the lines of text in a file. Reimplement the credit-inquiry program of Fig. 17.7 to use functional-programming techniques to separately filter the accounts that have credit, debit or zero balances.
21.19 (Advanced: Rolling a Die 60,000,000 Times) Figure 8.8 rolled a die 60,000,000 times and used an array to summarize the frequencies of each face. Use the technique shown in lines 13–15 of Fig. 21.9 to generate 60,000,000 random numbers from 1–6 representing the faces on a six-sided die. Rather than converting the IEnumerable<T>
to an array, use the GroupBy
extension method to summarize the results.
GroupBy
’s argument is a Func
delegate that receives one argument and returns a value, which
value => value
GroupBy
uses to create a collection of all elements with that value. For this exercise, you’ll group the elements by their random values, so use the lambda GroupBy
returns an IEnumerable
containing IGrouping
objects for each group. Each IGrouping
is an IEnumerable
(so you can iterate through all the elements in that group) and also has a Key
property representing the unique value that was used to group the elements in that IGrouping
. For the random die values, there will be a total of six IGrouping
objects for the values 1–6.
Use the OrderBy
extension method to order the IGrouping
s by Key
. Then iterate over the results and display them in tabular format as in Fig. 8.8. To determine the number of occurrences of each face, simply call the Count
extension method for the current IGrouping
.
21.20 (Advanced: Rolling a Die 60,000,000 Times) Modify Exercise 21.19 to map each IGrouping
to a new object that contains Face
and Frequency
properties where the Face
is the IGrouping
’s Key
and the Frequency
is the Count
of elements in the IGrouping
.
21.21 (Advanced: Summarizing Survey Results) Use the techniques of Exercise 21.19 to reimplement the student-poll analysis app of Fig. 8.9.