Homework 3 Due 10/12

(1) Basic Functions (20 pts)

Implement the following simple functions. No libraries are needed to implement them.

(1a) Exponents (10pts)

Write a function pow(a,b) that returns the value of a ^ b.

You MAY NOT use the built in ^ operator. Remember that by default ^ is only definied in the syntax of Lua, and that the Math library must be incldued for ^ to have meaning. Assume that the Math library is not loaded.

(1b) Array Summation (10 pts)

Write a function sigma that takes as it's input an array of numbers, and returns the sum of all the numbers added together. You can assume the array always contains only numbers.

(2) Advanced Functions (20 pts)

Functions in Lua are quite flexible. Variable numbers of parameters are handled gracefully and functions can return multiple values. The following questions will give you a chance to practice with these abilities.

(2a) Longest String (10 pts)

Write a function longestString that can take up to 4 strings, and returns the longest string.

You may find it helpful to look at the max function discussed in lecture.

(2b) Quadratic Formula (10 pts)

Write a function quadratic(a,b,c) that takes the coefficents for an equation of the format \( ax^2 + bx + c = 0 \) and returns the two values of x.

Reminder that the quadratic formula is \( x = \frac{-b \pm \sqrt{b ^ 2 - 4 a c}}{2a} \)

Hint: math.sqrt() is a function in the math library to find a square root.

(3) Boolean operations in Lua (20 pts)

The Boolean operators in Lua, and and or behave differently than most other programming languages. Remember and returns the first argument when false, while or returns the first argument when true.

For each of the following questions write what the example code does and why.

Example

  function f(a, b)
      return (a > b) and a or b
  end

Answer: The function f returns the maximum between a and b. When (a > b) is false, the and statement will return the first operand, so the statement becomes (a > b) or b. The or operator returns the second operand when the first is false, thus when (a > b) is false, b is returned. When (a > b) is true, the and statement returns a, so the statement becomes a or b. Since a is true, the or operator will return a.

(3a)Booleans I (10pts)

  t = {}
  function g(t,k)
    t[k] = t[k] or 0
  end

(3b) Booleans II (10 pts)

  function h(a)
    return (a > 0) and a or -a
  end

(4) Sorting the Turing Award Winners (40 pts)

The Alan M Turing Award is widely considered the most prestiguous award a computer scientist can recieve. It is named in honor of Alan Turing, OBE, one of the founders of computer science and awarded anually by the Association for Computing Machinery. Since last year, the award for the prize has been $1,000,000, the same amount as the Nobel Prize.

For this problem, you will be given a text file in the following format :

  1966   Alan Perlis      Compilers    Yale University
  1967   Maurice Wilkes    Hardware    University of Cambridge

The file is a tab delimited file with the following fields: year of the award, name, area of award, and institution

UPDATE (10/7): You can download it here. or use the following command to get it.

wget http://www.csee.umbc.edu/courses/331/fall15/05/hw/turing.txt

If you are working on GL, wget will not work, so use the following command to directy copy it from my public directory:

cp /afs/umbc.edu/users/b/w/bwilk1/pub/turing.txt .

(4a) Reading the file (10 pts)

Read the file into an array of tables.

Hint: The function fromCSV located at the bottom of http://www.lua.org/pil/20.4.html can be modifeid for use with tabs

(4b) Sort the table I (15 pts)

Sort the table alphabetically by the area of research the award was given for. Print the results in some neat form.

UPDATE (10/1) The update should look something like this:

1975    Allen Newell    Carnegie-Mellon University      AI
1971    John McCarthy   MIT     AI
2011    Judea Pearl     UCLA    AI
1969    Marvin Minsky   MIT     AI
1975    Herbert Simon   Carnegie-Mellon University      AI
1994    Raj Reddy       Carnegie-Mellon University      AI
1994    Edward Feigenbaum       Stanford University     AI
1986    John Hopcroft   Cornell University      Algorithms

(4c) Sort the table II (15pts)

Sort the table alphabetically by the institution of the winner. Print the results in some neat form.

UPDATE (10/1) The update should look something like this:

1983    Kenneth Thompson        Bell Telephone Laboratories     Operating Systems
1968    Richard Hamming Bell Telephone Laboratories     Coding Systems
1983    Dennis Ritchie  Bell Telephone Laboratories     Operating Systems
2004    Robert Kahn     CNRI    Internet
2007    Joseph Sifakis  CNRS    Model Checking
2007    Edmund Clarke   Carnegie Mellon University      Model Checking
1975    Herbert Simon   Carnegie-Mellon University      AI
1975    Allen Newell    Carnegie-Mellon University      AI

How to Submit

Your homework should be done on the GL systems. For question number 3, submit a text file named hw3.txt. For the other questions, write your answers in a file named hw3.lua.

Submit your homework using the following command:

submit cs331_bwilk1 hw3 hw3.txt hw3.lua

Please test out the submission command before the due date. If you have any problems submitting, email me.