Quantitative Reasoning 20
Practice Mid-Term Exam
Answer all parts of all 4 questions. The points listed for each question indicate the weight they will be given in averaging the grade. They are not an indication of how long it should take to answer. However, don't spend more than 1 minute per point on any one question until you have tried all of them. There are 100 points maximum and 80 minutes on the exam. No notes are permitted for the exam with the exception of the ANT instruction set for practice. It would be on the front screen for your reference if needed in the real exam.
1. (15 points) The following fragment of ANT assembler program, found without comments, is supposed to input 4 numbers from the binary port and add them up, but it doesn't work, in a particularly nasty way. What is the problem? Suggest a simple solution.
Label Op Add1 Add2 Add3
start lc r5 loop
lc r4 4
lc r3 0
loop in p0 r2
add r3 r3 r2
bgt r5 r4 r0
out p1 r3
hlt start
2a. (10 points) If the DECIMAL equivalent of the ASCII code for the symbol "a" is 97 and the DECIMAL equivalent of the ASCII code for the symbol "A" is 65, what are the HEXADECIMAL equivalents of the ASCII codes for the symbols "b", "C" and "Q"?
2b. (10 points) What are the binary and decimal (assuming binary is first signed and then unsigned) equivalents of the following hexadecimal numbers.
1.
0xff 2. 0x32 3. 0x84
3. (15 points) The following Python function (again no comments,
TSK!,TSK!) is supposed to return the positions of the largest and smallest values, in that order, in a list given as an argument. It has at least two syntactic errors, one made twice, and two logical errors.
Identify them and suggest corrections. You will not need to add or delete command lines. def high_and_low(s):
ihigh=0
ilow=0
for i in range(1:len(s)):
if s[i]<s[ihigh]then
ihigh=i
if s[i]>s[ilow]then
ilow=i
return ihigh,ilow
4a.(20 points) A palindrome is a string which reads the same either
backwards or forwards, for example “abba”, “32123” or the famous “able was I ere I saw elba”. Write a Python function which accepts a string as an argument and returns True if it is a palindrome and False otherwise.
4b.(15 points) Write a Python function, which calls your function from
3a, which accepts a list of strings and returns a list containing only those members of the argument list which are palindromes. If you weren’t able to complete 3a just assume you have a function which does work.
4c.(15 points) Write a Python function, which calls your function from
3a, which accepts a list of strings and returns the longest palindrome in the list. If there is a tie, you may return any one of the longest group. If there aren’t any just return an empty string, “”.