Homework 3
Homework 3
Homework 3
Submission instructions
Follow the Submit Tool link from the course home page and follow the instructions to submit an electronic copy of the assignment under Homework 1. The submission should include your name, student ID number, and section filled out at the top of the template. The template should contain all of your code, and all answers to any short answer questions should be commented out. It should run correctly, and print the results for all test cases when (load hw1.scm) is typed in the Scheme interpreter. Please check that it does indeed run correctly before submitting.
Instructions
This homework assignment is to be done individually. The standard University of Minnesota rules of scholastic conduct apply, as do the rules of scholastic conduct described on the course syllabus. General discussion of the homework is allowed, but there is to be no sharing of code or answers with classmates. You may obtain help on this assignment during lab, TA or professor office hours, or by emailing any of the TAs for the course.
Write a function (mode lst), which returns the least frequently occurring value in a list. For instance, (reverse_mode '(1 2 2 3 3 3 4 4 4 4 )) returns 1, as it occurs in that list more than any other number. For this problem, you can assume that the input list will have only one reverse_mode; that is, you won't see an input such as '(2 2 4 4 5 5 5 5). You can also assume that the input list is SORTED in ascending order. Furthermore, if the input list is null, simply return '(). (Hint: You'll probably want to do this iteratively and keep a variable that keeps track of the number you've seen the least of so far. If you begin with this number equal to an infinitely large number, it will help, as it would be greater than anything else. Since there are no infinitely large numbers in Scheme, try -1 and use the procedure negative?) (1b) (10 points) Write a function (greatest-average lst1 lst2), which takes in two lists, finds the average of the values of each list, and then returns the greater of the two averages. For instance, (greatest-average '(1 2 3) '(4 5 6)) returns 5, because (1+2+3) / 3 < (4+5+6) / 3, and (4+5+6) / 3 is 5. If the averages of the two lists are equal, return the string equal. For this problem, do NOT use sum, accumulate, or any of the abstractions of that sort. Instead, write your own procedure that will calculate each of the averages, and then determine which to return.
The list corresponding to the given grade book would look like this:
( Ben Anderson 95 90 100 -1 Mary Johnson 75 78 79 -5 Michael Walter 80 68 0)
Your professor has decided that he would like grades stored in the following format:
((<score-n> <score-(n-1)> <score-(n-2)> <score-1> <student-name> ) (<score-n> <score-(n-1)> <score-(n-2)> <score-1> <student-name> ) <etc>)
where the grades are stored as a list of student-grade elements. Student-grade elements will be lists where the first element is the students name. What your professor would like you to do is write a procedure called convert-grades that will take in a list in the old structure and transform it to the new structure. You may use the built-in reverse and string? procedures. string? takes in one argument and will return true if the argument passed is a string and false otherwise. e.g. (string? a) => #t
(string? 0) => #f
You may assume that the gradebook data given to you is in the correct format to begin with. ("name" <score-1> <score-2> ... <score-n> <negative-number> <next-name> ...)
(4b) (15 points) Write a procedure called get-student-final-grade that takes in a students name and a grade book as stored in the new structure and returns the average of all that students scores. You may use the built in string=? procedure. String=? takes in two strings and will return true if they are the same and false otherwise. e.g. (string=? a a) => #t
(string=? a b) => #f In the end, we should be able to execute the following code:
(define gradebook ((Ben Anderson 95 90 100) ( Mary Johnson 75 78 79) (Michael Walter 80 68 0))) (get-student-final-grade Ben Anderson gradebook) => 95
If the student is not in the grade book, return -1. (4c) (5 points) Write test cases for each of the above problems (3a and 3b). Be sure to fully test the functionality of your code.