

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Matloff; Class: Scripting Languages; Subject: Engineering Computer Science; University: University of California - Davis; Term: Summer 2005;
Typology: Exams
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Name:
Directions: Work only on this sheet (on both sides, if needed); do not turn in any supplementary sheets of paper. There is actually plenty of room for your answers, as long as you organize yourself BEFORE starting writing.
Do not use any Python or Perl constructs which were not introduced either in lecture, discussion section or our written materials.
use RadixNum;
tie $x,’RadixNum’,3,0; # base 3 tie $y,’RadixNum’,3,0; # base 3 $x = 5; $y = $x + 20; print $x,$y, "\n";
This will print out 5 and 25, but internally $y, for instance, will also be associated with a reference to an anonymous array (2,2,1), since the base-3 representation of 25 is 2 · 32 + 2 · 31 + 1 · 30. Note that the fancy name used for base is radix, so in this example the radix is 3.
The object created by TIESCALAR() (not shown) is an anonymous hash which consists of an integer Radix, a reference Digits to the anonymous array, and an integer Value containing the numeric value.
Part of the code in RadixNum.pm will be a subroutine torad(). It has as arguments the radix and value, and returns a reference to the corresponding radix form of the value. For instance, the call torad(3,25) will return (2,2,1).
(a) (15) Fill in the blanks in torad():
sub torad { my $rad = shift; my $vl = shift; my @ary = (); if ($vl == 0) {@ary = (0);} else { while ($vl > 0) { $t = $vl % $rad; $vl = int($vl/$rad);
} } return ______________; }
(b) (15) An instance method printrad() will print out the radix form of the stored number. For instance, if $y is storing 25, this function will print out 221. Fill in the blanks in the test code above (following the line printing $x and $y), so that the radix form of $y is printed out, and the blanks in the function itself below. For full credit, use no loops.
sub printrad {
}
class pthqueue: def init(self,qid): self.qid = qid pth.newevent(qid) self.queue = [] def get(self): (insert lines here) def put(self,work): (insert lines here)
Solutions:
x = z.pop(0) z.insert(0,x) sys.argv[4] z[9:13]
gb.scrn.addstr(gb.winrow,0,’kill this process?’) c = chr(gb.scrn.getch()) if c != ’y’: return pid = int(ln.split()[0]) os.kill(pid,9) ln = ln[:15] + ’k’ + ln[16:] # can’t change a tuple gb.cmdoutlines[gb.startrow+gb.winrow)] = ln gb.scrn.addstr(gb.winrow,0,ln,curses.A_BOLD)
3.a
unshift @ary,$k; @ary;
3.b
my $r = shift; my $aryref = $r->{Digits}; print @$aryref; ... $ry = tied $y; $ry->printrad();