Assignment #1 - University of Windsor

SOLUTIONS:
Assignment #1: Due during labs (6th or 7th November, 2003)
60-100 Fall 2003:Key Concepts in Computer Science
1. Write a Miranda program called p1 that outputs the last two (2) paragraphs from the University of
Windsor Undergraduate Calendar 2003-2004 Section 2.4.22, pages 28-29.
p1 = “Plagiarism also includes submitting one's own essay, paper, or thesis on more than one occasion.
Accordingly, it is expected that a thesis, essay, paper or a report has not been and is not concurrently
being submitted for credit for any other course. In exceptional circumstances and with the prior
agreement of the instructor, a student may use research completed for one course as part of his or her
written work for a second course.
A confirmed incident of plagiarism will result in a sanction ranging from a verbal warning, to a loss of
credit in the course, to expulsion.”
2. Write a conditional Miranda program p2 that takes three positive numbers as input and outputs
the value corresponding to neither the largest nor the smallest number and squared. e.g., p2 5 7 6 =
36
max x y = x, if x >y
= y, otherwise
p2 x y z = x*x, if max x y = x & max x z = z
= y*y, if max x y =y & max y z = z
= z*z, otherwise
OR
p2 x y z = x*x, if ~(x=(smallest x y z)) & ~(x=(largest x y z))
= y*y, if ~(y=(smallest x y z)) & ~(y=(largest x y z))
= z*z, if ~(z=(smallest x y z)) & ~(z=(largest x y z))
smallest x y z = x, if x<y & x<z
= y, if y<x & y<z
= z, otherwise
largest x y z = x, if x>y & x>z
= y, if y>x & y>z
= z, otherwise
3. Write a recursive Miranda program p3 that takes a list of numbers and returns the product of all the
numbers in the list except the first element. E.g., p3 [2,1,3,4] = 12 (bonus 1mark: suppose we wished to
exclude the first two elements, what would you do?)
Note: the following isn’t truly recursive however it does call a recursive function
get_product [ ] = 1
get_product (e:es) = e * get_product es
p3 [ ] = 1
p3 [e] = 1
p3 (x:xs) = get_product xs
||Bonus
p3a [ ] = 1
p3a [e] = 1
p3a [e,f] = 1
p3a (e:f:es) = get_product es
Now a recursive way to do this problem:
prod [ ] = 1
prod (a:as) = 1, if #(a:as)= 1
= (a:as)!1* prod as, otherwise
|| bonus begins here
end [a] = a
end (a:as) = end as
prod2 [ ] = 1
prod2 [b ] = 1
prod2 (b:a:as) = end (b:a:as), if #(b:a:as)= 2
= (a:as)!1* prod2 as, otherwise
3. A permutation of a list is a list with the same elements in a different order. Write a Miranda function,
p4, which outputs a list of ALL the permutations of a list. (Hint: this can be done recursively using
list comprehension.) e.g. p4 [1,2,3] = [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
p4 [ ] = [[ ]]
p4 x = [a:y|a<-x; y<-p4 (x--[a])]
5. Write a recursive Miranda program, ziptwo, which, when applied to two lists, returns a list of pairs,
formed by tupling together corresponding elements of the given lists.
E.g., ziptwo [0..3] “list” = [(0,’l’), (1,’i’), (2,’s’), (3,’t’)]
ziptwo [ ] (b:bs) = [ ]
ziptwo (a:as) [ ] = [ ]
ziptwo (a:as) (b:bs) = (a,b): ziptwo as bs
OR (but non-recursively)
ziptwo [ ] [ ] = [ ]
ziptwo x y = [(a,b)|a<-x;b<-y;m<-[1..#x];n<-[1..#y];a=x!(m-1);b=y!(n-1);m=n]
6. The scalar product of two vectors (assumed to be of the same length) is given by multiplying
together corresponding elements and taking the sum total of the results. Write a Miranda program,
scalarproduct, which calculates the scalar product of two vectors where we define vector to be of
type [num]. Eg. scalarproduct [2,3] [4,5] = 2*4 + 3*5 = 23
scalarproduct x y = foldr (+) 0 [a*b|a<-x;b<-y;m<-[1..#x]; a=x!(m-1);b=y!(m-1)]
OR
scalarproduct list1 list2 = foldr (+) 0 [a*b| (a,b) Å ziptwo list1 list2]
NOTE: they may use sumlist instead of foldr (+) 0
7. Write a Miranda program p7 that takes a string as input and returns a 2-tuple of the form (x,y) where
x is a list of pairs (u,v) and y is a list of pairs (w,z) such that:
u is a letter of the alphabet that occurs in the string an even number of times v
and
w is a letter of the alphabet that occurs in the string an odd number of times z.
Remember there should be only one tuple for every alphabet that occurs in the string. (Hint : a string is
a list of characters. You can write a recursive program that counts the occurrence of an alphabet in the
string.) Your tuples must correspond to the order of appearance of the letters in the string.
e.g., p7 “banana” = ( [(‘n’,2)], [(‘b’,1),(‘a’,3)] )
(One more hint: try using list comprehension in your solution.)
p7 x = (even_occ x, odd_occ x)
rem x [ ] = [ ]
rem x (a:as) = rem x as, if x = a
= a : rem x as, otherwise
count a [ ] = 0
count a (e:es) = 1 + count a es, if a=e
= count a es, otherwise
even_occ [ ] = [ ]
even_occ (x:xs) = [(a,count a (x:xs))|a<-[x];(((count a (x:xs)) mod 2)=0)]
++ even_occ (rem x xs)
odd_occ [ ] = [ ]
odd_occ (x:xs) = [(a,count a (x:xs))|a<-[x];(((count a (x:xs))mod 2)=1)]
++ odd_occ (rem x xs)
OR
alphabet = "abcdefghijklmnopqrstuvwxyz"
eventimes list = revers (rd (revers ([(x,counts x alphabet)|x <- list; (counts x alphabet) mod 2 =0])))
oddtimes list = revers (rd (revers ([(x,counts x alphabet)|x <- list; (counts x alphabet) mod 2 =1])))
p7 list = (eventimes list, oddtimes list)
8. Show by (mathematical or structural) induction:
(a)
1
1
1
n
, whenever n is a positive integer.
+
+ ... +
=
1* 4 4 * 7
(3n − 2)(3n + 1) 3n + 1
Base case: n=1
LHS= ¼ = RHS
Inductive Hypothesis:
Assume
1
1
1
k
, for some (positive) k
+
+ ... +
=
1* 4 4 * 7
(3k − 2)(3k + 1) 3k + 1
Inductive Step:
Show
LHS
k +1
1
1
1
1
+
+ ... +
+
=
1* 4 4 * 7
(3k − 2)(3k + 1) (3(k + 1) − 2)(3(k + 1) + 1) 3(k + 1) + 1
=
1
1
1
1
+
+ ... +
+
1* 4 4 * 7
(3k − 2)(3k + 1) (3(k + 1) − 2)(3(k + 1) + 1)
=
k
1
+
3k + 1 (3(k + 1) − 2)(3(k + 1) + 1)
(ex hypothesi)
=
k
1
+
3k + 1 (3k + 1)(3k + 4)
(algebra)
=
k (3k + 4)
1
+
(3k + 1)(3k + 4) (3k + 1)(3k + 4)
(common denominator)
=
3k 2 + 4k + 1
(3k + 1)(3k + 4)
=
(3k + 1)(k + 1)
(3k + 1)(3k + 4)
(k + 1)
(3k + 4)
= RHS
=
Therefore, by mathematical induction the theorem is proven.
„
(b)
Prove that every amount of postage of 56 cents or more can be formed using just 8-cent and 9cent stamps.
Base Case: Postage of 56 cents can be formed using seven 8-cent stamps.
Assume inductive hypothesis: that every amount of postage of 56 cents or more can be
formed using just 8-cent and 9-cent stamps. (i.e., postage of n cents can be formed using
8-cent and 9-cent stamps)
Show: that postage of n+1 cents can be formed using 8-cent and 9-cent stamps.
There are two cases to consider in this solution.
Case 1: At least one 8-cent stamp was used to form postage of n-cents.
In this case replace one 8-cent stamp with a 9-cent stamp to form postage of n+1 cents.
Case 2: No 8-cent stamps were used to form postage of n-cents.
That is, only 9-cent stamps were used. Since n≥56, at least seven 9-cent stamps had to be
used. So replace any seven 9-cent stamps with eight 8-cent stamps to form postage of
n+1 cents.
Therefore, by induction, every amount of postage of 56 cents or more can be formed
using just 8-cent and 9-cent stamps.
J