1 / 29

Midterm Exam Answers

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.

parker
Download Presentation

Midterm Exam Answers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Midterm Exam Answers The Programming Language Landscape Robert Dewar

  2. 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

  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)) ) )

  4. 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)) ) )

  5. 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.

  6. 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))) ))

  7. 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

  8. 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) ))

  9. 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)))) ))

  10. 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 

  11. 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))))) ) )

  12. 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))) ))

  13. 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))) ))

  14. 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).

  15. 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

  16. 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).

  17. 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

  18. 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;

  19. 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;

  20. 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;

  21. 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.

  22. 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.

  23. 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.

  24. 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!

  25. 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?

  26. 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 <>).

  27. 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.

  28. 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.

  29. 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 +).

More Related