

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


To practice writing code that uses arrays, here is some practice:
Write a function called rescale that takes an array (of numbers) and a number, as inputs, and returns a new array in which each element of the new array is the corresponding element of the old array multiplied by the number (called the “scale factor”). This function should work for any size array with any number of dimensions. If this is too hard, start by writing one for vectors, then a separate one for two-dimensional arrays and then for three-dimensional arrays.
In Graham, chapter 4, the first exercise is a good one. The idea is to be able to quarter turn any size of square array, i.e., a 3 by 3 or 4 by 4 or larger, not just the 2 by 2 example he shows. So the function will take one input, an array, and return a new array that has the elements of the old one but in different places.
Beat the “N Log N” time limit for sorting! Let’s implement the linear time (proportional to N) sorting algorithm as demonstrated by Jane on Tuesday.
Here is a database of SSNs and names. The SSNs are only one digit - this sample comes from a very small country!
((5 "Kosmo Kramer") (9 "Jerry Seinfeld") (2 "Al Gore") (7 "Dick Cheney") (4 "Elain Bennis") (3 "Bill Clinton") (1 "George Bush") (0 "George Kastanza") (6 "Diane Feinstein") (8 "Joe Lieberman"))
Write a function that allocates and returns an array holding this database (size 10 will do - no additions to come later) and inserts these records (each sublist) into the database so that they are sorted by increasing SSN - and do this in a single pass through the input data. There’s your “time N” sorting algorithm!
Now let’s implement a constant-time lookup algorithm - that is, one in which it takes a fixed time to find an entry in a database no matter how large the database!
Describe (that is, is words or “pseudocode”) an algorithm which does the following. Given a suitably large initially- empty array (and “suitably large” could mean trillions of trillions of bins!), describe how you could enter records consisting of names. For simplicity, you can consider each record to be only a single character string naming a person.
In a real application, of course, the record would also contain other data as well, but for this problem all we care about is being able to retrieve the correct record FAST!
What you need to do is to figure out how to generate from each name a number that can be used as an index into this giant array. Make sure that different names lead to different numbers.
Then you can stuff the name into the array at the slot number given by the number you have just computed. The same algorithm can later be used to retrieve that name - in constant time, regardless of database size!
Now let’s make the problem a bit more realistic. Assume your computer has somewhat less than “trillions and trillions” of memory locations. For concreteness, assume you wish to enter at most 10 names (you might, for example, use the 10 names from Problem 1). Assume also that your computer is so old (a 1981 IBM-PC) that you can allocate an array of size 10 bins, at most.
Will your original algorithm still work? If not, what is the problem? How can you fix it? If you are feeling particularly daring, you might wish to implement your new “fixed-up” algorithm in real Lisp code and test it on a sample database (the names from Problem 1 or another set you make up).
Hint: You might find the built-in function SXHASH rather useful. Given a string, it returns a number somewhere in the fixnum range. It is possible (but unlikely) that it might return the same number for two differently-spelled strings. (It also happens to ignore alphabetic case, which for this problem is irrelevant anyway.)
Remember Problem 3 from Graham, Chapter 3, which asks you to generate an association list containing symbols and their occurrence counts in an input list? Try the same problem using an array to hold the intermediate count information.
For example, with the input:
The output would be:
The sublists in the output can be in any order. All that matters is that each contains two elements: a symbol, and that symbol’s count of occurrences in the input list.
Hints: You can obtain the length of the input list (using the built-in LENGTH), allocate an array of that size, and use this array to accumulate counts of symbols as you encounter them in a scan of the input list. Your array will end up being larger than necessary (if any symbols occur more than once in the input), but ignore that slight inefficiency. After recording the symbols you encounter and their counts in the array, you can scan the array to build the list of sublists to return as the main function’s final output.