290 likes | 422 Views
Midterm Exam Answers. The Programming Language Landscape Robert Dewar. Question 1. Write a scheme function which takes two arguments, a list and an element and returns the number of times that the given element appears in the list. e.g. (count '(a b c d a a) 'a) = 3.
E N D
Midterm Exam Answers The Programming Language Landscape Robert Dewar
Question 1 • Write a scheme function which takes two arguments, a list and an element and returns the number of times that thegiven element appears in the list. • e.g. (count '(a b c d a a) 'a) = 3
Answer to Question 1 • Counting the number of elements in a list • (define (count list elmt) (cond ((null list) 0) ((eq elmt (car list)) (+ 1 (count (cdr list) elmt)) (T (count (cdr list) elmt)) ) )
Another answer to Question 1 • Avoid code duplication • (define (count list elmt) (if (null list) 0 (+ (if (eq elmt (car list)) 1 0) (count (cdr list) elmt)) ) )
Question 2 • Write a scheme function (together with any helper functions needed) that given a list, removes excessive duplicates. An excessive duplicate is defined as one that represents more than two occurrences of the same element. In such a case the first two occurrences are retained, but the 3rd and subsequent occurrences are removed. The order of the list is otherwise unchanged. • You may find it usetul to write some helper functions • Example: (removexd '(a b a b a b c c c d d)) = '(a b a b c c d d) • A best answer to this question has linear performance when the function is applied to a long list of identical elements.
Answer to Question 2 • Eliminating excessive duplicates • (define (removexd list) (cond ((null list) null) ((cond (> (count (cdr list) (car list)) 1) (removexd (cdr list)) (T (cons (car list) (removexd (cdr list))) ))
More on Answer to 2 • But that answer is quadratic for a list of all on the same element • Because count always goes through the entire list each time. • So instead we will define a helper function
Helper function for Answer 2 • Return true if (count elmt list) > 1 • (define (more1 list elmt) (cond ((null list) F ((and (eq elmt (car list)) (member (elmt (cdr list)))) T) (T (more1 (cdr list) elmt) ))
Better Answer for Question 2 • Eliminating excessive duplicates efficiently • (define (removexd list) (cond ((null list) null ((more1 (cdr list) (car list)) (removexd (cdr list)) (T (cons (car list) (elimxd (cdr list)))) ))
OOPS, Question 2 Revisited • Got 3 points off on the last answer • Why because we removed duplicates from the start of the list instead of the end. The spec was very clear • So let’s fix it and get full credit
Better Solution for Question 2 • Eliminating duplicates from end of list • (define (removexd list) (cond ((null list) null) (T (cons (car list) (removedx (stripextra (cdr list) (car list))))) ) )
New Helper Functions • Remove extra occurrences of E from L • i.e. if more than one occurrence, keep first • (define (stripextra L E) (cond ((null L) null) ((eq (car L) E) (cons E (stripall (cdr L) E))) (T (cons E (stripextra (cdr L) E))) ))
New Helper Functions • Remove all occurrences of E from L • (define (stripall L E) (cond ((null L) null) ((eq (car L) E) (stripall (cdr L) E)) (T (cons E (stripall (cdr L) E))) ))
Question 3 • Problem • Write a program to convert numbers in one base to numbers in • a second base. There are 62 different digits: • { 0-9,A-Z,a-z } • Input: • The first line of input contains a single positive integer. This is the • number of lines that follow. Each of the following lines will have a • (decimal) input base, followed by a (decimal) output base, followed by • a number expressed in the input base. Both the input base and the • output base will be in the range from 2-62. That is (in decimal) • A=10, B=11,...Z=35,a=36,b=37,...z=61 (0-9 have their usual • meanings).
Question 3 (continued) • Output: • The output of the program should consist of three lines of output for • each base conversion performed. The first line should be the input • base in decimal followed by a space then the input number (as given • expressed in the input base). The second output line should be the • output base followed by a space then the input number (as expressed • in the output base). The third output line is blank. • Example input • 2 • 62 2 abcdefghiz • 10 16 1234567890123456789012345678901234567890
Question 3 (continued) • Your job is to write an Ada program for this problem. Now that would be • too difficult to do in 20 minutes if you really had to write the whole • program (after all in the programming competition, 3 people worked for • 5 hours trying to solve 8 problems, with no one expeted to get all). • So how does this get made manageable. Answer, create resusable • packages for this, and you only have to write the specs not the • bodies! So the more reusability you extract, the less you have to • program :-) • Your specs need not be fully commented, as long as they are reasonably • clear (there is not time enough in an exam for doing full documentation).
Answer to Question 3 • This is the easiest question on the exam! • Because you don’t have to program much • Just spec out useful packages
Package 1 for Answer 3 • package Bases is subtype Base is Integer range 2 .. 62;function Convert (S : String; From, To : Base)return String; -- Convert from one base to anotherend Bases;
Package 2 for Answer 3 • package IO is type Strp is access all String;function Get_Int return Integer; -- read integer from standard inputfunction Get_Str return Strp; -- Read string terminated by EOL from -- standard input. Returns pointerfunction Put_Int (X : Integer); -- Put integer, no spc before, spc afterfunction Put_Str (S : String); -- Put out string with line feed afterend IO;
Final Answer for 3 • with Bases, IO;use Bases, IO;procedure Main is From, To : Base; S : Strp;beginfor J in 1 .. Get_Int loop From := Get_Int; Put_Int (From); To := Get_Int; S := Get_String; Put_Str (S.all); Put_Int (To); Put_Str (Convert (S.all, From, To)); Put_Str (“”);end loop;end Main;
Question 4 • Why would this be a bad inner loop for the traffic simulation problem? • loop N := N + 1;delay until Start + Period * N;accept Enquiry_from_Prev_Car do .. end; update position etcend loop; • Because car behind has to wait for answer. Every task stuck waiting on another.
Question 5 • Suppose you were grading assignment 2, and someone proposed the following reusable package: • package arith is type Number is private; function Add (A, B : Number) return Integer;function Sub (A, B : Number) return Number;function Mul (A, B : Number) return Number;function Div (A, B : Number) return Number;private type Number is new Integer;end; • They would not receive a very good grade. Briefly point out several problems with this package spec that make it dubious as a useful reusable package.
Answer 5a • package arith is type Number is private; function Add (A, B : Number) returnInteger;function Sub (A, B : Number) return Number;function Mul (A, B : Number) return Number;function Div (A, B : Number) return Number;private type Number is new Integer;end; • Plain mistake, should be Number.
Answer 5b • package arith is type Number is private; function Add (A, B : Number) return Integer;function Sub (A, B : Number) return Number;function Mul (A, B : Number) return Number;function Div (A, B : Number) return Number;private type Number is new Integer;end; • Private type, but no constructor or analysis subprograms. Cannot get numbers in or out!
Answer 5c • package arith is type Number is private; function Add (A, B : Number) return Integer;-- ?????function Sub (A, B : Number) return Number;function Mul (A, B : Number) return Number;function Div (A, B : Number) return Number;private type Number is new Integer;end; • No comments! Unacceptable! No idea what it does!Presumably does something interesting but what?
Answer 5d • package arith istype Number is private; function Add (A, B : Number) return Integer;function Sub (A, B : Number) return Number;function Mul (A, B : Number) return Number;function Div (A, B : Number) return Number;private type Number is new Integer;end; • This should be a generic package with Number being a generic formal discrete type (type Number is range <>).
Answer 5e • packageArith is type Number is private; function Add (A, B : Number) return Integer;function Sub (A, B : Number) return Number;function Mul (A, B : Number) return Number;function Div (A, B : Number) return Number;private type Number is new Integer;end Arith; • Poor style, package name should be capitalized, end line should repeat the name of the package.
Answer 5f • package arith is type Number is private; function Add (A, B : Number) return Integer;function Sub (A, B : Number) return Number;function Mul (A, B : Number) return Number;function Div (A, B : Number) return Number;private type Number is newInteger;end; • Type Integer should never be used in a case like this since it is target dependent.
Answer 5g • package arith is type Number is private; functionAdd (A, B : Number) return Integer;functionSub (A, B : Number) return Number;functionMul (A, B : Number) return Number;functionDiv (A, B : Number) return Number;private type Number is new Integer;end; • Poor name choice, should use “+” etc, allowing userto use nice infix notation (assuming Add means +).