CS 135 Winter 2015 Troy Vasiga Assignment: 6 Due: Language level: Allowed recursion: Files to submit: Tuesday, February 24, 2015 9:00pm Beginning Student with List Abbreviations Pure Structural Recursion, Structural Recursion with an Accumulator recursion.rkt, student.rkt, phone.rkt, a06bonus.rkt Warmup exercises: HtDP 12.2.1, 13.0.3, 13.0.4, 13.0.7, 13.0.8 Practise exercises: HtDP 12.4.1, 12.4.2, 13.0.5, 13.0.6 • Unless specifically asked in the question, you are not required to provide a data definition or a template in your solutions for any of the data types described in the questions. However, you may find it helpful to write them yourself and use them as a starting point. • In your solutions, if you create any data types yourself that are beyond the question descriptions, or data types discussed in the notes, your program file should include a data definition and a template (named my-<type>-fn for your particular <type>) for these cases. • Unless otherwise stated, if X is a known type then you may assume that (listof X) is also a known type. • You may use the abbreviation (list . . .) or quote notation for lists when defining constants for examples and tests. • In this assignment you will not be penalized for having an inefficient implementation (e.g. using append). Your Code Complexity/Quality grade will be determined by how clear your approach to solving the problem is. • You may reuse the provided examples, but you should ensure you have an appropriate number of examples and tests. • Your solutions must be entirely your own work. • For this and all subsequent assignments, unless otherwise stated, you should include the design recipe for all functions, including helper functions, as discussed in class. • Solutions will be marked for both correctness and good style as outlined in the Style Guide. • You may not use the Racket functions reverse, member?, or equal?, in any of your solutions, unless stated otherwise. • You must use the cond special form, and are not allowed to use if in any of your solutions. • You may only use the list functions that have been discussed in the notes, unless explicitly allowed in the question. Here are the assignment questions you need to submit. 1. Place your solutions to the following questions in recursion.rkt CS 135 — Winter 2015 Assignment 6 1 (a) Write the function after-n which consumes three parameters: the natural number n, the symbol to search for, v, and a list of symbols. The function after-n produces the list of symbols which occur after the nth occurrence of v in the consumed list. If v does not appear in the list at least n times, produce empty. For example, (after-n 2 ’a (list ’a ’b ’a ’e ’n ’d)) ⇒ (list ’e ’n ’d) (after-n 0 ’z (list ’a ’b ’a ’e ’n ’d)) ⇒ (list ’a ’b ’a ’e ’n ’d). (b) Write the function list-posn which consumes a list of lists of integers and a Posn (with both fields being positive integers), and produces an integer or false. The integer that is produced is the yth element of the xth list in the consumed list of lists. If there is no such element, the function produces false. For example: (list-posn (list (list 1 2 3) (list 4 5) (list 6 7 8)) (make-posn 2 1)) ⇒ 4 (list-posn (list (list 1 2 3) (list 4 5) (list 6 7 8)) (make-posn 1 4)) ⇒ false (c) Write the function every-nth which consumes a list and a positive integer n, and produces a list containing every nth element from the original list, where the first element in the list is at index 1. For example: (every-nth (list 1 2 3 4 5 6 7 8 9) 3) ⇒ (list 3 6 9) (every-nth (list 1 2 3 4) 1) ⇒ (list 1 2 3 4) (d) Write the function mult-score which consumes two lists of the same length. The first consumed list has the responses to multiple choice questions, where the response is one of ’A, ’B, ’C, ’D, ’E, ’blank. The second list has the correct answers to the corresponding questions, where each answer will be one of ’A, ’B, ’C, ’D or ’E. The score for each question is: • 5 if the answer is correct; • 2 if the answer is blank; • 0 if the answer is incorrect. The function mult-score should produce the total score, producing 0 if both lists are empty. 2. Place your answers for this question in the file student.rkt. (a) Write the function tallest which consumes a list of symbols (representing first names of students) and a list of positive numbers (representing the corresponding height of the students). You can assume that both lists are the same length and both are non-empty. The function should produce the name of the student who is the tallest: in the case of a tie, produce the name of the first student in the list with that maximal height. For this part, you must use pure structural recursion. (b) Write the function shortest which consumes a list of symbols (representing first names of students) and a list of positive numbers (representing the corresponding height of the students). You can assume that both lists are the same length and both are non-empty. The function should produce the name of the student who is the shortest: in the case of a tie, produce the name of the first student in the list with that minimal height. For CS 135 — Winter 2015 Assignment 6 2 this part, you must use accumulative recursion. Hint: write an accumulative helper function (with more parameters than shortest) that keeps track of the information you know so far as you progress down the list. That is, your main function does not need to use accumulative recursion, but your helper function should. (c) Write the function student-al which consumes a list of distinct symbols (representing first names of students) and a list of positive numbers (representing the corresponding height of the students). You can assume that the lists have the same length. The function student-al should produce an association list (i.e., a list of lists) with the keys being the student names (symbols) and the values being the height of the corresponding student (positive numbers). The order of the keys should be the same as the consumed list of names. (d) Write the function basketball which consumes an association list as created in the previous part (i.e., a list of lists, with the keys being the names as symbols, and the values being the numerical height of the students) and a positive height and produces the list of names (i.e., symbols) for those students that are at least as tall as the given height, in the same order that the names appear in the consumed list. 3. Many businesses use a mnemonic for their telephone number: FedEx uses “1-800-GoFedEx” and Bell uses “1-888-SKY-DISH” for satellite TV support. In the file phone.rkt: (a) Define an association-list named phone-letters that maps characters to digits. For example, #\a is the key for the value 2. Use the following mapping: abc → 2, de f → 3, ghi → 4, jkl → 5, mno → 6, pqrs → 7, tuv → 8, and wxyz → 9. That is, the keys a, b, and c all map to the same value (which is duplicated in the association list). The association-list should be sorted in alphabetical ordering by the keys, and contain only 26 keys. Note the extra letters for 7 and 9. (b) Write the function mnemonic->num. It consumes a string and an association list giving a mapping between characters and digits. It produces a natural number by substituting each character in the string with the appropriate digit from the association list. For uppercase letters, they should produce the value associated with the corresponding lowercase version. For example, (mnemonic->num "GoFedEx" phone-letters) produces 4633339. If any character (or its lowercase version) does not appear in the given association list, the function should produce the value false. If the string is empty, the function should produce 0. Hint: you should modify the lookup-al function from slide 06-57, and use it as a helper function for mnemonic->num. Restrictions: You may use string->list, char-downcase (to make characters lowercase), char-alphabetic? and the familiar list functions from class (first, rest, cons, list, etc.). You may not use list->string, reverse or string->number. Using accumulative recursion will be helpful. This concludes the list of questions for which you need to submit solutions. As always, check your email for the basic test results after making a submission. CS 135 — Winter 2015 Assignment 6 3 4. 5% Bonus: Write the inverse of mnemonic->num. That is, write num->possible-mnemonics. It consumes a natural number and an association list and produces all the possible mnemonics for that number as a list of lowercase strings, sorted in alphabetic order. For example, (define dig-to-char (list ; partial association list, used just for brevity in testing (list 4 (list #\g #\h #\i)) (list 6 (list #\m #\n #\o)) (list 5 (list #\j #\k #\l)))) (num->possible-mnemonics 5 dig-to-char) ⇒ (list "j" "k" "l") (num->possible-mnemonics 75 dig-to-char) ⇒ empty (num->possible-mnemonics 46 dig-to-char) ⇒ (list "gm" "gn" "go" "hm" "hn" "ho" "im" "in" "io") Note the “go”, as in “GoFedEx”, in the last example. You can use any built-in function in Beginning Student with List Abbreviations. Put your solution in a06bonus.rkt. Enhancements: Reminder—enhancements are for your interest and are not to be handed in. A cellular automaton is a way of describing the evolution of a system of cells (each of which can be in one of a small number of states). This line of research goes back to John von Neumann, a mathematician who had a considerable influence on computer science just after the Second World War. Stephen Wolfram, the inventor of the Mathematica math software system, has a nice way of describing simple cellular automata. Wolfram believes that more complex cellular automata can serve as a basis for a new theory of real-world physics (as described in his book “A New Kind of Science”, which is available online). But you don’t have to accept that rather controversial proposition to have fun with the simpler type of automata. The cells in Wolfram’s automata are in a one-dimensional line. Each cell is in one of two states: white or black. You can think of the evolution of the system as taking place at clock ticks. At one tick, each cell simultaneously changes state depending on its state and those of its neighbours to the left and right. Thus the next state of a cell is a function of the current state of three cells. There are thus 8 (23 ) possibilities for the input to this function, and each input produces one of two values; thus there are 28 or 256 different automata possible. CS 135 — Winter 2015 Assignment 6 4 If white is represented by 0, and black by 1, then each automaton can be represented by an 8-bit binary number, or an integer between 0 and 255. Wolfram calls these “rules”. Rule 0, for instance, states that no matter what the states of the three cells are, the next state of the middle cell is white, or 0. But Rule 1 says that in the case where all three cells are white (the input is 000, which is zero in binary), the next state of the middle cell is black (because the zeroth digit of 1, or the digit corresponding to the number of 20 s in 1, is 1, meaning black). In the other seven cases, the next state is white. This is all made clearer by the pictures at the following URL, from which the picture at the right is taken: http://mathworld.wolfram.com/CellularAutomaton.html Some of these rules, such as rule 30, generate unpredictable and apparently chaotic behaviour (starting with as little as one black cell with an infinite number of white cells to left and right); in fact, this is used as a random number generator in Mathematica. You can use DrRacket to investigate cellular automata and draw or animate their evolution, using the io.ss, draw.ss, image.ss, or world.ss teachpacks. Write a function that takes a rule number and a configuration of cells (a fixed-length list of states, of a size suitable for display) and computes the next configuration. CS 135 — Winter 2015 Assignment 6 5
© Copyright 2024