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!