Skip to main content

C# Assignment - HashTable, Dictionary Lab and HashSet

C# Data structure assignment - HashTable, Dictionary Lab and HashSet




Question 1

In this lab, we will create a very simple HashTable

a. Create a console application
b. Create a new Hash table.
c. Add the following key value pair to the hash table
Josh", "Joshua Whitetail"
"Sam", "Samantha Bernie"
"Eli", "Elizabeth Spiegel"
"Will", "William Johnson"
"Don", "Donald Regan"
d. Use foreach, to print the key and value to console window.


Question 2

 

Write a console application that does the following:
a. The main method should  take a String array (use the one given  below), and store the array's elements into a HashSet to eliminate duplicates, and finally output the number of unique strings in the array and print the strings to the console.
String [] sampleStrings = {"that", "that", "is", "is", "that", "that", "is", "not", "is", "not", "is", "that", "it", "it", "is"};
Output:
Number of Unique Strings: 4
that, is, not, it

b. Add new code to the above application to create a Dictionary that holds the number of occurrences of each unique string in the array, and finally output the unique strings and the number of occurrences of that string to the console.
that  - 5 occurrences
is  - 6 occurrences  and so on....


Question 3
Zimmermann Telegram is a popular german secret communication that was decoded by British Intelligence during World War I. Google to learn more about Zimmermann Telegram.  Zimmermann Telegram used Code book cipher. In this exercise we will create a simple codebook cipher to encrypt and decrypt text messages. The code book cipher will use the following code book extracted cryptology notes by Christensen (2005). This code book has codes for popular words and codes for 26l alphabets to spell the words that are not included in the codebook.

a 141 about 592 all 653 an 589 and 793 are 238 as 462 at 643 b 383 be 279 been 502 but 884 by 197 c 169 call 399 can 375 come 105 could 820 d 974 day 944 did 307 do 816 down 406 e 286 each 208 f 998 find 628 first 034 for 825 from 342 g 117 get 067 go 982 h 148 had 086 has 513 have 282 he 306 her 647 him 093 his 844 how 609 i 550 I 582 if 231 in 725 into 359 is 408 it 128 its 481 j 450 k 284 l 102 like 701 long 938 look 521 m 559 made 644 make 622 many 948 may 954 more 930 my 381 n 964 no 428 not 810 now 975 number 665 o 334 of 461 oil 756 on 482 one 337 or 867 other 831 out 652 p 712 part 019 people 091 q 456 r 856 s 692 said 346 see 861 she 045 so 432 some 664 t 821 than 339 that 607 the 260 their 249 them 273 then 724 there 870 these 066 they 063 this 155 time 881 to 488 two 152 u 092 up 096 use 254 v 715 w 364 was 367 water 892 way 590 we 360 were 011 what 330 when 305 which 204 who 213 will 841 with 469 word 519 would 415 write 116 x 094 y 572 you 703 your 657 z 595

Write a program that create and populates a dictionary with the code book given above. Then use that dictionary to encrypt or decrypt messages.  The main method and the output expected is shown below:

static void Main(string[] args)
        {
            CodeBookCipher cipher = new CodeBookCipher();
            Console.WriteLine(cipher.encrypt("This is a test message"));
            Console.WriteLine(cipher.decrypt("155 408 141 821 286 692 821 559 286 692 692 141 117 286"));
            Console.ReadLine();

        }


 155 408 141 821 286 692 821 559 286 692 692 141 117 286
 this is a t e s t m e s s a g e



Question 4
  1. What is the motivation to use HashTable or Dictionary over other collections such as List or ArrayList?
  2. When do you use a HashSet?




Reference

Christensen, C (2005), Cryptology notes on Codes and Nomenclators 

Hashtable and Dictionary Collection Types - https://msdn.microsoft.com/enus/library/4yh14awz(v=vs.110).aspx

Comments

Popular posts from this blog

CUMIPMT and CUMPRINC function

CUMIPMT Cumulative interest payment function allows you to calculate the interest paid for a loan or from an investment from period A to period B. When getting a loan, CUMIPMT function can be used to calculate the total amount of interest paid in the first five months or from period 12 to period 20. A period can be a month, a week or two week. Loan Amount : 350,000.00 APR: 4.5% Down payment: 0.00 Years: 25 Payment per year: 12 From the above data, we can calculate the following: No of Period: 25 × 12 = 300 Periodic Rate: 4.5/12 = 0.375% Here is how you will substitute these values into the function. = CUMIPMT (periodic rate, No of period, vehicle price, start period, end period,  ) = CUMIPMT (0.375, 300, 350000, 1, 5, 0) In an excel worksheet, we use cell address instead of actual values as shown below: Here is the formula view of the worksheet: CUMPRINC Another related function is CUMPRINC. CUMPRINC function is used to calculate cumul

Excel PMT Function

PMT function is very useful for calculating monthly payment required to payback a loan or mortgage at a fixed rate. This function require a minimum of three inputs, periodic rate, number of periods, present value or the loan amount. Here is a simple example. Home Loan: 350,000.00 Interest rate: 4.5% Number of years to repay the loan: 25 Note: To calculate monthly payment, we need to find the monthly rate and number of months as shown above. Then it is simply a matter of substituting the values into the payment function, as shown in the formula view below.

BCG's Brand Advocacy Index

The Boston Consulting Group's (BCG) Brand Advocacy Index (BAI) is a metric developed to help companies measure the degree of customer advocacy for their brands. BAI focuses on the likelihood of customers to recommend a brand to others, which is a powerful indicator of brand strength and customer loyalty. Unlike other customer satisfaction or loyalty metrics, BAI emphasizes the importance of customer referrals and word-of-mouth marketing. BAI is calculated based on a survey where customers are asked about their willingness to recommend a brand to their friends, family, or colleagues. The responses are then used to compute a score, which ranges from -100 to 100. A higher BAI score indicates that a brand has more advocates who are likely to recommend the brand to others, while a lower score suggests that the brand has fewer advocates or even a higher number of detractors. BCG's research has shown that companies with higher BAI scores tend to experience higher growth rates and bett