Transcript
Page 1: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Midterm Exam AnswersMidterm Exam Answers

The Programming LanguageThe Programming Language

LandscapeLandscape

Robert DewarRobert Dewar

Page 2: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Question 1Question 1

Write a scheme function which takes Write a scheme function which takes two arguments, a list and an element two arguments, a list and an element and returns the number of times that and returns the number of times that thethegiven element appears in the list.given element appears in the list.

e.g. (count '(a b c d a a) 'a) = 3e.g. (count '(a b c d a a) 'a) = 3

Page 3: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Answer to Question 1Answer to Question 1

Counting the number of elements in Counting the number of elements in a lista list(define (count list elmt)(define (count list elmt)

(cond (cond ((null list) 0) ((null list) 0) ((eq elmt (car list)) ((eq elmt (car list)) (+ 1 (count (cdr list) elmt)) (+ 1 (count (cdr list) elmt)) (T (count (cdr list) elmt)) (T (count (cdr list) elmt)) ) ) ) )

Page 4: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Another answer to Question 1Another answer to Question 1

Avoid code duplicationAvoid code duplication(define (count list elmt)(define (count list elmt)

(if (null list) (if (null list) 0 0 (+ (if (eq elmt (car list)) 1 0) (+ (if (eq elmt (car list)) 1 0) (count (cdr list) elmt)) (count (cdr list) elmt)) ) ) ) )

Page 5: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Question 2Question 2 Write a scheme function (together with any helper Write a scheme function (together with any helper

functions needed) that given a list, removes excessive functions needed) that given a list, removes excessive duplicates. An excessive duplicate is defined as one that duplicates. An excessive duplicate is defined as one that represents more than two occurrences of the same represents more than two occurrences of the same element. In such a case the first two occurrences are element. In such a case the first two occurrences are retained, but the 3rd and subsequent occurrences are retained, but the 3rd and subsequent occurrences are removed. The order of the list is otherwise unchanged.removed. The order of the list is otherwise unchanged.

You may find it usetul to write some helper functionsYou 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 Example: (removexd '(a b a b a b c c c d d)) = '(a b a b c c d d)d d)

A best answer to this question has linear performance when A best answer to this question has linear performance when the function is applied to a long list of identical elements.the function is applied to a long list of identical elements.

Page 6: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Answer to Question 2Answer to Question 2

Eliminating excessive duplicatesEliminating excessive duplicates(define (removexd list)(define (removexd list)

(cond (cond ((null list) null) ((null list) null) ((cond (> (count (cdr list) (car list)) 1) ((cond (> (count (cdr list) (car list)) 1) (removexd (cdr list)) (removexd (cdr list)) (T (cons (car list) (removexd (cdr (T (cons (car list) (removexd (cdr list)))list))) ) )))

Page 7: Midterm Exam Answers The Programming Language Landscape Robert Dewar

More on Answer to 2More on Answer to 2

But that answer is quadratic for a list But that answer is quadratic for a list of all on the same elementof all on the same elementBecause count always goes through the Because count always goes through the

entire list each time.entire list each time.So instead we will define a helper So instead we will define a helper

functionfunction

Page 8: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Helper function for Answer 2Helper function for Answer 2

Return true if (count elmt list) > 1Return true if (count elmt list) > 1(define (more1 list elmt)(define (more1 list elmt)

(cond (cond ((null list) F ((null list) F ((and (eq elmt (car list)) ((and (eq elmt (car list)) (member (elmt (cdr list)))) T) (member (elmt (cdr list)))) T) (T (more1 (cdr list) elmt) (T (more1 (cdr list) elmt) ) )))

Page 9: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Better Answer for Question 2Better Answer for Question 2

Eliminating excessive duplicates Eliminating excessive duplicates efficientlyefficiently(define (removexd list)(define (removexd list)

(cond (cond ((null list) null ((null list) null ((more1 (cdr list) (car list)) ((more1 (cdr list) (car list)) (removexd (cdr list)) (removexd (cdr list)) (T (cons (car list) (elimxd (cdr list)))) (T (cons (car list) (elimxd (cdr list)))) ) )))

Page 10: Midterm Exam Answers The Programming Language Landscape Robert Dewar

OOPS, Question 2 RevisitedOOPS, Question 2 Revisited

Got 3 points off on the last answer Got 3 points off on the last answer Why because we removed duplicates Why because we removed duplicates

from the start of the list instead of the from the start of the list instead of the end. The spec was very clearend. The spec was very clear

So let’s fix it and get full credit So let’s fix it and get full credit

Page 11: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Better Solution for Question 2Better Solution for Question 2

Eliminating duplicates from end of Eliminating duplicates from end of listlist(define (removexd list)(define (removexd list)

(cond (cond ((null list) null) ((null list) null) (T (cons (car list) (T (cons (car list) (removedx (removedx (stripextra (cdr list) (car list))))) (stripextra (cdr list) (car list))))) ) ) ))

Page 12: Midterm Exam Answers The Programming Language Landscape Robert Dewar

New Helper FunctionsNew Helper Functions

Remove extra occurrences of E from LRemove extra occurrences of E from Li.e. if more than one occurrence, keep firsti.e. if more than one occurrence, keep first(define (stripextra L E)(define (stripextra L E)

(cond (cond ((null L) null) ((null L) null) ((eq (car L) E) (cons E (stripall (cdr L) E))) ((eq (car L) E) (cons E (stripall (cdr L) E))) (T (cons E (stripextra (cdr L) E))) (T (cons E (stripextra (cdr L) E))) ) )))

Page 13: Midterm Exam Answers The Programming Language Landscape Robert Dewar

New Helper FunctionsNew Helper Functions

Remove all occurrences of E from LRemove all occurrences of E from L(define (stripall L E)(define (stripall L E)

(cond (cond ((null L) null) ((null L) null) ((eq (car L) E) (stripall (cdr L) E)) ((eq (car L) E) (stripall (cdr L) E)) (T (cons E (stripall (cdr L) E))) (T (cons E (stripall (cdr L) E))) ) )))

Page 14: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Question 3Question 3 ProblemProblem Write a program to convert numbers in one base to numbers inWrite a program to convert numbers in one base to numbers in a second base. There are 62 different digits:a second base. There are 62 different digits:

{ 0-9,A-Z,a-z }{ 0-9,A-Z,a-z }

Input:Input: The first line of input contains a single positive integer. This is theThe first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have anumber of lines that follow. Each of the following lines will have a (decimal) input base, followed by a (decimal) output base, (decimal) input base, followed by a (decimal) output base,

followed byfollowed by a number expressed in the input base. Both the input base and a number expressed in the input base. Both the input base and

thethe output base will be in the range from 2-62. That is (in decimal)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 usualA=10, B=11,...Z=35,a=36,b=37,...z=61 (0-9 have their usual meanings). meanings).

Page 15: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Question 3 (continued)Question 3 (continued) Output:Output:

The output of the program should consist of three lines of output The output of the program should consist of three lines of output forfor

each base conversion performed. The first line should be the inputeach base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as base in decimal followed by a space then the input number (as

givengiven expressed in the input base). The second output line should be expressed in the input base). The second output line should be

thethe output base followed by a space then the input number (as output base followed by a space then the input number (as

expressedexpressed in the output base). The third output line is blank.in the output base). The third output line is blank.

Example inputExample input

22 62 2 abcdefghiz62 2 abcdefghiz 10 16 123456789012345678901234567890123456789010 16 1234567890123456789012345678901234567890

Page 16: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Question 3 (continued)Question 3 (continued) Your job is to write an Ada program for this problem. Now that would Your job is to write an Ada program for this problem. Now that would

bebe too difficult to do in 20 minutes if you really had to write the wholetoo difficult to do in 20 minutes if you really had to write the whole program (after all in the programming competition, 3 people worked program (after all in the programming competition, 3 people worked

forfor 5 hours trying to solve 8 problems, with no one expeted to get all).5 hours trying to solve 8 problems, with no one expeted to get all).

So how does this get made manageable. Answer, create resusableSo how does this get made manageable. Answer, create resusable packages for this, and you only have to write the specs not thepackages for this, and you only have to write the specs not the bodies! So the more reusability you extract, the less you have tobodies! So the more reusability you extract, the less you have to program :-)program :-)

Your specs need not be fully commented, as long as they are Your specs need not be fully commented, as long as they are reasonablyreasonably

clear (there is not time enough in an exam for doing full clear (there is not time enough in an exam for doing full documentation).documentation).

Page 17: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Answer to Question 3Answer to Question 3

This is the easiest question on the This is the easiest question on the exam!exam!

Because you don’t have to program Because you don’t have to program muchmuch

Just spec out useful packagesJust spec out useful packages

Page 18: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Package 1 for Answer 3Package 1 for Answer 3

packagepackage Bases Bases isis subtype subtype Base Base isis Integer Integer rangerange 2 .. 62; 2 .. 62; functionfunction Convert Convert (S : String; From, To : Base) (S : String; From, To : Base) returnreturn String; String; -- Convert from one base to another -- Convert from one base to anotherendend Bases; Bases;

Page 19: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Package 2 for Answer 3Package 2 for Answer 3

packagepackage IO IO isis type type StrpStrp is access all is access all String;String; functionfunction Get_Int Get_Int returnreturn Integer; Integer; -- read integer from standard input -- read integer from standard input functionfunction Get_Str Get_Str returnreturn Strp; Strp; -- Read string terminated by EOL from -- Read string terminated by EOL from -- standard input. Returns pointer -- standard input. Returns pointer functionfunction Put_Int (X : Integer); Put_Int (X : Integer); -- Put integer, no spc before, spc after -- Put integer, no spc before, spc after functionfunction Put_Str (S : String); Put_Str (S : String); -- Put out string with line feed after -- Put out string with line feed afterendend IO; IO;

Page 20: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Final Answer for 3Final Answer for 3withwith Bases, IO; Bases, IO;

useuse Bases, IO; Bases, IO;procedureprocedure Main Main isis From, To : Base; From, To : Base; S : Strp; S : Strp;beginbegin forfor J J inin 1 .. Get_Int 1 .. Get_Int looploop From := Get_Int; From := Get_Int; Put_Int (From); Put_Int (From); To := Get_Int; To := Get_Int; S := Get_String; S := Get_String; Put_Str (S.all); Put_Str (S.all); Put_Int (To); Put_Int (To); Put_Str (Convert (S.all, From, To)); Put_Str (Convert (S.all, From, To)); Put_Str (“”); Put_Str (“”); end loopend loop;;endend Main; Main;

Page 21: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Question 4Question 4

Why would this be a bad inner loop for Why would this be a bad inner loop for the traffic simulation problem?the traffic simulation problem?

looploop N := N + 1; N := N + 1; delay untildelay until Start + Period * N; Start + Period * N; acceptaccept Enquiry_from_Prev_Car Enquiry_from_Prev_Car do do .. .. endend;; update position etc update position etc end loopend loop;;

Because car behind has to wait for answer. Because car behind has to wait for answer. Every task stuck waiting on anotherEvery task stuck waiting on another..

Page 22: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Question 5Question 5 Suppose you were grading assignment 2, and someone Suppose you were grading assignment 2, and someone

proposed the following reusable package:proposed the following reusable package:

packagepackage arith arith isis type type Number is Number is private;private; function function Add (A, B : Number) Add (A, B : Number) returnreturn Integer; Integer; functionfunction Sub (A, B : Number) Sub (A, B : Number) returnreturn Number; Number; functionfunction Mul (A, B : Number) Mul (A, B : Number) returnreturn Number; Number; functionfunction Div (A, B : Number) Div (A, B : Number) returnreturn Number; Number;privateprivate type type Number Number is newis new Integer; Integer;endend;;

They would not receive a very good grade. Briefly point out They would not receive a very good grade. Briefly point out several problems with this package spec that make it several problems with this package spec that make it dubious as a useful reusable package.dubious as a useful reusable package.

Page 23: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Answer 5aAnswer 5a

packagepackage arith arith isis type type Number is Number is private;private; function function Add (A, B : Number) Add (A, B : Number) returnreturn IntegerInteger;; functionfunction Sub (A, B : Number) Sub (A, B : Number) returnreturn Number; Number; functionfunction Mul (A, B : Number) Mul (A, B : Number) returnreturn Number; Number; functionfunction Div (A, B : Number) Div (A, B : Number) returnreturn Number; Number;privateprivate type type Number Number is newis new Integer; Integer;endend;;

Plain mistake, should be Number.Plain mistake, should be Number.

Page 24: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Answer 5bAnswer 5b

packagepackage arith arith isis type type Number is Number is privateprivate;; function function Add (A, B : Number) Add (A, B : Number) return return IntegerInteger;; functionfunction Sub (A, B : Number) Sub (A, B : Number) returnreturn Number; Number; functionfunction Mul (A, B : Number) Mul (A, B : Number) returnreturn Number; Number; functionfunction Div (A, B : Number) Div (A, B : Number) returnreturn Number; Number;privateprivate type type Number Number is newis new Integer; Integer;endend;;

Private type, but no constructor or analysis Private type, but no constructor or analysis subprograms. Cannot get numbers in or out!subprograms. Cannot get numbers in or out!

Page 25: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Answer 5cAnswer 5c

packagepackage arith arith isis type type Number is Number is private;private; function function Add (A, B : Number) Add (A, B : Number) return return IntegerInteger;; -- ?????-- ????? functionfunction Sub (A, B : Number) Sub (A, B : Number) returnreturn Number; Number; functionfunction Mul (A, B : Number) Mul (A, B : Number) returnreturn Number; Number; functionfunction Div (A, B : Number) Div (A, B : Number) returnreturn Number; Number;privateprivate type type Number Number is newis new Integer; Integer;endend;;

No comments! Unacceptable! No idea what it No comments! Unacceptable! No idea what it does!does!Presumably does something interesting but what?Presumably does something interesting but what?

Page 26: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Answer 5dAnswer 5d

packagepackage arith arith isis typetype Number is Number is privateprivate;; function function Add (A, B : Number) Add (A, B : Number) return return IntegerInteger;; functionfunction Sub (A, B : Number) Sub (A, B : Number) returnreturn Number; Number; functionfunction Mul (A, B : Number) Mul (A, B : Number) returnreturn Number; Number; functionfunction Div (A, B : Number) Div (A, B : Number) returnreturn Number; Number;privateprivate type type Number Number is newis new Integer; Integer;endend;;

This should be a generic package with Number This should be a generic package with Number being a generic formal discrete type (type Number being a generic formal discrete type (type Number is range <>).is range <>).

Page 27: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Answer 5eAnswer 5e

packagepackage AArith rith isis type type Number is Number is private;private; function function Add (A, B : Number) Add (A, B : Number) return return IntegerInteger;; functionfunction Sub (A, B : Number) Sub (A, B : Number) returnreturn Number; Number; functionfunction Mul (A, B : Number) Mul (A, B : Number) returnreturn Number; Number; functionfunction Div (A, B : Number) Div (A, B : Number) returnreturn Number; Number;privateprivate type type Number Number is newis new Integer; Integer;end end ArithArith;;

Poor style, package name should be capitalized, Poor style, package name should be capitalized, end line should repeat the name of the package.end line should repeat the name of the package.

Page 28: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Answer 5fAnswer 5f

packagepackage arith arith isis type type Number is Number is private;private; function function Add (A, B : Number) Add (A, B : Number) return return IntegerInteger;; functionfunction Sub (A, B : Number) Sub (A, B : Number) returnreturn Number; Number; functionfunction Mul (A, B : Number) Mul (A, B : Number) returnreturn Number; Number; functionfunction Div (A, B : Number) Div (A, B : Number) returnreturn Number; Number;privateprivate type type Number Number is newis new IntegerInteger;;endend;;

Type Integer should never be used in a case like Type Integer should never be used in a case like this since it is target dependent.this since it is target dependent.

Page 29: Midterm Exam Answers The Programming Language Landscape Robert Dewar

Answer 5gAnswer 5g

packagepackage arith arith isis type type Number is Number is private;private; function function AddAdd (A, B : Number) (A, B : Number) return return IntegerInteger;; functionfunction SubSub (A, B : Number) (A, B : Number) returnreturn Number; Number; functionfunction MulMul (A, B : Number) (A, B : Number) returnreturn Number; Number; functionfunction DivDiv (A, B : Number) (A, B : Number) returnreturn Number; Number;privateprivate type type Number Number is newis new Integer; Integer;endend;;

Poor name choice, should use “+” etc, allowing Poor name choice, should use “+” etc, allowing useruserto use nice infix notation (assuming Add means +).to use nice infix notation (assuming Add means +).


Top Related