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.

No comments:

Post a Comment