Friday, November 21, 2025

Week 6 Assignments

1. Mind reader:

Your computer tells you it has mind reading capabilities.  You certainly don't buy that because you go to the Opportunity Class.  Your computer suggests a game with which it will show you that it reads your mind.

You give it a range, like 1..100, and the computer guesses an integer within that range.  All you need to do is tell it 'too large' or 'too small'.  You two repeat this Q&A until the computer guesses the correct number you have in mind.  By doing so you are able to tell if the computer can really read your mind, or it's simply doing maths.

Input 2 integers that indicate the range.  For each integer the computer guesses, input 'too large''too small''correct', or 'quit'.  The game ends when the input is 'correct' or 'quit'.  The computer outputs the integer you have in mind, and the number of guesses taken.  If the computer finds out you are trying to cheat, it also ends guessing and outputs 'you can't fool me!'.

2. Kitty:

Follow the definition of the Dog class, create a Cat class which describe this adorable animal which:

  • has a name
  • an age
  • a breed
  • a colour
  • once first adopted (instantiated), can populate its properties based on input
  • can output it's own properties, by saying like 'my name is Kitty'
  • can purr, and say 'I'm happy that's why I'm purring'
  • can curl, and say 'zzz...'

3. Cat breeder:

You started a cat breeder business.  You want to keep track of all your cats by using Classes in Python.  Yesterday a batch of new kitties arrived, together with a file describing them.  File name is kitties.txt, format is

            name     age    breed    colour
            name     age    breed    colour
            name     age    breed    colour
            name     age    breed    colour
            ...

Please read from the text file, use the Cat class created in the previous problem to capture all the information.  (*think: now you have more than one cat, you'll have more than one Cat class variables.  Where should you put all these variable?)  

Note that your kitties, i.e., variables of this Cat class, in addition to what they can already do in the previous problem, can also to the following:

  • Get into the front of a line
  • Get into the end of a line
Once you get them all in a line, please print their names in their current order.

Week 6 Learning Summary

Week 6 subject 1: Scope of variables

  • Variables have scopes, i.e., parts of the program where they can be referenced.
  • There are 2 types of variables based on their scopes:
    • Global variables: those defined outside any function, and
    • Local variables: those defined inside a function
  • Global variables are valid inside functions, but local variables aren't valid outside functions.
  • All variables must be first defined before referenced, within their respective scopes.
  • Functions are like global variables, they must be defined before being called.
  • Expand your knowledge by doing research of the concept Namespace.
Week 5 subject 1: Python is an OOPL

  • Traditionally programming languages are divided into 2 wide categories: procedural programming languages and object-oriented programming languages.  Earlier languages are typically procedural languages, including BASIC, C, Fortran and many others.  More recent languages are often OOPLs, among which there are C++, Java and Python.
  • Programming in procedural languages is like writing a cook book, which includes a series of steps that are executed either sequentially or according to defined control flows.
  • On the other hand OOPLs mimic the real world by defining 'objects' and how they interact with each other, so that we can stay reasonably far away from the tedious work of recreating all the 'steps' each time we need to describe the interactions, and let the 'objects' do it according to their definitions.

Procedural Programming and Object-Oriented Programming in C++ - Scaler  Topics

Friday, November 14, 2025

Week 4 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 3 and 4 Learning Summary

Week 3 and 4 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, October 25, 2025

Week 2 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.

Week 2 Learning Summary

Week 2 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()
  • 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.

Saturday, October 18, 2025

Week 1 Assignments

1. The more the merrier:

You are organising 2 parties.  At one point it suddenly occurs to you: if people come to parties to have fun, why hold two when you can have everybody in one.  There’s twice the fun and you don’t have to work twice as hard.  You need to merge the 2 guest lists.  They are both sorted alphabetically.  You need a program to merge them so that the combined list is still sorted alphabetically.

Input 2 lines, each containing a list of names sorted alphabetically, output a combined list of names sorted alphabetically.

2. Lonely librarian:

You are the only librarian in the school library.  You get sick of keeping record of new books and writing labels, you decide to create some automation.  You want to keep entering names of new books, until at one point you enter ‘end’so the automation stops.  For each book entered, it’s added to your record, unless the same name already exists in the record.  When the automation stops, it prints a list of all the books entered.

Input a series of book names, output the list of books entered.

Extension: 

People make mistakes, a lonely librarian makes more.  When you discover a mistake, instead of entering the name of the book, you enter ‘del’ followed by the name of the book incorrectly entered.  The book is then removed from your list.  

Input a number of book names and delete commands, output the final list of books.

3. 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 jamesbondscodebooktopsecretcantletanybodyseebecauseitsnotencrypted.txt.  The book contains many lines, each containing the plain text word followed by its code word.  Example:

    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.