Friday, March 13, 2026

Week 7 Assignments

1. LED

You have a friend, who sells LED displays.    


He heard you learned Python and seeks your help with developing a driver program for the programmable LED display.  Basically you'll output a 'dot matrix' for any input number 0-9.  For example for number 369, you output:

*** *** ***
  * *   * *
*** *** ***
  * * *   *
*** *** ***

Input a positive number.  Output the 'dot matrix' format suitable for the LED display.

Extension 1:
If the number is very long, it'll have to be wrapped onto multiple lines.

Extension 2:
There are different kinds of LED displays which take different kind of characters as input.  Please get the user to input the character to use, and then output the 'dot matrix' using that character.  Example, the input character is 'O', then output becomes:

OOO OOO OOO
  O O   O O
OOO OOO OOO
  O O O   O
OOO OOO OOO

2. Everything has a price

Johnny English (2003) - IMDb

You are a secret agent.  You buy and sell classified information for a living.  Such classified information has several properties:

From country: a string of letters;
To country: a string of letters;
- Classification level: a number between 1 and 5;
Financial value: a number between 1 and 100.

When a piece of information is being traded, its price is calculated as follows:

1. You add values of all the letters in the from country, 'a' being 1 and 'z' being 26, to get a FC value;
2. You add values of all the letters in the to country, to get a TC value;
3. You use the Classification level C and Financial value F to calculate the CF value as follows: add up the squares of numbers from 1 to C, then add up the square roots of numbers from 1 to F, then add these 2 to get a CF value;
4. get the Price = (FC + TC) * CF

Get a list of classified information from an input file: jamesbondsclassifiedinformationforsaledontshowanybodybecauseitsstillnotencrypted.txt.  Each line contains the from country, to country, classification level and the financial value.  For each piece, write the calculated price to prices.txt.

First try to write the program without using functions.  Then rewrite your program using functions.  See if that simplifies your life. 

3. Scopes of variables

Write some simple functions for example adding 2 numbers.  Try different scenario like accessing values of local variables within / outside the functions, and accessing values of global variables within / outside the functions.

Also try changing values of variables within the functions, then see what happens to them outside the functions.

Week 6 and 7 Learning Summary

Week 6 and 7 subject 1: Functions

  • Function is a block of code with:
    • A name,
    • Zero or more parameters,
    • Zero or more return values.
  • Functions are useful for:
    • Eliminating repetition, and
    • Simplifying code, making it easier to read.
  • The same number of parameters need to be provided when calling a Function, as the number of parameters in the definition of the function.
  • You can specify default values of parameters in the definition.  When there are default values, less parameters can be provided when calling.
  • Use the return statement to return values to the caller.  You can return multiple values:
    • In the form of a tuple, i.e., multiple values separated by commas, or
    • In a list.

Saturday, March 7, 2026

Week 5 Learning Summary

Week 5 subject 1: File Operations (cont.)

  • Pay attention lines read from a File contain the newline characters at the end of each line.
  • You can use File like a list of lines with for l in f: 
  • ATTENTION: the File object keeps track of the current position read.  It always moves forward.

  • More ways to process File contents in Lists:
  • List(File) is a list of lines from File,
  • .readlines(hint) returns a list of lines.
  • One way to avoid explicitly closing files is using 
        with open(Filename) as File:

            File will be automatically closed at the end of the with block.

  • Opening files for output using the 'w' or 'a' modes in the open() function.  Note the different behaviour vs. 'r' when the file doesn't exist.
  • Output to files with the write() function.
  • Files by default is a sequential media.  There's a historical reason for this.  However there are ways to move in a file in both directions:
  • .seek(offsetfrom) moves forward or backward in file,
  • .tell() tells the current position in file.

Friday, February 20, 2026

Week 3 Assignments

1. Real Legend:

The Unix system (https://en.wikipedia.org/wiki/Unix) is a legend. It sets the foundation for most of the modern-day operating systems that run on billions of devices. More importantly its design philosophy of simplicity and reusability has a massive influence on generations of programmers, in simple terms, to create a program that does one thing really well, and let it work with other programs.

The Unix system has many very famous commands which are also legends.  wc is one of them.  See https://en.wikipedia.org/wiki/Wc_(Unix).  You want to recreate the functions of wc in Python so that you can be a real legend too.

Input the name of an existing text file.  Output 3 numbers, the number of lines, the number of words (delimited by spaces), and the number of characters (including spaces and newlines).

2. Head, tail, and more

You've managed to recreate the classical Unix command wc with Python.  Dennis Ritchie and Ken Thompson send their regards.

They would like you to continue with a few more extremely useful Unix commands:

head: takes an input file name, number n, and an output file name; writes the first n lines of the input file to the output file.  If the output file name is not provided, just write to the screen.

hint: what happens if there are less than n lines in the input file?

tail: very similar to head, only that it writes the last n lines.

more: takes an input file name.  Writes the contents of the file to screen, but pause at a full screen and wait for input.  Once the ENTER key is hit, show the next full screen of file content, until the entire file is shown.

hint: how do you know how many lines is a full screen?  Make some assumptions.  If you really want to be accurate, look up shutil.get_terminal_size().
Extension: what if the program accepts 'any key' instead of ENTER for the next screen of output?  Do some research.

3. James Bond's Book:

Bond's missions get more complex and his code book gets longer.  It has reach a point that he can no longer memorise all the codes.  He decides to save them to a text file jamesbondscodebooktopsecretcantletanybodyseebecauseitsnotencrypted.txt.   The file contains many lines, each containing the plain text word followed by its code word.  Example:

    I            poop
    hungry       *&%#
    bored        78534
    irritated    buzzinga!

Adapt your work last time to load the code book from that text file.  Then you can use the code book to encrypt your message.

Saturday, February 14, 2026

Week 3 Learning Summary

Week 3 subject 1: File Operations

  • File means text files.  It's a giant leap forward which means you are finally free from keystrokes and can now process something much more complex and more meaningful!
  • Always use the open() function to open a file before using, which returns a File object you'll need to refer to whenever you need to operate a file.
  • Always use the .close() method to close the file after use.
  • Read from files using:
    • .read(number of characters)
    • .readline()

Friday, February 6, 2026

Week 2 Assignments

 1. Not Lonely Anymore:

A second librarian, Charlie, joins you in the school library.  Charlie suggests that the book list you keep can be improved to include names of the authors.  You agree.  The two of you start to upgrade your automation to do the following:

  • Input a line which contains the book name followed by the author's name.  Both are single words.
  • If the book doesn't exist in your list, add it; if it does, update the author's name.
  • Handle deletion by book name as it already does.
  • If the input is 'search' followed by author's name, output a list of books by this author.
  • At the end of the process, output the complete book list, one line per book, each containing the book name followed by the author's name. 

2. Bond, James Bond:


You live a double life.  During the day you are the lonely librarian, but when you are off and nobody sees you, you are James Bond the MI6 secret agent, the King's spy, with a license to kill.

You can't let others know the content of your communication with M, the head of MI6.  You have a secret code book which looks like this:

    I            poop
    hungry       *&%#
    bored        78534
    irritated    buzzinga!

So that a message like 'I am always hungry or bored or irritated' will be translated to 'poop am always *&%# or 78534 or buzzinga!'

Input a line of message you need to send to M.  Use the code book to translate it word by word to the secret form and output the translated line.  Repeat this process until a blank line is input.

Week 2 Learning Summary

Week 2 subject 1: Tuples

  • Tuples are just like Lists, only they're immutable, i.e., their elements, and the numbers of elements, can't be altered.  HOWEVER, the elements are immutable doesn't mean that the elements of the elements can't be altered.  
    • Example, if an element of a Tuple is a List, you can't replace the List with another, but you can alter the elements of the List.
  • There are limited operations applicable to Tuples, including the len() function, and the .count() and .index() methods.

Week 2 subject 2: Dictionaries

  • Dictionarys are like Lists, but their elements are pairs: Key:Value.  Keys are just like subscripts of Lists only that they can be of many other data types and other values.
  • Dictionary is perfect for registers, dictionaries and anything you need to use an index to locate values.
  • Use methods like .items().keys() and .values().  .items() returns a list of Tuples, while other two return Lists.
  • You can't use arithmetic operations like + or * with a Dictionary.
  • Learn how to tell if one Key exists in a Dictionary?  What about one Value?
  • There's no .append().  Use assignments directly.  
    • Note the difference with Lists where you can't assign value to an element that hasn't been appended yet.
  •  A Dictionary can be initialised 
    • Using initialisers directly like d = {1:'one', 2:'two', 3:'three},
    • Using the dict() constructor like d = dict([(1, 'one'), (2, 'two'), (3, 'three)]), note the parameter of dict() is a list of tuples,
    • There's a fancier while more confusing way, when Keys are simple strings: d = dict(one=1, two=2, three=3)
    • Using the .fromkeys() method like d = dict.fromkeys([1,2,3], "numbers")
  • As I said, keep exploring all the possibilities!