Vous êtes sur la page 1sur 4

Homework 3: Working with Lists

CSci 1901, Summer 2012


Purpose
This assignment will give you more practice using computer science problem solving techniques while working with lists.

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.

Source Code and Template


Along with this homework write-up you are given (on the course web site) a homework template analogous to a template you would fill out for your weekly lab. This template contains an outline of this homework assignment and includes test cases that you should check to ensure correct values are returned when your code is run.

Comments and Loading (10 points)


Comments are a fundamental part of writing code. Be sure to document each procedure you write with the input(s) it takes, the output(s) it returns, and what it does. Commenting your code is worth 5 points on this assignment. Your file also needs to load correctly (it should print done after loading), displaying the results of all the test cases without errors. If it does not load, that is a good indication that you did something wrong. If you intended for something to error (such as a divide by zero test case), comment it out in the code and write an additional comment with it stating what happens and why it is commented out. You file loading properly is worth 5 points on this assignment.

Problem 1Manipulating Lists (15 pts)


(1a) (5 points)

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.

Problem 2Convert to Sorted Point List (20 pts)


In lab 4, you will construct a 2D point list. Write a procedure that, given some unsorted point list pt-list, returns the same list in ascending sorted order in distance from the origin (0 . 0). You may find it beneficial to use code from lab 4 after Wednesday to write convert-to-sorted-pt-list.

Problem 3Using Existing Functions with Lists (15 pts)


Here are some Scheme functions you have seen in Section 2.2: (map proc items) (page 105) (filter predicate sequence) (page 115) (accumulate op initial sequence) (page 116) Given a list of lists (each of which will contain 2 elements), such as (("sdas" 89) (239 10) ("asd" "asda") (1238 .12) (-53 "sad")), write as few lines of Scheme code as you can to accomplish the described task. All of the following can be done efficiently using one or more of the above procedures. On this problem, your answer will be graded not only on correctness, but on using the functions above to solve the problem by writing only a small amount of Scheme code. For most parts below, you should be able to write one line of Scheme code (perhaps including one or more lambda expressions) to accomplish the task. (3a) (5 points) Return the list with all numbers less than or equal to 100 removed. (Hint: Scheme has a number? procedure.) (3b) (5 points) Return the list with all the negative numbers made positive and then taken the square root of (for example, -9 = squareroot of 9 = 3.) (3c) (5 points) Return the list with each pair of numbers replaced with a list containing one element, the minimum of those two numbers. (ie, (9 5) => (5))

Problem 4Refactoring Lists (40 pts)


(4a) (20 points) For this problem, you are a TA for an introductory computer science course. Currently, you and your professor are having a terrible time managing students homework grades because of the data structure you are using to store them. With the existing system, student names and grades are stored in a single-level list and different student records are delimited by negative numbers. The students name always precedes his/her grades. E.g. Assume the following grade book: HW1 Ben Anderson 95 Mary Johnson 75 Michael Walter 80 HW2 90 78 68 HW3 100 79 0

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

In the end, we should be able to execute the following code:


(convert-grades ( Ben Anderson 95 90 100 -1 Mary Johnson 75 78 79 -5 Michael => ((100 90 95 "Ben Anderson") ( 79 78 75 "Mary Johnson") (0 68 80 "Michael Walter")) Walter 80 68 0))

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.

Vous aimerez peut-être aussi