Week 08 Tutorial Questions
Objectives
-
In assignment 2, you will implement a simple subset of the functionality of the
sedcommand.Here are some sed commands and their output.
Discuss what these print and why:
quit
seq 42 44 | sed 1q
sed 10q < dictionary.txt
seq 41 43 | sed 4q
seq 90 110 | sed /.1/q
sed '/r.*v/q' < dictionary.txt
yes | sed 3q
print
seq 41 43 | sed 2p
head dictionary.txt | sed 3p
seq 41 43 | sed -n 2p
sed -n 42p < dictionary.txt
head -n 1000 dictionary.txt | sed -n '/z.$/p'
substitute
seq 10 15 | sed 's/[15]/zzz/'
seq 10 15 | sed 's/[15]/zzz/g'
echo "Hello Andrew" | sed 's/e//'
echo "Hello Andrew" | sed 's/e//g'
addresses
seq 1 5 | sed '$d'
seq 42 44 | sed 2,3d
seq 10 21 | sed 3,/2/d
seq 10 21 | sed /2/,7d
seq 10 21 | sed /2/,/7/d
substitute
seq 1 5 | sed 'sX[15]XzzzX'
multiple commands
seq 1 5 | sed '4q;/2/d'
-f
echo "4q" > commands.script echo "/2/d" >> commands.script seq 1 5 | sed -f commands.script
input files
seq 1 2 > two.txt seq 1 5 > five.txt sed '4q;/2/d' two.txt five.txt
whitespace and comments
seq 24 42 | sed ' 3, 17 d # comment ; /2/p'
-i
seq 1 5 > five.txt sed -i /[24]/d five.txt cat five.txt
multiple commands
echo 'Punctuation characters include . , ; :' | sed 's/;/semicolon/g;/;/q'
-
What types are available as inbuilt types in Python?
-
What other useful types are available with Python's standard library?
-
Write a Python function
print_dict()that displays the contents of a dict in the format below:[key] => value [Andrew] => green [Anne] => red [John] => blue
-
Write a Python program,
times.pywhich prints a table of multiplications.Your program will be given the dimensions of the table and the width of the columns to be printed. For example:
./times.py 5 5 5 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25./times.py 10 10 3 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
-
Write a Python program
missing_words.pywhich given two files as arguments prints, in sorted order, all the words found in file1 but not file2.You can assume words occur one per line in each file.
-
Write a Python program
source_count.pywhich prints the number of lines of C source code in the current directory. In other words, this Python program should behave similarly towc -l *.[ch]. (Note: you are not allowed to usewcor other Unix programs from within the Python script). For example:./source_count.py 383 cyclorana.c 280 cyclorana.h 15 enum.c 194 frequency.c 624 model.c 293 parse.c 115 random.c 51 smooth.c 132 util.c 16 util.h 410 waveform.c 2513 total -
Write a Python program,
phone_numbers.pywhich given the URL of a web page fetches it by running wget and prints any strings that might be phone numbers in the web page.Assume the digits of phone numbers may be separated by zero or more spaces or hyphens ('-') and can contain between 8 and 15 digits inclusive.
You should print the phone numbers one per line with spaces & hyphens removed.
./phone_numbers.py https://www.unsw.edu.au 20151028 11187777 841430912571345 413200225 61293851000 57195873179