All the grades were scaled to fit with historical norms of 3rd year LCSEE subjects.
raw final mark scaled /13 /10 frequency mark --- --- --------- ----- 1 1 1.7 2,3 2 3.4 4 3 3 *** 5.1 5 4 7 ******* 6.9 6,7 5 16 **************** 8.6 8 6 14 ************** 10 9 7 6 ****** 10 10,11 8 2 ** 10 12 9 10 13 10 10
Answers follow. Note: a small variant of one of the following will be in the end of year quiz.
Assuming that "head /etc/passwd" yields the following lines, answer the questions below
% cat /etc/passwd | head root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh lp:x:7:7:lp:/var/spool/lpd:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh
What doe the following scripts produce?
1a) head /etc/passwd | cut -f1 -d:
root daemon bin sys sync games man lp mail news
1b) head /etc/passwd | cut -f1 -d: | sort -r
sys sync root news man mail lp games daemon bin
1c) head /etc/passwd | cut -f1 -d: | grep -v m | sort
bin lp news root sync sys
1d) head /etc/passwd | cut -f1 -d: | grep -v m |sort | sed 's/[aeiou]/ /g' | cut -d\ -f 1
b lp n r sync sys
2a) Here is a file called "dict.awk" that uses patterns, associative arrays, and self initializing variables.
BEGIN { while (getline < "words" ) # begin pattern
dict[$0] = 1 # associative array
}
{ if ( dict[$1]) # usual pattern
Bad++; # self-initializing variables
print $1
}
END { if (Bad) # end pattern
print Bad " words found"
}
For each one of {patterns, associate arrays, self-initializing variables}, then
2b) What is the purpose of the above code? Hint: there are at 3 typos in it.
2c) Assume that the file "words" contains 3 lines:
apple oranges watermelon
Given the above code (with all its typos), what is the output from "gawk -f dict.awk words"
OUTPUT: apple orane watermelon 3 words found
2d) Fix the typos. Write down the fixed code.
BEGIN { while (getline < "words" )
dict[$0] = 1
}
{ if ( !dict[$1]) { # negation added, curly added
Bad++;
print $1 } # curly added
}
END { if (Bad)
print Bad " words found"
}
Here is an awk program in a file "match.awk".
#1
/(apple|oranges)/ { print "1 [" $0 "]" }
#2
/^[^aeiou]/ { print "2 [" $0 "]" }
#3
/(happy|welcome)? mrs/ { print "3 [" $0 "]" }
#4
/[en]$/ { print "4 [" $0 "]" }
3a) There are four patterns about that use special characters in the regular expressions. For each of {#1,#2,#3,#4}
3b) What is output of the above code using "gawk -f match.awk words".
1 [apple] 4 [apple] 4 [orange] 2 [watermelon] 4 [watermelon]
4a) What is the output the following code. Why?
|s| s:= Set new. s addAll: #(1 1 2 2 3 2 4 5 6 4 6 6). s size !
OUTPUT:
6
Why? Because sets store unique values only.
4b) What is the output of the following code and how does the following code change that output?
|a| a:= Array new: 3. a at: 1 put: $a. a at: 2 put: $b. a at: 3 put: $c. a displayNl !
Output:
#(a b c ) "note trailing space"
Adding in this code...
! Array methods !
printOn: aStream
aStream nextPut: $(.
self first: [:elm| elm printOn: aStream]
then: [:elm| aStream space.
elm printOn: aStream].
aStream nextPut: $) !!
New output (note no final space):
#(a b c)
4c) Implement the first:then: method using by the above Array printOn: method.
! SequenceableCollection methods !
first: first then: then
| atFirst |
atFirst := true .
self do: [:elm |
atFirst ifFalse: [then value: elm]
ifTrue: [first value: elm].
atFirst := false] !!