############################################### # COMS3101.004 - Perl - Homework 1 # # Please fill in your solutions for part 1 to 4 # below. ############################################### $title = " COMS3101.004 - Perl - Homework 1 (35) "; print "\n", "*" x 60, "\n"; print "*", " " x ((60-length($title))/2-1), $title, " " x ((60-length($title))/2-1), "*"; ################################################ # Part 1 - Build A Hash (4 marks) # # Given an array, build a hash with each element of # the array as the key and the number of characters # in the array element as the value. # # For example, given @arr = ("Columbia", "Yale") # build %hash such that $hash{"Columbia"} = 8 # and $hash{"Yale"} = 4. This should work for an # arbitrary sized array. # # Extra credit (+4 marks) for the shortest code # # Hint: You can do this without using any perl loop print "\n", "*" x 60, "\n"; print "Part 1 - Build A Hash (4)\n"; print "*" x 60, "\n"; @arr = ("Columbia", "Yale", "Princeton", "Brown"); %hash = (); #YOUR CODE STARTS HERE #YOUR CODE ENDS HERE foreach (@arr) { print "$_ : $hash{$_}\n"; } ################################################ # Part 2 - Polynomial Evaluation (6 marks) # (Arrays and Loops) # # A polynomial is an expression of the form # a + bx + cx^2 + dx^3 ... (^ indicates exponentiation) # # Assume a polynomial is represented by an array named # @coeffs which contains co-efficients of increasing # degree terms # # Example: # (12,5,2) ==> 12 + 5x + 2x^2 # (3,0,4) ==> 3 + 4x^2 # (0,0,3,6) ==> 3x^2 + 6x^3 # # Given @coeffs and $x, evaluate the polynomial. # Assign the result to $res # In the second example above, $res gets the value 67. # # Hint: You will need a loop on @coeff. print "\n", "*" x 60, "\n"; print "Part 2 - Polynomial Evaluation (6)\n"; print "*" x 60, "\n"; @coeffs = (12, 5, 2); $x = 5; #YOUR CODE STARTS HERE #YOUR CODE ENDS HERE print "Polynomial evaluates to : $res\n"; ################################################ # Part 3 - Case Insensitive Lookup (10 marks) # (Hashes, Conditionals, Subroutines) # # Implement a subroutine CI_Lookup which looks up a hash # irrespective of the cAsE of letters in the key. # # The hash %hash is defined globally. All keys of %hash are # either all UPPERCASE or all lowercase. For any given key, # only one of either UPPERCASE or lowercase version will be # present as a key in the %hash. i.e. it may contain one of # either "JEN" or "jen" as key, but not both. (Note that, both are valid # distinct keys as far a Perl is concerned, but assume our hash # will have only either one. # # $key is passed as an argument to CI_Lookup. # Irrespective of the cAsE of any letter of $key, CI_Lookup should # return the value in the %hash. It should return the string "NIL" if the $key is # not present in either UPPER or lower case. # # Write the CI_Lookup subroutine print "\n", "*" x 60, "\n"; print "Part 3 - Case Insensitive Lookup (10)\n"; print "*" x 60, "\n"; %hash = ( "mango" => 4, "TANGO" => 5, "ZAMBA" => 3.4, "zumba" => 6, "jango" => 3 ); print "Value : ", CI_Lookup("tAnGo"), "\n"; print "Value : ", CI_Lookup("zynGa"), "\n"; print "Value : ", CI_Lookup("JanGo"), "\n"; print "Value : ", CI_Lookup("zamba"), "\n"; sub CI_Lookup { #YOUR CODE STARTS HERE #YOUR CODE ENDS HERE } ################################################ # # Part 4 - Word Count (15 marks) # (Input/Output, Hashes, Sort) # # The file article.txt contains a recent CNN article. # Create a file wordcounts.txt containing the # list of unique words and corresponding frequencies in the # increasing order of frequencies. Also assign the # number of unique words to the vaiable $count. # # Assume a space character splits a line to words. # For each line read from the article.txt, # you can simply split the line using space as the # delimeter to get the list of words. In other words, # do not worry about punctuations etc. # # For example, "He said, it's OK" would be 4 words # ("He", "said,", "it's", "OK) # # Each line in wordcounts.txt should contain the # word and the frequency separated by space. E.g., # # the 35 # an 45 # very 3 # print "\n", "*" x 60, "\n"; print "Part 4 - Word Count (15)\n"; print "*" x 60, "\n"; #YOUR CODE STARTS HERE #YOUR CODE ENDS HERE print "Number of unique words : ", $count, "\n"; print "\n";